-
Notifications
You must be signed in to change notification settings - Fork 18
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: add pubsublite.ordering.mode to kafka connector (#228)
* feat: add pubsublite.ordering.mode to kafka connector This is useful for migration cases using the kafka wire protocol. * feat: add pubsublite.ordering.mode to kafka connector This is useful for migration cases using the kafka wire protocol. Also clean up dependency management. * feat: add pubsublite.ordering.mode to kafka connector This is useful for migration cases using the kafka wire protocol. Also clean up dependency management. * feat: add pubsublite.ordering.mode to kafka connector This is useful for migration cases using the kafka wire protocol. Also clean up dependency management. * feat: add pubsublite.ordering.mode to kafka connector This is useful for migration cases using the kafka wire protocol. Also clean up dependency management. * feat: add pubsublite.ordering.mode to kafka connector This is useful for migration cases using the kafka wire protocol. Also clean up dependency management.
- Loading branch information
1 parent
423141f
commit c499c39
Showing
9 changed files
with
238 additions
and
190 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
57 changes: 57 additions & 0 deletions
57
src/main/java/com/google/pubsublite/kafka/sink/KafkaPartitionRoutingPolicy.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,57 @@ | ||
/* | ||
* Copyright 2023 Google LLC | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
package com.google.pubsublite.kafka.sink; | ||
|
||
import static com.google.cloud.pubsublite.internal.ExtractStatus.toCanonical; | ||
|
||
import com.google.api.gax.rpc.StatusCode.Code; | ||
import com.google.cloud.pubsublite.Partition; | ||
import com.google.cloud.pubsublite.internal.CheckedApiException; | ||
import com.google.cloud.pubsublite.internal.RoutingPolicy; | ||
import com.google.cloud.pubsublite.proto.PubSubMessage; | ||
|
||
/** A routing policy that extracts the original kafka partition and routes to that partition. */ | ||
class KafkaPartitionRoutingPolicy implements RoutingPolicy { | ||
private final long numPartitions; | ||
|
||
KafkaPartitionRoutingPolicy(long numPartitions) { | ||
this.numPartitions = numPartitions; | ||
} | ||
|
||
@Override | ||
public Partition route(PubSubMessage message) throws CheckedApiException { | ||
Partition partition = getPartition(message); | ||
if (partition.value() >= numPartitions) { | ||
throw new CheckedApiException( | ||
"Kafka topic has more partitions than Pub/Sub Lite topic. OrderingMode.KAFKA cannot be used.", | ||
Code.FAILED_PRECONDITION); | ||
} | ||
return partition; | ||
} | ||
|
||
private Partition getPartition(PubSubMessage message) throws CheckedApiException { | ||
try { | ||
return Partition.of( | ||
Long.parseLong( | ||
message | ||
.getAttributesOrThrow(Constants.KAFKA_PARTITION_HEADER) | ||
.getValues(0) | ||
.toStringUtf8())); | ||
} catch (Throwable t) { | ||
throw toCanonical(t); | ||
} | ||
} | ||
} |
23 changes: 23 additions & 0 deletions
23
src/main/java/com/google/pubsublite/kafka/sink/OrderingMode.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
/* | ||
* Copyright 2023 Google LLC | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
package com.google.pubsublite.kafka.sink; | ||
|
||
public enum OrderingMode { | ||
/* Order based on the standard Pub/Sub Lite logic. */ | ||
DEFAULT, | ||
/* Send messages to the same partition index they were from in Kafka. */ | ||
KAFKA | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.