Files
kopia/tests/robustness/engine/metadata.go
Steve Joachim d73e0d60ce robustness: reduce memory consumption (#1155)
Detailed changes:
* Implement Kopia API client
* Implement kopia client
* Implement new persister
* Separate storeLoad and Delete concurrency tests
* Update Store interface to return error for Delete
* Return errors from os.RemoveAll
* Robustness test updates
* Push robustness metadata without writing to fs
* Fix testenv.AssertNoError references
* Update tests to use new kopia persister
* Minor updates to multiclient test cases
* Use require.NoError instead of assertNoError
* Add context to store interface
* Update logging to be less verbose
* Use io instead of ioutil package
* Simplify restore by using object ID
* Accommodate repository.NewWriter signature change
* Improve tests to increase code coverage
* Spelling and error string fixes
* Address lint errors

Co-authored-by: Nick <nick@kasten.io>
2021-06-30 13:46:51 -07:00

106 lines
2.6 KiB
Go

// +build darwin,amd64 linux,amd64
package engine
import (
"context"
"encoding/json"
"errors"
"github.com/kopia/kopia/internal/clock"
"github.com/kopia/kopia/tests/robustness"
)
const (
engineStatsStoreKey = "cumulative-engine-stats"
engineLogsStoreKey = "engine-logs"
snapIDIndexStoreKey = "checker-snapID-index"
)
// saveLog saves the engine Log in the metadata store.
func (e *Engine) saveLog(ctx context.Context) error {
b, err := json.Marshal(e.EngineLog)
if err != nil {
return err
}
return e.MetaStore.Store(ctx, engineLogsStoreKey, b)
}
// loadLog loads the engine log from the metadata store.
func (e *Engine) loadLog(ctx context.Context) error {
b, err := e.MetaStore.Load(ctx, engineLogsStoreKey)
if err != nil {
if errors.Is(err, robustness.ErrKeyNotFound) {
// Swallow key-not-found error. May not have historical logs
return nil
}
return err
}
err = json.Unmarshal(b, &e.EngineLog)
if err != nil {
return err
}
e.EngineLog.runOffset = len(e.EngineLog.Log)
return err
}
// saveStats saves the engine Stats in the metadata store.
func (e *Engine) saveStats(ctx context.Context) error {
cumulStatRaw, err := json.Marshal(e.CumulativeStats)
if err != nil {
return err
}
return e.MetaStore.Store(ctx, engineStatsStoreKey, cumulStatRaw)
}
// loadStats loads the engine Stats from the metadata store.
func (e *Engine) loadStats(ctx context.Context) error {
b, err := e.MetaStore.Load(ctx, engineStatsStoreKey)
if err != nil {
if errors.Is(err, robustness.ErrKeyNotFound) {
// Swallow key-not-found error. We may not have historical
// stats data. Initialize the action map for the cumulative stats
e.CumulativeStats.PerActionStats = make(map[ActionKey]*ActionStats)
e.CumulativeStats.CreationTime = clock.Now()
return nil
}
return err
}
return json.Unmarshal(b, &e.CumulativeStats)
}
// saveSnapIDIndex saves the Checker's snapshot ID index in the metadata store.
func (e *Engine) saveSnapIDIndex(ctx context.Context) error {
snapIDIdxRaw, err := json.Marshal(e.Checker.SnapIDIndex)
if err != nil {
return err
}
return e.MetaStore.Store(ctx, snapIDIndexStoreKey, snapIDIdxRaw)
}
// loadSnapIDIndex loads the Checker's snapshot ID index from the metadata store.
func (e *Engine) loadSnapIDIndex(ctx context.Context) error {
b, err := e.MetaStore.Load(ctx, snapIDIndexStoreKey)
if err != nil {
if errors.Is(err, robustness.ErrKeyNotFound) {
// Swallow key-not-found error. We may not have historical
// index data.
return nil
}
return err
}
return json.Unmarshal(b, &e.Checker.SnapIDIndex)
}