use of org.apache.geode.internal.cache.partitioned.LockObject in project geode by apache.
the class BucketRegion method removeAndNotifyKeys.
/**
* After processed the keys, this method will remove them from CM. And notifyAll for each key. The
* thread needs to acquire lock of CM first.
*
* @param keys
*/
public void removeAndNotifyKeys(Object[] keys) {
final boolean isTraceEnabled = logger.isTraceEnabled();
synchronized (allKeysMap) {
for (int i = 0; i < keys.length; i++) {
LockObject lockValue = (LockObject) allKeysMap.remove(keys[i]);
if (lockValue != null) {
// let current thread become the monitor of the key object
synchronized (lockValue) {
lockValue.setRemoved();
if (isTraceEnabled) {
long waitTime = System.currentTimeMillis() - lockValue.lockedTimeStamp;
logger.trace("LockKeys: remove key {}, notifyAll for {}. It waited", keys[i], lockValue, waitTime);
}
if (lockValue.isSomeoneWaiting()) {
lockValue.notifyAll();
}
}
}
}
// for
}
}
use of org.apache.geode.internal.cache.partitioned.LockObject in project geode by apache.
the class BucketRegion method searchAndLock.
/**
* Search the CM for keys. If found any, return the first found one Otherwise, save the keys into
* the CM, and return null The thread will acquire the lock before searching.
*
* @param keys
* @return first key found in CM null means not found
*/
private LockObject searchAndLock(Object[] keys) {
final boolean isDebugEnabled = logger.isDebugEnabled();
LockObject foundLock = null;
synchronized (allKeysMap) {
// check if there's any key in map
for (int i = 0; i < keys.length; i++) {
if (allKeysMap.containsKey(keys[i])) {
foundLock = (LockObject) allKeysMap.get(keys[i]);
if (isDebugEnabled) {
logger.debug("LockKeys: found key: {}:{}", keys[i], foundLock.lockedTimeStamp);
}
foundLock.waiting();
break;
}
}
// save the keys when still locked
if (foundLock == null) {
for (int i = 0; i < keys.length; i++) {
LockObject lockValue = new LockObject(keys[i], isDebugEnabled ? System.currentTimeMillis() : 0);
allKeysMap.put(keys[i], lockValue);
if (isDebugEnabled) {
logger.debug("LockKeys: add key: {}:{}", keys[i], lockValue.lockedTimeStamp);
}
}
}
}
return foundLock;
}
use of org.apache.geode.internal.cache.partitioned.LockObject in project geode by apache.
the class BucketRegion method waitUntilLocked.
/**
* Keep checking if CM has contained any key in keys. If yes, wait for notify, then retry again.
* This method will block current thread for long time. It only exits when current thread
* successfully save its keys into CM.
*
* @param keys
*/
public void waitUntilLocked(Object[] keys) {
final boolean isDebugEnabled = logger.isDebugEnabled();
final String title = "BucketRegion.waitUntilLocked:";
while (true) {
LockObject foundLock = searchAndLock(keys);
if (foundLock != null) {
synchronized (foundLock) {
try {
while (!foundLock.isRemoved()) {
this.partitionedRegion.checkReadiness();
foundLock.wait(1000);
// primary could be changed by prRebalancing while waiting here
checkForPrimary();
}
} catch (InterruptedException e) {
// TODO this isn't a localizable string and it's being logged at info level
if (isDebugEnabled) {
logger.debug("{} interrupted while waiting for {}", title, foundLock, e.getMessage());
}
}
if (isDebugEnabled) {
long waitTime = System.currentTimeMillis() - foundLock.lockedTimeStamp;
logger.debug("{} waited {} ms to lock", title, waitTime, foundLock);
}
}
} else {
// now the keys have been locked by this thread
break;
}
// to lock and process
}
// while
}
Aggregations