Search in sources :

Example 61 with InterProcessLock

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

the class StorageDriverService method getStorageDriverOperationLock.

protected InterProcessLock getStorageDriverOperationLock() {
    // Try to acquire lock, succeed or throw Exception
    InterProcessLock lock = coordinator.getSiteLocalLock(STORAGE_DRIVER_OPERATION_lOCK);
    boolean acquired;
    try {
        acquired = lock.acquire(LOCK_WAIT_TIME_SEC, TimeUnit.SECONDS);
    } catch (Exception e) {
        try {
            lock.release();
        } catch (Exception ex) {
            log.error("Fail to release storage driver operation lock", ex);
        }
        throw APIException.internalServerErrors.installDriverPrecheckFailed("Acquiring lock failed, there's another trigger operation holding lock");
    }
    if (!acquired) {
        throw APIException.internalServerErrors.installDriverPrecheckFailed("Acquiring lock failed, there's another trigger operation holding lock");
    }
    // Check if there's ongoing storage operation, if there is, release lock
    // and throw exception
    StorageSystemType opOngoingStorageType = null;
    List<StorageSystemType> types = listStorageSystemTypes();
    for (StorageSystemType type : types) {
        String statusStr = type.getDriverStatus();
        if (statusStr == null) {
            log.info("Bypass type {} as it has no status field value", type.getStorageTypeName());
            continue;
        }
        StorageSystemType.STATUS status = Enum.valueOf(StorageSystemType.STATUS.class, type.getDriverStatus());
        if (status.isStorageOperationOngoing()) {
            opOngoingStorageType = type;
            break;
        }
    }
    if (opOngoingStorageType != null) {
        try {
            lock.release();
        } catch (Exception e) {
            log.error("Fail to release storage driver operation lock", e);
        }
        throw APIException.internalServerErrors.installDriverPrecheckFailed(String.format("Driver %s is in % state", opOngoingStorageType.getDriverName(), opOngoingStorageType.getDriverStatus()));
    }
    return lock;
}
Also used : InterProcessLock(org.apache.curator.framework.recipes.locks.InterProcessLock) StorageSystemType(com.emc.storageos.db.client.model.StorageSystemType) APIException(com.emc.storageos.svcs.errorhandling.resources.APIException) FileNotFoundException(java.io.FileNotFoundException) IOException(java.io.IOException)

Example 62 with InterProcessLock

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

the class InterVDCTokenCacheHelper method saveTokenKeysBundle.

/**
 * Stores the token key bundle in cache (zookeeper path based on vdcid)
 * Locks on vdcid for the write.
 *
 * @param vdcID
 * @param bundle
 */
private synchronized void saveTokenKeysBundle(String vdcID, TokenKeysBundle bundle) {
    InterProcessLock tokenBundleLock = null;
    try {
        tokenBundleLock = coordinator.getLock(FOREIGN_TOKEN_BUNDLE_CONFIG_LOCK);
        if (tokenBundleLock == null) {
            log.error("Could not acquire lock for tokenkeys bundle caching");
            throw SecurityException.fatals.couldNotAcquireLockTokenCaching();
        }
        tokenBundleLock.acquire();
        Configuration config = coordinator.queryConfiguration(FOREIGN_TOKEN_KEYS_BUNDLE_CONFIG, FOREIGN_TOKEN_KEYS_BUNDLE_KEYID);
        ConfigurationImpl configImpl = null;
        if (config == null) {
            configImpl = new ConfigurationImpl();
            configImpl.setId(FOREIGN_TOKEN_KEYS_BUNDLE_KEYID);
            configImpl.setKind(FOREIGN_TOKEN_KEYS_BUNDLE_CONFIG);
            log.debug("Creating new foreign tokens config");
        } else {
            configImpl = (ConfigurationImpl) config;
            log.debug("Updating existing foreign token config");
        }
        configImpl.setConfig(vdcID, SerializerUtils.serializeAsBase64EncodedString(bundle));
        coordinator.persistServiceConfiguration(configImpl);
        foreignTokenKeysMap.put(vdcID, bundle);
    } catch (Exception ex) {
        log.error("Could not acquire lock while trying to cache tokenkeys bundle.", ex);
    } finally {
        try {
            if (tokenBundleLock != null) {
                tokenBundleLock.release();
            }
        } catch (Exception ex) {
            log.error("Unable to release token keys bundle caching lock", ex);
        }
    }
}
Also used : Configuration(com.emc.storageos.coordinator.common.Configuration) InterProcessLock(org.apache.curator.framework.recipes.locks.InterProcessLock) ConfigurationImpl(com.emc.storageos.coordinator.common.impl.ConfigurationImpl) SecurityException(com.emc.storageos.security.exceptions.SecurityException)

Example 63 with InterProcessLock

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

the class InterVDCTokenCacheHelper method cacheForeignTokenArtifacts.

/**
 * Saves the token and user dao records to the db. Set the cache expiration time
 * to 10 minutes or time left on the token, whichever is sooner.
 * Note: this method assumes validity of the token (expiration) has been checked
 *
 * @param t
 * @param user
 * @param now current time in minutes
 */
