|
| 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, software |
| 13 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 14 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 15 | + * See the License for the specific language governing permissions and |
| 16 | + * limitations under the License. |
| 17 | + */ |
| 18 | +package org.apache.hadoop.hbase.logging; |
| 19 | + |
| 20 | +import java.io.Serializable; |
| 21 | +import java.nio.charset.StandardCharsets; |
| 22 | +import java.util.concurrent.atomic.AtomicBoolean; |
| 23 | +import java.util.concurrent.atomic.AtomicLong; |
| 24 | +import org.apache.logging.log4j.core.Appender; |
| 25 | +import org.apache.logging.log4j.core.Core; |
| 26 | +import org.apache.logging.log4j.core.Filter; |
| 27 | +import org.apache.logging.log4j.core.Layout; |
| 28 | +import org.apache.logging.log4j.core.LogEvent; |
| 29 | +import org.apache.logging.log4j.core.appender.AbstractOutputStreamAppender; |
| 30 | +import org.apache.logging.log4j.core.appender.ManagerFactory; |
| 31 | +import org.apache.logging.log4j.core.appender.OutputStreamManager; |
| 32 | +import org.apache.logging.log4j.core.appender.rolling.FileSize; |
| 33 | +import org.apache.logging.log4j.core.config.Property; |
| 34 | +import org.apache.logging.log4j.core.config.plugins.Plugin; |
| 35 | +import org.apache.logging.log4j.core.config.plugins.PluginBuilderAttribute; |
| 36 | +import org.apache.logging.log4j.core.config.plugins.PluginBuilderFactory; |
| 37 | +import org.apache.logging.log4j.core.config.plugins.validation.constraints.Required; |
| 38 | + |
| 39 | +/** |
| 40 | + * Log4j2 appender to be used when running UTs. |
| 41 | + * <p/> |
| 42 | + * The main point here is to limit the total output size to prevent eating all the space of our ci |
| 43 | + * system when something wrong in our code. |
| 44 | + * <p/> |
| 45 | + * See HBASE-26947 for more details. |
| 46 | + */ |
| 47 | +@Plugin(name = HBaseTestAppender.PLUGIN_NAME, category = Core.CATEGORY_NAME, |
| 48 | + elementType = Appender.ELEMENT_TYPE, printObject = true) |
| 49 | +public final class HBaseTestAppender extends AbstractOutputStreamAppender<OutputStreamManager> { |
| 50 | + |
| 51 | + public static final String PLUGIN_NAME = "HBaseTest"; |
| 52 | + private static final HBaseTestManagerFactory FACTORY = new HBaseTestManagerFactory(); |
| 53 | + |
| 54 | + public static class Builder<B extends Builder<B>> extends AbstractOutputStreamAppender.Builder<B> |
| 55 | + implements org.apache.logging.log4j.core.util.Builder<HBaseTestAppender> { |
| 56 | + |
| 57 | + @PluginBuilderAttribute |
| 58 | + @Required |
| 59 | + private Target target; |
| 60 | + |
| 61 | + @PluginBuilderAttribute |
| 62 | + @Required |
| 63 | + private String maxSize; |
| 64 | + |
| 65 | + public B setTarget(Target target) { |
| 66 | + this.target = target; |
| 67 | + return asBuilder(); |
| 68 | + } |
| 69 | + |
| 70 | + public B setMaxSize(String maxSize) { |
| 71 | + this.maxSize = maxSize; |
| 72 | + return asBuilder(); |
| 73 | + } |
| 74 | + |
| 75 | + @Override |
| 76 | + public HBaseTestAppender build() { |
| 77 | + long size = FileSize.parse(maxSize, -1); |
| 78 | + if (size <= 0) { |
| 79 | + LOGGER.error("Invalid maxSize {}", size); |
| 80 | + } |
| 81 | + Layout<? extends Serializable> layout = getOrCreateLayout(StandardCharsets.UTF_8); |
| 82 | + OutputStreamManager manager = |
| 83 | + OutputStreamManager.getManager(target.name(), FACTORY, new FactoryData(target, layout)); |
| 84 | + return new HBaseTestAppender(getName(), |
| 85 | + layout, |
| 86 | + getFilter(), |
| 87 | + isIgnoreExceptions(), |
| 88 | + isImmediateFlush(), |
| 89 | + getPropertyArray(), |
| 90 | + manager, |
| 91 | + size); |
| 92 | + } |
| 93 | + } |
| 94 | + |
| 95 | + /** |
| 96 | + * Data to pass to factory method.Unable to instantiate |
| 97 | + */ |
| 98 | + private static class FactoryData { |
| 99 | + private final Target target; |
| 100 | + private final Layout<? extends Serializable> layout; |
| 101 | + |
| 102 | + public FactoryData(Target target, Layout<? extends Serializable> layout) { |
| 103 | + this.target = target; |
| 104 | + this.layout = layout; |
| 105 | + } |
| 106 | + } |
| 107 | + |
| 108 | + /** |
| 109 | + * Factory to create the Appender. |
| 110 | + */ |
| 111 | + private static class HBaseTestManagerFactory |
| 112 | + implements ManagerFactory<HBaseTestOutputStreamManager, FactoryData> { |
| 113 | + |
| 114 | + /** |
| 115 | + * Create an OutputStreamManager. |
| 116 | + * @param name The name of the entity to manage. |
| 117 | + * @param data The data required to create the entity. |
| 118 | + * @return The OutputStreamManager |
| 119 | + */ |
| 120 | + @Override |
| 121 | + public HBaseTestOutputStreamManager createManager(final String name, final FactoryData data) { |
| 122 | + return new HBaseTestOutputStreamManager(data.target, data.layout); |
| 123 | + } |
| 124 | + } |
| 125 | + |
| 126 | + @PluginBuilderFactory |
| 127 | + public static <B extends Builder<B>> B newBuilder() { |
| 128 | + return new Builder<B>().asBuilder(); |
| 129 | + } |
| 130 | + |
| 131 | + private final long maxSize; |
| 132 | + |
| 133 | + private final AtomicLong size = new AtomicLong(0); |
| 134 | + |
| 135 | + private final AtomicBoolean stop = new AtomicBoolean(false); |
| 136 | + |
| 137 | + private HBaseTestAppender(String name, Layout<? extends Serializable> layout, Filter filter, |
| 138 | + boolean ignoreExceptions, boolean immediateFlush, Property[] properties, |
| 139 | + OutputStreamManager manager, long maxSize) { |
| 140 | + super(name, layout, filter, ignoreExceptions, immediateFlush, properties, manager); |
| 141 | + this.maxSize = maxSize; |
| 142 | + } |
| 143 | + |
| 144 | + @Override |
| 145 | + public void append(LogEvent event) { |
| 146 | + if (stop.get()) { |
| 147 | + return; |
| 148 | + } |
| 149 | + // for accounting, here we always convert the event to byte array first |
| 150 | + // this will effect performance a bit but it is OK since this is for UT only |
| 151 | + byte[] bytes = getLayout().toByteArray(event); |
| 152 | + if (bytes == null || bytes.length == 0) { |
| 153 | + return; |
| 154 | + } |
| 155 | + long sizeAfterAppend = size.addAndGet(bytes.length); |
| 156 | + if (sizeAfterAppend >= maxSize) { |
| 157 | + // stop logging if the log size exceeded the limit |
| 158 | + if (stop.compareAndSet(false, true)) { |
| 159 | + LOGGER.error("Log size exceeded the limit {}, will stop logging to prevent eating" |
| 160 | + + " too much disk space", maxSize); |
| 161 | + } |
| 162 | + return; |
| 163 | + } |
| 164 | + super.append(event); |
| 165 | + } |
| 166 | +} |
0 commit comments