diff options
-rw-r--r-- | cmd/praefect/main.go | 8 | ||||
-rw-r--r-- | cmd/praefect/subcmd_set_replication_factor_test.go | 13 | ||||
-rw-r--r-- | internal/praefect/assignment.go | 33 | ||||
-rw-r--r-- | internal/praefect/service/info/replication_factor.go | 4 |
4 files changed, 48 insertions, 10 deletions
diff --git a/cmd/praefect/main.go b/cmd/praefect/main.go index 156b89425..0e229b8fb 100644 --- a/cmd/praefect/main.go +++ b/cmd/praefect/main.go @@ -108,7 +108,6 @@ import ( "gitlab.com/gitlab-org/gitaly/internal/praefect/nodes/tracker" "gitlab.com/gitlab-org/gitaly/internal/praefect/protoregistry" "gitlab.com/gitlab-org/gitaly/internal/praefect/reconciler" - "gitlab.com/gitlab-org/gitaly/internal/praefect/service/info" "gitlab.com/gitlab-org/gitaly/internal/praefect/transactions" "gitlab.com/gitlab-org/gitaly/internal/version" "gitlab.com/gitlab-org/labkit/monitoring" @@ -239,7 +238,6 @@ func run(cfgs []starter.Config, conf config.Config) error { var rs datastore.RepositoryStore var csg datastore.ConsistentStoragesGetter var metricsCollectors []prometheus.Collector - var replicationFactorSetter info.ReplicationFactorSetter if conf.MemoryQueueEnabled { queue = datastore.NewMemoryReplicationEventQueue(conf) @@ -301,6 +299,7 @@ func run(cfgs []starter.Config, conf config.Config) error { return err } + assignmentStore := praefect.NewDisabledAssignmentStore(conf.StorageNames()) var ( healthChecker praefect.HealthChecker nodeSet praefect.NodeSet @@ -328,8 +327,7 @@ func run(cfgs []starter.Config, conf config.Config) error { } }() - assignmentStore := datastore.NewAssignmentStore(db, conf.StorageNames()) - replicationFactorSetter = assignmentStore + assignmentStore = datastore.NewAssignmentStore(db, conf.StorageNames()) router = praefect.NewPerRepositoryRouter( nodeSet.Connections(), @@ -383,7 +381,7 @@ func run(cfgs []starter.Config, conf config.Config) error { transactionManager, queue, rs, - replicationFactorSetter, + assignmentStore, protoregistry.GitalyProtoPreregistered, ) ) diff --git a/cmd/praefect/subcmd_set_replication_factor_test.go b/cmd/praefect/subcmd_set_replication_factor_test.go index 632ba0636..090da41ee 100644 --- a/cmd/praefect/subcmd_set_replication_factor_test.go +++ b/cmd/praefect/subcmd_set_replication_factor_test.go @@ -7,6 +7,7 @@ import ( "testing" "github.com/stretchr/testify/require" + "gitlab.com/gitlab-org/gitaly/internal/praefect" "gitlab.com/gitlab-org/gitaly/internal/praefect/config" "gitlab.com/gitlab-org/gitaly/internal/praefect/datastore" "gitlab.com/gitlab-org/gitaly/internal/praefect/service/info" @@ -19,6 +20,7 @@ func TestSetReplicationFactorSubcommand(t *testing.T) { for _, tc := range []struct { desc string args []string + store praefect.AssignmentStore error error stdout string }{ @@ -63,6 +65,12 @@ func TestSetReplicationFactorSubcommand(t *testing.T) { error: status.Error(codes.InvalidArgument, `set replication factor: repository "virtual-storage"/"non-existent" not found`), }, { + desc: "assignments are disabled", + args: []string{"-virtual-storage=virtual-storage", "-repository=relative-path", "-replication-factor=1"}, + store: praefect.NewDisabledAssignmentStore(nil), + error: status.Error(codes.Internal, `set replication factor: assignments are disabled`), + }, + { desc: "successfully set", args: []string{"-virtual-storage=virtual-storage", "-repository=relative-path", "-replication-factor=2"}, stdout: "current assignments: primary, secondary\n", @@ -74,7 +82,10 @@ func TestSetReplicationFactorSubcommand(t *testing.T) { db := getDB(t) - store := datastore.NewAssignmentStore(db, map[string][]string{"virtual-storage": {"primary", "secondary"}}) + store := tc.store + if tc.store == nil { + store = datastore.NewAssignmentStore(db, map[string][]string{"virtual-storage": {"primary", "secondary"}}) + } // create a repository record require.NoError(t, diff --git a/internal/praefect/assignment.go b/internal/praefect/assignment.go new file mode 100644 index 000000000..f0f38e5af --- /dev/null +++ b/internal/praefect/assignment.go @@ -0,0 +1,33 @@ +package praefect + +import ( + "context" + "errors" +) + +// AssignmentStore is the interface which Praefect uses to operate on repository assignments. +type AssignmentStore interface { + AssignmentGetter + // SetReplicationFactor sets a repository's replication factor to the desired value and returns the + // resulting assignments. + SetReplicationFactor(ctx context.Context, virtualStorage, relativePath string, replicationFactor int) ([]string, error) +} + +type disabledAssignments map[string][]string + +// NewDisabledAssignmentStore returns an assignments store that can be used if no +// database is configured. It returns every configured storage as assigned and +// errors when trying to set assignments. +func NewDisabledAssignmentStore(storages map[string][]string) AssignmentStore { + return disabledAssignments(storages) +} + +// GetHostAssigments simply returns all of the storages configured for the virtual storage. +func (storages disabledAssignments) GetHostAssignments(ctx context.Context, virtualStorage, relativePath string) ([]string, error) { + return storages[virtualStorage], nil +} + +// SetReplicationFactor errors when attempting to set assignments. +func (disabledAssignments) SetReplicationFactor(context.Context, string, string, int) ([]string, error) { + return nil, errors.New("assignments are disabled") +} diff --git a/internal/praefect/service/info/replication_factor.go b/internal/praefect/service/info/replication_factor.go index d7a508cb0..622c76a83 100644 --- a/internal/praefect/service/info/replication_factor.go +++ b/internal/praefect/service/info/replication_factor.go @@ -32,10 +32,6 @@ func (s *Server) SetReplicationFactor(ctx context.Context, req *gitalypb.SetRepl } func (s *Server) setReplicationFactor(ctx context.Context, req *gitalypb.SetReplicationFactorRequest) (*gitalypb.SetReplicationFactorResponse, error) { - if s.rfs == nil { - return nil, fmt.Errorf("setting replication factor is only possible when Praefect is ran with 'per_repository' elector") - } - storages, err := s.rfs.SetReplicationFactor(ctx, req.VirtualStorage, req.RelativePath, int(req.ReplicationFactor)) if err != nil { return nil, fmt.Errorf("set replication factor: %w", err) |