Skip to content

Commit a0019b1

Browse files
committed
Add test for archiver.waitObtainStorageLock
1 parent 1d743ba commit a0019b1

File tree

2 files changed

+25
-6
lines changed

2 files changed

+25
-6
lines changed

archiver/service/archiver.go

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -136,11 +136,9 @@ func (a *Archiver) persistBlobsForBlockToS3(ctx context.Context, blockIdentifier
136136
return currentHeader.Data, exists, nil
137137
}
138138

139-
const (
140-
LockUpdateInterval = 10 * time.Second
141-
ObtainLockRetryInterval = 10 * time.Second
142-
LockTimeout = int64(20) // 20 seconds
143-
)
139+
const LockUpdateInterval = 10 * time.Second
140+
const LockTimeout = int64(20) // 20 seconds
141+
var ObtainLockRetryInterval = 10 * time.Second
144142

145143
func (a *Archiver) waitObtainStorageLock(ctx context.Context) {
146144
lockfile, err := a.dataStoreClient.ReadLockfile(ctx)
@@ -153,7 +151,10 @@ func (a *Archiver) waitObtainStorageLock(ctx context.Context) {
153151
if lockfile != emptyLockfile {
154152
for lockfile.ArchiverId != a.id && lockfile.Timestamp+LockTimeout > currentTime {
155153
// Loop until the timestamp read from storage is expired
156-
a.log.Info("attempting to obtain storage lock")
154+
a.log.Info("waiting for storage lock timestamp to expire",
155+
"timestamp", strconv.FormatInt(lockfile.Timestamp, 10),
156+
"currentTime", strconv.FormatInt(currentTime, 10),
157+
)
157158
time.Sleep(ObtainLockRetryInterval)
158159
lockfile, err = a.dataStoreClient.ReadLockfile(ctx)
159160
if err != nil {

archiver/service/archiver_test.go

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -163,6 +163,24 @@ func TestArchiver_BackfillToExistingBlock(t *testing.T) {
163163
}
164164
}
165165

166+
func TestArchiver_ObtainLockfile(t *testing.T) {
167+
beacon := beacontest.NewDefaultStubBeaconClient(t)
168+
svc, _ := setup(t, beacon)
169+
170+
currentTime := time.Now().Unix()
171+
expiredTime := currentTime - 19
172+
err := svc.dataStoreClient.WriteLockfile(context.Background(), storage.Lockfile{ArchiverId: "FAKEID", Timestamp: expiredTime})
173+
require.NoError(t, err)
174+
175+
ObtainLockRetryInterval = 1 * time.Second
176+
svc.waitObtainStorageLock(context.Background())
177+
178+
lockfile, err := svc.dataStoreClient.ReadLockfile(context.Background())
179+
require.NoError(t, err)
180+
require.Equal(t, svc.id, lockfile.ArchiverId)
181+
require.True(t, lockfile.Timestamp >= currentTime)
182+
}
183+
166184
func TestArchiver_BackfillFinishOldProcess(t *testing.T) {
167185
beacon := beacontest.NewDefaultStubBeaconClient(t)
168186
svc, fs := setup(t, beacon)

0 commit comments

Comments
 (0)