Skip to content

Commit

Permalink
simplify wal config logic
Browse files Browse the repository at this point in the history
  • Loading branch information
dhiaayachi committed Sep 23, 2024
1 parent 7213782 commit e0c1f2a
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 5 deletions.
11 changes: 6 additions & 5 deletions agent/consul/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
26 changes: 26 additions & 0 deletions agent/consul/server_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down

0 comments on commit e0c1f2a

Please sign in to comment.