1
0
Fork 0
mirror of https://github.com/anyproto/any-sync.git synced 2025-06-08 05:57:03 +09:00

Add sync logger to reduce number of logs

This commit is contained in:
mcrakhman 2023-07-17 13:50:49 +02:00
parent 7049a884a7
commit a35d94a20a
No known key found for this signature in database
GPG key ID: DED12CFEF5B8396B
4 changed files with 50 additions and 11 deletions

View file

@ -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"`
}

View file

@ -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
}

View file

@ -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)

View file

@ -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),
)
}