Skip to content

fix: Change RoutingPolicy to accept the whole message, not just the key field #1344

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

Merged
merged 2 commits into from
Feb 22, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 12 additions & 0 deletions google-cloud-pubsublite/clirr-ignored-differences.xml
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,18 @@
<method>*</method>
<to>**</to>
</difference>
<difference>
<differenceType>7009</differenceType>
<className>com/google/cloud/pubsublite/internal/**</className>
<method>*</method>
<to>**</to>
</difference>
<difference>
<differenceType>7002</differenceType>
<className>com/google/cloud/pubsublite/internal/**</className>
<method>*</method>
<to>**</to>
</difference>
<difference>
<differenceType>7013</differenceType>
<className>com/google/cloud/pubsublite/internal/**</className>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@

import com.google.api.gax.rpc.ApiException;
import com.google.cloud.pubsublite.Partition;
import com.google.cloud.pubsublite.proto.PubSubMessage;
import com.google.common.hash.HashCode;
import com.google.common.hash.Hashing;
import com.google.protobuf.ByteString;
Expand All @@ -40,13 +41,16 @@ public DefaultRoutingPolicy(long numPartitions) throws ApiException {
}

@Override
public Partition routeWithoutKey() throws ApiException {
public Partition route(PubSubMessage message) {
return message.getKey().isEmpty() ? routeWithoutKey() : routeWithKey(message.getKey());
}

private Partition routeWithoutKey() throws ApiException {
long index = withoutKeyCounter.incrementAndGet();
return Partition.of(index % numPartitions);
}

@Override
public Partition route(ByteString messageKey) throws ApiException {
private Partition routeWithKey(ByteString messageKey) throws ApiException {
HashCode code = Hashing.sha256().hashBytes(messageKey.asReadOnlyByteBuffer());
checkArgument(code.bits() == 256); // sanity check.
BigInteger bigEndianValue = new BigInteger(/*signum=*/ 1, code.asBytes());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,15 +17,13 @@
package com.google.cloud.pubsublite.internal;

import com.google.cloud.pubsublite.Partition;
import com.google.protobuf.ByteString;
import com.google.cloud.pubsublite.proto.PubSubMessage;

