Skip to content

Commit a3c62a7

Browse files
committed
Simplify ActiveChannelTracker to not store channels
It previously needed to store channels for purging of network connections on error. Now this functionality is not needed because driver never performs an active purge. This commit makes `ActiveChannelTracker` only store number of active connections per host, not the actual connections.
1 parent 827c228 commit a3c62a7

File tree

2 files changed

+11
-70
lines changed

2 files changed

+11
-70
lines changed

driver/src/main/java/org/neo4j/driver/internal/async/pool/ActiveChannelTracker.java

Lines changed: 11 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -20,10 +20,10 @@
2020

2121
import io.netty.channel.Channel;
2222
import io.netty.channel.pool.ChannelPoolHandler;
23-
import io.netty.util.internal.ConcurrentSet;
2423

24+
import java.util.Map;
2525
import java.util.concurrent.ConcurrentHashMap;
26-
import java.util.concurrent.ConcurrentMap;
26+
import java.util.concurrent.atomic.AtomicInteger;
2727

2828
import org.neo4j.driver.internal.BoltServerAddress;
2929
import org.neo4j.driver.v1.Logger;
@@ -33,12 +33,11 @@
3333

3434
public class ActiveChannelTracker implements ChannelPoolHandler
3535
{
36-
private final ConcurrentMap<BoltServerAddress,ConcurrentSet<Channel>> addressToActiveChannelCount;
36+
private final Map<BoltServerAddress,AtomicInteger> addressToActiveChannelCount = new ConcurrentHashMap<>();
3737
private final Logger log;
3838

3939
public ActiveChannelTracker( Logging logging )
4040
{
41-
this.addressToActiveChannelCount = new ConcurrentHashMap<>();
4241
this.log = logging.getLog( getClass().getSimpleName() );
4342
}
4443

@@ -65,52 +64,25 @@ public void channelCreated( Channel channel )
6564

6665
public int activeChannelCount( BoltServerAddress address )
6766
{
68-
ConcurrentSet<Channel> activeChannels = addressToActiveChannelCount.get( address );
69-
return activeChannels == null ? 0 : activeChannels.size();
70-
}
71-
72-
public void purge( BoltServerAddress address )
73-
{
74-
ConcurrentSet<Channel> activeChannels = addressToActiveChannelCount.remove( address );
75-
if ( activeChannels != null )
76-
{
77-
for ( Channel channel : activeChannels )
78-
{
79-
channel.close();
80-
}
81-
}
67+
AtomicInteger count = addressToActiveChannelCount.get( address );
68+
return count == null ? 0 : count.get();
8269
}
8370

8471
private void channelActive( Channel channel )
8572
{
8673
BoltServerAddress address = serverAddress( channel );
87-
ConcurrentSet<Channel> activeChannels = addressToActiveChannelCount.get( address );
88-
if ( activeChannels == null )
89-
{
90-
ConcurrentSet<Channel> newActiveChannels = new ConcurrentSet<>();
91-
ConcurrentSet<Channel> existingActiveChannels = addressToActiveChannelCount.putIfAbsent( address,
92-
newActiveChannels );
93-
if ( existingActiveChannels == null )
94-
{
95-
activeChannels = newActiveChannels;
96-
}
97-
else
98-
{
99-
activeChannels = existingActiveChannels;
100-
}
101-
}
102-
103-
activeChannels.add( channel );
74+
AtomicInteger count = addressToActiveChannelCount.computeIfAbsent( address, k -> new AtomicInteger() );
75+
count.incrementAndGet();
10476
}
10577

10678
private void channelInactive( Channel channel )
10779
{
10880
BoltServerAddress address = serverAddress( channel );
109-
ConcurrentSet<Channel> activeChannels = addressToActiveChannelCount.get( address );
110-
if ( activeChannels == null )
81+
AtomicInteger count = addressToActiveChannelCount.get( address );
82+
if ( count == null )
11183
{
112-
throw new IllegalStateException( "No channels exist for address '" + address + "'" );
84+
throw new IllegalStateException( "No count exist for address '" + address + "'" );
11385
}
114-
activeChannels.remove( channel );
86+
count.decrementAndGet();
11587
}
11688
}

driver/src/test/java/org/neo4j/driver/internal/async/pool/ActiveChannelTrackerTest.java

Lines changed: 0 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -26,12 +26,10 @@
2626

2727
import static org.hamcrest.Matchers.instanceOf;
2828
import static org.junit.Assert.assertEquals;
29-
import static org.junit.Assert.assertNull;
3029
import static org.junit.Assert.assertThat;
3130
import static org.junit.Assert.fail;
3231
import static org.neo4j.driver.internal.async.ChannelAttributes.setServerAddress;
3332
import static org.neo4j.driver.internal.logging.DevNullLogging.DEV_NULL_LOGGING;
34-
import static org.neo4j.driver.v1.util.TestUtil.await;
3533

3634
public class ActiveChannelTrackerTest
3735
{
@@ -106,35 +104,6 @@ public void shouldReturnZeroActiveCountForUnknownAddress()
106104
assertEquals( 0, tracker.activeChannelCount( address ) );
107105
}
108106

109-
@Test
110-
public void shouldPruneForMissingAddress()
111-
{
112-
assertEquals( 0, tracker.activeChannelCount( address ) );
113-
tracker.purge( address );
114-
assertEquals( 0, tracker.activeChannelCount( address ) );
115-
}
116-
117-
@Test
118-
public void shouldPruneForExistingAddress()
119-
{
120-
Channel channel1 = newChannel();
121-
Channel channel2 = newChannel();
122-
Channel channel3 = newChannel();
123-
124-
tracker.channelAcquired( channel1 );
125-
tracker.channelAcquired( channel2 );
126-
tracker.channelAcquired( channel3 );
127-
128-
assertEquals( 3, tracker.activeChannelCount( address ) );
129-
130-
tracker.purge( address );
131-
132-
assertEquals( 0, tracker.activeChannelCount( address ) );
133-
assertNull( await( channel1.closeFuture() ) );
134-
assertNull( await( channel2.closeFuture() ) );
135-
assertNull( await( channel3.closeFuture() ) );
136-
}
137-
138107
private Channel newChannel()
139108
{
140109
EmbeddedChannel channel = new EmbeddedChannel();

0 commit comments

Comments
 (0)