Skip to content

Commit

Permalink
remove Ring access from Context
Browse files Browse the repository at this point in the history
  • Loading branch information
Hellblazer committed Mar 3, 2024
1 parent a073856 commit 2dab0d7
Show file tree
Hide file tree
Showing 10 changed files with 46 additions and 33 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -730,12 +730,12 @@ private boolean add(AccusationWrapper accusation, Participant accuser, Participa
if (!context.validRing(accusation.getRingNumber())) {
return false;
}
Ring<Participant> ring = context.ring(accusation.getRingNumber());

if (accused.isAccusedOn(ring.getIndex())) {
Participant currentAccuser = context.getMember(accused.getAccusation(ring.getIndex()).getAccuser());
if (accused.isAccusedOn(accusation.getRingNumber())) {
Participant currentAccuser = context.getMember(
accused.getAccusation(accusation.getRingNumber()).getAccuser());
if (!currentAccuser.equals(accuser)) {
if (ring.isBetween(currentAccuser, accuser, accused)) {
if (context.isBetween(accusation.getRingNumber(), currentAccuser, accuser, accused)) {
if (!accused.verify(accusation.getSignature(),
accusation.getWrapped().getAccusation().toByteString())) {
log.trace("Accusation discarded, accusation by: {} accused:{} signature invalid on: {}",
Expand All @@ -746,7 +746,7 @@ private boolean add(AccusationWrapper accusation, Participant accuser, Participa
pendingRebuttals.computeIfAbsent(accused.getId(), d -> roundTimers.schedule(() -> gc(accused),
params.rebuttalTimeout()));
log.debug("{} accused by: {} on ring: {} (replacing: {}) on: {}", accused.getId(), accuser.getId(),
ring.getIndex(), currentAccuser.getId(), node.getId());
accusation.getRingNumber(), currentAccuser.getId(), node.getId());
if (metrics != null) {
metrics.accusations().mark();
}
Expand All @@ -769,7 +769,8 @@ private boolean add(AccusationWrapper accusation, Participant accuser, Participa
}
return false;
}
Participant predecessor = ring.predecessor(accused, m -> (!m.isAccused()) || (m.equals(accuser)));
Participant predecessor = context.predecessor(accusation.getRingNumber(), accused,
m -> (!m.isAccused()) || (m.equals(accuser)));
if (accuser.equals(predecessor)) {
accused.addAccusation(accusation);
if (!accused.equals(node) && !pendingRebuttals.containsKey(accused.getId())) {
Expand Down Expand Up @@ -1937,7 +1938,7 @@ public Gossip rumors(SayWhat request, Digest from) {
}
}

Participant successor = context.ring(ring).successor(member, m -> context.isActive(m.getId()));
Participant successor = context.successor(ring, member, m -> context.isActive(m.getId()));
if (successor == null) {
log.debug("No active successor on ring: {} from: {} on: {}", ring, from, node.getId());
throw new StatusRuntimeException(Status.FAILED_PRECONDITION.withDescription(
Expand Down Expand Up @@ -1999,7 +2000,7 @@ public void update(State request, Digest from) {
Status.INVALID_ARGUMENT.withDescription("No successor of: " + from));
}
Participant member = context.getActiveMember(from);
Participant successor = context.ring(ring).successor(member, m -> context.isActive(m.getId()));
Participant successor = context.successor(ring, member, m -> context.isActive(m.getId()));
if (successor == null) {
log.debug("No successor, invalid update from: {} on ring: {} on: {}", from, ring, node.getId());
throw new StatusRuntimeException(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -240,7 +240,7 @@ public void churn() throws Exception {
for (View v : views) {
Graph<Participant> testGraph = new Graph<>();
for (int i = 0; i < v.getContext().getRingCount(); i++) {
testGraph.addEdge(v.getNode(), v.getContext().ring(i).successor(v.getNode()));
testGraph.addEdge(v.getNode(), v.getContext().successor(i, v.getNode()));
}
assertTrue(testGraph.isSC());
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -223,10 +223,10 @@ private void post() {
}

private void validateConstraints() {
final var reference = views.get(0).getContext().stream(0).collect(Collectors.toSet());
for (int i = 0; i < views.get(0).getContext().getRingCount(); i++) {
final var reference = views.get(0).getContext().ring(i).getRing();
for (View view : views) {
assertEquals(reference, view.getContext().ring(i).getRing());
assertTrue(reference.containsAll(view.getContext().stream(i).toList()));
}
}

Expand All @@ -241,7 +241,7 @@ private void validateConstraints() {
for (View v : views) {
Graph<Participant> testGraph = new Graph<>();
for (int i = 0; i < views.get(0).getContext().getRingCount(); i++) {
testGraph.addEdge(v.getNode(), v.getContext().ring(i).successor(v.getNode()));
testGraph.addEdge(v.getNode(), v.getContext().successor(i, v.getNode()));
}
assertTrue(testGraph.isSC());
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -163,10 +163,10 @@ public void swarm() throws Exception {
+ " members");

if (!largeTests) {
final var reference = views.get(0).getContext().stream(0).collect(Collectors.toSet());
for (int i = 0; i < views.get(0).getContext().getRingCount(); i++) {
final var reference = views.get(0).getContext().ring(i).getRing();
for (View view : views) {
assertEquals(reference, view.getContext().ring(i).getRing());
assertTrue(reference.containsAll(view.getContext().stream(i).toList()));
}
}

Expand All @@ -180,7 +180,7 @@ public void swarm() throws Exception {
for (View v : views) {
Graph<Participant> testGraph = new Graph<>();
for (int i = 0; i < views.get(0).getContext().getRingCount(); i++) {
testGraph.addEdge(v.getNode(), v.getContext().ring(i).successor(v.getNode()));
testGraph.addEdge(v.getNode(), v.getContext().successor(i, v.getNode()));
}
assertTrue(testGraph.isSC());
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -362,6 +362,14 @@ default int majority() {
*/
int size();

/**
* Stream the members of the ring in hashed order
*
* @param ring
* @return
*/
Stream<T> stream(int ring);

/**
* @param ring
* @param predicate
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -180,11 +180,6 @@ public DynamicContext<Z> build() {
*/
void remove(T m);

/**
* @return the indexed Ring<T>
*/
Ring<T> ring(int index);

/**
* @return the Stream of rings managed by the context
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -577,17 +577,6 @@ public void remove(T m) {
remove(m.getId());
}

/**
* @return the indexed Ring<T>
*/
@Override
public Ring<T> ring(int index) {
if (index < 0 || index >= rings.size()) {
throw new IllegalArgumentException("Not a valid ring #: " + index + " max: " + (rings.size() - 1));
}
return rings.get(index);
}

/**
* @return the Stream of rings managed by the context
*/
Expand Down Expand Up @@ -632,6 +621,11 @@ public int size() {
return members.size();
}

@Override
public Stream<T> stream(int ring) {
return ring(ring).stream();
}

@Override
public Stream<T> streamPredecessors(int ring, Digest location, Predicate<T> predicate) {
return ring(ring).streamPredecessors(location, predicate);
Expand Down Expand Up @@ -780,6 +774,16 @@ private Digest[] hashesFor(T m) {
return s;
}

/**
* @return the indexed Ring<T>
*/
private Ring<T> ring(int index) {
if (index < 0 || index >= rings.size()) {
throw new IllegalArgumentException("Not a valid ring #: " + index + " max: " + (rings.size() - 1));
}
return rings.get(index);
}

private Tracked<T> tracking(T m) {
return members.computeIfAbsent(m.getId(), id1 -> {
for (var ring : rings) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -275,6 +275,11 @@ public int size() {
return 0;
}

@Override
public Stream<T> stream(int ring) {
return null;
}

@Override
public Stream<T> streamPredecessors(int ring, Digest location, Predicate<T> predicate) {
return null;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ public void smokin() throws Exception {
assertEquals(context.getRingCount(), compact.getRingCount());

for (int i = 0; i < context.getRingCount(); i++) {
assertEquals(context.ring(i).stream().map(m -> m.getId()).toList(), compact.ring(i).stream().toList(),
assertEquals(context.stream(i).map(m -> m.getId()).toList(), compact.ring(i).stream().toList(),
"Ring " + i + " mismatched");
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,6 @@ public void consistency() throws Exception {

List<Member> successors = context.successors(members.get(1));
assertEquals(members.get(0), successors.get(0));
assertEquals(members.get(1), context.ring(1).successor(members.get(0)));
assertEquals(members.get(1), context.successor(1, members.get(0)));
}
}

0 comments on commit 2dab0d7

Please sign in to comment.