sandynz commented on code in PR #28953: URL: https://github.com/apache/shardingsphere/pull/28953#discussion_r1384798340
########## kernel/data-pipeline/scenario/cdc/client/src/main/java/org/apache/shardingsphere/data/pipeline/cdc/client/handler/RetryStreamingExceptionHandler.java: ########## @@ -0,0 +1,71 @@ +/* + * 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.SneakyThrows; +import lombok.extern.slf4j.Slf4j; +import org.apache.shardingsphere.data.pipeline.cdc.client.CDCClient; +import org.apache.shardingsphere.data.pipeline.cdc.client.context.ClientConnectionContext; +import org.apache.shardingsphere.data.pipeline.cdc.client.util.ServerErrorResult; + +import java.util.concurrent.CompletableFuture; +import java.util.concurrent.TimeUnit; +import java.util.concurrent.atomic.AtomicInteger; + +/** + * Retry streaming exception handler. + */ +@RequiredArgsConstructor +@Slf4j +public class RetryStreamingExceptionHandler implements ExceptionHandler { + + private final AtomicInteger retryTimes = new AtomicInteger(0); + + private CDCClient cdcClient; + + @Override + public void setCDCClient(final CDCClient cdcClient) { + this.cdcClient = cdcClient; + } Review Comment: Could be replaced by `@Setter` ########## kernel/data-pipeline/scenario/cdc/client/src/main/java/org/apache/shardingsphere/data/pipeline/cdc/client/handler/RetryStreamingExceptionHandler.java: ########## @@ -0,0 +1,71 @@ +/* + * 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.SneakyThrows; +import lombok.extern.slf4j.Slf4j; +import org.apache.shardingsphere.data.pipeline.cdc.client.CDCClient; +import org.apache.shardingsphere.data.pipeline.cdc.client.context.ClientConnectionContext; +import org.apache.shardingsphere.data.pipeline.cdc.client.util.ServerErrorResult; + +import java.util.concurrent.CompletableFuture; +import java.util.concurrent.TimeUnit; +import java.util.concurrent.atomic.AtomicInteger; + +/** + * Retry streaming exception handler. + */ +@RequiredArgsConstructor +@Slf4j +public class RetryStreamingExceptionHandler implements ExceptionHandler { + + private final AtomicInteger retryTimes = new AtomicInteger(0); + + private CDCClient cdcClient; + + @Override + public void setCDCClient(final CDCClient cdcClient) { + this.cdcClient = cdcClient; + } + + @Override + public void handleServerException(final ChannelHandlerContext ctx, final ServerErrorResult result) { + // Server exception maybe not be resolved by retrying + log.error("Server error, code: {}, message: {}", result.getErrorCode(), result.getErrorMessage()); + } + + @SneakyThrows(InterruptedException.class) + @Override + public void handleSocketException(final ChannelHandlerContext ctx, final Throwable throwable) { + if (null == cdcClient) { + log.warn("CDC client is null, could not retry"); + return; + } + ClientConnectionContext connectionContext = ctx.channel().attr(ClientConnectionContext.CONTEXT_KEY).get(); + if (retryTimes.get() > 5) { + log.warn("Retry times exceed 5, stop streaming"); + connectionContext.getStreamingIds().forEach(each -> CompletableFuture.runAsync(() -> cdcClient.stopStreaming(each))); + return; + } + retryTimes.incrementAndGet(); + TimeUnit.SECONDS.sleep(Math.min(5 >> retryTimes.get(), 10)); Review Comment: It's better to extract retryTimes and sleepTime as fields, so users could customize them ########## kernel/data-pipeline/scenario/cdc/client/src/main/java/org/apache/shardingsphere/data/pipeline/cdc/client/handler/RetryStreamingExceptionHandler.java: ########## @@ -0,0 +1,71 @@ +/* + * 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.SneakyThrows; +import lombok.extern.slf4j.Slf4j; +import org.apache.shardingsphere.data.pipeline.cdc.client.CDCClient; +import org.apache.shardingsphere.data.pipeline.cdc.client.context.ClientConnectionContext; +import org.apache.shardingsphere.data.pipeline.cdc.client.util.ServerErrorResult; + +import java.util.concurrent.CompletableFuture; +import java.util.concurrent.TimeUnit; +import java.util.concurrent.atomic.AtomicInteger; + +/** + * Retry streaming exception handler. + */ +@RequiredArgsConstructor +@Slf4j +public class RetryStreamingExceptionHandler implements ExceptionHandler { + + private final AtomicInteger retryTimes = new AtomicInteger(0); + + private CDCClient cdcClient; + + @Override + public void setCDCClient(final CDCClient cdcClient) { + this.cdcClient = cdcClient; + } + + @Override + public void handleServerException(final ChannelHandlerContext ctx, final ServerErrorResult result) { + // Server exception maybe not be resolved by retrying + log.error("Server error, code: {}, message: {}", result.getErrorCode(), result.getErrorMessage()); Review Comment: Looks it's not defined clearly, users might need to customize it again, but could not just reuse RetryStreamingExceptionHandler. Could we extract retry logic to utility class? Or extract two handlers, one is exception handler, and another is server error handler? -- This is an automated message from the Apache Git Service. To respond to the message, please log on to GitHub and use the URL above to go to the specific comment. To unsubscribe, e-mail: [email protected] For queries about this service, please contact Infrastructure at: [email protected]
