|
50 | 50 | import java.util.concurrent.ConcurrentHashMap; |
51 | 51 | import java.util.concurrent.ConcurrentMap; |
52 | 52 | import java.util.concurrent.ExecutionException; |
| 53 | +import java.util.concurrent.ExecutorService; |
53 | 54 | import java.util.concurrent.Executors; |
| 55 | +import java.util.concurrent.RejectedExecutionException; |
54 | 56 | import java.util.concurrent.ScheduledExecutorService; |
55 | 57 | import java.util.concurrent.TimeUnit; |
| 58 | +import java.util.concurrent.atomic.AtomicBoolean; |
56 | 59 | import java.util.function.Supplier; |
57 | 60 | import java.util.stream.Collectors; |
58 | 61 | import org.apache.http.client.methods.CloseableHttpResponse; |
@@ -122,6 +125,8 @@ public class PDClient extends AbstractGRPCClient<PDBlockingStub, PDFutureStub> |
122 | 125 | private ConcurrentMap<Long, Double> tiflashReplicaMap; |
123 | 126 | private HostMapping hostMapping; |
124 | 127 | private long lastUpdateLeaderTime; |
| 128 | + private final ExecutorService updateLeaderService = Executors.newSingleThreadExecutor(); |
| 129 | + private final AtomicBoolean updateLeaderNotify = new AtomicBoolean(); |
125 | 130 |
|
126 | 131 | public static final Histogram PD_GET_REGION_BY_KEY_REQUEST_LATENCY = |
127 | 132 | HistogramUtils.buildDuration() |
@@ -426,6 +431,8 @@ public void close() throws InterruptedException { |
426 | 431 | if (channelFactory != null) { |
427 | 432 | channelFactory.close(); |
428 | 433 | } |
| 434 | + |
| 435 | + updateLeaderService.shutdownNow(); |
429 | 436 | } |
430 | 437 |
|
431 | 438 | @VisibleForTesting |
@@ -462,11 +469,7 @@ private GetMembersResponse doGetMembers(BackOffer backOffer, URI uri) { |
462 | 469 | } |
463 | 470 |
|
464 | 471 | private GetMembersResponse getMembers(BackOffer backOffer, URI uri) { |
465 | | - try { |
466 | | - return doGetMembers(backOffer, uri); |
467 | | - } catch (Exception e) { |
468 | | - return null; |
469 | | - } |
| 472 | + return doGetMembers(backOffer, uri); |
470 | 473 | } |
471 | 474 |
|
472 | 475 | // return whether the leader has changed to target address `leaderUrlStr`. |
@@ -518,7 +521,26 @@ synchronized boolean createFollowerClientWrapper( |
518 | 521 | return true; |
519 | 522 | } |
520 | 523 |
|
521 | | - public synchronized void updateLeaderOrForwardFollower(BackOffer backOffer) { |
| 524 | + public void tryUpdateLeaderOrForwardFollower() { |
| 525 | + if (updateLeaderNotify.compareAndSet(false, true)) { |
| 526 | + try { |
| 527 | + BackOffer backOffer = defaultBackOffer(); |
| 528 | + updateLeaderService.submit( |
| 529 | + () -> { |
| 530 | + try { |
| 531 | + updateLeaderOrForwardFollower(backOffer); |
| 532 | + } finally { |
| 533 | + updateLeaderNotify.set(false); |
| 534 | + } |
| 535 | + }); |
| 536 | + } catch (RejectedExecutionException e) { |
| 537 | + logger.error("PDClient is shutdown", e); |
| 538 | + updateLeaderNotify.set(false); |
| 539 | + } |
| 540 | + } |
| 541 | + } |
| 542 | + |
| 543 | + private synchronized void updateLeaderOrForwardFollower(BackOffer backOffer) { |
522 | 544 | if (System.currentTimeMillis() - lastUpdateLeaderTime < MIN_TRY_UPDATE_DURATION) { |
523 | 545 | return; |
524 | 546 | } |
|
0 commit comments