private synchronized void cacheForeignTokenArtifacts(final Token token, final StorageOSUserDAO user) {
    long now = System.currentTimeMillis() / (MIN_TO_MSECS);
    InterProcessLock tokenLock = null;
    try {
        tokenLock = coordinator.getLock(token.getId().toString());
        if (tokenLock == null) {
            log.error("Could not acquire lock for token caching");
            throw SecurityException.fatals.couldNotAcquireLockTokenCaching();
        }
        tokenLock.acquire();
        StorageOSUserDAO userToPersist = dbClient.queryObject(StorageOSUserDAO.class, user.getId());
        userToPersist = (userToPersist == null) ? new StorageOSUserDAO() : userToPersist;
        userToPersist.setAttributes(user.getAttributes());
        userToPersist.setCreationTime(user.getCreationTime());
        userToPersist.setDistinguishedName(user.getDistinguishedName());
        userToPersist.setGroups(user.getGroups());
        userToPersist.setId(user.getId());
        userToPersist.setIsLocal(user.getIsLocal());
        userToPersist.setTenantId(user.getTenantId());
        userToPersist.setUserName(user.getUserName());
        dbClient.persistObject(userToPersist);
        Token tokenToPersist = dbClient.queryObject(Token.class, token.getId());
        tokenToPersist = (tokenToPersist == null) ? new Token() : tokenToPersist;
        if ((token.getExpirationTime() - now) > maxLifeValuesHolder.getForeignTokenCacheExpirationInMins()) {
            tokenToPersist.setCacheExpirationTime(now + maxLifeValuesHolder.getForeignTokenCacheExpirationInMins());
        } else {
            tokenToPersist.setCacheExpirationTime(token.getExpirationTime());
        }
        tokenToPersist.setId(token.getId());
        // relative index, Id of the userDAO record
        tokenToPersist.setUserId(user.getId());
        tokenToPersist.setIssuedTime(token.getIssuedTime());
        tokenToPersist.setLastAccessTime(now);
        tokenToPersist.setExpirationTime(token.getExpirationTime());
        tokenToPersist.setIndexed(true);
        tokenToPersist.setZoneId(token.getZoneId());
        dbClient.persistObject(tokenToPersist);
        log.info("Cached user {} and token", user.getUserName());
    } catch (Exception ex) {
        log.error("Could not acquire lock while trying to get a proxy token.", ex);
    } finally {
        try {
            if (tokenLock != null) {
                tokenLock.release();
            }
        } catch (Exception ex) {
            log.error("Unable to release token caching lock", ex);
        }
    }
}
Also used : StorageOSUserDAO(com.emc.storageos.db.client.model.StorageOSUserDAO) InterProcessLock(org.apache.curator.framework.recipes.locks.InterProcessLock) Token(com.emc.storageos.db.client.model.Token) SecurityException(com.emc.storageos.security.exceptions.SecurityException)

Example 64 with InterProcessLock

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

the class CoordinatorConfigStoringHelper method removeConfig.

/**
 * Removes the specified config from coordinator. If siteId is not null, config
 * is in global area. Otherwise it is in site specific area
 *
 * @param lockName
 *            the name of the lock to use while removing this object
 * @param configKInd
 * @param configId
 * @throws Exception
 */
public void removeConfig(String lockName, String siteId, String configKInd, String configId) throws Exception {
    InterProcessLock lock = acquireLock(lockName);
    try {
        Configuration config = coordinator.queryConfiguration(siteId, configKInd, configId);
        if (config != null) {
            coordinator.removeServiceConfiguration(siteId, config);
            log.debug("removed config successfully");
        } else {
            log.debug("config " + configId + " of kind " + configKInd + " was not removed since it could not be found");
        }
    } finally {
        releaseLock(lock);
    }
}
Also used : Configuration(com.emc.storageos.coordinator.common.Configuration) InterProcessLock(org.apache.curator.framework.recipes.locks.InterProcessLock)

Example 65 with InterProcessLock

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

the class CoordinatorConfigStoringHelper method acquireLock.

/**
 * Acquires an interprocess lock
 *
 * @param lockName
 *            the lock to acquire
 * @return the acquired lock
 * @throws Exception
 *             if failed to acquire the lock
 */
public synchronized InterProcessLock acquireLock(String lockName) throws Exception {
    InterProcessLock lock = nameLockMap.get(lockName);
    if (lock == null) {
        lock = coordinator.getSiteLocalLock(lockName);
        nameLockMap.put(lockName, lock);
    }
    lock.acquire();
    log.info("Acquired the lock {}", lockName);
    return lock;
}
Also used : InterProcessLock(org.apache.curator.framework.recipes.locks.InterProcessLock)

Aggregations

InterProcessLock (org.apache.curator.framework.recipes.locks.InterProcessLock)98 APIException (com.emc.storageos.svcs.errorhandling.resources.APIException)25 DatabaseException (com.emc.storageos.db.exceptions.DatabaseException)21 DeviceControllerException (com.emc.storageos.exceptions.DeviceControllerException)15 IOException (java.io.IOException)15 ControllerException (com.emc.storageos.volumecontroller.ControllerException)14 Configuration (com.emc.storageos.coordinator.common.Configuration)12 CoordinatorException (com.emc.storageos.coordinator.exceptions.CoordinatorException)12 UnknownHostException (java.net.UnknownHostException)12 Site (com.emc.storageos.coordinator.client.model.Site)11 RetryableCoordinatorException (com.emc.storageos.coordinator.exceptions.RetryableCoordinatorException)11 NetworkDeviceControllerException (com.emc.storageos.networkcontroller.exceptions.NetworkDeviceControllerException)10 CheckPermission (com.emc.storageos.security.authorization.CheckPermission)9 ServiceError (com.emc.storageos.svcs.errorhandling.model.ServiceError)9 BiosCommandResult (com.emc.storageos.volumecontroller.impl.BiosCommandResult)9 ArrayList (java.util.ArrayList)9 POST (javax.ws.rs.POST)9 NetworkSystem (com.emc.storageos.db.client.model.NetworkSystem)8 Path (javax.ws.rs.Path)8 ConfigurationImpl (com.emc.storageos.coordinator.common.impl.ConfigurationImpl)6