forked from apache/solr
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
SOLR-16968: The MemoryCircuitBreaker now uses average heap usage (apa…
- Loading branch information
Showing
8 changed files
with
253 additions
and
45 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
80 changes: 80 additions & 0 deletions
80
solr/core/src/java/org/apache/solr/util/circuitbreaker/AveragingMetricProvider.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,80 @@ | ||
/* | ||
* 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.solr.util.circuitbreaker; | ||
|
||
import com.google.common.util.concurrent.AtomicDouble; | ||
import java.io.Closeable; | ||
import java.util.concurrent.Executors; | ||
import java.util.concurrent.ScheduledExecutorService; | ||
import java.util.concurrent.TimeUnit; | ||
import org.apache.solr.common.util.ExecutorUtil; | ||
import org.apache.solr.common.util.SolrNamedThreadFactory; | ||
import org.apache.solr.logging.CircularList; | ||
|
||
/** Averages the metric value over a period of time */ | ||
public class AveragingMetricProvider implements Closeable { | ||
private final CircularList<Double> samplesRingBuffer; | ||
private ScheduledExecutorService executor; | ||
private final AtomicDouble currentAverageValue = new AtomicDouble(-1); | ||
|
||
/** | ||
* Creates an instance with an executor that runs every sampleInterval seconds and averages over | ||
* numSamples samples. | ||
* | ||
* @param metricProvider metric provider that will provide a value | ||
* @param numSamples number of samples to calculate average for | ||
* @param sampleInterval interval between each sample | ||
*/ | ||
public AveragingMetricProvider( | ||
MetricProvider metricProvider, int numSamples, long sampleInterval) { | ||
this.samplesRingBuffer = new CircularList<>(numSamples); | ||
executor = | ||
Executors.newSingleThreadScheduledExecutor( | ||
new SolrNamedThreadFactory( | ||
"AveragingMetricProvider-" + metricProvider.getClass().getSimpleName())); | ||
executor.scheduleWithFixedDelay( | ||
() -> { | ||
samplesRingBuffer.add(metricProvider.getMetricValue()); | ||
currentAverageValue.set( | ||
samplesRingBuffer.toList().stream() | ||
.mapToDouble(Double::doubleValue) | ||
.average() | ||
.orElse(-1)); | ||
}, | ||
0, | ||
sampleInterval, | ||
TimeUnit.SECONDS); | ||
} | ||
|
||
/** | ||
* Return current average. This is a cached value, so calling this method will not incur any | ||
* calculations | ||
*/ | ||
public double getMetricValue() { | ||
return currentAverageValue.get(); | ||
} | ||
|
||
@Override | ||
public void close() { | ||
ExecutorUtil.shutdownAndAwaitTermination(executor); | ||
} | ||
|
||
/** Interface to provide the metric value. */ | ||
public interface MetricProvider { | ||
double getMetricValue(); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.