Skip to content

Commit 604cc07

Browse files
GODRIVER-2737 ConnectionId returned in heartbeats may be int64 (#1266)
1 parent 55fd8c9 commit 604cc07

File tree

10 files changed

+151
-46
lines changed

10 files changed

+151
-46
lines changed

mongo/integration/mtest/opmsg_deployment.go

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -90,9 +90,15 @@ func (*connection) ID() string {
9090
return "<mock_connection>"
9191
}
9292

93+
// DriverConnectionID returns a fixed identifier for the driver pool connection.
94+
// TODO(GODRIVER-2824): replace return type with int64.
95+
func (*connection) DriverConnectionID() uint64 {
96+
return 0
97+
}
98+
9399
// ServerConnectionID returns a fixed identifier for the server connection.
94-
func (*connection) ServerConnectionID() *int32 {
95-
serverConnectionID := int32(42)
100+
func (*connection) ServerConnectionID() *int64 {
101+
serverConnectionID := int64(42)
96102
return &serverConnectionID
97103
}
98104

x/mongo/driver/driver.go

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -68,7 +68,8 @@ type Connection interface {
6868
Close() error
6969

7070
ID() string
71-
ServerConnectionID() *int32
71+
ServerConnectionID() *int64
72+
DriverConnectionID() uint64 // TODO(GODRIVER-2824): change type to int64.
7273
Address() address.Address
7374
Stale() bool
7475
}
@@ -178,7 +179,7 @@ type ErrorProcessor interface {
178179
type HandshakeInformation struct {
179180
Description description.Server
180181
SpeculativeAuthenticate bsoncore.Document
181-
ServerConnectionID *int32
182+
ServerConnectionID *int64
182183
SaslSupportedMechs []string
183184
}
184185

x/mongo/driver/drivertest/channel_conn.go

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -64,9 +64,15 @@ func (c *ChannelConn) ID() string {
6464
return "faked"
6565
}
6666

67+
// DriverConnectionID implements the driver.Connection interface.
68+
// TODO(GODRIVER-2824): replace return type with int64.
69+
func (c *ChannelConn) DriverConnectionID() uint64 {
70+
return 0
71+
}
72+
6773
// ServerConnectionID implements the driver.Connection interface.
68-
func (c *ChannelConn) ServerConnectionID() *int32 {
69-
serverConnectionID := int32(42)
74+
func (c *ChannelConn) ServerConnectionID() *int64 {
75+
serverConnectionID := int64(42)
7076
return &serverConnectionID
7177
}
7278

x/mongo/driver/operation.go

Lines changed: 40 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ import (
1111
"context"
1212
"errors"
1313
"fmt"
14+
"math"
1415
"strconv"
1516
"strings"
1617
"sync"
@@ -91,22 +92,40 @@ type startedInformation struct {
9192
cmdName string
9293
documentSequenceIncluded bool
9394
connID string
94-
serverConnID *int32
95+
driverConnectionID uint64 // TODO(GODRIVER-2824): change type to int64.
96+
serverConnID *int64
9597
redacted bool
9698
serviceID *primitive.ObjectID
9799
}
98100

99101
// finishedInformation keeps track of all of the information necessary for monitoring success and failure events.
100102
type finishedInformation struct {
101-
cmdName string
102-
requestID int32
103-
response bsoncore.Document
104-
cmdErr error
105-
connID string
106-
serverConnID *int32
107-
startTime time.Time
108-
redacted bool
109-
serviceID *primitive.ObjectID
103+
cmdName string
104+
requestID int32
105+
response bsoncore.Document
106+
cmdErr error
107+
connID string
108+
driverConnectionID uint64 // TODO(GODRIVER-2824): change type to int64.
109+
serverConnID *int64
110+
startTime time.Time
111+
redacted bool
112+
serviceID *primitive.ObjectID
113+
}
114+
115+
// convertInt64PtrToInt32Ptr will convert an int64 pointer reference to an int32 pointer
116+
// reference. If the int64 value cannot be converted to int32 without causing
117+
// an overflow, then this function will return nil.
118+
func convertInt64PtrToInt32Ptr(i64 *int64) *int32 {
119+
if i64 == nil {
120+
return nil
121+
}
122+
123+
if *i64 > math.MaxInt32 || *i64 < math.MinInt32 {
124+
return nil
125+
}
126+
127+
i32 := int32(*i64)
128+
return &i32
110129
}
111130

112131
// ResponseInfo contains the context required to parse a server response.
@@ -552,6 +571,7 @@ func (op Operation) Execute(ctx context.Context) error {
552571

553572
// set extra data and send event if possible
554573
startedInfo.connID = conn.ID()
574+
startedInfo.driverConnectionID = conn.DriverConnectionID()
555575
startedInfo.cmdName = op.getCommandName(startedInfo.cmd)
556576
op.cmdName = startedInfo.cmdName
557577
startedInfo.redacted = op.redactCommand(startedInfo.cmdName, startedInfo.cmd)
@@ -574,13 +594,14 @@ func (op Operation) Execute(ctx context.Context) error {
574594
}
575595

576596
finishedInfo := finishedInformation{
577-
cmdName: startedInfo.cmdName,
578-
requestID: startedInfo.requestID,
579-
startTime: time.Now(),
580-
connID: startedInfo.connID,
581-
serverConnID: startedInfo.serverConnID,
582-
redacted: startedInfo.redacted,
583-
serviceID: startedInfo.serviceID,
597+
cmdName: startedInfo.cmdName,
598+
requestID: startedInfo.requestID,
599+
startTime: time.Now(),
600+
connID: startedInfo.connID,
601+
driverConnectionID: startedInfo.driverConnectionID,
602+
serverConnID: startedInfo.serverConnID,
603+
redacted: startedInfo.redacted,
604+
serviceID: startedInfo.serviceID,
584605
}
585606

586607
// Check for possible context error. If no context error, check if there's enough time to perform a
@@ -1704,7 +1725,7 @@ func (op Operation) publishStartedEvent(ctx context.Context, info startedInforma
17041725
CommandName: info.cmdName,
17051726
RequestID: int64(info.requestID),
17061727
ConnectionID: info.connID,
1707-
ServerConnectionID: info.serverConnID,
1728+
ServerConnectionID: convertInt64PtrToInt32Ptr(info.serverConnID),
17081729
ServiceID: info.serviceID,
17091730
}
17101731
op.CommandMonitor.Started(ctx, started)
@@ -1732,7 +1753,7 @@ func (op Operation) publishFinishedEvent(ctx context.Context, info finishedInfor
17321753
RequestID: int64(info.requestID),
17331754
ConnectionID: info.connID,
17341755
DurationNanos: durationNanos,
1735-
ServerConnectionID: info.serverConnID,
1756+
ServerConnectionID: convertInt64PtrToInt32Ptr(info.serverConnID),
17361757
ServiceID: info.serviceID,
17371758
}
17381759

x/mongo/driver/operation/hello.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -240,7 +240,7 @@ func (h *Hello) GetHandshakeInformation(ctx context.Context, _ address.Address,
240240
if speculativeAuthenticate, ok := h.res.Lookup("speculativeAuthenticate").DocumentOK(); ok {
241241
info.SpeculativeAuthenticate = speculativeAuthenticate
242242
}
243-
if serverConnectionID, ok := h.res.Lookup("connectionId").Int32OK(); ok {
243+
if serverConnectionID, ok := h.res.Lookup("connectionId").AsInt64OK(); ok {
244244
info.ServerConnectionID = &serverConnectionID
245245
}
246246
// Cast to bson.Raw to lookup saslSupportedMechs to avoid converting from bsoncore.Value to bson.RawValue for the

x/mongo/driver/operation_test.go

Lines changed: 58 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ import (
1010
"bytes"
1111
"context"
1212
"errors"
13+
"math"
1314
"testing"
1415
"time"
1516

@@ -748,7 +749,7 @@ type mockConnection struct {
748749
rDesc description.Server
749750
rCloseErr error
750751
rID string
751-
rServerConnID *int32
752+
rServerConnID *int64
752753
rAddr address.Address
753754
rCanStream bool
754755
rStreaming bool
@@ -757,13 +758,16 @@ type mockConnection struct {
757758
func (m *mockConnection) Description() description.Server { return m.rDesc }
758759
func (m *mockConnection) Close() error { return m.rCloseErr }
759760
func (m *mockConnection) ID() string { return m.rID }
760-
func (m *mockConnection) ServerConnectionID() *int32 { return m.rServerConnID }
761+
func (m *mockConnection) ServerConnectionID() *int64 { return m.rServerConnID }
761762
func (m *mockConnection) Address() address.Address { return m.rAddr }
762763
func (m *mockConnection) SupportsStreaming() bool { return m.rCanStream }
763764
func (m *mockConnection) CurrentlyStreaming() bool { return m.rStreaming }
764765
func (m *mockConnection) SetStreaming(streaming bool) { m.rStreaming = streaming }
765766
func (m *mockConnection) Stale() bool { return false }
766767

768+
// TODO:(GODRIVER-2824) replace return type with int64.
769+
func (m *mockConnection) DriverConnectionID() uint64 { return 0 }
770+
767771
func (m *mockConnection) WriteWireMessage(_ context.Context, wm []byte) error {
768772
m.pWriteWM = wm
769773
return m.rWriteErr
@@ -836,3 +840,55 @@ func TestRetry(t *testing.T) {
836840
"expected operation to complete only after the context deadline is exceeded")
837841
})
838842
}
843+
844+
func TestConvertI64PtrToI32Ptr(t *testing.T) {
845+
t.Parallel()
846+
847+
newI64 := func(i64 int64) *int64 { return &i64 }
848+
newI32 := func(i32 int32) *int32 { return &i32 }
849+
850+
tests := []struct {
851+
name string
852+
i64 *int64
853+
want *int32
854+
}{
855+
{
856+
name: "empty",
857+
want: nil,
858+
},
859+
{
860+
name: "in bounds",
861+
i64: newI64(1),
862+
want: newI32(1),
863+
},
864+
{
865+
name: "out of bounds negative",
866+
i64: newI64(math.MinInt32 - 1),
867+
},
868+
{
869+
name: "out of bounds positive",
870+
i64: newI64(math.MaxInt32 + 1),
871+
},
872+
{
873+
name: "exact min int32",
874+
i64: newI64(math.MinInt32),
875+
want: newI32(math.MinInt32),
876+
},
877+
{
878+
name: "exact max int32",
879+
i64: newI64(math.MaxInt32),
880+
want: newI32(math.MaxInt32),
881+
},
882+
}
883+
884+
for _, test := range tests {
885+
test := test
886+
887+
t.Run(test.name, func(t *testing.T) {
888+
t.Parallel()
889+
890+
got := convertInt64PtrToInt32Ptr(test.i64)
891+
assert.Equal(t, test.want, got, "convertInt64PtrToInt32Ptr()=%v, got %v", test.want, got)
892+
})
893+
}
894+
}

x/mongo/driver/session/client_session.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -86,7 +86,8 @@ type LoadBalancedTransactionConnection interface {
8686
Description() description.Server
8787
Close() error
8888
ID() string
89-
ServerConnectionID() *int32
89+
ServerConnectionID() *int64
90+
DriverConnectionID() uint64 // TODO(GODRIVER-2824): change type to int64.
9091
Address() address.Address
9192
Stale() bool
9293

x/mongo/driver/topology/connection.go

Lines changed: 20 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -70,12 +70,14 @@ type connection struct {
7070
currentlyStreaming bool
7171
connectContextMutex sync.Mutex
7272
cancellationListener cancellationListener
73-
serverConnectionID *int32 // the server's ID for this client's connection
73+
serverConnectionID *int64 // the server's ID for this client's connection
7474

7575
// pool related fields
76-
pool *pool
77-
poolID uint64
78-
generation uint64
76+
pool *pool
77+
78+
// TODO(GODRIVER-2824): change driverConnectionID type to int64.
79+
driverConnectionID uint64
80+
generation uint64
7981
}
8082

8183
// newConnection handles the creation of a connection. It does not connect the connection.
@@ -105,6 +107,12 @@ func newConnection(addr address.Address, opts ...ConnectionOption) *connection {
105107
return c
106108
}
107109

110+
// DriverConnectionID returns the driver connection ID.
111+
// TODO(GODRIVER-2824): change return type to int64.
112+
func (c *connection) DriverConnectionID() uint64 {
113+
return c.driverConnectionID
114+
}
115+
108116
// setGenerationNumber sets the connection's generation number if a callback has been provided to do so in connection
109117
// configuration.
110118
func (c *connection) setGenerationNumber() {
@@ -527,7 +535,7 @@ func (c *connection) ID() string {
527535
return c.id
528536
}
529537

530-
func (c *connection) ServerConnectionID() *int32 {
538+
func (c *connection) ServerConnectionID() *int64 {
531539
return c.serverConnectionID
532540
}
533541

@@ -708,7 +716,7 @@ func (c *Connection) ID() string {
708716
}
709717

710718
// ServerConnectionID returns the server connection ID of this connection.
711-
func (c *Connection) ServerConnectionID() *int32 {
719+
func (c *Connection) ServerConnectionID() *int64 {
712720
if c.connection == nil {
713721
return nil
714722
}
@@ -794,6 +802,12 @@ func (c *Connection) unpin(reason string) error {
794802
return nil
795803
}
796804

805+
// DriverConnectionID returns the driver connection ID.
806+
// TODO(GODRIVER-2824): change return type to int64.
807+
func (c *Connection) DriverConnectionID() uint64 {
808+
return c.connection.DriverConnectionID()
809+
}
810+
797811
func configureTLS(ctx context.Context,
798812
tlsConnSource tlsConnectionSource,
799813
nc net.Conn,

x/mongo/driver/topology/connection_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -763,7 +763,7 @@ func TestConnection(t *testing.T) {
763763
t.Errorf("LocalAddresses do not match. got %v; want %v", got, want)
764764
}
765765

766-
want = (*int32)(nil)
766+
want = (*int64)(nil)
767767
got = conn.ServerConnectionID()
768768
if !cmp.Equal(got, want) {
769769
t.Errorf("ServerConnectionIDs do not match. got %v; want %v", got, want)

0 commit comments

Comments
 (0)