Skip to content

Commit 780816e

Browse files
hiroki-aistDaanHoogland
authored andcommitted
CLOUDSTACK-7539: [S3] Parallel deployment makes reference count of a cache in nfs secondary staging store negative(-1)
1 parent bcc8182 commit 780816e

1 file changed

Lines changed: 88 additions & 7 deletions

File tree

‎engine/storage/cache/src/org/apache/cloudstack/storage/cache/manager/StorageCacheManagerImpl.java‎

Lines changed: 88 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323
import java.util.List;
2424
import java.util.Map;
2525
import java.util.Random;
26+
import java.util.Date;
2627
import java.util.concurrent.ExecutionException;
2728
import java.util.concurrent.Executors;
2829
import java.util.concurrent.ScheduledExecutorService;
@@ -40,6 +41,7 @@
4041
import org.apache.cloudstack.engine.subsystem.api.storage.DataStore;
4142
import org.apache.cloudstack.engine.subsystem.api.storage.DataStoreManager;
4243
import org.apache.cloudstack.engine.subsystem.api.storage.ObjectInDataStoreStateMachine;
44+
import org.apache.cloudstack.engine.subsystem.api.storage.ObjectInDataStoreStateMachine.State;
4345
import org.apache.cloudstack.engine.subsystem.api.storage.ObjectInDataStoreStateMachine.Event;
4446
import org.apache.cloudstack.engine.subsystem.api.storage.Scope;
4547
import org.apache.cloudstack.engine.subsystem.api.storage.StorageCacheManager;
@@ -60,6 +62,8 @@
6062
import com.cloud.utils.db.SearchCriteria;
6163
import com.cloud.utils.exception.CloudRuntimeException;
6264

65+
import com.cloud.agent.api.to.DataObjectType;
66+
6367
public class StorageCacheManagerImpl implements StorageCacheManager, Manager {
6468
private static final Logger s_logger = Logger.getLogger(StorageCacheManagerImpl.class);
6569
@Inject
@@ -78,6 +82,9 @@ public class StorageCacheManagerImpl implements StorageCacheManager, Manager {
7882
int workers;
7983
ScheduledExecutorService executors;
8084
int cacheReplaceMentInterval;
85+
private static final Object templateLock = new Object();
86+
private static final Object volumeLock = new Object();
87+
private static final Object snapshotLock = new Object();
8188

8289
@Override
8390
public DataStore getCacheStorage(Scope scope) {
@@ -216,15 +223,82 @@ public boolean stop() {
216223

217224
@Override
218225
public DataObject createCacheObject(DataObject data, DataStore store) {
219-
DataObjectInStore obj = objectInStoreMgr.findObject(data, store);
220-
if (obj != null && obj.getState() == ObjectInDataStoreStateMachine.State.Ready) {
221-
s_logger.debug("there is already one in the cache store");
222-
DataObject dataObj = objectInStoreMgr.get(data, store);
223-
dataObj.incRefCount();
224-
return dataObj;
226+
DataObject objOnCacheStore;
227+
final Object lock;
228+
final DataObjectType type = data.getType();
229+
final String typeName;
230+
final DataStoreRole role = store.getRole();
231+
final long storeId = store.getId();
232+
final long dataId = data.getId();
233+
234+
/*
235+
* Make sure any thread knows own lock type.
236+
*/
237+
if (type == DataObjectType.TEMPLATE) {
238+
lock = templateLock;
239+
typeName = "template";
240+
} else if (type == DataObjectType.VOLUME) {
241+
lock = volumeLock;
242+
typeName = "volume";
243+
} else if (type == DataObjectType.SNAPSHOT) {
244+
lock = snapshotLock;
245+
typeName = "snapshot";
246+
} else {
247+
String msg = "unsupported DataObject comes, then can't acquire correct lock object";
248+
throw new CloudRuntimeException(msg);
225249
}
250+
s_logger.debug("check " + typeName + " cache entry(id: " + dataId + ") on store(id: " + storeId + ")");
251+
252+
synchronized (lock) {
253+
DataObjectInStore obj = objectInStoreMgr.findObject(data, store);
254+
if (obj != null) {
255+
State st = obj.getState();
256+
257+
long miliSeconds = 10000;
258+
long timeoutSeconds = 3600;
259+
long timeoutMiliSeconds = timeoutSeconds * 1000;
260+
Date now = new Date();
261+
long expiredEpoch = now.getTime() + timeoutMiliSeconds;
262+
Date expiredDate = new Date(expiredEpoch);
263+
264+
/*
265+
* Waiting for completion of cache copy.
266+
*/
267+
while (st == ObjectInDataStoreStateMachine.State.Allocated ||
268+
st == ObjectInDataStoreStateMachine.State.Creating ||
269+
st == ObjectInDataStoreStateMachine.State.Copying) {
270+
271+
/*
272+
* Threads must release lock within waiting for cache copy and
273+
* must be waken up at completion.
274+
*/
275+
s_logger.debug("waiting cache copy completion type: " + typeName + ", id: " + obj.getObjectId() + ", lock: " + lock.hashCode());
276+
try {
277+
lock.wait(miliSeconds);
278+
} catch (InterruptedException e) {}
279+
s_logger.debug("waken up");
280+
281+
now = new Date();
282+
if (now.after(expiredDate)) {
283+
String msg = "Waiting time exceeds timeout limit(" + timeoutSeconds + " s)";
284+
throw new CloudRuntimeException(msg);
285+
}
226286

227-
DataObject objOnCacheStore = store.create(data);
287+
obj = objectInStoreMgr.findObject(data, store);
288+
st = obj.getState();
289+
}
290+
291+
if (st == ObjectInDataStoreStateMachine.State.Ready) {
292+
s_logger.debug("there is already one in the cache store");
293+
DataObject dataObj = objectInStoreMgr.get(data, store);
294+
dataObj.incRefCount();
295+
return dataObj;
296+
}
297+
}
298+
299+
s_logger.debug("create " + typeName + " cache entry(id: " + dataId + ") on store(id: " + storeId + ")");
300+
objOnCacheStore = store.create(data);
301+
}
228302

229303
AsyncCallFuture<CopyCommandResult> future = new AsyncCallFuture<CopyCommandResult>();
230304
CopyCommandResult result = null;
@@ -251,6 +325,13 @@ public DataObject createCacheObject(DataObject data, DataStore store) {
251325
if (result == null) {
252326
objOnCacheStore.processEvent(Event.OperationFailed);
253327
}
328+
synchronized (lock) {
329+
/*
330+
* Wake up all threads waiting for cache copy.
331+
*/
332+
s_logger.debug("wake up all waiting threads(lock: " + lock.hashCode() + ")");
333+
lock.notifyAll();
334+
}
254335
}
255336
return null;
256337
}

0 commit comments

Comments
 (0)