public class RedisClusterClient extends AbstractRedisClient
getPartitions()
method.
Connections to the cluster members are opened on the first access to the cluster node and managed by the
StatefulRedisClusterConnection. You should not use transactional commands on cluster connections since MULTI,
EXEC and DISCARD have no key and cannot be assigned to a particular node. A cluster connection uses a default
connection to run non-keyed commands.
The Redis cluster client provides a sync, async and reactive API.
Connections to particular nodes can be obtained by StatefulRedisClusterConnection.getConnection(String) providing the
node id or StatefulRedisClusterConnection.getConnection(String, int) by host and port.
Multiple keys operations have to operate on a key that hashes to the same slot. Following commands do not need to follow that rule since they are pipelined according to its hash value to multiple nodes in parallel on the sync, async and, reactive API:
DELUNLINKMGETRedisStringAsyncCommands.mget(KeyValueStreamingChannel, Object[]) ) MGET with streaming}MSETMSETNXFollowing commands on the Cluster sync, async and, reactive API are implemented with a Cluster-flavor:
RedisAdvancedClusterAsyncCommands.clientSetname(Object) Executes CLIENT SET on all connections and
initializes new connections with the clientName.RedisAdvancedClusterAsyncCommands.flushall() Run FLUSHALL on all master nodes.RedisAdvancedClusterAsyncCommands.flushdb() Executes FLUSHDB on all master nodes.RedisAdvancedClusterAsyncCommands.keys(Object) Executes KEYS on all.RedisAdvancedClusterAsyncCommands.randomkey() Returns a random key from a random master node.RedisAdvancedClusterAsyncCommands.scriptFlush() Executes SCRIPT FLUSH on all nodes.RedisAdvancedClusterAsyncCommands.scriptKill() Executes SCRIPT KILL on all nodes.RedisAdvancedClusterAsyncCommands.shutdown(boolean) Executes SHUTDOWN on all nodes.RedisAdvancedClusterAsyncCommands.scan() Executes a SCAN on all nodes according to ReadFrom. The
resulting cursor must be reused across the SCAN to scan iteratively across the whole cluster.
Cluster commands can be issued to multiple hosts in parallel by using the NodeSelectionSupport API. A set of nodes is
selected using a Predicate and commands can be issued to the node selection
AsyncExecutions<String> ping = commands.masters().commands().ping(); Collection<RedisClusterNode> nodes = ping.nodes(); nodes.stream().forEach(redisClusterNode -> ping.get(redisClusterNode));
RedisClusterClient is an expensive resource. Reuse this instance or share external ClientResources as much as
possible.RedisURI,
StatefulRedisClusterConnection,
RedisCodec,
ClusterClientOptions,
ClientResources| Modifier and Type | Field and Description |
|---|---|
protected AtomicBoolean |
clusterTopologyRefreshActivated |
protected AtomicReference<ScheduledFuture<?>> |
clusterTopologyRefreshFuture |
BUF_ALLOCATOR, channels, clientOptions, clientResources, closeableResources, connectionEvents, eventLoopGroups, genericWorkerPool, timeout, timer| Modifier | Constructor and Description |
|---|---|
protected |
RedisClusterClient()
Non-private constructor to make
RedisClusterClient proxyable. |
protected |
RedisClusterClient(ClientResources clientResources,
Iterable<RedisURI> redisURIs)
Initialize the client with a list of cluster URI's.
|
| Modifier and Type | Method and Description |
|---|---|
StatefulRedisClusterConnection<String,String> |
connect()
Connect to a Redis Cluster and treat keys and values as UTF-8 strings.
|
<K,V> StatefulRedisClusterConnection<K,V> |
connect(RedisCodec<K,V> codec)
Connect to a Redis Cluster.
|
<K,V> CompletableFuture<StatefulRedisClusterConnection<K,V>> |
connectAsync(RedisCodec<K,V> codec)
Connect asynchronously to a Redis Cluster.
|
StatefulRedisClusterPubSubConnection<String,String> |
connectPubSub()
Connect to a Redis Cluster using pub/sub connections and treat keys and values as UTF-8 strings.
|
<K,V> StatefulRedisClusterPubSubConnection<K,V> |
connectPubSub(RedisCodec<K,V> codec)
Connect to a Redis Cluster using pub/sub connections.
|
<K,V> CompletableFuture<StatefulRedisClusterPubSubConnection<K,V>> |
connectPubSubAsync(RedisCodec<K,V> codec)
Connect asynchronously to a Redis Cluster using pub/sub connections.
|
static RedisClusterClient |
create(ClientResources clientResources,
Iterable<RedisURI> redisURIs)
Create a new client that connects to the supplied
uri with shared ClientResources. |
static RedisClusterClient |
create(ClientResources clientResources,
RedisURI redisURI)
Create a new client that connects to the supplied
uri with shared ClientResources. |
static RedisClusterClient |
create(ClientResources clientResources,
String uri)
Create a new client that connects to the supplied uri with shared
ClientResources.You need to shut down the
ClientResources upon shutting down your application. |
static RedisClusterClient |
create(Iterable<RedisURI> redisURIs)
Create a new client that connects to the supplied
uri with default ClientResources. |
static RedisClusterClient |
create(RedisURI redisURI)
Create a new client that connects to the supplied
uri with default ClientResources. |
static RedisClusterClient |
create(String uri)
Create a new client that connects to the supplied uri with default
ClientResources. |
protected Partitions |
determinePartitions(Partitions current,
Map<RedisURI,Partitions> topologyViews)
Determines a
topology view based on the current and the obtain topology views. |
protected <T extends Closeable> |
forEachCloseable(Predicate<? super Closeable> selector,
Consumer<T> function)
|
protected void |
forEachClusterConnection(Consumer<StatefulRedisClusterConnectionImpl<?,?>> function)
Apply a
Consumer of StatefulRedisClusterConnectionImpl to all active connections. |
protected void |
forEachClusterPubSubConnection(Consumer<io.lettuce.core.cluster.StatefulRedisClusterPubSubConnectionImpl<?,?>> function)
Apply a
Consumer of StatefulRedisClusterPubSubConnectionImpl to all active connections. |
protected RedisURI |
getFirstUri()
Returns the first
RedisURI configured with this RedisClusterClient instance. |
protected Iterable<RedisURI> |
getInitialUris()
|
Partitions |
getPartitions()
Retrieve the cluster view.
|
ClientResources |
getResources()
Returns the
ClientResources which are used with that client. |
protected Mono<SocketAddress> |
getSocketAddressSupplier(Function<Partitions,Collection<RedisClusterNode>> sortFunction)
Returns a
Supplier for connection points. |
protected Iterable<RedisURI> |
getTopologyRefreshSource()
Returns the seed
RedisURI for the topology refreshing. |
protected void |
initializePartitions() |
protected Partitions |
loadPartitions()
Retrieve partitions.
|
protected RedisCodec<String,String> |
newStringStringCodec()
|
void |
reloadPartitions()
Reload partitions and re-initialize the distribution table.
|
void |
setOptions(ClusterClientOptions clientOptions)
Set the
ClusterClientOptions for the client. |
void |
setPartitions(Partitions partitions)
Sets the new cluster topology.
|
void |
shutdown(long quietPeriod,
long timeout,
TimeUnit timeUnit)
Shutdown this client and close all open connections.
|
protected static <T> CompletableFuture<T> |
transformAsyncConnectionException(CompletionStage<T> future,
Iterable<RedisURI> target) |
protected void |
updatePartitionsInConnections() |
protected boolean |
useDynamicRefreshSources()
Returns true if
dynamic refresh sources are
enabled. |
addListener, channelType, connectionBuilder, getChannelCount, getConnection, getConnection, getOptions, getResourceCount, initializeChannelAsync, removeListener, setDefaultTimeout, setDefaultTimeout, setOptions, shutdown, shutdown, shutdownAsync, shutdownAsyncprotected final AtomicBoolean clusterTopologyRefreshActivated
protected final AtomicReference<ScheduledFuture<?>> clusterTopologyRefreshFuture
protected RedisClusterClient()
RedisClusterClient proxyable.protected RedisClusterClient(ClientResources clientResources, Iterable<RedisURI> redisURIs)
clientResources - the client resources. If null, the client will create a new dedicated instance of
client resources and keep track of them.redisURIs - iterable of initial cluster URIs. Must not be null and not empty.public static RedisClusterClient create(RedisURI redisURI)
uri with default ClientResources. You can
connect to different Redis servers but you must supply a RedisURI on connecting.redisURI - the Redis URI, must not be nullRedisClusterClientpublic static RedisClusterClient create(Iterable<RedisURI> redisURIs)
uri with default ClientResources. You can
connect to different Redis servers but you must supply a RedisURI on connecting.redisURIs - one or more Redis URI, must not be null and not empty.RedisClusterClientpublic static RedisClusterClient create(String uri)
ClientResources. You can connect to different
Redis servers but you must supply a RedisURI on connecting.uri - the Redis URI, must not be empty or null.RedisClusterClientpublic static RedisClusterClient create(ClientResources clientResources, RedisURI redisURI)
uri with shared ClientResources. You need to
shut down the ClientResources upon shutting down your application.You can connect to different Redis servers but
you must supply a RedisURI on connecting.clientResources - the client resources, must not be nullredisURI - the Redis URI, must not be nullRedisClusterClientpublic static RedisClusterClient create(ClientResources clientResources, String uri)
ClientResources.You need to shut down the
ClientResources upon shutting down your application. You can connect to different Redis servers but you must
supply a RedisURI on connecting.clientResources - the client resources, must not be nulluri - the Redis URI, must not be empty or null.RedisClusterClientpublic static RedisClusterClient create(ClientResources clientResources, Iterable<RedisURI> redisURIs)
uri with shared ClientResources. You need to
shut down the ClientResources upon shutting down your application.You can connect to different Redis servers but
you must supply a RedisURI on connecting.clientResources - the client resources, must not be nullredisURIs - one or more Redis URI, must not be null and not emptyRedisClusterClientpublic StatefulRedisClusterConnection<String,String> connect()
What to expect from this connection:
public <K,V> StatefulRedisClusterConnection<K,V> connect(RedisCodec<K,V> codec)
codec to encode/decode keys and values.
What to expect from this connection:
K - Key typeV - Value typecodec - Use this codec to encode/decode keys and values, must not be nullpublic <K,V> CompletableFuture<StatefulRedisClusterConnection<K,V>> connectAsync(RedisCodec<K,V> codec)
codec to encode/decode keys and values.
Connecting asynchronously requires an initialized topology. Call getPartitions() first, otherwise the connect
will fail with aIllegalStateException.
What to expect from this connection:
K - Key typeV - Value typecodec - Use this codec to encode/decode keys and values, must not be nullCompletableFuture that is notified with the connection progress.public StatefulRedisClusterPubSubConnection<String,String> connectPubSub()
What to expect from this connection:
public <K,V> StatefulRedisClusterPubSubConnection<K,V> connectPubSub(RedisCodec<K,V> codec)
codec to encode/decode keys and
values.
What to expect from this connection:
K - Key typeV - Value typecodec - Use this codec to encode/decode keys and values, must not be nullpublic <K,V> CompletableFuture<StatefulRedisClusterPubSubConnection<K,V>> connectPubSubAsync(RedisCodec<K,V> codec)
codec to
encode/decode keys and values. Connecting asynchronously requires an initialized topology. Call getPartitions()
first, otherwise the connect will fail with aIllegalStateException.
What to expect from this connection:
K - Key typeV - Value typecodec - Use this codec to encode/decode keys and values, must not be nullCompletableFuture that is notified with the connection progress.public void reloadPartitions()
protected void updatePartitionsInConnections()
protected void initializePartitions()
public Partitions getPartitions()
protected Partitions loadPartitions()
Partitions are ordered by latency. Lower latency nodes come first.protected Partitions determinePartitions(Partitions current, Map<RedisURI,Partitions> topologyViews)
topology view based on the current and the obtain topology views.current - the current topology view. May be null if RedisClusterClient has no topology view yet.topologyViews - the obtain topology viewstopology view to use.public void setPartitions(Partitions partitions)
partitions - partitions objectpublic ClientResources getResources()
ClientResources which are used with that client.ClientResources for this clientpublic void shutdown(long quietPeriod,
long timeout,
TimeUnit timeUnit)
shutdown in class AbstractRedisClientquietPeriod - the quiet period as described in the documentationtimeout - the maximum amount of time to wait until the executor is shutdown regardless if a task was submitted
during the quiet periodtimeUnit - the unit of quietPeriod and timeoutpublic void setOptions(ClusterClientOptions clientOptions)
ClusterClientOptions for the client.clientOptions - client options for the client and connections that are created after setting the optionsprotected RedisURI getFirstUri()
RedisURI configured with this RedisClusterClient instance.RedisURI.protected Mono<SocketAddress> getSocketAddressSupplier(Function<Partitions,Collection<RedisClusterNode>> sortFunction)
Supplier for connection points.sortFunction - Sort function to enforce a specific order. The sort function must not change the order or the input
parameter but create a new collection with the desired order, must not be null.Supplier for connection points.protected void forEachClusterConnection(Consumer<StatefulRedisClusterConnectionImpl<?,?>> function)
Consumer of StatefulRedisClusterConnectionImpl to all active connections.function - the Consumer.protected void forEachClusterPubSubConnection(Consumer<io.lettuce.core.cluster.StatefulRedisClusterPubSubConnectionImpl<?,?>> function)
Consumer of StatefulRedisClusterPubSubConnectionImpl to all active connections.function - the Consumer.protected <T extends Closeable> void forEachCloseable(Predicate<? super Closeable> selector, Consumer<T> function)
T - function - the Consumer.protected Iterable<RedisURI> getTopologyRefreshSource()
RedisURI for the topology refreshing. This method is called before each topology refresh to
provide an Iterable of RedisURI that is used to perform the next topology refresh.
Subclasses of RedisClusterClient may override that method.
protected boolean useDynamicRefreshSources()
dynamic refresh sources are
enabled.
Subclasses of RedisClusterClient may override that method.
ClusterTopologyRefreshOptions.useDynamicRefreshSources()protected RedisCodec<String,String> newStringStringCodec()
String codec.StringCodec.UTF8protected static <T> CompletableFuture<T> transformAsyncConnectionException(CompletionStage<T> future, Iterable<RedisURI> target)
Copyright © 2019 lettuce.io. All rights reserved.