Search in sources :

Example 26 with AsyncCallFuture

use of org.apache.cloudstack.framework.async.AsyncCallFuture in project cloudstack by apache.

the class TemplateTest method deleteTemplate.

// @Test
public void deleteTemplate() {
    TemplateInfo template = templateFactory.getTemplate(templateId, DataStoreRole.Image);
    DataStore store = dataStoreMgr.getImageStore(dcId);
    AsyncCallFuture<TemplateApiResult> future = new AsyncCallFuture<TemplateApiResult>();
    templateSvr.deleteTemplateAsync(template);
    try {
        TemplateApiResult result = future.get();
        assertTrue(result.isSuccess(), "failed to delete template: " + result.getResult());
    } catch (InterruptedException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
        assertTrue(false, e.getMessage());
    } catch (ExecutionException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
        assertTrue(false, e.getMessage());
    }
}
Also used : AsyncCallFuture(org.apache.cloudstack.framework.async.AsyncCallFuture) TemplateInfo(org.apache.cloudstack.engine.subsystem.api.storage.TemplateInfo) DataStore(org.apache.cloudstack.engine.subsystem.api.storage.DataStore) ExecutionException(java.util.concurrent.ExecutionException) TemplateApiResult(org.apache.cloudstack.engine.subsystem.api.storage.TemplateService.TemplateApiResult)

Example 27 with AsyncCallFuture

use of org.apache.cloudstack.framework.async.AsyncCallFuture in project cloudstack by apache.

the class TemplateTest method registerTemplate.

@Test
public void registerTemplate() {
    TemplateInfo template = templateFactory.getTemplate(templateId, DataStoreRole.Image);
    DataStore store = dataStoreMgr.getImageStore(dcId);
    AsyncCallFuture<TemplateApiResult> future = new AsyncCallFuture<TemplateApiResult>();
    templateSvr.createTemplateAsync(template, store, future);
    try {
        TemplateApiResult result = future.get();
        assertTrue(result.isSuccess(), "failed to register template: " + result.getResult());
    } catch (InterruptedException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
        assertTrue(false, e.getMessage());
    } catch (ExecutionException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
        assertTrue(false, e.getMessage());
    }
}
Also used : AsyncCallFuture(org.apache.cloudstack.framework.async.AsyncCallFuture) TemplateInfo(org.apache.cloudstack.engine.subsystem.api.storage.TemplateInfo) DataStore(org.apache.cloudstack.engine.subsystem.api.storage.DataStore) ExecutionException(java.util.concurrent.ExecutionException) TemplateApiResult(org.apache.cloudstack.engine.subsystem.api.storage.TemplateService.TemplateApiResult) Test(org.testng.annotations.Test)

Example 28 with AsyncCallFuture

use of org.apache.cloudstack.framework.async.AsyncCallFuture in project cloudstack by apache.

the class StorageCacheManagerImpl method createCacheObject.

