From 5fe94019e4db1f6f468b554148c8c8e1f67d7eb3 Mon Sep 17 00:00:00 2001 From: JiaShuo Date: Wed, 20 May 2020 16:29:02 +0800 Subject: [PATCH 1/5] change to bytes --- .../com/xiaomi/infra/pegasus/client/DelRangeOptions.java | 2 +- .../java/com/xiaomi/infra/pegasus/client/PegasusTable.java | 6 +++--- 2 files changed, 4 insertions(+), 4 deletions(-) diff --git a/src/main/java/com/xiaomi/infra/pegasus/client/DelRangeOptions.java b/src/main/java/com/xiaomi/infra/pegasus/client/DelRangeOptions.java index 98266a64..84a60b3d 100644 --- a/src/main/java/com/xiaomi/infra/pegasus/client/DelRangeOptions.java +++ b/src/main/java/com/xiaomi/infra/pegasus/client/DelRangeOptions.java @@ -4,7 +4,7 @@ package com.xiaomi.infra.pegasus.client; public class DelRangeOptions { - public String nextSortKey = ""; + public byte[] nextSortKey = null; public boolean startInclusive = true; // if the startSortKey is included public boolean stopInclusive = false; // if the stopSortKey is included public FilterType sortKeyFilterType = FilterType.FT_NO_FILTER; // filter type for sort key diff --git a/src/main/java/com/xiaomi/infra/pegasus/client/PegasusTable.java b/src/main/java/com/xiaomi/infra/pegasus/client/PegasusTable.java index ecec2f9d..e32dd4f4 100644 --- a/src/main/java/com/xiaomi/infra/pegasus/client/PegasusTable.java +++ b/src/main/java/com/xiaomi/infra/pegasus/client/PegasusTable.java @@ -1469,7 +1469,7 @@ public void delRange( scanOptions.sortKeyFilterType = options.sortKeyFilterType; scanOptions.sortKeyFilterPattern = options.sortKeyFilterPattern; - options.nextSortKey = new String(startSortKey); + options.nextSortKey = startSortKey; PegasusScannerInterface pegasusScanner = getScanner(hashKey, startSortKey, stopSortKey, scanOptions); lastCheckTime = System.currentTimeMillis(); @@ -1494,7 +1494,7 @@ public void delRange( while ((pairs = pegasusScanner.next()) != null) { sortKeys.add(pairs.getKey().getValue()); if (sortKeys.size() == maxBatchDelCount) { - options.nextSortKey = new String(sortKeys.get(0)); + options.nextSortKey = sortKeys.get(0); asyncMultiDel(hashKey, sortKeys, remainingTime).get(remainingTime, TimeUnit.MILLISECONDS); lastCheckTime = System.currentTimeMillis(); remainingTime = (int) (deadlineTime - lastCheckTime); @@ -1514,7 +1514,7 @@ public void delRange( "delRange of hashKey:" + new String(hashKey) + " from sortKey:" - + options.nextSortKey + + new String(options.nextSortKey) + "[index:" + count * maxBatchDelCount + "]" From 2983e3fca4fe1be07f6c42509392d91ec2c147fc Mon Sep 17 00:00:00 2001 From: JiaShuo Date: Wed, 20 May 2020 17:21:34 +0800 Subject: [PATCH 2/5] change to bytes --- .../infra/pegasus/client/PegasusTable.java | 12 +++-- .../infra/pegasus/client/TestBasic.java | 52 ++++++++++++++++--- 2 files changed, 54 insertions(+), 10 deletions(-) diff --git a/src/main/java/com/xiaomi/infra/pegasus/client/PegasusTable.java b/src/main/java/com/xiaomi/infra/pegasus/client/PegasusTable.java index e32dd4f4..15f64e43 100644 --- a/src/main/java/com/xiaomi/infra/pegasus/client/PegasusTable.java +++ b/src/main/java/com/xiaomi/infra/pegasus/client/PegasusTable.java @@ -1455,6 +1455,9 @@ public void multiDel(byte[] hashKey, List sortKeys, int timeout) throws public void delRange( byte[] hashKey, byte[] startSortKey, byte[] stopSortKey, DelRangeOptions options, int timeout) throws PException { + if (hashKey == null || hashKey.length == 0) { + throw new PException("Invalid parameter: hash key can't be empty"); + } if (timeout <= 0) timeout = defaultTimeout; long startTime = System.currentTimeMillis(); long lastCheckTime = startTime; @@ -1474,13 +1477,15 @@ public void delRange( getScanner(hashKey, startSortKey, stopSortKey, scanOptions); lastCheckTime = System.currentTimeMillis(); if (lastCheckTime >= deadlineTime) { + String startSortKeyStr = startSortKey == null ? "" : new String(startSortKey); + String stopSortKeyStr = stopSortKey == null ? "" : new String(stopSortKey); throw new PException( "Getting pegasusScanner takes too long time when delete hashKey:" + new String(hashKey) + ",startSortKey:" - + new String(startSortKey) + + startSortKeyStr + ",stopSortKey:" - + new String(stopSortKey) + + stopSortKeyStr + ",timeUsed:" + (lastCheckTime - startTime) + ":", @@ -1510,11 +1515,12 @@ public void delRange( options.nextSortKey = null; } } catch (InterruptedException | ExecutionException e) { + String nextSortKeyStr = options.nextSortKey == null ? "" : new String(options.nextSortKey); throw new PException( "delRange of hashKey:" + new String(hashKey) + " from sortKey:" - + new String(options.nextSortKey) + + nextSortKeyStr + "[index:" + count * maxBatchDelCount + "]" diff --git a/src/test/java/com/xiaomi/infra/pegasus/client/TestBasic.java b/src/test/java/com/xiaomi/infra/pegasus/client/TestBasic.java index e877aec5..64222829 100644 --- a/src/test/java/com/xiaomi/infra/pegasus/client/TestBasic.java +++ b/src/test/java/com/xiaomi/infra/pegasus/client/TestBasic.java @@ -2364,6 +2364,8 @@ public void delRange() throws PException { PegasusClientInterface client = PegasusClientFactory.getSingletonClient(); DelRangeOptions delRangeOptions = new DelRangeOptions(); + String tableName = "temp"; + // multi set values List> values = new ArrayList>(); int count = 0; @@ -2372,7 +2374,7 @@ public void delRange() throws PException { values.add(Pair.of(("k_" + count).getBytes(), ("v_" + count).getBytes())); count++; } - client.multiSet("temp", "delRange".getBytes(), values); + client.multiSet(tableName, "delRange".getBytes(), values); } catch (Exception e) { e.printStackTrace(); Assert.assertTrue(false); @@ -2381,7 +2383,7 @@ public void delRange() throws PException { // delRange with default delRangeOptions try { client.delRange( - "temp", "delRange".getBytes(), "k_0".getBytes(), "k_90".getBytes(), delRangeOptions); + tableName, "delRange".getBytes(), "k_0".getBytes(), "k_90".getBytes(), delRangeOptions); } catch (Exception e) { e.printStackTrace(); Assert.assertTrue(false); @@ -2403,7 +2405,7 @@ public void delRange() throws PException { List> remainingValue = new ArrayList>(); try { - client.multiGet("temp", "delRange".getBytes(), remainingSortKey, remainingValue); + client.multiGet(tableName, "delRange".getBytes(), remainingSortKey, remainingValue); } catch (Exception e) { e.printStackTrace(); Assert.assertTrue(false); @@ -2430,7 +2432,7 @@ public void delRange() throws PException { delRangeOptions.sortKeyFilterPattern = "k_93".getBytes(); try { client.delRange( - "temp", "delRange".getBytes(), "k_90".getBytes(), "k_95".getBytes(), delRangeOptions); + tableName, "delRange".getBytes(), "k_90".getBytes(), "k_95".getBytes(), delRangeOptions); } catch (Exception e) { e.printStackTrace(); Assert.assertTrue(false); @@ -2439,7 +2441,7 @@ public void delRange() throws PException { remainingValue.clear(); valueStr.clear(); try { - client.multiGet("temp", "delRange".getBytes(), remainingSortKey, remainingValue); + client.multiGet(tableName, "delRange".getBytes(), remainingSortKey, remainingValue); } catch (Exception e) { e.printStackTrace(); Assert.assertTrue(false); @@ -2458,7 +2460,7 @@ public void delRange() throws PException { delRangeOptions.sortKeyFilterPattern = null; try { client.delRange( - "temp", "delRange".getBytes(), "k_90".getBytes(), "k_95".getBytes(), delRangeOptions); + tableName, "delRange".getBytes(), "k_90".getBytes(), "k_95".getBytes(), delRangeOptions); } catch (Exception e) { e.printStackTrace(); Assert.assertTrue(false); @@ -2467,7 +2469,7 @@ public void delRange() throws PException { remainingValue.clear(); valueStr.clear(); try { - client.multiGet("temp", "delRange".getBytes(), remainingSortKey, remainingValue); + client.multiGet(tableName, "delRange".getBytes(), remainingSortKey, remainingValue); } catch (Exception e) { e.printStackTrace(); Assert.assertTrue(false); @@ -2482,6 +2484,42 @@ public void delRange() throws PException { Assert.assertTrue(valueStr.contains("v_97")); Assert.assertTrue(valueStr.contains("v_98")); Assert.assertTrue(valueStr.contains("v_99")); + + remainingValue.clear(); + valueStr.clear(); + DelRangeOptions delRangeOptions2 = new DelRangeOptions(); + + // test hashKey can't be null or "" + + try { + client.delRange( + tableName, "delRange".getBytes(), "k1".getBytes(), "k2".getBytes(), delRangeOptions2); + } catch (Exception e) { + Assert.assertTrue(e.getMessage().contains("Invalid parameter: hash key can't be empty")); + } + + try { + client.delRange(tableName, "".getBytes(), "k1".getBytes(), "k2".getBytes(), delRangeOptions2); + } catch (Exception e) { + Assert.assertTrue(e.getMessage().contains("Invalid parameter: hash key can't be empty")); + } + + // test sortKey can be null, means delete from first to last + try { + client.multiSet(tableName, "delRange".getBytes(), values); + client.delRange(tableName, "delRange".getBytes(), null, null, delRangeOptions2); + client.multiGet(tableName, "delRange".getBytes(), remainingSortKey, remainingValue); + } catch (Exception e) { + e.printStackTrace(); + Assert.assertTrue(false); + } + + for (Pair pair : remainingValue) { + valueStr.add(new String(pair.getValue())); + } + + Assert.assertNull(delRangeOptions.nextSortKey); + Assert.assertEquals(valueStr.size(), 0); } @Test From f5b00a34d15d31d28a0ea4647029347de5374409 Mon Sep 17 00:00:00 2001 From: JiaShuo Date: Wed, 20 May 2020 17:23:52 +0800 Subject: [PATCH 3/5] change to bytes --- src/test/java/com/xiaomi/infra/pegasus/client/TestBasic.java | 1 - 1 file changed, 1 deletion(-) diff --git a/src/test/java/com/xiaomi/infra/pegasus/client/TestBasic.java b/src/test/java/com/xiaomi/infra/pegasus/client/TestBasic.java index 64222829..8c28162a 100644 --- a/src/test/java/com/xiaomi/infra/pegasus/client/TestBasic.java +++ b/src/test/java/com/xiaomi/infra/pegasus/client/TestBasic.java @@ -2490,7 +2490,6 @@ public void delRange() throws PException { DelRangeOptions delRangeOptions2 = new DelRangeOptions(); // test hashKey can't be null or "" - try { client.delRange( tableName, "delRange".getBytes(), "k1".getBytes(), "k2".getBytes(), delRangeOptions2); From 55147cc09159a04ff36933200355be01c352ee50 Mon Sep 17 00:00:00 2001 From: JiaShuo Date: Thu, 21 May 2020 11:28:34 +0800 Subject: [PATCH 4/5] update to junit5 --- pom.xml | 25 +- .../infra/pegasus/client/TestBasic.java | 220 +++++++++--------- 2 files changed, 126 insertions(+), 119 deletions(-) diff --git a/pom.xml b/pom.xml index 0732fece..e8de1ca9 100644 --- a/pom.xml +++ b/pom.xml @@ -8,9 +8,15 @@ Pegasus Java Client - junit - junit - 4.8.1 + org.junit.jupiter + junit-jupiter-engine + 5.3.2 + test + + + org.junit.platform + junit-platform-runner + 1.3.0 test @@ -140,6 +146,19 @@ org.apache.maven.plugins maven-surefire-plugin 2.19.1 + + + + org.junit.platform + junit-platform-surefire-provider + 1.0.0-M4 + + + org.junit.vintage + junit-vintage-engine + 4.12.0-M4 + + ${skipTests} diff --git a/src/test/java/com/xiaomi/infra/pegasus/client/TestBasic.java b/src/test/java/com/xiaomi/infra/pegasus/client/TestBasic.java index 8c28162a..86aa56aa 100644 --- a/src/test/java/com/xiaomi/infra/pegasus/client/TestBasic.java +++ b/src/test/java/com/xiaomi/infra/pegasus/client/TestBasic.java @@ -12,7 +12,9 @@ import org.apache.commons.lang3.RandomStringUtils; import org.apache.commons.lang3.tuple.Pair; import org.junit.Assert; -import org.junit.Test; +import org.junit.jupiter.api.Assertions; +import org.junit.jupiter.api.Assertions.*; +import org.junit.jupiter.api.Test; /** Created by mi on 16-3-22. */ public class TestBasic { @@ -2369,156 +2371,142 @@ public void delRange() throws PException { // multi set values List> values = new ArrayList>(); int count = 0; - try { - while (count < 150) { - values.add(Pair.of(("k_" + count).getBytes(), ("v_" + count).getBytes())); - count++; - } - client.multiSet(tableName, "delRange".getBytes(), values); - } catch (Exception e) { - e.printStackTrace(); - Assert.assertTrue(false); - } - // delRange with default delRangeOptions - try { - client.delRange( - tableName, "delRange".getBytes(), "k_0".getBytes(), "k_90".getBytes(), delRangeOptions); - } catch (Exception e) { - e.printStackTrace(); - Assert.assertTrue(false); + while (count < 150) { + values.add(Pair.of(("k_" + count).getBytes(), ("v_" + count).getBytes())); + count++; } - - Assert.assertTrue(delRangeOptions.nextSortKey == null); List remainingSortKey = new ArrayList(); - - remainingSortKey.add("k_90".getBytes()); - remainingSortKey.add("k_91".getBytes()); - remainingSortKey.add("k_92".getBytes()); - remainingSortKey.add("k_93".getBytes()); - remainingSortKey.add("k_94".getBytes()); - remainingSortKey.add("k_95".getBytes()); - remainingSortKey.add("k_96".getBytes()); - remainingSortKey.add("k_97".getBytes()); - remainingSortKey.add("k_98".getBytes()); - remainingSortKey.add("k_99".getBytes()); List> remainingValue = new ArrayList>(); - try { - client.multiGet(tableName, "delRange".getBytes(), remainingSortKey, remainingValue); - } catch (Exception e) { - e.printStackTrace(); - Assert.assertTrue(false); - } + Assertions.assertNull( + Assertions.assertDoesNotThrow( + () -> { + client.multiSet(tableName, "delRange".getBytes(), values); + client.delRange( + tableName, + "delRange".getBytes(), + "k_0".getBytes(), + "k_90".getBytes(), + delRangeOptions); + + remainingSortKey.add("k_90".getBytes()); + remainingSortKey.add("k_91".getBytes()); + remainingSortKey.add("k_92".getBytes()); + remainingSortKey.add("k_93".getBytes()); + remainingSortKey.add("k_94".getBytes()); + remainingSortKey.add("k_95".getBytes()); + remainingSortKey.add("k_96".getBytes()); + remainingSortKey.add("k_97".getBytes()); + remainingSortKey.add("k_98".getBytes()); + remainingSortKey.add("k_99".getBytes()); + client.multiGet(tableName, "delRange".getBytes(), remainingSortKey, remainingValue); + + return delRangeOptions.nextSortKey; + })); List valueStr = new ArrayList(); for (Pair pair : remainingValue) { valueStr.add(new String(pair.getValue())); } - Assert.assertEquals(10, valueStr.size()); - Assert.assertTrue(valueStr.contains("v_90")); - Assert.assertTrue(valueStr.contains("v_91")); - Assert.assertTrue(valueStr.contains("v_92")); - Assert.assertTrue(valueStr.contains("v_93")); - Assert.assertTrue(valueStr.contains("v_94")); - Assert.assertTrue(valueStr.contains("v_95")); - Assert.assertTrue(valueStr.contains("v_96")); - Assert.assertTrue(valueStr.contains("v_97")); - Assert.assertTrue(valueStr.contains("v_98")); - Assert.assertTrue(valueStr.contains("v_99")); + Assertions.assertEquals(10, valueStr.size()); + Assertions.assertTrue(valueStr.contains("v_90")); + Assertions.assertTrue(valueStr.contains("v_91")); + Assertions.assertTrue(valueStr.contains("v_92")); + Assertions.assertTrue(valueStr.contains("v_93")); + Assertions.assertTrue(valueStr.contains("v_94")); + Assertions.assertTrue(valueStr.contains("v_95")); + Assertions.assertTrue(valueStr.contains("v_96")); + Assertions.assertTrue(valueStr.contains("v_97")); + Assertions.assertTrue(valueStr.contains("v_98")); + Assertions.assertTrue(valueStr.contains("v_99")); + remainingValue.clear(); + valueStr.clear(); // delRange with FT_MATCH_POSTFIX option delRangeOptions.sortKeyFilterType = FilterType.FT_MATCH_POSTFIX; delRangeOptions.sortKeyFilterPattern = "k_93".getBytes(); - try { - client.delRange( - tableName, "delRange".getBytes(), "k_90".getBytes(), "k_95".getBytes(), delRangeOptions); - } catch (Exception e) { - e.printStackTrace(); - Assert.assertTrue(false); - } - remainingValue.clear(); - valueStr.clear(); - try { - client.multiGet(tableName, "delRange".getBytes(), remainingSortKey, remainingValue); - } catch (Exception e) { - e.printStackTrace(); - Assert.assertTrue(false); - } + Assertions.assertDoesNotThrow( + () -> { + client.delRange( + tableName, + "delRange".getBytes(), + "k_90".getBytes(), + "k_95".getBytes(), + delRangeOptions); + client.multiGet(tableName, "delRange".getBytes(), remainingSortKey, remainingValue); + }); for (Pair pair : remainingValue) { valueStr.add(new String(pair.getValue())); } - - Assert.assertEquals(9, valueStr.size()); - Assert.assertTrue(!valueStr.contains("v_93")); + Assertions.assertEquals(9, valueStr.size()); + Assertions.assertTrue(!valueStr.contains("v_93")); + remainingValue.clear(); + valueStr.clear(); // delRange with "*Inclusive" option delRangeOptions.startInclusive = false; delRangeOptions.stopInclusive = true; delRangeOptions.sortKeyFilterType = FilterType.FT_NO_FILTER; delRangeOptions.sortKeyFilterPattern = null; - try { - client.delRange( - tableName, "delRange".getBytes(), "k_90".getBytes(), "k_95".getBytes(), delRangeOptions); - } catch (Exception e) { - e.printStackTrace(); - Assert.assertTrue(false); - } + Assertions.assertDoesNotThrow( + () -> { + client.delRange( + tableName, + "delRange".getBytes(), + "k_90".getBytes(), + "k_95".getBytes(), + delRangeOptions); + client.multiGet(tableName, "delRange".getBytes(), remainingSortKey, remainingValue); + }); - remainingValue.clear(); - valueStr.clear(); - try { - client.multiGet(tableName, "delRange".getBytes(), remainingSortKey, remainingValue); - } catch (Exception e) { - e.printStackTrace(); - Assert.assertTrue(false); - } for (Pair pair : remainingValue) { valueStr.add(new String(pair.getValue())); } - Assert.assertEquals(5, valueStr.size()); - Assert.assertTrue(valueStr.contains("v_90")); - Assert.assertTrue(valueStr.contains("v_96")); - Assert.assertTrue(valueStr.contains("v_97")); - Assert.assertTrue(valueStr.contains("v_98")); - Assert.assertTrue(valueStr.contains("v_99")); - + Assertions.assertEquals(5, valueStr.size()); + Assertions.assertTrue(valueStr.contains("v_90")); + Assertions.assertTrue(valueStr.contains("v_96")); + Assertions.assertTrue(valueStr.contains("v_97")); + Assertions.assertTrue(valueStr.contains("v_98")); + Assertions.assertTrue(valueStr.contains("v_99")); remainingValue.clear(); valueStr.clear(); - DelRangeOptions delRangeOptions2 = new DelRangeOptions(); + DelRangeOptions delRangeOptions2 = new DelRangeOptions(); // test hashKey can't be null or "" - try { - client.delRange( - tableName, "delRange".getBytes(), "k1".getBytes(), "k2".getBytes(), delRangeOptions2); - } catch (Exception e) { - Assert.assertTrue(e.getMessage().contains("Invalid parameter: hash key can't be empty")); - } - - try { - client.delRange(tableName, "".getBytes(), "k1".getBytes(), "k2".getBytes(), delRangeOptions2); - } catch (Exception e) { - Assert.assertTrue(e.getMessage().contains("Invalid parameter: hash key can't be empty")); - } + Assertions.assertEquals( + "{version}: Invalid parameter: hash key can't be empty", + Assertions.assertThrows( + PException.class, + () -> { + client.delRange( + tableName, null, "k1".getBytes(), "k2".getBytes(), delRangeOptions2); + }) + .getMessage()); + + Assertions.assertEquals( + "{version}: Invalid parameter: hash key can't be empty", + Assertions.assertThrows( + PException.class, + () -> { + client.delRange( + tableName, "".getBytes(), "k1".getBytes(), "k2".getBytes(), delRangeOptions2); + }) + .getMessage()); // test sortKey can be null, means delete from first to last - try { - client.multiSet(tableName, "delRange".getBytes(), values); - client.delRange(tableName, "delRange".getBytes(), null, null, delRangeOptions2); - client.multiGet(tableName, "delRange".getBytes(), remainingSortKey, remainingValue); - } catch (Exception e) { - e.printStackTrace(); - Assert.assertTrue(false); - } - - for (Pair pair : remainingValue) { - valueStr.add(new String(pair.getValue())); - } - - Assert.assertNull(delRangeOptions.nextSortKey); - Assert.assertEquals(valueStr.size(), 0); + Assertions.assertNull( + Assertions.assertDoesNotThrow( + () -> { + client.multiSet(tableName, "delRange".getBytes(), values); + client.delRange(tableName, "delRange".getBytes(), null, null, delRangeOptions2); + client.multiGet(tableName, "delRange".getBytes(), remainingSortKey, remainingValue); + return delRangeOptions2.nextSortKey; + })); + + Assertions.assertEquals(remainingValue.size(), 0); } @Test From 52c1859ca2efd3ca7409c0c8b829cb1510b66f3a Mon Sep 17 00:00:00 2001 From: JiaShuo Date: Thu, 21 May 2020 11:37:32 +0800 Subject: [PATCH 5/5] update to junit5 --- pom.xml | 13 ------------- 1 file changed, 13 deletions(-) diff --git a/pom.xml b/pom.xml index e8de1ca9..eedad5b9 100644 --- a/pom.xml +++ b/pom.xml @@ -146,19 +146,6 @@ org.apache.maven.plugins maven-surefire-plugin 2.19.1 - - - - org.junit.platform - junit-platform-surefire-provider - 1.0.0-M4 - - - org.junit.vintage - junit-vintage-engine - 4.12.0-M4 - - ${skipTests}