From c3efb3d424770cfdded7d090a6b34e51676951e6 Mon Sep 17 00:00:00 2001 From: Arthur Chern Date: Mon, 25 Jul 2022 17:02:33 +0800 Subject: [PATCH 1/3] chore: re-format using the new format settings --- .../java/com/ceresdb/common/Endpoint.java | 4 +- .../java/com/ceresdb/common/Lifecycle.java | 3 +- .../common/signal/SignalHandlersLoader.java | 2 +- .../ceresdb/common/signal/SignalHelper.java | 2 +- .../java/com/ceresdb/common/util/Cpus.java | 4 +- .../java/com/ceresdb/common/util/Files.java | 8 +- .../util/LogScheduledThreadPoolExecutor.java | 4 +- .../common/util/LogThreadPoolExecutor.java | 6 +- .../ceresdb/common/util/MetricReporter.java | 86 ++-- .../MetricScheduledThreadPoolExecutor.java | 4 +- .../common/util/MetricThreadPoolExecutor.java | 4 +- .../com/ceresdb/common/util/MetricsUtil.java | 22 +- .../common/util/NamedThreadFactory.java | 12 +- .../com/ceresdb/common/util/Platform.java | 10 +- .../ceresdb/common/util/RcResourceHolder.java | 4 +- .../common/util/SerializingExecutor.java | 9 +- .../common/util/StringBuilderHelper.java | 2 +- .../common/util/SystemPropertyUtil.java | 2 +- .../common/util/ThreadPoolMetricRegistry.java | 3 +- .../ceresdb/common/util/ThreadPoolUtil.java | 18 +- .../common/util/internal/ThrowUtil.java | 4 +- .../internal/UnsafeIntegerFieldUpdater.java | 2 +- .../util/internal/UnsafeLongFieldUpdater.java | 2 +- .../internal/UnsafeReferenceFieldUpdater.java | 2 +- .../util/ExecutorServiceHelperTest.java | 14 +- .../ceresdb/common/util/RcObjectPoolTest.java | 18 +- .../ceresdb/common/util/TopKSelectorTest.java | 4 +- .../test/java/com/ceresdb/CeresDBTest.java | 126 +++-- .../main/java/com/ceresdb/rpc/GrpcClient.java | 312 ++++++------- .../java/com/ceresdb/rpc/GrpcFactory.java | 4 +- .../main/java/com/ceresdb/rpc/IdChannel.java | 4 +- .../com/ceresdb/rpc/MarshallerRegistry.java | 18 +- .../interceptors/AuthHeadersInterceptor.java | 5 +- .../ClientRequestLimitInterceptor.java | 14 +- .../ContextToHeadersInterceptor.java | 3 +- .../rpc/interceptors/MetricInterceptor.java | 14 +- .../com/ceresdb/rpc/limit/Gradient2Limit.java | 36 +- .../rpc/limit/RequestLimiterBuilder.java | 4 +- .../com/ceresdb/rpc/limit/VegasLimit.java | 105 ++--- .../test/java/com/ceresdb/rpc/LimitTest.java | 20 +- .../rpc/RequestLimiterInterceptorTest.java | 51 +- .../ceresdb/http/HttpManagementClient.java | 43 +- .../main/java/com/ceresdb/http/HttpUtil.java | 21 +- .../com/ceresdb/BlockingStreamIterator.java | 6 +- .../main/java/com/ceresdb/CeresDBxClient.java | 40 +- .../main/java/com/ceresdb/LimitedPolicy.java | 24 +- .../main/java/com/ceresdb/MetricParser.java | 13 +- .../ceresdb/MetricParserFactoryProvider.java | 4 +- .../main/java/com/ceresdb/QueryClient.java | 66 ++- .../main/java/com/ceresdb/RouterClient.java | 118 +++-- .../java/com/ceresdb/RpcServiceRegister.java | 50 +- .../src/main/java/com/ceresdb/Utils.java | 33 +- .../main/java/com/ceresdb/WriteClient.java | 172 ++++--- .../src/main/java/com/ceresdb/models/Err.java | 14 +- .../main/java/com/ceresdb/models/QueryOk.java | 2 +- .../main/java/com/ceresdb/models/Record.java | 30 +- .../main/java/com/ceresdb/models/Result.java | 4 +- .../main/java/com/ceresdb/models/Rows.java | 2 +- .../java/com/ceresdb/models/SqlResult.java | 2 +- .../main/java/com/ceresdb/models/WriteOk.java | 4 +- .../com/ceresdb/options/CeresDBxOptions.java | 32 +- .../com/ceresdb/options/QueryOptions.java | 6 +- .../com/ceresdb/options/RouterOptions.java | 6 +- .../com/ceresdb/options/WriteOptions.java | 8 +- .../ceresdb/signal/DisplaySignalHandler.java | 8 +- .../ceresdb/signal/MetricsSignalHandler.java | 10 +- .../signal/ShowRouteCacheSignalHandler.java | 9 +- .../java/com/ceresdb/CeresDBxClientTest.java | 20 +- .../java/com/ceresdb/QueryClientTest.java | 74 ++- .../java/com/ceresdb/WriteClientTest.java | 442 +++++++++--------- .../test/java/com/ceresdb/WriteLimitTest.java | 3 +- .../java/com/ceresdb/models/RowsTest.java | 34 +- .../test/java/com/ceresdb/util/TestUtil.java | 38 +- .../main/java/com/ceresdb/rpc/RpcClient.java | 22 +- .../com/ceresdb/rpc/RpcFactoryProvider.java | 2 +- .../main/java/com/ceresdb/rpc/RpcOptions.java | 34 +- .../rpc/errors/ConnectFailException.java | 3 +- .../rpc/errors/InvokeTimeoutException.java | 3 +- .../com/ceresdb/sql/CeresDBxMetricParser.java | 2 +- .../com/ceresdb/sql/CeresDBxSqlStatement.java | 26 +- .../com/ceresdb/sql/MetricParserTest.java | 4 +- .../com/ceresdb/sql/JSqlMetricParser.java | 2 +- .../java/com/ceresdb/sql/SqlParserUtil.java | 89 ++-- .../com/ceresdb/sql/JSqlMetricParserTest.java | 62 +-- pom.xml | 2 - 85 files changed, 1212 insertions(+), 1348 deletions(-) diff --git a/ceresdb-common/src/main/java/com/ceresdb/common/Endpoint.java b/ceresdb-common/src/main/java/com/ceresdb/common/Endpoint.java index d1b8f12..151c726 100644 --- a/ceresdb-common/src/main/java/com/ceresdb/common/Endpoint.java +++ b/ceresdb-common/src/main/java/com/ceresdb/common/Endpoint.java @@ -30,8 +30,8 @@ public class Endpoint implements Serializable { private static final long serialVersionUID = -7329681263115546100L; @SuppressWarnings("PMD") - private String ip = "0.0.0.0"; - private int port; + private String ip = "0.0.0.0"; + private int port; public static Endpoint of(final String ip, final int port) { return new Endpoint(ip, port); diff --git a/ceresdb-common/src/main/java/com/ceresdb/common/Lifecycle.java b/ceresdb-common/src/main/java/com/ceresdb/common/Lifecycle.java index cbb4e0d..06f74e0 100644 --- a/ceresdb-common/src/main/java/com/ceresdb/common/Lifecycle.java +++ b/ceresdb-common/src/main/java/com/ceresdb/common/Lifecycle.java @@ -36,5 +36,6 @@ public interface Lifecycle { */ void shutdownGracefully(); - default void ensureInitialized() {} + default void ensureInitialized() { + } } diff --git a/ceresdb-common/src/main/java/com/ceresdb/common/signal/SignalHandlersLoader.java b/ceresdb-common/src/main/java/com/ceresdb/common/signal/SignalHandlersLoader.java index 1d76cc8..c21c726 100644 --- a/ceresdb-common/src/main/java/com/ceresdb/common/signal/SignalHandlersLoader.java +++ b/ceresdb-common/src/main/java/com/ceresdb/common/signal/SignalHandlersLoader.java @@ -37,7 +37,7 @@ */ public class SignalHandlersLoader { - private static final Logger LOG = LoggerFactory.getLogger(SignalHandlersLoader.class); + private static final Logger LOG = LoggerFactory.getLogger(SignalHandlersLoader.class); private static final boolean USE_OS_SIGNAL = SystemPropertyUtil.getBool(OptKeys.USE_OS_SIGNAL, true); diff --git a/ceresdb-common/src/main/java/com/ceresdb/common/signal/SignalHelper.java b/ceresdb-common/src/main/java/com/ceresdb/common/signal/SignalHelper.java index 11880e0..3ab5f22 100644 --- a/ceresdb-common/src/main/java/com/ceresdb/common/signal/SignalHelper.java +++ b/ceresdb-common/src/main/java/com/ceresdb/common/signal/SignalHelper.java @@ -30,7 +30,7 @@ */ public final class SignalHelper { - private static final Logger LOG = LoggerFactory.getLogger(SignalHelper.class); + private static final Logger LOG = LoggerFactory.getLogger(SignalHelper.class); private static final SignalAccessor SIGNAL_ACCESSOR = getSignalAccessor0(); diff --git a/ceresdb-common/src/main/java/com/ceresdb/common/util/Cpus.java b/ceresdb-common/src/main/java/com/ceresdb/common/util/Cpus.java index 064c89c..c8db108 100644 --- a/ceresdb-common/src/main/java/com/ceresdb/common/util/Cpus.java +++ b/ceresdb-common/src/main/java/com/ceresdb/common/util/Cpus.java @@ -25,8 +25,8 @@ */ public class Cpus { - private static final int CPUS = SystemPropertyUtil.getInt(OptKeys.AVAILABLE_CPUS, Runtime.getRuntime() - .availableProcessors()); + private static final int CPUS = SystemPropertyUtil.getInt(OptKeys.AVAILABLE_CPUS, + Runtime.getRuntime().availableProcessors()); /** * The configured number of available processors. The default is diff --git a/ceresdb-common/src/main/java/com/ceresdb/common/util/Files.java b/ceresdb-common/src/main/java/com/ceresdb/common/util/Files.java index b817afe..b1a047a 100644 --- a/ceresdb-common/src/main/java/com/ceresdb/common/util/Files.java +++ b/ceresdb-common/src/main/java/com/ceresdb/common/util/Files.java @@ -41,8 +41,8 @@ public static void fsync(final File file) throws IOException { return; } - try (FileChannel fc = FileChannel.open(file.toPath(), isDir ? StandardOpenOption.READ - : StandardOpenOption.WRITE)) { + try (FileChannel fc = FileChannel.open(file.toPath(), + isDir ? StandardOpenOption.READ : StandardOpenOption.WRITE)) { fc.force(true); } } @@ -56,8 +56,8 @@ public static void mkdirIfNotExists(final String path) throws IOException { final File dir = Paths.get(path).toFile().getAbsoluteFile(); if (dir.exists()) { if (!dir.isDirectory()) { - throw new IOException("File " + dir + " exists and is " - + "not a directory. Unable to create directory."); + throw new IOException( + "File " + dir + " exists and is " + "not a directory. Unable to create directory."); } } else if (!dir.mkdirs() && !dir.isDirectory()) { // Double-check that some other thread or process hasn't made diff --git a/ceresdb-common/src/main/java/com/ceresdb/common/util/LogScheduledThreadPoolExecutor.java b/ceresdb-common/src/main/java/com/ceresdb/common/util/LogScheduledThreadPoolExecutor.java index 9b94ee2..edb527a 100644 --- a/ceresdb-common/src/main/java/com/ceresdb/common/util/LogScheduledThreadPoolExecutor.java +++ b/ceresdb-common/src/main/java/com/ceresdb/common/util/LogScheduledThreadPoolExecutor.java @@ -37,8 +37,8 @@ public class LogScheduledThreadPoolExecutor extends ScheduledThreadPoolExecutor private static final Logger LOG = LoggerFactory.getLogger(LogScheduledThreadPoolExecutor.class); - private final int corePoolSize; - private final String name; + private final int corePoolSize; + private final String name; public LogScheduledThreadPoolExecutor(int corePoolSize, String name) { super(corePoolSize); diff --git a/ceresdb-common/src/main/java/com/ceresdb/common/util/LogThreadPoolExecutor.java b/ceresdb-common/src/main/java/com/ceresdb/common/util/LogThreadPoolExecutor.java index dd8bb34..9775963 100644 --- a/ceresdb-common/src/main/java/com/ceresdb/common/util/LogThreadPoolExecutor.java +++ b/ceresdb-common/src/main/java/com/ceresdb/common/util/LogThreadPoolExecutor.java @@ -38,9 +38,9 @@ public class LogThreadPoolExecutor extends ThreadPoolExecutor { private static final Logger LOG = LoggerFactory.getLogger(LogThreadPoolExecutor.class); - private final int corePoolSize; - private final int maximumPoolSize; - private final String name; + private final int corePoolSize; + private final int maximumPoolSize; + private final String name; public LogThreadPoolExecutor(int corePoolSize, int maximumPoolSize, long keepAliveTime, TimeUnit unit, BlockingQueue workQueue, String name) { diff --git a/ceresdb-common/src/main/java/com/ceresdb/common/util/MetricReporter.java b/ceresdb-common/src/main/java/com/ceresdb/common/util/MetricReporter.java index 75c3dd4..6c44539 100644 --- a/ceresdb-common/src/main/java/com/ceresdb/common/util/MetricReporter.java +++ b/ceresdb-common/src/main/java/com/ceresdb/common/util/MetricReporter.java @@ -61,10 +61,10 @@ public static Builder forRegistry(final MetricRegistry registry) { public void report() { synchronized (this) { report(this.registry.getGauges(this.filter), // - this.registry.getCounters(this.filter), // - this.registry.getHistograms(this.filter), // - this.registry.getMeters(this.filter), // - this.registry.getTimers(this.filter)); + this.registry.getCounters(this.filter), // + this.registry.getHistograms(this.filter), // + this.registry.getMeters(this.filter), // + this.registry.getTimers(this.filter)); } } @@ -207,19 +207,19 @@ public Builder disabledMetricAttributes(final Set disabledMetri */ public MetricReporter build() { return new MetricReporter(this.registry, // - this.output, // - this.prefix, // - this.locale, // - this.clock, // - this.timeZone, // - this.rateUnit, // - this.durationUnit, // - this.filter, // - this.disabledMetricAttributes); + this.output, // + this.prefix, // + this.locale, // + this.clock, // + this.timeZone, // + this.rateUnit, // + this.durationUnit, // + this.filter, // + this.disabledMetricAttributes); } } - private static final int CONSOLE_WIDTH = 80; + private static final int CONSOLE_WIDTH = 80; private final MetricRegistry registry; private final Set disabledMetricAttributes; @@ -256,8 +256,8 @@ private MetricReporter(MetricRegistry registry, // this.durationFactor = durationUnit.toNanos(1); this.durationUnit = durationUnit.toString().toLowerCase(Locale.US); this.filter = filter; - this.disabledMetricAttributes = disabledMetricAttributes != null ? disabledMetricAttributes : Collections - .emptySet(); + this.disabledMetricAttributes = disabledMetricAttributes != null ? disabledMetricAttributes : + Collections.emptySet(); } @SuppressWarnings("rawtypes") @@ -320,13 +320,13 @@ public void report(final SortedMap gauges, final SortedMap entry) { @@ -339,60 +339,60 @@ private void printGauge(final Gauge gauge) { private void printHistogram(final Histogram histogram) { printIfEnabled(MetricAttribute.COUNT, - String.format(this.locale, " count = %d", histogram.getCount())); + String.format(this.locale, " count = %d", histogram.getCount())); final Snapshot snapshot = histogram.getSnapshot(); printIfEnabled(MetricAttribute.MIN, String.format(this.locale, " min = %d", snapshot.getMin())); printIfEnabled(MetricAttribute.MAX, String.format(this.locale, " max = %d", snapshot.getMax())); printIfEnabled(MetricAttribute.MEAN, - String.format(this.locale, " mean = %2.2f", snapshot.getMean())); + String.format(this.locale, " mean = %2.2f", snapshot.getMean())); printIfEnabled(MetricAttribute.STDDEV, - String.format(this.locale, " stddev = %2.2f", snapshot.getStdDev())); + String.format(this.locale, " stddev = %2.2f", snapshot.getStdDev())); printIfEnabled(MetricAttribute.P50, - String.format(this.locale, " median = %2.2f", snapshot.getMedian())); + String.format(this.locale, " median = %2.2f", snapshot.getMedian())); printIfEnabled(MetricAttribute.P75, - String.format(this.locale, " 75%% <= %2.2f", snapshot.get75thPercentile())); + String.format(this.locale, " 75%% <= %2.2f", snapshot.get75thPercentile())); printIfEnabled(MetricAttribute.P95, - String.format(this.locale, " 95%% <= %2.2f", snapshot.get95thPercentile())); + String.format(this.locale, " 95%% <= %2.2f", snapshot.get95thPercentile())); printIfEnabled(MetricAttribute.P98, - String.format(this.locale, " 98%% <= %2.2f", snapshot.get98thPercentile())); + String.format(this.locale, " 98%% <= %2.2f", snapshot.get98thPercentile())); printIfEnabled(MetricAttribute.P99, - String.format(this.locale, " 99%% <= %2.2f", snapshot.get99thPercentile())); + String.format(this.locale, " 99%% <= %2.2f", snapshot.get99thPercentile())); printIfEnabled(MetricAttribute.P999, - String.format(this.locale, " 99.9%% <= %2.2f", snapshot.get999thPercentile())); + String.format(this.locale, " 99.9%% <= %2.2f", snapshot.get999thPercentile())); } private void printTimer(final Timer timer) { final Snapshot snapshot = timer.getSnapshot(); printIfEnabled(MetricAttribute.COUNT, String.format(this.locale, " count = %d", timer.getCount())); printIfEnabled(MetricAttribute.MEAN_RATE, String.format(this.locale, " mean rate = %2.2f calls/%s", - convertRate(timer.getMeanRate()), this.rateUnit)); + convertRate(timer.getMeanRate()), this.rateUnit)); printIfEnabled(MetricAttribute.M1_RATE, String.format(this.locale, " 1-minute rate = %2.2f calls/%s", - convertRate(timer.getOneMinuteRate()), this.rateUnit)); + convertRate(timer.getOneMinuteRate()), this.rateUnit)); printIfEnabled(MetricAttribute.M5_RATE, String.format(this.locale, " 5-minute rate = %2.2f calls/%s", - convertRate(timer.getFiveMinuteRate()), this.rateUnit)); + convertRate(timer.getFiveMinuteRate()), this.rateUnit)); printIfEnabled(MetricAttribute.M15_RATE, String.format(this.locale, " 15-minute rate = %2.2f calls/%s", - convertRate(timer.getFifteenMinuteRate()), this.rateUnit)); + convertRate(timer.getFifteenMinuteRate()), this.rateUnit)); printIfEnabled(MetricAttribute.MIN, String.format(this.locale, " min = %2.2f %s", - convertDuration(snapshot.getMin()), this.durationUnit)); + convertDuration(snapshot.getMin()), this.durationUnit)); printIfEnabled(MetricAttribute.MAX, String.format(this.locale, " max = %2.2f %s", - convertDuration(snapshot.getMax()), this.durationUnit)); + convertDuration(snapshot.getMax()), this.durationUnit)); printIfEnabled(MetricAttribute.MEAN, String.format(this.locale, " mean = %2.2f %s", - convertDuration(snapshot.getMean()), this.durationUnit)); + convertDuration(snapshot.getMean()), this.durationUnit)); printIfEnabled(MetricAttribute.STDDEV, String.format(this.locale, " stddev = %2.2f %s", - convertDuration(snapshot.getStdDev()), this.durationUnit)); + convertDuration(snapshot.getStdDev()), this.durationUnit)); printIfEnabled(MetricAttribute.P50, String.format(this.locale, " median = %2.2f %s", - convertDuration(snapshot.getMedian()), this.durationUnit)); + convertDuration(snapshot.getMedian()), this.durationUnit)); printIfEnabled(MetricAttribute.P75, String.format(this.locale, " 75%% <= %2.2f %s", - convertDuration(snapshot.get75thPercentile()), this.durationUnit)); + convertDuration(snapshot.get75thPercentile()), this.durationUnit)); printIfEnabled(MetricAttribute.P95, String.format(this.locale, " 95%% <= %2.2f %s", - convertDuration(snapshot.get95thPercentile()), this.durationUnit)); + convertDuration(snapshot.get95thPercentile()), this.durationUnit)); printIfEnabled(MetricAttribute.P98, String.format(this.locale, " 98%% <= %2.2f %s", - convertDuration(snapshot.get98thPercentile()), this.durationUnit)); + convertDuration(snapshot.get98thPercentile()), this.durationUnit)); printIfEnabled(MetricAttribute.P99, String.format(this.locale, " 99%% <= %2.2f %s", - convertDuration(snapshot.get99thPercentile()), this.durationUnit)); + convertDuration(snapshot.get99thPercentile()), this.durationUnit)); printIfEnabled(MetricAttribute.P999, String.format(this.locale, " 99.9%% <= %2.2f %s", - convertDuration(snapshot.get999thPercentile()), this.durationUnit)); + convertDuration(snapshot.get999thPercentile()), this.durationUnit)); } private void printWithBanner(final String s, final char c) { diff --git a/ceresdb-common/src/main/java/com/ceresdb/common/util/MetricScheduledThreadPoolExecutor.java b/ceresdb-common/src/main/java/com/ceresdb/common/util/MetricScheduledThreadPoolExecutor.java index 1fa488f..db1cc3d 100644 --- a/ceresdb-common/src/main/java/com/ceresdb/common/util/MetricScheduledThreadPoolExecutor.java +++ b/ceresdb-common/src/main/java/com/ceresdb/common/util/MetricScheduledThreadPoolExecutor.java @@ -55,8 +55,8 @@ protected void beforeExecute(final Thread t, final Runnable r) { @Override protected void afterExecute(final Runnable r, final Throwable t) { ThreadPoolMetricRegistry.metricRegistry() // - .timer("scheduled_thread_pool." + getName()) // - .update(ThreadPoolMetricRegistry.finish(), TimeUnit.MILLISECONDS); + .timer("scheduled_thread_pool." + getName()) // + .update(ThreadPoolMetricRegistry.finish(), TimeUnit.MILLISECONDS); super.afterExecute(r, t); } } diff --git a/ceresdb-common/src/main/java/com/ceresdb/common/util/MetricThreadPoolExecutor.java b/ceresdb-common/src/main/java/com/ceresdb/common/util/MetricThreadPoolExecutor.java index 3a7699e..1b036ea 100644 --- a/ceresdb-common/src/main/java/com/ceresdb/common/util/MetricThreadPoolExecutor.java +++ b/ceresdb-common/src/main/java/com/ceresdb/common/util/MetricThreadPoolExecutor.java @@ -59,8 +59,8 @@ protected void beforeExecute(final Thread t, final Runnable r) { @Override protected void afterExecute(final Runnable r, final Throwable t) { ThreadPoolMetricRegistry.metricRegistry() // - .timer("thread_pool." + getName()) // - .update(ThreadPoolMetricRegistry.finish(), TimeUnit.MILLISECONDS); + .timer("thread_pool." + getName()) // + .update(ThreadPoolMetricRegistry.finish(), TimeUnit.MILLISECONDS); super.afterExecute(r, t); } } diff --git a/ceresdb-common/src/main/java/com/ceresdb/common/util/MetricsUtil.java b/ceresdb-common/src/main/java/com/ceresdb/common/util/MetricsUtil.java index 53183c7..f8505b6 100644 --- a/ceresdb-common/src/main/java/com/ceresdb/common/util/MetricsUtil.java +++ b/ceresdb-common/src/main/java/com/ceresdb/common/util/MetricsUtil.java @@ -38,29 +38,29 @@ */ public final class MetricsUtil { - private static final Logger LOG = LoggerFactory.getLogger(MetricsUtil.class); + private static final Logger LOG = LoggerFactory.getLogger(MetricsUtil.class); private static final MetricRegistry METRIC_REGISTRY = new MetricRegistry(); private static final ScheduledReporter SCHEDULED_REPORTER; static { final ScheduledExecutorService scheduledPool = ThreadPoolUtil.newScheduledBuilder() // - .enableMetric(true) // - .coreThreads(1) // - .poolName("metrics.reporter") // - .threadFactory(new NamedThreadFactory("metrics.reporter", true)) // - .build(); + .enableMetric(true) // + .coreThreads(1) // + .poolName("metrics.reporter") // + .threadFactory(new NamedThreadFactory("metrics.reporter", true)) // + .build(); SCHEDULED_REPORTER = createReporter(scheduledPool); } private static ScheduledReporter createReporter(final ScheduledExecutorService scheduledPool) { try { return Slf4jReporter.forRegistry(MetricsUtil.METRIC_REGISTRY) // - .withLoggingLevel(Slf4jReporter.LoggingLevel.INFO) // - .outputTo(LOG) // - .scheduleOn(scheduledPool) // - .shutdownExecutorOnStop(true) // - .build(); + .withLoggingLevel(Slf4jReporter.LoggingLevel.INFO) // + .outputTo(LOG) // + .scheduleOn(scheduledPool) // + .shutdownExecutorOnStop(true) // + .build(); } catch (final Throwable ex) { LOG.warn("Fail to create metrics reporter.", ex); return null; diff --git a/ceresdb-common/src/main/java/com/ceresdb/common/util/NamedThreadFactory.java b/ceresdb-common/src/main/java/com/ceresdb/common/util/NamedThreadFactory.java index 44a304e..e7ee8b9 100644 --- a/ceresdb-common/src/main/java/com/ceresdb/common/util/NamedThreadFactory.java +++ b/ceresdb-common/src/main/java/com/ceresdb/common/util/NamedThreadFactory.java @@ -29,15 +29,15 @@ */ public class NamedThreadFactory implements ThreadFactory { - private static final Logger LOG = LoggerFactory.getLogger(NamedThreadFactory.class); + private static final Logger LOG = LoggerFactory.getLogger(NamedThreadFactory.class); private static final AtomicInteger FACTORY_ID = new AtomicInteger(0); - private final AtomicInteger id = new AtomicInteger(0); - private final String name; - private final boolean daemon; - private final int priority; - private final ThreadGroup group; + private final AtomicInteger id = new AtomicInteger(0); + private final String name; + private final boolean daemon; + private final int priority; + private final ThreadGroup group; public NamedThreadFactory(String name) { this(name, false, Thread.NORM_PRIORITY); diff --git a/ceresdb-common/src/main/java/com/ceresdb/common/util/Platform.java b/ceresdb-common/src/main/java/com/ceresdb/common/util/Platform.java index d0346c5..36da302 100644 --- a/ceresdb-common/src/main/java/com/ceresdb/common/util/Platform.java +++ b/ceresdb-common/src/main/java/com/ceresdb/common/util/Platform.java @@ -29,10 +29,10 @@ */ public class Platform { - private static final Logger LOG = LoggerFactory.getLogger(Platform.class); + private static final Logger LOG = LoggerFactory.getLogger(Platform.class); - private static final String WIN_KEY = "win"; - private static final String MAC_KEY = "mac os x"; + private static final String WIN_KEY = "win"; + private static final String MAC_KEY = "mac os x"; private static final boolean IS_WINDOWS = isWindows0(); private static final boolean IS_MAC = isMac0(); @@ -69,7 +69,7 @@ private static boolean isWindows0() { private static boolean checkOS(final String osKey) { return SystemPropertyUtil.get(OptKeys.OS_NAME, "") // - .toLowerCase(Locale.US) // - .contains(osKey); + .toLowerCase(Locale.US) // + .contains(osKey); } } diff --git a/ceresdb-common/src/main/java/com/ceresdb/common/util/RcResourceHolder.java b/ceresdb-common/src/main/java/com/ceresdb/common/util/RcResourceHolder.java index ccc186c..e7fb097 100644 --- a/ceresdb-common/src/main/java/com/ceresdb/common/util/RcResourceHolder.java +++ b/ceresdb-common/src/main/java/com/ceresdb/common/util/RcResourceHolder.java @@ -28,7 +28,7 @@ */ public class RcResourceHolder { - private static final Logger LOG = LoggerFactory.getLogger(RcResourceHolder.class); + private static final Logger LOG = LoggerFactory.getLogger(RcResourceHolder.class); private final Map, Instance> instances = new IdentityHashMap<>(); @@ -47,7 +47,7 @@ public synchronized void release(final ObjectPool.Resource resource, final T final Instance cached = this.instances.get(resource); Requires.requireNonNull(cached, "No cached instance found for " + resource); Requires.requireTrue(returned == cached.payload(), "Releasing the wrong instance, expected=%s, actual=%s", - cached.payload(), returned); + cached.payload(), returned); Requires.requireTrue(cached.rc() > 0, "RefCount has already reached zero"); if (cached.decAndGet() == 0) { LOG.info("[RcResourceHolder] close instance: {}.", cached); diff --git a/ceresdb-common/src/main/java/com/ceresdb/common/util/SerializingExecutor.java b/ceresdb-common/src/main/java/com/ceresdb/common/util/SerializingExecutor.java index 31c4ebe..b7aef5d 100644 --- a/ceresdb-common/src/main/java/com/ceresdb/common/util/SerializingExecutor.java +++ b/ceresdb-common/src/main/java/com/ceresdb/common/util/SerializingExecutor.java @@ -36,10 +36,9 @@ */ public class SerializingExecutor implements Executor { - private static final Logger LOG = LoggerFactory - .getLogger(SerializingExecutor.class); + private static final Logger LOG = LoggerFactory.getLogger(SerializingExecutor.class); - private static final int QUEUE_SIZE_THRESHOLD = 512; + private static final int QUEUE_SIZE_THRESHOLD = 512; private final String name; private final Timer singleTaskTimer; @@ -47,8 +46,8 @@ public class SerializingExecutor implements Executor { private final Histogram drainNumHis; private final Thread.UncaughtExceptionHandler uncaughtExceptionHandler; - private final Queue queue = new ConcurrentLinkedQueue<>(); - private final AtomicReference drainingThread = new AtomicReference<>(); + private final Queue queue = new ConcurrentLinkedQueue<>(); + private final AtomicReference drainingThread = new AtomicReference<>(); public SerializingExecutor(String name) { this(name, LogUncaughtExceptionHandler.INSTANCE); diff --git a/ceresdb-common/src/main/java/com/ceresdb/common/util/StringBuilderHelper.java b/ceresdb-common/src/main/java/com/ceresdb/common/util/StringBuilderHelper.java index 5fdc705..1706b93 100644 --- a/ceresdb-common/src/main/java/com/ceresdb/common/util/StringBuilderHelper.java +++ b/ceresdb-common/src/main/java/com/ceresdb/common/util/StringBuilderHelper.java @@ -29,7 +29,7 @@ public class StringBuilderHelper { private static final ThreadLocal HOLDER_THREAD_LOCAL; static { - MAX_BUF_SIZE = 1024 << 3; // 8k + MAX_BUF_SIZE = 1024 << 3; // 8k HOLDER_THREAD_LOCAL = ThreadLocal.withInitial(StringBuilderHolder::new); } diff --git a/ceresdb-common/src/main/java/com/ceresdb/common/util/SystemPropertyUtil.java b/ceresdb-common/src/main/java/com/ceresdb/common/util/SystemPropertyUtil.java index 9e0628e..2173bb3 100644 --- a/ceresdb-common/src/main/java/com/ceresdb/common/util/SystemPropertyUtil.java +++ b/ceresdb-common/src/main/java/com/ceresdb/common/util/SystemPropertyUtil.java @@ -170,7 +170,7 @@ public static long getLong(final String key, final long def) { } LOG.warn("Unable to parse the long integer system property '{}':{} - using the default value: {}.", key, value, - def); + def); return def; } diff --git a/ceresdb-common/src/main/java/com/ceresdb/common/util/ThreadPoolMetricRegistry.java b/ceresdb-common/src/main/java/com/ceresdb/common/util/ThreadPoolMetricRegistry.java index 21e42ae..f1d4e5c 100644 --- a/ceresdb-common/src/main/java/com/ceresdb/common/util/ThreadPoolMetricRegistry.java +++ b/ceresdb-common/src/main/java/com/ceresdb/common/util/ThreadPoolMetricRegistry.java @@ -25,7 +25,8 @@ */ public class ThreadPoolMetricRegistry { - private static final ThreadLocal TIME_THREAD_LOCAL = ThreadLocal.withInitial(() -> Clock.defaultClock().getTick()); + private static final ThreadLocal TIME_THREAD_LOCAL = ThreadLocal + .withInitial(() -> Clock.defaultClock().getTick()); /** * Return the global registry of metric instances. diff --git a/ceresdb-common/src/main/java/com/ceresdb/common/util/ThreadPoolUtil.java b/ceresdb-common/src/main/java/com/ceresdb/common/util/ThreadPoolUtil.java index 48b0e35..f4eb055 100644 --- a/ceresdb-common/src/main/java/com/ceresdb/common/util/ThreadPoolUtil.java +++ b/ceresdb-common/src/main/java/com/ceresdb/common/util/ThreadPoolUtil.java @@ -70,11 +70,10 @@ public static ScheduledPoolBuilder newScheduledBuilder() { */ public static ThreadPoolExecutor newThreadPool(final String poolName, final boolean enableMetric, final int coreThreads, final int maximumThreads, - final long keepAliveSeconds, - final BlockingQueue workQueue, + final long keepAliveSeconds, final BlockingQueue workQueue, final ThreadFactory threadFactory) { return newThreadPool(poolName, enableMetric, coreThreads, maximumThreads, keepAliveSeconds, workQueue, - threadFactory, defaultHandler); + threadFactory, defaultHandler); } /** @@ -105,17 +104,16 @@ public static ThreadPoolExecutor newThreadPool(final String poolName, final bool */ public static ThreadPoolExecutor newThreadPool(final String poolName, final boolean enableMetric, final int coreThreads, final int maximumThreads, - final long keepAliveSeconds, - final BlockingQueue workQueue, + final long keepAliveSeconds, final BlockingQueue workQueue, final ThreadFactory threadFactory, final RejectedExecutionHandler rejectedHandler) { final TimeUnit unit = TimeUnit.SECONDS; if (enableMetric) { return new MetricThreadPoolExecutor(coreThreads, maximumThreads, keepAliveSeconds, unit, workQueue, - threadFactory, rejectedHandler, poolName); + threadFactory, rejectedHandler, poolName); } else { return new LogThreadPoolExecutor(coreThreads, maximumThreads, keepAliveSeconds, unit, workQueue, - threadFactory, rejectedHandler, poolName); + threadFactory, rejectedHandler, poolName); } } @@ -233,8 +231,8 @@ public ThreadPoolExecutor build() { Requires.requireNonNull(this.threadFactory, "threadFactory"); Requires.requireNonNull(this.handler, "handler"); - return ThreadPoolUtil.newThreadPool(this.poolName, this.enableMetric, this.coreThreads, - this.maximumThreads, this.keepAliveSeconds, this.workQueue, this.threadFactory, this.handler); + return ThreadPoolUtil.newThreadPool(this.poolName, this.enableMetric, this.coreThreads, this.maximumThreads, + this.keepAliveSeconds, this.workQueue, this.threadFactory, this.handler); } } @@ -279,7 +277,7 @@ public ScheduledThreadPoolExecutor build() { Requires.requireNonNull(this.handler, "handler"); return ThreadPoolUtil.newScheduledThreadPool(this.poolName, this.enableMetric, this.coreThreads, - this.threadFactory, this.handler); + this.threadFactory, this.handler); } } } diff --git a/ceresdb-common/src/main/java/com/ceresdb/common/util/internal/ThrowUtil.java b/ceresdb-common/src/main/java/com/ceresdb/common/util/internal/ThrowUtil.java index e147e56..7f8623e 100644 --- a/ceresdb-common/src/main/java/com/ceresdb/common/util/internal/ThrowUtil.java +++ b/ceresdb-common/src/main/java/com/ceresdb/common/util/internal/ThrowUtil.java @@ -23,8 +23,8 @@ */ public final class ThrowUtil { - private static final ReferenceFieldUpdater causeUpdater = Updaters.newReferenceFieldUpdater( - Throwable.class, "cause"); + private static final ReferenceFieldUpdater causeUpdater = Updaters + .newReferenceFieldUpdater(Throwable.class, "cause"); /** * Raises an exception bypassing compiler checks for checked exceptions. diff --git a/ceresdb-common/src/main/java/com/ceresdb/common/util/internal/UnsafeIntegerFieldUpdater.java b/ceresdb-common/src/main/java/com/ceresdb/common/util/internal/UnsafeIntegerFieldUpdater.java index c965c4e..06415ad 100644 --- a/ceresdb-common/src/main/java/com/ceresdb/common/util/internal/UnsafeIntegerFieldUpdater.java +++ b/ceresdb-common/src/main/java/com/ceresdb/common/util/internal/UnsafeIntegerFieldUpdater.java @@ -29,7 +29,7 @@ final class UnsafeIntegerFieldUpdater implements IntegerFieldUpdater { private final sun.misc.Unsafe unsafe; UnsafeIntegerFieldUpdater(sun.misc.Unsafe unsafe, Class tClass, String fieldName) - throws NoSuchFieldException { + throws NoSuchFieldException { final Field field = tClass.getDeclaredField(fieldName); if (unsafe == null) { throw new NullPointerException("unsafe"); diff --git a/ceresdb-common/src/main/java/com/ceresdb/common/util/internal/UnsafeLongFieldUpdater.java b/ceresdb-common/src/main/java/com/ceresdb/common/util/internal/UnsafeLongFieldUpdater.java index 55b5bb5..d6024d7 100644 --- a/ceresdb-common/src/main/java/com/ceresdb/common/util/internal/UnsafeLongFieldUpdater.java +++ b/ceresdb-common/src/main/java/com/ceresdb/common/util/internal/UnsafeLongFieldUpdater.java @@ -29,7 +29,7 @@ final class UnsafeLongFieldUpdater implements LongFieldUpdater { private final sun.misc.Unsafe unsafe; UnsafeLongFieldUpdater(sun.misc.Unsafe unsafe, Class tClass, String fieldName) - throws NoSuchFieldException { + throws NoSuchFieldException { final Field field = tClass.getDeclaredField(fieldName); if (unsafe == null) { throw new NullPointerException("unsafe"); diff --git a/ceresdb-common/src/main/java/com/ceresdb/common/util/internal/UnsafeReferenceFieldUpdater.java b/ceresdb-common/src/main/java/com/ceresdb/common/util/internal/UnsafeReferenceFieldUpdater.java index 97be595..5966887 100644 --- a/ceresdb-common/src/main/java/com/ceresdb/common/util/internal/UnsafeReferenceFieldUpdater.java +++ b/ceresdb-common/src/main/java/com/ceresdb/common/util/internal/UnsafeReferenceFieldUpdater.java @@ -29,7 +29,7 @@ final class UnsafeReferenceFieldUpdater implements ReferenceFieldUpdater tClass, String fieldName) - throws NoSuchFieldException { + throws NoSuchFieldException { final Field field = tClass.getDeclaredField(fieldName); if (unsafe == null) { throw new NullPointerException("unsafe"); diff --git a/ceresdb-common/src/test/java/com/ceresdb/common/util/ExecutorServiceHelperTest.java b/ceresdb-common/src/test/java/com/ceresdb/common/util/ExecutorServiceHelperTest.java index 563cc58..0800456 100644 --- a/ceresdb-common/src/test/java/com/ceresdb/common/util/ExecutorServiceHelperTest.java +++ b/ceresdb-common/src/test/java/com/ceresdb/common/util/ExecutorServiceHelperTest.java @@ -35,13 +35,13 @@ public void shutdownNullTest() { @Test public void shutdownNotStart() { final ExecutorService e = ThreadPoolUtil.newBuilder().poolName("test_shutdown") // - .coreThreads(1) // - .maximumThreads(1) // - .keepAliveSeconds(100L) // - .workQueue(new SynchronousQueue<>()) // - .enableMetric(false) // - .threadFactory(new NamedThreadFactory("test_shutdown")) // - .build(); + .coreThreads(1) // + .maximumThreads(1) // + .keepAliveSeconds(100L) // + .workQueue(new SynchronousQueue<>()) // + .enableMetric(false) // + .threadFactory(new NamedThreadFactory("test_shutdown")) // + .build(); Assert.assertTrue(ExecutorServiceHelper.shutdownAndAwaitTermination(e)); } } diff --git a/ceresdb-common/src/test/java/com/ceresdb/common/util/RcObjectPoolTest.java b/ceresdb-common/src/test/java/com/ceresdb/common/util/RcObjectPoolTest.java index bbde562..ecee09e 100644 --- a/ceresdb-common/src/test/java/com/ceresdb/common/util/RcObjectPoolTest.java +++ b/ceresdb-common/src/test/java/com/ceresdb/common/util/RcObjectPoolTest.java @@ -27,17 +27,17 @@ public class RcObjectPoolTest { static final ObjectPool.Resource RESOURCE = new ObjectPool.Resource() { - @Override - public Object create() { - return new Object(); - } + @Override + public Object create() { + return new Object(); + } - @Override - public void close(final Object instance) { - } - }; + @Override + public void close(final Object instance) { + } + }; - static final RcObjectPool POOL = new RcObjectPool<>(RESOURCE); + static final RcObjectPool POOL = new RcObjectPool<>(RESOURCE); @Test public void sharedTest() { diff --git a/ceresdb-common/src/test/java/com/ceresdb/common/util/TopKSelectorTest.java b/ceresdb-common/src/test/java/com/ceresdb/common/util/TopKSelectorTest.java index c9dfea3..93cd1aa 100644 --- a/ceresdb-common/src/test/java/com/ceresdb/common/util/TopKSelectorTest.java +++ b/ceresdb-common/src/test/java/com/ceresdb/common/util/TopKSelectorTest.java @@ -51,8 +51,8 @@ public void selectTopKTest() { } }; - final List top5Keys = TopKSelector.selectTopK(map.entrySet(), 3, - (o1, o2) -> -o1.getValue().compareTo(o2.getValue())) // + final List top5Keys = TopKSelector + .selectTopK(map.entrySet(), 3, (o1, o2) -> -o1.getValue().compareTo(o2.getValue())) // .map(Map.Entry::getKey) // .collect(Collectors.toList()); Assert.assertEquals(Arrays.asList("j", "k", "l"), top5Keys); diff --git a/ceresdb-example/src/test/java/com/ceresdb/CeresDBTest.java b/ceresdb-example/src/test/java/com/ceresdb/CeresDBTest.java index cb8d14a..a154286 100644 --- a/ceresdb-example/src/test/java/com/ceresdb/CeresDBTest.java +++ b/ceresdb-example/src/test/java/com/ceresdb/CeresDBTest.java @@ -60,12 +60,12 @@ */ public class CeresDBTest { - private static final Logger LOG = LoggerFactory.getLogger(CeresDBTest.class); + private static final Logger LOG = LoggerFactory.getLogger(CeresDBTest.class); - private String TEST_TABLE_NAME = "all_type_test_table_%d"; + private String TEST_TABLE_NAME = "all_type_test_table_%d"; - private CeresDBxOptions opts; - private CeresDBxClient client; + private CeresDBxOptions opts; + private CeresDBxClient client; @Before public void before() { @@ -75,11 +75,11 @@ public void before() { rpcOpts.setLimitKind(RpcOptions.LimitKind.Gradient); rpcOpts.setLogOnLimitChange(true); this.opts = CeresDBxOptions.newBuilder("127.0.0.1", 8831, 5000) // - .tenant("public", "sub_test", "test_token") // - .rpcOptions(rpcOpts) // - .writeMaxRetries(0) // - .readMaxRetries(1) // - .build(); + .tenant("public", "sub_test", "test_token") // + .rpcOptions(rpcOpts) // + .writeMaxRetries(0) // + .readMaxRetries(1) // + .build(); this.client = new CeresDBxClient(); final boolean ret = this.client.init(this.opts); @@ -90,29 +90,29 @@ public void before() { final SqlResult existsResult = management.executeSql("EXISTS TABLE %s", TEST_TABLE_NAME); LOG.info("EXISTS TABLE before: {}.", existsResult); - final SqlResult result = management.executeSql(String.format( - "CREATE TABLE %s(" + // - "ts TIMESTAMP NOT NULL," + // - "c1 STRING TAG NOT NULL," + // - "c2 INT64 TAG NULL," + // - "c3 DOUBLE NULL," + // - "c4 STRING NULL," + // - "c5 INT64 NULL," + // - "c6 FLOAT NULL," + // - "c7 INT32 NULL," + // - "c8 INT16 NULL," + // - "c9 INT8 NULL," + // - "c10 BOOLEAN NULL," + // - "c11 UINT64 NULL," + // - "c12 UINT32 NULL," + // - "c13 UINT16 NULL," + // - "c14 UINT8 NULL," + // - "c15 TIMESTAMP NULL," + // - "c16 VARBINARY NULL," + // - "TIMESTAMP KEY(ts)) ENGINE=Analytic WITH (ttl='7d')", TEST_TABLE_NAME)); + final SqlResult result = management.executeSql(String.format("CREATE TABLE %s(" + // + "ts TIMESTAMP NOT NULL," + // + "c1 STRING TAG NOT NULL," + // + "c2 INT64 TAG NULL," + // + "c3 DOUBLE NULL," + // + "c4 STRING NULL," + // + "c5 INT64 NULL," + // + "c6 FLOAT NULL," + // + "c7 INT32 NULL," + // + "c8 INT16 NULL," + // + "c9 INT8 NULL," + // + "c10 BOOLEAN NULL," + // + "c11 UINT64 NULL," + // + "c12 UINT32 NULL," + // + "c13 UINT16 NULL," + // + "c14 UINT8 NULL," + // + "c15 TIMESTAMP NULL," + // + "c16 VARBINARY NULL," + // + "TIMESTAMP KEY(ts)) ENGINE=Analytic WITH (ttl='7d')", + TEST_TABLE_NAME)); LOG.info("Start CeresDBx client {}, with options: {}, create table {}: {}.", result(ret), this.opts, - TEST_TABLE_NAME, result); + TEST_TABLE_NAME, result); final SqlResult existsResult2 = management.executeSql("EXISTS TABLE %s", TEST_TABLE_NAME); LOG.info("EXISTS TABLE after: {}.", existsResult2); @@ -154,7 +154,7 @@ public void comprehensiveTest() throws ExecutionException, InterruptedException .ql("select * from %s where ts < to_timestamp_millis('%s')", TEST_TABLE_NAME, timeString) // .build(); - final Result queryR = this.client.query(req).get(); + final Result queryR = this.client.query(req).get(); LOG.info("#comprehensiveTest query result={}.", queryR); @@ -165,33 +165,32 @@ public void comprehensiveTest() throws ExecutionException, InterruptedException ok.mapToRecord().forEach(rd -> { LOG.info("Field descriptor: {}", rd.getFieldDescriptors()); LOG.info("Data: ts={}, c1={}, c2={}, c3={}, c4={}, c5={}, c6={}, c7={}, c8={}, c9={}," + // - "c10={}, c11={}, c12={}, c13={}, c14={}, c15={}, c16={}", - rd.getTimestamp("ts"), // - rd.getString("c1"), // - rd.getInt64("c2"), // - rd.getDouble("c3"), // - rd.getString("c4"), // - rd.getInt64("c5"), // - rd.getFloat("c6"), // - rd.getInteger("c7"), // - rd.getInt16("c8"), // - rd.getInt8("c9"), // - rd.getBoolean("c10"), // - rd.getUInt64("c11"), // - rd.getUInt32("c12"), // - rd.getUInt16("c13"), // - rd.getUInt8("c14"), // - rd.getTimestamp("c15"), - rd.getBytes("c16")); - } - ); + "c10={}, c11={}, c12={}, c13={}, c14={}, c15={}, c16={}", rd.getTimestamp("ts"), // + rd.getString("c1"), // + rd.getInt64("c2"), // + rd.getDouble("c3"), // + rd.getString("c4"), // + rd.getInt64("c5"), // + rd.getFloat("c6"), // + rd.getInteger("c7"), // + rd.getInt16("c8"), // + rd.getInt8("c9"), // + rd.getBoolean("c10"), // + rd.getUInt64("c11"), // + rd.getUInt32("c12"), // + rd.getUInt16("c13"), // + rd.getUInt8("c14"), // + rd.getTimestamp("c15"), rd.getBytes("c16")); + }); final Management management = this.client.management(); - final SqlResult alterResult1 = management.executeSql("ALTER TABLE %s ADD COLUMN (c18 UINT64, c19 STRING TAG)", TEST_TABLE_NAME); + final SqlResult alterResult1 = management.executeSql("ALTER TABLE %s ADD COLUMN (c18 UINT64, c19 STRING TAG)", + TEST_TABLE_NAME); LOG.info("ALTER TABLE 1: {}.", alterResult1); - final SqlResult alterResult2 = management.executeSql("ALTER TABLE %s ADD COLUMN c20 STRING TAG", TEST_TABLE_NAME); + final SqlResult alterResult2 = management.executeSql("ALTER TABLE %s ADD COLUMN c20 STRING TAG", + TEST_TABLE_NAME); LOG.info("ALTER TABLE 2: {}.", alterResult2); final SqlResult descResult = management.executeSql("DESCRIBE %s", TEST_TABLE_NAME); @@ -221,10 +220,8 @@ private Collection makeRows(final Calendar time, final int count) { final long timestamp = time.getTimeInMillis(); final Collection rows = new ArrayList<>(); for (long ts = (timestamp - count); ts < timestamp; ts++) { - final Rows.Builder builder = Series.newBuilder(TEST_TABLE_NAME) - .tag("c1", "first_c1") - .tag("c2", TagValue.withInt64(12)) - .toRowsBuilder() // + final Rows.Builder builder = Series.newBuilder(TEST_TABLE_NAME).tag("c1", "first_c1") + .tag("c2", TagValue.withInt64(12)).toRowsBuilder() // .fields(ts, in -> { in.put("c3", FieldValue.withDouble(0.1)); in.put("c4", FieldValue.withString("string value")); @@ -234,7 +231,8 @@ private Collection makeRows(final Calendar time, final int count) { in.put("c8", FieldValue.withInt16(16)); in.put("c9", FieldValue.withInt8(8)); in.put("c10", FieldValue.withBoolean(true)); - in.put("c11", FieldValue.withUInt64(UnsignedUtil.getUInt64(Long.MAX_VALUE).add(BigInteger.ONE))); + in.put("c11", + FieldValue.withUInt64(UnsignedUtil.getUInt64(Long.MAX_VALUE).add(BigInteger.ONE))); in.put("c12", FieldValue.withUInt32(33)); in.put("c13", FieldValue.withUInt16(17)); in.put("c14", FieldValue.withUInt8(9)); @@ -285,12 +283,12 @@ public void loopToWriteTest() throws Exception { public void streamWriteTest() { final StreamWriteBuf writeBuf = this.client.streamWrite(TEST_TABLE_NAME); final CompletableFuture future = writeBuf.write(makeRows(Calendar.getInstance(), 2)) // - .write(makeRows(Calendar.getInstance(), 3)) // - .flush() // - .writeAndFlush(makeRows(Calendar.getInstance(), 10)) // - .write(makeRows(Calendar.getInstance(), 10)) // - .writeAndFlush(makeRows(Calendar.getInstance(), 10)) // - .completed(); + .write(makeRows(Calendar.getInstance(), 3)) // + .flush() // + .writeAndFlush(makeRows(Calendar.getInstance(), 10)) // + .write(makeRows(Calendar.getInstance(), 10)) // + .writeAndFlush(makeRows(Calendar.getInstance(), 10)) // + .completed(); Assert.assertEquals(35, future.join().getSuccess()); } diff --git a/ceresdb-grpc/src/main/java/com/ceresdb/rpc/GrpcClient.java b/ceresdb-grpc/src/main/java/com/ceresdb/rpc/GrpcClient.java index 6c82a54..6f2f474 100644 --- a/ceresdb-grpc/src/main/java/com/ceresdb/rpc/GrpcClient.java +++ b/ceresdb-grpc/src/main/java/com/ceresdb/rpc/GrpcClient.java @@ -89,46 +89,44 @@ */ public class GrpcClient implements RpcClient { - private static final Logger LOG = LoggerFactory.getLogger(GrpcClient.class); - - private static final SharedThreadPool SHARED_ASYNC_POOL = new SharedThreadPool( - new ObjectPool.Resource() { - - @Override - public ExecutorService create() { - return createDefaultRpcExecutor(); - } - - @Override - public void close(final ExecutorService ins) { - ExecutorServiceHelper - .shutdownAndAwaitTermination(ins); - } - }); - - private static final int CONN_RESET_THRESHOLD = SystemPropertyUtil.getInt( - OptKeys.GRPC_CONN_RESET_THRESHOLD, 3); - private static final int MAX_SIZE_TO_USE_ARRAY = 8192; - private static final String LIMITER_NAME = "grpc_call"; - private static final String EXECUTOR_NAME = "grpc_executor"; - private static final String REQ_RT = "req_rt"; - private static final String REQ_FAILED = "req_failed"; - private static final String UNARY_CALL = "unary-call"; - private static final String SERVER_STREAMING_CALL = "server-streaming-call"; - private static final String CLIENT_STREAMING_CALL = "client-streaming-call"; - - private final Map managedChannelPool = new ConcurrentHashMap<>(); - private final Map transientFailures = new ConcurrentHashMap<>(); - private final List interceptors = new CopyOnWriteArrayList<>(); - private final AtomicBoolean started = new AtomicBoolean(false); - private final List connectionObservers = new CopyOnWriteArrayList<>(); + private static final Logger LOG = LoggerFactory.getLogger(GrpcClient.class); + + private static final SharedThreadPool SHARED_ASYNC_POOL = new SharedThreadPool( + new ObjectPool.Resource() { + + @Override + public ExecutorService create() { + return createDefaultRpcExecutor(); + } + + @Override + public void close(final ExecutorService ins) { + ExecutorServiceHelper.shutdownAndAwaitTermination(ins); + } + }); + + private static final int CONN_RESET_THRESHOLD = SystemPropertyUtil.getInt(OptKeys.GRPC_CONN_RESET_THRESHOLD, 3); + private static final int MAX_SIZE_TO_USE_ARRAY = 8192; + private static final String LIMITER_NAME = "grpc_call"; + private static final String EXECUTOR_NAME = "grpc_executor"; + private static final String REQ_RT = "req_rt"; + private static final String REQ_FAILED = "req_failed"; + private static final String UNARY_CALL = "unary-call"; + private static final String SERVER_STREAMING_CALL = "server-streaming-call"; + private static final String CLIENT_STREAMING_CALL = "client-streaming-call"; + + private final Map managedChannelPool = new ConcurrentHashMap<>(); + private final Map transientFailures = new ConcurrentHashMap<>(); + private final List interceptors = new CopyOnWriteArrayList<>(); + private final AtomicBoolean started = new AtomicBoolean(false); + private final List connectionObservers = new CopyOnWriteArrayList<>(); private final MarshallerRegistry marshallerRegistry; - private String tenant = "none"; - private String defaultChildTenant = "none"; - private RpcOptions opts; - private ExecutorService asyncPool; - private boolean useSharedAsyncPool; + private String tenant = "none"; + private String defaultChildTenant = "none"; + private RpcOptions opts; + private ExecutorService asyncPool; + private boolean useSharedAsyncPool; public GrpcClient(MarshallerRegistry marshallerRegistry) { this.marshallerRegistry = marshallerRegistry; @@ -197,7 +195,8 @@ public void registerConnectionObserver(final ConnectionObserver observer) { public Resp invokeSync(final Endpoint endpoint, // final Req request, // final Context ctx, // - final long timeoutMs) throws RemotingException { + final long timeoutMs) + throws RemotingException { final long timeout = calcTimeout(timeoutMs); final CompletableFuture future = new CompletableFuture<>(); @@ -233,12 +232,11 @@ public void invokeAsync(final Endpoint endpoint, // final long timeoutMs) { checkArgs(endpoint, request, ctx, observer); - final MethodDescriptor method = getCallMethod(request, - MethodDescriptor.MethodType.UNARY); + final MethodDescriptor method = getCallMethod(request, MethodDescriptor.MethodType.UNARY); final long timeout = calcTimeout(timeoutMs); final CallOptions callOpts = CallOptions.DEFAULT // - .withDeadlineAfter(timeout, TimeUnit.MILLISECONDS) // - .withExecutor(getObserverExecutor(observer)); + .withDeadlineAfter(timeout, TimeUnit.MILLISECONDS) // + .withExecutor(getObserverExecutor(observer)); final String childTenant = addChildTenantIntoCtx(ctx); final String methodName = method.getFullMethodName(); @@ -302,7 +300,7 @@ public void invokeServerStreaming(final Endpoint endpoint, // checkArgs(endpoint, request, ctx, observer); final MethodDescriptor method = getCallMethod(request, - MethodDescriptor.MethodType.SERVER_STREAMING); + MethodDescriptor.MethodType.SERVER_STREAMING); final CallOptions callOpts = CallOptions.DEFAULT.withExecutor(getObserverExecutor(observer)); final String childTenant = addChildTenantIntoCtx(ctx); @@ -322,25 +320,25 @@ public void invokeServerStreaming(final Endpoint endpoint, // final String target = target(ch, address); ClientCalls.asyncServerStreamingCall(ch.newCall(method, callOpts), (Message) request, - new StreamObserver() { + new StreamObserver() { - @SuppressWarnings("unchecked") - @Override - public void onNext(final Message value) { - observer.onNext((Resp) value); - } + @SuppressWarnings("unchecked") + @Override + public void onNext(final Message value) { + observer.onNext((Resp) value); + } - @Override - public void onError(final Throwable err) { - attachErrMsg(err, SERVER_STREAMING_CALL, methodName, childTenant, target, startCall, -1, ctx); - observer.onError(err); - } + @Override + public void onError(final Throwable err) { + attachErrMsg(err, SERVER_STREAMING_CALL, methodName, childTenant, target, startCall, -1, ctx); + observer.onError(err); + } - @Override - public void onCompleted() { - observer.onCompleted(); - } - }); + @Override + public void onCompleted() { + observer.onCompleted(); + } + }); } @Override @@ -351,7 +349,7 @@ public Observer invokeClientStreaming(final Endpoint endpoint, checkArgs(endpoint, defaultReqIns, ctx, respObserver); final MethodDescriptor method = getCallMethod(defaultReqIns, - MethodDescriptor.MethodType.CLIENT_STREAMING); + MethodDescriptor.MethodType.CLIENT_STREAMING); final CallOptions callOpts = CallOptions.DEFAULT.withExecutor(getObserverExecutor(respObserver)); final String childTenant = addChildTenantIntoCtx(ctx); @@ -373,25 +371,25 @@ public Observer invokeClientStreaming(final Endpoint endpoint, final String target = target(ch, address); final StreamObserver gRpcObs = ClientCalls.asyncClientStreamingCall(ch.newCall(method, callOpts), - new StreamObserver() { - - @SuppressWarnings("unchecked") - @Override - public void onNext(final Message value) { - respObserver.onNext((Resp) value); - } - - @Override - public void onError(final Throwable err) { - attachErrMsg(err, CLIENT_STREAMING_CALL, methodName, childTenant, target, startCall, -1, ctx); - respObserver.onError(err); - } - - @Override - public void onCompleted() { - respObserver.onCompleted(); - } - }); + new StreamObserver() { + + @SuppressWarnings("unchecked") + @Override + public void onNext(final Message value) { + respObserver.onNext((Resp) value); + } + + @Override + public void onError(final Throwable err) { + attachErrMsg(err, CLIENT_STREAMING_CALL, methodName, childTenant, target, startCall, -1, ctx); + respObserver.onError(err); + } + + @Override + public void onCompleted() { + respObserver.onCompleted(); + } + }); return new Observer() { @@ -469,8 +467,7 @@ private ClientRequestLimitInterceptor createRequestLimitInterceptor(final RpcOpt throw new IllegalArgumentException("Unsupported limit kind: " + kind); } - final RequestLimiterBuilder limiterBuilder = RequestLimiterBuilder.newBuilder() - .named(LIMITER_NAME) // + final RequestLimiterBuilder limiterBuilder = RequestLimiterBuilder.newBuilder().named(LIMITER_NAME) // .metricRegistry(metricRegistry) // .blockOnLimit(this.opts.isBlockOnLimit(), this.opts.getDefaultRpcTimeout()) // .limit(limit); @@ -486,7 +483,8 @@ private ClientRequestLimitInterceptor createRequestLimitInterceptor(final RpcOpt Requires.requireTrue(Math.abs(sum - 1.0) < 0.1, "the total percent sum of partitions must be near 100%"); methodsLimitPercent.forEach(limiterBuilder::partition); - return new ClientRequestLimitInterceptor(limiterBuilder.partitionByMethod().build(), methodsLimitPercent::containsKey); + return new ClientRequestLimitInterceptor(limiterBuilder.partitionByMethod().build(), + methodsLimitPercent::containsKey); } } @@ -499,26 +497,26 @@ private void attachErrMsg(final Throwable err, // final long duration, // final Context ctx) { final StringBuilder buf = StringBuilderHelper.get() // - .append("GRPC ") // - .append(callType) // - .append(" got an error,") // - .append(" method=") // - .append(method) // - .append(", tenant=") // - .append(this.tenant) // - .append(", childTenant=") // - .append(childTenant != null ? childTenant : this.defaultChildTenant) // - .append(", target=") // - .append(target) // - .append(", startCall=") // - .append(startCall); + .append("GRPC ") // + .append(callType) // + .append(" got an error,") // + .append(" method=") // + .append(method) // + .append(", tenant=") // + .append(this.tenant) // + .append(", childTenant=") // + .append(childTenant != null ? childTenant : this.defaultChildTenant) // + .append(", target=") // + .append(target) // + .append(", startCall=") // + .append(startCall); if (duration > 0) { buf.append(", duration=") // - .append(duration) // - .append(" millis"); + .append(duration) // + .append(" millis"); } buf.append(", ctx=") // - .append(ctx); + .append(ctx); err.addSuppressed(new OnlyErrorMessage(buf.toString())); } @@ -591,7 +589,7 @@ private boolean checkConnectivity(final Endpoint endpoint, final ManagedChannel } LOG.warn("Channel {} in [INACTIVE] state {} times, it has been removed from the pool.", - target(removedCh, endpoint), c); + target(removedCh, endpoint), c); if (removedCh != ch) { // Now that it's removed, close it @@ -621,12 +619,12 @@ private MethodDescriptor getCallMethod(final Object request, / Requires.requireNonNull(defaultRespIns, "null default response instance: " + reqCls.getName()); return MethodDescriptor // - . newBuilder() // - .setType(methodType) // - .setFullMethodName(this.marshallerRegistry.getMethodName(reqCls, methodType)) // - .setRequestMarshaller(ProtoUtils.marshaller(defaultReqIns)) // - .setResponseMarshaller(ProtoUtils.marshaller(defaultRespIns)) // - .build(); + . newBuilder() // + .setType(methodType) // + .setFullMethodName(this.marshallerRegistry.getMethodName(reqCls, methodType)) // + .setRequestMarshaller(ProtoUtils.marshaller(defaultReqIns)) // + .setResponseMarshaller(ProtoUtils.marshaller(defaultRespIns)) // + .build(); } private Channel getCheckedChannel(final Endpoint endpoint, final Consumer onFailed) { @@ -651,18 +649,18 @@ private ManagedChannel getChannel(final Endpoint endpoint, final boolean createI private IdChannel newChannel(final Endpoint endpoint) { final ManagedChannel innerChannel = NettyChannelBuilder.forAddress(endpoint.getIp(), endpoint.getPort()) // - .usePlaintext() // - .executor(this.asyncPool) // - .intercept(this.interceptors) // - .maxInboundMessageSize(this.opts.getMaxInboundMessageSize()) // - .flowControlWindow(this.opts.getFlowControlWindow()) // - .idleTimeout(this.opts.getIdleTimeoutSeconds(), TimeUnit.SECONDS) // - .keepAliveTime(this.opts.getKeepAliveTimeSeconds(), TimeUnit.SECONDS) // - .keepAliveTimeout(this.opts.getKeepAliveTimeoutSeconds(), TimeUnit.SECONDS) // - .keepAliveWithoutCalls(this.opts.isKeepAliveWithoutCalls()) // - .withOption(ChannelOption.SO_REUSEADDR, true) // - .withOption(ChannelOption.TCP_NODELAY, true) // - .build(); + .usePlaintext() // + .executor(this.asyncPool) // + .intercept(this.interceptors) // + .maxInboundMessageSize(this.opts.getMaxInboundMessageSize()) // + .flowControlWindow(this.opts.getFlowControlWindow()) // + .idleTimeout(this.opts.getIdleTimeoutSeconds(), TimeUnit.SECONDS) // + .keepAliveTime(this.opts.getKeepAliveTimeSeconds(), TimeUnit.SECONDS) // + .keepAliveTimeout(this.opts.getKeepAliveTimeoutSeconds(), TimeUnit.SECONDS) // + .keepAliveWithoutCalls(this.opts.isKeepAliveWithoutCalls()) // + .withOption(ChannelOption.SO_REUSEADDR, true) // + .withOption(ChannelOption.TCP_NODELAY, true) // + .build(); final IdChannel idChannel = new IdChannel(innerChannel); @@ -723,20 +721,20 @@ private void notifyShutdown(final Endpoint endpoint) { @Override public void display(final Printer out) { out.println("--- GrpcClient ---")// - .print("started=") // - .println(this.started) // - .print("opts=") // - .println(this.opts) // - .print("connectionObservers=") // - .println(this.connectionObservers) // - .print("asyncPool=") // - .println(this.asyncPool) // - .print("interceptors=") // - .println(this.interceptors) // - .print("managedChannelPool=") // - .println(this.managedChannelPool) // - .print("transientFailures=") // - .println(this.transientFailures); + .print("started=") // + .println(this.started) // + .print("opts=") // + .println(this.opts) // + .print("connectionObservers=") // + .println(this.connectionObservers) // + .print("asyncPool=") // + .println(this.asyncPool) // + .print("interceptors=") // + .println(this.interceptors) // + .print("managedChannelPool=") // + .println(this.managedChannelPool) // + .print("transientFailures=") // + .println(this.transientFailures); } private static String target(final Channel ch, final Endpoint ep) { @@ -745,12 +743,12 @@ private static String target(final Channel ch, final Endpoint ep) { private static String target(final Channel ch, final String address) { return StringBuilderHelper.get() // - .append('[') // - .append(channelId(ch)) // - .append('/') // - .append(address) // - .append(']') // - .toString(); + .append('[') // + .append(channelId(ch)) // + .append('/') // + .append(address) // + .append(']') // + .toString(); } private static long channelId(final Channel ch) { @@ -782,29 +780,29 @@ private static ExecutorService createRpcExecutor(final RpcOptions opts) { } return ThreadPoolUtil.newBuilder() // - .poolName(EXECUTOR_NAME) // - .enableMetric(true) // - .coreThreads(Math.min(Cpus.cpus(), opts.getRpcThreadPoolSize())) // - .maximumThreads(opts.getRpcThreadPoolSize()) // - .keepAliveSeconds(60L) // - .workQueue(workQueue) // - .threadFactory(new NamedThreadFactory(EXECUTOR_NAME, true)) // - .rejectedHandler(new AsyncPoolRejectedHandler(EXECUTOR_NAME)) // - .build(); + .poolName(EXECUTOR_NAME) // + .enableMetric(true) // + .coreThreads(Math.min(Cpus.cpus(), opts.getRpcThreadPoolSize())) // + .maximumThreads(opts.getRpcThreadPoolSize()) // + .keepAliveSeconds(60L) // + .workQueue(workQueue) // + .threadFactory(new NamedThreadFactory(EXECUTOR_NAME, true)) // + .rejectedHandler(new AsyncPoolRejectedHandler(EXECUTOR_NAME)) // + .build(); } private static ExecutorService createDefaultRpcExecutor() { final String name = "default_shared_" + EXECUTOR_NAME; return ThreadPoolUtil.newBuilder() // - .poolName(name) // - .enableMetric(true) // - .coreThreads(Cpus.cpus()) // - .maximumThreads(Cpus.cpus() << 2) // - .keepAliveSeconds(60L) // - .workQueue(new ArrayBlockingQueue<>(512)) // - .threadFactory(new NamedThreadFactory(name, true)) // - .rejectedHandler(new AsyncPoolRejectedHandler(name)) // - .build(); + .poolName(name) // + .enableMetric(true) // + .coreThreads(Cpus.cpus()) // + .maximumThreads(Cpus.cpus() << 2) // + .keepAliveSeconds(60L) // + .workQueue(new ArrayBlockingQueue<>(512)) // + .threadFactory(new NamedThreadFactory(name, true)) // + .rejectedHandler(new AsyncPoolRejectedHandler(name)) // + .build(); } private static class AsyncPoolRejectedHandler implements RejectedExecutionHandler { @@ -818,7 +816,7 @@ private static class AsyncPoolRejectedHandler implements RejectedExecutionHandle @Override public void rejectedExecution(final Runnable r, final ThreadPoolExecutor executor) { LOG.error("Thread poll {} is busy, the caller thread {} will run this task {}.", this.name, - Thread.currentThread(), r); + Thread.currentThread(), r); if (!executor.isShutdown()) { r.run(); } diff --git a/ceresdb-grpc/src/main/java/com/ceresdb/rpc/GrpcFactory.java b/ceresdb-grpc/src/main/java/com/ceresdb/rpc/GrpcFactory.java index e750237..f7c694e 100644 --- a/ceresdb-grpc/src/main/java/com/ceresdb/rpc/GrpcFactory.java +++ b/ceresdb-grpc/src/main/java/com/ceresdb/rpc/GrpcFactory.java @@ -34,8 +34,8 @@ public void register(final MethodDescriptor method, // final Object defaultReqIns, // final Object defaultRespIns) { getMarshallerRegistry() // - .registerMarshaller(method, (Class) reqCls, (Message) defaultReqIns, - (Message) defaultRespIns); + .registerMarshaller(method, (Class) reqCls, (Message) defaultReqIns, + (Message) defaultRespIns); } @Override diff --git a/ceresdb-grpc/src/main/java/com/ceresdb/rpc/IdChannel.java b/ceresdb-grpc/src/main/java/com/ceresdb/rpc/IdChannel.java index 70a5d32..a4dc117 100644 --- a/ceresdb-grpc/src/main/java/com/ceresdb/rpc/IdChannel.java +++ b/ceresdb-grpc/src/main/java/com/ceresdb/rpc/IdChannel.java @@ -34,8 +34,8 @@ public class IdChannel extends ManagedChannel { private static final AtomicLong ID_ALLOC = new AtomicLong(); - private final long channelId; - private final ManagedChannel channel; + private final long channelId; + private final ManagedChannel channel; private static long getNextId() { return ID_ALLOC.incrementAndGet(); diff --git a/ceresdb-grpc/src/main/java/com/ceresdb/rpc/MarshallerRegistry.java b/ceresdb-grpc/src/main/java/com/ceresdb/rpc/MarshallerRegistry.java index 850e07e..57b02c8 100644 --- a/ceresdb-grpc/src/main/java/com/ceresdb/rpc/MarshallerRegistry.java +++ b/ceresdb-grpc/src/main/java/com/ceresdb/rpc/MarshallerRegistry.java @@ -116,15 +116,13 @@ default MethodDescriptor.MethodType supportedMethodType(final io.grpc.MethodDesc enum DefaultMarshallerRegistry implements MarshallerRegistry { INSTANCE; - private final Map, Map> methods = - new ConcurrentHashMap<>(); - private final Map, Message> requests = - new ConcurrentHashMap<>(); - private final Map, Message> responses = - new ConcurrentHashMap<>(); + private final Map, Map> methods = new ConcurrentHashMap<>(); + private final Map, Message> requests = new ConcurrentHashMap<>(); + private final Map, Message> responses = new ConcurrentHashMap<>(); @Override - public String getMethodName(final Class reqCls, final MethodDescriptor.MethodType methodType) { + public String getMethodName(final Class reqCls, + final MethodDescriptor.MethodType methodType) { final Map methods = this.methods.get(reqCls); Requires.requireNonNull(methods, "Could not find method by " + reqCls); final MethodDescriptor md = methods.get(methodType); @@ -134,11 +132,7 @@ public String getMethodName(final Class reqCls, final MethodD @Override public Set getAllMethodDescriptors() { - return this.methods - .values() - .stream() - .flatMap(map -> map.values().stream()) - .collect(Collectors.toSet()); + return this.methods.values().stream().flatMap(map -> map.values().stream()).collect(Collectors.toSet()); } @Override diff --git a/ceresdb-grpc/src/main/java/com/ceresdb/rpc/interceptors/AuthHeadersInterceptor.java b/ceresdb-grpc/src/main/java/com/ceresdb/rpc/interceptors/AuthHeadersInterceptor.java index 7c7e5f7..eadd864 100644 --- a/ceresdb-grpc/src/main/java/com/ceresdb/rpc/interceptors/AuthHeadersInterceptor.java +++ b/ceresdb-grpc/src/main/java/com/ceresdb/rpc/interceptors/AuthHeadersInterceptor.java @@ -40,7 +40,7 @@ public class AuthHeadersInterceptor implements ClientInterceptor { private static final ThreadLocal CURRENT_CHILD_TENANT = new ThreadLocal<>(); - private final Tenant tenant; + private final Tenant tenant; public AuthHeadersInterceptor(Tenant tenant) { this.tenant = Requires.requireNonNull(tenant, "tenant"); @@ -62,8 +62,7 @@ public ClientCall interceptCall(final MethodDescripto } private final class HeaderAttachingClientCall - extends - ForwardingClientCall.SimpleForwardingClientCall { + extends ForwardingClientCall.SimpleForwardingClientCall { // Non private to avoid synthetic class HeaderAttachingClientCall(ClientCall call) { diff --git a/ceresdb-grpc/src/main/java/com/ceresdb/rpc/interceptors/ClientRequestLimitInterceptor.java b/ceresdb-grpc/src/main/java/com/ceresdb/rpc/interceptors/ClientRequestLimitInterceptor.java index 2c7e0f3..b860848 100644 --- a/ceresdb-grpc/src/main/java/com/ceresdb/rpc/interceptors/ClientRequestLimitInterceptor.java +++ b/ceresdb-grpc/src/main/java/com/ceresdb/rpc/interceptors/ClientRequestLimitInterceptor.java @@ -45,10 +45,9 @@ */ public class ClientRequestLimitInterceptor implements ClientInterceptor { - private static final Status LIMIT_EXCEEDED_STATUS = Status.UNAVAILABLE - .withDescription("Client limit reached"); + private static final Status LIMIT_EXCEEDED_STATUS = Status.UNAVAILABLE.withDescription("Client limit reached"); - private static final AtomicBoolean LIMIT_SWITCH = new AtomicBoolean(true); + private static final AtomicBoolean LIMIT_SWITCH = new AtomicBoolean(true); private final Limiter limiter; private final Function filter; @@ -74,13 +73,15 @@ public ClientCall interceptCall(final MethodDescripto return MetricsUtil.timer(LimitMetricRegistry.RPC_LIMITER, "acquire_time", methodName) .timeSupplier(() -> this.limiter.acquire(() -> methodName)) - .map(listener -> (ClientCall) new ForwardingClientCall.SimpleForwardingClientCall(next.newCall(method, callOpts)) { + .map(listener -> (ClientCall) new ForwardingClientCall.SimpleForwardingClientCall( + next.newCall(method, callOpts)) { private final AtomicBoolean done = new AtomicBoolean(false); @Override public void start(final Listener respListener, final Metadata headers) { - super.start(new ForwardingClientCallListener.SimpleForwardingClientCallListener(respListener) { + super.start(new ForwardingClientCallListener.SimpleForwardingClientCallListener( + respListener) { @Override public void onClose(final Status status, final Metadata trailers) { @@ -111,8 +112,7 @@ public void cancel(final String message, final Throwable cause) { } } } - }) - .orElseGet(() -> new ClientCall() { + }).orElseGet(() -> new ClientCall() { private Listener respListener; diff --git a/ceresdb-grpc/src/main/java/com/ceresdb/rpc/interceptors/ContextToHeadersInterceptor.java b/ceresdb-grpc/src/main/java/com/ceresdb/rpc/interceptors/ContextToHeadersInterceptor.java index 85776ba..12557d3 100644 --- a/ceresdb-grpc/src/main/java/com/ceresdb/rpc/interceptors/ContextToHeadersInterceptor.java +++ b/ceresdb-grpc/src/main/java/com/ceresdb/rpc/interceptors/ContextToHeadersInterceptor.java @@ -47,8 +47,7 @@ public ClientCall interceptCall(final MethodDescripto } private static final class HeaderAttachingClientCall - extends - ForwardingClientCall.SimpleForwardingClientCall { + extends ForwardingClientCall.SimpleForwardingClientCall { // Non private to avoid synthetic class HeaderAttachingClientCall(ClientCall delegate) { diff --git a/ceresdb-grpc/src/main/java/com/ceresdb/rpc/interceptors/MetricInterceptor.java b/ceresdb-grpc/src/main/java/com/ceresdb/rpc/interceptors/MetricInterceptor.java index 63b0e5c..c846247 100644 --- a/ceresdb-grpc/src/main/java/com/ceresdb/rpc/interceptors/MetricInterceptor.java +++ b/ceresdb-grpc/src/main/java/com/ceresdb/rpc/interceptors/MetricInterceptor.java @@ -36,15 +36,15 @@ */ public class MetricInterceptor implements ClientInterceptor { - private static final String REQ_TYPE = "req"; - private static final String RESP_TYPE = "resp"; + private static final String REQ_TYPE = "req"; + private static final String RESP_TYPE = "resp"; - private static final String QPS = "qps"; - private static final String BYTES = "bytes"; - private static final String SERIALIZED_BYTES = "serialized_bytes"; + private static final String QPS = "qps"; + private static final String BYTES = "bytes"; + private static final String SERIALIZED_BYTES = "serialized_bytes"; - private static final Counter REQ_BYTES = MetricsUtil.counter(REQ_TYPE, BYTES); - private static final Counter RESP_BYTES = MetricsUtil.counter(RESP_TYPE, BYTES); + private static final Counter REQ_BYTES = MetricsUtil.counter(REQ_TYPE, BYTES); + private static final Counter RESP_BYTES = MetricsUtil.counter(RESP_TYPE, BYTES); @Override public ClientCall interceptCall(final MethodDescriptor method, // diff --git a/ceresdb-grpc/src/main/java/com/ceresdb/rpc/limit/Gradient2Limit.java b/ceresdb-grpc/src/main/java/com/ceresdb/rpc/limit/Gradient2Limit.java index d73160a..7529383 100644 --- a/ceresdb-grpc/src/main/java/com/ceresdb/rpc/limit/Gradient2Limit.java +++ b/ceresdb-grpc/src/main/java/com/ceresdb/rpc/limit/Gradient2Limit.java @@ -76,16 +76,16 @@ public class Gradient2Limit extends AbstractLimit { private static final Logger LOG = LoggerFactory.getLogger(Gradient2Limit.class); public static class Builder { - private int initialLimit = 20; - private int minLimit = 20; + private int initialLimit = 20; + private int minLimit = 20; private int maxConcurrency = 200; - private double smoothing = 0.2; - private Function queueSize = concurrency -> 4; - private MetricRegistry registry = EmptyMetricRegistry.INSTANCE; - private int longWindow = 600; - private double rttTolerance = 1.5; - private boolean logOnLimitChange = true; + private double smoothing = 0.2; + private Function queueSize = concurrency -> 4; + private MetricRegistry registry = EmptyMetricRegistry.INSTANCE; + private int longWindow = 600; + private double rttTolerance = 1.5; + private boolean logOnLimitChange = true; /** * Initial limit used by the limiter @@ -227,15 +227,15 @@ public static Gradient2Limit newDefault() { /** * Maximum allowed limit providing an upper bound failsafe */ - private final int maxLimit; - private final int minLimit; - private final Function queueSize; - private final double smoothing; + private final int maxLimit; + private final int minLimit; + private final Function queueSize; + private final double smoothing; private final MetricRegistry.SampleListener longRttSampleListener; private final MetricRegistry.SampleListener shortRttSampleListener; private final MetricRegistry.SampleListener queueSizeSampleListener; - private final double tolerance; - private final boolean logOnLimitChange; + private final double tolerance; + private final boolean logOnLimitChange; private Gradient2Limit(Builder builder) { super(builder.initialLimit); @@ -292,12 +292,8 @@ public int _update(final long startTime, final long rtt, final int inflight, fin if (this.logOnLimitChange && (int) currLimit != (int) newLimit) { LOG.info("New limit={}, previous limit={}, shortRtt={} ms, longRtt={} ms, queueSize={}, gradient={}.", - (int) newLimit, - (int) currLimit, - getLastRtt(TimeUnit.MICROSECONDS) / 1000.0, - getRttNoLoad(TimeUnit.MICROSECONDS) / 1000.0, - queueSize, - gradient); + (int) newLimit, (int) currLimit, getLastRtt(TimeUnit.MICROSECONDS) / 1000.0, + getRttNoLoad(TimeUnit.MICROSECONDS) / 1000.0, queueSize, gradient); } this.estimatedLimit = newLimit; diff --git a/ceresdb-grpc/src/main/java/com/ceresdb/rpc/limit/RequestLimiterBuilder.java b/ceresdb-grpc/src/main/java/com/ceresdb/rpc/limit/RequestLimiterBuilder.java index 8f82b65..b039272 100644 --- a/ceresdb-grpc/src/main/java/com/ceresdb/rpc/limit/RequestLimiterBuilder.java +++ b/ceresdb-grpc/src/main/java/com/ceresdb/rpc/limit/RequestLimiterBuilder.java @@ -55,8 +55,8 @@ public RequestLimiterBuilder blockOnLimit(final boolean blockOnLimit, final long } public Limiter build() { - return this.blockOnLimit ? BlockingLimiter.wrap(super.build(), Duration.ofMillis(this.blockTimeoutMillis)) - : super.build(); + return this.blockOnLimit ? BlockingLimiter.wrap(super.build(), Duration.ofMillis(this.blockTimeoutMillis)) : + super.build(); } @Override diff --git a/ceresdb-grpc/src/main/java/com/ceresdb/rpc/limit/VegasLimit.java b/ceresdb-grpc/src/main/java/com/ceresdb/rpc/limit/VegasLimit.java index 4238e69..3cea584 100644 --- a/ceresdb-grpc/src/main/java/com/ceresdb/rpc/limit/VegasLimit.java +++ b/ceresdb-grpc/src/main/java/com/ceresdb/rpc/limit/VegasLimit.java @@ -45,26 +45,26 @@ public class VegasLimit extends AbstractLimit { private static final Logger LOG = LoggerFactory.getLogger(VegasLimit.class); - + private static final Function LOG10 = Log10RootFunction.create(0); public static class Builder { - private int initialLimit = 20; - private int maxConcurrency = 1000; - private MetricRegistry registry = EmptyMetricRegistry.INSTANCE; - private double smoothing = 1.0; - - private Function alphaFunc = (limit) -> 3 * LOG10.apply(limit); - private Function betaFunc = (limit) -> 6 * LOG10.apply(limit); - private Function thresholdFunc = LOG10; - private Function increaseFunc = (limit) -> limit + LOG10.apply(limit.intValue()); - private Function decreaseFunc = (limit) -> limit - LOG10.apply(limit.intValue()); - private int probeMultiplier = 30; - private boolean logOnLimitChange = true; - + private int initialLimit = 20; + private int maxConcurrency = 1000; + private MetricRegistry registry = EmptyMetricRegistry.INSTANCE; + private double smoothing = 1.0; + + private Function alphaFunc = (limit) -> 3 * LOG10.apply(limit); + private Function betaFunc = (limit) -> 6 * LOG10.apply(limit); + private Function thresholdFunc = LOG10; + private Function increaseFunc = (limit) -> limit + LOG10.apply(limit.intValue()); + private Function decreaseFunc = (limit) -> limit - LOG10.apply(limit.intValue()); + private int probeMultiplier = 30; + private boolean logOnLimitChange = true; + private Builder() { } - + /** * The limiter will probe for a new noload RTT every probeMultiplier * current limit * iterations. Default value is 30. @@ -76,47 +76,47 @@ public Builder probeMultiplier(final int probeMultiplier) { this.probeMultiplier = probeMultiplier; return this; } - + public Builder alpha(final int alpha) { this.alphaFunc = (ignore) -> alpha; return this; } - + public Builder threshold(final Function threshold) { this.thresholdFunc = threshold; return this; } - + public Builder alpha(final Function alpha) { this.alphaFunc = alpha; return this; } - + public Builder beta(final int beta) { this.betaFunc = (ignore) -> beta; return this; } - + public Builder beta(final Function beta) { this.betaFunc = beta; return this; } - + public Builder increase(final Function increase) { this.increaseFunc = increase; return this; } - + public Builder decrease(final Function decrease) { this.decreaseFunc = decrease; return this; } - + public Builder smoothing(final double smoothing) { this.smoothing = smoothing; return this; } - + public Builder initialLimit(final int initialLimit) { this.initialLimit = initialLimit; return this; @@ -136,43 +136,43 @@ public Builder metricRegistry(final MetricRegistry registry) { this.registry = registry; return this; } - + public VegasLimit build() { return new VegasLimit(this); } } - + public static Builder newBuilder() { return new Builder(); } - + public static VegasLimit newDefault() { return newBuilder().build(); } - + /** * Estimated concurrency limit based on our algorithm */ private volatile double estimatedLimit; - + private volatile long rtt_noload = 0; - + /** * Maximum allowed limit providing an upper bound failsafe */ - private final int maxLimit; - - private final double smoothing; + private final int maxLimit; + + private final double smoothing; private final Function alphaFunc; private final Function betaFunc; private final Function thresholdFunc; - private final Function increaseFunc; - private final Function decreaseFunc; - private final SampleListener rttSampleListener; - private final int probeMultiplier; - private final boolean logOnLimitChange; - private int probeCount = 0; - private double probeJitter; + private final Function increaseFunc; + private final Function decreaseFunc; + private final SampleListener rttSampleListener; + private final int probeMultiplier; + private final boolean logOnLimitChange; + private int probeCount = 0; + private double probeJitter; private VegasLimit(Builder builder) { super(builder.initialLimit); @@ -212,13 +212,13 @@ protected int _update(final long startTime, final long rtt, final int inflight, this.rtt_noload = rtt; return (int) this.estimatedLimit; } - + if (this.rtt_noload == 0 || rtt < this.rtt_noload) { LOG.debug("New MinRTT {}.", TimeUnit.NANOSECONDS.toMicros(rtt) / 1000.0); this.rtt_noload = rtt; return (int) this.estimatedLimit; } - + this.rttSampleListener.addSample(getRttMillis(this.rtt_noload)); return updateEstimatedLimit(rtt, inflight, didDrop); @@ -233,7 +233,7 @@ private int updateEstimatedLimit(final long rtt, final int inflight, final boole // Treat any drop (i.e timeout) as needing to reduce the limit if (didDrop) { newLimit = this.decreaseFunc.apply(currLimit); - // Prevent upward drift if not close to the limit + // Prevent upward drift if not close to the limit } else if (inflight * 2 < currLimit) { return (int) currLimit; } else { @@ -244,13 +244,13 @@ private int updateEstimatedLimit(final long rtt, final int inflight, final boole // Aggressive increase when no queuing if (queueSize <= threshold) { newLimit = currLimit + beta; - // Increase the limit if queue is still manageable + // Increase the limit if queue is still manageable } else if (queueSize < alpha) { newLimit = this.increaseFunc.apply(currLimit); - // Detecting latency so decrease + // Detecting latency so decrease } else if (queueSize > beta) { newLimit = this.decreaseFunc.apply(currLimit); - // We're within he sweet spot so nothing to do + // We're within he sweet spot so nothing to do } else { return (int) currLimit; } @@ -260,12 +260,9 @@ private int updateEstimatedLimit(final long rtt, final int inflight, final boole newLimit = (1 - this.smoothing) * currLimit + this.smoothing * newLimit; if (this.logOnLimitChange && (int) newLimit != (int) currLimit) { - LOG.info("New limit={}, previous limit={}, minRtt={} ms, winRtt={} ms, queueSize={}.", - (int) newLimit, - (int) currLimit, - TimeUnit.NANOSECONDS.toMicros(this.rtt_noload) / 1000.0, - TimeUnit.NANOSECONDS.toMicros(rtt) / 1000.0, - queueSize); + LOG.info("New limit={}, previous limit={}, minRtt={} ms, winRtt={} ms, queueSize={}.", (int) newLimit, + (int) currLimit, TimeUnit.NANOSECONDS.toMicros(this.rtt_noload) / 1000.0, + TimeUnit.NANOSECONDS.toMicros(rtt) / 1000.0, queueSize); } this.estimatedLimit = newLimit; @@ -280,7 +277,7 @@ private long getRttMillis(final long nanos) { @Override public String toString() { return "VegasLimit [limit=" + getLimit() + // - ", rtt_noload=" + TimeUnit.NANOSECONDS.toMicros(this.rtt_noload) / 1000.0 + // - " ms]"; + ", rtt_noload=" + TimeUnit.NANOSECONDS.toMicros(this.rtt_noload) / 1000.0 + // + " ms]"; } } diff --git a/ceresdb-grpc/src/test/java/com/ceresdb/rpc/LimitTest.java b/ceresdb-grpc/src/test/java/com/ceresdb/rpc/LimitTest.java index 6ddf2d3..a277a47 100644 --- a/ceresdb-grpc/src/test/java/com/ceresdb/rpc/LimitTest.java +++ b/ceresdb-grpc/src/test/java/com/ceresdb/rpc/LimitTest.java @@ -36,20 +36,20 @@ public class LimitTest { static Gradient2Limit createGradient2() { return Gradient2Limit.newBuilder() // - .initialLimit(512) // - .maxConcurrency(1024) // - .smoothing(0.2) // - .longWindow(100) // - .queueSize(16) // - .build(); + .initialLimit(512) // + .maxConcurrency(1024) // + .smoothing(0.2) // + .longWindow(100) // + .queueSize(16) // + .build(); } static VegasLimit createVegas() { return VegasLimit.newBuilder() // - .initialLimit(512) // - .maxConcurrency(1024) // - .smoothing(0.2) // - .build(); + .initialLimit(512) // + .maxConcurrency(1024) // + .smoothing(0.2) // + .build(); } @Test diff --git a/ceresdb-grpc/src/test/java/com/ceresdb/rpc/RequestLimiterInterceptorTest.java b/ceresdb-grpc/src/test/java/com/ceresdb/rpc/RequestLimiterInterceptorTest.java index 1059e31..58750c9 100644 --- a/ceresdb-grpc/src/test/java/com/ceresdb/rpc/RequestLimiterInterceptorTest.java +++ b/ceresdb-grpc/src/test/java/com/ceresdb/rpc/RequestLimiterInterceptorTest.java @@ -50,54 +50,43 @@ public class RequestLimiterInterceptorTest { private static final MethodDescriptor METHOD_DESCRIPTOR = MethodDescriptor - . newBuilder() - .setType( - MethodDescriptor.MethodType.UNARY) - .setFullMethodName("service/method") - .setRequestMarshaller( - StringMarshaller.INSTANCE) - .setResponseMarshaller( - StringMarshaller.INSTANCE).build(); + . newBuilder().setType(MethodDescriptor.MethodType.UNARY) + .setFullMethodName("service/method").setRequestMarshaller(StringMarshaller.INSTANCE) + .setResponseMarshaller(StringMarshaller.INSTANCE).build(); @Ignore @Test public void simulation() throws IOException { final Semaphore sem = new Semaphore(10, true); - final Server server = NettyServerBuilder.forPort(0) - .addService(ServerServiceDefinition.builder("service") - .addMethod(METHOD_DESCRIPTOR, ServerCalls.asyncUnaryCall((req, observer) -> { - try { - sem.acquire(); - TimeUnit.MILLISECONDS.sleep(100); - } catch (final InterruptedException ignored) { - } finally { - sem.release(); - } + final Server server = NettyServerBuilder.forPort(0).addService(ServerServiceDefinition.builder("service") + .addMethod(METHOD_DESCRIPTOR, ServerCalls.asyncUnaryCall((req, observer) -> { + try { + sem.acquire(); + TimeUnit.MILLISECONDS.sleep(100); + } catch (final InterruptedException ignored) { + } finally { + sem.release(); + } - observer.onNext("response"); - observer.onCompleted(); - })) - .build()) - .build() - .start(); + observer.onNext("response"); + observer.onCompleted(); + })).build()).build().start(); - final Limiter limiter = RequestLimiterBuilder.newBuilder() - .named("limit_simulation") + final Limiter limiter = RequestLimiterBuilder.newBuilder().named("limit_simulation") .metricRegistry(new LimitMetricRegistry()) // .blockOnLimit(true, 1000) // .build(); - final Channel channel = NettyChannelBuilder.forTarget("localhost:" + server.getPort()) - .usePlaintext() // + final Channel channel = NettyChannelBuilder.forTarget("localhost:" + server.getPort()).usePlaintext() // .intercept(new ClientRequestLimitInterceptor(limiter)) // .build(); final AtomicLong counter = new AtomicLong(); Executors.newSingleThreadScheduledExecutor().scheduleAtFixedRate( - () -> System.out.println(" " + counter.getAndSet(0) + " : " + limiter.toString()), 1, 1, TimeUnit.SECONDS - ); + () -> System.out.println(" " + counter.getAndSet(0) + " : " + limiter.toString()), 1, 1, + TimeUnit.SECONDS); - for (int i = 0 ; i < 10000000; i++) { + for (int i = 0; i < 10000000; i++) { counter.incrementAndGet(); ClientCalls.futureUnaryCall(channel.newCall(METHOD_DESCRIPTOR, CallOptions.DEFAULT), "request"); if (i % 10000 == 0) { diff --git a/ceresdb-http/src/main/java/com/ceresdb/http/HttpManagementClient.java b/ceresdb-http/src/main/java/com/ceresdb/http/HttpManagementClient.java index 3d6b2ab..b40c5a0 100644 --- a/ceresdb-http/src/main/java/com/ceresdb/http/HttpManagementClient.java +++ b/ceresdb-http/src/main/java/com/ceresdb/http/HttpManagementClient.java @@ -54,17 +54,17 @@ */ public class HttpManagementClient implements Management { - private static final Logger LOG = LoggerFactory.getLogger(HttpManagementClient.class); + private static final Logger LOG = LoggerFactory.getLogger(HttpManagementClient.class); private static final String AFFECTED_ROWS = "affected_rows"; private static final String ROWS = "rows"; - private final AtomicBoolean started = new AtomicBoolean(false); + private final AtomicBoolean started = new AtomicBoolean(false); - private ManagementOptions opts; + private ManagementOptions opts; - private Tenant tenant; - private RouterClient routerClient; + private Tenant tenant; + private RouterClient routerClient; @Override public boolean init(final ManagementOptions opts) { @@ -88,14 +88,15 @@ public void shutdownGracefully() { @Override public void display(final Printer out) { out.println("--- HttpManagementClient ---") // - .print("started=") // - .println(this.started) // - .print("tenant=") // - .println(this.tenant.getTenant()); + .print("started=") // + .println(this.started) // + .print("tenant=") // + .println(this.tenant.getTenant()); } @Override - public SqlResult executeSql(final boolean autoRouting, final Context ctx, final String fmtSql, final Object... args) { + public SqlResult executeSql(final boolean autoRouting, final Context ctx, final String fmtSql, + final Object... args) { final String sql = getSql(fmtSql, args); final Endpoint managementAddress = this.opts.getManagementAddress(); @@ -130,17 +131,17 @@ public SqlResult executeSql(final boolean autoRouting, final Context ctx, final private SqlResult doExecuteSql(final String sql, final Endpoint endpoint, final Context ctx) { final Request request = contextToHeaders(newBaseRequestBuilder(), ctx) // - .url(getUrl(endpoint)) // - .post(HttpUtil.requestBody(HttpUtil.params("query", sql))) // - .build(); + .url(getUrl(endpoint)) // + .post(HttpUtil.requestBody(HttpUtil.params("query", sql))) // + .build(); LOG.info("Executing sql: {}, to: {}.", sql, endpoint); try (Response resp = HttpUtil.httpClient().newCall(request).execute()) { if (!resp.isSuccessful()) { - throw new ManagementException(String.format( - "Execute sql [%s] error from server %s, err_code=%d, err_msg=%s, detail_msg=%s", // - sql, endpoint, resp.code(), resp.message(), getRespBody(resp))); + throw new ManagementException( + String.format("Execute sql [%s] error from server %s, err_code=%d, err_msg=%s, detail_msg=%s", // + sql, endpoint, resp.code(), resp.message(), getRespBody(resp))); } return toSqlResult(resp); } catch (final Throwable t) { @@ -153,11 +154,9 @@ private SqlResult doExecuteSql(final String sql, final Endpoint endpoint, final private Endpoint getTargetEndpoint(final Collection routes) { final Endpoint managementAddress = this.opts.getManagementAddress(); - return routes == null ? managementAddress : routes - .stream() - .findFirst() - .map(r -> Endpoint.of(r.getEndpoint().getIp(), managementAddress.getPort())) - .orElse(managementAddress); + return routes == null ? managementAddress : + routes.stream().findFirst().map(r -> Endpoint.of(r.getEndpoint().getIp(), managementAddress.getPort())) + .orElse(managementAddress); } private String getSql(final String fmtSql, final Object... args) { @@ -185,7 +184,7 @@ private Request.Builder contextToHeaders(final Request.Builder builder, final Co private Request.Builder newBaseRequestBuilder() { return authHeaders(new Request.Builder()) // - .addHeader("Content-Type", "application/json"); + .addHeader("Content-Type", "application/json"); } private Request.Builder authHeaders(final Request.Builder builder) { diff --git a/ceresdb-http/src/main/java/com/ceresdb/http/HttpUtil.java b/ceresdb-http/src/main/java/com/ceresdb/http/HttpUtil.java index d3d6c19..2cf83c3 100644 --- a/ceresdb-http/src/main/java/com/ceresdb/http/HttpUtil.java +++ b/ceresdb-http/src/main/java/com/ceresdb/http/HttpUtil.java @@ -36,23 +36,21 @@ */ public class HttpUtil { - public static final String PROTOCOL = "http"; + public static final String PROTOCOL = "http"; - private static final long READ_TIMEOUT_MS = SystemPropertyUtil - .getLong(OptKeys.HTTP_READ_TIMEOUT_MS, 10000); - private static final long WRITE_TIMEOUT_MS = SystemPropertyUtil.getLong(OptKeys.HTTP_WRITE_TIMEOUT_MS, - 10000); + private static final long READ_TIMEOUT_MS = SystemPropertyUtil.getLong(OptKeys.HTTP_READ_TIMEOUT_MS, 10000); + private static final long WRITE_TIMEOUT_MS = SystemPropertyUtil.getLong(OptKeys.HTTP_WRITE_TIMEOUT_MS, 10000); - private static final MediaType JSON_MEDIA_TYPE = MediaType.parse("application/json; charset=utf-8"); + private static final MediaType JSON_MEDIA_TYPE = MediaType.parse("application/json; charset=utf-8"); // The singleton HTTP client. // // Shutdown isn’t necessary, the threads and connections that // are held will be released automatically if they remain idle. - private static final OkHttpClient OK_HTTP_CLIENT = new OkHttpClient.Builder() // - .readTimeout(READ_TIMEOUT_MS, TimeUnit.MILLISECONDS) // - .writeTimeout(WRITE_TIMEOUT_MS, TimeUnit.MILLISECONDS) // - .build(); + private static final OkHttpClient OK_HTTP_CLIENT = new OkHttpClient.Builder() // + .readTimeout(READ_TIMEOUT_MS, TimeUnit.MILLISECONDS) // + .writeTimeout(WRITE_TIMEOUT_MS, TimeUnit.MILLISECONDS) // + .build(); public static OkHttpClient httpClient() { return OK_HTTP_CLIENT; @@ -62,7 +60,8 @@ public static Map params(final String key, final String val) { return params(key, val, HashMap::new); } - public static Map params(final String key, final String val, final Supplier> ctx) { + public static Map params(final String key, final String val, + final Supplier> ctx) { final Map map = ctx.get(); map.put(key, val); return map; diff --git a/ceresdb-protocol/src/main/java/com/ceresdb/BlockingStreamIterator.java b/ceresdb-protocol/src/main/java/com/ceresdb/BlockingStreamIterator.java index be426a5..a7baf95 100644 --- a/ceresdb-protocol/src/main/java/com/ceresdb/BlockingStreamIterator.java +++ b/ceresdb-protocol/src/main/java/com/ceresdb/BlockingStreamIterator.java @@ -35,10 +35,10 @@ */ public class BlockingStreamIterator implements Iterator> { - private static final QueryOk EOF = QueryOk.emptyOk(); + private static final QueryOk EOF = QueryOk.emptyOk(); - private final long timeout; - private final TimeUnit unit; + private final long timeout; + private final TimeUnit unit; private final BlockingQueue staging = new LinkedBlockingQueue<>(); private final Observer observer; diff --git a/ceresdb-protocol/src/main/java/com/ceresdb/CeresDBxClient.java b/ceresdb-protocol/src/main/java/com/ceresdb/CeresDBxClient.java index 5c837f6..fa8591e 100644 --- a/ceresdb-protocol/src/main/java/com/ceresdb/CeresDBxClient.java +++ b/ceresdb-protocol/src/main/java/com/ceresdb/CeresDBxClient.java @@ -62,7 +62,7 @@ */ public class CeresDBxClient implements Write, Query, Lifecycle, Display { - private static final Logger LOG = LoggerFactory.getLogger(CeresDBxClient.class); + private static final Logger LOG = LoggerFactory.getLogger(CeresDBxClient.class); private static final Map INSTANCES = new ConcurrentHashMap<>(); private static final AtomicInteger ID = new AtomicInteger(0); @@ -70,8 +70,8 @@ public class CeresDBxClient implements Write, Query, Lifecycle, private static final String VERSION_KEY = "client.version"; private static final String VERSION = loadVersion(); - private final int id; - private final AtomicBoolean started = new AtomicBoolean(false); + private final int id; + private final AtomicBoolean started = new AtomicBoolean(false); private CeresDBxOptions opts; private RouterClient routerClient; @@ -79,10 +79,10 @@ public class CeresDBxClient implements Write, Query, Lifecycle, private QueryClient queryClient; // CeresDBxClient is only intended to manage the instance and does not // intend to broker any of its behavior - private Management management; + private Management management; // Note: We do not close it to free resources, as we view it as shared - private Executor asyncWritePool; - private Executor asyncReadPool; + private Executor asyncWritePool; + private Executor asyncReadPool; static { // load all signal handlers @@ -226,18 +226,18 @@ public QueryClient queryClient() { @Override public void display(final Printer out) { out.println("--- CeresDBxClient ---") // - .print("id=") // - .println(this.id) // - .print("version=") // - .println(version()) // - .print("clusterAddress=") // - .println(this.opts.getClusterAddress()) // - .print("tenant=") // - .println(this.opts.getTenant().getTenant()) // - .print("userAsyncWritePool=") // - .println(this.opts.getAsyncWritePool()) // - .print("userAsyncReadPool=") // - .println(this.opts.getAsyncReadPool()); + .print("id=") // + .println(this.id) // + .print("version=") // + .println(version()) // + .print("clusterAddress=") // + .println(this.opts.getClusterAddress()) // + .print("tenant=") // + .println(this.opts.getTenant().getTenant()) // + .print("userAsyncWritePool=") // + .println(this.opts.getAsyncWritePool()) // + .print("userAsyncReadPool=") // + .println(this.opts.getAsyncReadPool()); if (this.routerClient != null) { out.println(""); @@ -345,8 +345,8 @@ private static Management initManagementClient(final CeresDBxOptions opts, final private static String loadVersion() { try { return Utils // - .loadProperties(CeresDBxClient.class.getClassLoader(), "client_version.properties") // - .getProperty(VERSION_KEY, "Unknown version"); + .loadProperties(CeresDBxClient.class.getClassLoader(), "client_version.properties") // + .getProperty(VERSION_KEY, "Unknown version"); } catch (final Exception ignored) { return "Unknown version(err)"; } diff --git a/ceresdb-protocol/src/main/java/com/ceresdb/LimitedPolicy.java b/ceresdb-protocol/src/main/java/com/ceresdb/LimitedPolicy.java index 6be35d5..6f3de7d 100644 --- a/ceresdb-protocol/src/main/java/com/ceresdb/LimitedPolicy.java +++ b/ceresdb-protocol/src/main/java/com/ceresdb/LimitedPolicy.java @@ -62,10 +62,10 @@ public boolean acquire(final Limiter limiter, final int permits) { } final String err = String.format( - "Limited by `AbortPolicy`, acquirePermits=%d, maxPermits=%d, availablePermits=%d.", // - permits, // - limiter.maxPermits(), // - limiter.availablePermits()); + "Limited by `AbortPolicy`, acquirePermits=%d, maxPermits=%d, availablePermits=%d.", // + permits, // + limiter.maxPermits(), // + limiter.availablePermits()); throw new LimitedException(err); } } @@ -115,14 +115,14 @@ public boolean acquire(final Limiter limiter, final int permits) { return true; } - final String err = String.format( - "Limited by `AbortOnBlockingTimeoutPolicy[timeout=%d, unit=%s]`, acquirePermits=%d, " + // - "maxPermits=%d, availablePermits=%d.", // - timeout(), // - unit(), // - permits, // - limiter.maxPermits(), // - limiter.availablePermits()); + final String err = String + .format("Limited by `AbortOnBlockingTimeoutPolicy[timeout=%d, unit=%s]`, acquirePermits=%d, " + // + "maxPermits=%d, availablePermits=%d.", // + timeout(), // + unit(), // + permits, // + limiter.maxPermits(), // + limiter.availablePermits()); throw new LimitedException(err); } } diff --git a/ceresdb-protocol/src/main/java/com/ceresdb/MetricParser.java b/ceresdb-protocol/src/main/java/com/ceresdb/MetricParser.java index 2a9b70c..00e4751 100644 --- a/ceresdb-protocol/src/main/java/com/ceresdb/MetricParser.java +++ b/ceresdb-protocol/src/main/java/com/ceresdb/MetricParser.java @@ -45,22 +45,13 @@ public interface MetricParser { List createColumns(); enum StatementType { - Unknown, - Create, - Select, - Alter, - Describe, - Show, - Drop, - Insert, - Exists + Unknown, Create, Select, Alter, Describe, Show, Drop, Insert, Exists } interface Column { enum ColumnType { - Unknown, - Timestamp, // + Unknown, Timestamp, // Tag, // Field, } diff --git a/ceresdb-protocol/src/main/java/com/ceresdb/MetricParserFactoryProvider.java b/ceresdb-protocol/src/main/java/com/ceresdb/MetricParserFactoryProvider.java index 79ced38..9af91eb 100644 --- a/ceresdb-protocol/src/main/java/com/ceresdb/MetricParserFactoryProvider.java +++ b/ceresdb-protocol/src/main/java/com/ceresdb/MetricParserFactoryProvider.java @@ -25,8 +25,8 @@ public class MetricParserFactoryProvider { private static final MetricParserFactory METRIC_PARSER_FACTORY = ServiceLoader // - .load(MetricParserFactory.class) // - .firstOrDefault(() -> MetricParserFactory.DEFAULT); + .load(MetricParserFactory.class) // + .firstOrDefault(() -> MetricParserFactory.DEFAULT); public static MetricParserFactory getMetricParserFactory() { return METRIC_PARSER_FACTORY; diff --git a/ceresdb-protocol/src/main/java/com/ceresdb/QueryClient.java b/ceresdb-protocol/src/main/java/com/ceresdb/QueryClient.java index 54d4533..0b48825 100644 --- a/ceresdb-protocol/src/main/java/com/ceresdb/QueryClient.java +++ b/ceresdb-protocol/src/main/java/com/ceresdb/QueryClient.java @@ -53,10 +53,10 @@ public class QueryClient implements Query, Lifecycle, Display { private static final Logger LOG = LoggerFactory.getLogger(QueryClient.class); - private QueryOptions opts; - private RouterClient routerClient; - private Executor asyncPool; - private QueryLimiter queryLimiter; + private QueryOptions opts; + private RouterClient routerClient; + private Executor asyncPool; + private QueryLimiter queryLimiter; static final class InnerMetrics { static final Histogram READ_ROW_COUNT = MetricsUtil.histogram("read_row_count"); @@ -88,7 +88,7 @@ public boolean init(final QueryOptions opts) { final Executor pool = this.opts.getAsyncPool(); this.asyncPool = pool != null ? pool : new SerializingExecutor("query_client"); this.queryLimiter = new DefaultQueryLimiter(this.opts.getMaxInFlightQueryRequests(), - this.opts.getLimitedPolicy()); + this.opts.getLimitedPolicy()); return true; } @@ -108,11 +108,8 @@ public CompletableFuture> query(final QueryRequest req, fin final int rowCount = r.mapOr(0, QueryOk::getRowCount); InnerMetrics.readRowCount().update(rowCount); if (Utils.isRwLogging()) { - LOG.info("Read from {}, duration={} ms, rowCount={}.", - Utils.DB_NAME, - Clock.defaultClock().duration(startCall), - rowCount - ); + LOG.info("Read from {}, duration={} ms, rowCount={}.", Utils.DB_NAME, + Clock.defaultClock().duration(startCall), rowCount); } if (r.isOk()) { return; @@ -130,10 +127,7 @@ public void streamQuery(final QueryRequest req, final Context ctx, final Observe setMetricsIfAbsent(req); this.routerClient.routeFor(req.getMetrics()) - .thenApply(routes -> routes.values() - .stream() - .findAny() - .orElse(this.routerClient.clusterRoute())) + .thenApply(routes -> routes.values().stream().findAny().orElse(this.routerClient.clusterRoute())) .thenAccept(route -> streamQueryFrom(route.getEndpoint(), req, ctx, observer)); } @@ -144,9 +138,9 @@ private CompletableFuture> query0(final QueryRequest req, / return this.routerClient.routeFor(req.getMetrics()) // .thenApplyAsync(routes -> routes.values() // - .stream() // - .findAny() // everyone is OK - .orElse(this.routerClient.clusterRoute()), this.asyncPool) // + .stream() // + .findAny() // everyone is OK + .orElse(this.routerClient.clusterRoute()), this.asyncPool) // .thenComposeAsync(route -> queryFrom(route.getEndpoint(), req, ctx, retries), this.asyncPool) .thenComposeAsync(r -> { if (r.isOk()) { @@ -163,9 +157,9 @@ private CompletableFuture> query0(final QueryRequest req, / // Should refresh route table final Set toRefresh = err.stream() // - .filter(Utils::shouldRefreshRouteTable) // - .flatMap(e -> e.getFailedMetrics().stream()) // - .collect(Collectors.toSet()); + .filter(Utils::shouldRefreshRouteTable) // + .flatMap(e -> e.getFailedMetrics().stream()) // + .collect(Collectors.toSet()); if (toRefresh.isEmpty()) { return Utils.completedCf(r); @@ -173,7 +167,7 @@ private CompletableFuture> query0(final QueryRequest req, / // Async to refresh route info return this.routerClient.routeRefreshFor(toRefresh) - .thenComposeAsync(routes -> query0(req, ctx, retries + 1), this.asyncPool); + .thenComposeAsync(routes -> query0(req, ctx, retries + 1), this.asyncPool); }, this.asyncPool); } @@ -208,16 +202,14 @@ private CompletableFuture> queryFrom(final Endpoint endpoin .setQl(req.getQl()) // .build(); - final CompletableFuture qrf = this.routerClient.invoke( - endpoint, // + final CompletableFuture qrf = this.routerClient.invoke(endpoint, // request, // ctx.with("retries", retries) // server can use this in metrics ); return qrf.thenApplyAsync( resp -> Utils.toResult(resp, req.getQl(), endpoint, req.getMetrics(), new ErrHandler(req)), - this.asyncPool - ); + this.asyncPool); } private void streamQueryFrom(final Endpoint endpoint, // @@ -225,16 +217,16 @@ private void streamQueryFrom(final Endpoint endpoint, // final Context ctx, // final Observer observer) { final Storage.QueryRequest request = Storage.QueryRequest.newBuilder() // - .addAllMetrics(req.getMetrics()) // - .setQl(req.getQl()) // - .build(); + .addAllMetrics(req.getMetrics()) // + .setQl(req.getQl()) // + .build(); this.routerClient.invokeServerStreaming(endpoint, request, ctx, new Observer() { @Override public void onNext(final Storage.QueryResponse value) { final Result ret = Utils.toResult(value, req.getQl(), endpoint, req.getMetrics(), - new ErrHandler(req)); + new ErrHandler(req)); if (ret.isOk()) { observer.onNext(ret.getOk()); } else { @@ -262,10 +254,10 @@ public Executor executor() { @Override public void display(final Printer out) { out.println("--- QueryClient ---") // - .print("maxRetries=") // - .println(this.opts.getMaxRetries()) // - .print("asyncPool=") // - .println(this.asyncPool); + .print("maxRetries=") // + .println(this.opts.getMaxRetries()) // + .print("asyncPool=") // + .println(this.asyncPool); } @Override @@ -292,10 +284,10 @@ public int calculatePermits(final QueryRequest request) { @Override public Result rejected(final QueryRequest request, final RejectedState state) { final String errMsg = String.format( - "Query limited by client, acquirePermits=%d, maxPermits=%d, availablePermits=%d.", // - state.acquirePermits(), // - state.maxPermits(), // - state.availablePermits()); + "Query limited by client, acquirePermits=%d, maxPermits=%d, availablePermits=%d.", // + state.acquirePermits(), // + state.maxPermits(), // + state.availablePermits()); return Result.err(Err.queryErr(Result.FLOW_CONTROL, errMsg, null, request.getQl(), request.getMetrics())); } } diff --git a/ceresdb-protocol/src/main/java/com/ceresdb/RouterClient.java b/ceresdb-protocol/src/main/java/com/ceresdb/RouterClient.java index a1924db..741ffa3 100644 --- a/ceresdb-protocol/src/main/java/com/ceresdb/RouterClient.java +++ b/ceresdb-protocol/src/main/java/com/ceresdb/RouterClient.java @@ -63,31 +63,29 @@ */ public class RouterClient implements Lifecycle, Display, Iterable { - private static final Logger LOG = LoggerFactory.getLogger(RouterClient.class); + private static final Logger LOG = LoggerFactory.getLogger(RouterClient.class); // I don't think they needs to be open to user configuration, so I'll just put a fixed value here - private static final float CLEAN_CACHE_THRESHOLD = 0.75f; - private static final float CLEAN_THRESHOLD = 0.1f; - private static final int MAX_CONTINUOUS_GC_TIMES = 3; + private static final float CLEAN_CACHE_THRESHOLD = 0.75f; + private static final float CLEAN_THRESHOLD = 0.1f; + private static final int MAX_CONTINUOUS_GC_TIMES = 3; - private static final int ITEM_COUNT_EACH_REFRESH = 512; - private static final long BLOCKING_ROUTE_TIMEOUT_MS = 3000; + private static final int ITEM_COUNT_EACH_REFRESH = 512; + private static final long BLOCKING_ROUTE_TIMEOUT_MS = 3000; - private static final SharedScheduledPool CLEANER_POOL = Utils.getSharedScheduledPool( - "route_cache_cleaner", 1); - private static final SharedScheduledPool REFRESHER_POOL = Utils.getSharedScheduledPool( - "route_cache_refresher", - Math.min(4, Cpus.cpus())); + private static final SharedScheduledPool CLEANER_POOL = Utils.getSharedScheduledPool("route_cache_cleaner", 1); + private static final SharedScheduledPool REFRESHER_POOL = Utils.getSharedScheduledPool("route_cache_refresher", + Math.min(4, Cpus.cpus())); - private ScheduledExecutorService cleaner; - private ScheduledExecutorService refresher; + private ScheduledExecutorService cleaner; + private ScheduledExecutorService refresher; - private RouterOptions opts; - private RpcClient rpcClient; - private RouterByMetrics router; - private InnerMetrics metrics; + private RouterOptions opts; + private RpcClient rpcClient; + private RouterByMetrics router; + private InnerMetrics metrics; - private final ConcurrentMap routeCache = new ConcurrentHashMap<>(); + private final ConcurrentMap routeCache = new ConcurrentHashMap<>(); static final class InnerMetrics { final Histogram refreshedSize; @@ -153,7 +151,8 @@ public boolean init(final RouterOptions opts) { final long refreshPeriod = this.opts.getRefreshPeriodSeconds(); if (refreshPeriod > 0) { this.refresher = REFRESHER_POOL.getObject(); - this.refresher.scheduleWithFixedDelay(this::refresh, Utils.randomInitialDelay(180), refreshPeriod, TimeUnit.SECONDS); + this.refresher.scheduleWithFixedDelay(this::refresh, Utils.randomInitialDelay(180), refreshPeriod, + TimeUnit.SECONDS); LOG.info("Route table cache refresher has been started."); } @@ -208,39 +207,38 @@ public CompletableFuture> routeFor(final Collection m } return routeRefreshFor(misses) // refresh from remote - .thenApply(remote -> { // then merge result - final Map ret; - if (remote.size() > local.size()) { - remote.putAll(local); - ret = remote; - } else { - local.putAll(remote); - ret = local; - } - return ret; - }) // - .thenApply(hits -> { // update cache hits - final long now = Clock.defaultClock().getTick(); - hits.values().forEach(route -> route.tryWeekSetHit(now)); - return hits; - }); + .thenApply(remote -> { // then merge result + final Map ret; + if (remote.size() > local.size()) { + remote.putAll(local); + ret = remote; + } else { + local.putAll(remote); + ret = local; + } + return ret; + }) // + .thenApply(hits -> { // update cache hits + final long now = Clock.defaultClock().getTick(); + hits.values().forEach(route -> route.tryWeekSetHit(now)); + return hits; + }); } public CompletableFuture> routeRefreshFor(final Collection metrics) { final long startCall = Clock.defaultClock().getTick(); - return this.router.routeFor(metrics) - .whenComplete((remote, err) -> { - if (err == null) { - this.routeCache.putAll(remote); - this.metrics.refreshedSize().update(remote.size()); - this.metrics.cachedSize().update(this.routeCache.size()); - this.metrics.refreshTimer().update(Clock.defaultClock().duration(startCall), TimeUnit.MILLISECONDS); - - LOG.info("Route refreshed: {}, cached_size={}.", metrics, this.routeCache.size()); - } else { - LOG.warn("Route refresh failed: {}.", metrics, err); - } - }); + return this.router.routeFor(metrics).whenComplete((remote, err) -> { + if (err == null) { + this.routeCache.putAll(remote); + this.metrics.refreshedSize().update(remote.size()); + this.metrics.cachedSize().update(this.routeCache.size()); + this.metrics.refreshTimer().update(Clock.defaultClock().duration(startCall), TimeUnit.MILLISECONDS); + + LOG.info("Route refreshed: {}, cached_size={}.", metrics, this.routeCache.size()); + } else { + LOG.warn("Route refresh failed: {}.", metrics, err); + } + }); } private void blockingRouteRefreshFor(final Collection metrics) { @@ -310,8 +308,8 @@ private int gc0(final int times) { itemsToGC, // (o1, o2) -> -Long.compare(o1.getValue().getLastHit(), o2.getValue().getLastHit()) // ) // - .map(Map.Entry::getKey) // - .collect(Collectors.toList()); + .map(Map.Entry::getKey) // + .collect(Collectors.toList()); this.metrics.gcItems().update(topK.size()); @@ -385,10 +383,7 @@ public Observer invokeClientStreaming(final Endpoint endpoint, } private Collection reserveAddresses() { - return this.routeCache.values() - .stream() - .map(Route::getEndpoint) - .collect(Collectors.toSet()); + return this.routeCache.values().stream().map(Route::getEndpoint).collect(Collectors.toSet()); } private boolean checkConn(final Endpoint endpoint, final boolean create) { @@ -398,10 +393,10 @@ private boolean checkConn(final Endpoint endpoint, final boolean create) { @Override public void display(final Printer out) { out.println("--- RouterClient ---") // - .print("opts=") // - .println(this.opts) // - .print("routeCache.size=") // - .println(this.routeCache.size()); + .print("opts=") // + .println(this.opts) // + .print("routeCache.size=") // + .println(this.routeCache.size()); if (this.rpcClient != null) { out.println(""); @@ -432,16 +427,13 @@ public CompletableFuture> routeFor(final Collection r return Utils.completedCf(Collections.emptyMap()); } - final Storage.RouteRequest req = Storage.RouteRequest.newBuilder() - .addAllMetrics(request) - .build(); + final Storage.RouteRequest req = Storage.RouteRequest.newBuilder().addAllMetrics(request).build(); final Context ctx = Context.of("call_priority", "100"); // Mysterious trick!!! \(^▽^)/ final CompletableFuture f = invokeRpc(req, ctx); return f.thenCompose(resp -> { if (Utils.isSuccess(resp.getHeader())) { - final Map ret = resp.getRoutesList() - .stream() + final Map ret = resp.getRoutesList().stream() .collect(Collectors.toMap(Storage.Route::getMetric, this::toRouteObj)); return Utils.completedCf(ret); } diff --git a/ceresdb-protocol/src/main/java/com/ceresdb/RpcServiceRegister.java b/ceresdb-protocol/src/main/java/com/ceresdb/RpcServiceRegister.java index 0d3d46a..695d8f0 100644 --- a/ceresdb-protocol/src/main/java/com/ceresdb/RpcServiceRegister.java +++ b/ceresdb-protocol/src/main/java/com/ceresdb/RpcServiceRegister.java @@ -28,41 +28,41 @@ */ public class RpcServiceRegister { - private static final double WRITE_LIMIT_PERCENT = writeLimitPercent(); + private static final double WRITE_LIMIT_PERCENT = writeLimitPercent(); private static final String STORAGE_METHOD_TEMPLATE = "storage.StorageService/%s"; public static void registerStorageService() { // register protobuf serializer RpcFactoryProvider.getRpcFactory().register( - MethodDescriptor.of(String.format(STORAGE_METHOD_TEMPLATE, "Route"), MethodDescriptor.MethodType.UNARY), // - Storage.RouteRequest.class, // - Storage.RouteRequest.getDefaultInstance(), // - Storage.RouteResponse.getDefaultInstance()); + MethodDescriptor.of(String.format(STORAGE_METHOD_TEMPLATE, "Route"), MethodDescriptor.MethodType.UNARY), // + Storage.RouteRequest.class, // + Storage.RouteRequest.getDefaultInstance(), // + Storage.RouteResponse.getDefaultInstance()); RpcFactoryProvider.getRpcFactory().register( - MethodDescriptor.of(String.format(STORAGE_METHOD_TEMPLATE, "Write"), MethodDescriptor.MethodType.UNARY, - WRITE_LIMIT_PERCENT), // - Storage.WriteRequest.class, // - Storage.WriteRequest.getDefaultInstance(), // - Storage.WriteResponse.getDefaultInstance()); + MethodDescriptor.of(String.format(STORAGE_METHOD_TEMPLATE, "Write"), MethodDescriptor.MethodType.UNARY, + WRITE_LIMIT_PERCENT), // + Storage.WriteRequest.class, // + Storage.WriteRequest.getDefaultInstance(), // + Storage.WriteResponse.getDefaultInstance()); RpcFactoryProvider.getRpcFactory().register( - MethodDescriptor.of(String.format(STORAGE_METHOD_TEMPLATE, "StreamWrite"), - MethodDescriptor.MethodType.CLIENT_STREAMING), // - Storage.WriteRequest.class, // - Storage.WriteRequest.getDefaultInstance(), // - Storage.WriteResponse.getDefaultInstance()); + MethodDescriptor.of(String.format(STORAGE_METHOD_TEMPLATE, "StreamWrite"), + MethodDescriptor.MethodType.CLIENT_STREAMING), // + Storage.WriteRequest.class, // + Storage.WriteRequest.getDefaultInstance(), // + Storage.WriteResponse.getDefaultInstance()); RpcFactoryProvider.getRpcFactory().register( - MethodDescriptor.of(String.format(STORAGE_METHOD_TEMPLATE, "Query"), MethodDescriptor.MethodType.UNARY, - 1 - WRITE_LIMIT_PERCENT), // - Storage.QueryRequest.class, // - Storage.QueryRequest.getDefaultInstance(), // - Storage.QueryResponse.getDefaultInstance()); + MethodDescriptor.of(String.format(STORAGE_METHOD_TEMPLATE, "Query"), MethodDescriptor.MethodType.UNARY, + 1 - WRITE_LIMIT_PERCENT), // + Storage.QueryRequest.class, // + Storage.QueryRequest.getDefaultInstance(), // + Storage.QueryResponse.getDefaultInstance()); RpcFactoryProvider.getRpcFactory().register( - MethodDescriptor.of(String.format(STORAGE_METHOD_TEMPLATE, "StreamQuery"), - MethodDescriptor.MethodType.SERVER_STREAMING), // - Storage.QueryRequest.class, // - Storage.QueryRequest.getDefaultInstance(), // - Storage.QueryResponse.getDefaultInstance()); + MethodDescriptor.of(String.format(STORAGE_METHOD_TEMPLATE, "StreamQuery"), + MethodDescriptor.MethodType.SERVER_STREAMING), // + Storage.QueryRequest.class, // + Storage.QueryRequest.getDefaultInstance(), // + Storage.QueryResponse.getDefaultInstance()); } private static double writeLimitPercent() { diff --git a/ceresdb-protocol/src/main/java/com/ceresdb/Utils.java b/ceresdb-protocol/src/main/java/com/ceresdb/Utils.java index 11f0964..f17e732 100644 --- a/ceresdb-protocol/src/main/java/com/ceresdb/Utils.java +++ b/ceresdb-protocol/src/main/java/com/ceresdb/Utils.java @@ -65,9 +65,9 @@ */ public final class Utils { - public static final String DB_NAME = "CeresDB"; + public static final String DB_NAME = "CeresDB"; - private static final AtomicBoolean RW_LOGGING; + private static final AtomicBoolean RW_LOGGING; private static final int REPORT_PERIOD_MIN; private static final ScheduledExecutorService DISPLAY; @@ -75,15 +75,15 @@ public final class Utils { static { RW_LOGGING = new AtomicBoolean(SystemPropertyUtil.getBool(OptKeys.RW_LOGGING, true)); REPORT_PERIOD_MIN = SystemPropertyUtil.getInt(OptKeys.REPORT_PERIOD, 30); - DISPLAY = ThreadPoolUtil.newScheduledBuilder() - .poolName("display_self") // + DISPLAY = ThreadPoolUtil.newScheduledBuilder().poolName("display_self") // .coreThreads(1) // .enableMetric(true) // .threadFactory(new NamedThreadFactory("display_self", true)) // .rejectedHandler(new ThreadPoolExecutor.DiscardOldestPolicy()) // .build(); - Runtime.getRuntime().addShutdownHook(new Thread(() -> ExecutorServiceHelper.shutdownAndAwaitTermination(DISPLAY))); + Runtime.getRuntime() + .addShutdownHook(new Thread(() -> ExecutorServiceHelper.shutdownAndAwaitTermination(DISPLAY))); } /** @@ -196,11 +196,9 @@ public static Result toResult(final Storage.WriteResponse resp, // final int failed = resp.getFailed(); if (code == Result.SUCCESS) { - final Set metrics = rows != null && WriteOk.isCollectWroteDetail() - ? rows.stream() - .map(Rows::getMetric) - .collect(Collectors.toSet()) - : null; + final Set metrics = rows != null && WriteOk.isCollectWroteDetail() ? + rows.stream().map(Rows::getMetric).collect(Collectors.toSet()) : + null; return WriteOk.ok(success, failed, metrics).mapToResult(); } else { return Err.writeErr(code, msg, to, rows).mapToResult(); @@ -220,17 +218,14 @@ public static Result toResult(final Storage.WriteResponse resp, // public static Result toResult(final Storage.QueryResponse resp, // final String ql, // final Endpoint to, // - final Collection metrics, - final Runnable errHandler) { + final Collection metrics, final Runnable errHandler) { final Common.ResponseHeader header = resp.getHeader(); final int code = header.getCode(); final String msg = header.getError(); if (code == Result.SUCCESS) { final int rowCount = resp.getRowsCount(); - final Stream rows = resp.getRowsList() - .stream() - .map(ByteStringHelper::sealByteArray); + final Stream rows = resp.getRowsList().stream().map(ByteStringHelper::sealByteArray); return QueryOk.ok(ql, toSchema(resp), rowCount, rows).mapToResult(); } else { if (errHandler != null) { @@ -303,10 +298,10 @@ public static CompletableFuture errorCf(final Throwable t) { public static Map> splitDataByRoute(final Collection data, // final Map routes) { final Map> splits = routes.values() // - .stream() // - .map(Route::getEndpoint) // - .distinct() // - .collect(Collectors.toMap(k -> k, k -> Spines.newBuf(), (v1, v2) -> v1)); + .stream() // + .map(Route::getEndpoint) // + .distinct() // + .collect(Collectors.toMap(k -> k, k -> Spines.newBuf(), (v1, v2) -> v1)); if (splits.size() == 1) { // fast path, zero copy splits.replaceAll((ep, empty) -> data); diff --git a/ceresdb-protocol/src/main/java/com/ceresdb/WriteClient.java b/ceresdb-protocol/src/main/java/com/ceresdb/WriteClient.java index 6ddacd1..5f95875 100644 --- a/ceresdb-protocol/src/main/java/com/ceresdb/WriteClient.java +++ b/ceresdb-protocol/src/main/java/com/ceresdb/WriteClient.java @@ -64,10 +64,10 @@ public class WriteClient implements Write, Lifecycle, Display { private static final Logger LOG = LoggerFactory.getLogger(WriteClient.class); - private WriteOptions opts; - private RouterClient routerClient; - private Executor asyncPool; - private WriteLimiter writeLimiter; + private WriteOptions opts; + private RouterClient routerClient; + private Executor asyncPool; + private WriteLimiter writeLimiter; static final class InnerMetrics { static final Histogram WRITE_ROWS_SUCCESS = MetricsUtil.histogram("write_rows_success_num"); @@ -125,11 +125,8 @@ public CompletableFuture> write(final Collection data InnerMetrics.writeQps().mark(); if (r != null) { if (Utils.isRwLogging()) { - LOG.info("Write to {}, duration={} ms, result={}.", - Utils.DB_NAME, - Clock.defaultClock().duration(startCall), - r - ); + LOG.info("Write to {}, duration={} ms, result={}.", Utils.DB_NAME, + Clock.defaultClock().duration(startCall), r); } if (r.isOk()) { final WriteOk ok = r.getOk(); @@ -149,10 +146,7 @@ public StreamWriteBuf streamWrite(final String metric, final Cont final CompletableFuture respFuture = new CompletableFuture<>(); return this.routerClient.routeFor(Collections.singleton(metric)) - .thenApply(routes -> routes.values() - .stream() - .findFirst() - .orElseGet(() -> Route.invalid(metric))) + .thenApply(routes -> routes.values().stream().findFirst().orElseGet(() -> Route.invalid(metric))) .thenApply(route -> streamWriteTo(route, ctx, Utils.toUnaryObserver(respFuture))) .thenApply(reqObserver -> new StreamWriteBuf() { @@ -211,58 +205,58 @@ private CompletableFuture> write0(final Collection da // 1. Get routes return this.routerClient.routeFor(metrics) - // 2. Split data by route info and write to DB - .thenComposeAsync(routes -> Utils.splitDataByRoute(data, routes) - .entrySet() - .stream() - // Write to database - .map(e -> writeTo(e.getKey(), e.getValue(), ctx.copy(), retries)) - // Reduce and combine write result - .reduce((f1, f2) -> f1.thenCombineAsync(f2, Utils::combineResult, this.asyncPool)) - .orElse(Utils.completedCf(WriteOk.emptyOk().mapToResult())), this.asyncPool) - // 3. If failed, refresh route info and retry on INVALID_ROUTE - .thenComposeAsync(r -> { - if (r.isOk()) { - LOG.debug("Success to write to {}, ok={}.", Utils.DB_NAME, r.getOk()); - return Utils.completedCf(r); - } + // 2. Split data by route info and write to DB + .thenComposeAsync(routes -> Utils.splitDataByRoute(data, routes).entrySet().stream() + // Write to database + .map(e -> writeTo(e.getKey(), e.getValue(), ctx.copy(), retries)) + // Reduce and combine write result + .reduce((f1, f2) -> f1.thenCombineAsync(f2, Utils::combineResult, this.asyncPool)) + .orElse(Utils.completedCf(WriteOk.emptyOk().mapToResult())), this.asyncPool) + // 3. If failed, refresh route info and retry on INVALID_ROUTE + .thenComposeAsync(r -> { + if (r.isOk()) { + LOG.debug("Success to write to {}, ok={}.", Utils.DB_NAME, r.getOk()); + return Utils.completedCf(r); + } - final Err err = r.getErr(); - LOG.warn("Failed to write to {}, retries={}, err={}.", Utils.DB_NAME, retries, err); - if (retries + 1 > this.opts.getMaxRetries()) { - LOG.error("Retried {} times still failed.", retries); - return Utils.completedCf(r); - } + final Err err = r.getErr(); + LOG.warn("Failed to write to {}, retries={}, err={}.", Utils.DB_NAME, retries, err); + if (retries + 1 > this.opts.getMaxRetries()) { + LOG.error("Retried {} times still failed.", retries); + return Utils.completedCf(r); + } - // Should refresh route table - final Set toRefresh = err.stream() // - .filter(Utils::shouldRefreshRouteTable) // - .flatMap(e -> e.getFailedWrites().stream()) // - .map(Rows::getMetric) // - .collect(Collectors.toSet()); - - // Should retries - final List rowsToRetry = err.stream() // - .filter(Utils::shouldRetry) // - .flatMap(e -> e.getFailedWrites().stream()) // - .collect(Collectors.toList()); - - // Should not retries - final Optional noRetryErr = err.stream() // - .filter(Utils::shouldNotRetry) // - .reduce(Err::combine); - - // Async refresh route info - final CompletableFuture> rwf = this.routerClient.routeRefreshFor(toRefresh) - // Even for some data that does not require a refresh of the routing table, - // we still wait until the routing table is flushed successfully before - // retrying it, in order to give the server a break. - .thenComposeAsync(routes -> write0(rowsToRetry, ctx, retries + 1), this.asyncPool); - - return noRetryErr.isPresent() - ? rwf.thenApplyAsync(ret -> Utils.combineResult(noRetryErr.get().mapToResult(), ret), this.asyncPool) - : rwf.thenApplyAsync(ret -> Utils.combineResult(err.getSubOk().mapToResult(), ret), this.asyncPool); - }, this.asyncPool); + // Should refresh route table + final Set toRefresh = err.stream() // + .filter(Utils::shouldRefreshRouteTable) // + .flatMap(e -> e.getFailedWrites().stream()) // + .map(Rows::getMetric) // + .collect(Collectors.toSet()); + + // Should retries + final List rowsToRetry = err.stream() // + .filter(Utils::shouldRetry) // + .flatMap(e -> e.getFailedWrites().stream()) // + .collect(Collectors.toList()); + + // Should not retries + final Optional noRetryErr = err.stream() // + .filter(Utils::shouldNotRetry) // + .reduce(Err::combine); + + // Async refresh route info + final CompletableFuture> rwf = this.routerClient.routeRefreshFor(toRefresh) + // Even for some data that does not require a refresh of the routing table, + // we still wait until the routing table is flushed successfully before + // retrying it, in order to give the server a break. + .thenComposeAsync(routes -> write0(rowsToRetry, ctx, retries + 1), this.asyncPool); + + return noRetryErr.isPresent() ? + rwf.thenApplyAsync(ret -> Utils.combineResult(noRetryErr.get().mapToResult(), ret), + this.asyncPool) : + rwf.thenApplyAsync(ret -> Utils.combineResult(err.getSubOk().mapToResult(), ret), + this.asyncPool); + }, this.asyncPool); } private CompletableFuture> writeTo(final Endpoint endpoint, // @@ -292,8 +286,8 @@ private CompletableFuture> writeTo(final Endpoint endpoint, } return fs.build() // - .reduce((f1, f2) -> f1.thenCombineAsync(f2, Utils::combineResult, this.asyncPool)) // - .orElse(Utils.completedCf(WriteOk.emptyOk().mapToResult())); + .reduce((f1, f2) -> f1.thenCombineAsync(f2, Utils::combineResult, this.asyncPool)) // + .orElse(Utils.completedCf(WriteOk.emptyOk().mapToResult())); } private static class PartBuf { @@ -329,8 +323,7 @@ private CompletableFuture> writeTo0(final Endpoint endpoint final Collection data, // final Context ctx, // final int retries) { - final CompletableFuture wrf = this.routerClient.invoke( - endpoint, // + final CompletableFuture wrf = this.routerClient.invoke(endpoint, // toWriteRequestObj(data.stream()), // ctx.with("retries", retries) // server can use this in metrics ); @@ -341,8 +334,7 @@ private CompletableFuture> writeTo0(final Endpoint endpoint private Observer> streamWriteTo(final Route route, // final Context ctx, // final Observer respObserver) { - final Observer rpcObs = this.routerClient.invokeClientStreaming( - route.getEndpoint(), // + final Observer rpcObs = this.routerClient.invokeClientStreaming(route.getEndpoint(), // Storage.WriteRequest.getDefaultInstance(), // ctx, // new Observer() { @@ -378,7 +370,8 @@ public void onNext(final Stream value) { if (this.metric.equals(rs.getMetric())) { return true; } - throw new StreamException(String.format("Invalid metric %s, only can write %s.", rs.getMetric(), this.metric)); + throw new StreamException( + String.format("Invalid metric %s, only can write %s.", rs.getMetric(), this.metric)); }); rpcObs.onNext(toWriteRequestObj(data)); @@ -450,9 +443,9 @@ public NameDict getFieldDict() { public Storage.WriteMetric build() { return this.wmcBui // - .addAllTagNames(this.tagDict.toOrdered()) // - .addAllFieldNames(this.fieldDict.toOrdered()) // - .build(); + .addAllTagNames(this.tagDict.toOrdered()) // + .addAllFieldNames(this.fieldDict.toOrdered()) // + .build(); } } @@ -471,22 +464,19 @@ public Storage.WriteRequest toWriteRequestObj(final Stream data) { if (Value.isNull(tagV)) { return; } - final Storage.Tag.Builder tBui = Storage.Tag.newBuilder() - .setNameIndex(tagDict.insert(tagK)) + final Storage.Tag.Builder tBui = Storage.Tag.newBuilder().setNameIndex(tagDict.insert(tagK)) .setValue(Utils.toProtoValue(tagV)); weyBui.addTags(tBui.build()); }); final NameDict fieldDict = tp3.getFieldDict(); rs.getFields().forEach((ts, fields) -> { - final Storage.FieldGroup.Builder fgBui = Storage.FieldGroup.newBuilder() - .setTimestamp(ts); + final Storage.FieldGroup.Builder fgBui = Storage.FieldGroup.newBuilder().setTimestamp(ts); fields.forEach((name, field) -> { if (Value.isNull(field)) { return; } - final Storage.Field.Builder fBui = Storage.Field.newBuilder() - .setNameIndex(fieldDict.insert(name)) + final Storage.Field.Builder fBui = Storage.Field.newBuilder().setNameIndex(fieldDict.insert(name)) .setValue(Utils.toProtoValue(field)); fgBui.addFields(fBui.build()); }); @@ -504,12 +494,12 @@ public Storage.WriteRequest toWriteRequestObj(final Stream data) { @Override public void display(final Printer out) { out.println("--- WriteClient ---") // - .print("maxRetries=") // - .println(this.opts.getMaxRetries()) // - .print("maxWriteSize=") // - .println(this.opts.getMaxWriteSize()) // - .print("asyncPool=") // - .println(this.asyncPool); + .print("maxRetries=") // + .println(this.opts.getMaxRetries()) // + .print("maxWriteSize=") // + .println(this.opts.getMaxWriteSize()) // + .print("asyncPool=") // + .println(this.asyncPool); } @Override @@ -530,18 +520,16 @@ public DefaultWriteLimiter(int maxInFlight, LimitedPolicy policy) { @Override public int calculatePermits(final Collection in) { - return in == null ? 0 : in.stream() - .map(Rows::getRowCount) - .reduce(0, Integer::sum); + return in == null ? 0 : in.stream().map(Rows::getRowCount).reduce(0, Integer::sum); } @Override public Result rejected(final Collection in, final RejectedState state) { final String errMsg = String.format( - "Write limited by client, acquirePermits=%d, maxPermits=%d, availablePermits=%d.", // - state.acquirePermits(), // - state.maxPermits(), // - state.availablePermits()); + "Write limited by client, acquirePermits=%d, maxPermits=%d, availablePermits=%d.", // + state.acquirePermits(), // + state.maxPermits(), // + state.availablePermits()); return Result.err(Err.writeErr(Result.FLOW_CONTROL, errMsg, null, in)); } } diff --git a/ceresdb-protocol/src/main/java/com/ceresdb/models/Err.java b/ceresdb-protocol/src/main/java/com/ceresdb/models/Err.java index 5cd860f..e353213 100644 --- a/ceresdb-protocol/src/main/java/com/ceresdb/models/Err.java +++ b/ceresdb-protocol/src/main/java/com/ceresdb/models/Err.java @@ -33,21 +33,21 @@ */ public class Err implements Streamable { // error code from server - private int code; + private int code; // error message - private String error; + private String error; // the server address where the error occurred - private Endpoint errTo; + private Endpoint errTo; // the data of wrote failed, can be used to retry - private Collection failedWrites; + private Collection failedWrites; // other successful server results are merged here - private WriteOk subOk; + private WriteOk subOk; // the QL failed to query - private String failedQl; + private String failedQl; // the metrics of failed to query private Collection failedMetrics; // child err merged here - private Collection children; + private Collection children; public int getCode() { return code; diff --git a/ceresdb-protocol/src/main/java/com/ceresdb/models/QueryOk.java b/ceresdb-protocol/src/main/java/com/ceresdb/models/QueryOk.java index 9802ad2..b530662 100644 --- a/ceresdb-protocol/src/main/java/com/ceresdb/models/QueryOk.java +++ b/ceresdb-protocol/src/main/java/com/ceresdb/models/QueryOk.java @@ -105,6 +105,6 @@ public static QueryOk ok(final String ql, final Schema schema, final int rowCoun private static void ensureAvroType(final Schema schema) { Requires.requireNonNull(schema, "NUll.schema"); Requires.requireTrue(schema.getType() == Schema.Type.Avro, "Invalid schema type %s, [Avro] type is required", - schema); + schema); } } diff --git a/ceresdb-protocol/src/main/java/com/ceresdb/models/Record.java b/ceresdb-protocol/src/main/java/com/ceresdb/models/Record.java index 2d0c707..319e2d5 100644 --- a/ceresdb-protocol/src/main/java/com/ceresdb/models/Record.java +++ b/ceresdb-protocol/src/main/java/com/ceresdb/models/Record.java @@ -248,10 +248,10 @@ private static boolean isTimestamp(final LogicalType logicalType) { @Override public String toString() { return "[" + // - "name='" + name + '\'' + // - ", type='" + type + '\'' + // - ", subTypes='" + subTypes + '\'' + // - ']'; + "name='" + name + '\'' + // + ", type='" + type + '\'' + // + ", subTypes='" + subTypes + '\'' + // + ']'; } } @@ -279,9 +279,9 @@ public LogicalType getLogicalType() { @Override public String toString() { return "FieldType{" + // - "type=" + type + // - ", logicalType=" + logicalType + // - '}'; + "type=" + type + // + ", logicalType=" + logicalType + // + '}'; } } @@ -289,22 +289,10 @@ public String toString() { * Data types, represents any valid schema. */ enum Type { - Null, - Double, - Float, - Bytes, - String, - Long, - Int, - Boolean, - Union, - Unknown + Null, Double, Float, Bytes, String, Long, Int, Boolean, Union, Unknown } enum LogicalType { - TimestampMillis, - TimestampMicros, - Null, - Unknown + TimestampMillis, TimestampMicros, Null, Unknown } } diff --git a/ceresdb-protocol/src/main/java/com/ceresdb/models/Result.java b/ceresdb-protocol/src/main/java/com/ceresdb/models/Result.java index e3eaa82..47d5890 100644 --- a/ceresdb-protocol/src/main/java/com/ceresdb/models/Result.java +++ b/ceresdb-protocol/src/main/java/com/ceresdb/models/Result.java @@ -32,8 +32,8 @@ public final class Result { public static final int SHOULD_RETRY = 310; public static final int FLOW_CONTROL = 503; - private final Ok ok; - private final Err err; + private final Ok ok; + private final Err err; public static Result ok(final Ok ok) { Requires.requireNonNull(ok, "Null.ok"); diff --git a/ceresdb-protocol/src/main/java/com/ceresdb/models/Rows.java b/ceresdb-protocol/src/main/java/com/ceresdb/models/Rows.java index a7105de..27ac93d 100644 --- a/ceresdb-protocol/src/main/java/com/ceresdb/models/Rows.java +++ b/ceresdb-protocol/src/main/java/com/ceresdb/models/Rows.java @@ -35,7 +35,7 @@ */ public class Rows { - private Series series; + private Series series; /** * Map> diff --git a/ceresdb-protocol/src/main/java/com/ceresdb/models/SqlResult.java b/ceresdb-protocol/src/main/java/com/ceresdb/models/SqlResult.java index eb8681c..5be02d9 100644 --- a/ceresdb-protocol/src/main/java/com/ceresdb/models/SqlResult.java +++ b/ceresdb-protocol/src/main/java/com/ceresdb/models/SqlResult.java @@ -25,7 +25,7 @@ */ public class SqlResult { - public static final SqlResult EMPTY_RESULT = new SqlResult(-1, null); + public static final SqlResult EMPTY_RESULT = new SqlResult(-1, null); private final long affectedRows; private final List> rows; diff --git a/ceresdb-protocol/src/main/java/com/ceresdb/models/WriteOk.java b/ceresdb-protocol/src/main/java/com/ceresdb/models/WriteOk.java index 8302d06..ffbce15 100644 --- a/ceresdb-protocol/src/main/java/com/ceresdb/models/WriteOk.java +++ b/ceresdb-protocol/src/main/java/com/ceresdb/models/WriteOk.java @@ -34,8 +34,8 @@ public static boolean isCollectWroteDetail() { return COLLECT_WROTE_DETAIL; } - private int success; - private int failed; + private int success; + private int failed; /** * Empty if {@link #COLLECT_WROTE_DETAIL == false}. diff --git a/ceresdb-protocol/src/main/java/com/ceresdb/options/CeresDBxOptions.java b/ceresdb-protocol/src/main/java/com/ceresdb/options/CeresDBxOptions.java index 4eaf4da..ad162c4 100644 --- a/ceresdb-protocol/src/main/java/com/ceresdb/options/CeresDBxOptions.java +++ b/ceresdb-protocol/src/main/java/com/ceresdb/options/CeresDBxOptions.java @@ -197,45 +197,45 @@ public static Builder newBuilder(final String clusterHost, final int clusterPort */ public static Builder newBuilder(final String clusterHost, final int clusterPort, final int managementPort) { return new Builder(Endpoint.of(clusterHost, clusterPort)) // - .managementAddress(Endpoint.of(clusterHost, managementPort)); + .managementAddress(Endpoint.of(clusterHost, managementPort)); } public static final class Builder { // The only constant address of this cluster. private final Endpoint clusterAddress; // Database management address, such as creating tables. - private Endpoint managementAddress; + private Endpoint managementAddress; // Asynchronous thread pool, which is used to handle various asynchronous tasks in the SDK. - private Executor asyncWritePool; - private Executor asyncReadPool; + private Executor asyncWritePool; + private Executor asyncReadPool; // Tenant - private Tenant tenant; + private Tenant tenant; // Rpc options, in general, the default configuration is fine. - private RpcOptions rpcOptions = RpcOptions.newDefault(); + private RpcOptions rpcOptions = RpcOptions.newDefault(); // Write options // Maximum data entry per write - private int maxWriteSize = 512; + private int maxWriteSize = 512; // In the case of routing table failure or some other retry able error, a retry of the write is attempted. - private int writeMaxRetries = 1; + private int writeMaxRetries = 1; // Write flow control: maximum number of data rows in-flight. - private int maxInFlightWriteRows = 8192; + private int maxInFlightWriteRows = 8192; // Write flow control: limited policy - private LimitedPolicy writeLimitedPolicy = LimitedPolicy.defaultWriteLimitedPolicy(); + private LimitedPolicy writeLimitedPolicy = LimitedPolicy.defaultWriteLimitedPolicy(); // Query options // In the case of routing table failure, a retry of the read is attempted. - private int readMaxRetries = 1; + private int readMaxRetries = 1; // Query flow control: maximum number of query requests in-flight. - private int maxInFlightQueryRequests = 8; + private int maxInFlightQueryRequests = 8; // Query flow control: limited policy - private LimitedPolicy queryLimitedPolicy = LimitedPolicy.defaultQueryLimitedPolicy(); + private LimitedPolicy queryLimitedPolicy = LimitedPolicy.defaultQueryLimitedPolicy(); // Specifies the maximum number of routing table caches. When the number reaches the limit, the ones that // have not been used for a long time are cleared first - private int routeTableMaxCachedSize = 10_000; + private int routeTableMaxCachedSize = 10_000; // The frequency at which the route tables garbage collector is triggered. The default is 60 seconds. - private long routeTableGcPeriodSeconds = 60; + private long routeTableGcPeriodSeconds = 60; // Refresh frequency of route tables. The background refreshes all route tables periodically. By default, // all route tables are refreshed every 30 seconds. - private long routeTableRefreshPeriodSeconds = 30; + private long routeTableRefreshPeriodSeconds = 30; public Builder(Endpoint clusterAddress) { this.clusterAddress = clusterAddress; diff --git a/ceresdb-protocol/src/main/java/com/ceresdb/options/QueryOptions.java b/ceresdb-protocol/src/main/java/com/ceresdb/options/QueryOptions.java index 46c9b22..cf4edf6 100644 --- a/ceresdb-protocol/src/main/java/com/ceresdb/options/QueryOptions.java +++ b/ceresdb-protocol/src/main/java/com/ceresdb/options/QueryOptions.java @@ -29,11 +29,11 @@ */ public class QueryOptions implements Copiable { - private RouterClient routerClient; - private Executor asyncPool; + private RouterClient routerClient; + private Executor asyncPool; // In the case of routing table failure, a retry of the read is attempted. - private int maxRetries = 1; + private int maxRetries = 1; // Query flow limit: maximum number of query requests in-flight. private int maxInFlightQueryRequests = 8; private LimitedPolicy limitedPolicy = LimitedPolicy.defaultQueryLimitedPolicy(); diff --git a/ceresdb-protocol/src/main/java/com/ceresdb/options/RouterOptions.java b/ceresdb-protocol/src/main/java/com/ceresdb/options/RouterOptions.java index 042e2a8..f30fbed 100644 --- a/ceresdb-protocol/src/main/java/com/ceresdb/options/RouterOptions.java +++ b/ceresdb-protocol/src/main/java/com/ceresdb/options/RouterOptions.java @@ -31,12 +31,12 @@ public class RouterOptions implements Copiable { private Endpoint clusterAddress; // Specifies the maximum number of routing table caches. When the number reaches the limit, the ones that // have not been used for a long time are cleared first - private int maxCachedSize = 10_000; + private int maxCachedSize = 10_000; // The frequency at which the route tables garbage collector is triggered. The default is 60 seconds - private long gcPeriodSeconds = 60; + private long gcPeriodSeconds = 60; // Refresh frequency of route tables. The background refreshes all route tables periodically. By default, // all route tables are refreshed every 30 seconds. - private long refreshPeriodSeconds = 30; + private long refreshPeriodSeconds = 30; public RpcClient getRpcClient() { return rpcClient; diff --git a/ceresdb-protocol/src/main/java/com/ceresdb/options/WriteOptions.java b/ceresdb-protocol/src/main/java/com/ceresdb/options/WriteOptions.java index a75e14d..d1ffaa7 100644 --- a/ceresdb-protocol/src/main/java/com/ceresdb/options/WriteOptions.java +++ b/ceresdb-protocol/src/main/java/com/ceresdb/options/WriteOptions.java @@ -29,13 +29,13 @@ */ public class WriteOptions implements Copiable { - private RouterClient routerClient; - private Executor asyncPool; + private RouterClient routerClient; + private Executor asyncPool; // Maximum data entry per write - private int maxRetries = 1; + private int maxRetries = 1; // In the case of routing table failure or some other retry able error, a retry of the write is attempted. - private int maxWriteSize = 512; + private int maxWriteSize = 512; // Write flow limit: maximum number of data rows in-flight. private int maxInFlightWriteRows = 8192; private LimitedPolicy limitedPolicy = LimitedPolicy.defaultWriteLimitedPolicy(); diff --git a/ceresdb-protocol/src/main/java/com/ceresdb/signal/DisplaySignalHandler.java b/ceresdb-protocol/src/main/java/com/ceresdb/signal/DisplaySignalHandler.java index 899782e..667b948 100644 --- a/ceresdb-protocol/src/main/java/com/ceresdb/signal/DisplaySignalHandler.java +++ b/ceresdb-protocol/src/main/java/com/ceresdb/signal/DisplaySignalHandler.java @@ -43,7 +43,7 @@ @SPI(priority = 98) public class DisplaySignalHandler implements SignalHandler { - private static final Logger LOG = LoggerFactory.getLogger(DisplaySignalHandler.class); + private static final Logger LOG = LoggerFactory.getLogger(DisplaySignalHandler.class); private static final String BASE_NAME = "CeresDB_client_display.log"; @@ -62,10 +62,10 @@ public void handle(final String signalName) { final File file = FileOutputHelper.getOutputFile(BASE_NAME); LOG.info("Displaying CeresDBx clients triggered by signal: {} to file: {}.", signalName, - file.getAbsoluteFile()); + file.getAbsoluteFile()); - try (PrintWriter out = new PrintWriter(new OutputStreamWriter(new FileOutputStream(file, true), - StandardCharsets.UTF_8))) { + try (PrintWriter out = new PrintWriter( + new OutputStreamWriter(new FileOutputStream(file, true), StandardCharsets.UTF_8))) { final Display.Printer printer = new Display.DefaultPrinter(out); for (final CeresDBxClient ins : instances) { ins.display(printer); diff --git a/ceresdb-protocol/src/main/java/com/ceresdb/signal/MetricsSignalHandler.java b/ceresdb-protocol/src/main/java/com/ceresdb/signal/MetricsSignalHandler.java index f69e395..0261263 100644 --- a/ceresdb-protocol/src/main/java/com/ceresdb/signal/MetricsSignalHandler.java +++ b/ceresdb-protocol/src/main/java/com/ceresdb/signal/MetricsSignalHandler.java @@ -40,7 +40,7 @@ @SPI(priority = 97) public class MetricsSignalHandler implements SignalHandler { - private static final Logger LOG = LoggerFactory.getLogger(MetricsSignalHandler.class); + private static final Logger LOG = LoggerFactory.getLogger(MetricsSignalHandler.class); private static final String BASE_NAME = "CeresDB_client_metrics.log"; @@ -54,13 +54,13 @@ public void handle(final String signalName) { final File file = FileOutputHelper.getOutputFile(BASE_NAME); LOG.info("Printing CeresDB client metrics triggered by signal: {} to file: {}.", signalName, - file.getAbsoluteFile()); + file.getAbsoluteFile()); try (PrintStream out = new PrintStream(new FileOutputStream(file, true))) { final MetricReporter reporter = MetricReporter.forRegistry(MetricsUtil.metricRegistry()) // - .outputTo(out) // - .prefixedWith("-- CeresDB") // - .build(); + .outputTo(out) // + .prefixedWith("-- CeresDB") // + .build(); reporter.report(); out.flush(); } diff --git a/ceresdb-protocol/src/main/java/com/ceresdb/signal/ShowRouteCacheSignalHandler.java b/ceresdb-protocol/src/main/java/com/ceresdb/signal/ShowRouteCacheSignalHandler.java index 62eba5e..f5aac19 100644 --- a/ceresdb-protocol/src/main/java/com/ceresdb/signal/ShowRouteCacheSignalHandler.java +++ b/ceresdb-protocol/src/main/java/com/ceresdb/signal/ShowRouteCacheSignalHandler.java @@ -43,7 +43,7 @@ @SPI(priority = 96) public class ShowRouteCacheSignalHandler implements SignalHandler { - private static final Logger LOG = LoggerFactory.getLogger(ShowRouteCacheSignalHandler.class); + private static final Logger LOG = LoggerFactory.getLogger(ShowRouteCacheSignalHandler.class); private static final String BASE_NAME = "CeresDB_route_cache.log"; @@ -61,12 +61,11 @@ public void handle(final String signalName) { try { final File file = FileOutputHelper.getOutputFile(BASE_NAME); - LOG.info("Logging all of the route cache items triggered by signal: {} to file: {}.", - signalName, file.getAbsoluteFile()); + LOG.info("Logging all of the route cache items triggered by signal: {} to file: {}.", signalName, + file.getAbsoluteFile()); try (PrintWriter out = new PrintWriter( - new OutputStreamWriter(new FileOutputStream(file, true), - StandardCharsets.UTF_8))) { + new OutputStreamWriter(new FileOutputStream(file, true), StandardCharsets.UTF_8))) { final Display.Printer printer = new Display.DefaultPrinter(out); for (final CeresDBxClient ins : instances) { printer.print("clientId=").println(ins.id()); diff --git a/ceresdb-protocol/src/test/java/com/ceresdb/CeresDBxClientTest.java b/ceresdb-protocol/src/test/java/com/ceresdb/CeresDBxClientTest.java index ce7ab62..8700251 100644 --- a/ceresdb-protocol/src/test/java/com/ceresdb/CeresDBxClientTest.java +++ b/ceresdb-protocol/src/test/java/com/ceresdb/CeresDBxClientTest.java @@ -47,22 +47,20 @@ public class CeresDBxClientTest { private static final ReferenceFieldUpdater WC_UPDATER = Updaters // - .newReferenceFieldUpdater( - CeresDBxClient.class, - "writeClient"); + .newReferenceFieldUpdater(CeresDBxClient.class, "writeClient"); - private CeresDBxClient client; - private CeresDBxOptions opts; + private CeresDBxClient client; + private CeresDBxOptions opts; @Mock - private WriteClient writeClient; + private WriteClient writeClient; @Before public void before() { this.opts = CeresDBxOptions.newBuilder("127.0.0.1", 8081) // - .tenant("test", "sub_test", "test_token") // - .writeMaxRetries(1) // - .readMaxRetries(1) // - .build(); + .tenant("test", "sub_test", "test_token") // + .writeMaxRetries(1) // + .readMaxRetries(1) // + .build(); this.client = new CeresDBxClient(); } @@ -100,7 +98,7 @@ public void helloWorldTest() throws ExecutionException, InterruptedException { final Rows rows = TestUtil.newRow("test_metric1"); Mockito.when(this.writeClient.write(Mockito.anyList(), Mockito.any())) // - .thenReturn(Utils.completedCf(WriteOk.ok(2, 0, null).mapToResult())); + .thenReturn(Utils.completedCf(WriteOk.ok(2, 0, null).mapToResult())); final CompletableFuture> f = this.client.write(rows); final Result ret = f.get(); Assert.assertTrue(ret.isOk()); diff --git a/ceresdb-protocol/src/test/java/com/ceresdb/QueryClientTest.java b/ceresdb-protocol/src/test/java/com/ceresdb/QueryClientTest.java index a7bf50e..2aa8f1e 100644 --- a/ceresdb-protocol/src/test/java/com/ceresdb/QueryClientTest.java +++ b/ceresdb-protocol/src/test/java/com/ceresdb/QueryClientTest.java @@ -92,8 +92,7 @@ public void queryOkNoRouteTest() throws ExecutionException, InterruptedException final Common.ResponseHeader header = Common.ResponseHeader.newBuilder() // .setCode(Result.SUCCESS) // .build(); - final Storage.QueryResponse resp = Storage.QueryResponse.newBuilder() - .setHeader(header) // + final Storage.QueryResponse resp = Storage.QueryResponse.newBuilder().setHeader(header) // .addRows(ByteStringHelper.wrap(new byte[] { 1, 2, 3 })) // .build(); final Endpoint ep = Endpoint.of("127.0.0.1", 8081); @@ -105,8 +104,7 @@ public void queryOkNoRouteTest() throws ExecutionException, InterruptedException Mockito.when(this.routerClient.clusterRoute()) // .thenReturn(Route.of(ep)); - final QueryRequest req = QueryRequest.newBuilder() - .forMetrics("query_test_table") // + final QueryRequest req = QueryRequest.newBuilder().forMetrics("query_test_table") // .ql("select number from query_test_table") // .build(); final CompletableFuture> f = this.queryClient.query(req, Context.newDefault()); @@ -127,8 +125,7 @@ public void queryOkByValidRouteTest() throws ExecutionException, InterruptedExce final Common.ResponseHeader header = Common.ResponseHeader.newBuilder() // .setCode(Result.SUCCESS) // .build(); - final Storage.QueryResponse resp = Storage.QueryResponse.newBuilder() - .setHeader(header) // + final Storage.QueryResponse resp = Storage.QueryResponse.newBuilder().setHeader(header) // .addRows(ByteStringHelper.wrap(new byte[] { 1, 2, 3 })) // .build(); final Endpoint ep = Endpoint.of("127.0.0.1", 8081); @@ -144,8 +141,7 @@ public void queryOkByValidRouteTest() throws ExecutionException, InterruptedExce } })); - final QueryRequest req = QueryRequest.newBuilder() - .forMetrics("query_test_table") // + final QueryRequest req = QueryRequest.newBuilder().forMetrics("query_test_table") // .ql("select number from query_test_table") // .build(); final CompletableFuture> f = this.queryClient.query(req, Context.newDefault()); @@ -164,34 +160,34 @@ public void queryOkByValidRouteTest() throws ExecutionException, InterruptedExce @Test public void queryFailedTest() throws ExecutionException, InterruptedException { final Common.ResponseHeader header = Common.ResponseHeader.newBuilder() // - .setCode(500) // failed code - .build(); + .setCode(500) // failed code + .build(); final Storage.QueryResponse resp = Storage.QueryResponse.newBuilder().setHeader(header) // - .build(); + .build(); final Endpoint ep = Endpoint.of("127.0.0.1", 8081); Mockito.when(this.routerClient.invoke(Mockito.eq(ep), Mockito.any(), Mockito.any())) // - .thenReturn(Utils.completedCf(resp)); + .thenReturn(Utils.completedCf(resp)); Mockito.when(this.routerClient.routeFor(Mockito.eq(Collections.singletonList("query_test_table")))) // - .thenReturn(Utils.completedCf(new HashMap() { - private static final long serialVersionUID = 4517371149948738282L; + .thenReturn(Utils.completedCf(new HashMap() { + private static final long serialVersionUID = 4517371149948738282L; - { - put("query_test_table", Route.of("query_test_table", ep)); - } - })); - Mockito.when(this.routerClient.routeRefreshFor(Mockito.any())).thenReturn( - Utils.completedCf(new HashMap() { - private static final long serialVersionUID = 2347114952231996366L; + { + put("query_test_table", Route.of("query_test_table", ep)); + } + })); + Mockito.when(this.routerClient.routeRefreshFor(Mockito.any())) + .thenReturn(Utils.completedCf(new HashMap() { + private static final long serialVersionUID = 2347114952231996366L; - { - put("query_test_table", Route.of("query_test_table", ep)); - } - })); + { + put("query_test_table", Route.of("query_test_table", ep)); + } + })); final QueryRequest req = QueryRequest.newBuilder().forMetrics("query_test_table") // - .ql("select number from query_test_table") // - .build(); + .ql("select number from query_test_table") // + .build(); final CompletableFuture> f = this.queryClient.query(req, Context.newDefault()); final Result r = f.get(); @@ -269,15 +265,15 @@ private Result queryByAvro() throws IOException, ExecutionExceptio final Endpoint ep = Endpoint.of("127.0.0.1", 8081); Mockito.when(this.routerClient.invoke(Mockito.eq(ep), Mockito.any(), Mockito.any())) // - .thenReturn(Utils.completedCf(resp)); + .thenReturn(Utils.completedCf(resp)); Mockito.when(this.routerClient.routeFor(Mockito.any())) // - .thenReturn(Utils.completedCf(new HashMap<>())); + .thenReturn(Utils.completedCf(new HashMap<>())); Mockito.when(this.routerClient.clusterRoute()) // - .thenReturn(Route.of(ep)); + .thenReturn(Route.of(ep)); final QueryRequest req = QueryRequest.newBuilder().forMetrics("query_test_table") // - .ql("select number from query_test_table") // - .build(); + .ql("select number from query_test_table") // + .build(); final CompletableFuture> f = this.queryClient.query(req, Context.newDefault()); return f.get(); @@ -295,8 +291,8 @@ private QueryOk mockQueryOk() { private Storage.QueryResponse mockQueryResponse() throws IOException { final Common.ResponseHeader header = Common.ResponseHeader.newBuilder() // - .setCode(Result.SUCCESS) // - .build(); + .setCode(Result.SUCCESS) // + .build(); final String userSchema = "{\"type\":\"record\"," + "\"name\":\"my_record\"," + "\"fields\":[" // + "{\"name\":\"f1\",\"type\":\"string\"}," // + "{\"name\":\"f2\",\"type\":\"int\"}," // @@ -323,11 +319,11 @@ private Storage.QueryResponse mockQueryResponse() throws IOException { baos.flush(); return Storage.QueryResponse.newBuilder() // - .setHeader(header) // - .setSchemaType(Storage.QueryResponse.SchemaType.AVRO) // - .setSchemaContent(userSchema) // - .addRows(ByteStringHelper.wrap(baos.toByteArray())) // - .build(); + .setHeader(header) // + .setSchemaType(Storage.QueryResponse.SchemaType.AVRO) // + .setSchemaContent(userSchema) // + .addRows(ByteStringHelper.wrap(baos.toByteArray())) // + .build(); } @Test diff --git a/ceresdb-protocol/src/test/java/com/ceresdb/WriteClientTest.java b/ceresdb-protocol/src/test/java/com/ceresdb/WriteClientTest.java index be47a97..121803b 100644 --- a/ceresdb-protocol/src/test/java/com/ceresdb/WriteClientTest.java +++ b/ceresdb-protocol/src/test/java/com/ceresdb/WriteClientTest.java @@ -82,36 +82,36 @@ public void after() { @Test public void writeAllSuccessTest() throws ExecutionException, InterruptedException { final List data = TestUtil.newListOfRows("write_client_test_metric1", // - "write_client_test_metric2", // - "write_client_test_metric3"); + "write_client_test_metric2", // + "write_client_test_metric3"); final Endpoint ep1 = Endpoint.of("127.0.0.1", 8081); final Endpoint ep2 = Endpoint.of("127.0.0.2", 8081); final Endpoint ep3 = Endpoint.of("127.0.0.3", 8081); final Common.ResponseHeader header = Common.ResponseHeader.newBuilder() // - .setCode(Result.SUCCESS) // - .build(); + .setCode(Result.SUCCESS) // + .build(); final Storage.WriteResponse resp = Storage.WriteResponse.newBuilder() // - .setHeader(header) // - .setSuccess(2) // - .build(); + .setHeader(header) // + .setSuccess(2) // + .build(); Mockito.when(this.routerClient.invoke(Mockito.eq(ep1), Mockito.any(), Mockito.any())) // - .thenReturn(Utils.completedCf(resp)); + .thenReturn(Utils.completedCf(resp)); Mockito.when(this.routerClient.invoke(Mockito.eq(ep2), Mockito.any(), Mockito.any())) // - .thenReturn(Utils.completedCf(resp)); + .thenReturn(Utils.completedCf(resp)); Mockito.when(this.routerClient.invoke(Mockito.eq(ep3), Mockito.any(), Mockito.any())) // - .thenReturn(Utils.completedCf(resp)); + .thenReturn(Utils.completedCf(resp)); Mockito.when(this.routerClient.routeFor(Mockito.any())) // - .thenReturn(Utils.completedCf(new HashMap() { - private static final long serialVersionUID = 4694599978937545735L; + .thenReturn(Utils.completedCf(new HashMap() { + private static final long serialVersionUID = 4694599978937545735L; - { - put("write_client_test_metric1", Route.of("write_client_test_metric1", ep1)); - put("write_client_test_metric2", Route.of("write_client_test_metric2", ep2)); - put("write_client_test_metric3", Route.of("write_client_test_metric3", ep3)); - } - })); + { + put("write_client_test_metric1", Route.of("write_client_test_metric1", ep1)); + put("write_client_test_metric2", Route.of("write_client_test_metric2", ep2)); + put("write_client_test_metric3", Route.of("write_client_test_metric3", ep3)); + } + })); final CompletableFuture> f = this.writeClient.write(data, Context.newDefault()); final Result ret = f.get(); @@ -124,8 +124,8 @@ public void writeAllSuccessTest() throws ExecutionException, InterruptedExceptio @Test public void write3And1InvalidRoute() throws ExecutionException, InterruptedException { final List data = TestUtil.newListOfRows("write_client_test_metric1", // - "write_client_test_metric2", // - "write_client_test_metric3"); + "write_client_test_metric2", // + "write_client_test_metric3"); final Endpoint ep1 = Endpoint.of("127.0.0.1", 8081); final Endpoint ep2 = Endpoint.of("127.0.0.2", 8081); @@ -136,41 +136,40 @@ public void write3And1InvalidRoute() throws ExecutionException, InterruptedExcep final Storage.WriteResponse errResp = TestUtil.newFailedWriteResp(Result.INVALID_ROUTE, 2); Mockito.when(this.routerClient.invoke(Mockito.eq(ep1), Mockito.any(), Mockito.any())) // - .thenReturn(Utils.completedCf(resp)); + .thenReturn(Utils.completedCf(resp)); Mockito.when(this.routerClient.invoke(Mockito.eq(ep2), Mockito.any(), Mockito.any())) // - .thenReturn(Utils.completedCf(resp)); + .thenReturn(Utils.completedCf(resp)); Mockito.when(this.routerClient.invoke(Mockito.eq(ep3), Mockito.any(), Mockito.any())) // - .thenReturn(Utils.completedCf(errResp)); + .thenReturn(Utils.completedCf(errResp)); Mockito.when(this.routerClient.invoke(Mockito.eq(ep4), Mockito.any(), Mockito.any())) // - .thenReturn(Utils.completedCf(resp)); - Mockito.when( - this.routerClient.routeFor(Mockito.eq(TestUtil.asSet("write_client_test_metric1", // - "write_client_test_metric2", "write_client_test_metric3")))) // - .thenReturn(Utils.completedCf(new HashMap() { - private static final long serialVersionUID = -8646902388192715970L; - - { - put("write_client_test_metric1", Route.of("write_client_test_metric1", ep1)); - put("write_client_test_metric2", Route.of("write_client_test_metric2", ep2)); - put("write_client_test_metric3", Route.of("write_client_test_metric3", ep3)); - } - })); - Mockito.when(this.routerClient.routeRefreshFor(Mockito.any())).thenReturn( - Utils.completedCf(new HashMap() { - private static final long serialVersionUID = -3271323053870289591L; - - { - put("write_client_test_metric3", Route.of("write_client_test_metric3", ep4)); - } - })); + .thenReturn(Utils.completedCf(resp)); + Mockito.when(this.routerClient.routeFor(Mockito.eq(TestUtil.asSet("write_client_test_metric1", // + "write_client_test_metric2", "write_client_test_metric3")))) // + .thenReturn(Utils.completedCf(new HashMap() { + private static final long serialVersionUID = -8646902388192715970L; + + { + put("write_client_test_metric1", Route.of("write_client_test_metric1", ep1)); + put("write_client_test_metric2", Route.of("write_client_test_metric2", ep2)); + put("write_client_test_metric3", Route.of("write_client_test_metric3", ep3)); + } + })); + Mockito.when(this.routerClient.routeRefreshFor(Mockito.any())) + .thenReturn(Utils.completedCf(new HashMap() { + private static final long serialVersionUID = -3271323053870289591L; + + { + put("write_client_test_metric3", Route.of("write_client_test_metric3", ep4)); + } + })); Mockito.when(this.routerClient.routeFor(Mockito.eq(TestUtil.asSet("write_client_test_metric3")))) // - .thenReturn(Utils.completedCf(new HashMap() { - private static final long serialVersionUID = 4340010451723257789L; + .thenReturn(Utils.completedCf(new HashMap() { + private static final long serialVersionUID = 4340010451723257789L; - { - put("write_client_test_metric3", Route.of("write_client_test_metric3", ep4)); - } - })); + { + put("write_client_test_metric3", Route.of("write_client_test_metric3", ep4)); + } + })); final CompletableFuture> f = this.writeClient.write(data, Context.newDefault()); final Result ret = f.get(); @@ -183,8 +182,8 @@ public void write3And1InvalidRoute() throws ExecutionException, InterruptedExcep @Test public void write3And1InvalidRouteAndRetryFailed() throws ExecutionException, InterruptedException { final List data = TestUtil.newListOfRows("write_client_test_metric1", // - "write_client_test_metric2", // - "write_client_test_metric3"); + "write_client_test_metric2", // + "write_client_test_metric3"); final Endpoint ep1 = Endpoint.of("127.0.0.1", 8081); final Endpoint ep2 = Endpoint.of("127.0.0.2", 8081); @@ -194,39 +193,38 @@ public void write3And1InvalidRouteAndRetryFailed() throws ExecutionException, In final Storage.WriteResponse errResp = TestUtil.newFailedWriteResp(Result.INVALID_ROUTE, 2); Mockito.when(this.routerClient.invoke(Mockito.eq(ep1), Mockito.any(), Mockito.any())) // - .thenReturn(Utils.completedCf(resp)); + .thenReturn(Utils.completedCf(resp)); Mockito.when(this.routerClient.invoke(Mockito.eq(ep2), Mockito.any(), Mockito.any())) // - .thenReturn(Utils.completedCf(resp)); + .thenReturn(Utils.completedCf(resp)); Mockito.when(this.routerClient.invoke(Mockito.eq(ep3), Mockito.any(), Mockito.any())) // - .thenReturn(Utils.completedCf(errResp)); - Mockito.when( - this.routerClient.routeFor(Mockito.eq(TestUtil.asSet("write_client_test_metric1", - "write_client_test_metric2", "write_client_test_metric3")))) // - .thenReturn(Utils.completedCf(new HashMap() { - private static final long serialVersionUID = -7535390185627686991L; - - { - put("write_client_test_metric1", Route.of("write_client_test_metric1", ep1)); - put("write_client_test_metric2", Route.of("write_client_test_metric2", ep2)); - put("write_client_test_metric3", Route.of("write_client_test_metric3", ep3)); - } - })); - Mockito.when(this.routerClient.routeRefreshFor(Mockito.any())).thenReturn( - Utils.completedCf(new HashMap() { - private static final long serialVersionUID = -3191375160670801662L; - - { - put("write_client_test_metric3", Route.of("write_client_test_metric3", ep3)); - } - })); + .thenReturn(Utils.completedCf(errResp)); + Mockito.when(this.routerClient.routeFor(Mockito.eq( + TestUtil.asSet("write_client_test_metric1", "write_client_test_metric2", "write_client_test_metric3")))) // + .thenReturn(Utils.completedCf(new HashMap() { + private static final long serialVersionUID = -7535390185627686991L; + + { + put("write_client_test_metric1", Route.of("write_client_test_metric1", ep1)); + put("write_client_test_metric2", Route.of("write_client_test_metric2", ep2)); + put("write_client_test_metric3", Route.of("write_client_test_metric3", ep3)); + } + })); + Mockito.when(this.routerClient.routeRefreshFor(Mockito.any())) + .thenReturn(Utils.completedCf(new HashMap() { + private static final long serialVersionUID = -3191375160670801662L; + + { + put("write_client_test_metric3", Route.of("write_client_test_metric3", ep3)); + } + })); Mockito.when(this.routerClient.routeFor(Mockito.eq(TestUtil.asSet("write_client_test_metric3")))) // - .thenReturn(Utils.completedCf(new HashMap() { - private static final long serialVersionUID = 1341458669202248824L; + .thenReturn(Utils.completedCf(new HashMap() { + private static final long serialVersionUID = 1341458669202248824L; - { - put("write_client_test_metric3", Route.of("write_client_test_metric3", ep3)); - } - })); + { + put("write_client_test_metric3", Route.of("write_client_test_metric3", ep3)); + } + })); final CompletableFuture> f = this.writeClient.write(data, Context.newDefault()); final Result ret = f.get(); @@ -242,8 +240,8 @@ public void write3And1InvalidRouteAndRetryFailed() throws ExecutionException, In @Test public void write3And2FailedAndRetryFailed() throws ExecutionException, InterruptedException { final List data = TestUtil.newListOfRows("write_client_test_metric1", // - "write_client_test_metric2", // - "write_client_test_metric3"); + "write_client_test_metric2", // + "write_client_test_metric3"); final Endpoint ep1 = Endpoint.of("127.0.0.1", 8081); final Endpoint ep2 = Endpoint.of("127.0.0.2", 8081); @@ -253,54 +251,51 @@ public void write3And2FailedAndRetryFailed() throws ExecutionException, Interrup final Storage.WriteResponse errResp = TestUtil.newFailedWriteResp(Result.SHOULD_RETRY, 2); Mockito.when(this.routerClient.invoke(Mockito.eq(ep1), Mockito.any(), Mockito.any())) // - .thenReturn(Utils.completedCf(resp)); + .thenReturn(Utils.completedCf(resp)); Mockito.when(this.routerClient.invoke(Mockito.eq(ep2), Mockito.any(), Mockito.any())) // - .thenReturn(Utils.completedCf(errResp)); + .thenReturn(Utils.completedCf(errResp)); Mockito.when(this.routerClient.invoke(Mockito.eq(ep3), Mockito.any(), Mockito.any())) // - .thenReturn(Utils.completedCf(errResp)); - Mockito.when( - this.routerClient.routeFor(Mockito.eq(TestUtil.asSet("write_client_test_metric1", - "write_client_test_metric2", "write_client_test_metric3")))) // - .thenReturn(Utils.completedCf(new HashMap() { - private static final long serialVersionUID = -5936788008084035345L; - - { - put("write_client_test_metric1", Route.of("write_client_test_metric1", ep1)); - put("write_client_test_metric2", Route.of("write_client_test_metric2", ep2)); - put("write_client_test_metric3", Route.of("write_client_test_metric3", ep3)); - } - })); - Mockito.when(this.routerClient.routeRefreshFor(Mockito.any())).thenReturn( - Utils.completedCf(new HashMap() { - private static final long serialVersionUID = -4748944007591733357L; - - { - put("write_client_test_metric2", Route.of("write_client_test_metric2", ep2)); - put("write_client_test_metric3", Route.of("write_client_test_metric3", ep3)); - } - })); - Mockito.when( - this.routerClient.routeFor(Mockito.eq(TestUtil.asSet("write_client_test_metric2", - "write_client_test_metric3")))) // - .thenReturn(Utils.completedCf(new HashMap() { - private static final long serialVersionUID = -1811964578845864624L; - - { - put("write_client_test_metric2", Route.of("write_client_test_metric2", ep2)); - put("write_client_test_metric3", Route.of("write_client_test_metric3", ep3)); - } - })); - Mockito.when( - this.routerClient.routeFor(Mockito.eq(TestUtil.asSet("write_client_test_metric3", - "write_client_test_metric2")))) // - .thenReturn(Utils.completedCf(new HashMap() { - private static final long serialVersionUID = 3940955382371644111L; - - { - put("write_client_test_metric2", Route.of("write_client_test_metric2", ep2)); - put("write_client_test_metric3", Route.of("write_client_test_metric3", ep3)); - } - })); + .thenReturn(Utils.completedCf(errResp)); + Mockito.when(this.routerClient.routeFor(Mockito.eq( + TestUtil.asSet("write_client_test_metric1", "write_client_test_metric2", "write_client_test_metric3")))) // + .thenReturn(Utils.completedCf(new HashMap() { + private static final long serialVersionUID = -5936788008084035345L; + + { + put("write_client_test_metric1", Route.of("write_client_test_metric1", ep1)); + put("write_client_test_metric2", Route.of("write_client_test_metric2", ep2)); + put("write_client_test_metric3", Route.of("write_client_test_metric3", ep3)); + } + })); + Mockito.when(this.routerClient.routeRefreshFor(Mockito.any())) + .thenReturn(Utils.completedCf(new HashMap() { + private static final long serialVersionUID = -4748944007591733357L; + + { + put("write_client_test_metric2", Route.of("write_client_test_metric2", ep2)); + put("write_client_test_metric3", Route.of("write_client_test_metric3", ep3)); + } + })); + Mockito.when(this.routerClient + .routeFor(Mockito.eq(TestUtil.asSet("write_client_test_metric2", "write_client_test_metric3")))) // + .thenReturn(Utils.completedCf(new HashMap() { + private static final long serialVersionUID = -1811964578845864624L; + + { + put("write_client_test_metric2", Route.of("write_client_test_metric2", ep2)); + put("write_client_test_metric3", Route.of("write_client_test_metric3", ep3)); + } + })); + Mockito.when(this.routerClient + .routeFor(Mockito.eq(TestUtil.asSet("write_client_test_metric3", "write_client_test_metric2")))) // + .thenReturn(Utils.completedCf(new HashMap() { + private static final long serialVersionUID = 3940955382371644111L; + + { + put("write_client_test_metric2", Route.of("write_client_test_metric2", ep2)); + put("write_client_test_metric3", Route.of("write_client_test_metric3", ep3)); + } + })); final CompletableFuture> f = this.writeClient.write(data, Context.newDefault()); final Result ret = f.get(); @@ -316,8 +311,8 @@ public void write3And2FailedAndRetryFailed() throws ExecutionException, Interrup @Test public void write3And2FailedAndSomeNoRetry() throws ExecutionException, InterruptedException { final List data = TestUtil.newListOfRows("write_client_test_metric1", // - "write_client_test_metric2", // - "write_client_test_metric3"); + "write_client_test_metric2", // + "write_client_test_metric3"); final Endpoint ep1 = Endpoint.of("127.0.0.1", 8081); final Endpoint ep2 = Endpoint.of("127.0.0.2", 8081); @@ -328,40 +323,39 @@ public void write3And2FailedAndSomeNoRetry() throws ExecutionException, Interrup final Storage.WriteResponse errResp2 = TestUtil.newFailedWriteResp(400, 2); Mockito.when(this.routerClient.invoke(Mockito.eq(ep1), Mockito.any(), Mockito.any())) // - .thenReturn(Utils.completedCf(resp)); + .thenReturn(Utils.completedCf(resp)); Mockito.when(this.routerClient.invoke(Mockito.eq(ep2), Mockito.any(), Mockito.any())) // - .thenReturn(Utils.completedCf(errResp1)); + .thenReturn(Utils.completedCf(errResp1)); Mockito.when(this.routerClient.invoke(Mockito.eq(ep3), Mockito.any(), Mockito.any())) // - .thenReturn(Utils.completedCf(errResp2)); - Mockito.when( - this.routerClient.routeFor(Mockito.eq(TestUtil.asSet("write_client_test_metric1", - "write_client_test_metric2", "write_client_test_metric3")))) // - .thenReturn(Utils.completedCf(new HashMap() { - private static final long serialVersionUID = 1040769477529210661L; - - { - put("write_client_test_metric1", Route.of("write_client_test_metric1", ep1)); - put("write_client_test_metric2", Route.of("write_client_test_metric2", ep2)); - put("write_client_test_metric3", Route.of("write_client_test_metric3", ep3)); - } - })); - Mockito.when(this.routerClient.routeRefreshFor(Mockito.any())).thenReturn( - Utils.completedCf(new HashMap() { - private static final long serialVersionUID = -6892083230027668740L; - - { - put("write_client_test_metric2", Route.of("write_client_test_metric2", ep2)); - put("write_client_test_metric3", Route.of("write_client_test_metric3", ep3)); - } - })); + .thenReturn(Utils.completedCf(errResp2)); + Mockito.when(this.routerClient.routeFor(Mockito.eq( + TestUtil.asSet("write_client_test_metric1", "write_client_test_metric2", "write_client_test_metric3")))) // + .thenReturn(Utils.completedCf(new HashMap() { + private static final long serialVersionUID = 1040769477529210661L; + + { + put("write_client_test_metric1", Route.of("write_client_test_metric1", ep1)); + put("write_client_test_metric2", Route.of("write_client_test_metric2", ep2)); + put("write_client_test_metric3", Route.of("write_client_test_metric3", ep3)); + } + })); + Mockito.when(this.routerClient.routeRefreshFor(Mockito.any())) + .thenReturn(Utils.completedCf(new HashMap() { + private static final long serialVersionUID = -6892083230027668740L; + + { + put("write_client_test_metric2", Route.of("write_client_test_metric2", ep2)); + put("write_client_test_metric3", Route.of("write_client_test_metric3", ep3)); + } + })); Mockito.when(this.routerClient.routeFor(Mockito.eq(TestUtil.asSet("write_client_test_metric2")))) // - .thenReturn(Utils.completedCf(new HashMap() { - private static final long serialVersionUID = -9174308983134252825L; + .thenReturn(Utils.completedCf(new HashMap() { + private static final long serialVersionUID = -9174308983134252825L; - { - put("write_client_test_metric2", Route.of("write_client_test_metric2", ep2)); - } - })); + { + put("write_client_test_metric2", Route.of("write_client_test_metric2", ep2)); + } + })); final CompletableFuture> f = this.writeClient.write(data, Context.newDefault()); final Result ret = f.get(); @@ -384,7 +378,8 @@ public void writeSplitTest() throws ExecutionException, InterruptedException { writeSplit(8, 8); } - private void writeSplit(final int maxWriteSize, final int partOfSuccess) throws ExecutionException, InterruptedException { + private void writeSplit(final int maxWriteSize, final int partOfSuccess) + throws ExecutionException, InterruptedException { // re-init this.writeClient.shutdownGracefully(); final WriteOptions writeOpts = new WriteOptions(); @@ -443,51 +438,48 @@ public void streamWriteTest() { final String testMetric = "stream_metric_test"; final Endpoint ep = Endpoint.of("127.0.0.1", 8081); Mockito.when(this.routerClient.routeFor(Mockito.eq(Collections.singleton(testMetric)))) // - .thenReturn(Utils.completedCf(new HashMap() { + .thenReturn(Utils.completedCf(new HashMap() { - private static final long serialVersionUID = 8473563130528272901L; + private static final long serialVersionUID = 8473563130528272901L; - { - put(testMetric, Route.of(testMetric, ep)); - } - })); + { + put(testMetric, Route.of(testMetric, ep)); + } + })); final CompletableFuture f = new CompletableFuture<>(); final AtomicInteger dataCount = new AtomicInteger(); - Mockito.when( - this.routerClient.invokeClientStreaming(Mockito.any(), Mockito.any(Storage.WriteRequest.class), - Mockito.any(), Mockito.any())).thenReturn(new Observer() { - - @Override - public void onNext(final Storage.WriteRequest value) { - final int c = value.getMetricsList() - .stream() // - .flatMap(wmc -> wmc.getEntriesList().stream()) // - .map(Storage.WriteEntry::getFieldGroupsCount) - .reduce(0, Integer::sum); - dataCount.addAndGet(c); - } + Mockito.when(this.routerClient.invokeClientStreaming(Mockito.any(), Mockito.any(Storage.WriteRequest.class), + Mockito.any(), Mockito.any())).thenReturn(new Observer() { + + @Override + public void onNext(final Storage.WriteRequest value) { + final int c = value.getMetricsList().stream() // + .flatMap(wmc -> wmc.getEntriesList().stream()) // + .map(Storage.WriteEntry::getFieldGroupsCount).reduce(0, Integer::sum); + dataCount.addAndGet(c); + } - @Override - public void onError(final Throwable err) { - // ignored - } + @Override + public void onError(final Throwable err) { + // ignored + } - @Override - public void onCompleted() { - f.complete(WriteOk.ok(dataCount.get(), 0, null)); - } - }); + @Override + public void onCompleted() { + f.complete(WriteOk.ok(dataCount.get(), 0, null)); + } + }); final StreamWriteBuf writer = this.writeClient.streamWrite(testMetric); final CompletableFuture ret = writer // - .write(TestUtil.newRow(testMetric)) // - .write(TestUtil.newRow(testMetric)) // - .write(TestUtil.newRow(testMetric)) // - .flush() // - .write(TestUtil.newRow(testMetric)) // - .flush() // - .writeAndFlush(TestUtil.newListOfRows(testMetric, testMetric)) // - .completed(); + .write(TestUtil.newRow(testMetric)) // + .write(TestUtil.newRow(testMetric)) // + .write(TestUtil.newRow(testMetric)) // + .flush() // + .write(TestUtil.newRow(testMetric)) // + .flush() // + .writeAndFlush(TestUtil.newListOfRows(testMetric, testMetric)) // + .completed(); f.whenComplete((r, e) -> { if (e != null) { @@ -550,35 +542,29 @@ public void rowsToWriteProtoTest() { Assert.assertNotNull(writeReq); Assert.assertEquals(2, writeReq.getMetricsCount()); final List metrics = writeReq.getMetricsList() // - .stream() // - .map(Storage.WriteMetric::getMetric) // - .collect(Collectors.toList()); + .stream() // + .map(Storage.WriteMetric::getMetric) // + .collect(Collectors.toList()); Assert.assertEquals(Arrays.asList("metric1", "metric2"), metrics); { - final Optional opl = writeReq.getMetricsList() - .stream() + final Optional opl = writeReq.getMetricsList().stream() .filter(wm -> wm.getMetric().equals("metric1")) // .findFirst(); // Assert.assertTrue(opl.isPresent()); final Storage.WriteMetric metric = opl.get(); - Assert.assertEquals(3 , metric.getTagNamesCount()); - Assert.assertEquals(3 , metric.getFieldNamesCount()); + Assert.assertEquals(3, metric.getTagNamesCount()); + Assert.assertEquals(3, metric.getFieldNamesCount()); Assert.assertEquals(2, metric.getEntriesCount()); { - final Optional opl2 = metric.getEntriesList() - .stream() - .filter(w -> { - final Set tagVSet = w.getTagsList() - .stream() - .map(tag -> tag.getValue().getStringValue()) - .collect(Collectors.toSet()); - - return tagVSet.contains("v1") && tagVSet.contains("v2") && tagVSet.contains("v3"); - }) - .findFirst(); + final Optional opl2 = metric.getEntriesList().stream().filter(w -> { + final Set tagVSet = w.getTagsList().stream().map(tag -> tag.getValue().getStringValue()) + .collect(Collectors.toSet()); + + return tagVSet.contains("v1") && tagVSet.contains("v2") && tagVSet.contains("v3"); + }).findFirst(); Assert.assertTrue(opl2.isPresent()); Assert.assertEquals(0, opl2.get().getTags(0).getNameIndex()); Assert.assertEquals(1, opl2.get().getTags(1).getNameIndex()); @@ -595,29 +581,23 @@ public void rowsToWriteProtoTest() { } { - final Optional opl = writeReq.getMetricsList() - .stream() + final Optional opl = writeReq.getMetricsList().stream() .filter(wm -> wm.getMetric().equals("metric2")) // .findFirst(); // Assert.assertTrue(opl.isPresent()); final Storage.WriteMetric metric = opl.get(); - Assert.assertEquals(3 , metric.getTagNamesCount()); - Assert.assertEquals(3 , metric.getFieldNamesCount()); + Assert.assertEquals(3, metric.getTagNamesCount()); + Assert.assertEquals(3, metric.getFieldNamesCount()); Assert.assertEquals(1, metric.getEntriesCount()); { - final Optional opl2 = metric.getEntriesList() - .stream() - .filter(w -> { - final Set tagVSet = w.getTagsList() - .stream() - .map(tag -> tag.getValue().getStringValue()) - .collect(Collectors.toSet()); - - return tagVSet.contains("v1") && tagVSet.contains("v2") && tagVSet.contains("v3"); - }) - .findFirst(); + final Optional opl2 = metric.getEntriesList().stream().filter(w -> { + final Set tagVSet = w.getTagsList().stream().map(tag -> tag.getValue().getStringValue()) + .collect(Collectors.toSet()); + + return tagVSet.contains("v1") && tagVSet.contains("v2") && tagVSet.contains("v3"); + }).findFirst(); Assert.assertTrue(opl2.isPresent()); Assert.assertEquals(0, opl2.get().getTags(0).getNameIndex()); Assert.assertEquals(1, opl2.get().getTags(1).getNameIndex()); diff --git a/ceresdb-protocol/src/test/java/com/ceresdb/WriteLimitTest.java b/ceresdb-protocol/src/test/java/com/ceresdb/WriteLimitTest.java index 8be0049..0cf1c00 100644 --- a/ceresdb-protocol/src/test/java/com/ceresdb/WriteLimitTest.java +++ b/ceresdb-protocol/src/test/java/com/ceresdb/WriteLimitTest.java @@ -60,7 +60,8 @@ public void discardWriteLimitTest() throws ExecutionException, InterruptedExcept Assert.assertFalse(ret.isOk()); Assert.assertEquals(Result.FLOW_CONTROL, ret.getErr().getCode()); - Assert.assertEquals("Write limited by client, acquirePermits=4, maxPermits=1, availablePermits=0.", ret.getErr().getError()); + Assert.assertEquals("Write limited by client, acquirePermits=4, maxPermits=1, availablePermits=0.", + ret.getErr().getError()); } @Test diff --git a/ceresdb-protocol/src/test/java/com/ceresdb/models/RowsTest.java b/ceresdb-protocol/src/test/java/com/ceresdb/models/RowsTest.java index 3ac2564..6b262f6 100644 --- a/ceresdb-protocol/src/test/java/com/ceresdb/models/RowsTest.java +++ b/ceresdb-protocol/src/test/java/com/ceresdb/models/RowsTest.java @@ -31,28 +31,28 @@ public class RowsTest { @Test(expected = IllegalArgumentException.class) public void keywordInTagsTest() { Series.newBuilder("test_metric") // - .tag("timestamp", "ts") // - .tag("tag2", "v") // - .toRowsBuilder() // - .field(Clock.defaultClock().getTick(), "test", FieldValue.withFloat(0.1f)).build(); + .tag("timestamp", "ts") // + .tag("tag2", "v") // + .toRowsBuilder() // + .field(Clock.defaultClock().getTick(), "test", FieldValue.withFloat(0.1f)).build(); } @Test(expected = IllegalArgumentException.class) public void keywordInFieldsTest() { Series.newBuilder("test_metric") // - .tag("tag1", "ts") // - .tag("tag2", "v") // - .toRowsBuilder() // - .field(Clock.defaultClock().getTick(), "tsid", FieldValue.withFloat(0.1f)) // - .build(); + .tag("tag1", "ts") // + .tag("tag2", "v") // + .toRowsBuilder() // + .field(Clock.defaultClock().getTick(), "tsid", FieldValue.withFloat(0.1f)) // + .build(); } @Test(expected = UnsupportedOperationException.class) public void unmodifiableTagsTest() { final Series series = Series.newBuilder("test_metric") // - .tag("tag1", "ts") // - .tag("tag2", "v") // - .build(); + .tag("tag1", "ts") // + .tag("tag2", "v") // + .build(); series.getTags().put("not_allowed", TagValue.withString("test")); } @@ -60,11 +60,11 @@ public void unmodifiableTagsTest() { @Test(expected = UnsupportedOperationException.class) public void unmodifiableFieldsTest() { final Rows rs = Series.newBuilder("test_metric") // - .tag("tag1", "ts") // - .tag("tag2", "v") // - .toRowsBuilder() // - .field(Clock.defaultClock().getTick(), "test", FieldValue.withFloat(0.1f)) // - .build(); + .tag("tag1", "ts") // + .tag("tag2", "v") // + .toRowsBuilder() // + .field(Clock.defaultClock().getTick(), "test", FieldValue.withFloat(0.1f)) // + .build(); rs.getFields().put(Clock.defaultClock().getTick(), new HashMap<>()); } diff --git a/ceresdb-protocol/src/test/java/com/ceresdb/util/TestUtil.java b/ceresdb-protocol/src/test/java/com/ceresdb/util/TestUtil.java index c45cbe6..5de3ffc 100644 --- a/ceresdb-protocol/src/test/java/com/ceresdb/util/TestUtil.java +++ b/ceresdb-protocol/src/test/java/com/ceresdb/util/TestUtil.java @@ -38,14 +38,14 @@ public class TestUtil { public static Rows newRow(final String metric) { final long time = Clock.defaultClock().getTick() - 1; return Series.newBuilder(metric) // - .tag("tag1", "tag_v1") // - .tag("tag2", "tag_v2") // - .toRowsBuilder() // - .field(time, "field1", FieldValue.withDouble(0.1)) // - .field(time, "field2", FieldValue.withString("string_value")) // - .field(time + 1, "field1", FieldValue.withDouble(0.2)) // - .field(time + 1, "field2", FieldValue.withString("string_value_2")) // - .build(); + .tag("tag1", "tag_v1") // + .tag("tag2", "tag_v2") // + .toRowsBuilder() // + .field(time, "field1", FieldValue.withDouble(0.1)) // + .field(time, "field2", FieldValue.withString("string_value")) // + .field(time + 1, "field1", FieldValue.withDouble(0.2)) // + .field(time + 1, "field2", FieldValue.withString("string_value_2")) // + .build(); } public static List newListOfRows(final String... metrics) { @@ -58,12 +58,12 @@ public static List newListOfRows(final String... metrics) { public static Storage.WriteResponse newSuccessWriteResp(final int success) { final Common.ResponseHeader header = Common.ResponseHeader.newBuilder() // - .setCode(Result.SUCCESS) // - .build(); + .setCode(Result.SUCCESS) // + .build(); return Storage.WriteResponse.newBuilder() // - .setHeader(header) // - .setSuccess(success) // - .build(); + .setHeader(header) // + .setSuccess(success) // + .build(); } public static Storage.WriteResponse newFailedWriteResp(final int errCode, final int failed) { @@ -72,13 +72,13 @@ public static Storage.WriteResponse newFailedWriteResp(final int errCode, final public static Storage.WriteResponse newFailedWriteResp(final int errCode, final String err, final int failed) { final Common.ResponseHeader errHeader = Common.ResponseHeader.newBuilder() // - .setCode(errCode) // - .setError(err) // - .build(); + .setCode(errCode) // + .setError(err) // + .build(); return Storage.WriteResponse.newBuilder() // - .setHeader(errHeader) // - .setFailed(failed) // - .build(); + .setHeader(errHeader) // + .setFailed(failed) // + .build(); } @SuppressWarnings("unchecked") diff --git a/ceresdb-rpc/src/main/java/com/ceresdb/rpc/RpcClient.java b/ceresdb-rpc/src/main/java/com/ceresdb/rpc/RpcClient.java index 2d4927d..388d545 100644 --- a/ceresdb-rpc/src/main/java/com/ceresdb/rpc/RpcClient.java +++ b/ceresdb-rpc/src/main/java/com/ceresdb/rpc/RpcClient.java @@ -98,7 +98,8 @@ interface ConnectionObserver { */ default Resp invokeSync(final Endpoint endpoint, // final Req request, // - final long timeoutMs) throws RemotingException { + final long timeoutMs) + throws RemotingException { return invokeSync(endpoint, request, null, timeoutMs); } @@ -116,7 +117,8 @@ default Resp invokeSync(final Endpoint endpoint, // Resp invokeSync(final Endpoint endpoint, // final Req request, // final Context ctx, // - final long timeoutMs) throws RemotingException; + final long timeoutMs) + throws RemotingException; /** * Executes a asynchronous call with a response {@link Observer}. @@ -131,7 +133,8 @@ Resp invokeSync(final Endpoint endpoint, // default void invokeAsync(final Endpoint endpoint, // final Req request, // final Observer observer, // - final long timeoutMs) throws RemotingException { + final long timeoutMs) + throws RemotingException { invokeAsync(endpoint, request, null, observer, timeoutMs); } @@ -150,7 +153,8 @@ void invokeAsync(final Endpoint endpoint, // final Req request, // final Context ctx, // final Observer observer, // - final long timeoutMs) throws RemotingException; + final long timeoutMs) + throws RemotingException; /** * Executes a server-streaming call with a response {@link Observer}. @@ -167,7 +171,8 @@ void invokeAsync(final Endpoint endpoint, // void invokeServerStreaming(final Endpoint endpoint, // final Req request, // final Context ctx, // - final Observer observer) throws RemotingException; + final Observer observer) + throws RemotingException; /** * Executes a client-streaming call with a request {@link Observer} @@ -181,8 +186,7 @@ void invokeServerStreaming(final Endpoint endpoint, // * @param response message type * @return request {@link Observer}. */ - Observer invokeClientStreaming(final Endpoint endpoint, - final Req defaultReqIns, - final Context ctx, - final Observer respObserver) throws RemotingException; + Observer invokeClientStreaming(final Endpoint endpoint, final Req defaultReqIns, final Context ctx, + final Observer respObserver) + throws RemotingException; } diff --git a/ceresdb-rpc/src/main/java/com/ceresdb/rpc/RpcFactoryProvider.java b/ceresdb-rpc/src/main/java/com/ceresdb/rpc/RpcFactoryProvider.java index 98bcc21..e039032 100644 --- a/ceresdb-rpc/src/main/java/com/ceresdb/rpc/RpcFactoryProvider.java +++ b/ceresdb-rpc/src/main/java/com/ceresdb/rpc/RpcFactoryProvider.java @@ -28,7 +28,7 @@ public class RpcFactoryProvider { * SPI RPC factory, default is GrpcFactory */ private static final RpcFactory RPC_FACTORY = ServiceLoader.load(RpcFactory.class) // - .first(); + .first(); /** * Get the {@link RpcFactory} impl, base on SPI. diff --git a/ceresdb-rpc/src/main/java/com/ceresdb/rpc/RpcOptions.java b/ceresdb-rpc/src/main/java/com/ceresdb/rpc/RpcOptions.java index 923a511..8b26e92 100644 --- a/ceresdb-rpc/src/main/java/com/ceresdb/rpc/RpcOptions.java +++ b/ceresdb-rpc/src/main/java/com/ceresdb/rpc/RpcOptions.java @@ -33,80 +33,80 @@ public class RpcOptions implements Copiable { * RPC request default timeout in milliseconds * Default: 10000(10s) */ - private int defaultRpcTimeout = 10000; + private int defaultRpcTimeout = 10000; /** * RPC process thread pool size * Default: 32 */ - private int rpcThreadPoolSize = 0; - private int rpcThreadPoolQueueSize = Cpus.cpus() << 1; + private int rpcThreadPoolSize = 0; + private int rpcThreadPoolQueueSize = Cpus.cpus() << 1; /** * Sets the maximum message size allowed to be received on a channel. */ - private int maxInboundMessageSize = 64 * 1024 * 1024; + private int maxInboundMessageSize = 64 * 1024 * 1024; - private int flowControlWindow = 64 * 1024 * 1024; + private int flowControlWindow = 64 * 1024 * 1024; /** * Set the duration without ongoing RPCs before going to idle mode. * In idle mode the channel shuts down all connections. */ - private long idleTimeoutSeconds = TimeUnit.MINUTES.toSeconds(5); + private long idleTimeoutSeconds = TimeUnit.MINUTES.toSeconds(5); // --- keep-alive options: default will disable keep-alive /** * Sets the time without read activity before sending a keep-alive ping. */ - private long keepAliveTimeSeconds = Long.MAX_VALUE; + private long keepAliveTimeSeconds = Long.MAX_VALUE; /** * Sets the time waiting for read activity after sending a keep-alive ping. * If the time expires without any read activity on the connection, the * connection is considered dead. */ - private long keepAliveTimeoutSeconds = 3; + private long keepAliveTimeoutSeconds = 3; /** * Sets whether keep-alive will be performed when there are no outstanding * RPC on a connection. */ - private boolean keepAliveWithoutCalls = false; + private boolean keepAliveWithoutCalls = false; // --- keep-alive options: default will disable keep-alive - private LimitKind limitKind = LimitKind.Gradient; + private LimitKind limitKind = LimitKind.Gradient; /** * Initial limit used by the limiter */ - private int initialLimit = 64; + private int initialLimit = 64; /** * Maximum allowable concurrency. Any estimated concurrency will be capped * at this value */ - private int maxLimit = 1024; + private int maxLimit = 1024; - private int longRttWindow = 100; + private int longRttWindow = 100; /** * Smoothing factor to limit how aggressively the estimated limit can shrink * when queuing has been detected. */ - private double smoothing = 0.2; + private double smoothing = 0.2; /** * When set to true new calls to the channel will block when the limit has * been reached instead of failing fast with an UNAVAILABLE status. */ - private boolean blockOnLimit = false; + private boolean blockOnLimit = false; - private boolean logOnLimitChange = true; + private boolean logOnLimitChange = true; - private Tenant tenant; + private Tenant tenant; public int getDefaultRpcTimeout() { return defaultRpcTimeout; diff --git a/ceresdb-rpc/src/main/java/com/ceresdb/rpc/errors/ConnectFailException.java b/ceresdb-rpc/src/main/java/com/ceresdb/rpc/errors/ConnectFailException.java index 5335355..e49e347 100644 --- a/ceresdb-rpc/src/main/java/com/ceresdb/rpc/errors/ConnectFailException.java +++ b/ceresdb-rpc/src/main/java/com/ceresdb/rpc/errors/ConnectFailException.java @@ -38,7 +38,8 @@ public ConnectFailException(Throwable cause) { super(cause); } - public ConnectFailException(String message, Throwable cause, boolean enableSuppression, boolean writableStackTrace) { + public ConnectFailException(String message, Throwable cause, boolean enableSuppression, + boolean writableStackTrace) { super(message, cause, enableSuppression, writableStackTrace); } } diff --git a/ceresdb-rpc/src/main/java/com/ceresdb/rpc/errors/InvokeTimeoutException.java b/ceresdb-rpc/src/main/java/com/ceresdb/rpc/errors/InvokeTimeoutException.java index cc532d1..d48cf27 100644 --- a/ceresdb-rpc/src/main/java/com/ceresdb/rpc/errors/InvokeTimeoutException.java +++ b/ceresdb-rpc/src/main/java/com/ceresdb/rpc/errors/InvokeTimeoutException.java @@ -38,7 +38,8 @@ public InvokeTimeoutException(Throwable cause) { super(cause); } - public InvokeTimeoutException(String message, Throwable cause, boolean enableSuppression, boolean writableStackTrace) { + public InvokeTimeoutException(String message, Throwable cause, boolean enableSuppression, + boolean writableStackTrace) { super(message, cause, enableSuppression, writableStackTrace); } } diff --git a/ceresdb-sql-javacc/src/main/java/com/ceresdb/sql/CeresDBxMetricParser.java b/ceresdb-sql-javacc/src/main/java/com/ceresdb/sql/CeresDBxMetricParser.java index 86aed21..1040d14 100644 --- a/ceresdb-sql-javacc/src/main/java/com/ceresdb/sql/CeresDBxMetricParser.java +++ b/ceresdb-sql-javacc/src/main/java/com/ceresdb/sql/CeresDBxMetricParser.java @@ -29,7 +29,7 @@ */ public class CeresDBxMetricParser implements MetricParser { - private final String ql; + private final String ql; private boolean parsed; private StatementType statementType = StatementType.Unknown; diff --git a/ceresdb-sql-javacc/src/main/java/com/ceresdb/sql/CeresDBxSqlStatement.java b/ceresdb-sql-javacc/src/main/java/com/ceresdb/sql/CeresDBxSqlStatement.java index 0645ff0..2f0bb61 100644 --- a/ceresdb-sql-javacc/src/main/java/com/ceresdb/sql/CeresDBxSqlStatement.java +++ b/ceresdb-sql-javacc/src/main/java/com/ceresdb/sql/CeresDBxSqlStatement.java @@ -31,17 +31,17 @@ public class CeresDBxSqlStatement { public static final List DEFAULT_PARAMETERS = Collections.emptyList(); public static final Map DEFAULT_POSITIONS = Collections.emptyMap(); - public static final String KEYWORD_EXISTS = "EXISTS"; - public static final String KEYWORD_REPLACE = "REPLACE"; - public static final String KEYWORD_TOTALS = "TOTALS"; - public static final String KEYWORD_VALUES = "VALUES"; - - private final String sql; - private final StatementType stmtType; - private final String database; - private final List tables; - private final List parameters; - private final Map positions; + public static final String KEYWORD_EXISTS = "EXISTS"; + public static final String KEYWORD_REPLACE = "REPLACE"; + public static final String KEYWORD_TOTALS = "TOTALS"; + public static final String KEYWORD_VALUES = "VALUES"; + + private final String sql; + private final StatementType stmtType; + private final String database; + private final List tables; + private final List parameters; + private final Map positions; public CeresDBxSqlStatement(String sql) { this(sql, StatementType.UNKNOWN, null, null, null, null); @@ -61,8 +61,8 @@ public CeresDBxSqlStatement(String sql, // this.stmtType = stmtType; this.database = database; - this.tables = tables == null || tables.isEmpty() ? DEFAULT_TABLES : Collections - .unmodifiableList(new ArrayList<>(tables)); + this.tables = tables == null || tables.isEmpty() ? DEFAULT_TABLES : + Collections.unmodifiableList(new ArrayList<>(tables)); if (parameters != null && parameters.size() > 0) { this.parameters = Collections.unmodifiableList(new ArrayList<>(parameters)); diff --git a/ceresdb-sql-javacc/src/test/java/com/ceresdb/sql/MetricParserTest.java b/ceresdb-sql-javacc/src/test/java/com/ceresdb/sql/MetricParserTest.java index 791a7f5..db187a7 100644 --- a/ceresdb-sql-javacc/src/test/java/com/ceresdb/sql/MetricParserTest.java +++ b/ceresdb-sql-javacc/src/test/java/com/ceresdb/sql/MetricParserTest.java @@ -125,8 +125,8 @@ public void createTableTest() { @Test public void createTableIfNotExistsTest() { - final MetricParser parser = getParser("CREATE TABLE IF NOT EXISTS with_primary_key(\n" - + " ts TIMESTAMP NOT NULL,\n" // + final MetricParser parser = getParser( + "CREATE TABLE IF NOT EXISTS with_primary_key(\n" + " ts TIMESTAMP NOT NULL,\n" // + " c1 STRING NOT NULL,\n" // + " c2 STRING NULL,\n" // + " c3 DOUBLE NULL,\n" // diff --git a/ceresdb-sql/src/main/java/com/ceresdb/sql/JSqlMetricParser.java b/ceresdb-sql/src/main/java/com/ceresdb/sql/JSqlMetricParser.java index 5186597..930e044 100644 --- a/ceresdb-sql/src/main/java/com/ceresdb/sql/JSqlMetricParser.java +++ b/ceresdb-sql/src/main/java/com/ceresdb/sql/JSqlMetricParser.java @@ -40,7 +40,7 @@ */ public class JSqlMetricParser implements MetricParser { - private final String ql; + private final String ql; private boolean parsed; private StatementType statementType = StatementType.Unknown; diff --git a/ceresdb-sql/src/main/java/com/ceresdb/sql/SqlParserUtil.java b/ceresdb-sql/src/main/java/com/ceresdb/sql/SqlParserUtil.java index 024359e..fcfae36 100644 --- a/ceresdb-sql/src/main/java/com/ceresdb/sql/SqlParserUtil.java +++ b/ceresdb-sql/src/main/java/com/ceresdb/sql/SqlParserUtil.java @@ -38,9 +38,9 @@ public class SqlParserUtil { private static final String _TIMESTAMP_KEY_UC = "TIMESTAMP KEY"; private static final String _TIMESTAMP_KEY_LC = _TIMESTAMP_KEY_UC.toLowerCase(); // This syntax is not supported in CeresDB - private static final String _UNIQUE_KEY = "UNIQUE KEY"; - private static final String _TAG = "TAG"; - private static final String _CREATE_TABLE = "CREATE TABLE"; + private static final String _UNIQUE_KEY = "UNIQUE KEY"; + private static final String _TAG = "TAG"; + private static final String _CREATE_TABLE = "CREATE TABLE"; public static List extractTableNames(final Statement stmt) { final TablesNamesFinder tablesFinder = new TablesNamesFinder(); @@ -55,7 +55,7 @@ public static String amendSql(final String sql) { Requires.requireTrue(!ucSql.contains(_UNIQUE_KEY), "`unique key` not supported"); // Case mixing is not supported return sql.replace(_TIMESTAMP_KEY_UC, _UNIQUE_KEY) // - .replace(_TIMESTAMP_KEY_LC, _UNIQUE_KEY); + .replace(_TIMESTAMP_KEY_LC, _UNIQUE_KEY); } return sql; @@ -66,51 +66,42 @@ public static List extractCreateColumns(final CreateTable c // timestamp final String tsColName = createTable.getIndexes() // must not null - .stream() - .filter(SqlParserUtil::isTimestampColumn) - .flatMap(idx -> idx.getColumnsNames().stream()) - .findFirst() - .orElseThrow(() -> new IllegalArgumentException("There must be a `timestamp` column")); - - final Set tags = createTable.getColumnDefinitions() - .stream() - .filter(SqlParserUtil::isTagColumn) - .map(ColumnDefinition::getColumnName) - .collect(Collectors.toSet()); - - return createTable.getColumnDefinitions() - .stream() - .map(col -> new MetricParser.Column() { - - @Override - public String metricName() { - return metricName; - } - - @Override - public String columnName() { - return col.getColumnName(); - } - - @Override - public ColumnType columnType() { - if (tsColName.equals(columnName())) { - return ColumnType.Timestamp; - } - - if (tags.contains(columnName())) { - return ColumnType.Tag; - } - - return ColumnType.Field; - } - - @Override - public String valueType() { - return col.getColDataType().getDataType(); - } - }) - .collect(Collectors.toList()); + .stream().filter(SqlParserUtil::isTimestampColumn).flatMap(idx -> idx.getColumnsNames().stream()) + .findFirst().orElseThrow(() -> new IllegalArgumentException("There must be a `timestamp` column")); + + final Set tags = createTable.getColumnDefinitions().stream().filter(SqlParserUtil::isTagColumn) + .map(ColumnDefinition::getColumnName).collect(Collectors.toSet()); + + return createTable.getColumnDefinitions().stream().map(col -> new MetricParser.Column() { + + @Override + public String metricName() { + return metricName; + } + + @Override + public String columnName() { + return col.getColumnName(); + } + + @Override + public ColumnType columnType() { + if (tsColName.equals(columnName())) { + return ColumnType.Timestamp; + } + + if (tags.contains(columnName())) { + return ColumnType.Tag; + } + + return ColumnType.Field; + } + + @Override + public String valueType() { + return col.getColDataType().getDataType(); + } + }).collect(Collectors.toList()); } private static boolean isTagColumn(final ColumnDefinition col) { diff --git a/ceresdb-sql/src/test/java/com/ceresdb/sql/JSqlMetricParserTest.java b/ceresdb-sql/src/test/java/com/ceresdb/sql/JSqlMetricParserTest.java index 5fef681..1e512c5 100644 --- a/ceresdb-sql/src/test/java/com/ceresdb/sql/JSqlMetricParserTest.java +++ b/ceresdb-sql/src/test/java/com/ceresdb/sql/JSqlMetricParserTest.java @@ -57,8 +57,8 @@ public void descTableTest() { @Test public void extractMetricNamesTest() { - final MetricParser parser = getParser("select func1(table1.a), func2(table2.b), * from table1 inner join " - + "table2 on table1.id=table2.id"); + final MetricParser parser = getParser( + "select func1(table1.a), func2(table2.b), * from table1 inner join " + "table2 on table1.id=table2.id"); final List metricNames = parser.metricNames(); Assert.assertEquals(Arrays.asList("table1", "table2"), metricNames); @@ -93,22 +93,17 @@ public void extractMetricNames4Test() { @Test public void extractFromCreateTableWithPrimaryKeyTest() { - final MetricParser parser = getParser("CREATE TABLE with_primary_key(\n" + - " ts TIMESTAMP NOT NULL,\n" + - " c1 STRING NOT NULL,\n" + - " c2 STRING NULL,\n" + - " c3 DOUBLE NULL,\n" + - " c4 STRING NULL,\n" + - " c5 STRING NULL,\n" + - " TIMESTAMP KEY(ts),\n" + - " PRIMARY KEY(c1, ts)\n" + - ") ENGINE=Analytic WITH (ttl='7d', update_mode='APPEND');"); + final MetricParser parser = getParser( + "CREATE TABLE with_primary_key(\n" + " ts TIMESTAMP NOT NULL,\n" + " c1 STRING NOT NULL,\n" + + " c2 STRING NULL,\n" + " c3 DOUBLE NULL,\n" + + " c4 STRING NULL,\n" + " c5 STRING NULL,\n" + + " TIMESTAMP KEY(ts),\n" + " PRIMARY KEY(c1, ts)\n" + + ") ENGINE=Analytic WITH (ttl='7d', update_mode='APPEND');"); final List metricNames = parser.metricNames(); Assert.assertEquals(Collections.singletonList("with_primary_key"), metricNames); - final List columns = parser.createColumns() - .stream() + final List columns = parser.createColumns().stream() .map(col -> col.columnType() + "-" + col.columnName() + "-" + col.valueType()) .collect(Collectors.toList()); Assert.assertEquals(Arrays.asList("Timestamp-ts-TIMESTAMP", "Field-c1-STRING", "Field-c2-STRING", @@ -117,24 +112,17 @@ public void extractFromCreateTableWithPrimaryKeyTest() { @Test public void extractFromCreateTableWithPrimaryKeyTagTest() { - final MetricParser parser = getParser("CREATE TABLE with_primary_key_tag(\n" + - " ts TIMESTAMP NOT NULL,\n" + - " c1 STRING TAG NOT NULL,\n" + - " c2 STRING TAG NULL,\n" + - " c3 STRING TAG NULL,\n" + - " c4 DOUBLE NULL,\n" + - " c5 STRING NULL,\n" + - " c6 STRING NULL,\n" + - " c7 TIMESTAMP NULL,\n" + - " TIMESTAMP KEY(ts),\n" + - " PRIMARY KEY(c1, ts)\n" + - ") ENGINE=Analytic;"); + final MetricParser parser = getParser("CREATE TABLE with_primary_key_tag(\n" + " ts TIMESTAMP NOT NULL,\n" + + " c1 STRING TAG NOT NULL,\n" + " c2 STRING TAG NULL,\n" + + " c3 STRING TAG NULL,\n" + " c4 DOUBLE NULL,\n" + + " c5 STRING NULL,\n" + " c6 STRING NULL,\n" + + " c7 TIMESTAMP NULL,\n" + " TIMESTAMP KEY(ts),\n" + + " PRIMARY KEY(c1, ts)\n" + ") ENGINE=Analytic;"); final List metricNames = parser.metricNames(); Assert.assertEquals(Collections.singletonList("with_primary_key_tag"), metricNames); - final List columns = parser.createColumns() - .stream() + final List columns = parser.createColumns().stream() .map(col -> col.columnType() + "-" + col.columnName() + "-" + col.valueType()) .collect(Collectors.toList()); Assert.assertEquals(Arrays.asList("Timestamp-ts-TIMESTAMP", "Tag-c1-STRING", "Tag-c2-STRING", "Tag-c3-STRING", @@ -143,22 +131,16 @@ public void extractFromCreateTableWithPrimaryKeyTagTest() { @Test public void extractFromCreateTableWithTagTest() { - final MetricParser parser = getParser("CREATE TABLE with_tag(\n" + - " ts TIMESTAMP NOT NULL,\n" + - " c1 STRING TAG NOT NULL,\n" + - " c2 STRING TAG NULL,\n" + - " c3 STRING TAG NULL,\n" + - " c4 DOUBLE NULL,\n" + - " c5 STRING NULL,\n" + - " c6 STRING NULL,\n" + - " TIMESTAMP KEY(ts)\n" + - ") ENGINE=Analytic;"); + final MetricParser parser = getParser("CREATE TABLE with_tag(\n" + " ts TIMESTAMP NOT NULL,\n" + + " c1 STRING TAG NOT NULL,\n" + " c2 STRING TAG NULL,\n" + + " c3 STRING TAG NULL,\n" + " c4 DOUBLE NULL,\n" + + " c5 STRING NULL,\n" + " c6 STRING NULL,\n" + + " TIMESTAMP KEY(ts)\n" + ") ENGINE=Analytic;"); final List metricNames = parser.metricNames(); Assert.assertEquals(Collections.singletonList("with_tag"), metricNames); - final List columns = parser.createColumns() - .stream() + final List columns = parser.createColumns().stream() .map(col -> col.columnType() + "-" + col.columnName() + "-" + col.valueType()) .collect(Collectors.toList()); Assert.assertEquals(Arrays.asList("Timestamp-ts-TIMESTAMP", "Tag-c1-STRING", "Tag-c2-STRING", "Tag-c3-STRING", diff --git a/pom.xml b/pom.xml index 912c8c4..522130b 100644 --- a/pom.xml +++ b/pom.xml @@ -305,7 +305,6 @@ net.revelc.code.formatter formatter-maven-plugin 2.15.0 - ${main.user.dir}/tools/codestyle/formatter.xml ${project.encoding} From 6dd02f1b208c984fc9fcf4e61a29bd3b4bd60cef Mon Sep 17 00:00:00 2001 From: Arthur Chern Date: Mon, 25 Jul 2022 17:12:58 +0800 Subject: [PATCH 2/3] chore: re-format using the new format settings --- .../ceresdb/common/util/ServiceLoader.java | 13 +++--- .../common/util/internal/UnsafeUtil.java | 42 +++++++++---------- .../com/google/protobuf/BytesStealer.java | 2 +- .../com/ceresdb/rpc/limit/Gradient2Limit.java | 2 +- .../com/ceresdb/rpc/limit/VegasLimit.java | 2 +- .../main/java/com/ceresdb/MetricParser.java | 4 +- .../main/java/com/ceresdb/RouterClient.java | 3 +- .../java/com/ceresdb/RpcServiceRegister.java | 3 +- .../java/com/ceresdb/sql/ParseHandler.java | 6 +-- pom.xml | 2 - 10 files changed, 40 insertions(+), 39 deletions(-) diff --git a/ceresdb-common/src/main/java/com/ceresdb/common/util/ServiceLoader.java b/ceresdb-common/src/main/java/com/ceresdb/common/util/ServiceLoader.java index c379a9c..f393ae1 100644 --- a/ceresdb-common/src/main/java/com/ceresdb/common/util/ServiceLoader.java +++ b/ceresdb-common/src/main/java/com/ceresdb/common/util/ServiceLoader.java @@ -45,21 +45,21 @@ @SuppressWarnings("PMD") public final class ServiceLoader implements Iterable { - private static final Logger LOG = LoggerFactory.getLogger(ServiceLoader.class); + private static final Logger LOG = LoggerFactory.getLogger(ServiceLoader.class); - private static final String PREFIX = "META-INF/services/"; + private static final String PREFIX = "META-INF/services/"; // the class or interface representing the service being loaded - private final Class service; + private final Class service; // the class loader used to locate, load, and instantiate providers - private final ClassLoader loader; + private final ClassLoader loader; // cached providers, in instantiation order private final LinkedHashMap providers = new LinkedHashMap<>(); // the current lazy-lookup iterator - private LazyIterator lookupIterator; + private LazyIterator lookupIterator; public static ServiceLoader load(final Class service) { return ServiceLoader.load(service, Thread.currentThread().getContextClassLoader()); @@ -179,7 +179,8 @@ private static ServiceConfigurationError fail(final Class service, final URL // parse a single line from the given configuration file, adding the name // on the line to the names list. private int parseLine(final Class service, final URL u, final BufferedReader r, final int lc, - final List names) throws IOException, ServiceConfigurationError { + final List names) + throws IOException, ServiceConfigurationError { String ln = r.readLine(); if (ln == null) { diff --git a/ceresdb-common/src/main/java/com/ceresdb/common/util/internal/UnsafeUtil.java b/ceresdb-common/src/main/java/com/ceresdb/common/util/internal/UnsafeUtil.java index bee4c65..b6273d4 100644 --- a/ceresdb-common/src/main/java/com/ceresdb/common/util/internal/UnsafeUtil.java +++ b/ceresdb-common/src/main/java/com/ceresdb/common/util/internal/UnsafeUtil.java @@ -33,37 +33,37 @@ @SuppressWarnings({ "ConstantConditions", "PMD" }) public final class UnsafeUtil { - private static final Logger LOG = LoggerFactory.getLogger(UnsafeUtil.class); + private static final Logger LOG = LoggerFactory.getLogger(UnsafeUtil.class); - private static final Object UNSAFE = getUnsafe0(); + private static final Object UNSAFE = getUnsafe0(); - private static final UnsafeAccessor UNSAFE_ACCESSOR = getUnsafeAccessor0(); + private static final UnsafeAccessor UNSAFE_ACCESSOR = getUnsafeAccessor0(); - private static final long BYTE_ARRAY_BASE_OFFSET = arrayBaseOffset(byte[].class); + private static final long BYTE_ARRAY_BASE_OFFSET = arrayBaseOffset(byte[].class); // Micro-optimization: we can assume a scale of 1 and skip the multiply // private static final long BYTE_ARRAY_INDEX_SCALE = 1; - private static final long BOOLEAN_ARRAY_BASE_OFFSET = arrayBaseOffset(boolean[].class); - private static final long BOOLEAN_ARRAY_INDEX_SCALE = arrayIndexScale(boolean[].class); + private static final long BOOLEAN_ARRAY_BASE_OFFSET = arrayBaseOffset(boolean[].class); + private static final long BOOLEAN_ARRAY_INDEX_SCALE = arrayIndexScale(boolean[].class); - private static final long INT_ARRAY_BASE_OFFSET = arrayBaseOffset(int[].class); - private static final long INT_ARRAY_INDEX_SCALE = arrayIndexScale(int[].class); + private static final long INT_ARRAY_BASE_OFFSET = arrayBaseOffset(int[].class); + private static final long INT_ARRAY_INDEX_SCALE = arrayIndexScale(int[].class); - private static final long LONG_ARRAY_BASE_OFFSET = arrayBaseOffset(long[].class); - private static final long LONG_ARRAY_INDEX_SCALE = arrayIndexScale(long[].class); + private static final long LONG_ARRAY_BASE_OFFSET = arrayBaseOffset(long[].class); + private static final long LONG_ARRAY_INDEX_SCALE = arrayIndexScale(long[].class); - private static final long FLOAT_ARRAY_BASE_OFFSET = arrayBaseOffset(float[].class); - private static final long FLOAT_ARRAY_INDEX_SCALE = arrayIndexScale(float[].class); + private static final long FLOAT_ARRAY_BASE_OFFSET = arrayBaseOffset(float[].class); + private static final long FLOAT_ARRAY_INDEX_SCALE = arrayIndexScale(float[].class); - private static final long DOUBLE_ARRAY_BASE_OFFSET = arrayBaseOffset(double[].class); - private static final long DOUBLE_ARRAY_INDEX_SCALE = arrayIndexScale(double[].class); + private static final long DOUBLE_ARRAY_BASE_OFFSET = arrayBaseOffset(double[].class); + private static final long DOUBLE_ARRAY_INDEX_SCALE = arrayIndexScale(double[].class); - private static final long OBJECT_ARRAY_BASE_OFFSET = arrayBaseOffset(Object[].class); - private static final long OBJECT_ARRAY_INDEX_SCALE = arrayIndexScale(Object[].class); + private static final long OBJECT_ARRAY_BASE_OFFSET = arrayBaseOffset(Object[].class); + private static final long OBJECT_ARRAY_INDEX_SCALE = arrayIndexScale(Object[].class); - private static final long BUFFER_ADDRESS_OFFSET = objectFieldOffset(bufferAddressField()); + private static final long BUFFER_ADDRESS_OFFSET = objectFieldOffset(bufferAddressField()); - private static final long STRING_VALUE_OFFSET = objectFieldOffset(stringValueField()); + private static final long STRING_VALUE_OFFSET = objectFieldOffset(stringValueField()); /** * Whether or not can use the unsafe api. @@ -240,13 +240,13 @@ public static void putLongVolatile(final long[] target, final long index, final } public static boolean getBooleanVolatile(final boolean[] target, final long index) { - return UNSAFE_ACCESSOR.getBooleanVolatile(target, BOOLEAN_ARRAY_BASE_OFFSET - + (index * BOOLEAN_ARRAY_INDEX_SCALE)); + return UNSAFE_ACCESSOR.getBooleanVolatile(target, + BOOLEAN_ARRAY_BASE_OFFSET + (index * BOOLEAN_ARRAY_INDEX_SCALE)); } public static void putBooleanVolatile(final boolean[] target, final long index, final boolean value) { UNSAFE_ACCESSOR.putBooleanVolatile(target, BOOLEAN_ARRAY_BASE_OFFSET + (index * BOOLEAN_ARRAY_INDEX_SCALE), - value); + value); } public static float getFloatVolatile(final float[] target, final long index) { diff --git a/ceresdb-common/src/main/java/com/google/protobuf/BytesStealer.java b/ceresdb-common/src/main/java/com/google/protobuf/BytesStealer.java index 7e4bbd8..01deb74 100644 --- a/ceresdb-common/src/main/java/com/google/protobuf/BytesStealer.java +++ b/ceresdb-common/src/main/java/com/google/protobuf/BytesStealer.java @@ -21,7 +21,7 @@ /** * Try to gets the bytes form ByteString with no copy. - * + * * @author jiachun.fjc */ public class BytesStealer extends ByteOutput { diff --git a/ceresdb-grpc/src/main/java/com/ceresdb/rpc/limit/Gradient2Limit.java b/ceresdb-grpc/src/main/java/com/ceresdb/rpc/limit/Gradient2Limit.java index 7529383..0ebc934 100644 --- a/ceresdb-grpc/src/main/java/com/ceresdb/rpc/limit/Gradient2Limit.java +++ b/ceresdb-grpc/src/main/java/com/ceresdb/rpc/limit/Gradient2Limit.java @@ -68,7 +68,7 @@ * In this state the system goes back to steady state after a prolonged period of excessive load. Requests aren't rejected * and the sample RTT remains low. During this state the long term RTT may take some time to go back to normal and could * potentially be several multiples higher than the current RTT. - * + * * Refer to {@link com.netflix.concurrency.limits.limit.Gradient2Limit} */ public class Gradient2Limit extends AbstractLimit { diff --git a/ceresdb-grpc/src/main/java/com/ceresdb/rpc/limit/VegasLimit.java b/ceresdb-grpc/src/main/java/com/ceresdb/rpc/limit/VegasLimit.java index 3cea584..4d704c4 100644 --- a/ceresdb-grpc/src/main/java/com/ceresdb/rpc/limit/VegasLimit.java +++ b/ceresdb-grpc/src/main/java/com/ceresdb/rpc/limit/VegasLimit.java @@ -33,7 +33,7 @@ /** * Limiter based on TCP Vegas where the limit increases by alpha if the queue_use is small ({@literal <} alpha) * and decreases by alpha if the queue_use is large ({@literal >} beta). - * + * * Queue size is calculated using the formula, * queue_use = limit − BWE×RTTnoLoad = limit × (1 − RTTnoLoad/RTTactual) * diff --git a/ceresdb-protocol/src/main/java/com/ceresdb/MetricParser.java b/ceresdb-protocol/src/main/java/com/ceresdb/MetricParser.java index 00e4751..7e8b86a 100644 --- a/ceresdb-protocol/src/main/java/com/ceresdb/MetricParser.java +++ b/ceresdb-protocol/src/main/java/com/ceresdb/MetricParser.java @@ -21,7 +21,7 @@ /** * A metric QL parser that can extract metric names from the given QL. - * + * * @author jiachun.fjc */ public interface MetricParser { @@ -32,7 +32,7 @@ public interface MetricParser { /** * Extract metric(table) names from the given QL. - * + * * @return metric names */ List metricNames(); diff --git a/ceresdb-protocol/src/main/java/com/ceresdb/RouterClient.java b/ceresdb-protocol/src/main/java/com/ceresdb/RouterClient.java index 741ffa3..0b55d7d 100644 --- a/ceresdb-protocol/src/main/java/com/ceresdb/RouterClient.java +++ b/ceresdb-protocol/src/main/java/com/ceresdb/RouterClient.java @@ -294,7 +294,8 @@ private int gc0(final int times) { return times; } - LOG.warn("Now that the number of cached entries [{}] is about to exceed its limit [{}], we need to clean up.", // + LOG.warn("Now that the number of cached entries [{}] is about to exceed its limit [{}], we need to clean up.", + // this.routeCache.size(), this.opts.getMaxCachedSize()); final int itemsToGC = (int) (this.routeCache.size() * CLEAN_THRESHOLD); diff --git a/ceresdb-protocol/src/main/java/com/ceresdb/RpcServiceRegister.java b/ceresdb-protocol/src/main/java/com/ceresdb/RpcServiceRegister.java index 695d8f0..2d608b5 100644 --- a/ceresdb-protocol/src/main/java/com/ceresdb/RpcServiceRegister.java +++ b/ceresdb-protocol/src/main/java/com/ceresdb/RpcServiceRegister.java @@ -35,7 +35,8 @@ public class RpcServiceRegister { public static void registerStorageService() { // register protobuf serializer RpcFactoryProvider.getRpcFactory().register( - MethodDescriptor.of(String.format(STORAGE_METHOD_TEMPLATE, "Route"), MethodDescriptor.MethodType.UNARY), // + MethodDescriptor.of(String.format(STORAGE_METHOD_TEMPLATE, "Route"), MethodDescriptor.MethodType.UNARY), + // Storage.RouteRequest.class, // Storage.RouteRequest.getDefaultInstance(), // Storage.RouteResponse.getDefaultInstance()); diff --git a/ceresdb-sql-javacc/src/main/java/com/ceresdb/sql/ParseHandler.java b/ceresdb-sql-javacc/src/main/java/com/ceresdb/sql/ParseHandler.java index 7b0cfce..b6586d4 100644 --- a/ceresdb-sql-javacc/src/main/java/com/ceresdb/sql/ParseHandler.java +++ b/ceresdb-sql-javacc/src/main/java/com/ceresdb/sql/ParseHandler.java @@ -24,7 +24,7 @@ public abstract class ParseHandler { /** * Handle macro like "#include('/tmp/template.sql')". - * + * * @param name name of the macro * @param parameters parameters * @return output of the macro, could be null or empty string @@ -35,7 +35,7 @@ public String handleMacro(final String name, final List parameters) { /** * Handle parameter. - * + * * @param database database * @param tables tables * @param columnIndex columnIndex(starts from 1 not 0) @@ -47,7 +47,7 @@ public String handleParameter(final String database, final List tables, /** * Handle statement. - * + * * @param sql sql statement * @param stmtType statement type * @param database database diff --git a/pom.xml b/pom.xml index 522130b..6fabb0b 100644 --- a/pom.xml +++ b/pom.xml @@ -317,8 +317,6 @@ ${main.user.dir}/tools/codestyle/formatter.xml ${project.encoding} - **/ServiceLoader.java - **/UnsafeUtil.java **/proto/* **/sql/AbstractCharStream.java **/sql/CeresDBxSqlParser.java From b6d82108a172f37d1342f8274d394e195f168500 Mon Sep 17 00:00:00 2001 From: Arthur Chern Date: Mon, 25 Jul 2022 20:49:25 +0800 Subject: [PATCH 3/3] fix: delete lifecycleMappingMetadata config in pom, IDEA reports this is not allowed --- pom.xml | 43 ------------------------------------------- 1 file changed, 43 deletions(-) diff --git a/pom.xml b/pom.xml index 6fabb0b..c3435e3 100644 --- a/pom.xml +++ b/pom.xml @@ -244,49 +244,6 @@ - - - - - org.eclipse.m2e - lifecycle-mapping - 1.0.0 - - - - - - com.mycila - license-maven-plugin - [3.0,) - - remove - format - - - - - - - - - com.github.ekryd.sortpom - sortpom-maven-plugin - [2.4.0,) - - sort - - - - - - - - - - - - org.apache.maven.plugins