diff --git a/agent/config/builder.go b/agent/config/builder.go index 5c2eba5c55..dd1719bb1b 100644 --- a/agent/config/builder.go +++ b/agent/config/builder.go @@ -2857,7 +2857,7 @@ func (b *builder) raftLogStoreConfigVal(raw *RaftLogStoreRaw) consul.RaftLogStor cfg.Backend = stringValWithDefault(raw.Backend, consul.LogStoreBackendDefault) cfg.DisableLogCache = boolVal(raw.DisableLogCache) - cfg.Verification.Enabled = boolVal(raw.Verification.Enabled) + cfg.Verification.Enabled = boolValWithDefault(raw.Verification.Enabled, true) cfg.Verification.Interval = b.durationVal("raft_logstore.verification.interval", raw.Verification.Interval) cfg.BoltDB.NoFreelistSync = boolVal(raw.BoltDBConfig.NoFreelistSync) diff --git a/agent/consul/server.go b/agent/consul/server.go index 12386cc9df..4ec9c29610 100644 --- a/agent/consul/server.go +++ b/agent/consul/server.go @@ -79,7 +79,6 @@ import ( "github.com/hashicorp/consul/internal/tenancy" "github.com/hashicorp/consul/lib" "github.com/hashicorp/consul/lib/routine" - "github.com/hashicorp/consul/lib/stringslice" "github.com/hashicorp/consul/logging" "github.com/hashicorp/consul/proto-public/pbmesh/v2beta1/pbproxystate" "github.com/hashicorp/consul/proto-public/pbresource" @@ -747,7 +746,7 @@ func NewServer(config *Config, flat Deps, externalGRPCServer *grpc.Server, } // Initialize the Raft server. - if err := s.setupRaft(stringslice.Contains(flat.Experiments, CatalogResourceExperimentName)); err != nil { + if err := s.setupRaft(); err != nil { s.Shutdown() return nil, fmt.Errorf("Failed to start Raft: %v", err) } @@ -1109,7 +1108,7 @@ func (s *Server) connectCARootsMonitor(ctx context.Context) { } // setupRaft is used to setup and initialize Raft -func (s *Server) setupRaft(isCatalogResourceExperiment bool) error { +func (s *Server) setupRaft() error { // If we have an unclean exit then attempt to close the Raft store. defer func() { if s.raft == nil && s.raftStore != nil { @@ -1190,16 +1189,8 @@ func (s *Server) setupRaft(isCatalogResourceExperiment bool) error { return nil } // Only use WAL if there is no existing raft.db, even if it's enabled. - if s.config.LogStoreConfig.Backend == LogStoreBackendDefault && !boltFileExists && isCatalogResourceExperiment { + if s.config.LogStoreConfig.Backend == LogStoreBackendDefault && !boltFileExists { s.config.LogStoreConfig.Backend = LogStoreBackendWAL - if !s.config.LogStoreConfig.Verification.Enabled { - s.config.LogStoreConfig.Verification.Enabled = true - s.config.LogStoreConfig.Verification.Interval = 1 * time.Minute - } - if err = initWAL(); err != nil { - return err - } - } else if s.config.LogStoreConfig.Backend == LogStoreBackendWAL && !boltFileExists { if err = initWAL(); err != nil { return err } @@ -1230,11 +1221,14 @@ func (s *Server) setupRaft(isCatalogResourceExperiment bool) error { // See if log verification is enabled if s.config.LogStoreConfig.Verification.Enabled { + if s.config.LogStoreConfig.Verification.Interval == 0 { + s.config.LogStoreConfig.Verification.Interval = 1 * time.Minute + } mc := walmetrics.NewGoMetricsCollector([]string{"raft", "logstore", "verifier"}, nil, nil) reportFn := makeLogVerifyReportFn(s.logger.Named("raft.logstore.verifier")) - verifier := verifier.NewLogStore(log, isLogVerifyCheckpoint, reportFn, mc) - s.raftStore = verifier - log = verifier + v := verifier.NewLogStore(log, isLogVerifyCheckpoint, reportFn, mc) + s.raftStore = v + log = v } // Wrap the store in a LogCache to improve performance. diff --git a/agent/consul/server_test.go b/agent/consul/server_test.go index e685f25ca4..6b147d8eae 100644 --- a/agent/consul/server_test.go +++ b/agent/consul/server_test.go @@ -19,10 +19,6 @@ import ( "github.com/armon/go-metrics" "github.com/google/tcpproxy" - "github.com/hashicorp/go-hclog" - "github.com/hashicorp/go-uuid" - "github.com/hashicorp/memberlist" - "github.com/hashicorp/raft" "github.com/stretchr/testify/mock" "github.com/stretchr/testify/require" "golang.org/x/time/rate" @@ -30,6 +26,13 @@ import ( "google.golang.org/grpc/keepalive" "github.com/hashicorp/consul-net-rpc/net/rpc" + "github.com/hashicorp/go-hclog" + "github.com/hashicorp/go-uuid" + "github.com/hashicorp/memberlist" + "github.com/hashicorp/raft" + raftboltdb "github.com/hashicorp/raft-boltdb/v2" + raftwal "github.com/hashicorp/raft-wal" + "github.com/hashicorp/raft-wal/verifier" "github.com/hashicorp/consul/agent/connect" "github.com/hashicorp/consul/agent/consul/multilimiter" @@ -390,6 +393,92 @@ func TestServer_StartStop(t *testing.T) { } } +func TestServer_RaftBackend_Default(t *testing.T) { + t.Parallel() + // Start up a server and then stop it. + _, s1 := testServerWithConfig(t, func(config *Config) { + config.LogStoreConfig.Backend = LogStoreBackendDefault + config.LogStoreConfig.Verification.Enabled = false + }) + _, ok := s1.raftStore.(*raftwal.WAL) + defer func() { + if err := s1.Shutdown(); err != nil { + t.Fatalf("err: %v", err) + } + }() + require.True(t, ok) + +} + +func TestServer_RaftBackend_Verifier_WAL(t *testing.T) { + t.Parallel() + // Start up a server and then stop it. + _, s1 := testServerWithConfig(t, func(config *Config) { + config.LogStoreConfig.Backend = LogStoreBackendDefault + config.LogStoreConfig.Verification.Enabled = true + }) + _, ok := s1.raftStore.(*verifier.LogStore) + 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. + _, s1 := testServerWithConfig(t, func(config *Config) { + config.LogStoreConfig.Backend = LogStoreBackendWAL + config.LogStoreConfig.Verification.Enabled = false + }) + _, ok := s1.raftStore.(*raftwal.WAL) + defer func() { + if err := s1.Shutdown(); err != nil { + t.Fatalf("err: %v", err) + } + }() + require.True(t, ok) + +} + +func TestServer_RaftBackend_Verifier_BoltDB(t *testing.T) { + t.Parallel() + // Start up a server and then stop it. + _, s1 := testServerWithConfig(t, func(config *Config) { + config.LogStoreConfig.Backend = LogStoreBackendBoltDB + config.LogStoreConfig.Verification.Enabled = true + }) + _, ok := s1.raftStore.(*verifier.LogStore) + defer func() { + if err := s1.Shutdown(); err != nil { + t.Fatalf("err: %v", err) + } + }() + require.True(t, ok) + +} + +func TestServer_RaftBackend_BoltDB(t *testing.T) { + t.Parallel() + // Start up a server and then stop it. + _, s1 := testServerWithConfig(t, func(config *Config) { + config.LogStoreConfig.Backend = LogStoreBackendBoltDB + config.LogStoreConfig.Verification.Enabled = true + }) + store, ok := s1.raftStore.(*raftboltdb.BoltStore) + defer func() { + if err := s1.Shutdown(); err != nil { + t.Fatalf("err: %v", err) + } + }() + fmt.Printf("%v\n", store) + require.True(t, ok) + +} + func TestServer_fixupACLDatacenter(t *testing.T) { if testing.Short() { t.Skip("too slow for testing.Short")