Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

shardsManager: fix unchanged shard assignment causes exception #171

Merged
merged 4 commits into from
Jul 17, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
*/
package io.streamnative.oxia.client.shard;

import static com.google.common.base.Throwables.getRootCause;
import static io.streamnative.oxia.client.shard.HashRangeShardStrategy.Xxh332HashRangeShardStrategy;
import static java.util.Collections.unmodifiableMap;
import static java.util.Collections.unmodifiableSet;
Expand Down Expand Up @@ -142,7 +143,7 @@ public void onError(Throwable error) {
}
}
}
log.warn("Failed receiving shard assignments: {}", error.getMessage());
log.warn("Failed receiving shard assignments: {}", getRootCause(error).getMessage());
executor.schedule(
() -> {
if (!closed) {
Expand Down Expand Up @@ -215,7 +216,12 @@ static Map<Long, Shard> recomputeShardHashBoundaries(
.filter(e -> !toDelete.contains(e.getKey()))
.map(Map.Entry::getValue),
updates.stream())
.collect(toMap(Shard::id, identity())));
.collect(
toMap(
Shard::id,
identity(),
// merge function to avoid throw any exception when receive unchanged events
(existing, newValue) -> newValue)));
}

@VisibleForTesting
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@
import io.streamnative.oxia.proto.ShardAssignments;
import io.streamnative.oxia.proto.ShardAssignmentsRequest;
import java.time.Duration;
import java.util.HashSet;
import java.util.Map;
import java.util.Set;
import java.util.concurrent.Executors;
Expand Down Expand Up @@ -88,6 +89,36 @@ void recomputeShardHashBoundaries() {
assertThat(a).isUnmodifiable();
});
}

@Test
void recomputeShardHashBoundariesWithSameValue() {
var existing =
Map.of(
1L, new Shard(1L, "leader 1", new HashRange(1, 3)),
2L, new Shard(2L, "leader 1", new HashRange(7, 9)),
3L, new Shard(3L, "leader 2", new HashRange(4, 6)),
4L, new Shard(4L, "leader 2", new HashRange(10, 11)),
5L, new Shard(5L, "leader 3", new HashRange(11, 12)),
6L, new Shard(6L, "leader 4", new HashRange(13, 13)));
var assignments =
ShardManager.recomputeShardHashBoundaries(
existing,
// same values
new HashSet<>(existing.values()));
assertThat(assignments)
.satisfies(
a -> {
assertThat(a)
.containsOnly(
entry(1L, new Shard(1L, "leader 1", new HashRange(1, 3))),
entry(2L, new Shard(2L, "leader 1", new HashRange(7, 9))),
entry(3L, new Shard(3L, "leader 2", new HashRange(4, 6))),
entry(4L, new Shard(4L, "leader 2", new HashRange(10, 11))),
entry(5L, new Shard(5L, "leader 3", new HashRange(11, 12))),
entry(6L, new Shard(6L, "leader 4", new HashRange(13, 13))));
assertThat(a).isUnmodifiable();
});
}
}

@Nested
Expand Down
Loading