Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: support etag for declarative stores #3287

Merged
merged 3 commits into from
Jul 19, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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())
erka marked this conversation as resolved.
Show resolved Hide resolved
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
}
10 changes: 10 additions & 0 deletions internal/storage/fs/snapshot_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1740,6 +1740,16 @@ func (fis *FSWithoutIndexSuite) TestListAndGetRules() {
}
}

func (fis *FSWithoutIndexSuite) TestGetVersion() {
t := fis.T()
version, err := fis.store.GetVersion(context.Background(), storage.NewNamespace("production"))
require.NoError(t, err)
require.NotEmpty(t, version)
version, err = fis.store.GetVersion(context.Background(), storage.NewNamespace("unknown"))
require.Error(t, err)
require.Empty(t, version)
}

func TestFS_Empty_Features_File(t *testing.T) {
fs, _ := fs.Sub(testdata, "testdata/valid/empty_features")
ss, err := SnapshotFromFS(zaptest.NewLogger(t), fs)
Expand Down
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
Loading