-
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 DeserializingSplitReader #11
Merged
Merged
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
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
40 changes: 40 additions & 0 deletions
40
src/main/java/com/google/cloud/pubsublite/flink/MessageTimestampExtractor.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,40 @@ | ||
/* | ||
* 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.Timestamp; | ||
import com.google.cloud.pubsublite.SequencedMessage; | ||
import java.io.Serializable; | ||
import java.time.Instant; | ||
|
||
public interface MessageTimestampExtractor extends Serializable { | ||
static MessageTimestampExtractor publishTimeExtractor() { | ||
return (MessageTimestampExtractor) | ||
m -> Timestamp.fromProto(m.publishTime()).toDate().toInstant(); | ||
} | ||
|
||
static MessageTimestampExtractor eventTimeExtractor() { | ||
return (MessageTimestampExtractor) | ||
m -> { | ||
if (m.message().eventTime().isPresent()) { | ||
return Timestamp.fromProto(m.message().eventTime().get()).toDate().toInstant(); | ||
} | ||
return Timestamp.fromProto(m.publishTime()).toDate().toInstant(); | ||
}; | ||
} | ||
|
||
Instant timestamp(SequencedMessage m) throws Exception; | ||
} |
61 changes: 61 additions & 0 deletions
61
src/main/java/com/google/cloud/pubsublite/flink/PubsubLiteDeserializationSchema.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,61 @@ | ||
/* | ||
* 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.pubsublite.SequencedMessage; | ||
import java.io.Serializable; | ||
import javax.annotation.Nullable; | ||
import org.apache.flink.api.common.serialization.DeserializationSchema; | ||
import org.apache.flink.api.common.typeinfo.TypeInformation; | ||
|
||
public interface PubsubLiteDeserializationSchema<T> extends Serializable { | ||
|
||
static <T> PubsubLiteDeserializationSchema<T> dataOnly(DeserializationSchema<T> schema) { | ||
return new PubsubLiteDeserializationSchema<T>() { | ||
@Override | ||
public void open(DeserializationSchema.InitializationContext context) throws Exception { | ||
schema.open(context); | ||
} | ||
|
||
@Override | ||
public T deserialize(SequencedMessage message) throws Exception { | ||
return schema.deserialize(message.message().data().toByteArray()); | ||
} | ||
|
||
@Override | ||
public TypeInformation<T> getProducedType() { | ||
return schema.getProducedType(); | ||
} | ||
}; | ||
} | ||
|
||
void open(DeserializationSchema.InitializationContext context) throws Exception; | ||
|
||
/** | ||
* Deserialize a Pub/Sub Lite message | ||
* | ||
* <p>If a message cannot be deserialized, the schema can either thrown a exception which will | ||
* fail the source, or it can return null in which case the source will skip the message and | ||
* proceed. | ||
* | ||
* @param message The pub/sub lite message | ||
* @return The deserialized message as an object (null if the message cannot be deserialized). | ||
*/ | ||
@Nullable | ||
T deserialize(SequencedMessage message) throws Exception; | ||
|
||
TypeInformation<T> getProducedType(); | ||
} |
82 changes: 82 additions & 0 deletions
82
src/main/java/com/google/cloud/pubsublite/flink/reader/DeserializingSplitReader.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,82 @@ | ||
/* | ||
* 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.reader; | ||
|
||
import com.google.cloud.pubsublite.SequencedMessage; | ||
import com.google.cloud.pubsublite.flink.MessageTimestampExtractor; | ||
import com.google.cloud.pubsublite.flink.PubsubLiteDeserializationSchema; | ||
import com.google.cloud.pubsublite.flink.split.SubscriptionPartitionSplit; | ||
import com.google.common.collect.Multimap; | ||
import java.io.IOException; | ||
import java.time.Instant; | ||
import java.util.Map; | ||
import java.util.Optional; | ||
import javax.annotation.Nullable; | ||
import org.apache.flink.connector.base.source.reader.RecordsBySplits; | ||
import org.apache.flink.connector.base.source.reader.RecordsWithSplitIds; | ||
import org.apache.flink.connector.base.source.reader.splitreader.SplitReader; | ||
import org.apache.flink.connector.base.source.reader.splitreader.SplitsChange; | ||
|
||
public class DeserializingSplitReader<T> | ||
implements SplitReader<Record<T>, SubscriptionPartitionSplit> { | ||
final SplitReader<SequencedMessage, SubscriptionPartitionSplit> underlying; | ||
final PubsubLiteDeserializationSchema<T> schema; | ||
final MessageTimestampExtractor extractor; | ||
|
||
public DeserializingSplitReader( | ||
SplitReader<SequencedMessage, SubscriptionPartitionSplit> underlying, | ||
PubsubLiteDeserializationSchema<T> schema, | ||
MessageTimestampExtractor extractor) { | ||
this.underlying = underlying; | ||
this.schema = schema; | ||
this.extractor = extractor; | ||
} | ||
|
||
@Override | ||
public RecordsBySplits<Record<T>> fetch() throws IOException { | ||
RecordsBySplits.Builder<Record<T>> builder = new RecordsBySplits.Builder<>(); | ||
RecordsWithSplitIds<SequencedMessage> fetch = underlying.fetch(); | ||
Multimap<String, SequencedMessage> messageMap = ReaderUtils.recordWithSplitsToMap(fetch); | ||
for (Map.Entry<String, SequencedMessage> entry : messageMap.entries()) { | ||
String split = entry.getKey(); | ||
SequencedMessage message = entry.getValue(); | ||
try { | ||
@Nullable T value = schema.deserialize(message); | ||
Instant timestamp = extractor.timestamp(message); | ||
builder.add(split, Record.create(Optional.ofNullable(value), message.offset(), timestamp)); | ||
} catch (Exception e) { | ||
throw new IOException(e); | ||
} | ||
} | ||
builder.addFinishedSplits(fetch.finishedSplits()); | ||
return builder.build(); | ||
} | ||
|
||
@Override | ||
public void handleSplitsChanges(SplitsChange<SubscriptionPartitionSplit> splitsChange) { | ||
underlying.handleSplitsChanges(splitsChange); | ||
} | ||
|
||
@Override | ||
public void wakeUp() { | ||
underlying.wakeUp(); | ||
} | ||
|
||
@Override | ||
public void close() throws Exception { | ||
underlying.close(); | ||
} | ||
} |
32 changes: 32 additions & 0 deletions
32
src/main/java/com/google/cloud/pubsublite/flink/reader/ReaderUtils.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,32 @@ | ||
/* | ||
* 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.reader; | ||
|
||
import com.google.common.collect.ImmutableListMultimap; | ||
import com.google.common.collect.Multimap; | ||
import org.apache.flink.connector.base.source.reader.RecordsWithSplitIds; | ||
|
||
public class ReaderUtils { | ||
public static <T> Multimap<String, T> recordWithSplitsToMap(RecordsWithSplitIds<T> records) { | ||
ImmutableListMultimap.Builder<String, T> builder = ImmutableListMultimap.builder(); | ||
for (String split = records.nextSplit(); split != null; split = records.nextSplit()) { | ||
for (T m = records.nextRecordFromSplit(); m != null; m = records.nextRecordFromSplit()) { | ||
builder.put(split, m); | ||
} | ||
} | ||
return builder.build(); | ||
} | ||
} |
34 changes: 34 additions & 0 deletions
34
src/main/java/com/google/cloud/pubsublite/flink/reader/Record.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,34 @@ | ||
/* | ||
* 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.reader; | ||
|
||
import com.google.auto.value.AutoValue; | ||
import com.google.cloud.pubsublite.Offset; | ||
import java.time.Instant; | ||
import java.util.Optional; | ||
|
||
@AutoValue | ||
dpcollins-google marked this conversation as resolved.
Show resolved
Hide resolved
|
||
public abstract class Record<T> { | ||
public abstract Optional<T> value(); | ||
|
||
public abstract Offset offset(); | ||
|
||
public abstract Instant timestamp(); | ||
|
||
public static <T> Record<T> create(Optional<T> value, Offset offset, Instant timestamp) { | ||
return new AutoValue_Record<>(value, offset, timestamp); | ||
} | ||
} |
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.
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.
Mark as
@Nullable
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.
Done