Skip to content

Commit

Permalink
[#noissue] Polishing
Browse files Browse the repository at this point in the history
  • Loading branch information
emeroad committed Oct 28, 2022
1 parent 05d286b commit 8cdb4ea
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 30 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@

package com.navercorp.pinpoint.collector.monitor;

import java.util.ArrayList;
import java.util.List;
import java.util.Objects;
import java.util.concurrent.RejectedExecutionHandler;
Expand All @@ -27,14 +28,9 @@
public class RejectedExecutionHandlerChain implements RejectedExecutionHandler {
private final RejectedExecutionHandler[] handlerChain;

public static RejectedExecutionHandler build(List<RejectedExecutionHandler> chain) {
Objects.requireNonNull(chain, "handlerChain");
RejectedExecutionHandler[] handlerChain = chain.toArray(new RejectedExecutionHandler[0]);
return new RejectedExecutionHandlerChain(handlerChain);
}

private RejectedExecutionHandlerChain(RejectedExecutionHandler[] handlerChain) {
this.handlerChain = Objects.requireNonNull(handlerChain, "handlerChain");
private RejectedExecutionHandlerChain(List<RejectedExecutionHandler> handlerChain) {
Objects.requireNonNull(handlerChain, "handlerChain");
this.handlerChain = handlerChain.toArray(new RejectedExecutionHandler[0]);
}

@Override
Expand All @@ -43,4 +39,20 @@ public void rejectedExecution(Runnable r, ThreadPoolExecutor executor) {
rejectedExecutionHandler.rejectedExecution(r, executor);
}
}

public static class Builder {
private final List<RejectedExecutionHandler> chain = new ArrayList<>();

public Builder() {
}

public void addRejectHandler(RejectedExecutionHandler handler) {
Objects.requireNonNull(handler, "handler");
this.chain.add(handler);
}

public RejectedExecutionHandler build() {
return new RejectedExecutionHandlerChain(chain);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -27,8 +27,6 @@
import com.codahale.metrics.Gauge;
import com.codahale.metrics.MetricRegistry;

import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.BlockingQueue;
import java.util.concurrent.RejectedExecutionHandler;
import java.util.concurrent.ThreadFactory;
Expand Down Expand Up @@ -105,24 +103,21 @@ private ThreadPoolExecutor newMonitoredExecutorService(int corePoolSize, int max

private RejectedExecutionHandler wrapHandlerChain(RejectedExecutionHandler rejectedExecutionHandler) {

final List<RejectedExecutionHandler> handlerList = new ArrayList<>();
RejectedExecutionHandlerChain.Builder builder = new RejectedExecutionHandlerChain.Builder();
if (registry != null) {
RejectedExecutionHandler countingHandler = new CountingRejectedExecutionHandler(beanName, registry);
handlerList.add(countingHandler);
builder.addRejectHandler(countingHandler);
}

if (logRate > -1) {
RejectedExecutionHandler loggingHandler = new LoggingRejectedExecutionHandler(beanName, logRate);
handlerList.add(loggingHandler);
builder.addRejectHandler(loggingHandler);
}

// original exception policy
handlerList.add(rejectedExecutionHandler);
builder.addRejectHandler(rejectedExecutionHandler);

if (handlerList.isEmpty()) {
return rejectedExecutionHandler;
}
return RejectedExecutionHandlerChain.build(handlerList);
return builder.build();
}


Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,7 @@
import com.navercorp.pinpoint.bootstrap.plugin.monitor.metric.CustomMetric;
import com.navercorp.pinpoint.bootstrap.plugin.monitor.metric.LongCounter;

import java.util.ArrayList;
import java.util.Collections;
import java.util.Arrays;
import java.util.List;
import java.util.Objects;

Expand All @@ -33,26 +32,23 @@ public class DefaultCustomMetricRegistryFilter implements CustomMetricRegistryFi
private static final AllowedSource<LongCounter> NETTY_USED_DIRECT_MEMORY = new AllowedSource<>("custom/netty/usedDirectMemory", LongCounter.class);
private static final AllowedSource<LongCounter> NETTY_MAX_DIRECT_MEMORY = new AllowedSource<>("custom/netty/maxDirectMemory ", LongCounter.class);

private final List<AllowedSource> allowedSourceList;
private final AllowedSource<? extends CustomMetric>[] allowedSourceList;

public DefaultCustomMetricRegistryFilter() {
List<AllowedSource> allowedSourceList = new ArrayList<AllowedSource>();
allowedSourceList.add(NETTY_USED_DIRECT_MEMORY);
allowedSourceList.add(NETTY_MAX_DIRECT_MEMORY);

this.allowedSourceList = Collections.unmodifiableList(allowedSourceList);
this(Arrays.asList(NETTY_USED_DIRECT_MEMORY, NETTY_MAX_DIRECT_MEMORY));
}

public DefaultCustomMetricRegistryFilter(List<AllowedSource> allowedSourceList) {
@SuppressWarnings("unchecked")
public DefaultCustomMetricRegistryFilter(List<AllowedSource<? extends CustomMetric>> allowedSourceList) {
Objects.requireNonNull(allowedSourceList, "allowedSourceList");
this.allowedSourceList = Collections.unmodifiableList(allowedSourceList);
this.allowedSourceList = allowedSourceList.toArray(new AllowedSource[0]);
}

@Override
public boolean filter(CustomMetric value) {
Objects.requireNonNull(value, "value");

for (AllowedSource allowedSource : allowedSourceList) {
for (AllowedSource<? extends CustomMetric> allowedSource : allowedSourceList) {
if (contains(allowedSource, value)) {
return NOT_FILTERED;
}
Expand All @@ -61,7 +57,7 @@ public boolean filter(CustomMetric value) {
}


private boolean contains(AllowedSource allowedSource, CustomMetric value) {
private boolean contains(AllowedSource<? extends CustomMetric> allowedSource, CustomMetric value) {
if (!allowedSource.getMetricName().equals(value.getName())) {
return false;
}
Expand Down

0 comments on commit 8cdb4ea

Please sign in to comment.