-
Notifications
You must be signed in to change notification settings - Fork 5
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
feat: add the pubsublite sink #21
Merged
Merged
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
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
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
74 changes: 74 additions & 0 deletions
74
src/main/java/com/google/cloud/pubsublite/flink/PubsubLiteSink.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,74 @@ | ||
/* | ||
* Copyright 2021 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.cloud.pubsublite.flink; | ||
|
||
import com.google.cloud.Tuple; | ||
import com.google.cloud.pubsublite.flink.sink.BulkWaitPublisher; | ||
import com.google.cloud.pubsublite.flink.sink.MessagePublisher; | ||
import com.google.cloud.pubsublite.flink.sink.PerServerPublisherCache; | ||
import com.google.cloud.pubsublite.flink.sink.SerializingPublisher; | ||
import com.google.errorprone.annotations.concurrent.GuardedBy; | ||
import java.time.Instant; | ||
import org.apache.flink.configuration.Configuration; | ||
import org.apache.flink.runtime.state.FunctionInitializationContext; | ||
import org.apache.flink.runtime.state.FunctionSnapshotContext; | ||
import org.apache.flink.streaming.api.checkpoint.CheckpointedFunction; | ||
import org.apache.flink.streaming.api.functions.sink.RichSinkFunction; | ||
|
||
public class PubsubLiteSink<T> extends RichSinkFunction<T> implements CheckpointedFunction { | ||
private final PubsubLiteSinkSettings<T> settings; | ||
|
||
@GuardedBy("this") | ||
private transient BulkWaitPublisher<Tuple<T, Instant>> publisher; | ||
|
||
public PubsubLiteSink(PubsubLiteSinkSettings<T> settings) { | ||
this.settings = settings; | ||
} | ||
|
||
@Override | ||
public void initializeState(FunctionInitializationContext functionInitializationContext) | ||
throws Exception {} | ||
|
||
@Override | ||
public synchronized void snapshotState(FunctionSnapshotContext functionSnapshotContext) | ||
throws Exception { | ||
publisher.waitUntilNoOutstandingPublishes(); | ||
} | ||
|
||
@Override | ||
public synchronized void invoke(T value, Context context) throws Exception { | ||
Long timestamp = context.timestamp(); | ||
if (timestamp == null) { | ||
timestamp = context.currentProcessingTime(); | ||
} | ||
publisher.publish(Tuple.of(value, Instant.ofEpochMilli(timestamp))); | ||
} | ||
|
||
@Override | ||
public synchronized void open(Configuration parameters) throws Exception { | ||
super.open(parameters); | ||
publisher = | ||
new SerializingPublisher<>( | ||
new MessagePublisher( | ||
PerServerPublisherCache.getOrCreate(settings.getPublisherConfig())), | ||
settings.serializationSchema()); | ||
} | ||
|
||
@Override | ||
public synchronized void close() throws Exception { | ||
publisher.waitUntilNoOutstandingPublishes(); | ||
} | ||
} |
57 changes: 57 additions & 0 deletions
57
src/main/java/com/google/cloud/pubsublite/flink/PubsubLiteSinkSettings.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 2021 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.cloud.pubsublite.flink; | ||
|
||
import com.google.auto.value.AutoValue; | ||
import com.google.cloud.pubsublite.Message; | ||
import com.google.cloud.pubsublite.TopicPath; | ||
import com.google.cloud.pubsublite.flink.sink.PublisherOptions; | ||
import java.io.Serializable; | ||
|
||
@AutoValue | ||
public abstract class PubsubLiteSinkSettings<InputT> implements Serializable { | ||
// Create a builder which will accept messages of type InputT and serialize them using the | ||
// provided serialization schema. | ||
public static <InputT> Builder<InputT> builder(PubsubLiteSerializationSchema<InputT> schema) { | ||
return new AutoValue_PubsubLiteSinkSettings.Builder<InputT>().setSerializationSchema(schema); | ||
} | ||
|
||
// Create a sink which will accept already serialized pubsub messages/ | ||
public static Builder<Message> messagesBuilder() { | ||
return builder(PubsubLiteSerializationSchema.messageSchema()); | ||
} | ||
|
||
// Required. The path of the topic to publish messages to. | ||
public abstract TopicPath topicPath(); | ||
|
||
// Internal. | ||
abstract PubsubLiteSerializationSchema<InputT> serializationSchema(); | ||
|
||
PublisherOptions getPublisherConfig() { | ||
return PublisherOptions.create(topicPath()); | ||
} | ||
|
||
@AutoValue.Builder | ||
abstract static class Builder<InputT> { | ||
// Required. | ||
public abstract Builder<InputT> setTopicPath(TopicPath value); | ||
|
||
// Internal. | ||
abstract Builder<InputT> setSerializationSchema(PubsubLiteSerializationSchema<InputT> value); | ||
|
||
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. is this the proper user interface? If so, add comments. 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. Added comments. |
||
public abstract PubsubLiteSinkSettings<InputT> build(); | ||
} | ||
} |
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
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
nit. add a property "flink.version" and use it everywhere you have 1.13.0. See pubsublite-beam-io for an example.
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.
Will do in another pr, tianzi suggested that I do this for pubsublite too