Skip to content

Commit 983dc9a

Browse files
JCBC-2165 no channel id in EndpointConnectedEvent
Change-Id: I13a10a81b8c19d5a2576e8163fe6d6d7454d0b4b Reviewed-on: https://review.couchbase.org/c/couchbase-jvm-clients/+/231352 Reviewed-by: David Nault <[email protected]> Tested-by: Build Bot <[email protected]>
1 parent c668e93 commit 983dc9a

File tree

3 files changed

+30
-27
lines changed

3 files changed

+30
-27
lines changed

core-io/src/main/java/com/couchbase/client/core/endpoint/BaseEndpoint.java

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -79,6 +79,7 @@
7979
import java.net.SocketAddress;
8080
import java.time.Duration;
8181
import java.util.Optional;
82+
import java.util.Random;
8283
import java.util.concurrent.CompletableFuture;
8384
import java.util.concurrent.TimeUnit;
8485
import java.util.concurrent.TimeoutException;
@@ -274,6 +275,32 @@ protected SocketAddress remoteAddress() {
274275
return InetSocketAddress.createUnresolved(ctx.remoteSocket().host(), ctx.remoteSocket().port());
275276
}
276277

278+
/**
279+
* Pad the long input into a string encoded hex value.
280+
*
281+
* @param input the number to format.
282+
* @return the padded long hex value.
283+
*/
284+
private static String paddedHex(long input) {
285+
return String.format("%016X", input);
286+
}
287+
288+
private static String getChannelId(Channel channel, EndpointContext endpointContext) {
289+
String channelId = "0x" + channel.id().asShortText();
290+
291+
long convertedChannelId;
292+
try {
293+
convertedChannelId = channelId.equals("0xembedded") ? 1L : Long.decode(channelId);
294+
} catch (NumberFormatException ex) {
295+
// This is just a safeguard in place should the netty channel ID
296+
// format ever change and trigger a failure of decoding the channel ID into a long
297+
convertedChannelId = new Random().nextInt();
298+
}
299+
300+
String paddedChannelId = paddedHex(convertedChannelId);
301+
return paddedHex(endpointContext.id()) + "/" + paddedChannelId;
302+
}
303+
277304
/**
278305
* This method performs the actual connecting logic.
279306
*
@@ -319,6 +346,7 @@ private void reconnect() {
319346
@Override
320347
protected void initChannel(final Channel ch) {
321348
ChannelPipeline pipeline = ch.pipeline();
349+
ch.attr(ChannelAttributes.CHANNEL_ID_KEY).set(getChannelId(ch, endpointContext));
322350

323351
SecurityConfig config = env.securityConfig();
324352
if (config.tlsEnabled()) {

core-io/src/main/java/com/couchbase/client/core/io/netty/kv/FeatureNegotiatingHandler.java

Lines changed: 1 addition & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,6 @@
3737
import java.util.EnumSet;
3838
import java.util.List;
3939
import java.util.Optional;
40-
import java.util.Random;
4140
import java.util.Set;
4241
import java.util.TreeMap;
4342
import java.util.concurrent.TimeUnit;
@@ -301,35 +300,11 @@ private ByteBuf buildHelloKey(final ChannelHandlerContext ctx) {
301300
agent = agent.substring(0, 200);
302301
}
303302
result.put("a", agent);
304-
305-
String channelId = "0x" + ctx.channel().id().asShortText();
306-
long convertedChannelId;
307-
try {
308-
convertedChannelId = channelId.equals("0xembedded") ? 1L : Long.decode(channelId);
309-
} catch (NumberFormatException ex) {
310-
// This is just a safeguard in place should the netty channel ID
311-
// format ever change and trigger a failure of decoding the channel ID into a long
312-
convertedChannelId = new Random().nextInt();
313-
}
314-
String paddedChannelId = paddedHex(convertedChannelId);
315-
String fullId = paddedHex(endpointContext.id()) + "/" + paddedChannelId;
316-
result.put("i", fullId);
317-
318-
ctx.channel().attr(ChannelAttributes.CHANNEL_ID_KEY).set(fullId);
303+
result.put("i", ctx.channel().attr(ChannelAttributes.CHANNEL_ID_KEY).get());
319304

320305
return ctx.alloc().buffer().writeBytes(Mapper.encodeAsBytes(result));
321306
}
322307

323-
/**
324-
* Pad the long input into a string encoded hex value.
325-
*
326-
* @param input the number to format.
327-
* @return the padded long hex value.
328-
*/
329-
private static String paddedHex(long input) {
330-
return String.format("%016X", input);
331-
}
332-
333308
/**
334309
* If there is an exception raised while we are waiting for our connect phase to complete, the error
335310
* should be propagated as a cause up the pipeline.

core-io/src/test/java/com/couchbase/client/core/io/netty/kv/FeatureNegotiatingHandlerTest.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -182,7 +182,7 @@ void encodeAndSendHelloRequest(Set<ServerFeature> enabledFeatures) {
182182

183183
assertEquals(handler, channel.pipeline().get(FeatureNegotiatingHandler.class));
184184
channel.connect(new InetSocketAddress("1.2.3.4", 1234));
185-
185+
channel.attr(ChannelAttributes.CHANNEL_ID_KEY).set("0000000000000001/0000000000000001");
186186
channel.pipeline().fireChannelActive();
187187
channel.runPendingTasks();
188188
ByteBuf writtenRequest = channel.readOutbound();

0 commit comments

Comments
 (0)