2022-09-08 18:20:05 +00:00
|
|
|
package consensus
|
|
|
|
|
|
|
|
import (
|
2022-09-29 10:56:57 +00:00
|
|
|
"io/ioutil"
|
2022-09-29 12:56:22 +00:00
|
|
|
"path/filepath"
|
2022-09-13 21:41:06 +00:00
|
|
|
"time"
|
|
|
|
|
2022-09-08 18:20:05 +00:00
|
|
|
hraft "github.com/hashicorp/raft"
|
2022-09-13 17:05:48 +00:00
|
|
|
"golang.org/x/xerrors"
|
2022-09-13 21:41:06 +00:00
|
|
|
|
|
|
|
"github.com/filecoin-project/lotus/node/config"
|
2022-09-30 16:45:04 +00:00
|
|
|
"github.com/filecoin-project/lotus/node/repo"
|
2022-09-08 18:20:05 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
// ConfigKey is the default configuration key for holding this component's
|
|
|
|
// configuration section.
|
|
|
|
var configKey = "raft"
|
|
|
|
var envConfigKey = "cluster_raft"
|
|
|
|
|
|
|
|
// Configuration defaults
|
|
|
|
var (
|
2022-09-29 12:56:22 +00:00
|
|
|
DefaultDataSubFolder = "raft-cluster"
|
2022-09-08 18:20:05 +00:00
|
|
|
DefaultWaitForLeaderTimeout = 15 * time.Second
|
|
|
|
DefaultCommitRetries = 1
|
|
|
|
DefaultNetworkTimeout = 100 * time.Second
|
|
|
|
DefaultCommitRetryDelay = 200 * time.Millisecond
|
|
|
|
DefaultBackupsRotate = 6
|
2022-10-04 16:49:09 +00:00
|
|
|
DefaultDatastoreNamespace = "/r"
|
2022-09-08 18:20:05 +00:00
|
|
|
)
|
|
|
|
|
2022-09-29 10:56:57 +00:00
|
|
|
// ClusterRaftConfig allows to configure the Raft Consensus component for the node cluster.
|
|
|
|
type ClusterRaftConfig struct {
|
|
|
|
// config to enabled node cluster with raft consensus
|
|
|
|
ClusterModeEnabled bool
|
|
|
|
// will shutdown libp2p host on shutdown. Useful for testing
|
|
|
|
HostShutdown bool
|
|
|
|
// A folder to store Raft's data.
|
|
|
|
DataFolder string
|
|
|
|
// InitPeerset provides the list of initial cluster peers for new Raft
|
|
|
|
// peers (with no prior state). It is ignored when Raft was already
|
|
|
|
// initialized or when starting in staging mode.
|
2022-09-30 16:45:04 +00:00
|
|
|
InitPeerset []string
|
2022-09-29 10:56:57 +00:00
|
|
|
// LeaderTimeout specifies how long to wait for a leader before
|
|
|
|
// failing an operation.
|
|
|
|
WaitForLeaderTimeout time.Duration
|
|
|
|
// NetworkTimeout specifies how long before a Raft network
|
|
|
|
// operation is timed out
|
|
|
|
NetworkTimeout time.Duration
|
|
|
|
// CommitRetries specifies how many times we retry a failed commit until
|
|
|
|
// we give up.
|
|
|
|
CommitRetries int
|
|
|
|
// How long to wait between retries
|
|
|
|
CommitRetryDelay time.Duration
|
|
|
|
// BackupsRotate specifies the maximum number of Raft's DataFolder
|
|
|
|
// copies that we keep as backups (renaming) after cleanup.
|
|
|
|
BackupsRotate int
|
|
|
|
// Namespace to use when writing keys to the datastore
|
|
|
|
DatastoreNamespace string
|
|
|
|
|
|
|
|
// A Hashicorp Raft's configuration object.
|
|
|
|
RaftConfig *hraft.Config
|
|
|
|
|
|
|
|
// Tracing enables propagation of contexts across binary boundaries.
|
|
|
|
Tracing bool
|
|
|
|
}
|
|
|
|
|
|
|
|
func DefaultClusterRaftConfig() *ClusterRaftConfig {
|
|
|
|
var cfg ClusterRaftConfig
|
|
|
|
cfg.DataFolder = "" // empty so it gets omitted
|
2022-09-30 16:45:04 +00:00
|
|
|
cfg.InitPeerset = []string{}
|
2022-09-29 10:56:57 +00:00
|
|
|
cfg.WaitForLeaderTimeout = DefaultWaitForLeaderTimeout
|
|
|
|
cfg.NetworkTimeout = DefaultNetworkTimeout
|
|
|
|
cfg.CommitRetries = DefaultCommitRetries
|
|
|
|
cfg.CommitRetryDelay = DefaultCommitRetryDelay
|
|
|
|
cfg.BackupsRotate = DefaultBackupsRotate
|
|
|
|
cfg.DatastoreNamespace = DefaultDatastoreNamespace
|
|
|
|
cfg.RaftConfig = hraft.DefaultConfig()
|
|
|
|
|
|
|
|
// These options are imposed over any Default Raft Config.
|
|
|
|
cfg.RaftConfig.ShutdownOnRemove = false
|
|
|
|
cfg.RaftConfig.LocalID = "will_be_set_automatically"
|
|
|
|
|
|
|
|
// Set up logging
|
|
|
|
cfg.RaftConfig.LogOutput = ioutil.Discard
|
|
|
|
return &cfg
|
|
|
|
}
|
|
|
|
|
|
|
|
func NewClusterRaftConfig(userRaftConfig *config.UserRaftConfig) *ClusterRaftConfig {
|
|
|
|
var cfg ClusterRaftConfig
|
|
|
|
cfg.DataFolder = userRaftConfig.DataFolder
|
2022-09-30 16:45:04 +00:00
|
|
|
cfg.InitPeerset = userRaftConfig.InitPeersetMultiAddr
|
2022-09-29 10:56:57 +00:00
|
|
|
cfg.WaitForLeaderTimeout = time.Duration(userRaftConfig.WaitForLeaderTimeout)
|
|
|
|
cfg.NetworkTimeout = time.Duration(userRaftConfig.NetworkTimeout)
|
|
|
|
cfg.CommitRetries = userRaftConfig.CommitRetries
|
|
|
|
cfg.CommitRetryDelay = time.Duration(userRaftConfig.CommitRetryDelay)
|
|
|
|
cfg.BackupsRotate = userRaftConfig.BackupsRotate
|
|
|
|
cfg.DatastoreNamespace = userRaftConfig.DatastoreNamespace
|
|
|
|
|
|
|
|
// Keep this to be default hraft config for now
|
|
|
|
cfg.RaftConfig = hraft.DefaultConfig()
|
|
|
|
|
|
|
|
// These options are imposed over any Default Raft Config.
|
|
|
|
cfg.RaftConfig.ShutdownOnRemove = false
|
|
|
|
cfg.RaftConfig.LocalID = "will_be_set_automatically"
|
|
|
|
|
|
|
|
// Set up logging
|
|
|
|
cfg.RaftConfig.LogOutput = ioutil.Discard
|
2022-09-30 16:45:04 +00:00
|
|
|
|
2022-09-29 10:56:57 +00:00
|
|
|
return &cfg
|
|
|
|
|
|
|
|
}
|
2022-09-08 18:20:05 +00:00
|
|
|
|
|
|
|
//// Validate checks that this configuration has working values,
|
|
|
|
//// at least in appearance.
|
2022-09-29 10:56:57 +00:00
|
|
|
func ValidateConfig(cfg *ClusterRaftConfig) error {
|
2022-09-13 17:05:48 +00:00
|
|
|
if cfg.RaftConfig == nil {
|
|
|
|
return xerrors.Errorf("no hashicorp/raft.Config")
|
|
|
|
}
|
|
|
|
if cfg.WaitForLeaderTimeout <= 0 {
|
|
|
|
return xerrors.Errorf("wait_for_leader_timeout <= 0")
|
|
|
|
}
|
|
|
|
|
|
|
|
if cfg.NetworkTimeout <= 0 {
|
|
|
|
return xerrors.Errorf("network_timeout <= 0")
|
|
|
|
}
|
|
|
|
|
|
|
|
if cfg.CommitRetries < 0 {
|
|
|
|
return xerrors.Errorf("commit_retries is invalid")
|
|
|
|
}
|
|
|
|
|
|
|
|
if cfg.CommitRetryDelay <= 0 {
|
|
|
|
return xerrors.Errorf("commit_retry_delay is invalid")
|
|
|
|
}
|
|
|
|
|
|
|
|
if cfg.BackupsRotate <= 0 {
|
|
|
|
return xerrors.Errorf("backups_rotate should be larger than 0")
|
|
|
|
}
|
|
|
|
|
|
|
|
return hraft.ValidateConfig(cfg.RaftConfig)
|
|
|
|
}
|
2022-09-08 18:20:05 +00:00
|
|
|
|
2022-09-29 12:56:22 +00:00
|
|
|
// GetDataFolder returns the Raft data folder that we are using.
|
|
|
|
func (cfg *ClusterRaftConfig) GetDataFolder(repo repo.LockedRepo) string {
|
|
|
|
if cfg.DataFolder == "" {
|
2022-09-30 16:45:04 +00:00
|
|
|
return filepath.Join(repo.Path(), DefaultDataSubFolder)
|
2022-09-29 12:56:22 +00:00
|
|
|
}
|
2022-09-30 16:45:04 +00:00
|
|
|
return filepath.Join(repo.Path(), cfg.DataFolder)
|
2022-09-29 12:56:22 +00:00
|
|
|
}
|