Skip to content

Commit

Permalink
#8 Auto filter logic
Browse files Browse the repository at this point in the history
  • Loading branch information
mjok committed Jun 29, 2020
1 parent 29adaf8 commit 39440c4
Show file tree
Hide file tree
Showing 6 changed files with 66 additions and 15 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -21,14 +21,24 @@
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.concurrent.Executors;
import java.util.concurrent.ScheduledExecutorService;
import java.util.concurrent.TimeUnit;
import java.util.stream.Collectors;

import com.jkoolcloud.remora.advices.BaseTransformers;
import com.jkoolcloud.remora.advices.RemoraAdvice;
import com.jkoolcloud.remora.filters.AdviceFilter;
import com.jkoolcloud.remora.filters.FilterManager;
import com.jkoolcloud.remora.filters.LimitingFilter;

public enum AdviceRegistry {
INSTANCE;

public static final String AUTO_LIMITING_FILTER = "AUTO_LIMITING_FILTER";
public static final int FILTER_ADVANCE = 2;
public static final int INITIAL_FILTER_EVERY_N_TH = 256;
public static final int RELEASE_TIME_SEC = 60;
private List<RemoraAdvice> adviceList = new ArrayList<>(50);
private Map<String, RemoraAdvice> adviceMap = new HashMap<>(50);

Expand Down Expand Up @@ -60,4 +70,32 @@ public BaseTransformers getBaseTransformerByName(String name) throws ClassNotFou
}
}

public static void limit() {
LimitingFilter limitingFilter = (LimitingFilter) FilterManager.INSTANCE.get(AUTO_LIMITING_FILTER);
if (limitingFilter == null) {

limitingFilter = new LimitingFilter();
limitingFilter.everyNth = INITIAL_FILTER_EVERY_N_TH * FILTER_ADVANCE;
FilterManager.INSTANCE.add(AUTO_LIMITING_FILTER, limitingFilter);
}
AdviceFilter filter = FilterManager.INSTANCE.get(AUTO_LIMITING_FILTER);
AdviceRegistry.INSTANCE.adviceList.stream()
.filter(advice -> !((BaseTransformers) advice).filters.contains(filter))
.forEach(advice -> ((BaseTransformers) advice).filters.add(filter));

((LimitingFilter) limitingFilter).everyNth /= FILTER_ADVANCE;
ScheduledExecutorService executorService = Executors.newSingleThreadScheduledExecutor();

executorService.scheduleAtFixedRate(AdviceRegistry::release, 0, RELEASE_TIME_SEC, TimeUnit.SECONDS);
}

