Package org.jgroups.blocks
Class ReplCache<K,V>
- java.lang.Object
-
- org.jgroups.blocks.ReplCache<K,V>
-
- All Implemented Interfaces:
Cache.ChangeListener,Receiver
public class ReplCache<K,V> extends java.lang.Object implements Receiver, Cache.ChangeListener
Cache which allows for replication factors per data items; the factor determines how many replicas of a key/value we create across the cluster.
See doc/design/ReplCache.txt for details.
-
-
Nested Class Summary
Nested Classes Modifier and Type Class Description static interfaceReplCache.ChangeListenerstatic classReplCache.ConsistentHashFunction<K>static interfaceReplCache.HashFunction<K>static interfaceReplCache.HashFunctionFactory<K>static classReplCache.Value<V>
-
Field Summary
Fields Modifier and Type Field Description private longcaching_timeprivate longcall_timeoutprivate JChannelchprivate java.util.Set<ReplCache.ChangeListener>change_listenersprivate java.lang.Stringcluster_nameprivate shortdefault_replication_countprivate RpcDispatcherdispprivate static shortGETprivate ReplCache.HashFunction<K>hash_functionprivate ReplCache.HashFunctionFactory<K>hash_function_factoryprivate Cache<K,V>l1_cacheThe local bounded cache, to speed up access to frequently accessed entries.private Cache<K,ReplCache.Value<V>>l2_cacheThe cache in which all entries are located.private Addresslocal_addrprivate static Loglogprotected static java.util.Map<java.lang.Short,java.lang.reflect.Method>methodsprivate booleanmigrate_dataOn a view change, if a member P1 detects that for any given key K, P1 is not the owner of K, then it will compute the new owner P2 and transfer ownership for all Ks for which P2 is the new owner.private java.lang.Stringpropsprivate static shortPUTprivate static shortPUT_FORCEprivate java.util.Set<Receiver>receiversprivate static shortREMOVEprivate static shortREMOVE_MANYprivate TimeSchedulertimerprivate Viewview
-
Constructor Summary
Constructors Constructor Description ReplCache(java.lang.String props, java.lang.String cluster_name)
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description Cache.Value<ReplCache.Value<V>>_get(K key)V_put(K key, V val, short repl_count, long timeout)V_put(K key, V val, short repl_count, long timeout, boolean force)V_remove(K key)void_removeMany(java.util.Set<K> keys)voidaddChangeListener(ReplCache.ChangeListener l)voidaddReceiver(Receiver r)voidchanged()voidclear()Removes all keys and values in the L2 and L1 cachesjava.lang.Stringdump()Vget(K key)Returns the value associated with keylonggetCachingTime()longgetCallTimeout()java.lang.StringgetClusterName()intgetClusterSize()shortgetDefaultReplicationCount()ReplCache.HashFunctiongetHashFunction()ReplCache.HashFunctionFactorygetHashFunctionFactory()Cache<K,V>getL1Cache()Cache<K,ReplCache.Value<V>>getL2Cache()AddressgetLocalAddress()java.lang.StringgetLocalAddressAsString()java.lang.StringgetProps()java.lang.StringgetView()booleanisL1CacheEnabled()booleanisMigrateData()private voidmcastClear(java.util.Set<K> keys, boolean synchronous)voidmcastEntries()private voidmcastPut(K key, V val, short repl_count, long caching_time, boolean synchronous)private voidmove(Address dest, K key, V val, short repl_count, long caching_time, boolean synchronous)private voidnotifyChangeListeners()voidput(K key, V val)voidput(K key, V val, short repl_count, long timeout)Places a key/value pair into one or several nodes in the cluster.voidput(K key, V val, short repl_count, long timeout, boolean synchronous)Places a key/value pair into one or several nodes in the cluster.private voidrebalance(java.util.List<Address> old_nodes, java.util.List<Address> new_nodes)voidremove(K key)Removes key in all nodes in the cluster, both from their local hashmaps and L1 cachesvoidremove(K key, boolean synchronous)Removes key in all nodes in the cluster, both from their local hashmaps and L1 cachesvoidremoveChangeListener(ReplCache.ChangeListener l)voidremoveMembershipListener(Receiver r)voidsetCachingTime(long caching_time)voidsetCallTimeout(long call_timeout)voidsetClusterName(java.lang.String cluster_name)voidsetDefaultReplicationCount(short default_replication_count)voidsetHashFunction(ReplCache.HashFunction<K> hash_function)voidsetHashFunctionFactory(ReplCache.HashFunctionFactory<K> hash_function_factory)voidsetL1Cache(Cache<K,V> cache)voidsetL2Cache(Cache<K,ReplCache.Value<V>> cache)voidsetMigrateData(boolean migrate_data)voidsetProps(java.lang.String props)voidstart()voidstop()java.lang.StringtoString()voidviewAccepted(View new_view)Called when a change in membership has occurred.
-
-
-
Field Detail
-
l2_cache
private Cache<K,ReplCache.Value<V>> l2_cache
The cache in which all entries are located. The value is a tuple, consisting of the replication count and the actual value
-
l1_cache
private Cache<K,V> l1_cache
The local bounded cache, to speed up access to frequently accessed entries. Can be disabled or enabled
-
log
private static final Log log
-
ch
private JChannel ch
-
local_addr
private Address local_addr
-
view
private View view
-
disp
private RpcDispatcher disp
-
props
private java.lang.String props
-
cluster_name
private java.lang.String cluster_name
-
call_timeout
private long call_timeout
-
caching_time
private long caching_time
-
default_replication_count
private short default_replication_count
-
hash_function
private ReplCache.HashFunction<K> hash_function
-
hash_function_factory
private ReplCache.HashFunctionFactory<K> hash_function_factory
-
receivers
private final java.util.Set<Receiver> receivers
-
change_listeners
private final java.util.Set<ReplCache.ChangeListener> change_listeners
-
migrate_data
private boolean migrate_data
On a view change, if a member P1 detects that for any given key K, P1 is not the owner of K, then it will compute the new owner P2 and transfer ownership for all Ks for which P2 is the new owner. P1 will then also evict those keys from its L2 cache
-
PUT
private static final short PUT
- See Also:
- Constant Field Values
-
PUT_FORCE
private static final short PUT_FORCE
- See Also:
- Constant Field Values
-
GET
private static final short GET
- See Also:
- Constant Field Values
-
REMOVE
private static final short REMOVE
- See Also:
- Constant Field Values
-
REMOVE_MANY
private static final short REMOVE_MANY
- See Also:
- Constant Field Values
-
methods
protected static final java.util.Map<java.lang.Short,java.lang.reflect.Method> methods
-
timer
private TimeScheduler timer
-
-
Method Detail
-
getProps
public java.lang.String getProps()
-
setProps
public void setProps(java.lang.String props)
-
getLocalAddress
public Address getLocalAddress()
-
getLocalAddressAsString
public java.lang.String getLocalAddressAsString()
-
getView
public java.lang.String getView()
-
getClusterSize
public int getClusterSize()
-
isL1CacheEnabled
public boolean isL1CacheEnabled()
-
getClusterName
public java.lang.String getClusterName()
-
setClusterName
public void setClusterName(java.lang.String cluster_name)
-
getCallTimeout
public long getCallTimeout()
-
setCallTimeout
public void setCallTimeout(long call_timeout)
-
getCachingTime
public long getCachingTime()
-
setCachingTime
public void setCachingTime(long caching_time)
-
isMigrateData
public boolean isMigrateData()
-
setMigrateData
public void setMigrateData(boolean migrate_data)
-
getDefaultReplicationCount
public short getDefaultReplicationCount()
-
setDefaultReplicationCount
public void setDefaultReplicationCount(short default_replication_count)
-
getHashFunction
public ReplCache.HashFunction getHashFunction()
-
setHashFunction
public void setHashFunction(ReplCache.HashFunction<K> hash_function)
-
getHashFunctionFactory
public ReplCache.HashFunctionFactory getHashFunctionFactory()
-
setHashFunctionFactory
public void setHashFunctionFactory(ReplCache.HashFunctionFactory<K> hash_function_factory)
-
addReceiver
public void addReceiver(Receiver r)
-
removeMembershipListener
public void removeMembershipListener(Receiver r)
-
addChangeListener
public void addChangeListener(ReplCache.ChangeListener l)
-
removeChangeListener
public void removeChangeListener(ReplCache.ChangeListener l)
-
getL2Cache
public Cache<K,ReplCache.Value<V>> getL2Cache()
-
setL2Cache
public void setL2Cache(Cache<K,ReplCache.Value<V>> cache)
-
start
public void start() throws java.lang.Exception- Throws:
java.lang.Exception
-
stop
public void stop()
-
put
public void put(K key, V val, short repl_count, long timeout, boolean synchronous)
Places a key/value pair into one or several nodes in the cluster.- Parameters:
key- The key, needs to be serializableval- The value, needs to be serializablerepl_count- Number of replicas. The total number of times a data item should be present in a cluster. Needs to be > 0- -1: create key/val in all the nodes in the cluster
- 1: create key/val only in one node in the cluster, picked by computing the consistent hash of KEY
- K > 1: create key/val in those nodes in the cluster which match the consistent hashes created for KEY
timeout- Expiration time for key/value.- -1: don't cache at all in the L1 cache
- 0: cache forever, until removed or evicted because we need space for newer elements
- > 0: number of milliseconds to keep an idle element in the cache. An element is idle when not accessed.
synchronous- Whether or not to block until all cluster nodes have applied the change
-
put
public void put(K key, V val, short repl_count, long timeout)
Places a key/value pair into one or several nodes in the cluster.- Parameters:
key- The key, needs to be serializableval- The value, needs to be serializablerepl_count- Number of replicas. The total number of times a data item should be present in a cluster. Needs to be > 0- -1: create key/val in all the nodes in the cluster
- 1: create key/val only in one node in the cluster, picked by computing the consistent hash of KEY
- K > 1: create key/val in those nodes in the cluster which match the consistent hashes created for KEY
timeout- Expiration time for key/value.- -1: don't cache at all in the L1 cache
- 0: cache forever, until removed or evicted because we need space for newer elements
- > 0: number of milliseconds to keep an idle element in the cache. An element is idle when not accessed.
-
get
public V get(K key)
Returns the value associated with key- Parameters:
key- The key, has to be serializable- Returns:
- The value associated with key, or null
-
remove
public void remove(K key)
Removes key in all nodes in the cluster, both from their local hashmaps and L1 caches- Parameters:
key- The key, needs to be serializable
-
remove
public void remove(K key, boolean synchronous)
Removes key in all nodes in the cluster, both from their local hashmaps and L1 caches- Parameters:
key- The key, needs to be serializable
-
clear
public void clear()
Removes all keys and values in the L2 and L1 caches
-
_put
public V _put(K key, V val, short repl_count, long timeout, boolean force)
- Parameters:
key-val-repl_count-timeout-force- Skips acceptance checking and simply adds the key/value- Returns:
-
_get
public Cache.Value<ReplCache.Value<V>> _get(K key)
-
_removeMany
public void _removeMany(java.util.Set<K> keys)
-
viewAccepted
public void viewAccepted(View new_view)
Description copied from interface:ReceiverCalled when a change in membership has occurred. No long running actions, sending of messages or anything that could block should be done in this callback. If some long running action needs to be performed, it should be done in a separate thread.
Note that on reception of the first view (a new member just joined), the channel will not yet be in the connected state. This only happens whenJChannel.connect(String)returns.- Specified by:
viewAcceptedin interfaceReceiver
-
changed
public void changed()
- Specified by:
changedin interfaceCache.ChangeListener
-
toString
public java.lang.String toString()
- Overrides:
toStringin classjava.lang.Object
-
dump
public java.lang.String dump()
-
notifyChangeListeners
private void notifyChangeListeners()
-
rebalance
private void rebalance(java.util.List<Address> old_nodes, java.util.List<Address> new_nodes)
-
mcastEntries
public void mcastEntries()
-
mcastPut
private void mcastPut(K key, V val, short repl_count, long caching_time, boolean synchronous)
-
mcastClear
private void mcastClear(java.util.Set<K> keys, boolean synchronous)
-
-