Skip to content

Commit

Permalink
ElectionConfig: configurable Zlg
Browse files Browse the repository at this point in the history
  • Loading branch information
ekoutanov committed Apr 21, 2018
1 parent 4c497c1 commit b64ef8c
Show file tree
Hide file tree
Showing 4 changed files with 32 additions and 17 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
HazelQ
<img src="https://raw.githubusercontent.com/wiki/obsidiandynamics/hazelq/images/hazelq-logo.png" width="90px" alt="logo"/> HazelQ
===
Message streaming over Hazelcast IMDG.

Expand Down
30 changes: 14 additions & 16 deletions elect/src/main/java/com/obsidiandynamics/hazelq/Election.java
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,6 @@
import com.obsidiandynamics.zerolog.*;

public final class Election implements Terminable, Joinable {
private static final Zlg zlg = Zlg.forDeclaringClass().get();

private final ElectionConfig config;

private final RetryableMap<String, byte[]> leases;
Expand All @@ -36,8 +34,8 @@ public Election(ElectionConfig config, IMap<String, byte[]> leases) {
.withExceptionClass(HazelcastException.class)
.withAttempts(Integer.MAX_VALUE)
.withBackoff(100)
.withFaultHandler(zlg::w)
.withErrorHandler(zlg::e);
.withFaultHandler(config.getZlg()::w)
.withErrorHandler(config.getZlg()::e);
this.leases = new RetryableMap<>(retry, leases);
registry = new Registry();

Expand Down Expand Up @@ -70,14 +68,14 @@ void scavenge() {
final Lease existingLease = leaseView.getOrDefault(resource, Lease.vacant());
if (! existingLease.isCurrent()) {
if (existingLease.isVacant()) {
zlg.d("Lease of %s is vacant", z -> z.arg(resource));
config.getZlg().d("Lease of %s is vacant", z -> z.arg(resource));
} else {
scavengeWatcher.onExpire(resource, existingLease.getTenant());
zlg.d("Lease of %s by %s expired at %s",
z -> z
.arg(resource)
.arg(existingLease::getTenant)
.arg(Args.map(existingLease::getExpiry, Lease::formatExpiry)));
config.getZlg().d("Lease of %s by %s expired at %s",
z -> z
.arg(resource)
.arg(existingLease::getTenant)
.arg(Args.map(existingLease::getExpiry, Lease::formatExpiry)));
}

final UUID nextCandidate = registry.getRandomCandidate(resource);
Expand All @@ -92,14 +90,14 @@ void scavenge() {
}

if (success) {
zlg.d("New lease of %s by %s until %s",
z -> z
.arg(resource)
.arg(nextCandidate)
.arg(Args.map(newLease::getExpiry, Lease::formatExpiry)));
config.getZlg().d("New lease of %s by %s until %s",
z -> z
.arg(resource)
.arg(nextCandidate)
.arg(Args.map(newLease::getExpiry, Lease::formatExpiry)));
reloadView();
scavengeWatcher.onAssign(resource, nextCandidate);
}
}
}
}
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,14 +1,27 @@
package com.obsidiandynamics.hazelq;

import com.obsidiandynamics.yconf.*;
import com.obsidiandynamics.zerolog.*;

@Y
public final class ElectionConfig {
@YInject
private Zlg zlg = Zlg.forDeclaringClass().get();

@YInject
private int scavengeIntervalMillis = 100;

@YInject
private int leaseDurationMillis = 60_000;

Zlg getZlg() {
return zlg;
}

public ElectionConfig withZlg(Zlg zlg) {
this.zlg = zlg;
return this;
}

int getScavengeInterval() {
return scavengeIntervalMillis;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,18 @@
import org.junit.*;

import com.obsidiandynamics.assertion.*;
import com.obsidiandynamics.zerolog.*;

public final class ElectionConfigTest {
@Test
public void testFields() {
final Zlg zlg = Zlg.forDeclaringClass().get();
final ElectionConfig c = new ElectionConfig()
.withZlg(zlg)
.withLeaseDuration(100)
.withScavengeInterval(200);

assertEquals(zlg, c.getZlg());
assertEquals(100, c.getLeaseDuration());
assertEquals(200, c.getScavengeInterval());
}
Expand Down

0 comments on commit b64ef8c

Please sign in to comment.