This is an automated email from the ASF dual-hosted git repository.

mmiller pushed a commit to branch main
in repository https://gitbox.apache.org/repos/asf/accumulo.git


The following commit(s) were added to refs/heads/main by this push:
     new 89b411c  Adds tests to LogFileKeyTest (#2177)
89b411c is described below

commit 89b411c391675cb3f1faed83f2dd4451b2ac6d12
Author: Mike Miller <mmil...@apache.org>
AuthorDate: Wed Jun 23 09:57:31 2021 -0400

    Adds tests to LogFileKeyTest (#2177)
---
 .../apache/accumulo/tserver/logger/LogFileKey.java | 15 ++++++-
 .../accumulo/tserver/log/LogFileKeyTest.java       | 49 ++++++++++++++++++++++
 2 files changed, 62 insertions(+), 2 deletions(-)

diff --git 
a/server/tserver/src/main/java/org/apache/accumulo/tserver/logger/LogFileKey.java
 
b/server/tserver/src/main/java/org/apache/accumulo/tserver/logger/LogFileKey.java
index cf5dc21..b860be5 100644
--- 
a/server/tserver/src/main/java/org/apache/accumulo/tserver/logger/LogFileKey.java
+++ 
b/server/tserver/src/main/java/org/apache/accumulo/tserver/logger/LogFileKey.java
@@ -28,6 +28,7 @@ import java.io.DataInput;
 import java.io.DataOutput;
 import java.io.IOException;
 import java.io.UncheckedIOException;
+import java.util.Arrays;
 
 import org.apache.accumulo.core.data.Key;
 import org.apache.accumulo.core.data.Range;
@@ -36,10 +37,13 @@ import org.apache.hadoop.io.DataInputBuffer;
 import org.apache.hadoop.io.DataOutputBuffer;
 import org.apache.hadoop.io.Text;
 import org.apache.hadoop.io.WritableComparable;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
 
 import com.google.common.base.Preconditions;
 
 public class LogFileKey implements WritableComparable<LogFileKey> {
+  private static final Logger log = LoggerFactory.getLogger(LogFileKey.class);
 
   public LogEvents event;
   public String filename = null;
@@ -258,7 +262,9 @@ public class LogFileKey implements 
WritableComparable<LogFileKey> {
 
   /**
    * Format the row using 13 bytes encoded to allow proper sorting of the 
RFile Key. The highest
-   * byte is for the event number, 4 bytes for the tabletId and 8 bytes for 
the sequence long.
+   * byte is for the event number, 4 bytes for the tabletId and 8 bytes for 
the sequence long. The
+   * conversions of integer to byte[] and long to byte[] is similar to what 
DataOutputStream does
+   * for writeInt() and writeLong()
    */
   private byte[] formatRow(int tabletId, long seq) {
     byte eventNum = getEventByte();
@@ -283,11 +289,14 @@ public class LogFileKey implements 
WritableComparable<LogFileKey> {
     row[10] = (byte) ((seq >>> 16) & mask);
     row[11] = (byte) ((seq >>> 8) & mask);
     row[12] = (byte) (seq & mask);
+
+    log.trace("Convert {} {} {} to row {}", event, tabletId, seq, 
Arrays.toString(row));
     return row;
   }
 
   /**
-   * Extract the tabletId integer from the byte encoded Row.
+   * Extract the tabletId integer from the byte encoded Row. Similar to what 
DataInputStream does
+   * for readInt()
    */
   private static int getTabletId(byte[] row) {
     int mask = 0xff; // use a mask of int type to convert byte to int without 
sign extension
@@ -335,6 +344,8 @@ public class LogFileKey implements 
WritableComparable<LogFileKey> {
     if (eventType(logFileKey.event) != rowParts[0]) {
       throw new AssertionError("Event in row differs from column family. Key: 
" + key);
     }
+    log.trace("From row {} get {} {} {}", Arrays.toString(rowParts), 
logFileKey.event,
+        logFileKey.tabletId, logFileKey.seq);
 
     // handle special cases of what is stored in the qualifier
     switch (logFileKey.event) {
diff --git 
a/server/tserver/src/test/java/org/apache/accumulo/tserver/log/LogFileKeyTest.java
 
b/server/tserver/src/test/java/org/apache/accumulo/tserver/log/LogFileKeyTest.java
index 72cc339..0f04aca 100644
--- 
a/server/tserver/src/test/java/org/apache/accumulo/tserver/log/LogFileKeyTest.java
+++ 
b/server/tserver/src/test/java/org/apache/accumulo/tserver/log/LogFileKeyTest.java
@@ -20,12 +20,17 @@ package org.apache.accumulo.tserver.log;
 
 import static org.junit.Assert.assertEquals;
 
+import java.io.IOException;
 import java.util.ArrayList;
 import java.util.Collections;
 import java.util.List;
 
+import org.apache.accumulo.core.data.Key;
+import org.apache.accumulo.core.data.TableId;
+import org.apache.accumulo.core.dataImpl.KeyExtent;
 import org.apache.accumulo.tserver.logger.LogEvents;
 import org.apache.accumulo.tserver.logger.LogFileKey;
+import org.apache.hadoop.io.Text;
 import org.junit.Test;
 
 public class LogFileKeyTest {
@@ -98,4 +103,48 @@ public class LogFileKeyTest {
     }
 
   }
+
+  private void testToFromKey(int tabletId, long seq) throws IOException {
+    var logFileKey = nk(LogEvents.DEFINE_TABLET, tabletId, seq);
+    logFileKey.tablet = new KeyExtent(TableId.of("5"), new Text("b"), null);
+    Key k = logFileKey.toKey();
+
+    var converted = LogFileKey.fromKey(k);
+    assertEquals("Failed to convert tabletId = " + tabletId + " seq = " + seq, 
logFileKey,
+        converted);
+  }
+
+  @Test
+  public void convertToKeyLargeTabletId() throws IOException {
+    // test continuous range of numbers to make sure all cases of signed bytes 
are covered
+    for (int i = -1_000; i < 1_000_000; i++) {
+      testToFromKey(i, 1);
+    }
+  }
+
+  @Test
+  public void convertToKeyLargeSeq() throws IOException {
+    // use numbers large enough to cover all bytes used
+    testToFromKey(1, 8); // 1 byte
+    testToFromKey(1, 1_000L); // 2 bytes
+    testToFromKey(1, 1_222_333L); // 3 bytes
+    testToFromKey(1, 100_000_000L); // 4 bytes
+    testToFromKey(1, 111_222_333_444L); // 5 bytes
+    testToFromKey(1, 1_000_222_333_777L); // 6 bytes
+    testToFromKey(1, 5_222_000_222_333_777L); // 7 bytes
+    testToFromKey(1, 500_222_333_444_555_666L); // 8 bytes
+
+    // test with more tabletId bytes
+    testToFromKey(-1000, 500_222_333_444_555_666L);
+    testToFromKey(10_000_000, 500_222_333_444_555_666L);
+    testToFromKey(Integer.MAX_VALUE, Long.MAX_VALUE);
+    testToFromKey(Integer.MIN_VALUE, Long.MAX_VALUE);
+    testToFromKey(Integer.MIN_VALUE, 0);
+    testToFromKey(Integer.MAX_VALUE, 0);
+  }
+
+  @Test(expected = IllegalArgumentException.class)
+  public void testNegativeSeq() throws IOException {
+    testToFromKey(1, -1);
+  }
 }

Reply via email to