mirror of
https://github.com/EchoVault/SugarDB.git
synced 2026-04-22 15:37:05 +08:00
Added election timeout config option for raft (#165)
Expose ElectionTimeout, HearbeatTimeout and CommitTimeout - @NicoleStrel
This commit is contained in:
@@ -19,8 +19,6 @@ import (
|
||||
"errors"
|
||||
"flag"
|
||||
"fmt"
|
||||
"github.com/echovault/sugardb/internal"
|
||||
"github.com/echovault/sugardb/internal/constants"
|
||||
"log"
|
||||
"os"
|
||||
"path"
|
||||
@@ -28,6 +26,9 @@ import (
|
||||
"strings"
|
||||
"time"
|
||||
|
||||
"github.com/echovault/sugardb/internal"
|
||||
"github.com/echovault/sugardb/internal/constants"
|
||||
|
||||
"gopkg.in/yaml.v3"
|
||||
)
|
||||
|
||||
@@ -55,6 +56,9 @@ type Config struct {
|
||||
EvictionPolicy string `json:"EvictionPolicy" yaml:"EvictionPolicy"`
|
||||
EvictionSample uint `json:"EvictionSample" yaml:"EvictionSample"`
|
||||
EvictionInterval time.Duration `json:"EvictionInterval" yaml:"EvictionInterval"`
|
||||
ElectionTimeout time.Duration `json:"ElectionTimeout" yaml:"ElectionTimeout"`
|
||||
HeartbeatTimeout time.Duration `json:"HeartbeatTimeout" yaml:"HeartbeatTimeout"`
|
||||
CommitTimeout time.Duration `json:"CommitTimeout" yaml:"CommitTimeout"`
|
||||
Modules []string `json:"Plugins" yaml:"Plugins"`
|
||||
DiscoveryPort uint16 `json:"DiscoveryPort" yaml:"DiscoveryPort"`
|
||||
RaftBindAddr string
|
||||
@@ -160,6 +164,9 @@ There is no limit by default.`, func(memory string) error {
|
||||
restoreAOF := flag.Bool("restore-aof", false, "This flag prompts the echovault to restore state from append-only logs. Only works in standalone mode. Lower priority than restoreSnapshot.")
|
||||
evictionSample := flag.Uint("eviction-sample", 20, "An integer specifying the number of keys to sample when checking for expired keys.")
|
||||
evictionInterval := flag.Duration("eviction-interval", 100*time.Millisecond, "The interval between each sampling of keys to evict.")
|
||||
electionTimeout := flag.Duration("election-timeout", 1000*time.Millisecond, "The maximum duration the leader will wait for followers to reach consensus on an election before starting a new election")
|
||||
heartbeatTimeout := flag.Duration("heartbeat-timeout", 1000*time.Millisecond, "The interval between heartbeats sent by the leader to followers. In other words, the time in candidate state without leader contact.")
|
||||
commitTimeout := flag.Duration("commit-timeout", 50*time.Millisecond, "The time the leader waits before sending a message to followers to confirm log entries are committed. May be delayed by up to 2x this value due to random staggering.")
|
||||
forwardCommand := flag.Bool(
|
||||
"forward-commands",
|
||||
false,
|
||||
@@ -217,6 +224,9 @@ It is a plain text value by default but you can provide a SHA256 hash by adding
|
||||
EvictionPolicy: evictionPolicy,
|
||||
EvictionSample: *evictionSample,
|
||||
EvictionInterval: *evictionInterval,
|
||||
ElectionTimeout: *electionTimeout,
|
||||
HeartbeatTimeout: *heartbeatTimeout,
|
||||
CommitTimeout: *commitTimeout,
|
||||
Modules: modules,
|
||||
DiscoveryPort: uint16(*discoveryPort),
|
||||
RaftBindAddr: raftBindAddr,
|
||||
|
||||
@@ -1,9 +1,10 @@
|
||||
package config
|
||||
|
||||
import (
|
||||
"time"
|
||||
|
||||
"github.com/echovault/sugardb/internal"
|
||||
"github.com/echovault/sugardb/internal/constants"
|
||||
"time"
|
||||
)
|
||||
|
||||
func DefaultConfig() Config {
|
||||
@@ -37,6 +38,9 @@ func DefaultConfig() Config {
|
||||
EvictionPolicy: constants.NoEviction,
|
||||
EvictionSample: 20,
|
||||
EvictionInterval: 100 * time.Millisecond,
|
||||
ElectionTimeout: 1000 * time.Millisecond,
|
||||
HeartbeatTimeout: 1000 * time.Millisecond,
|
||||
CommitTimeout: 50 * time.Millisecond,
|
||||
Modules: make([]string, 0),
|
||||
}
|
||||
}
|
||||
|
||||
@@ -18,15 +18,16 @@ import (
|
||||
"context"
|
||||
"errors"
|
||||
"fmt"
|
||||
"github.com/echovault/sugardb/internal"
|
||||
"github.com/echovault/sugardb/internal/config"
|
||||
"github.com/echovault/sugardb/internal/memberlist"
|
||||
"log"
|
||||
"net"
|
||||
"os"
|
||||
"path/filepath"
|
||||
"time"
|
||||
|
||||
"github.com/echovault/sugardb/internal"
|
||||
"github.com/echovault/sugardb/internal/config"
|
||||
"github.com/echovault/sugardb/internal/memberlist"
|
||||
|
||||
"github.com/hashicorp/raft"
|
||||
raftboltdb "github.com/hashicorp/raft-boltdb"
|
||||
)
|
||||
@@ -62,6 +63,9 @@ func (r *Raft) RaftInit(ctx context.Context) {
|
||||
raftConfig.LocalID = raft.ServerID(conf.ServerID)
|
||||
raftConfig.SnapshotThreshold = conf.SnapShotThreshold
|
||||
raftConfig.SnapshotInterval = conf.SnapshotInterval
|
||||
raftConfig.ElectionTimeout = conf.ElectionTimeout
|
||||
raftConfig.HeartbeatTimeout = conf.HeartbeatTimeout
|
||||
raftConfig.CommitTimeout = conf.CommitTimeout
|
||||
|
||||
var logStore raft.LogStore
|
||||
var stableStore raft.StableStore
|
||||
|
||||
+29
-1
@@ -16,10 +16,11 @@ package sugardb
|
||||
|
||||
import (
|
||||
"context"
|
||||
"time"
|
||||
|
||||
"github.com/echovault/sugardb/internal"
|
||||
"github.com/echovault/sugardb/internal/config"
|
||||
"github.com/echovault/sugardb/internal/constants"
|
||||
"time"
|
||||
)
|
||||
|
||||
// DefaultConfig returns the default configuration.
|
||||
@@ -315,6 +316,33 @@ func WithEvictionInterval(evictionInterval time.Duration) func(sugardb *SugarDB)
|
||||
}
|
||||
}
|
||||
|
||||
// WithElectionTimeout is an option to the NewSugarDB function that allows you to pass a
|
||||
// custom ElectionTimeout to SugarDB.
|
||||
// If not specified, SugarDB will use the default configuration from config.DefaultConfig().
|
||||
func WithElectionTimeout(electionTimeout time.Duration) func(sugardb *SugarDB) {
|
||||
return func(sugardb *SugarDB) {
|
||||
sugardb.config.ElectionTimeout = electionTimeout
|
||||
}
|
||||
}
|
||||
|
||||
// WithHeartbeatTimeout is an option to the NewSugarDB function that allows you to pass a
|
||||
// custom HeartbeatTimeout to SugarDB.
|
||||
// If not specified, SugarDB will use the default configuration from config.DefaultConfig().
|
||||
func WithHeartbeatTimeout(heartbeatTimeout time.Duration) func(sugardb *SugarDB) {
|
||||
return func(sugardb *SugarDB) {
|
||||
sugardb.config.HeartbeatTimeout = heartbeatTimeout
|
||||
}
|
||||
}
|
||||
|
||||
// WithHeartbeatTimeout is an option to the NewSugarDB function that allows you to pass a
|
||||
// custom HeartbeatTimeout to SugarDB.
|
||||
// If not specified, SugarDB will use the default configuration from config.DefaultConfig().
|
||||
func WithCommitTimeout(commitTimeout time.Duration) func(sugardb *SugarDB) {
|
||||
return func(sugardb *SugarDB) {
|
||||
sugardb.config.CommitTimeout = commitTimeout
|
||||
}
|
||||
}
|
||||
|
||||
// WithModules is an option to the NewSugarDB function that allows you to pass a
|
||||
// custom Modules to SugarDB.
|
||||
// If not specified, SugarDB will use the default configuration from config.DefaultConfig().
|
||||
|
||||
Reference in New Issue
Block a user