-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #65 from ydb-platform/feature/NBYDB-281
feat(ydbcp): support ttl for backups
- Loading branch information
Showing
13 changed files
with
290 additions
and
6 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,103 @@ | ||
package ttl_watcher | ||
|
||
import ( | ||
"context" | ||
table_types "github.com/ydb-platform/ydb-go-sdk/v3/table/types" | ||
"go.uber.org/zap" | ||
"google.golang.org/protobuf/types/known/timestamppb" | ||
"sync" | ||
"time" | ||
"ydbcp/internal/connectors/db" | ||
"ydbcp/internal/connectors/db/yql/queries" | ||
"ydbcp/internal/types" | ||
"ydbcp/internal/util/xlog" | ||
"ydbcp/internal/watchers" | ||
pb "ydbcp/pkg/proto/ydbcp/v1alpha1" | ||
) | ||
|
||
func NewTtlWatcher( | ||
ctx context.Context, | ||
wg *sync.WaitGroup, | ||
db db.DBConnector, | ||
queryBuilderFactory queries.WriteQueryBulderFactory, | ||
options ...watchers.Option, | ||
) *watchers.WatcherImpl { | ||
return watchers.NewWatcher( | ||
ctx, | ||
wg, | ||
func(ctx context.Context, period time.Duration) { | ||
TtlWatcherAction(ctx, period, db, queryBuilderFactory) | ||
}, | ||
time.Hour, | ||
"Ttl", | ||
options..., | ||
) | ||
} | ||
|
||
func TtlWatcherAction( | ||
baseCtx context.Context, | ||
period time.Duration, | ||
db db.DBConnector, | ||
queryBuilderFactory queries.WriteQueryBulderFactory, | ||
) { | ||
ctx, cancel := context.WithTimeout(baseCtx, period) | ||
defer cancel() | ||
|
||
backups, err := db.SelectBackups( | ||
ctx, queries.NewReadTableQuery( | ||
queries.WithTableName("Backups"), | ||
queries.WithSelectFields(queries.AllBackupFields...), | ||
queries.WithQueryFilters( | ||
queries.QueryFilter{ | ||
Field: "status", | ||
Values: []table_types.Value{ | ||
table_types.StringValueFromString(types.BackupStateAvailable), | ||
table_types.StringValueFromString(types.BackupStateError), | ||
table_types.StringValueFromString(types.BackupStateCancelled), | ||
}, | ||
}, | ||
), | ||
), | ||
) | ||
|
||
if err != nil { | ||
xlog.Error(ctx, "can't select backups", zap.Error(err)) | ||
return | ||
} | ||
|
||
for _, backup := range backups { | ||
if backup.ExpireAt != nil && backup.ExpireAt.Before(time.Now()) { | ||
now := timestamppb.Now() | ||
dbOp := &types.DeleteBackupOperation{ | ||
ContainerID: backup.ContainerID, | ||
BackupID: backup.ID, | ||
State: types.OperationStatePending, | ||
YdbConnectionParams: types.YdbConnectionParams{ | ||
DatabaseName: backup.DatabaseName, | ||
Endpoint: backup.DatabaseEndpoint, | ||
}, | ||
Audit: &pb.AuditInfo{ | ||
CreatedAt: now, | ||
Creator: types.OperationCreatorName, | ||
}, | ||
PathPrefix: backup.S3PathPrefix, | ||
UpdatedAt: now, | ||
} | ||
|
||
backupToWrite := types.Backup{ | ||
ID: backup.ID, | ||
Status: types.BackupStateDeleting, | ||
} | ||
|
||
err := db.ExecuteUpsert( | ||
ctx, queryBuilderFactory().WithCreateOperation(dbOp).WithUpdateBackup(backupToWrite), | ||
) | ||
|
||
if err != nil { | ||
xlog.Error(ctx, "can't create DeleteBackup operation", zap.String("BackupID", backup.ID), zap.Error(err)) | ||
} | ||
|
||
xlog.Debug(ctx, "DeleteBackup operation was created successfully", zap.String("BackupID", backup.ID)) | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,77 @@ | ||
package ttl_watcher | ||
|
||
import ( | ||
"context" | ||
"github.com/jonboulle/clockwork" | ||
"github.com/stretchr/testify/assert" | ||
"sync" | ||
"testing" | ||
"time" | ||
"ydbcp/internal/connectors/db" | ||
"ydbcp/internal/connectors/db/yql/queries" | ||
"ydbcp/internal/types" | ||
"ydbcp/internal/util/ticker" | ||
"ydbcp/internal/watchers" | ||
) | ||
|
||
func TestTtlWatcher(t *testing.T) { | ||
var wg sync.WaitGroup | ||
ctx, cancel := context.WithCancel(context.Background()) | ||
defer cancel() | ||
|
||
// Prepare fake clock and ticker | ||
clock := clockwork.NewFakeClock() | ||
var fakeTicker *ticker.FakeTicker | ||
tickerInitialized := make(chan struct{}) | ||
tickerProvider := func(duration time.Duration) ticker.Ticker { | ||
assert.Empty(t, fakeTicker, "ticker reuse") | ||
fakeTicker = ticker.NewFakeTicker(duration) | ||
tickerInitialized <- struct{}{} | ||
return fakeTicker | ||
} | ||
|
||
// Prepare a backup | ||
backupID := types.GenerateObjectID() | ||
expireAt := time.Now() | ||
backup := types.Backup{ | ||
ID: backupID, | ||
Status: types.BackupStateAvailable, | ||
ExpireAt: &expireAt, | ||
} | ||
backupMap := make(map[string]types.Backup) | ||
backupMap[backupID] = backup | ||
|
||
// Prepare mock db and ttl watcher | ||
db := db.NewMockDBConnector( | ||
db.WithBackups(backupMap), | ||
) | ||
_ = NewTtlWatcher( | ||
ctx, | ||
&wg, | ||
db, | ||
queries.NewWriteTableQueryMock, | ||
watchers.WithTickerProvider(tickerProvider), | ||
) | ||
|
||
// Wait for the ticker to be initialized | ||
select { | ||
case <-ctx.Done(): | ||
t.Error("ticker not initialized") | ||
case <-tickerInitialized: | ||
assert.Equal(t, fakeTicker.Period, time.Hour, "incorrect period") | ||
} | ||
|
||
// Send a tick to the fake ticker | ||
t0 := clock.Now().Add(time.Hour) | ||
fakeTicker.Send(t0) | ||
|
||
cancel() | ||
wg.Wait() | ||
|
||
// Check that DeleteBackup operation was created | ||
ops, err := db.ActiveOperations(ctx) | ||
assert.Empty(t, err) | ||
assert.Equal(t, len(ops), 1) | ||
assert.Equal(t, ops[0].GetType(), types.OperationTypeDB, "operation type should be DB") | ||
assert.Equal(t, ops[0].GetState(), types.OperationStatePending, "operation state should be Pending") | ||
} |
Oops, something went wrong.