Skip to content

Commit

Permalink
fix a flaky test in boltdb shipper (#2550)
Browse files Browse the repository at this point in the history
  • Loading branch information
sandeepsukhani authored Aug 25, 2020
1 parent 750ab36 commit dab393a
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 7 deletions.
11 changes: 8 additions & 3 deletions pkg/storage/stores/shipper/uploads/table.go
Original file line number Diff line number Diff line change
Expand Up @@ -198,9 +198,7 @@ func (lt *Table) Upload(ctx context.Context, force bool) error {
lt.dbsMtx.RLock()
defer lt.dbsMtx.RUnlock()

// upload files excluding active shard. It could so happen that we just started a new shard but the file for last shard is still being updated due to pending writes or pending flush to disk.
// To avoid uploading it, excluding previous active shard as well if it has been not more than a minute since it became inactive.
uploadShardsBefore := fmt.Sprint(time.Now().Add(-time.Minute).Truncate(shardDBsByDuration).Unix())
uploadShardsBefore := fmt.Sprint(getOldestActiveShardTime().Unix())

// Adding check for considering only files which are sharded and have just an epoch in their name.
// Before introducing sharding we had a single file per table which were were moved inside the folder per table as part of migration.
Expand Down Expand Up @@ -379,3 +377,10 @@ func loadBoltDBsFromDir(dir string) (map[string]*bbolt.DB, error) {

return dbs, nil
}

// getOldestActiveShardTime returns the time of oldest active shard with a buffer of 1 minute.
func getOldestActiveShardTime() time.Time {
// upload files excluding active shard. It could so happen that we just started a new shard but the file for last shard is still being updated due to pending writes or pending flush to disk.
// To avoid uploading it, excluding previous active shard as well if it has been not more than a minute since it became inactive.
return time.Now().Add(-time.Minute).Truncate(shardDBsByDuration)
}
9 changes: 5 additions & 4 deletions pkg/storage/stores/shipper/uploads/table_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -378,13 +378,14 @@ func TestTable_ImmutableUploads(t *testing.T) {
boltDBIndexClient.Stop()
}()

activeShard := time.Now().Truncate(shardDBsByDuration)
// shardCutoff is calulated based on when shards are considered to not be active anymore and are safe to be uploaded.
shardCutoff := getOldestActiveShardTime()

// some dbs to setup
dbNames := []int64{
activeShard.Add(-shardDBsByDuration).Unix(), // inactive shard, should upload
activeShard.Add(-2 * time.Minute).Unix(), // 2 minutes before active shard, should upload
activeShard.Unix(), // active shard, should not upload
shardCutoff.Add(-shardDBsByDuration).Unix(), // inactive shard, should upload
shardCutoff.Add(-1 * time.Minute).Unix(), // 1 minute before shard cutoff, should upload
time.Now().Truncate(shardDBsByDuration).Unix(), // active shard, should not upload
}

dbs := map[string]testutil.DBRecords{}
Expand Down

0 comments on commit dab393a

Please sign in to comment.