-
Notifications
You must be signed in to change notification settings - Fork 3.8k
/
Copy pathcancel_sessions.go
82 lines (65 loc) · 2.15 KB
/
cancel_sessions.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
// Copyright 2018 The Cockroach Authors.
//
// Use of this software is governed by the Business Source License
// included in the file licenses/BSL.txt.
//
// As of the Change Date specified in that file, in accordance with
// the Business Source License, use of this software will be governed
// by the Apache License, Version 2.0, included in the file
// licenses/APL.txt.
package sql
import (
"context"
"fmt"
"github.com/cockroachdb/cockroach/pkg/server/serverpb"
"github.com/cockroachdb/cockroach/pkg/sql/pgwire/pgcode"
"github.com/cockroachdb/cockroach/pkg/sql/pgwire/pgerror"
"github.com/cockroachdb/cockroach/pkg/sql/sem/tree"
"github.com/cockroachdb/errors"
)
type cancelSessionsNode struct {
rows planNode
ifExists bool
}
func (n *cancelSessionsNode) startExec(runParams) error {
return nil
}
func (n *cancelSessionsNode) Next(params runParams) (bool, error) {
// TODO(knz): instead of performing the cancels sequentially,
// accumulate all the query IDs and then send batches to each of the
// nodes.
if ok, err := n.rows.Next(params); err != nil || !ok {
return ok, err
}
datum := n.rows.Values()[0]
if datum == tree.DNull {
return true, nil
}
sessionIDString, ok := tree.AsDString(datum)
if !ok {
return false, errors.AssertionFailedf("%q: expected *DString, found %T", datum, datum)
}
sessionID, err := StringToClusterWideID(string(sessionIDString))
if err != nil {
return false, pgerror.Wrapf(err, pgcode.Syntax, "invalid session ID %s", datum)
}
// Get the lowest 32 bits of the session ID.
nodeID := sessionID.GetNodeID()
request := &serverpb.CancelSessionRequest{
NodeId: fmt.Sprintf("%d", nodeID),
SessionID: sessionID.GetBytes(),
Username: params.SessionData().User().Normalized(),
}
response, err := params.extendedEvalCtx.SQLStatusServer.CancelSession(params.ctx, request)
if err != nil {
return false, err
}
if !response.Canceled && !n.ifExists {
return false, errors.Newf("could not cancel session %s: %s", sessionID, response.Error)
}
return true, nil
}
func (*cancelSessionsNode) Values() tree.Datums { return nil }
func (n *cancelSessionsNode) Close(ctx context.Context) {
n.rows.Close(ctx)
}