cshannon commented on code in PR #4558:
URL: https://github.com/apache/accumulo/pull/4558#discussion_r1605377744


##########
core/src/main/java/org/apache/accumulo/core/logging/ConditionalLogger.java:
##########
@@ -0,0 +1,197 @@
+/*
+ * 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
+ *
+ *   https://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.accumulo.core.logging;
+
+import java.time.Duration;
+import java.util.Arrays;
+import java.util.List;
+import java.util.function.BiFunction;
+
+import org.slf4j.Logger;
+import org.slf4j.Marker;
+import org.slf4j.event.Level;
+import org.slf4j.helpers.AbstractLogger;
+
+import com.github.benmanes.caffeine.cache.Cache;
+import com.github.benmanes.caffeine.cache.Caffeine;
+
+/**
+ * Logger that wraps another Logger and only emits a log message once per the 
supplied duration.
+ *
+ */
+public abstract class ConditionalLogger extends AbstractLogger {
+
+  private static final long serialVersionUID = 1L;
+
+  /**
+   * A Logger implementation that will log a message at the supplied elevated 
level if it has not
+   * been seen in the supplied duration. For repeat occurrences the message 
will be logged at the
+   * level used in code (which is likely a lower level). Note that the first 
log message will be
+   * logged at the elevated level because it has not been seen before.
+   */
+  public static class EscalatingLogger extends DeduplicatingLogger {
+
+    private static final long serialVersionUID = 1L;
+    private final Level elevatedLevel;
+
+    public EscalatingLogger(Logger log, Duration threshold, Level 
elevatedLevel) {
+      super(log, threshold);
+      this.elevatedLevel = elevatedLevel;
+    }
+
+    @Override
+    protected void handleNormalizedLoggingCall(Level level, Marker marker, 
String messagePattern,
+        Object[] arguments, Throwable throwable) {
+
+      if (arguments == null) {
+        arguments = new Object[0];
+      }
+      if (!condition.apply(messagePattern, Arrays.asList(arguments))) {
+        
delegate.atLevel(level).addMarker(marker).setCause(throwable).log(messagePattern,
+            arguments);
+      } else {
+        
delegate.atLevel(elevatedLevel).addMarker(marker).setCause(throwable).log(messagePattern,
+            arguments);
+      }
+
+    }
+
+  }
+
+  /**
+   * A Logger implementation that will suppress duplicate messages within the 
supplied duration.
+   */
+  public static class DeduplicatingLogger extends ConditionalLogger {
+
+    private static final long serialVersionUID = 1L;
+
+    public DeduplicatingLogger(Logger log, Duration threshold) {
+      super(log, new BiFunction<>() {
+
+        private final Cache<String,List<Object>> cache =
+            
Caffeine.newBuilder().expireAfterWrite(threshold).weakKeys().weakValues().build();
+
+        /**
+         * Function that will return true if the message has not been seen in 
the supplied duration.
+         *
+         * @param msg log message
+         * @param args log message arguments
+         * @return true if message has not been seen in duration, else false.
+         */
+        @Override
+        public Boolean apply(String msg, List<Object> args) {
+
+          // WeakKeys will perform == check, this should work?
+          List<Object> storedArgs = cache.getIfPresent(msg);

Review Comment:
   This has a race condition the way it is written and requires external 
synchronization since another thread could change the cache in between the read 
and the put.  
   
   I think you would need to either sync here or write it differently. You 
could do something like use the ConcurrentMap api and call putIfAbsent which 
will return null if there was no previous mapping so you know that it was the 
first time. If you did that then you'd probably need to do combine the msg and 
args into the key and just use a dummy value.
   
   Something like:
   
   ```java
   
   Cache<Pair<String, List<Object>>,Boolean> = ....
   
   @Override
   public Boolean apply(String msg, List<Object> args) {
     Pair<String, List<Object>> cacheVal = new Pair<>(msg, args);
     return cache.asMap().putIfAbsent(cacheVal, true) == null;
   }
   ```
   
   Although it would maybe be best to store something else besides a 
List<Object>, maybe List<String> for the equality check as I mention in my 
other comment.
   
   
   



##########
core/src/main/java/org/apache/accumulo/core/logging/ConditionalLogger.java:
##########
@@ -0,0 +1,197 @@
+/*
+ * 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
+ *
+ *   https://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.accumulo.core.logging;
+
+import java.time.Duration;
+import java.util.Arrays;
+import java.util.List;
+import java.util.function.BiFunction;
+
+import org.slf4j.Logger;
+import org.slf4j.Marker;
+import org.slf4j.event.Level;
+import org.slf4j.helpers.AbstractLogger;
+
+import com.github.benmanes.caffeine.cache.Cache;
+import com.github.benmanes.caffeine.cache.Caffeine;
+
+/**
+ * Logger that wraps another Logger and only emits a log message once per the 
supplied duration.
+ *
+ */
+public abstract class ConditionalLogger extends AbstractLogger {
+
+  private static final long serialVersionUID = 1L;
+
+  /**
+   * A Logger implementation that will log a message at the supplied elevated 
level if it has not
+   * been seen in the supplied duration. For repeat occurrences the message 
will be logged at the
+   * level used in code (which is likely a lower level). Note that the first 
log message will be
+   * logged at the elevated level because it has not been seen before.
+   */
+  public static class EscalatingLogger extends DeduplicatingLogger {
+
+    private static final long serialVersionUID = 1L;
+    private final Level elevatedLevel;
+
+    public EscalatingLogger(Logger log, Duration threshold, Level 
elevatedLevel) {
+      super(log, threshold);
+      this.elevatedLevel = elevatedLevel;
+    }
+
+    @Override
+    protected void handleNormalizedLoggingCall(Level level, Marker marker, 
String messagePattern,
+        Object[] arguments, Throwable throwable) {
+
+      if (arguments == null) {
+        arguments = new Object[0];
+      }
+      if (!condition.apply(messagePattern, Arrays.asList(arguments))) {
+        
delegate.atLevel(level).addMarker(marker).setCause(throwable).log(messagePattern,
+            arguments);
+      } else {
+        
delegate.atLevel(elevatedLevel).addMarker(marker).setCause(throwable).log(messagePattern,
+            arguments);
+      }
+
+    }
+
+  }
+
+  /**
+   * A Logger implementation that will suppress duplicate messages within the 
supplied duration.
+   */
+  public static class DeduplicatingLogger extends ConditionalLogger {
+
+    private static final long serialVersionUID = 1L;
+
+    public DeduplicatingLogger(Logger log, Duration threshold) {
+      super(log, new BiFunction<>() {
+
+        private final Cache<String,List<Object>> cache =
+            
Caffeine.newBuilder().expireAfterWrite(threshold).weakKeys().weakValues().build();
+
+        /**
+         * Function that will return true if the message has not been seen in 
the supplied duration.
+         *
+         * @param msg log message
+         * @param args log message arguments
+         * @return true if message has not been seen in duration, else false.
+         */
+        @Override
+        public Boolean apply(String msg, List<Object> args) {
+
+          // WeakKeys will perform == check, this should work?
+          List<Object> storedArgs = cache.getIfPresent(msg);
+
+          if (storedArgs == null || !storedArgs.equals(args)) {

Review Comment:
   I'm not sure if the equality check here would work correctly if the 
different objects have not implemented equals. You migtht have different 
references. I wonder if you would need to go through the args list call 
toString() or something to make sure you could compare (after all toString() is 
what is used when logging anyways)



##########
core/src/main/java/org/apache/accumulo/core/logging/ConditionalLogger.java:
##########
@@ -0,0 +1,197 @@
+/*
+ * 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
+ *
+ *   https://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.accumulo.core.logging;
+
+import java.time.Duration;
+import java.util.Arrays;
+import java.util.List;
+import java.util.function.BiFunction;
+
+import org.slf4j.Logger;
+import org.slf4j.Marker;
+import org.slf4j.event.Level;
+import org.slf4j.helpers.AbstractLogger;
+
+import com.github.benmanes.caffeine.cache.Cache;
+import com.github.benmanes.caffeine.cache.Caffeine;
+
+/**
+ * Logger that wraps another Logger and only emits a log message once per the 
supplied duration.
+ *
+ */
+public abstract class ConditionalLogger extends AbstractLogger {
+
+  private static final long serialVersionUID = 1L;
+
+  /**
+   * A Logger implementation that will log a message at the supplied elevated 
level if it has not
+   * been seen in the supplied duration. For repeat occurrences the message 
will be logged at the
+   * level used in code (which is likely a lower level). Note that the first 
log message will be
+   * logged at the elevated level because it has not been seen before.
+   */
+  public static class EscalatingLogger extends DeduplicatingLogger {
+
+    private static final long serialVersionUID = 1L;
+    private final Level elevatedLevel;
+
+    public EscalatingLogger(Logger log, Duration threshold, Level 
elevatedLevel) {
+      super(log, threshold);
+      this.elevatedLevel = elevatedLevel;
+    }
+
+    @Override
+    protected void handleNormalizedLoggingCall(Level level, Marker marker, 
String messagePattern,
+        Object[] arguments, Throwable throwable) {
+
+      if (arguments == null) {
+        arguments = new Object[0];
+      }
+      if (!condition.apply(messagePattern, Arrays.asList(arguments))) {
+        
delegate.atLevel(level).addMarker(marker).setCause(throwable).log(messagePattern,
+            arguments);
+      } else {
+        
delegate.atLevel(elevatedLevel).addMarker(marker).setCause(throwable).log(messagePattern,
+            arguments);
+      }
+
+    }
+
+  }
+
+  /**
+   * A Logger implementation that will suppress duplicate messages within the 
supplied duration.
+   */
+  public static class DeduplicatingLogger extends ConditionalLogger {
+
+    private static final long serialVersionUID = 1L;
+
+    public DeduplicatingLogger(Logger log, Duration threshold) {
+      super(log, new BiFunction<>() {
+
+        private final Cache<String,List<Object>> cache =

Review Comment:
   I don't think weakKeys() and weakValues() are what you would want to use 
here as it will evict if no more references. If you want to remove when low on 
memory you could look at using soft references instead.
   
   But generally speaking though I think it's a better idea to simply put a 
maximum size on the cache and avoid weak/soft keys and references, there's more 
info about it here:  
https://github.com/ben-manes/caffeine/wiki/Eviction#reference-based



-- 
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]

Reply via email to