kudu-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From t...@apache.org
Subject incubator-kudu git commit: [java] Add an error collector
Date Thu, 28 Jan 2016 02:41:44 GMT
Repository: incubator-kudu
Updated Branches:
  refs/heads/master 143826e88 -> 67f2ed946


[java] Add an error collector

This is basically a port of the c++ client's error collector.

Change-Id: Ifd7b948c649270751b27e48a9aa6968413968660
Reviewed-on: http://gerrit.cloudera.org:8080/1923
Tested-by: Kudu Jenkins
Reviewed-by: Todd Lipcon <todd@apache.org>


Project: http://git-wip-us.apache.org/repos/asf/incubator-kudu/repo
Commit: http://git-wip-us.apache.org/repos/asf/incubator-kudu/commit/67f2ed94
Tree: http://git-wip-us.apache.org/repos/asf/incubator-kudu/tree/67f2ed94
Diff: http://git-wip-us.apache.org/repos/asf/incubator-kudu/diff/67f2ed94

Branch: refs/heads/master
Commit: 67f2ed946d28bf1b16db92fa164735a1e1ffc07c
Parents: 143826e
Author: Jean-Daniel Cryans <jdcryans@cloudera.com>
Authored: Tue Jan 26 20:04:48 2016 -0800
Committer: Todd Lipcon <todd@apache.org>
Committed: Thu Jan 28 02:22:16 2016 +0000

