|
| 1 | +/* |
| 2 | + * Licensed to the Apache Software Foundation (ASF) under one |
| 3 | + * or more contributor license agreements. See the NOTICE file |
| 4 | + * distributed with this work for additional information |
| 5 | + * regarding copyright ownership. The ASF licenses this file |
| 6 | + * to you under the Apache License, Version 2.0 (the |
| 7 | + * "License"); you may not use this file except in compliance |
| 8 | + * with the License. You may obtain a copy of the License at |
| 9 | + * |
| 10 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 11 | + * |
| 12 | + * Unless required by applicable law or agreed to in writing, |
| 13 | + * software distributed under the License is distributed on an |
| 14 | + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY |
| 15 | + * KIND, either express or implied. See the License for the |
| 16 | + * specific language governing permissions and limitations |
| 17 | + * under the License. |
| 18 | + */ |
| 19 | +package org.apache.pulsar.broker.loadbalance.extensions.manager; |
| 20 | + |
| 21 | +import static org.apache.pulsar.broker.loadbalance.extensions.models.SplitDecision.Label.Failure; |
| 22 | +import static org.apache.pulsar.broker.loadbalance.extensions.models.SplitDecision.Reason.Unknown; |
| 23 | +import java.util.Map; |
| 24 | +import java.util.concurrent.CompletableFuture; |
| 25 | +import java.util.concurrent.ConcurrentHashMap; |
| 26 | +import java.util.concurrent.TimeUnit; |
| 27 | +import lombok.extern.slf4j.Slf4j; |
| 28 | +import org.apache.pulsar.broker.loadbalance.extensions.channel.ServiceUnitState; |
| 29 | +import org.apache.pulsar.broker.loadbalance.extensions.channel.ServiceUnitStateData; |
| 30 | +import org.apache.pulsar.broker.loadbalance.extensions.models.SplitCounter; |
| 31 | +import org.apache.pulsar.broker.loadbalance.extensions.models.SplitDecision; |
| 32 | + |
| 33 | +/** |
| 34 | + * Split manager. |
| 35 | + */ |
| 36 | +@Slf4j |
| 37 | +public class SplitManager implements StateChangeListener { |
| 38 | + |
| 39 | + record InFlightSplitRequest(SplitDecision splitDecision, CompletableFuture<Void> future) { |
| 40 | + } |
| 41 | + |
| 42 | + private final Map<String, InFlightSplitRequest> inFlightSplitRequests; |
| 43 | + |
| 44 | + private final SplitCounter counter; |
| 45 | + |
| 46 | + public SplitManager(SplitCounter splitCounter) { |
| 47 | + this.inFlightSplitRequests = new ConcurrentHashMap<>(); |
| 48 | + this.counter = splitCounter; |
| 49 | + } |
| 50 | + |
| 51 | + private void complete(String serviceUnit, Throwable ex) { |
| 52 | + inFlightSplitRequests.computeIfPresent(serviceUnit, (__, inFlightSplitRequest) -> { |
| 53 | + var future = inFlightSplitRequest.future; |
| 54 | + if (!future.isDone()) { |
| 55 | + if (ex != null) { |
| 56 | + counter.update(Failure, Unknown); |
| 57 | + future.completeExceptionally(ex); |
| 58 | + if (log.isDebugEnabled()) { |
| 59 | + log.debug("Complete exceptionally split bundle: {}", serviceUnit, ex); |
| 60 | + } |
| 61 | + } else { |
| 62 | + counter.update(inFlightSplitRequest.splitDecision); |
| 63 | + future.complete(null); |
| 64 | + if (log.isDebugEnabled()) { |
| 65 | + log.debug("Complete split bundle: {}", serviceUnit); |
| 66 | + } |
| 67 | + } |
| 68 | + } |
| 69 | + return null; |
| 70 | + }); |
| 71 | + } |
| 72 | + |
| 73 | + public CompletableFuture<Void> waitAsync(CompletableFuture<Void> eventPubFuture, |
| 74 | + String bundle, |
| 75 | + SplitDecision decision, |
| 76 | + long timeout, |
| 77 | + TimeUnit timeoutUnit) { |
| 78 | + |
| 79 | + return eventPubFuture.thenCompose(__ -> inFlightSplitRequests.computeIfAbsent(bundle, ignore -> { |
| 80 | + if (log.isDebugEnabled()) { |
| 81 | + log.debug("Handle split bundle: {}, timeout: {} {}", bundle, timeout, timeoutUnit); |
| 82 | + } |
| 83 | + CompletableFuture<Void> future = new CompletableFuture<>(); |
| 84 | + future.orTimeout(timeout, timeoutUnit).whenComplete((v, ex) -> { |
| 85 | + if (ex != null) { |
| 86 | + inFlightSplitRequests.remove(bundle); |
| 87 | + log.warn("Failed to wait for split for serviceUnit: {}", bundle, ex); |
| 88 | + } |
| 89 | + }); |
| 90 | + return new InFlightSplitRequest(decision, future); |
| 91 | + }).future); |
| 92 | + } |
| 93 | + |
| 94 | + @Override |
| 95 | + public void handleEvent(String serviceUnit, ServiceUnitStateData data, Throwable t) { |
| 96 | + ServiceUnitState state = ServiceUnitStateData.state(data); |
| 97 | + if (t != null && inFlightSplitRequests.containsKey(serviceUnit)) { |
| 98 | + this.complete(serviceUnit, t); |
| 99 | + return; |
| 100 | + } |
| 101 | + switch (state) { |
| 102 | + case Deleted, Owned, Init -> this.complete(serviceUnit, t); |
| 103 | + default -> { |
| 104 | + if (log.isDebugEnabled()) { |
| 105 | + log.debug("Handling {} for service unit {}", data, serviceUnit); |
| 106 | + } |
| 107 | + } |
| 108 | + } |
| 109 | + } |
| 110 | + |
| 111 | + public void close() { |
| 112 | + inFlightSplitRequests.forEach((bundle, inFlightSplitRequest) -> { |
| 113 | + if (!inFlightSplitRequest.future.isDone()) { |
| 114 | + String msg = String.format("Splitting bundle: %s, but the manager already closed.", bundle); |
| 115 | + log.warn(msg); |
| 116 | + inFlightSplitRequest.future.completeExceptionally(new IllegalStateException(msg)); |
| 117 | + } |
| 118 | + }); |
| 119 | + inFlightSplitRequests.clear(); |
| 120 | + } |
| 121 | +} |
0 commit comments