|
| 1 | +/* |
| 2 | + * Copyright 2019 NAVER Corp. |
| 3 | + * |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | + * you may not use this file except in compliance with the License. |
| 6 | + * You may obtain a copy of the License at |
| 7 | + * |
| 8 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | + * |
| 10 | + * Unless required by applicable law or agreed to in writing, software |
| 11 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | + * See the License for the specific language governing permissions and |
| 14 | + * limitations under the License. |
| 15 | + */ |
| 16 | + |
| 17 | +package com.navercorp.pinpoint.common.profiler.concurrent.executor; |
| 18 | + |
| 19 | +import com.navercorp.pinpoint.common.profiler.concurrent.PinpointThreadFactory; |
| 20 | +import org.apache.logging.log4j.LogManager; |
| 21 | +import org.apache.logging.log4j.Logger; |
| 22 | + |
| 23 | +import java.util.Collection; |
| 24 | +import java.util.Objects; |
| 25 | +import java.util.concurrent.LinkedBlockingQueue; |
| 26 | +import java.util.concurrent.ThreadFactory; |
| 27 | +import java.util.concurrent.TimeUnit; |
| 28 | +import java.util.concurrent.atomic.AtomicBoolean; |
| 29 | + |
| 30 | +/** |
| 31 | + * @author emeroad |
| 32 | + */ |
| 33 | +public class AsyncQueueingExecutor<T> { |
| 34 | + |
| 35 | + private final Logger logger; |
| 36 | + private final boolean isWarn; |
| 37 | + |
| 38 | + private final LinkedBlockingQueue<T> queue; |
| 39 | + private final AtomicBoolean isRun = new AtomicBoolean(true); |
| 40 | + private final Thread executeThread; |
| 41 | + private final String executorName; |
| 42 | + |
| 43 | + private final int maxDrainSize; |
| 44 | + // Caution. single thread only. this Collection is simpler than ArrayList. |
| 45 | + private final Collection<T> drain; |
| 46 | + |
| 47 | + private final AsyncQueueingExecutorListener<T> listener; |
| 48 | + |
| 49 | + |
| 50 | + public AsyncQueueingExecutor(int queueSize, String executorName, AsyncQueueingExecutorListener<T> listener) { |
| 51 | + Objects.requireNonNull(executorName, "executorName"); |
| 52 | + |
| 53 | + this.logger = LogManager.getLogger(this.getClass().getName() + "@" + executorName); |
| 54 | + this.isWarn = logger.isWarnEnabled(); |
| 55 | + |
| 56 | + // BEFORE executeThread start |
| 57 | + this.maxDrainSize = 10; |
| 58 | + this.drain = new UnsafeArrayCollection<>(maxDrainSize); |
| 59 | + this.queue = new LinkedBlockingQueue<>(queueSize); |
| 60 | + |
| 61 | + this.executeThread = this.createExecuteThread(executorName); |
| 62 | + this.executorName = executeThread.getName(); |
| 63 | + |
| 64 | + this.listener = Objects.requireNonNull(listener, "listener"); |
| 65 | + } |
| 66 | + |
| 67 | + private Thread createExecuteThread(String executorName) { |
| 68 | + final ThreadFactory threadFactory = new PinpointThreadFactory(executorName, true); |
| 69 | + Thread thread = threadFactory.newThread(this::doExecute); |
| 70 | + thread.start(); |
| 71 | + return thread; |
| 72 | + } |
| 73 | + |
| 74 | + private void doExecute() { |
| 75 | + long timeout = 2000; |
| 76 | + drainStartEntry: |
| 77 | + while (isRun()) { |
| 78 | + try { |
| 79 | + final Collection<T> dtoList = getDrainQueue(); |
| 80 | + final int drainSize = takeN(dtoList, this.maxDrainSize); |
| 81 | + if (drainSize > 0) { |
| 82 | + doExecute(dtoList); |
| 83 | + continue; |
| 84 | + } |
| 85 | + |
| 86 | + while (isRun()) { |
| 87 | + final T dto = takeOne(timeout); |
| 88 | + if (dto != null) { |
| 89 | + doExecute(dto); |
| 90 | + continue drainStartEntry; |
| 91 | + } else { |
| 92 | + pollTimeout(timeout); |
| 93 | + } |
| 94 | + } |
| 95 | + } catch (Throwable th) { |
| 96 | + logger.warn("{} doExecute(). Unexpected Error. Cause:{}", executorName, th.getMessage(), th); |
| 97 | + } |
| 98 | + } |
| 99 | + flushQueue(); |
| 100 | + } |
| 101 | + |
| 102 | + private void flushQueue() { |
| 103 | + boolean debugEnabled = logger.isDebugEnabled(); |
| 104 | + if (debugEnabled) { |
| 105 | + logger.debug("Loop is stop."); |
| 106 | + } |
| 107 | + while(true) { |
| 108 | + final Collection<T> elementList = getDrainQueue(); |
| 109 | + int drainSize = takeN(elementList, this.maxDrainSize); |
| 110 | + if (drainSize == 0) { |
| 111 | + break; |
| 112 | + } |
| 113 | + if (debugEnabled) { |
| 114 | + logger.debug("flushData size {}", drainSize); |
| 115 | + } |
| 116 | + doExecute(elementList); |
| 117 | + } |
| 118 | + } |
| 119 | + |
| 120 | + private T takeOne(long timeout) { |
| 121 | + try { |
| 122 | + return queue.poll(timeout, TimeUnit.MILLISECONDS); |
| 123 | + } catch (InterruptedException e) { |
| 124 | + Thread.currentThread().interrupt(); |
| 125 | + return null; |
| 126 | + } |
| 127 | + } |
| 128 | + |
| 129 | + private int takeN(Collection<T> drain, int maxDrainSize) { |
| 130 | + return queue.drainTo(drain, maxDrainSize); |
| 131 | + } |
| 132 | + |
| 133 | + protected void pollTimeout(long timeout) { |
| 134 | + // do nothing |
| 135 | + } |
| 136 | + |
| 137 | + public boolean execute(T data) { |
| 138 | + if (data == null) { |
| 139 | + if (isWarn) { |
| 140 | + logger.warn("execute(). data is null"); |
| 141 | + } |
| 142 | + return false; |
| 143 | + } |
| 144 | + if (!isRun.get()) { |
| 145 | + if (isWarn) { |
| 146 | + logger.warn("{} is shutdown. discard data:{}", executorName, data); |
| 147 | + } |
| 148 | + return false; |
| 149 | + } |
| 150 | + boolean offer = queue.offer(data); |
| 151 | + if (!offer) { |
| 152 | + if (isWarn) { |
| 153 | + logger.warn("{} Drop data. queue is full. size:{}", executorName, queue.size()); |
| 154 | + } |
| 155 | + } |
| 156 | + return offer; |
| 157 | + } |
| 158 | + |
| 159 | + |
| 160 | + private void doExecute(Collection<T> dtoList) { |
| 161 | + this.listener.execute(dtoList); |
| 162 | + } |
| 163 | + |
| 164 | + private void doExecute(T dto) { |
| 165 | + this.listener.execute(dto); |
| 166 | + } |
| 167 | + |
| 168 | + public boolean isEmpty() { |
| 169 | + return queue.isEmpty(); |
| 170 | + } |
| 171 | + |
| 172 | + public boolean isRun() { |
| 173 | + return isRun.get(); |
| 174 | + } |
| 175 | + |
| 176 | + public void stop() { |
| 177 | + isRun.set(false); |
| 178 | + |
| 179 | + if (!isEmpty()) { |
| 180 | + logger.info("Wait 5 seconds. Flushing queued data."); |
| 181 | + } |
| 182 | + executeThread.interrupt(); |
| 183 | + try { |
| 184 | + executeThread.join(5000); |
| 185 | + } catch (InterruptedException e) { |
| 186 | + Thread.currentThread().interrupt(); |
| 187 | + logger.warn("{} stopped incompletely.", executorName); |
| 188 | + } |
| 189 | + |
| 190 | + logger.info("{} stopped.", executorName); |
| 191 | + } |
| 192 | + |
| 193 | + Collection<T> getDrainQueue() { |
| 194 | + this.drain.clear(); |
| 195 | + return drain; |
| 196 | + } |
| 197 | +} |
0 commit comments