Repository: netty/netty
Issue: Improved channel ID entropy
Labels: improvement

The Netty channel ID is an ideal key that can be used to help track and diagnose problems occurring within a server. For example I log the channel ID with all notable events that occur in my server. I also store usage and throughput statistic keyed on channel ID. These can then be queried and correlated when we have an issue to diagnose. I could create my own GUID for this but the existing channel ID is great for this as it is always available everywhere in your code that you need to log an IO event. 

Unfortunately, however, the current 32-bit channel ID which is based on System.identityHashCode(channel) has poor entropy making it less useful than it could be. On testing a server handling many thousands of connections over a long period of time I have never observed a channel ID greater than 0x01FFFFFF. Over time, as client connections come and go, repeat channel IDs are more common that you would expect from a 32-bit value.

It would be helpful if either:
1. The algorithm in AbstractChannel could be changed to use a 32-bit random number, or
2. There was a mechanism to override the default algorithm (perhaps via the ChannelFactory). 


Issue comments:
Ah, if only this was Launchpad - I'd assign myself to this 

I've made a patch to use Random, but it looks like the IDs have to be negative - To "avoid conflicts with user-specified IDs"

I'll make this pull request and add some test cases later, then if required, I'll see if it's viable to use Long values instead :)

Thank to @CruzBishop for the patches!

Any time :)

Have you considered using `ThreadLocalRandom` to reduce concurrent contendancy?

@shpikat, sounds like a good idea


Pull request review comments:
Please remove the public getter..

Alright


Patch that resolved the issue:
diff --git a/src/main/java/org/jboss/netty/channel/AbstractChannel.java b/src/main/java/org/jboss/netty/channel/AbstractChannel.java
index 6de954403ee..bf182668275 100644
--- a/src/main/java/org/jboss/netty/channel/AbstractChannel.java
+++ b/src/main/java/org/jboss/netty/channel/AbstractChannel.java
@@ -16,6 +16,7 @@
 package org.jboss.netty.channel;
 
 import java.net.SocketAddress;
+import java.util.Random;
 import java.util.concurrent.ConcurrentMap;
 
 import org.jboss.netty.util.internal.ConcurrentHashMap;
@@ -27,8 +28,10 @@ public abstract class AbstractChannel implements Channel {
 
     static final ConcurrentMap<Integer, Channel> allChannels = new ConcurrentHashMap<Integer, Channel>();
 
+    private static final Random random = new Random();
+
     private static Integer allocateId(Channel channel) {
-        Integer id = System.identityHashCode(channel);
+        Integer id = random.nextInt();
         for (;;) {
             // Loop until a unique ID is acquired.
             // It should be found in one loop practically.
@@ -139,12 +142,11 @@ protected ChannelFuture getUnsupportedOperationFuture() {
     }
 
     /**
-     * Returns the {@linkplain System#identityHashCode(Object) identity hash code}
-     * of this channel.
+     * Returns the ID of this channel.
      */
     @Override
     public final int hashCode() {
-        return System.identityHashCode(this);
+        return this.id;
     }
 
     /**


Test patch: