Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Make ConnectionId.serverValue/localValue of the Long/long type #1280

Merged
merged 3 commits into from
Jan 5, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
26 changes: 12 additions & 14 deletions driver-core/src/main/com/mongodb/connection/ConnectionId.java
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@
import com.mongodb.lang.Nullable;

import java.util.Objects;
import java.util.concurrent.atomic.AtomicInteger;
import java.util.concurrent.atomic.AtomicLong;

import static com.mongodb.assertions.Assertions.isTrue;
import static com.mongodb.assertions.Assertions.notNull;
Expand All @@ -35,11 +35,12 @@
*/
@Immutable
public final class ConnectionId {
private static final AtomicInteger INCREMENTING_ID = new AtomicInteger();
private static final AtomicLong INCREMENTING_ID = new AtomicLong();

private final ServerId serverId;
private final int localValue;
private final Integer serverValue;
private final long localValue;
@Nullable
private final Long serverValue;
private final String stringValue;

/**
Expand All @@ -56,16 +57,16 @@ public ConnectionId(final ServerId serverId) {
* Construct an instance with the given serverId, localValue, and serverValue.
*
* <p>
* Useful for testing, but generally prefer {@link #withServerValue(int)}
* Useful for testing, but generally prefer {@link #withServerValue(long)}
* </p>
*
* @param serverId the server id
* @param localValue the local value
* @param serverValue the server value, which may be null
* @see #withServerValue(int)
* @see #withServerValue(long)
* @since 3.11
*/
public ConnectionId(final ServerId serverId, final int localValue, @Nullable final Integer serverValue) {
public ConnectionId(final ServerId serverId, final long localValue, @Nullable final Long serverValue) {
this.serverId = notNull("serverId", serverId);
this.localValue = localValue;
this.serverValue = serverValue;
Expand All @@ -83,7 +84,7 @@ public ConnectionId(final ServerId serverId, final int localValue, @Nullable fin
* @return the new connection id
* @since 3.8
*/
public ConnectionId withServerValue(final int serverValue) {
public ConnectionId withServerValue(final long serverValue) {
isTrue("server value is null", this.serverValue == null);
return new ConnectionId(serverId, localValue, serverValue);
}
Expand All @@ -102,7 +103,7 @@ public ServerId getServerId() {
*
* @return the locally created id value for the connection
*/
public int getLocalValue() {
public long getLocalValue() {
return localValue;
}

Expand All @@ -112,7 +113,7 @@ public int getLocalValue() {
* @return the server generated id value for the connection or null if not set.
*/
@Nullable
public Integer getServerValue() {
public Long getServerValue() {
return serverValue;
}

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

@Override
public int hashCode() {
int result = serverId.hashCode();
result = 31 * result + localValue;
result = 31 * result + (serverValue != null ? serverValue.hashCode() : 0);
return result;
return Objects.hash(serverId, localValue, serverValue);
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1587,7 +1587,7 @@ boolean throwIfClosedOrPaused() {
}

}
private void logEventMessage(final String messageId, final String format, final int driverConnectionId) {
private void logEventMessage(final String messageId, final String format, final long driverConnectionId) {
ClusterId clusterId = serverId.getClusterId();
if (requiresLogging(clusterId)) {
List<LogMessage.Entry> entries = createBasicEntries();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ static ConnectionDescription createConnectionDescription(final ClusterConnection
helloResult.getArray("saslSupportedMechs", null), getLogicalSessionTimeoutMinutes(helloResult));
if (helloResult.containsKey("connectionId")) {
ConnectionId newConnectionId =
connectionDescription.getConnectionId().withServerValue(helloResult.getNumber("connectionId").intValue());
connectionDescription.getConnectionId().withServerValue(helloResult.getNumber("connectionId").longValue());
connectionDescription = connectionDescription.withConnectionId(newConnectionId);
}
if (clusterConnectionMode == ClusterConnectionMode.LOAD_BALANCED) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -244,7 +244,7 @@ private InternalConnectionInitializationDescription applyGetLastErrorResult(

if (getLastErrorResult.containsKey("connectionId")) {
connectionId = connectionDescription.getConnectionId()
.withServerValue(getLastErrorResult.getNumber("connectionId").intValue());
.withServerValue(getLastErrorResult.getNumber("connectionId").longValue());
} else {
connectionId = connectionDescription.getConnectionId();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,16 +26,16 @@ class ConnectionIdSpecification extends Specification {
def 'should set all properties'() {
given:
def id1 = new ConnectionId(serverId)
def id2 = new ConnectionId(serverId, 11, 32)
def id2 = new ConnectionId(serverId, Long.MAX_VALUE - 1, Long.MAX_VALUE)

expect:
id1.serverId == serverId
id1.localValue > 0
!id1.serverValue

id2.serverId == serverId
id2.localValue == 11
id2.serverValue == 32
id2.localValue == Long.MAX_VALUE - 1
id2.serverValue == Long.MAX_VALUE
}

def 'should increment local value'() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@
import java.util.concurrent.Executors;
import java.util.concurrent.Future;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.atomic.AtomicInteger;
import java.util.concurrent.atomic.AtomicLong;

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

private void assertConnectionIdMatch(final BsonDocument expectedEvent, final ConnectionId actualConnectionId) {
int actualConnectionIdLocalValue = actualConnectionId.getLocalValue();
int adjustedConnectionIdLocalValue = adjustedConnectionIdLocalValue(actualConnectionIdLocalValue);
long actualConnectionIdLocalValue = actualConnectionId.getLocalValue();
long adjustedConnectionIdLocalValue = adjustedConnectionIdLocalValue(actualConnectionIdLocalValue);
String connectionIdKey = "connectionId";
if (expectedEvent.containsKey(connectionIdKey)) {
int expectedConnectionId = expectedEvent.getInt32(connectionIdKey).intValue();
Expand All @@ -400,7 +400,7 @@ private void assertConnectionIdMatch(final BsonDocument expectedEvent, final Con
}
}

private int adjustedConnectionIdLocalValue(final int connectionIdLocalValue) {
private long adjustedConnectionIdLocalValue(final long connectionIdLocalValue) {
if (pool instanceof ConnectionIdAdjustingConnectionPool) {
return ((ConnectionIdAdjustingConnectionPool) pool).adjustedConnectionIdLocalValue(connectionIdLocalValue);
} else {
Expand Down Expand Up @@ -541,21 +541,21 @@ public static Style of(final String name) {
}

private static final class ConnectionIdAdjustingConnectionPool implements ConnectionPool {
private static final int UNINITIALIZED = Integer.MAX_VALUE;
private static final long UNINITIALIZED = Long.MAX_VALUE;

private final DefaultConnectionPool pool;
private final AtomicInteger connectionIdLocalValueAdjustment;
private final AtomicLong connectionIdLocalValueAdjustment;

private ConnectionIdAdjustingConnectionPool(final DefaultConnectionPool pool) {
this.pool = pool;
connectionIdLocalValueAdjustment = new AtomicInteger(UNINITIALIZED);
connectionIdLocalValueAdjustment = new AtomicLong(UNINITIALIZED);
}

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

int adjustedConnectionIdLocalValue(final int connectionIdLocalValue) {
long adjustedConnectionIdLocalValue(final long connectionIdLocalValue) {
return connectionIdLocalValue - connectionIdLocalValueAdjustment.get();
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -435,7 +435,7 @@ class InternalStreamConnectionInitializerSpecification extends Specification {
async << [true, false]
}

private ConnectionDescription getExpectedConnectionDescription(final Integer localValue, final Integer serverValue) {
private ConnectionDescription getExpectedConnectionDescription(final Long localValue, final Long serverValue) {
new ConnectionDescription(new ConnectionId(serverId, localValue, serverValue),
3, ServerType.STANDALONE, 512, 16777216, 33554432, [])
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -819,7 +819,7 @@ public void connectionClosed(final ConnectionClosedEvent event) {

private BsonDocument createEventDocument(final String name, final ConnectionId connectionId) {
return createEventDocument(name, connectionId.getServerId())
.append("connectionId", new BsonString(Integer.toString(connectionId.getLocalValue())));
.append("connectionId", new BsonString(Long.toString(connectionId.getLocalValue())));
}

private BsonDocument createEventDocument(final String name, final ServerId serverId) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,7 @@ public void assertCommandEventsEquality(final String client, final boolean ignor

if (expected.containsKey("hasServerConnectionId")) {
boolean hasServerConnectionId = expected.getBoolean("hasServerConnectionId").getValue();
Integer serverConnectionId = actual.getConnectionDescription().getConnectionId().getServerValue();
Long serverConnectionId = actual.getConnectionDescription().getConnectionId().getServerValue();
if (hasServerConnectionId) {
assertNotNull(context.getMessage("Expected serverConnectionId"), serverConnectionId);
} else {
Expand Down