You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@kudu.apache.org by da...@apache.org on 2016/06/30 20:58:12 UTC
incubator-kudu git commit: [java-client] refactor AsyncKuduSession
Repository: incubator-kudu
Updated Branches:
refs/heads/master 9f9e3b80d -> aa3d4473d
[java-client] refactor AsyncKuduSession
This commit refactors how AsyncKuduSession handles buffering internally. Instead
of using a buffer per tablet and a separate list of operations in tablet
location lookup, we buffer all operations into a single large buffer, and then
dispatch into per-tablet batches during flush.
This result is simplified logic in AsyncKuduSession. AsyncKuduSession#apply is
no longer reentrant, and lookups no longer need to be tracked explicitly.
This patch was tested against YCSB to make sure that it does not result in
performance regressions. With an increased buffer size of 5000 to match the 0.9
client's buffer size, the performance is largely the same. With this refactor
the effective batch size is much smaller since mutationBufferSize now refers to
the one buffer for all tablets instead of to a per-tablet buffer.
Change-Id: I2ee6d029b1a56e254bfb9a870917883abeadb6b8
Reviewed-on: http://gerrit.cloudera.org:8080/3477
Reviewed-by: Jean-Daniel Cryans <jd...@apache.org>
Tested-by: Kudu Jenkins
Project: http://git-wip-us.apache.org/repos/asf/incubator-kudu/repo
Commit: http://git-wip-us.apache.org/repos/asf/incubator-kudu/commit/aa3d4473
Tree: http://git-wip-us.apache.org/repos/asf/incubator-kudu/tree/aa3d4473
Diff: http://git-wip-us.apache.org/repos/asf/incubator-kudu/diff/aa3d4473
Branch: refs/heads/master
Commit: aa3d4473d229807fa8ee27c6ffb5b4b6fb2893a4
Parents: 9f9e3b8
Author: Dan Burkert <da...@cloudera.com>
Authored: Fri Jun 17 12:05:13 2016 -0700
Committer: Dan Burkert <da...@cloudera.com>
Committed: Thu Jun 30 20:57:56 2016 +0000
----------------------------------------------------------------------
.../java/org/kududb/client/AsyncKuduClient.java | 44 +-
.../org/kududb/client/AsyncKuduSession.java | 883 +++++++++----------
.../src/main/java/org/kududb/client/Batch.java | 99 +--
.../src/main/java/org/kududb/client/Bytes.java | 47 -
.../main/java/org/kududb/client/Operation.java | 39 +-
.../main/java/org/kududb/client/RowError.java | 2 +-
.../main/java/org/kududb/util/AsyncUtil.java | 27 +-
.../org/kududb/client/TestAsyncKuduSession.java | 2 +
.../java/org/kududb/client/TestOperation.java | 7 +-
9 files changed, 552 insertions(+), 598 deletions(-)
----------------------------------------------------------------------
http://git-wip-us.apache.org/repos/asf/incubator-kudu/blob/aa3d4473/java/kudu-client/src/main/java/org/kududb/client/AsyncKuduClient.java
----------------------------------------------------------------------
diff --git a/java/kudu-client/src/main/java/org/kududb/client/AsyncKuduClient.java b/java/kudu-client/src/main/java/org/kududb/client/AsyncKuduClient.java
index de95f08..c51115b 100644
--- a/java/kudu-client/src/main/java/org/kududb/client/AsyncKuduClient.java
+++ b/java/kudu-client/src/main/java/org/kududb/client/AsyncKuduClient.java
@@ -73,6 +73,7 @@ import java.net.SocketAddress;
import java.net.UnknownHostException;
import java.nio.charset.Charset;
import java.util.ArrayList;
+import java.util.Arrays;
import java.util.Collections;
import java.util.HashMap;
import java.util.HashSet;
@@ -1041,7 +1042,8 @@ public class AsyncKuduClient implements AutoCloseable {
* @param partitionKey can be null, if not we'll find the exact tablet that contains it
* @return Deferred to track the progress
*/
- Deferred<Master.GetTableLocationsResponsePB> locateTablet(KuduTable table, byte[] partitionKey) {
+ private Deferred<Master.GetTableLocationsResponsePB> locateTablet(KuduTable table,
+ byte[] partitionKey) {
final boolean has_permit = acquireMasterLookupPermit();
String tableId = table.getTableId();
if (!has_permit) {
@@ -1391,6 +1393,37 @@ public class AsyncKuduClient implements AutoCloseable {
}
/**
+ * Returns a deferred containing the located tablet which covers the partition key in the table.
+ * @param table the table
+ * @param partitionKey the partition key of the tablet to look up in the table
+ * @param deadline deadline in milliseconds for this lookup to finish
+ * @return a deferred containing the located tablet
+ */
+ Deferred<LocatedTablet> getTabletLocation(KuduTable table, byte[] partitionKey, long deadline) {
+ // Locate the tablets at the partition key by locating all tablets between
+ // the partition key (inclusive), and the incremented partition key (exclusive).
+
+ Deferred<List<LocatedTablet>> locatedTablets;
+ if (partitionKey.length == 0) {
+ locatedTablets = locateTable(table, null, new byte[] { 0x00 }, deadline);
+ } else {
+ locatedTablets = locateTable(table, partitionKey,
+ Arrays.copyOf(partitionKey, partitionKey.length + 1), deadline);
+ }
+
+ // Then pick out the single tablet result from the list.
+ return locatedTablets.addCallback(new Callback<LocatedTablet, List<LocatedTablet>>() {
+ @Override
+ public LocatedTablet call(List<LocatedTablet> tablets) {
+ Preconditions.checkArgument(tablets.size() <= 1,
+ "found more than one tablet for a single partition key");
+ Preconditions.checkArgument(!tablets.isEmpty(), "found non-covered partition range");
+ return tablets.get(0);
+ }
+ });
+ }
+
+ /**
* Retrieve the master registration (see {@link GetMasterRegistrationResponse}
* for a replica.
* @param masterClient An initialized client for the master replica.
@@ -2100,15 +2133,6 @@ public class AsyncKuduClient implements AutoCloseable {
return tabletId.toString(Charset.defaultCharset());
}
- List<Common.HostPortPB> getAddressesFromPb(Master.TabletLocationsPB tabletLocations) {
- List<Common.HostPortPB> addresses = new ArrayList<Common.HostPortPB>(tabletLocations
- .getReplicasCount());
- for (Master.TabletLocationsPB.ReplicaPB replica : tabletLocations.getReplicasList()) {
- addresses.add(replica.getTsInfo().getRpcAddresses(0));
- }
- return addresses;
- }
-
@Override
public int compareTo(RemoteTablet remoteTablet) {
if (remoteTablet == null) {
http://git-wip-us.apache.org/repos/asf/incubator-kudu/blob/aa3d4473/java/kudu-client/src/main/java/org/kududb/client/AsyncKuduSession.java
----------------------------------------------------------------------
diff --git a/java/kudu-client/src/main/java/org/kududb/client/AsyncKuduSession.java b/java/kudu-client/src/main/java/org/kududb/client/AsyncKuduSession.java
index a9c4c8a..a4f126b 100644
--- a/java/kudu-client/src/main/java/org/kududb/client/AsyncKuduSession.java
+++ b/java/kudu-client/src/main/java/org/kududb/client/AsyncKuduSession.java
@@ -17,14 +17,15 @@
package org.kududb.client;
import com.google.common.annotations.VisibleForTesting;
+import com.google.common.base.MoreObjects;
+import com.google.common.base.Preconditions;
import com.google.common.collect.ImmutableList;
import com.google.common.collect.Range;
-import com.google.common.collect.Sets;
import com.stumbleupon.async.Callback;
import com.stumbleupon.async.Deferred;
import org.kududb.annotations.InterfaceAudience;
import org.kududb.annotations.InterfaceStability;
-import org.kududb.master.Master;
+import org.kududb.util.AsyncUtil;
import org.kududb.util.Slice;
import org.jboss.netty.util.Timeout;
import org.jboss.netty.util.TimerTask;
@@ -32,7 +33,16 @@ import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import javax.annotation.concurrent.GuardedBy;
-import java.util.*;
+import javax.annotation.concurrent.NotThreadSafe;
+import java.util.ArrayList;
+import java.util.HashMap;
+import java.util.List;
+import java.util.Map;
+import java.util.Random;
+import java.util.concurrent.ArrayBlockingQueue;
+import java.util.concurrent.BlockingQueue;
+import java.util.concurrent.atomic.AtomicInteger;
+import java.util.concurrent.atomic.AtomicReference;
import static org.kududb.client.ExternalConsistencyMode.CLIENT_PROPAGATED;
@@ -76,10 +86,11 @@ import static org.kududb.client.ExternalConsistencyMode.CLIENT_PROPAGATED;
* With AUTO_FLUSH_SYNC, the timeout is set on each apply()'d operation.
* With AUTO_FLUSH_BACKGROUND and MANUAL_FLUSH, the timeout is assigned to a whole batch of
* operations upon flush()'ing. It means that in a situation with a timeout of 500ms and a flush
- * interval of 1000ms, an operation can be oustanding for up to 1500ms before being timed out.
+ * interval of 1000ms, an operation can be outstanding for up to 1500ms before being timed out.
*/
@InterfaceAudience.Public
@InterfaceStability.Unstable
+@NotThreadSafe
public class AsyncKuduSession implements SessionConfiguration {
public static final Logger LOG = LoggerFactory.getLogger(AsyncKuduSession.class);
@@ -96,41 +107,55 @@ public class AsyncKuduSession implements SessionConfiguration {
private ExternalConsistencyMode consistencyMode;
private long timeoutMs;
- // We assign a number to each operation that we batch, so that the batch can sort itself before
- // being sent to the server. We never reset this number.
- private long nextSequenceNumber = 0;
+ /**
+ * Protects internal state from concurrent access. {@code AsyncKuduSession} is not threadsafe
+ * from the application's perspective, but because internally async timers and async flushing
+ * tasks may access the session concurrently with the application, synchronization is still
+ * needed.
+ */
+ private final Object monitor = new Object();
/**
- * The following two maps are to be handled together when batching is enabled. The first one is
- * where the batching happens, the second one is where we keep track of what's been sent but
- * hasn't come back yet. A batch cannot be in both maps at the same time. A batch cannot be
- * added to the in flight map if there's already another batch for the same tablet. If this
- * happens, and the batch in the first map is full, then we fail fast and send it back to the
- * client.
- * The second map stores Deferreds because KuduRpc.callback clears out the Deferred it contains
- * (as a way to reset the RPC), so we want to store the Deferred that's with the RPC that's
- * sent out.
+ * Tracks the currently active buffer.
+ *
+ * When in mode {@link FlushMode#AUTO_FLUSH_BACKGROUND} or {@link FlushMode#AUTO_FLUSH_SYNC},
+ * {@code AsyncKuduSession} uses double buffering to improve write throughput. While the
+ * application is {@link #apply}ing operations to one buffer (the {@code activeBuffer}), the
+ * second buffer is either being flushed, or if it has already been flushed, it waits in the
+ * {@link #inactiveBuffers} queue. When the currently active buffer is flushed,
+ * {@code activeBuffer} is set to {@code null}. On the next call to {@code apply}, an inactive
+ * buffer is taken from {@code inactiveBuffers} and made the new active buffer. If both
+ * buffers are still flushing, then the {@code apply} call throws {@link PleaseThrottleException}.
*/
- @GuardedBy("this")
- private final Map<Slice, Batch> operations = new HashMap<>();
+ @GuardedBy("monitor")
+ private Buffer activeBuffer;
- @GuardedBy("this")
- private final Map<Slice, Deferred<BatchResponse>> operationsInFlight = new HashMap<>();
+ /**
+ * The buffers. May either be active (pointed to by {@link #activeBuffer},
+ * inactive (in the {@link #inactiveBuffers}) queue, or flushing.
+ */
+ private final Buffer bufferA = new Buffer();
+ private final Buffer bufferB = new Buffer();
/**
- * This Set is used when not in AUTO_FLUSH_SYNC mode in order to keep track of the operations
- * that are looking up their tablet, meaning that they aren't in any of the maps above. This is
- * not expected to grow a lot except when a client starts and only for a short amount of time.
+ * Queue containing flushed, inactive buffers. May be accessed from callbacks (I/O threads).
+ * We restrict the session to only two buffers, so {@link BlockingQueue#add} can
+ * be used without chance of failure.
*/
- @GuardedBy("this")
- private final Set<Operation> operationsInLookup = Sets.newIdentityHashSet();
- // Only populated when we're waiting to flush and there are operations in lookup
- private Deferred<Void> lookupsDone;
+ private final BlockingQueue<Buffer> inactiveBuffers = new ArrayBlockingQueue<>(2, false);
+
+ /**
+ * Deferred used to notify on flush events. Atomically swapped and completed every time a buffer
+ * is flushed. This can be used to notify handlers of {@link PleaseThrottleException} that more
+ * capacity may be available in the active buffer.
+ */
+ private final AtomicReference<Deferred<Void>> flushNotification =
+ new AtomicReference<>(new Deferred<Void>());
/**
* Tracks whether the session has been closed.
*/
- volatile boolean closed;
+ private volatile boolean closed = false;
private boolean ignoreAllDuplicateRows = false;
@@ -140,11 +165,13 @@ public class AsyncKuduSession implements SessionConfiguration {
*/
AsyncKuduSession(AsyncKuduClient client) {
this.client = client;
- this.flushMode = FlushMode.AUTO_FLUSH_SYNC;
- this.consistencyMode = CLIENT_PROPAGATED;
- this.timeoutMs = client.getDefaultOperationTimeoutMs();
- setMutationBufferLowWatermark(this.mutationBufferLowWatermarkPercentage);
+ flushMode = FlushMode.AUTO_FLUSH_SYNC;
+ consistencyMode = CLIENT_PROPAGATED;
+ timeoutMs = client.getDefaultOperationTimeoutMs();
+ inactiveBuffers.add(bufferA);
+ inactiveBuffers.add(bufferB);
errorCollector = new ErrorCollector(mutationBufferSpace);
+ setMutationBufferLowWatermark(this.mutationBufferLowWatermarkPercentage);
}
@Override
@@ -243,7 +270,7 @@ public class AsyncKuduSession implements SessionConfiguration {
}
/**
- * Flushes the buffered operations and marks this sessions as closed.
+ * Flushes the buffered operations and marks this session as closed.
* See the javadoc on {@link #flush()} on how to deal with exceptions coming out of this method.
* @return a Deferred whose callback chain will be invoked when.
* everything that was buffered at the time of the call has been flushed.
@@ -252,100 +279,181 @@ public class AsyncKuduSession implements SessionConfiguration {
if (!closed) {
closed = true;
client.removeSession(this);
- return flush();
- } else {
- // Deferred#fromResult(T) is invariant on T, so the cast is necessary.
- return Deferred.fromResult((List<OperationResponse>) ImmutableList.<OperationResponse>of());
}
+ return flush();
}
/**
- * Flushes the buffered operations.
- * @return a Deferred whose callback chain will be invoked when
- * everything that was buffered at the time of the call has been flushed.
+ * Returns a buffer to the inactive queue after flushing.
+ * @param buffer the buffer to return to the inactive queue.
*/
- public Deferred<List<OperationResponse>> flush() {
- LOG.trace("Flushing all tablets");
- synchronized (this) {
- if (!operationsInLookup.isEmpty()) {
- lookupsDone = new Deferred<>();
- return lookupsDone
- .addCallbackDeferring(new OperationsInLookupDoneCB())
- .addCallbackDeferring(new ConvertBatchToListOfResponsesCB());
+ private void queueBuffer(Buffer buffer) {
+ buffer.callbackFlushNotification();
+ Deferred<Void> localFlushNotification = flushNotification.getAndSet(new Deferred<Void>());
+ inactiveBuffers.add(buffer);
+ localFlushNotification.callback(null);
+ }
+
+ /**
+ * Callback which waits for all tablet location lookups to complete, groups all operations into
+ * batches by tablet, and dispatches them. When all of the batches are complete, a deferred is
+ * fired and the buffer is added to the inactive queue.
+ */
+ private final class TabletLookupCB implements Callback<Void, Object> {
+ private final AtomicInteger lookupsOutstanding;
+ private final Buffer buffer;
+ private final Deferred<List<BatchResponse>> deferred;
+
+ public TabletLookupCB(Buffer buffer, Deferred<List<BatchResponse>> deferred) {
+ this.lookupsOutstanding = new AtomicInteger(buffer.getOperations().size());
+ this.buffer = buffer;
+ this.deferred = deferred;
+ }
+
+ @Override
+ public Void call(Object _void) throws Exception {
+ if (lookupsOutstanding.decrementAndGet() != 0) return null;
+
+ // The final tablet lookup is complete. Batch all of the buffered
+ // operations into their respective tablet, and then send the batches.
+
+ // Group the operations by tablet.
+ Map<Slice, Batch> batches = new HashMap<>();
+
+ for (BufferedOperation bufferedOp : buffer.getOperations()) {
+ Operation operation = bufferedOp.getOperation();
+ // TODO: when we have non-covered range partitioning the tablet lookup
+ // may fail with a NonCoveredRangeException. In this case we need to
+ // handle the exception, by adding it to a special BatchResponse below
+ // containing all such failed rows.
+ LocatedTablet tablet = bufferedOp.getTablet();
+ Slice tabletId = new Slice(tablet.getTabletId());
+
+ Batch batch = batches.get(tabletId);
+ if (batch == null) {
+ batch = new Batch(operation.getTable(), tablet, ignoreAllDuplicateRows);
+ batches.put(tabletId, batch);
+ }
+ batch.add(operation);
}
+
+ List<Deferred<BatchResponse>> batchResponses = new ArrayList<>(batches.size());
+
+ for (Batch batch : batches.values()) {
+ if (timeoutMs != 0) {
+ batch.deadlineTracker.reset();
+ batch.setTimeoutMillis(timeoutMs);
+ }
+ addBatchCallbacks(batch);
+ batchResponses.add(client.sendRpcToTablet(batch));
+ }
+
+ // On completion of all batches, fire the completion deferred, and add the buffer
+ // back to the inactive buffers queue. This frees it up for new inserts.
+ AsyncUtil.addBoth(
+ Deferred.group(batchResponses),
+ new Callback<Void, Object>() {
+ @Override
+ public Void call(Object responses) {
+ queueBuffer(buffer);
+ deferred.callback(responses);
+ return null;
+ }
+ });
+
+ return null;
}
- return flushAllBatches().addCallbackDeferring(new ConvertBatchToListOfResponsesCB());
}
- class OperationsInLookupDoneCB implements
- Callback<Deferred<ArrayList<BatchResponse>>, Void> {
- @Override
- public Deferred<ArrayList<BatchResponse>>
- call(Void nothing) throws Exception {
- return flushAllBatches();
+ /**
+ * Flush buffered writes.
+ * @return a {@link Deferred} whose callback chain will be invoked when all applied operations at
+ * the time of the call have been flushed.
+ */
+ public Deferred<List<OperationResponse>> flush() {
+ Buffer buffer;
+ Deferred<Void> nonActiveBufferFlush;
+ synchronized (monitor) {
+ nonActiveBufferFlush = getNonActiveFlushNotification();
+ buffer = activeBuffer;
+ activeBuffer = null;
}
+
+ final Deferred<List<OperationResponse>> activeBufferFlush = buffer == null ?
+ Deferred.<List<OperationResponse>>fromResult(ImmutableList.<OperationResponse>of()) :
+ doFlush(buffer);
+
+ return AsyncUtil.addBothDeferring(nonActiveBufferFlush,
+ new Callback<Deferred<List<OperationResponse>>, Object>() {
+ @Override
+ public Deferred<List<OperationResponse>> call(Object arg) {
+ return activeBufferFlush;
+ }
+ });
}
/**
- * Deferring callback used to send a list of OperationResponse instead of BatchResponse since the
- * latter is an implementation detail.
+ * Flushes a write buffer. This method takes ownership of the buffer, no other concurrent access
+ * is allowed.
+ *
+ * @param buffer the buffer to flush, must not be modified once passed to this method
+ * @return the operation responses
*/
- class ConvertBatchToListOfResponsesCB implements
- Callback<Deferred<List<OperationResponse>>, ArrayList<BatchResponse>> {
- @Override
- public Deferred<List<OperationResponse>> call(ArrayList<BatchResponse> batchResponsesList)
- throws Exception {
- Deferred<List<OperationResponse>> deferred = new Deferred<>();
- if (batchResponsesList == null) {
- deferred.callback(null);
- return deferred;
- }
+ private Deferred<List<OperationResponse>> doFlush(Buffer buffer) {
+ LOG.info("flushing buffer: {}", buffer);
+ if (buffer.getOperations().isEmpty()) {
+ // no-op.
+ return Deferred.<List<OperationResponse>>fromResult(ImmutableList.<OperationResponse>of());
+ }
- // flushTablet() can return null when a tablet we wanted to flush was already flushed. Those
- // nulls along with BatchResponses are then put in a list by Deferred.group(). We first need
- // to filter the nulls out.
- batchResponsesList.removeAll(Collections.singleton(null));
- if (batchResponsesList.isEmpty()) {
- deferred.callback(null);
- return deferred;
- }
+ Deferred<List<BatchResponse>> batchResponses = new Deferred<>();
+ Callback<Void, Object> tabletLookupCB = new TabletLookupCB(buffer, batchResponses);
+
+ for (BufferedOperation bufferedOperation : buffer.getOperations()) {
+ AsyncUtil.addBoth(bufferedOperation.getTabletLookup(), tabletLookupCB);
+ }
+ return batchResponses.addCallback(ConvertBatchToListOfResponsesCB.getInstance());
+ }
+
+ /**
+ * Callback used to send a list of OperationResponse instead of BatchResponse since the
+ * latter is an implementation detail.
+ */
+ private static class ConvertBatchToListOfResponsesCB implements Callback<List<OperationResponse>,
+ List<BatchResponse>> {
+ private static final ConvertBatchToListOfResponsesCB INSTANCE =
+ new ConvertBatchToListOfResponsesCB();
+ @Override
+ public List<OperationResponse> call(List<BatchResponse> batchResponses) throws Exception {
// First compute the size of the union of all the lists so that we don't trigger expensive
// list growths while adding responses to it.
int size = 0;
- for (BatchResponse batchResponse : batchResponsesList) {
+ for (BatchResponse batchResponse : batchResponses) {
size += batchResponse.getIndividualResponses().size();
}
- ArrayList<OperationResponse> responsesList = new ArrayList<>(size);
- for (BatchResponse batchResponse : batchResponsesList) {
- responsesList.addAll(batchResponse.getIndividualResponses());
+ ArrayList<OperationResponse> responses = new ArrayList<>(size);
+ for (BatchResponse batchResponse : batchResponses) {
+ responses.addAll(batchResponse.getIndividualResponses());
}
- deferred.callback(responsesList);
- return deferred;
- }
- }
- /**
- * This will flush all the batches but not the operations that are currently in lookup.
- */
- private Deferred<ArrayList<BatchResponse>> flushAllBatches() {
- Map<Slice, Batch> copyOfOps;
- final List<Deferred<BatchResponse>> d = new ArrayList<>(operations.size());
- synchronized (this) {
- copyOfOps = new HashMap<>(operations);
+ return responses;
+ }
+ @Override
+ public String toString() {
+ return "ConvertBatchToListOfResponsesCB";
}
- for (Map.Entry<Slice, Batch> entry : copyOfOps.entrySet()) {
- d.add(flushTablet(entry.getKey(), entry.getValue()));
+ public static ConvertBatchToListOfResponsesCB getInstance() {
+ return INSTANCE;
}
- return Deferred.group(d);
}
@Override
public boolean hasPendingOperations() {
- synchronized (this) {
- return !this.operations.isEmpty() || !this.operationsInFlight.isEmpty() ||
- !this.operationsInLookup.isEmpty();
+ synchronized (monitor) {
+ return activeBuffer == null ? inactiveBuffers.size() < 2 :
+ activeBuffer.getOperations().size() > 0 || !inactiveBufferAvailable();
}
}
@@ -358,18 +466,12 @@ public class AsyncKuduSession implements SessionConfiguration {
* @return a Deferred to track this operation
*/
public Deferred<OperationResponse> apply(final Operation operation) {
- if (operation == null) {
- throw new NullPointerException("Cannot apply a null operation");
- }
+ Preconditions.checkNotNull(operation, "Can not apply a null operation");
- if (AsyncKuduClient.cannotRetryRequest(operation)) {
- return AsyncKuduClient.tooManyAttemptsOrTimeout(operation, null);
- }
-
- // This can be called multiple times but it's fine, we don't allow "thawing".
+ // Freeze the row so that the client can not concurrently modify it while it is in flight.
operation.getRow().freeze();
- // If we autoflush, just send it to the TS
+ // If immediate flush mode, send the operation directly.
if (flushMode == FlushMode.AUTO_FLUSH_SYNC) {
if (timeoutMs != 0) {
operation.setTimeoutMillis(timeoutMs);
@@ -379,200 +481,135 @@ public class AsyncKuduSession implements SessionConfiguration {
return client.sendRpcToTablet(operation);
}
- // We need this protection because apply() can be called multiple times for the same operations
- // due to retries, but we only want to set the sequence number once. Since a session isn't
- // thread-safe, it means that we'll always set the sequence number from the user's thread, and
- // we'll read later from other threads.
- if (operation.getSequenceNumber() == -1) {
- operation.setSequenceNumber(nextSequenceNumber++);
- }
+ // Kick off a location lookup.
+ Deferred<LocatedTablet> tablet = client.getTabletLocation(operation.getTable(),
+ operation.partitionKey(),
+ timeoutMs);
- String tableId = operation.getTable().getTableId();
- byte[] partitionKey = operation.partitionKey();
- AsyncKuduClient.RemoteTablet tablet = client.getTablet(tableId, partitionKey);
- // We go straight to the buffer if we know the tabletSlice
- if (tablet != null) {
- operation.setTablet(tablet);
- // Handles the difference between manual and auto flush
- return addToBuffer(tablet.getTabletId(), operation);
- }
+ // Holds a buffer that should be flushed outside the synchronized block, if necessary.
+ Buffer fullBuffer = null;
+ try {
+ synchronized (monitor) {
+ if (activeBuffer == null) {
+ // If the active buffer is null then we recently flushed. Check if there
+ // is an inactive buffer available to replace as the active.
+ if (inactiveBufferAvailable()) {
+ refreshActiveBuffer();
+ } else {
+ // This can happen if the user writes into a buffer, flushes it, writes
+ // into the second, flushes it, and immediately tries to write again.
+ throw new PleaseThrottleException("All buffers are currently flushing",
+ null, operation, flushNotification.get());
+ }
+ }
- synchronized (this) {
- operationsInLookup.add(operation);
- }
- // TODO starts looking a lot like sendRpcToTablet
- operation.attempt++;
- if (client.isTableNotServed(tableId)) {
- Callback<Deferred<OperationResponse>, Master.IsCreateTableDoneResponsePB> cb =
- new TabletLookupCB<>(operation);
- return client.delayedIsCreateTableDone(operation.getTable(), operation,
- cb, getOpInLookupErrback(operation));
- }
+ if (flushMode == FlushMode.MANUAL_FLUSH) {
+ if (activeBuffer.getOperations().size() < mutationBufferSpace) {
+ activeBuffer.getOperations().add(new BufferedOperation(tablet, operation));
+ } else {
+ throw new NonRecoverableException(
+ "MANUAL_FLUSH mode is enabled but the buffer is full");
+ }
+ } else {
+ assert flushMode == FlushMode.AUTO_FLUSH_BACKGROUND;
+ int activeBufferSize = activeBuffer.getOperations().size();
+
+ if (activeBufferSize >= mutationBufferSpace) {
+ // Save the active buffer into fullBuffer so that it gets flushed when we leave this
+ // synchronized block.
+ fullBuffer = activeBuffer;
+ activeBuffer = null;
+ if (inactiveBufferAvailable()) {
+ refreshActiveBuffer();
+ } else {
+ throw new PleaseThrottleException("All buffers are currently flushing",
+ null, operation, flushNotification.get());
+ }
+ }
- Deferred<Master.GetTableLocationsResponsePB> d =
- client.locateTablet(operation.getTable(), partitionKey);
- d.addErrback(getOpInLookupErrback(operation));
- return d.addCallbackDeferring(
- new TabletLookupCB<Master.GetTableLocationsResponsePB>(operation));
- }
+ if (mutationBufferLowWatermark < mutationBufferSpace && // low watermark is enabled
+ activeBufferSize >= mutationBufferLowWatermark && // buffer is over low water mark
+ !inactiveBufferAvailable()) { // no inactive buffers
+
+ // Check if we are over the low water mark.
+ int randomWatermark = activeBufferSize + 1 +
+ randomizer.nextInt(mutationBufferSpace -
+ mutationBufferLowWatermark);
+
+ if (randomWatermark > mutationBufferSpace) {
+ throw new PleaseThrottleException(
+ "The previous buffer hasn't been flushed and the " +
+ "current buffer is over the low watermark, please retry later",
+ null, operation, flushNotification.get());
+ }
+ }
- /**
- * This errback is different from the one in AsyncKuduClient because we need to be able to remove
- * the operation from operationsInLookup if whatever master query we issue throws an Exception.
- * @param operation Operation to errback to.
- * @return An errback.
- */
- Callback<Exception, Exception> getOpInLookupErrback(final Operation operation) {
- return new Callback<Exception, Exception>() {
- @Override
- public Exception call(Exception e) throws Exception {
- // TODO maybe we can retry it?
- synchronized (this) {
- operationsInLookup.remove(operation);
+ activeBuffer.getOperations().add(new BufferedOperation(tablet, operation));
+
+ if (activeBufferSize + 1 >= mutationBufferSpace && inactiveBufferAvailable()) {
+ // If the operation filled the buffer, then flush it.
+ LOG.debug("flushing full buffer: {}", activeBuffer);
+ fullBuffer = activeBuffer;
+ activeBuffer = null;
+ } else if (activeBufferSize == 0) {
+ // If this is the first operation in the buffer, start a background flush timer.
+ client.newTimeout(activeBuffer.getFlusherTask(), interval);
+ }
}
- operation.errback(e);
- return e;
}
- };
- }
-
- final class TabletLookupCB<D> implements Callback<Deferred<OperationResponse>, D> {
- final Operation operation;
- TabletLookupCB(Operation operation) {
- this.operation = operation;
- }
- public Deferred<OperationResponse> call(final D arg) {
- return handleOperationInLookup(operation);
- }
- public String toString() {
- return "retry RPC after lookup";
- }
- }
-
- // This method takes an Object since we use it for both callback and errback.
- // The actual type doesn't matter, we just want to be called back in order to retry.
- Callback<Deferred<OperationResponse>, Object>
- getRetryOpInLookupCB(final Operation operation) {
- final class RetryOpInFlightCB implements Callback<Deferred<OperationResponse>, Object> {
- public Deferred<OperationResponse> call(final Object arg) {
- return handleOperationInLookup(operation);
- }
-
- public String toString() {
- return "retry RPC after PleaseThrottleException";
+ } finally {
+ // Flush the buffer outside of the synchronized block, if required.
+ if (fullBuffer != null) {
+ doFlush(fullBuffer);
}
}
- return new RetryOpInFlightCB();
+ return operation.getDeferred();
}
- private Deferred<OperationResponse> handleOperationInLookup(Operation operation) {
- try {
- return apply(operation); // Retry the RPC.
- } catch (PleaseThrottleException pte) {
- return pte.getDeferred().addBothDeferring(getRetryOpInLookupCB(operation));
- }
+ /**
+ * Returns {@code true} if there is an inactive buffer available.
+ * @return true if there is currently an inactive buffer available
+ */
+ private boolean inactiveBufferAvailable() {
+ return inactiveBuffers.peek() != null;
}
/**
- * For manual and background flushing, this will batch the given operation
- * with the others, if any, for the specified tablet.
- * @param tablet tablet used to for batching
- * @param operation operation to batch
- * @return Defered to track the operation
+ * Refreshes the active buffer. This should only be called after a
+ * {@link #flush()} when the active buffer is {@code null}, there is an
+ * inactive buffer available (see {@link #inactiveBufferAvailable()}, and
+ * {@link #monitor} is locked.
*/
- private Deferred<OperationResponse> addToBuffer(Slice tablet, Operation operation) {
- boolean scheduleFlush = false;
- boolean batchIsFull = false;
- Batch batch;
-
- // First check if we need to flush the current batch.
- synchronized (this) {
- batch = operations.get(tablet);
- if (batch != null && batch.ops.size() + 1 > mutationBufferSpace) {
- if (flushMode == FlushMode.MANUAL_FLUSH) {
- throw new NonRecoverableException("MANUAL_FLUSH is enabled but the buffer is too big");
- }
- if (operationsInFlight.containsKey(tablet)) {
- // There's is already another batch in flight for this tablet.
- // We cannot continue here, we have to send this back to the client.
- // This is our high watermark.
- throw new PleaseThrottleException("The RPC cannot be buffered because the current " +
- "buffer is full and the previous buffer hasn't been flushed yet", null,
- operation, operationsInFlight.get(tablet));
- }
- batchIsFull = true;
- }
- }
-
- // We're doing this out of the synchronized block because flushTablet can take some time
- // encoding all the data.
- if (batchIsFull) {
- flushTablet(tablet, batch);
- }
-
- Deferred<Void> lookupsDoneCopy = null;
- synchronized (this) {
- // We need to get the batch again since we went out of the synchronized block. We can get a
- // new one, the same one, or null.
- batch = operations.get(tablet);
-
- if (mutationBufferLowWatermark < mutationBufferSpace && // look if it's enabled
- batch != null && // and if we have a batch
- operationsInFlight.containsKey(tablet) && // and if there's another batch outstanding
- batch.ops.size() + 1 > mutationBufferLowWatermark) { // and if we'll be over the mark
-
- // This is our low watermark, we throw PleaseThrottleException before hitting the high
- // mark. As we get fuller past the watermark it becomes likelier to trigger it.
- int randomWatermark = batch.ops.size() + 1 + randomizer.nextInt(mutationBufferSpace -
- mutationBufferLowWatermark);
- if (randomWatermark > mutationBufferSpace) {
- throw new PleaseThrottleException("The previous buffer hasn't been flushed and the " +
- "current one is over the low watermark, please retry later", null, operation,
- operationsInFlight.get(tablet));
- }
- }
- if (batch == null) {
- // We found a tablet that needs batching, this is the only place where
- // we schedule a flush.
- batch = new Batch(operation.getTable(), ignoreAllDuplicateRows);
- batch.setExternalConsistencyMode(this.consistencyMode);
- Batch oldBatch = operations.put(tablet, batch);
- assert (oldBatch == null);
- addBatchCallbacks(batch);
- scheduleFlush = true;
- }
- batch.ops.add(operation);
- if (!operationsInLookup.isEmpty()) {
-
- boolean operationWasLookingUpTablet = operationsInLookup.remove(operation);
- if (operationWasLookingUpTablet) {
- // We know that the operation we just added was in the 'operationsInLookup' list so we're
- // very likely adding it out of order from a different thread.
- // We'll need to sort the whole list later.
- batch.needsSorting = true;
- }
+ @GuardedBy("monitor")
+ private void refreshActiveBuffer() {
+ Preconditions.checkState(activeBuffer == null);
+ activeBuffer = inactiveBuffers.remove();
+ activeBuffer.reset();
+ LOG.trace("active buffer refreshed: {}", activeBuffer);
+ }
- if (lookupsDone != null && operationsInLookup.isEmpty()) {
- lookupsDoneCopy = lookupsDone;
- lookupsDone = null;
+ /**
+ * Returns a flush notification for the currently non-active buffers.
+ * This is used during manual {@link #flush} calls to ensure that all buffers (not just the active
+ * buffer) are fully flushed before completing.
+ */
+ @GuardedBy("monitor")
+ private Deferred<Void> getNonActiveFlushNotification() {
+ final Deferred<Void> notificationA = bufferA.getFlushNotification();
+ final Deferred<Void> notificationB = bufferB.getFlushNotification();
+ if (activeBuffer == null) {
+ // Both buffers are either flushing or inactive.
+ return AsyncUtil.addBothDeferring(notificationA, new Callback<Deferred<Void>, Object>() {
+ @Override
+ public Deferred<Void> call(Object _obj) throws Exception {
+ return notificationB;
}
- }
- if (flushMode == FlushMode.AUTO_FLUSH_BACKGROUND && scheduleFlush) {
- // Accumulated a first insert but we're not in manual mode,
- // schedule the flush.
- LOG.trace("Scheduling a flush");
- scheduleNextPeriodicFlush(tablet, batch);
- }
- }
-
- // We do this outside of the synchronized block because we might end up calling flushTablet.
- if (lookupsDoneCopy != null) {
- lookupsDoneCopy.callback(null);
+ });
+ } else if (activeBuffer == bufferA) {
+ return notificationB;
+ } else {
+ return notificationA;
}
-
- // Get here if we accumulated an insert, regardless of if it scheduled
- // a flush.
- return operation.getDeferred();
}
/**
@@ -580,10 +617,9 @@ public class AsyncKuduSession implements SessionConfiguration {
* @param request the request for which we must handle the response
*/
private void addBatchCallbacks(final Batch request) {
- final class BatchCallback implements
- Callback<BatchResponse, BatchResponse> {
+ final class BatchCallback implements Callback<BatchResponse, BatchResponse> {
public BatchResponse call(final BatchResponse response) {
- LOG.trace("Got a Batch response for " + request.ops.size() + " rows");
+ LOG.trace("Got a Batch response for {} rows", request.operations.size());
if (response.getWriteTimestamp() != 0) {
AsyncKuduSession.this.client.updateLastPropagatedTimestamp(response.getWriteTimestamp());
}
@@ -605,16 +641,15 @@ public class AsyncKuduSession implements SessionConfiguration {
}
}
- final class BatchErrCallback implements Callback<Exception, Exception> {
+ final class BatchErrCallback implements Callback<Void, Exception> {
@Override
- public Exception call(Exception e) throws Exception {
+ public Void call(Exception e) throws Exception {
// Send the same exception to all the operations.
- for (int i = 0; i < request.ops.size(); i++) {
- request.ops.get(i).errback(e);
+ for (Operation operation : request.operations) {
+ operation.errback(e);
}
- return e;
+ return null;
}
-
@Override
public String toString() {
return "apply batch error response";
@@ -625,201 +660,143 @@ public class AsyncKuduSession implements SessionConfiguration {
}
/**
- * Schedules the next periodic flush of buffered edits.
+ * A FlusherTask is created for each active buffer in mode
+ * {@link FlushMode#AUTO_FLUSH_BACKGROUND}.
*/
- private void scheduleNextPeriodicFlush(Slice tablet, Batch batch) {
- client.newTimeout(new FlusherTask(tablet, batch), interval);
- }
-
- /**
- * Flushes the edits for the given tablet. It will also check that the Batch we're flushing is
- * the one that was requested. This is mostly done so that the FlusherTask doesn't trigger
- * lots of small flushes under a write-heavy scenario where we're able to fill a Batch multiple
- * times per interval.
- *
- * Also, if there's already a Batch in flight for the given tablet,
- * the flush will be delayed and the returned Deferred will be chained to it.
- *
- * This method should not be called within a synchronized block because we can spend a lot of
- * time encoding the batch.
- */
- private Deferred<BatchResponse> flushTablet(Slice tablet, Batch expectedBatch) {
- assert (expectedBatch != null);
- assert (!Thread.holdsLock(this));
- Batch batch;
- synchronized (this) {
- // Check this first, no need to wait after anyone if the batch we were supposed to flush
- // was already flushed.
- if (operations.get(tablet) != expectedBatch) {
- LOG.trace("Had to flush a tablet but it was already flushed: " + Bytes.getString(tablet));
- // It is OK to return null here, since we currently do not use the returned value
- // when doing background flush or auto flushing when buffer is full.
- // The returned value is used when doing manual flush, but it will not run into this
- // condition, or there is a bug.
- return Deferred.fromResult(null);
- }
-
- if (operationsInFlight.containsKey(tablet)) {
- if (LOG.isTraceEnabled()) {
- LOG.trace("Tablet " + Bytes.getString(tablet)
- + " is already in flight, attaching a callback to retry "
- + expectedBatch.toDebugString() + " later.");
+ private final class FlusherTask implements TimerTask {
+ public void run(final Timeout timeout) {
+ Buffer buffer = null;
+ synchronized (monitor) {
+ if (activeBuffer == null) {
+ return;
+ }
+ if (activeBuffer.getFlusherTask() == this) {
+ buffer = activeBuffer;
+ activeBuffer = null;
}
- // No matter previous batch get error or not, we still have to flush this batch.
- FlushRetryCallback retryCallback = new FlushRetryCallback(tablet, operations.get(tablet));
- FlushRetryErrback retryErrback = new FlushRetryErrback(tablet, operations.get(tablet));
- // Note that if we do manual flushing multiple times when previous batch is still inflight,
- // we may add the same callback multiple times, later retry of flushTablet will return null
- // immediately. Since it is an illegal use case, we do not handle this currently.
- operationsInFlight.get(tablet).addCallbacks(retryCallback, retryErrback);
- return expectedBatch.getDeferred();
}
- batch = operations.remove(tablet);
- if (batch == null) {
- LOG.trace("Had to flush a tablet but there was nothing to flush: " +
- Bytes.getString(tablet));
- return Deferred.fromResult(null);
- }
- Deferred<BatchResponse> batchDeferred = batch.getDeferred();
- batchDeferred.addCallbacks(getOpInFlightCallback(tablet), getOpInFlightErrback(tablet));
- Deferred<BatchResponse> oldBatch = operationsInFlight.put(tablet, batchDeferred);
- assert (oldBatch == null);
- if (timeoutMs != 0) {
- batch.deadlineTracker.reset();
- batch.setTimeoutMillis(timeoutMs);
+ if (buffer != null) {
+ doFlush(buffer);
}
}
- return client.sendRpcToTablet(batch);
}
-
/**
- * Simple callback so that we try to flush this tablet again if we were waiting on the previous
- * Batch to finish.
+ * The {@code Buffer} consists of a list of operations, an optional pointer to a flush task,
+ * and a flush notification.
+ *
+ * The {@link #flusherTask} is used in mode {@link FlushMode#AUTO_FLUSH_BACKGROUND} to point to
+ * the background flusher task assigned to the buffer when it becomes active and the first
+ * operation is applied to it. When the flusher task executes after the timeout, it checks
+ * that the currently active buffer's flusher task points to itself before executing the flush.
+ * This protects against the background task waking up after one or more manual flushes and
+ * attempting to flush the active buffer.
+ *
+ * The {@link #flushNotification} deferred is used when executing manual {@link #flush}es to
+ * ensure that non-active buffers are fully flushed. {@code flushNotification} is completed
+ * when this buffer is successfully flushed. When the buffer is promoted from inactive to active,
+ * the deferred is replaced with a new one to indicate that the buffer is not yet flushed.
+ *
+ * Buffer is externally synchronized. When the active buffer, {@link #monitor}
+ * synchronizes access to it.
*/
- class FlushRetryCallback implements Callback<BatchResponse, BatchResponse> {
- private final Slice tablet;
- private final Batch expectedBatch;
- public FlushRetryCallback(Slice tablet, Batch expectedBatch) {
- this.tablet = tablet;
- this.expectedBatch = expectedBatch;
+ private final class Buffer {
+ private final List<BufferedOperation> operations = new ArrayList<>();
+
+ private FlusherTask flusherTask = null;
+
+ private Deferred<Void> flushNotification = Deferred.fromResult(null);
+
+ public List<BufferedOperation> getOperations() {
+ return operations;
}
- @Override
- public BatchResponse call(BatchResponse o) throws Exception {
- if (LOG.isTraceEnabled()) {
- LOG.trace("Previous batch in flight is done. " + toString());
+ @GuardedBy("monitor")
+ public FlusherTask getFlusherTask() {
+ if (flusherTask == null) {
+ flusherTask = new FlusherTask();
}
- flushTablet(tablet, expectedBatch);
- return o;
+ return flusherTask;
}
- @Override
- public String toString() {
- return String.format("FlushRetryCallback: retry flush tablet %s %s", Bytes.getString(tablet),
- expectedBatch.toDebugString());
+ /**
+ * Returns a {@link Deferred} which will be completed when this buffer is flushed. If the buffer
+ * is inactive (its flush is complete and it has been enqueued into {@link #inactiveBuffers}),
+ * then the deferred will already be complete.
+ */
+ public Deferred<Void> getFlushNotification() {
+ return flushNotification;
}
- }
- /**
- * Same callback as above FlushRetryCallback, for the case that previous batch has error.
- */
- class FlushRetryErrback implements Callback<Exception, Exception> {
- private final Slice tablet;
- private final Batch expectedBatch;
- public FlushRetryErrback(Slice tablet, Batch expectedBatch) {
- this.tablet = tablet;
- this.expectedBatch = expectedBatch;
+ /**
+ * Completes the buffer's flush notification. Should be called when the buffer has been
+ * successfully flushed.
+ */
+ public void callbackFlushNotification() {
+ LOG.debug("callbackFlushNotification on {}", flushNotification);
+ flushNotification.callback(null);
}
- @Override
- public Exception call(Exception e) throws Exception {
- if (LOG.isTraceEnabled()) {
- LOG.trace("Previous batch ended with an error. " + toString());
- }
- flushTablet(tablet, expectedBatch);
- return e;
+ /**
+ * Resets the buffer's internal state. Should be called when the buffer is promoted from
+ * inactive to active.
+ */
+ @GuardedBy("monitor")
+ public void reset() {
+ operations.clear();
+ flushNotification = new Deferred<>();
+ flusherTask = null;
}
@Override
public String toString() {
- return String.format("FlushRetryErrback: retry flush tablet %s %s", Bytes.getString(tablet),
- expectedBatch.toDebugString());
+ return MoreObjects.toStringHelper(this)
+ .add("operations", operations.size())
+ .add("flusherTask", flusherTask)
+ .add("flushNotification", flushNotification)
+ .toString();
}
}
/**
- * Simple callback that removes the tablet from the in flight operations map once it completed.
- */
- private Callback<BatchResponse, BatchResponse>
- getOpInFlightCallback(final Slice tablet) {
- return new Callback<BatchResponse, BatchResponse>() {
- @Override
- public BatchResponse call(BatchResponse o) throws Exception {
- tabletInFlightDone(tablet);
- return o;
- }
-
- @Override
- public String toString() {
- return "callback: mark tablet " + Bytes.getString(tablet) + " inflight done";
- }
- };
- }
-
- /**
- * We need a separate callback for errors since the generics are different. We still remove the
- * tablet from the in flight operations since there's nothing we can do about it,
- * and by returning the Exception we will bubble it up to the user.
+ * Container class holding all the state associated with a buffered operation.
*/
- private Callback<Exception, Exception> getOpInFlightErrback(final Slice tablet) {
- return new Callback<Exception, Exception>() {
- @Override
- public Exception call(Exception e) throws Exception {
- tabletInFlightDone(tablet);
- return e;
- }
-
- @Override
- public String toString() {
- return "errback: mark tablet " + Bytes.getString(tablet) + " inflight done";
- }
- };
- }
-
- private void tabletInFlightDone(Slice tablet) {
- synchronized (AsyncKuduSession.this) {
- LOG.trace("Unmarking this tablet as in flight: " + Bytes.getString(tablet));
- operationsInFlight.remove(tablet);
+ private static final class BufferedOperation {
+ private LocatedTablet tablet = null;
+ private final Deferred<Void> tabletLookup;
+ private final Operation operation;
+
+ public BufferedOperation(Deferred<LocatedTablet> tablet,
+ Operation operation) {
+ tabletLookup = tablet.addCallback(new Callback<Void, LocatedTablet>() {
+ @Override
+ public Void call(final LocatedTablet tablet) {
+ BufferedOperation.this.tablet = tablet;
+ return null;
+ }
+ });
+ this.operation = Preconditions.checkNotNull(operation);
}
- }
- /**
- * A FlusherTask is created for each scheduled flush per tabletSlice.
- */
- class FlusherTask implements TimerTask {
- final Slice tabletSlice;
- final Batch expectedBatch;
+ public LocatedTablet getTablet() {
+ return tablet;
+ }
- FlusherTask(Slice tabletSlice, Batch expectedBatch) {
- this.tabletSlice = tabletSlice;
- this.expectedBatch = expectedBatch;
+ public Deferred<Void> getTabletLookup() {
+ return tabletLookup;
}
- public void run(final Timeout timeout) {
- // This is a TOCTOU violation, but since {@link #flushTablet} is
- // synchronized internally it is only an optimistic check, and
- // false negatives do not cause thread safety issues.
- if (isClosed()) {
- return; // we ran too late, no-op
- }
- LOG.trace("Timed flushing: " + Bytes.getString(tabletSlice));
- flushTablet(this.tabletSlice, this.expectedBatch);
+ public Operation getOperation() {
+ return operation;
}
+
+ @Override
public String toString() {
- return "flush commits of session " + AsyncKuduSession.this +
- " for tabletSlice " + Bytes.getString(tabletSlice);
+ return MoreObjects.toStringHelper(this)
+ .add("tablet", tablet)
+ .add("operation", operation)
+ .toString();
}
- };
+ }
}
http://git-wip-us.apache.org/repos/asf/incubator-kudu/blob/aa3d4473/java/kudu-client/src/main/java/org/kududb/client/Batch.java
----------------------------------------------------------------------
diff --git a/java/kudu-client/src/main/java/org/kududb/client/Batch.java b/java/kudu-client/src/main/java/org/kududb/client/Batch.java
index 05fdd9f..0a14ae8 100644
--- a/java/kudu-client/src/main/java/org/kududb/client/Batch.java
+++ b/java/kudu-client/src/main/java/org/kududb/client/Batch.java
@@ -17,9 +17,15 @@
package org.kududb.client;
import com.google.common.annotations.VisibleForTesting;
+import com.google.common.base.MoreObjects;
import com.google.protobuf.Message;
import com.google.protobuf.ZeroCopyLiteralByteString;
+import java.util.ArrayList;
+import java.util.Collections;
+import java.util.List;
+
+import org.jboss.netty.buffer.ChannelBuffer;
import org.kududb.WireProtocol;
import org.kududb.annotations.InterfaceAudience;
import org.kududb.client.Statistics.Statistic;
@@ -28,21 +34,19 @@ import org.kududb.tserver.Tserver;
import org.kududb.tserver.Tserver.TabletServerErrorPB;
import org.kududb.util.Pair;
import org.kududb.util.Slice;
-import org.jboss.netty.buffer.ChannelBuffer;
-
-import java.util.ArrayList;
-import java.util.Collections;
-import java.util.Comparator;
-import java.util.List;
/**
- * Used internally to batch Operations together before sending to the cluster
+ * Used internally to group Operations for a single tablet together before sending to the tablet
+ * server.
*/
@InterfaceAudience.Private
-class Batch extends KuduRpc<BatchResponse> implements KuduRpc.HasKey {
+public class Batch extends KuduRpc<BatchResponse> implements KuduRpc.HasKey {
+
+ /** Holds batched operations. */
+ final List<Operation> operations = new ArrayList<>();
- private static final OperationsComparatorBySequenceNumber SEQUENCE_NUMBER_COMPARATOR =
- new OperationsComparatorBySequenceNumber();
+ /** The tablet this batch will be routed to. */
+ private final LocatedTablet tablet;
/**
* This size will be set when serialize is called. It stands for the size of rows in all
@@ -50,23 +54,14 @@ class Batch extends KuduRpc<BatchResponse> implements KuduRpc.HasKey {
*/
private long rowOperationsSizeBytes = 0;
- final List<Operation> ops;
-
- // Operations can be added out of order to 'ops' if the tablet had to be looked up. We can detect
- // this situation in AsyncKuduSession and set this to true.
- boolean needsSorting = false;
-
/** See {@link SessionConfiguration#setIgnoreAllDuplicateRows(boolean)} */
- final boolean ignoreAllDuplicateRows;
+ private final boolean ignoreAllDuplicateRows;
- Batch(KuduTable table, boolean ignoreAllDuplicateRows) {
- this(table, ignoreAllDuplicateRows, 1000);
- }
- Batch(KuduTable table, boolean ignoreAllDuplicateRows, int estimatedBatchSize) {
+ Batch(KuduTable table, LocatedTablet tablet, boolean ignoreAllDuplicateRows) {
super(table);
- this.ops = new ArrayList<Operation>(estimatedBatchSize);
this.ignoreAllDuplicateRows = ignoreAllDuplicateRows;
+ this.tablet = tablet;
}
/**
@@ -81,26 +76,30 @@ class Batch extends KuduRpc<BatchResponse> implements KuduRpc.HasKey {
return this.rowOperationsSizeBytes;
}
- @Override
- ChannelBuffer serialize(Message header) {
+ public void add(Operation operation) {
+ assert Bytes.memcmp(operation.partitionKey(),
+ tablet.getPartition().getPartitionKeyStart()) >= 0 &&
+ (tablet.getPartition().getPartitionKeyEnd().length == 0 ||
+ Bytes.memcmp(operation.partitionKey(),
+ tablet.getPartition().getPartitionKeyEnd()) < 0);
- // This should only happen if at least one operation triggered a tablet lookup, which is rare
- // on a long-running client.
- if (needsSorting) {
- Collections.sort(ops, SEQUENCE_NUMBER_COMPARATOR);
- }
+ operations.add(operation);
+ }
- final Tserver.WriteRequestPB.Builder builder =
- Operation.createAndFillWriteRequestPB(ops.toArray(new Operation[ops.size()]));
- this.rowOperationsSizeBytes = builder.getRowOperations().getRows().size()
- + builder.getRowOperations().getIndirectData().size();
+ @Override
+ ChannelBuffer serialize(Message header) {
+ final Tserver.WriteRequestPB.Builder builder = Operation.createAndFillWriteRequestPB(operations);
+ rowOperationsSizeBytes = builder.getRowOperations().getRows().size() +
+ builder.getRowOperations().getIndirectData().size();
builder.setTabletId(ZeroCopyLiteralByteString.wrap(getTablet().getTabletIdAsBytes()));
- builder.setExternalConsistencyMode(this.externalConsistencyMode.pbVersion());
+ builder.setExternalConsistencyMode(externalConsistencyMode.pbVersion());
return toChannelBuffer(header, builder.build());
}
@Override
- String serviceName() { return TABLET_SERVER_SERVICE_NAME; }
+ String serviceName() {
+ return TABLET_SERVER_SERVICE_NAME;
+ }
@Override
String method() {
@@ -108,8 +107,8 @@ class Batch extends KuduRpc<BatchResponse> implements KuduRpc.HasKey {
}
@Override
- Pair<BatchResponse, Object> deserialize(final CallResponse callResponse,
- String tsUUID) throws Exception {
+ Pair<BatchResponse, Object> deserialize(CallResponse callResponse,
+ String tsUUID) throws Exception {
Tserver.WriteResponsePB.Builder builder = Tserver.WriteResponsePB.newBuilder();
readProtobuf(callResponse.getPBMessage(), builder);
@@ -128,7 +127,7 @@ class Batch extends KuduRpc<BatchResponse> implements KuduRpc.HasKey {
}
BatchResponse response = new BatchResponse(deadlineTracker.getElapsedMillis(), tsUUID,
- builder.getTimestamp(), errorsPB, ops);
+ builder.getTimestamp(), errorsPB, operations);
if (injectedError != null) {
if (injectedlatencyMs > 0) {
@@ -145,8 +144,7 @@ class Batch extends KuduRpc<BatchResponse> implements KuduRpc.HasKey {
@Override
public byte[] partitionKey() {
- assert this.ops.size() > 0;
- return this.ops.get(0).partitionKey();
+ return tablet.getPartition().getPartitionKeyStart();
}
@Override
@@ -155,7 +153,7 @@ class Batch extends KuduRpc<BatchResponse> implements KuduRpc.HasKey {
String tableName = this.getTable().getName();
TabletStatistics tabletStatistics = statistics.getTabletStatistics(tableName, tabletId);
if (response == null) {
- tabletStatistics.incrementStatistic(Statistic.OPS_ERRORS, this.ops.size());
+ tabletStatistics.incrementStatistic(Statistic.OPS_ERRORS, operations.size());
tabletStatistics.incrementStatistic(Statistic.RPC_ERRORS, 1);
return;
}
@@ -170,18 +168,13 @@ class Batch extends KuduRpc<BatchResponse> implements KuduRpc.HasKey {
tabletStatistics.incrementStatistic(Statistic.BYTES_WRITTEN, getRowOperationsSizeBytes());
}
- public String toDebugString() {
- return "Batch(" + ops.size() + " ops)@" + Integer.toHexString(hashCode());
- }
-
- /**
- * Sorts the Operations by their sequence number.
- */
- private static class OperationsComparatorBySequenceNumber implements Comparator<Operation> {
- @Override
- public int compare(Operation o1, Operation o2) {
- return Long.compare(o1.getSequenceNumber(), o2.getSequenceNumber());
- }
+ @Override
+ public String toString() {
+ return MoreObjects.toStringHelper(this)
+ .add("operations", operations.size())
+ .add("tablet", tablet)
+ .add("ignoreAllDuplicateRows", ignoreAllDuplicateRows)
+ .toString();
}
private static TabletServerErrorPB injectedError;
http://git-wip-us.apache.org/repos/asf/incubator-kudu/blob/aa3d4473/java/kudu-client/src/main/java/org/kududb/client/Bytes.java
----------------------------------------------------------------------
diff --git a/java/kudu-client/src/main/java/org/kududb/client/Bytes.java b/java/kudu-client/src/main/java/org/kududb/client/Bytes.java
index 2c6554d..0efaaa0 100644
--- a/java/kudu-client/src/main/java/org/kududb/client/Bytes.java
+++ b/java/kudu-client/src/main/java/org/kududb/client/Bytes.java
@@ -43,9 +43,6 @@ import java.nio.ByteBuffer;
import java.util.Arrays;
import java.util.BitSet;
import java.util.Comparator;
-import java.util.Iterator;
-import java.util.Map;
-import java.util.TreeMap;
/**
* Helper functions to manipulate byte arrays.
@@ -1103,48 +1100,4 @@ public final class Bytes {
dataInput.readFully(data);
return data;
}
-
- /** A convenient map keyed with a byte array. */
- public static final class ByteMap<V> extends TreeMap<byte[], V>
- implements Iterable<Map.Entry<byte[], V>> {
-
- public ByteMap() {
- super(MEMCMP);
- }
-
- /** Returns an iterator that goes through all the entries in this map. */
- @Override
- public Iterator<Map.Entry<byte[], V>> iterator() {
- return super.entrySet().iterator();
- }
-
- /** {@code byte[]} friendly implementation. */
- @Override
- public String toString() {
- final int size = size();
- if (size == 0) {
- return "{}";
- }
- final StringBuilder buf = new StringBuilder(size << 4);
- buf.append('{');
- for (final Map.Entry<byte[], V> e : this) {
- Bytes.pretty(buf, e.getKey());
- buf.append('=');
- final V value = e.getValue();
- if (value instanceof byte[]) {
- Bytes.pretty(buf, (byte[]) value);
- } else {
- buf.append(value == this ? "(this map)" : value);
- }
- buf.append(", ");
- }
- buf.setLength(buf.length() - 2); // Remove the extra ", ".
- buf.append('}');
- return buf.toString();
- }
-
- private static final long serialVersionUID = 1280744742;
-
- }
-
}
http://git-wip-us.apache.org/repos/asf/incubator-kudu/blob/aa3d4473/java/kudu-client/src/main/java/org/kududb/client/Operation.java
----------------------------------------------------------------------
diff --git a/java/kudu-client/src/main/java/org/kududb/client/Operation.java b/java/kudu-client/src/main/java/org/kududb/client/Operation.java
index 91cadcf..8539f01 100644
--- a/java/kudu-client/src/main/java/org/kududb/client/Operation.java
+++ b/java/kudu-client/src/main/java/org/kududb/client/Operation.java
@@ -16,6 +16,7 @@
// under the License.
package org.kududb.client;
+import com.google.common.collect.ImmutableList;
import com.google.protobuf.ByteString;
import com.google.protobuf.Message;
import com.google.protobuf.ZeroCopyLiteralByteString;
@@ -46,10 +47,6 @@ import java.util.List;
@InterfaceAudience.Public
@InterfaceStability.Evolving
public abstract class Operation extends KuduRpc<OperationResponse> implements KuduRpc.HasKey {
-
- // Number given by the session when apply()'d for the first time. Necessary to retain operations
- // in their original order even after tablet lookup.
- private long sequenceNumber = -1;
/**
* This size will be set when serialize is called. It stands for the size of the row in this
* operation.
@@ -102,25 +99,6 @@ public abstract class Operation extends KuduRpc<OperationResponse> implements Ku
*/
abstract ChangeType getChangeType();
-
- /**
- * Sets the sequence number used when batching operations. Should only be called once.
- * @param sequenceNumber a new sequence number
- */
- void setSequenceNumber(long sequenceNumber) {
- assert (this.sequenceNumber == -1);
- this.sequenceNumber = sequenceNumber;
- }
-
- /**
- * Returns the sequence number given to this operation.
- * @return a long representing the sequence number given to this operation after it was applied,
- * can be -1 if it wasn't set
- */
- long getSequenceNumber() {
- return this.sequenceNumber;
- }
-
/**
* Returns the size in bytes of this operation's row after serialization.
* @return size in bytes
@@ -143,7 +121,8 @@ public abstract class Operation extends KuduRpc<OperationResponse> implements Ku
@Override
ChannelBuffer serialize(Message header) {
- final Tserver.WriteRequestPB.Builder builder = createAndFillWriteRequestPB(this);
+ final Tserver.WriteRequestPB.Builder builder =
+ createAndFillWriteRequestPB(ImmutableList.of(this));
this.rowOperationSizeBytes = builder.getRowOperations().getRows().size()
+ builder.getRowOperations().getIndirectData().size();
builder.setTabletId(ZeroCopyLiteralByteString.wrap(getTablet().getTabletIdAsBytes()));
@@ -221,9 +200,9 @@ public abstract class Operation extends KuduRpc<OperationResponse> implements Ku
* @return A fully constructed WriteRequestPB containing the passed rows, or
* null if no rows were passed.
*/
- static Tserver.WriteRequestPB.Builder createAndFillWriteRequestPB(Operation... operations) {
- if (operations == null || operations.length == 0) return null;
- Schema schema = operations[0].table.getSchema();
+ static Tserver.WriteRequestPB.Builder createAndFillWriteRequestPB(List<Operation> operations) {
+ if (operations == null || operations.isEmpty()) return null;
+ Schema schema = operations.get(0).table.getSchema();
RowOperationsPB rowOps = new OperationsEncoder().encodeOperations(operations);
if (rowOps == null) return null;
@@ -325,9 +304,9 @@ public abstract class Operation extends KuduRpc<OperationResponse> implements Ku
}
}
- public RowOperationsPB encodeOperations(Operation... operations) {
- if (operations == null || operations.length == 0) return null;
- init(operations[0].table.getSchema(), operations.length);
+ public RowOperationsPB encodeOperations(List<Operation> operations) {
+ if (operations == null || operations.isEmpty()) return null;
+ init(operations.get(0).table.getSchema(), operations.size());
for (Operation operation : operations) {
encodeRow(operation.row, operation.getChangeType());
}
http://git-wip-us.apache.org/repos/asf/incubator-kudu/blob/aa3d4473/java/kudu-client/src/main/java/org/kududb/client/RowError.java
----------------------------------------------------------------------
diff --git a/java/kudu-client/src/main/java/org/kududb/client/RowError.java b/java/kudu-client/src/main/java/org/kududb/client/RowError.java
index 1570376..1fd8381 100644
--- a/java/kudu-client/src/main/java/org/kududb/client/RowError.java
+++ b/java/kudu-client/src/main/java/org/kududb/client/RowError.java
@@ -84,7 +84,7 @@ public class RowError {
@Override
public String toString() {
return "Row error for primary key=" + Bytes.pretty(operation.getRow().encodePrimaryKey()) +
- ", tablet=" + operation.getTablet().getTabletIdAsString() +
+ ", tablet=" + operation.getTablet() +
", server=" + tsUUID +
", status=" + status.toString();
}
http://git-wip-us.apache.org/repos/asf/incubator-kudu/blob/aa3d4473/java/kudu-client/src/main/java/org/kududb/util/AsyncUtil.java
----------------------------------------------------------------------
diff --git a/java/kudu-client/src/main/java/org/kududb/util/AsyncUtil.java b/java/kudu-client/src/main/java/org/kududb/util/AsyncUtil.java
index e05c5da..a93d1b9 100644
--- a/java/kudu-client/src/main/java/org/kududb/util/AsyncUtil.java
+++ b/java/kudu-client/src/main/java/org/kududb/util/AsyncUtil.java
@@ -18,6 +18,7 @@ package org.kududb.util;
import com.stumbleupon.async.Callback;
import com.stumbleupon.async.Deferred;
+
import org.kududb.annotations.InterfaceAudience;
/**
@@ -44,7 +45,29 @@ public class AsyncUtil {
Deferred<R> addCallbacksDeferring(final Deferred<T> d,
final Callback<D, T> cb,
final Callback<D, E> eb) {
- return d.addCallbacks((Callback<R, T>) ((Object) cb),
- (Callback<R, E>) ((Object) eb));
+ return d.addCallbacks((Callback<R, T>) cb, eb);
+ }
+
+ /**
+ * Workaround for {@link Deferred#addBoth}'s failure to use generics correctly. Allows callers
+ * to provide a {@link Callback} which takes an {@link Object} instead of the type of the deferred
+ * it is applied to, which avoids a runtime {@link ClassCastException} when the deferred fails.
+ */
+ @SuppressWarnings("unchecked")
+ public static <T, U> Deferred<U> addBoth(final Deferred<T> deferred,
+ final Callback<? extends U, Object> callback) {
+ return ((Deferred) deferred).addBoth(callback);
+ }
+
+ /**
+ * Workaround for {@link Deferred#addBothDeferring}'s failure to use generics correctly. Allows
+ * callers to provide a {@link Callback} which takes an {@link Object} instead of the type of the
+ * deferred it is applied to, which avoids a runtime {@link ClassCastException} when the deferred
+ * fails.
+ */
+ @SuppressWarnings("unchecked")
+ public static <T, U> Deferred<U> addBothDeferring(final Deferred<T> deferred,
+ final Callback<Deferred<U>, Object> callback) {
+ return ((Deferred) deferred).addBothDeferring(callback);
}
}
http://git-wip-us.apache.org/repos/asf/incubator-kudu/blob/aa3d4473/java/kudu-client/src/test/java/org/kududb/client/TestAsyncKuduSession.java
----------------------------------------------------------------------
diff --git a/java/kudu-client/src/test/java/org/kududb/client/TestAsyncKuduSession.java b/java/kudu-client/src/test/java/org/kududb/client/TestAsyncKuduSession.java
index 94dc55b..dba1437 100644
--- a/java/kudu-client/src/test/java/org/kududb/client/TestAsyncKuduSession.java
+++ b/java/kudu-client/src/test/java/org/kududb/client/TestAsyncKuduSession.java
@@ -202,6 +202,8 @@ public class TestAsyncKuduSession extends BaseKuduTest {
session.flush().join(DEFAULT_SLEEP);
assertEquals(10, countInRange(10, 20)); // now everything should be there
+ session.flush().join(DEFAULT_SLEEP); // flushing empty buffer should be a no-op.
+
session.setFlushMode(AsyncKuduSession.FlushMode.AUTO_FLUSH_BACKGROUND);
d = session.apply(createInsert(20));
http://git-wip-us.apache.org/repos/asf/incubator-kudu/blob/aa3d4473/java/kudu-client/src/test/java/org/kududb/client/TestOperation.java
----------------------------------------------------------------------
diff --git a/java/kudu-client/src/test/java/org/kududb/client/TestOperation.java b/java/kudu-client/src/test/java/org/kududb/client/TestOperation.java
index b84dc36..f305fbf 100644
--- a/java/kudu-client/src/test/java/org/kududb/client/TestOperation.java
+++ b/java/kudu-client/src/test/java/org/kududb/client/TestOperation.java
@@ -30,6 +30,7 @@ import org.kududb.client.Operation.ChangeType;
import org.kududb.tserver.Tserver.WriteRequestPBOrBuilder;
import org.mockito.Mockito;
+import com.google.common.collect.ImmutableList;
import com.google.common.primitives.Longs;
/**
@@ -60,7 +61,8 @@ public class TestOperation {
row.addString("c4", "c4_val");
{
- WriteRequestPBOrBuilder pb = Operation.createAndFillWriteRequestPB(insert);
+ WriteRequestPBOrBuilder pb =
+ Operation.createAndFillWriteRequestPB(ImmutableList.<Operation>of(insert));
RowOperationsPB rowOps = pb.getRowOperations();
assertEquals(6 * 5, rowOps.getIndirectData().size());
assertEquals("c0_valc1_valc2_valc3_valc4_val", rowOps.getIndirectData().toStringUtf8());
@@ -90,7 +92,8 @@ public class TestOperation {
// the old value from the indirect buffer.
row.setNull("c3");
{
- WriteRequestPBOrBuilder pb = Operation.createAndFillWriteRequestPB(insert);
+ WriteRequestPBOrBuilder pb =
+ Operation.createAndFillWriteRequestPB(ImmutableList.<Operation>of(insert));
RowOperationsPB rowOps = pb.getRowOperations();
assertEquals(6 * 4, rowOps.getIndirectData().size());
assertEquals("c0_valc1_valc2_valc4_val", rowOps.getIndirectData().toStringUtf8());