[JBoss JIRA] (ISPN-8070) Remove deprecated code in 10.0
by Radim Vansa (JIRA)
Radim Vansa created ISPN-8070:
---------------------------------
Summary: Remove deprecated code in 10.0
Key: ISPN-8070
URL: https://issues.jboss.org/browse/ISPN-8070
Project: Infinispan
Issue Type: Task
Components: Core
Reporter: Radim Vansa
Assignee: Tristan Tarrant
Fix For: 10.0.0.Final
This is an umbrella task for removal of deprecated methods throughout code in 10.0 as we keep forgetting about that when a major version approaches.
--
This message was sent by Atlassian JIRA
(v7.2.3#72005)
7 years, 6 months
[JBoss JIRA] (ISPN-7086) Writes via memcached server cause UnknownOperationException
by Tristan Tarrant (JIRA)
[ https://issues.jboss.org/browse/ISPN-7086?page=com.atlassian.jira.plugin.... ]
Tristan Tarrant commented on ISPN-7086:
---------------------------------------
This is happening because the MemcachedDecoder is not checking if bytes are available before reading them.
> Writes via memcached server cause UnknownOperationException
> -----------------------------------------------------------
>
> Key: ISPN-7086
> URL: https://issues.jboss.org/browse/ISPN-7086
> Project: Infinispan
> Issue Type: Bug
> Components: Server
> Affects Versions: 8.2.6.Final, 9.0.1.Final, 9.1.0.Final
> Environment: Ubuntu 16.04.1 LTS
> openjdk version "1.8.0_91"
> OpenJDK Runtime Environment (build 1.8.0_91-8u91-b14-3ubuntu1~16.04.1-b14)
> OpenJDK 64-Bit Server VM (build 25.91-b14, mixed mode)
> The bug can be reproduced with OS X and other Linux distributions as well
> Reporter: Wolfram Huesken
> Labels: memcached
>
> We're using the stable server version (http://infinispan.org/download/) as a memcached replacement in a PHP application. When we're writing keys (or values) with different sizes via memcached, we encounter the following exception:
> {{17:14:42,531 ERROR [org.infinispan.server.memcached.RequestResolver$] (MemcachedServerWorker-8-1) ISPN005003: Exception reported: org.infinispan.server.memcached.UnknownOperationException: Unknown operation: }}
> When the exception occurs, the data is not stored.
> We're currently closing and reopening the connection after an error like that occurred, but it's slowing down the data transfer quite a lot.
> The bug is not only related to the server, but happens with the embedded version of infinispan, too.
--
This message was sent by Atlassian JIRA
(v7.2.3#72005)
7 years, 6 months
[JBoss JIRA] (ISPN-8069) Clustered Locks Embedded Mode
by Katia Aresti (JIRA)
[ https://issues.jboss.org/browse/ISPN-8069?page=com.atlassian.jira.plugin.... ]
Katia Aresti updated ISPN-8069:
-------------------------------
Description:
h2. ClusteredLockManager and configuration
{code:java}
package org.infinispan.lock.api;
public class ClusteredLockManager {
boolean defineLock(String name, LockConfiguration configuration);
InfinispanLock get(String name);
LockConfiguration getConfiguration(String name);
boolean isDefined(String name);
CompletableFuture<Boolean> remove(String name);
CompletableFuture<Void> reset(String name);
}
public class LockConfiguration {
private final RentrancyLevel lentrancyLevel; // default NOT_REENTRANT
private final boolean silentFailover; // default true
// The maximum length of time for which a client can hold and renew a lock aquisition
private final long maxLeaseTime;
// Maximum length of time a lock may be held without updating the lease,
// after that time any attempt to lock it will succeed
private final long renewalLeaseTime;
}
public enum RentrancyLevel {
NODE, // Node can lock if it owns the lock without blocking, only the owner node can unlock
INSTANCE, // Instance can lock multiple times if it owns the lock without blocking, only the owner instance can unlock
NOT_REENTRANT // Nobody can take the lock if already taken, but everybody can release it
}
{code}
h4. InfinispanLock defineLock(String name, LockConfiguration configuration)
Defines a lock with the specific name and LockConfiguration. It does not overwrite existing configurations.
Returns true if successfully defined or false if the lock is already defined or any other failure. If silentFailover is false, then InfinispanLockException will be raised.
h4. InfinispanLock get(String name)
Get’s a InfinipanLock by it’s name and throws InfinispanLockException if the lock is not not defined. User must call defineLock before this method.
h4. Optional<LockConfiguration> getConfiguration(String name);
Get’s the configuration for a Lock. If the Lock does not exist, Optional.empty() will be returned.
h4. boolean isDefined(String name)
True if the lock exists, false if it doesn’t
h4. CompletableFuture<Boolean> remove(String name)
Removes a Lock from the system. Returns true when it was removed, false when the lock does not exist. If any other Runtime problems appear, InfinispanLockException will be raised withe the reason. As Locks are not removed automatically, so this has to be done programatically when the Lock is no longer needed. Otherwise, OutOfMemoryException could happen.
Remove must be executed when the lock is locked, because running that without exclusive access should result in an exception. Internally, the implementation should contain generation number so that attempts to acquire a lock of a removed generation will result it exceptions in the other callers, too.
h4. CompletableFuture<Void> reset(String name)
Resets the lock to its initial state. If any parties are currently waiting at the lock, they will return with failure on the CompletableFuture
h2. InfinispanLock
When a cluster node holding a Lock dies, this lock is released and available for the others.
{code:java}
public interface InfinispanLock {
CompletableFuture<Void> lock();
CompletableFuture<Boolean> tryLock();
CompletableFuture<Boolean> tryLock(long time, TimeUnit unit);
CompletableFuture<Void> unlock();
}
{code}
h4. CompletableFuture<Void> lock();
CompletableFuture is completed successfully when the lock is acquired When a lock is aquired by a client, it will be automatically released after the maxLeaseTime specified. RenewalLeaseTime is the interval time is the time a client can aquire a lock consecutively User should set the timeouts to non-positive value The initial embedded implementation does not have to support positive values
h4. CompletableFuture<Boolean> tryLock();
Acquires the lock only if it is free at the time of invocation. Acquires the lock if it is available and returns with the value true. If the lock is not available then this method with the value false.
h4. CompletableFuture<Boolean> tryLock(long time, TimeUnit unit);
Acquires the lock if it is free within the given waiting time. If the lock is available this method returns with the value true.
Parameters: time - the maximum time to wait for the lock unit - the time unit of the time argument Returns: true if the lock was acquired and false if the waiting time elapsed before the lock was acquired
CompletableFuture fails with InfinispanLockException in case of error (InterruptedException, or any other non checked exceptions)
h4. CompletableFuture<Boolean> unlock();
If the lock is rentrant (Node or Instance), only the instance or node holding the lock will be able to unlock, otherwise, anybody can unlock and it will behave as a Semaphore with one permit. True answer will say that the operation was succesul and the lock has been released, false the lock has not been relased
h2. Demo
{code:java}
public static void main(String[] args) throws Exception {
EmbeddedCacheManager cm = Infinispan.createClustered();
CounterManager counterManager = EmbeddedCounterManagerFactory.asCounterManager(cm);
counterManager.defineCounter("counter", ...);
WeakCounter counter = counterManager.weakCounter("counter");
ClusteredLockManager lockManager = EmbeddedLockManagerFactory.asClusteredLockManager(cm);
lockManager.defineLock("lock", ...);
InfinispanLock lock = lockManager.get("lock");
for (int i = 0; i < 100; i++) {
System.out.println("Counter on " + i + " is => " + counter.getValue());
lock.lock()
.thenRun(new CounterExample(counter))
.whenComplete((nil, t) -> lock.unlock());
}
cm.stop();
}
static class CounterExample implements Runnable {
private WeakCounter counter;
public CounterExample(WeakCounter counter) {
this.counter = counter;
}
@Override
public void run() {
counter.increment();
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
e.printStackTrace();
}
counter.decrement();
}
}
{code}
was:
h3. ClusteredLockManager and configuration
{code:java}
package org.infinispan.lock.api;
public class ClusteredLockManager {
boolean defineLock(String name, LockConfiguration configuration);
InfinispanLock get(String name);
LockConfiguration getConfiguration(String name);
boolean isDefined(String name);
CompletableFuture<Boolean> remove(String name);
CompletableFuture<Void> reset(String name);
}
public class LockConfiguration {
private final RentrancyLevel lentrancyLevel; // default NOT_REENTRANT
private final boolean silentFailover; // default true
// The maximum length of time for which a client can hold and renew a lock aquisition
private final long maxLeaseTime;
// Maximum length of time a lock may be held without updating the lease,
// after that time any attempt to lock it will succeed
private final long renewalLeaseTime;
}
public enum RentrancyLevel {
NODE, // Node can lock if it owns the lock without blocking, only the owner node can unlock
INSTANCE, // Instance can lock multiple times if it owns the lock without blocking, only the owner instance can unlock
NOT_REENTRANT // Nobody can take the lock if already taken, but everybody can release it
}
{code}
h4. InfinispanLock defineLock(String name, LockConfiguration configuration)
Defines a lock with the specific name and LockConfiguration. It does not overwrite existing configurations.
Returns true if successfully defined or false if the lock is already defined or any other failure. If silentFailover is false, then InfinispanLockException will be raised.
h4. InfinispanLock get(String name)
Get’s a InfinipanLock by it’s name and throws InfinispanLockException if the lock is not not defined. User must call defineLock before this method.
h4. Optional<LockConfiguration> getConfiguration(String name);
Get’s the configuration for a Lock. If the Lock does not exist, Optional.empty() will be returned.
h4. boolean isDefined(String name)
True if the lock exists, false if it doesn’t
h4. CompletableFuture<Boolean> remove(String name)
Removes a Lock from the system. Returns true when it was removed, false when the lock does not exist. If any other Runtime problems appear, InfinispanLockException will be raised withe the reason. As Locks are not removed automatically, so this has to be done programatically when the Lock is no longer needed. Otherwise, OutOfMemoryException could happen.
Remove must be executed when the lock is locked, because running that without exclusive access should result in an exception. Internally, the implementation should contain generation number so that attempts to acquire a lock of a removed generation will result it exceptions in the other callers, too.
h4. CompletableFuture<Void> reset(String name)
Resets the lock to its initial state. If any parties are currently waiting at the lock, they will return with failure on the CompletableFuture
h3. InfinispanLock
When a cluster node holding a Lock dies, this lock is released and available for the others.
{code:java}
public interface InfinispanLock {
CompletableFuture<Void> lock();
CompletableFuture<Boolean> tryLock();
CompletableFuture<Boolean> tryLock(long time, TimeUnit unit);
CompletableFuture<Void> unlock();
}
{code}
h4. CompletableFuture<Void> lock();
CompletableFuture is completed successfully when the lock is acquired When a lock is aquired by a client, it will be automatically released after the maxLeaseTime specified. RenewalLeaseTime is the interval time is the time a client can aquire a lock consecutively User should set the timeouts to non-positive value The initial embedded implementation does not have to support positive values
h4. CompletableFuture<Boolean> tryLock();
Acquires the lock only if it is free at the time of invocation. Acquires the lock if it is available and returns with the value true. If the lock is not available then this method with the value false.
h4. CompletableFuture<Boolean> tryLock(long time, TimeUnit unit);
Acquires the lock if it is free within the given waiting time. If the lock is available this method returns with the value true.
Parameters: time - the maximum time to wait for the lock unit - the time unit of the time argument Returns: true if the lock was acquired and false if the waiting time elapsed before the lock was acquired
CompletableFuture fails with InfinispanLockException in case of error (InterruptedException, or any other non checked exceptions)
h4. CompletableFuture<Boolean> unlock();
If the lock is rentrant (Node or Instance), only the instance or node holding the lock will be able to unlock, otherwise, anybody can unlock and it will behave as a Semaphore with one permit. True answer will say that the operation was succesul and the lock has been released, false the lock has not been relased
h3. Demo
{code:java}
public static void main(String[] args) throws Exception {
EmbeddedCacheManager cm = Infinispan.createClustered();
CounterManager counterManager = EmbeddedCounterManagerFactory.asCounterManager(cm);
counterManager.defineCounter("counter", ...);
WeakCounter counter = counterManager.weakCounter("counter");
ClusteredLockManager lockManager = EmbeddedLockManagerFactory.asClusteredLockManager(cm);
lockManager.defineLock("lock", ...);
InfinispanLock lock = lockManager.get("lock");
for (int i = 0; i < 100; i++) {
System.out.println("Counter on " + i + " is => " + counter.getValue());
lock.lock()
.thenRun(new CounterExample(counter))
.whenComplete((nil, t) -> lock.unlock());
}
cm.stop();
}
static class CounterExample implements Runnable {
private WeakCounter counter;
public CounterExample(WeakCounter counter) {
this.counter = counter;
}
@Override
public void run() {
counter.increment();
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
e.printStackTrace();
}
counter.decrement();
}
}
{code}
> Clustered Locks Embedded Mode
> -----------------------------
>
> Key: ISPN-8069
> URL: https://issues.jboss.org/browse/ISPN-8069
> Project: Infinispan
> Issue Type: Feature Request
> Reporter: Katia Aresti
> Assignee: Katia Aresti
>
> h2. ClusteredLockManager and configuration
> {code:java}
> package org.infinispan.lock.api;
> public class ClusteredLockManager {
> boolean defineLock(String name, LockConfiguration configuration);
> InfinispanLock get(String name);
> LockConfiguration getConfiguration(String name);
> boolean isDefined(String name);
> CompletableFuture<Boolean> remove(String name);
> CompletableFuture<Void> reset(String name);
> }
> public class LockConfiguration {
> private final RentrancyLevel lentrancyLevel; // default NOT_REENTRANT
> private final boolean silentFailover; // default true
> // The maximum length of time for which a client can hold and renew a lock aquisition
> private final long maxLeaseTime;
> // Maximum length of time a lock may be held without updating the lease,
> // after that time any attempt to lock it will succeed
> private final long renewalLeaseTime;
> }
> public enum RentrancyLevel {
> NODE, // Node can lock if it owns the lock without blocking, only the owner node can unlock
> INSTANCE, // Instance can lock multiple times if it owns the lock without blocking, only the owner instance can unlock
> NOT_REENTRANT // Nobody can take the lock if already taken, but everybody can release it
> }
> {code}
> h4. InfinispanLock defineLock(String name, LockConfiguration configuration)
> Defines a lock with the specific name and LockConfiguration. It does not overwrite existing configurations.
> Returns true if successfully defined or false if the lock is already defined or any other failure. If silentFailover is false, then InfinispanLockException will be raised.
> h4. InfinispanLock get(String name)
> Get’s a InfinipanLock by it’s name and throws InfinispanLockException if the lock is not not defined. User must call defineLock before this method.
> h4. Optional<LockConfiguration> getConfiguration(String name);
> Get’s the configuration for a Lock. If the Lock does not exist, Optional.empty() will be returned.
> h4. boolean isDefined(String name)
> True if the lock exists, false if it doesn’t
> h4. CompletableFuture<Boolean> remove(String name)
> Removes a Lock from the system. Returns true when it was removed, false when the lock does not exist. If any other Runtime problems appear, InfinispanLockException will be raised withe the reason. As Locks are not removed automatically, so this has to be done programatically when the Lock is no longer needed. Otherwise, OutOfMemoryException could happen.
> Remove must be executed when the lock is locked, because running that without exclusive access should result in an exception. Internally, the implementation should contain generation number so that attempts to acquire a lock of a removed generation will result it exceptions in the other callers, too.
> h4. CompletableFuture<Void> reset(String name)
> Resets the lock to its initial state. If any parties are currently waiting at the lock, they will return with failure on the CompletableFuture
> h2. InfinispanLock
>
> When a cluster node holding a Lock dies, this lock is released and available for the others.
> {code:java}
> public interface InfinispanLock {
> CompletableFuture<Void> lock();
> CompletableFuture<Boolean> tryLock();
> CompletableFuture<Boolean> tryLock(long time, TimeUnit unit);
> CompletableFuture<Void> unlock();
> }
> {code}
> h4. CompletableFuture<Void> lock();
> CompletableFuture is completed successfully when the lock is acquired When a lock is aquired by a client, it will be automatically released after the maxLeaseTime specified. RenewalLeaseTime is the interval time is the time a client can aquire a lock consecutively User should set the timeouts to non-positive value The initial embedded implementation does not have to support positive values
> h4. CompletableFuture<Boolean> tryLock();
> Acquires the lock only if it is free at the time of invocation. Acquires the lock if it is available and returns with the value true. If the lock is not available then this method with the value false.
> h4. CompletableFuture<Boolean> tryLock(long time, TimeUnit unit);
> Acquires the lock if it is free within the given waiting time. If the lock is available this method returns with the value true.
> Parameters: time - the maximum time to wait for the lock unit - the time unit of the time argument Returns: true if the lock was acquired and false if the waiting time elapsed before the lock was acquired
> CompletableFuture fails with InfinispanLockException in case of error (InterruptedException, or any other non checked exceptions)
> h4. CompletableFuture<Boolean> unlock();
> If the lock is rentrant (Node or Instance), only the instance or node holding the lock will be able to unlock, otherwise, anybody can unlock and it will behave as a Semaphore with one permit. True answer will say that the operation was succesul and the lock has been released, false the lock has not been relased
> h2. Demo
> {code:java}
> public static void main(String[] args) throws Exception {
> EmbeddedCacheManager cm = Infinispan.createClustered();
> CounterManager counterManager = EmbeddedCounterManagerFactory.asCounterManager(cm);
> counterManager.defineCounter("counter", ...);
> WeakCounter counter = counterManager.weakCounter("counter");
> ClusteredLockManager lockManager = EmbeddedLockManagerFactory.asClusteredLockManager(cm);
> lockManager.defineLock("lock", ...);
> InfinispanLock lock = lockManager.get("lock");
> for (int i = 0; i < 100; i++) {
> System.out.println("Counter on " + i + " is => " + counter.getValue());
> lock.lock()
> .thenRun(new CounterExample(counter))
> .whenComplete((nil, t) -> lock.unlock());
> }
> cm.stop();
> }
> static class CounterExample implements Runnable {
> private WeakCounter counter;
> public CounterExample(WeakCounter counter) {
> this.counter = counter;
> }
> @Override
> public void run() {
> counter.increment();
> try {
> Thread.sleep(1000);
> } catch (InterruptedException e) {
> e.printStackTrace();
> }
> counter.decrement();
> }
> }
> {code}
--
This message was sent by Atlassian JIRA
(v7.2.3#72005)
7 years, 6 months
[JBoss JIRA] (ISPN-8069) Clustered Locks Embedded Mode
by Katia Aresti (JIRA)
[ https://issues.jboss.org/browse/ISPN-8069?page=com.atlassian.jira.plugin.... ]
Katia Aresti updated ISPN-8069:
-------------------------------
Description:
h3. ClusteredLockManager and configuration
{code:java}
package org.infinispan.lock.api;
public class ClusteredLockManager {
boolean defineLock(String name, LockConfiguration configuration);
InfinispanLock get(String name);
LockConfiguration getConfiguration(String name);
boolean isDefined(String name);
CompletableFuture<Boolean> remove(String name);
CompletableFuture<Void> reset(String name);
}
public class LockConfiguration {
private final RentrancyLevel lentrancyLevel; // default NOT_REENTRANT
private final boolean silentFailover; // default true
// The maximum length of time for which a client can hold and renew a lock aquisition
private final long maxLeaseTime;
// Maximum length of time a lock may be held without updating the lease,
// after that time any attempt to lock it will succeed
private final long renewalLeaseTime;
}
public enum RentrancyLevel {
NODE, // Node can lock if it owns the lock without blocking, only the owner node can unlock
INSTANCE, // Instance can lock multiple times if it owns the lock without blocking, only the owner instance can unlock
NOT_REENTRANT // Nobody can take the lock if already taken, but everybody can release it
}
{code}
h4. InfinispanLock defineLock(String name, LockConfiguration configuration)
Defines a lock with the specific name and LockConfiguration. It does not overwrite existing configurations.
Returns true if successfully defined or false if the lock is already defined or any other failure. If silentFailover is false, then InfinispanLockException will be raised.
h4. InfinispanLock get(String name)
Get’s a InfinipanLock by it’s name and throws InfinispanLockException if the lock is not not defined. User must call defineLock before this method.
h4. Optional<LockConfiguration> getConfiguration(String name);
Get’s the configuration for a Lock. If the Lock does not exist, Optional.empty() will be returned.
h4. boolean isDefined(String name)
True if the lock exists, false if it doesn’t
h4. CompletableFuture<Boolean> remove(String name)
Removes a Lock from the system. Returns true when it was removed, false when the lock does not exist. If any other Runtime problems appear, InfinispanLockException will be raised withe the reason. As Locks are not removed automatically, so this has to be done programatically when the Lock is no longer needed. Otherwise, OutOfMemoryException could happen.
Remove must be executed when the lock is locked, because running that without exclusive access should result in an exception. Internally, the implementation should contain generation number so that attempts to acquire a lock of a removed generation will result it exceptions in the other callers, too.
h4. CompletableFuture<Void> reset(String name)
Resets the lock to its initial state. If any parties are currently waiting at the lock, they will return with failure on the CompletableFuture
h3. InfinispanLock
When a cluster node holding a Lock dies, this lock is released and available for the others.
{code:java}
public interface InfinispanLock {
CompletableFuture<Void> lock();
CompletableFuture<Boolean> tryLock();
CompletableFuture<Boolean> tryLock(long time, TimeUnit unit);
CompletableFuture<Void> unlock();
}
{code}
h4. CompletableFuture<Void> lock();
CompletableFuture is completed successfully when the lock is acquired When a lock is aquired by a client, it will be automatically released after the maxLeaseTime specified. RenewalLeaseTime is the interval time is the time a client can aquire a lock consecutively User should set the timeouts to non-positive value The initial embedded implementation does not have to support positive values
h4. CompletableFuture<Boolean> tryLock();
Acquires the lock only if it is free at the time of invocation. Acquires the lock if it is available and returns with the value true. If the lock is not available then this method with the value false.
h4. CompletableFuture<Boolean> tryLock(long time, TimeUnit unit);
Acquires the lock if it is free within the given waiting time. If the lock is available this method returns with the value true.
Parameters: time - the maximum time to wait for the lock unit - the time unit of the time argument Returns: true if the lock was acquired and false if the waiting time elapsed before the lock was acquired
CompletableFuture fails with InfinispanLockException in case of error (InterruptedException, or any other non checked exceptions)
h4. CompletableFuture<Boolean> unlock();
If the lock is rentrant (Node or Instance), only the instance or node holding the lock will be able to unlock, otherwise, anybody can unlock and it will behave as a Semaphore with one permit. True answer will say that the operation was succesul and the lock has been released, false the lock has not been relased
h3. Demo
{code:java}
public static void main(String[] args) throws Exception {
EmbeddedCacheManager cm = Infinispan.createClustered();
CounterManager counterManager = EmbeddedCounterManagerFactory.asCounterManager(cm);
counterManager.defineCounter("counter", ...);
WeakCounter counter = counterManager.weakCounter("counter");
ClusteredLockManager lockManager = EmbeddedLockManagerFactory.asClusteredLockManager(cm);
lockManager.defineLock("lock", ...);
InfinispanLock lock = lockManager.get("lock");
for (int i = 0; i < 100; i++) {
System.out.println("Counter on " + i + " is => " + counter.getValue());
lock.lock()
.thenRun(new CounterExample(counter))
.whenComplete((nil, t) -> lock.unlock());
}
cm.stop();
}
static class CounterExample implements Runnable {
private WeakCounter counter;
public CounterExample(WeakCounter counter) {
this.counter = counter;
}
@Override
public void run() {
counter.increment();
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
e.printStackTrace();
}
counter.decrement();
}
}
{code}
was:
h3. *ClusteredLockManager and configuration
{code:java}
package org.infinispan.lock.api;
public class ClusteredLockManager {
boolean defineLock(String name, LockConfiguration configuration);
InfinispanLock get(String name);
LockConfiguration getConfiguration(String name);
boolean isDefined(String name);
CompletableFuture<Boolean> remove(String name);
CompletableFuture<Void> reset(String name);
}
public class LockConfiguration {
private final RentrancyLevel lentrancyLevel; // default NOT_REENTRANT
private final boolean silentFailover; // default true
// The maximum length of time for which a client can hold and renew a lock aquisition
private final long maxLeaseTime;
// Maximum length of time a lock may be held without updating the lease,
// after that time any attempt to lock it will succeed
private final long renewalLeaseTime;
}
public enum RentrancyLevel {
NODE, // Node can lock if it owns the lock without blocking, only the owner node can unlock
INSTANCE, // Instance can lock multiple times if it owns the lock without blocking, only the owner instance can unlock
NOT_REENTRANT // Nobody can take the lock if already taken, but everybody can release it
}
{code}
*h4. InfinispanLock defineLock(String name, LockConfiguration configuration)
Defines a lock with the specific name and LockConfiguration. It does not overwrite existing configurations.
Returns true if successfully defined or false if the lock is already defined or any other failure. If silentFailover is false, then InfinispanLockException will be raised.
*h4. InfinispanLock get(String name)
Get’s a InfinipanLock by it’s name and throws InfinispanLockException if the lock is not not defined. User must call defineLock before this method.
*h4. Optional<LockConfiguration> getConfiguration(String name);
Get’s the configuration for a Lock. If the Lock does not exist, Optional.empty() will be returned.
*h4. boolean isDefined(String name)
True if the lock exists, false if it doesn’t
*h4. CompletableFuture<Boolean> remove(String name)
Removes a Lock from the system. Returns true when it was removed, false when the lock does not exist. If any other Runtime problems appear, InfinispanLockException will be raised withe the reason. As Locks are not removed automatically, so this has to be done programatically when the Lock is no longer needed. Otherwise, OutOfMemoryException could happen.
Remove must be executed when the lock is locked, because running that without exclusive access should result in an exception. Internally, the implementation should contain generation number so that attempts to acquire a lock of a removed generation will result it exceptions in the other callers, too.
*h4. CompletableFuture<Void> reset(String name)
Resets the lock to its initial state. If any parties are currently waiting at the lock, they will return with failure on the CompletableFuture
*h3. InfinispanLock
When a cluster node holding a Lock dies, this lock is released and available for the others.
{code:java}
public interface InfinispanLock {
CompletableFuture<Void> lock();
CompletableFuture<Boolean> tryLock();
CompletableFuture<Boolean> tryLock(long time, TimeUnit unit);
CompletableFuture<Void> unlock();
}
{code}
*h4. CompletableFuture<Void> lock();
CompletableFuture is completed successfully when the lock is acquired When a lock is aquired by a client, it will be automatically released after the maxLeaseTime specified. RenewalLeaseTime is the interval time is the time a client can aquire a lock consecutively User should set the timeouts to non-positive value The initial embedded implementation does not have to support positive values
*h4. CompletableFuture<Boolean> tryLock();
Acquires the lock only if it is free at the time of invocation. Acquires the lock if it is available and returns with the value true. If the lock is not available then this method with the value false.
*h4. CompletableFuture<Boolean> tryLock(long time, TimeUnit unit);
Acquires the lock if it is free within the given waiting time. If the lock is available this method returns with the value true.
Parameters: time - the maximum time to wait for the lock unit - the time unit of the time argument Returns: true if the lock was acquired and false if the waiting time elapsed before the lock was acquired
CompletableFuture fails with InfinispanLockException in case of error (InterruptedException, or any other non checked exceptions)
*h4. CompletableFuture<Boolean> unlock();
If the lock is rentrant (Node or Instance), only the instance or node holding the lock will be able to unlock, otherwise, anybody can unlock and it will behave as a Semaphore with one permit. True answer will say that the operation was succesul and the lock has been released, false the lock has not been relased
*h3. Demo
{code:java}
public static void main(String[] args) throws Exception {
EmbeddedCacheManager cm = Infinispan.createClustered();
CounterManager counterManager = EmbeddedCounterManagerFactory.asCounterManager(cm);
counterManager.defineCounter("counter", ...);
WeakCounter counter = counterManager.weakCounter("counter");
ClusteredLockManager lockManager = EmbeddedLockManagerFactory.asClusteredLockManager(cm);
lockManager.defineLock("lock", ...);
InfinispanLock lock = lockManager.get("lock");
for (int i = 0; i < 100; i++) {
System.out.println("Counter on " + i + " is => " + counter.getValue());
lock.lock()
.thenRun(new CounterExample(counter))
.whenComplete((nil, t) -> lock.unlock());
}
cm.stop();
}
static class CounterExample implements Runnable {
private WeakCounter counter;
public CounterExample(WeakCounter counter) {
this.counter = counter;
}
@Override
public void run() {
counter.increment();
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
e.printStackTrace();
}
counter.decrement();
}
}
{code}
> Clustered Locks Embedded Mode
> -----------------------------
>
> Key: ISPN-8069
> URL: https://issues.jboss.org/browse/ISPN-8069
> Project: Infinispan
> Issue Type: Feature Request
> Reporter: Katia Aresti
> Assignee: Katia Aresti
>
> h3. ClusteredLockManager and configuration
> {code:java}
> package org.infinispan.lock.api;
> public class ClusteredLockManager {
> boolean defineLock(String name, LockConfiguration configuration);
> InfinispanLock get(String name);
> LockConfiguration getConfiguration(String name);
> boolean isDefined(String name);
> CompletableFuture<Boolean> remove(String name);
> CompletableFuture<Void> reset(String name);
> }
> public class LockConfiguration {
> private final RentrancyLevel lentrancyLevel; // default NOT_REENTRANT
> private final boolean silentFailover; // default true
> // The maximum length of time for which a client can hold and renew a lock aquisition
> private final long maxLeaseTime;
> // Maximum length of time a lock may be held without updating the lease,
> // after that time any attempt to lock it will succeed
> private final long renewalLeaseTime;
> }
> public enum RentrancyLevel {
> NODE, // Node can lock if it owns the lock without blocking, only the owner node can unlock
> INSTANCE, // Instance can lock multiple times if it owns the lock without blocking, only the owner instance can unlock
> NOT_REENTRANT // Nobody can take the lock if already taken, but everybody can release it
> }
> {code}
> h4. InfinispanLock defineLock(String name, LockConfiguration configuration)
> Defines a lock with the specific name and LockConfiguration. It does not overwrite existing configurations.
> Returns true if successfully defined or false if the lock is already defined or any other failure. If silentFailover is false, then InfinispanLockException will be raised.
> h4. InfinispanLock get(String name)
> Get’s a InfinipanLock by it’s name and throws InfinispanLockException if the lock is not not defined. User must call defineLock before this method.
> h4. Optional<LockConfiguration> getConfiguration(String name);
> Get’s the configuration for a Lock. If the Lock does not exist, Optional.empty() will be returned.
> h4. boolean isDefined(String name)
> True if the lock exists, false if it doesn’t
> h4. CompletableFuture<Boolean> remove(String name)
> Removes a Lock from the system. Returns true when it was removed, false when the lock does not exist. If any other Runtime problems appear, InfinispanLockException will be raised withe the reason. As Locks are not removed automatically, so this has to be done programatically when the Lock is no longer needed. Otherwise, OutOfMemoryException could happen.
> Remove must be executed when the lock is locked, because running that without exclusive access should result in an exception. Internally, the implementation should contain generation number so that attempts to acquire a lock of a removed generation will result it exceptions in the other callers, too.
> h4. CompletableFuture<Void> reset(String name)
> Resets the lock to its initial state. If any parties are currently waiting at the lock, they will return with failure on the CompletableFuture
> h3. InfinispanLock
>
> When a cluster node holding a Lock dies, this lock is released and available for the others.
> {code:java}
> public interface InfinispanLock {
> CompletableFuture<Void> lock();
> CompletableFuture<Boolean> tryLock();
> CompletableFuture<Boolean> tryLock(long time, TimeUnit unit);
> CompletableFuture<Void> unlock();
> }
> {code}
> h4. CompletableFuture<Void> lock();
> CompletableFuture is completed successfully when the lock is acquired When a lock is aquired by a client, it will be automatically released after the maxLeaseTime specified. RenewalLeaseTime is the interval time is the time a client can aquire a lock consecutively User should set the timeouts to non-positive value The initial embedded implementation does not have to support positive values
> h4. CompletableFuture<Boolean> tryLock();
> Acquires the lock only if it is free at the time of invocation. Acquires the lock if it is available and returns with the value true. If the lock is not available then this method with the value false.
> h4. CompletableFuture<Boolean> tryLock(long time, TimeUnit unit);
> Acquires the lock if it is free within the given waiting time. If the lock is available this method returns with the value true.
> Parameters: time - the maximum time to wait for the lock unit - the time unit of the time argument Returns: true if the lock was acquired and false if the waiting time elapsed before the lock was acquired
> CompletableFuture fails with InfinispanLockException in case of error (InterruptedException, or any other non checked exceptions)
> h4. CompletableFuture<Boolean> unlock();
> If the lock is rentrant (Node or Instance), only the instance or node holding the lock will be able to unlock, otherwise, anybody can unlock and it will behave as a Semaphore with one permit. True answer will say that the operation was succesul and the lock has been released, false the lock has not been relased
> h3. Demo
> {code:java}
> public static void main(String[] args) throws Exception {
> EmbeddedCacheManager cm = Infinispan.createClustered();
> CounterManager counterManager = EmbeddedCounterManagerFactory.asCounterManager(cm);
> counterManager.defineCounter("counter", ...);
> WeakCounter counter = counterManager.weakCounter("counter");
> ClusteredLockManager lockManager = EmbeddedLockManagerFactory.asClusteredLockManager(cm);
> lockManager.defineLock("lock", ...);
> InfinispanLock lock = lockManager.get("lock");
> for (int i = 0; i < 100; i++) {
> System.out.println("Counter on " + i + " is => " + counter.getValue());
> lock.lock()
> .thenRun(new CounterExample(counter))
> .whenComplete((nil, t) -> lock.unlock());
> }
> cm.stop();
> }
> static class CounterExample implements Runnable {
> private WeakCounter counter;
> public CounterExample(WeakCounter counter) {
> this.counter = counter;
> }
> @Override
> public void run() {
> counter.increment();
> try {
> Thread.sleep(1000);
> } catch (InterruptedException e) {
> e.printStackTrace();
> }
> counter.decrement();
> }
> }
> {code}
--
This message was sent by Atlassian JIRA
(v7.2.3#72005)
7 years, 6 months
[JBoss JIRA] (ISPN-8069) Clustered Locks Embedded Mode
by Katia Aresti (JIRA)
[ https://issues.jboss.org/browse/ISPN-8069?page=com.atlassian.jira.plugin.... ]
Katia Aresti updated ISPN-8069:
-------------------------------
Description:
*h3. ClusteredLockManager and configuration
{code:java}
package org.infinispan.lock.api;
public class ClusteredLockManager {
boolean defineLock(String name, LockConfiguration configuration);
InfinispanLock get(String name);
LockConfiguration getConfiguration(String name);
boolean isDefined(String name);
CompletableFuture<Boolean> remove(String name);
CompletableFuture<Void> reset(String name);
}
public class LockConfiguration {
private final RentrancyLevel lentrancyLevel; // default NOT_REENTRANT
private final boolean silentFailover; // default true
// The maximum length of time for which a client can hold and renew a lock aquisition
private final long maxLeaseTime;
// Maximum length of time a lock may be held without updating the lease,
// after that time any attempt to lock it will succeed
private final long renewalLeaseTime;
}
public enum RentrancyLevel {
NODE, // Node can lock if it owns the lock without blocking, only the owner node can unlock
INSTANCE, // Instance can lock multiple times if it owns the lock without blocking, only the owner instance can unlock
NOT_REENTRANT // Nobody can take the lock if already taken, but everybody can release it
}
{code}
*h4. InfinispanLock defineLock(String name, LockConfiguration configuration)
Defines a lock with the specific name and LockConfiguration. It does not overwrite existing configurations.
Returns true if successfully defined or false if the lock is already defined or any other failure. If silentFailover is false, then InfinispanLockException will be raised.
*h4. InfinispanLock get(String name)
Get’s a InfinipanLock by it’s name and throws InfinispanLockException if the lock is not not defined. User must call defineLock before this method.
*h4. Optional<LockConfiguration> getConfiguration(String name);
Get’s the configuration for a Lock. If the Lock does not exist, Optional.empty() will be returned.
*h4. boolean isDefined(String name)
True if the lock exists, false if it doesn’t
*h4. CompletableFuture<Boolean> remove(String name)
Removes a Lock from the system. Returns true when it was removed, false when the lock does not exist. If any other Runtime problems appear, InfinispanLockException will be raised withe the reason. As Locks are not removed automatically, so this has to be done programatically when the Lock is no longer needed. Otherwise, OutOfMemoryException could happen.
Remove must be executed when the lock is locked, because running that without exclusive access should result in an exception. Internally, the implementation should contain generation number so that attempts to acquire a lock of a removed generation will result it exceptions in the other callers, too.
*h4. CompletableFuture<Void> reset(String name)
Resets the lock to its initial state. If any parties are currently waiting at the lock, they will return with failure on the CompletableFuture
*h3. InfinispanLock
When a cluster node holding a Lock dies, this lock is released and available for the others.
{code:java}
public interface InfinispanLock {
CompletableFuture<Void> lock();
CompletableFuture<Boolean> tryLock();
CompletableFuture<Boolean> tryLock(long time, TimeUnit unit);
CompletableFuture<Void> unlock();
}
{code}
*h4. CompletableFuture<Void> lock();
CompletableFuture is completed successfully when the lock is acquired When a lock is aquired by a client, it will be automatically released after the maxLeaseTime specified. RenewalLeaseTime is the interval time is the time a client can aquire a lock consecutively User should set the timeouts to non-positive value The initial embedded implementation does not have to support positive values
*h4. CompletableFuture<Boolean> tryLock();
Acquires the lock only if it is free at the time of invocation. Acquires the lock if it is available and returns with the value true. If the lock is not available then this method with the value false.
*h4. CompletableFuture<Boolean> tryLock(long time, TimeUnit unit);
Acquires the lock if it is free within the given waiting time. If the lock is available this method returns with the value true.
Parameters: time - the maximum time to wait for the lock unit - the time unit of the time argument Returns: true if the lock was acquired and false if the waiting time elapsed before the lock was acquired
CompletableFuture fails with InfinispanLockException in case of error (InterruptedException, or any other non checked exceptions)
*h4. CompletableFuture<Boolean> unlock();
If the lock is rentrant (Node or Instance), only the instance or node holding the lock will be able to unlock, otherwise, anybody can unlock and it will behave as a Semaphore with one permit. True answer will say that the operation was succesul and the lock has been released, false the lock has not been relased
*h3. Demo
{code:java}
public static void main(String[] args) throws Exception {
EmbeddedCacheManager cm = Infinispan.createClustered();
CounterManager counterManager = EmbeddedCounterManagerFactory.asCounterManager(cm);
counterManager.defineCounter("counter", ...);
WeakCounter counter = counterManager.weakCounter("counter");
ClusteredLockManager lockManager = EmbeddedLockManagerFactory.asClusteredLockManager(cm);
lockManager.defineLock("lock", ...);
InfinispanLock lock = lockManager.get("lock");
for (int i = 0; i < 100; i++) {
System.out.println("Counter on " + i + " is => " + counter.getValue());
lock.lock()
.thenRun(new CounterExample(counter))
.whenComplete((nil, t) -> lock.unlock());
}
cm.stop();
}
static class CounterExample implements Runnable {
private WeakCounter counter;
public CounterExample(WeakCounter counter) {
this.counter = counter;
}
@Override
public void run() {
counter.increment();
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
e.printStackTrace();
}
counter.decrement();
}
}
{code}
was:
h3. ClusteredLockManager and configuration
{code:java}
package org.infinispan.lock.api;
public class ClusteredLockManager {
boolean defineLock(String name, LockConfiguration configuration);
InfinispanLock get(String name);
LockConfiguration getConfiguration(String name);
boolean isDefined(String name);
CompletableFuture<Boolean> remove(String name);
CompletableFuture<Void> reset(String name);
}
public class LockConfiguration {
private final RentrancyLevel lentrancyLevel; // default NOT_REENTRANT
private final boolean silentFailover; // default true
// The maximum length of time for which a client can hold and renew a lock aquisition
private final long maxLeaseTime;
// Maximum length of time a lock may be held without updating the lease,
// after that time any attempt to lock it will succeed
private final long renewalLeaseTime;
}
public enum RentrancyLevel {
NODE, // Node can lock if it owns the lock without blocking, only the owner node can unlock
INSTANCE, // Instance can lock multiple times if it owns the lock without blocking, only the owner instance can unlock
NOT_REENTRANT // Nobody can take the lock if already taken, but everybody can release it
}
{code}
h4. InfinispanLock defineLock(String name, LockConfiguration configuration)
Defines a lock with the specific name and LockConfiguration. It does not overwrite existing configurations.
Returns true if successfully defined or false if the lock is already defined or any other failure. If silentFailover is false, then InfinispanLockException will be raised.
h4. InfinispanLock get(String name)
Get’s a InfinipanLock by it’s name and throws InfinispanLockException if the lock is not not defined. User must call defineLock before this method.
h4. Optional<LockConfiguration> getConfiguration(String name);
Get’s the configuration for a Lock. If the Lock does not exist, Optional.empty() will be returned.
h4. boolean isDefined(String name)
True if the lock exists, false if it doesn’t
h4. CompletableFuture<Boolean> remove(String name)
Removes a Lock from the system. Returns true when it was removed, false when the lock does not exist. If any other Runtime problems appear, InfinispanLockException will be raised withe the reason. As Locks are not removed automatically, so this has to be done programatically when the Lock is no longer needed. Otherwise, OutOfMemoryException could happen.
Remove must be executed when the lock is locked, because running that without exclusive access should result in an exception. Internally, the implementation should contain generation number so that attempts to acquire a lock of a removed generation will result it exceptions in the other callers, too.
h4. CompletableFuture<Void> reset(String name)
Resets the lock to its initial state. If any parties are currently waiting at the lock, they will return with failure on the CompletableFuture
h3. InfinispanLock
When a cluster node holding a Lock dies, this lock is released and available for the others.
{code:java}
public interface InfinispanLock {
CompletableFuture<Void> lock();
CompletableFuture<Boolean> tryLock();
CompletableFuture<Boolean> tryLock(long time, TimeUnit unit);
CompletableFuture<Void> unlock();
}
{code}
h4. CompletableFuture<Void> lock();
CompletableFuture is completed successfully when the lock is acquired When a lock is aquired by a client, it will be automatically released after the maxLeaseTime specified. RenewalLeaseTime is the interval time is the time a client can aquire a lock consecutively User should set the timeouts to non-positive value The initial embedded implementation does not have to support positive values
h4. CompletableFuture<Boolean> tryLock();
Acquires the lock only if it is free at the time of invocation. Acquires the lock if it is available and returns with the value true. If the lock is not available then this method with the value false.
h4. CompletableFuture<Boolean> tryLock(long time, TimeUnit unit);
Acquires the lock if it is free within the given waiting time. If the lock is available this method returns with the value true.
Parameters: time - the maximum time to wait for the lock unit - the time unit of the time argument Returns: true if the lock was acquired and false if the waiting time elapsed before the lock was acquired
CompletableFuture fails with InfinispanLockException in case of error (InterruptedException, or any other non checked exceptions)
h4. CompletableFuture<Boolean> unlock();
If the lock is rentrant (Node or Instance), only the instance or node holding the lock will be able to unlock, otherwise, anybody can unlock and it will behave as a Semaphore with one permit. True answer will say that the operation was succesul and the lock has been released, false the lock has not been relased
h3. Demo
{code:java}
public static void main(String[] args) throws Exception {
EmbeddedCacheManager cm = Infinispan.createClustered();
CounterManager counterManager = EmbeddedCounterManagerFactory.asCounterManager(cm);
counterManager.defineCounter("counter", ...);
WeakCounter counter = counterManager.weakCounter("counter");
ClusteredLockManager lockManager = EmbeddedLockManagerFactory.asClusteredLockManager(cm);
lockManager.defineLock("lock", ...);
InfinispanLock lock = lockManager.get("lock");
for (int i = 0; i < 100; i++) {
System.out.println("Counter on " + i + " is => " + counter.getValue());
lock.lock()
.thenRun(new CounterExample(counter))
.whenComplete((nil, t) -> lock.unlock());
}
cm.stop();
}
static class CounterExample implements Runnable {
private WeakCounter counter;
public CounterExample(WeakCounter counter) {
this.counter = counter;
}
@Override
public void run() {
counter.increment();
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
e.printStackTrace();
}
counter.decrement();
}
}
{code}
> Clustered Locks Embedded Mode
> -----------------------------
>
> Key: ISPN-8069
> URL: https://issues.jboss.org/browse/ISPN-8069
> Project: Infinispan
> Issue Type: Feature Request
> Reporter: Katia Aresti
> Assignee: Katia Aresti
>
> *h3. ClusteredLockManager and configuration
> {code:java}
> package org.infinispan.lock.api;
> public class ClusteredLockManager {
> boolean defineLock(String name, LockConfiguration configuration);
> InfinispanLock get(String name);
> LockConfiguration getConfiguration(String name);
> boolean isDefined(String name);
> CompletableFuture<Boolean> remove(String name);
> CompletableFuture<Void> reset(String name);
> }
> public class LockConfiguration {
> private final RentrancyLevel lentrancyLevel; // default NOT_REENTRANT
> private final boolean silentFailover; // default true
> // The maximum length of time for which a client can hold and renew a lock aquisition
> private final long maxLeaseTime;
> // Maximum length of time a lock may be held without updating the lease,
> // after that time any attempt to lock it will succeed
> private final long renewalLeaseTime;
> }
> public enum RentrancyLevel {
> NODE, // Node can lock if it owns the lock without blocking, only the owner node can unlock
> INSTANCE, // Instance can lock multiple times if it owns the lock without blocking, only the owner instance can unlock
> NOT_REENTRANT // Nobody can take the lock if already taken, but everybody can release it
> }
> {code}
> *h4. InfinispanLock defineLock(String name, LockConfiguration configuration)
> Defines a lock with the specific name and LockConfiguration. It does not overwrite existing configurations.
> Returns true if successfully defined or false if the lock is already defined or any other failure. If silentFailover is false, then InfinispanLockException will be raised.
> *h4. InfinispanLock get(String name)
> Get’s a InfinipanLock by it’s name and throws InfinispanLockException if the lock is not not defined. User must call defineLock before this method.
> *h4. Optional<LockConfiguration> getConfiguration(String name);
> Get’s the configuration for a Lock. If the Lock does not exist, Optional.empty() will be returned.
> *h4. boolean isDefined(String name)
> True if the lock exists, false if it doesn’t
> *h4. CompletableFuture<Boolean> remove(String name)
> Removes a Lock from the system. Returns true when it was removed, false when the lock does not exist. If any other Runtime problems appear, InfinispanLockException will be raised withe the reason. As Locks are not removed automatically, so this has to be done programatically when the Lock is no longer needed. Otherwise, OutOfMemoryException could happen.
> Remove must be executed when the lock is locked, because running that without exclusive access should result in an exception. Internally, the implementation should contain generation number so that attempts to acquire a lock of a removed generation will result it exceptions in the other callers, too.
> *h4. CompletableFuture<Void> reset(String name)
> Resets the lock to its initial state. If any parties are currently waiting at the lock, they will return with failure on the CompletableFuture
> *h3. InfinispanLock
>
> When a cluster node holding a Lock dies, this lock is released and available for the others.
> {code:java}
> public interface InfinispanLock {
> CompletableFuture<Void> lock();
> CompletableFuture<Boolean> tryLock();
> CompletableFuture<Boolean> tryLock(long time, TimeUnit unit);
> CompletableFuture<Void> unlock();
> }
> {code}
> *h4. CompletableFuture<Void> lock();
> CompletableFuture is completed successfully when the lock is acquired When a lock is aquired by a client, it will be automatically released after the maxLeaseTime specified. RenewalLeaseTime is the interval time is the time a client can aquire a lock consecutively User should set the timeouts to non-positive value The initial embedded implementation does not have to support positive values
> *h4. CompletableFuture<Boolean> tryLock();
> Acquires the lock only if it is free at the time of invocation. Acquires the lock if it is available and returns with the value true. If the lock is not available then this method with the value false.
> *h4. CompletableFuture<Boolean> tryLock(long time, TimeUnit unit);
> Acquires the lock if it is free within the given waiting time. If the lock is available this method returns with the value true.
> Parameters: time - the maximum time to wait for the lock unit - the time unit of the time argument Returns: true if the lock was acquired and false if the waiting time elapsed before the lock was acquired
> CompletableFuture fails with InfinispanLockException in case of error (InterruptedException, or any other non checked exceptions)
> *h4. CompletableFuture<Boolean> unlock();
> If the lock is rentrant (Node or Instance), only the instance or node holding the lock will be able to unlock, otherwise, anybody can unlock and it will behave as a Semaphore with one permit. True answer will say that the operation was succesul and the lock has been released, false the lock has not been relased
> *h3. Demo
> {code:java}
> public static void main(String[] args) throws Exception {
> EmbeddedCacheManager cm = Infinispan.createClustered();
> CounterManager counterManager = EmbeddedCounterManagerFactory.asCounterManager(cm);
> counterManager.defineCounter("counter", ...);
> WeakCounter counter = counterManager.weakCounter("counter");
> ClusteredLockManager lockManager = EmbeddedLockManagerFactory.asClusteredLockManager(cm);
> lockManager.defineLock("lock", ...);
> InfinispanLock lock = lockManager.get("lock");
> for (int i = 0; i < 100; i++) {
> System.out.println("Counter on " + i + " is => " + counter.getValue());
> lock.lock()
> .thenRun(new CounterExample(counter))
> .whenComplete((nil, t) -> lock.unlock());
> }
> cm.stop();
> }
> static class CounterExample implements Runnable {
> private WeakCounter counter;
> public CounterExample(WeakCounter counter) {
> this.counter = counter;
> }
> @Override
> public void run() {
> counter.increment();
> try {
> Thread.sleep(1000);
> } catch (InterruptedException e) {
> e.printStackTrace();
> }
> counter.decrement();
> }
> }
> {code}
--
This message was sent by Atlassian JIRA
(v7.2.3#72005)
7 years, 6 months
[JBoss JIRA] (ISPN-8069) Clustered Locks Embedded Mode
by Katia Aresti (JIRA)
[ https://issues.jboss.org/browse/ISPN-8069?page=com.atlassian.jira.plugin.... ]
Katia Aresti updated ISPN-8069:
-------------------------------
Description:
h3. *ClusteredLockManager and configuration
{code:java}
package org.infinispan.lock.api;
public class ClusteredLockManager {
boolean defineLock(String name, LockConfiguration configuration);
InfinispanLock get(String name);
LockConfiguration getConfiguration(String name);
boolean isDefined(String name);
CompletableFuture<Boolean> remove(String name);
CompletableFuture<Void> reset(String name);
}
public class LockConfiguration {
private final RentrancyLevel lentrancyLevel; // default NOT_REENTRANT
private final boolean silentFailover; // default true
// The maximum length of time for which a client can hold and renew a lock aquisition
private final long maxLeaseTime;
// Maximum length of time a lock may be held without updating the lease,
// after that time any attempt to lock it will succeed
private final long renewalLeaseTime;
}
public enum RentrancyLevel {
NODE, // Node can lock if it owns the lock without blocking, only the owner node can unlock
INSTANCE, // Instance can lock multiple times if it owns the lock without blocking, only the owner instance can unlock
NOT_REENTRANT // Nobody can take the lock if already taken, but everybody can release it
}
{code}
*h4. InfinispanLock defineLock(String name, LockConfiguration configuration)
Defines a lock with the specific name and LockConfiguration. It does not overwrite existing configurations.
Returns true if successfully defined or false if the lock is already defined or any other failure. If silentFailover is false, then InfinispanLockException will be raised.
*h4. InfinispanLock get(String name)
Get’s a InfinipanLock by it’s name and throws InfinispanLockException if the lock is not not defined. User must call defineLock before this method.
*h4. Optional<LockConfiguration> getConfiguration(String name);
Get’s the configuration for a Lock. If the Lock does not exist, Optional.empty() will be returned.
*h4. boolean isDefined(String name)
True if the lock exists, false if it doesn’t
*h4. CompletableFuture<Boolean> remove(String name)
Removes a Lock from the system. Returns true when it was removed, false when the lock does not exist. If any other Runtime problems appear, InfinispanLockException will be raised withe the reason. As Locks are not removed automatically, so this has to be done programatically when the Lock is no longer needed. Otherwise, OutOfMemoryException could happen.
Remove must be executed when the lock is locked, because running that without exclusive access should result in an exception. Internally, the implementation should contain generation number so that attempts to acquire a lock of a removed generation will result it exceptions in the other callers, too.
*h4. CompletableFuture<Void> reset(String name)
Resets the lock to its initial state. If any parties are currently waiting at the lock, they will return with failure on the CompletableFuture
*h3. InfinispanLock
When a cluster node holding a Lock dies, this lock is released and available for the others.
{code:java}
public interface InfinispanLock {
CompletableFuture<Void> lock();
CompletableFuture<Boolean> tryLock();
CompletableFuture<Boolean> tryLock(long time, TimeUnit unit);
CompletableFuture<Void> unlock();
}
{code}
*h4. CompletableFuture<Void> lock();
CompletableFuture is completed successfully when the lock is acquired When a lock is aquired by a client, it will be automatically released after the maxLeaseTime specified. RenewalLeaseTime is the interval time is the time a client can aquire a lock consecutively User should set the timeouts to non-positive value The initial embedded implementation does not have to support positive values
*h4. CompletableFuture<Boolean> tryLock();
Acquires the lock only if it is free at the time of invocation. Acquires the lock if it is available and returns with the value true. If the lock is not available then this method with the value false.
*h4. CompletableFuture<Boolean> tryLock(long time, TimeUnit unit);
Acquires the lock if it is free within the given waiting time. If the lock is available this method returns with the value true.
Parameters: time - the maximum time to wait for the lock unit - the time unit of the time argument Returns: true if the lock was acquired and false if the waiting time elapsed before the lock was acquired
CompletableFuture fails with InfinispanLockException in case of error (InterruptedException, or any other non checked exceptions)
*h4. CompletableFuture<Boolean> unlock();
If the lock is rentrant (Node or Instance), only the instance or node holding the lock will be able to unlock, otherwise, anybody can unlock and it will behave as a Semaphore with one permit. True answer will say that the operation was succesul and the lock has been released, false the lock has not been relased
*h3. Demo
{code:java}
public static void main(String[] args) throws Exception {
EmbeddedCacheManager cm = Infinispan.createClustered();
CounterManager counterManager = EmbeddedCounterManagerFactory.asCounterManager(cm);
counterManager.defineCounter("counter", ...);
WeakCounter counter = counterManager.weakCounter("counter");
ClusteredLockManager lockManager = EmbeddedLockManagerFactory.asClusteredLockManager(cm);
lockManager.defineLock("lock", ...);
InfinispanLock lock = lockManager.get("lock");
for (int i = 0; i < 100; i++) {
System.out.println("Counter on " + i + " is => " + counter.getValue());
lock.lock()
.thenRun(new CounterExample(counter))
.whenComplete((nil, t) -> lock.unlock());
}
cm.stop();
}
static class CounterExample implements Runnable {
private WeakCounter counter;
public CounterExample(WeakCounter counter) {
this.counter = counter;
}
@Override
public void run() {
counter.increment();
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
e.printStackTrace();
}
counter.decrement();
}
}
{code}
was:
*h3. ClusteredLockManager and configuration
{code:java}
package org.infinispan.lock.api;
public class ClusteredLockManager {
boolean defineLock(String name, LockConfiguration configuration);
InfinispanLock get(String name);
LockConfiguration getConfiguration(String name);
boolean isDefined(String name);
CompletableFuture<Boolean> remove(String name);
CompletableFuture<Void> reset(String name);
}
public class LockConfiguration {
private final RentrancyLevel lentrancyLevel; // default NOT_REENTRANT
private final boolean silentFailover; // default true
// The maximum length of time for which a client can hold and renew a lock aquisition
private final long maxLeaseTime;
// Maximum length of time a lock may be held without updating the lease,
// after that time any attempt to lock it will succeed
private final long renewalLeaseTime;
}
public enum RentrancyLevel {
NODE, // Node can lock if it owns the lock without blocking, only the owner node can unlock
INSTANCE, // Instance can lock multiple times if it owns the lock without blocking, only the owner instance can unlock
NOT_REENTRANT // Nobody can take the lock if already taken, but everybody can release it
}
{code}
*h4. InfinispanLock defineLock(String name, LockConfiguration configuration)
Defines a lock with the specific name and LockConfiguration. It does not overwrite existing configurations.
Returns true if successfully defined or false if the lock is already defined or any other failure. If silentFailover is false, then InfinispanLockException will be raised.
*h4. InfinispanLock get(String name)
Get’s a InfinipanLock by it’s name and throws InfinispanLockException if the lock is not not defined. User must call defineLock before this method.
*h4. Optional<LockConfiguration> getConfiguration(String name);
Get’s the configuration for a Lock. If the Lock does not exist, Optional.empty() will be returned.
*h4. boolean isDefined(String name)
True if the lock exists, false if it doesn’t
*h4. CompletableFuture<Boolean> remove(String name)
Removes a Lock from the system. Returns true when it was removed, false when the lock does not exist. If any other Runtime problems appear, InfinispanLockException will be raised withe the reason. As Locks are not removed automatically, so this has to be done programatically when the Lock is no longer needed. Otherwise, OutOfMemoryException could happen.
Remove must be executed when the lock is locked, because running that without exclusive access should result in an exception. Internally, the implementation should contain generation number so that attempts to acquire a lock of a removed generation will result it exceptions in the other callers, too.
*h4. CompletableFuture<Void> reset(String name)
Resets the lock to its initial state. If any parties are currently waiting at the lock, they will return with failure on the CompletableFuture
*h3. InfinispanLock
When a cluster node holding a Lock dies, this lock is released and available for the others.
{code:java}
public interface InfinispanLock {
CompletableFuture<Void> lock();
CompletableFuture<Boolean> tryLock();
CompletableFuture<Boolean> tryLock(long time, TimeUnit unit);
CompletableFuture<Void> unlock();
}
{code}
*h4. CompletableFuture<Void> lock();
CompletableFuture is completed successfully when the lock is acquired When a lock is aquired by a client, it will be automatically released after the maxLeaseTime specified. RenewalLeaseTime is the interval time is the time a client can aquire a lock consecutively User should set the timeouts to non-positive value The initial embedded implementation does not have to support positive values
*h4. CompletableFuture<Boolean> tryLock();
Acquires the lock only if it is free at the time of invocation. Acquires the lock if it is available and returns with the value true. If the lock is not available then this method with the value false.
*h4. CompletableFuture<Boolean> tryLock(long time, TimeUnit unit);
Acquires the lock if it is free within the given waiting time. If the lock is available this method returns with the value true.
Parameters: time - the maximum time to wait for the lock unit - the time unit of the time argument Returns: true if the lock was acquired and false if the waiting time elapsed before the lock was acquired
CompletableFuture fails with InfinispanLockException in case of error (InterruptedException, or any other non checked exceptions)
*h4. CompletableFuture<Boolean> unlock();
If the lock is rentrant (Node or Instance), only the instance or node holding the lock will be able to unlock, otherwise, anybody can unlock and it will behave as a Semaphore with one permit. True answer will say that the operation was succesul and the lock has been released, false the lock has not been relased
*h3. Demo
{code:java}
public static void main(String[] args) throws Exception {
EmbeddedCacheManager cm = Infinispan.createClustered();
CounterManager counterManager = EmbeddedCounterManagerFactory.asCounterManager(cm);
counterManager.defineCounter("counter", ...);
WeakCounter counter = counterManager.weakCounter("counter");
ClusteredLockManager lockManager = EmbeddedLockManagerFactory.asClusteredLockManager(cm);
lockManager.defineLock("lock", ...);
InfinispanLock lock = lockManager.get("lock");
for (int i = 0; i < 100; i++) {
System.out.println("Counter on " + i + " is => " + counter.getValue());
lock.lock()
.thenRun(new CounterExample(counter))
.whenComplete((nil, t) -> lock.unlock());
}
cm.stop();
}
static class CounterExample implements Runnable {
private WeakCounter counter;
public CounterExample(WeakCounter counter) {
this.counter = counter;
}
@Override
public void run() {
counter.increment();
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
e.printStackTrace();
}
counter.decrement();
}
}
{code}
> Clustered Locks Embedded Mode
> -----------------------------
>
> Key: ISPN-8069
> URL: https://issues.jboss.org/browse/ISPN-8069
> Project: Infinispan
> Issue Type: Feature Request
> Reporter: Katia Aresti
> Assignee: Katia Aresti
>
> h3. *ClusteredLockManager and configuration
> {code:java}
> package org.infinispan.lock.api;
> public class ClusteredLockManager {
> boolean defineLock(String name, LockConfiguration configuration);
> InfinispanLock get(String name);
> LockConfiguration getConfiguration(String name);
> boolean isDefined(String name);
> CompletableFuture<Boolean> remove(String name);
> CompletableFuture<Void> reset(String name);
> }
> public class LockConfiguration {
> private final RentrancyLevel lentrancyLevel; // default NOT_REENTRANT
> private final boolean silentFailover; // default true
> // The maximum length of time for which a client can hold and renew a lock aquisition
> private final long maxLeaseTime;
> // Maximum length of time a lock may be held without updating the lease,
> // after that time any attempt to lock it will succeed
> private final long renewalLeaseTime;
> }
> public enum RentrancyLevel {
> NODE, // Node can lock if it owns the lock without blocking, only the owner node can unlock
> INSTANCE, // Instance can lock multiple times if it owns the lock without blocking, only the owner instance can unlock
> NOT_REENTRANT // Nobody can take the lock if already taken, but everybody can release it
> }
> {code}
> *h4. InfinispanLock defineLock(String name, LockConfiguration configuration)
> Defines a lock with the specific name and LockConfiguration. It does not overwrite existing configurations.
> Returns true if successfully defined or false if the lock is already defined or any other failure. If silentFailover is false, then InfinispanLockException will be raised.
> *h4. InfinispanLock get(String name)
> Get’s a InfinipanLock by it’s name and throws InfinispanLockException if the lock is not not defined. User must call defineLock before this method.
> *h4. Optional<LockConfiguration> getConfiguration(String name);
> Get’s the configuration for a Lock. If the Lock does not exist, Optional.empty() will be returned.
> *h4. boolean isDefined(String name)
> True if the lock exists, false if it doesn’t
> *h4. CompletableFuture<Boolean> remove(String name)
> Removes a Lock from the system. Returns true when it was removed, false when the lock does not exist. If any other Runtime problems appear, InfinispanLockException will be raised withe the reason. As Locks are not removed automatically, so this has to be done programatically when the Lock is no longer needed. Otherwise, OutOfMemoryException could happen.
> Remove must be executed when the lock is locked, because running that without exclusive access should result in an exception. Internally, the implementation should contain generation number so that attempts to acquire a lock of a removed generation will result it exceptions in the other callers, too.
> *h4. CompletableFuture<Void> reset(String name)
> Resets the lock to its initial state. If any parties are currently waiting at the lock, they will return with failure on the CompletableFuture
> *h3. InfinispanLock
>
> When a cluster node holding a Lock dies, this lock is released and available for the others.
> {code:java}
> public interface InfinispanLock {
> CompletableFuture<Void> lock();
> CompletableFuture<Boolean> tryLock();
> CompletableFuture<Boolean> tryLock(long time, TimeUnit unit);
> CompletableFuture<Void> unlock();
> }
> {code}
> *h4. CompletableFuture<Void> lock();
> CompletableFuture is completed successfully when the lock is acquired When a lock is aquired by a client, it will be automatically released after the maxLeaseTime specified. RenewalLeaseTime is the interval time is the time a client can aquire a lock consecutively User should set the timeouts to non-positive value The initial embedded implementation does not have to support positive values
> *h4. CompletableFuture<Boolean> tryLock();
> Acquires the lock only if it is free at the time of invocation. Acquires the lock if it is available and returns with the value true. If the lock is not available then this method with the value false.
> *h4. CompletableFuture<Boolean> tryLock(long time, TimeUnit unit);
> Acquires the lock if it is free within the given waiting time. If the lock is available this method returns with the value true.
> Parameters: time - the maximum time to wait for the lock unit - the time unit of the time argument Returns: true if the lock was acquired and false if the waiting time elapsed before the lock was acquired
> CompletableFuture fails with InfinispanLockException in case of error (InterruptedException, or any other non checked exceptions)
> *h4. CompletableFuture<Boolean> unlock();
> If the lock is rentrant (Node or Instance), only the instance or node holding the lock will be able to unlock, otherwise, anybody can unlock and it will behave as a Semaphore with one permit. True answer will say that the operation was succesul and the lock has been released, false the lock has not been relased
> *h3. Demo
> {code:java}
> public static void main(String[] args) throws Exception {
> EmbeddedCacheManager cm = Infinispan.createClustered();
> CounterManager counterManager = EmbeddedCounterManagerFactory.asCounterManager(cm);
> counterManager.defineCounter("counter", ...);
> WeakCounter counter = counterManager.weakCounter("counter");
> ClusteredLockManager lockManager = EmbeddedLockManagerFactory.asClusteredLockManager(cm);
> lockManager.defineLock("lock", ...);
> InfinispanLock lock = lockManager.get("lock");
> for (int i = 0; i < 100; i++) {
> System.out.println("Counter on " + i + " is => " + counter.getValue());
> lock.lock()
> .thenRun(new CounterExample(counter))
> .whenComplete((nil, t) -> lock.unlock());
> }
> cm.stop();
> }
> static class CounterExample implements Runnable {
> private WeakCounter counter;
> public CounterExample(WeakCounter counter) {
> this.counter = counter;
> }
> @Override
> public void run() {
> counter.increment();
> try {
> Thread.sleep(1000);
> } catch (InterruptedException e) {
> e.printStackTrace();
> }
> counter.decrement();
> }
> }
> {code}
--
This message was sent by Atlassian JIRA
(v7.2.3#72005)
7 years, 6 months
[JBoss JIRA] (ISPN-8069) Clustered Locks Embedded Mode
by Katia Aresti (JIRA)
[ https://issues.jboss.org/browse/ISPN-8069?page=com.atlassian.jira.plugin.... ]
Katia Aresti updated ISPN-8069:
-------------------------------
Description:
h3. ClusteredLockManager and configuration
{code:java}
package org.infinispan.lock.api;
public class ClusteredLockManager {
boolean defineLock(String name, LockConfiguration configuration);
InfinispanLock get(String name);
LockConfiguration getConfiguration(String name);
boolean isDefined(String name);
CompletableFuture<Boolean> remove(String name);
CompletableFuture<Void> reset(String name);
}
public class LockConfiguration {
private final RentrancyLevel lentrancyLevel; // default NOT_REENTRANT
private final boolean silentFailover; // default true
// The maximum length of time for which a client can hold and renew a lock aquisition
private final long maxLeaseTime;
// Maximum length of time a lock may be held without updating the lease,
// after that time any attempt to lock it will succeed
private final long renewalLeaseTime;
}
public enum RentrancyLevel {
NODE, // Node can lock if it owns the lock without blocking, only the owner node can unlock
INSTANCE, // Instance can lock multiple times if it owns the lock without blocking, only the owner instance can unlock
NOT_REENTRANT // Nobody can take the lock if already taken, but everybody can release it
}
{code}
h4. InfinispanLock defineLock(String name, LockConfiguration configuration)
Defines a lock with the specific name and LockConfiguration. It does not overwrite existing configurations.
Returns true if successfully defined or false if the lock is already defined or any other failure. If silentFailover is false, then InfinispanLockException will be raised.
h4. InfinispanLock get(String name)
Get’s a InfinipanLock by it’s name and throws InfinispanLockException if the lock is not not defined. User must call defineLock before this method.
h4. Optional<LockConfiguration> getConfiguration(String name);
Get’s the configuration for a Lock. If the Lock does not exist, Optional.empty() will be returned.
h4. boolean isDefined(String name)
True if the lock exists, false if it doesn’t
h4. CompletableFuture<Boolean> remove(String name)
Removes a Lock from the system. Returns true when it was removed, false when the lock does not exist. If any other Runtime problems appear, InfinispanLockException will be raised withe the reason. As Locks are not removed automatically, so this has to be done programatically when the Lock is no longer needed. Otherwise, OutOfMemoryException could happen.
Remove must be executed when the lock is locked, because running that without exclusive access should result in an exception. Internally, the implementation should contain generation number so that attempts to acquire a lock of a removed generation will result it exceptions in the other callers, too.
h4. CompletableFuture<Void> reset(String name)
Resets the lock to its initial state. If any parties are currently waiting at the lock, they will return with failure on the CompletableFuture
h3. InfinispanLock
When a cluster node holding a Lock dies, this lock is released and available for the others.
{code:java}
public interface InfinispanLock {
CompletableFuture<Void> lock();
CompletableFuture<Boolean> tryLock();
CompletableFuture<Boolean> tryLock(long time, TimeUnit unit);
CompletableFuture<Void> unlock();
}
{code}
h4. CompletableFuture<Void> lock();
CompletableFuture is completed successfully when the lock is acquired When a lock is aquired by a client, it will be automatically released after the maxLeaseTime specified. RenewalLeaseTime is the interval time is the time a client can aquire a lock consecutively User should set the timeouts to non-positive value The initial embedded implementation does not have to support positive values
h4. CompletableFuture<Boolean> tryLock();
Acquires the lock only if it is free at the time of invocation. Acquires the lock if it is available and returns with the value true. If the lock is not available then this method with the value false.
h4. CompletableFuture<Boolean> tryLock(long time, TimeUnit unit);
Acquires the lock if it is free within the given waiting time. If the lock is available this method returns with the value true.
Parameters: time - the maximum time to wait for the lock unit - the time unit of the time argument Returns: true if the lock was acquired and false if the waiting time elapsed before the lock was acquired
CompletableFuture fails with InfinispanLockException in case of error (InterruptedException, or any other non checked exceptions)
h4. CompletableFuture<Boolean> unlock();
If the lock is rentrant (Node or Instance), only the instance or node holding the lock will be able to unlock, otherwise, anybody can unlock and it will behave as a Semaphore with one permit. True answer will say that the operation was succesul and the lock has been released, false the lock has not been relased
h3. Demo
This main is an example on calling the API as if it was blocking, the corrent implementation should be done
{code:java}
public static void main(String[] args) throws Exception {
EmbeddedCacheManager cm = Infinispan.createClustered();
CounterManager counterManager = EmbeddedCounterManagerFactory.asCounterManager(cm);
counterManager.defineCounter("counter", ...);
WeakCounter counter = counterManager.weakCounter("counter");
ClusteredLockManager lockManager = EmbeddedLockManagerFactory.asClusteredLockManager(cm);
lockManager.defineLock("lock", ...);
InfinispanLock lock = lockManager.get("lock");
for (int i = 0; i < 100; i++) {
System.out.println("Counter on " + i + " is => " + counter.getValue());
lock.lock()
.thenRun(new CounterExample(counter))
.whenComplete((nil, t) -> lock.unlock());
}
cm.stop();
}
static class CounterExample implements Runnable {
private WeakCounter counter;
public CounterExample(WeakCounter counter) {
this.counter = counter;
}
@Override
public void run() {
counter.increment();
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
e.printStackTrace();
}
counter.decrement();
}
}
{code}
was:
h3. ClusteredLockManager and configuration
{code:java}
package org.infinispan.lock.api;
public class ClusteredLockManager {
boolean defineLock(String name, LockConfiguration configuration);
InfinispanLock get(String name);
LockConfiguration getConfiguration(String name);
boolean isDefined(String name);
CompletableFuture<Boolean> remove(String name);
CompletableFuture<Void> reset(String name);
}
public class LockConfiguration {
private final RentrancyLevel lentrancyLevel; // default NOT_REENTRANT
private final boolean silentFailover; // default true
// The maximum length of time for which a client can hold and renew a lock aquisition
private final long maxLeaseTime;
// Maximum length of time a lock may be held without updating the lease,
// after that time any attempt to lock it will succeed
private final long renewalLeaseTime;
}
public enum RentrancyLevel {
NODE, // Node can lock if it owns the lock without blocking, only the owner node can unlock
INSTANCE, // Instance can lock multiple times if it owns the lock without blocking, only the owner instance can unlock
NOT_REENTRANT // Nobody can take the lock if already taken, but everybody can release it
}
{code}
@NODE : when a node owns the lock,
InfinispanLock defineLock(String name, LockConfiguration configuration)
Defines a lock with the specific name and LockConfiguration. It does not overwrite existing configurations.
Returns true if successfully defined or false if the lock is already defined or any other failure. If silentFailover is false, then InfinispanLockException will be raised.
InfinispanLock get(String name)
Get’s a InfinipanLock by it’s name and throws InfinispanLockException if the lock is not not defined. User must call defineLock before this method.
Optional<LockConfiguration> getConfiguration(String name);
Get’s the configuration for a Lock. If the Lock does not exist, Optional.empty() will be returned.
boolean isDefined(String name)
True if the lock exists, false if it doesn’t
CompletableFuture<Boolean> remove(String name)
Removes a Lock from the system. Returns true when it was removed, false when the lock does not exist. If any other Runtime problems appear, InfinispanLockException will be raised withe the reason. As Locks are not removed automatically, so this has to be done programatically when the Lock is no longer needed. Otherwise, OutOfMemoryException could happen.
Remove must be executed when the lock is locked, because running that without exclusive access should result in an exception. Internally, the implementation should contain generation number so that attempts to acquire a lock of a removed generation will result it exceptions in the other callers, too.
CompletableFuture<Void> reset(String name)
Resets the lock to its initial state. If any parties are currently waiting at the lock, they will return with failure on the CompletableFuture
h4. InfinispanLock
h4.
When a cluster node holding a Lock dies, this lock is released and available for the others.
public interface InfinispanLock {
CompletableFuture<Void> lock();
CompletableFuture<Boolean> tryLock();
CompletableFuture<Boolean> tryLock(long time, TimeUnit unit);
CompletableFuture<Void> unlock();
}
CompletableFuture<Void> lock();
CompletableFuture is completed successfully when the lock is acquired When a lock is aquired by a client, it will be automatically released after the maxLeaseTime specified. RenewalLeaseTime is the interval time is the time a client can aquire a lock consecutively User should set the timeouts to non-positive value The initial embedded implementation does not have to support positive values
CompletableFuture<Boolean> tryLock();
Acquires the lock only if it is free at the time of invocation. Acquires the lock if it is available and returns with the value true. If the lock is not available then this method with the value false.
CompletableFuture<Boolean> tryLock(long time, TimeUnit unit);
Acquires the lock if it is free within the given waiting time. If the lock is available this method returns with the value true.
Parameters: time - the maximum time to wait for the lock unit - the time unit of the time argument Returns: true if the lock was acquired and false if the waiting time elapsed before the lock was acquired
CompletableFuture fails with InfinispanLockException in case of error (InterruptedException, or any other non checked exceptions)
CompletableFuture<Boolean> unlock();
If the lock is rentrant (Node or Instance), only the instance or node holding the lock will be able to unlock, otherwise, anybody can unlock and it will behave as a Semaphore with one permit. True answer will say that the operation was succesul and the lock has been released, false the lock has not been relased
Demo
This main is an example on calling the API as if it was blocking, the corrent implementation should be done
public static void main(String[] args) throws Exception {
EmbeddedCacheManager cm = Infinispan.createClustered();
CounterManager counterManager = EmbeddedCounterManagerFactory.asCounterManager(cm);
counterManager.defineCounter("counter", ...);
WeakCounter counter = counterManager.weakCounter("counter");
ClusteredLockManager lockManager = EmbeddedLockManagerFactory.asClusteredLockManager(cm);
lockManager.defineLock("lock", ...);
InfinispanLock lock = lockManager.get("lock");
for (int i = 0; i < 100; i++) {
System.out.println("Counter on " + i + " is => " + counter.getValue());
lock.lock()
.thenRun(new CounterExample(counter))
.whenComplete((nil, t) -> lock.unlock());
}
cm.stop();
}
static class CounterExample implements Runnable {
private WeakCounter counter;
public CounterExample(WeakCounter counter) {
this.counter = counter;
}
@Override
public void run() {
counter.increment();
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
e.printStackTrace();
}
counter.decrement();
}
}
> Clustered Locks Embedded Mode
> -----------------------------
>
> Key: ISPN-8069
> URL: https://issues.jboss.org/browse/ISPN-8069
> Project: Infinispan
> Issue Type: Feature Request
> Reporter: Katia Aresti
> Assignee: Katia Aresti
>
> h3. ClusteredLockManager and configuration
> {code:java}
> package org.infinispan.lock.api;
> public class ClusteredLockManager {
> boolean defineLock(String name, LockConfiguration configuration);
> InfinispanLock get(String name);
> LockConfiguration getConfiguration(String name);
> boolean isDefined(String name);
> CompletableFuture<Boolean> remove(String name);
> CompletableFuture<Void> reset(String name);
> }
> public class LockConfiguration {
> private final RentrancyLevel lentrancyLevel; // default NOT_REENTRANT
> private final boolean silentFailover; // default true
> // The maximum length of time for which a client can hold and renew a lock aquisition
> private final long maxLeaseTime;
> // Maximum length of time a lock may be held without updating the lease,
> // after that time any attempt to lock it will succeed
> private final long renewalLeaseTime;
> }
> public enum RentrancyLevel {
> NODE, // Node can lock if it owns the lock without blocking, only the owner node can unlock
> INSTANCE, // Instance can lock multiple times if it owns the lock without blocking, only the owner instance can unlock
> NOT_REENTRANT // Nobody can take the lock if already taken, but everybody can release it
> }
> {code}
> h4. InfinispanLock defineLock(String name, LockConfiguration configuration)
> Defines a lock with the specific name and LockConfiguration. It does not overwrite existing configurations.
> Returns true if successfully defined or false if the lock is already defined or any other failure. If silentFailover is false, then InfinispanLockException will be raised.
> h4. InfinispanLock get(String name)
> Get’s a InfinipanLock by it’s name and throws InfinispanLockException if the lock is not not defined. User must call defineLock before this method.
> h4. Optional<LockConfiguration> getConfiguration(String name);
> Get’s the configuration for a Lock. If the Lock does not exist, Optional.empty() will be returned.
> h4. boolean isDefined(String name)
> True if the lock exists, false if it doesn’t
> h4. CompletableFuture<Boolean> remove(String name)
> Removes a Lock from the system. Returns true when it was removed, false when the lock does not exist. If any other Runtime problems appear, InfinispanLockException will be raised withe the reason. As Locks are not removed automatically, so this has to be done programatically when the Lock is no longer needed. Otherwise, OutOfMemoryException could happen.
> Remove must be executed when the lock is locked, because running that without exclusive access should result in an exception. Internally, the implementation should contain generation number so that attempts to acquire a lock of a removed generation will result it exceptions in the other callers, too.
> h4. CompletableFuture<Void> reset(String name)
> Resets the lock to its initial state. If any parties are currently waiting at the lock, they will return with failure on the CompletableFuture
> h3. InfinispanLock
>
> When a cluster node holding a Lock dies, this lock is released and available for the others.
> {code:java}
> public interface InfinispanLock {
> CompletableFuture<Void> lock();
> CompletableFuture<Boolean> tryLock();
> CompletableFuture<Boolean> tryLock(long time, TimeUnit unit);
> CompletableFuture<Void> unlock();
> }
> {code}
> h4. CompletableFuture<Void> lock();
> CompletableFuture is completed successfully when the lock is acquired When a lock is aquired by a client, it will be automatically released after the maxLeaseTime specified. RenewalLeaseTime is the interval time is the time a client can aquire a lock consecutively User should set the timeouts to non-positive value The initial embedded implementation does not have to support positive values
> h4. CompletableFuture<Boolean> tryLock();
> Acquires the lock only if it is free at the time of invocation. Acquires the lock if it is available and returns with the value true. If the lock is not available then this method with the value false.
> h4. CompletableFuture<Boolean> tryLock(long time, TimeUnit unit);
> Acquires the lock if it is free within the given waiting time. If the lock is available this method returns with the value true.
> Parameters: time - the maximum time to wait for the lock unit - the time unit of the time argument Returns: true if the lock was acquired and false if the waiting time elapsed before the lock was acquired
> CompletableFuture fails with InfinispanLockException in case of error (InterruptedException, or any other non checked exceptions)
> h4. CompletableFuture<Boolean> unlock();
> If the lock is rentrant (Node or Instance), only the instance or node holding the lock will be able to unlock, otherwise, anybody can unlock and it will behave as a Semaphore with one permit. True answer will say that the operation was succesul and the lock has been released, false the lock has not been relased
> h3. Demo
> This main is an example on calling the API as if it was blocking, the corrent implementation should be done
> {code:java}
> public static void main(String[] args) throws Exception {
> EmbeddedCacheManager cm = Infinispan.createClustered();
> CounterManager counterManager = EmbeddedCounterManagerFactory.asCounterManager(cm);
> counterManager.defineCounter("counter", ...);
> WeakCounter counter = counterManager.weakCounter("counter");
> ClusteredLockManager lockManager = EmbeddedLockManagerFactory.asClusteredLockManager(cm);
> lockManager.defineLock("lock", ...);
> InfinispanLock lock = lockManager.get("lock");
> for (int i = 0; i < 100; i++) {
> System.out.println("Counter on " + i + " is => " + counter.getValue());
> lock.lock()
> .thenRun(new CounterExample(counter))
> .whenComplete((nil, t) -> lock.unlock());
> }
> cm.stop();
> }
> static class CounterExample implements Runnable {
> private WeakCounter counter;
> public CounterExample(WeakCounter counter) {
> this.counter = counter;
> }
> @Override
> public void run() {
> counter.increment();
> try {
> Thread.sleep(1000);
> } catch (InterruptedException e) {
> e.printStackTrace();
> }
> counter.decrement();
> }
> }
> {code}
--
This message was sent by Atlassian JIRA
(v7.2.3#72005)
7 years, 6 months
[JBoss JIRA] (ISPN-8069) Clustered Locks Embedded Mode
by Katia Aresti (JIRA)
[ https://issues.jboss.org/browse/ISPN-8069?page=com.atlassian.jira.plugin.... ]
Katia Aresti updated ISPN-8069:
-------------------------------
Description:
h3. ClusteredLockManager and configuration
{code:java}
package org.infinispan.lock.api;
public class ClusteredLockManager {
boolean defineLock(String name, LockConfiguration configuration);
InfinispanLock get(String name);
LockConfiguration getConfiguration(String name);
boolean isDefined(String name);
CompletableFuture<Boolean> remove(String name);
CompletableFuture<Void> reset(String name);
}
public class LockConfiguration {
private final RentrancyLevel lentrancyLevel; // default NOT_REENTRANT
private final boolean silentFailover; // default true
// The maximum length of time for which a client can hold and renew a lock aquisition
private final long maxLeaseTime;
// Maximum length of time a lock may be held without updating the lease,
// after that time any attempt to lock it will succeed
private final long renewalLeaseTime;
}
public enum RentrancyLevel {
NODE, // Node can lock if it owns the lock without blocking, only the owner node can unlock
INSTANCE, // Instance can lock multiple times if it owns the lock without blocking, only the owner instance can unlock
NOT_REENTRANT // Nobody can take the lock if already taken, but everybody can release it
}
{code}
h4. InfinispanLock defineLock(String name, LockConfiguration configuration)
Defines a lock with the specific name and LockConfiguration. It does not overwrite existing configurations.
Returns true if successfully defined or false if the lock is already defined or any other failure. If silentFailover is false, then InfinispanLockException will be raised.
h4. InfinispanLock get(String name)
Get’s a InfinipanLock by it’s name and throws InfinispanLockException if the lock is not not defined. User must call defineLock before this method.
h4. Optional<LockConfiguration> getConfiguration(String name);
Get’s the configuration for a Lock. If the Lock does not exist, Optional.empty() will be returned.
h4. boolean isDefined(String name)
True if the lock exists, false if it doesn’t
h4. CompletableFuture<Boolean> remove(String name)
Removes a Lock from the system. Returns true when it was removed, false when the lock does not exist. If any other Runtime problems appear, InfinispanLockException will be raised withe the reason. As Locks are not removed automatically, so this has to be done programatically when the Lock is no longer needed. Otherwise, OutOfMemoryException could happen.
Remove must be executed when the lock is locked, because running that without exclusive access should result in an exception. Internally, the implementation should contain generation number so that attempts to acquire a lock of a removed generation will result it exceptions in the other callers, too.
h4. CompletableFuture<Void> reset(String name)
Resets the lock to its initial state. If any parties are currently waiting at the lock, they will return with failure on the CompletableFuture
h3. InfinispanLock
When a cluster node holding a Lock dies, this lock is released and available for the others.
{code:java}
public interface InfinispanLock {
CompletableFuture<Void> lock();
CompletableFuture<Boolean> tryLock();
CompletableFuture<Boolean> tryLock(long time, TimeUnit unit);
CompletableFuture<Void> unlock();
}
{code}
h4. CompletableFuture<Void> lock();
CompletableFuture is completed successfully when the lock is acquired When a lock is aquired by a client, it will be automatically released after the maxLeaseTime specified. RenewalLeaseTime is the interval time is the time a client can aquire a lock consecutively User should set the timeouts to non-positive value The initial embedded implementation does not have to support positive values
h4. CompletableFuture<Boolean> tryLock();
Acquires the lock only if it is free at the time of invocation. Acquires the lock if it is available and returns with the value true. If the lock is not available then this method with the value false.
h4. CompletableFuture<Boolean> tryLock(long time, TimeUnit unit);
Acquires the lock if it is free within the given waiting time. If the lock is available this method returns with the value true.
Parameters: time - the maximum time to wait for the lock unit - the time unit of the time argument Returns: true if the lock was acquired and false if the waiting time elapsed before the lock was acquired
CompletableFuture fails with InfinispanLockException in case of error (InterruptedException, or any other non checked exceptions)
h4. CompletableFuture<Boolean> unlock();
If the lock is rentrant (Node or Instance), only the instance or node holding the lock will be able to unlock, otherwise, anybody can unlock and it will behave as a Semaphore with one permit. True answer will say that the operation was succesul and the lock has been released, false the lock has not been relased
h3. Demo
{code:java}
public static void main(String[] args) throws Exception {
EmbeddedCacheManager cm = Infinispan.createClustered();
CounterManager counterManager = EmbeddedCounterManagerFactory.asCounterManager(cm);
counterManager.defineCounter("counter", ...);
WeakCounter counter = counterManager.weakCounter("counter");
ClusteredLockManager lockManager = EmbeddedLockManagerFactory.asClusteredLockManager(cm);
lockManager.defineLock("lock", ...);
InfinispanLock lock = lockManager.get("lock");
for (int i = 0; i < 100; i++) {
System.out.println("Counter on " + i + " is => " + counter.getValue());
lock.lock()
.thenRun(new CounterExample(counter))
.whenComplete((nil, t) -> lock.unlock());
}
cm.stop();
}
static class CounterExample implements Runnable {
private WeakCounter counter;
public CounterExample(WeakCounter counter) {
this.counter = counter;
}
@Override
public void run() {
counter.increment();
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
e.printStackTrace();
}
counter.decrement();
}
}
{code}
was:
h3. ClusteredLockManager and configuration
{code:java}
package org.infinispan.lock.api;
public class ClusteredLockManager {
boolean defineLock(String name, LockConfiguration configuration);
InfinispanLock get(String name);
LockConfiguration getConfiguration(String name);
boolean isDefined(String name);
CompletableFuture<Boolean> remove(String name);
CompletableFuture<Void> reset(String name);
}
public class LockConfiguration {
private final RentrancyLevel lentrancyLevel; // default NOT_REENTRANT
private final boolean silentFailover; // default true
// The maximum length of time for which a client can hold and renew a lock aquisition
private final long maxLeaseTime;
// Maximum length of time a lock may be held without updating the lease,
// after that time any attempt to lock it will succeed
private final long renewalLeaseTime;
}
public enum RentrancyLevel {
NODE, // Node can lock if it owns the lock without blocking, only the owner node can unlock
INSTANCE, // Instance can lock multiple times if it owns the lock without blocking, only the owner instance can unlock
NOT_REENTRANT // Nobody can take the lock if already taken, but everybody can release it
}
{code}
h4. InfinispanLock defineLock(String name, LockConfiguration configuration)
Defines a lock with the specific name and LockConfiguration. It does not overwrite existing configurations.
Returns true if successfully defined or false if the lock is already defined or any other failure. If silentFailover is false, then InfinispanLockException will be raised.
h4. InfinispanLock get(String name)
Get’s a InfinipanLock by it’s name and throws InfinispanLockException if the lock is not not defined. User must call defineLock before this method.
h4. Optional<LockConfiguration> getConfiguration(String name);
Get’s the configuration for a Lock. If the Lock does not exist, Optional.empty() will be returned.
h4. boolean isDefined(String name)
True if the lock exists, false if it doesn’t
h4. CompletableFuture<Boolean> remove(String name)
Removes a Lock from the system. Returns true when it was removed, false when the lock does not exist. If any other Runtime problems appear, InfinispanLockException will be raised withe the reason. As Locks are not removed automatically, so this has to be done programatically when the Lock is no longer needed. Otherwise, OutOfMemoryException could happen.
Remove must be executed when the lock is locked, because running that without exclusive access should result in an exception. Internally, the implementation should contain generation number so that attempts to acquire a lock of a removed generation will result it exceptions in the other callers, too.
h4. CompletableFuture<Void> reset(String name)
Resets the lock to its initial state. If any parties are currently waiting at the lock, they will return with failure on the CompletableFuture
h3. InfinispanLock
When a cluster node holding a Lock dies, this lock is released and available for the others.
{code:java}
public interface InfinispanLock {
CompletableFuture<Void> lock();
CompletableFuture<Boolean> tryLock();
CompletableFuture<Boolean> tryLock(long time, TimeUnit unit);
CompletableFuture<Void> unlock();
}
{code}
h4. CompletableFuture<Void> lock();
CompletableFuture is completed successfully when the lock is acquired When a lock is aquired by a client, it will be automatically released after the maxLeaseTime specified. RenewalLeaseTime is the interval time is the time a client can aquire a lock consecutively User should set the timeouts to non-positive value The initial embedded implementation does not have to support positive values
h4. CompletableFuture<Boolean> tryLock();
Acquires the lock only if it is free at the time of invocation. Acquires the lock if it is available and returns with the value true. If the lock is not available then this method with the value false.
h4. CompletableFuture<Boolean> tryLock(long time, TimeUnit unit);
Acquires the lock if it is free within the given waiting time. If the lock is available this method returns with the value true.
Parameters: time - the maximum time to wait for the lock unit - the time unit of the time argument Returns: true if the lock was acquired and false if the waiting time elapsed before the lock was acquired
CompletableFuture fails with InfinispanLockException in case of error (InterruptedException, or any other non checked exceptions)
h4. CompletableFuture<Boolean> unlock();
If the lock is rentrant (Node or Instance), only the instance or node holding the lock will be able to unlock, otherwise, anybody can unlock and it will behave as a Semaphore with one permit. True answer will say that the operation was succesul and the lock has been released, false the lock has not been relased
h3. Demo
This main is an example on calling the API as if it was blocking, the corrent implementation should be done
{code:java}
public static void main(String[] args) throws Exception {
EmbeddedCacheManager cm = Infinispan.createClustered();
CounterManager counterManager = EmbeddedCounterManagerFactory.asCounterManager(cm);
counterManager.defineCounter("counter", ...);
WeakCounter counter = counterManager.weakCounter("counter");
ClusteredLockManager lockManager = EmbeddedLockManagerFactory.asClusteredLockManager(cm);
lockManager.defineLock("lock", ...);
InfinispanLock lock = lockManager.get("lock");
for (int i = 0; i < 100; i++) {
System.out.println("Counter on " + i + " is => " + counter.getValue());
lock.lock()
.thenRun(new CounterExample(counter))
.whenComplete((nil, t) -> lock.unlock());
}
cm.stop();
}
static class CounterExample implements Runnable {
private WeakCounter counter;
public CounterExample(WeakCounter counter) {
this.counter = counter;
}
@Override
public void run() {
counter.increment();
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
e.printStackTrace();
}
counter.decrement();
}
}
{code}
> Clustered Locks Embedded Mode
> -----------------------------
>
> Key: ISPN-8069
> URL: https://issues.jboss.org/browse/ISPN-8069
> Project: Infinispan
> Issue Type: Feature Request
> Reporter: Katia Aresti
> Assignee: Katia Aresti
>
> h3. ClusteredLockManager and configuration
> {code:java}
> package org.infinispan.lock.api;
> public class ClusteredLockManager {
> boolean defineLock(String name, LockConfiguration configuration);
> InfinispanLock get(String name);
> LockConfiguration getConfiguration(String name);
> boolean isDefined(String name);
> CompletableFuture<Boolean> remove(String name);
> CompletableFuture<Void> reset(String name);
> }
> public class LockConfiguration {
> private final RentrancyLevel lentrancyLevel; // default NOT_REENTRANT
> private final boolean silentFailover; // default true
> // The maximum length of time for which a client can hold and renew a lock aquisition
> private final long maxLeaseTime;
> // Maximum length of time a lock may be held without updating the lease,
> // after that time any attempt to lock it will succeed
> private final long renewalLeaseTime;
> }
> public enum RentrancyLevel {
> NODE, // Node can lock if it owns the lock without blocking, only the owner node can unlock
> INSTANCE, // Instance can lock multiple times if it owns the lock without blocking, only the owner instance can unlock
> NOT_REENTRANT // Nobody can take the lock if already taken, but everybody can release it
> }
> {code}
> h4. InfinispanLock defineLock(String name, LockConfiguration configuration)
> Defines a lock with the specific name and LockConfiguration. It does not overwrite existing configurations.
> Returns true if successfully defined or false if the lock is already defined or any other failure. If silentFailover is false, then InfinispanLockException will be raised.
> h4. InfinispanLock get(String name)
> Get’s a InfinipanLock by it’s name and throws InfinispanLockException if the lock is not not defined. User must call defineLock before this method.
> h4. Optional<LockConfiguration> getConfiguration(String name);
> Get’s the configuration for a Lock. If the Lock does not exist, Optional.empty() will be returned.
> h4. boolean isDefined(String name)
> True if the lock exists, false if it doesn’t
> h4. CompletableFuture<Boolean> remove(String name)
> Removes a Lock from the system. Returns true when it was removed, false when the lock does not exist. If any other Runtime problems appear, InfinispanLockException will be raised withe the reason. As Locks are not removed automatically, so this has to be done programatically when the Lock is no longer needed. Otherwise, OutOfMemoryException could happen.
> Remove must be executed when the lock is locked, because running that without exclusive access should result in an exception. Internally, the implementation should contain generation number so that attempts to acquire a lock of a removed generation will result it exceptions in the other callers, too.
> h4. CompletableFuture<Void> reset(String name)
> Resets the lock to its initial state. If any parties are currently waiting at the lock, they will return with failure on the CompletableFuture
> h3. InfinispanLock
>
> When a cluster node holding a Lock dies, this lock is released and available for the others.
> {code:java}
> public interface InfinispanLock {
> CompletableFuture<Void> lock();
> CompletableFuture<Boolean> tryLock();
> CompletableFuture<Boolean> tryLock(long time, TimeUnit unit);
> CompletableFuture<Void> unlock();
> }
> {code}
> h4. CompletableFuture<Void> lock();
> CompletableFuture is completed successfully when the lock is acquired When a lock is aquired by a client, it will be automatically released after the maxLeaseTime specified. RenewalLeaseTime is the interval time is the time a client can aquire a lock consecutively User should set the timeouts to non-positive value The initial embedded implementation does not have to support positive values
> h4. CompletableFuture<Boolean> tryLock();
> Acquires the lock only if it is free at the time of invocation. Acquires the lock if it is available and returns with the value true. If the lock is not available then this method with the value false.
> h4. CompletableFuture<Boolean> tryLock(long time, TimeUnit unit);
> Acquires the lock if it is free within the given waiting time. If the lock is available this method returns with the value true.
> Parameters: time - the maximum time to wait for the lock unit - the time unit of the time argument Returns: true if the lock was acquired and false if the waiting time elapsed before the lock was acquired
> CompletableFuture fails with InfinispanLockException in case of error (InterruptedException, or any other non checked exceptions)
> h4. CompletableFuture<Boolean> unlock();
> If the lock is rentrant (Node or Instance), only the instance or node holding the lock will be able to unlock, otherwise, anybody can unlock and it will behave as a Semaphore with one permit. True answer will say that the operation was succesul and the lock has been released, false the lock has not been relased
> h3. Demo
> {code:java}
> public static void main(String[] args) throws Exception {
> EmbeddedCacheManager cm = Infinispan.createClustered();
> CounterManager counterManager = EmbeddedCounterManagerFactory.asCounterManager(cm);
> counterManager.defineCounter("counter", ...);
> WeakCounter counter = counterManager.weakCounter("counter");
> ClusteredLockManager lockManager = EmbeddedLockManagerFactory.asClusteredLockManager(cm);
> lockManager.defineLock("lock", ...);
> InfinispanLock lock = lockManager.get("lock");
> for (int i = 0; i < 100; i++) {
> System.out.println("Counter on " + i + " is => " + counter.getValue());
> lock.lock()
> .thenRun(new CounterExample(counter))
> .whenComplete((nil, t) -> lock.unlock());
> }
> cm.stop();
> }
> static class CounterExample implements Runnable {
> private WeakCounter counter;
> public CounterExample(WeakCounter counter) {
> this.counter = counter;
> }
> @Override
> public void run() {
> counter.increment();
> try {
> Thread.sleep(1000);
> } catch (InterruptedException e) {
> e.printStackTrace();
> }
> counter.decrement();
> }
> }
> {code}
--
This message was sent by Atlassian JIRA
(v7.2.3#72005)
7 years, 6 months
[JBoss JIRA] (ISPN-8069) Clustered Locks Embedded Mode
by Katia Aresti (JIRA)
[ https://issues.jboss.org/browse/ISPN-8069?page=com.atlassian.jira.plugin.... ]
Katia Aresti updated ISPN-8069:
-------------------------------
Description:
h3. ClusteredLockManager and configuration
{code : java}
package org.infinispan.lock.api;
public class ClusteredLockManager {
boolean defineLock(String name, LockConfiguration configuration);
InfinispanLock get(String name);
LockConfiguration getConfiguration(String name);
boolean isDefined(String name);
CompletableFuture<Boolean> remove(String name);
CompletableFuture<Void> reset(String name);
}
public class LockConfiguration {
private final RentrancyLevel lentrancyLevel; // default NOT_REENTRANT
private final boolean silentFailover; // default true
// The maximum length of time for which a client can hold and renew a lock aquisition
private final long maxLeaseTime;
// Maximum length of time a lock may be held without updating the lease,
// after that time any attempt to lock it will succeed
private final long renewalLeaseTime;
}
public enum RentrancyLevel {
NODE, // Node can lock if it owns the lock without blocking, only the owner node can unlock
INSTANCE, // Instance can lock multiple times if it owns the lock without blocking, only the owner instance can unlock
NOT_REENTRANT // Nobody can take the lock if already taken, but everybody can release it
}
{code}
@NODE : when a node owns the lock,
InfinispanLock defineLock(String name, LockConfiguration configuration)
Defines a lock with the specific name and LockConfiguration. It does not overwrite existing configurations.
Returns true if successfully defined or false if the lock is already defined or any other failure. If silentFailover is false, then InfinispanLockException will be raised.
InfinispanLock get(String name)
Get’s a InfinipanLock by it’s name and throws InfinispanLockException if the lock is not not defined. User must call defineLock before this method.
Optional<LockConfiguration> getConfiguration(String name);
Get’s the configuration for a Lock. If the Lock does not exist, Optional.empty() will be returned.
boolean isDefined(String name)
True if the lock exists, false if it doesn’t
CompletableFuture<Boolean> remove(String name)
Removes a Lock from the system. Returns true when it was removed, false when the lock does not exist. If any other Runtime problems appear, InfinispanLockException will be raised withe the reason. As Locks are not removed automatically, so this has to be done programatically when the Lock is no longer needed. Otherwise, OutOfMemoryException could happen.
Remove must be executed when the lock is locked, because running that without exclusive access should result in an exception. Internally, the implementation should contain generation number so that attempts to acquire a lock of a removed generation will result it exceptions in the other callers, too.
CompletableFuture<Void> reset(String name)
Resets the lock to its initial state. If any parties are currently waiting at the lock, they will return with failure on the CompletableFuture
h4. InfinispanLock
h4.
When a cluster node holding a Lock dies, this lock is released and available for the others.
public interface InfinispanLock {
CompletableFuture<Void> lock();
CompletableFuture<Boolean> tryLock();
CompletableFuture<Boolean> tryLock(long time, TimeUnit unit);
CompletableFuture<Void> unlock();
}
CompletableFuture<Void> lock();
CompletableFuture is completed successfully when the lock is acquired When a lock is aquired by a client, it will be automatically released after the maxLeaseTime specified. RenewalLeaseTime is the interval time is the time a client can aquire a lock consecutively User should set the timeouts to non-positive value The initial embedded implementation does not have to support positive values
CompletableFuture<Boolean> tryLock();
Acquires the lock only if it is free at the time of invocation. Acquires the lock if it is available and returns with the value true. If the lock is not available then this method with the value false.
CompletableFuture<Boolean> tryLock(long time, TimeUnit unit);
Acquires the lock if it is free within the given waiting time. If the lock is available this method returns with the value true.
Parameters: time - the maximum time to wait for the lock unit - the time unit of the time argument Returns: true if the lock was acquired and false if the waiting time elapsed before the lock was acquired
CompletableFuture fails with InfinispanLockException in case of error (InterruptedException, or any other non checked exceptions)
CompletableFuture<Boolean> unlock();
If the lock is rentrant (Node or Instance), only the instance or node holding the lock will be able to unlock, otherwise, anybody can unlock and it will behave as a Semaphore with one permit. True answer will say that the operation was succesul and the lock has been released, false the lock has not been relased
Demo
This main is an example on calling the API as if it was blocking, the corrent implementation should be done
public static void main(String[] args) throws Exception {
EmbeddedCacheManager cm = Infinispan.createClustered();
CounterManager counterManager = EmbeddedCounterManagerFactory.asCounterManager(cm);
counterManager.defineCounter("counter", ...);
WeakCounter counter = counterManager.weakCounter("counter");
ClusteredLockManager lockManager = EmbeddedLockManagerFactory.asClusteredLockManager(cm);
lockManager.defineLock("lock", ...);
InfinispanLock lock = lockManager.get("lock");
for (int i = 0; i < 100; i++) {
System.out.println("Counter on " + i + " is => " + counter.getValue());
lock.lock()
.thenRun(new CounterExample(counter))
.whenComplete((nil, t) -> lock.unlock());
}
cm.stop();
}
static class CounterExample implements Runnable {
private WeakCounter counter;
public CounterExample(WeakCounter counter) {
this.counter = counter;
}
@Override
public void run() {
counter.increment();
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
e.printStackTrace();
}
counter.decrement();
}
}
was:
h3. ClusteredLockManager and configuration
{code}
package org.infinispan.lock.api;
public class ClusteredLockManager {
boolean defineLock(String name, LockConfiguration configuration);
InfinispanLock get(String name);
LockConfiguration getConfiguration(String name);
boolean isDefined(String name);
CompletableFuture<Boolean> remove(String name);
CompletableFuture<Void> reset(String name);
}
public class LockConfiguration {
private final RentrancyLevel lentrancyLevel; // default NOT_REENTRANT
private final boolean silentFailover; // default true
// The maximum length of time for which a client can hold and renew a lock aquisition
private final long maxLeaseTime;
// Maximum length of time a lock may be held without updating the lease,
// after that time any attempt to lock it will succeed
private final long renewalLeaseTime;
}
public enum RentrancyLevel {
NODE, // Node can lock if it owns the lock without blocking, only the owner node can unlock
INSTANCE, // Instance can lock multiple times if it owns the lock without blocking, only the owner instance can unlock
NOT_REENTRANT // Nobody can take the lock if already taken, but everybody can release it
}
{code}
@NODE : when a node owns the lock,
InfinispanLock defineLock(String name, LockConfiguration configuration)
Defines a lock with the specific name and LockConfiguration. It does not overwrite existing configurations.
Returns true if successfully defined or false if the lock is already defined or any other failure. If silentFailover is false, then InfinispanLockException will be raised.
InfinispanLock get(String name)
Get’s a InfinipanLock by it’s name and throws InfinispanLockException if the lock is not not defined. User must call defineLock before this method.
Optional<LockConfiguration> getConfiguration(String name);
Get’s the configuration for a Lock. If the Lock does not exist, Optional.empty() will be returned.
boolean isDefined(String name)
True if the lock exists, false if it doesn’t
CompletableFuture<Boolean> remove(String name)
Removes a Lock from the system. Returns true when it was removed, false when the lock does not exist. If any other Runtime problems appear, InfinispanLockException will be raised withe the reason. As Locks are not removed automatically, so this has to be done programatically when the Lock is no longer needed. Otherwise, OutOfMemoryException could happen.
Remove must be executed when the lock is locked, because running that without exclusive access should result in an exception. Internally, the implementation should contain generation number so that attempts to acquire a lock of a removed generation will result it exceptions in the other callers, too.
CompletableFuture<Void> reset(String name)
Resets the lock to its initial state. If any parties are currently waiting at the lock, they will return with failure on the CompletableFuture
h4. InfinispanLock
h4.
When a cluster node holding a Lock dies, this lock is released and available for the others.
public interface InfinispanLock {
CompletableFuture<Void> lock();
CompletableFuture<Boolean> tryLock();
CompletableFuture<Boolean> tryLock(long time, TimeUnit unit);
CompletableFuture<Void> unlock();
}
CompletableFuture<Void> lock();
CompletableFuture is completed successfully when the lock is acquired When a lock is aquired by a client, it will be automatically released after the maxLeaseTime specified. RenewalLeaseTime is the interval time is the time a client can aquire a lock consecutively User should set the timeouts to non-positive value The initial embedded implementation does not have to support positive values
CompletableFuture<Boolean> tryLock();
Acquires the lock only if it is free at the time of invocation. Acquires the lock if it is available and returns with the value true. If the lock is not available then this method with the value false.
CompletableFuture<Boolean> tryLock(long time, TimeUnit unit);
Acquires the lock if it is free within the given waiting time. If the lock is available this method returns with the value true.
Parameters: time - the maximum time to wait for the lock unit - the time unit of the time argument Returns: true if the lock was acquired and false if the waiting time elapsed before the lock was acquired
CompletableFuture fails with InfinispanLockException in case of error (InterruptedException, or any other non checked exceptions)
CompletableFuture<Boolean> unlock();
If the lock is rentrant (Node or Instance), only the instance or node holding the lock will be able to unlock, otherwise, anybody can unlock and it will behave as a Semaphore with one permit. True answer will say that the operation was succesul and the lock has been released, false the lock has not been relased
Demo
This main is an example on calling the API as if it was blocking, the corrent implementation should be done
public static void main(String[] args) throws Exception {
EmbeddedCacheManager cm = Infinispan.createClustered();
CounterManager counterManager = EmbeddedCounterManagerFactory.asCounterManager(cm);
counterManager.defineCounter("counter", ...);
WeakCounter counter = counterManager.weakCounter("counter");
ClusteredLockManager lockManager = EmbeddedLockManagerFactory.asClusteredLockManager(cm);
lockManager.defineLock("lock", ...);
InfinispanLock lock = lockManager.get("lock");
for (int i = 0; i < 100; i++) {
System.out.println("Counter on " + i + " is => " + counter.getValue());
lock.lock()
.thenRun(new CounterExample(counter))
.whenComplete((nil, t) -> lock.unlock());
}
cm.stop();
}
static class CounterExample implements Runnable {
private WeakCounter counter;
public CounterExample(WeakCounter counter) {
this.counter = counter;
}
@Override
public void run() {
counter.increment();
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
e.printStackTrace();
}
counter.decrement();
}
}
> Clustered Locks Embedded Mode
> -----------------------------
>
> Key: ISPN-8069
> URL: https://issues.jboss.org/browse/ISPN-8069
> Project: Infinispan
> Issue Type: Feature Request
> Reporter: Katia Aresti
> Assignee: Katia Aresti
>
> h3. ClusteredLockManager and configuration
> {code : java}
> package org.infinispan.lock.api;
> public class ClusteredLockManager {
> boolean defineLock(String name, LockConfiguration configuration);
> InfinispanLock get(String name);
> LockConfiguration getConfiguration(String name);
> boolean isDefined(String name);
> CompletableFuture<Boolean> remove(String name);
> CompletableFuture<Void> reset(String name);
> }
> public class LockConfiguration {
> private final RentrancyLevel lentrancyLevel; // default NOT_REENTRANT
> private final boolean silentFailover; // default true
> // The maximum length of time for which a client can hold and renew a lock aquisition
> private final long maxLeaseTime;
> // Maximum length of time a lock may be held without updating the lease,
> // after that time any attempt to lock it will succeed
> private final long renewalLeaseTime;
> }
> public enum RentrancyLevel {
> NODE, // Node can lock if it owns the lock without blocking, only the owner node can unlock
> INSTANCE, // Instance can lock multiple times if it owns the lock without blocking, only the owner instance can unlock
> NOT_REENTRANT // Nobody can take the lock if already taken, but everybody can release it
> }
> {code}
> @NODE : when a node owns the lock,
> InfinispanLock defineLock(String name, LockConfiguration configuration)
> Defines a lock with the specific name and LockConfiguration. It does not overwrite existing configurations.
> Returns true if successfully defined or false if the lock is already defined or any other failure. If silentFailover is false, then InfinispanLockException will be raised.
> InfinispanLock get(String name)
> Get’s a InfinipanLock by it’s name and throws InfinispanLockException if the lock is not not defined. User must call defineLock before this method.
> Optional<LockConfiguration> getConfiguration(String name);
> Get’s the configuration for a Lock. If the Lock does not exist, Optional.empty() will be returned.
> boolean isDefined(String name)
> True if the lock exists, false if it doesn’t
> CompletableFuture<Boolean> remove(String name)
> Removes a Lock from the system. Returns true when it was removed, false when the lock does not exist. If any other Runtime problems appear, InfinispanLockException will be raised withe the reason. As Locks are not removed automatically, so this has to be done programatically when the Lock is no longer needed. Otherwise, OutOfMemoryException could happen.
> Remove must be executed when the lock is locked, because running that without exclusive access should result in an exception. Internally, the implementation should contain generation number so that attempts to acquire a lock of a removed generation will result it exceptions in the other callers, too.
> CompletableFuture<Void> reset(String name)
> Resets the lock to its initial state. If any parties are currently waiting at the lock, they will return with failure on the CompletableFuture
> h4. InfinispanLock
> h4.
> When a cluster node holding a Lock dies, this lock is released and available for the others.
> public interface InfinispanLock {
> CompletableFuture<Void> lock();
> CompletableFuture<Boolean> tryLock();
> CompletableFuture<Boolean> tryLock(long time, TimeUnit unit);
> CompletableFuture<Void> unlock();
> }
> CompletableFuture<Void> lock();
> CompletableFuture is completed successfully when the lock is acquired When a lock is aquired by a client, it will be automatically released after the maxLeaseTime specified. RenewalLeaseTime is the interval time is the time a client can aquire a lock consecutively User should set the timeouts to non-positive value The initial embedded implementation does not have to support positive values
> CompletableFuture<Boolean> tryLock();
> Acquires the lock only if it is free at the time of invocation. Acquires the lock if it is available and returns with the value true. If the lock is not available then this method with the value false.
> CompletableFuture<Boolean> tryLock(long time, TimeUnit unit);
> Acquires the lock if it is free within the given waiting time. If the lock is available this method returns with the value true.
> Parameters: time - the maximum time to wait for the lock unit - the time unit of the time argument Returns: true if the lock was acquired and false if the waiting time elapsed before the lock was acquired
> CompletableFuture fails with InfinispanLockException in case of error (InterruptedException, or any other non checked exceptions)
> CompletableFuture<Boolean> unlock();
> If the lock is rentrant (Node or Instance), only the instance or node holding the lock will be able to unlock, otherwise, anybody can unlock and it will behave as a Semaphore with one permit. True answer will say that the operation was succesul and the lock has been released, false the lock has not been relased
> Demo
> This main is an example on calling the API as if it was blocking, the corrent implementation should be done
> public static void main(String[] args) throws Exception {
> EmbeddedCacheManager cm = Infinispan.createClustered();
> CounterManager counterManager = EmbeddedCounterManagerFactory.asCounterManager(cm);
> counterManager.defineCounter("counter", ...);
> WeakCounter counter = counterManager.weakCounter("counter");
> ClusteredLockManager lockManager = EmbeddedLockManagerFactory.asClusteredLockManager(cm);
> lockManager.defineLock("lock", ...);
> InfinispanLock lock = lockManager.get("lock");
> for (int i = 0; i < 100; i++) {
> System.out.println("Counter on " + i + " is => " + counter.getValue());
> lock.lock()
> .thenRun(new CounterExample(counter))
> .whenComplete((nil, t) -> lock.unlock());
> }
> cm.stop();
> }
> static class CounterExample implements Runnable {
> private WeakCounter counter;
> public CounterExample(WeakCounter counter) {
> this.counter = counter;
> }
> @Override
> public void run() {
> counter.increment();
> try {
> Thread.sleep(1000);
> } catch (InterruptedException e) {
> e.printStackTrace();
> }
> counter.decrement();
> }
> }
--
This message was sent by Atlassian JIRA
(v7.2.3#72005)
7 years, 6 months