Module lettuce.core

Class Partitions

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.

Topology changes are:

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 Details

    • Partitions

      public Partitions()
  • Method Details

    • clone

      public Partitions clone()
      Create a deep copy of this Partitions object.
      Overrides:
      clone in class Object
      Returns:
      a deep copy of this Partitions object.
    • getPartitionBySlot

      public RedisClusterNode getPartitionBySlot​(int slot)
      Retrieve a RedisClusterNode by its slot number. This method does not distinguish between masters and slaves.
      Parameters:
      slot - the slot hash.
      Returns:
      the RedisClusterNode or null if not found.
    • getPartitionByNodeId

      public RedisClusterNode getPartitionByNodeId​(String nodeId)
      Retrieve a RedisClusterNode by its node id.
      Parameters:
      nodeId - the nodeId.
      Returns:
      the RedisClusterNode or null if not found.
    • getPartition

      public RedisClusterNode getPartition​(String host, int port)
      Retrieve a RedisClusterNode by its hostname/port considering node aliases.
      Parameters:
      host - hostname.
      port - port number.
      Returns:
      the RedisClusterNode or null if not found.
    • updateCache

      public void updateCache()
      Update the partition cache. Updates are necessary after the partition details have changed.
    • iterator

      public Iterator<RedisClusterNode> iterator()
      Returns an iterator over the nodes in this Partitions from the read-view. The Iterator remains consistent during partition updates with the nodes that have been part of the Partitions . Nodes added/removed during iteration/after obtaining the Iterator don't become visible during iteration but upon the next call to iterator().
      Specified by:
      iterator in interface Collection<RedisClusterNode>
      Specified by:
      iterator in interface Iterable<RedisClusterNode>
      Returns:
      an iterator over the nodes in this Partitions from the read-view.
    • getPartitions

      public List<RedisClusterNode> getPartitions()
      Returns the internal List of RedisClusterNode that holds the partition source. This List is used to populate partition caches and should not be used directly and subject to change by refresh processes. Access (read/write) requires synchronization on getPartitions().
      Returns:
      the internal partition source.
    • addPartition

      public void addPartition​(RedisClusterNode partition)
      Adds a partition without updating the read view/cache.
      Parameters:
      partition - the partition
    • size

      public int size()
      Specified by:
      size in interface Collection<RedisClusterNode>
      Returns:
      the number of elements using the read-view.
    • getPartition

      public RedisClusterNode getPartition​(int index)
      Returns the RedisClusterNode at index.
      Parameters:
      index - the index
      Returns:
      the requested element using the read-view.
    • reload

      public void reload​(List<RedisClusterNode> partitions)
      Update partitions and rebuild slot cache.
      Parameters:
      partitions - list of new partitions
    • isEmpty

      public boolean isEmpty()
      Returns true if this Partitions contains no elements using the read-view.
      Specified by:
      isEmpty in interface Collection<RedisClusterNode>
      Returns:
      true if this Partitions contains no elements using the read-view.
    • contains

      public boolean contains​(Object o)
      Returns true if this Partitions contains the specified element.
      Specified by:
      contains in interface Collection<RedisClusterNode>
      Parameters:
      o - the element to check for
      Returns:
      true if this Partitions contains the specified element
    • addAll

      public boolean addAll​(Collection<? extends RedisClusterNode> c)
      Add all nodes from the given collection and update the read-view/caches.
      Specified by:
      addAll in interface Collection<RedisClusterNode>
      Parameters:
      c - must not be null
      Returns:
      true if this Partitions changed as a result of the call
    • removeAll

      public boolean removeAll​(Collection<?> c)
      Remove all nodes from the Partitions using elements from the given collection and update the read-view/caches.
      Specified by:
      removeAll in interface Collection<RedisClusterNode>
      Parameters:
      c - must not be null
      Returns:
      true if this Partitions changed as a result of the call
    • retainAll

      public boolean retainAll​(Collection<?> c)
      Retains only the elements in this Partitions that 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:
      retainAll in interface Collection<RedisClusterNode>
      Parameters:
      c - must not be null
      Returns:
      true if this Partitions changed as a result of the call
    • clear

      public void clear()
      Removes all nodes and update the read-view/caches.
      Specified by:
      clear in interface Collection<RedisClusterNode>
    • toArray

      public Object[] toArray()
      Returns an array containing all of the elements in this Partitions using the read-view.
      Specified by:
      toArray in interface Collection<RedisClusterNode>
      Returns:
      an array containing all of the elements in this Partitions using the read-view.
    • toArray

      public <T> T[] toArray​(T[] a)
      Returns an array containing all of the elements in this Partitions using the read-view.
      Specified by:
      toArray in interface Collection<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 Partitions using the read-view.
    • add

      public boolean add​(RedisClusterNode redisClusterNode)
      Adds the RedisClusterNode to this Partitions.
      Specified by:
      add in interface Collection<RedisClusterNode>
      Parameters:
      redisClusterNode - must not be null
      Returns:
      true if this Partitions changed as a result of the call
    • remove

      public boolean remove​(Object o)
      Remove the element from this Partitions.
      Specified by:
      remove in interface Collection<RedisClusterNode>
      Parameters:
      o - must not be null
      Returns:
      true if this Partitions changed as a result of the call
    • containsAll

      public boolean containsAll​(Collection<?> c)
      Returns true if this collection contains all of the elements in the specified collection.
      Specified by:
      containsAll in interface Collection<RedisClusterNode>
      Parameters:
      c - collection to be checked for containment in this collection, must not be null
      Returns:
    • toString

      public String toString()
      Overrides:
      toString in class Object