Skip to content

Commit 1d2591f

Browse files
daschlMichael Nitschinger
authored and
Michael Nitschinger
committed
JVMCBC-823: Add a global portion to the core ID.
This changeset adds a fixed somewhat globally unique 32 bit integer to the core id so that it now consist of a global and a local section. This allows helping identify an instance across logs but also distinguish multiple core instances inside the same JVM. Output changed from "coreId": 1 to "coreId":"0x47215cfa00000001" Change-Id: Ic71bbb0030563d28625b6987604e9304fcbd9c76 Reviewed-on: http://review.couchbase.org/122391 Tested-by: Build Bot <[email protected]> Reviewed-by: Graham Pople <[email protected]>
1 parent e7f5c00 commit 1d2591f

File tree

3 files changed

+28
-5
lines changed

3 files changed

+28
-5
lines changed

core-io/src/main/java/com/couchbase/client/core/Core.java

Lines changed: 26 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -57,15 +57,18 @@
5757
import reactor.core.publisher.Flux;
5858
import reactor.core.publisher.Mono;
5959

60+
import java.security.SecureRandom;
6061
import java.time.Duration;
6162
import java.util.ArrayList;
6263
import java.util.Arrays;
6364
import java.util.Map;
6465
import java.util.Optional;
66+
import java.util.Random;
6567
import java.util.Set;
68+
import java.util.UUID;
6669
import java.util.concurrent.CopyOnWriteArrayList;
6770
import java.util.concurrent.atomic.AtomicBoolean;
68-
import java.util.concurrent.atomic.AtomicLong;
71+
import java.util.concurrent.atomic.AtomicInteger;
6972
import java.util.stream.Stream;
7073

7174
import static com.couchbase.client.core.util.CbCollections.isNullOrEmpty;
@@ -81,10 +84,15 @@
8184
@Stability.Volatile
8285
public class Core {
8386

87+
/**
88+
* A reasonably unique instance ID.
89+
*/
90+
private static final int GLOBAL_ID = new SecureRandom().nextInt();
91+
8492
/**
8593
* Counts up core ids for each new instance.
8694
*/
87-
private static final AtomicLong CORE_IDS = new AtomicLong();
95+
private static final AtomicInteger CORE_IDS = new AtomicInteger();
8896

8997
/**
9098
* Locates the right node for the KV service.
@@ -192,7 +200,7 @@ protected Core(final CoreEnvironment environment, final Authenticator authentica
192200
}
193201

194202
this.seedNodes = seedNodes;
195-
this.coreContext = new CoreContext(this, CORE_IDS.incrementAndGet(), environment, authenticator);
203+
this.coreContext = new CoreContext(this, createInstanceId(), environment, authenticator);
196204
this.configurationProvider = createConfigurationProvider();
197205
this.nodes = new CopyOnWriteArrayList<>();
198206
this.eventBus = environment.eventBus();
@@ -205,6 +213,21 @@ protected Core(final CoreEnvironment environment, final Authenticator authentica
205213
eventBus.publish(new CoreCreatedEvent(coreContext, environment));
206214
}
207215

216+
/**
217+
* Creates a (somewhat) globally unique ID for this instance.
218+
* <p>
219+
* The 64 bit long is split up into an upper and lower 32 bit halves. The upper half
220+
* is reusing the same global ID for all instances while the lower half is always
221+
* incrementing for each instance. So it has a global and a local component which can
222+
* be used to correlate instances across logs but also help distinguish multiple
223+
* instances in the same JVM.
224+
*
225+
* @return the created instance ID.
226+
*/
227+
private long createInstanceId() {
228+
return (((long) GLOBAL_ID) << 32) | (CORE_IDS.incrementAndGet() & 0xffffffffL);
229+
}
230+
208231
/**
209232
* During testing this can be overridden so that a custom configuration provider is used
210233
* in the system.

core-io/src/main/java/com/couchbase/client/core/CoreContext.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -125,7 +125,7 @@ public Core core() {
125125

126126
@Override
127127
public void injectExportableParams(final Map<String, Object> input) {
128-
input.put("coreId", id);
128+
input.put("coreId", "0x" + Long.toHexString(id));
129129
alternateAddress.ifPresent(a -> input.put("alternateIdentifier", a));
130130
}
131131

core-io/src/test/java/com/couchbase/client/core/CoreContextTest.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@ void getAndExportProperties() {
4747
assertEquals(authenticator, ctx.authenticator());
4848

4949
String result = ctx.exportAsString(Context.ExportFormat.JSON);
50-
assertEquals("{\"coreId\":12345}", result);
50+
assertEquals("{\"coreId\":\"0x3039\"}", result);
5151
}
5252

5353
@Test

0 commit comments

Comments
 (0)