etcd/main.go

280 lines
7.5 KiB
Go
Raw Normal View History

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"
"net/url"
2014-09-05 08:15:39 +04:00
"os"
"path"
"strings"
2014-09-03 01:46:07 +04:00
"time"
2014-09-03 08:36:14 +04:00
"github.com/coreos/etcd/etcdserver"
"github.com/coreos/etcd/etcdserver/etcdhttp"
"github.com/coreos/etcd/pkg"
flagtypes "github.com/coreos/etcd/pkg/flags"
2014-09-25 21:47:14 +04:00
"github.com/coreos/etcd/pkg/transport"
"github.com/coreos/etcd/proxy"
2014-09-03 00:48:38 +04:00
"github.com/coreos/etcd/raft"
2014-09-17 05:18:45 +04:00
"github.com/coreos/etcd/snap"
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
)
const (
// the owner can make/remove files inside the directory
privateDirMode = 0700
2014-09-18 18:50:52 +04:00
2014-09-25 04:08:42 +04:00
version = "0.5.0-alpha"
2014-07-06 21:19:23 +04:00
)
2014-09-03 00:48:38 +04:00
var (
name = flag.String("name", "default", "Unique human-readable name for this node")
2014-09-25 04:08:42 +04:00
timeout = flag.Duration("timeout", 10*time.Second, "Request Timeout")
dir = flag.String("data-dir", "", "Path to the data directory")
snapCount = flag.Int64("snapshot-count", etcdserver.DefaultSnapCount, "Number of committed transactions to trigger a snapshot")
printVersion = flag.Bool("version", false, "Print the version and exit")
cluster = &etcdserver.Cluster{}
lcurls = &flagtypes.URLs{}
acurls = &flagtypes.URLs{}
lpurls = &flagtypes.URLs{}
apurls = &flagtypes.URLs{}
2014-09-25 22:43:24 +04:00
cors = &pkg.CORSInfo{}
proxyFlag = new(flagtypes.Proxy)
2014-09-23 03:50:26 +04:00
clientTLSInfo = transport.TLSInfo{}
2014-09-23 19:21:16 +04:00
peerTLSInfo = transport.TLSInfo{}
2014-09-24 22:28:41 +04:00
deprecated = []string{
2014-09-25 03:49:11 +04:00
"addr",
"cluster-active-size",
"cluster-remove-delay",
"cluster-sync-interval",
2014-09-24 22:28:41 +04:00
"config",
2014-09-25 03:49:11 +04:00
"force",
2014-09-24 22:28:41 +04:00
"max-result-buffer",
"max-retry-attempts",
2014-09-25 03:49:11 +04:00
"peer-addr",
"peer-heartbeat-interval",
"peer-election-timeout",
2014-09-24 22:28:41 +04:00
"retry-interval",
"snapshot",
2014-09-25 03:44:32 +04:00
"v",
"vv",
2014-09-24 22:28:41 +04:00
}
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() {
flag.Var(cluster, "bootstrap-config", "Initial cluster configuration for bootstrapping")
flag.Var(apurls, "advertise-peer-urls", "List of this member's peer URLs to advertise to the rest of the cluster")
flag.Var(acurls, "advertise-client-urls", "List of this member's client URLs to advertise to the rest of the cluster")
flag.Var(lpurls, "listen-peer-urls", "List of this URLs to listen on for peer traffic")
flag.Var(lcurls, "listen-client-urls", "List of this URLs to listen on for client traffic")
2014-09-20 01:58:00 +04:00
flag.Var(cors, "cors", "Comma-separated white list of origins for CORS (cross-origin resource sharing).")
flag.Var(proxyFlag, "proxy", fmt.Sprintf("Valid values include %s", strings.Join(flagtypes.ProxyValues, ", ")))
cluster.Set("default=http://localhost:2380,default=http://localhost:7001")
lcurls.Set("http://localhost:2379,http://localhost:4001")
acurls.Set("http://localhost:2379,http://localhost:4001")
lpurls.Set("http://localhost:2380,http://localhost:7001")
apurls.Set("http://localhost:2380,http://localhost:7001")
proxyFlag.Set(flagtypes.ProxyValueOff)
2014-09-23 03:50:26 +04:00
flag.StringVar(&clientTLSInfo.CAFile, "ca-file", "", "Path to the client server TLS CA file.")
flag.StringVar(&clientTLSInfo.CertFile, "cert-file", "", "Path to the client server TLS cert file.")
flag.StringVar(&clientTLSInfo.KeyFile, "key-file", "", "Path to the client server TLS key file.")
2014-09-23 19:21:16 +04:00
flag.StringVar(&peerTLSInfo.CAFile, "peer-ca-file", "", "Path to the peer server TLS CA file.")
flag.StringVar(&peerTLSInfo.CertFile, "peer-cert-file", "", "Path to the peer server TLS cert file.")
flag.StringVar(&peerTLSInfo.KeyFile, "peer-key-file", "", "Path to the peer server TLS key file.")
2014-09-24 22:28:41 +04:00
for _, f := range deprecated {
flag.Var(&pkg.DeprecatedFlag{f}, f, "")
2014-09-24 22:28:41 +04:00
}
2014-09-03 01:46:07 +04:00
}
2014-09-03 00:48:38 +04:00
func main() {
flag.Usage = pkg.UsageWithIgnoredFlagsFunc(flag.CommandLine, deprecated)
2014-09-03 02:04:57 +04:00
flag.Parse()
2014-09-25 04:08:42 +04:00
if *printVersion {
fmt.Println("etcd version", version)
os.Exit(0)
}
pkg.SetFlagsFromEnv(flag.CommandLine)
2014-09-17 23:05:36 +04:00
if string(*proxyFlag) == flagtypes.ProxyValueOff {
startEtcd()
2014-09-18 18:50:52 +04:00
} else {
startProxy()
}
// Block indefinitely
<-make(chan struct{})
2014-09-10 22:06:31 +04:00
}
// startEtcd launches the etcd server and HTTP handlers for client/server communication.
func startEtcd() {
self := cluster.FindName(*name)
if self == nil {
log.Fatalf("etcd: no member with name=%q exists", *name)
2014-09-15 09:44:59 +04:00
}
2014-09-03 00:48:38 +04:00
if self.ID == raft.None {
log.Fatalf("etcd: cannot use None(%d) as member id", raft.None)
2014-09-03 02:15:10 +04:00
}
2014-09-17 05:18:45 +04:00
if *snapCount <= 0 {
log.Fatalf("etcd: snapshot-count must be greater than 0: snapshot-count=%d", *snapCount)
}
2014-09-05 08:15:39 +04:00
if *dir == "" {
*dir = fmt.Sprintf("%v_etcd_data", self.ID)
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
}
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-17 05:18:45 +04:00
snapdir := path.Join(*dir, "snap")
if err := os.MkdirAll(snapdir, privateDirMode); err != nil {
log.Fatalf("etcd: cannot create snapshot directory: %v", err)
}
snapshotter := snap.New(snapdir)
2014-09-05 08:15:39 +04:00
2014-09-17 05:18:45 +04:00
waldir := path.Join(*dir, "wal")
var w *wal.WAL
var n raft.Node
var err error
2014-09-17 05:18:45 +04:00
st := store.New()
if !wal.Exist(waldir) {
w, err = wal.Create(waldir)
if err != nil {
log.Fatal(err)
}
n = raft.StartNode(self.ID, cluster.IDs(), 10, 1)
2014-09-17 05:18:45 +04:00
} else {
var index int64
snapshot, err := snapshotter.Load()
if err != nil && err != snap.ErrNoSnapshot {
log.Fatal(err)
}
if snapshot != nil {
log.Printf("etcd: restart from snapshot at index %d", snapshot.Index)
st.Recovery(snapshot.Data)
index = snapshot.Index
}
// restart a node from previous wal
if w, err = wal.OpenAtIndex(waldir, index); err != nil {
log.Fatal(err)
}
wid, st, ents, err := w.ReadAll()
if err != nil {
log.Fatal(err)
}
// TODO(xiangli): save/recovery nodeID?
if wid != 0 {
log.Fatalf("unexpected nodeid %d: nodeid should always be zero until we save nodeid into wal", wid)
}
n = raft.RestartNode(self.ID, cluster.IDs(), 10, 1, snapshot, st, ents)
2014-09-17 05:18:45 +04:00
}
2014-09-03 02:57:15 +04:00
2014-09-23 19:21:16 +04:00
pt, err := transport.NewTransport(peerTLSInfo)
if err != nil {
log.Fatal(err)
}
cls := etcdserver.NewClusterStore(st, *cluster)
s := &etcdserver.EtcdServer{
2014-09-17 05:18:45 +04:00
Store: st,
Node: n,
Storage: struct {
*wal.WAL
*snap.Snapshotter
}{w, snapshotter},
Send: etcdserver.Sender(pt, cls),
Ticker: time.Tick(100 * time.Millisecond),
SyncTicker: time.Tick(500 * time.Millisecond),
SnapCount: *snapCount,
ClusterStore: cls,
2014-07-09 01:07:25 +04:00
}
s.Start()
2014-09-25 22:43:24 +04:00
ch := &pkg.CORSHandler{
Handler: etcdhttp.NewClientHandler(s, cls, *timeout),
2014-09-20 01:58:00 +04:00
Info: cors,
}
ph := etcdhttp.NewPeerHandler(s)
for _, u := range []url.URL(*lpurls) {
l, err := transport.NewListener(u.Host, peerTLSInfo)
if err != nil {
log.Fatal(err)
}
2014-09-23 03:35:00 +04:00
// Start the peer server in a goroutine
urlStr := u.String()
go func() {
log.Print("Listening for peers on ", urlStr)
log.Fatal(http.Serve(l, ph))
}()
}
// Start a client server goroutine for each listen address
for _, u := range []url.URL(*lcurls) {
l, err := transport.NewListener(u.Host, clientTLSInfo)
2014-09-23 03:35:00 +04:00
if err != nil {
log.Fatal(err)
}
urlStr := u.String()
go func() {
log.Print("Listening for client requests on ", urlStr)
2014-09-23 03:35:00 +04:00
log.Fatal(http.Serve(l, ch))
}()
}
2014-07-09 01:07:25 +04:00
}
// startProxy launches an HTTP proxy for client communication which proxies to other etcd nodes.
func startProxy() {
2014-09-23 21:19:01 +04:00
pt, err := transport.NewTransport(clientTLSInfo)
if err != nil {
log.Fatal(err)
}
2014-09-23 21:19:01 +04:00
ph, err := proxy.NewHandler(pt, (*cluster).PeerURLs())
2014-09-23 21:19:01 +04:00
if err != nil {
log.Fatal(err)
}
2014-09-25 22:43:24 +04:00
ph = &pkg.CORSHandler{
2014-09-20 01:58:00 +04:00
Handler: ph,
Info: cors,
}
2014-09-18 21:34:16 +04:00
if string(*proxyFlag) == flagtypes.ProxyValueReadonly {
2014-09-18 21:34:16 +04:00
ph = proxy.NewReadonlyHandler(ph)
}
// Start a proxy server goroutine for each listen address
for _, u := range []url.URL(*lcurls) {
l, err := transport.NewListener(u.Host, clientTLSInfo)
2014-09-23 03:35:00 +04:00
if err != nil {
log.Fatal(err)
}
host := u.Host
go func() {
log.Print("Listening for client requests on ", host)
2014-09-23 03:35:00 +04:00
log.Fatal(http.Serve(l, ph))
}()
}
}