Skip to content

Commit d6306e1

Browse files
authored
injectLocalDetails changes (#100)
* injectLocalDetails changes * add err check * fix sb.injectLocalDetails on StateRebuild * fix test
1 parent 3445df4 commit d6306e1

File tree

9 files changed

+70
-32
lines changed

9 files changed

+70
-32
lines changed

cmd/grpcserver/grpc.go

+2-25
Original file line numberDiff line numberDiff line change
@@ -4,23 +4,20 @@
44
package main
55

66
import (
7-
"bytes"
8-
"compress/gzip"
97
"context"
10-
"encoding/base64"
118
"fmt"
129
"net"
1310
"net/http"
1411
//nolint: gosec
1512
_ "net/http/pprof"
1613
"os"
1714
"os/signal"
18-
"runtime"
1915
"strconv"
2016
"syscall"
2117
"time"
2218

2319
"github.com/anyproto/any-sync/app"
20+
"github.com/anyproto/anytype-heart/util/debug"
2421
grpc_middleware "github.com/grpc-ecosystem/go-grpc-middleware"
2522
grpc_zap "github.com/grpc-ecosystem/go-grpc-middleware/logging/zap"
2623
grpc_prometheus "github.com/grpc-ecosystem/go-grpc-prometheus"
@@ -125,7 +122,7 @@ func main() {
125122
select {
126123
case <-doneCh:
127124
case <-time.After(defaultUnaryWarningAfter):
128-
log.With("method", info.FullMethod).With("in_progress", true).With("goroutines", base64GzippedStack()).With("total", defaultUnaryWarningAfter.Milliseconds()).Warnf("grpc unary request is taking too long")
125+
log.With("method", info.FullMethod).With("in_progress", true).With("goroutines", debug.StackCompact(true)).With("total", defaultUnaryWarningAfter.Milliseconds()).Warnf("grpc unary request is taking too long")
129126
}
130127
}()
131128
resp, err = handler(ctx, req)
@@ -294,23 +291,3 @@ func onNotLoggedInError(resp interface{}, rerr error) interface{} {
294291
}
295292
return resp
296293
}
297-
298-
func stackAllGoroutines() []byte {
299-
buf := make([]byte, 1024)
300-
for {
301-
n := runtime.Stack(buf, true)
302-
if n < len(buf) {
303-
return buf[:n]
304-
}
305-
buf = make([]byte, 2*len(buf))
306-
}
307-
}
308-
309-
func base64GzippedStack() string {
310-
var buf bytes.Buffer
311-
gz := gzip.NewWriter(&buf)
312-
_, _ = gz.Write(stackAllGoroutines())
313-
_ = gz.Close()
314-
315-
return base64.StdEncoding.EncodeToString(buf.Bytes())
316-
}

core/block/editor/smartblock/smartblock.go

+16-7
Original file line numberDiff line numberDiff line change
@@ -892,6 +892,15 @@ func (sb *smartBlock) injectLinksDetails(s *state.State) {
892892
}
893893

894894
func (sb *smartBlock) injectLocalDetails(s *state.State) error {
895+
if pbtypes.GetString(s.LocalDetails(), bundle.RelationKeyWorkspaceId.String()) == "" {
896+
wsId, err := sb.coreService.GetWorkspaceIdForObject(sb.Id())
897+
if wsId != "" {
898+
s.SetDetailAndBundledRelation(bundle.RelationKeyWorkspaceId, pbtypes.String(wsId))
899+
} else {
900+
log.With("objectID", sb.Id()).Errorf("injectLocalDetails empty workspace: %v", err)
901+
}
902+
}
903+
895904
if sb.objectStore == nil {
896905
return nil
897906
}
@@ -943,15 +952,11 @@ func (sb *smartBlock) injectLocalDetails(s *state.State) error {
943952
s.SetDetailAndBundledRelation(bundle.RelationKeyCreator, pbtypes.String(creator))
944953
}
945954

946-
s.SetDetailAndBundledRelation(bundle.RelationKeyCreatedDate, pbtypes.Float64(float64(createdDate)))
947-
}
948-
949-
if pbtypes.GetString(s.LocalDetails(), bundle.RelationKeyWorkspaceId.String()) == "" {
950-
wsId, _ := sb.coreService.GetWorkspaceIdForObject(sb.Id())
951-
if wsId != "" {
952-
s.SetDetailAndBundledRelation(bundle.RelationKeyWorkspaceId, pbtypes.String(wsId))
955+
if createdDate != 0 {
956+
s.SetDetailAndBundledRelation(bundle.RelationKeyCreatedDate, pbtypes.Float64(float64(createdDate)))
953957
}
954958
}
959+
955960
return nil
956961
}
957962

@@ -1022,6 +1027,10 @@ func (sb *smartBlock) StateRebuild(d state.Doc) (err error) {
10221027
return ErrIsDeleted
10231028
}
10241029
d.(*state.State).InjectDerivedDetails()
1030+
err = sb.injectLocalDetails(d.(*state.State))
1031+
if err != nil {
1032+
log.Errorf("failed to inject local details in StateRebuild: %v", err)
1033+
}
10251034
d.(*state.State).SetParent(sb.Doc.(*state.State))
10261035
// todo: make store diff
10271036
sb.execHooks(HookBeforeApply, ApplyInfo{State: d.(*state.State)})

core/block/editor/smartblock/smartblock_test.go

+1
Original file line numberDiff line numberDiff line change
@@ -105,6 +105,7 @@ func newFixture(t *testing.T) *fixture {
105105

106106
coreService := testMock.NewMockService(ctrl)
107107
coreService.EXPECT().ProfileID().Return("").AnyTimes()
108+
coreService.EXPECT().GetWorkspaceIdForObject(gomock.Any()).AnyTimes()
108109

109110
source := mockSource.NewMockSource(ctrl)
110111
source.EXPECT().Type().AnyTimes().Return(model.SmartBlockType_Page)

core/block/source/anytypeprofile.go

+4
Original file line numberDiff line numberDiff line change
@@ -86,3 +86,7 @@ func (s *anytypeProfile) GetFileKeysSnapshot() []*pb.ChangeFileKeys {
8686
func (s *anytypeProfile) PushChange(params PushChangeParams) (id string, err error) {
8787
return
8888
}
89+
90+
func (s *anytypeProfile) GetCreationInfo() (creator string, createdDate int64, err error) {
91+
return addr.AnytypeProfileId, 0, nil
92+
}

core/block/source/bundledobjecttype.go

+5
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ package source
33
import (
44
"context"
55

6+
"github.com/anyproto/anytype-heart/pkg/lib/localstore/addr"
67
"github.com/gogo/protobuf/types"
78

89
"github.com/anyproto/anytype-heart/core/block/editor/state"
@@ -90,3 +91,7 @@ func (v *bundledObjectType) Heads() []string {
9091
func (s *bundledObjectType) GetFileKeysSnapshot() []*pb.ChangeFileKeys {
9192
return nil
9293
}
94+
95+
func (s *bundledObjectType) GetCreationInfo() (creator string, createdDate int64, err error) {
96+
return addr.AnytypeProfileId, 0, nil
97+
}

core/block/source/bundledrelation.go

+4
Original file line numberDiff line numberDiff line change
@@ -97,3 +97,7 @@ func (v *bundledRelation) Heads() []string {
9797
func (s *bundledRelation) GetFileKeysSnapshot() []*pb.ChangeFileKeys {
9898
return nil
9999
}
100+
101+
func (s *bundledRelation) GetCreationInfo() (creator string, createdDate int64, err error) {
102+
return addr.AnytypeProfileId, 0, nil
103+
}

core/block/source/date.go

+4
Original file line numberDiff line numberDiff line change
@@ -114,3 +114,7 @@ func (v *date) Heads() []string {
114114
func (s *date) GetFileKeysSnapshot() []*pb.ChangeFileKeys {
115115
return nil
116116
}
117+
118+
func (s *date) GetCreationInfo() (creator string, createdDate int64, err error) {
119+
return s.coreService.ProfileID(), 0, nil
120+
}

pkg/lib/localstore/objectstore/update.go

+5
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import (
44
"errors"
55
"fmt"
66

7+
"github.com/anyproto/anytype-heart/util/debug"
78
"github.com/dgraph-io/badger/v3"
89
"github.com/gogo/protobuf/proto"
910
"github.com/gogo/protobuf/types"
@@ -212,6 +213,10 @@ func (s *dsObjectStore) updateDetails(txn noctxds.Txn, id string, oldDetails *mo
212213
return err
213214
}
214215

216+
if pbtypes.GetString(newDetails.Details, bundle.RelationKeyWorkspaceId.String()) == "" {
217+
log.With("objectID", id).With("stack", debug.StackCompact(false)).Warnf("workspaceId erased")
218+
}
219+
215220
if oldDetails.GetDetails().Equal(newDetails.GetDetails()) {
216221
return ErrDetailsNotChanged
217222
}

util/debug/debug.go

+29
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
package debug
2+
3+
import (
4+
"bytes"
5+
"compress/gzip"
6+
"encoding/base64"
7+
"runtime"
8+
)
9+
10+
func Stack(allGoroutines bool) []byte {
11+
buf := make([]byte, 1024)
12+
for {
13+
n := runtime.Stack(buf, allGoroutines)
14+
if n < len(buf) {
15+
return buf[:n]
16+
}
17+
buf = make([]byte, 2*len(buf))
18+
}
19+
}
20+
21+
// StackCompact returns base64 of gzipped stack
22+
func StackCompact(allGoroutines bool) string {
23+
var buf bytes.Buffer
24+
gz := gzip.NewWriter(&buf)
25+
_, _ = gz.Write(Stack(allGoroutines))
26+
_ = gz.Close()
27+
28+
return base64.StdEncoding.EncodeToString(buf.Bytes())
29+
}

0 commit comments

Comments
 (0)