Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Backport of services: fix data integrity errors for Nomad native services into release/1.6.x #20591

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions .changelog/20590.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
```release-note:bug
services: Fixed bug where Nomad services might not be deregistered when nodes are marked down or allocations are terminal
```
63 changes: 30 additions & 33 deletions nomad/core_sched_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,8 +37,7 @@ func TestCoreScheduler_EvalGC(t *testing.T) {
eval := mock.Eval()
eval.Status = structs.EvalStatusFailed
store.UpsertJobSummary(999, mock.JobSummary(eval.JobID))
err := store.UpsertEvals(structs.MsgTypeTestSetup, 1000, []*structs.Evaluation{eval})
require.Nil(t, err)
must.NoError(t, store.UpsertEvals(structs.MsgTypeTestSetup, 1000, []*structs.Evaluation{eval}))

// Insert mock job with rescheduling disabled
job := mock.Job()
Expand All @@ -47,8 +46,7 @@ func TestCoreScheduler_EvalGC(t *testing.T) {
Attempts: 0,
Interval: 0 * time.Second,
}
err = store.UpsertJob(structs.MsgTypeTestSetup, 1001, nil, job)
require.Nil(t, err)
must.NoError(t, store.UpsertJob(structs.MsgTypeTestSetup, 1001, nil, job))

// Insert "dead" alloc
alloc := mock.Alloc()
Expand All @@ -64,54 +62,53 @@ func TestCoreScheduler_EvalGC(t *testing.T) {
alloc2.ClientStatus = structs.AllocClientStatusLost
alloc2.JobID = eval.JobID
alloc2.TaskGroup = job.TaskGroups[0].Name
err = store.UpsertAllocs(structs.MsgTypeTestSetup, 1001, []*structs.Allocation{alloc, alloc2})
if err != nil {
t.Fatalf("err: %v", err)
}
must.NoError(t, store.UpsertAllocs(structs.MsgTypeTestSetup, 1001, []*structs.Allocation{alloc, alloc2}))

// Insert service for "dead" alloc
service := &structs.ServiceRegistration{
ID: fmt.Sprintf("_nomad-task-%s-group-api-countdash-api-http", alloc.ID),
ServiceName: "countdash-api",
Namespace: eval.Namespace,
NodeID: alloc.NodeID,
Datacenter: "dc1",
JobID: eval.JobID,
AllocID: alloc.ID,
Address: "192.168.200.200",
Port: 29001,
}
must.NoError(t, store.UpsertServiceRegistrations(
structs.MsgTypeTestSetup, 1002, []*structs.ServiceRegistration{service}))

// Update the time tables to make this work
tt := s1.fsm.TimeTable()
tt.Witness(2000, time.Now().UTC().Add(-1*s1.config.EvalGCThreshold))

// Create a core scheduler
snap, err := store.Snapshot()
if err != nil {
t.Fatalf("err: %v", err)
}
must.NoError(t, err)
core := NewCoreScheduler(s1, snap)

// Attempt the GC
gc := s1.coreJobEval(structs.CoreJobEvalGC, 2000)
err = core.Process(gc)
if err != nil {
t.Fatalf("err: %v", err)
}
must.NoError(t, core.Process(gc))

// Should be gone
ws := memdb.NewWatchSet()
out, err := store.EvalByID(ws, eval.ID)
if err != nil {
t.Fatalf("err: %v", err)
}
if out != nil {
t.Fatalf("bad: %v", out)
}
must.NoError(t, err)
must.Nil(t, out, must.Sprint("expected eval to be GC'd"))

outA, err := store.AllocByID(ws, alloc.ID)
if err != nil {
t.Fatalf("err: %v", err)
}
if outA != nil {
t.Fatalf("bad: %v", outA)
}
must.NoError(t, err)
must.Nil(t, outA, must.Sprint("expected alloc to be GC'd"))

outA2, err := store.AllocByID(ws, alloc2.ID)
if err != nil {
t.Fatalf("err: %v", err)
}
if outA2 != nil {
t.Fatalf("bad: %v", outA2)
}
must.NoError(t, err)
must.Nil(t, outA2, must.Sprint("expected alloc to be GC'd"))

services, err := store.GetServiceRegistrationsByNodeID(nil, alloc.NodeID)
must.NoError(t, err)
must.Len(t, 0, services)
}

// Tests GC behavior on allocations being rescheduled
Expand Down
25 changes: 0 additions & 25 deletions nomad/node_endpoint.go
Original file line number Diff line number Diff line change
Expand Up @@ -686,31 +686,6 @@ func (n *Node) UpdateStatus(args *structs.NodeUpdateStatusRequest, reply *struct
_ = n.srv.consulACLs.RevokeTokens(context.Background(), accessors, true)
}

// Identify the service registrations current placed on the downed
// node.
serviceRegistrations, err := n.srv.State().GetServiceRegistrationsByNodeID(ws, args.NodeID)
if err != nil {
n.logger.Error("looking up service registrations for node failed",
"node_id", args.NodeID, "error", err)
return err
}

// If the node has service registrations assigned to it, delete these
// via Raft.
if l := len(serviceRegistrations); l > 0 {
n.logger.Debug("deleting service registrations on node due to down state",
"num_service_registrations", l, "node_id", args.NodeID)

deleteRegReq := structs.ServiceRegistrationDeleteByNodeIDRequest{NodeID: args.NodeID}

_, index, err = n.srv.raftApply(structs.ServiceRegistrationDeleteByNodeIDRequestType, &deleteRegReq)
if err != nil {
n.logger.Error("failed to delete service registrations for node",
"node_id", args.NodeID, "error", err)
return err
}
}

default:
ttl, err := n.srv.resetHeartbeatTimer(args.NodeID)
if err != nil {
Expand Down
Loading
Loading