diff --git a/src/main/java/net/spy/memcached/ArcusClient.java b/src/main/java/net/spy/memcached/ArcusClient.java index c83b89d89..26ff72701 100644 --- a/src/main/java/net/spy/memcached/ArcusClient.java +++ b/src/main/java/net/spy/memcached/ArcusClient.java @@ -340,6 +340,42 @@ protected static ArcusClient getInstance(ConnectionFactory cf, return new ArcusClient(cf, name, addrs); } + /** + * Create an Arcus client for the given memcached server addresses. + * Recommend only invoked with arcus server without zk. + * @param addrs socket addresses for the memcached servers + * @param cfb connection factory builder to configure connections for this client + * @return Arcus client instance + * @throws IOException + */ + public static ArcusClient createArcusClient(List addrs, + ConnectionFactoryBuilder cfb) + throws IOException { + return new ArcusClient(cfb.build(), addrs); + } + + /** + * Create an Arcus client pool for the given memcached server addresses. + * Recommend only invoked with arcus server without zk. + * @param addrs socket addresses for the memcached servers + * @param cfb connection factory builder to configure connections for this client + * @param poolSize pool size + * @return Arcus client pool instance + * @throws IOException + */ + public static ArcusClientPool createArcusClientPool(List addrs, + ConnectionFactoryBuilder cfb, int poolSize) + throws IOException { + ArcusClient[] clients = new ArcusClient[poolSize]; + int poolId = CacheManager.POOL_ID.getAndIncrement(); + for (int i = 0; i < poolSize; i++) { + String clientName = "ArcusClient" + "-" + poolId + + "(" + (i + 1) + "-" + poolSize + ")"; + clients[i] = new ArcusClient(cfb.build(), clientName, addrs); + } + return new ArcusClientPool(poolSize, clients); + } + /** * Create an Arcus client for the given memcached server addresses. * @@ -347,8 +383,10 @@ protected static ArcusClient getInstance(ConnectionFactory cf, * @param name client name * @param addrs socket addresses for the memcached servers * @throws IOException if connections cannot be established + * @deprecated Use {@link #createArcusClient(java.util.List, ConnectionFactoryBuilder)} instead. */ @SuppressWarnings("this-escape") + @Deprecated public ArcusClient(ConnectionFactory cf, String name, List addrs) throws IOException { super(cf, name, addrs); @@ -369,7 +407,9 @@ public ArcusClient(ConnectionFactory cf, String name, List ad * @param cf connection factory to configure connections for this client * @param addrs socket addresses for the memcached servers * @throws IOException if connections cannot be established + * @deprecated Use {@link #createArcusClient(java.util.List, ConnectionFactoryBuilder)} instead. */ + @Deprecated public ArcusClient(ConnectionFactory cf, List addrs) throws IOException { this(cf, DEFAULT_ARCUS_CLIENT_NAME + "-" + CLIENT_ID.getAndIncrement(), addrs); diff --git a/src/main/java/net/spy/memcached/CacheManager.java b/src/main/java/net/spy/memcached/CacheManager.java index 752ccd307..ac578b4c7 100644 --- a/src/main/java/net/spy/memcached/CacheManager.java +++ b/src/main/java/net/spy/memcached/CacheManager.java @@ -70,7 +70,7 @@ public class CacheManager extends SpyThread implements Watcher, private static final int ZK_SESSION_TIMEOUT = 15000; private static final long ZK_CONNECT_TIMEOUT = ZK_SESSION_TIMEOUT; - private static final AtomicInteger POOL_ID = new AtomicInteger(1); + static final AtomicInteger POOL_ID = new AtomicInteger(1); private final String zkConnectString; diff --git a/src/test/java/net/spy/memcached/ArcusTimeoutTest.java b/src/test/java/net/spy/memcached/ArcusTimeoutTest.java index 1a272d863..0a85bbeb1 100644 --- a/src/test/java/net/spy/memcached/ArcusTimeoutTest.java +++ b/src/test/java/net/spy/memcached/ArcusTimeoutTest.java @@ -53,17 +53,10 @@ protected void setUp() throws Exception { } private void initClient() throws IOException { - mc = new ArcusClient(new DefaultConnectionFactory() { - @Override - public long getOperationTimeout() { - return 1; - } - - @Override - public FailureMode getFailureMode() { - return FailureMode.Retry; - } - }, AddrUtil.getAddresses("0.0.0.0:23456")); + mc = ArcusClient.createArcusClient(AddrUtil.getAddresses("0.0.0.0:23456"), + new ConnectionFactoryBuilder() + .setOpTimeout(1) + .setFailureMode(FailureMode.Retry)); } @AfterEach diff --git a/src/test/java/net/spy/memcached/ClientBaseCase.java b/src/test/java/net/spy/memcached/ClientBaseCase.java index 6c36f5432..7c2b67304 100644 --- a/src/test/java/net/spy/memcached/ClientBaseCase.java +++ b/src/test/java/net/spy/memcached/ClientBaseCase.java @@ -301,7 +301,10 @@ protected void initClient(ConnectionFactory cf) throws Exception { if (USE_ZK) { openFromZK(new CFB(cf)); } else { - openDirect(cf); + openDirect(new ConnectionFactoryBuilder() + .setOpTimeout(cf.getOperationTimeout()) + .setFailureMode(cf.getFailureMode()) + .setInitialObservers(cf.getInitialObservers())); } } @@ -309,8 +312,8 @@ protected void openFromZK(ConnectionFactoryBuilder cfb) { client = ArcusClient.createArcusClient(ZK_ADDRESS, SERVICE_CODE, cfb); } - protected void openDirect(ConnectionFactory cf) throws Exception { - client = new ArcusClient(cf, AddrUtil.getAddresses(ARCUS_HOST)); + protected void openDirect(ConnectionFactoryBuilder cfb) throws Exception { + client = ArcusClient.createArcusClient(AddrUtil.getAddresses(ARCUS_HOST), cfb); } @BeforeEach diff --git a/src/test/manual/net/spy/memcached/ArcusClientCreateTest.java b/src/test/manual/net/spy/memcached/ArcusClientCreateTest.java index 28d4975ba..2b6c03cef 100644 --- a/src/test/manual/net/spy/memcached/ArcusClientCreateTest.java +++ b/src/test/manual/net/spy/memcached/ArcusClientCreateTest.java @@ -15,7 +15,6 @@ import org.junit.jupiter.api.Test; import static org.junit.jupiter.api.Assertions.assertEquals; -import static org.junit.jupiter.api.Assertions.assertThrows; import static org.junit.jupiter.api.Assertions.assertTrue; import static org.junit.jupiter.api.Assumptions.assumeFalse; @@ -37,20 +36,9 @@ protected void release() { addrs.clear(); } - @Test - void testCreateClientWithCustomName() throws IOException { - ArcusClient arcusClient = new ArcusClient(new DefaultConnectionFactory(), clientName, addrs); - - Collection nodes = arcusClient.getAllNodes(); - MemcachedNode node = nodes.iterator().next(); - - assertEquals(nodes.size(), 1); - assertEquals(node.getNodeName(), clientName + " " + hostName); - } - @Test void testCreateClientWithDefaultName() throws IOException { - ArcusClient arcusClient = new ArcusClient(new DefaultConnectionFactory(), addrs); + ArcusClient arcusClient = ArcusClient.createArcusClient(addrs, new ConnectionFactoryBuilder()); Collection nodes = arcusClient.getAllNodes(); assertEquals(nodes.size(), 1); @@ -60,11 +48,4 @@ void testCreateClientWithDefaultName() throws IOException { Matcher matcher = compile.matcher(node.getNodeName()); assertTrue(matcher.matches()); } - - @Test - void testCreateClientNullName() throws IOException { - assertThrows(NullPointerException.class, () -> { - new ArcusClient(new DefaultConnectionFactory(), null, addrs); - }); - } } diff --git a/src/test/manual/net/spy/memcached/collection/BaseIntegrationTest.java b/src/test/manual/net/spy/memcached/collection/BaseIntegrationTest.java index b20baab2f..f8bce65ea 100644 --- a/src/test/manual/net/spy/memcached/collection/BaseIntegrationTest.java +++ b/src/test/manual/net/spy/memcached/collection/BaseIntegrationTest.java @@ -120,7 +120,7 @@ public void connectionLost(SocketAddress sa) { }; cfb.setInitialObservers(Collections.singleton(obs)); - mc = new ArcusClient(cfb.build(), AddrUtil.getAddresses(ARCUS_HOST)); + mc = ArcusClient.createArcusClient(AddrUtil.getAddresses(ARCUS_HOST), cfb); latch.await(); }