|
| 1 | +// Copyright (c) 2023 VMware, Inc. or its affiliates. All rights reserved. |
| 2 | +// |
| 3 | +// This software, the RabbitMQ Stream Java client library, is dual-licensed under the |
| 4 | +// Mozilla Public License 2.0 ("MPL"), and the Apache License version 2 ("ASL"). |
| 5 | +// For the MPL, please see LICENSE-MPL-RabbitMQ. For the ASL, |
| 6 | +// please see LICENSE-APACHE2. |
| 7 | +// |
| 8 | +// This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY KIND, |
| 9 | +// either express or implied. See the LICENSE file for specific language governing |
| 10 | +// rights and limitations of this software. |
| 11 | +// |
| 12 | +// If you have any questions regarding licensing, please contact us at |
| 13 | + |
| 14 | +package com.rabbitmq.stream.impl; |
| 15 | + |
| 16 | +import com.rabbitmq.stream.impl.Utils.NamedThreadFactory; |
| 17 | +import java.util.ArrayList; |
| 18 | +import java.util.Comparator; |
| 19 | +import java.util.List; |
| 20 | +import java.util.concurrent.CopyOnWriteArrayList; |
| 21 | +import java.util.concurrent.ExecutorService; |
| 22 | +import java.util.concurrent.Executors; |
| 23 | +import java.util.concurrent.ThreadFactory; |
| 24 | +import java.util.concurrent.atomic.AtomicBoolean; |
| 25 | +import java.util.concurrent.atomic.AtomicInteger; |
| 26 | +import java.util.function.Supplier; |
| 27 | +import java.util.stream.IntStream; |
| 28 | +import org.slf4j.Logger; |
| 29 | +import org.slf4j.LoggerFactory; |
| 30 | + |
| 31 | +class DefaultExecutorServiceFactory implements ExecutorServiceFactory { |
| 32 | + |
| 33 | + private static final Logger LOGGER = LoggerFactory.getLogger(DefaultExecutorServiceFactory.class); |
| 34 | + private static final Comparator<Executor> EXECUTOR_COMPARATOR = |
| 35 | + Comparator.comparingInt(Executor::usage); |
| 36 | + |
| 37 | + private final List<Executor> executors; |
| 38 | + private final AtomicBoolean closed = new AtomicBoolean(false); |
| 39 | + private final ThreadFactory threadFactory; |
| 40 | + private final int minSize; |
| 41 | + private final int clientPerExecutor; |
| 42 | + private final Supplier<Executor> executorFactory; |
| 43 | + |
| 44 | + DefaultExecutorServiceFactory() { |
| 45 | + this(Runtime.getRuntime().availableProcessors(), 10); |
| 46 | + } |
| 47 | + |
| 48 | + DefaultExecutorServiceFactory(int minSize, int clientPerExecutor) { |
| 49 | + this.minSize = minSize; |
| 50 | + this.clientPerExecutor = clientPerExecutor; |
| 51 | + this.threadFactory = new NamedThreadFactory("rabbitmq-stream-connection-"); |
| 52 | + this.executorFactory = () -> newExecutor(); |
| 53 | + List<Executor> l = new ArrayList<>(Runtime.getRuntime().availableProcessors()); |
| 54 | + IntStream.range(0, Runtime.getRuntime().availableProcessors()) |
| 55 | + .forEach(ignored -> l.add(this.executorFactory.get())); |
| 56 | + executors = new CopyOnWriteArrayList<>(l); |
| 57 | + } |
| 58 | + |
| 59 | + static void maybeResize( |
| 60 | + List<Executor> current, int min, int clientsPerResource, Supplier<Executor> factory) { |
| 61 | + LOGGER.debug( |
| 62 | + "Resizing {}, with min = {}, clients per resource = {}", current, min, clientsPerResource); |
| 63 | + int clientCount = 0; |
| 64 | + for (Executor executor : current) { |
| 65 | + clientCount += executor.usage(); |
| 66 | + } |
| 67 | + LOGGER.debug("Total usage is {}", clientCount); |
| 68 | + |
| 69 | + int target = Math.max((clientCount / clientsPerResource) + 1, min); |
| 70 | + LOGGER.debug("Target size is {}, current size is {}", target, current.size()); |
| 71 | + if (target > current.size()) { |
| 72 | + LOGGER.debug("Upsizing..."); |
| 73 | + List<Executor> l = new ArrayList<>(); |
| 74 | + for (int i = 0; i < target; i++) { |
| 75 | + if (i < current.size()) { |
| 76 | + l.add(current.get(i)); |
| 77 | + } else { |
| 78 | + l.add(factory.get()); |
| 79 | + } |
| 80 | + } |
| 81 | + current.clear(); |
| 82 | + current.addAll(l); |
| 83 | + LOGGER.debug("New list is {}", current); |
| 84 | + } else if (target < current.size()) { |
| 85 | + LOGGER.debug("Downsizing..."); |
| 86 | + boolean hasUnusedExecutors = current.stream().filter(ex -> ex.usage() == 0).count() > 0; |
| 87 | + if (!hasUnusedExecutors) { |
| 88 | + LOGGER.debug("No downsizing, there is no unused executor"); |
| 89 | + } |
| 90 | + if (hasUnusedExecutors) { |
| 91 | + List<Executor> l = new ArrayList<>(target); |
| 92 | + for (int i = 0; i < current.size(); i++) { |
| 93 | + Executor executor = current.get(i); |
| 94 | + if (executor.usage() == 0) { |
| 95 | + executor.close(); |
| 96 | + } else { |
| 97 | + l.add(executor); |
| 98 | + } |
| 99 | + } |
| 100 | + if (l.size() < target) { |
| 101 | + for (int i = l.size(); i < target; i++) { |
| 102 | + l.add(factory.get()); |
| 103 | + } |
| 104 | + } |
| 105 | + current.clear(); |
| 106 | + current.addAll(l); |
| 107 | + LOGGER.debug("New list is {}", current); |
| 108 | + } |
| 109 | + } |
| 110 | + } |
| 111 | + |
| 112 | + private Executor newExecutor() { |
| 113 | + return new Executor(Executors.newSingleThreadExecutor(threadFactory)); |
| 114 | + } |
| 115 | + |
| 116 | + @Override |
| 117 | + public synchronized ExecutorService get() { |
| 118 | + if (closed.get()) { |
| 119 | + throw new IllegalStateException("Executor service factory is closed"); |
| 120 | + } else { |
| 121 | + maybeResize(this.executors, this.minSize, this.clientPerExecutor, this.executorFactory); |
| 122 | + LOGGER.debug("Looking least used executor in {}", this.executors); |
| 123 | + Executor executor = this.executors.stream().min(EXECUTOR_COMPARATOR).get(); |
| 124 | + LOGGER.debug("Least used executor is {}", executor); |
| 125 | + executor.incrementUsage(); |
| 126 | + return executor.executorService; |
| 127 | + } |
| 128 | + } |
| 129 | + |
| 130 | + @Override |
| 131 | + public synchronized void clientClosed(ExecutorService executorService) { |
| 132 | + if (!closed.get()) { |
| 133 | + Executor executor = find(executorService); |
| 134 | + if (executor == null) { |
| 135 | + LOGGER.info("Could not find executor service wrapper"); |
| 136 | + } else { |
| 137 | + executor.decrementUsage(); |
| 138 | + maybeResize(this.executors, this.minSize, this.clientPerExecutor, this.executorFactory); |
| 139 | + } |
| 140 | + } |
| 141 | + } |
| 142 | + |
| 143 | + private Executor find(ExecutorService executorService) { |
| 144 | + for (Executor executor : this.executors) { |
| 145 | + if (executor.executorService.equals(executorService)) { |
| 146 | + return executor; |
| 147 | + } |
| 148 | + } |
| 149 | + return null; |
| 150 | + } |
| 151 | + |
| 152 | + @Override |
| 153 | + public synchronized void close() { |
| 154 | + if (closed.compareAndSet(false, true)) { |
| 155 | + this.executors.forEach(executor -> executor.executorService.shutdownNow()); |
| 156 | + } |
| 157 | + } |
| 158 | + |
| 159 | + static class Executor { |
| 160 | + |
| 161 | + private final ExecutorService executorService; |
| 162 | + private AtomicInteger usage = new AtomicInteger(0); |
| 163 | + |
| 164 | + Executor(ExecutorService executorService) { |
| 165 | + this.executorService = executorService; |
| 166 | + } |
| 167 | + |
| 168 | + Executor incrementUsage() { |
| 169 | + this.usage.incrementAndGet(); |
| 170 | + return this; |
| 171 | + } |
| 172 | + |
| 173 | + Executor decrementUsage() { |
| 174 | + this.usage.decrementAndGet(); |
| 175 | + return this; |
| 176 | + } |
| 177 | + |
| 178 | + Executor addUsage(int delta) { |
| 179 | + this.usage.addAndGet(delta); |
| 180 | + return this; |
| 181 | + } |
| 182 | + |
| 183 | + Executor substractUsage(int delta) { |
| 184 | + this.usage.addAndGet(-delta); |
| 185 | + return this; |
| 186 | + } |
| 187 | + |
| 188 | + private int usage() { |
| 189 | + return this.usage.get(); |
| 190 | + } |
| 191 | + |
| 192 | + private void close() { |
| 193 | + this.executorService.shutdownNow(); |
| 194 | + } |
| 195 | + |
| 196 | + @Override |
| 197 | + public String toString() { |
| 198 | + return "Executor{" + "usage=" + usage.get() + '}'; |
| 199 | + } |
| 200 | + } |
| 201 | +} |
0 commit comments