|
| 1 | +/* |
| 2 | + * Licensed to the Apache Software Foundation (ASF) under one or more |
| 3 | + * contributor license agreements. See the NOTICE file distributed with |
| 4 | + * this work for additional information regarding copyright ownership. |
| 5 | + * The ASF licenses this file to you under the Apache License, Version 2.0 |
| 6 | + * (the "License"); you may not use this file except in compliance with |
| 7 | + * the License. You may obtain a copy of the License at |
| 8 | + * |
| 9 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 10 | + * |
| 11 | + * Unless required by applicable law or agreed to in writing, software |
| 12 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 13 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 14 | + * See the License for the specific language governing permissions and |
| 15 | + * limitations under the License. |
| 16 | + */ |
| 17 | +package org.apache.logging.log4j.conversant; |
| 18 | + |
| 19 | +import static java.util.Objects.requireNonNull; |
| 20 | + |
| 21 | +import aQute.bnd.annotation.spi.ServiceProvider; |
| 22 | +import com.conversantmedia.util.concurrent.DisruptorBlockingQueue; |
| 23 | +import com.conversantmedia.util.concurrent.SpinPolicy; |
| 24 | +import java.util.Queue; |
| 25 | +import java.util.function.Consumer; |
| 26 | +import java.util.function.Supplier; |
| 27 | +import org.apache.logging.log4j.Logger; |
| 28 | +import org.apache.logging.log4j.core.util.Integers; |
| 29 | +import org.apache.logging.log4j.kit.env.PropertyEnvironment; |
| 30 | +import org.apache.logging.log4j.kit.recycler.Recycler; |
| 31 | +import org.apache.logging.log4j.kit.recycler.RecyclerFactory; |
| 32 | +import org.apache.logging.log4j.kit.recycler.RecyclerFactoryProvider; |
| 33 | +import org.apache.logging.log4j.kit.recycler.RecyclerProperties; |
| 34 | +import org.apache.logging.log4j.kit.recycler.support.AbstractRecycler; |
| 35 | +import org.apache.logging.log4j.status.StatusLogger; |
| 36 | + |
| 37 | +/** |
| 38 | + * A {@link Recycler} factory provider implementation based on |
| 39 | + * <a href="https://github.com/conversant/disruptor>Conversant's Disruptor BlockingQueue</a>. |
| 40 | + * |
| 41 | + * @since 3.0.0 |
| 42 | + */ |
| 43 | +@ServiceProvider(value = RecyclerFactoryProvider.class) |
| 44 | +public final class DisruptorRecyclerFactoryProvider implements RecyclerFactoryProvider { |
| 45 | + |
| 46 | + private static final Logger LOGGER = StatusLogger.getLogger(); |
| 47 | + |
| 48 | + @Override |
| 49 | + public int getOrder() { |
| 50 | + return 600; |
| 51 | + } |
| 52 | + |
| 53 | + @Override |
| 54 | + public String getName() { |
| 55 | + return "conversant-disruptor"; |
| 56 | + } |
| 57 | + |
| 58 | + @Override |
| 59 | + public RecyclerFactory createForEnvironment(final PropertyEnvironment environment) { |
| 60 | + requireNonNull(environment, "environment"); |
| 61 | + return new DisruptorRecyclerFactory( |
| 62 | + environment.getProperty(RecyclerProperties.class), |
| 63 | + environment.getProperty(DisruptorRecyclerProperties.class)); |
| 64 | + } |
| 65 | + |
| 66 | + private static final class DisruptorRecyclerFactory implements RecyclerFactory { |
| 67 | + |
| 68 | + /** |
| 69 | + * Minimum capacity of the disruptor |
| 70 | + */ |
| 71 | + private static final int MIN_CAPACITY = 8; |
| 72 | + |
| 73 | + private final int capacity; |
| 74 | + private final SpinPolicy spinPolicy; |
| 75 | + |
| 76 | + private DisruptorRecyclerFactory( |
| 77 | + final RecyclerProperties recyclerProperties, final DisruptorRecyclerProperties disruptorProperties) { |
| 78 | + this.capacity = validateCapacity(recyclerProperties.capacity()); |
| 79 | + this.spinPolicy = disruptorProperties.spinPolicy(); |
| 80 | + } |
| 81 | + |
| 82 | + @Override |
| 83 | + public <V> Recycler<V> create(final Supplier<V> supplier, final Consumer<V> cleaner) { |
| 84 | + requireNonNull(supplier, "supplier"); |
| 85 | + requireNonNull(cleaner, "cleaner"); |
| 86 | + final DisruptorBlockingQueue<V> queue = new DisruptorBlockingQueue<>(capacity, spinPolicy); |
| 87 | + return new DisruptorRecycler<>(supplier, cleaner, queue); |
| 88 | + } |
| 89 | + |
| 90 | + private static Integer validateCapacity(final int capacity) { |
| 91 | + if (capacity < MIN_CAPACITY) { |
| 92 | + LOGGER.warn( |
| 93 | + "Invalid DisruptorBlockingQueue capacity {}, using minimum size {}.", capacity, MIN_CAPACITY); |
| 94 | + return MIN_CAPACITY; |
| 95 | + } |
| 96 | + final int roundedCapacity = Integers.ceilingNextPowerOfTwo(capacity); |
| 97 | + if (capacity != roundedCapacity) { |
| 98 | + LOGGER.warn( |
| 99 | + "Invalid DisruptorBlockingQueue size {}, using rounded size {}.", capacity, roundedCapacity); |
| 100 | + } |
| 101 | + return roundedCapacity; |
| 102 | + } |
| 103 | + |
| 104 | + private static final class DisruptorRecycler<V> extends AbstractRecycler<V> { |
| 105 | + |
| 106 | + private final Consumer<V> cleaner; |
| 107 | + |
| 108 | + private final Queue<V> queue; |
| 109 | + |
| 110 | + private DisruptorRecycler(final Supplier<V> supplier, final Consumer<V> cleaner, final Queue<V> queue) { |
| 111 | + super(supplier); |
| 112 | + this.cleaner = cleaner; |
| 113 | + this.queue = queue; |
| 114 | + } |
| 115 | + |
| 116 | + @Override |
| 117 | + public V acquire() { |
| 118 | + final V value = queue.poll(); |
| 119 | + return value != null ? value : createInstance(); |
| 120 | + } |
| 121 | + |
| 122 | + @Override |
| 123 | + public void release(final V value) { |
| 124 | + requireNonNull(value, "value"); |
| 125 | + cleaner.accept(value); |
| 126 | + queue.offer(value); |
| 127 | + } |
| 128 | + } |
| 129 | + } |
| 130 | +} |
0 commit comments