// Route the user message key to a given partition.
public interface RoutingPolicy {
interface Factory {
RoutingPolicy newPolicy(long numPartitions);
}
// Route a message without a key to a partition.
Partition routeWithoutKey() throws CheckedApiException;
// Route a message with a key to a partition.
Partition route(ByteString messageKey) throws CheckedApiException;
// Route a message to a partition.
Partition route(PubSubMessage messageKey) throws CheckedApiException;
}
Original file line number Diff line number Diff line change
Expand Up @@ -58,10 +58,7 @@ private PartitionsWithRouting(

public ApiFuture<MessageMetadata> publish(PubSubMessage message) throws CheckedApiException {
try {
Partition routedPartition =
message.getKey().isEmpty()
? routingPolicy.routeWithoutKey()
: routingPolicy.route(message.getKey());
Partition routedPartition = routingPolicy.route(message);
checkState(
publishers.containsKey(routedPartition),
"Routed to partition %s for which there is no publisher available.",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -48,8 +48,7 @@ public class RoutingPublisher extends ProxyService implements Publisher<MessageM
@Override
public ApiFuture<MessageMetadata> publish(PubSubMessage message) {
try {
Partition routedPartition =
message.getKey().isEmpty() ? policy.routeWithoutKey() : policy.route(message.getKey());
Partition routedPartition = policy.route(message);
checkState(
partitionPublishers.containsKey(routedPartition),
"Routed to partition %s for which there is no publisher available.",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
import static com.google.common.truth.Truth.assertThat;

import com.google.cloud.pubsublite.Partition;
import com.google.cloud.pubsublite.proto.PubSubMessage;
import com.google.common.collect.ImmutableMap;
import com.google.gson.Gson;
import com.google.protobuf.ByteString;
Expand Down Expand Up @@ -54,7 +55,7 @@ public void routingPerformedCorrectly() throws Exception {
Map<ByteString, Partition> map = loadTestCases();
ImmutableMap.Builder<ByteString, Partition> results = ImmutableMap.builder();
for (ByteString key : map.keySet()) {
results.put(key, policy.route(key));
results.put(key, policy.route(PubSubMessage.newBuilder().setKey(key).build()));
}
assertThat(results.build()).containsExactlyEntriesIn(map);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -111,8 +111,8 @@ public void testPublishWithKey() throws Exception {
PubSubMessage.newBuilder().setKey(ByteString.copyFromUtf8("0")).build();
PubSubMessage message1 =
PubSubMessage.newBuilder().setKey(ByteString.copyFromUtf8("1")).build();
when(mockRoutingPolicy.route(message0.getKey())).thenReturn(Partition.of(0));
when(mockRoutingPolicy.route(message1.getKey())).thenReturn(Partition.of(1));
when(mockRoutingPolicy.route(message0)).thenReturn(Partition.of(0));
when(mockRoutingPolicy.route(message1)).thenReturn(Partition.of(1));

Future<?> unusedFuture0 = publisher.publish(message0);
Future<?> unusedFuture1 = publisher.publish(message1);
Expand All @@ -128,9 +128,8 @@ public void testPublishWithoutKey() throws Exception {
PubSubMessage messageB =
PubSubMessage.newBuilder().setData(ByteString.copyFromUtf8("b")).build();

when(mockRoutingPolicy.routeWithoutKey())
.thenReturn(Partition.of(0))
.thenReturn(Partition.of(1));
when(mockRoutingPolicy.route(messageA)).thenReturn(Partition.of(0));
when(mockRoutingPolicy.route(messageB)).thenReturn(Partition.of(1));

Future<?> unusedFutureA = publisher.publish(messageA);
Future<?> unusedFutureB = publisher.publish(messageB);
Expand All @@ -143,7 +142,7 @@ public void testPublishWithoutKey() throws Exception {
public void testPublishWithBadRouting() throws Exception {
PubSubMessage message = PubSubMessage.newBuilder().build();

when(mockRoutingPolicy.routeWithoutKey()).thenReturn(Partition.of(4));
when(mockRoutingPolicy.route(message)).thenReturn(Partition.of(4));
Future<?> unusedFuture = publisher.publish(message);

ApiExceptionMatcher.assertThrowableMatches(
Expand Down Expand Up @@ -191,9 +190,9 @@ public void testIncreaseSucceeds() throws Exception {
PubSubMessage.newBuilder().setKey(ByteString.copyFromUtf8("1")).build();
PubSubMessage message2 =
PubSubMessage.newBuilder().setKey(ByteString.copyFromUtf8("2")).build();
when(mockRoutingPolicy.route(message0.getKey())).thenReturn(Partition.of(0));
when(mockRoutingPolicy.route(message1.getKey())).thenReturn(Partition.of(1));
when(mockRoutingPolicy.route(message2.getKey())).thenReturn(Partition.of(2));
when(mockRoutingPolicy.route(message0)).thenReturn(Partition.of(0));
when(mockRoutingPolicy.route(message1)).thenReturn(Partition.of(1));
when(mockRoutingPolicy.route(message2)).thenReturn(Partition.of(2));

Future<?> unusedFuture0 = publisher.publish(message0);
Future<?> unusedFuture1 = publisher.publish(message1);
Expand All @@ -212,8 +211,8 @@ public void testDecreaseIgnored() throws Exception {
PubSubMessage.newBuilder().setKey(ByteString.copyFromUtf8("0")).build();
PubSubMessage message1 =
PubSubMessage.newBuilder().setKey(ByteString.copyFromUtf8("1")).build();
when(mockRoutingPolicy.route(message0.getKey())).thenReturn(Partition.of(0));
when(mockRoutingPolicy.route(message1.getKey())).thenReturn(Partition.of(1));
when(mockRoutingPolicy.route(message0)).thenReturn(Partition.of(0));
when(mockRoutingPolicy.route(message1)).thenReturn(Partition.of(1));

Future<?> unusedFuture0 = publisher.publish(message0);
Future<?> unusedFuture1 = publisher.publish(message1);
Expand All @@ -231,8 +230,8 @@ public void testNoopConfigUpdate() throws Exception {
PubSubMessage.newBuilder().setKey(ByteString.copyFromUtf8("0")).build();
PubSubMessage message1 =
PubSubMessage.newBuilder().setKey(ByteString.copyFromUtf8("1")).build();
when(mockRoutingPolicy.route(message0.getKey())).thenReturn(Partition.of(0));
when(mockRoutingPolicy.route(message1.getKey())).thenReturn(Partition.of(1));
when(mockRoutingPolicy.route(message0)).thenReturn(Partition.of(0));
when(mockRoutingPolicy.route(message1)).thenReturn(Partition.of(1));

Future<?> unusedFuture0 = publisher.publish(message0);
Future<?> unusedFuture1 = publisher.publish(message1);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,7 @@ public void cancelOutstandingCancelsAll() throws Exception {
public void publishValidRoute() throws Exception {
PubSubMessage message =
PubSubMessage.newBuilder().setKey(ByteString.copyFromUtf8("abc")).build();
when(routingPolicy.route(message.getKey())).thenReturn(Partition.of(1));
when(routingPolicy.route(message)).thenReturn(Partition.of(1));
MessageMetadata meta = MessageMetadata.of(Partition.of(1), Offset.of(3));
when(publisher1.publish(message)).thenReturn(ApiFutures.immediateFuture(meta));
ApiFuture<MessageMetadata> fut = routing.publish(message);
Expand All @@ -97,7 +97,7 @@ public void publishValidRoute() throws Exception {
public void publishInvalidRoute() throws Exception {
PubSubMessage message =
PubSubMessage.newBuilder().setKey(ByteString.copyFromUtf8("abc")).build();
when(routingPolicy.route(message.getKey())).thenReturn(Partition.of(77));
when(routingPolicy.route(message)).thenReturn(Partition.of(77));
ApiFuture<MessageMetadata> fut = routing.publish(message);
ApiExceptionMatcher.assertFutureThrowsCode(fut, Code.FAILED_PRECONDITION);
}
Expand Down