From efc1f685a5604dbb08aeb5b75b1a61f7a3999eaa Mon Sep 17 00:00:00 2001 From: "A.Fink" Date: Mon, 11 Sep 2023 13:44:13 +0300 Subject: [PATCH] =?UTF-8?q?https://github.com/eclipse-vertx/vert.x/issues/?= =?UTF-8?q?4851=20ConcurrentHashSet=E2=86=92Utils.concurrentHashSet?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../java/io/vertx/core/impl/ConcurrentHashSet.java | 2 ++ .../java/io/vertx/core/impl/DeploymentManager.java | 7 +++---- src/main/java/io/vertx/core/impl/Utils.java | 11 +++++++++++ src/test/java/io/vertx/core/ComplexHATest.java | 11 ++++------- .../io/vertx/core/eventbus/LocalEventBusTest.java | 11 +++++------ src/test/java/io/vertx/core/http/Http1xTest.java | 6 +++--- src/test/java/io/vertx/core/http/WebSocketTest.java | 5 +++-- src/test/java/io/vertx/core/net/NetTest.java | 8 ++++---- .../vertx/test/fakemetrics/FakeHttpServerMetrics.java | 5 +++-- 9 files changed, 38 insertions(+), 28 deletions(-) diff --git a/src/main/java/io/vertx/core/impl/ConcurrentHashSet.java b/src/main/java/io/vertx/core/impl/ConcurrentHashSet.java index d6c6c67b2c8..43383232686 100644 --- a/src/main/java/io/vertx/core/impl/ConcurrentHashSet.java +++ b/src/main/java/io/vertx/core/impl/ConcurrentHashSet.java @@ -18,8 +18,10 @@ import java.util.concurrent.ConcurrentHashMap; /** + * @deprecated use {@link Utils#concurrentHashSet} * @author Tim Fox */ +@Deprecated public class ConcurrentHashSet implements Set { private final Map map; diff --git a/src/main/java/io/vertx/core/impl/DeploymentManager.java b/src/main/java/io/vertx/core/impl/DeploymentManager.java index 8181f601c2f..8c5fb67da0e 100644 --- a/src/main/java/io/vertx/core/impl/DeploymentManager.java +++ b/src/main/java/io/vertx/core/impl/DeploymentManager.java @@ -12,7 +12,6 @@ package io.vertx.core.impl; import io.vertx.core.AsyncResult; -import io.vertx.core.CompositeFuture; import io.vertx.core.Context; import io.vertx.core.DeploymentOptions; import io.vertx.core.Future; @@ -75,9 +74,9 @@ public Future deployVerticle(Callable verticleSupplier, Deploy public Future undeployVerticle(String deploymentID) { Deployment deployment = deployments.get(deploymentID); - Context currentContext = vertx.getOrCreateContext(); + ContextInternal currentContext = vertx.getOrCreateContext(); if (deployment == null) { - return ((ContextInternal) currentContext).failedFuture(new IllegalStateException("Unknown deployment")); + return currentContext.failedFuture(new IllegalStateException("Unknown deployment")); } else { return deployment.doUndeploy(vertx.getOrCreateContext()); } @@ -255,7 +254,7 @@ private class DeploymentImpl implements Deployment { private final JsonObject conf; private final String verticleIdentifier; private final List verticles = new CopyOnWriteArrayList<>(); - private final Set children = new ConcurrentHashSet<>(); + private final Set children = Utils.concurrentHashSet(); private final DeploymentOptions options; private Handler undeployHandler; private int status = ST_DEPLOYED; diff --git a/src/main/java/io/vertx/core/impl/Utils.java b/src/main/java/io/vertx/core/impl/Utils.java index 9aedf0d2716..c3aad99c9d1 100644 --- a/src/main/java/io/vertx/core/impl/Utils.java +++ b/src/main/java/io/vertx/core/impl/Utils.java @@ -13,6 +13,10 @@ import io.netty.util.internal.PlatformDependent; +import java.util.Collections; +import java.util.Set; +import java.util.concurrent.ConcurrentHashMap; + /** * Simple generic utility methods and constants * @@ -45,4 +49,11 @@ public static boolean isWindows() { return isWindows; } + public static Set concurrentHashSet (){ + return Collections.newSetFromMap(new ConcurrentHashMap<>()); + } + + public static Set concurrentHashSet (int initialSize){ + return Collections.newSetFromMap(new ConcurrentHashMap<>(initialSize)); + } } diff --git a/src/test/java/io/vertx/core/ComplexHATest.java b/src/test/java/io/vertx/core/ComplexHATest.java index 0798d2a52a7..244a5633c2b 100644 --- a/src/test/java/io/vertx/core/ComplexHATest.java +++ b/src/test/java/io/vertx/core/ComplexHATest.java @@ -11,11 +11,8 @@ package io.vertx.core; -import io.vertx.core.DeploymentOptions; -import io.vertx.core.Vertx; -import io.vertx.core.VertxOptions; -import io.vertx.core.impl.ConcurrentHashSet; import io.vertx.core.impl.Deployment; +import io.vertx.core.impl.Utils; import io.vertx.core.impl.VertxInternal; import io.vertx.core.json.JsonObject; import io.vertx.core.spi.cluster.ClusterManager; @@ -40,11 +37,11 @@ */ public class ComplexHATest extends VertxTestBase { - protected ClusterManager getClusterManager() { + @Override protected ClusterManager getClusterManager() { return new FakeClusterManager(); } - private Random random = new Random(); + private final Random random = new Random(); protected final int maxVerticlesPerNode = 20; protected Set[] deploymentSnapshots; @@ -162,7 +159,7 @@ protected void takeDeploymentSnapshots() { } protected Set takeDeploymentSnapshot(int pos) { - Set snapshot = new ConcurrentHashSet<>(); + Set snapshot = Utils.concurrentHashSet(); VertxInternal v = (VertxInternal)vertices[pos]; for (String depID: v.deploymentIDs()) { snapshot.add(v.getDeployment(depID)); diff --git a/src/test/java/io/vertx/core/eventbus/LocalEventBusTest.java b/src/test/java/io/vertx/core/eventbus/LocalEventBusTest.java index c26086c2cb3..757d75a0c0a 100644 --- a/src/test/java/io/vertx/core/eventbus/LocalEventBusTest.java +++ b/src/test/java/io/vertx/core/eventbus/LocalEventBusTest.java @@ -14,9 +14,9 @@ import io.vertx.core.*; import io.vertx.core.eventbus.impl.EventBusInternal; import io.vertx.core.eventbus.impl.MessageConsumerImpl; -import io.vertx.core.impl.ConcurrentHashSet; import io.vertx.core.impl.ContextInternal; import io.vertx.core.impl.EventLoopContext; +import io.vertx.core.impl.Utils; import io.vertx.core.impl.VertxInternal; import io.vertx.core.streams.ReadStream; import io.vertx.test.core.TestUtils; @@ -41,7 +41,7 @@ public class LocalEventBusTest extends EventBusTestBase { private EventBusInternal eb; private boolean running; - public void setUp() throws Exception { + @Override public void setUp() throws Exception { super.setUp(); vertx.close(); vertx = Vertx.vertx(); @@ -52,7 +52,7 @@ public void setUp() throws Exception { running = true; } - protected void tearDown() throws Exception { + @Override protected void tearDown() throws Exception { closeVertx(); super.tearDown(); } @@ -712,7 +712,7 @@ public void start() { @Test public void testContextsSend() throws Exception { - Set contexts = new ConcurrentHashSet<>(); + Set contexts = Utils.concurrentHashSet(); CountDownLatch latch = new CountDownLatch(2); vertx.eventBus().consumer(ADDRESS1).handler(msg -> { msg.reply("bar"); @@ -730,7 +730,7 @@ public void testContextsSend() throws Exception { @Test public void testContextsPublish() throws Exception { - Set contexts = new ConcurrentHashSet<>(); + Set contexts = Utils.concurrentHashSet(); AtomicInteger cnt = new AtomicInteger(); int numHandlers = 10; for (int i = 0; i < numHandlers; i++) { @@ -1449,4 +1449,3 @@ public void testEarlyTimeoutOnHandlerUnregistration() { await(); } } - diff --git a/src/test/java/io/vertx/core/http/Http1xTest.java b/src/test/java/io/vertx/core/http/Http1xTest.java index 79d0a6215c8..b257ce61b6a 100644 --- a/src/test/java/io/vertx/core/http/Http1xTest.java +++ b/src/test/java/io/vertx/core/http/Http1xTest.java @@ -1520,7 +1520,7 @@ private void testKeepAlive(boolean keepAlive, int poolSize, int numServers, int // Start the servers HttpServer[] servers = new HttpServer[numServers]; CountDownLatch startServerLatch = new CountDownLatch(numServers); - Set connectedServers = new ConcurrentHashSet<>(); + Set connectedServers = Utils.concurrentHashSet(); for (int i = 0; i < numServers; i++) { HttpServer server = vertx.createHttpServer(new HttpServerOptions().setHost(DEFAULT_HTTP_HOST).setPort(DEFAULT_HTTP_PORT)); server.requestHandler(req -> { @@ -1736,7 +1736,7 @@ public void testSharedServersRoundRobin() throws Exception { Map requestCount = new ConcurrentHashMap<>(); CountDownLatch latchConns = new CountDownLatch(numRequests); - Set threads = new ConcurrentHashSet<>(); + Set threads = Utils.concurrentHashSet(); Future listenLatch = vertx.deployVerticle(() -> new AbstractVerticle() { Thread thread; @Override @@ -2199,7 +2199,7 @@ public void testContexts() throws Exception { }); expectedThreads.add(th.get()); } - Set threads = new ConcurrentHashSet<>(); + Set threads = Utils.concurrentHashSet(); for (int i = 0; i < numReqs; i++) { Context requestCtx = contexts.get(i); CompletableFuture cf = new CompletableFuture<>(); diff --git a/src/test/java/io/vertx/core/http/WebSocketTest.java b/src/test/java/io/vertx/core/http/WebSocketTest.java index 0fc0dd72033..0231623a354 100644 --- a/src/test/java/io/vertx/core/http/WebSocketTest.java +++ b/src/test/java/io/vertx/core/http/WebSocketTest.java @@ -35,6 +35,7 @@ import io.vertx.core.http.impl.WebSocketInternal; import io.vertx.core.http.impl.ws.WebSocketFrameImpl; import io.vertx.core.impl.ConcurrentHashSet; +import io.vertx.core.impl.Utils; import io.vertx.core.net.NetServer; import io.vertx.core.net.NetSocket; import io.vertx.core.net.SocketAddress; @@ -519,7 +520,7 @@ public void testSharedServersRoundRobin() throws Exception { int numConnections = numServers * 100; List servers = new ArrayList<>(); - Set connectedServers = new ConcurrentHashSet<>(); + Set connectedServers = Utils.concurrentHashSet(); Map connectCount = new ConcurrentHashMap<>(); CountDownLatch latchConns = new CountDownLatch(numConnections); @@ -3651,7 +3652,7 @@ private void testFanout(T hello, T bye, Function hand String path = "/some/path"; int numConnections = 10; - Set connections = new ConcurrentHashSet<>(); + Set connections = Utils.concurrentHashSet(); HttpServerOptions httpServerOptions = new HttpServerOptions() .setPort(DEFAULT_HTTP_PORT) .setRegisterWebSocketWriteHandlers(true); diff --git a/src/test/java/io/vertx/core/net/NetTest.java b/src/test/java/io/vertx/core/net/NetTest.java index 4ded4e0e3b2..2de378390bc 100755 --- a/src/test/java/io/vertx/core/net/NetTest.java +++ b/src/test/java/io/vertx/core/net/NetTest.java @@ -1975,8 +1975,8 @@ public void testSharedServersRoundRobin() throws Exception { int numConnections = numServers * (domainSocket ? 10 : 20); List servers = new ArrayList<>(); - Set connectedServers = new ConcurrentHashSet<>(); - Set threads = new ConcurrentHashSet<>(); + Set connectedServers = Utils.concurrentHashSet(); + Set threads = Utils.concurrentHashSet(); Future listenLatch = vertx.deployVerticle(() -> new AbstractVerticle() { NetServer server; @@ -2105,7 +2105,7 @@ public void testFanout() throws Exception { int numConnections = 10; - Set connections = new ConcurrentHashSet<>(); + Set connections = Utils.concurrentHashSet(); server.connectHandler(socket -> { connections.add(socket.writeHandlerID()); if (connections.size() == numConnections) { @@ -2552,7 +2552,7 @@ public void testContexts() throws Exception { })); awaitLatch(listenLatch); - Set contexts = new ConcurrentHashSet<>(); + Set contexts = Utils.concurrentHashSet(); AtomicInteger connectCount = new AtomicInteger(); CountDownLatch clientLatch = new CountDownLatch(1); // Each connect should be in its own context diff --git a/src/test/java/io/vertx/test/fakemetrics/FakeHttpServerMetrics.java b/src/test/java/io/vertx/test/fakemetrics/FakeHttpServerMetrics.java index a2aeb3b307e..78954f4fa3b 100644 --- a/src/test/java/io/vertx/test/fakemetrics/FakeHttpServerMetrics.java +++ b/src/test/java/io/vertx/test/fakemetrics/FakeHttpServerMetrics.java @@ -15,12 +15,13 @@ import io.vertx.core.http.HttpServerRequest; import io.vertx.core.http.ServerWebSocket; import io.vertx.core.http.WebSocketBase; -import io.vertx.core.impl.ConcurrentHashSet; +import io.vertx.core.impl.Utils; import io.vertx.core.net.SocketAddress; import io.vertx.core.spi.metrics.HttpServerMetrics; import io.vertx.core.spi.observability.HttpRequest; import io.vertx.core.spi.observability.HttpResponse; +import java.util.Set; import java.util.concurrent.ConcurrentHashMap; import java.util.concurrent.ConcurrentMap; @@ -30,7 +31,7 @@ public class FakeHttpServerMetrics extends FakeTCPMetrics implements HttpServerMetrics { private final ConcurrentMap webSockets = new ConcurrentHashMap<>(); - private final ConcurrentHashSet requests = new ConcurrentHashSet<>(); + private final Set requests = Utils.concurrentHashSet(); public WebSocketMetric getWebSocketMetric(ServerWebSocket ws) { return webSockets.get(ws);