Search in sources :

Example 6 with Lease

use of org.apache.curator.framework.recipes.locks.Lease in project fabric8 by jboss-fuse.

the class ZookeeperPortService method registerPort.

@Override
public void registerPort(Container container, String pid, String key, int port, Lock lock) {
    Lease lease = null;
    if (lock != null && lock instanceof LeaseLock) {
        lease = ((LeaseLock) lock).getLease();
    }
    registerPort(container, pid, key, port, lease);
}
Also used : Lease(org.apache.curator.framework.recipes.locks.Lease)

Example 7 with Lease

use of org.apache.curator.framework.recipes.locks.Lease in project fabric8 by jboss-fuse.

the class ZookeeperPortService method unregisterPort.

private void unregisterPort(Container container, String pid, Lease existingLease) {
    assertValid();
    String containerPortsPidPath = ZkPath.PORTS_CONTAINER_PID.getPath(container.getId(), pid);
    Lease lease = null;
    try {
        if (existingLease != null) {
            lease = existingLease;
        } else {
            lease = interProcessLock.acquire(60, TimeUnit.SECONDS);
        }
        if (lease != null) {
            if (exists(curator.get(), containerPortsPidPath) != null) {
                for (String key : getChildren(curator.get(), containerPortsPidPath)) {
                    unregisterPort(container, pid, key, lease);
                }
                deleteSafe(curator.get(), containerPortsPidPath);
            }
        } else {
            throw new FabricException("Could not acquire port lock for pid " + pid);
        }
    } catch (InterruptedException ex) {
        cleanUpDirtyZKNodes(interProcessLock);
        throw FabricException.launderThrowable(ex);
    } catch (Exception ex) {
        throw FabricException.launderThrowable(ex);
    } finally {
        if (existingLease == null) {
            releaseLock(lease);
        }
    }
}
Also used : Lease(org.apache.curator.framework.recipes.locks.Lease) FabricException(io.fabric8.api.FabricException) FabricException(io.fabric8.api.FabricException)

Example 8 with Lease

use of org.apache.curator.framework.recipes.locks.Lease in project coprhd-controller by CoprHD.

the class SmisCreateMultiVolumeJob method changeVolumeName.

/**
 * Method will modify the name of a given volume to a generate name.
 *
 * @param dbClient [in] - Client instance for reading/writing from/to DB
 * @param client [in] - WBEMClient used for reading/writing from/to SMI-S
 * @param volumePath [in] - CIMObjectPath referencing the volume
 * @param volume [in] - Volume object
 */
private void changeVolumeName(DbClient dbClient, WBEMClient client, CIMObjectPath volumePath, Volume volume, String name) {
    Lease lease = null;
    try {
        _log.info(String.format("Attempting to modify volume %s to %s", volumePath.toString(), name));
        if (_propertyFactoryRef.get() == null) {
            _propertyFactoryRef.compareAndSet(null, (CIMPropertyFactory) ControllerServiceImpl.getBean("CIMPropertyFactory"));
        }
        CIMInstance toUpdate = new CIMInstance(volumePath, new CIMProperty[] { _propertyFactoryRef.get().string(SmisConstants.CP_ELEMENT_NAME, name) });
        if (_distributedLock.get() == null) {
            if (_coordinator.get() == null) {
                _coordinator.compareAndSet(null, (CoordinatorClient) ControllerServiceImpl.getBean("coordinator"));
            }
            _distributedLock.compareAndSet(null, _coordinator.get().getSemaphore(this.getClass().getSimpleName(), MAX_PERMITS));
        }
        lease = _distributedLock.get().acquireLease();
        client.modifyInstance(toUpdate, SmisConstants.PS_ELEMENT_NAME);
        _distributedLock.get().returnLease(lease);
        lease = null;
        volume.setDeviceLabel(name);
        dbClient.persistObject(volume);
        _log.info(String.format("Volume name has been modified to %s", name));
    } catch (WBEMException e) {
        _log.error("Encountered an error while trying to set the volume name", e);
    } catch (DatabaseException e) {
        _log.error("Encountered an error while trying to set the volume name", e);
    } catch (Exception e) {
        _log.error("Encountered an error while trying to set the volume name", e);
    } finally {
        if (lease != null) {
            try {
                _distributedLock.get().returnLease(lease);
            } catch (Exception e) {
                _log.error("Exception when trying to return lease", e);
            }
        }
    }
}
Also used : Lease(org.apache.curator.framework.recipes.locks.Lease) WBEMException(javax.wbem.WBEMException) DatabaseException(com.emc.storageos.db.exceptions.DatabaseException) CIMInstance(javax.cim.CIMInstance) WBEMException(javax.wbem.WBEMException) DatabaseException(com.emc.storageos.db.exceptions.DatabaseException)