@Override
public DataObject createCacheObject(DataObject data, DataStore store) {
    DataObject objOnCacheStore = null;
    final Object lock;
    final DataObjectType type = data.getType();
    final String typeName;
    final long storeId = store.getId();
    final long dataId = data.getId();
    /*
         * Make sure any thread knows own lock type.
         */
    if (type == DataObjectType.TEMPLATE) {
        lock = templateLock;
        typeName = "template";
    } else if (type == DataObjectType.VOLUME) {
        lock = volumeLock;
        typeName = "volume";
    } else if (type == DataObjectType.SNAPSHOT) {
        lock = snapshotLock;
        typeName = "snapshot";
    } else {
        String msg = "unsupported DataObject comes, then can't acquire correct lock object";
        throw new CloudRuntimeException(msg);
    }
    s_logger.debug("check " + typeName + " cache entry(id: " + dataId + ") on store(id: " + storeId + ")");
    DataObject existingDataObj = null;
    synchronized (lock) {
        DataObjectInStore obj = objectInStoreMgr.findObject(data, store);
        if (obj != null) {
            State st = obj.getState();
            long miliSeconds = 10000;
            long timeoutSeconds = 3600;
            long timeoutMiliSeconds = timeoutSeconds * 1000;
            Date now = new Date();
            long expiredEpoch = now.getTime() + timeoutMiliSeconds;
            Date expiredDate = new Date(expiredEpoch);
            /*
                 * Waiting for completion of cache copy.
                 */
            while (st == ObjectInDataStoreStateMachine.State.Allocated || st == ObjectInDataStoreStateMachine.State.Creating || st == ObjectInDataStoreStateMachine.State.Copying) {
                /*
                     * Threads must release lock within waiting for cache copy and
                     * must be waken up at completion.
                     */
                s_logger.debug("waiting cache copy completion type: " + typeName + ", id: " + obj.getObjectId() + ", lock: " + lock.hashCode());
                try {
                    lock.wait(miliSeconds);
                } catch (InterruptedException e) {
                    s_logger.debug("[ignored] interupted while waiting for cache copy completion.");
                }
                s_logger.debug("waken up");
                now = new Date();
                if (now.after(expiredDate)) {
                    String msg = "Waiting time exceeds timeout limit(" + timeoutSeconds + " s)";
                    throw new CloudRuntimeException(msg);
                }
                obj = objectInStoreMgr.findObject(data, store);
                st = obj.getState();
            }
            if (st == ObjectInDataStoreStateMachine.State.Ready) {
                s_logger.debug("there is already one in the cache store");
                DataObject dataObj = objectInStoreMgr.get(data, store);
                dataObj.incRefCount();
                existingDataObj = dataObj;
            }
        }
        if (existingDataObj == null) {
            s_logger.debug("create " + typeName + " cache entry(id: " + dataId + ") on store(id: " + storeId + ")");
            objOnCacheStore = store.create(data);
        }
        lock.notifyAll();
    }
    if (existingDataObj != null) {
        return existingDataObj;
    }
    if (objOnCacheStore == null) {
        s_logger.error("create " + typeName + " cache entry(id: " + dataId + ") on store(id: " + storeId + ") failed");
        return null;
    }
    AsyncCallFuture<CopyCommandResult> future = new AsyncCallFuture<CopyCommandResult>();
    CopyCommandResult result = null;
    try {
        objOnCacheStore.processEvent(Event.CreateOnlyRequested);
        dataMotionSvr.copyAsync(data, objOnCacheStore, future);
        result = future.get();
        if (result.isFailed()) {
            objOnCacheStore.processEvent(Event.OperationFailed);
        } else {
            objOnCacheStore.processEvent(Event.OperationSuccessed, result.getAnswer());
            objOnCacheStore.incRefCount();
            return objOnCacheStore;
        }
    } catch (InterruptedException e) {
        s_logger.debug("create cache storage failed: " + e.toString());
        throw new CloudRuntimeException(e);
    } catch (ExecutionException e) {
        s_logger.debug("create cache storage failed: " + e.toString());
        throw new CloudRuntimeException(e);
    } finally {
        if (result == null) {
            objOnCacheStore.processEvent(Event.OperationFailed);
        }
        synchronized (lock) {
            /*
                 * Wake up all threads waiting for cache copy.
                 */
            s_logger.debug("wake up all waiting threads(lock: " + lock.hashCode() + ")");
            lock.notifyAll();
        }
    }
    return null;
}
Also used : DataObjectInStore(org.apache.cloudstack.engine.subsystem.api.storage.DataObjectInStore) Date(java.util.Date) AsyncCallFuture(org.apache.cloudstack.framework.async.AsyncCallFuture) DataObject(org.apache.cloudstack.engine.subsystem.api.storage.DataObject) CloudRuntimeException(com.cloud.utils.exception.CloudRuntimeException) State(org.apache.cloudstack.engine.subsystem.api.storage.ObjectInDataStoreStateMachine.State) DataObject(org.apache.cloudstack.engine.subsystem.api.storage.DataObject) ExecutionException(java.util.concurrent.ExecutionException) DataObjectType(com.cloud.agent.api.to.DataObjectType) CopyCommandResult(org.apache.cloudstack.engine.subsystem.api.storage.CopyCommandResult)

Aggregations

AsyncCallFuture (org.apache.cloudstack.framework.async.AsyncCallFuture)28 CopyCommandResult (org.apache.cloudstack.engine.subsystem.api.storage.CopyCommandResult)19 CloudRuntimeException (com.cloud.utils.exception.CloudRuntimeException)18 TemplateInfo (org.apache.cloudstack.engine.subsystem.api.storage.TemplateInfo)9 CommandResult (org.apache.cloudstack.storage.command.CommandResult)9 ExecutionException (java.util.concurrent.ExecutionException)8 ConcurrentOperationException (com.cloud.exception.ConcurrentOperationException)7 ResourceAllocationException (com.cloud.exception.ResourceAllocationException)7 CreateCmdResult (org.apache.cloudstack.engine.subsystem.api.storage.CreateCmdResult)7 DataStore (org.apache.cloudstack.engine.subsystem.api.storage.DataStore)7 VolumeInfo (org.apache.cloudstack.engine.subsystem.api.storage.VolumeInfo)7 DataObject (org.apache.cloudstack.engine.subsystem.api.storage.DataObject)6 PrimaryDataStore (org.apache.cloudstack.engine.subsystem.api.storage.PrimaryDataStore)6 SnapshotResult (org.apache.cloudstack.engine.subsystem.api.storage.SnapshotResult)5 TemplateObject (org.apache.cloudstack.storage.image.store.TemplateObject)4 HashMap (java.util.HashMap)3 SnapshotInfo (org.apache.cloudstack.engine.subsystem.api.storage.SnapshotInfo)3 TemplateApiResult (org.apache.cloudstack.engine.subsystem.api.storage.TemplateService.TemplateApiResult)3 VolumeDataStoreVO (org.apache.cloudstack.storage.datastore.db.VolumeDataStoreVO)3 VMTemplateStoragePoolVO (com.cloud.storage.VMTemplateStoragePoolVO)2