use of io.crate.concurrent.limits.ConcurrencyLimit in project crate by crate.
the class ShardDMLExecutor method executeBatch.
private CompletableFuture<TAcc> executeBatch(TReq request) {
ConcurrencyLimit nodeLimit = nodeLimits.get(resolveNodeId(request));
long startTime = nodeLimit.startSample();
FutureActionListener<ShardResponse, TAcc> listener = new FutureActionListener<>((a) -> {
nodeLimit.onSample(startTime, false);
TAcc acc = collector.supplier().get();
collector.accumulator().accept(acc, a);
return acc;
});
operation.accept(request, withRetry(request, nodeLimit, listener));
return listener;
}
use of io.crate.concurrent.limits.ConcurrencyLimit in project crate by crate.
the class InsertFromValues method execute.
private CompletableFuture<ShardResponse.CompressedResult> execute(NodeLimits nodeLimits, ClusterState state, Collection<ShardUpsertRequest> shardUpsertRequests, TransportShardUpsertAction shardUpsertAction, ScheduledExecutorService scheduler) {
ShardResponse.CompressedResult compressedResult = new ShardResponse.CompressedResult();
if (shardUpsertRequests.isEmpty()) {
return CompletableFuture.completedFuture(compressedResult);
}
CompletableFuture<ShardResponse.CompressedResult> result = new CompletableFuture<>();
AtomicInteger numRequests = new AtomicInteger(shardUpsertRequests.size());
AtomicReference<Throwable> lastFailure = new AtomicReference<>(null);
Consumer<ShardUpsertRequest> countdown = request -> {
if (numRequests.decrementAndGet() == 0) {
Throwable throwable = lastFailure.get();
if (throwable == null) {
result.complete(compressedResult);
} else {
throwable = SQLExceptions.unwrap(throwable, t -> t instanceof RuntimeException);
// we want to report duplicate key exceptions
if (!SQLExceptions.isDocumentAlreadyExistsException(throwable) && (partitionWasDeleted(throwable, request.index()) || partitionClosed(throwable, request.index()) || mixedArgumentTypesFailure(throwable))) {
result.complete(compressedResult);
} else {
result.completeExceptionally(throwable);
}
}
}
};
for (ShardUpsertRequest request : shardUpsertRequests) {
String nodeId;
try {
nodeId = state.routingTable().shardRoutingTable(request.shardId()).primaryShard().currentNodeId();
} catch (IndexNotFoundException e) {
lastFailure.set(e);
if (!IndexParts.isPartitioned(request.index())) {
synchronized (compressedResult) {
compressedResult.markAsFailed(request.items());
}
}
countdown.accept(request);
continue;
}
final ConcurrencyLimit nodeLimit = nodeLimits.get(nodeId);
final long startTime = nodeLimit.startSample();
ActionListener<ShardResponse> listener = new ActionListener<>() {
@Override
public void onResponse(ShardResponse shardResponse) {
Throwable throwable = shardResponse.failure();
if (throwable == null) {
nodeLimit.onSample(startTime, false);
synchronized (compressedResult) {
compressedResult.update(shardResponse);
}
} else {
nodeLimit.onSample(startTime, true);
lastFailure.set(throwable);
}
countdown.accept(request);
}
@Override
public void onFailure(Exception e) {
nodeLimit.onSample(startTime, true);
Throwable t = SQLExceptions.unwrap(e);
if (!partitionWasDeleted(t, request.index())) {
synchronized (compressedResult) {
compressedResult.markAsFailed(request.items());
}
}
lastFailure.set(t);
countdown.accept(request);
}
};
shardUpsertAction.execute(request, new RetryListener<>(scheduler, l -> shardUpsertAction.execute(request, l), listener, BackoffPolicy.limitedDynamic(nodeLimit)));
}
return result;
}
use of io.crate.concurrent.limits.ConcurrencyLimit in project crate by crate.
the class NodeLimitsTest method test_can_receive_limits_for_null_node.
@Test
public void test_can_receive_limits_for_null_node() {
ConcurrencyLimit limit = nodeLimits.get(null);
assertThat(limit, notNullValue());
}
Aggregations