-
Notifications
You must be signed in to change notification settings - Fork 4.5k
[BEAM-53] Pub/sub client with gRPC implementation #120
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
Changes from all commits
bfa5629
b78c947
ecf057f
d040526
3d69f09
06c37a0
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,322 @@ | ||
| /* | ||
| * Licensed to the Apache Software Foundation (ASF) under one | ||
| * or more contributor license agreements. See the NOTICE file | ||
| * distributed with this work for additional information | ||
| * regarding copyright ownership. The ASF licenses this file | ||
| * to you 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.cloud.dataflow.sdk.io; | ||
|
|
||
| import com.google.api.client.repackaged.com.google.common.base.Preconditions; | ||
| import java.io.IOException; | ||
| import java.io.Serializable; | ||
| import java.util.Collection; | ||
|
|
||
| /** | ||
| * A helper interface for talking to Pubsub via an underlying transport. | ||
| */ | ||
| public interface PubsubClient extends AutoCloseable { | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. It occurs to me that this PR would really benefit from a follow-up with an in-memory testing fake, with a test suite that can be applied to both it and the gRPC implementation. |
||
| /** | ||
| * Path representing a cloud project id. | ||
| */ | ||
| class ProjectPath implements Serializable { | ||
| private final String path; | ||
|
|
||
| public ProjectPath(String path) { | ||
| this.path = path; | ||
| } | ||
|
|
||
| public String getPath() { | ||
| return path; | ||
| } | ||
|
|
||
| @Override | ||
| public boolean equals(Object o) { | ||
| if (this == o) { | ||
| return true; | ||
| } | ||
| if (o == null || getClass() != o.getClass()) { | ||
| return false; | ||
| } | ||
|
|
||
| ProjectPath that = (ProjectPath) o; | ||
|
|
||
| return path.equals(that.path); | ||
|
|
||
| } | ||
|
|
||
| @Override | ||
| public int hashCode() { | ||
| return path.hashCode(); | ||
| } | ||
|
|
||
| @Override | ||
| public String toString() { | ||
| return path; | ||
| } | ||
|
|
||
| public static ProjectPath fromId(String projectId) { | ||
| return new ProjectPath(String.format("projects/%s", projectId)); | ||
| } | ||
| } | ||
|
|
||
| /** | ||
| * Path representing a Pubsub subscription. | ||
| */ | ||
| class SubscriptionPath implements Serializable { | ||
| private final String path; | ||
|
|
||
| public SubscriptionPath(String path) { | ||
| this.path = path; | ||
| } | ||
|
|
||
| public String getPath() { | ||
| return path; | ||
| } | ||
|
|
||
| public String getV1Beta1Path() { | ||
| String[] splits = path.split("/"); | ||
| Preconditions.checkState(splits.length == 4); | ||
| return String.format("/subscriptions/%s/%s", splits[1], splits[3]); | ||
| } | ||
|
|
||
| @Override | ||
| public boolean equals(Object o) { | ||
| if (this == o) { | ||
| return true; | ||
| } | ||
| if (o == null || getClass() != o.getClass()) { | ||
| return false; | ||
| } | ||
| SubscriptionPath that = (SubscriptionPath) o; | ||
| return path.equals(that.path); | ||
| } | ||
|
|
||
| @Override | ||
| public int hashCode() { | ||
| return path.hashCode(); | ||
| } | ||
|
|
||
| @Override | ||
| public String toString() { | ||
| return path; | ||
| } | ||
|
|
||
| public static SubscriptionPath fromName(String projectId, String subscriptionName) { | ||
| return new SubscriptionPath(String.format("projects/%s/subscriptions/%s", | ||
| projectId, subscriptionName)); | ||
| } | ||
| } | ||
|
|
||
| /** | ||
| * Path representing a Pubsub topic. | ||
| */ | ||
| class TopicPath implements Serializable { | ||
| private final String path; | ||
|
|
||
| public TopicPath(String path) { | ||
| this.path = path; | ||
| } | ||
|
|
||
| public String getPath() { | ||
| return path; | ||
| } | ||
|
|
||
| public String getV1Beta1Path() { | ||
| String[] splits = path.split("/"); | ||
| Preconditions.checkState(splits.length == 4); | ||
| return String.format("/topics/%s/%s", splits[1], splits[3]); | ||
| } | ||
|
|
||
| @Override | ||
| public boolean equals(Object o) { | ||
| if (this == o) { | ||
| return true; | ||
| } | ||
| if (o == null || getClass() != o.getClass()) { | ||
| return false; | ||
| } | ||
| TopicPath topicPath = (TopicPath) o; | ||
| return path.equals(topicPath.path); | ||
| } | ||
|
|
||
| @Override | ||
| public int hashCode() { | ||
| return path.hashCode(); | ||
| } | ||
|
|
||
| @Override | ||
| public String toString() { | ||
| return path; | ||
| } | ||
|
|
||
| public static TopicPath fromName(String projectId, String topicName) { | ||
| return new TopicPath(String.format("projects/%s/topics/%s", projectId, topicName)); | ||
| } | ||
| } | ||
|
|
||
| /** | ||
| * A message to be sent to Pubsub. | ||
| */ | ||
| class OutgoingMessage { | ||
| /** | ||
| * Underlying (encoded) element. | ||
| */ | ||
| public final byte[] elementBytes; | ||
|
|
||
| /** | ||
| * Timestamp for element (ms since epoch). | ||
| */ | ||
| public final long timestampMsSinceEpoch; | ||
|
|
||
| public OutgoingMessage(byte[] elementBytes, long timestampMsSinceEpoch) { | ||
| this.elementBytes = elementBytes; | ||
| this.timestampMsSinceEpoch = timestampMsSinceEpoch; | ||
| } | ||
| } | ||
|
|
||
| /** | ||
| * A message received from Pubsub. | ||
| */ | ||
| class IncomingMessage { | ||
| /** | ||
| * Underlying (encoded) element. | ||
| */ | ||
| public final byte[] elementBytes; | ||
|
|
||
| /** | ||
| * Timestamp for element (ms since epoch). Either Pubsub's processing time, | ||
| * or the custom timestamp associated with the message. | ||
| */ | ||
| public final long timestampMsSinceEpoch; | ||
|
|
||
| /** | ||
| * Timestamp (in system time) at which we requested the message (ms since epoch). | ||
| */ | ||
| public final long requestTimeMsSinceEpoch; | ||
|
|
||
| /** | ||
| * Id to pass back to Pubsub to acknowledge receipt of this message. | ||
| */ | ||
| public final String ackId; | ||
|
|
||
| /** | ||
| * Id to pass to the runner to distinguish this message from all others. | ||
| */ | ||
| public final byte[] recordId; | ||
|
|
||
| public IncomingMessage( | ||
| byte[] elementBytes, | ||
| long timestampMsSinceEpoch, | ||
| long requestTimeMsSinceEpoch, | ||
| String ackId, | ||
| byte[] recordId) { | ||
| this.elementBytes = elementBytes; | ||
| this.timestampMsSinceEpoch = timestampMsSinceEpoch; | ||
| this.requestTimeMsSinceEpoch = requestTimeMsSinceEpoch; | ||
| this.ackId = ackId; | ||
| this.recordId = recordId; | ||
| } | ||
| } | ||
|
|
||
| /** | ||
| * Gracefully close the underlying transport. | ||
| */ | ||
| @Override | ||
| void close(); | ||
|
|
||
|
|
||
| /** | ||
| * Publish {@code outgoingMessages} to Pubsub {@code topic}. Return number of messages | ||
| * published. | ||
| * | ||
| * @throws IOException | ||
| */ | ||
| int publish(TopicPath topic, Iterable<OutgoingMessage> outgoingMessages) throws IOException; | ||
|
|
||
| /** | ||
| * Request the next batch of up to {@code batchSize} messages from {@code subscription}. | ||
| * Return the received messages, or empty collection if none were available. Does not | ||
| * wait for messages to arrive. Returned messages will record heir request time | ||
| * as {@code requestTimeMsSinceEpoch}. | ||
| * | ||
| * @throws IOException | ||
| */ | ||
| Collection<IncomingMessage> pull( | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. It would seem pretty normal to have explicit For example, if Related - publishing and subscribing seem to be two separable activities for pub/sub but this client seems like roughly the (disjoint) union of the two.
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This class a) captures common bookkeeping between unbounded pubsub source/sink and benchmarking code in a way which avoids making any grpc or protoc details public b) provides a hook for (as yet unwritten) mocks. Feels like scope creep for it to go any further.
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Given that this is blocking other work, I'm OK to wait on developing it into a richer client. What we really need is a thick Pubsub client, which I agree is a slightly larger scope that your immediate goals. No need to do everything up front. An intermediate approach that leverages the type system to prevent silly errors and improves readability: a simple rich string wrapper to make it explicitly type-incompatible with
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Done. Decided not to add dep on com.google.auto.value:auto-value though it is certainly tempting. |
||
| long requestTimeMsSinceEpoch, SubscriptionPath subscription, int batchSize) | ||
| throws IOException; | ||
|
|
||
| /** | ||
| * Acknowldege messages from {@code subscription} with {@code ackIds}. | ||
| * | ||
| * @throws IOException | ||
| */ | ||
| void acknowledge(SubscriptionPath subscription, Iterable<String> ackIds) throws IOException; | ||
|
|
||
| /** | ||
| * Modify the ack deadline for messages from {@code subscription} with {@code ackIds} to | ||
| * be {@code deadlineSeconds} from now. | ||
| * | ||
| * @throws IOException | ||
| */ | ||
| void modifyAckDeadline( | ||
| SubscriptionPath subscription, Iterable<String> ackIds, | ||
| int deadlineSeconds) | ||
| throws IOException; | ||
|
|
||
| /** | ||
| * Create {@code topic}. | ||
| * | ||
| * @throws IOException | ||
| */ | ||
| void createTopic(TopicPath topic) throws IOException; | ||
|
|
||
| /* | ||
| * Delete {@code topic}. | ||
| * | ||
| * @throws IOException | ||
| */ | ||
| void deleteTopic(TopicPath topic) throws IOException; | ||
|
|
||
| /** | ||
| * Return a list of topics for {@code project}. | ||
| * | ||
| * @throws IOException | ||
| */ | ||
| Collection<TopicPath> listTopics(ProjectPath project) throws IOException; | ||
|
|
||
| /** | ||
| * Create {@code subscription} to {@code topic}. | ||
| * | ||
| * @throws IOException | ||
| */ | ||
| void createSubscription( | ||
| TopicPath topic, SubscriptionPath subscription, | ||
| int ackDeadlineSeconds) throws IOException; | ||
|
|
||
| /** | ||
| * Delete {@code subscription}. | ||
| * | ||
| * @throws IOException | ||
| */ | ||
| void deleteSubscription(SubscriptionPath subscription) throws IOException; | ||
|
|
||
| /** | ||
| * Return a list of subscriptions for {@code topic} in {@code project}. | ||
| * | ||
| * @throws IOException | ||
| */ | ||
| Collection<SubscriptionPath> listSubscriptions(ProjectPath project, TopicPath topic) | ||
| throws IOException; | ||
| } | ||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Whoops