simplify wal config logic

pull/21700/head
Dhia Ayachi 2024-09-23 11:48:43 -04:00
parent 7213782ab7
commit e0c1f2a417
No known key found for this signature in database
2 changed files with 32 additions and 5 deletions

View File

@ -1056,16 +1056,17 @@ func (s *Server) setupRaft() error {
return nil
}
// Default to WAL. Only use WAL if there is no existing raft.db, even if it's enabled. Log a warning otherwise
if s.config.LogStoreConfig.Backend == LogStoreBackendDefault && !boltFileExists {
s.config.LogStoreConfig.Backend = LogStoreBackendWAL
} else if s.config.LogStoreConfig.Backend == LogStoreBackendWAL || s.config.LogStoreConfig.Backend == LogStoreBackendDefault {
// Only use WAL when no boltdb file exists
useWal := (s.config.LogStoreConfig.Backend == LogStoreBackendWAL || s.config.LogStoreConfig.Backend == LogStoreBackendDefault) && !boltFileExists
if !useWal && (s.config.LogStoreConfig.Backend == LogStoreBackendWAL || s.config.LogStoreConfig.Backend == LogStoreBackendDefault) {
// User configured the new storage, but still has old raft.db. Warn
// them!
s.logger.Warn("BoltDB file raft.db found, IGNORING raft_logstore.backend which is set to 'wal'")
}
if s.config.LogStoreConfig.Backend == LogStoreBackendWAL && !boltFileExists {
// Default to WAL. Only use WAL if there is no existing raft.db, even if it's enabled. Log a warning otherwise
if useWal {
s.config.LogStoreConfig.Backend = LogStoreBackendWAL
if err = initWAL(); err != nil {
return err

View File

@ -425,6 +425,32 @@ func TestServer_RaftBackend_Verifier_WAL(t *testing.T) {
}
func TestServer_RaftBackend_WAL_WithExistingBoltDB(t *testing.T) {
t.Parallel()
dir := testutil.TempDir(t, "consul")
require.NoError(t, os.MkdirAll(dir+"/"+raftState, os.ModePerm))
dbFile, err := os.Create(dir + "/" + raftState + "raft.db")
require.NoError(t, err)
require.NoError(t, dbFile.Close())
// Start up a server and then stop it.
_, s1 := testServerWithConfig(t, func(config *Config) {
config.LogStoreConfig.Backend = LogStoreBackendWAL
config.LogStoreConfig.Verification.Enabled = false
config.DataDir = dir
})
_, ok := s1.raftStore.(*raftboltdb.BoltStore)
defer func() {
if err := s1.Shutdown(); err != nil {
t.Fatalf("err: %v", err)
}
}()
require.True(t, ok)
}
func TestServer_RaftBackend_WAL(t *testing.T) {
t.Parallel()
// Start up a server and then stop it.