Skip to content

Commit

Permalink
Merge pull request #7 from leancloud/feat/compatible-java8
Browse files Browse the repository at this point in the history
compatible with java8
  • Loading branch information
ylgrgyq authored Oct 30, 2019
2 parents 15bae9a + 4c081e1 commit 1406d78
Show file tree
Hide file tree
Showing 12 changed files with 231 additions and 219 deletions.
11 changes: 10 additions & 1 deletion filter-service-core/bin/filter-service
Original file line number Diff line number Diff line change
Expand Up @@ -11,13 +11,22 @@ if [ ! -d "$LOG_DIR" ]; then
mkdir -p $LOG_DIR
fi

JAVA_VERSION=$($JAVA -version 2>&1 | grep 'version' | awk '{print $3}'')
echo "Using java version:$JAVA_VERSION path:$JAVA"
JVM_GC_LOG_CONFIGS="-XX:+PrintGCDateStamps -XX:+UseGCLogFileRotation -XX:NumberOfGCLogFiles=5 -XX:GCLogFileSize=10M -XX:+PrintGCDetails -XX:+PrintReferenceGC -Xloggc:$LOG_DIR/gc.log"
JAVA_VERSION=$($JAVA -version 2>&1 >/dev/null | grep 'version' | awk '{print $3}')
if [[ $JAVA_VERSION =~ ^\"1[1-9]+.* ]]; then
JVM_GC_LOG_CONFIGS="-Xlog:gc+heap=info:file=$LOG_DIR/gc.log:time,level,tags:filecount=5,filesize=10240"
fi
$JAVA \
-Xmx1G -Xms1G \
-XX:+UseG1GC \
-XX:+ParallelRefProcEnabled \
-XX:MaxGCPauseMillis=1000 \
-server \
-Xlog:gc+heap=info:file=$LOG_DIR/gc.log:time,level,tags:filecount=5,filesize=10240 \
$JVM_GC_LOG_CONFIGS \
-DLog4jContextSelector=org.apache.logging.log4j.core.async.AsyncLoggerContextSelector \
-Dlog4j.logdir=$LOG_DIR \
-cp $CLASSPATH \
Expand Down
8 changes: 0 additions & 8 deletions filter-service-core/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -85,14 +85,6 @@
<plugins>
<plugin>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.8.1</version>
<configuration>
<release>11</release>
<useIncrementalCompilation>false</useIncrementalCompilation>
<compilerArgs>
<arg>-parameters</arg>
</compilerArgs>
</configuration>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
Expand Down
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
package cn.leancloud.filter.service;

import cn.leancloud.filter.service.BloomFilterManager.CreateFilterResult;
import com.fasterxml.jackson.annotation.JsonGetter;
import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.node.ArrayNode;
import com.fasterxml.jackson.databind.node.BooleanNode;
import com.linecorp.armeria.common.HttpResponse;
import com.linecorp.armeria.common.HttpStatus;
Expand Down Expand Up @@ -33,7 +33,7 @@ public HttpResponse create(@Param String name,
final JsonNode fpp = req.get("fpp");
final JsonNode validPeriod = req.get("validPeriod");
final JsonNode overwrite = req.get("overwrite");
final var config = new ExpirableBloomFilterConfig();
final ExpirableBloomFilterConfig config = new ExpirableBloomFilterConfig();

if (expectedInsertions != null) {
config.setExpectedInsertions(expectedInsertions.intValue());
Expand All @@ -60,15 +60,15 @@ public HttpResponse create(@Param String name,

@Get("/{name}")
public JsonNode getFilterInfo(@Param String name) throws FilterNotFoundException {
var filter = bloomFilterManager.safeGetFilter(name);
final BloomFilter filter = bloomFilterManager.safeGetFilter(name);
return MAPPER.valueToTree(filter);
}

@Get("/list")
public JsonNode list() {
final var response = MAPPER.createArrayNode();
final ArrayNode response = MAPPER.createArrayNode();

for (final var name : bloomFilterManager.getAllFilterNames()) {
for (final String name : bloomFilterManager.getAllFilterNames()) {
response.add(name);
}

Expand All @@ -79,24 +79,24 @@ public JsonNode list() {
public JsonNode check(@Param String name,
@RequestObject JsonNode req)
throws FilterNotFoundException {
final var testingValue = checkNotNull("value", req.get("value"));
final JsonNode testingValue = checkNotNull("value", req.get("value"));
checkParameter("value", testingValue.isTextual(), "expect string type");

final var filter = bloomFilterManager.safeGetFilter(name);
final var contain = filter.mightContain(testingValue.textValue());
final BloomFilter filter = bloomFilterManager.safeGetFilter(name);
final boolean contain = filter.mightContain(testingValue.textValue());
return BooleanNode.valueOf(contain);
}

@Post("/{name}/multi-check")
public JsonNode multiCheck(@Param String name,
@RequestObject JsonNode req)
throws FilterNotFoundException {
final var values = checkNotNull("values", req.get("values"));
final JsonNode values = checkNotNull("values", req.get("values"));
checkParameter("values", values.isArray(), "expect Json array");

final var filter = bloomFilterManager.safeGetFilter(name);
final var response = MAPPER.createArrayNode();
for (final var value : values) {
final BloomFilter filter = bloomFilterManager.safeGetFilter(name);
final ArrayNode response = MAPPER.createArrayNode();
for (final JsonNode value : values) {
response.add(value.isTextual() && filter.mightContain(value.textValue()));
}
return response;
Expand All @@ -106,24 +106,24 @@ public JsonNode multiCheck(@Param String name,
public JsonNode checkAndSet(@Param String name,
@RequestObject JsonNode req)
throws FilterNotFoundException {
final var testingValue = checkNotNull("value", req.get("value"));
final JsonNode testingValue = checkNotNull("value", req.get("value"));
checkParameter("value", testingValue.isTextual(), "expect string type");

final var filter = bloomFilterManager.safeGetFilter(name);
final var contain = !filter.set(testingValue.textValue());
final BloomFilter filter = bloomFilterManager.safeGetFilter(name);
final boolean contain = !filter.set(testingValue.textValue());
return BooleanNode.valueOf(contain);
}

@Post("/{name}/multi-check-and-set")
public JsonNode multiCheckAndSet(@Param String name,
@RequestObject JsonNode req)
throws FilterNotFoundException {
final var values = checkNotNull("values", req.get("values"));
final JsonNode values = checkNotNull("values", req.get("values"));
checkParameter("values", values.isArray(), "expect Json array");

final var filter = bloomFilterManager.safeGetFilter(name);
final var response = MAPPER.createArrayNode();
for (final var value : values) {
final BloomFilter filter = bloomFilterManager.safeGetFilter(name);
final ArrayNode response = MAPPER.createArrayNode();
for (final JsonNode value : values) {
if (value.isTextual()) {
response.add(BooleanNode.valueOf(!filter.set(value.textValue())));
} else {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,12 +25,12 @@ public final class BloomFilterManagerImpl<T extends ExpirableBloomFilter>

@Override
public CreateFilterResult<T> createFilter(String name, ExpirableBloomFilterConfig config, boolean overwrite) {
var filter = factory.createFilter(config);
var created = true;
T filter = factory.createFilter(config);
boolean created = true;
if (overwrite) {
filterMap.put(name, filter);
} else {
final var prevFilter = filterMap.putIfAbsent(name, filter);
final T prevFilter = filterMap.putIfAbsent(name, filter);
if (prevFilter != null) {
created = false;
filter = prevFilter;
Expand Down Expand Up @@ -61,7 +61,7 @@ public T getFilter(String name) {

@Override
public T safeGetFilter(String name) throws FilterNotFoundException {
final var filter = getFilter(name);
final T filter = getFilter(name);
if (filter == null) {
throw FILTER_NOT_FOUND_EXCEPTION;
}
Expand All @@ -79,7 +79,7 @@ public int size() {

@Override
public void remove(String name) {
final var filter = filterMap.remove(name);
final T filter = filterMap.remove(name);
if (filter != null) {
notifyBloomFilterRemoved(name, filter);
}
Expand All @@ -88,9 +88,9 @@ public void remove(String name) {
@Override
public void purge() {
for (Map.Entry<String, T> entry : filterMap.entrySet()) {
final var filter = entry.getValue();
final T filter = entry.getValue();
if (filter.expired()) {
final var name = entry.getKey();
final String name = entry.getKey();
if (filterMap.remove(name, filter)) {
notifyBloomFilterRemoved(name, filter);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,44 +17,44 @@

import javax.annotation.Nullable;
import java.time.Duration;
import java.util.Iterator;
import java.util.Optional;
import java.util.ServiceLoader;
import java.util.concurrent.CompletableFuture;
import java.util.concurrent.Executors;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.*;

public final class Bootstrap {
private static final Logger logger = LoggerFactory.getLogger(Bootstrap.class);

public static void main(String[] args) {
final var ret = parseCommandLineArgs(args);
final ParseCommandLineArgsResult ret = parseCommandLineArgs(args);
if (ret.isExit()) {
System.exit(ret.getExitCode());
return;
}

final var opts = ret.getOptions();
final ServerOptions opts = ret.getOptions();
assert opts != null;

final var scheduledExecutorService = Executors.newScheduledThreadPool(10,
final ScheduledExecutorService scheduledExecutorService = Executors.newScheduledThreadPool(10,
new ThreadFactoryBuilder().setNameFormat("scheduled-worker-%s").build());
final var bloomFilterManager = newBloomFilterManager();
final var purgeFuture = scheduledExecutorService.scheduleWithFixedDelay(() -> {
final BloomFilterManagerImpl<GuavaBloomFilter> bloomFilterManager = newBloomFilterManager();
final ScheduledFuture<?> purgeFuture = scheduledExecutorService.scheduleWithFixedDelay(() -> {
try {
bloomFilterManager.purge();
} catch (Exception ex) {
logger.error("Purge bloom filter service failed.", ex);
}
}, 0, 300, TimeUnit.MILLISECONDS);
final var metricsService = loadMetricsService();
final var registry = metricsService.createMeterRegistry();
final MetricsService metricsService = loadMetricsService();
final MeterRegistry registry = metricsService.createMeterRegistry();
metricsService.start();
final var server = newServer(registry, opts, bloomFilterManager);
final Server server = newServer(registry, opts, bloomFilterManager);

Runtime.getRuntime().addShutdownHook(new Thread(() -> {
server.stop().join();
purgeFuture.cancel(false);

final var shutdownFuture = new CompletableFuture<>();
final CompletableFuture<Void> shutdownFuture = new CompletableFuture<>();
scheduledExecutorService.execute(() ->
shutdownFuture.complete(null)
);
Expand All @@ -72,8 +72,8 @@ public static void main(String[] args) {
}

private static ParseCommandLineArgsResult parseCommandLineArgs(String[] args) {
final var opts = new ServerOptions();
final var cli = new CommandLine(opts);
final ServerOptions opts = new ServerOptions();
final CommandLine cli = new CommandLine(opts);
try {
cli.parseArgs(args);

Expand All @@ -100,7 +100,7 @@ private static ParseCommandLineArgsResult parseCommandLineArgs(String[] args) {
private static BloomFilterManagerImpl<GuavaBloomFilter> newBloomFilterManager() {
final GuavaBloomFilterFactory factory = new GuavaBloomFilterFactory();
final BloomFilterManagerImpl<GuavaBloomFilter> bloomFilterManager = new BloomFilterManagerImpl<>(factory);
bloomFilterManager.addListener(new BloomFilterManagerListener<>() {
bloomFilterManager.addListener(new BloomFilterManagerListener<GuavaBloomFilter, ExpirableBloomFilterConfig>() {
@Override
public void onBloomFilterCreated(String name, ExpirableBloomFilterConfig config, GuavaBloomFilter filter) {
logger.info("Bloom filter with name: {} was created.", name);
Expand All @@ -119,9 +119,13 @@ public void onBloomFilterRemoved(String name, GuavaBloomFilter filter) {
}

private static MetricsService loadMetricsService() {
final var loader = ServiceLoader.load(MetricsService.class);
final var optService = loader.findFirst();
return optService.orElseGet(DefaultMetricsService::new);
final ServiceLoader<MetricsService> loader = ServiceLoader.load(MetricsService.class);
final Iterator<MetricsService> iterator = loader.iterator();
if (iterator.hasNext()) {
return iterator.next();
} else {
return new DefaultMetricsService();
}
}

private static Server newServer(MeterRegistry registry,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,26 +10,26 @@
public class AbstractBloomFilterConfigTest {
@Test
public void testGetAndSetExpectedInsertions() {
final var expectedEInsertions = ThreadLocalRandom.current().nextInt(1, Integer.MAX_VALUE);
final var config = new TestingBloomFilterConfig();
final int expectedEInsertions = ThreadLocalRandom.current().nextInt(1, Integer.MAX_VALUE);
final TestingBloomFilterConfig config = new TestingBloomFilterConfig();
assertThat(config.expectedInsertions()).isEqualTo(BloomFilterConfig.DEFAULT_EXPECTED_INSERTIONS);
assertThat(config.setExpectedInsertions(expectedEInsertions)).isSameAs(config);
assertThat(config.expectedInsertions()).isEqualTo(expectedEInsertions);
}

@Test
public void testGetAndSetFpp() {
final var expectedFpp = ThreadLocalRandom.current().nextDouble(0.0001, 1);
final var config = new TestingBloomFilterConfig();
final double expectedFpp = ThreadLocalRandom.current().nextDouble(0.0001, 1);
final TestingBloomFilterConfig config = new TestingBloomFilterConfig();
assertThat(config.fpp()).isEqualTo(BloomFilterConfig.DEFAULT_FALSE_POSITIVE_PROBABILITY);
assertThat(config.setFpp(expectedFpp)).isSameAs(config);
assertThat(config.fpp()).isEqualTo(expectedFpp);
}

@Test
public void testGetAndSetInvalidExpectedInsertions() {
final var invalidExpectedInsertions = -1 * Math.abs(ThreadLocalRandom.current().nextInt());
final var config = new TestingBloomFilterConfig();
final int invalidExpectedInsertions = -1 * Math.abs(ThreadLocalRandom.current().nextInt());
final TestingBloomFilterConfig config = new TestingBloomFilterConfig();

assertThatThrownBy(() -> config.setExpectedInsertions(invalidExpectedInsertions))
.isInstanceOf(BadParameterException.class)
Expand All @@ -38,8 +38,8 @@ public void testGetAndSetInvalidExpectedInsertions() {

@Test
public void testGetAndSetInvalidFpp() {
final var invalidFpp = ThreadLocalRandom.current().nextDouble(1, Long.MAX_VALUE);
final var config = new TestingBloomFilterConfig();
final double invalidFpp = ThreadLocalRandom.current().nextDouble(1, Long.MAX_VALUE);
final TestingBloomFilterConfig config = new TestingBloomFilterConfig();

assertThatThrownBy(() -> config.setFpp(invalidFpp))
.isInstanceOf(BadParameterException.class)
Expand All @@ -48,13 +48,13 @@ public void testGetAndSetInvalidFpp() {

@Test
public void testEquals() {
final var fpp = ThreadLocalRandom.current().nextDouble(0.0001, 1);
final var expectedInsertions = ThreadLocalRandom.current().nextInt(1, Integer.MAX_VALUE);
final var filterA = new TestingBloomFilterConfig()
final double fpp = ThreadLocalRandom.current().nextDouble(0.0001, 1);
final int expectedInsertions = ThreadLocalRandom.current().nextInt(1, Integer.MAX_VALUE);
final TestingBloomFilterConfig filterA = new TestingBloomFilterConfig()
.setFpp(fpp)
.setExpectedInsertions(expectedInsertions);

final var filterB = new TestingBloomFilterConfig()
final TestingBloomFilterConfig filterB = new TestingBloomFilterConfig()
.setFpp(fpp)
.setExpectedInsertions(expectedInsertions);

Expand All @@ -63,13 +63,13 @@ public void testEquals() {

@Test
public void testHashCode() {
final var fpp = ThreadLocalRandom.current().nextDouble(0.0001, 1);
final var expectedInsertions = ThreadLocalRandom.current().nextInt(1, Integer.MAX_VALUE);
final var filterA = new TestingBloomFilterConfig()
final double fpp = ThreadLocalRandom.current().nextDouble(0.0001, 1);
final int expectedInsertions = ThreadLocalRandom.current().nextInt(1, Integer.MAX_VALUE);
final TestingBloomFilterConfig filterA = new TestingBloomFilterConfig()
.setFpp(fpp)
.setExpectedInsertions(expectedInsertions);

final var filterB = new TestingBloomFilterConfig()
final TestingBloomFilterConfig filterB = new TestingBloomFilterConfig()
.setFpp(fpp)
.setExpectedInsertions(expectedInsertions);

Expand Down
Loading

0 comments on commit 1406d78

Please sign in to comment.