From 18c4958e79a1fbdeddff533bf4641f5aadee8a2f Mon Sep 17 00:00:00 2001 From: brido4125 Date: Tue, 19 Sep 2023 10:52:03 +0900 Subject: [PATCH] ENHANCE: Remove redundant lock and sync tool. --- .../java/net/spy/memcached/ArcusClient.java | 341 ++++++++---------- 1 file changed, 158 insertions(+), 183 deletions(-) diff --git a/src/main/java/net/spy/memcached/ArcusClient.java b/src/main/java/net/spy/memcached/ArcusClient.java index 4b7ca26a2..89b00d853 100644 --- a/src/main/java/net/spy/memcached/ArcusClient.java +++ b/src/main/java/net/spy/memcached/ArcusClient.java @@ -43,7 +43,6 @@ import java.util.concurrent.TimeoutException; import java.util.concurrent.atomic.AtomicBoolean; import java.util.concurrent.atomic.AtomicInteger; -import java.util.concurrent.locks.ReentrantLock; import java.util.jar.JarFile; import java.util.jar.Manifest; @@ -2110,23 +2109,19 @@ private SMGetFuture>> smget( final CountDownLatch blatch = new CountDownLatch(smGetList.size()); final ConcurrentLinkedQueue ops = new ConcurrentLinkedQueue(); - final List missedKeyList = Collections.synchronizedList(new ArrayList()); - final Map missedKeys = - Collections.synchronizedMap(new HashMap()); - final List mergedTrimmedKeys = - Collections.synchronizedList(new ArrayList()); + final List missedKeyList + = Collections.synchronizedList(new ArrayList()); + final Map missedKeys + = Collections.synchronizedMap(new HashMap()); + final List mergedTrimmedKeys + = Collections.synchronizedList(new ArrayList()); final int totalResultElementCount = count + offset; - final List> mergedResult = - Collections.synchronizedList(new ArrayList>(totalResultElementCount)); - - final ReentrantLock lock = new ReentrantLock(); - - final List resultOperationStatus = - Collections.synchronizedList(new ArrayList(1)); - - final List failedOperationStatus = - Collections.synchronizedList(new ArrayList(1)); + final List> mergedResult = new ArrayList>(totalResultElementCount); + final List resultOperationStatus + = Collections.synchronizedList(new ArrayList(1)); + final List failedOperationStatus + = Collections.synchronizedList(new ArrayList(1)); // if processedSMGetCount is 0, then all smget is done. final AtomicInteger processedSMGetCount = new AtomicInteger(smGetList.size()); @@ -2154,76 +2149,71 @@ public void receivedStatus(OperationStatus status) { boolean isTrimmed = (TRIMMED.equals(status.getMessage()) || DUPLICATED_TRIMMED.equals(status.getMessage())) ? true : false; - lock.lock(); - try { - if (mergedResult.size() == 0) { - // merged result is empty, add all. - mergedResult.addAll(eachResult); - mergedTrim.set(isTrimmed); - } else { - boolean addAll = true; - int pos = 0; - for (SMGetElement result : eachResult) { - for (; pos < mergedResult.size(); pos++) { - if ((reverse) ? (0 < result.compareTo(mergedResult.get(pos))) - : (0 > result.compareTo(mergedResult.get(pos)))) { - break; - } - } - if (pos >= totalResultElementCount) { - addAll = false; - break; - } - if (pos >= mergedResult.size() && mergedTrim.get() && - result.compareBkeyTo(mergedResult.get(pos - 1)) != 0) { - addAll = false; + if (mergedResult.size() == 0) { + // merged result is empty, add all. + mergedResult.addAll(eachResult); + mergedTrim.set(isTrimmed); + } else { + boolean addAll = true; + int pos = 0; + for (SMGetElement result : eachResult) { + for (; pos < mergedResult.size(); pos++) { + if ((reverse) ? (0 < result.compareTo(mergedResult.get(pos))) + : (0 > result.compareTo(mergedResult.get(pos)))) { break; } - mergedResult.add(pos, result); - if (mergedResult.size() > totalResultElementCount) { - mergedResult.remove(totalResultElementCount); - } - pos += 1; } - if (isTrimmed && addAll) { - while (pos < mergedResult.size()) { - if (mergedResult.get(pos).compareBkeyTo(mergedResult.get(pos - 1)) == 0) { - pos += 1; - } else { - mergedResult.remove(pos); - } - } - mergedTrim.set(true); + if (pos >= totalResultElementCount) { + addAll = false; + break; } - if (mergedResult.size() >= totalResultElementCount) { - mergedTrim.set(false); + if (pos >= mergedResult.size() && mergedTrim.get() && + result.compareBkeyTo(mergedResult.get(pos - 1)) != 0) { + addAll = false; + break; } - } - - if (processedSMGetCount.get() == 0) { - boolean isDuplicated = false; - for (int i = 1; i < mergedResult.size(); i++) { - if (mergedResult.get(i).compareBkeyTo(mergedResult.get(i - 1)) == 0) { - isDuplicated = true; - break; - } + mergedResult.add(pos, result); + if (mergedResult.size() > totalResultElementCount) { + mergedResult.remove(totalResultElementCount); } - if (mergedTrim.get()) { - if (isDuplicated) { - resultOperationStatus.add(new OperationStatus(true, "DUPLICATED_TRIMMED")); + pos += 1; + } + if (isTrimmed && addAll) { + while (pos < mergedResult.size()) { + if (mergedResult.get(pos).compareBkeyTo(mergedResult.get(pos - 1)) == 0) { + pos += 1; } else { - resultOperationStatus.add(new OperationStatus(true, "TRIMMED")); + mergedResult.remove(pos); } + } + mergedTrim.set(true); + } + if (mergedResult.size() >= totalResultElementCount) { + mergedTrim.set(false); + } + } + + if (processedSMGetCount.get() == 0) { + boolean isDuplicated = false; + for (int i = 1; i < mergedResult.size(); i++) { + if (mergedResult.get(i).compareBkeyTo(mergedResult.get(i - 1)) == 0) { + isDuplicated = true; + break; + } + } + if (mergedTrim.get()) { + if (isDuplicated) { + resultOperationStatus.add(new OperationStatus(true, "DUPLICATED_TRIMMED")); } else { - if (isDuplicated) { - resultOperationStatus.add(new OperationStatus(true, "DUPLICATED")); - } else { - resultOperationStatus.add(new OperationStatus(true, "END")); - } + resultOperationStatus.add(new OperationStatus(true, "TRIMMED")); + } + } else { + if (isDuplicated) { + resultOperationStatus.add(new OperationStatus(true, "DUPLICATED")); + } else { + resultOperationStatus.add(new OperationStatus(true, "END")); } } - } finally { - lock.unlock(); } } @@ -2327,31 +2317,21 @@ private SMGetFuture>> smget( final List> smGetList, final int count, final boolean reverse, final Transcoder tc, final SMGetMode smgetMode) { - final String END = "END"; - final String TRIMMED = "TRIMMED"; - final String DUPLICATED = "DUPLICATED"; - final String DUPLICATED_TRIMMED = "DUPLICATED_TRIMMED"; - final CountDownLatch blatch = new CountDownLatch(smGetList.size()); final ConcurrentLinkedQueue ops = new ConcurrentLinkedQueue(); - final List missedKeyList = - Collections.synchronizedList(new ArrayList()); - final Map missedKeys = - Collections.synchronizedMap(new HashMap()); + final List missedKeyList + = Collections.synchronizedList(new ArrayList()); + final Map missedKeys + = Collections.synchronizedMap(new HashMap()); final int totalResultElementCount = count; - final List> mergedResult = - Collections.synchronizedList(new ArrayList>(totalResultElementCount)); - final List mergedTrimmedKeys = - Collections.synchronizedList(new ArrayList()); - - final ReentrantLock lock = new ReentrantLock(); - - final List resultOperationStatus = - Collections.synchronizedList(new ArrayList(1)); + final List> mergedResult = new ArrayList>(totalResultElementCount); + final List mergedTrimmedKeys = new ArrayList(); - final List failedOperationStatus = - Collections.synchronizedList(new ArrayList(1)); + final List resultOperationStatus + = Collections.synchronizedList(new ArrayList(1)); + final List failedOperationStatus + = Collections.synchronizedList(new ArrayList(1)); final AtomicBoolean stopCollect = new AtomicBoolean(false); // if processedSMGetCount is 0, then all smget is done. @@ -2377,114 +2357,109 @@ public void receivedStatus(OperationStatus status) { return; } - lock.lock(); - try { - if (mergedResult.size() == 0) { - // merged result is empty, add all. - mergedResult.addAll(eachResult); - } else { - // do sort merge - boolean duplicated; - int comp, pos = 0; - for (SMGetElement result : eachResult) { - duplicated = false; - for (; pos < mergedResult.size(); pos++) { - // compare b+tree key - comp = result.compareBkeyTo(mergedResult.get(pos)); + if (mergedResult.isEmpty()) { + // merged result is empty, add all. + mergedResult.addAll(eachResult); + } else { + // do sort merge + boolean duplicated; + int comp, pos = 0; + for (SMGetElement result : eachResult) { + duplicated = false; + for (; pos < mergedResult.size(); pos++) { + // compare b+tree key + comp = result.compareBkeyTo(mergedResult.get(pos)); + if ((reverse) ? (0 < comp) : (0 > comp)) { + break; + } + if (comp == 0) { // compare key string + comp = result.compareKeyTo(mergedResult.get(pos)); if ((reverse) ? (0 < comp) : (0 > comp)) { + if (smgetMode == SMGetMode.UNIQUE) { + mergedResult.remove(pos); // remove dup bkey + } break; - } - if (comp == 0) { // compare key string - comp = result.compareKeyTo(mergedResult.get(pos)); - if ((reverse) ? (0 < comp) : (0 > comp)) { - if (smgetMode == SMGetMode.UNIQUE) { - mergedResult.remove(pos); // remove dup bkey - } + } else { + if (smgetMode == SMGetMode.UNIQUE) { + duplicated = true; break; - } else { - if (smgetMode == SMGetMode.UNIQUE) { - duplicated = true; - break; - } } } } - if (duplicated) { // UNIQUE - continue; - } - if (pos >= totalResultElementCount) { - // At this point, following conditions are met. - // - mergedResult.size() == totalResultElementCount && - // - The current of eachResult is - // behind of the last of mergedResult. - // Then, all the next elements of eachResult are - // definitely behind of the last of mergedResult. - // So, stop the current sort-merge. - break; - } + } + if (duplicated) { // UNIQUE + continue; + } + if (pos >= totalResultElementCount) { + // At this point, following conditions are met. + // - mergedResult.size() == totalResultElementCount && + // - The current of eachResult is + // behind of the last of mergedResult. + // Then, all the next elements of eachResult are + // definitely behind of the last of mergedResult. + // So, stop the current sort-merge. + break; + } - mergedResult.add(pos, result); - if (mergedResult.size() > totalResultElementCount) { - mergedResult.remove(totalResultElementCount); - } - pos += 1; + mergedResult.add(pos, result); + if (mergedResult.size() > totalResultElementCount) { + mergedResult.remove(totalResultElementCount); } + pos += 1; } + } - if (eachTrimmedResult.size() > 0) { - if (mergedTrimmedKeys.size() == 0) { - mergedTrimmedKeys.addAll(eachTrimmedResult); - } else { - // do sort merge trimmed list - int pos = 0; - for (SMGetTrimKey result : eachTrimmedResult) { - for (; pos < mergedTrimmedKeys.size(); pos++) { - if ((reverse) ? (0 < result.compareTo(mergedTrimmedKeys.get(pos))) - : (0 > result.compareTo(mergedTrimmedKeys.get(pos)))) { - break; - } + if (eachTrimmedResult.size() > 0) { + if (mergedTrimmedKeys.size() == 0) { + mergedTrimmedKeys.addAll(eachTrimmedResult); + } else { + // do sort merge trimmed list + int pos = 0; + for (SMGetTrimKey result : eachTrimmedResult) { + for (; pos < mergedTrimmedKeys.size(); pos++) { + if ((reverse) ? (0 < result.compareTo(mergedTrimmedKeys.get(pos))) + : (0 > result.compareTo(mergedTrimmedKeys.get(pos)))) { + break; } - mergedTrimmedKeys.add(pos, result); - pos += 1; } + mergedTrimmedKeys.add(pos, result); + pos += 1; } } + } - if (processedSMGetCount.get() == 0) { - if (mergedTrimmedKeys.size() > 0 && count <= mergedResult.size()) { - // remove trimed keys whose bkeys are behind of the last element. - SMGetElement lastElement = mergedResult.get(mergedResult.size() - 1); - SMGetTrimKey lastTrimKey = new SMGetTrimKey(lastElement.getKey(), - lastElement.getBkeyObject()); - for (int i = mergedTrimmedKeys.size() - 1; i >= 0; i--) { - SMGetTrimKey me = mergedTrimmedKeys.get(i); - if ((reverse) ? (0 >= me.compareTo(lastTrimKey)) - : (0 <= me.compareTo(lastTrimKey))) { - mergedTrimmedKeys.remove(i); - } else { - break; - } + if (processedSMGetCount.get() == 0) { + if (mergedTrimmedKeys.size() > 0 && count <= mergedResult.size()) { + // remove trimed keys whose bkeys are behind of the last element. + SMGetElement lastElement = mergedResult.get(mergedResult.size() - 1); + SMGetTrimKey lastTrimKey = new SMGetTrimKey(lastElement.getKey(), + lastElement.getBkeyObject()); + for (int i = mergedTrimmedKeys.size() - 1; i >= 0; i--) { + SMGetTrimKey me = mergedTrimmedKeys.get(i); + if ((reverse) ? (0 >= me.compareTo(lastTrimKey)) + : (0 <= me.compareTo(lastTrimKey))) { + mergedTrimmedKeys.remove(i); + } else { + break; } } - if (smgetMode == SMGetMode.UNIQUE) { - resultOperationStatus.add(new OperationStatus(true, "END")); - } else { - boolean isDuplicated = false; - for (int i = 1; i < mergedResult.size(); i++) { - if (mergedResult.get(i).compareBkeyTo(mergedResult.get(i - 1)) == 0) { - isDuplicated = true; - break; - } - } - if (isDuplicated) { - resultOperationStatus.add(new OperationStatus(true, "DUPLICATED")); - } else { - resultOperationStatus.add(new OperationStatus(true, "END")); + } + if (smgetMode == SMGetMode.UNIQUE) { + resultOperationStatus.add(new OperationStatus(true, "END")); + } else { + boolean isDuplicated = false; + for (int i = 1; i < mergedResult.size(); i++) { + if (mergedResult.get(i).compareBkeyTo(mergedResult.get(i - 1)) == 0) { + isDuplicated = true; + break; } } + if (isDuplicated) { + resultOperationStatus.add(new OperationStatus(true, "DUPLICATED")); + } else { + resultOperationStatus.add(new OperationStatus(true, "END")); + } } - } finally { - lock.unlock(); } }