Skip to content

Commit

Permalink
[SPARK-13652][CORE] Copy ByteBuffer in sendRpcSync as it will be recy…
Browse files Browse the repository at this point in the history
…cled

## What changes were proposed in this pull request?

`sendRpcSync` should copy the response content because the underlying buffer will be recycled and reused.

## How was this patch tested?

Jenkins unit tests.

Author: Shixiong Zhu <shixiong@databricks.com>

Closes #11499 from zsxwing/SPARK-13652.

(cherry picked from commit 465c665)
Signed-off-by: Shixiong Zhu <shixiong@databricks.com>
  • Loading branch information
zsxwing committed Mar 4, 2016
1 parent b3a5129 commit 51c676e
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,12 @@
* failure.
*/
public interface RpcResponseCallback {
/** Successful serialized result from server. */
/**
* Successful serialized result from server.
*
* After `onSuccess` returns, `response` will be recycled and its content will become invalid.
* Please copy the content of `response` if you want to use it after `onSuccess` returns.
*/
void onSuccess(ByteBuffer response);

/** Exception either propagated from server or raised on client side. */
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -257,7 +257,11 @@ public ByteBuffer sendRpcSync(ByteBuffer message, long timeoutMs) {
sendRpc(message, new RpcResponseCallback() {
@Override
public void onSuccess(ByteBuffer response) {
result.set(response);
ByteBuffer copy = ByteBuffer.allocate(response.remaining());
copy.put(response);
// flip "copy" to make it readable
copy.flip();
result.set(copy);
}

@Override
Expand Down

0 comments on commit 51c676e

Please sign in to comment.