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-08-26 05:39:02 +04:00
|
|
|
package etcdserver
|
|
|
|
|
|
|
|
import (
|
2017-09-07 00:57:25 +03:00
|
|
|
"context"
|
2014-09-27 01:18:59 +04:00
|
|
|
"encoding/json"
|
2015-04-04 00:26:15 +03:00
|
|
|
"expvar"
|
2014-10-25 02:26:05 +04:00
|
|
|
"fmt"
|
2016-08-02 20:29:23 +03:00
|
|
|
"math"
|
2014-09-18 01:38:10 +04:00
|
|
|
"math/rand"
|
2014-10-24 03:41:58 +04:00
|
|
|
"net/http"
|
2015-08-08 15:58:29 +03:00
|
|
|
"os"
|
2014-10-21 22:47:03 +04:00
|
|
|
"path"
|
2014-10-24 12:13:47 +04:00
|
|
|
"regexp"
|
2016-03-10 08:37:10 +03:00
|
|
|
"sync"
|
2014-09-25 05:05:33 +04:00
|
|
|
"sync/atomic"
|
2014-08-27 03:17:02 +04:00
|
|
|
"time"
|
2014-08-27 02:42:10 +04:00
|
|
|
|
2018-02-26 21:33:13 +03:00
|
|
|
"github.com/coreos/etcd/alarm"
|
2018-02-26 21:38:30 +03:00
|
|
|
"github.com/coreos/etcd/auth"
|
2018-02-26 21:32:24 +03:00
|
|
|
"github.com/coreos/etcd/compactor"
|
2018-02-26 21:34:15 +03:00
|
|
|
"github.com/coreos/etcd/discovery"
|
2016-07-26 21:33:11 +03:00
|
|
|
"github.com/coreos/etcd/etcdserver/api"
|
2016-04-05 02:31:35 +03:00
|
|
|
"github.com/coreos/etcd/etcdserver/api/v2http/httptypes"
|
2014-09-03 08:36:14 +04:00
|
|
|
pb "github.com/coreos/etcd/etcdserver/etcdserverpb"
|
2016-04-07 22:02:37 +03:00
|
|
|
"github.com/coreos/etcd/etcdserver/membership"
|
2014-10-11 02:53:28 +04:00
|
|
|
"github.com/coreos/etcd/etcdserver/stats"
|
2018-02-26 22:06:38 +03:00
|
|
|
"github.com/coreos/etcd/etcdserver/v2store"
|
2018-02-26 21:41:45 +03:00
|
|
|
"github.com/coreos/etcd/lease"
|
|
|
|
"github.com/coreos/etcd/lease/leasehttp"
|
2018-02-26 22:12:45 +03:00
|
|
|
"github.com/coreos/etcd/mvcc"
|
|
|
|
"github.com/coreos/etcd/mvcc/backend"
|
2014-11-29 08:32:28 +03:00
|
|
|
"github.com/coreos/etcd/pkg/fileutil"
|
2015-01-13 22:19:27 +03:00
|
|
|
"github.com/coreos/etcd/pkg/idutil"
|
2014-10-11 16:41:00 +04:00
|
|
|
"github.com/coreos/etcd/pkg/pbutil"
|
2015-04-04 00:26:15 +03:00
|
|
|
"github.com/coreos/etcd/pkg/runtime"
|
2016-02-12 06:21:30 +03:00
|
|
|
"github.com/coreos/etcd/pkg/schedule"
|
2014-10-31 03:31:14 +03:00
|
|
|
"github.com/coreos/etcd/pkg/types"
|
2014-11-13 20:11:53 +03:00
|
|
|
"github.com/coreos/etcd/pkg/wait"
|
2014-08-26 05:39:02 +04:00
|
|
|
"github.com/coreos/etcd/raft"
|
2014-08-28 05:53:18 +04:00
|
|
|
"github.com/coreos/etcd/raft/raftpb"
|
2014-11-19 00:53:14 +03:00
|
|
|
"github.com/coreos/etcd/rafthttp"
|
2018-02-26 22:08:41 +03:00
|
|
|
"github.com/coreos/etcd/raftsnap"
|
2018-02-26 21:36:33 +03:00
|
|
|
"github.com/coreos/etcd/version"
|
2014-10-03 03:20:14 +04:00
|
|
|
"github.com/coreos/etcd/wal"
|
2018-04-16 08:40:44 +03:00
|
|
|
humanize "github.com/dustin/go-humanize"
|
2017-09-07 00:57:25 +03:00
|
|
|
|
2016-03-23 03:10:28 +03:00
|
|
|
"github.com/coreos/go-semver/semver"
|
|
|
|
"github.com/coreos/pkg/capnslog"
|
2018-04-16 08:40:44 +03:00
|
|
|
"go.uber.org/zap"
|
|
|
|
"go.uber.org/zap/zapcore"
|
2014-08-26 05:39:02 +04:00
|
|
|
)
|
|
|
|
|
2014-09-17 05:18:45 +04:00
|
|
|
const (
|
2017-01-14 05:00:28 +03:00
|
|
|
DefaultSnapCount = 100000
|
2014-10-15 11:19:27 +04:00
|
|
|
|
2015-04-29 22:05:40 +03:00
|
|
|
StoreClusterPrefix = "/0"
|
|
|
|
StoreKeysPrefix = "/1"
|
2014-11-29 08:32:28 +03:00
|
|
|
|
2016-08-05 03:20:35 +03:00
|
|
|
// HealthInterval is the minimum time the cluster should be healthy
|
|
|
|
// before accepting add member requests.
|
|
|
|
HealthInterval = 5 * time.Second
|
|
|
|
|
2016-01-25 03:16:05 +03:00
|
|
|
purgeFileInterval = 30 * time.Second
|
|
|
|
// monitorVersionInterval should be smaller than the timeout
|
2016-02-01 08:42:39 +03:00
|
|
|
// on the connection. Or we will not be able to reuse the connection
|
2016-01-25 03:16:05 +03:00
|
|
|
// (since it will timeout).
|
|
|
|
monitorVersionInterval = rafthttp.ConnWriteTimeout - time.Second
|
2015-09-15 08:15:23 +03:00
|
|
|
|
2015-12-08 18:52:54 +03:00
|
|
|
// max number of in-flight snapshot messages etcdserver allows to have
|
|
|
|
// This number is more than enough for most clusters with 5 machines.
|
|
|
|
maxInFlightMsgSnap = 16
|
2015-12-24 00:55:39 +03:00
|
|
|
|
2015-12-24 03:03:23 +03:00
|
|
|
releaseDelayAfterSnapshot = 30 * time.Second
|
2016-09-17 02:57:55 +03:00
|
|
|
|
|
|
|
// maxPendingRevokes is the maximum number of outstanding expired lease revocations.
|
|
|
|
maxPendingRevokes = 16
|
2017-05-23 02:54:50 +03:00
|
|
|
|
|
|
|
recommendedMaxRequestBytes = 10 * 1024 * 1024
|
2014-09-17 05:18:45 +04:00
|
|
|
)
|
2014-09-16 04:35:02 +04:00
|
|
|
|
2014-08-29 03:41:42 +04:00
|
|
|
var (
|
2015-06-08 23:28:34 +03:00
|
|
|
plog = capnslog.NewPackageLogger("github.com/coreos/etcd", "etcdserver")
|
|
|
|
|
2016-04-07 22:02:37 +03:00
|
|
|
storeMemberAttributeRegexp = regexp.MustCompile(path.Join(membership.StoreMembersPrefix, "[[:xdigit:]]{1,16}", "attributes"))
|
2014-08-29 03:41:42 +04:00
|
|
|
)
|
2014-08-27 02:42:10 +04:00
|
|
|
|
2014-09-18 01:38:10 +04:00
|
|
|
func init() {
|
|
|
|
rand.Seed(time.Now().UnixNano())
|
2015-04-04 00:26:15 +03:00
|
|
|
|
|
|
|
expvar.Publish(
|
|
|
|
"file_descriptor_limit",
|
|
|
|
expvar.Func(
|
|
|
|
func() interface{} {
|
|
|
|
n, _ := runtime.FDLimit()
|
|
|
|
return n
|
|
|
|
},
|
|
|
|
),
|
|
|
|
)
|
2014-09-18 01:38:10 +04:00
|
|
|
}
|
|
|
|
|
2014-08-26 05:39:02 +04:00
|
|
|
type Response struct {
|
2017-08-07 10:48:05 +03:00
|
|
|
Term uint64
|
|
|
|
Index uint64
|
2018-02-26 22:06:38 +03:00
|
|
|
Event *v2store.Event
|
|
|
|
Watcher v2store.Watcher
|
2017-08-07 13:55:18 +03:00
|
|
|
Err error
|
2014-08-26 05:39:02 +04:00
|
|
|
}
|
|
|
|
|
2017-08-07 10:48:05 +03:00
|
|
|
type ServerV2 interface {
|
|
|
|
Server
|
2018-03-16 02:30:08 +03:00
|
|
|
Leader() types.ID
|
|
|
|
|
2017-08-07 10:48:05 +03:00
|
|
|
// Do takes a V2 request and attempts to fulfill it, returning a Response.
|
|
|
|
Do(ctx context.Context, r pb.Request) (Response, error)
|
|
|
|
stats.Stats
|
|
|
|
ClientCertAuthEnabled() bool
|
|
|
|
}
|
|
|
|
|
|
|
|
type ServerV3 interface {
|
|
|
|
Server
|
2018-03-16 02:30:08 +03:00
|
|
|
RaftStatusGetter
|
2017-08-07 10:48:05 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
func (s *EtcdServer) ClientCertAuthEnabled() bool { return s.Cfg.ClientCertAuthEnabled }
|
|
|
|
|
|
|
|
type Server interface {
|
2014-10-16 20:49:26 +04:00
|
|
|
// AddMember attempts to add a member into the cluster. It will return
|
|
|
|
// ErrIDRemoved if member ID is removed from the cluster, or return
|
|
|
|
// ErrIDExists if member ID exists in the cluster.
|
2017-04-13 01:27:28 +03:00
|
|
|
AddMember(ctx context.Context, memb membership.Member) ([]*membership.Member, error)
|
2014-10-16 20:49:26 +04:00
|
|
|
// RemoveMember attempts to remove a member from the cluster. It will
|
|
|
|
// return ErrIDRemoved if member ID is removed from the cluster, or return
|
|
|
|
// ErrIDNotFound if member ID is not in the cluster.
|
2017-04-13 01:27:28 +03:00
|
|
|
RemoveMember(ctx context.Context, id uint64) ([]*membership.Member, error)
|
2015-09-14 23:41:13 +03:00
|
|
|
// UpdateMember attempts to update an existing member in the cluster. It will
|
2014-11-11 22:46:49 +03:00
|
|
|
// return ErrIDNotFound if the member ID does not exist.
|
2017-04-13 01:27:28 +03:00
|
|
|
UpdateMember(ctx context.Context, updateMemb membership.Member) ([]*membership.Member, error)
|
*: add cluster version and cluster version detection.
Cluster version is the min major.minor of all members in
the etcd cluster. Cluster version is set to the min version
that a etcd member is compatible with when first bootstrapp.
During a rolling upgrades, the cluster version will be updated
automatically.
For example:
```
Cluster [a:1, b:1 ,c:1] -> clusterVersion 1
update a -> 2, b -> 2
after a detection
Cluster [a:2, b:2 ,c:1] -> clusterVersion 1, since c is still 1
update c -> 2
after a detection
Cluster [a:2, b:2 ,c:2] -> clusterVersion 2
```
The API/raft component can utilize clusterVersion to determine if
it can accept a client request or a raft RPC.
We choose polling rather than pushing since we want to use the same
logic for cluster version detection and (TODO) cluster version checking.
Before a member actually joins a etcd cluster, it should check the version
of the cluster. Push does not work since the other members cannot push
version info to it before it actually joins. Moreover, we do not want our
raft RPC system (which is doing the heartbeat pushing) to coordinate cluster version.
2015-04-29 20:56:34 +03:00
|
|
|
|
|
|
|
// ClusterVersion is the cluster-wide minimum major.minor version.
|
2016-01-28 13:16:29 +03:00
|
|
|
// Cluster version is set to the min version that an etcd member is
|
*: add cluster version and cluster version detection.
Cluster version is the min major.minor of all members in
the etcd cluster. Cluster version is set to the min version
that a etcd member is compatible with when first bootstrapp.
During a rolling upgrades, the cluster version will be updated
automatically.
For example:
```
Cluster [a:1, b:1 ,c:1] -> clusterVersion 1
update a -> 2, b -> 2
after a detection
Cluster [a:2, b:2 ,c:1] -> clusterVersion 1, since c is still 1
update c -> 2
after a detection
Cluster [a:2, b:2 ,c:2] -> clusterVersion 2
```
The API/raft component can utilize clusterVersion to determine if
it can accept a client request or a raft RPC.
We choose polling rather than pushing since we want to use the same
logic for cluster version detection and (TODO) cluster version checking.
Before a member actually joins a etcd cluster, it should check the version
of the cluster. Push does not work since the other members cannot push
version info to it before it actually joins. Moreover, we do not want our
raft RPC system (which is doing the heartbeat pushing) to coordinate cluster version.
2015-04-29 20:56:34 +03:00
|
|
|
// compatible with when first bootstrap.
|
|
|
|
//
|
2015-05-08 21:10:12 +03:00
|
|
|
// ClusterVersion is nil until the cluster is bootstrapped (has a quorum).
|
|
|
|
//
|
*: add cluster version and cluster version detection.
Cluster version is the min major.minor of all members in
the etcd cluster. Cluster version is set to the min version
that a etcd member is compatible with when first bootstrapp.
During a rolling upgrades, the cluster version will be updated
automatically.
For example:
```
Cluster [a:1, b:1 ,c:1] -> clusterVersion 1
update a -> 2, b -> 2
after a detection
Cluster [a:2, b:2 ,c:1] -> clusterVersion 1, since c is still 1
update c -> 2
after a detection
Cluster [a:2, b:2 ,c:2] -> clusterVersion 2
```
The API/raft component can utilize clusterVersion to determine if
it can accept a client request or a raft RPC.
We choose polling rather than pushing since we want to use the same
logic for cluster version detection and (TODO) cluster version checking.
Before a member actually joins a etcd cluster, it should check the version
of the cluster. Push does not work since the other members cannot push
version info to it before it actually joins. Moreover, we do not want our
raft RPC system (which is doing the heartbeat pushing) to coordinate cluster version.
2015-04-29 20:56:34 +03:00
|
|
|
// During a rolling upgrades, the ClusterVersion will be updated
|
2015-05-08 21:10:12 +03:00
|
|
|
// automatically after a sync. (5 second by default)
|
*: add cluster version and cluster version detection.
Cluster version is the min major.minor of all members in
the etcd cluster. Cluster version is set to the min version
that a etcd member is compatible with when first bootstrapp.
During a rolling upgrades, the cluster version will be updated
automatically.
For example:
```
Cluster [a:1, b:1 ,c:1] -> clusterVersion 1
update a -> 2, b -> 2
after a detection
Cluster [a:2, b:2 ,c:1] -> clusterVersion 1, since c is still 1
update c -> 2
after a detection
Cluster [a:2, b:2 ,c:2] -> clusterVersion 2
```
The API/raft component can utilize clusterVersion to determine if
it can accept a client request or a raft RPC.
We choose polling rather than pushing since we want to use the same
logic for cluster version detection and (TODO) cluster version checking.
Before a member actually joins a etcd cluster, it should check the version
of the cluster. Push does not work since the other members cannot push
version info to it before it actually joins. Moreover, we do not want our
raft RPC system (which is doing the heartbeat pushing) to coordinate cluster version.
2015-04-29 20:56:34 +03:00
|
|
|
//
|
|
|
|
// The API/raft component can utilize ClusterVersion to determine if
|
|
|
|
// it can accept a client request or a raft RPC.
|
2015-05-08 21:10:12 +03:00
|
|
|
// NOTE: ClusterVersion might be nil when etcd 2.1 works with etcd 2.0 and
|
|
|
|
// the leader is etcd 2.0. etcd 2.0 leader will not update clusterVersion since
|
|
|
|
// this feature is introduced post 2.0.
|
*: add cluster version and cluster version detection.
Cluster version is the min major.minor of all members in
the etcd cluster. Cluster version is set to the min version
that a etcd member is compatible with when first bootstrapp.
During a rolling upgrades, the cluster version will be updated
automatically.
For example:
```
Cluster [a:1, b:1 ,c:1] -> clusterVersion 1
update a -> 2, b -> 2
after a detection
Cluster [a:2, b:2 ,c:1] -> clusterVersion 1, since c is still 1
update c -> 2
after a detection
Cluster [a:2, b:2 ,c:2] -> clusterVersion 2
```
The API/raft component can utilize clusterVersion to determine if
it can accept a client request or a raft RPC.
We choose polling rather than pushing since we want to use the same
logic for cluster version detection and (TODO) cluster version checking.
Before a member actually joins a etcd cluster, it should check the version
of the cluster. Push does not work since the other members cannot push
version info to it before it actually joins. Moreover, we do not want our
raft RPC system (which is doing the heartbeat pushing) to coordinate cluster version.
2015-04-29 20:56:34 +03:00
|
|
|
ClusterVersion() *semver.Version
|
2017-08-07 10:48:05 +03:00
|
|
|
Cluster() api.Cluster
|
|
|
|
Alarms() []*pb.AlarmMember
|
2014-09-12 04:14:41 +04:00
|
|
|
}
|
|
|
|
|
2014-10-11 17:03:03 +04:00
|
|
|
// EtcdServer is the production implementation of the Server interface
|
|
|
|
type EtcdServer struct {
|
2016-07-08 21:20:47 +03:00
|
|
|
// inflightSnapshots holds count the number of snapshots currently inflight.
|
|
|
|
inflightSnapshots int64 // must use atomic operations to access; keep 64-bit aligned.
|
|
|
|
appliedIndex uint64 // must use atomic operations to access; keep 64-bit aligned.
|
2016-07-12 23:16:35 +03:00
|
|
|
committedIndex uint64 // must use atomic operations to access; keep 64-bit aligned.
|
2018-03-16 02:30:08 +03:00
|
|
|
term uint64 // must use atomic operations to access; keep 64-bit aligned.
|
|
|
|
lead uint64 // must use atomic operations to access; keep 64-bit aligned.
|
|
|
|
|
2016-07-08 21:20:47 +03:00
|
|
|
// consistIndex used to hold the offset of current executing entry
|
|
|
|
// It is initialized to 0 before executing any entry.
|
|
|
|
consistIndex consistentIndex // must use atomic operations to access; keep 64-bit aligned.
|
2017-05-10 23:55:06 +03:00
|
|
|
r raftNode // uses 64-bit atomics; keep 64-bit aligned.
|
2016-04-23 05:57:33 +03:00
|
|
|
|
|
|
|
readych chan struct{}
|
2017-05-10 23:55:06 +03:00
|
|
|
Cfg ServerConfig
|
2015-01-16 01:39:30 +03:00
|
|
|
|
2018-04-16 08:40:44 +03:00
|
|
|
lgMu *sync.RWMutex
|
|
|
|
lg *zap.Logger
|
|
|
|
|
2016-10-27 02:26:00 +03:00
|
|
|
w wait.Wait
|
2016-09-13 06:16:53 +03:00
|
|
|
|
|
|
|
readMu sync.RWMutex
|
|
|
|
// read routine notifies etcd server that it waits for reading by sending an empty struct to
|
|
|
|
// readwaitC
|
|
|
|
readwaitc chan struct{}
|
|
|
|
// readNotifier is used to notify the read routine that it can process the request
|
|
|
|
// when there is no error
|
|
|
|
readNotifier *notifier
|
|
|
|
|
2016-09-14 01:46:05 +03:00
|
|
|
// stop signals the run goroutine should shutdown.
|
|
|
|
stop chan struct{}
|
|
|
|
// stopping is closed by run goroutine on shutdown.
|
|
|
|
stopping chan struct{}
|
|
|
|
// done is closed when all goroutines from start() complete.
|
2016-09-13 06:16:53 +03:00
|
|
|
done chan struct{}
|
|
|
|
|
2015-01-03 07:00:29 +03:00
|
|
|
errorc chan error
|
2014-10-31 03:31:14 +03:00
|
|
|
id types.ID
|
2016-04-07 22:02:37 +03:00
|
|
|
attributes membership.Attributes
|
2014-10-11 17:03:03 +04:00
|
|
|
|
2016-04-07 22:02:37 +03:00
|
|
|
cluster *membership.RaftCluster
|
2014-10-11 17:03:03 +04:00
|
|
|
|
2018-02-26 22:06:38 +03:00
|
|
|
v2store v2store.Store
|
2018-01-24 02:55:19 +03:00
|
|
|
snapshotter *raftsnap.Snapshotter
|
2015-12-11 04:29:36 +03:00
|
|
|
|
2016-05-17 00:40:52 +03:00
|
|
|
applyV2 ApplierV2
|
2016-04-20 10:31:58 +03:00
|
|
|
|
2016-07-27 21:49:58 +03:00
|
|
|
// applyV3 is the applier with auth and quotas
|
|
|
|
applyV3 applierV3
|
|
|
|
// applyV3Base is the core applier without auth or quotas
|
|
|
|
applyV3Base applierV3
|
2016-08-20 02:10:58 +03:00
|
|
|
applyWait wait.WaitTime
|
|
|
|
|
|
|
|
kv mvcc.ConsistentWatchableKV
|
|
|
|
lessor lease.Lessor
|
|
|
|
bemu sync.Mutex
|
|
|
|
be backend.Backend
|
|
|
|
authStore auth.AuthStore
|
|
|
|
alarmStore *alarm.AlarmStore
|
2014-10-11 17:03:03 +04:00
|
|
|
|
2014-10-11 02:53:28 +04:00
|
|
|
stats *stats.ServerStats
|
|
|
|
lstats *stats.LeaderStats
|
|
|
|
|
2017-03-07 01:08:46 +03:00
|
|
|
SyncTicker *time.Ticker
|
2016-02-25 06:35:15 +03:00
|
|
|
// compactor is used to auto-compact the KV.
|
2017-06-17 01:12:49 +03:00
|
|
|
compactor compactor.Compactor
|
2014-10-11 17:03:03 +04:00
|
|
|
|
2016-01-25 01:56:07 +03:00
|
|
|
// peerRt used to send requests (version, lease) to peers.
|
|
|
|
peerRt http.RoundTripper
|
|
|
|
reqIDGen *idutil.Generator
|
*: add cluster version and cluster version detection.
Cluster version is the min major.minor of all members in
the etcd cluster. Cluster version is set to the min version
that a etcd member is compatible with when first bootstrapp.
During a rolling upgrades, the cluster version will be updated
automatically.
For example:
```
Cluster [a:1, b:1 ,c:1] -> clusterVersion 1
update a -> 2, b -> 2
after a detection
Cluster [a:2, b:2 ,c:1] -> clusterVersion 1, since c is still 1
update c -> 2
after a detection
Cluster [a:2, b:2 ,c:2] -> clusterVersion 2
```
The API/raft component can utilize clusterVersion to determine if
it can accept a client request or a raft RPC.
We choose polling rather than pushing since we want to use the same
logic for cluster version detection and (TODO) cluster version checking.
Before a member actually joins a etcd cluster, it should check the version
of the cluster. Push does not work since the other members cannot push
version info to it before it actually joins. Moreover, we do not want our
raft RPC system (which is doing the heartbeat pushing) to coordinate cluster version.
2015-04-29 20:56:34 +03:00
|
|
|
|
2015-05-08 21:10:12 +03:00
|
|
|
// forceVersionC is used to force the version monitor loop
|
|
|
|
// to detect the cluster version immediately.
|
|
|
|
forceVersionC chan struct{}
|
2015-12-08 18:52:54 +03:00
|
|
|
|
2016-10-12 21:51:48 +03:00
|
|
|
// wgMu blocks concurrent waitgroup mutation while server stopping
|
|
|
|
wgMu sync.RWMutex
|
2016-04-21 18:48:11 +03:00
|
|
|
// wg is used to wait for the go routines that depends on the server state
|
|
|
|
// to exit when stopping the server.
|
|
|
|
wg sync.WaitGroup
|
2017-04-01 03:59:37 +03:00
|
|
|
|
|
|
|
// ctx is used for etcd-initiated requests that may need to be canceled
|
|
|
|
// on etcd server shutdown.
|
|
|
|
ctx context.Context
|
|
|
|
cancel context.CancelFunc
|
2017-04-18 02:32:36 +03:00
|
|
|
|
|
|
|
leadTimeMu sync.RWMutex
|
|
|
|
leadElectedTime time.Time
|
2018-03-01 01:03:03 +03:00
|
|
|
|
2018-03-27 03:10:12 +03:00
|
|
|
*AccessController
|
2014-10-11 17:03:03 +04:00
|
|
|
}
|
|
|
|
|
2014-10-03 03:20:14 +04:00
|
|
|
// NewServer creates a new EtcdServer from the supplied configuration. The
|
|
|
|
// configuration is considered static for the lifetime of the EtcdServer.
|
2017-05-10 23:55:06 +03:00
|
|
|
func NewServer(cfg ServerConfig) (srv *EtcdServer, err error) {
|
2018-02-26 22:06:38 +03:00
|
|
|
st := v2store.New(StoreClusterPrefix, StoreKeysPrefix)
|
2016-04-08 23:52:19 +03:00
|
|
|
|
2015-12-08 18:52:54 +03:00
|
|
|
var (
|
|
|
|
w *wal.WAL
|
|
|
|
n raft.Node
|
|
|
|
s *raft.MemoryStorage
|
|
|
|
id types.ID
|
2016-04-07 22:02:37 +03:00
|
|
|
cl *membership.RaftCluster
|
2015-12-08 18:52:54 +03:00
|
|
|
)
|
2015-02-12 02:24:32 +03:00
|
|
|
|
2017-05-23 02:54:50 +03:00
|
|
|
if cfg.MaxRequestBytes > recommendedMaxRequestBytes {
|
2018-04-16 08:40:44 +03:00
|
|
|
if cfg.Logger != nil {
|
|
|
|
cfg.Logger.Warn(
|
|
|
|
"exceeded recommended requet limit",
|
|
|
|
zap.Uint("max-request-bytes", cfg.MaxRequestBytes),
|
|
|
|
zap.String("max-request-size", humanize.Bytes(uint64(cfg.MaxRequestBytes))),
|
|
|
|
zap.Int("recommended-request-bytes", recommendedMaxRequestBytes),
|
|
|
|
zap.String("recommended-request-size", humanize.Bytes(uint64(recommendedMaxRequestBytes))),
|
|
|
|
)
|
|
|
|
} else {
|
|
|
|
plog.Warningf("MaxRequestBytes %v exceeds maximum recommended size %v", cfg.MaxRequestBytes, recommendedMaxRequestBytes)
|
|
|
|
}
|
2017-05-23 02:54:50 +03:00
|
|
|
}
|
|
|
|
|
2015-12-29 21:40:58 +03:00
|
|
|
if terr := fileutil.TouchDirAll(cfg.DataDir); terr != nil {
|
|
|
|
return nil, fmt.Errorf("cannot access data directory: %v", terr)
|
|
|
|
}
|
|
|
|
|
2015-03-25 01:13:34 +03:00
|
|
|
haveWAL := wal.Exist(cfg.WALDir())
|
2016-04-08 23:52:19 +03:00
|
|
|
|
2016-06-19 21:23:11 +03:00
|
|
|
if err = fileutil.TouchDirAll(cfg.SnapDir()); err != nil {
|
2018-04-16 08:40:44 +03:00
|
|
|
if cfg.Logger != nil {
|
|
|
|
cfg.Logger.Fatal(
|
|
|
|
"failed to create snapshot directory",
|
|
|
|
zap.String("path", cfg.SnapDir()),
|
|
|
|
zap.Error(err),
|
|
|
|
)
|
|
|
|
} else {
|
|
|
|
plog.Fatalf("create snapshot directory error: %v", err)
|
|
|
|
}
|
2016-04-08 23:52:19 +03:00
|
|
|
}
|
2018-04-16 08:40:44 +03:00
|
|
|
ss := raftsnap.New(cfg.Logger, cfg.SnapDir())
|
2016-06-27 06:14:43 +03:00
|
|
|
|
2017-05-11 21:13:45 +03:00
|
|
|
bepath := cfg.backendPath()
|
2016-06-27 06:14:43 +03:00
|
|
|
beExist := fileutil.Exist(bepath)
|
2017-05-11 21:13:45 +03:00
|
|
|
be := openBackend(cfg)
|
2016-11-02 03:55:16 +03:00
|
|
|
|
2016-04-08 23:52:19 +03:00
|
|
|
defer func() {
|
|
|
|
if err != nil {
|
|
|
|
be.Close()
|
|
|
|
}
|
|
|
|
}()
|
2014-11-18 21:43:20 +03:00
|
|
|
|
2015-12-29 10:16:08 +03:00
|
|
|
prt, err := rafthttp.NewRoundTripper(cfg.PeerTLSInfo, cfg.peerDialTimeout())
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
2015-10-11 09:04:10 +03:00
|
|
|
}
|
2016-06-27 20:27:27 +03:00
|
|
|
var (
|
|
|
|
remotes []*membership.Member
|
|
|
|
snapshot *raftpb.Snapshot
|
|
|
|
)
|
|
|
|
|
2014-10-24 03:41:58 +04:00
|
|
|
switch {
|
2014-11-06 22:09:33 +03:00
|
|
|
case !haveWAL && !cfg.NewCluster:
|
2016-04-08 23:52:19 +03:00
|
|
|
if err = cfg.VerifyJoinExisting(); err != nil {
|
2015-03-23 21:56:34 +03:00
|
|
|
return nil, err
|
|
|
|
}
|
2018-04-16 08:40:44 +03:00
|
|
|
cl, err = membership.NewClusterFromURLsMap(cfg.Logger, cfg.InitialClusterToken, cfg.InitialPeerURLsMap)
|
2015-04-27 05:49:01 +03:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2018-04-16 08:40:44 +03:00
|
|
|
existingCluster, gerr := GetClusterFromRemotePeers(cfg.Logger, getRemotePeerURLs(cl, cfg.Name), prt)
|
2016-04-08 23:52:19 +03:00
|
|
|
if gerr != nil {
|
|
|
|
return nil, fmt.Errorf("cannot fetch cluster info from peer urls: %v", gerr)
|
2014-10-25 02:26:05 +04:00
|
|
|
}
|
2016-04-08 23:52:19 +03:00
|
|
|
if err = membership.ValidateClusterAndAssignIDs(cl, existingCluster); err != nil {
|
2014-11-18 01:08:45 +03:00
|
|
|
return nil, fmt.Errorf("error validating peerURLs %s: %v", existingCluster, err)
|
2014-10-24 03:41:58 +04:00
|
|
|
}
|
2018-04-16 08:40:44 +03:00
|
|
|
if !isCompatibleWithCluster(cfg.Logger, cl, cl.MemberByName(cfg.Name).ID, prt) {
|
2016-05-31 00:54:30 +03:00
|
|
|
return nil, fmt.Errorf("incompatible with current running cluster")
|
2015-05-14 17:57:25 +03:00
|
|
|
}
|
|
|
|
|
2015-04-21 09:48:36 +03:00
|
|
|
remotes = existingCluster.Members()
|
2018-04-18 22:54:43 +03:00
|
|
|
cl.SetID(types.ID(0), existingCluster.ID())
|
2015-04-27 05:49:01 +03:00
|
|
|
cl.SetStore(st)
|
2016-04-08 23:52:19 +03:00
|
|
|
cl.SetBackend(be)
|
2014-11-13 07:11:04 +03:00
|
|
|
cfg.Print()
|
2015-04-27 05:49:01 +03:00
|
|
|
id, n, s, w = startNode(cfg, cl, nil)
|
2018-04-18 22:54:43 +03:00
|
|
|
cl.SetID(id, existingCluster.ID())
|
2018-04-16 08:40:44 +03:00
|
|
|
|
2014-11-06 22:09:33 +03:00
|
|
|
case !haveWAL && cfg.NewCluster:
|
2016-04-08 23:52:19 +03:00
|
|
|
if err = cfg.VerifyBootstrap(); err != nil {
|
2014-11-05 00:09:24 +03:00
|
|
|
return nil, err
|
2014-10-11 17:03:03 +04:00
|
|
|
}
|
2018-04-16 08:40:44 +03:00
|
|
|
cl, err = membership.NewClusterFromURLsMap(cfg.Logger, cfg.InitialClusterToken, cfg.InitialPeerURLsMap)
|
2015-04-27 05:49:01 +03:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
m := cl.MemberByName(cfg.Name)
|
2018-04-16 08:40:44 +03:00
|
|
|
if isMemberBootstrapped(cfg.Logger, cl, cfg.Name, prt, cfg.bootstrapTimeout()) {
|
2014-12-26 08:37:20 +03:00
|
|
|
return nil, fmt.Errorf("member %s has already been bootstrapped", m.ID)
|
|
|
|
}
|
2014-10-11 17:47:44 +04:00
|
|
|
if cfg.ShouldDiscover() {
|
2015-12-29 10:16:08 +03:00
|
|
|
var str string
|
|
|
|
str, err = discovery.JoinCluster(cfg.DiscoveryURL, cfg.DiscoveryProxy, m.ID, cfg.InitialPeerURLsMap.String())
|
|
|
|
if err != nil {
|
|
|
|
return nil, &DiscoveryError{Op: "join", Err: err}
|
2014-10-04 16:20:45 +04:00
|
|
|
}
|
2016-04-15 09:56:26 +03:00
|
|
|
var urlsmap types.URLsMap
|
|
|
|
urlsmap, err = types.NewURLsMap(str)
|
2015-12-29 10:16:08 +03:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
2014-10-04 16:20:45 +04:00
|
|
|
}
|
2015-04-27 05:49:01 +03:00
|
|
|
if checkDuplicateURL(urlsmap) {
|
|
|
|
return nil, fmt.Errorf("discovery cluster %s has duplicate url", urlsmap)
|
|
|
|
}
|
2018-04-16 08:40:44 +03:00
|
|
|
if cl, err = membership.NewClusterFromURLsMap(cfg.Logger, cfg.InitialClusterToken, urlsmap); err != nil {
|
2015-04-27 05:49:01 +03:00
|
|
|
return nil, err
|
2015-02-14 01:32:24 +03:00
|
|
|
}
|
2014-10-03 04:06:35 +04:00
|
|
|
}
|
2015-04-27 05:49:01 +03:00
|
|
|
cl.SetStore(st)
|
2016-04-08 23:52:19 +03:00
|
|
|
cl.SetBackend(be)
|
2014-11-13 07:11:04 +03:00
|
|
|
cfg.PrintWithInitial()
|
2015-04-27 05:49:01 +03:00
|
|
|
id, n, s, w = startNode(cfg, cl, cl.MemberIDs())
|
2018-04-18 22:54:43 +03:00
|
|
|
cl.SetID(id, cl.ID())
|
2018-04-16 08:40:44 +03:00
|
|
|
|
2014-10-24 03:41:58 +04:00
|
|
|
case haveWAL:
|
2016-04-08 23:52:19 +03:00
|
|
|
if err = fileutil.IsDirWriteable(cfg.MemberDir()); err != nil {
|
2015-02-12 20:45:12 +03:00
|
|
|
return nil, fmt.Errorf("cannot write to member directory: %v", err)
|
|
|
|
}
|
|
|
|
|
2016-04-08 23:52:19 +03:00
|
|
|
if err = fileutil.IsDirWriteable(cfg.WALDir()); err != nil {
|
2015-09-01 01:01:18 +03:00
|
|
|
return nil, fmt.Errorf("cannot write to WAL directory: %v", err)
|
|
|
|
}
|
|
|
|
|
2014-10-11 17:47:44 +04:00
|
|
|
if cfg.ShouldDiscover() {
|
2018-04-16 08:40:44 +03:00
|
|
|
if cfg.Logger != nil {
|
|
|
|
cfg.Logger.Warn(
|
|
|
|
"discovery token is ignored since cluster already initialized; valid logs are found",
|
|
|
|
zap.String("wal-dir", cfg.WALDir()),
|
|
|
|
)
|
|
|
|
} else {
|
|
|
|
plog.Warningf("discovery token ignored since a cluster has already been initialized. Valid log found at %q", cfg.WALDir())
|
|
|
|
}
|
2014-10-09 03:09:12 +04:00
|
|
|
}
|
2015-12-29 10:16:08 +03:00
|
|
|
snapshot, err = ss.Load()
|
2018-01-24 02:55:19 +03:00
|
|
|
if err != nil && err != raftsnap.ErrNoSnapshot {
|
2015-12-29 10:16:08 +03:00
|
|
|
return nil, err
|
2014-10-03 03:20:14 +04:00
|
|
|
}
|
|
|
|
if snapshot != nil {
|
2016-04-15 09:56:26 +03:00
|
|
|
if err = st.Recovery(snapshot.Data); err != nil {
|
2018-04-16 08:40:44 +03:00
|
|
|
if cfg.Logger != nil {
|
|
|
|
cfg.Logger.Panic("failed to recover from snapshot")
|
|
|
|
} else {
|
|
|
|
plog.Panicf("recovered store from snapshot error: %v", err)
|
|
|
|
}
|
2014-12-09 01:53:39 +03:00
|
|
|
}
|
2018-04-16 08:40:44 +03:00
|
|
|
|
|
|
|
if cfg.Logger != nil {
|
|
|
|
cfg.Logger.Info(
|
|
|
|
"recovered v2 store from snapshot",
|
|
|
|
zap.Uint64("snapshot-index", snapshot.Metadata.Index),
|
|
|
|
zap.String("snapshot-size", humanize.Bytes(uint64(snapshot.Size()))),
|
|
|
|
)
|
|
|
|
} else {
|
|
|
|
plog.Infof("recovered store from snapshot at index %d", snapshot.Metadata.Index)
|
|
|
|
}
|
|
|
|
|
2017-05-11 21:13:45 +03:00
|
|
|
if be, err = recoverSnapshotBackend(cfg, be, *snapshot); err != nil {
|
2018-04-16 08:40:44 +03:00
|
|
|
if cfg.Logger != nil {
|
|
|
|
cfg.Logger.Panic("failed to recover v3 backend from snapshot", zap.Error(err))
|
|
|
|
} else {
|
|
|
|
plog.Panicf("recovering backend from snapshot error: %v", err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if cfg.Logger != nil {
|
|
|
|
s1, s2 := be.Size(), be.SizeInUse()
|
|
|
|
cfg.Logger.Info(
|
|
|
|
"recovered v3 backend from snapshot",
|
|
|
|
zap.Int64("backend-size-bytes", s1),
|
|
|
|
zap.String("backend-size", humanize.Bytes(uint64(s1))),
|
|
|
|
zap.Int64("backend-size-in-use-bytes", s2),
|
|
|
|
zap.String("backend-size-in-use", humanize.Bytes(uint64(s2))),
|
|
|
|
)
|
2017-05-05 01:57:25 +03:00
|
|
|
}
|
2014-10-03 03:20:14 +04:00
|
|
|
}
|
2018-04-16 08:40:44 +03:00
|
|
|
|
2014-11-13 07:11:04 +03:00
|
|
|
cfg.Print()
|
2018-04-16 08:40:44 +03:00
|
|
|
|
2014-11-07 08:50:35 +03:00
|
|
|
if !cfg.ForceNewCluster {
|
2015-04-27 05:49:01 +03:00
|
|
|
id, cl, n, s, w = restartNode(cfg, snapshot)
|
2014-11-07 08:50:35 +03:00
|
|
|
} else {
|
2015-04-27 05:49:01 +03:00
|
|
|
id, cl, n, s, w = restartAsStandaloneNode(cfg, snapshot)
|
2014-11-07 08:50:35 +03:00
|
|
|
}
|
2018-04-16 08:40:44 +03:00
|
|
|
|
2015-04-27 05:49:01 +03:00
|
|
|
cl.SetStore(st)
|
2016-04-08 23:52:19 +03:00
|
|
|
cl.SetBackend(be)
|
2016-07-26 21:33:11 +03:00
|
|
|
cl.Recover(api.UpdateCapability)
|
2016-07-01 19:10:01 +03:00
|
|
|
if cl.Version() != nil && !cl.Version().LessThan(semver.Version{Major: 3}) && !beExist {
|
2016-06-27 06:14:43 +03:00
|
|
|
os.RemoveAll(bepath)
|
|
|
|
return nil, fmt.Errorf("database file (%v) of the backend is missing", bepath)
|
|
|
|
}
|
2018-04-16 08:40:44 +03:00
|
|
|
|
2014-10-24 03:41:58 +04:00
|
|
|
default:
|
2014-11-05 00:09:24 +03:00
|
|
|
return nil, fmt.Errorf("unsupported bootstrap config")
|
2014-10-03 03:20:14 +04:00
|
|
|
}
|
|
|
|
|
2015-12-29 21:40:58 +03:00
|
|
|
if terr := fileutil.TouchDirAll(cfg.MemberDir()); terr != nil {
|
|
|
|
return nil, fmt.Errorf("cannot access member directory: %v", terr)
|
2015-12-29 10:17:54 +03:00
|
|
|
}
|
|
|
|
|
2017-06-04 07:31:20 +03:00
|
|
|
sstats := stats.NewServerStats(cfg.Name, id.String())
|
2014-10-31 03:31:14 +03:00
|
|
|
lstats := stats.NewLeaderStats(id.String())
|
2014-10-11 02:53:28 +04:00
|
|
|
|
2016-10-07 23:18:39 +03:00
|
|
|
heartbeat := time.Duration(cfg.TickMs) * time.Millisecond
|
2016-04-08 23:52:19 +03:00
|
|
|
srv = &EtcdServer{
|
2017-05-11 21:13:45 +03:00
|
|
|
readych: make(chan struct{}),
|
|
|
|
Cfg: cfg,
|
2018-04-16 08:40:44 +03:00
|
|
|
lgMu: new(sync.RWMutex),
|
|
|
|
lg: cfg.Logger,
|
2017-05-11 21:13:45 +03:00
|
|
|
errorc: make(chan error, 1),
|
2018-02-26 22:06:38 +03:00
|
|
|
v2store: st,
|
2017-05-11 21:13:45 +03:00
|
|
|
snapshotter: ss,
|
2017-04-18 02:32:36 +03:00
|
|
|
r: *newRaftNode(
|
|
|
|
raftNodeConfig{
|
2018-04-16 08:40:44 +03:00
|
|
|
lg: cfg.Logger,
|
2017-04-18 02:32:36 +03:00
|
|
|
isIDRemoved: func(id uint64) bool { return cl.IsIDRemoved(types.ID(id)) },
|
|
|
|
Node: n,
|
|
|
|
heartbeat: heartbeat,
|
|
|
|
raftStorage: s,
|
|
|
|
storage: NewStorage(w, ss),
|
|
|
|
},
|
|
|
|
),
|
2018-03-27 03:10:12 +03:00
|
|
|
id: id,
|
|
|
|
attributes: membership.Attributes{Name: cfg.Name, ClientURLs: cfg.ClientURLs.StringSlice()},
|
|
|
|
cluster: cl,
|
|
|
|
stats: sstats,
|
|
|
|
lstats: lstats,
|
|
|
|
SyncTicker: time.NewTicker(500 * time.Millisecond),
|
|
|
|
peerRt: prt,
|
|
|
|
reqIDGen: idutil.NewGenerator(uint16(id), time.Now()),
|
|
|
|
forceVersionC: make(chan struct{}),
|
|
|
|
AccessController: &AccessController{CORS: cfg.CORS, HostWhitelist: cfg.HostWhitelist},
|
2014-10-03 03:20:14 +04:00
|
|
|
}
|
2014-12-29 23:08:13 +03:00
|
|
|
|
2018-02-26 22:06:38 +03:00
|
|
|
srv.applyV2 = &applierV2store{store: srv.v2store, cluster: srv.cluster}
|
2016-04-20 10:31:58 +03:00
|
|
|
|
2016-04-08 23:52:19 +03:00
|
|
|
srv.be = be
|
2016-10-07 23:18:39 +03:00
|
|
|
minTTL := time.Duration((3*cfg.ElectionTicks)/2) * heartbeat
|
2016-08-05 06:39:32 +03:00
|
|
|
|
|
|
|
// always recover lessor before kv. When we recover the mvcc.KV it will reattach keys to its leases.
|
|
|
|
// If we recover mvcc.KV first, it will attach the keys to the wrong lessor before it recovers.
|
2016-08-02 20:29:23 +03:00
|
|
|
srv.lessor = lease.NewLessor(srv.be, int64(math.Ceil(minTTL.Seconds())))
|
2018-04-16 08:40:44 +03:00
|
|
|
srv.kv = mvcc.New(srv.getLogger(), srv.be, srv.lessor, &srv.consistIndex)
|
2016-06-27 20:27:27 +03:00
|
|
|
if beExist {
|
|
|
|
kvindex := srv.kv.ConsistentIndex()
|
2016-08-30 20:43:36 +03:00
|
|
|
// TODO: remove kvindex != 0 checking when we do not expect users to upgrade
|
|
|
|
// etcd from pre-3.0 release.
|
2016-06-27 20:27:27 +03:00
|
|
|
if snapshot != nil && kvindex < snapshot.Metadata.Index {
|
2016-08-30 20:43:36 +03:00
|
|
|
if kvindex != 0 {
|
2018-04-16 08:40:44 +03:00
|
|
|
return nil, fmt.Errorf("database file (%v index %d) does not match with snapshot (index %d)", bepath, kvindex, snapshot.Metadata.Index)
|
|
|
|
}
|
|
|
|
if cfg.Logger != nil {
|
|
|
|
cfg.Logger.Warn(
|
|
|
|
"consistent index was never saved",
|
|
|
|
zap.Uint64("snapshot-index", snapshot.Metadata.Index),
|
|
|
|
)
|
|
|
|
} else {
|
|
|
|
plog.Warningf("consistent index never saved (snapshot index=%d)", snapshot.Metadata.Index)
|
2016-08-30 20:43:36 +03:00
|
|
|
}
|
2016-06-27 20:27:27 +03:00
|
|
|
}
|
|
|
|
}
|
2017-05-23 01:36:07 +03:00
|
|
|
newSrv := srv // since srv == nil in defer if srv is returned as nil
|
|
|
|
defer func() {
|
|
|
|
// closing backend without first closing kv can cause
|
|
|
|
// resumed compactions to fail with closed tx errors
|
|
|
|
if err != nil {
|
|
|
|
newSrv.kv.Close()
|
|
|
|
}
|
|
|
|
}()
|
|
|
|
|
2016-03-30 21:37:55 +03:00
|
|
|
srv.consistIndex.setConsistentIndex(srv.kv.ConsistentIndex())
|
2018-04-16 08:40:44 +03:00
|
|
|
tp, err := auth.NewTokenProvider(cfg.Logger, cfg.AuthToken,
|
2016-11-24 10:34:29 +03:00
|
|
|
func(index uint64) <-chan struct{} {
|
|
|
|
return srv.applyWait.Wait(index)
|
2016-07-21 08:13:57 +03:00
|
|
|
},
|
|
|
|
)
|
|
|
|
if err != nil {
|
2018-04-16 08:40:44 +03:00
|
|
|
if cfg.Logger != nil {
|
|
|
|
cfg.Logger.Warn("failed to create token provider", zap.Error(err))
|
|
|
|
} else {
|
|
|
|
plog.Errorf("failed to create token provider: %s", err)
|
|
|
|
}
|
2016-07-21 08:13:57 +03:00
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
srv.authStore = auth.NewAuthStore(srv.be, tp)
|
2017-06-17 01:12:49 +03:00
|
|
|
if num := cfg.AutoCompactionRetention; num != 0 {
|
|
|
|
srv.compactor, err = compactor.New(cfg.AutoCompactionMode, num, srv.kv, srv)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2016-03-24 02:39:15 +03:00
|
|
|
srv.compactor.Run()
|
2015-08-08 15:58:29 +03:00
|
|
|
}
|
2016-03-24 03:20:52 +03:00
|
|
|
|
2017-06-05 22:16:39 +03:00
|
|
|
srv.applyV3Base = srv.newApplierV3Backend()
|
2016-04-08 23:52:19 +03:00
|
|
|
if err = srv.restoreAlarms(); err != nil {
|
2016-03-26 01:14:32 +03:00
|
|
|
return nil, err
|
2016-03-24 03:20:52 +03:00
|
|
|
}
|
2015-08-08 15:58:29 +03:00
|
|
|
|
2015-04-21 09:48:36 +03:00
|
|
|
// TODO: move transport initialization near the definition of remote
|
2015-10-11 01:58:02 +03:00
|
|
|
tr := &rafthttp.Transport{
|
2018-04-16 08:40:44 +03:00
|
|
|
Logger: cfg.Logger,
|
2015-10-11 09:04:10 +03:00
|
|
|
TLSInfo: cfg.PeerTLSInfo,
|
|
|
|
DialTimeout: cfg.peerDialTimeout(),
|
|
|
|
ID: id,
|
2016-02-05 09:05:59 +03:00
|
|
|
URLs: cfg.PeerURLs,
|
2015-10-11 09:04:10 +03:00
|
|
|
ClusterID: cl.ID(),
|
|
|
|
Raft: srv,
|
2015-12-08 18:52:54 +03:00
|
|
|
Snapshotter: ss,
|
2015-10-11 09:04:10 +03:00
|
|
|
ServerStats: sstats,
|
|
|
|
LeaderStats: lstats,
|
|
|
|
ErrorC: srv.errorc,
|
|
|
|
}
|
2016-04-08 23:52:19 +03:00
|
|
|
if err = tr.Start(); err != nil {
|
2015-10-11 09:04:10 +03:00
|
|
|
return nil, err
|
2015-10-11 01:58:02 +03:00
|
|
|
}
|
2015-04-21 09:48:36 +03:00
|
|
|
// add all remotes into transport
|
|
|
|
for _, m := range remotes {
|
|
|
|
if m.ID != id {
|
|
|
|
tr.AddRemote(m.ID, m.PeerURLs)
|
|
|
|
}
|
|
|
|
}
|
2015-04-27 05:49:01 +03:00
|
|
|
for _, m := range cl.Members() {
|
2015-04-19 21:08:03 +03:00
|
|
|
if m.ID != id {
|
|
|
|
tr.AddPeer(m.ID, m.PeerURLs)
|
|
|
|
}
|
|
|
|
}
|
2015-01-16 01:39:30 +03:00
|
|
|
srv.r.transport = tr
|
2015-10-11 21:49:52 +03:00
|
|
|
|
2014-11-03 23:04:24 +03:00
|
|
|
return srv, nil
|
2014-10-03 03:20:14 +04:00
|
|
|
}
|
|
|
|
|
2018-04-16 08:40:44 +03:00
|
|
|
func (s *EtcdServer) getLogger() *zap.Logger {
|
|
|
|
s.lgMu.RLock()
|
|
|
|
l := s.lg
|
|
|
|
s.lgMu.RUnlock()
|
|
|
|
return l
|
|
|
|
}
|
|
|
|
|
2018-03-06 13:07:48 +03:00
|
|
|
func (s *EtcdServer) adjustTicks() {
|
2018-04-16 08:40:44 +03:00
|
|
|
lg := s.getLogger()
|
2018-03-06 13:07:48 +03:00
|
|
|
clusterN := len(s.cluster.Members())
|
|
|
|
|
|
|
|
// single-node fresh start, or single-node recovers from snapshot
|
|
|
|
if clusterN == 1 {
|
|
|
|
ticks := s.Cfg.ElectionTicks - 1
|
2018-04-16 08:40:44 +03:00
|
|
|
if lg != nil {
|
|
|
|
lg.Info(
|
|
|
|
"started as single-node; fast-forwarding election ticks",
|
|
|
|
zap.String("local-member-id", s.ID().String()),
|
|
|
|
zap.Int("forward-ticks", ticks),
|
|
|
|
zap.Int("election-ticks", s.Cfg.ElectionTicks),
|
|
|
|
)
|
|
|
|
} else {
|
|
|
|
plog.Infof("%s as single-node; fast-forwarding %d ticks (election ticks %d)", s.ID(), ticks, s.Cfg.ElectionTicks)
|
|
|
|
}
|
2018-03-06 13:07:48 +03:00
|
|
|
s.r.advanceTicks(ticks)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2018-04-20 00:00:31 +03:00
|
|
|
if !s.Cfg.InitialElectionTickAdvance {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2018-03-06 13:07:48 +03:00
|
|
|
// retry up to "rafthttp.ConnReadTimeout", which is 5-sec
|
|
|
|
// until peer connection reports; otherwise:
|
|
|
|
// 1. all connections failed, or
|
|
|
|
// 2. no active peers, or
|
|
|
|
// 3. restarted single-node with no snapshot
|
|
|
|
// then, do nothing, because advancing ticks would have no effect
|
|
|
|
waitTime := rafthttp.ConnReadTimeout
|
|
|
|
itv := 50 * time.Millisecond
|
|
|
|
for i := int64(0); i < int64(waitTime/itv); i++ {
|
|
|
|
select {
|
|
|
|
case <-time.After(itv):
|
|
|
|
case <-s.stopping:
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
peerN := s.r.transport.ActivePeers()
|
|
|
|
if peerN > 1 {
|
|
|
|
// multi-node received peer connection reports
|
|
|
|
// adjust ticks, in case slow leader message receive
|
|
|
|
ticks := s.Cfg.ElectionTicks - 2
|
2018-04-16 08:40:44 +03:00
|
|
|
|
|
|
|
if lg != nil {
|
|
|
|
lg.Info(
|
|
|
|
"initialized peer connections; fast-forwarding election ticks",
|
|
|
|
zap.String("local-member-id", s.ID().String()),
|
|
|
|
zap.Int("forward-ticks", ticks),
|
|
|
|
zap.Int("election-ticks", s.Cfg.ElectionTicks),
|
|
|
|
zap.Int("active-remote-members", peerN),
|
|
|
|
)
|
|
|
|
} else {
|
|
|
|
plog.Infof("%s initialized peer connection; fast-forwarding %d ticks (election ticks %d) with %d active peer(s)", s.ID(), ticks, s.Cfg.ElectionTicks, peerN)
|
|
|
|
}
|
|
|
|
|
2018-03-06 13:07:48 +03:00
|
|
|
s.r.advanceTicks(ticks)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-08-07 10:48:05 +03:00
|
|
|
// Start performs any initialization of the Server necessary for it to
|
|
|
|
// begin serving requests. It must be called before Do or Process.
|
|
|
|
// Start must be non-blocking; any long-running server functionality
|
|
|
|
// should be implemented in goroutines.
|
2014-09-30 04:14:22 +04:00
|
|
|
func (s *EtcdServer) Start() {
|
2014-09-29 22:52:36 +04:00
|
|
|
s.start()
|
2018-03-06 13:07:48 +03:00
|
|
|
s.goAttach(func() { s.adjustTicks() })
|
2016-09-14 01:46:05 +03:00
|
|
|
s.goAttach(func() { s.publish(s.Cfg.ReqTimeout()) })
|
|
|
|
s.goAttach(s.purgeFile)
|
|
|
|
s.goAttach(func() { monitorFileDescriptor(s.stopping) })
|
|
|
|
s.goAttach(s.monitorVersions)
|
2016-09-13 06:16:53 +03:00
|
|
|
s.goAttach(s.linearizableReadLoop)
|
2017-08-16 23:28:55 +03:00
|
|
|
s.goAttach(s.monitorKVHash)
|
2014-09-29 22:52:36 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
// start prepares and starts server in a new goroutine. It is no longer safe to
|
|
|
|
// modify a server's fields after it has been sent to Start.
|
|
|
|
// This function is just used for testing.
|
|
|
|
func (s *EtcdServer) start() {
|
2018-04-16 08:40:44 +03:00
|
|
|
lg := s.getLogger()
|
2017-05-10 23:55:06 +03:00
|
|
|
if s.Cfg.SnapCount == 0 {
|
2018-04-16 08:40:44 +03:00
|
|
|
if lg != nil {
|
|
|
|
|
|
|
|
} else {
|
|
|
|
plog.Infof("set snapshot count to default %d", DefaultSnapCount)
|
|
|
|
}
|
2017-05-10 23:55:06 +03:00
|
|
|
s.Cfg.SnapCount = DefaultSnapCount
|
2014-09-17 05:18:45 +04:00
|
|
|
}
|
2014-08-29 03:41:42 +04:00
|
|
|
s.w = wait.New()
|
2016-08-20 02:10:58 +03:00
|
|
|
s.applyWait = wait.NewTimeList()
|
2014-08-29 03:41:42 +04:00
|
|
|
s.done = make(chan struct{})
|
2014-11-14 00:47:12 +03:00
|
|
|
s.stop = make(chan struct{})
|
2016-09-14 01:46:05 +03:00
|
|
|
s.stopping = make(chan struct{})
|
2017-04-01 03:59:37 +03:00
|
|
|
s.ctx, s.cancel = context.WithCancel(context.Background())
|
2016-09-13 06:16:53 +03:00
|
|
|
s.readwaitc = make(chan struct{}, 1)
|
|
|
|
s.readNotifier = newNotifier()
|
2015-05-08 21:10:12 +03:00
|
|
|
if s.ClusterVersion() != nil {
|
2018-04-16 08:40:44 +03:00
|
|
|
if lg != nil {
|
|
|
|
lg.Info(
|
2018-04-17 23:27:55 +03:00
|
|
|
"starting etcd server",
|
2018-04-16 08:40:44 +03:00
|
|
|
zap.String("local-member-id", s.ID().String()),
|
|
|
|
zap.String("local-server-version", version.Version),
|
|
|
|
zap.String("cluster-id", s.Cluster().ID().String()),
|
|
|
|
zap.String("cluster-version", version.Cluster(s.ClusterVersion().String())),
|
|
|
|
)
|
|
|
|
} else {
|
|
|
|
plog.Infof("starting server... [version: %v, cluster version: %v]", version.Version, version.Cluster(s.ClusterVersion().String()))
|
|
|
|
}
|
2015-05-08 21:10:12 +03:00
|
|
|
} else {
|
2018-04-16 08:40:44 +03:00
|
|
|
if lg != nil {
|
|
|
|
lg.Info(
|
2018-04-17 23:27:55 +03:00
|
|
|
"starting etcd server",
|
2018-04-16 08:40:44 +03:00
|
|
|
zap.String("local-member-id", s.ID().String()),
|
|
|
|
zap.String("local-server-version", version.Version),
|
|
|
|
zap.String("cluster-version", "to_be_decided"),
|
|
|
|
)
|
|
|
|
} else {
|
|
|
|
plog.Infof("starting server... [version: %v, cluster version: to_be_decided]", version.Version)
|
|
|
|
}
|
2015-05-08 21:10:12 +03:00
|
|
|
}
|
2014-09-25 06:51:27 +04:00
|
|
|
// TODO: if this is an empty log, writes all peer infos
|
|
|
|
// into the first entry
|
2014-08-29 03:41:42 +04:00
|
|
|
go s.run()
|
|
|
|
}
|
2014-08-28 00:37:22 +04:00
|
|
|
|
2014-11-29 08:32:28 +03:00
|
|
|
func (s *EtcdServer) purgeFile() {
|
2017-05-23 01:44:21 +03:00
|
|
|
var dberrc, serrc, werrc <-chan error
|
2016-05-12 22:34:03 +03:00
|
|
|
if s.Cfg.MaxSnapFiles > 0 {
|
2018-04-16 08:40:44 +03:00
|
|
|
dberrc = fileutil.PurgeFile(s.getLogger(), s.Cfg.SnapDir(), "snap.db", s.Cfg.MaxSnapFiles, purgeFileInterval, s.done)
|
|
|
|
serrc = fileutil.PurgeFile(s.getLogger(), s.Cfg.SnapDir(), "snap", s.Cfg.MaxSnapFiles, purgeFileInterval, s.done)
|
2014-11-29 08:32:28 +03:00
|
|
|
}
|
2016-05-12 22:34:03 +03:00
|
|
|
if s.Cfg.MaxWALFiles > 0 {
|
2018-04-16 08:40:44 +03:00
|
|
|
werrc = fileutil.PurgeFile(s.getLogger(), s.Cfg.WALDir(), "wal", s.Cfg.MaxWALFiles, purgeFileInterval, s.done)
|
2014-11-29 08:32:28 +03:00
|
|
|
}
|
2018-04-16 08:40:44 +03:00
|
|
|
|
|
|
|
lg := s.getLogger()
|
2014-11-29 08:32:28 +03:00
|
|
|
select {
|
2017-05-23 01:44:21 +03:00
|
|
|
case e := <-dberrc:
|
2018-04-16 08:40:44 +03:00
|
|
|
if lg != nil {
|
|
|
|
lg.Fatal("failed to purge snap db file", zap.Error(e))
|
|
|
|
} else {
|
|
|
|
plog.Fatalf("failed to purge snap db file %v", e)
|
|
|
|
}
|
2014-11-29 08:32:28 +03:00
|
|
|
case e := <-serrc:
|
2018-04-16 08:40:44 +03:00
|
|
|
if lg != nil {
|
|
|
|
lg.Fatal("failed to purge snap file", zap.Error(e))
|
|
|
|
} else {
|
|
|
|
plog.Fatalf("failed to purge snap file %v", e)
|
|
|
|
}
|
2017-05-23 01:44:21 +03:00
|
|
|
case e := <-werrc:
|
2018-04-16 08:40:44 +03:00
|
|
|
if lg != nil {
|
|
|
|
lg.Fatal("failed to purge wal file", zap.Error(e))
|
|
|
|
} else {
|
|
|
|
plog.Fatalf("failed to purge wal file %v", e)
|
|
|
|
}
|
2016-09-14 01:46:05 +03:00
|
|
|
case <-s.stopping:
|
2014-11-29 08:32:28 +03:00
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-08-07 10:48:05 +03:00
|
|
|
func (s *EtcdServer) Cluster() api.Cluster { return s.cluster }
|
2015-05-13 03:22:06 +03:00
|
|
|
|
2017-08-07 10:48:05 +03:00
|
|
|
func (s *EtcdServer) ApplyWait() <-chan struct{} { return s.applyWait.Wait(s.getCommittedIndex()) }
|
2014-11-19 00:53:14 +03:00
|
|
|
|
2017-08-07 10:48:05 +03:00
|
|
|
type ServerPeer interface {
|
|
|
|
ServerV2
|
|
|
|
RaftHandler() http.Handler
|
|
|
|
LeaseHandler() http.Handler
|
|
|
|
}
|
2016-01-20 08:09:09 +03:00
|
|
|
|
2017-08-07 10:48:05 +03:00
|
|
|
func (s *EtcdServer) LeaseHandler() http.Handler {
|
|
|
|
if s.lessor == nil {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
return leasehttp.NewHandler(s.lessor, s.ApplyWait)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *EtcdServer) RaftHandler() http.Handler { return s.r.transport.Handler() }
|
2016-12-15 23:09:20 +03:00
|
|
|
|
2017-08-07 10:48:05 +03:00
|
|
|
// Process takes a raft message and applies it to the server's raft state
|
|
|
|
// machine, respecting any timeout of the given context.
|
2014-09-12 04:14:41 +04:00
|
|
|
func (s *EtcdServer) Process(ctx context.Context, m raftpb.Message) error {
|
2015-05-13 03:22:06 +03:00
|
|
|
if s.cluster.IsIDRemoved(types.ID(m.From)) {
|
2018-04-16 08:40:44 +03:00
|
|
|
if lg := s.getLogger(); lg != nil {
|
|
|
|
lg.Warn(
|
|
|
|
"rejected Raft message from removed member",
|
|
|
|
zap.String("local-member-id", s.ID().String()),
|
|
|
|
zap.String("removed-member-id", types.ID(m.From).String()),
|
|
|
|
)
|
|
|
|
} else {
|
|
|
|
plog.Warningf("reject message from removed member %s", types.ID(m.From).String())
|
|
|
|
}
|
2014-11-17 03:23:47 +03:00
|
|
|
return httptypes.NewHTTPError(http.StatusForbidden, "cannot process message from removed member")
|
2014-10-21 22:04:42 +04:00
|
|
|
}
|
2014-11-18 01:40:21 +03:00
|
|
|
if m.Type == raftpb.MsgApp {
|
|
|
|
s.stats.RecvAppendReq(types.ID(m.From).String(), m.Size())
|
2014-10-21 22:04:42 +04:00
|
|
|
}
|
2015-01-16 01:39:30 +03:00
|
|
|
return s.r.Step(ctx, m)
|
2014-09-12 04:14:41 +04:00
|
|
|
}
|
|
|
|
|
2015-05-13 03:22:06 +03:00
|
|
|
func (s *EtcdServer) IsIDRemoved(id uint64) bool { return s.cluster.IsIDRemoved(types.ID(id)) }
|
2015-04-26 09:24:59 +03:00
|
|
|
|
2015-03-01 18:02:24 +03:00
|
|
|
func (s *EtcdServer) ReportUnreachable(id uint64) { s.r.ReportUnreachable(id) }
|
|
|
|
|
2015-10-14 03:29:01 +03:00
|
|
|
// ReportSnapshot reports snapshot sent status to the raft state machine,
|
|
|
|
// and clears the used snapshot from the snapshot store.
|
2015-03-01 19:17:14 +03:00
|
|
|
func (s *EtcdServer) ReportSnapshot(id uint64, status raft.SnapshotStatus) {
|
|
|
|
s.r.ReportSnapshot(id, status)
|
|
|
|
}
|
|
|
|
|
2015-12-22 14:41:22 +03:00
|
|
|
type etcdProgress struct {
|
|
|
|
confState raftpb.ConfState
|
|
|
|
snapi uint64
|
2017-03-15 21:04:25 +03:00
|
|
|
appliedt uint64
|
2015-12-22 14:41:22 +03:00
|
|
|
appliedi uint64
|
|
|
|
}
|
|
|
|
|
2016-10-07 23:18:39 +03:00
|
|
|
// raftReadyHandler contains a set of EtcdServer operations to be called by raftNode,
|
|
|
|
// and helps decouple state machine logic from Raft algorithms.
|
|
|
|
// TODO: add a state machine interface to apply the commit entries and do snapshot/recover
|
|
|
|
type raftReadyHandler struct {
|
2018-03-16 02:30:08 +03:00
|
|
|
getLead func() (lead uint64)
|
|
|
|
updateLead func(lead uint64)
|
2017-04-18 02:32:36 +03:00
|
|
|
updateLeadership func(newLeader bool)
|
2016-12-22 23:03:49 +03:00
|
|
|
updateCommittedIndex func(uint64)
|
2016-10-07 23:18:39 +03:00
|
|
|
}
|
|
|
|
|
2014-09-12 04:14:41 +04:00
|
|
|
func (s *EtcdServer) run() {
|
2018-04-16 08:40:44 +03:00
|
|
|
lg := s.getLogger()
|
|
|
|
|
2017-03-07 01:08:46 +03:00
|
|
|
sn, err := s.r.raftStorage.Snapshot()
|
2014-12-09 03:13:16 +03:00
|
|
|
if err != nil {
|
2018-04-16 08:40:44 +03:00
|
|
|
if lg != nil {
|
|
|
|
lg.Panic("failed to get snapshot from Raft storage", zap.Error(err))
|
|
|
|
} else {
|
|
|
|
plog.Panicf("get snapshot from raft storage error: %v", err)
|
|
|
|
}
|
2014-12-09 03:13:16 +03:00
|
|
|
}
|
2016-10-07 23:18:39 +03:00
|
|
|
|
2017-03-14 21:25:02 +03:00
|
|
|
// asynchronously accept apply packets, dispatch progress in-order
|
|
|
|
sched := schedule.NewFIFOScheduler()
|
|
|
|
|
2016-10-07 23:18:39 +03:00
|
|
|
var (
|
|
|
|
smu sync.RWMutex
|
|
|
|
syncC <-chan time.Time
|
|
|
|
)
|
|
|
|
setSyncC := func(ch <-chan time.Time) {
|
|
|
|
smu.Lock()
|
|
|
|
syncC = ch
|
|
|
|
smu.Unlock()
|
|
|
|
}
|
|
|
|
getSyncC := func() (ch <-chan time.Time) {
|
|
|
|
smu.RLock()
|
|
|
|
ch = syncC
|
|
|
|
smu.RUnlock()
|
|
|
|
return
|
|
|
|
}
|
|
|
|
rh := &raftReadyHandler{
|
2018-03-16 02:30:08 +03:00
|
|
|
getLead: func() (lead uint64) { return s.getLead() },
|
|
|
|
updateLead: func(lead uint64) { s.setLead(lead) },
|
2017-04-18 02:32:36 +03:00
|
|
|
updateLeadership: func(newLeader bool) {
|
2016-10-07 23:18:39 +03:00
|
|
|
if !s.isLeader() {
|
|
|
|
if s.lessor != nil {
|
|
|
|
s.lessor.Demote()
|
|
|
|
}
|
|
|
|
if s.compactor != nil {
|
|
|
|
s.compactor.Pause()
|
|
|
|
}
|
|
|
|
setSyncC(nil)
|
|
|
|
} else {
|
2017-04-18 02:32:36 +03:00
|
|
|
if newLeader {
|
|
|
|
t := time.Now()
|
|
|
|
s.leadTimeMu.Lock()
|
|
|
|
s.leadElectedTime = t
|
|
|
|
s.leadTimeMu.Unlock()
|
|
|
|
}
|
2017-03-07 01:08:46 +03:00
|
|
|
setSyncC(s.SyncTicker.C)
|
2017-01-03 22:22:21 +03:00
|
|
|
if s.compactor != nil {
|
|
|
|
s.compactor.Resume()
|
|
|
|
}
|
2016-10-07 23:18:39 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
// TODO: remove the nil checking
|
|
|
|
// current test utility does not provide the stats
|
|
|
|
if s.stats != nil {
|
|
|
|
s.stats.BecomeLeader()
|
|
|
|
}
|
|
|
|
},
|
2016-12-22 23:03:49 +03:00
|
|
|
updateCommittedIndex: func(ci uint64) {
|
|
|
|
cci := s.getCommittedIndex()
|
|
|
|
if ci > cci {
|
|
|
|
s.setCommittedIndex(ci)
|
|
|
|
}
|
|
|
|
},
|
2016-10-07 23:18:39 +03:00
|
|
|
}
|
|
|
|
s.r.start(rh)
|
2014-12-09 03:13:16 +03:00
|
|
|
|
2016-02-12 06:21:30 +03:00
|
|
|
ep := etcdProgress{
|
2017-03-07 01:08:46 +03:00
|
|
|
confState: sn.Metadata.ConfState,
|
|
|
|
snapi: sn.Metadata.Index,
|
2017-03-15 21:04:25 +03:00
|
|
|
appliedt: sn.Metadata.Term,
|
2017-03-07 01:08:46 +03:00
|
|
|
appliedi: sn.Metadata.Index,
|
2016-02-12 06:21:30 +03:00
|
|
|
}
|
2015-12-22 14:42:56 +03:00
|
|
|
|
|
|
|
defer func() {
|
2016-10-12 21:51:48 +03:00
|
|
|
s.wgMu.Lock() // block concurrent waitgroup adds in goAttach while stopping
|
2016-09-14 01:46:05 +03:00
|
|
|
close(s.stopping)
|
2016-10-12 21:51:48 +03:00
|
|
|
s.wgMu.Unlock()
|
2017-04-01 03:59:37 +03:00
|
|
|
s.cancel()
|
2016-09-14 01:46:05 +03:00
|
|
|
|
2016-02-12 06:21:30 +03:00
|
|
|
sched.Stop()
|
|
|
|
|
2016-09-14 01:46:05 +03:00
|
|
|
// wait for gouroutines before closing raft so wal stays open
|
2016-05-18 00:16:29 +03:00
|
|
|
s.wg.Wait()
|
|
|
|
|
2017-03-07 01:08:46 +03:00
|
|
|
s.SyncTicker.Stop()
|
|
|
|
|
2016-04-23 03:13:57 +03:00
|
|
|
// must stop raft after scheduler-- etcdserver can leak rafthttp pipelines
|
|
|
|
// by adding a peer after raft stops the transport
|
|
|
|
s.r.stop()
|
|
|
|
|
2016-01-12 20:00:57 +03:00
|
|
|
// kv, lessor and backend can be nil if running without v3 enabled
|
2016-01-07 03:25:25 +03:00
|
|
|
// or running unit tests.
|
2016-01-12 20:00:57 +03:00
|
|
|
if s.lessor != nil {
|
|
|
|
s.lessor.Stop()
|
|
|
|
}
|
2016-01-07 03:25:25 +03:00
|
|
|
if s.kv != nil {
|
|
|
|
s.kv.Close()
|
|
|
|
}
|
2016-11-14 22:17:55 +03:00
|
|
|
if s.authStore != nil {
|
|
|
|
s.authStore.Close()
|
|
|
|
}
|
2016-01-07 03:25:25 +03:00
|
|
|
if s.be != nil {
|
|
|
|
s.be.Close()
|
|
|
|
}
|
2016-02-25 06:35:15 +03:00
|
|
|
if s.compactor != nil {
|
|
|
|
s.compactor.Stop()
|
|
|
|
}
|
2015-12-22 14:42:56 +03:00
|
|
|
close(s.done)
|
|
|
|
}()
|
2015-12-22 14:41:22 +03:00
|
|
|
|
2016-01-09 00:17:54 +03:00
|
|
|
var expiredLeaseC <-chan []*lease.Lease
|
|
|
|
if s.lessor != nil {
|
|
|
|
expiredLeaseC = s.lessor.ExpiredLeasesC()
|
|
|
|
}
|
|
|
|
|
|
|
|
for {
|
|
|
|
select {
|
2016-02-12 06:21:30 +03:00
|
|
|
case ap := <-s.r.apply():
|
|
|
|
f := func(context.Context) { s.applyAll(&ep, &ap) }
|
|
|
|
sched.Schedule(f)
|
2016-01-09 00:17:54 +03:00
|
|
|
case leases := <-expiredLeaseC:
|
2016-09-14 01:46:05 +03:00
|
|
|
s.goAttach(func() {
|
2016-09-17 02:57:55 +03:00
|
|
|
// Increases throughput of expired leases deletion process through parallelization
|
|
|
|
c := make(chan struct{}, maxPendingRevokes)
|
|
|
|
for _, lease := range leases {
|
|
|
|
select {
|
|
|
|
case c <- struct{}{}:
|
|
|
|
case <-s.stopping:
|
|
|
|
return
|
|
|
|
}
|
|
|
|
lid := lease.ID
|
|
|
|
s.goAttach(func() {
|
2017-06-05 10:20:09 +03:00
|
|
|
ctx := s.authStore.WithRoot(s.ctx)
|
2018-04-10 23:57:17 +03:00
|
|
|
_, lerr := s.LeaseRevoke(ctx, &pb.LeaseRevokeRequest{ID: int64(lid)})
|
|
|
|
if lerr == nil {
|
|
|
|
leaseExpired.Inc()
|
|
|
|
} else {
|
2018-04-16 08:40:44 +03:00
|
|
|
if lg != nil {
|
|
|
|
lg.Warn(
|
|
|
|
"failed to revoke lease",
|
|
|
|
zap.String("lease-id", fmt.Sprintf("%016x", lid)),
|
|
|
|
zap.Error(lerr),
|
|
|
|
)
|
|
|
|
} else {
|
|
|
|
plog.Warningf("failed to revoke %016x (%q)", lid, lerr.Error())
|
|
|
|
}
|
2017-12-15 07:11:20 +03:00
|
|
|
}
|
2018-04-10 23:57:17 +03:00
|
|
|
|
2016-09-17 02:57:55 +03:00
|
|
|
<-c
|
|
|
|
})
|
2016-01-09 00:17:54 +03:00
|
|
|
}
|
2016-09-14 01:46:05 +03:00
|
|
|
})
|
2016-01-09 00:17:54 +03:00
|
|
|
case err := <-s.errorc:
|
2018-04-16 08:40:44 +03:00
|
|
|
if lg != nil {
|
|
|
|
lg.Warn("server error", zap.Error(err))
|
|
|
|
lg.Warn("data-dir used by this member must be removed")
|
|
|
|
} else {
|
|
|
|
plog.Errorf("%s", err)
|
|
|
|
plog.Infof("the data-dir used by this member must be removed.")
|
|
|
|
}
|
2016-01-09 00:17:54 +03:00
|
|
|
return
|
2016-10-07 23:18:39 +03:00
|
|
|
case <-getSyncC():
|
2018-02-26 22:06:38 +03:00
|
|
|
if s.v2store.HasTTLKeys() {
|
2016-12-07 21:17:04 +03:00
|
|
|
s.sync(s.Cfg.ReqTimeout())
|
|
|
|
}
|
2016-01-09 00:17:54 +03:00
|
|
|
case <-s.stop:
|
|
|
|
return
|
|
|
|
}
|
2014-08-26 05:39:02 +04:00
|
|
|
}
|
2015-12-22 14:42:56 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
func (s *EtcdServer) applyAll(ep *etcdProgress, apply *apply) {
|
|
|
|
s.applySnapshot(ep, apply)
|
|
|
|
s.applyEntries(ep, apply)
|
2018-02-06 23:15:52 +03:00
|
|
|
|
2016-06-17 21:35:51 +03:00
|
|
|
proposalsApplied.Set(float64(ep.appliedi))
|
2016-08-20 02:10:58 +03:00
|
|
|
s.applyWait.Trigger(ep.appliedi)
|
2018-04-16 08:40:44 +03:00
|
|
|
|
2015-12-22 14:42:56 +03:00
|
|
|
// wait for the raft routine to finish the disk writes before triggering a
|
|
|
|
// snapshot. or applied index might be greater than the last index in raft
|
|
|
|
// storage, since the raft routine might be slower than apply routine.
|
2017-05-05 01:57:25 +03:00
|
|
|
<-apply.notifyc
|
2016-08-20 02:10:58 +03:00
|
|
|
|
2015-12-22 14:42:56 +03:00
|
|
|
s.triggerSnapshot(ep)
|
2016-02-12 06:21:30 +03:00
|
|
|
select {
|
|
|
|
// snapshot requested via send()
|
2016-10-27 02:26:00 +03:00
|
|
|
case m := <-s.r.msgSnapC:
|
2017-03-15 21:04:25 +03:00
|
|
|
merged := s.createMergedSnapshotMessage(m, ep.appliedt, ep.appliedi, ep.confState)
|
2016-02-12 06:21:30 +03:00
|
|
|
s.sendMergedSnap(merged)
|
|
|
|
default:
|
|
|
|
}
|
2014-08-26 05:39:02 +04:00
|
|
|
}
|
|
|
|
|
2015-12-22 14:41:22 +03:00
|
|
|
func (s *EtcdServer) applySnapshot(ep *etcdProgress, apply *apply) {
|
|
|
|
if raft.IsEmptySnap(apply.snapshot) {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2018-04-16 08:40:44 +03:00
|
|
|
lg := s.getLogger()
|
|
|
|
if lg != nil {
|
|
|
|
lg.Info(
|
|
|
|
"applying snapshot",
|
|
|
|
zap.Uint64("current-snapshot-index", ep.snapi),
|
|
|
|
zap.Uint64("current-applied-index", ep.appliedi),
|
|
|
|
zap.Uint64("incoming-snapshot-index", apply.snapshot.Metadata.Index),
|
|
|
|
)
|
|
|
|
} else {
|
|
|
|
plog.Infof("applying snapshot at index %d...", ep.snapi)
|
|
|
|
}
|
|
|
|
defer func() {
|
|
|
|
if lg != nil {
|
|
|
|
lg.Info(
|
|
|
|
"applied snapshot",
|
|
|
|
zap.Uint64("current-snapshot-index", ep.snapi),
|
|
|
|
zap.Uint64("current-applied-index", ep.appliedi),
|
|
|
|
zap.Uint64("incoming-snapshot-index", apply.snapshot.Metadata.Index),
|
|
|
|
)
|
|
|
|
} else {
|
|
|
|
plog.Infof("finished applying incoming snapshot at index %d", ep.snapi)
|
|
|
|
}
|
|
|
|
}()
|
2016-06-01 07:12:41 +03:00
|
|
|
|
2015-12-22 14:41:22 +03:00
|
|
|
if apply.snapshot.Metadata.Index <= ep.appliedi {
|
2018-04-16 08:40:44 +03:00
|
|
|
if lg != nil {
|
|
|
|
// expect apply.snapshot.Metadata.Index > ep.appliedi + 1
|
|
|
|
lg.Panic(
|
|
|
|
"unexpected snapshot from future index",
|
|
|
|
zap.Uint64("current-snapshot-index", ep.snapi),
|
|
|
|
zap.Uint64("current-applied-index", ep.appliedi),
|
|
|
|
zap.Uint64("incoming-snapshot-index", apply.snapshot.Metadata.Index),
|
|
|
|
)
|
|
|
|
} else {
|
|
|
|
plog.Panicf("snapshot index [%d] should > appliedi[%d] + 1",
|
|
|
|
apply.snapshot.Metadata.Index, ep.appliedi)
|
|
|
|
}
|
2015-12-22 14:41:22 +03:00
|
|
|
}
|
|
|
|
|
2017-05-11 21:13:45 +03:00
|
|
|
// wait for raftNode to persist snapshot onto the disk
|
2017-05-05 01:57:25 +03:00
|
|
|
<-apply.notifyc
|
|
|
|
|
2017-05-11 21:13:45 +03:00
|
|
|
newbe, err := openSnapshotBackend(s.Cfg, s.snapshotter, apply.snapshot)
|
2016-03-24 02:39:15 +03:00
|
|
|
if err != nil {
|
2018-04-16 08:40:44 +03:00
|
|
|
if lg != nil {
|
|
|
|
lg.Panic("failed to open snapshot backend", zap.Error(err))
|
|
|
|
} else {
|
|
|
|
plog.Panic(err)
|
|
|
|
}
|
2016-03-24 02:39:15 +03:00
|
|
|
}
|
2015-12-22 14:41:22 +03:00
|
|
|
|
2016-08-05 06:39:32 +03:00
|
|
|
// always recover lessor before kv. When we recover the mvcc.KV it will reattach keys to its leases.
|
|
|
|
// If we recover mvcc.KV first, it will attach the keys to the wrong lessor before it recovers.
|
2016-08-04 18:06:19 +03:00
|
|
|
if s.lessor != nil {
|
2018-04-16 08:40:44 +03:00
|
|
|
if lg != nil {
|
|
|
|
lg.Info("restoring lease store")
|
|
|
|
} else {
|
|
|
|
plog.Info("recovering lessor...")
|
|
|
|
}
|
|
|
|
|
2017-01-05 12:13:47 +03:00
|
|
|
s.lessor.Recover(newbe, func() lease.TxnDelete { return s.kv.Write() })
|
2018-04-16 08:40:44 +03:00
|
|
|
|
|
|
|
if lg != nil {
|
|
|
|
lg.Info("restored lease store")
|
|
|
|
} else {
|
|
|
|
plog.Info("finished recovering lessor")
|
|
|
|
}
|
2016-08-04 18:06:19 +03:00
|
|
|
}
|
|
|
|
|
2018-04-16 08:40:44 +03:00
|
|
|
if lg != nil {
|
|
|
|
lg.Info("restoring mvcc store")
|
|
|
|
} else {
|
|
|
|
plog.Info("restoring mvcc store...")
|
|
|
|
}
|
2016-06-01 07:12:41 +03:00
|
|
|
|
2016-03-24 02:39:15 +03:00
|
|
|
if err := s.kv.Restore(newbe); err != nil {
|
2018-04-16 08:40:44 +03:00
|
|
|
if lg != nil {
|
|
|
|
lg.Panic("failed to restore mvcc store", zap.Error(err))
|
|
|
|
} else {
|
|
|
|
plog.Panicf("restore KV error: %v", err)
|
|
|
|
}
|
2016-03-24 02:39:15 +03:00
|
|
|
}
|
2015-12-22 14:41:22 +03:00
|
|
|
|
2018-04-16 08:40:44 +03:00
|
|
|
s.consistIndex.setConsistentIndex(s.kv.ConsistentIndex())
|
|
|
|
if lg != nil {
|
|
|
|
lg.Info("restored mvcc store")
|
|
|
|
} else {
|
|
|
|
plog.Info("finished restoring mvcc store")
|
|
|
|
}
|
2016-06-01 07:12:41 +03:00
|
|
|
|
2016-03-24 02:39:15 +03:00
|
|
|
// Closing old backend might block until all the txns
|
|
|
|
// on the backend are finished.
|
|
|
|
// We do not want to wait on closing the old backend.
|
|
|
|
s.bemu.Lock()
|
|
|
|
oldbe := s.be
|
|
|
|
go func() {
|
2018-04-16 08:40:44 +03:00
|
|
|
if lg != nil {
|
|
|
|
lg.Info("closing old backend file")
|
|
|
|
} else {
|
|
|
|
plog.Info("closing old backend...")
|
|
|
|
}
|
|
|
|
defer func() {
|
|
|
|
if lg != nil {
|
|
|
|
lg.Info("closed old backend file")
|
|
|
|
} else {
|
|
|
|
plog.Info("finished closing old backend")
|
|
|
|
}
|
|
|
|
}()
|
2016-03-24 02:39:15 +03:00
|
|
|
if err := oldbe.Close(); err != nil {
|
2018-04-16 08:40:44 +03:00
|
|
|
if lg != nil {
|
|
|
|
lg.Panic("failed to close old backend", zap.Error(err))
|
|
|
|
} else {
|
|
|
|
plog.Panicf("close backend error: %v", err)
|
|
|
|
}
|
2016-03-24 02:39:15 +03:00
|
|
|
}
|
|
|
|
}()
|
2016-01-07 03:25:25 +03:00
|
|
|
|
2016-03-24 02:39:15 +03:00
|
|
|
s.be = newbe
|
|
|
|
s.bemu.Unlock()
|
2016-03-11 04:06:10 +03:00
|
|
|
|
2018-04-16 08:40:44 +03:00
|
|
|
if lg != nil {
|
|
|
|
lg.Info("restoring alarm store")
|
|
|
|
} else {
|
|
|
|
plog.Info("recovering alarms...")
|
|
|
|
}
|
|
|
|
|
2016-03-26 01:14:32 +03:00
|
|
|
if err := s.restoreAlarms(); err != nil {
|
2018-04-16 08:40:44 +03:00
|
|
|
if lg != nil {
|
|
|
|
lg.Panic("failed to restore alarm store", zap.Error(err))
|
|
|
|
} else {
|
|
|
|
plog.Panicf("restore alarms error: %v", err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if lg != nil {
|
|
|
|
lg.Info("restored alarm store")
|
|
|
|
} else {
|
|
|
|
plog.Info("finished recovering alarms")
|
2016-03-26 01:14:32 +03:00
|
|
|
}
|
|
|
|
|
2016-03-24 02:39:15 +03:00
|
|
|
if s.authStore != nil {
|
2018-04-16 08:40:44 +03:00
|
|
|
if lg != nil {
|
|
|
|
lg.Info("restoring auth store")
|
|
|
|
} else {
|
|
|
|
plog.Info("recovering auth store...")
|
|
|
|
}
|
|
|
|
|
2016-03-24 02:39:15 +03:00
|
|
|
s.authStore.Recover(newbe)
|
2018-04-16 08:40:44 +03:00
|
|
|
|
|
|
|
if lg != nil {
|
|
|
|
lg.Info("restored auth store")
|
|
|
|
} else {
|
|
|
|
plog.Info("finished recovering auth store")
|
|
|
|
}
|
2015-12-22 14:41:22 +03:00
|
|
|
}
|
2016-03-24 02:39:15 +03:00
|
|
|
|
2018-04-16 08:40:44 +03:00
|
|
|
if lg != nil {
|
|
|
|
lg.Info("restoring v2 store")
|
|
|
|
} else {
|
|
|
|
plog.Info("recovering store v2...")
|
|
|
|
}
|
2018-02-26 22:06:38 +03:00
|
|
|
if err := s.v2store.Recovery(apply.snapshot.Data); err != nil {
|
2018-04-16 08:40:44 +03:00
|
|
|
if lg != nil {
|
|
|
|
lg.Panic("failed to restore v2 store", zap.Error(err))
|
|
|
|
} else {
|
|
|
|
plog.Panicf("recovery store error: %v", err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if lg != nil {
|
|
|
|
lg.Info("restored v2 store")
|
|
|
|
} else {
|
|
|
|
plog.Info("finished recovering store v2")
|
2015-12-22 14:41:22 +03:00
|
|
|
}
|
2016-06-01 07:12:41 +03:00
|
|
|
|
2016-04-08 23:52:19 +03:00
|
|
|
s.cluster.SetBackend(s.be)
|
2018-04-16 08:40:44 +03:00
|
|
|
|
|
|
|
if lg != nil {
|
|
|
|
lg.Info("restoring cluster configuration")
|
|
|
|
} else {
|
|
|
|
plog.Info("recovering cluster configuration...")
|
|
|
|
}
|
|
|
|
|
2016-07-26 21:33:11 +03:00
|
|
|
s.cluster.Recover(api.UpdateCapability)
|
2015-12-22 14:41:22 +03:00
|
|
|
|
2018-04-16 08:40:44 +03:00
|
|
|
if lg != nil {
|
|
|
|
lg.Info("restored cluster configuration")
|
|
|
|
lg.Info("removing old peers from network")
|
|
|
|
} else {
|
|
|
|
plog.Info("finished recovering cluster configuration")
|
|
|
|
plog.Info("removing old peers from network...")
|
|
|
|
}
|
|
|
|
|
2015-12-22 14:41:22 +03:00
|
|
|
// recover raft transport
|
|
|
|
s.r.transport.RemoveAllPeers()
|
2016-06-01 07:12:41 +03:00
|
|
|
|
2018-04-16 08:40:44 +03:00
|
|
|
if lg != nil {
|
|
|
|
lg.Info("removed old peers from network")
|
|
|
|
lg.Info("adding peers from new cluster configuration")
|
|
|
|
} else {
|
|
|
|
plog.Info("finished removing old peers from network")
|
|
|
|
plog.Info("adding peers from new cluster configuration into network...")
|
|
|
|
}
|
|
|
|
|
2015-12-22 14:41:22 +03:00
|
|
|
for _, m := range s.cluster.Members() {
|
|
|
|
if m.ID == s.ID() {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
s.r.transport.AddPeer(m.ID, m.PeerURLs)
|
|
|
|
}
|
2018-04-16 08:40:44 +03:00
|
|
|
|
|
|
|
if lg != nil {
|
|
|
|
lg.Info("added peers from new cluster configuration")
|
|
|
|
} else {
|
|
|
|
plog.Info("finished adding peers from new cluster configuration into network...")
|
|
|
|
}
|
2015-12-22 14:41:22 +03:00
|
|
|
|
2017-03-15 21:04:25 +03:00
|
|
|
ep.appliedt = apply.snapshot.Metadata.Term
|
2015-12-22 14:41:22 +03:00
|
|
|
ep.appliedi = apply.snapshot.Metadata.Index
|
|
|
|
ep.snapi = ep.appliedi
|
|
|
|
ep.confState = apply.snapshot.Metadata.ConfState
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *EtcdServer) applyEntries(ep *etcdProgress, apply *apply) {
|
|
|
|
if len(apply.entries) == 0 {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
firsti := apply.entries[0].Index
|
|
|
|
if firsti > ep.appliedi+1 {
|
2018-04-16 08:40:44 +03:00
|
|
|
if lg := s.getLogger(); lg != nil {
|
|
|
|
lg.Panic(
|
|
|
|
"unexpected committed entry index",
|
|
|
|
zap.Uint64("current-applied-index", ep.appliedi),
|
|
|
|
zap.Uint64("first-committed-entry-index", firsti),
|
|
|
|
)
|
|
|
|
} else {
|
|
|
|
plog.Panicf("first index of committed entry[%d] should <= appliedi[%d] + 1", firsti, ep.appliedi)
|
|
|
|
}
|
2015-12-22 14:41:22 +03:00
|
|
|
}
|
|
|
|
var ents []raftpb.Entry
|
|
|
|
if ep.appliedi+1-firsti < uint64(len(apply.entries)) {
|
|
|
|
ents = apply.entries[ep.appliedi+1-firsti:]
|
|
|
|
}
|
2016-01-26 21:19:39 +03:00
|
|
|
if len(ents) == 0 {
|
|
|
|
return
|
|
|
|
}
|
2015-12-22 14:41:22 +03:00
|
|
|
var shouldstop bool
|
2017-03-15 21:04:25 +03:00
|
|
|
if ep.appliedt, ep.appliedi, shouldstop = s.apply(ents, &ep.confState); shouldstop {
|
2015-12-22 14:41:22 +03:00
|
|
|
go s.stopWithDelay(10*100*time.Millisecond, fmt.Errorf("the member has been permanently removed from the cluster"))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *EtcdServer) triggerSnapshot(ep *etcdProgress) {
|
2017-05-10 23:55:06 +03:00
|
|
|
if ep.appliedi-ep.snapi <= s.Cfg.SnapCount {
|
2015-12-22 14:41:22 +03:00
|
|
|
return
|
|
|
|
}
|
2015-12-24 03:03:23 +03:00
|
|
|
|
2018-04-16 08:40:44 +03:00
|
|
|
if lg := s.getLogger(); lg != nil {
|
|
|
|
lg.Info(
|
|
|
|
"triggering snapshot",
|
|
|
|
zap.String("local-member-id", s.ID().String()),
|
|
|
|
zap.Uint64("local-member-applied-index", ep.appliedi),
|
|
|
|
zap.Uint64("local-member-snapshot-index", ep.snapi),
|
|
|
|
zap.Uint64("local-member-snapshot-count", s.Cfg.SnapCount),
|
|
|
|
)
|
|
|
|
} else {
|
|
|
|
plog.Infof("start to snapshot (applied: %d, lastsnap: %d)", ep.appliedi, ep.snapi)
|
|
|
|
}
|
|
|
|
|
2015-12-22 14:41:22 +03:00
|
|
|
s.snapshot(ep.appliedi, ep.confState)
|
|
|
|
ep.snapi = ep.appliedi
|
|
|
|
}
|
|
|
|
|
2016-08-13 22:41:04 +03:00
|
|
|
func (s *EtcdServer) isMultiNode() bool {
|
|
|
|
return s.cluster != nil && len(s.cluster.MemberIDs()) > 1
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *EtcdServer) isLeader() bool {
|
|
|
|
return uint64(s.ID()) == s.Lead()
|
|
|
|
}
|
|
|
|
|
2017-06-23 22:50:52 +03:00
|
|
|
// MoveLeader transfers the leader to the given transferee.
|
|
|
|
func (s *EtcdServer) MoveLeader(ctx context.Context, lead, transferee uint64) error {
|
2016-08-13 22:41:04 +03:00
|
|
|
now := time.Now()
|
|
|
|
interval := time.Duration(s.Cfg.TickMs) * time.Millisecond
|
|
|
|
|
2018-04-16 08:40:44 +03:00
|
|
|
if lg := s.getLogger(); lg != nil {
|
|
|
|
lg.Info(
|
|
|
|
"leadership transfer starting",
|
|
|
|
zap.String("local-member-id", s.ID().String()),
|
|
|
|
zap.String("current-leader-member-id", types.ID(lead).String()),
|
|
|
|
zap.String("transferee-member-id", types.ID(transferee).String()),
|
|
|
|
)
|
|
|
|
} else {
|
|
|
|
plog.Infof("%s starts leadership transfer from %s to %s", s.ID(), types.ID(lead), types.ID(transferee))
|
|
|
|
}
|
|
|
|
|
2016-08-13 22:41:04 +03:00
|
|
|
s.r.TransferLeadership(ctx, lead, transferee)
|
|
|
|
for s.Lead() != transferee {
|
|
|
|
select {
|
|
|
|
case <-ctx.Done(): // time out
|
|
|
|
return ErrTimeoutLeaderTransfer
|
|
|
|
case <-time.After(interval):
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// TODO: drain all requests, or drop all messages to the old leader
|
2018-04-16 08:40:44 +03:00
|
|
|
if lg := s.getLogger(); lg != nil {
|
|
|
|
lg.Info(
|
|
|
|
"leadership transfer finished",
|
|
|
|
zap.String("local-member-id", s.ID().String()),
|
|
|
|
zap.String("old-leader-member-id", types.ID(lead).String()),
|
|
|
|
zap.String("new-leader-member-id", types.ID(transferee).String()),
|
|
|
|
zap.Duration("took", time.Since(now)),
|
|
|
|
)
|
|
|
|
} else {
|
|
|
|
plog.Infof("%s finished leadership transfer from %s to %s (took %v)", s.ID(), types.ID(lead), types.ID(transferee), time.Since(now))
|
|
|
|
}
|
2016-08-13 22:41:04 +03:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// TransferLeadership transfers the leader to the chosen transferee.
|
|
|
|
func (s *EtcdServer) TransferLeadership() error {
|
2016-08-17 21:40:46 +03:00
|
|
|
if !s.isLeader() {
|
2018-04-16 08:40:44 +03:00
|
|
|
if lg := s.getLogger(); lg != nil {
|
|
|
|
lg.Info(
|
|
|
|
"skipped leadership transfer; local server is not leader",
|
|
|
|
zap.String("local-member-id", s.ID().String()),
|
|
|
|
zap.String("current-leader-member-id", types.ID(s.Lead()).String()),
|
|
|
|
)
|
|
|
|
} else {
|
|
|
|
plog.Printf("skipped leadership transfer for stopping non-leader member")
|
|
|
|
}
|
2016-08-17 21:40:46 +03:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
if !s.isMultiNode() {
|
2018-04-16 08:40:44 +03:00
|
|
|
if lg := s.getLogger(); lg != nil {
|
|
|
|
lg.Info(
|
|
|
|
"skipped leadership transfer; it's a single-node cluster",
|
|
|
|
zap.String("local-member-id", s.ID().String()),
|
|
|
|
zap.String("current-leader-member-id", types.ID(s.Lead()).String()),
|
|
|
|
)
|
|
|
|
} else {
|
|
|
|
plog.Printf("skipped leadership transfer for single member cluster")
|
|
|
|
}
|
2016-08-13 22:41:04 +03:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
transferee, ok := longestConnected(s.r.transport, s.cluster.MemberIDs())
|
|
|
|
if !ok {
|
|
|
|
return ErrUnhealthy
|
|
|
|
}
|
|
|
|
|
|
|
|
tm := s.Cfg.ReqTimeout()
|
2017-04-01 03:59:37 +03:00
|
|
|
ctx, cancel := context.WithTimeout(s.ctx, tm)
|
2017-06-23 22:50:52 +03:00
|
|
|
err := s.MoveLeader(ctx, s.Lead(), uint64(transferee))
|
2016-08-13 22:41:04 +03:00
|
|
|
cancel()
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
// HardStop stops the server without coordination with other members in the cluster.
|
|
|
|
func (s *EtcdServer) HardStop() {
|
2014-11-14 00:47:12 +03:00
|
|
|
select {
|
|
|
|
case s.stop <- struct{}{}:
|
|
|
|
case <-s.done:
|
|
|
|
return
|
|
|
|
}
|
|
|
|
<-s.done
|
2014-08-30 02:32:41 +04:00
|
|
|
}
|
2014-08-29 03:41:42 +04:00
|
|
|
|
2016-08-13 22:41:04 +03:00
|
|
|
// Stop stops the server gracefully, and shuts down the running goroutine.
|
|
|
|
// Stop should be called after a Start(s), otherwise it will block forever.
|
|
|
|
// When stopping leader, Stop transfers its leadership to one of its peers
|
|
|
|
// before stopping the server.
|
2017-08-07 10:48:05 +03:00
|
|
|
// Stop terminates the Server and performs any necessary finalization.
|
|
|
|
// Do and Process cannot be called after Stop has been invoked.
|
2016-08-13 22:41:04 +03:00
|
|
|
func (s *EtcdServer) Stop() {
|
|
|
|
if err := s.TransferLeadership(); err != nil {
|
2018-04-16 08:40:44 +03:00
|
|
|
if lg := s.getLogger(); lg != nil {
|
|
|
|
lg.Warn("leadership transfer failed", zap.String("local-member-id", s.ID().String()), zap.Error(err))
|
|
|
|
} else {
|
|
|
|
plog.Warningf("%s failed to transfer leadership (%v)", s.ID(), err)
|
|
|
|
}
|
2016-08-13 22:41:04 +03:00
|
|
|
}
|
|
|
|
s.HardStop()
|
|
|
|
}
|
|
|
|
|
2016-04-23 05:57:33 +03:00
|
|
|
// ReadyNotify returns a channel that will be closed when the server
|
|
|
|
// is ready to serve client requests
|
|
|
|
func (s *EtcdServer) ReadyNotify() <-chan struct{} { return s.readych }
|
|
|
|
|
2015-01-03 07:00:29 +03:00
|
|
|
func (s *EtcdServer) stopWithDelay(d time.Duration, err error) {
|
2016-01-26 06:44:18 +03:00
|
|
|
select {
|
|
|
|
case <-time.After(d):
|
|
|
|
case <-s.done:
|
|
|
|
}
|
2015-01-03 07:00:29 +03:00
|
|
|
select {
|
|
|
|
case s.errorc <- err:
|
|
|
|
default:
|
|
|
|
}
|
2014-12-24 02:47:41 +03:00
|
|
|
}
|
|
|
|
|
2014-11-14 01:16:48 +03:00
|
|
|
// StopNotify returns a channel that receives a empty struct
|
|
|
|
// when the server is stopped.
|
|
|
|
func (s *EtcdServer) StopNotify() <-chan struct{} { return s.done }
|
|
|
|
|
2014-11-14 01:01:56 +03:00
|
|
|
func (s *EtcdServer) SelfStats() []byte { return s.stats.JSON() }
|
2014-10-17 03:56:58 +04:00
|
|
|
|
2014-10-17 05:39:55 +04:00
|
|
|
func (s *EtcdServer) LeaderStats() []byte {
|
2018-03-16 02:30:08 +03:00
|
|
|
lead := s.getLead()
|
2014-12-16 03:41:11 +03:00
|
|
|
if lead != uint64(s.id) {
|
|
|
|
return nil
|
|
|
|
}
|
2014-10-17 05:39:55 +04:00
|
|
|
return s.lstats.JSON()
|
2014-10-11 02:53:28 +04:00
|
|
|
}
|
|
|
|
|
2018-02-26 22:06:38 +03:00
|
|
|
func (s *EtcdServer) StoreStats() []byte { return s.v2store.JsonStats() }
|
2014-10-11 02:53:28 +04:00
|
|
|
|
2016-11-28 08:30:51 +03:00
|
|
|
func (s *EtcdServer) checkMembershipOperationPermission(ctx context.Context) error {
|
|
|
|
if s.authStore == nil {
|
2017-03-21 17:50:16 +03:00
|
|
|
// In the context of ordinary etcd process, s.authStore will never be nil.
|
2016-11-28 08:30:51 +03:00
|
|
|
// This branch is for handling cases in server_test.go
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// Note that this permission check is done in the API layer,
|
|
|
|
// so TOCTOU problem can be caused potentially in a schedule like this:
|
|
|
|
// update membership with user A -> revoke root role of A -> apply membership change
|
|
|
|
// in the state machine layer
|
|
|
|
// However, both of membership change and role management requires the root privilege.
|
|
|
|
// So careful operation by admins can prevent the problem.
|
2016-11-21 07:47:29 +03:00
|
|
|
authInfo, err := s.AuthInfoFromCtx(ctx)
|
2016-11-28 08:30:51 +03:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
return s.AuthStore().IsAdminPermitted(authInfo)
|
|
|
|
}
|
|
|
|
|
2017-04-13 01:27:28 +03:00
|
|
|
func (s *EtcdServer) AddMember(ctx context.Context, memb membership.Member) ([]*membership.Member, error) {
|
2016-11-28 08:30:51 +03:00
|
|
|
if err := s.checkMembershipOperationPermission(ctx); err != nil {
|
2017-04-13 01:27:28 +03:00
|
|
|
return nil, err
|
2016-11-28 08:30:51 +03:00
|
|
|
}
|
|
|
|
|
2016-08-05 03:20:35 +03:00
|
|
|
if s.Cfg.StrictReconfigCheck {
|
|
|
|
// by default StrictReconfigCheck is enabled; reject new members if unhealthy
|
|
|
|
if !s.cluster.IsReadyToAddNewMember() {
|
2018-04-16 08:40:44 +03:00
|
|
|
if lg := s.getLogger(); lg != nil {
|
|
|
|
lg.Warn(
|
|
|
|
"rejecting member add request; not enough healthy members",
|
|
|
|
zap.String("local-member-id", s.ID().String()),
|
|
|
|
zap.String("requested-member-add", fmt.Sprintf("%+v", memb)),
|
|
|
|
zap.Error(ErrNotEnoughStartedMembers),
|
|
|
|
)
|
|
|
|
} else {
|
|
|
|
plog.Warningf("not enough started members, rejecting member add %+v", memb)
|
|
|
|
}
|
2017-04-13 01:27:28 +03:00
|
|
|
return nil, ErrNotEnoughStartedMembers
|
2016-08-05 03:20:35 +03:00
|
|
|
}
|
2018-04-16 08:40:44 +03:00
|
|
|
|
2016-08-05 03:20:35 +03:00
|
|
|
if !isConnectedFullySince(s.r.transport, time.Now().Add(-HealthInterval), s.ID(), s.cluster.Members()) {
|
2018-04-16 08:40:44 +03:00
|
|
|
if lg := s.getLogger(); lg != nil {
|
|
|
|
lg.Warn(
|
|
|
|
"rejecting member add request; local member has not been connected to all peers, reconfigure breaks active quorum",
|
|
|
|
zap.String("local-member-id", s.ID().String()),
|
|
|
|
zap.String("requested-member-add", fmt.Sprintf("%+v", memb)),
|
|
|
|
zap.Error(ErrUnhealthy),
|
|
|
|
)
|
|
|
|
} else {
|
|
|
|
plog.Warningf("not healthy for reconfigure, rejecting member add %+v", memb)
|
|
|
|
}
|
2017-04-13 01:27:28 +03:00
|
|
|
return nil, ErrUnhealthy
|
2016-08-05 03:20:35 +03:00
|
|
|
}
|
2015-09-10 09:45:34 +03:00
|
|
|
}
|
|
|
|
|
2014-10-08 03:19:51 +04:00
|
|
|
// TODO: move Member to protobuf type
|
2014-10-01 02:14:44 +04:00
|
|
|
b, err := json.Marshal(memb)
|
|
|
|
if err != nil {
|
2017-04-13 01:27:28 +03:00
|
|
|
return nil, err
|
2014-10-01 02:14:44 +04:00
|
|
|
}
|
2014-09-23 23:02:44 +04:00
|
|
|
cc := raftpb.ConfChange{
|
|
|
|
Type: raftpb.ConfChangeAddNode,
|
2014-10-31 03:31:14 +03:00
|
|
|
NodeID: uint64(memb.ID),
|
2014-10-01 02:14:44 +04:00
|
|
|
Context: b,
|
2014-09-19 23:36:19 +04:00
|
|
|
}
|
2014-09-23 08:59:13 +04:00
|
|
|
return s.configure(ctx, cc)
|
2014-09-19 23:36:19 +04:00
|
|
|
}
|
|
|
|
|
2017-04-13 01:27:28 +03:00
|
|
|
func (s *EtcdServer) RemoveMember(ctx context.Context, id uint64) ([]*membership.Member, error) {
|
2016-11-28 08:30:51 +03:00
|
|
|
if err := s.checkMembershipOperationPermission(ctx); err != nil {
|
2017-04-13 01:27:28 +03:00
|
|
|
return nil, err
|
2016-11-28 08:30:51 +03:00
|
|
|
}
|
|
|
|
|
2016-08-11 02:29:03 +03:00
|
|
|
// by default StrictReconfigCheck is enabled; reject removal if leads to quorum loss
|
|
|
|
if err := s.mayRemoveMember(types.ID(id)); err != nil {
|
2017-04-13 01:27:28 +03:00
|
|
|
return nil, err
|
2015-09-16 07:18:05 +03:00
|
|
|
}
|
|
|
|
|
2014-09-23 23:02:44 +04:00
|
|
|
cc := raftpb.ConfChange{
|
|
|
|
Type: raftpb.ConfChangeRemoveNode,
|
2014-09-19 23:36:19 +04:00
|
|
|
NodeID: id,
|
|
|
|
}
|
2014-09-23 08:59:13 +04:00
|
|
|
return s.configure(ctx, cc)
|
2014-09-19 23:36:19 +04:00
|
|
|
}
|
|
|
|
|
2016-08-11 02:29:03 +03:00
|
|
|
func (s *EtcdServer) mayRemoveMember(id types.ID) error {
|
|
|
|
if !s.Cfg.StrictReconfigCheck {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
if !s.cluster.IsReadyToRemoveMember(uint64(id)) {
|
2018-04-16 08:40:44 +03:00
|
|
|
if lg := s.getLogger(); lg != nil {
|
|
|
|
lg.Warn(
|
|
|
|
"rejecting member remove request; not enough healthy members",
|
|
|
|
zap.String("local-member-id", s.ID().String()),
|
|
|
|
zap.String("requested-member-remove-id", id.String()),
|
|
|
|
zap.Error(ErrNotEnoughStartedMembers),
|
|
|
|
)
|
|
|
|
} else {
|
|
|
|
plog.Warningf("not enough started members, rejecting remove member %s", id)
|
|
|
|
}
|
2016-08-11 02:29:03 +03:00
|
|
|
return ErrNotEnoughStartedMembers
|
|
|
|
}
|
|
|
|
|
|
|
|
// downed member is safe to remove since it's not part of the active quorum
|
|
|
|
if t := s.r.transport.ActiveSince(id); id != s.ID() && t.IsZero() {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// protect quorum if some members are down
|
|
|
|
m := s.cluster.Members()
|
|
|
|
active := numConnectedSince(s.r.transport, time.Now().Add(-HealthInterval), s.ID(), m)
|
|
|
|
if (active - 1) < 1+((len(m)-1)/2) {
|
2018-04-16 08:40:44 +03:00
|
|
|
if lg := s.getLogger(); lg != nil {
|
|
|
|
lg.Warn(
|
|
|
|
"rejecting member remove request; local member has not been connected to all peers, reconfigure breaks active quorum",
|
|
|
|
zap.String("local-member-id", s.ID().String()),
|
|
|
|
zap.String("requested-member-remove", id.String()),
|
|
|
|
zap.Int("active-peers", active),
|
|
|
|
zap.Error(ErrUnhealthy),
|
|
|
|
)
|
|
|
|
} else {
|
|
|
|
plog.Warningf("reconfigure breaks active quorum, rejecting remove member %s", id)
|
|
|
|
}
|
2016-08-11 02:29:03 +03:00
|
|
|
return ErrUnhealthy
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2017-04-13 01:27:28 +03:00
|
|
|
func (s *EtcdServer) UpdateMember(ctx context.Context, memb membership.Member) ([]*membership.Member, error) {
|
2016-11-28 08:30:51 +03:00
|
|
|
b, merr := json.Marshal(memb)
|
|
|
|
if merr != nil {
|
2017-04-13 01:27:28 +03:00
|
|
|
return nil, merr
|
2016-11-28 08:30:51 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
if err := s.checkMembershipOperationPermission(ctx); err != nil {
|
2017-04-13 01:27:28 +03:00
|
|
|
return nil, err
|
2014-11-11 22:46:49 +03:00
|
|
|
}
|
|
|
|
cc := raftpb.ConfChange{
|
|
|
|
Type: raftpb.ConfChangeUpdateNode,
|
|
|
|
NodeID: uint64(memb.ID),
|
|
|
|
Context: b,
|
|
|
|
}
|
|
|
|
return s.configure(ctx, cc)
|
|
|
|
}
|
|
|
|
|
2018-03-16 02:30:08 +03:00
|
|
|
func (s *EtcdServer) setCommittedIndex(v uint64) {
|
|
|
|
atomic.StoreUint64(&s.committedIndex, v)
|
|
|
|
}
|
2016-02-21 16:05:03 +03:00
|
|
|
|
2018-03-16 02:30:08 +03:00
|
|
|
func (s *EtcdServer) getCommittedIndex() uint64 {
|
|
|
|
return atomic.LoadUint64(&s.committedIndex)
|
|
|
|
}
|
2014-09-25 05:05:33 +04:00
|
|
|
|
2018-03-16 02:30:08 +03:00
|
|
|
func (s *EtcdServer) setAppliedIndex(v uint64) {
|
|
|
|
atomic.StoreUint64(&s.appliedIndex, v)
|
|
|
|
}
|
2018-01-19 21:28:43 +03:00
|
|
|
|
2018-03-16 02:30:08 +03:00
|
|
|
func (s *EtcdServer) getAppliedIndex() uint64 {
|
|
|
|
return atomic.LoadUint64(&s.appliedIndex)
|
|
|
|
}
|
2014-09-25 05:05:33 +04:00
|
|
|
|
2018-03-16 02:30:08 +03:00
|
|
|
func (s *EtcdServer) setTerm(v uint64) {
|
|
|
|
atomic.StoreUint64(&s.term, v)
|
|
|
|
}
|
2014-09-25 05:05:33 +04:00
|
|
|
|
2018-03-16 02:30:08 +03:00
|
|
|
func (s *EtcdServer) getTerm() uint64 {
|
|
|
|
return atomic.LoadUint64(&s.term)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *EtcdServer) setLead(v uint64) {
|
|
|
|
atomic.StoreUint64(&s.lead, v)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *EtcdServer) getLead() uint64 {
|
|
|
|
return atomic.LoadUint64(&s.lead)
|
|
|
|
}
|
|
|
|
|
|
|
|
// RaftStatusGetter represents etcd server and Raft progress.
|
|
|
|
type RaftStatusGetter interface {
|
|
|
|
ID() types.ID
|
|
|
|
Leader() types.ID
|
|
|
|
CommittedIndex() uint64
|
|
|
|
AppliedIndex() uint64
|
|
|
|
Term() uint64
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *EtcdServer) ID() types.ID { return s.id }
|
|
|
|
|
|
|
|
func (s *EtcdServer) Leader() types.ID { return types.ID(s.getLead()) }
|
|
|
|
|
|
|
|
func (s *EtcdServer) Lead() uint64 { return s.getLead() }
|
|
|
|
|
|
|
|
func (s *EtcdServer) CommittedIndex() uint64 { return s.getCommittedIndex() }
|
|
|
|
|
|
|
|
func (s *EtcdServer) AppliedIndex() uint64 { return s.getAppliedIndex() }
|
|
|
|
|
|
|
|
func (s *EtcdServer) Term() uint64 { return s.getTerm() }
|
2015-01-06 07:20:11 +03:00
|
|
|
|
2017-04-13 01:27:28 +03:00
|
|
|
type confChangeResponse struct {
|
|
|
|
membs []*membership.Member
|
|
|
|
err error
|
|
|
|
}
|
|
|
|
|
2014-11-06 23:09:21 +03:00
|
|
|
// configure sends a configuration change through consensus and
|
|
|
|
// then waits for it to be applied to the server. It
|
|
|
|
// will block until the change is performed or there is an error.
|
2017-04-13 01:27:28 +03:00
|
|
|
func (s *EtcdServer) configure(ctx context.Context, cc raftpb.ConfChange) ([]*membership.Member, error) {
|
2014-12-16 03:07:47 +03:00
|
|
|
cc.ID = s.reqIDGen.Next()
|
2014-10-12 04:35:58 +04:00
|
|
|
ch := s.w.Register(cc.ID)
|
2018-04-16 08:40:44 +03:00
|
|
|
|
2015-08-12 23:38:43 +03:00
|
|
|
start := time.Now()
|
2015-01-16 01:39:30 +03:00
|
|
|
if err := s.r.ProposeConfChange(ctx, cc); err != nil {
|
2014-10-12 04:35:58 +04:00
|
|
|
s.w.Trigger(cc.ID, nil)
|
2017-04-13 01:27:28 +03:00
|
|
|
return nil, err
|
2014-09-19 23:36:19 +04:00
|
|
|
}
|
2018-04-16 08:40:44 +03:00
|
|
|
|
2014-09-19 23:36:19 +04:00
|
|
|
select {
|
2014-10-16 20:49:26 +04:00
|
|
|
case x := <-ch:
|
2017-04-13 01:27:28 +03:00
|
|
|
if x == nil {
|
2018-04-16 08:40:44 +03:00
|
|
|
if lg := s.getLogger(); lg != nil {
|
|
|
|
lg.Panic("failed to configure")
|
|
|
|
} else {
|
|
|
|
plog.Panicf("configure trigger value should never be nil")
|
|
|
|
}
|
2014-10-16 20:49:26 +04:00
|
|
|
}
|
2017-04-13 01:27:28 +03:00
|
|
|
resp := x.(*confChangeResponse)
|
|
|
|
return resp.membs, resp.err
|
2018-04-16 08:40:44 +03:00
|
|
|
|
2014-09-19 23:36:19 +04:00
|
|
|
case <-ctx.Done():
|
2014-10-12 04:35:58 +04:00
|
|
|
s.w.Trigger(cc.ID, nil) // GC wait
|
2017-04-13 01:27:28 +03:00
|
|
|
return nil, s.parseProposeCtxErr(ctx.Err(), start)
|
2018-04-16 08:40:44 +03:00
|
|
|
|
2016-09-14 01:46:05 +03:00
|
|
|
case <-s.stopping:
|
2017-04-13 01:27:28 +03:00
|
|
|
return nil, ErrStopped
|
2014-09-19 23:36:19 +04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-09-16 04:35:02 +04:00
|
|
|
// sync proposes a SYNC request and is non-blocking.
|
|
|
|
// This makes no guarantee that the request will be proposed or performed.
|
2016-01-08 11:21:19 +03:00
|
|
|
// The request will be canceled after the given timeout.
|
2014-09-16 04:35:02 +04:00
|
|
|
func (s *EtcdServer) sync(timeout time.Duration) {
|
|
|
|
req := pb.Request{
|
|
|
|
Method: "SYNC",
|
2014-12-16 03:07:47 +03:00
|
|
|
ID: s.reqIDGen.Next(),
|
2014-09-16 04:35:02 +04:00
|
|
|
Time: time.Now().UnixNano(),
|
|
|
|
}
|
2014-10-11 16:41:00 +04:00
|
|
|
data := pbutil.MustMarshal(&req)
|
2014-09-16 04:35:02 +04:00
|
|
|
// There is no promise that node has leader when do SYNC request,
|
|
|
|
// so it uses goroutine to propose.
|
2017-04-01 03:59:37 +03:00
|
|
|
ctx, cancel := context.WithTimeout(s.ctx, timeout)
|
2016-09-14 01:46:05 +03:00
|
|
|
s.goAttach(func() {
|
2015-01-16 01:39:30 +03:00
|
|
|
s.r.Propose(ctx, data)
|
2014-09-16 04:35:02 +04:00
|
|
|
cancel()
|
2016-09-14 01:46:05 +03:00
|
|
|
})
|
2014-09-16 04:35:02 +04:00
|
|
|
}
|
|
|
|
|
2014-09-27 01:18:59 +04:00
|
|
|
// publish registers server information into the cluster. The information
|
2014-10-03 03:20:14 +04:00
|
|
|
// is the JSON representation of this server's member struct, updated with the
|
|
|
|
// static clientURLs of the server.
|
2014-09-27 01:18:59 +04:00
|
|
|
// The function keeps attempting to register until it succeeds,
|
|
|
|
// or its server is stopped.
|
2015-07-23 20:09:28 +03:00
|
|
|
func (s *EtcdServer) publish(timeout time.Duration) {
|
2014-10-03 02:48:58 +04:00
|
|
|
b, err := json.Marshal(s.attributes)
|
2014-09-27 01:18:59 +04:00
|
|
|
if err != nil {
|
2018-04-16 08:40:44 +03:00
|
|
|
if lg := s.getLogger(); lg != nil {
|
|
|
|
lg.Panic("failed to marshal JSON", zap.Error(err))
|
|
|
|
} else {
|
|
|
|
plog.Panicf("json marshal error: %v", err)
|
|
|
|
}
|
2014-09-27 01:18:59 +04:00
|
|
|
return
|
|
|
|
}
|
|
|
|
req := pb.Request{
|
|
|
|
Method: "PUT",
|
2016-04-07 22:02:37 +03:00
|
|
|
Path: membership.MemberAttributesStorePath(s.id),
|
2014-09-27 01:18:59 +04:00
|
|
|
Val: string(b),
|
|
|
|
}
|
|
|
|
|
|
|
|
for {
|
2017-04-01 03:59:37 +03:00
|
|
|
ctx, cancel := context.WithTimeout(s.ctx, timeout)
|
2014-09-27 01:18:59 +04:00
|
|
|
_, err := s.Do(ctx, req)
|
|
|
|
cancel()
|
|
|
|
switch err {
|
|
|
|
case nil:
|
2016-04-23 05:57:33 +03:00
|
|
|
close(s.readych)
|
2018-04-16 08:40:44 +03:00
|
|
|
if lg := s.getLogger(); lg != nil {
|
|
|
|
lg.Info(
|
|
|
|
"published local member to cluster",
|
|
|
|
zap.String("local-member-id", s.ID().String()),
|
|
|
|
zap.String("local-member-attributes", fmt.Sprintf("%+v", s.attributes)),
|
|
|
|
zap.String("cluster-id", s.cluster.ID().String()),
|
|
|
|
)
|
|
|
|
} else {
|
|
|
|
plog.Infof("published %+v to cluster %s", s.attributes, s.cluster.ID())
|
|
|
|
}
|
2014-09-27 01:18:59 +04:00
|
|
|
return
|
2018-04-16 08:40:44 +03:00
|
|
|
|
2014-09-27 01:18:59 +04:00
|
|
|
case ErrStopped:
|
2018-04-16 08:40:44 +03:00
|
|
|
if lg := s.getLogger(); lg != nil {
|
|
|
|
lg.Warn(
|
|
|
|
"stopped publish because server is stopped",
|
|
|
|
zap.String("local-member-id", s.ID().String()),
|
|
|
|
zap.String("local-member-attributes", fmt.Sprintf("%+v", s.attributes)),
|
|
|
|
zap.Error(err),
|
|
|
|
)
|
|
|
|
} else {
|
|
|
|
plog.Infof("aborting publish because server is stopped")
|
|
|
|
}
|
2014-09-27 01:18:59 +04:00
|
|
|
return
|
2018-04-16 08:40:44 +03:00
|
|
|
|
2014-09-27 01:18:59 +04:00
|
|
|
default:
|
2018-04-16 08:40:44 +03:00
|
|
|
if lg := s.getLogger(); lg != nil {
|
|
|
|
lg.Warn(
|
|
|
|
"failed to publish",
|
|
|
|
zap.String("local-member-id", s.ID().String()),
|
|
|
|
zap.String("local-member-attributes", fmt.Sprintf("%+v", s.attributes)),
|
|
|
|
zap.Error(err),
|
|
|
|
)
|
|
|
|
} else {
|
|
|
|
plog.Errorf("publish error: %v", err)
|
|
|
|
}
|
2014-09-27 01:18:59 +04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-01-24 02:55:19 +03:00
|
|
|
func (s *EtcdServer) sendMergedSnap(merged raftsnap.Message) {
|
2015-12-24 03:03:23 +03:00
|
|
|
atomic.AddInt64(&s.inflightSnapshots, 1)
|
|
|
|
|
2018-04-16 08:40:44 +03:00
|
|
|
lg := s.getLogger()
|
|
|
|
fields := []zapcore.Field{
|
|
|
|
zap.String("from", s.ID().String()),
|
|
|
|
zap.String("to", types.ID(merged.To).String()),
|
|
|
|
zap.Int64("bytes", merged.TotalSize),
|
|
|
|
zap.String("size", humanize.Bytes(uint64(merged.TotalSize))),
|
|
|
|
}
|
|
|
|
|
|
|
|
now := time.Now()
|
2015-12-24 03:03:23 +03:00
|
|
|
s.r.transport.SendSnapshot(merged)
|
2018-04-16 08:40:44 +03:00
|
|
|
if lg != nil {
|
|
|
|
lg.Info("sending merged snapshot", fields...)
|
|
|
|
}
|
|
|
|
|
2016-09-14 01:46:05 +03:00
|
|
|
s.goAttach(func() {
|
2015-12-24 03:03:23 +03:00
|
|
|
select {
|
|
|
|
case ok := <-merged.CloseNotify():
|
|
|
|
// delay releasing inflight snapshot for another 30 seconds to
|
|
|
|
// block log compaction.
|
|
|
|
// If the follower still fails to catch up, it is probably just too slow
|
|
|
|
// to catch up. We cannot avoid the snapshot cycle anyway.
|
|
|
|
if ok {
|
2015-12-30 00:03:12 +03:00
|
|
|
select {
|
|
|
|
case <-time.After(releaseDelayAfterSnapshot):
|
2016-09-14 01:46:05 +03:00
|
|
|
case <-s.stopping:
|
2015-12-30 00:03:12 +03:00
|
|
|
}
|
2015-12-24 03:03:23 +03:00
|
|
|
}
|
2018-04-16 08:40:44 +03:00
|
|
|
|
2015-12-24 03:03:23 +03:00
|
|
|
atomic.AddInt64(&s.inflightSnapshots, -1)
|
2018-04-16 08:40:44 +03:00
|
|
|
|
|
|
|
if lg != nil {
|
|
|
|
lg.Info("sent merged snapshot", append(fields, zap.Duration("took", time.Since(now)))...)
|
|
|
|
}
|
|
|
|
|
2016-09-14 01:46:05 +03:00
|
|
|
case <-s.stopping:
|
2018-04-16 08:40:44 +03:00
|
|
|
if lg != nil {
|
|
|
|
lg.Warn("canceled sending merged snapshot; server stopping", fields...)
|
|
|
|
}
|
2015-12-24 03:03:23 +03:00
|
|
|
return
|
|
|
|
}
|
2016-09-14 01:46:05 +03:00
|
|
|
})
|
2015-12-24 03:03:23 +03:00
|
|
|
}
|
|
|
|
|
2014-12-09 02:54:52 +03:00
|
|
|
// apply takes entries received from Raft (after it has been committed) and
|
|
|
|
// applies them to the current state of the EtcdServer.
|
|
|
|
// The given entries should not be empty.
|
2018-04-16 08:40:44 +03:00
|
|
|
func (s *EtcdServer) apply(
|
|
|
|
es []raftpb.Entry,
|
|
|
|
confState *raftpb.ConfState,
|
|
|
|
) (appliedt uint64, appliedi uint64, shouldStop bool) {
|
2014-10-11 17:58:39 +04:00
|
|
|
for i := range es {
|
|
|
|
e := es[i]
|
|
|
|
switch e.Type {
|
|
|
|
case raftpb.EntryNormal:
|
2016-04-20 10:31:58 +03:00
|
|
|
s.applyEntryNormal(&e)
|
2018-03-16 02:35:44 +03:00
|
|
|
s.setAppliedIndex(e.Index)
|
|
|
|
s.setTerm(e.Term)
|
2018-04-16 08:40:44 +03:00
|
|
|
|
2014-10-11 17:58:39 +04:00
|
|
|
case raftpb.EntryConfChange:
|
2017-05-02 22:07:49 +03:00
|
|
|
// set the consistent index of current executing entry
|
|
|
|
if e.Index > s.consistIndex.ConsistentIndex() {
|
|
|
|
s.consistIndex.setConsistentIndex(e.Index)
|
|
|
|
}
|
2014-10-11 17:58:39 +04:00
|
|
|
var cc raftpb.ConfChange
|
2014-10-11 16:41:00 +04:00
|
|
|
pbutil.MustUnmarshal(&cc, e.Data)
|
2015-12-24 02:51:00 +03:00
|
|
|
removedSelf, err := s.applyConfChange(cc, confState)
|
2017-05-02 22:07:49 +03:00
|
|
|
s.setAppliedIndex(e.Index)
|
2018-03-16 02:35:44 +03:00
|
|
|
s.setTerm(e.Term)
|
2017-03-15 21:04:25 +03:00
|
|
|
shouldStop = shouldStop || removedSelf
|
2017-04-13 01:27:28 +03:00
|
|
|
s.w.Trigger(cc.ID, &confChangeResponse{s.cluster.Members(), err})
|
2018-04-16 08:40:44 +03:00
|
|
|
|
2014-10-11 17:58:39 +04:00
|
|
|
default:
|
2018-04-16 08:40:44 +03:00
|
|
|
if lg := s.getLogger(); lg != nil {
|
|
|
|
lg.Panic(
|
|
|
|
"unknown entry type; must be either EntryNormal or EntryConfChange",
|
|
|
|
zap.String("type", e.Type.String()),
|
|
|
|
)
|
|
|
|
} else {
|
|
|
|
plog.Panicf("entry type should be either EntryNormal or EntryConfChange")
|
|
|
|
}
|
2014-10-11 17:58:39 +04:00
|
|
|
}
|
2018-03-16 02:35:44 +03:00
|
|
|
appliedi, appliedt = e.Index, e.Term
|
2014-10-11 17:58:39 +04:00
|
|
|
}
|
2017-03-15 21:04:25 +03:00
|
|
|
return appliedt, appliedi, shouldStop
|
2014-10-11 17:58:39 +04:00
|
|
|
}
|
|
|
|
|
2016-04-20 10:31:58 +03:00
|
|
|
// applyEntryNormal apples an EntryNormal type raftpb request to the EtcdServer
|
|
|
|
func (s *EtcdServer) applyEntryNormal(e *raftpb.Entry) {
|
2016-06-27 20:27:27 +03:00
|
|
|
shouldApplyV3 := false
|
|
|
|
if e.Index > s.consistIndex.ConsistentIndex() {
|
|
|
|
// set the consistent index of current executing entry
|
|
|
|
s.consistIndex.setConsistentIndex(e.Index)
|
|
|
|
shouldApplyV3 = true
|
|
|
|
}
|
|
|
|
|
2016-04-20 10:31:58 +03:00
|
|
|
// raft state machine may generate noop entry when leader confirmation.
|
|
|
|
// skip it in advance to avoid some potential bug in the future
|
|
|
|
if len(e.Data) == 0 {
|
|
|
|
select {
|
|
|
|
case s.forceVersionC <- struct{}{}:
|
|
|
|
default:
|
|
|
|
}
|
2016-10-06 00:24:03 +03:00
|
|
|
// promote lessor when the local member is leader and finished
|
|
|
|
// applying all entries from the last term.
|
|
|
|
if s.isLeader() {
|
|
|
|
s.lessor.Promote(s.Cfg.electionTimeout())
|
|
|
|
}
|
2016-04-20 10:31:58 +03:00
|
|
|
return
|
2014-08-31 02:27:43 +04:00
|
|
|
}
|
2016-03-01 04:05:28 +03:00
|
|
|
|
2016-04-20 10:31:58 +03:00
|
|
|
var raftReq pb.InternalRaftRequest
|
|
|
|
if !pbutil.MaybeUnmarshal(&raftReq, e.Data) { // backward compatible
|
|
|
|
var r pb.Request
|
2017-08-07 13:55:18 +03:00
|
|
|
rp := &r
|
|
|
|
pbutil.MustUnmarshal(rp, e.Data)
|
|
|
|
s.w.Trigger(r.ID, s.applyV2Request((*RequestV2)(rp)))
|
2016-04-20 10:31:58 +03:00
|
|
|
return
|
|
|
|
}
|
|
|
|
if raftReq.V2 != nil {
|
2017-08-07 13:55:18 +03:00
|
|
|
req := (*RequestV2)(raftReq.V2)
|
2016-04-20 10:31:58 +03:00
|
|
|
s.w.Trigger(req.ID, s.applyV2Request(req))
|
|
|
|
return
|
2016-03-01 04:05:28 +03:00
|
|
|
}
|
|
|
|
|
2016-04-20 10:31:58 +03:00
|
|
|
// do not re-apply applied entries.
|
2016-06-27 20:27:27 +03:00
|
|
|
if !shouldApplyV3 {
|
2016-04-20 10:31:58 +03:00
|
|
|
return
|
|
|
|
}
|
2016-05-17 09:20:50 +03:00
|
|
|
|
|
|
|
id := raftReq.ID
|
|
|
|
if id == 0 {
|
|
|
|
id = raftReq.Header.ID
|
|
|
|
}
|
|
|
|
|
2016-06-16 07:31:07 +03:00
|
|
|
var ar *applyResult
|
2016-07-08 09:29:37 +03:00
|
|
|
needResult := s.w.IsRegistered(id)
|
|
|
|
if needResult || !noSideEffect(&raftReq) {
|
|
|
|
if !needResult && raftReq.Txn != nil {
|
|
|
|
removeNeedlessRangeReqs(raftReq.Txn)
|
|
|
|
}
|
2016-06-16 07:31:07 +03:00
|
|
|
ar = s.applyV3.Apply(&raftReq)
|
|
|
|
}
|
|
|
|
|
|
|
|
if ar == nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2016-04-20 10:31:58 +03:00
|
|
|
if ar.err != ErrNoSpace || len(s.alarmStore.Get(pb.AlarmType_NOSPACE)) > 0 {
|
2016-05-17 09:20:50 +03:00
|
|
|
s.w.Trigger(id, ar)
|
2016-04-20 10:31:58 +03:00
|
|
|
return
|
2014-08-27 03:17:02 +04:00
|
|
|
}
|
2016-04-20 10:31:58 +03:00
|
|
|
|
2018-04-16 08:40:44 +03:00
|
|
|
if lg := s.getLogger(); lg != nil {
|
|
|
|
lg.Warn(
|
|
|
|
"message exceeded backend quota; raising alarm",
|
|
|
|
zap.Int64("quota-size-bytes", s.Cfg.QuotaBackendBytes),
|
|
|
|
zap.String("quota-size", humanize.Bytes(uint64(s.Cfg.QuotaBackendBytes))),
|
|
|
|
zap.Error(ar.err),
|
|
|
|
)
|
|
|
|
} else {
|
|
|
|
plog.Errorf("applying raft message exceeded backend quota")
|
|
|
|
}
|
|
|
|
|
2016-09-14 01:46:05 +03:00
|
|
|
s.goAttach(func() {
|
2016-04-20 10:31:58 +03:00
|
|
|
a := &pb.AlarmRequest{
|
|
|
|
MemberID: uint64(s.ID()),
|
|
|
|
Action: pb.AlarmRequest_ACTIVATE,
|
|
|
|
Alarm: pb.AlarmType_NOSPACE,
|
|
|
|
}
|
2017-07-26 00:24:19 +03:00
|
|
|
s.raftRequest(s.ctx, pb.InternalRaftRequest{Alarm: a})
|
2016-05-17 09:20:50 +03:00
|
|
|
s.w.Trigger(id, ar)
|
2016-09-14 01:46:05 +03:00
|
|
|
})
|
2014-08-26 22:37:07 +04:00
|
|
|
}
|
2014-08-27 22:54:29 +04:00
|
|
|
|
2015-04-19 21:08:03 +03:00
|
|
|
// applyConfChange applies a ConfChange to the server. It is only
|
|
|
|
// invoked with a ConfChange that has already passed through Raft
|
|
|
|
func (s *EtcdServer) applyConfChange(cc raftpb.ConfChange, confState *raftpb.ConfState) (bool, error) {
|
2015-05-13 03:22:06 +03:00
|
|
|
if err := s.cluster.ValidateConfigurationChange(cc); err != nil {
|
2014-10-16 20:49:26 +04:00
|
|
|
cc.NodeID = raft.None
|
2015-01-16 01:39:30 +03:00
|
|
|
s.r.ApplyConfChange(cc)
|
2014-11-14 10:03:34 +03:00
|
|
|
return false, err
|
2014-10-16 20:49:26 +04:00
|
|
|
}
|
2018-04-16 08:40:44 +03:00
|
|
|
|
|
|
|
lg := s.getLogger()
|
2015-01-16 01:39:30 +03:00
|
|
|
*confState = *s.r.ApplyConfChange(cc)
|
2014-10-01 02:14:44 +04:00
|
|
|
switch cc.Type {
|
|
|
|
case raftpb.ConfChangeAddNode:
|
2016-04-07 22:02:37 +03:00
|
|
|
m := new(membership.Member)
|
2014-10-23 03:15:37 +04:00
|
|
|
if err := json.Unmarshal(cc.Context, m); err != nil {
|
2018-04-16 08:40:44 +03:00
|
|
|
if lg != nil {
|
|
|
|
lg.Panic("failed to unmarshal member", zap.Error(err))
|
|
|
|
} else {
|
|
|
|
plog.Panicf("unmarshal member should never fail: %v", err)
|
|
|
|
}
|
2014-10-01 02:14:44 +04:00
|
|
|
}
|
2014-10-31 03:31:14 +03:00
|
|
|
if cc.NodeID != uint64(m.ID) {
|
2018-04-16 08:40:44 +03:00
|
|
|
if lg != nil {
|
|
|
|
lg.Panic(
|
|
|
|
"got different member ID",
|
|
|
|
zap.String("member-id-from-config-change-entry", types.ID(cc.NodeID).String()),
|
|
|
|
zap.String("member-id-from-message", types.ID(m.ID).String()),
|
|
|
|
)
|
|
|
|
} else {
|
|
|
|
plog.Panicf("nodeID should always be equal to member ID")
|
|
|
|
}
|
2014-10-04 02:21:10 +04:00
|
|
|
}
|
2015-05-13 03:22:06 +03:00
|
|
|
s.cluster.AddMember(m)
|
2016-06-01 07:12:41 +03:00
|
|
|
if m.ID != s.id {
|
2015-04-19 21:08:03 +03:00
|
|
|
s.r.transport.AddPeer(m.ID, m.PeerURLs)
|
2014-11-13 07:37:50 +03:00
|
|
|
}
|
2018-04-16 08:40:44 +03:00
|
|
|
|
2014-10-01 02:14:44 +04:00
|
|
|
case raftpb.ConfChangeRemoveNode:
|
2014-11-05 01:39:43 +03:00
|
|
|
id := types.ID(cc.NodeID)
|
2015-05-13 03:22:06 +03:00
|
|
|
s.cluster.RemoveMember(id)
|
2014-11-13 07:37:50 +03:00
|
|
|
if id == s.id {
|
2014-11-14 10:03:34 +03:00
|
|
|
return true, nil
|
2014-11-13 07:37:50 +03:00
|
|
|
}
|
2016-06-01 07:12:41 +03:00
|
|
|
s.r.transport.RemovePeer(id)
|
2018-04-16 08:40:44 +03:00
|
|
|
|
2014-11-11 22:46:49 +03:00
|
|
|
case raftpb.ConfChangeUpdateNode:
|
2016-04-07 22:02:37 +03:00
|
|
|
m := new(membership.Member)
|
2014-11-11 22:46:49 +03:00
|
|
|
if err := json.Unmarshal(cc.Context, m); err != nil {
|
2018-04-16 08:40:44 +03:00
|
|
|
if lg != nil {
|
|
|
|
lg.Panic("failed to unmarshal member", zap.Error(err))
|
|
|
|
} else {
|
|
|
|
plog.Panicf("unmarshal member should never fail: %v", err)
|
|
|
|
}
|
2014-11-11 22:46:49 +03:00
|
|
|
}
|
|
|
|
if cc.NodeID != uint64(m.ID) {
|
2018-04-16 08:40:44 +03:00
|
|
|
if lg != nil {
|
|
|
|
lg.Panic(
|
|
|
|
"got different member ID",
|
|
|
|
zap.String("member-id-from-config-change-entry", types.ID(cc.NodeID).String()),
|
|
|
|
zap.String("member-id-from-message", types.ID(m.ID).String()),
|
|
|
|
)
|
|
|
|
} else {
|
|
|
|
plog.Panicf("nodeID should always be equal to member ID")
|
|
|
|
}
|
2014-11-11 22:46:49 +03:00
|
|
|
}
|
2015-05-13 03:22:06 +03:00
|
|
|
s.cluster.UpdateRaftAttributes(m.ID, m.RaftAttributes)
|
2016-06-01 07:12:41 +03:00
|
|
|
if m.ID != s.id {
|
2015-04-19 21:08:03 +03:00
|
|
|
s.r.transport.UpdatePeer(m.ID, m.PeerURLs)
|
2014-11-13 07:37:50 +03:00
|
|
|
}
|
2014-10-16 20:49:26 +04:00
|
|
|
}
|
2014-11-14 10:03:34 +03:00
|
|
|
return false, nil
|
2014-10-16 20:49:26 +04:00
|
|
|
}
|
|
|
|
|
2014-09-17 05:18:45 +04:00
|
|
|
// TODO: non-blocking snapshot
|
2015-02-26 23:39:50 +03:00
|
|
|
func (s *EtcdServer) snapshot(snapi uint64, confState raftpb.ConfState) {
|
2018-02-26 22:06:38 +03:00
|
|
|
clone := s.v2store.Clone()
|
2016-08-23 07:34:48 +03:00
|
|
|
// commit kv to write metadata (for example: consistent index) to disk.
|
|
|
|
// KV().commit() updates the consistent index in backend.
|
|
|
|
// All operations that update consistent index must be called sequentially
|
|
|
|
// from applyAll function.
|
|
|
|
// So KV().Commit() cannot run in parallel with apply. It has to be called outside
|
|
|
|
// the go routine created below.
|
|
|
|
s.KV().Commit()
|
2015-02-26 23:39:50 +03:00
|
|
|
|
2016-09-14 01:46:05 +03:00
|
|
|
s.goAttach(func() {
|
2018-04-16 08:40:44 +03:00
|
|
|
lg := s.getLogger()
|
|
|
|
|
2015-02-26 23:39:50 +03:00
|
|
|
d, err := clone.SaveNoCopy()
|
|
|
|
// TODO: current store will never fail to do a snapshot
|
|
|
|
// what should we do if the store might fail?
|
|
|
|
if err != nil {
|
2018-04-16 08:40:44 +03:00
|
|
|
if lg != nil {
|
|
|
|
lg.Panic("failed to save v2 store", zap.Error(err))
|
|
|
|
} else {
|
|
|
|
plog.Panicf("store save should never fail: %v", err)
|
|
|
|
}
|
2015-02-26 23:39:50 +03:00
|
|
|
}
|
|
|
|
snap, err := s.r.raftStorage.CreateSnapshot(snapi, &confState, d)
|
|
|
|
if err != nil {
|
|
|
|
// the snapshot was done asynchronously with the progress of raft.
|
|
|
|
// raft might have already got a newer snapshot.
|
|
|
|
if err == raft.ErrSnapOutOfDate {
|
|
|
|
return
|
|
|
|
}
|
2018-04-16 08:40:44 +03:00
|
|
|
if lg != nil {
|
|
|
|
lg.Panic("failed to create snapshot", zap.Error(err))
|
|
|
|
} else {
|
|
|
|
plog.Panicf("unexpected create snapshot error %v", err)
|
|
|
|
}
|
2015-02-26 23:39:50 +03:00
|
|
|
}
|
2015-10-27 07:06:08 +03:00
|
|
|
// SaveSnap saves the snapshot and releases the locked wal files
|
|
|
|
// to the snapshot index.
|
2015-12-12 15:25:27 +03:00
|
|
|
if err = s.r.storage.SaveSnap(snap); err != nil {
|
2018-04-16 08:40:44 +03:00
|
|
|
if lg != nil {
|
|
|
|
lg.Panic("failed to save snapshot", zap.Error(err))
|
|
|
|
} else {
|
|
|
|
plog.Fatalf("save snapshot error: %v", err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if lg != nil {
|
|
|
|
lg.Info(
|
|
|
|
"saved snapshot",
|
|
|
|
zap.Uint64("snapshot-index", snap.Metadata.Index),
|
|
|
|
)
|
|
|
|
} else {
|
|
|
|
plog.Infof("saved snapshot at index %d", snap.Metadata.Index)
|
2015-01-23 21:50:43 +03:00
|
|
|
}
|
|
|
|
|
2016-06-16 18:57:02 +03:00
|
|
|
// When sending a snapshot, etcd will pause compaction.
|
|
|
|
// After receives a snapshot, the slow follower needs to get all the entries right after
|
|
|
|
// the snapshot sent to catch up. If we do not pause compaction, the log entries right after
|
|
|
|
// the snapshot sent might already be compacted. It happens when the snapshot takes long time
|
|
|
|
// to send and save. Pausing compaction avoids triggering a snapshot sending cycle.
|
|
|
|
if atomic.LoadInt64(&s.inflightSnapshots) != 0 {
|
2018-04-16 08:40:44 +03:00
|
|
|
if lg != nil {
|
|
|
|
lg.Info("skip compaction since there is an inflight snapshot")
|
|
|
|
} else {
|
|
|
|
plog.Infof("skip compaction since there is an inflight snapshot")
|
|
|
|
}
|
2016-06-16 18:57:02 +03:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2015-03-01 07:33:28 +03:00
|
|
|
// keep some in memory log entries for slow followers.
|
|
|
|
compacti := uint64(1)
|
|
|
|
if snapi > numberOfCatchUpEntries {
|
|
|
|
compacti = snapi - numberOfCatchUpEntries
|
|
|
|
}
|
|
|
|
err = s.r.raftStorage.Compact(compacti)
|
2015-02-26 23:39:50 +03:00
|
|
|
if err != nil {
|
|
|
|
// the compaction was done asynchronously with the progress of raft.
|
|
|
|
// raft log might already been compact.
|
|
|
|
if err == raft.ErrCompacted {
|
|
|
|
return
|
|
|
|
}
|
2018-04-16 08:40:44 +03:00
|
|
|
if lg != nil {
|
|
|
|
lg.Panic("failed to compact", zap.Error(err))
|
|
|
|
} else {
|
|
|
|
plog.Panicf("unexpected compaction error %v", err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if lg != nil {
|
|
|
|
lg.Info(
|
|
|
|
"compacted Raft logs",
|
|
|
|
zap.Uint64("compact-index", compacti),
|
|
|
|
)
|
|
|
|
} else {
|
|
|
|
plog.Infof("compacted raft log at %d", compacti)
|
2014-11-26 02:13:07 +03:00
|
|
|
}
|
2016-09-14 01:46:05 +03:00
|
|
|
})
|
2014-09-17 05:18:45 +04:00
|
|
|
}
|
|
|
|
|
2016-08-12 17:38:52 +03:00
|
|
|
// CutPeer drops messages to the specified peer.
|
|
|
|
func (s *EtcdServer) CutPeer(id types.ID) {
|
|
|
|
tr, ok := s.r.transport.(*rafthttp.Transport)
|
|
|
|
if ok {
|
|
|
|
tr.CutPeer(id)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// MendPeer recovers the message dropping behavior of the given peer.
|
|
|
|
func (s *EtcdServer) MendPeer(id types.ID) {
|
|
|
|
tr, ok := s.r.transport.(*rafthttp.Transport)
|
|
|
|
if ok {
|
|
|
|
tr.MendPeer(id)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-01-16 01:39:30 +03:00
|
|
|
func (s *EtcdServer) PauseSending() { s.r.pauseSending() }
|
2014-12-26 08:37:20 +03:00
|
|
|
|
2015-01-16 01:39:30 +03:00
|
|
|
func (s *EtcdServer) ResumeSending() { s.r.resumeSending() }
|
*: add cluster version and cluster version detection.
Cluster version is the min major.minor of all members in
the etcd cluster. Cluster version is set to the min version
that a etcd member is compatible with when first bootstrapp.
During a rolling upgrades, the cluster version will be updated
automatically.
For example:
```
Cluster [a:1, b:1 ,c:1] -> clusterVersion 1
update a -> 2, b -> 2
after a detection
Cluster [a:2, b:2 ,c:1] -> clusterVersion 1, since c is still 1
update c -> 2
after a detection
Cluster [a:2, b:2 ,c:2] -> clusterVersion 2
```
The API/raft component can utilize clusterVersion to determine if
it can accept a client request or a raft RPC.
We choose polling rather than pushing since we want to use the same
logic for cluster version detection and (TODO) cluster version checking.
Before a member actually joins a etcd cluster, it should check the version
of the cluster. Push does not work since the other members cannot push
version info to it before it actually joins. Moreover, we do not want our
raft RPC system (which is doing the heartbeat pushing) to coordinate cluster version.
2015-04-29 20:56:34 +03:00
|
|
|
|
|
|
|
func (s *EtcdServer) ClusterVersion() *semver.Version {
|
2015-05-13 03:22:06 +03:00
|
|
|
if s.cluster == nil {
|
2015-05-08 21:10:12 +03:00
|
|
|
return nil
|
|
|
|
}
|
2015-05-13 03:22:06 +03:00
|
|
|
return s.cluster.Version()
|
*: add cluster version and cluster version detection.
Cluster version is the min major.minor of all members in
the etcd cluster. Cluster version is set to the min version
that a etcd member is compatible with when first bootstrapp.
During a rolling upgrades, the cluster version will be updated
automatically.
For example:
```
Cluster [a:1, b:1 ,c:1] -> clusterVersion 1
update a -> 2, b -> 2
after a detection
Cluster [a:2, b:2 ,c:1] -> clusterVersion 1, since c is still 1
update c -> 2
after a detection
Cluster [a:2, b:2 ,c:2] -> clusterVersion 2
```
The API/raft component can utilize clusterVersion to determine if
it can accept a client request or a raft RPC.
We choose polling rather than pushing since we want to use the same
logic for cluster version detection and (TODO) cluster version checking.
Before a member actually joins a etcd cluster, it should check the version
of the cluster. Push does not work since the other members cannot push
version info to it before it actually joins. Moreover, we do not want our
raft RPC system (which is doing the heartbeat pushing) to coordinate cluster version.
2015-04-29 20:56:34 +03:00
|
|
|
}
|
|
|
|
|
2016-02-01 08:42:39 +03:00
|
|
|
// monitorVersions checks the member's version every monitorVersionInterval.
|
*: add cluster version and cluster version detection.
Cluster version is the min major.minor of all members in
the etcd cluster. Cluster version is set to the min version
that a etcd member is compatible with when first bootstrapp.
During a rolling upgrades, the cluster version will be updated
automatically.
For example:
```
Cluster [a:1, b:1 ,c:1] -> clusterVersion 1
update a -> 2, b -> 2
after a detection
Cluster [a:2, b:2 ,c:1] -> clusterVersion 1, since c is still 1
update c -> 2
after a detection
Cluster [a:2, b:2 ,c:2] -> clusterVersion 2
```
The API/raft component can utilize clusterVersion to determine if
it can accept a client request or a raft RPC.
We choose polling rather than pushing since we want to use the same
logic for cluster version detection and (TODO) cluster version checking.
Before a member actually joins a etcd cluster, it should check the version
of the cluster. Push does not work since the other members cannot push
version info to it before it actually joins. Moreover, we do not want our
raft RPC system (which is doing the heartbeat pushing) to coordinate cluster version.
2015-04-29 20:56:34 +03:00
|
|
|
// It updates the cluster version if all members agrees on a higher one.
|
|
|
|
// It prints out log if there is a member with a higher version than the
|
|
|
|
// local version.
|
|
|
|
func (s *EtcdServer) monitorVersions() {
|
|
|
|
for {
|
|
|
|
select {
|
2015-05-08 21:10:12 +03:00
|
|
|
case <-s.forceVersionC:
|
*: add cluster version and cluster version detection.
Cluster version is the min major.minor of all members in
the etcd cluster. Cluster version is set to the min version
that a etcd member is compatible with when first bootstrapp.
During a rolling upgrades, the cluster version will be updated
automatically.
For example:
```
Cluster [a:1, b:1 ,c:1] -> clusterVersion 1
update a -> 2, b -> 2
after a detection
Cluster [a:2, b:2 ,c:1] -> clusterVersion 1, since c is still 1
update c -> 2
after a detection
Cluster [a:2, b:2 ,c:2] -> clusterVersion 2
```
The API/raft component can utilize clusterVersion to determine if
it can accept a client request or a raft RPC.
We choose polling rather than pushing since we want to use the same
logic for cluster version detection and (TODO) cluster version checking.
Before a member actually joins a etcd cluster, it should check the version
of the cluster. Push does not work since the other members cannot push
version info to it before it actually joins. Moreover, we do not want our
raft RPC system (which is doing the heartbeat pushing) to coordinate cluster version.
2015-04-29 20:56:34 +03:00
|
|
|
case <-time.After(monitorVersionInterval):
|
2016-09-14 01:46:05 +03:00
|
|
|
case <-s.stopping:
|
2015-05-08 21:10:12 +03:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
if s.Leader() != s.ID() {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
2018-04-16 08:40:44 +03:00
|
|
|
v := decideClusterVersion(s.getLogger(), getVersions(s.getLogger(), s.cluster, s.id, s.peerRt))
|
2015-05-08 21:10:12 +03:00
|
|
|
if v != nil {
|
2016-01-08 11:21:19 +03:00
|
|
|
// only keep major.minor version for comparison
|
2015-05-08 21:10:12 +03:00
|
|
|
v = &semver.Version{
|
|
|
|
Major: v.Major,
|
|
|
|
Minor: v.Minor,
|
*: add cluster version and cluster version detection.
Cluster version is the min major.minor of all members in
the etcd cluster. Cluster version is set to the min version
that a etcd member is compatible with when first bootstrapp.
During a rolling upgrades, the cluster version will be updated
automatically.
For example:
```
Cluster [a:1, b:1 ,c:1] -> clusterVersion 1
update a -> 2, b -> 2
after a detection
Cluster [a:2, b:2 ,c:1] -> clusterVersion 1, since c is still 1
update c -> 2
after a detection
Cluster [a:2, b:2 ,c:2] -> clusterVersion 2
```
The API/raft component can utilize clusterVersion to determine if
it can accept a client request or a raft RPC.
We choose polling rather than pushing since we want to use the same
logic for cluster version detection and (TODO) cluster version checking.
Before a member actually joins a etcd cluster, it should check the version
of the cluster. Push does not work since the other members cannot push
version info to it before it actually joins. Moreover, we do not want our
raft RPC system (which is doing the heartbeat pushing) to coordinate cluster version.
2015-04-29 20:56:34 +03:00
|
|
|
}
|
2015-05-08 21:10:12 +03:00
|
|
|
}
|
*: add cluster version and cluster version detection.
Cluster version is the min major.minor of all members in
the etcd cluster. Cluster version is set to the min version
that a etcd member is compatible with when first bootstrapp.
During a rolling upgrades, the cluster version will be updated
automatically.
For example:
```
Cluster [a:1, b:1 ,c:1] -> clusterVersion 1
update a -> 2, b -> 2
after a detection
Cluster [a:2, b:2 ,c:1] -> clusterVersion 1, since c is still 1
update c -> 2
after a detection
Cluster [a:2, b:2 ,c:2] -> clusterVersion 2
```
The API/raft component can utilize clusterVersion to determine if
it can accept a client request or a raft RPC.
We choose polling rather than pushing since we want to use the same
logic for cluster version detection and (TODO) cluster version checking.
Before a member actually joins a etcd cluster, it should check the version
of the cluster. Push does not work since the other members cannot push
version info to it before it actually joins. Moreover, we do not want our
raft RPC system (which is doing the heartbeat pushing) to coordinate cluster version.
2015-04-29 20:56:34 +03:00
|
|
|
|
2015-05-08 21:10:12 +03:00
|
|
|
// if the current version is nil:
|
|
|
|
// 1. use the decided version if possible
|
|
|
|
// 2. or use the min cluster version
|
2015-05-13 03:22:06 +03:00
|
|
|
if s.cluster.Version() == nil {
|
2016-09-14 01:46:05 +03:00
|
|
|
verStr := version.MinClusterVersion
|
2015-05-08 21:10:12 +03:00
|
|
|
if v != nil {
|
2016-09-14 01:46:05 +03:00
|
|
|
verStr = v.String()
|
*: add cluster version and cluster version detection.
Cluster version is the min major.minor of all members in
the etcd cluster. Cluster version is set to the min version
that a etcd member is compatible with when first bootstrapp.
During a rolling upgrades, the cluster version will be updated
automatically.
For example:
```
Cluster [a:1, b:1 ,c:1] -> clusterVersion 1
update a -> 2, b -> 2
after a detection
Cluster [a:2, b:2 ,c:1] -> clusterVersion 1, since c is still 1
update c -> 2
after a detection
Cluster [a:2, b:2 ,c:2] -> clusterVersion 2
```
The API/raft component can utilize clusterVersion to determine if
it can accept a client request or a raft RPC.
We choose polling rather than pushing since we want to use the same
logic for cluster version detection and (TODO) cluster version checking.
Before a member actually joins a etcd cluster, it should check the version
of the cluster. Push does not work since the other members cannot push
version info to it before it actually joins. Moreover, we do not want our
raft RPC system (which is doing the heartbeat pushing) to coordinate cluster version.
2015-04-29 20:56:34 +03:00
|
|
|
}
|
2016-09-14 01:46:05 +03:00
|
|
|
s.goAttach(func() { s.updateClusterVersion(verStr) })
|
2015-05-08 21:10:12 +03:00
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
// update cluster version only if the decided version is greater than
|
|
|
|
// the current cluster version
|
2015-05-13 03:22:06 +03:00
|
|
|
if v != nil && s.cluster.Version().LessThan(*v) {
|
2016-09-14 01:46:05 +03:00
|
|
|
s.goAttach(func() { s.updateClusterVersion(v.String()) })
|
*: add cluster version and cluster version detection.
Cluster version is the min major.minor of all members in
the etcd cluster. Cluster version is set to the min version
that a etcd member is compatible with when first bootstrapp.
During a rolling upgrades, the cluster version will be updated
automatically.
For example:
```
Cluster [a:1, b:1 ,c:1] -> clusterVersion 1
update a -> 2, b -> 2
after a detection
Cluster [a:2, b:2 ,c:1] -> clusterVersion 1, since c is still 1
update c -> 2
after a detection
Cluster [a:2, b:2 ,c:2] -> clusterVersion 2
```
The API/raft component can utilize clusterVersion to determine if
it can accept a client request or a raft RPC.
We choose polling rather than pushing since we want to use the same
logic for cluster version detection and (TODO) cluster version checking.
Before a member actually joins a etcd cluster, it should check the version
of the cluster. Push does not work since the other members cannot push
version info to it before it actually joins. Moreover, we do not want our
raft RPC system (which is doing the heartbeat pushing) to coordinate cluster version.
2015-04-29 20:56:34 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2015-05-08 21:10:12 +03:00
|
|
|
|
|
|
|
func (s *EtcdServer) updateClusterVersion(ver string) {
|
2018-04-16 08:40:44 +03:00
|
|
|
lg := s.getLogger()
|
|
|
|
|
2015-05-13 03:22:06 +03:00
|
|
|
if s.cluster.Version() == nil {
|
2018-04-16 08:40:44 +03:00
|
|
|
if lg != nil {
|
|
|
|
lg.Info(
|
|
|
|
"setting up initial cluster version",
|
|
|
|
zap.String("cluster-version", version.Cluster(ver)),
|
|
|
|
)
|
|
|
|
} else {
|
|
|
|
plog.Infof("setting up the initial cluster version to %s", version.Cluster(ver))
|
|
|
|
}
|
2015-05-08 21:10:12 +03:00
|
|
|
} else {
|
2018-04-16 08:40:44 +03:00
|
|
|
if lg != nil {
|
|
|
|
lg.Info(
|
|
|
|
"updating cluster version",
|
|
|
|
zap.String("from", version.Cluster(s.cluster.Version().String())),
|
|
|
|
zap.String("to", version.Cluster(ver)),
|
|
|
|
)
|
|
|
|
} else {
|
|
|
|
plog.Infof("updating the cluster version from %s to %s", version.Cluster(s.cluster.Version().String()), version.Cluster(ver))
|
|
|
|
}
|
2015-05-08 21:10:12 +03:00
|
|
|
}
|
2018-04-16 08:40:44 +03:00
|
|
|
|
2015-05-08 21:10:12 +03:00
|
|
|
req := pb.Request{
|
|
|
|
Method: "PUT",
|
2016-04-12 21:37:22 +03:00
|
|
|
Path: membership.StoreClusterVersionKey(),
|
2015-05-08 21:10:12 +03:00
|
|
|
Val: ver,
|
|
|
|
}
|
2018-04-16 08:40:44 +03:00
|
|
|
|
2017-04-01 03:59:37 +03:00
|
|
|
ctx, cancel := context.WithTimeout(s.ctx, s.Cfg.ReqTimeout())
|
2015-05-08 21:10:12 +03:00
|
|
|
_, err := s.Do(ctx, req)
|
|
|
|
cancel()
|
2018-04-16 08:40:44 +03:00
|
|
|
|
2015-05-08 21:10:12 +03:00
|
|
|
switch err {
|
|
|
|
case nil:
|
2018-04-16 08:40:44 +03:00
|
|
|
if lg != nil {
|
|
|
|
lg.Info("cluster version is updated", zap.String("cluster-version", version.Cluster(ver)))
|
|
|
|
}
|
2015-05-08 21:10:12 +03:00
|
|
|
return
|
2018-04-16 08:40:44 +03:00
|
|
|
|
2015-05-08 21:10:12 +03:00
|
|
|
case ErrStopped:
|
2018-04-16 08:40:44 +03:00
|
|
|
if lg != nil {
|
|
|
|
lg.Warn("aborting cluster version update; server is stopped", zap.Error(err))
|
|
|
|
} else {
|
|
|
|
plog.Infof("aborting update cluster version because server is stopped")
|
|
|
|
}
|
2015-05-08 21:10:12 +03:00
|
|
|
return
|
2018-04-16 08:40:44 +03:00
|
|
|
|
2015-05-08 21:10:12 +03:00
|
|
|
default:
|
2018-04-16 08:40:44 +03:00
|
|
|
if lg != nil {
|
|
|
|
lg.Warn("failed to update cluster version", zap.Error(err))
|
|
|
|
} else {
|
|
|
|
plog.Errorf("error updating cluster version (%v)", err)
|
|
|
|
}
|
2015-05-08 21:10:12 +03:00
|
|
|
}
|
|
|
|
}
|
2015-08-12 23:38:43 +03:00
|
|
|
|
|
|
|
func (s *EtcdServer) parseProposeCtxErr(err error, start time.Time) error {
|
|
|
|
switch err {
|
|
|
|
case context.Canceled:
|
|
|
|
return ErrCanceled
|
2018-04-16 08:40:44 +03:00
|
|
|
|
2015-08-12 23:38:43 +03:00
|
|
|
case context.DeadlineExceeded:
|
2017-04-18 02:32:36 +03:00
|
|
|
s.leadTimeMu.RLock()
|
|
|
|
curLeadElected := s.leadElectedTime
|
|
|
|
s.leadTimeMu.RUnlock()
|
2016-05-12 22:34:03 +03:00
|
|
|
prevLeadLost := curLeadElected.Add(-2 * time.Duration(s.Cfg.ElectionTicks) * time.Duration(s.Cfg.TickMs) * time.Millisecond)
|
2015-08-12 23:38:43 +03:00
|
|
|
if start.After(prevLeadLost) && start.Before(curLeadElected) {
|
2015-08-14 01:46:21 +03:00
|
|
|
return ErrTimeoutDueToLeaderFail
|
2015-08-12 23:38:43 +03:00
|
|
|
}
|
2018-03-16 02:30:08 +03:00
|
|
|
lead := types.ID(s.getLead())
|
2015-08-26 01:14:41 +03:00
|
|
|
switch lead {
|
|
|
|
case types.ID(raft.None):
|
|
|
|
// TODO: return error to specify it happens because the cluster does not have leader now
|
|
|
|
case s.ID():
|
|
|
|
if !isConnectedToQuorumSince(s.r.transport, start, s.ID(), s.cluster.Members()) {
|
|
|
|
return ErrTimeoutDueToConnectionLost
|
|
|
|
}
|
|
|
|
default:
|
|
|
|
if !isConnectedSince(s.r.transport, start, lead) {
|
|
|
|
return ErrTimeoutDueToConnectionLost
|
|
|
|
}
|
|
|
|
}
|
2015-08-12 23:38:43 +03:00
|
|
|
return ErrTimeout
|
2018-04-16 08:40:44 +03:00
|
|
|
|
2015-08-12 23:38:43 +03:00
|
|
|
default:
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
2015-08-26 01:14:41 +03:00
|
|
|
|
2016-04-25 22:32:58 +03:00
|
|
|
func (s *EtcdServer) KV() mvcc.ConsistentWatchableKV { return s.kv }
|
2016-03-10 08:37:10 +03:00
|
|
|
func (s *EtcdServer) Backend() backend.Backend {
|
|
|
|
s.bemu.Lock()
|
|
|
|
defer s.bemu.Unlock()
|
|
|
|
return s.be
|
|
|
|
}
|
2016-03-08 11:31:48 +03:00
|
|
|
|
|
|
|
func (s *EtcdServer) AuthStore() auth.AuthStore { return s.authStore }
|
2016-03-26 01:14:32 +03:00
|
|
|
|
|
|
|
func (s *EtcdServer) restoreAlarms() error {
|
2016-06-15 08:45:09 +03:00
|
|
|
s.applyV3 = s.newApplierV3()
|
2016-03-26 01:14:32 +03:00
|
|
|
as, err := alarm.NewAlarmStore(s)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
s.alarmStore = as
|
|
|
|
if len(as.Get(pb.AlarmType_NOSPACE)) > 0 {
|
|
|
|
s.applyV3 = newApplierV3Capped(s.applyV3)
|
|
|
|
}
|
2017-08-16 23:28:55 +03:00
|
|
|
if len(as.Get(pb.AlarmType_CORRUPT)) > 0 {
|
|
|
|
s.applyV3 = newApplierV3Corrupt(s.applyV3)
|
|
|
|
}
|
2016-03-26 01:14:32 +03:00
|
|
|
return nil
|
|
|
|
}
|
2016-06-09 03:11:26 +03:00
|
|
|
|
2016-09-14 01:46:05 +03:00
|
|
|
// goAttach creates a goroutine on a given function and tracks it using
|
|
|
|
// the etcdserver waitgroup.
|
|
|
|
func (s *EtcdServer) goAttach(f func()) {
|
2016-10-12 21:51:48 +03:00
|
|
|
s.wgMu.RLock() // this blocks with ongoing close(s.stopping)
|
|
|
|
defer s.wgMu.RUnlock()
|
|
|
|
select {
|
|
|
|
case <-s.stopping:
|
2018-04-16 08:40:44 +03:00
|
|
|
if lg := s.getLogger(); lg != nil {
|
|
|
|
lg.Warn("server has stopped; skipping goAttach")
|
|
|
|
} else {
|
|
|
|
plog.Warning("server has stopped (skipping goAttach)")
|
|
|
|
}
|
2016-10-12 21:51:48 +03:00
|
|
|
return
|
|
|
|
default:
|
|
|
|
}
|
|
|
|
|
|
|
|
// now safe to add since waitgroup wait has not started yet
|
2016-09-14 01:46:05 +03:00
|
|
|
s.wg.Add(1)
|
|
|
|
go func() {
|
|
|
|
defer s.wg.Done()
|
|
|
|
f()
|
|
|
|
}()
|
|
|
|
}
|
2017-07-18 01:29:44 +03:00
|
|
|
|
|
|
|
func (s *EtcdServer) Alarms() []*pb.AlarmMember {
|
|
|
|
return s.alarmStore.Get(pb.AlarmType_NONE)
|
|
|
|
}
|