Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

djain.metrics-for-blacklist-rules-usage #701

Closed
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,10 @@
*/
package com.demandware.carbonj.service.engine;

import org.apache.commons.lang3.tuple.Pair;
import com.codahale.metrics.Counter;
import com.codahale.metrics.MetricRegistry;
import com.demandware.carbonj.service.db.util.StatsAware;
import com.demandware.carbonj.service.strings.StringsCache;
import com.google.common.base.Charsets;
import com.google.common.base.Preconditions;
import com.google.re2j.Pattern;
Expand Down Expand Up @@ -37,18 +37,23 @@ public class MetricList implements StatsAware

volatile private List<String> configLines = new ArrayList<>( );

volatile private List<Pattern> patterns = new ArrayList<>( );
volatile private List<Pair<Pattern, Counter>> patternsAndCounters = new ArrayList<>( );

volatile private boolean empty = patternsAndCounters.isEmpty();

private final String confSrc;

private final ConfigServerUtil configServerUtil;

private final MetricRegistry metricRegistry;

public MetricList(MetricRegistry metricRegistry, String name, File confFile, String confSrc,
ConfigServerUtil configServerUtil )
ConfigServerUtil configServerUtil )
{
this.name = Preconditions.checkNotNull(name);
this.confFile = Preconditions.checkNotNull( confFile );
log.info( String.format("Creating metric list [%s] with config file [%s]", name, confFile) );
this.metricRegistry = metricRegistry;
this.droppedMetrics = metricRegistry.counter( MetricRegistry.name( name, "drop" ) );
this.confSrc = confSrc;
this.configServerUtil = configServerUtil;
Expand All @@ -58,42 +63,41 @@ public MetricList(MetricRegistry metricRegistry, String name, File confFile, St

public boolean match(String name)
{
if ( patterns.isEmpty() )
if ( patternsAndCounters.isEmpty() )
{
return false;
}

StringsCache.State state = StringsCache.getState(name);
boolean isBlackListed = false;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@diwanshu-crm You removed all my caching code.

if (state != null && state.getBlackListed() != null) {
isBlackListed = state.getBlackListed();
if (isBlackListed) {
droppedMetrics.inc();
}
return isBlackListed;
}

List<Pattern> currentPatterns = patterns; // copy so we don't keep hitting the volatile barrier
for ( Pattern p : currentPatterns )
List<Pair<Pattern, Counter>> currentPatternsAndCounters = patternsAndCounters; // copy so we don't keep hitting the volatile barrier
for ( int i = 0; i < currentPatternsAndCounters.size(); i++ )
{
if( ".*".equals( p.pattern() ) )
Pair<Pattern, Counter> p = currentPatternsAndCounters.get(i);

if( ".*".equals( p.getLeft().pattern() ) )
{
patternsAndCounters.get(i).getRight().inc();
droppedMetrics.inc();
isBlackListed = true;
break;
return true;
}

if ( p.matcher( name ).find() )
long startTime = 0;
if (log.isDebugEnabled()) {
startTime = System.nanoTime(); // Record start time
}
if ( p.getLeft().matcher( name ).find() )
{
patternsAndCounters.get(i).getRight().inc();
droppedMetrics.inc();
isBlackListed = true;
break;
if (log.isDebugEnabled()) {
long endTime = System.nanoTime(); // Record end time
long duration = endTime - startTime; // Calculate duration in nanoseconds
log.debug("Pattern match runtime for {}: {} nanoseconds", p.getLeft().pattern(), duration);
}
return true;
}

}
if (state != null) {
state.setBlackListed(isBlackListed);
}
return isBlackListed;
return false;
}

public void reload()
Expand All @@ -113,7 +117,7 @@ public void reload()
}
lines = FileUtils.readLines(confFile, Charsets.UTF_8);
} else if (confSrc.equalsIgnoreCase("server")) {
if (configServerUtil == null || configServerUtil.getConfigLines(name).isEmpty()) {
if (configServerUtil == null || !configServerUtil.getConfigLines(name).isPresent()) {
log.warn("Unable to read metric list configuration from config server. Falling back to file.");
if (!confFile.exists()) {
log.warn(String.format("Metric list [%s] configuration file doesn't exist. File: [%s]", name, confFile));
Expand All @@ -136,10 +140,9 @@ public void reload()
log.info(String.format("Metric list [%s] configuration file has changed. File: [%s]", name, confFile));

List<String> oldLines = this.configLines;

this.patterns = parseConfig( lines );
List<Pair<Pattern, Counter>> newPatternsAndCounters = parseConfig( lines );
this.patternsAndCounters = newPatternsAndCounters;
this.configLines = lines;
StringsCache.invalidateCache();
log.info(String.format("Metric list [%s] updated.", name));
if( log.isDebugEnabled() )
{
Expand All @@ -152,12 +155,24 @@ public void reload()
}
}

private List<Pattern> parseConfig(List<String> lines)
private List<Pair<Pattern, Counter>> parseConfig(List<String> lines)
{
return lines.stream()
.map( String::trim )
.filter( line -> line.length() > 0 && !line.startsWith( "#" ) )
.map( Pattern::compile ).collect( Collectors.toList() );
// Create an empty list to hold pairs of Pattern and Counter
List<Pair<Pattern, Counter>> patternCounterPairs = lines.stream()
.map(String::trim)
.filter(line -> line.length() > 0 && !line.startsWith("#"))
.map(line -> {
Pattern pattern = Pattern.compile(line);
Counter counter = metricRegistry.counter( MetricRegistry.name( name, "blacklist" ) );
return Pair.of(pattern, counter); // Create and return the Pair
})
.collect(Collectors.toList());

// Reset the counters here if needed
for (Pair<Pattern, Counter> pair : patternCounterPairs) {
pair.getValue().dec(pair.getValue().getCount()); // Reset the counter to zero
}
return patternCounterPairs;
}

@Override
Expand All @@ -168,6 +183,6 @@ public void dumpStats()

public boolean isEmpty()
{
return patterns.isEmpty();
return patternsAndCounters.isEmpty();
}
}
}
Loading