diff --git a/pkg/server/drain.go b/pkg/server/drain.go index 89d554a235c3..6e01e5338cdb 100644 --- a/pkg/server/drain.go +++ b/pkg/server/drain.go @@ -462,11 +462,13 @@ func (s *drainServer) drainClients( if err != nil { return err } - - instanceID := s.sqlServer.sqlIDContainer.SQLInstanceID() - err = s.sqlServer.sqlInstanceStorage.ReleaseInstance(ctx, session, instanceID) - if err != nil { - return err + // If we started a sql session on this node. + if session != "" { + instanceID := s.sqlServer.sqlIDContainer.SQLInstanceID() + err = s.sqlServer.sqlInstanceStorage.ReleaseInstance(ctx, session, instanceID) + if err != nil { + return err + } } // Mark the node as fully drained. diff --git a/pkg/server/drain_test.go b/pkg/server/drain_test.go index c1a5b69c4f22..8b9792777a78 100644 --- a/pkg/server/drain_test.go +++ b/pkg/server/drain_test.go @@ -343,3 +343,27 @@ func TestServerShutdownReleasesSession(t *testing.T) { require.False(t, sessionExists(*session), "expected session %s to be deleted from the sqlliveness table, but it still exists", *session) require.Nil(t, queryOwner(tmpSQLInstance), "expected sql_instance %d to have no owning session_id", tmpSQLInstance) } + +// Verify that drain works correctly even if we don't start the sql instance. +func TestNoSQLServer(t *testing.T) { + defer leaktest.AfterTest(t)() + defer log.Scope(t).Close(t) + + ctx := context.Background() + tc := testcluster.StartTestCluster(t, 2, + base.TestClusterArgs{ + ServerArgs: base.TestServerArgs{ + DefaultTestTenant: base.TestIsSpecificToStorageLayerAndNeedsASystemTenant, + DisableSQLServer: true, + }, + }) + + defer tc.Stopper().Stop(ctx) + req := serverpb.DrainRequest{Shutdown: false, DoDrain: true, NodeId: "2"} + drainStream, err := tc.Server(0).ApplicationLayer().GetAdminClient(t).Drain(ctx, &req) + require.NoError(t, err) + // When we get this next response the drain has started - check the error. + drainResp, err := drainStream.Recv() + require.NoError(t, err) + require.True(t, drainResp.IsDraining) +} diff --git a/pkg/sql/sqlliveness/slinstance/slinstance.go b/pkg/sql/sqlliveness/slinstance/slinstance.go index 1d937357f943..324b47148216 100644 --- a/pkg/sql/sqlliveness/slinstance/slinstance.go +++ b/pkg/sql/sqlliveness/slinstance/slinstance.go @@ -457,7 +457,7 @@ func (l *Instance) Release(ctx context.Context) (sqlliveness.SessionID, error) { }() if session == nil { - return sqlliveness.SessionID(""), errors.New("no session to release") + return "", nil } if err := l.storage.Delete(ctx, session.ID()); err != nil {