Example 9 with Lease

use of org.apache.curator.framework.recipes.locks.Lease in project coprhd-controller by CoprHD.

the class DistributedSemaphoreTest method testDistributedSemaphoreFiniteWait.

/**
 * Executes multiple workers using the semaphore (but acquiring with finite wait).
 * If a worker is unable to acquire a lease within a specified time, it retries.
 *
 * @throws Exception
 */
@Test
public void testDistributedSemaphoreFiniteWait() throws Exception {
    final DistributedSemaphore mySem = connectClient().getSemaphore(SEMAPHORE_NAME_TEST2, 1);
    _logger.info("*** DistributedSemaphoreFiniteWaitTest start");
    for (int i = 0; i < POOLSIZE; i++) {
        _logger.info(": spawning worker number : " + i);
        _workers2.execute(new Runnable() {

            @Override
            public void run() {
                for (int i = 0; i < 25; i++) {
                    _logger.info(": execution run number : " + i);
                    Lease lease = null;
                    Random rand = new Random();
                    try {
                        _logger.info(": Going to acquire lease.");
                        lease = mySem.acquireLease(50, TimeUnit.MILLISECONDS);
                        if (lease == null) {
                            _logger.info(": Could not acquire lease.");
                            Thread.sleep(rand.nextInt(500));
                        } else {
                            _logger.info(": Doing work holding lease : " + lease.toString());
                        }
                    } catch (Exception e) {
                        _logger.info(": Problem when acquiring lease or doing work.");
                        Assert.assertNull(e);
                    } finally {
                        if (lease != null) {
                            _logger.info(": Work done .. going to return lease.");
                            try {
                                mySem.returnLease(lease);
                                lease = null;
                            } catch (Exception e) {
                                _logger.info(": Problem while returning lease: " + lease.toString());
                                Assert.assertNull(lease);
                            }
                        } else {
                            _logger.info(": No lease to return.");
                        }
                    }
                }
            }
        });
    }
    _workers2.shutdown();
    Assert.assertTrue(_workers2.awaitTermination(60, TimeUnit.SECONDS));
    _logger.info("*** DistributedSemaphoreFiniteWaitTest end");
}
Also used : Lease(org.apache.curator.framework.recipes.locks.Lease) Random(java.util.Random) Test(org.junit.Test)

Example 10 with Lease

use of org.apache.curator.framework.recipes.locks.Lease in project coprhd-controller by CoprHD.

the class DistributedSemaphoreTest method testDistributedSemaphore.

/**
 * Executes multiple workers using the semaphore (but acquiring with infinite wait).
 * If a worker is unable to acquire a lease, it blocks.
 *
 * @throws Exception
 */
@Test
public void testDistributedSemaphore() throws Exception {
    final DistributedSemaphore mySem = connectClient().getSemaphore(SEMAPHORE_NAME_TEST1, 1);
    _logger.info("*** DistributedSemaphoreTest start");
    for (int i = 0; i < POOLSIZE; i++) {
        _logger.info(": spawning worker number : " + i);
        _workers1.execute(new Runnable() {

            @Override
            public void run() {
                for (int i = 0; i < 25; i++) {
                    _logger.info(": execution run number : " + i);
                    Lease lease = null;
                    Random rand = new Random();
                    try {
                        _logger.info(": Going to acquire lease.");
                        lease = mySem.acquireLease();
                        _logger.info(": Doing work holding lease : " + lease.toString());
                    } catch (Exception e) {
                        _logger.info(": Problem when acquiring lease or doing work.");
                        Assert.assertNull(e);
                    } finally {
                        if (lease != null) {
                            _logger.info(": Work done .. going to return lease.");
                            try {
                                mySem.returnLease(lease);
                                lease = null;
                                Thread.sleep(rand.nextInt(500));
                            } catch (Exception e) {
                                _logger.info(": Problem while returning lease: " + lease.toString());
                                Assert.assertNull(lease);
                            }
                        } else {
                            _logger.info(": No lease to return.");
                        }
                    }
                }
            }
        });
    }
    _workers1.shutdown();
    Assert.assertTrue(_workers1.awaitTermination(60, TimeUnit.SECONDS));
    _logger.info("*** DistributedSemaphoreTest end");
}
Also used : Lease(org.apache.curator.framework.recipes.locks.Lease) Random(java.util.Random) Test(org.junit.Test)

Aggregations

Lease (org.apache.curator.framework.recipes.locks.Lease)10 FabricException (io.fabric8.api.FabricException)6 HashSet (java.util.HashSet)2 Random (java.util.Random)2 Test (org.junit.Test)2 DatabaseException (com.emc.storageos.db.exceptions.DatabaseException)1 CIMInstance (javax.cim.CIMInstance)1 WBEMException (javax.wbem.WBEMException)1