Skip to content

Commit

Permalink
Add Coherence to simulator
Browse files Browse the repository at this point in the history
  • Loading branch information
ben-manes committed Jun 25, 2020
1 parent c13ed5b commit 3150a92
Show file tree
Hide file tree
Showing 12 changed files with 170 additions and 51 deletions.
1 change: 1 addition & 0 deletions build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -156,5 +156,6 @@ tasks.coveralls {
}

dependencyUpdates.resolutionStrategy {
force 'javax.json.bind:javax.json.bind-api:1.0'
force testLibraries.truth
}
11 changes: 11 additions & 0 deletions checksum.xml
Original file line number Diff line number Diff line change
Expand Up @@ -51,10 +51,12 @@
<trusted-key id='e0cb7823cfd00fbf' group='com.jakewharton.fliptables' />
<trusted-key id='a50569c7ca7fa1f0' group='com.jcraft' />
<trusted-key id='52931f4b72b4f54c' group='com.koloboke' />
<trusted-key id='0c33729405f13ee8' group='com.oracle.coherence.ce' />
<trusted-key id='1063fe98bcecb758' group='com.puppycrawl.tools' />
<trusted-key id='685c46769dbb5e5d' group='com.squareup' />
<trusted-key id='80c08b1c29100955' group='com.squareup' />
<trusted-key id='8671a8df71296252' group='com.squareup' />
<trusted-key id='bf984b4145ea13f7' group='com.squareup' />
<trusted-key id='0315bfb7970a144f' group='com.sun.istack' />
<trusted-key id='0315bfb7970a144f' group='com.sun.xml.fastinfoset' />
<trusted-key id='de3b15b43edfe54b' group='com.tdunning' />
Expand Down Expand Up @@ -83,6 +85,8 @@
<trusted-key id='6425559c47cc79c4' group='javax.activation' />
<trusted-key id='8db9fa0b0718bbf9' group='javax.cache' />
<trusted-key id='d5e520459cb3aa49' group='javax.cache' />
<trusted-key id='0315bfb7970a144f' group='javax.json' />
<trusted-key id='5f46eb39af9138fe' group='javax.json.bind' />
<trusted-key id='0315bfb7970a144f' group='javax.xml.bind' />
<trusted-key id='4044edf1bb73efea' group='jaxen' />
<trusted-key id='72385ff0af338d52' group='joda-time' />
Expand Down Expand Up @@ -228,6 +232,9 @@
<dependency group='com.github.jengelman.gradle.plugins' module='shadow' version='5.2.0'>
<sha512>39308A31635C2B4728354CC79A1EA2F0DE10F7F80BAE0205E3D960A92A42A25E55E0B4ABD18960502B22E5467474DCC8E35079A6866742773D47A3D2D1A206FB</sha512>
</dependency>
<dependency group='com.github.jengelman.gradle.plugins' module='shadow' version='6.0.0'>
<sha512>BEA7726A32C1541ADBC5968B4BC44187CF7B75E48868933AC9114DE5D611A312B5BA990207460AF5FD46AA14094ADB74DE2D13EFD8941EDB55C8410F62C8BED4</sha512>
</dependency>
<dependency group='com.github.spotbugs' module='spotbugs-gradle-plugin' version='2.0.1'>
<sha512>2C09B102601DF179B615EF37F5E56B9AA1100BB2A17D742B8CB452C1A193C44ED3EBE029FEAFB9822D55A680D99B682184C733D56F835874DD01B02209AFA13D</sha512>
</dependency>
Expand Down Expand Up @@ -261,6 +268,9 @@
<dependency group='gradle.plugin.com.github.spotbugs.snom' module='spotbugs-gradle-plugin' version='4.2.4'>
<sha512>56D0480F4DD5A53D5A13A4B2FE58EE861589D1ECAC9A42BF7DAC7376A08C82A3654E3E1B5ECC03622BAE1FD9BFBD2D8CC9BF36292AB96A9BC3ABB3A1FE0A328</sha512>
</dependency>
<dependency group='gradle.plugin.com.github.spotbugs.snom' module='spotbugs-gradle-plugin' version='4.4.3'>
<sha512>E4C006C0F02D96E33AC434A94002AC1FE4B7851C163CD77B89B93D662C5C632CE36B207657DC763B00940DA19B5DB5B364B0088A89214BE9CA28B0559444EF0</sha512>
</dependency>
<dependency group='gradle.plugin.com.github.spotbugs' module='spotbugs-gradle-plugin' version='2.0.0'>
<sha512>B3BFAD07E6A3D4D73CBCE802D8614CF4AC84E589166D243D41028DC077F84C027DF4D514F145360405F37DA73A8F2E7B65D90877A9EE1151174D2440530F9051</sha512>
</dependency>
Expand Down Expand Up @@ -361,6 +371,7 @@
<sha512>7EBA7097109389F48FA9142BF22D225F5D3104D68013D3B8E0A6D4054A8CD0EF64614372BDCB38987D68ADB6D33A3804E98DDF112BA84DBB09D448CE702DACC4</sha512>
</dependency>
<dependency group='org.sonarsource.scanner.gradle' module='sonarqube-gradle-plugin' version='3.0'>
<sha512>18519D5F13C5FEF9AE97F388362B3F62798F268D07B37FC5DF3F01741C259B8D91D33E477718D0C9D2D204069D9E9C0B5806E48C392314ED2AA8D71DE6D15A44</sha512>
<sha512>A056FCE66EA9664D1CF22C64D948FE7BFB809253ABAB646EB6C9DFAA5B2894377D2E403A40B5F965C4BFDFA83EAC06F0CC9511CBFA7FA4E8DB3F3F0C59716CCD</sha512>
</dependency>
<dependency group='org.testng' module='testng' version='7.0.0'>
Expand Down
19 changes: 12 additions & 7 deletions gradle/dependencies.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -25,9 +25,10 @@
*/
ext {
versions = [
akka: '2.6.5',
akka: '2.6.6',
cache2k: '1.3.1.Alpha',
checkerFramework: '3.4.0',
checkerFramework: '3.4.1',
coherence: '20.06',
collision: '0.3.3',
commonsCompress: '1.20',
commonsLang3: '3.10',
Expand All @@ -37,7 +38,7 @@ ext {
ehcache3: '3.8.1',
errorprone: '2.4.0',
errorproneJavac: '9+181-r4173-1',
elasticSearch: '7.7.0',
elasticSearch: '7.8.0',
expiringMap: '0.5.9',
fastfilter: 'bf0b02297f',
fastutil: '8.3.1',
Expand All @@ -46,7 +47,7 @@ ext {
jackrabbit: '1.30.0',
jamm: '0.3.3',
javaObjectLayout: '0.10',
javapoet: '1.12.1',
javapoet: '1.13.0',
jcache: '1.1.1',
jmh: '1.23',
jsr330: '1',
Expand Down Expand Up @@ -75,7 +76,7 @@ ext {
]
pluginVersions = [
apt: '0.21',
bnd: '5.0.1',
bnd: '5.1.1',
checkstyle: '8.33',
coveralls: '2.8.4',
coverity: '1.0.10',
Expand All @@ -86,10 +87,10 @@ ext {
nullaway: '1.0.1',
pmd: '6.24.0',
semanticVersioning: '1.1.0',
shadow: '5.2.0',
shadow: '6.0.0',
sonarqube: '3.0',
spotbugs: '4.0.3',
spotbugsPlugin: '4.2.4',
spotbugsPlugin: '4.4.3',
stats: '0.2.2',
versions: '0.28.0',
]
Expand All @@ -98,6 +99,10 @@ ext {
akka: "com.typesafe.akka:akka-actor_2.12:${versions.akka}",
cache2k: "org.cache2k:cache2k-core:${versions.cache2k}",
checkerAnnotations: "org.checkerframework:checker-qual:${versions.checkerFramework}",
coherence: [
"com.oracle.coherence.ce:coherence:${versions.coherence}",
'javax.json.bind:javax.json.bind-api:1.0',
],
collision: "systems.comodal:collision:${versions.collision}",
commonsCompress: "org.apache.commons:commons-compress:${versions.commonsCompress}",
commonsLang3: "org.apache.commons:commons-lang3:${versions.commonsLang3}",
Expand Down
2 changes: 1 addition & 1 deletion gradle/wrapper/gradle-wrapper.properties
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
distributionUrl=https\://services.gradle.org/distributions/gradle-6.5-rc-1-bin.zip
distributionUrl=https\://services.gradle.org/distributions/gradle-6.5-bin.zip
distributionBase=GRADLE_USER_HOME
distributionPath=wrapper/dists
zipStoreBase=GRADLE_USER_HOME
Expand Down
1 change: 1 addition & 0 deletions simulator/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ dependencies {
implementation libraries.ehcache3
implementation libraries.fastutil
implementation libraries.slf4jNop
implementation libraries.coherence
implementation libraries.collision
implementation libraries.fastfilter
implementation libraries.flipTables
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@
import com.github.benmanes.caffeine.cache.simulator.policy.AccessEvent;
import com.github.benmanes.caffeine.cache.simulator.policy.Policy;
import com.github.benmanes.caffeine.cache.simulator.policy.Registry;
import com.google.common.collect.ImmutableSet;
import com.typesafe.config.ConfigFactory;

/**
Expand All @@ -52,13 +53,15 @@ public class TraceBenchmark {
@Param({"0", "1000"})
int missPenalty;

BasicSettings settings;
AccessEvent[] events;
Registry registry;

@Setup
public void setup() throws IOException {
settings = new BasicSettings(ConfigFactory.load().getConfig("caffeine.simulator"));
BasicSettings settings = new BasicSettings(
ConfigFactory.load().getConfig("caffeine.simulator"));
events = readEventStream(settings).toArray(AccessEvent[]::new);
registry = new Registry(settings, ImmutableSet.of());
}

@Benchmark
Expand All @@ -72,7 +75,7 @@ public Policy trace() {
}

public Policy makePolicy() {
Set<Policy> policies = Registry.policy(settings, policyName);
Set<Policy> policies = registry.policy(policyName);
if (policies.size() > 1) {
throw new IllegalArgumentException("Use one variation per policy configuration: "
+ policies.stream().map(policy -> policy.stats().name()).collect(toList()));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -131,7 +131,7 @@ private TraceReader makeTraceReader() {

/** Returns the actors to broadcast trace events to. */
private List<Routee> makeRoutes() {
return Registry.policies(settings, traceReader.characteristics()).stream().map(policy -> {
return new Registry(settings, traceReader.characteristics()).policies().stream().map(policy -> {
ActorRef actorRef = context().actorOf(Props.create(PolicyActor.class, policy));
context().watch(actorRef);
return new ActorRefRoutee(actorRef);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,12 +20,11 @@

import com.github.benmanes.caffeine.cache.simulator.parser.TextTraceReader;
import com.github.benmanes.caffeine.cache.simulator.parser.TraceReader.KeyOnlyTraceReader;
import com.google.common.hash.Hashing;

/**
* A reader for the page views log provided by
* <a href="https://www.kaggle.com/c/outbrain-click-prediction/data">Outbrain</a>.
* Notice: you should reorder the file according to the timestamp, e.g. by
* Notice: you should reorder the file according to the timestamp, e.g. by
* {@code sort -t, -k 3,3n -s page_views_sample.csv > page_views_sample_sorted.csv}
*
* @author ohadey@gmail.com (Ohad Eytan)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@
import com.github.benmanes.caffeine.cache.simulator.policy.opt.UnboundedPolicy;
import com.github.benmanes.caffeine.cache.simulator.policy.product.Cache2kPolicy;
import com.github.benmanes.caffeine.cache.simulator.policy.product.CaffeinePolicy;
import com.github.benmanes.caffeine.cache.simulator.policy.product.CoherencePolicy;
import com.github.benmanes.caffeine.cache.simulator.policy.product.CollisionPolicy;
import com.github.benmanes.caffeine.cache.simulator.policy.product.Ehcache3Policy;
import com.github.benmanes.caffeine.cache.simulator.policy.product.ElasticSearchPolicy;
Expand Down Expand Up @@ -74,48 +75,57 @@
* @author ben.manes@gmail.com (Ben Manes)
*/
public final class Registry {
private static final Map<String, Function<Config, Set<Policy>>> FACTORIES = makeRegistry();

private Registry() {}

private static Map<String, Function<Config, Set<Policy>>> makeRegistry() {
Map<String, Function<Config, Set<Policy>>> factories = new HashMap<>();
registerIrr(factories);
registerLinked(factories);
registerSketch(factories);
registerOptimal(factories);
registerSampled(factories);
registerProduct(factories);
registerTwoQueue(factories);
registerAdaptive(factories);
return factories.entrySet().stream().collect(
toMap(entry -> entry.getKey().toLowerCase(US), Map.Entry::getValue));
private final Set<Characteristic> characteristics;
private final Map<String, Factory> factories;
private final BasicSettings settings;

public Registry(BasicSettings settings, Set<Characteristic> characteristics) {
this.characteristics = characteristics;
this.factories = new HashMap<>();
this.settings = settings;
buildRegistry();
}

/**
* Returns all of the policies that have been configured for simulation and that meet a minimal
* set of supported characteristics.
*/
public static Set<Policy> policies(BasicSettings settings, Set<Characteristic> characteristics) {
public Set<Policy> policies() {
return settings.policies().stream()
.flatMap(name -> policy(settings, name).stream())
.flatMap(name -> policy(name).stream())
.filter(policy -> policy.characteristics().containsAll(characteristics))
.collect(toSet());
}

/** Returns all of the policy variations that have been configured. */
public static Set<Policy> policy(BasicSettings settings, String name) {
Function<Config, Set<Policy>> factory = FACTORIES.get(name.toLowerCase(US));
public Set<Policy> policy(String name) {
Factory factory = factories.get(name.toLowerCase(US));
checkNotNull(factory, "%s not found", name);
return factory.apply(settings.config());
}

private static void registerOptimal(Map<String, Function<Config, Set<Policy>>> factories) {
private void buildRegistry() {
registerIrr();
registerLinked();
registerSketch();
registerOptimal();
registerSampled();
registerProduct();
registerTwoQueue();
registerAdaptive();

Map<String, Factory> normalized = factories.entrySet().stream()
.collect(toMap(entry -> entry.getKey().toLowerCase(US), Map.Entry::getValue));
factories.clear();
factories.putAll(normalized);
}

private void registerOptimal() {
factories.put("opt.Clairvoyant", ClairvoyantPolicy::policies);
factories.put("opt.Unbounded", UnboundedPolicy::policies);
}

private static void registerLinked(Map<String, Function<Config, Set<Policy>>> factories) {
private void registerLinked() {
Stream.of(LinkedPolicy.EvictionPolicy.values()).forEach(priority -> {
String id = "linked." + priority.name();
factories.put(id, config -> LinkedPolicy.policies(config, priority));
Expand All @@ -129,19 +139,19 @@ private static void registerLinked(Map<String, Function<Config, Set<Policy>>> fa
factories.put("linked.S4Lru", S4LruPolicy::policies);
}

private static void registerSampled(Map<String, Function<Config, Set<Policy>>> factories) {
private void registerSampled() {
Stream.of(SampledPolicy.EvictionPolicy.values()).forEach(priority -> {
String id = "sampled." + priority.name();
factories.put(id, config -> SampledPolicy.policies(config, priority));
});
}

private static void registerTwoQueue(Map<String, Function<Config, Set<Policy>>> factories) {
private void registerTwoQueue() {
factories.put("two-queue.TuQueue", TuQueuePolicy::policies);
factories.put("two-queue.TwoQueue", TwoQueuePolicy::policies);
}

private static void registerSketch(Map<String, Function<Config, Set<Policy>>> factories) {
private void registerSketch() {
factories.put("sketch.WindowTinyLfu", WindowTinyLfuPolicy::policies);
factories.put("sketch.S4WindowTinyLfu", S4WindowTinyLfuPolicy::policies);
factories.put("sketch.LruWindowTinyLfu", LruWindowTinyLfuPolicy::policies);
Expand All @@ -159,29 +169,32 @@ private static void registerSketch(Map<String, Function<Config, Set<Policy>>> fa
factories.put("sketch.TinyCache_GhostCache", TinyCacheWithGhostCachePolicy::policies);
}

private static void registerIrr(Map<String, Function<Config, Set<Policy>>> factories) {
private void registerIrr() {
factories.put("irr.Frd", FrdPolicy::policies);
factories.put("irr.IndicatorFrd", IndicatorFrdPolicy::policies);
factories.put("irr.ClimberFrd", HillClimberFrdPolicy::policies);
factories.put("irr.Lirs", LirsPolicy::policies);
factories.put("irr.ClockPro", ClockProPolicy::policies);
}

private static void registerAdaptive(Map<String, Function<Config, Set<Policy>>> factories) {
private void registerAdaptive() {
factories.put("adaptive.Arc", ArcPolicy::policies);
factories.put("adaptive.Car", CarPolicy::policies);
factories.put("adaptive.Cart", CartPolicy::policies);
}

private static void registerProduct(Map<String, Function<Config, Set<Policy>>> factories) {
private void registerProduct() {
factories.put("product.OHC", OhcPolicy::policies);
factories.put("product.Guava", GuavaPolicy::policies);
factories.put("product.Tcache", TCachePolicy::policies);
factories.put("product.Cache2k", Cache2kPolicy::policies);
factories.put("product.Ehcache3", Ehcache3Policy::policies);
factories.put("product.Caffeine", CaffeinePolicy::policies);
factories.put("product.Coherence", CoherencePolicy::policies);
factories.put("product.Collision", CollisionPolicy::policies);
factories.put("product.ExpiringMap", ExpiringMapPolicy::policies);
factories.put("product.Elasticsearch", ElasticSearchPolicy::policies);
factories.put("product.Caffeine", config -> CaffeinePolicy.policies(config, characteristics));
}

private interface Factory extends Function<Config, Set<Policy>> {}
}
Original file line number Diff line number Diff line change
Expand Up @@ -39,22 +39,26 @@ public final class CaffeinePolicy implements Policy {
private final Cache<Long, AccessEvent> cache;
private final PolicyStats policyStats;

public CaffeinePolicy(Config config) {
public CaffeinePolicy(Config config, Set<Characteristic> characteristics) {
policyStats = new PolicyStats("product.Caffeine");
BasicSettings settings = new BasicSettings(config);
cache = Caffeine.newBuilder()
Caffeine<Long, AccessEvent> builder = Caffeine.newBuilder()
.removalListener((Long key, AccessEvent value, RemovalCause cause) ->
policyStats.recordEviction())
.weigher((key, value) -> value.weight())
.initialCapacity(settings.maximumSize())
.maximumWeight(settings.maximumSize())
.executor(Runnable::run)
.build();
.executor(Runnable::run);
if (characteristics.contains(WEIGHTED)) {
builder.maximumWeight(settings.maximumSize());
builder.weigher((key, value) -> value.weight());
} else {
builder.maximumSize(settings.maximumSize());
}
cache = builder.build();
}

/** Returns all variations of this policy based on the configuration parameters. */
public static Set<Policy> policies(Config config) {
return ImmutableSet.of(new CaffeinePolicy(config));
public static Set<Policy> policies(Config config, Set<Characteristic> characteristics) {
return ImmutableSet.of(new CaffeinePolicy(config, characteristics));
}

@Override public Set<Characteristic> characteristics() {
Expand Down
Loading

0 comments on commit 3150a92

Please sign in to comment.