2020
2121import java .util .List ;
2222import java .util .Map ;
23+ import java .util .concurrent .ExecutionException ;
2324
2425import javax .inject .Inject ;
2526import javax .naming .ConfigurationException ;
2627
28+ import org .apache .cloudstack .engine .subsystem .api .storage .CommandResult ;
2729import org .apache .cloudstack .engine .subsystem .api .storage .DataMotionService ;
2830import org .apache .cloudstack .engine .subsystem .api .storage .DataObject ;
2931import org .apache .cloudstack .engine .subsystem .api .storage .DataStore ;
32+ import org .apache .cloudstack .engine .subsystem .api .storage .ObjectInDataStoreStateMachine .Event ;
3033import org .apache .cloudstack .engine .subsystem .api .storage .Scope ;
3134import org .apache .cloudstack .engine .subsystem .api .storage .StorageCacheManager ;
35+ import org .apache .cloudstack .framework .async .AsyncCallFuture ;
36+ import org .apache .cloudstack .framework .async .AsyncCallbackDispatcher ;
37+ import org .apache .cloudstack .framework .async .AsyncCompletionCallback ;
38+ import org .apache .cloudstack .framework .async .AsyncRpcConext ;
3239import org .apache .cloudstack .storage .cache .allocator .StorageCacheAllocator ;
40+ import org .apache .log4j .Logger ;
3341
3442import com .cloud .utils .component .Manager ;
3543
3644public class StorageCacheManagerImpl implements StorageCacheManager , Manager {
45+ private static final Logger s_logger = Logger
46+ .getLogger (StorageCacheManagerImpl .class );
3747 @ Inject
3848 List <StorageCacheAllocator > storageCacheAllocator ;
3949 @ Inject
@@ -101,14 +111,62 @@ public boolean stop() {
101111 // TODO Auto-generated method stub
102112 return true ;
103113 }
114+
115+
116+
117+ private class CreateCacheObjectContext <T > extends AsyncRpcConext <T > {
118+ final AsyncCallFuture <CommandResult > future ;
119+ /**
120+ * @param callback
121+ */
122+ public CreateCacheObjectContext (AsyncCompletionCallback <T > callback , AsyncCallFuture <CommandResult > future ) {
123+ super (callback );
124+ this .future = future ;
125+ }
126+
127+ }
104128
105129 @ Override
106130 public DataObject createCacheObject (DataObject data , Scope scope ) {
107131 DataStore cacheStore = this .getCacheStorage (scope );
108132 DataObject objOnCacheStore = cacheStore .create (data );
109- //AsyncCallFuture<>
110- //dataMotionSvr.copyAsync(data, objOnCacheStore, callback);
111- // TODO Auto-generated method stub
133+ AsyncCallFuture <CommandResult > future = new AsyncCallFuture <CommandResult >();
134+ CreateCacheObjectContext <CommandResult > context = new CreateCacheObjectContext <CommandResult >(null , future );
135+ AsyncCallbackDispatcher <StorageCacheManagerImpl , CommandResult > caller = AsyncCallbackDispatcher .create (this );
136+ caller .setContext (context );
137+
138+
139+ CommandResult result = null ;
140+ try {
141+ objOnCacheStore .processEvent (Event .CreateOnlyRequested );
142+
143+ dataMotionSvr .copyAsync (data , objOnCacheStore , caller );
144+ result = future .get ();
145+
146+ if (result .isFailed ()) {
147+ cacheStore .delete (data );
148+ } else {
149+ objOnCacheStore .processEvent (Event .OperationSuccessed );
150+ }
151+ } catch (InterruptedException e ) {
152+ s_logger .debug ("create cache storage failed: " + e .toString ());
153+ } catch (ExecutionException e ) {
154+ s_logger .debug ("create cache storage failed: " + e .toString ());
155+ } catch (Exception e ) {
156+ s_logger .debug ("create cache storage failed: " + e .toString ());
157+ } finally {
158+ if (result == null ) {
159+ cacheStore .delete (data );
160+ }
161+ }
162+
112163 return null ;
113164 }
165+
166+ protected Void createCacheObjectCallBack (AsyncCallbackDispatcher <StorageCacheManagerImpl , CommandResult > callback ,
167+ CreateCacheObjectContext <CommandResult > context ) {
168+ AsyncCallFuture <CommandResult > future = context .future ;
169+ future .complete (callback .getResult ());
170+ return null ;
171+ }
114172}
0 commit comments