2013-06-09 21:42:34 +04:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
2014-09-03 00:48:38 +04:00
|
|
|
"flag"
|
2014-09-05 08:15:39 +04:00
|
|
|
"fmt"
|
2014-07-06 21:19:23 +04:00
|
|
|
"log"
|
|
|
|
"net/http"
|
2014-09-05 08:15:39 +04:00
|
|
|
"os"
|
|
|
|
"path"
|
2014-09-03 01:46:07 +04:00
|
|
|
"strconv"
|
|
|
|
"time"
|
2014-01-18 22:09:54 +04:00
|
|
|
|
2014-09-03 08:36:14 +04:00
|
|
|
"github.com/coreos/etcd/etcdserver"
|
|
|
|
"github.com/coreos/etcd/etcdserver/etcdhttp"
|
2014-09-03 00:48:38 +04:00
|
|
|
"github.com/coreos/etcd/raft"
|
2014-09-03 02:04:57 +04:00
|
|
|
"github.com/coreos/etcd/store"
|
2014-09-05 08:15:39 +04:00
|
|
|
"github.com/coreos/etcd/wal"
|
2014-07-06 21:19:23 +04:00
|
|
|
)
|
|
|
|
|
2014-09-09 02:45:58 +04:00
|
|
|
const (
|
|
|
|
// the owner can make/remove files inside the directory
|
|
|
|
privateDirMode = 0700
|
2014-07-06 21:19:23 +04:00
|
|
|
)
|
|
|
|
|
2014-09-03 00:48:38 +04:00
|
|
|
var (
|
2014-09-05 09:26:42 +04:00
|
|
|
fid = flag.String("id", "0x1", "Id of this server")
|
2014-09-03 01:46:07 +04:00
|
|
|
timeout = flag.Duration("timeout", 10*time.Second, "Request Timeout")
|
|
|
|
laddr = flag.String("l", ":8080", "HTTP service address (e.g., ':8080')")
|
2014-09-05 20:56:25 +04:00
|
|
|
dir = flag.String("data-dir", "", "Path to the data directory")
|
2014-09-05 22:59:23 +04:00
|
|
|
|
|
|
|
peers = &etcdhttp.Peers{}
|
2014-09-03 00:48:38 +04:00
|
|
|
)
|
2014-07-06 21:19:23 +04:00
|
|
|
|
2014-09-03 01:46:07 +04:00
|
|
|
func init() {
|
2014-09-05 09:26:42 +04:00
|
|
|
peers.Set("0x1=localhost:8080")
|
2014-09-03 01:46:07 +04:00
|
|
|
flag.Var(peers, "peers", "your peers")
|
|
|
|
}
|
|
|
|
|
2014-09-03 00:48:38 +04:00
|
|
|
func main() {
|
2014-09-03 02:04:57 +04:00
|
|
|
flag.Parse()
|
|
|
|
|
|
|
|
id, err := strconv.ParseInt(*fid, 0, 64)
|
2014-09-03 01:46:07 +04:00
|
|
|
if err != nil {
|
|
|
|
log.Fatal(err)
|
|
|
|
}
|
2014-09-03 00:48:38 +04:00
|
|
|
|
2014-09-03 02:15:10 +04:00
|
|
|
if peers.Pick(id) == "" {
|
2014-09-03 07:13:18 +04:00
|
|
|
log.Fatalf("%#x=<addr> must be specified in peers", id)
|
2014-09-03 02:15:10 +04:00
|
|
|
}
|
|
|
|
|
2014-09-05 08:15:39 +04:00
|
|
|
if *dir == "" {
|
2014-09-09 03:15:18 +04:00
|
|
|
*dir = fmt.Sprintf("%v_etcd_data", *fid)
|
2014-09-08 21:32:49 +04:00
|
|
|
log.Printf("main: no data-dir is given, using default data-dir ./%s", *dir)
|
2014-09-05 08:15:39 +04:00
|
|
|
}
|
2014-09-09 02:45:58 +04:00
|
|
|
if err := os.MkdirAll(*dir, privateDirMode); err != nil {
|
2014-09-05 21:02:46 +04:00
|
|
|
log.Fatalf("main: cannot create data directory: %v", err)
|
2014-09-05 08:15:39 +04:00
|
|
|
}
|
|
|
|
|
2014-09-05 21:16:55 +04:00
|
|
|
n, w := startRaft(id, peers.Ids(), path.Join(*dir, "wal"))
|
2014-09-03 02:57:15 +04:00
|
|
|
|
2014-09-03 04:05:03 +04:00
|
|
|
tk := time.NewTicker(100 * time.Millisecond)
|
2014-09-03 00:48:38 +04:00
|
|
|
s := &etcdserver.Server{
|
2014-09-03 04:05:03 +04:00
|
|
|
Store: store.New(),
|
|
|
|
Node: n,
|
2014-09-05 08:15:39 +04:00
|
|
|
Save: w.Save,
|
2014-09-05 22:59:23 +04:00
|
|
|
Send: etcdhttp.Sender(*peers),
|
2014-09-03 04:05:03 +04:00
|
|
|
Ticker: tk.C,
|
2014-07-09 01:07:25 +04:00
|
|
|
}
|
2014-09-03 00:48:38 +04:00
|
|
|
etcdserver.Start(s)
|
|
|
|
h := &etcdhttp.Handler{
|
2014-09-03 01:46:07 +04:00
|
|
|
Timeout: *timeout,
|
2014-09-03 00:48:38 +04:00
|
|
|
Server: s,
|
2014-09-09 03:34:29 +04:00
|
|
|
Peers: peers,
|
2014-07-09 01:07:25 +04:00
|
|
|
}
|
2014-09-03 01:46:07 +04:00
|
|
|
http.Handle("/", h)
|
2014-09-03 00:48:38 +04:00
|
|
|
log.Fatal(http.ListenAndServe(*laddr, nil))
|
2014-07-09 01:07:25 +04:00
|
|
|
}
|
2014-09-05 21:16:55 +04:00
|
|
|
|
|
|
|
// startRaft starts a raft node from the given wal dir.
|
|
|
|
// If the wal dir does not exist, startRaft will start a new raft node.
|
|
|
|
// If the wal dir exists, startRaft will restart the previous raft node.
|
|
|
|
// startRaft returns the started raft node and the opened wal.
|
|
|
|
func startRaft(id int64, peerIds []int64, waldir string) (raft.Node, *wal.WAL) {
|
|
|
|
if !wal.Exist(waldir) {
|
|
|
|
w, err := wal.Create(waldir)
|
|
|
|
if err != nil {
|
|
|
|
log.Fatal(err)
|
|
|
|
}
|
|
|
|
n := raft.Start(id, peerIds, 10, 1)
|
|
|
|
return n, w
|
|
|
|
}
|
|
|
|
|
|
|
|
// restart a node from previous wal
|
|
|
|
// TODO(xiangli): check snapshot; not open from zero
|
|
|
|
w, err := wal.OpenFromIndex(waldir, 0)
|
|
|
|
if err != nil {
|
|
|
|
log.Fatal(err)
|
|
|
|
}
|
2014-09-09 02:31:11 +04:00
|
|
|
wid, st, ents, err := w.ReadAll()
|
2014-09-05 21:16:55 +04:00
|
|
|
// TODO(xiangli): save/recovery nodeID?
|
2014-09-09 02:31:11 +04:00
|
|
|
if wid != 0 {
|
2014-09-09 03:21:27 +04:00
|
|
|
log.Fatalf("unexpected nodeid %d: nodeid should always be zero until we save nodeid into wal", wid)
|
2014-09-09 02:31:11 +04:00
|
|
|
}
|
2014-09-05 21:16:55 +04:00
|
|
|
if err != nil {
|
|
|
|
log.Fatal(err)
|
|
|
|
}
|
|
|
|
n := raft.Restart(id, peerIds, 10, 1, st, ents)
|
|
|
|
return n, w
|
|
|
|
}
|