From 33b1c191d790118c049462fa0489ce3ebfd286ac Mon Sep 17 00:00:00 2001 From: azexcy <13588031592@qq.com> Date: Wed, 8 Nov 2023 10:56:12 +0800 Subject: [PATCH] Improve parameter and error log --- .../handler/LoggerExceptionErrorHandler.java | 41 ------------------- .../RetryStreamingExceptionHandler.java | 23 ++++------- .../cdc/client/example/Bootstrap.java | 26 ++++-------- .../e2e/data/pipeline/cases/cdc/CDCE2EIT.java | 5 +-- 4 files changed, 18 insertions(+), 77 deletions(-) delete mode 100644 kernel/data-pipeline/scenario/cdc/client/src/main/java/org/apache/shardingsphere/data/pipeline/cdc/client/handler/LoggerExceptionErrorHandler.java diff --git a/kernel/data-pipeline/scenario/cdc/client/src/main/java/org/apache/shardingsphere/data/pipeline/cdc/client/handler/LoggerExceptionErrorHandler.java b/kernel/data-pipeline/scenario/cdc/client/src/main/java/org/apache/shardingsphere/data/pipeline/cdc/client/handler/LoggerExceptionErrorHandler.java deleted file mode 100644 index 1ea3bea3dc7fc..0000000000000 --- a/kernel/data-pipeline/scenario/cdc/client/src/main/java/org/apache/shardingsphere/data/pipeline/cdc/client/handler/LoggerExceptionErrorHandler.java +++ /dev/null @@ -1,41 +0,0 @@ -/* - * Licensed to the Apache Software Foundation (ASF) under one or more - * contributor license agreements. See the NOTICE file distributed with - * this work for additional information regarding copyright ownership. - * The ASF licenses this file to You under the Apache License, Version 2.0 - * (the "License"); you may not use this file except in compliance with - * the License. You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -package org.apache.shardingsphere.data.pipeline.cdc.client.handler; - -import io.netty.channel.ChannelHandlerContext; -import lombok.RequiredArgsConstructor; -import lombok.extern.slf4j.Slf4j; -import org.apache.shardingsphere.data.pipeline.cdc.client.util.ServerErrorResult; - -/** - * Logger exception error handler. - */ -@RequiredArgsConstructor -@Slf4j -public final class LoggerExceptionErrorHandler implements ExceptionHandler, ServerErrorResultHandler { - - @Override - public void handleServerError(final ChannelHandlerContext ctx, final ServerErrorResult result) { - log.error("Server error, code: {}, message: {}", result.getErrorCode(), result.getErrorMessage()); - } - - @Override - public void handleException(final ChannelHandlerContext ctx, final Throwable throwable) { - log.error("Exception error: ", throwable); - } -} diff --git a/kernel/data-pipeline/scenario/cdc/client/src/main/java/org/apache/shardingsphere/data/pipeline/cdc/client/handler/RetryStreamingExceptionHandler.java b/kernel/data-pipeline/scenario/cdc/client/src/main/java/org/apache/shardingsphere/data/pipeline/cdc/client/handler/RetryStreamingExceptionHandler.java index a495db77b6f0c..7efa76da1953a 100644 --- a/kernel/data-pipeline/scenario/cdc/client/src/main/java/org/apache/shardingsphere/data/pipeline/cdc/client/handler/RetryStreamingExceptionHandler.java +++ b/kernel/data-pipeline/scenario/cdc/client/src/main/java/org/apache/shardingsphere/data/pipeline/cdc/client/handler/RetryStreamingExceptionHandler.java @@ -31,44 +31,39 @@ * Retry streaming exception handler. */ @Slf4j -public class RetryStreamingExceptionHandler implements ExceptionHandler { +public final class RetryStreamingExceptionHandler implements ExceptionHandler { private final CDCClient cdcClient; - private final AtomicInteger maxRetryTimes = new AtomicInteger(0); + private final AtomicInteger maxRetryTimes; private final int retryIntervalMills; - private final int retryTimes; + private final AtomicInteger retryTimes = new AtomicInteger(0); public RetryStreamingExceptionHandler(final CDCClient cdcClient, final int maxRetryTimes, final int retryIntervalMills) { this.cdcClient = cdcClient; - this.maxRetryTimes.set(maxRetryTimes); + this.maxRetryTimes = new AtomicInteger(maxRetryTimes); this.retryIntervalMills = retryIntervalMills; - retryTimes = 0; } @Override public void handleException(final ChannelHandlerContext ctx, final Throwable throwable) { - log.error("Socket error: {}", throwable.getMessage()); + log.error("Catch exception: ", throwable); reconnect(ctx); } @SneakyThrows(InterruptedException.class) private void reconnect(final ChannelHandlerContext ctx) { - maxRetryTimes.incrementAndGet(); - if (null == cdcClient) { - log.warn("CDC client is null, could not retry"); - return; - } + retryTimes.incrementAndGet(); ClientConnectionContext connectionContext = ctx.channel().attr(ClientConnectionContext.CONTEXT_KEY).get(); - if (retryTimes > maxRetryTimes.get()) { - log.warn("Retry times exceed 5, stop streaming"); + if (retryTimes.get() > maxRetryTimes.get()) { + log.warn("Stop try to reconnect, stop streaming ids: {}", connectionContext.getStreamingIds()); connectionContext.getStreamingIds().forEach(each -> CompletableFuture.runAsync(() -> cdcClient.stopStreaming(each))); return; } TimeUnit.MILLISECONDS.sleep(retryIntervalMills); - log.info("Retry to restart streaming, retry count: {}", maxRetryTimes.get()); + log.info("Retry to restart streaming, retry times: {}", retryTimes.get()); connectionContext.getStreamingIds().forEach(each -> CompletableFuture.runAsync(() -> cdcClient.restartStreaming(each))); } } diff --git a/kernel/data-pipeline/scenario/cdc/client/src/test/java/org/apache/shardingsphere/data/pipeline/cdc/client/example/Bootstrap.java b/kernel/data-pipeline/scenario/cdc/client/src/test/java/org/apache/shardingsphere/data/pipeline/cdc/client/example/Bootstrap.java index 0a8e2591792b5..5b6b9ba0f07de 100644 --- a/kernel/data-pipeline/scenario/cdc/client/src/test/java/org/apache/shardingsphere/data/pipeline/cdc/client/example/Bootstrap.java +++ b/kernel/data-pipeline/scenario/cdc/client/src/test/java/org/apache/shardingsphere/data/pipeline/cdc/client/example/Bootstrap.java @@ -21,7 +21,6 @@ import lombok.extern.slf4j.Slf4j; import org.apache.shardingsphere.data.pipeline.cdc.client.CDCClient; import org.apache.shardingsphere.data.pipeline.cdc.client.config.CDCClientConfiguration; -import org.apache.shardingsphere.data.pipeline.cdc.client.handler.LoggerExceptionErrorHandler; import org.apache.shardingsphere.data.pipeline.cdc.client.handler.RetryStreamingExceptionHandler; import org.apache.shardingsphere.data.pipeline.cdc.client.parameter.CDCLoginParameter; import org.apache.shardingsphere.data.pipeline.cdc.client.parameter.StartStreamingParameter; @@ -42,24 +41,13 @@ public static void main(final String[] args) { // Pay attention to the time zone, to avoid the problem of incorrect time zone, it is best to ensure that the time zone of the program is consistent with the time zone of the database server // TimeZone.setDefault(TimeZone.getTimeZone("UTC")); String address = "127.0.0.1"; - int reconnectCount = 0; - int maxReconnectCount = 5; - while (reconnectCount < maxReconnectCount) { - try (CDCClient cdcClient = new CDCClient(new CDCClientConfiguration(address, 33071, 5000))) { - LoggerExceptionErrorHandler loggerExceptionErrorHandler = new LoggerExceptionErrorHandler(); - cdcClient.connect(records -> log.info("records: {}", records), new RetryStreamingExceptionHandler(cdcClient, 5, 5000), loggerExceptionErrorHandler); - cdcClient.login(new CDCLoginParameter("root", "root")); - String streamingId = cdcClient.startStreaming(new StartStreamingParameter("sharding_db", Collections.singleton(SchemaTable.newBuilder().setTable("t_order123").build()), true)); - log.info("Streaming id={}", streamingId); - cdcClient.await(); - // CHECKSTYLE:OFF - } catch (final Exception ex) { - // CHECKSTYLE:ON - log.error("Exception occur: {}", ex.getMessage()); - } - Thread.sleep(5000); - log.info("Reconnect count: {}", reconnectCount); - reconnectCount++; + try (CDCClient cdcClient = new CDCClient(new CDCClientConfiguration(address, 33071, 10000))) { + cdcClient.connect(records -> log.info("records: {}", records), new RetryStreamingExceptionHandler(cdcClient, 5, 5000), + (ctx, result) -> log.error("Server error: {}", result.getErrorMessage())); + cdcClient.login(new CDCLoginParameter("root", "root")); + String streamingId = cdcClient.startStreaming(new StartStreamingParameter("sharding_db", Collections.singleton(SchemaTable.newBuilder().setTable("t_order").build()), true)); + log.info("Streaming id={}", streamingId); + cdcClient.await(); } } } diff --git a/test/e2e/operation/pipeline/src/test/java/org/apache/shardingsphere/test/e2e/data/pipeline/cases/cdc/CDCE2EIT.java b/test/e2e/operation/pipeline/src/test/java/org/apache/shardingsphere/test/e2e/data/pipeline/cases/cdc/CDCE2EIT.java index 53db08e4b2961..efc2ca5f19253 100644 --- a/test/e2e/operation/pipeline/src/test/java/org/apache/shardingsphere/test/e2e/data/pipeline/cases/cdc/CDCE2EIT.java +++ b/test/e2e/operation/pipeline/src/test/java/org/apache/shardingsphere/test/e2e/data/pipeline/cases/cdc/CDCE2EIT.java @@ -23,7 +23,7 @@ import org.apache.shardingsphere.data.pipeline.cdc.api.job.type.CDCJobType; import org.apache.shardingsphere.data.pipeline.cdc.client.CDCClient; import org.apache.shardingsphere.data.pipeline.cdc.client.config.CDCClientConfiguration; -import org.apache.shardingsphere.data.pipeline.cdc.client.handler.LoggerExceptionErrorHandler; +import org.apache.shardingsphere.data.pipeline.cdc.client.handler.RetryStreamingExceptionHandler; import org.apache.shardingsphere.data.pipeline.cdc.client.parameter.CDCLoginParameter; import org.apache.shardingsphere.data.pipeline.cdc.client.parameter.StartStreamingParameter; import org.apache.shardingsphere.data.pipeline.cdc.protocol.request.StreamDataRequestBody.SchemaTable; @@ -171,8 +171,7 @@ private void startCDCClient(final PipelineContainerComposer containerComposer, f DataSourceRecordConsumer recordConsumer = new DataSourceRecordConsumer(dataSource, containerComposer.getDatabaseType()); String schema = dialectDatabaseMetaData.isSchemaAvailable() ? "test" : ""; CDCClient cdcClient = new CDCClient(new CDCClientConfiguration("localhost", containerComposer.getContainerComposer().getProxyCDCPort(), 5000)); - LoggerExceptionErrorHandler handler = new LoggerExceptionErrorHandler(); - cdcClient.connect(recordConsumer, handler, handler); + cdcClient.connect(recordConsumer, new RetryStreamingExceptionHandler(cdcClient, 5, 5000), (ctx, result) -> log.error("Server error: {}", result.getErrorMessage())); cdcClient.login(new CDCLoginParameter(ProxyContainerConstants.USERNAME, ProxyContainerConstants.PASSWORD)); // TODO add full=false test case later cdcClient.startStreaming(new StartStreamingParameter("sharding_db", new HashSet<>(Arrays.asList(SchemaTable.newBuilder().setTable(SOURCE_TABLE_NAME).setSchema(schema).build(),