Skip to content

Simplified state in connection queue #435

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

Merged
merged 1 commit into from
Nov 24, 2017
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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@
import java.util.concurrent.BlockingQueue;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.LinkedBlockingQueue;
import java.util.concurrent.atomic.AtomicBoolean;
import java.util.concurrent.atomic.AtomicInteger;

import org.neo4j.driver.internal.logging.DelegatingLogger;
import org.neo4j.driver.internal.net.BoltServerAddress;
Expand All @@ -40,12 +40,15 @@ public class BlockingPooledConnectionQueue
{
public static final String LOG_NAME = "ConnectionQueue";

private static final int ACTIVE = 1;
private static final int INACTIVE = 2;
private static final int TERMINATED = 3;

/** The backing queue, keeps track of connections currently in queue */
private final BlockingQueue<PooledConnection> queue;
private final Logger logger;

private final AtomicBoolean isDeactivated = new AtomicBoolean( false );
private final AtomicBoolean isTerminating = new AtomicBoolean( false );
private final AtomicInteger state = new AtomicInteger( ACTIVE );

/** Keeps track of acquired connections */
private final Set<PooledConnection> acquiredConnections =
Expand All @@ -72,7 +75,7 @@ public boolean offer( PooledConnection pooledConnection )
{
disposeSafely( pooledConnection );
}
if ( isDeactivated.get() || isTerminating.get() )
if ( state.get() != ACTIVE )
{
terminateIdleConnections();
}
Expand All @@ -93,12 +96,13 @@ public PooledConnection acquire( Supplier<PooledConnection> supplier )
}
acquiredConnections.add( connection );

if ( isDeactivated.get() || isTerminating.get() )
int poolState = state.get();
if ( poolState != ACTIVE )
{
acquiredConnections.remove( connection );
disposeSafely( connection );
throw new IllegalStateException( "Pool is " + (isDeactivated.get() ? "deactivated" : "terminated") + ", " +
"new connections can't be acquired" );
throw new IllegalStateException( "Pool is " + (poolState == INACTIVE ? "deactivated" : "terminated") +
", new connections can't be acquired" );
}
else
{
Expand Down Expand Up @@ -129,20 +133,20 @@ public boolean contains( PooledConnection pooledConnection )

public void activate()
{
isDeactivated.compareAndSet( true, false );
state.compareAndSet( INACTIVE, ACTIVE );
}

public void deactivate()
{
if ( isDeactivated.compareAndSet( false, true ) )
if ( state.compareAndSet( ACTIVE, INACTIVE ) )
{
terminateIdleConnections();
}
}

public boolean isActive()
{
return !isDeactivated.get();
return state.get() == ACTIVE;
}

/**
Expand All @@ -153,7 +157,7 @@ public boolean isActive()
*/
public void terminate()
{
if ( isTerminating.compareAndSet( false, true ) )
if ( state.getAndSet( TERMINATED ) != TERMINATED )
{
terminateIdleConnections();
terminateAcquiredConnections();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@
import static org.hamcrest.MatcherAssert.assertThat;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertSame;
import static org.junit.Assert.assertTrue;
import static org.junit.Assert.fail;
Expand Down Expand Up @@ -333,11 +334,104 @@ public void shouldTerminateOfferedConnectionWhenDeactivated()
}

@Test
public void shouldReportWhenActive()
public void shouldBeActiveWhenNotDeactivatedAndNotTerminated()
{
BlockingPooledConnectionQueue queue = newConnectionQueue( 1 );
assertTrue( queue.isActive() );
}

@Test
public void shouldNotBeActiveWhenDeactivated()
{
BlockingPooledConnectionQueue queue = newConnectionQueue( 1 );
assertTrue( queue.isActive() );
queue.deactivate();
assertFalse( queue.isActive() );
}

@Test
public void shouldNotBeActiveWhenTerminated()
{
BlockingPooledConnectionQueue queue = newConnectionQueue( 1 );
assertTrue( queue.isActive() );
queue.terminate();
assertFalse( queue.isActive() );
}

@Test
public void shouldBeActiveAfterDeactivationAndActivation()
{
BlockingPooledConnectionQueue queue = newConnectionQueue( 1 );
assertTrue( queue.isActive() );
queue.deactivate();
assertFalse( queue.isActive() );
queue.activate();
assertTrue( queue.isActive() );
}

@Test
public void shouldNotBeActiveAfterTerminationAndActivation()
{
BlockingPooledConnectionQueue queue = newConnectionQueue( 1 );
assertTrue( queue.isActive() );
queue.terminate();
assertFalse( queue.isActive() );
queue.activate();
assertFalse( queue.isActive() );
}

@Test
public void shouldBePossibleToAcquireFromActivatedQueue()
{
Supplier<PooledConnection> connectionSupplier = connectionSupplierMock();
when( connectionSupplier.get() ).thenReturn( mock( PooledConnection.class ) );
BlockingPooledConnectionQueue queue = newConnectionQueue( 3 );
queue.deactivate();

try
{
queue.acquire( connectionSupplier );
fail( "Exception expected" );
}
catch ( IllegalStateException e )
{
assertThat( e.getMessage(), startsWith( "Pool is deactivated" ) );
}

queue.activate();

assertNotNull( queue.acquire( connectionSupplier ) );
}

@Test
public void shouldNotBePossibleToActivateTerminatedQueue()
{
Supplier<PooledConnection> connectionSupplier = connectionSupplierMock();
when( connectionSupplier.get() ).thenReturn( mock( PooledConnection.class ) );
BlockingPooledConnectionQueue queue = newConnectionQueue( 3 );
queue.terminate();

try
{
queue.acquire( connectionSupplier );
fail( "Exception expected" );
}
catch ( IllegalStateException e )
{
assertThat( e.getMessage(), startsWith( "Pool is terminated" ) );
}

queue.activate();

try
{
queue.acquire( connectionSupplier );
fail( "Exception expected" );
}
catch ( IllegalStateException e )
{
assertThat( e.getMessage(), startsWith( "Pool is terminated" ) );
}
assertFalse( queue.isActive() );
}

Expand Down