Skip to content

Commit 26df551

Browse files
authored
Make ConnectionId.serverValue/localValue of the Long/long type (#1280)
JAVA-4846
1 parent e78a2dc commit 26df551

File tree

9 files changed

+29
-31
lines changed

9 files changed

+29
-31
lines changed

driver-core/src/main/com/mongodb/connection/ConnectionId.java

Lines changed: 12 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@
2020
import com.mongodb.lang.Nullable;
2121

2222
import java.util.Objects;
23-
import java.util.concurrent.atomic.AtomicInteger;
23+
import java.util.concurrent.atomic.AtomicLong;
2424

2525
import static com.mongodb.assertions.Assertions.isTrue;
2626
import static com.mongodb.assertions.Assertions.notNull;
@@ -35,11 +35,12 @@
3535
*/
3636
@Immutable
3737
public final class ConnectionId {
38-
private static final AtomicInteger INCREMENTING_ID = new AtomicInteger();
38+
private static final AtomicLong INCREMENTING_ID = new AtomicLong();
3939

4040
private final ServerId serverId;
41-
private final int localValue;
42-
private final Integer serverValue;
41+
private final long localValue;
42+
@Nullable
43+
private final Long serverValue;
4344
private final String stringValue;
4445

4546
/**
@@ -56,16 +57,16 @@ public ConnectionId(final ServerId serverId) {
5657
* Construct an instance with the given serverId, localValue, and serverValue.
5758
*
5859
* <p>
59-
* Useful for testing, but generally prefer {@link #withServerValue(int)}
60+
* Useful for testing, but generally prefer {@link #withServerValue(long)}
6061
* </p>
6162
*
6263
* @param serverId the server id
6364
* @param localValue the local value
6465
* @param serverValue the server value, which may be null
65-
* @see #withServerValue(int)
66+
* @see #withServerValue(long)
6667
* @since 3.11
6768
*/
68-
public ConnectionId(final ServerId serverId, final int localValue, @Nullable final Integer serverValue) {
69+
public ConnectionId(final ServerId serverId, final long localValue, @Nullable final Long serverValue) {
6970
this.serverId = notNull("serverId", serverId);
7071
this.localValue = localValue;
7172
this.serverValue = serverValue;
@@ -83,7 +84,7 @@ public ConnectionId(final ServerId serverId, final int localValue, @Nullable fin
8384
* @return the new connection id
8485
* @since 3.8
8586
*/
86-
public ConnectionId withServerValue(final int serverValue) {
87+
public ConnectionId withServerValue(final long serverValue) {
8788
isTrue("server value is null", this.serverValue == null);
8889
return new ConnectionId(serverId, localValue, serverValue);
8990
}
@@ -102,7 +103,7 @@ public ServerId getServerId() {
102103
*
103104
* @return the locally created id value for the connection
104105
*/
105-
public int getLocalValue() {
106+
public long getLocalValue() {
106107
return localValue;
107108
}
108109

@@ -112,7 +113,7 @@ public int getLocalValue() {
112113
* @return the server generated id value for the connection or null if not set.
113114
*/
114115
@Nullable
115-
public Integer getServerValue() {
116+
public Long getServerValue() {
116117
return serverValue;
117118
}
118119

@@ -142,10 +143,7 @@ public boolean equals(final Object o) {
142143

143144
@Override
144145
public int hashCode() {
145-
int result = serverId.hashCode();
146-
result = 31 * result + localValue;
147-
result = 31 * result + (serverValue != null ? serverValue.hashCode() : 0);
148-
return result;
146+
return Objects.hash(serverId, localValue, serverValue);
149147
}
150148

151149
@Override

driver-core/src/main/com/mongodb/internal/connection/DefaultConnectionPool.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1587,7 +1587,7 @@ boolean throwIfClosedOrPaused() {
15871587
}
15881588

15891589
}
1590-
private void logEventMessage(final String messageId, final String format, final int driverConnectionId) {
1590+
private void logEventMessage(final String messageId, final String format, final long driverConnectionId) {
15911591
ClusterId clusterId = serverId.getClusterId();
15921592
if (requiresLogging(clusterId)) {
15931593
List<LogMessage.Entry> entries = createBasicEntries();

driver-core/src/main/com/mongodb/internal/connection/DescriptionHelper.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -71,7 +71,7 @@ static ConnectionDescription createConnectionDescription(final ClusterConnection
7171
helloResult.getArray("saslSupportedMechs", null), getLogicalSessionTimeoutMinutes(helloResult));
7272
if (helloResult.containsKey("connectionId")) {
7373
ConnectionId newConnectionId =
74-
connectionDescription.getConnectionId().withServerValue(helloResult.getNumber("connectionId").intValue());
74+
connectionDescription.getConnectionId().withServerValue(helloResult.getNumber("connectionId").longValue());
7575
connectionDescription = connectionDescription.withConnectionId(newConnectionId);
7676
}
7777
if (clusterConnectionMode == ClusterConnectionMode.LOAD_BALANCED) {

driver-core/src/main/com/mongodb/internal/connection/InternalStreamConnectionInitializer.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -244,7 +244,7 @@ private InternalConnectionInitializationDescription applyGetLastErrorResult(
244244

245245
if (getLastErrorResult.containsKey("connectionId")) {
246246
connectionId = connectionDescription.getConnectionId()
247-
.withServerValue(getLastErrorResult.getNumber("connectionId").intValue());
247+
.withServerValue(getLastErrorResult.getNumber("connectionId").longValue());
248248
} else {
249249
connectionId = connectionDescription.getConnectionId();
250250
}

driver-core/src/test/unit/com/mongodb/connection/ConnectionIdSpecification.groovy

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -26,16 +26,16 @@ class ConnectionIdSpecification extends Specification {
2626
def 'should set all properties'() {
2727
given:
2828
def id1 = new ConnectionId(serverId)
29-
def id2 = new ConnectionId(serverId, 11, 32)
29+
def id2 = new ConnectionId(serverId, Long.MAX_VALUE - 1, Long.MAX_VALUE)
3030

3131
expect:
3232
id1.serverId == serverId
3333
id1.localValue > 0
3434
!id1.serverValue
3535

3636
id2.serverId == serverId
37-
id2.localValue == 11
38-
id2.serverValue == 32
37+
id2.localValue == Long.MAX_VALUE - 1
38+
id2.serverValue == Long.MAX_VALUE
3939
}
4040

4141
def 'should increment local value'() {

driver-core/src/test/unit/com/mongodb/internal/connection/AbstractConnectionPoolTest.java

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -77,7 +77,7 @@
7777
import java.util.concurrent.Executors;
7878
import java.util.concurrent.Future;
7979
import java.util.concurrent.TimeUnit;
80-
import java.util.concurrent.atomic.AtomicInteger;
80+
import java.util.concurrent.atomic.AtomicLong;
8181

8282
import static com.mongodb.assertions.Assertions.assertFalse;
8383
import static com.mongodb.internal.thread.InterruptionUtil.interruptAndCreateMongoInterruptedException;
@@ -386,8 +386,8 @@ private static void assertAddressMatch(final BsonDocument expectedEvent, final S
386386
}
387387

388388
private void assertConnectionIdMatch(final BsonDocument expectedEvent, final ConnectionId actualConnectionId) {
389-
int actualConnectionIdLocalValue = actualConnectionId.getLocalValue();
390-
int adjustedConnectionIdLocalValue = adjustedConnectionIdLocalValue(actualConnectionIdLocalValue);
389+
long actualConnectionIdLocalValue = actualConnectionId.getLocalValue();
390+
long adjustedConnectionIdLocalValue = adjustedConnectionIdLocalValue(actualConnectionIdLocalValue);
391391
String connectionIdKey = "connectionId";
392392
if (expectedEvent.containsKey(connectionIdKey)) {
393393
int expectedConnectionId = expectedEvent.getInt32(connectionIdKey).intValue();
@@ -400,7 +400,7 @@ private void assertConnectionIdMatch(final BsonDocument expectedEvent, final Con
400400
}
401401
}
402402

403-
private int adjustedConnectionIdLocalValue(final int connectionIdLocalValue) {
403+
private long adjustedConnectionIdLocalValue(final long connectionIdLocalValue) {
404404
if (pool instanceof ConnectionIdAdjustingConnectionPool) {
405405
return ((ConnectionIdAdjustingConnectionPool) pool).adjustedConnectionIdLocalValue(connectionIdLocalValue);
406406
} else {
@@ -541,21 +541,21 @@ public static Style of(final String name) {
541541
}
542542

543543
private static final class ConnectionIdAdjustingConnectionPool implements ConnectionPool {
544-
private static final int UNINITIALIZED = Integer.MAX_VALUE;
544+
private static final long UNINITIALIZED = Long.MAX_VALUE;
545545

546546
private final DefaultConnectionPool pool;
547-
private final AtomicInteger connectionIdLocalValueAdjustment;
547+
private final AtomicLong connectionIdLocalValueAdjustment;
548548

549549
private ConnectionIdAdjustingConnectionPool(final DefaultConnectionPool pool) {
550550
this.pool = pool;
551-
connectionIdLocalValueAdjustment = new AtomicInteger(UNINITIALIZED);
551+
connectionIdLocalValueAdjustment = new AtomicLong(UNINITIALIZED);
552552
}
553553

554554
private void updateConnectionIdLocalValueAdjustment(final InternalConnection conn) {
555555
connectionIdLocalValueAdjustment.accumulateAndGet(conn.getDescription().getConnectionId().getLocalValue() - 1, Math::min);
556556
}
557557

558-
int adjustedConnectionIdLocalValue(final int connectionIdLocalValue) {
558+
long adjustedConnectionIdLocalValue(final long connectionIdLocalValue) {
559559
return connectionIdLocalValue - connectionIdLocalValueAdjustment.get();
560560
}
561561

driver-core/src/test/unit/com/mongodb/internal/connection/InternalStreamConnectionInitializerSpecification.groovy

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -435,7 +435,7 @@ class InternalStreamConnectionInitializerSpecification extends Specification {
435435
async << [true, false]
436436
}
437437

438-
private ConnectionDescription getExpectedConnectionDescription(final Integer localValue, final Integer serverValue) {
438+
private ConnectionDescription getExpectedConnectionDescription(final Long localValue, final Long serverValue) {
439439
new ConnectionDescription(new ConnectionId(serverId, localValue, serverValue),
440440
3, ServerType.STANDALONE, 512, 16777216, 33554432, [])
441441
}

driver-sync/src/test/functional/com/mongodb/client/unified/Entities.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -819,7 +819,7 @@ public void connectionClosed(final ConnectionClosedEvent event) {
819819

820820
private BsonDocument createEventDocument(final String name, final ConnectionId connectionId) {
821821
return createEventDocument(name, connectionId.getServerId())
822-
.append("connectionId", new BsonString(Integer.toString(connectionId.getLocalValue())));
822+
.append("connectionId", new BsonString(Long.toString(connectionId.getLocalValue())));
823823
}
824824

825825
private BsonDocument createEventDocument(final String name, final ServerId serverId) {

driver-sync/src/test/functional/com/mongodb/client/unified/EventMatcher.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -99,7 +99,7 @@ public void assertCommandEventsEquality(final String client, final boolean ignor
9999

100100
if (expected.containsKey("hasServerConnectionId")) {
101101
boolean hasServerConnectionId = expected.getBoolean("hasServerConnectionId").getValue();
102-
Integer serverConnectionId = actual.getConnectionDescription().getConnectionId().getServerValue();
102+
Long serverConnectionId = actual.getConnectionDescription().getConnectionId().getServerValue();
103103
if (hasServerConnectionId) {
104104
assertNotNull(context.getMessage("Expected serverConnectionId"), serverConnectionId);
105105
} else {

0 commit comments

Comments
 (0)