Skip to content
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
13 changes: 13 additions & 0 deletions src/main/java/com/basho/riak/client/api/RiakClient.java
Original file line number Diff line number Diff line change
Expand Up @@ -427,4 +427,17 @@ public RiakCluster getRiakCluster()
{
return cluster;
}

/**
* Cleans up any Thread-Local variables after shutdown.
* This operation is useful when you are in a container environment, and you
* do not want to leave the thread local variables in the threads you do not manage.
* Call this method when your application is being unloaded from the container, <b>after</b>
* all {@link RiakNode}, {@link RiakCluster}, and {@link com.basho.riak.client.api.RiakClient}
* objects are in the shutdown state.
*/
public void cleanup()
{
cluster.cleanup();
}
}
15 changes: 15 additions & 0 deletions src/main/java/com/basho/riak/client/core/RiakCluster.java
Original file line number Diff line number Diff line change
Expand Up @@ -635,6 +635,21 @@ public static Builder builder(RiakNode node)
return new Builder(node);
}

/**
* Cleans up any Thread-Local variables after shutdown.
* This operation is useful when you are in a container environment, and you
* do not want to leave the thread local variables in the threads you do not manage.
* Call this method when your application is being unloaded from the container, <b>after</b>
* all {@link RiakNode}, {@link RiakCluster}, and {@link com.basho.riak.client.api.RiakClient}
* objects are in the shutdown state.
*/
public synchronized void cleanup()
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should this just be part of shutdown?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nah, b/c:

  1. If "the user" instantiated 2 RiakCluster or RiakClient objects for some reason, it could clear all the Netty threadlocals if one is still running.
  2. It would turn shutdown() into a partially synchronous method, as we'd have to wait for the shutdown future to finish, then call cleanup.
  3. This is only for environments that swap out classloaders.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

OK

{
stateCheck(State.SHUTDOWN);
io.netty.util.concurrent.FastThreadLocal.removeAll();
io.netty.util.concurrent.FastThreadLocal.destroy();
}

/**
* Builder used to create {@link RiakCluster} instances.
*/
Expand Down
36 changes: 34 additions & 2 deletions src/test/java/com/basho/riak/client/core/RiakClusterTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
package com.basho.riak.client.core;

import com.google.protobuf.Message;
import io.netty.util.concurrent.FastThreadLocal;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.powermock.api.mockito.PowerMockito;
Expand All @@ -30,14 +31,14 @@

import static org.junit.Assert.*;
import static org.mockito.Mockito.*;

import static org.powermock.api.mockito.PowerMockito.verifyStatic;
/**
*
* @author Brian Roach <roach at basho dot com>
* @author Sergey Galkin <srggal at gmail dot com>
*/
@RunWith(PowerMockRunner.class)
@PrepareForTest(FutureOperation.class)
@PrepareForTest({FutureOperation.class, FastThreadLocal.class})
public class RiakClusterTest
{
@Test
Expand Down Expand Up @@ -222,6 +223,37 @@ public void nodeOperationQueue() throws Exception
verify(nodeManager, times(1)).executeOnNode(operation4, null);
}

@Test
public void testCleanup() throws Exception
{
RiakNode node = mock(RiakNode.class);
RiakNode.Builder nodeBuilder = spy(new RiakNode.Builder());
doReturn(node).when(nodeBuilder).build();
PowerMockito.mockStatic(FastThreadLocal.class);
PowerMockito.doNothing().when(FastThreadLocal.class, "destroy");
PowerMockito.doNothing().when(FastThreadLocal.class, "removeAll");

RiakCluster cluster = new RiakCluster.Builder(nodeBuilder.build()).build();
Whitebox.setInternalState(cluster, "state", RiakCluster.State.SHUTDOWN);

cluster.cleanup();

verifyStatic(times(2));
}

@Test(expected = IllegalStateException.class)
public void testCleanupNotShutdown()
{
RiakNode node = mock(RiakNode.class);
RiakNode.Builder nodeBuilder = spy(new RiakNode.Builder());
doReturn(node).when(nodeBuilder).build();

RiakCluster cluster = new RiakCluster.Builder(nodeBuilder.build()).build();
Whitebox.setInternalState(cluster, "state", RiakCluster.State.RUNNING);

cluster.cleanup();
}

private void assertQueueStatus(RiakCluster cluster, Integer expectedQueueSize,
RiakCluster.State expectedClusterState,
FutureOperation expectedQueueHead)
Expand Down