diff --git a/Makefile b/Makefile index 106d375b4..4dade1d46 100644 --- a/Makefile +++ b/Makefile @@ -72,7 +72,7 @@ test-integration: test-race: @echo 'Running tests with race-detector...' - @ANYTYPE_LOG_NOGELF=1 go test -race github.com/anyproto/anytype-heart/... + @ANYTYPE_LOG_NOGELF=1 go test -count=1 -race github.com/anyproto/anytype-heart/... test-deps: @echo 'Generating test mocks...' diff --git a/core/block/editor/dataview/dataview.go b/core/block/editor/dataview/dataview.go index 54ef6e5b3..f1297ae92 100644 --- a/core/block/editor/dataview/dataview.go +++ b/core/block/editor/dataview/dataview.go @@ -2,8 +2,10 @@ package dataview import ( "context" + "errors" "fmt" + anystore "github.com/anyproto/any-store" "github.com/globalsign/mgo/bson" "github.com/google/uuid" @@ -21,7 +23,6 @@ import ( "github.com/anyproto/anytype-heart/pkg/lib/localstore/objectstore" "github.com/anyproto/anytype-heart/pkg/lib/logging" "github.com/anyproto/anytype-heart/pkg/lib/pb/model" - "github.com/anyproto/anytype-heart/util/badgerhelper" "github.com/anyproto/anytype-heart/util/pbtypes" "github.com/anyproto/anytype-heart/util/slice" ) @@ -428,7 +429,7 @@ func (d *sdataview) checkDVBlocks(info smartblock.ApplyInfo) (err error) { func (d *sdataview) injectActiveViews(info smartblock.ApplyInfo) (err error) { s := info.State views, err := d.objectStore.GetActiveViews(d.Id()) - if badgerhelper.IsNotFound(err) { + if errors.Is(err, anystore.ErrDocNotFound) { return nil } if err != nil { diff --git a/core/block/editor/dataview/dataview_test.go b/core/block/editor/dataview/dataview_test.go index 4d7208931..6f41b372b 100644 --- a/core/block/editor/dataview/dataview_test.go +++ b/core/block/editor/dataview/dataview_test.go @@ -5,7 +5,7 @@ import ( "fmt" "testing" - "github.com/dgraph-io/badger/v4" + anystore "github.com/anyproto/any-store" "github.com/stretchr/testify/assert" "github.com/stretchr/testify/mock" "github.com/stretchr/testify/require" @@ -128,7 +128,7 @@ func TestInjectActiveView(t *testing.T) { fx := newFixture(t) fx.store.EXPECT().GetActiveViews(mock.Anything).RunAndReturn(func(id string) (map[string]string, error) { assert.Equal(t, objId, id) - return nil, badger.ErrKeyNotFound + return nil, anystore.ErrDocNotFound }) info := getInfo() diff --git a/core/filestorage/filesync/stats_test.go b/core/filestorage/filesync/stats_test.go index 42ede9cef..32a844477 100644 --- a/core/filestorage/filesync/stats_test.go +++ b/core/filestorage/filesync/stats_test.go @@ -1,6 +1,9 @@ package filesync import ( + "encoding/json" + "fmt" + "os" "testing" "github.com/stretchr/testify/assert" @@ -91,6 +94,12 @@ func TestSpaceUsageUpdate(t *testing.T) { makeLimitUpdatedEvent(limit * 10), } - assert.Equal(t, wantEvents, fx.events) + if !assert.Equal(t, wantEvents, fx.events) { + m := json.NewEncoder(os.Stdout) + m.SetIndent("", " ") + m.Encode(wantEvents) + fmt.Println("---") + m.Encode(fx.events) + } }) } diff --git a/core/indexer/fulltext_test.go b/core/indexer/fulltext_test.go index fa3c32a0f..6e1d9035b 100644 --- a/core/indexer/fulltext_test.go +++ b/core/indexer/fulltext_test.go @@ -23,6 +23,7 @@ import ( "github.com/anyproto/anytype-heart/pb" "github.com/anyproto/anytype-heart/pkg/lib/bundle" coresb "github.com/anyproto/anytype-heart/pkg/lib/core/smartblock" + "github.com/anyproto/anytype-heart/pkg/lib/datastore" "github.com/anyproto/anytype-heart/pkg/lib/localstore/filestore" "github.com/anyproto/anytype-heart/pkg/lib/localstore/ftsearch" "github.com/anyproto/anytype-heart/pkg/lib/localstore/objectstore" @@ -53,7 +54,11 @@ func NewIndexerFixture(t *testing.T) *IndexerFixture { fileStore := filestore.New() + ds, err := datastore.NewInMemory() + require.NoError(t, err) + testApp := &app.App{} + testApp.Register(ds) testApp.Register(walletService) testApp.Register(objectStore.FTSearch()) diff --git a/core/indexer/reindex.go b/core/indexer/reindex.go index 242b14a26..bf5046741 100644 --- a/core/indexer/reindex.go +++ b/core/indexer/reindex.go @@ -7,8 +7,8 @@ import ( "strings" "time" + anystore "github.com/anyproto/any-store" "github.com/anyproto/any-sync/util/slice" - "github.com/dgraph-io/badger/v4" "github.com/globalsign/mgo/bson" "github.com/gogo/protobuf/types" "go.uber.org/zap" @@ -50,12 +50,12 @@ const ( func (i *indexer) buildFlags(spaceID string) (reindexFlags, error) { checksums, err := i.store.GetChecksums(spaceID) - if err != nil && !errors.Is(err, badger.ErrKeyNotFound) { + if err != nil && !errors.Is(err, anystore.ErrDocNotFound) { return reindexFlags{}, err } if checksums == nil { checksums, err = i.store.GetGlobalChecksums() - if err != nil && !errors.Is(err, badger.ErrKeyNotFound) { + if err != nil && !errors.Is(err, anystore.ErrDocNotFound) { return reindexFlags{}, err } diff --git a/metrics/service.go b/metrics/service.go index f41e09d29..c877940d3 100644 --- a/metrics/service.go +++ b/metrics/service.go @@ -51,6 +51,7 @@ type MetricsService interface { } type service struct { + startOnce *sync.Once lock sync.RWMutex appVersion string startVersion string @@ -72,6 +73,7 @@ func NewService() MetricsService { ctx := context.Background() ctx, cancel := context.WithCancel(ctx) return &service{ + startOnce: &sync.Once{}, clients: [1]*client{ inhouse: { aggregatableMap: make(map[string]SamplableEvent), @@ -84,9 +86,11 @@ func NewService() MetricsService { } func (s *service) InitWithKeys(inHouseKey string) { - s.lock.Lock() - defer s.lock.Unlock() - s.clients[inhouse].telemetry = anymetry.New(inHouseEndpoint, inHouseKey, true) + s.startOnce.Do(func() { + s.lock.Lock() + defer s.lock.Unlock() + s.clients[inhouse].telemetry = anymetry.New(inHouseEndpoint, inHouseKey, true) + }) } func (s *service) SetDeviceId(t string) { diff --git a/pkg/lib/localstore/objectstore/account_store.go b/pkg/lib/localstore/objectstore/account_store.go index c32b620ac..f5755fdf6 100644 --- a/pkg/lib/localstore/objectstore/account_store.go +++ b/pkg/lib/localstore/objectstore/account_store.go @@ -2,10 +2,8 @@ package objectstore import ( "encoding/json" - "errors" "fmt" - anystore "github.com/anyproto/any-store" "github.com/anyproto/any-sync/coordinator/coordinatorproto" "github.com/valyala/fastjson" ) @@ -43,9 +41,6 @@ func (s *dsObjectStore) SaveAccountStatus(status *coordinatorproto.SpaceStatusPa func (s *dsObjectStore) GetAccountStatus() (*coordinatorproto.SpaceStatusPayload, error) { doc, err := s.system.FindId(s.componentCtx, accountStatusKey) - if errors.Is(err, anystore.ErrDocNotFound) { - return &coordinatorproto.SpaceStatusPayload{}, nil - } if err != nil { return nil, fmt.Errorf("find account status: %w", err) } diff --git a/pkg/lib/localstore/objectstore/activeview.go b/pkg/lib/localstore/objectstore/activeview.go index 8939b7d5e..ed6a142b3 100644 --- a/pkg/lib/localstore/objectstore/activeview.go +++ b/pkg/lib/localstore/objectstore/activeview.go @@ -40,6 +40,9 @@ func (s *dsObjectStore) SetActiveView(objectId, blockId, viewId string) error { // GetActiveViews returns a map of activeViews by block ids func (s *dsObjectStore) GetActiveViews(objectId string) (map[string]string, error) { doc, err := s.activeViews.FindId(s.componentCtx, objectId) + if errors.Is(err, anystore.ErrDocNotFound) { + return nil, nil + } if err != nil { return nil, fmt.Errorf("get active view: %w", err) } diff --git a/pkg/lib/localstore/objectstore/indexer_store.go b/pkg/lib/localstore/objectstore/indexer_store.go index 46d4a86af..59b56d0f3 100644 --- a/pkg/lib/localstore/objectstore/indexer_store.go +++ b/pkg/lib/localstore/objectstore/indexer_store.go @@ -75,9 +75,6 @@ func (s *dsObjectStore) RemoveIDsFromFullTextQueue(ids []string) error { func (s *dsObjectStore) GetChecksums(spaceID string) (*model.ObjectStoreChecksums, error) { doc, err := s.indexerChecksums.FindId(s.componentCtx, spaceID) - if errors.Is(err, anystore.ErrDocNotFound) { - return &model.ObjectStoreChecksums{}, nil - } if err != nil { return nil, fmt.Errorf("get checksums: %w", err) } diff --git a/util/badgerhelper/helper.go b/util/badgerhelper/helper.go index 4dcf0ebb6..dbf895e33 100644 --- a/util/badgerhelper/helper.go +++ b/util/badgerhelper/helper.go @@ -5,6 +5,7 @@ import ( "errors" "fmt" + anystore "github.com/anyproto/any-store" "github.com/dgraph-io/badger/v4" "github.com/gogo/protobuf/proto" ) @@ -99,7 +100,7 @@ func GetValueTxn[T any](txn *badger.Txn, key []byte, unmarshaler func([]byte) (T } func IsNotFound(err error) bool { - return errors.Is(err, badger.ErrKeyNotFound) + return errors.Is(err, badger.ErrKeyNotFound) || errors.Is(err, anystore.ErrDocNotFound) } func ViewTxnWithResult[T any](db *badger.DB, f func(txn *badger.Txn) (T, error)) (T, error) {