Skip to content

Commit

Permalink
dynamic host volumes: fix unblocking bug in state store
Browse files Browse the repository at this point in the history
The `HostVolumeByID` state store method didn't add a watch channel to the
watchset, which meant that it would never unblock. The tests missed this because
they were racy, so move the updates for unblocking tests into a `time.After`
call to ensure the queries are blocked before the update happens.
  • Loading branch information
tgross committed Dec 9, 2024
1 parent 704f476 commit 51dd0b2
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 6 deletions.
21 changes: 16 additions & 5 deletions nomad/host_volume_endpoint_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -177,9 +177,14 @@ func TestHostVolumeEndpoint_CreateRegisterGetDelete(t *testing.T) {
volCh <- getResp.Volume
}()

var registerResp structs.HostVolumeRegisterResponse
err = msgpackrpc.CallWithCodec(codec, "HostVolume.Register", registerReq, &registerResp)
must.NoError(t, err)
// re-register the volume long enough later that we can be sure we won't
// win a race with the get RPC goroutine
time.AfterFunc(200*time.Millisecond, func() {
codec := rpcClient(t, srv)
var registerResp structs.HostVolumeRegisterResponse
err = msgpackrpc.CallWithCodec(codec, "HostVolume.Register", registerReq, &registerResp)
must.NoError(t, err)
})

select {
case <-ctx.Done():
Expand Down Expand Up @@ -480,8 +485,14 @@ func TestHostVolumeEndpoint_List(t *testing.T) {
respCh <- &listResp
}()

err = msgpackrpc.CallWithCodec(codec, "HostVolume.Register", registerReq, &registerResp)
must.NoError(t, err)
// re-register the volume long enough later that we can be sure we won't
// win a race with the get RPC goroutine
time.AfterFunc(200*time.Millisecond, func() {
codec := rpcClient(t, srv)
var registerResp structs.HostVolumeRegisterResponse
err = msgpackrpc.CallWithCodec(codec, "HostVolume.Register", registerReq, &registerResp)
must.NoError(t, err)
})

select {
case <-ctx.Done():
Expand Down
4 changes: 3 additions & 1 deletion nomad/state/state_store_host_volumes.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,12 @@ import (
// HostVolumeByID retrieve a specific host volume
func (s *StateStore) HostVolumeByID(ws memdb.WatchSet, ns, id string, withAllocs bool) (*structs.HostVolume, error) {
txn := s.db.ReadTxn()
obj, err := txn.First(TableHostVolumes, indexID, ns, id)
watchCh, obj, err := txn.FirstWatch(TableHostVolumes, indexID, ns, id)
if err != nil {
return nil, err
}
ws.Add(watchCh)

if obj == nil {
return nil, nil
}
Expand Down

0 comments on commit 51dd0b2

Please sign in to comment.