Skip to content

Commit

Permalink
Skip duplicate full snapshot over previous
Browse files Browse the repository at this point in the history
Signed-off-by: Swapnil Mhamane <[email protected]>
  • Loading branch information
Swapnil Mhamane committed Dec 11, 2018
1 parent 97cc311 commit d960b82
Showing 1 changed file with 40 additions and 24 deletions.
64 changes: 40 additions & 24 deletions pkg/snapshot/snapshotter/snapshotter.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ import (
"github.com/coreos/etcd/clientv3"
"github.com/gardener/etcd-backup-restore/pkg/errors"
"github.com/gardener/etcd-backup-restore/pkg/etcdutil"
"github.com/gardener/etcd-backup-restore/pkg/miscellaneous"
"github.com/gardener/etcd-backup-restore/pkg/snapstore"
"github.com/robfig/cron"
"github.com/sirupsen/logrus"
Expand Down Expand Up @@ -57,7 +58,16 @@ func NewSnapshotterConfig(schedule string, store snapstore.SnapStore, maxBackups
// NewSnapshotter returns the snapshotter object.
func NewSnapshotter(logger *logrus.Logger, config *Config) *Snapshotter {
// Create dummy previous snapshot
prevSnapshot := snapstore.NewSnapshot(snapstore.SnapshotKindFull, 0, 0)
var prevSnapshot *snapstore.Snapshot
fullSnap, deltaSnapList, err := miscellaneous.GetLatestFullSnapshotAndDeltaSnapList(config.store)
if err != nil || fullSnap == nil {
prevSnapshot = snapstore.NewSnapshot(snapstore.SnapshotKindFull, 0, 0)
} else if len(deltaSnapList) == 0 {
prevSnapshot = fullSnap
} else {
prevSnapshot = deltaSnapList[len(deltaSnapList)-1]
}

return &Snapshotter{
logger: logger,
prevSnapshot: prevSnapshot,
Expand Down Expand Up @@ -163,32 +173,38 @@ func (ssr *Snapshotter) takeFullSnapshot() error {
}
}
lastRevision := resp.Header.Revision
ctx, cancel = context.WithTimeout(context.TODO(), ssr.config.etcdConnectionTimeout*time.Second)
defer cancel()
rc, err := client.Snapshot(ctx)
if err != nil {
return &errors.EtcdError{
Message: fmt.Sprintf("failed to create etcd snapshot: %v", err),

if ssr.prevSnapshot.Kind == snapstore.SnapshotKindFull && ssr.prevSnapshot.LastRevision == lastRevision {
ssr.logger.Infof("There are no updates since last snapshot, skipping full snapshot.")
} else {
ctx, cancel = context.WithTimeout(context.TODO(), ssr.config.etcdConnectionTimeout*time.Second)
defer cancel()
rc, err := client.Snapshot(ctx)
if err != nil {
return &errors.EtcdError{
Message: fmt.Sprintf("failed to create etcd snapshot: %v", err),
}
}
}
ssr.logger.Infof("Successfully opened snapshot reader on etcd")
s := &snapstore.Snapshot{
Kind: snapstore.SnapshotKindFull,
CreatedOn: time.Now().UTC(),
StartRevision: 0,
LastRevision: lastRevision,
}
s.GenerateSnapshotDirectory()
s.GenerateSnapshotName()
startTime := time.Now()
if err := ssr.config.store.Save(*s, rc); err != nil {
return &errors.SnapstoreError{
Message: fmt.Sprintf("failed to save snapshot: %v", err),
ssr.logger.Infof("Successfully opened snapshot reader on etcd")
s := &snapstore.Snapshot{
Kind: snapstore.SnapshotKindFull,
CreatedOn: time.Now().UTC(),
StartRevision: 0,
LastRevision: lastRevision,
}
s.GenerateSnapshotDirectory()
s.GenerateSnapshotName()
startTime := time.Now()
if err := ssr.config.store.Save(*s, rc); err != nil {
return &errors.SnapstoreError{
Message: fmt.Sprintf("failed to save snapshot: %v", err),
}
}
logrus.Infof("Total time to save snapshot: %f seconds.", time.Now().Sub(startTime).Seconds())
ssr.prevSnapshot = s
ssr.logger.Infof("Successfully saved full snapshot at: %s", path.Join(s.SnapDir, s.SnapName))
}
logrus.Infof("Total time to save snapshot: %f seconds.", time.Now().Sub(startTime).Seconds())
ssr.prevSnapshot = s
ssr.logger.Infof("Successfully saved full snapshot at: %s", path.Join(s.SnapDir, s.SnapName))

//make event array empty as any event prior to full snapshot should not be uploaded in delta.
ssr.events = []*event{}
watchCtx, cancelWatch := context.WithCancel(context.TODO())
Expand Down

0 comments on commit d960b82

Please sign in to comment.