Skip to content

Commit

Permalink
disallow duplicate workers
Browse files Browse the repository at this point in the history
  • Loading branch information
dbw9580 committed Nov 30, 2023
1 parent 0fd41af commit d93f454
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,13 @@ public WorkerClusterView(Iterable<WorkerInfo> workers) {
WorkerClusterView(Iterable<WorkerInfo> workers, Instant createdTime) {
mWorkers = Streams.stream(workers)
.collect(ImmutableMap.toImmutableMap(
WorkerInfo::getIdentity, Function.identity(), (first, second) -> second));
WorkerInfo::getIdentity,
Function.identity(),
(first, second) -> {
throw new IllegalArgumentException(
String.format("duplicate workers with the same ID: first: %s, second: %s",
first, second));
}));
// Note Instant.now() uses the system clock and is NOT monotonic
// which is fine because we want to invalidate stale snapshots based on wall clock time
mInstantCreated = createdTime;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -65,9 +65,9 @@ public void keyedByWorkerId() {
.setAddress(new WorkerNetAddress().setHost("host2"));
WorkerInfo worker3 = new WorkerInfo().setIdentity(id)
.setAddress(new WorkerNetAddress().setHost("host3"));
WorkerClusterView view = new WorkerClusterView(ImmutableList.of(worker1, worker2, worker3));
assertEquals(1, view.size());
assertEquals(Optional.of(id), view.getWorkerById(id).map(WorkerInfo::getIdentity));
List<WorkerInfo> workers = ImmutableList.of(worker1, worker2, worker3);
assertThrows("duplicate workers not allowed", IllegalArgumentException.class,
() -> new WorkerClusterView(workers));
}

@Test
Expand Down

0 comments on commit d93f454

Please sign in to comment.