Skip to content

Commit

Permalink
#8 Auto filter logic fix; RemoraConfig fix
Browse files Browse the repository at this point in the history
  • Loading branch information
mjok committed Jun 29, 2020
1 parent 39440c4 commit 0fcb0c6
Show file tree
Hide file tree
Showing 5 changed files with 95 additions and 16 deletions.
34 changes: 23 additions & 11 deletions remora-core/src/main/java/com/jkoolcloud/remora/AdviceRegistry.java
Original file line number Diff line number Diff line change
Expand Up @@ -26,9 +26,11 @@
import java.util.concurrent.TimeUnit;
import java.util.stream.Collectors;

import org.tinylog.Logger;
import org.tinylog.TaggedLogger;

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;

Expand Down Expand Up @@ -78,24 +80,34 @@ public static void limit() {
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) FilterManager.INSTANCE.get(AUTO_LIMITING_FILTER);
LimitingFilter finalLimitingFilter = limitingFilter;
AdviceRegistry.INSTANCE.adviceList.stream().filter(advice -> advice instanceof BaseTransformers)
.filter(advice -> !((BaseTransformers) advice).filters.contains(finalLimitingFilter))
.forEach(advice -> ((BaseTransformers) advice).filters.add(finalLimitingFilter));

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

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

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));
try {
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().filter(advice -> advice instanceof BaseTransformers)
.map(advice -> (BaseTransformers) advice)
.forEach(advice -> advice.filters.remove(limitingFilter));
}

} catch (Exception e) {
TaggedLogger init = Logger.tag("INIT");
init.error("Cannot release filter");
}

}

}
Original file line number Diff line number Diff line change
Expand Up @@ -104,8 +104,15 @@ public static Object getAppliedValue(Field field, String configValue) {
break;

case "int":
case "java.lang.Integer":
appliedValue = Integer.parseInt(configValue);
break;

case "long":
case "java.lang.Long":
appliedValue = Long.parseLong(configValue);
break;

case "default":
// logger.info("Unsupported property");

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,29 +16,37 @@

package com.jkoolcloud.remora.filters;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;
import java.util.stream.Collectors;

import com.jkoolcloud.remora.AdviceRegistry;
import com.jkoolcloud.remora.advices.BaseTransformers;

public enum FilterManager {
INSTANCE;

Map<String, StatisticEnabledFilter> filters = new HashMap<>(10);
Map<String, StatisticEnabledFilter> filters = new ConcurrentHashMap<>(10);

public List<AdviceFilter> get(List<?> list) {
return filters.entrySet().stream().filter(entry -> list.contains(entry.getKey())).map(entry -> entry.getValue())
.collect(Collectors.toList());
}

public void add(String filterName, StatisticEnabledFilter filter) {
filters.put(filterName, filter);
StatisticEnabledFilter put = filters.put(filterName, filter);
if (put != null) {
AdviceRegistry.INSTANCE.getRegisteredAdvices().stream().filter(advice -> advice instanceof BaseTransformers)
.map(advice -> (BaseTransformers) advice)
.forEach(baseTransformers -> baseTransformers.filters.remove(put));
}
}

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

public AdviceFilter get(String filterName) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,17 @@ public static class TestForSingleStringConfigrable {
String testField;
}

public static class TestForNumbersConfigrable {
@RemoraConfig.Configurable
Integer testField;
@RemoraConfig.Configurable
int testField2;
@RemoraConfig.Configurable
long testField3;
@RemoraConfig.Configurable
Long testField4;
}

public static class TestForListConfigrable {
@RemoraConfig.Configurable
List testField;
Expand Down Expand Up @@ -88,6 +99,27 @@ public void configTestHappyPath() throws Exception {
assertNotNull("Configuring field failed", test.testField);
}

@Test
public void configTestHappyPathNumbers() throws Exception {
Properties properties = new Properties() {
{
put(TestForNumbersConfigrable.class.getName() + "." + "testField", "1");
put(TestForNumbersConfigrable.class.getName() + "." + "testField2", "2");
put(TestForNumbersConfigrable.class.getName() + "." + "testField3", "3");
put(TestForNumbersConfigrable.class.getName() + "." + "testField4", "4");
}
};
prepareConfigFile(properties);
TestForNumbersConfigrable test = new TestForNumbersConfigrable();
RemoraConfig.INSTANCE.init(); // you need to initialise repeatidly 'cause multiple tests will fail
RemoraConfig.configure(test);
cleanup();
assertEquals("Configuring field Integer failed", new Integer(1), test.testField);
assertEquals("Configuring field int failed", 2, test.testField2);
assertEquals("Configuring field Long failed", 3L, test.testField3);
assertEquals("Configuring field long failed", new Long(4L), test.testField4);
}

@Test
public void configTestBooleanHappyPath() throws Exception {
Properties properties = new Properties() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -98,4 +98,24 @@ public void testCompatibleWrite() throws IOException {
}
}

@Test
public void testIntermediateQueue() throws InterruptedException {
ChronicleOutput output = new ChronicleOutput();
output.rollCycle = RollCycles.TEST_SECONDLY;
output.keepQueueRolls = 2;
output.workerSize = 1;
output.intermediateQueueSize = 1;
File tempDir = Files.createTempDir();
tempDir.deleteOnExit();
System.out.println(tempDir.getAbsolutePath());
output.queuePath = tempDir.getPath();
output.init();

for (int i = 0; i <= 50; i++) {
output.send(new EntryDefinition(Advice1.class, true));
Thread.sleep(100);
}

}

}

0 comments on commit 0fcb0c6

Please sign in to comment.