----------------------------------------------------------------------
 .../java/org/kududb/client/ErrorCollector.java  | 83 ++++++++++++++++++
 .../main/java/org/kududb/client/RowError.java   |  5 +-
 .../client/RowErrorsAndOverflowStatus.java      | 51 +++++++++++
 .../org/kududb/client/TestErrorCollector.java   | 90 ++++++++++++++++++++
 4 files changed, 228 insertions(+), 1 deletion(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/incubator-kudu/blob/67f2ed94/java/kudu-client/src/main/java/org/kududb/client/ErrorCollector.java
----------------------------------------------------------------------
diff --git a/java/kudu-client/src/main/java/org/kududb/client/ErrorCollector.java b/java/kudu-client/src/main/java/org/kududb/client/ErrorCollector.java
new file mode 100644
index 0000000..095a108
--- /dev/null
+++ b/java/kudu-client/src/main/java/org/kududb/client/ErrorCollector.java
@@ -0,0 +1,83 @@
+// Licensed to the Apache Software Foundation (ASF) under one
+// or more contributor license agreements.  See the NOTICE file
+// distributed with this work for additional information
+// regarding copyright ownership.  The ASF licenses this file
+// to you 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 org.kududb.client;
+
+import com.google.common.base.Preconditions;
+import org.kududb.annotations.InterfaceAudience;
+import org.kududb.annotations.InterfaceStability;
+
+import java.util.ArrayDeque;
+import java.util.Queue;
+
+/**
+ * Class that helps tracking row errors. All methods are thread-safe.
+ */
+@InterfaceAudience.Private
+@InterfaceStability.Evolving
+public class ErrorCollector {
+  private final Queue<RowError> errorQueue;
+  private final int maxCapacity;
+  private boolean overflowed;
+
+  /**
+   * Create a new error collector with a maximum capacity.
+   * @param maxCapacity how many errors can be stored, has to be higher than 0
+   */
+  public ErrorCollector(int maxCapacity) {
+    Preconditions.checkArgument(maxCapacity > 0, "Need to be able to store at least one
row error");
+    this.maxCapacity = maxCapacity;
+    this.errorQueue = new ArrayDeque<>(maxCapacity);
+  }
+
+  /**
+   * Add a new error to this collector. If it is already at max capacity, the oldest error
will be
+   * discarded before the new one is added.
+   * @param rowError a row error to collect
+   */
+  public synchronized void addError(RowError rowError) {
+    if (errorQueue.size() >= maxCapacity) {
+      errorQueue.poll();
+      overflowed = true;
+    }
+    errorQueue.add(rowError);
+  }
+
+  /**
+   * Get the current count collected row errors. Cannot be greater than the max capacity
this
+   * instance was configured with.
+   * @return the count of errors
+   */
+  public synchronized int countErrors() {
+    return errorQueue.size();
+  }
+
+  /**
+   * Get all the errors that have been collected and an indication if the list overflowed.
+   * The list of errors cleared and the overflow state is reset.
+   * @return an object that contains both the list of row errors and the overflow status
+   */
+  public synchronized RowErrorsAndOverflowStatus getErrors() {
+    RowError[] returnedErrors = new RowError[errorQueue.size()];
+    errorQueue.toArray(returnedErrors);
+    errorQueue.clear();
+
+    RowErrorsAndOverflowStatus returnObject =
+        new RowErrorsAndOverflowStatus(returnedErrors, overflowed);
+    overflowed = false;
+    return returnObject;
+  }
+}

http://git-wip-us.apache.org/repos/asf/incubator-kudu/blob/67f2ed94/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 b243709..c2e0b59 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
@@ -32,7 +32,10 @@ public class RowError {
   private final Operation operation;
   private final String tsUUID;
 
-  private RowError(String errorStatus, String errorMessage, Operation operation, String tsUUID)
{
+  /**
+   * Package-private for unit tests.
+   */
+  RowError(String errorStatus, String errorMessage, Operation operation, String tsUUID) {
     this.status = errorStatus;
     this.message = errorMessage;
     this.operation = operation;

http://git-wip-us.apache.org/repos/asf/incubator-kudu/blob/67f2ed94/java/kudu-client/src/main/java/org/kududb/client/RowErrorsAndOverflowStatus.java
----------------------------------------------------------------------
diff --git a/java/kudu-client/src/main/java/org/kududb/client/RowErrorsAndOverflowStatus.java
b/java/kudu-client/src/main/java/org/kududb/client/RowErrorsAndOverflowStatus.java
new file mode 100644
index 0000000..17a4778
--- /dev/null
+++ b/java/kudu-client/src/main/java/org/kududb/client/RowErrorsAndOverflowStatus.java
@@ -0,0 +1,51 @@
+// Licensed to the Apache Software Foundation (ASF) under one
+// or more contributor license agreements.  See the NOTICE file
+// distributed with this work for additional information
+// regarding copyright ownership.  The ASF licenses this file
+// to you 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 org.kududb.client;
+
+import org.kududb.annotations.InterfaceAudience;
+import org.kududb.annotations.InterfaceStability;
+
+/**
+ * Container class used as a response when retrieving pending row errors.
+ */
+@InterfaceAudience.Public
+@InterfaceStability.Evolving
+public class RowErrorsAndOverflowStatus {
+  private final RowError[] rowErrors;
+  private final boolean overflowed;
+
+  RowErrorsAndOverflowStatus(RowError[] rowErrors, boolean overflowed) {
+    this.rowErrors = rowErrors;
+    this.overflowed = overflowed;
+  }
+
+  /**
+   * Get the collected row errors.
+   * @return an array of row errors, may be empty
+   */
+  public RowError[] getRowErrors() {
+    return rowErrors;
+  }
+
+  /**
+   * Check if the error collector had an overflow and had to discard row errors.
+   * @return true if row errors were discarded, false otherwise
+   */
+  public boolean isOverflowed() {
+    return overflowed;
+  }
+}

http://git-wip-us.apache.org/repos/asf/incubator-kudu/blob/67f2ed94/java/kudu-client/src/test/java/org/kududb/client/TestErrorCollector.java
----------------------------------------------------------------------
diff --git a/java/kudu-client/src/test/java/org/kududb/client/TestErrorCollector.java b/java/kudu-client/src/test/java/org/kududb/client/TestErrorCollector.java
new file mode 100644
index 0000000..01be75c
--- /dev/null
+++ b/java/kudu-client/src/test/java/org/kududb/client/TestErrorCollector.java
@@ -0,0 +1,90 @@
+// Licensed to the Apache Software Foundation (ASF) under one
+// or more contributor license agreements.  See the NOTICE file
+// distributed with this work for additional information
+// regarding copyright ownership.  The ASF licenses this file
+// to you 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 org.kududb.client;
+
+import org.junit.Assert;
+import org.junit.Test;
+
+public class TestErrorCollector {
+
+  @Test
+  public void testErrorCollector() {
+    int maxErrors = 10;
+    ErrorCollector collector = new ErrorCollector(maxErrors);
+
+    // Test with no errors.
+    int countToTest = 0;
+    Assert.assertEquals(countToTest, collector.countErrors());
+    RowErrorsAndOverflowStatus reos = collector.getErrors();
+    Assert.assertEquals(0, collector.countErrors());
+    Assert.assertFalse(reos.isOverflowed());
+    Assert.assertEquals(countToTest, reos.getRowErrors().length);
+
+    // Test a single row error.
+    countToTest = 1;
+    collector.addError(createRowError(countToTest));
+    Assert.assertEquals(countToTest, collector.countErrors());
+    reos = collector.getErrors();
+    Assert.assertEquals(0, collector.countErrors());
+    Assert.assertFalse(reos.isOverflowed());
+    Assert.assertEquals(countToTest, reos.getRowErrors().length);
+    Assert.assertEquals(countToTest + "", reos.getRowErrors()[0].getStatus());
+
+    // Test filling the collector to the max.
+    countToTest = maxErrors;
+    fillCollectorWith(collector, countToTest);
+    Assert.assertEquals(countToTest, collector.countErrors());
+    reos = collector.getErrors();
+    Assert.assertEquals(0, collector.countErrors());
+    Assert.assertFalse(reos.isOverflowed());
+    Assert.assertEquals(countToTest, reos.getRowErrors().length);
+    Assert.assertEquals((countToTest - 1) + "", reos.getRowErrors()[9].getStatus());
+
+    // Test overflowing.
+    countToTest = 95;
+    fillCollectorWith(collector, countToTest);
+    Assert.assertEquals(maxErrors, collector.countErrors());
+    reos = collector.getErrors();
+    Assert.assertEquals(0, collector.countErrors());
+    Assert.assertTrue(reos.isOverflowed());
+    Assert.assertEquals(maxErrors, reos.getRowErrors().length);
+    Assert.assertEquals((countToTest - 1) + "", reos.getRowErrors()[9].getStatus());
+
+    // Test overflowing on a newly created collector.
+    countToTest = 95;
+    collector = new ErrorCollector(maxErrors);
+    fillCollectorWith(collector, countToTest);
+    Assert.assertEquals(maxErrors, collector.countErrors());
+    reos = collector.getErrors();
+    Assert.assertEquals(0, collector.countErrors());
+    Assert.assertTrue(reos.isOverflowed());
+    Assert.assertEquals(maxErrors, reos.getRowErrors().length);
+    Assert.assertEquals((countToTest - 1) + "", reos.getRowErrors()[9].getStatus());
+  }
+
+  private void fillCollectorWith(ErrorCollector collector, int errorsToAdd) {
+    for (int i = 0; i < errorsToAdd; i++) {
+      collector.addError(createRowError(i));
+    }
+  }
+
+  private RowError createRowError(int id) {
+    // Use the error status as a way to message pass and so that we can test we're getting
the right
+    // messages on the other end.
+    return new RowError(id + "", "test", null, "test");
+  }
+}


Mime
View raw message