-
Notifications
You must be signed in to change notification settings - Fork 49
FEATURE: Add CompletableFuture Pipeline API #1035
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
Open
oliviarla
wants to merge
1
commit into
naver:develop
Choose a base branch
from
oliviarla:pipenewapi
base: develop
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
31 changes: 31 additions & 0 deletions
31
src/main/java/net/spy/memcached/ops/MultiPipelineOperationCallback.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,31 @@ | ||
| /* | ||
| * arcus-java-client : Arcus Java client | ||
| * Copyright 2010-2014 NAVER Corp. | ||
| * Copyright 2014-present JaM2in Co., Ltd. | ||
| * | ||
| * Licensed under the Apache License, Version 2.0 (the "License"); | ||
| * you may not use this file except in compliance with the License. | ||
| * You may obtain a copy of the License at | ||
| * | ||
| * http://www.apache.org/licenses/LICENSE-2.0 | ||
| * | ||
| * Unless required by applicable law or agreed to in writing, software | ||
| * distributed under the License is distributed on an "AS IS" BASIS, | ||
| * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| * See the License for the specific language governing permissions and | ||
| * limitations under the License. | ||
| */ | ||
| package net.spy.memcached.ops; | ||
|
|
||
| public class MultiPipelineOperationCallback extends MultiOperationCallback | ||
| implements PipelineOperation.Callback { | ||
|
|
||
| public MultiPipelineOperationCallback(OperationCallback original, int todo) { | ||
| super(original, todo); | ||
| } | ||
|
|
||
| @Override | ||
| public void gotStatus(Operation op, OperationStatus status) { | ||
| ((PipelineOperation.Callback) originalCallback).gotStatus(op, status); | ||
| } | ||
| } |
12 changes: 12 additions & 0 deletions
12
src/main/java/net/spy/memcached/ops/PipelineOperation.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,12 @@ | ||
| package net.spy.memcached.ops; | ||
|
|
||
| import java.util.List; | ||
|
|
||
| public interface PipelineOperation extends KeyedOperation { | ||
|
|
||
| List<KeyedOperation> getOps(); | ||
|
|
||
| interface Callback extends OperationCallback { | ||
| void gotStatus(Operation op, OperationStatus status); | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
269 changes: 269 additions & 0 deletions
269
src/main/java/net/spy/memcached/protocol/ascii/PipelineOperationImpl.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,269 @@ | ||
| /* | ||
| * arcus-java-client : Arcus Java client | ||
| * Copyright 2010-2014 NAVER Corp. | ||
| * Copyright 2014-present JaM2in Co., Ltd. | ||
| * | ||
| * Licensed under the Apache License, Version 2.0 (the "License"); | ||
| * you may not use this file except in compliance with the License. | ||
| * You may obtain a copy of the License at | ||
| * | ||
| * http://www.apache.org/licenses/LICENSE-2.0 | ||
| * | ||
| * Unless required by applicable law or agreed to in writing, software | ||
| * distributed under the License is distributed on an "AS IS" BASIS, | ||
| * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| * See the License for the specific language governing permissions and | ||
| * limitations under the License. | ||
| */ | ||
| package net.spy.memcached.protocol.ascii; | ||
|
|
||
| import java.nio.ByteBuffer; | ||
| import java.util.List; | ||
|
|
||
| import net.spy.memcached.ops.APIType; | ||
| import net.spy.memcached.ops.KeyedOperation; | ||
| import net.spy.memcached.ops.Operation; | ||
| import net.spy.memcached.ops.OperationCallback; | ||
| import net.spy.memcached.ops.OperationState; | ||
| import net.spy.memcached.ops.OperationStatus; | ||
| import net.spy.memcached.ops.OperationType; | ||
| import net.spy.memcached.ops.PipelineOperation; | ||
| import net.spy.memcached.ops.StatusCode; | ||
|
|
||
| /** | ||
| * Operation for executing multiple commands as pipeline. | ||
| */ | ||
| public final class PipelineOperationImpl extends OperationImpl implements PipelineOperation { | ||
|
|
||
| // PIPE RESPONSES | ||
| private static final OperationStatus END = | ||
| new OperationStatus(true, "END", StatusCode.SUCCESS); | ||
| private static final OperationStatus FAILED_END = | ||
| new OperationStatus(false, "FAILED_END", StatusCode.ERR_FAILED_END); | ||
|
|
||
| // EACH COMMAND'S SUCCEED RESPONSES | ||
| private static final OperationStatus CREATED_STORED = | ||
| new OperationStatus(true, "CREATED_STORED", StatusCode.SUCCESS); | ||
| private static final OperationStatus STORED = | ||
| new OperationStatus(true, "STORED", StatusCode.SUCCESS); | ||
| private static final OperationStatus REPLACED = | ||
| new OperationStatus(true, "REPLACED", StatusCode.SUCCESS); | ||
| private static final OperationStatus UPDATED = | ||
| new OperationStatus(true, "UPDATED", StatusCode.SUCCESS); | ||
| private static final OperationStatus EXIST = | ||
| new OperationStatus(true, "EXIST", StatusCode.EXIST); | ||
| private static final OperationStatus NOT_EXIST = | ||
| new OperationStatus(true, "NOT_EXIST", StatusCode.NOT_EXIST); | ||
| private static final OperationStatus DELETED = | ||
| new OperationStatus(true, "DELETED", StatusCode.SUCCESS); | ||
| private static final OperationStatus DELETED_DROPPED = | ||
| new OperationStatus(true, "DELETED_DROPPED", StatusCode.SUCCESS); | ||
|
|
||
| // EACH COMMAND'S FAILED RESPONSES | ||
| private static final OperationStatus NOT_FOUND = | ||
| new OperationStatus(false, "NOT_FOUND", StatusCode.ERR_NOT_FOUND); | ||
| private static final OperationStatus NOT_FOUND_ELEMENT = | ||
| new OperationStatus(false, "NOT_FOUND_ELEMENT", StatusCode.ERR_NOT_FOUND_ELEMENT); | ||
| private static final OperationStatus NOTHING_TO_UPDATE = | ||
| new OperationStatus(false, "NOTHING_TO_UPDATE", StatusCode.ERR_NOTHING_TO_UPDATE); | ||
| private static final OperationStatus ELEMENT_EXISTS = | ||
| new OperationStatus(false, "ELEMENT_EXISTS", StatusCode.ERR_ELEMENT_EXISTS); | ||
| private static final OperationStatus OVERFLOWED = | ||
| new OperationStatus(false, "OVERFLOWED", StatusCode.ERR_OVERFLOWED); | ||
| private static final OperationStatus OUT_OF_RANGE = | ||
| new OperationStatus(false, "OUT_OF_RANGE", StatusCode.ERR_OUT_OF_RANGE); | ||
| private static final OperationStatus TYPE_MISMATCH = | ||
| new OperationStatus(false, "TYPE_MISMATCH", StatusCode.ERR_TYPE_MISMATCH); | ||
| private static final OperationStatus BKEY_MISMATCH = | ||
| new OperationStatus(false, "BKEY_MISMATCH", StatusCode.ERR_BKEY_MISMATCH); | ||
| private static final OperationStatus EFLAG_MISMATCH = | ||
| new OperationStatus(false, "EFLAG_MISMATCH", StatusCode.ERR_EFLAG_MISMATCH); | ||
| private static final OperationStatus UNREADABLE = | ||
| new OperationStatus(false, "UNREADABLE", StatusCode.ERR_UNREADABLE); | ||
|
|
||
| private final List<KeyedOperation> ops; | ||
| private final List<String> keys; | ||
| private final PipelineOperation.Callback cb; | ||
|
|
||
| private int responseIndex = 0; | ||
| private boolean expectingResponse = false; | ||
| private boolean successAll = true; | ||
|
|
||
| /** | ||
| * @param ops each command's operation to be pipelined | ||
| * @param keys keys involved in this pipeline operation without duplicate | ||
| * @param cb callback for this pipeline operation | ||
| */ | ||
| public PipelineOperationImpl(List<KeyedOperation> ops, List<String> keys, | ||
| OperationCallback cb) { | ||
| super(cb); | ||
| if (ops == null || ops.isEmpty()) { | ||
| throw new IllegalArgumentException("Ops cannot be null or empty"); | ||
| } | ||
| this.ops = ops; | ||
| this.keys = keys; | ||
| this.cb = (PipelineOperation.Callback) cb; | ||
| setAPIType(APIType.PIPE); | ||
| setOperationType(OperationType.WRITE); | ||
| } | ||
|
|
||
| /** | ||
| * Make a pipelined command buffer using each command's buffer. | ||
| */ | ||
| @Override | ||
| public void initialize() { | ||
| // 1) Initialize operations and collect each buffers | ||
| // to handle switchover/redirect single key situations, | ||
| // make buffer from responseIndex Operation | ||
| int opCount = ops.size() - responseIndex; | ||
| ByteBuffer[] buffers = new ByteBuffer[opCount]; | ||
| int bufferCount = 0; | ||
| for (int i = responseIndex; i < ops.size(); i++) { | ||
| Operation op = ops.get(i); | ||
| op.initialize(); | ||
| ByteBuffer buffer = op.getBuffer(); | ||
| if (buffer != null && buffer.hasRemaining()) { | ||
| buffers[bufferCount++] = buffer; | ||
| } | ||
| } | ||
|
|
||
| // 2) Remove "pipe" from the last command buffer | ||
| if (bufferCount > 0) { | ||
| buffers[bufferCount - 1] = removePipeFromLastBuffer(buffers[bufferCount - 1]); | ||
| } | ||
|
|
||
| // 3) Create a concatenated pipedBuffer | ||
| int totalSize = 0; | ||
| for (int i = 0; i < bufferCount; i++) { | ||
| totalSize += buffers[i].remaining(); | ||
| } | ||
|
|
||
| ByteBuffer pipedBuffer = ByteBuffer.allocate(totalSize); | ||
| for (int i = 0; i < bufferCount; i++) { | ||
| pipedBuffer.put(buffers[i]); | ||
| } | ||
|
|
||
| pipedBuffer.flip(); | ||
| setBuffer(pipedBuffer); | ||
| } | ||
|
|
||
| private static ByteBuffer removePipeFromLastBuffer(ByteBuffer buffer) { | ||
| byte[] bufferBytes = new byte[buffer.remaining()]; | ||
| buffer.mark(); | ||
| buffer.get(bufferBytes); | ||
| buffer.reset(); | ||
|
|
||
| String command = new String(bufferBytes); | ||
| String modifiedCommand = command.replaceFirst("\\s+pipe\\r\\n", "\r\n"); | ||
| byte[] modifiedBytes = modifiedCommand.getBytes(); | ||
| ByteBuffer newBuffer = ByteBuffer.allocate(modifiedBytes.length); | ||
| newBuffer.put(modifiedBytes); | ||
| newBuffer.flip(); | ||
| return newBuffer; | ||
| } | ||
|
|
||
| @Override | ||
| public void handleLine(String line) { | ||
|
|
||
| /* ENABLE_REPLICATION if */ | ||
| if (hasSwitchedOver(line)) { | ||
| prepareSwitchover(line); | ||
| return; | ||
| } | ||
| /* ENABLE_REPLICATION end */ | ||
|
|
||
| /* ENABLE_MIGRATION if */ | ||
| if (hasNotMyKey(line)) { | ||
| String key = ops.get(responseIndex).getKeys().iterator().next(); | ||
| if (isBulkOperation()) { | ||
| addRedirectMultiKeyOperation(line, key); | ||
| responseIndex++; | ||
| } else { | ||
| // Only one NOT_MY_KEY is provided in response of | ||
| // single key piped operation when redirection. | ||
| addRedirectSingleKeyOperation(line, key); | ||
| transitionState(OperationState.REDIRECT); | ||
| } | ||
| return; | ||
| } | ||
| /* ENABLE_MIGRATION end */ | ||
|
|
||
| /* | ||
| 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")) { | ||
| /* ENABLE_MIGRATION if */ | ||
| if (needRedirect()) { | ||
| transitionState(OperationState.REDIRECT); | ||
| return; | ||
| } | ||
| /* ENABLE_MIGRATION end */ | ||
|
|
||
| OperationStatus status = successAll ? END : FAILED_END; | ||
| complete(status); | ||
| } else if (line.startsWith("PIPE_ERROR")) { | ||
| String errorMessage = line.substring(11); | ||
| OperationStatus status = | ||
| new OperationStatus(false, errorMessage, StatusCode.ERR_INTERNAL); | ||
| complete(status); | ||
| } else if (line.startsWith("RESPONSE ")) { | ||
| expectingResponse = true; | ||
| responseIndex = 0; | ||
| } else if (expectingResponse) { | ||
| // Handle status line for each command | ||
| OperationStatus status = parseStatusLine(line); | ||
| if (!status.isSuccess()) { | ||
| successAll = false; | ||
| } | ||
|
|
||
| // Notify callback with current response index | ||
| cb.gotStatus(ops.get(responseIndex), status); | ||
| responseIndex++; | ||
| } else { | ||
| // Handle single command response (non-pipe case) | ||
| // When only one command or last command without "pipe", server sends direct status | ||
| OperationStatus status = parseStatusLine(line); | ||
| if (!status.isSuccess()) { | ||
| successAll = false; | ||
| } | ||
|
|
||
| // Notify callback for single command | ||
| cb.gotStatus(ops.get(0), status); | ||
|
|
||
| // Complete the operation immediately for single command | ||
| complete(successAll ? END : FAILED_END); | ||
| } | ||
| } | ||
|
|
||
| private OperationStatus parseStatusLine(String line) { | ||
| return matchStatus(line, | ||
| END, FAILED_END, CREATED_STORED, STORED, REPLACED, UPDATED, | ||
| EXIST, NOT_EXIST, DELETED, DELETED_DROPPED, NOT_FOUND, NOT_FOUND_ELEMENT, | ||
| NOTHING_TO_UPDATE, ELEMENT_EXISTS, OVERFLOWED, OUT_OF_RANGE, | ||
| TYPE_MISMATCH, BKEY_MISMATCH, EFLAG_MISMATCH, UNREADABLE); | ||
| } | ||
|
|
||
| @Override | ||
| public boolean isPipeOperation() { | ||
| return ops.size() > 1; | ||
| } | ||
|
|
||
| @Override | ||
| public boolean isBulkOperation() { | ||
| return keys.size() > 1; | ||
| } | ||
|
|
||
| @Override | ||
| public List<String> getKeys() { | ||
| return keys; | ||
| } | ||
|
|
||
| @Override | ||
| public List<KeyedOperation> getOps() { | ||
| return ops; | ||
| } | ||
| } | ||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.