Skip to content

Commit

Permalink
Add test for archiver.waitObtainStorageLock
Browse files Browse the repository at this point in the history
  • Loading branch information
bitwiseguy committed Jun 20, 2024
1 parent 1d743ba commit a0019b1
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 6 deletions.
13 changes: 7 additions & 6 deletions archiver/service/archiver.go
Original file line number Diff line number Diff line change
Expand Up @@ -136,11 +136,9 @@ func (a *Archiver) persistBlobsForBlockToS3(ctx context.Context, blockIdentifier
return currentHeader.Data, exists, nil
}

const (
LockUpdateInterval = 10 * time.Second
ObtainLockRetryInterval = 10 * time.Second
LockTimeout = int64(20) // 20 seconds
)
const LockUpdateInterval = 10 * time.Second
const LockTimeout = int64(20) // 20 seconds
var ObtainLockRetryInterval = 10 * time.Second

func (a *Archiver) waitObtainStorageLock(ctx context.Context) {
lockfile, err := a.dataStoreClient.ReadLockfile(ctx)
Expand All @@ -153,7 +151,10 @@ func (a *Archiver) waitObtainStorageLock(ctx context.Context) {
if lockfile != emptyLockfile {
for lockfile.ArchiverId != a.id && lockfile.Timestamp+LockTimeout > currentTime {
// Loop until the timestamp read from storage is expired
a.log.Info("attempting to obtain storage lock")
a.log.Info("waiting for storage lock timestamp to expire",
"timestamp", strconv.FormatInt(lockfile.Timestamp, 10),
"currentTime", strconv.FormatInt(currentTime, 10),
)
time.Sleep(ObtainLockRetryInterval)
lockfile, err = a.dataStoreClient.ReadLockfile(ctx)
if err != nil {
Expand Down
18 changes: 18 additions & 0 deletions archiver/service/archiver_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -163,6 +163,24 @@ func TestArchiver_BackfillToExistingBlock(t *testing.T) {
}
}

func TestArchiver_ObtainLockfile(t *testing.T) {
beacon := beacontest.NewDefaultStubBeaconClient(t)
svc, _ := setup(t, beacon)

currentTime := time.Now().Unix()
expiredTime := currentTime - 19
err := svc.dataStoreClient.WriteLockfile(context.Background(), storage.Lockfile{ArchiverId: "FAKEID", Timestamp: expiredTime})
require.NoError(t, err)

ObtainLockRetryInterval = 1 * time.Second
svc.waitObtainStorageLock(context.Background())

lockfile, err := svc.dataStoreClient.ReadLockfile(context.Background())
require.NoError(t, err)
require.Equal(t, svc.id, lockfile.ArchiverId)
require.True(t, lockfile.Timestamp >= currentTime)
}

func TestArchiver_BackfillFinishOldProcess(t *testing.T) {
beacon := beacontest.NewDefaultStubBeaconClient(t)
svc, fs := setup(t, beacon)
Expand Down

0 comments on commit a0019b1

Please sign in to comment.