maintenance: send logs to content log as well (#1496)

This commit is contained in:
Jarek Kowalski
2021-11-06 23:08:00 -07:00
committed by GitHub
parent e41c53b01b
commit 89edfbf257
6 changed files with 38 additions and 15 deletions

View File

@@ -91,13 +91,13 @@ func TestLogsMaintenance(t *testing.T) {
e.RunAndVerifyOutputLineCount(t, 5, "logs", "list")
e.RunAndExpectSuccess(t, "maintenance", "run")
e.RunAndVerifyOutputLineCount(t, 2, "logs", "list")
e.RunAndExpectSuccess(t, "maintenance", "set", "--max-retained-log-age=1ms")
e.RunAndVerifyOutputLineCount(t, 3, "logs", "list")
e.RunAndExpectSuccess(t, "maintenance", "set", "--max-retained-log-age=1ms")
e.RunAndVerifyOutputLineCount(t, 4, "logs", "list")
e.RunAndExpectSuccess(t, "maintenance", "run")
e.RunAndVerifyOutputLineCount(t, 0, "logs", "list")
e.RunAndVerifyOutputLineCount(t, 1, "logs", "list")
e.RunAndExpectSuccess(t, "maintenance", "set",
"--max-retained-log-age=22h",

View File

@@ -491,9 +491,12 @@ func (sm *SharedManager) release(ctx context.Context) error {
return errors.Wrap(sm.st.Close(ctx), "error closing storage")
}
// InternalLogger returns the internal logger.
func (sm *SharedManager) InternalLogger() logging.Logger {
return sm.internalLogger
// AlsoLogToContentLog wraps the provided content so that all logs are also sent to
// internal content log.
func (sm *SharedManager) AlsoLogToContentLog(ctx context.Context) context.Context {
sm.internalLogManager.enable()
return logging.AlsoLogTo(ctx, sm.log)
}
func (sm *SharedManager) shouldRefreshIndexes() bool {

View File

@@ -33,3 +33,22 @@ func WithLogger(ctx context.Context, l LoggerFactory) context.Context {
createLoggerForModule: l,
})
}
// loggerFactoryFromContext returns a LoggerFactory associated with current context.
func loggerFactoryFromContext(ctx context.Context) LoggerFactory {
v := ctx.Value(loggerCacheKey)
if v == nil {
return getNullLogger
}
return v.(*loggerCache).getLogger
}
// AlsoLogTo returns a context where all logging is emitted the the original output plus the provided loggers.
func AlsoLogTo(ctx context.Context, loggers ...Logger) context.Context {
originalLogFactory := loggerFactoryFromContext(ctx)
return WithLogger(ctx, func(module string) Logger {
return append(Broadcast{originalLogFactory(module)}, loggers...)
})
}

View File

@@ -140,9 +140,11 @@ func (e NotOwnedError) Error() string {
// RunExclusive runs the provided callback if the maintenance is owned by local user and
// lock can be acquired. Lock is passed to the function, which ensures that every call to Run()
// is within the exclusive context.
func RunExclusive(ctx context.Context, rep repo.DirectRepositoryWriter, mode Mode, force bool, cb func(runParams RunParameters) error) error {
func RunExclusive(ctx context.Context, rep repo.DirectRepositoryWriter, mode Mode, force bool, cb func(ctx context.Context, runParams RunParameters) error) error {
rep.DisableIndexRefresh()
ctx = rep.AlsoLogToContentLog(ctx)
p, err := GetParams(ctx, rep)
if err != nil {
return errors.Wrap(err, "unable to get maintenance params")
@@ -208,7 +210,7 @@ func RunExclusive(ctx context.Context, rep repo.DirectRepositoryWriter, mode Mod
return errors.Wrap(err, "error refreshing indexes before maintenance")
}
return cb(runParams)
return cb(ctx, runParams)
}
func ensureNoClockSkew(rp RunParameters) error {

View File

@@ -11,7 +11,6 @@
"github.com/kopia/kopia/internal/clock"
"github.com/kopia/kopia/repo/blob"
"github.com/kopia/kopia/repo/content"
"github.com/kopia/kopia/repo/logging"
"github.com/kopia/kopia/repo/manifest"
"github.com/kopia/kopia/repo/object"
)
@@ -57,7 +56,7 @@ type DirectRepository interface {
Crypter() *content.Crypter
NewDirectWriter(ctx context.Context, opt WriteSessionOptions) (context.Context, DirectRepositoryWriter, error)
InternalLogger() logging.Logger
AlsoLogToContentLog(ctx context.Context) context.Context
// misc
UniqueID() []byte
@@ -197,9 +196,9 @@ func (r *directRepository) UpdateDescription(d string) {
r.cliOpts.Description = d
}
// Logger returns the internal logger for the repository.
func (r *directRepository) InternalLogger() logging.Logger {
return r.sm.InternalLogger()
// AlsoLogToContentLog returns a context that causes all logs to also be sent to content log.
func (r *directRepository) AlsoLogToContentLog(ctx context.Context) context.Context {
return r.sm.AlsoLogToContentLog(ctx)
}
// NewWriter returns new RepositoryWriter session for repository.

View File

@@ -15,7 +15,7 @@
func Run(ctx context.Context, dr repo.DirectRepositoryWriter, mode maintenance.Mode, force bool, safety maintenance.SafetyParameters) error {
// nolint:wrapcheck
return maintenance.RunExclusive(ctx, dr, mode, force,
func(runParams maintenance.RunParameters) error {
func(ctx context.Context, runParams maintenance.RunParameters) error {
// run snapshot GC before full maintenance
if runParams.Mode == maintenance.ModeFull {
if _, err := snapshotgc.Run(ctx, dr, true, safety); err != nil {