Skip to content

Commit

Permalink
feat: support etag for declarative stores
Browse files Browse the repository at this point in the history
related flipt-io#3248

Signed-off-by: Roman Dmytrenko <[email protected]>
  • Loading branch information
erka committed Jul 18, 2024
1 parent b64891e commit 8f6556b
Show file tree
Hide file tree
Showing 7 changed files with 29 additions and 13 deletions.
1 change: 1 addition & 0 deletions go.work.sum
Original file line number Diff line number Diff line change
Expand Up @@ -605,6 +605,7 @@ google.golang.org/genproto/googleapis/rpc v0.0.0-20240415180920-8c6c420018be/go.
google.golang.org/genproto/googleapis/rpc v0.0.0-20240429193739-8cf5692501f6/go.mod h1:WtryC6hu0hhx87FDGxWCDptyssuo68sk10vYjF+T9fY=
google.golang.org/genproto/googleapis/rpc v0.0.0-20240528184218-531527333157/go.mod h1:EfXuqaE1J41VCDicxHzUDm+8rk+7ZdXzHV0IhO/I6s0=
google.golang.org/genproto/googleapis/rpc v0.0.0-20240610135401-a8a62080eff3/go.mod h1:EfXuqaE1J41VCDicxHzUDm+8rk+7ZdXzHV0IhO/I6s0=
google.golang.org/genproto/googleapis/rpc v0.0.0-20240624140628-dc46fd24d27d/go.mod h1:Ue6ibwXGpU+dqIcODieyLOcgj7z8+IcskoNIgZxtrFY=
google.golang.org/grpc v1.37.0/go.mod h1:NREThFqKR1f3iQ6oBuvc5LadQuXVGo9rkm5ZGrQdJfM=
google.golang.org/grpc v1.62.1/go.mod h1:IWTG0VlJLCh1SkC58F7np9ka9mx/WNkjl4PGJaiq+QE=
google.golang.org/grpc v1.63.2/go.mod h1:WAX/8DgncnokcFUldAxq7GeB5DXHDbMF+lLvDomNkRA=
Expand Down
2 changes: 1 addition & 1 deletion internal/common/store_mock.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ func (m *StoreMock) String() string {
}

func (m *StoreMock) GetVersion(ctx context.Context, ns storage.NamespaceRequest) (string, error) {
args := m.Called(ctx)
args := m.Called(ctx, ns)
return args.String(0), args.Error(1)
}

Expand Down
1 change: 1 addition & 0 deletions internal/ext/common.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ type Document struct {
Namespace string `yaml:"namespace,omitempty" json:"namespace,omitempty"`
Flags []*Flag `yaml:"flags,omitempty" json:"flags,omitempty"`
Segments []*Segment `yaml:"segments,omitempty" json:"segments,omitempty"`
Etag string `yaml:"-" json:"-"`
}

type Flag struct {
Expand Down
5 changes: 0 additions & 5 deletions internal/storage/fs/object/store.go
Original file line number Diff line number Diff line change
Expand Up @@ -161,8 +161,3 @@ func (s *SnapshotStore) getIndex(ctx context.Context) (*storagefs.FliptIndex, er
return idx, nil

}

func (s *SnapshotStore) GetVersion(ctx context.Context) (string, error) {
// TODO: implement
return "", nil
}
13 changes: 9 additions & 4 deletions internal/storage/fs/snapshot.go
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@ type namespace struct {
rollouts map[string]*flipt.Rollout
evalRules map[string][]*storage.EvaluationRule
evalRollouts map[string][]*storage.EvaluationRollout
etag string
}

func newNamespace(key, name string, created *timestamppb.Timestamp) *namespace {
Expand Down Expand Up @@ -226,6 +227,7 @@ func documentsFromFile(fi fs.File, opts SnapshotOption) ([]*ext.Document, error)
if doc.Namespace == "" {
doc.Namespace = "default"
}
doc.Etag = fmt.Sprintf("%x-%x", stat.ModTime().Unix(), stat.Size())
docs = append(docs, doc)
}

Expand Down Expand Up @@ -537,7 +539,7 @@ func (ss *Snapshot) addDoc(doc *ext.Document) error {

ns.evalRollouts[f.Key] = evalRollouts
}

ns.etag = doc.Etag
ss.ns[doc.Namespace] = ns

ss.evalDists = evalDists
Expand Down Expand Up @@ -860,7 +862,10 @@ func (ss *Snapshot) getNamespace(key string) (namespace, error) {
return *ns, nil
}

func (ss *Snapshot) GetVersion(context.Context, storage.NamespaceRequest) (string, error) {
// TODO: implement
return "", nil
func (ss *Snapshot) GetVersion(ctx context.Context, req storage.NamespaceRequest) (string, error) {
ns, err := ss.getNamespace(req.Namespace())
if err != nil {
return "", err
}
return ns.etag, nil
}
8 changes: 5 additions & 3 deletions internal/storage/fs/store.go
Original file line number Diff line number Diff line change
Expand Up @@ -316,7 +316,9 @@ func (s *Store) OrderRollouts(ctx context.Context, r *flipt.OrderRolloutsRequest
return ErrNotImplemented
}

func (s *Store) GetVersion(context.Context, storage.NamespaceRequest) (string, error) {
// TODO: implement
return "", nil
func (s *Store) GetVersion(ctx context.Context, ns storage.NamespaceRequest) (version string, err error) {
return version, s.viewer.View(ctx, ns.Reference, func(ss storage.ReadOnlyStore) error {
version, err = ss.GetVersion(ctx, ns)
return err
})
}
12 changes: 12 additions & 0 deletions internal/storage/fs/store_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -218,6 +218,18 @@ func TestGetEvaluationRollouts(t *testing.T) {
require.NoError(t, err)
}

func TestGetVersion(t *testing.T) {
storeMock := newSnapshotStoreMock()
ss := NewStore(storeMock)

ns := storage.NewNamespace("default")
storeMock.On("GetVersion", mock.Anything, ns).Return("x0-y1", nil)

version, err := ss.GetVersion(context.TODO(), ns)
require.NoError(t, err)
require.Equal(t, "x0-y1", version)
}

type snapshotStoreMock struct {
*common.StoreMock
}
Expand Down

0 comments on commit 8f6556b

Please sign in to comment.