-
Notifications
You must be signed in to change notification settings - Fork 50
INTERNAL: make piped insert operations process synchronously #795
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. Weโll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -38,6 +38,7 @@ | |
| import java.util.concurrent.TimeUnit; | ||
| import java.util.concurrent.atomic.AtomicBoolean; | ||
| import java.util.concurrent.atomic.AtomicInteger; | ||
| import java.util.function.BiFunction; | ||
| import java.util.jar.JarFile; | ||
| import java.util.jar.Manifest; | ||
|
|
||
|
|
@@ -1783,7 +1784,7 @@ public <T> CollectionFuture<Map<Integer, CollectionOperationStatus>> asyncBopPip | |
| insertList.add(new BTreePipedInsert<>(key, elementMap, attributesForCreate, tc)); | ||
| } | ||
| } | ||
| return asyncCollectionPipedInsert(key, insertList); | ||
| return syncCollectionPipedInsert(key, insertList); | ||
| } | ||
|
|
||
| @Override | ||
|
|
@@ -1806,7 +1807,7 @@ public <T> CollectionFuture<Map<Integer, CollectionOperationStatus>> asyncBopPip | |
| insertList.add(new ByteArraysBTreePipedInsert<>(key, elementList, attributesForCreate, tc)); | ||
| } | ||
| } | ||
| return asyncCollectionPipedInsert(key, insertList); | ||
| return syncCollectionPipedInsert(key, insertList); | ||
| } | ||
|
|
||
| @Override | ||
|
|
@@ -1833,7 +1834,7 @@ public <T> CollectionFuture<Map<Integer, CollectionOperationStatus>> asyncMopPip | |
| insertList.add(new MapPipedInsert<>(key, elementMap, attributesForCreate, tc)); | ||
| } | ||
| } | ||
| return asyncCollectionPipedInsert(key, insertList); | ||
| return syncCollectionPipedInsert(key, insertList); | ||
| } | ||
|
|
||
| @Override | ||
|
|
@@ -1859,7 +1860,7 @@ public <T> CollectionFuture<Map<Integer, CollectionOperationStatus>> asyncLopPip | |
| } | ||
| } | ||
| } | ||
| return asyncCollectionPipedInsert(key, insertList); | ||
| return syncCollectionPipedInsert(key, insertList); | ||
| } | ||
|
|
||
| @Override | ||
|
|
@@ -1882,7 +1883,7 @@ public <T> CollectionFuture<Map<Integer, CollectionOperationStatus>> asyncSopPip | |
| insertList.add(new SetPipedInsert<>(key, elementList, attributesForCreate, tc)); | ||
| } | ||
| } | ||
| return asyncCollectionPipedInsert(key, insertList); | ||
| return syncCollectionPipedInsert(key, insertList); | ||
| } | ||
|
|
||
| @Override | ||
|
|
@@ -3106,6 +3107,78 @@ public void gotStatus(Integer index, OperationStatus status) { | |
| return rv; | ||
| } | ||
|
|
||
| /** | ||
| * insert items into collection synchronously. | ||
| * | ||
| * @param key arcus cache key | ||
| * @param insertList must not be empty. | ||
| * @return future holding the map of element index and the reason why insert operation failed | ||
| */ | ||
| private <T> CollectionFuture<Map<Integer, CollectionOperationStatus>> syncCollectionPipedInsert( | ||
| final String key, final List<CollectionPipedInsert<T>> insertList) { | ||
| final CountDownLatch latch = new CountDownLatch(1); | ||
| final PipedCollectionFuture<Integer, CollectionOperationStatus> rv = | ||
| new PipedCollectionFuture<>(latch, operationTimeout); | ||
|
|
||
| BiFunction<Integer, Integer, OperationCallback> makeCallback = (opIdx, itemCount) -> new PipedOperationCallback() { | ||
|
|
||
| private int currentItemIdx = -1; | ||
|
|
||
| public void receivedStatus(OperationStatus status) { | ||
| CollectionOperationStatus cstatus; | ||
|
|
||
| if (status instanceof CollectionOperationStatus) { | ||
| cstatus = (CollectionOperationStatus) status; | ||
| } else { | ||
| getLogger().warn("Unhandled state: " + status); | ||
| cstatus = new CollectionOperationStatus(status); | ||
| } | ||
| rv.setOperationStatus(cstatus); | ||
| } | ||
|
|
||
| public void complete() { | ||
| if (rv.getOperationStatus().isSuccess()) { | ||
| Operation nextOp = rv.getNextOp(); | ||
| if (nextOp != null && !nextOp.isCancelled()) { | ||
| addOp(key, nextOp); | ||
| rv.incrCurrentOpIdx(); | ||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. ์ด์ ์ฝ๋ฉํธ๋ฅผ ๊ฐ์ ํ๋ฉด, ์์ |
||
| return; | ||
| } | ||
| } else { | ||
oliviarla marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| // If this operation has been errored or cancelled, some items in the operation may not be executed. | ||
| // The first item that is not executed will have the STOPPED flag. | ||
| if ((currentItemIdx + 1) < itemCount || (opIdx + 1 < insertList.size())) { | ||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
|
||
| this.gotStatus(currentItemIdx + 1, | ||
| new CollectionOperationStatus(false, "STOPPED", CollectionResponse.STOPPED)); | ||
| } | ||
oliviarla marked this conversation as resolved.
Show resolved
Hide resolved
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @oliviarla
Collaborator
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
์ด ๊ฒฝ์ฐ๋ ๋ฐ์ํ์ง ์์ ๊ฒ ๊ฐ์ต๋๋ค. gotStatus๊ฐ ํธ์ถ๋๋ ค๋ฉด op๊ฐ ์ด๋ฏธ node์ queue์ ์ถ๊ฐ๋์ด ์์ด์ผ ํ๋๋ฐ, NOT_EXECUTED๊ฐ ์ถ๊ฐ๋๋ ์์ ์๋ op๊ฐ node์ queue์ ์์ ์ ์์ผ๋ฏ๋ก gotStatus ์ญ์ ํธ์ถ๋ ์ ์์ต๋๋ค.
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
์ด ๋ถ๋ถ์ ๊ดํ ์ฝ๋ฉํธํฉ๋๋ค. ํด๋น op๊ฐ readQ์ ์๋ ์ํ์์ cancel ๋์๋ค๊ณ ๊ฐ์ ํฉ๋๋ค. ๋ฐ๋ผ์, ํด๋น op ์ํ์ ๋ฐ๋ผ ํ๋จํด์ผ ํ ๊ฒ ๊ฐ์ต๋๋ค. ๊ฒํ ๋ฐ๋๋๋ค.
๊ทธ๋ฆฌ๊ณ , currentItemIdx => processedItemIdx ์ฉ์ด๊ฐ ๋์ ๊ฒ ๊ฐ์ต๋๋ค.
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @oliviarla
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @oliviarla |
||
| } | ||
| latch.countDown(); | ||
| } | ||
oliviarla marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
|
||
| public void gotStatus(Integer index, OperationStatus status) { | ||
| if (!status.isSuccess()) { | ||
| if (status instanceof CollectionOperationStatus) { | ||
| rv.addEachResult(index + (opIdx * MAX_PIPED_ITEM_COUNT), | ||
| (CollectionOperationStatus) status); | ||
| } else { | ||
| rv.addEachResult(index + (opIdx * MAX_PIPED_ITEM_COUNT), | ||
| new CollectionOperationStatus(status)); | ||
| } | ||
| } | ||
| currentItemIdx = index; | ||
| } | ||
| }; | ||
|
|
||
| for (int i = 0; i < insertList.size(); i++) { | ||
| final CollectionPipedInsert<T> insert = insertList.get(i); | ||
| Operation op = opFact.collectionPipedInsert(key, insert, makeCallback.apply(i, insert.getItemCount())); | ||
| rv.addOperation(op); | ||
| } | ||
| addOp(key, rv.getOperation(0)); | ||
| rv.incrCurrentOpIdx(); | ||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. ์ด์ ์ฝ๋ฉํธ๋ฅผ ๊ฐ์ ํ๊ณ ์๋์ ๊ฐ์ด ํธ์ถํ ์ ์์ต๋๋ค. addOp(key, rv.GetFirstOp()); |
||
| return rv; | ||
| } | ||
|
|
||
| @Override | ||
| public Future<Map<String, CollectionOperationStatus>> asyncBopInsertBulk( | ||
| List<String> keyList, long bkey, byte[] eFlag, Object value, | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -2,6 +2,7 @@ | |
|
|
||
| import java.util.ArrayList; | ||
| import java.util.Collection; | ||
| import java.util.List; | ||
| import java.util.Map; | ||
| import java.util.concurrent.ConcurrentHashMap; | ||
| import java.util.concurrent.CountDownLatch; | ||
|
|
@@ -17,9 +18,10 @@ | |
|
|
||
| public class PipedCollectionFuture<K, V> | ||
| extends CollectionFuture<Map<K, V>> { | ||
| private final Collection<Operation> ops = new ArrayList<>(); | ||
| private final List<Operation> ops = new ArrayList<>(); | ||
| private final AtomicReference<CollectionOperationStatus> operationStatus | ||
| = new AtomicReference<>(null); | ||
| private int currentOpIdx = -1; | ||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. ์ด ๊ฐ์ด -1์ธ ์ํ์์ ์ผ๋ถ ํจ์์์ for ๋ฃจํ๋ฅผ ๋๋ฉด IndexOutOfBoundsException์ด ๋ฐ์ํ๊ฒ ๋ฉ๋๋ค. |
||
|
|
||
| private final Map<K, V> failedResult = | ||
| new ConcurrentHashMap<>(); | ||
|
|
@@ -30,17 +32,21 @@ public PipedCollectionFuture(CountDownLatch l, long opTimeout) { | |
|
|
||
| @Override | ||
| public boolean cancel(boolean ign) { | ||
| boolean rv = false; | ||
| for (Operation op : ops) { | ||
| rv |= op.cancel("by application."); | ||
| for (int i = currentOpIdx; i < ops.size(); i++) { | ||
| if (ops.get(i).cancel("by application.")) { | ||
| return true; | ||
| } | ||
| } | ||
| return rv; | ||
| return false; | ||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. cancel() ํจ์๋ฅผ ์๋ฐํ๊ฒ ์ดํด๋ณด๋,
์๋์ ๊ฐ์ด public boolean cancel(boolean ign) {
if (cancelled) {
return false;
}
for (int i = currentOpIdx; i < ops.size(); i++) {
cancelled = ops.get(i).cancel("by application.");
if (cancelled) {
return true;
}
}
return false;
}
public boolean isCancelled() {
return cancelled;
} |
||
| } | ||
|
|
||
| /** | ||
| * @return true if any operation is cancelled. | ||
| */ | ||
oliviarla marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| @Override | ||
| public boolean isCancelled() { | ||
| for (Operation op : ops) { | ||
| if (op.isCancelled()) { | ||
| for (int i = currentOpIdx; i < ops.size(); i++) { | ||
| if (ops.get(i).isCancelled()) { | ||
| return true; | ||
| } | ||
| } | ||
|
|
@@ -119,4 +125,20 @@ public void addEachResult(K index, V status) { | |
| public void addOperation(Operation op) { | ||
| ops.add(op); | ||
| } | ||
|
|
||
| public Operation getOperation(int idx) { | ||
| return ops.get(idx); | ||
| } | ||
|
|
||
| public void incrCurrentOpIdx() { | ||
| this.currentOpIdx++; | ||
| } | ||
|
|
||
| public Operation getNextOp() { | ||
| try { | ||
| return ops.get(currentOpIdx + 1); | ||
| } catch (IndexOutOfBoundsException e) { | ||
| return null; | ||
| } | ||
| } | ||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. ์๋ ์์์ getFirstOp(), getNextOp()๊ฐ ์์ผ๋ฉด, ๋ค์ method๋ ์ ๊ฑฐํ ์ ์์ต๋๋ค.
public Operation getFirstOp() {
currentOpIdx = 0;
return ops.get(currentOpIdx);
}
public Operation getNextOp() {
if (isCancelled() || (currentOpIdx + 1) >= ops.size()) {
return null;
}
currentOpIdx += 1;
return ops.get(currentOpIdx);
}์ถ๊ฐ ์ฝ๋ฉํธ๋ก,
|
||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,99 @@ | ||
| package net.spy.memcached.protocol.ascii; | ||
|
|
||
| import java.util.List; | ||
|
|
||
| import net.spy.memcached.collection.CollectionPipe; | ||
| import net.spy.memcached.ops.OperationCallback; | ||
| import net.spy.memcached.ops.OperationState; | ||
| import net.spy.memcached.ops.OperationStatus; | ||
|
|
||
| public abstract class SingleKeyPipeOperationImpl extends PipeOperationImpl { | ||
|
|
||
| protected SingleKeyPipeOperationImpl(List<String> keys, | ||
| CollectionPipe collectionPipe, | ||
| OperationCallback cb) { | ||
| super(keys, collectionPipe, cb); | ||
| } | ||
|
|
||
| @Override | ||
| public void handleLine(String line) { | ||
| assert getState() == OperationState.READING | ||
| : "Read ``" + line + "'' when in " + getState() + " state"; | ||
|
|
||
| /* ENABLE_REPLICATION if */ | ||
| if (isWriteOperation() && hasSwitchedOver(line)) { | ||
| collectionPipe.setNextOpIndex(index); | ||
| prepareSwitchover(line); | ||
| return; | ||
| } | ||
| /* ENABLE_REPLICATION end */ | ||
|
|
||
| /* ENABLE_MIGRATION if */ | ||
| if (hasNotMyKey(line)) { | ||
| // Only one NOT_MY_KEY is provided in response of | ||
| // single key piped operation when redirection. | ||
uhm0311 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| addRedirectSingleKeyOperation(line, keys.get(0)); | ||
| if (collectionPipe.isNotPiped()) { | ||
| transitionState(OperationState.REDIRECT); | ||
| } else { | ||
| collectionPipe.setNextOpIndex(index); | ||
| } | ||
| return; | ||
| } | ||
| /* ENABLE_MIGRATION end */ | ||
|
|
||
| if (collectionPipe.isNotPiped()) { | ||
| OperationStatus status = checkStatus(line); | ||
| if (!status.isSuccess()) { | ||
| successAll = false; | ||
| } | ||
| cb.gotStatus(index, status); | ||
|
|
||
| cb.receivedStatus((successAll) ? END : FAILED_END); | ||
| transitionState(OperationState.COMPLETE); | ||
| return; | ||
| } | ||
|
|
||
| /* | ||
| RESPONSE <count>\r\n | ||
| <status of the 1st pipelined command>\r\n | ||
| [ ... ] | ||
| <status of the last pipelined command>\r\n | ||
| END|PIPE_ERROR <error_string>\r\n | ||
| */ | ||
| if (line.startsWith("END") || line.startsWith("PIPE_ERROR ")) { | ||
| /* ENABLE_MIGRATION if */ | ||
| if (needRedirect()) { | ||
| transitionState(OperationState.REDIRECT); | ||
| return; | ||
| } | ||
| /* ENABLE_MIGRATION end */ | ||
| cb.receivedStatus((index == collectionPipe.getItemCount() && successAll) ? END : FAILED_END); | ||
| transitionState(OperationState.COMPLETE); | ||
| } else if (line.startsWith("RESPONSE ")) { | ||
| getLogger().debug("Got line %s", line); | ||
|
|
||
| // TODO server should be fixed | ||
| line = line.replace(" ", " "); | ||
| line = line.replace(" ", " "); | ||
|
|
||
| String[] stuff = line.split(" "); | ||
| assert "RESPONSE".equals(stuff[0]); | ||
| readUntilLastLine = true; | ||
| } else { | ||
| OperationStatus status = checkStatus(line); | ||
| if (!status.isSuccess()) { | ||
| successAll = false; | ||
| } | ||
| cb.gotStatus(index, status); | ||
|
|
||
| index++; | ||
| } | ||
| } | ||
|
|
||
| @Override | ||
| protected OperationStatus checkStatus(String line) { | ||
| return null; | ||
| } | ||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. checkStatus() ๊ดํ ์ง๋ฌธ์
๋๋ค. |
||
|
|
||
| } | ||
Uh oh!
There was an error while loading. Please reload this page.