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

fix(pkger): lazy init the pkger store #18385

Merged
merged 1 commit into from
Jun 5, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
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 CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
1. [18331](https://github.com/influxdata/influxdb/pull/18331): Support organization name in addition to ID in DBRP operations
1. [18335](https://github.com/influxdata/influxdb/pull/18335): Disable failing when providing an unexpected error to influx CLI
1. [18345](https://github.com/influxdata/influxdb/pull/18345): Have influx delete cmd respect the config
1. [18385](https://github.com/influxdata/influxdb/pull/18385): Store initialization for pkger enforced on reads

### UI Improvements

Expand Down
24 changes: 14 additions & 10 deletions cmd/influxd/launcher/pkger_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -193,17 +193,11 @@ func TestLauncher_Pkger(t *testing.T) {
}

t.Run("managing pkg state with stacks", func(t *testing.T) {
t.Run("creating a stack", func(t *testing.T) {
_, cleanup := newStackFn(t, pkger.Stack{
OrgID: l.Org.ID,
Name: "first stack",
Description: "desc",
URLs: []string{"http://example.com"},
})
cleanup()
})

t.Run("list stacks", func(t *testing.T) {
stacks, err := svc.ListStacks(ctx, l.Org.ID, pkger.ListFilter{})
require.NoError(t, err)
require.Empty(t, stacks)

newStack1, cleanup1 := newStackFn(t, pkger.Stack{
Name: "first stack",
})
Expand Down Expand Up @@ -251,6 +245,16 @@ func TestLauncher_Pkger(t *testing.T) {
})
})

t.Run("creating a stack", func(t *testing.T) {
_, cleanup := newStackFn(t, pkger.Stack{
OrgID: l.Org.ID,
Name: "first stack",
Description: "desc",
URLs: []string{"http://example.com"},
})
cleanup()
})

t.Run("delete a stack", func(t *testing.T) {
t.Run("should delete the stack and all resources associated with it", func(t *testing.T) {
newStack, cleanup := newStackFn(t, pkger.Stack{})
Expand Down
22 changes: 20 additions & 2 deletions pkger/store.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"bytes"
"context"
"encoding/json"
"sync"
"time"

"github.com/influxdata/influxdb/v2"
Expand Down Expand Up @@ -42,6 +43,8 @@ type (
type StoreKV struct {
kvStore kv.Store
indexBase *kv.IndexStore

once sync.Once
}

var _ Store = (*StoreKV)(nil)
Expand Down Expand Up @@ -88,7 +91,7 @@ func (s *StoreKV) ListStacks(ctx context.Context, orgID influxdb.ID, f ListFilte
}

var stacks []Stack
err = s.kvStore.View(ctx, func(tx kv.Tx) error {
err = s.view(ctx, func(tx kv.Tx) error {
return s.indexBase.Find(ctx, tx, kv.FindOpts{
CaptureFn: func(key []byte, decodedVal interface{}) error {
stack, err := convertStackEntToStack(decodedVal.(*entStack))
Expand Down Expand Up @@ -165,7 +168,7 @@ func (s *StoreKV) listStacksByID(ctx context.Context, orgID influxdb.ID, stackID
// ReadStackByID reads a stack by the provided ID.
func (s *StoreKV) ReadStackByID(ctx context.Context, id influxdb.ID) (Stack, error) {
var stack Stack
err := s.kvStore.View(ctx, func(tx kv.Tx) error {
err := s.view(ctx, func(tx kv.Tx) error {
decodedEnt, err := s.indexBase.FindEnt(ctx, tx, kv.Entity{PK: kv.EncID(id)})
if err != nil {
return err
Expand Down Expand Up @@ -256,6 +259,21 @@ func (s *StoreKV) indexStoreBase(resource string) *kv.StoreBase {
return kv.NewStoreBase(resource, indexBucket, kv.EncUniqKey, kv.EncIDKey, kv.DecIndexID, decValToEntFn)
}

func (s *StoreKV) view(ctx context.Context, fn func(tx kv.Tx) error) error {
if err := s.lazyInit(ctx); err != nil {
return err
}
return s.kvStore.View(ctx, fn)
}

func (s *StoreKV) lazyInit(ctx context.Context) error {
var err error
s.once.Do(func() {
err = s.Init(ctx)
})
return err
}

func convertStackToEnt(stack Stack) (kv.Entity, error) {
idBytes, err := stack.ID.Encode()
if err != nil {
Expand Down