2015-01-25 06:19:16 +03:00
|
|
|
// Copyright 2015 CoreOS, Inc.
|
|
|
|
//
|
|
|
|
// Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
// you may not use this file except in compliance with the License.
|
|
|
|
// You may obtain a copy of the License at
|
|
|
|
//
|
|
|
|
// http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
//
|
|
|
|
// Unless required by applicable law or agreed to in writing, software
|
|
|
|
// distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
// See the License for the specific language governing permissions and
|
|
|
|
// limitations under the License.
|
2014-10-18 02:41:22 +04:00
|
|
|
|
2014-10-07 03:42:13 +04:00
|
|
|
package etcdserver
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
2014-11-13 07:11:04 +03:00
|
|
|
"log"
|
2014-10-07 03:42:13 +04:00
|
|
|
"net/http"
|
2014-10-11 17:03:03 +04:00
|
|
|
"path"
|
2014-11-11 00:45:29 +03:00
|
|
|
"sort"
|
2014-10-07 03:42:13 +04:00
|
|
|
|
2015-01-26 10:26:21 +03:00
|
|
|
"github.com/coreos/etcd/pkg/netutil"
|
2014-10-07 03:42:13 +04:00
|
|
|
"github.com/coreos/etcd/pkg/types"
|
2014-10-18 02:37:30 +04:00
|
|
|
"github.com/coreos/etcd/raft"
|
2014-10-07 03:42:13 +04:00
|
|
|
)
|
|
|
|
|
|
|
|
// ServerConfig holds the configuration of etcd as taken from the command line or discovery.
|
|
|
|
type ServerConfig struct {
|
2014-11-07 08:50:35 +03:00
|
|
|
Name string
|
|
|
|
DiscoveryURL string
|
|
|
|
DiscoveryProxy string
|
|
|
|
ClientURLs types.URLs
|
2014-11-11 00:45:29 +03:00
|
|
|
PeerURLs types.URLs
|
2014-11-07 08:50:35 +03:00
|
|
|
DataDir string
|
|
|
|
SnapCount uint64
|
2014-11-29 08:32:28 +03:00
|
|
|
MaxSnapFiles uint
|
|
|
|
MaxWALFiles uint
|
2014-11-07 08:50:35 +03:00
|
|
|
Cluster *Cluster
|
|
|
|
NewCluster bool
|
|
|
|
ForceNewCluster bool
|
2014-11-07 21:31:19 +03:00
|
|
|
Transport *http.Transport
|
2015-01-07 21:46:57 +03:00
|
|
|
|
2015-01-15 22:11:33 +03:00
|
|
|
TickMs uint
|
|
|
|
ElectionTicks int
|
2014-10-07 03:42:13 +04:00
|
|
|
}
|
|
|
|
|
2014-10-18 02:43:56 +04:00
|
|
|
// VerifyBootstrapConfig sanity-checks the initial config and returns an error
|
|
|
|
// for things that should never happen.
|
|
|
|
func (c *ServerConfig) VerifyBootstrapConfig() error {
|
2014-10-23 22:08:42 +04:00
|
|
|
m := c.Cluster.MemberByName(c.Name)
|
2014-10-23 01:31:27 +04:00
|
|
|
// Make sure the cluster at least contains the local server.
|
|
|
|
if m == nil {
|
2014-11-10 23:55:35 +03:00
|
|
|
return fmt.Errorf("couldn't find local name %q in the initial cluster configuration", c.Name)
|
2014-10-23 01:31:27 +04:00
|
|
|
}
|
2014-10-31 03:31:14 +03:00
|
|
|
if uint64(m.ID) == raft.None {
|
2014-10-24 09:39:53 +04:00
|
|
|
return fmt.Errorf("cannot use %x as member id", raft.None)
|
2014-10-22 21:49:24 +04:00
|
|
|
}
|
|
|
|
|
2014-11-06 22:09:33 +03:00
|
|
|
if c.DiscoveryURL == "" && !c.NewCluster {
|
2014-10-18 02:43:56 +04:00
|
|
|
return fmt.Errorf("initial cluster state unset and no wal or discovery URL found")
|
|
|
|
}
|
|
|
|
|
2015-02-14 01:32:24 +03:00
|
|
|
if err := c.Cluster.Validate(); err != nil {
|
|
|
|
return err
|
2014-10-07 03:42:13 +04:00
|
|
|
}
|
2014-11-11 00:45:29 +03:00
|
|
|
|
|
|
|
// Advertised peer URLs must match those in the cluster peer list
|
2015-01-26 10:26:21 +03:00
|
|
|
// TODO: Remove URLStringsEqual after improvement of using hostnames #2150 #2123
|
2014-11-11 00:45:29 +03:00
|
|
|
apurls := c.PeerURLs.StringSlice()
|
|
|
|
sort.Strings(apurls)
|
2015-01-26 10:26:21 +03:00
|
|
|
if !netutil.URLStringsEqual(apurls, m.PeerURLs) {
|
2014-11-11 00:45:29 +03:00
|
|
|
return fmt.Errorf("%s has different advertised URLs in the cluster and advertised peer URLs list", c.Name)
|
|
|
|
}
|
2014-10-07 03:42:13 +04:00
|
|
|
return nil
|
|
|
|
}
|
2014-10-11 17:03:03 +04:00
|
|
|
|
2015-02-12 20:45:12 +03:00
|
|
|
func (c *ServerConfig) MemberDir() string { return path.Join(c.DataDir, "member") }
|
2014-10-11 17:03:03 +04:00
|
|
|
|
2015-02-12 20:45:12 +03:00
|
|
|
func (c *ServerConfig) WALDir() string { return path.Join(c.MemberDir(), "wal") }
|
|
|
|
|
|
|
|
func (c *ServerConfig) SnapDir() string { return path.Join(c.MemberDir(), "snap") }
|
2014-10-11 17:03:03 +04:00
|
|
|
|
2014-12-12 09:17:36 +03:00
|
|
|
func (c *ServerConfig) ShouldDiscover() bool { return c.DiscoveryURL != "" }
|
2014-11-13 07:11:04 +03:00
|
|
|
|
2014-12-12 09:17:36 +03:00
|
|
|
func (c *ServerConfig) PrintWithInitial() { c.print(true) }
|
2014-11-13 07:11:04 +03:00
|
|
|
|
2014-12-12 09:17:36 +03:00
|
|
|
func (c *ServerConfig) Print() { c.print(false) }
|
2014-11-13 07:11:04 +03:00
|
|
|
|
|
|
|
func (c *ServerConfig) print(initial bool) {
|
|
|
|
log.Printf("etcdserver: name = %s", c.Name)
|
|
|
|
if c.ForceNewCluster {
|
|
|
|
log.Println("etcdserver: force new cluster")
|
|
|
|
}
|
|
|
|
log.Printf("etcdserver: data dir = %s", c.DataDir)
|
2015-02-12 20:45:12 +03:00
|
|
|
log.Printf("etcdserver: member dir = %s", c.MemberDir())
|
2015-01-15 22:11:33 +03:00
|
|
|
log.Printf("etcdserver: heartbeat = %dms", c.TickMs)
|
|
|
|
log.Printf("etcdserver: election = %dms", c.ElectionTicks*int(c.TickMs))
|
2014-11-13 07:11:04 +03:00
|
|
|
log.Printf("etcdserver: snapshot count = %d", c.SnapCount)
|
|
|
|
if len(c.DiscoveryURL) != 0 {
|
|
|
|
log.Printf("etcdserver: discovery URL= %s", c.DiscoveryURL)
|
|
|
|
if len(c.DiscoveryProxy) != 0 {
|
|
|
|
log.Printf("etcdserver: discovery proxy = %s", c.DiscoveryProxy)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
log.Printf("etcdserver: advertise client URLs = %s", c.ClientURLs)
|
|
|
|
if initial {
|
|
|
|
log.Printf("etcdserver: initial advertise peer URLs = %s", c.PeerURLs)
|
|
|
|
log.Printf("etcdserver: initial cluster = %s", c.Cluster)
|
|
|
|
}
|
|
|
|
}
|