From a8ed400adab49417779682bc42335c280e1ca245 Mon Sep 17 00:00:00 2001 From: jeremyhi Date: Sun, 4 Feb 2024 16:38:20 +0800 Subject: [PATCH] refactor: protocol module --- .../src/main/java/io/greptime/Status.java | 6 ++- .../greptime/models/IntervalMonthDayNano.java | 12 ----- .../java/io/greptime/models/RowHelper.java | 30 ++++++------ .../models/{Util.java => ValueUtil.java} | 2 +- .../greptime/signal/MetricsSignalHandler.java | 2 +- .../greptime/limit/InFlightLimiterTest.java | 46 +++++++++++++++++ .../{UtilTest.java => ValueUtilTest.java} | 49 +++++++++++++------ 7 files changed, 100 insertions(+), 47 deletions(-) rename ingester-protocol/src/main/java/io/greptime/models/{Util.java => ValueUtil.java} (99%) create mode 100644 ingester-protocol/src/test/java/io/greptime/limit/InFlightLimiterTest.java rename ingester-protocol/src/test/java/io/greptime/models/{UtilTest.java => ValueUtilTest.java} (57%) diff --git a/ingester-protocol/src/main/java/io/greptime/Status.java b/ingester-protocol/src/main/java/io/greptime/Status.java index 141902a..702a0a9 100644 --- a/ingester-protocol/src/main/java/io/greptime/Status.java +++ b/ingester-protocol/src/main/java/io/greptime/Status.java @@ -62,7 +62,10 @@ public enum Status { // TableColumnNotFound(4002), // TableColumnExists(4003), // - DatabaseNotFound(4004), RegionNotFound(4005), RegionAlreadyExists(4006), RegionReadonly(4007), + DatabaseNotFound(4004), // + RegionNotFound(4005), // + RegionAlreadyExists(4006), // + RegionReadonly(4007), // ====== End of catalog related status code ======= // ====== Begin of storage related status code ===== @@ -125,7 +128,6 @@ public int getStatusCode() { /** * Returns {@code true} if the status code is {@link #Success}. */ - @SuppressWarnings("unused") public static boolean isSuccess(int statusCode) { return statusCode == Success.getStatusCode(); } diff --git a/ingester-protocol/src/main/java/io/greptime/models/IntervalMonthDayNano.java b/ingester-protocol/src/main/java/io/greptime/models/IntervalMonthDayNano.java index 4c84eac..69e09d9 100644 --- a/ingester-protocol/src/main/java/io/greptime/models/IntervalMonthDayNano.java +++ b/ingester-protocol/src/main/java/io/greptime/models/IntervalMonthDayNano.java @@ -32,18 +32,6 @@ public IntervalMonthDayNano(int months, int days, long nanoseconds) { this.nanoseconds = nanoseconds; } - public int getMonths() { - return months; - } - - public int getDays() { - return days; - } - - public long getNanoseconds() { - return nanoseconds; - } - @Override public Common.IntervalMonthDayNano into() { return Common.IntervalMonthDayNano.newBuilder() // diff --git a/ingester-protocol/src/main/java/io/greptime/models/RowHelper.java b/ingester-protocol/src/main/java/io/greptime/models/RowHelper.java index 9aaf9ff..046a025 100644 --- a/ingester-protocol/src/main/java/io/greptime/models/RowHelper.java +++ b/ingester-protocol/src/main/java/io/greptime/models/RowHelper.java @@ -47,7 +47,7 @@ public static void addValue(RowData.Row.Builder builder, // valueBuilder.setI32Value((int) value); break; case INT64: - valueBuilder.setI64Value(Util.getLongValue(value)); + valueBuilder.setI64Value(ValueUtil.getLongValue(value)); break; case UINT8: valueBuilder.setU8Value((int) value); @@ -59,7 +59,7 @@ public static void addValue(RowData.Row.Builder builder, // valueBuilder.setU32Value((int) value); break; case UINT64: - valueBuilder.setU64Value(Util.getLongValue(value)); + valueBuilder.setU64Value(ValueUtil.getLongValue(value)); break; case FLOAT32: valueBuilder.setF32Value(((Number) value).floatValue()); @@ -77,46 +77,46 @@ public static void addValue(RowData.Row.Builder builder, // valueBuilder.setStringValue((String) value); break; case DATE: - valueBuilder.setDateValue(Util.getDateValue(value)); + valueBuilder.setDateValue(ValueUtil.getDateValue(value)); break; case DATETIME: - valueBuilder.setDatetimeValue(Util.getDateTimeValue(value)); + valueBuilder.setDatetimeValue(ValueUtil.getDateTimeValue(value)); break; case TIMESTAMP_SECOND: - valueBuilder.setTimestampSecondValue(Util.getLongValue(value)); + valueBuilder.setTimestampSecondValue(ValueUtil.getLongValue(value)); break; case TIMESTAMP_MILLISECOND: - valueBuilder.setTimestampMillisecondValue(Util.getLongValue(value)); + valueBuilder.setTimestampMillisecondValue(ValueUtil.getLongValue(value)); break; case TIMESTAMP_MICROSECOND: - valueBuilder.setTimestampMicrosecondValue(Util.getLongValue(value)); + valueBuilder.setTimestampMicrosecondValue(ValueUtil.getLongValue(value)); break; case TIMESTAMP_NANOSECOND: - valueBuilder.setTimestampNanosecondValue(Util.getLongValue(value)); + valueBuilder.setTimestampNanosecondValue(ValueUtil.getLongValue(value)); break; case TIME_SECOND: - valueBuilder.setTimeSecondValue(Util.getLongValue(value)); + valueBuilder.setTimeSecondValue(ValueUtil.getLongValue(value)); break; case TIME_MILLISECOND: - valueBuilder.setTimeMillisecondValue(Util.getLongValue(value)); + valueBuilder.setTimeMillisecondValue(ValueUtil.getLongValue(value)); break; case TIME_MICROSECOND: - valueBuilder.setTimeMicrosecondValue(Util.getLongValue(value)); + valueBuilder.setTimeMicrosecondValue(ValueUtil.getLongValue(value)); break; case TIME_NANOSECOND: - valueBuilder.setTimeNanosecondValue(Util.getLongValue(value)); + valueBuilder.setTimeNanosecondValue(ValueUtil.getLongValue(value)); break; case INTERVAL_YEAR_MONTH: valueBuilder.setIntervalYearMonthValue((int) value); break; case INTERVAL_DAY_TIME: - valueBuilder.setIntervalDayTimeValue(Util.getLongValue(value)); + valueBuilder.setIntervalDayTimeValue(ValueUtil.getLongValue(value)); break; case INTERVAL_MONTH_DAY_NANO: - valueBuilder.setIntervalMonthDayNanoValue(Util.getIntervalMonthDayNanoValue(value)); + valueBuilder.setIntervalMonthDayNanoValue(ValueUtil.getIntervalMonthDayNanoValue(value)); break; case DECIMAL128: - valueBuilder.setDecimal128Value(Util.getDecimal128Value(dataTypeExtension, value)); + valueBuilder.setDecimal128Value(ValueUtil.getDecimal128Value(dataTypeExtension, value)); break; default: throw new IllegalArgumentException(String.format("Unsupported `data_type`: %s", dataType)); diff --git a/ingester-protocol/src/main/java/io/greptime/models/Util.java b/ingester-protocol/src/main/java/io/greptime/models/ValueUtil.java similarity index 99% rename from ingester-protocol/src/main/java/io/greptime/models/Util.java rename to ingester-protocol/src/main/java/io/greptime/models/ValueUtil.java index de801f5..20d7db0 100644 --- a/ingester-protocol/src/main/java/io/greptime/models/Util.java +++ b/ingester-protocol/src/main/java/io/greptime/models/ValueUtil.java @@ -27,7 +27,7 @@ /** * @author jiachun.fjc */ -public class Util { +public class ValueUtil { static int ONE_DAY_IN_SECONDS = 86400; diff --git a/ingester-protocol/src/main/java/io/greptime/signal/MetricsSignalHandler.java b/ingester-protocol/src/main/java/io/greptime/signal/MetricsSignalHandler.java index 63bec4f..0c96685 100644 --- a/ingester-protocol/src/main/java/io/greptime/signal/MetricsSignalHandler.java +++ b/ingester-protocol/src/main/java/io/greptime/signal/MetricsSignalHandler.java @@ -50,7 +50,7 @@ public void handle(String signalName) { try { File file = FileOutputHelper.getOutputFile(BASE_NAME); - LOG.info("Printing GreptimeDB client metrics triggered by signal: {} to file: {}.", signalName, + LOG.info("Printing GreptimeDB clients metrics triggered by signal: {} to file: {}.", signalName, file.getAbsoluteFile()); try (PrintStream out = new PrintStream(new FileOutputStream(file, true))) { diff --git a/ingester-protocol/src/test/java/io/greptime/limit/InFlightLimiterTest.java b/ingester-protocol/src/test/java/io/greptime/limit/InFlightLimiterTest.java new file mode 100644 index 0000000..edace9d --- /dev/null +++ b/ingester-protocol/src/test/java/io/greptime/limit/InFlightLimiterTest.java @@ -0,0 +1,46 @@ +/* + * Copyright 2023 Greptime Team + * + * Licensed 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 io.greptime.limit; + +import org.junit.Assert; +import org.junit.Test; + +/** + * @author jiachun.fjc + */ +public class InFlightLimiterTest { + + @Test + public void testAcquire() { + InFlightLimiter limiter = new InFlightLimiter(10, "test"); + Assert.assertEquals(10, limiter.maxPermits()); + limiter.acquire(1); + Assert.assertEquals(9, limiter.availablePermits()); + limiter.release(1); + Assert.assertEquals(10, limiter.availablePermits()); + } + + @Test + public void testTryAcquire() { + InFlightLimiter limiter = new InFlightLimiter(10, "test"); + Assert.assertEquals(10, limiter.maxPermits()); + Assert.assertTrue(limiter.tryAcquire(1)); + Assert.assertEquals(9, limiter.availablePermits()); + limiter.release(1); + Assert.assertEquals(10, limiter.availablePermits()); + Assert.assertFalse(limiter.tryAcquire(11)); + } +} diff --git a/ingester-protocol/src/test/java/io/greptime/models/UtilTest.java b/ingester-protocol/src/test/java/io/greptime/models/ValueUtilTest.java similarity index 57% rename from ingester-protocol/src/test/java/io/greptime/models/UtilTest.java rename to ingester-protocol/src/test/java/io/greptime/models/ValueUtilTest.java index 90873e5..af11761 100644 --- a/ingester-protocol/src/test/java/io/greptime/models/UtilTest.java +++ b/ingester-protocol/src/test/java/io/greptime/models/ValueUtilTest.java @@ -30,16 +30,16 @@ /** * @author jiachun.fjc */ -public class UtilTest { +public class ValueUtilTest { @Test public void testGetLongValue() { - Assert.assertEquals(1L, Util.getLongValue(1)); - Assert.assertEquals(1L, Util.getLongValue(1L)); - Assert.assertEquals(1L, Util.getLongValue(1.0)); - Assert.assertEquals(1L, Util.getLongValue(1.0f)); - Assert.assertEquals(1L, Util.getLongValue(BigInteger.valueOf(1))); - Assert.assertEquals(1L, Util.getLongValue(BigDecimal.valueOf(1))); + Assert.assertEquals(1L, ValueUtil.getLongValue(1)); + Assert.assertEquals(1L, ValueUtil.getLongValue(1L)); + Assert.assertEquals(1L, ValueUtil.getLongValue(1.0)); + Assert.assertEquals(1L, ValueUtil.getLongValue(1.0f)); + Assert.assertEquals(1L, ValueUtil.getLongValue(BigInteger.valueOf(1))); + Assert.assertEquals(1L, ValueUtil.getLongValue(BigDecimal.valueOf(1))); } @Test @@ -48,10 +48,10 @@ public void testGetDateValue() { TimeZone gmtTimeZone = TimeZone.getTimeZone("GMT"); cal.setTimeZone(gmtTimeZone); cal.set(1970, Calendar.JANUARY, 2); - Assert.assertEquals(1, Util.getDateValue(cal.getTime())); - Assert.assertEquals(1, Util.getDateValue(Instant.ofEpochSecond(86400))); - Assert.assertEquals(1, Util.getDateValue(LocalDate.ofEpochDay(1))); - Assert.assertEquals(1, Util.getDateValue(1)); + Assert.assertEquals(1, ValueUtil.getDateValue(cal.getTime())); + Assert.assertEquals(1, ValueUtil.getDateValue(Instant.ofEpochSecond(86400))); + Assert.assertEquals(1, ValueUtil.getDateValue(LocalDate.ofEpochDay(1))); + Assert.assertEquals(1, ValueUtil.getDateValue(1)); } @Test @@ -61,10 +61,27 @@ public void testGetDateTimeValue() { cal.setTimeZone(gmtTimeZone); cal.set(1970, Calendar.JANUARY, 2, 0, 0, 0); cal.set(Calendar.MILLISECOND, 111); - Assert.assertEquals(86400111, Util.getDateTimeValue(cal.getTime())); - Assert.assertEquals(86400111, Util.getDateTimeValue(cal.getTime().toInstant())); - Assert.assertEquals(86400000, Util.getDateTimeValue(Instant.ofEpochSecond(86400))); - Assert.assertEquals(86400, Util.getDateTimeValue(86400)); + Assert.assertEquals(86400111, ValueUtil.getDateTimeValue(cal.getTime())); + Assert.assertEquals(86400111, ValueUtil.getDateTimeValue(cal.getTime().toInstant())); + Assert.assertEquals(86400000, ValueUtil.getDateTimeValue(Instant.ofEpochSecond(86400))); + Assert.assertEquals(86400, ValueUtil.getDateTimeValue(86400)); + } + + @Test + public void testGetIntervalMonthDayNanoValue() { + Common.IntervalMonthDayNano result = ValueUtil.getIntervalMonthDayNanoValue(new IntervalMonthDayNano(1, 2, 3)); + Assert.assertEquals(1, result.getMonths()); + Assert.assertEquals(2, result.getDays()); + Assert.assertEquals(3, result.getNanoseconds()); + + // test invalid type + try { + ValueUtil.getIntervalMonthDayNanoValue(1); + Assert.fail(); + } catch (IllegalArgumentException e) { + Assert.assertEquals("Expected type: `IntervalMonthDayNano`, actual: class java.lang.Integer", + e.getMessage()); + } } @Test @@ -84,7 +101,7 @@ public void testGetDecimal128Value() { BigInteger bigInt = BigInteger.valueOf(new Random().nextLong()).shiftLeft(64); bigInt = bigInt.add(BigInteger.valueOf(new Random().nextLong())); BigDecimal value = new BigDecimal(bigInt, scale); - Common.Decimal128 result = Util.getDecimal128Value(dataTypeExtension, value); + Common.Decimal128 result = ValueUtil.getDecimal128Value(dataTypeExtension, value); BigDecimal value2 = TestUtil.getDecimal(result, scale);