Skip to content
Open
Show file tree
Hide file tree
Changes from 2 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 @@ -37,6 +37,7 @@
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.Future;
import java.util.concurrent.TimeUnit;
import org.apache.bookkeeper.util.ZkUtils;
import org.apache.pulsar.broker.loadbalance.LoadManager;
import org.apache.pulsar.common.policies.data.ResourceQuota;
Expand All @@ -63,7 +64,7 @@
@Command(name = "simulation-controller",
description = "Provides a shell for the user to dictate how simulation clients should "
+ "incur load.")
public class LoadSimulationController extends CmdBase{
public class LoadSimulationController extends CmdBase implements AutoCloseable {
private static final Logger log = LoggerFactory.getLogger(LoadSimulationController.class);

// Input streams for each client to send commands through.
Expand All @@ -77,7 +78,20 @@ public class LoadSimulationController extends CmdBase{

private Random random;

private static final ExecutorService threadPool = Executors.newCachedThreadPool();
private final ExecutorService threadPool = Executors.newCachedThreadPool();

@Override
public void close() {
threadPool.shutdown();
try {
if (!threadPool.awaitTermination(5, TimeUnit.SECONDS)) {
threadPool.shutdownNow();
}
} catch (InterruptedException e) {
threadPool.shutdownNow();
Thread.currentThread().interrupt();
}
}

// picocli arguments for starting a controller via main.

Expand Down Expand Up @@ -690,18 +704,22 @@ public void start() throws Exception {
*/
@Override
public void run() throws Exception {
random = new Random();
clients = this.clientHostNames.split(",");
final Socket[] sockets = new Socket[clients.length];
inputStreams = new DataInputStream[clients.length];
outputStreams = new DataOutputStream[clients.length];
log.info("Found {} clients:", clients.length);
for (int i = 0; i < clients.length; ++i) {
sockets[i] = new Socket(clients[i], clientPort);
inputStreams[i] = new DataInputStream(sockets[i].getInputStream());
outputStreams[i] = new DataOutputStream(sockets[i].getOutputStream());
log.info("Connected to {}", clients[i]);
try {
random = new Random();
clients = this.clientHostNames.split(",");
final Socket[] sockets = new Socket[clients.length];
inputStreams = new DataInputStream[clients.length];
outputStreams = new DataOutputStream[clients.length];
log.info("Found {} clients:", clients.length);
for (int i = 0; i < clients.length; ++i) {
sockets[i] = new Socket(clients[i], clientPort);
inputStreams[i] = new DataInputStream(sockets[i].getInputStream());
outputStreams[i] = new DataOutputStream(sockets[i].getOutputStream());
log.info("Connected to {}", clients[i]);
}
start();
} finally {
close();
}
start();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
/*
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance
* with the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
*/
package org.apache.pulsar.testclient;

import java.lang.reflect.Field;
import java.util.Map;
import java.util.concurrent.ExecutorService;
import org.junit.jupiter.api.Test;

import static org.junit.jupiter.api.Assertions.assertTrue;
Copy link
Member

Choose a reason for hiding this comment

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

In Pulsar, we use TestNG instead of Junit 5. Please use TestNG imports instead.

Copy link
Member

Choose a reason for hiding this comment

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

Please ensure that CI passes in "Personal CI". The instructions for setting this up are at https://pulsar.apache.org/contribute/personal-ci/.


public class LoadSimulationControllerTest {

@Test
public void shouldLeakThreadsWhenExecutorIsNotShutdown() throws Exception {
LoadSimulationController controller = new LoadSimulationController();
Field threadPoolField = LoadSimulationController.class.getDeclaredField("threadPool");
threadPoolField.setAccessible(true);
ExecutorService threadPool = (ExecutorService) threadPoolField.get(controller);

threadPool.submit(() -> {
try {
Thread.sleep(50);
} catch (InterruptedException e) {
Thread.currentThread().interrupt();
}
});

Thread.sleep(100);

controller.close();

long poolThreadCount = 0;
for (int i = 0; i < 20; i++) {
Map<Thread, StackTraceElement[]> threads = Thread.getAllStackTraces();
poolThreadCount = threads.keySet().stream()
.filter(Thread::isAlive)
.filter(t -> !t.isDaemon())
.filter(t -> t.getName().startsWith("pool-"))
.count();
if (poolThreadCount == 0) {
break;
}
Thread.sleep(25);
}

assertTrue(poolThreadCount == 0,
String.format("Found %d alive non-daemon pool- threads after close", poolThreadCount));
}
}

Loading