
com.lambdaworks.redis.cluster.MultiNodeExecution Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of lettuce Show documentation
Show all versions of lettuce Show documentation
Advanced and thread-safe Java Redis client for synchronous, asynchronous, and
reactive usage. Supports Cluster, Sentinel, Pipelining, Auto-Reconnect, Codecs
and much more.
The newest version!
/*
* Copyright 2011-2016 the original author or authors.
*
* Licensed 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 com.lambdaworks.redis.cluster;
import java.lang.reflect.Proxy;
import java.util.Map;
import java.util.concurrent.Callable;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.atomic.AtomicLong;
import java.util.function.Predicate;
import com.lambdaworks.redis.RedisCommandInterruptedException;
import com.lambdaworks.redis.RedisException;
import com.lambdaworks.redis.RedisFuture;
import com.lambdaworks.redis.api.StatefulConnection;
import com.lambdaworks.redis.api.sync.RedisCommands;
import com.lambdaworks.redis.cluster.api.NodeSelectionSupport;
import com.lambdaworks.redis.cluster.api.StatefulRedisClusterConnection;
import com.lambdaworks.redis.cluster.api.sync.NodeSelection;
import com.lambdaworks.redis.cluster.api.sync.NodeSelectionCommands;
import com.lambdaworks.redis.cluster.models.partitions.RedisClusterNode;
/**
* Utility to perform and synchronize command executions on multiple cluster nodes.
*
* @author Mark Paluch
*/
class MultiNodeExecution {
static T execute(Callable function) {
try {
return function.call();
} catch (Exception e) {
throw new RedisException(e);
}
}
/**
* Aggregate (sum) results of the {@link RedisFuture}s.
*
* @param executions mapping of a key to the future
* @return future producing an aggregation result
*/
protected static RedisFuture aggregateAsync(Map, RedisFuture> executions) {
return new PipelinedRedisFuture<>(executions, objectPipelinedRedisFuture -> {
AtomicLong result = new AtomicLong();
for (RedisFuture future : executions.values()) {
Long value = execute(() -> future.get());
if (value != null) {
result.getAndAdd(value);
}
}
return result.get();
});
}
/**
* Returns the result of the first {@link RedisFuture} and guarantee that all futures are finished.
*
* @param executions mapping of a key to the future
* @param result type
* @return future returning the first result.
*/
protected static RedisFuture firstOfAsync(Map, RedisFuture> executions) {
return new PipelinedRedisFuture<>(executions, objectPipelinedRedisFuture -> {
// make sure, that all futures are executed before returning the result.
for (RedisFuture future : executions.values()) {
execute(() -> future.get());
}
for (RedisFuture future : executions.values()) {
return execute(() -> future.get());
}
return null;
});
}
/**
* Returns always {@literal OK} and guarantee that all futures are finished.
*
* @param executions mapping of a key to the future
* @return future returning the first result.
*/
protected static RedisFuture alwaysOkOfAsync(Map, RedisFuture> executions) {
return new PipelinedRedisFuture<>(executions, objectPipelinedRedisFuture -> {
// make sure, that all futures are executed before returning the result.
for (RedisFuture future : executions.values()) {
try {
future.get();
} catch (InterruptedException e) {
Thread.currentThread().interrupt();
throw new RedisCommandInterruptedException(e);
} catch (ExecutionException e) {
// swallow exceptions
}
}
return "OK";
} );
}
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy