
io.takari.orchestra.plugins.nexus.perf.AgentPoolManager Maven / Gradle / Ivy
package io.takari.orchestra.plugins.nexus.perf;
import com.sonatype.nexus.perftest.controller.Agent;
import com.sonatype.nexus.perftest.controller.AgentPool;
import com.sonatype.nexus.perftest.controller.Swarm;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import java.util.ArrayList;
import java.util.Collection;
import java.util.List;
import java.util.Map;
import java.util.function.Function;
public class AgentPoolManager {
private static final Logger log = LoggerFactory.getLogger(AgentPoolManager.class);
private static final long DEFAULT_POLLING_INTERVAL = 5000;
private final AgentPool pool;
private final List loaded;
public AgentPoolManager(AgentPool pool) {
this.pool = pool;
this.loaded = new ArrayList<>();
}
public void load(String scenario, int count, Map overrides) {
synchronized (loaded) {
Collection agents = pool.acquire(count);
if (agents.size() != count) {
throw new IllegalStateException("Can't acquire enough agents to continue. Required: " + count + ", got: " + agents.size());
}
agents.forEach(a -> {
a.abort();
a.load(scenario, overrides);
});
loaded.addAll(agents);
}
}
public void start() {
synchronized (loaded) {
loaded.parallelStream().forEach(a -> a.start());
}
}
public void release() {
pool.releaseAll();
}
public void waitToStart(long timeout) {
synchronized (loaded) {
loaded.parallelStream().forEach(a -> waitFor(a, x -> x.getControlBean().isRunning(), timeout));
}
}
public void waitToFinish(long timeout) {
synchronized (loaded) {
loaded.parallelStream().forEach(a -> waitFor(a, x -> !x.getControlBean().isRunning(), timeout));
}
}
private static void waitFor(Agent a, Function condition, long timeout) {
long t1 = System.currentTimeMillis();
while (true) {
if (condition.apply(a)) {
return;
}
long t2 = System.currentTimeMillis();
if (t2 - t1 >= timeout) {
throw new RuntimeException("Timeout waiting for " + a);
}
log.info("waitFor [{}] -> waiting for '{}'", timeout, a);
try {
Thread.sleep(DEFAULT_POLLING_INTERVAL);
} catch (InterruptedException e) {
Thread.currentThread().interrupt();
}
}
}
public boolean hasFailures() {
boolean result = false;
synchronized (loaded) {
for (Agent a : loaded) {
for (Swarm s : a.getSwarms()) {
List failures = s.getControl().getFailures();
if (failures != null && !failures.isEmpty()) {
StringBuilder b = new StringBuilder();
for (String f : failures) {
b.append(f).append("\n");
}
log.warn("hasFailures -> '{}' reported:\n{}", a, b);
result = true;
}
}
}
}
return result;
}
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy