|
| 1 | +package com.navercorp.pinpoint.collector.util; |
| 2 | + |
| 3 | + |
| 4 | +import com.navercorp.pinpoint.rpc.PinpointSocketException; |
| 5 | +import io.netty.util.Timeout; |
| 6 | +import io.netty.util.Timer; |
| 7 | +import io.netty.util.TimerTask; |
| 8 | +import org.apache.logging.log4j.LogManager; |
| 9 | +import org.apache.logging.log4j.Logger; |
| 10 | + |
| 11 | +import java.util.Map; |
| 12 | +import java.util.Objects; |
| 13 | +import java.util.concurrent.CompletableFuture; |
| 14 | +import java.util.concurrent.ConcurrentHashMap; |
| 15 | +import java.util.concurrent.ConcurrentMap; |
| 16 | +import java.util.concurrent.TimeUnit; |
| 17 | +import java.util.concurrent.TimeoutException; |
| 18 | +import java.util.concurrent.atomic.AtomicInteger; |
| 19 | +import java.util.function.BiConsumer; |
| 20 | +import java.util.function.Supplier; |
| 21 | + |
| 22 | +/** |
| 23 | + * @author emeroad |
| 24 | + */ |
| 25 | +public class RequestManager<RES> { |
| 26 | + |
| 27 | + private final Logger logger = LogManager.getLogger(this.getClass()); |
| 28 | + |
| 29 | + private final AtomicInteger requestId = new AtomicInteger(1); |
| 30 | + |
| 31 | + private final ConcurrentMap<Integer, CompletableFuture<RES>> requestMap = new ConcurrentHashMap<>(); |
| 32 | + // Have to move Timer into factory? |
| 33 | + private final Timer timer; |
| 34 | + private final long defaultTimeoutMillis; |
| 35 | + |
| 36 | + public RequestManager(Timer timer, long defaultTimeoutMillis) { |
| 37 | + this.timer = Objects.requireNonNull(timer, "timer"); |
| 38 | + |
| 39 | + if (defaultTimeoutMillis <= 0) { |
| 40 | + throw new IllegalArgumentException("defaultTimeoutMillis must greater than zero."); |
| 41 | + } |
| 42 | + this.defaultTimeoutMillis = defaultTimeoutMillis; |
| 43 | + } |
| 44 | + |
| 45 | + private BiConsumer<RES, Throwable> createFailureEventHandler(final int requestId) { |
| 46 | + return new RequestFailListener<>(requestId, this::removeMessageFuture); |
| 47 | + } |
| 48 | + |
| 49 | + private void addTimeoutTask(CompletableFuture<RES> future, long timeoutMillis) { |
| 50 | + Objects.requireNonNull(future, "future"); |
| 51 | + |
| 52 | + try { |
| 53 | + Timeout timeout = timer.newTimeout(new TimerTask() { |
| 54 | + @Override |
| 55 | + public void run(Timeout timeout) throws Exception { |
| 56 | + if (timeout.isCancelled()) { |
| 57 | + return; |
| 58 | + } |
| 59 | + if (future.isDone()) { |
| 60 | + return; |
| 61 | + } |
| 62 | + future.completeExceptionally(new TimeoutException("Timeout by RequestManager-TIMER")); |
| 63 | + } |
| 64 | + }, timeoutMillis, TimeUnit.MILLISECONDS); |
| 65 | + future.thenAccept(t -> timeout.cancel()); |
| 66 | + } catch (IllegalStateException e) { |
| 67 | + // this case is that timer has been shutdown. That maybe just means that socket has been closed. |
| 68 | + future.completeExceptionally(new PinpointSocketException("socket closed")) ; |
| 69 | + } |
| 70 | + } |
| 71 | + |
| 72 | + public int nextRequestId() { |
| 73 | + return this.requestId.getAndIncrement(); |
| 74 | + } |
| 75 | + |
| 76 | + |
| 77 | + public CompletableFuture<RES> messageReceived(int responseId, Supplier<Object> sourceName) { |
| 78 | + final CompletableFuture<RES> future = removeMessageFuture(responseId); |
| 79 | + if (future == null) { |
| 80 | + logger.warn("ResponseFuture not found. responseId:{}, sourceName:{}", responseId, sourceName.get()); |
| 81 | + return null; |
| 82 | + } |
| 83 | + if (logger.isDebugEnabled()) { |
| 84 | + logger.debug("ResponsePacket is arrived responseId:{}, sourceName:{}", responseId, sourceName.get()); |
| 85 | + } |
| 86 | + return future; |
| 87 | + } |
| 88 | + |
| 89 | + |
| 90 | + public CompletableFuture<RES> removeMessageFuture(int requestId) { |
| 91 | + return this.requestMap.remove(requestId); |
| 92 | + } |
| 93 | + |
| 94 | + public CompletableFuture<RES> register(int requestId) { |
| 95 | + return register(requestId, defaultTimeoutMillis); |
| 96 | + } |
| 97 | + |
| 98 | + public CompletableFuture<RES> register(int requestId, long timeoutMillis) { |
| 99 | + // shutdown check |
| 100 | + final CompletableFuture<RES> responseFuture = new CompletableFuture<>(); |
| 101 | + |
| 102 | + final CompletableFuture<RES> old = this.requestMap.put(requestId, responseFuture); |
| 103 | + if (old != null) { |
| 104 | + throw new PinpointSocketException("unexpected error. old future exist:" + old + " id:" + requestId); |
| 105 | + } |
| 106 | + // when future fails, put a handle in order to remove a failed future in the requestMap. |
| 107 | + BiConsumer<RES, Throwable> removeTable = createFailureEventHandler(requestId); |
| 108 | + responseFuture.whenComplete(removeTable); |
| 109 | + |
| 110 | + addTimeoutTask(responseFuture, timeoutMillis); |
| 111 | + return responseFuture; |
| 112 | + } |
| 113 | + |
| 114 | + public void close() { |
| 115 | + logger.debug("close()"); |
| 116 | + final PinpointSocketException closed = new PinpointSocketException("socket closed"); |
| 117 | + |
| 118 | + int requestFailCount = 0; |
| 119 | + for (Map.Entry<Integer, CompletableFuture<RES>> entry : requestMap.entrySet()) { |
| 120 | + if (entry.getValue().completeExceptionally(closed)) { |
| 121 | + requestFailCount++; |
| 122 | + } |
| 123 | + } |
| 124 | + this.requestMap.clear(); |
| 125 | + if (requestFailCount > 0) { |
| 126 | + logger.info("Close fail count:{}", requestFailCount); |
| 127 | + } |
| 128 | + |
| 129 | + } |
| 130 | + |
| 131 | +} |
0 commit comments