Skip to content

Commit 2d6133c

Browse files
committed
change the top level async call using future
1 parent 2e9c55f commit 2d6133c

9 files changed

Lines changed: 219 additions & 113 deletions

File tree

engine/storage/integration-test/test/org/apache/cloudstack/storage/test/volumeServiceTest.java

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -171,7 +171,7 @@ public void setUp() {
171171
host.setClusterId(cluster.getId());
172172

173173
host = hostDao.persist(host);
174-
174+
primaryStore = createPrimaryDataStore();
175175

176176
//CreateVolumeAnswer createVolumeFromImageAnswer = new CreateVolumeAnswer(UUID.randomUUID().toString());
177177

@@ -280,8 +280,8 @@ private VolumeVO createVolume(Long templateId, long dataStoreId) {
280280

281281
@Test(priority=2)
282282
public void createVolumeFromTemplate() {
283+
primaryStore = createPrimaryDataStore();
283284
TemplateEntity te = createTemplate();
284-
primaryStore = createPrimaryDataStore();
285285
VolumeVO volume = createVolume(te.getId(), primaryStore.getId());
286286
VolumeEntity ve = volumeService.getVolumeEntity(volume.getId());
287287
ve.createVolumeFromTemplate(primaryStore.getId(), new VHD(), te);
@@ -290,6 +290,7 @@ public void createVolumeFromTemplate() {
290290

291291
@Test(priority=3)
292292
public void createDataDisk() {
293+
primaryStore = createPrimaryDataStore();
293294
VolumeVO volume = createVolume(null, primaryStore.getId());
294295
VolumeEntity ve = volumeService.getVolumeEntity(volume.getId());
295296
ve.createVolume(primaryStore.getId(), new VHD());

engine/storage/src/org/apache/cloudstack/storage/volume/VolumeEntityImpl.java

Lines changed: 20 additions & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222
import java.util.Date;
2323
import java.util.List;
2424
import java.util.Map;
25+
import java.util.concurrent.ExecutionException;
2526

2627
import org.apache.cloudstack.engine.cloud.entity.api.SnapshotEntity;
2728
import org.apache.cloudstack.engine.cloud.entity.api.TemplateEntity;
@@ -30,6 +31,7 @@
3031
import org.apache.cloudstack.engine.subsystem.api.storage.VolumeInfo;
3132
import org.apache.cloudstack.engine.subsystem.api.storage.disktype.VolumeDiskType;
3233
import org.apache.cloudstack.engine.subsystem.api.storage.type.VolumeType;
34+
import org.apache.cloudstack.framework.async.AsyncCallFuture;
3335
import org.apache.cloudstack.framework.async.AsyncCallbackDispatcher;
3436
import org.apache.cloudstack.framework.async.AsyncCompletionCallback;
3537
import org.apache.cloudstack.storage.datastore.PrimaryDataStoreEntityImpl;
@@ -170,81 +172,52 @@ public StorageEntity getDataStore() {
170172
@Override
171173
public boolean createVolumeFromTemplate(long dataStoreId, VolumeDiskType diskType, TemplateEntity template) {
172174
TemplateInfo ti = ((TemplateEntityImpl)template).getTemplateInfo();
173-
174-
AsyncCallbackDispatcher<VolumeEntityImpl, VolumeInfo> caller = AsyncCallbackDispatcher.create(this);
175-
caller.setCallback(caller.getTarget().createVolumeFromTemplateAsyncCallback(null, null));
176175

177-
vs.createVolumeFromTemplateAsync(volumeInfo, dataStoreId, diskType, ti, caller);
176+
AsyncCallFuture<VolumeApiResult> future = vs.createVolumeFromTemplateAsync(volumeInfo, dataStoreId, diskType, ti);
178177
try {
179-
synchronized (volumeInfo) {
180-
volumeInfo.wait();
178+
result = future.get();
179+
if (!result.isSuccess()) {
180+
throw new CloudRuntimeException("create volume from template failed: " + result.getResult());
181181
}
182+
return true;
182183
} catch (InterruptedException e) {
183-
throw new CloudRuntimeException("wait volume info failed", e);
184-
}
185-
return true;
186-
}
187-
188-
189-
public Object createVolumeFromTemplateAsyncCallback(AsyncCallbackDispatcher<VolumeEntityImpl, VolumeInfo> callback, Object context) {
190-
synchronized (volumeInfo) {
191-
volumeInfo.notify();
184+
throw new CloudRuntimeException("wait result failed", e);
185+
} catch (ExecutionException e) {
186+
throw new CloudRuntimeException("wait result failed", e);
192187
}
193-
return null;
194188
}
195189

196190
@Override
197191
public boolean createVolume(long dataStoreId, VolumeDiskType diskType) {
198-
AsyncCallbackDispatcher<VolumeEntityImpl, VolumeApiResult> caller = AsyncCallbackDispatcher.create(this);
199-
caller.setCallback(caller.getTarget().createVolumeCallback(null, null));
200-
vs.createVolumeAsync(volumeInfo, dataStoreId, diskType, caller);
192+
AsyncCallFuture<VolumeApiResult> future = vs.createVolumeAsync(volumeInfo, dataStoreId, diskType);
201193
try {
202-
synchronized (volumeInfo) {
203-
volumeInfo.wait();
204-
}
194+
result = future.get();
205195
if (result.isSuccess()) {
206196
return true;
207197
} else {
208198
throw new CloudRuntimeException("Failed to create volume:" + result.getResult());
209199
}
210200
} catch (InterruptedException e) {
211-
throw new CloudRuntimeException("wait volume info failed", e);
212-
}
213-
}
214-
215-
public Void createVolumeCallback(AsyncCallbackDispatcher<VolumeApiResult, VolumeApiResult> callback, Object context) {
216-
synchronized (volumeInfo) {
217-
this.result = callback.getResult();
218-
volumeInfo.notify();
201+
throw new CloudRuntimeException("wait volume info failed", e);
202+
} catch (ExecutionException e) {
203+
throw new CloudRuntimeException("wait volume failed", e);
219204
}
220-
return null;
221205
}
222206

223-
224207
@Override
225208
public void destroy() {
226-
AsyncCallbackDispatcher<VolumeEntityImpl, VolumeApiResult> caller = AsyncCallbackDispatcher.create(this);
227-
caller.setCallback(caller.getTarget().destroyCallback(null, null));
228-
vs.deleteVolumeAsync(volumeInfo, caller);
209+
AsyncCallFuture<VolumeApiResult> future = vs.deleteVolumeAsync(volumeInfo);
229210
try {
230-
synchronized (volumeInfo) {
231-
volumeInfo.wait();
232-
}
211+
result = future.get();
233212
if (!result.isSuccess()) {
234213
throw new CloudRuntimeException("Failed to create volume:" + result.getResult());
235214
}
236215
} catch (InterruptedException e) {
237-
throw new CloudRuntimeException("wait volume info failed", e);
216+
throw new CloudRuntimeException("wait to delete volume info failed", e);
217+
} catch (ExecutionException e) {
218+
throw new CloudRuntimeException("wait to delete volume failed", e);
238219
}
239220
}
240-
241-
public Void destroyCallback(AsyncCallbackDispatcher<VolumeApiResult, VolumeApiResult> callback, Object context) {
242-
synchronized (volumeInfo) {
243-
this.result = callback.getResult();
244-
volumeInfo.notify();
245-
}
246-
return null;
247-
}
248221

249222
@Override
250223
public Map<String, String> getDetails() {

engine/storage/src/org/apache/cloudstack/storage/volume/VolumeService.java

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222
import org.apache.cloudstack.engine.subsystem.api.storage.VolumeInfo;
2323
import org.apache.cloudstack.engine.subsystem.api.storage.disktype.VolumeDiskType;
2424
import org.apache.cloudstack.engine.subsystem.api.storage.type.VolumeType;
25+
import org.apache.cloudstack.framework.async.AsyncCallFuture;
2526
import org.apache.cloudstack.framework.async.AsyncCompletionCallback;
2627
import org.apache.cloudstack.storage.EndPoint;
2728
import org.apache.cloudstack.storage.command.CommandResult;
@@ -51,7 +52,7 @@ public VolumeInfo getVolume() {
5152
*
5253
* @return the volume object
5354
*/
54-
void createVolumeAsync(VolumeInfo volume, long dataStoreId, VolumeDiskType diskType, AsyncCompletionCallback<VolumeApiResult> callback);
55+
AsyncCallFuture<VolumeApiResult> createVolumeAsync(VolumeInfo volume, long dataStoreId, VolumeDiskType diskType);
5556

5657
/**
5758
* Delete volume
@@ -60,7 +61,7 @@ public VolumeInfo getVolume() {
6061
* @return
6162
* @throws ConcurrentOperationException
6263
*/
63-
void deleteVolumeAsync(VolumeInfo volume, AsyncCompletionCallback<VolumeApiResult> callback);
64+
AsyncCallFuture<VolumeApiResult> deleteVolumeAsync(VolumeInfo volume);
6465

6566
/**
6667
*
@@ -86,6 +87,5 @@ public VolumeInfo getVolume() {
8687

8788
VolumeEntity getVolumeEntity(long volumeId);
8889

89-
void createVolumeFromTemplateAsync(VolumeInfo volume, long dataStoreId, VolumeDiskType diskType, TemplateInfo template,
90-
AsyncCompletionCallback<VolumeApiResult> callback);
90+
AsyncCallFuture<VolumeApiResult> createVolumeFromTemplateAsync(VolumeInfo volume, long dataStoreId, VolumeDiskType diskType, TemplateInfo template);
9191
}

0 commit comments

Comments
 (0)