diff --git a/java/org/apache/catalina/core/StandardWrapperValve.java b/java/org/apache/catalina/core/StandardWrapperValve.java index cfed5bd8ced0..dba5383748d0 100644 --- a/java/org/apache/catalina/core/StandardWrapperValve.java +++ b/java/org/apache/catalina/core/StandardWrapperValve.java @@ -18,7 +18,6 @@ import java.io.IOException; -import java.util.concurrent.atomic.LongAdder; import jakarta.servlet.DispatcherType; import jakarta.servlet.RequestDispatcher; @@ -34,6 +33,7 @@ import org.apache.catalina.connector.ClientAbortException; import org.apache.catalina.connector.Request; import org.apache.catalina.connector.Response; +import org.apache.catalina.util.Counter; import org.apache.catalina.valves.ValveBase; import org.apache.coyote.CloseNowException; import org.apache.tomcat.util.ExceptionUtils; @@ -66,8 +66,8 @@ public StandardWrapperValve() { private volatile long processingTime; private volatile long maxTime; private volatile long minTime = Long.MAX_VALUE; - private final LongAdder requestCount = new LongAdder(); - private final LongAdder errorCount = new LongAdder(); + private Counter requestCount; + private Counter errorCount; // --------------------------------------------------------- Public Methods @@ -294,7 +294,7 @@ public long getMinTime() { * @return the number of requests processed by the associated wrapper. */ public long getRequestCount() { - return requestCount.sum(); + return requestCount.get(); } /** @@ -303,15 +303,32 @@ public long getRequestCount() { * @return the number of requests processed by the associated wrapper that resulted in an error. */ public long getErrorCount() { - return errorCount.sum(); + return errorCount.get(); } public void incrementErrorCount() { errorCount.increment(); } + /** + * Enable/disable the request count and error count. + * + * @param enabled {@code true} if request and error counts are tracked, {@code false} otherwise. + */ + public void setEnableCounters(boolean enabled) { + requestCount = Counter.of(enabled); + errorCount = Counter.of(enabled); + } + @Override protected void initInternal() throws LifecycleException { - // NOOP - Don't register this Valve in JMX + // Don't register this Valve in JMX + + if (requestCount == null) { + requestCount = Counter.of(true); + } + if (errorCount == null) { + errorCount = Counter.of(true); + } } } diff --git a/java/org/apache/catalina/util/Counter.java b/java/org/apache/catalina/util/Counter.java new file mode 100644 index 000000000000..a11977bc7a8f --- /dev/null +++ b/java/org/apache/catalina/util/Counter.java @@ -0,0 +1,57 @@ +/* + * 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.catalina.util; + +import java.util.concurrent.atomic.LongAdder; + +public interface Counter { + void increment(); + long get(); + + static Counter of(boolean enabled) { + return enabled ? new Simple() : Noop.INSTANCE; + } + + class Noop implements Counter { + private static final Counter INSTANCE = new Noop(); + + @Override + public void increment() { + // no-op + } + + @Override + public long get() { + return 0; + } + } + + class Simple implements Counter { + private final LongAdder adder = new LongAdder(); + + @Override + public void increment() { + adder.increment(); + } + + @Override + public long get() { + return adder.sum(); + } + } +} diff --git a/java/org/apache/catalina/webresources/Cache.java b/java/org/apache/catalina/webresources/Cache.java index 4893a289cb17..70f0e43fa68c 100644 --- a/java/org/apache/catalina/webresources/Cache.java +++ b/java/org/apache/catalina/webresources/Cache.java @@ -22,10 +22,10 @@ import java.util.concurrent.ConcurrentHashMap; import java.util.concurrent.ConcurrentMap; import java.util.concurrent.atomic.AtomicLong; -import java.util.concurrent.atomic.LongAdder; import org.apache.catalina.WebResource; import org.apache.catalina.WebResourceRoot.CacheStrategy; +import org.apache.catalina.util.Counter; import org.apache.juli.logging.Log; import org.apache.juli.logging.LogFactory; import org.apache.tomcat.util.res.StringManager; @@ -49,8 +49,8 @@ public class Cache { private int objectMaxSize = (int) maxSize / OBJECT_MAX_SIZE_FACTOR; private CacheStrategy cacheStrategy; - private LongAdder lookupCount = new LongAdder(); - private LongAdder hitCount = new LongAdder(); + private Counter lookupCount = Counter.of(true); + private Counter hitCount = Counter.of(true); private final ConcurrentMap resourceCache = new ConcurrentHashMap<>(); @@ -265,6 +265,11 @@ void removeCacheEntry(String path) { } } + public void setEnableCounters(boolean enabled) { + lookupCount = Counter.of(enabled); + hitCount = Counter.of(enabled); + } + public CacheStrategy getCacheStrategy() { return cacheStrategy; } @@ -292,11 +297,11 @@ public void setMaxSize(long maxSize) { } public long getLookupCount() { - return lookupCount.sum(); + return lookupCount.get(); } public long getHitCount() { - return hitCount.sum(); + return hitCount.get(); } public void setObjectMaxSize(int objectMaxSize) {