Skip to content

Commit bc60ac5

Browse files
committed
Generate EndSessions operation
GODRIVER-1051 Change-Id: I861e747c15ae88d6bda24e3b0df38b1c33edab5b
1 parent 79f4e63 commit bc60ac5

File tree

6 files changed

+216
-10
lines changed

6 files changed

+216
-10
lines changed

mongo/client.go

Lines changed: 26 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ package mongo
99
import (
1010
"context"
1111
"crypto/tls"
12+
"strconv"
1213
"strings"
1314
"time"
1415

@@ -19,6 +20,7 @@ import (
1920
"go.mongodb.org/mongo-driver/mongo/readconcern"
2021
"go.mongodb.org/mongo-driver/mongo/readpref"
2122
"go.mongodb.org/mongo-driver/mongo/writeconcern"
23+
"go.mongodb.org/mongo-driver/x/bsonx/bsoncore"
2224
"go.mongodb.org/mongo-driver/x/mongo/driver"
2325
"go.mongodb.org/mongo-driver/x/mongo/driver/auth"
2426
"go.mongodb.org/mongo-driver/x/mongo/driver/connstring"
@@ -27,11 +29,10 @@ import (
2729
"go.mongodb.org/mongo-driver/x/mongo/driver/session"
2830
"go.mongodb.org/mongo-driver/x/mongo/driver/topology"
2931
"go.mongodb.org/mongo-driver/x/mongo/driver/uuid"
30-
"go.mongodb.org/mongo-driver/x/mongo/driverlegacy"
31-
"go.mongodb.org/mongo-driver/x/network/command"
3232
)
3333

3434
const defaultLocalThreshold = 15 * time.Millisecond
35+
const batchSize = 10000
3536

3637
// Client performs operations on a given topology.
3738
type Client struct {
@@ -190,12 +191,31 @@ func (c *Client) endSessions(ctx context.Context) {
190191
if c.topology.SessionPool == nil {
191192
return
192193
}
193-
cmd := command.EndSessions{
194-
Clock: c.clock,
195-
SessionIDs: c.topology.SessionPool.IDSlice(),
194+
195+
ids := c.topology.SessionPool.IDSlice()
196+
idx, idArray := bsoncore.AppendArrayStart(nil)
197+
for i, id := range ids {
198+
idDoc, _ := id.MarshalBSON()
199+
idArray = bsoncore.AppendDocumentElement(idArray, strconv.Itoa(i), idDoc)
200+
}
201+
idArray, _ = bsoncore.AppendArrayEnd(idArray, idx)
202+
203+
op := operation.NewEndSessions(idArray).ClusterClock(c.clock).Deployment(c.topology).
204+
ServerSelector(description.ReadPrefSelector(readpref.PrimaryPreferred())).CommandMonitor(c.monitor).Database("admin")
205+
206+
idx, idArray = bsoncore.AppendArrayStart(nil)
207+
totalNumIDs := len(ids)
208+
for i := 0; i < totalNumIDs; i++ {
209+
idDoc, _ := ids[i].MarshalBSON()
210+
idArray = bsoncore.AppendDocumentElement(idArray, strconv.Itoa(i), idDoc)
211+
if ((i+1)%batchSize) == 0 || i == totalNumIDs-1 {
212+
idArray, _ = bsoncore.AppendArrayEnd(idArray, idx)
213+
_ = op.SessionIDs(idArray).Execute(ctx)
214+
idArray = idArray[:0]
215+
idx = 0
216+
}
196217
}
197218

198-
_, _ = driverlegacy.EndSessions(ctx, cmd, c.topology, description.ReadPrefSelector(readpref.PrimaryPreferred()))
199219
}
200220

201221
func (c *Client) configure(opts *options.ClientOptions) error {

mongo/client_internal_test.go

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ import (
2020
"go.mongodb.org/mongo-driver/bson"
2121
"go.mongodb.org/mongo-driver/bson/bsoncodec"
2222
"go.mongodb.org/mongo-driver/bson/bsonrw"
23+
"go.mongodb.org/mongo-driver/event"
2324
"go.mongodb.org/mongo-driver/internal/testutil"
2425
"go.mongodb.org/mongo-driver/mongo/options"
2526
"go.mongodb.org/mongo-driver/mongo/readpref"
@@ -546,3 +547,29 @@ func TestClient_Watch_Disconnected(t *testing.T) {
546547
require.Nil(t, change)
547548
require.Equal(t, err, ErrClientDisconnected)
548549
}
550+
551+
func TestEndSessions(t *testing.T) {
552+
skipIfBelow36(t)
553+
cs := testutil.ConnString(t)
554+
client, err := NewClient(options.Client().ApplyURI(cs.String()).SetMonitor(monitor))
555+
require.NoError(t, err)
556+
err = client.Connect(nil)
557+
require.NoError(t, err)
558+
559+
_, err = client.ListDatabases(ctx, bsonx.Doc{})
560+
require.NoError(t, err)
561+
562+
drainChannels()
563+
564+
err = client.Disconnect(ctx)
565+
require.NoError(t, err)
566+
567+
var started *event.CommandStartedEvent
568+
select {
569+
case started = <-startedChan:
570+
default:
571+
t.Fatalf("expected a CommandStartedEvent but none found")
572+
}
573+
574+
require.Equal(t, "endSessions", started.CommandName)
575+
}

x/mongo/driver/drivergen/drivergen.go

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -154,9 +154,11 @@ func (op Operation) CommandMethod() (string, error) {
154154
return "", fmt.Errorf("unknown request field type %s", field.Type)
155155
}
156156
var rf struct {
157-
ShortName string
158-
Name string
159-
ParameterName string
157+
ShortName string
158+
Name string
159+
ParameterName string
160+
MinWireVersion int
161+
MinWireVersionRequired int
160162
}
161163
rf.ShortName = op.ShortName()
162164
rf.Name = op.Command.Parameter
@@ -173,7 +175,7 @@ func (op Operation) CommandMethod() (string, error) {
173175
sort.Strings(names)
174176
for _, name := range names {
175177
field := op.Request[name]
176-
if name == op.Properties.Batches {
178+
if name == op.Properties.Batches || field.Skip {
177179
continue
178180
}
179181
var tmpl *template.Template
@@ -538,6 +540,7 @@ type RequestField struct {
538540
Slice bool
539541
Constructor bool
540542
Variadic bool
543+
Skip bool
541544
Documentation string
542545
MinWireVersion int
543546
MinWireVersionRequired int

x/mongo/driver/operation/end_sessions.go

Lines changed: 139 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
version = 0
2+
name = "EndSessions"
3+
documentation = "EndSessions performs an endSessions operation."
4+
5+
[properties]
6+
disabled = ["collection"]
7+
8+
[command]
9+
name = "endSessions"
10+
parameter = "sessionIDs"
11+
12+
[request.sessionIDs]
13+
type = "array"
14+
documentation = "sessionIDs specify the sessions to be expired."
15+
skip = true
16+
constructor = true

x/mongo/driver/operation/operation.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,3 +12,4 @@ package operation
1212
//go:generate operationgen commit_transaction.toml operation commit_transaction.go
1313
//go:generate operationgen abort_transaction.toml operation abort_transaction.go
1414
//go:generate operationgen count.toml operation count.go
15+
//go:generate operationgen end_sessions.toml operation end_sessions.go

0 commit comments

Comments
 (0)