-
Notifications
You must be signed in to change notification settings - Fork 6.8k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
6 changed files
with
96 additions
and
22 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
71 changes: 71 additions & 0 deletions
71
...pache/shardingsphere/data/pipeline/cdc/client/handler/RetryStreamingExceptionHandler.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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)); | ||
connectionContext.getStreamingIds().forEach(each -> CompletableFuture.runAsync(() -> cdcClient.restartStreaming(each))); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters