}
@Override
public ClusterState execute(ClusterState currentState) {
MetaData metaData = currentState.getMetaData();
BenchmarkMetaData bmd = metaData.custom(BenchmarkMetaData.TYPE);
MetaData.Builder mdBuilder = MetaData.builder(metaData);
ImmutableList.Builder<BenchmarkMetaData.Entry> builder = ImmutableList.builder();
if (bmd != null) {
for (BenchmarkMetaData.Entry entry : bmd.entries()) {
if (request.benchmarkName().equals(entry.benchmarkId())) {
if (entry.state() != BenchmarkMetaData.State.SUCCESS && entry.state() != BenchmarkMetaData.State.FAILED) {
throw new ElasticsearchException("A benchmark with ID [" + request.benchmarkName() + "] is already running in state [" + entry.state() + "]");
}
// just drop the entry it it has finished successfully or it failed!
} else {
builder.add(entry);
}
}
}
List<DiscoveryNode> nodes = findNodes(request);
String[] nodeIds = new String[nodes.size()];
int i = 0;
for (DiscoveryNode node : nodes) {
nodeIds[i++] = node.getId();
}
BenchmarkMetaData.Entry entry = new BenchmarkMetaData.Entry(request.benchmarkName(), BenchmarkMetaData.State.STARTED, nodeIds);
newBenchmark.add(entry);
bmd = new BenchmarkMetaData(builder.add(entry).build());
mdBuilder.putCustom(BenchmarkMetaData.TYPE, bmd);
return ClusterState.builder(currentState).metaData(mdBuilder).build();
}