Skip to content

Commit c33bdf7

Browse files
authored
Shutdown ExecutorService(s) in multi node pipelines (#3467)
* Shutdown ExecutorService(s) in multi node pipelines * Use only shutdownNow() * format import
1 parent f9a8575 commit c33bdf7

File tree

2 files changed

+40
-2
lines changed

2 files changed

+40
-2
lines changed

src/main/java/redis/clients/jedis/MultiNodePipelineBase.java

+4-2
Original file line numberDiff line numberDiff line change
@@ -38,8 +38,6 @@ public abstract class MultiNodePipelineBase extends PipelineBase
3838
private final Map<HostAndPort, Connection> connections;
3939
private volatile boolean syncing = false;
4040

41-
private final ExecutorService executorService = Executors.newFixedThreadPool(MULTI_NODE_PIPELINE_SYNC_WORKERS);
42-
4341
public MultiNodePipelineBase(CommandObjects commandObjects) {
4442
super(commandObjects);
4543
pipelinedResponses = new LinkedHashMap<>();
@@ -104,6 +102,8 @@ public final void sync() {
104102
}
105103
syncing = true;
106104

105+
ExecutorService executorService = Executors.newFixedThreadPool(MULTI_NODE_PIPELINE_SYNC_WORKERS);
106+
107107
CountDownLatch countDownLatch = new CountDownLatch(pipelinedResponses.size());
108108
Iterator<Map.Entry<HostAndPort, Queue<Response<?>>>> pipelinedResponsesIterator
109109
= pipelinedResponses.entrySet().iterator();
@@ -136,6 +136,8 @@ public final void sync() {
136136
log.error("Thread is interrupted during sync.", e);
137137
}
138138

139+
executorService.shutdownNow();
140+
139141
syncing = false;
140142
}
141143

src/test/java/redis/clients/jedis/ClusterPipeliningTest.java

+36
Original file line numberDiff line numberDiff line change
@@ -1061,4 +1061,40 @@ public void transaction() {
10611061
assertThrows(UnsupportedOperationException.class, () -> cluster.multi());
10621062
}
10631063
}
1064+
1065+
@Test(timeout = 10_000L)
1066+
public void multiple() {
1067+
final int maxTotal = 100;
1068+
ConnectionPoolConfig poolConfig = new ConnectionPoolConfig();
1069+
poolConfig.setMaxTotal(maxTotal);
1070+
try (JedisCluster cluster = new JedisCluster(nodes, DEFAULT_CLIENT_CONFIG, 5, poolConfig)) {
1071+
for (int i = 0; i < maxTotal; i++) {
1072+
assertThreadsCount();
1073+
String s = Integer.toString(i);
1074+
try (ClusterPipeline pipeline = cluster.pipelined()) {
1075+
pipeline.set(s, s);
1076+
pipeline.sync();
1077+
}
1078+
assertThreadsCount();
1079+
}
1080+
}
1081+
}
1082+
1083+
private static void assertThreadsCount() {
1084+
// Get the root thread group
1085+
final ThreadGroup rootGroup = Thread.currentThread().getThreadGroup().getParent();
1086+
1087+
// Create a buffer to store the thread information
1088+
final Thread[] threads = new Thread[rootGroup.activeCount()];
1089+
1090+
// Enumerate all threads into the buffer
1091+
rootGroup.enumerate(threads);
1092+
1093+
// Assert information about threads
1094+
final int count = (int) Arrays.stream(threads)
1095+
.filter(thread -> thread != null && thread.getName() != null
1096+
&& thread.getName().startsWith("pool-"))
1097+
.count();
1098+
assertTrue(count < 9);
1099+
}
10641100
}

0 commit comments

Comments
 (0)