java.lang.Object
io.lettuce.core.cluster.models.partitions.Partitions
- All Implemented Interfaces:
Iterable<RedisClusterNode>,Collection<RedisClusterNode>
public class Partitions extends Object implements Collection<RedisClusterNode>
Cluster topology view. An instance of
Partitions provides access to the partitions of a Redis Cluster. A partition is
represented by a Redis Cluster node that has a nodeId and
connection point details.
Partitions can be looked up by nodeId or slot (masters only). A nodeId can be migrated to a different host.
Partitions are cached to ensure a cheap lookup by slot. Users of Partitions are required to call
updateCache() after topology changes occur.
- Changes in
RedisClusterNode.NodeFlag.UPSTREAM/RedisClusterNode.NodeFlag.REPLICAstate - Newly added or removed nodes to/from the Redis Cluster
- Changes in
RedisClusterNode.getSlots()responsibility - Changes to the
replication source(the master of a replica) - Changes to the
RedisClusterNode.getUri()() connection point}
All query/read operations use the read-only view. Updates to Partitions are performed in an atomic way. Changes to the read-only cache become visible after the partition update is completed.
- Since:
- 3.0
- Author:
- Mark Paluch
-
Constructor Summary
Constructors Constructor Description Partitions() -
Method Summary
Modifier and Type Method Description booleanadd(RedisClusterNode redisClusterNode)Adds theRedisClusterNodeto thisPartitions.booleanaddAll(Collection<? extends RedisClusterNode> c)Add allnodesfrom the given collection and update the read-view/caches.voidaddPartition(RedisClusterNode partition)Adds a partition without updating the read view/cache.voidclear()Removes allnodesand update the read-view/caches.Partitionsclone()Create a deep copy of thisPartitionsobject.booleancontains(Object o)Returnstrueif thisPartitionscontains the specified element.booleancontainsAll(Collection<?> c)Returnstrueif this collection contains all of the elements in the specified collection.RedisClusterNodegetPartition(int index)Returns theRedisClusterNodeatindex.RedisClusterNodegetPartition(String host, int port)Retrieve aRedisClusterNodeby its hostname/port considering node aliases.RedisClusterNodegetPartitionByNodeId(String nodeId)Retrieve aRedisClusterNodeby its node id.RedisClusterNodegetPartitionBySlot(int slot)Retrieve aRedisClusterNodeby its slot number.List<RedisClusterNode>getPartitions()Returns the internalListofRedisClusterNodethat holds the partition source.booleanisEmpty()Returnstrueif thisPartitionscontains no elements using the read-view.Iterator<RedisClusterNode>iterator()Returns an iterator over thenodesin thisPartitionsfrom the read-view.voidreload(List<RedisClusterNode> partitions)Update partitions and rebuild slot cache.booleanremove(Object o)Remove the element from thisPartitions.booleanremoveAll(Collection<?> c)Remove allnodesfrom thePartitionsusing elements from the given collection and update the read-view/caches.booleanretainAll(Collection<?> c)Retains only the elements in thisPartitionsthat are contained in the specified collection (optional operation)and update the read-view/caches.intsize()Object[]toArray()Returns an array containing all of the elements in thisPartitionsusing the read-view.<T> T[]toArray(T[] a)Returns an array containing all of the elements in thisPartitionsusing the read-view.StringtoString()voidupdateCache()Update the partition cache.Methods inherited from class java.lang.Object
equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, waitMethods inherited from interface java.util.Collection
equals, hashCode, parallelStream, removeIf, spliterator, stream, toArray
-
Constructor Details
-
Partitions
public Partitions()
-
-
Method Details
-
clone
Create a deep copy of thisPartitionsobject.- Overrides:
clonein classObject- Returns:
- a deep copy of this
Partitionsobject.
-
getPartitionBySlot
Retrieve aRedisClusterNodeby its slot number. This method does not distinguish between masters and slaves.- Parameters:
slot- the slot hash.- Returns:
- the
RedisClusterNodeornullif not found.
-
getPartitionByNodeId
Retrieve aRedisClusterNodeby its node id.- Parameters:
nodeId- the nodeId.- Returns:
- the
RedisClusterNodeornullif not found.
-
getPartition
Retrieve aRedisClusterNodeby its hostname/port considering node aliases.- Parameters:
host- hostname.port- port number.- Returns:
- the
RedisClusterNodeornullif not found.
-
updateCache
public void updateCache()Update the partition cache. Updates are necessary after the partition details have changed. -
iterator
Returns an iterator over thenodesin thisPartitionsfrom the read-view. TheIteratorremains consistent during partition updates with the nodes that have been part of thePartitions.Nodesadded/removed during iteration/after obtaining theIteratordon't become visible during iteration but upon the next call toiterator().- Specified by:
iteratorin interfaceCollection<RedisClusterNode>- Specified by:
iteratorin interfaceIterable<RedisClusterNode>- Returns:
- an iterator over the
nodesin thisPartitionsfrom the read-view.
-
getPartitions
Returns the internalListofRedisClusterNodethat holds the partition source. ThisListis used to populate partition caches and should not be used directly and subject to change by refresh processes. Access (read/write) requires synchronization ongetPartitions().- Returns:
- the internal partition source.
-
addPartition
Adds a partition without updating the read view/cache.- Parameters:
partition- the partition
-
size
public int size()- Specified by:
sizein interfaceCollection<RedisClusterNode>- Returns:
- the number of elements using the read-view.
-
getPartition
Returns theRedisClusterNodeatindex.- Parameters:
index- the index- Returns:
- the requested element using the read-view.
-
reload
Update partitions and rebuild slot cache.- Parameters:
partitions- list of new partitions
-
isEmpty
public boolean isEmpty()Returnstrueif thisPartitionscontains no elements using the read-view.- Specified by:
isEmptyin interfaceCollection<RedisClusterNode>- Returns:
trueif thisPartitionscontains no elements using the read-view.
-
contains
Returnstrueif thisPartitionscontains the specified element.- Specified by:
containsin interfaceCollection<RedisClusterNode>- Parameters:
o- the element to check for- Returns:
trueif thisPartitionscontains the specified element
-
addAll
Add allnodesfrom the given collection and update the read-view/caches.- Specified by:
addAllin interfaceCollection<RedisClusterNode>- Parameters:
c- must not benull- Returns:
trueif thisPartitionschanged as a result of the call
-
removeAll
Remove allnodesfrom thePartitionsusing elements from the given collection and update the read-view/caches.- Specified by:
removeAllin interfaceCollection<RedisClusterNode>- Parameters:
c- must not benull- Returns:
trueif thisPartitionschanged as a result of the call
-
retainAll
Retains only the elements in thisPartitionsthat are contained in the specified collection (optional operation)and update the read-view/caches. In other words, removes from this collection all of its elements that are not contained in the specified collection.- Specified by:
retainAllin interfaceCollection<RedisClusterNode>- Parameters:
c- must not benull- Returns:
trueif thisPartitionschanged as a result of the call
-
clear
public void clear()Removes allnodesand update the read-view/caches.- Specified by:
clearin interfaceCollection<RedisClusterNode>
-
toArray
Returns an array containing all of the elements in thisPartitionsusing the read-view.- Specified by:
toArrayin interfaceCollection<RedisClusterNode>- Returns:
- an array containing all of the elements in this
Partitionsusing the read-view.
-
toArray
public <T> T[] toArray(T[] a)Returns an array containing all of the elements in thisPartitionsusing the read-view.- Specified by:
toArrayin interfaceCollection<RedisClusterNode>- Type Parameters:
T- type of the array to contain the collection- Parameters:
a- the array into which the elements of this collection are to be stored, if it is big enough; otherwise, a new array of the same runtime type is allocated for this purpose.- Returns:
- an array containing all of the elements in this
Partitionsusing the read-view.
-
add
Adds theRedisClusterNodeto thisPartitions.- Specified by:
addin interfaceCollection<RedisClusterNode>- Parameters:
redisClusterNode- must not benull- Returns:
trueif thisPartitionschanged as a result of the call
-
remove
Remove the element from thisPartitions.- Specified by:
removein interfaceCollection<RedisClusterNode>- Parameters:
o- must not benull- Returns:
trueif thisPartitionschanged as a result of the call
-
containsAll
Returnstrueif this collection contains all of the elements in the specified collection.- Specified by:
containsAllin interfaceCollection<RedisClusterNode>- Parameters:
c- collection to be checked for containment in this collection, must not benull- Returns:
-
toString
-