[accumulo] branch main updated: Adds tests to LogFileKeyTest (#2177)

2021-06-23 Thread mmiller
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 
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 {
+  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 {
 
   /**
* 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 {
 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 {
 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);
+ 

[accumulo] branch main updated: Fix row encoding/decoding in LogFileKey (#2176)

2021-06-23 Thread mmiller
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 b8262a4  Fix row encoding/decoding in LogFileKey (#2176)
b8262a4 is described below

commit b8262a424db92729c89d6870ba5973386b9e3ea1
Author: Christopher Tubbs 
AuthorDate: Wed Jun 23 08:46:15 2021 -0400

Fix row encoding/decoding in LogFileKey (#2176)

Fix bitwise operations to encode and decode the tabletId and sequence
number to/from a row

This fixes #2173
---
 .../apache/accumulo/tserver/logger/LogFileKey.java | 54 +-
 1 file changed, 31 insertions(+), 23 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 bf08ca4..cf5dc21 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
@@ -245,8 +245,8 @@ public class LogFileKey implements 
WritableComparable {
* byte in the row.
*/
   private byte getEventByte() {
-int evenTypeInteger = eventType(event);
-return (byte) (evenTypeInteger & 0xff);
+int eventTypeInteger = eventType(event);
+return (byte) (eventTypeInteger & 0xff);
   }
 
   /**
@@ -269,19 +269,20 @@ public class LogFileKey implements 
WritableComparable {
 // encode the signed integer so negatives will sort properly for tabletId
 int encodedTabletId = tabletId ^ 0x8000;
 
+int mask = 0xff; // use a mask of int type to truncate the selected 8 bits
 row[0] = eventNum;
-row[1] = (byte) ((encodedTabletId >>> 24) & 0xff);
-row[2] = (byte) ((encodedTabletId >>> 16) & 0xff);
-row[3] = (byte) ((encodedTabletId >>> 8) & 0xff);
-row[4] = (byte) (encodedTabletId & 0xff);
-row[5] = (byte) (seq >>> 56);
-row[6] = (byte) (seq >>> 48);
-row[7] = (byte) (seq >>> 40);
-row[8] = (byte) (seq >>> 32);
-row[9] = (byte) (seq >>> 24);
-row[10] = (byte) (seq >>> 16);
-row[11] = (byte) (seq >>> 8);
-row[12] = (byte) (seq); // >>> 0
+row[1] = (byte) ((encodedTabletId >>> 24) & mask);
+row[2] = (byte) ((encodedTabletId >>> 16) & mask);
+row[3] = (byte) ((encodedTabletId >>> 8) & mask);
+row[4] = (byte) (encodedTabletId & mask);
+row[5] = (byte) ((seq >>> 56) & mask);
+row[6] = (byte) ((seq >>> 48) & mask);
+row[7] = (byte) ((seq >>> 40) & mask);
+row[8] = (byte) ((seq >>> 32) & mask);
+row[9] = (byte) ((seq >>> 24) & mask);
+row[10] = (byte) ((seq >>> 16) & mask);
+row[11] = (byte) ((seq >>> 8) & mask);
+row[12] = (byte) (seq & mask);
 return row;
   }
 
@@ -289,7 +290,13 @@ public class LogFileKey implements 
WritableComparable {
* Extract the tabletId integer from the byte encoded Row.
*/
   private static int getTabletId(byte[] row) {
-int encoded = ((row[1] << 24) + (row[2] << 16) + (row[3] << 8) + row[4]);
+int mask = 0xff; // use a mask of int type to convert byte to int without 
sign extension
+// @formatter:off
+int encoded = (row[1] & mask) << 24 |
+  (row[2] & mask) << 16 |
+  (row[3] & mask) <<  8 |
+  (row[4] & mask);
+// @formatter:on
 return encoded ^ 0x8000;
   }
 
@@ -297,15 +304,16 @@ public class LogFileKey implements 
WritableComparable {
* Extract the sequence long from the byte encoded Row.
*/
   private static long getSequence(byte[] row) {
+long mask = 0xff; // use a mask of long type to convert byte to long 
without sign extension
 // @formatter:off
-return (((long) row[5] << 56) +
-((long) (row[6] & 255) << 48) +
-((long) (row[7] & 255) << 40) +
-((long) (row[8] & 255) << 32) +
-((long) (row[9] & 255) << 24) +
-((row[10] & 255) << 16) +
-((row[11] & 255) << 8) +
-((row[12] & 255)));
+return (row[5]  & mask) << 56 |
+   (row[6]  & mask) << 48 |
+   (row[7]  & mask) << 40 |
+   (row[8]  & mask) << 32 |
+   (row[9]  & mask) << 24 |
+   (row[10] & mask) << 16 |
+   (row[11] & mask) <<  8 |
+   (row[12] & mask);
 // @formatter:on
   }