SzyWilliam commented on code in PR #997: URL: https://github.com/apache/ratis/pull/997#discussion_r1433368525
########## ratis-common/src/main/java/org/apache/ratis/util/BatchLogger.java: ########## @@ -0,0 +1,130 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.apache.ratis.util; + +import org.apache.ratis.thirdparty.com.google.common.cache.Cache; +import org.apache.ratis.thirdparty.com.google.common.cache.CacheBuilder; +import org.apache.ratis.thirdparty.com.google.common.collect.Lists; +import org.slf4j.Logger; + +import java.util.List; +import java.util.concurrent.ExecutionException; +import java.util.concurrent.TimeUnit; +import java.util.concurrent.atomic.AtomicBoolean; +import java.util.concurrent.atomic.AtomicInteger; +import java.util.function.Consumer; + +public final class BatchLogger { + + private BatchLogger() { + } + + public final static class UniqueId { + private final List<Object> components; + private UniqueId(Object... components) { + this.components = Lists.newArrayList(components); + } + + @Override + public boolean equals(Object obj) { + if (!(obj instanceof UniqueId)) { + return false; + } + + return components.equals(((UniqueId) obj).components); + } + + @Override + public int hashCode() { + return components.hashCode(); + } + } + + public static UniqueId makeUniqueId(Object... components) { + return new UniqueId(components); + } + + private final static class BatchedLogEntry { + private final Consumer<String> log; + private final AtomicInteger count; + private final Runnable logOp; + private final AtomicBoolean batchStarted; + private Timestamp startTime; + + private BatchedLogEntry(Consumer<String> log, Runnable logOp) { + this.log = log; + this.logOp = logOp; + this.count = new AtomicInteger(); + this.startTime = Timestamp.currentTime(); + this.batchStarted = new AtomicBoolean(false); + } + + private void increaseCount() { + this.count.incrementAndGet(); + } + + private synchronized void execute() { + log.accept(String.format("Received %s logs of following between %s and %s:", + count.get(), startTime, Timestamp.currentTime())); + logOp.run(); + batchStarted.set(false); + } + + private synchronized boolean tryStartBatch() { + if (batchStarted.compareAndSet(false, true)) { + count.set(0); + startTime = Timestamp.currentTime(); + return true; + } + return false; + } + } + + private static final TimeoutExecutor SCHEDULER = TimeoutExecutor.getInstance(); + private static final Cache<UniqueId, BatchedLogEntry> LOG_CACHE = + CacheBuilder.newBuilder() + .expireAfterAccess(1, TimeUnit.MINUTES) + .build(); + + public static void warn(Logger log, UniqueId id, Runnable op, TimeDuration batchDuration) { + warn(log, id, op, batchDuration, true); + } + + public static void warn(Logger log, UniqueId id, Runnable op, TimeDuration batchDuration, boolean shouldBatch) { + if (!shouldBatch || batchDuration.equals(TimeDuration.ZERO)) { Review Comment: Done -- This is an automated message from the Apache Git Service. To respond to the message, please log on to GitHub and use the URL above to go to the specific comment. To unsubscribe, e-mail: [email protected] For queries about this service, please contact Infrastructure at: [email protected]
