2016-05-13 06:49:40 +03:00
|
|
|
// Copyright 2015 The etcd Authors
|
2015-01-25 06:19:16 +03:00
|
|
|
//
|
|
|
|
// 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 (
|
2017-09-07 00:57:25 +03:00
|
|
|
"context"
|
2014-10-07 03:42:13 +04:00
|
|
|
"fmt"
|
2017-03-16 05:31:10 +03:00
|
|
|
"path/filepath"
|
2014-11-11 00:45:29 +03:00
|
|
|
"sort"
|
2015-09-22 05:29:50 +03:00
|
|
|
"strings"
|
2015-08-12 07:09:03 +03:00
|
|
|
"time"
|
2014-10-07 03:42:13 +04:00
|
|
|
|
2015-08-21 19:41:13 +03:00
|
|
|
"github.com/coreos/etcd/pkg/netutil"
|
2015-10-11 09:04:10 +03:00
|
|
|
"github.com/coreos/etcd/pkg/transport"
|
2014-10-07 03:42:13 +04:00
|
|
|
"github.com/coreos/etcd/pkg/types"
|
|
|
|
)
|
|
|
|
|
|
|
|
// ServerConfig holds the configuration of etcd as taken from the command line or discovery.
|
|
|
|
type ServerConfig struct {
|
2015-09-01 01:01:18 +03:00
|
|
|
Name string
|
|
|
|
DiscoveryURL string
|
|
|
|
DiscoveryProxy string
|
|
|
|
ClientURLs types.URLs
|
|
|
|
PeerURLs types.URLs
|
|
|
|
DataDir string
|
|
|
|
// DedicatedWALDir config will make the etcd to write the WAL to the WALDir
|
|
|
|
// rather than the dataDir/member/wal.
|
|
|
|
DedicatedWALDir string
|
2015-04-27 05:49:01 +03:00
|
|
|
SnapCount uint64
|
|
|
|
MaxSnapFiles uint
|
|
|
|
MaxWALFiles uint
|
|
|
|
InitialPeerURLsMap types.URLsMap
|
|
|
|
InitialClusterToken string
|
|
|
|
NewCluster bool
|
2015-10-11 09:04:10 +03:00
|
|
|
PeerTLSInfo transport.TLSInfo
|
2015-01-07 21:46:57 +03:00
|
|
|
|
2018-03-27 03:10:41 +03:00
|
|
|
CORS map[string]struct{}
|
|
|
|
|
2018-03-01 01:03:03 +03:00
|
|
|
// HostWhitelist lists acceptable hostnames from client requests.
|
|
|
|
// If server is insecure (no TLS), server only accepts requests
|
|
|
|
// whose Host header value exists in this white list.
|
|
|
|
HostWhitelist map[string]struct{}
|
|
|
|
|
2016-02-03 01:31:50 +03:00
|
|
|
TickMs uint
|
|
|
|
ElectionTicks int
|
|
|
|
BootstrapTimeout time.Duration
|
2015-08-08 15:58:29 +03:00
|
|
|
|
2017-09-26 20:06:45 +03:00
|
|
|
AutoCompactionRetention time.Duration
|
2017-06-17 01:12:49 +03:00
|
|
|
AutoCompactionMode string
|
2016-03-30 03:55:18 +03:00
|
|
|
QuotaBackendBytes int64
|
2017-05-24 03:50:20 +03:00
|
|
|
MaxTxnOps uint
|
2015-09-10 09:45:34 +03:00
|
|
|
|
2017-05-23 02:54:50 +03:00
|
|
|
// MaxRequestBytes is the maximum request size to send over raft.
|
|
|
|
MaxRequestBytes uint
|
|
|
|
|
2015-09-10 09:45:34 +03:00
|
|
|
StrictReconfigCheck bool
|
2016-01-07 10:15:27 +03:00
|
|
|
|
2016-07-21 02:22:59 +03:00
|
|
|
// ClientCertAuthEnabled is true when cert has been signed by the client CA.
|
|
|
|
ClientCertAuthEnabled bool
|
2016-07-21 08:13:57 +03:00
|
|
|
|
|
|
|
AuthToken string
|
2017-08-16 23:28:55 +03:00
|
|
|
|
2017-11-22 00:17:59 +03:00
|
|
|
// InitialCorruptCheck is true to check data corruption on boot
|
|
|
|
// before serving any peer/client traffic.
|
|
|
|
InitialCorruptCheck bool
|
|
|
|
CorruptCheckTime time.Duration
|
2018-01-02 18:40:50 +03:00
|
|
|
|
2018-02-23 08:19:23 +03:00
|
|
|
// PreVote is true to enable Raft Pre-Vote.
|
|
|
|
PreVote bool
|
|
|
|
|
2018-01-02 18:40:50 +03:00
|
|
|
Debug bool
|
2018-03-23 15:25:20 +03:00
|
|
|
|
|
|
|
ForceNewCluster bool
|
2014-10-07 03:42:13 +04:00
|
|
|
}
|
|
|
|
|
2016-02-01 08:42:39 +03:00
|
|
|
// VerifyBootstrap sanity-checks the initial config for bootstrap case
|
2015-03-23 21:56:34 +03:00
|
|
|
// and returns an error for things that should never happen.
|
|
|
|
func (c *ServerConfig) VerifyBootstrap() error {
|
2016-12-15 01:21:41 +03:00
|
|
|
if err := c.hasLocalMember(); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
if err := c.advertiseMatchesCluster(); err != nil {
|
2015-03-23 21:56:34 +03:00
|
|
|
return err
|
|
|
|
}
|
2015-04-27 05:49:01 +03:00
|
|
|
if checkDuplicateURL(c.InitialPeerURLsMap) {
|
|
|
|
return fmt.Errorf("initial cluster %s has duplicate url", c.InitialPeerURLsMap)
|
2015-03-23 21:56:34 +03:00
|
|
|
}
|
2015-04-27 05:49:01 +03:00
|
|
|
if c.InitialPeerURLsMap.String() == "" && c.DiscoveryURL == "" {
|
2015-03-23 21:56:34 +03:00
|
|
|
return fmt.Errorf("initial cluster unset and no discovery URL found")
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// VerifyJoinExisting sanity-checks the initial config for join existing cluster
|
|
|
|
// case and returns an error for things that should never happen.
|
|
|
|
func (c *ServerConfig) VerifyJoinExisting() error {
|
2016-12-15 01:21:41 +03:00
|
|
|
// The member has announced its peer urls to the cluster before starting; no need to
|
|
|
|
// set the configuration again.
|
|
|
|
if err := c.hasLocalMember(); err != nil {
|
2015-03-23 21:56:34 +03:00
|
|
|
return err
|
|
|
|
}
|
2015-04-27 05:49:01 +03:00
|
|
|
if checkDuplicateURL(c.InitialPeerURLsMap) {
|
|
|
|
return fmt.Errorf("initial cluster %s has duplicate url", c.InitialPeerURLsMap)
|
2015-03-23 21:56:34 +03:00
|
|
|
}
|
|
|
|
if c.DiscoveryURL != "" {
|
|
|
|
return fmt.Errorf("discovery URL should not be set when joining existing initial cluster")
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2016-12-15 01:21:41 +03:00
|
|
|
// hasLocalMember checks that the cluster at least contains the local server.
|
|
|
|
func (c *ServerConfig) hasLocalMember() error {
|
|
|
|
if urls := c.InitialPeerURLsMap[c.Name]; urls == 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
|
|
|
}
|
2016-12-15 01:21:41 +03:00
|
|
|
return nil
|
|
|
|
}
|
2014-10-22 21:49:24 +04:00
|
|
|
|
2016-12-15 01:21:41 +03:00
|
|
|
// advertiseMatchesCluster confirms peer URLs match those in the cluster peer list.
|
|
|
|
func (c *ServerConfig) advertiseMatchesCluster() error {
|
|
|
|
urls, apurls := c.InitialPeerURLsMap[c.Name], c.PeerURLs.StringSlice()
|
|
|
|
urls.Sort()
|
|
|
|
sort.Strings(apurls)
|
|
|
|
ctx, cancel := context.WithTimeout(context.TODO(), 30*time.Second)
|
|
|
|
defer cancel()
|
2018-01-24 02:30:31 +03:00
|
|
|
ok, err := netutil.URLStringsEqual(ctx, apurls, urls.StringSlice())
|
|
|
|
if ok {
|
2017-06-12 23:36:32 +03:00
|
|
|
return nil
|
2014-11-11 00:45:29 +03:00
|
|
|
}
|
2017-06-12 23:36:32 +03:00
|
|
|
|
|
|
|
initMap, apMap := make(map[string]struct{}), make(map[string]struct{})
|
|
|
|
for _, url := range c.PeerURLs {
|
|
|
|
apMap[url.String()] = struct{}{}
|
|
|
|
}
|
|
|
|
for _, url := range c.InitialPeerURLsMap[c.Name] {
|
|
|
|
initMap[url.String()] = struct{}{}
|
|
|
|
}
|
|
|
|
|
|
|
|
missing := []string{}
|
|
|
|
for url := range initMap {
|
|
|
|
if _, ok := apMap[url]; !ok {
|
|
|
|
missing = append(missing, url)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if len(missing) > 0 {
|
|
|
|
for i := range missing {
|
|
|
|
missing[i] = c.Name + "=" + missing[i]
|
|
|
|
}
|
|
|
|
mstr := strings.Join(missing, ",")
|
|
|
|
apStr := strings.Join(apurls, ",")
|
2018-01-24 02:30:31 +03:00
|
|
|
return fmt.Errorf("--initial-cluster has %s but missing from --initial-advertise-peer-urls=%s (%v)", mstr, apStr, err)
|
2017-06-12 23:36:32 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
for url := range apMap {
|
|
|
|
if _, ok := initMap[url]; !ok {
|
|
|
|
missing = append(missing, url)
|
|
|
|
}
|
|
|
|
}
|
2018-01-24 02:30:31 +03:00
|
|
|
if len(missing) > 0 {
|
|
|
|
mstr := strings.Join(missing, ",")
|
|
|
|
umap := types.URLsMap(map[string]types.URLs{c.Name: c.PeerURLs})
|
|
|
|
return fmt.Errorf("--initial-advertise-peer-urls has %s but missing from --initial-cluster=%s", mstr, umap.String())
|
|
|
|
}
|
|
|
|
|
|
|
|
// resolved URLs from "--initial-advertise-peer-urls" and "--initial-cluster" did not match or failed
|
|
|
|
apStr := strings.Join(apurls, ",")
|
2017-06-12 23:36:32 +03:00
|
|
|
umap := types.URLsMap(map[string]types.URLs{c.Name: c.PeerURLs})
|
2018-01-24 02:30:31 +03:00
|
|
|
return fmt.Errorf("failed to resolve %s to match --initial-cluster=%s (%v)", apStr, umap.String(), err)
|
2014-10-07 03:42:13 +04:00
|
|
|
}
|
2014-10-11 17:03:03 +04:00
|
|
|
|
2017-03-16 05:31:10 +03:00
|
|
|
func (c *ServerConfig) MemberDir() string { return filepath.Join(c.DataDir, "member") }
|
2014-10-11 17:03:03 +04:00
|
|
|
|
2015-09-01 01:01:18 +03:00
|
|
|
func (c *ServerConfig) WALDir() string {
|
|
|
|
if c.DedicatedWALDir != "" {
|
|
|
|
return c.DedicatedWALDir
|
|
|
|
}
|
2017-03-16 05:31:10 +03:00
|
|
|
return filepath.Join(c.MemberDir(), "wal")
|
2015-09-01 01:01:18 +03:00
|
|
|
}
|
2015-02-12 20:45:12 +03:00
|
|
|
|
2017-03-16 05:31:10 +03:00
|
|
|
func (c *ServerConfig) SnapDir() string { return filepath.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
|
|
|
|
2015-08-12 07:22:05 +03:00
|
|
|
// ReqTimeout returns timeout for request to finish.
|
|
|
|
func (c *ServerConfig) ReqTimeout() time.Duration {
|
2015-08-12 07:09:03 +03:00
|
|
|
// 5s for queue waiting, computation and disk IO delay
|
2015-08-18 00:54:25 +03:00
|
|
|
// + 2 * election timeout for possible leader election
|
2017-09-23 05:35:41 +03:00
|
|
|
return 5*time.Second + 2*time.Duration(c.ElectionTicks*int(c.TickMs))*time.Millisecond
|
2015-08-12 07:09:03 +03:00
|
|
|
}
|
|
|
|
|
2016-03-14 06:19:47 +03:00
|
|
|
func (c *ServerConfig) electionTimeout() time.Duration {
|
2017-09-23 05:35:41 +03:00
|
|
|
return time.Duration(c.ElectionTicks*int(c.TickMs)) * time.Millisecond
|
2016-03-14 06:19:47 +03:00
|
|
|
}
|
|
|
|
|
2015-10-11 09:04:10 +03:00
|
|
|
func (c *ServerConfig) peerDialTimeout() time.Duration {
|
2017-09-23 00:55:52 +03:00
|
|
|
// 1s for queue wait and election timeout
|
2017-09-23 05:35:41 +03:00
|
|
|
return time.Second + time.Duration(c.ElectionTicks*int(c.TickMs))*time.Millisecond
|
2015-10-11 09:04:10 +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) {
|
2015-06-08 23:28:34 +03:00
|
|
|
plog.Infof("name = %s", c.Name)
|
2014-11-13 07:11:04 +03:00
|
|
|
if c.ForceNewCluster {
|
2015-06-08 23:28:34 +03:00
|
|
|
plog.Infof("force new cluster")
|
2014-11-13 07:11:04 +03:00
|
|
|
}
|
2015-06-08 23:28:34 +03:00
|
|
|
plog.Infof("data dir = %s", c.DataDir)
|
|
|
|
plog.Infof("member dir = %s", c.MemberDir())
|
2015-09-01 01:01:18 +03:00
|
|
|
if c.DedicatedWALDir != "" {
|
|
|
|
plog.Infof("dedicated WAL dir = %s", c.DedicatedWALDir)
|
|
|
|
}
|
2015-06-08 23:28:34 +03:00
|
|
|
plog.Infof("heartbeat = %dms", c.TickMs)
|
|
|
|
plog.Infof("election = %dms", c.ElectionTicks*int(c.TickMs))
|
|
|
|
plog.Infof("snapshot count = %d", c.SnapCount)
|
2014-11-13 07:11:04 +03:00
|
|
|
if len(c.DiscoveryURL) != 0 {
|
2015-06-08 23:28:34 +03:00
|
|
|
plog.Infof("discovery URL= %s", c.DiscoveryURL)
|
2014-11-13 07:11:04 +03:00
|
|
|
if len(c.DiscoveryProxy) != 0 {
|
2015-06-08 23:28:34 +03:00
|
|
|
plog.Infof("discovery proxy = %s", c.DiscoveryProxy)
|
2014-11-13 07:11:04 +03:00
|
|
|
}
|
|
|
|
}
|
2015-06-08 23:28:34 +03:00
|
|
|
plog.Infof("advertise client URLs = %s", c.ClientURLs)
|
2014-11-13 07:11:04 +03:00
|
|
|
if initial {
|
2015-06-08 23:28:34 +03:00
|
|
|
plog.Infof("initial advertise peer URLs = %s", c.PeerURLs)
|
|
|
|
plog.Infof("initial cluster = %s", c.InitialPeerURLsMap)
|
2015-04-27 05:49:01 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func checkDuplicateURL(urlsmap types.URLsMap) bool {
|
|
|
|
um := make(map[string]bool)
|
|
|
|
for _, urls := range urlsmap {
|
|
|
|
for _, url := range urls {
|
|
|
|
u := url.String()
|
|
|
|
if um[u] {
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
um[u] = true
|
|
|
|
}
|
2014-11-13 07:11:04 +03:00
|
|
|
}
|
2015-04-27 05:49:01 +03:00
|
|
|
return false
|
2014-11-13 07:11:04 +03:00
|
|
|
}
|
2016-02-03 01:31:50 +03:00
|
|
|
|
|
|
|
func (c *ServerConfig) bootstrapTimeout() time.Duration {
|
|
|
|
if c.BootstrapTimeout != 0 {
|
|
|
|
return c.BootstrapTimeout
|
|
|
|
}
|
|
|
|
return time.Second
|
|
|
|
}
|
2017-05-11 21:13:45 +03:00
|
|
|
|
|
|
|
func (c *ServerConfig) backendPath() string { return filepath.Join(c.SnapDir(), "db") }
|