public static void release() {
LimitingFilter limitingFilter = (LimitingFilter) FilterManager.INSTANCE.get(AUTO_LIMITING_FILTER);
limitingFilter.everyNth *= FILTER_ADVANCE;
if (limitingFilter.everyNth > INITIAL_FILTER_EVERY_N_TH) {
AdviceRegistry.INSTANCE.adviceList.stream().map(advice -> (BaseTransformers) advice)
.forEach(advice -> advice.filters.remove(limitingFilter));
}
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -29,8 +29,8 @@
import com.jkoolcloud.remora.AdviceRegistry;
import com.jkoolcloud.remora.RemoraConfig;
import com.jkoolcloud.remora.core.CallStack;
import com.jkoolcloud.remora.core.EmptyStack;
import com.jkoolcloud.remora.core.EntryDefinition;
import com.jkoolcloud.remora.core.FilteredOutEntryStack;
import com.jkoolcloud.remora.core.output.OutputManager;
import com.jkoolcloud.remora.filters.AdviceFilter;

Expand Down Expand Up @@ -150,7 +150,7 @@ public static void fillDefaultValuesAfter(EntryDefinition entryDefinition, long
entryDefinition.addProperty("PARENT", lastEntryDefinition.getId());
}
}
if (!entryDefinition.isTransparent() && !(stackThreadLocal.get() instanceof FilteredOutEntryStack)) {
if (!entryDefinition.isTransparent() && !(stackThreadLocal.get() instanceof EmptyStack)) {
OutputManager.send(entryDefinition);
}
}
Expand All @@ -167,7 +167,6 @@ public static void handleInstrumentedMethodException(EntryDefinition entryDefini

public static long fillDefaultValuesBefore(EntryDefinition entryDefinition,
ThreadLocal<CallStack<EntryDefinition>> stackThreadLocal, Object thiz, Method method, TaggedLogger logger) {
invokeOnIntercept(entryDefinition.getAdviceClassClass(), thiz, method);
if (entryDefinition.isChained()) {
return 0;
}
Expand Down Expand Up @@ -201,7 +200,7 @@ public static long fillDefaultValuesBefore(EntryDefinition entryDefinition,
if (getAdviceInstance(entryDefinition.getAdviceClassClass()).sendStackTrace) {
entryDefinition.setStackTrace(getStackTrace());
}
if (!entryDefinition.isTransparent() || !(stackThreadLocal.get() instanceof FilteredOutEntryStack)) {
if (!entryDefinition.isTransparent() || !(stackThreadLocal.get() instanceof EmptyStack)) {
OutputManager.send(entryDefinition);
}
} catch (Throwable t) {
Expand Down Expand Up @@ -395,14 +394,18 @@ private static <T extends BaseTransformers> T getAdviceInstance(Class<T> tClass)

public static boolean intercept(Class<? extends BaseTransformers> tClass, Object thiz, Method method,
TaggedLogger logger, Object... arguments) {
invokeOnIntercept(tClass, thiz, method);
if (stackThreadLocal.get() instanceof EmptyStack) {
return false;
}
if (!getAdviceInstance(tClass).enabled) {
return false;
}
for (AdviceFilter filter : getAdviceInstance(tClass).filters) {
if (!filter.intercept(thiz, method, arguments)) {
if (filter.excludeWholeStack()) {
if (stackThreadLocal.get() == null || stackThreadLocal.get().isEmpty()) {
stackThreadLocal.set(new FilteredOutEntryStack(logger, callStackLimit));
stackThreadLocal.set(new EmptyStack(logger, callStackLimit));
}
}
return false;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,8 @@

import org.tinylog.TaggedLogger;

public class FilteredOutEntryStack extends CallStack {
public FilteredOutEntryStack(TaggedLogger logger, int limit) {
public class EmptyStack extends CallStack {
public EmptyStack(TaggedLogger logger, int limit) {
super(logger, limit);
push(new EntryDefinition.DummyEntryDefinition());
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
import org.tinylog.Logger;
import org.tinylog.TaggedLogger;

import com.jkoolcloud.remora.AdviceRegistry;
import com.jkoolcloud.remora.Remora;
import com.jkoolcloud.remora.RemoraConfig;
import com.jkoolcloud.remora.core.EntryDefinition;
Expand Down Expand Up @@ -123,8 +124,11 @@ public void onReleased(int cycle, File file) {
}

queueWorkers = new ThreadPoolExecutor(workerSize, workerSize, 0, TimeUnit.MILLISECONDS,
new ArrayBlockingQueue<>(intermediateQueueSize), threadFactory,
(r, executor) -> ScheduledQueueErrorReporter.intermediateQueueFailCount.incrementAndGet());
new ArrayBlockingQueue<>(intermediateQueueSize), threadFactory, (r, executor) -> {
ScheduledQueueErrorReporter.intermediateQueueFailCount.incrementAndGet();
logger.warn("Limiting advices, Overfilled queue");
AdviceRegistry.limit();
});

}

Expand Down Expand Up @@ -165,7 +169,7 @@ public ExcerptAppender getAppender() {
public ChronicleAppenderThread(Runnable r, ExcerptAppender appender) {
super(r);

threadAppender = appender;
threadAppender = appender;
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,12 @@ public void add(String filterName, StatisticEnabledFilter filter) {
filters.put(filterName, filter);
}

public void add(String filterName, AdviceFilter filter) {
if (filter instanceof StatisticEnabledFilter) {
add(filterName, filter);
}
}

public AdviceFilter get(String filterName) {
return filters.get(filterName);
}
Expand Down
10 changes: 5 additions & 5 deletions remora-websphere/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -37,11 +37,11 @@
<url>https://github.com/Nastel/</url>

<properties>
<ibm.thinclient>${websphere.install.dir}//lib//com.ibm.jaxws.thinclient_8.5.0.jar</ibm.thinclient>
<ibm.runtimes>${websphere.install.dir}//lib//com.ibm.ws.runtime.jar</ibm.runtimes>
<ibm.webcontainer>${websphere.install.dir}//lib//com.ibm.ws.webcontainer.jar</ibm.webcontainer>
<ibm.rsaHelpers>${websphere.install.dir}//lib//rsahelpers.jar</ibm.rsaHelpers>
<ibm.adminService>${websphere.install.dir}//lib//com.ibm.ws.admin.core.jar</ibm.adminService>
<ibm.thinclient>${websphere.install.dir}\runtimes\com.ibm.jaxws.thinclient_8.5.0.jar</ibm.thinclient>
<ibm.runtimes>${websphere.install.dir}\plugins\com.ibm.ws.runtime.jar</ibm.runtimes>
<ibm.webcontainer>${websphere.install.dir}\plugins\com.ibm.ws.webcontainer.jar</ibm.webcontainer>
<ibm.rsaHelpers>${websphere.install.dir}\lib\rsahelpers.jar</ibm.rsaHelpers>
<ibm.adminService>${websphere.install.dir}\plugins\com.ibm.ws.admin.core.jar</ibm.adminService>
</properties>

<dependencies>
Expand Down

0 comments on commit 39440c4

Please sign in to comment.