diff --git a/commonspace/config/config.go b/commonspace/config/config.go index cce4b548..a8864fef 100644 --- a/commonspace/config/config.go +++ b/commonspace/config/config.go @@ -7,5 +7,6 @@ type ConfigGetter interface { type Config struct { GCTTL int `yaml:"gcTTL"` SyncPeriod int `yaml:"syncPeriod"` + SyncLogPeriod int `yaml:"syncLogPeriod"` KeepTreeDataInMemory bool `yaml:"keepTreeDataInMemory"` } diff --git a/commonspace/headsync/diffsyncer.go b/commonspace/headsync/diffsyncer.go index a0539681..2acafa92 100644 --- a/commonspace/headsync/diffsyncer.go +++ b/commonspace/headsync/diffsyncer.go @@ -35,7 +35,7 @@ func newDiffSyncer(hs *headSync) DiffSyncer { peerManager: hs.peerManager, clientFactory: spacesyncproto.ClientFactoryFunc(spacesyncproto.NewDRPCSpaceSyncClient), credentialProvider: hs.credentialProvider, - log: log, + log: newSyncLogger(hs.log, hs.syncLogPeriod), syncStatus: hs.syncStatus, deletionState: hs.deletionState, } @@ -48,7 +48,7 @@ type diffSyncer struct { treeManager treemanager.TreeManager storage spacestorage.SpaceStorage clientFactory spacesyncproto.ClientFactory - log logger.CtxLogger + log syncLogger deletionState deletionstate.ObjectDeletionState credentialProvider credentialprovider.CredentialProvider syncStatus syncstatus.StatusUpdater @@ -100,7 +100,7 @@ func (d *diffSyncer) Sync(ctx context.Context) error { d.log.ErrorCtx(ctx, "can't sync with peer", zap.String("peer", p.Id()), zap.Error(err)) } } - d.log.InfoCtx(ctx, "diff done", zap.String("spaceId", d.spaceId), zap.Duration("dur", time.Since(st))) + d.log.DebugCtx(ctx, "diff done", zap.String("spaceId", d.spaceId), zap.Duration("dur", time.Since(st))) return nil } @@ -145,12 +145,7 @@ func (d *diffSyncer) syncWithPeer(ctx context.Context, p peer.Peer) (err error) if err != nil { return err } - d.log.Info("sync done:", zap.Int("newIds", len(newIds)), - zap.Int("changedIds", len(changedIds)), - zap.Int("removedIds", len(removedIds)), - zap.Int("already deleted ids", totalLen-len(existingIds)-len(missingIds)), - zap.String("peerId", p.Id()), - ) + d.log.logSyncDone(p.Id(), len(newIds), len(changedIds), len(removedIds), totalLen-len(existingIds)-len(missingIds)) return } diff --git a/commonspace/headsync/headsync.go b/commonspace/headsync/headsync.go index 18ed7357..b5477053 100644 --- a/commonspace/headsync/headsync.go +++ b/commonspace/headsync/headsync.go @@ -6,7 +6,7 @@ import ( "github.com/anyproto/any-sync/app" "github.com/anyproto/any-sync/app/ldiff" "github.com/anyproto/any-sync/app/logger" - config2 "github.com/anyproto/any-sync/commonspace/config" + "github.com/anyproto/any-sync/commonspace/config" "github.com/anyproto/any-sync/commonspace/credentialprovider" "github.com/anyproto/any-sync/commonspace/deletionstate" "github.com/anyproto/any-sync/commonspace/object/treemanager" @@ -48,6 +48,7 @@ type headSync struct { spaceId string spaceIsDeleted *atomic.Bool syncPeriod int + syncLogPeriod int periodicSync periodicsync.PeriodicSync storage spacestorage.SpaceStorage @@ -70,10 +71,11 @@ var createDiffSyncer = newDiffSyncer func (h *headSync) Init(a *app.App) (err error) { shared := a.MustComponent(spacestate.CName).(*spacestate.SpaceState) - cfg := a.MustComponent("config").(config2.ConfigGetter) + cfg := a.MustComponent("config").(config.ConfigGetter) h.spaceId = shared.SpaceId h.spaceIsDeleted = shared.SpaceIsDeleted h.syncPeriod = cfg.GetSpace().SyncPeriod + h.syncLogPeriod = cfg.GetSpace().SyncLogPeriod h.configuration = a.MustComponent(nodeconf.CName).(nodeconf.NodeConf) h.log = log.With(zap.String("spaceId", h.spaceId)) h.storage = a.MustComponent(spacestorage.CName).(spacestorage.SpaceStorage) diff --git a/commonspace/headsync/synclogger.go b/commonspace/headsync/synclogger.go new file mode 100644 index 00000000..1c13ec62 --- /dev/null +++ b/commonspace/headsync/synclogger.go @@ -0,0 +1,41 @@ +package headsync + +import ( + "time" + + "github.com/anyproto/any-sync/app/logger" + "go.uber.org/zap" +) + +type syncLogger struct { + lastLogged map[string]time.Time + logInterval time.Duration + logger.CtxLogger +} + +func newSyncLogger(log logger.CtxLogger, syncLogPeriodSecs int) syncLogger { + return syncLogger{ + lastLogged: map[string]time.Time{}, + logInterval: time.Duration(syncLogPeriodSecs) * time.Second, + CtxLogger: log, + } +} + +func (s syncLogger) logSyncDone(peerId string, newIds, changedIds, removedIds, deltedIds int) { + now := time.Now() + differentIds := newIds + changedIds + removedIds + deltedIds + // always logging if some ids are different or there are no log interval + if differentIds == 0 && s.logInterval > 0 { + lastLogged := s.lastLogged[peerId] + if now.Before(lastLogged.Add(s.logInterval)) { + return + } + } + s.lastLogged[peerId] = now + s.Info("sync done:", zap.Int("newIds", newIds), + zap.Int("changedIds", changedIds), + zap.Int("removedIds", removedIds), + zap.Int("already deleted ids", deltedIds), + zap.String("peerId", peerId), + ) +}