From d581888ae0ccc1736f63f6be6763a89fb6b3ff07 Mon Sep 17 00:00:00 2001 From: Mikhail Iudin Date: Mon, 4 Mar 2024 15:00:11 +0100 Subject: [PATCH] GO-3010 Turn on fast building --- core/block/editor/state/change.go | 15 +++++++++++++-- core/block/editor/state/state.go | 17 +++++++++++++++-- core/block/source/source.go | 10 ++++++++-- core/debug/treearchive/treeimporter.go | 1 + core/history/history.go | 1 + docs/Debug.md | 5 +++-- 6 files changed, 41 insertions(+), 8 deletions(-) diff --git a/core/block/editor/state/change.go b/core/block/editor/state/change.go index 576418079..794cc60dc 100644 --- a/core/block/editor/state/change.go +++ b/core/block/editor/state/change.go @@ -143,8 +143,14 @@ func (s *State) Merge(s2 *State) *State { return s } +// ApplyChange used in tests only func (s *State) ApplyChange(changes ...*pb.ChangeContent) (err error) { - defer s.resetParentIdsCache() + alreadyEnabled := s.EnableParentIdsCache() + defer func() { + if !alreadyEnabled { + s.ResetParentIdsCache() + } + }() for _, ch := range changes { if err = s.applyChange(ch); err != nil { return @@ -175,7 +181,12 @@ func (s *State) GetAndUnsetFileKeys() (keys []pb.ChangeFileKeys) { // ApplyChangeIgnoreErr should be called with changes from the single pb.Change func (s *State) ApplyChangeIgnoreErr(changes ...*pb.ChangeContent) { - defer s.resetParentIdsCache() + alreadyEnabled := s.EnableParentIdsCache() + defer func() { + if !alreadyEnabled { + s.ResetParentIdsCache() + } + }() for _, ch := range changes { if err := s.applyChange(ch); err != nil { log.With("objectID", s.RootId()).Warnf("error while applying change %T: %v; ignore", ch.Value, err) diff --git a/core/block/editor/state/state.go b/core/block/editor/state/state.go index 208b4370b..85ab43686 100644 --- a/core/block/editor/state/state.go +++ b/core/block/editor/state/state.go @@ -340,11 +340,19 @@ func (s *State) PickParentOf(id string) (res simple.Block) { return } -func (s *State) resetParentIdsCache() { +func (s *State) ResetParentIdsCache() { s.parentIdsCache = nil s.isParentIdsCacheEnabled = false } +func (s *State) EnableParentIdsCache() bool { + if s.isParentIdsCacheEnabled { + return true + } + s.isParentIdsCacheEnabled = true + return false +} + func (s *State) getParentIdsCache() map[string]string { if s.parentIdsCache == nil { s.parentIdsCache = make(map[string]string) @@ -462,7 +470,12 @@ func ApplyStateFastOne(s *State) (msgs []simple.EventMessage, action undo.Action } func (s *State) apply(fast, one, withLayouts bool) (msgs []simple.EventMessage, action undo.Action, err error) { - defer s.resetParentIdsCache() + alreadyEnabled := s.EnableParentIdsCache() + defer func() { + if !alreadyEnabled { + s.ResetParentIdsCache() + } + }() if s.parent != nil && (s.parent.parent != nil || fast) { s.intermediateApply() if one { diff --git a/core/block/source/source.go b/core/block/source/source.go index cc6c8aed1..9958bcb53 100644 --- a/core/block/source/source.go +++ b/core/block/source/source.go @@ -207,6 +207,7 @@ func (s *source) Update(ot objecttree.ObjectTree) { // todo: check this one err := s.receiver.StateAppend(func(d state.Doc) (st *state.State, changes []*pb.ChangeContent, err error) { st, changes, sinceSnapshot, err := BuildStateFull(s.spaceID, d.(*state.State), ot, "") + defer st.ResetParentIdsCache() if prevSnapshot != s.lastSnapshotId { s.changesSinceSnapshot = sinceSnapshot } else { @@ -270,6 +271,7 @@ func (s *source) readDoc(receiver ChangeReceiver) (doc state.Doc, err error) { func (s *source) buildState() (doc state.Doc, err error) { st, _, changesAppliedSinceSnapshot, err := BuildState(s.spaceID, nil, s.ObjectTree) + defer st.ResetParentIdsCache() if err != nil { return } @@ -540,6 +542,7 @@ func BuildState(spaceId string, initState *state.State, ot objecttree.ReadableOb startId = ot.Root().Id } else { st = initState + st.EnableParentIdsCache() startId = st.ChangeId() } @@ -561,6 +564,7 @@ func BuildState(spaceId string, initState *state.State, ot objecttree.ReadableOb st = state.NewDoc(ot.Id(), nil).(*state.State) } st.SetChangeId(change.Id) + st.EnableParentIdsCache() return true } @@ -572,10 +576,10 @@ func BuildState(spaceId string, initState *state.State, ot objecttree.ReadableOb if st == nil { changesAppliedSinceSnapshot = 0 st = state.NewDocFromSnapshot(ot.Id(), model.Snapshot, state.WithChangeId(startId), state.WithInternalKey(uniqueKeyInternalKey)).(*state.State) - return true } else { st = st.NewState() } + st.EnableParentIdsCache() return true } if model.Snapshot != nil { @@ -605,7 +609,6 @@ func BuildState(spaceId string, initState *state.State, ot objecttree.ReadableOb return } -// BuildStateFull is deprecated, used in tests only, use BuildState instead func BuildStateFull(spaceId string, initState *state.State, ot objecttree.ReadableObjectTree, profileId string) (st *state.State, appliedContent []*pb.ChangeContent, changesAppliedSinceSnapshot int, err error) { var ( startId string @@ -618,6 +621,7 @@ func BuildStateFull(spaceId string, initState *state.State, ot objecttree.Readab } else { st = initState startId = st.ChangeId() + st.EnableParentIdsCache() } var lastMigrationVersion uint32 @@ -628,6 +632,7 @@ func BuildStateFull(spaceId string, initState *state.State, ot objecttree.Readab if change.Id == ot.Id() { st = state.NewDoc(ot.Id(), nil).(*state.State) st.SetChangeId(change.Id) + st.EnableParentIdsCache() return true } @@ -643,6 +648,7 @@ func BuildStateFull(spaceId string, initState *state.State, ot objecttree.Readab } else { st = st.NewState() } + st.EnableParentIdsCache() return true } if model.Snapshot != nil { diff --git a/core/debug/treearchive/treeimporter.go b/core/debug/treearchive/treeimporter.go index 178079b2e..a6ad40cd0 100644 --- a/core/debug/treearchive/treeimporter.go +++ b/core/debug/treearchive/treeimporter.go @@ -83,6 +83,7 @@ func (t *treeImporter) State(fullStateChain bool) (*state.State, error) { return nil, err } } + defer st.ResetParentIdsCache() if _, _, err = state.ApplyStateFast(st); err != nil { return nil, err diff --git a/core/history/history.go b/core/history/history.go index bbef2d5b8..766faff02 100644 --- a/core/history/history.go +++ b/core/history/history.go @@ -214,6 +214,7 @@ func (h *history) buildState(id domain.FullID, versionId string) (st *state.Stat } st, _, _, err = source.BuildState(id.SpaceID, nil, tree) + defer st.ResetParentIdsCache() if err != nil { return } diff --git a/docs/Debug.md b/docs/Debug.md index 8410b0a6d..32a6b6c5e 100644 --- a/docs/Debug.md +++ b/docs/Debug.md @@ -25,7 +25,7 @@ You can find all endpoints in `/debug` page. For example: http://localhost:6061/ In order to log mw gRPC requests/responses use `ANYTYPE_GRPC_LOG` env var: - `ANYTYPE_LOG_LEVEL="grpc=DEBUG" ANYTYPE_GRPC_LOG=1` - log only method names - `ANYTYPE_LOG_LEVEL="grpc=DEBUG" ANYTYPE_GRPC_LOG=2` - log method names + payloads for commands -- `ANYTYPE_LOG_LEVEL="grpc=DEBUG" ANYTYPE_GRPC_LOG=2` - log method names + payloads for commands&events +- `ANYTYPE_LOG_LEVEL="grpc=DEBUG" ANYTYPE_GRPC_LOG=3` - log method names + payloads for commands&events ### gRPC tracing 1. Run jaeger UI on the local machine: @@ -33,8 +33,9 @@ In order to log mw gRPC requests/responses use `ANYTYPE_GRPC_LOG` env var: 2. Run mw with `ANYTYPE_GRPC_TRACE` env var: - `ANYTYPE_GRPC_TRACE=1` - log only method names/times - `ANYTYPE_GRPC_TRACE=2` - log method names + payloads for commands -- `ANYTYPE_GRPC_TRACE=2` - log method names + payloads for commands&events +- `ANYTYPE_GRPC_TRACE=3` - log method names + payloads for commands&events 3. Open Jaeger UI at http://localhost:16686 +4. If you can't see anything use JAEGER_SAMPLER_TYPE="const" and JAEGER_SAMPLER_PARAM=1 env vars to force sampling ### Debug tree 1. You can use `cmd/debugtree.go` to perform different operations with tree exported in zip archive (`rpc DebugTree`)