Repository navigation
feat: Separates Offsets from different context (Spark vs PSL) #404
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
Changes from all commits
Commits
Show all changes
5 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 hidden or 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 hidden or 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 hidden or 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
44 changes: 44 additions & 0 deletions
44
...k-sql-streaming/src/main/java/com/google/cloud/pubsublite/spark/SparkPartitionOffset.java
This file contains hidden or 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,44 @@ | ||
| /* | ||
| * Copyright 2020 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.spark; | ||
|
|
||
| import com.google.auto.value.AutoValue; | ||
| import com.google.cloud.pubsublite.Partition; | ||
| import java.io.Serializable; | ||
| import org.apache.spark.sql.sources.v2.reader.streaming.PartitionOffset; | ||
|
|
||
| @AutoValue | ||
| abstract class SparkPartitionOffset implements PartitionOffset, Serializable { | ||
| private static final long serialVersionUID = -3398208694782540866L; | ||
|
|
||
| abstract Partition partition(); | ||
|
|
||
| abstract long offset(); | ||
|
|
||
| public static Builder builder() { | ||
| return new AutoValue_SparkPartitionOffset.Builder(); | ||
| } | ||
|
|
||
| @AutoValue.Builder | ||
| public abstract static class Builder { | ||
| public abstract Builder partition(Partition partition); | ||
|
|
||
| public abstract Builder offset(long offset); | ||
|
|
||
| public abstract SparkPartitionOffset build(); | ||
| } | ||
| } |
118 changes: 118 additions & 0 deletions
118
...park-sql-streaming/src/main/java/com/google/cloud/pubsublite/spark/SparkSourceOffset.java
This file contains hidden or 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,118 @@ | ||
| /* | ||
| * Copyright 2020 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.spark; | ||
|
|
||
| import com.fasterxml.jackson.core.JsonProcessingException; | ||
| import com.fasterxml.jackson.databind.ObjectMapper; | ||
| import com.fasterxml.jackson.databind.SerializationFeature; | ||
| import com.google.cloud.pubsublite.Partition; | ||
| import com.google.common.collect.ImmutableList; | ||
| import com.google.common.collect.ImmutableMap; | ||
| import java.io.IOException; | ||
| import java.util.Collections; | ||
| import java.util.HashMap; | ||
| import java.util.Map; | ||
| import java.util.Objects; | ||
| import java.util.stream.Collectors; | ||
|
|
||
| public final class SparkSourceOffset | ||
| extends org.apache.spark.sql.sources.v2.reader.streaming.Offset { | ||
| private static final ObjectMapper objectMapper = | ||
| new ObjectMapper().configure(SerializationFeature.ORDER_MAP_ENTRIES_BY_KEYS, true); | ||
|
|
||
| // Using a map to ensure unique partitions. | ||
| private final ImmutableMap<Partition, SparkPartitionOffset> partitionOffsetMap; | ||
|
|
||
| public SparkSourceOffset(Map<Partition, SparkPartitionOffset> map) { | ||
| validateMap(map); | ||
| this.partitionOffsetMap = ImmutableMap.copyOf(map); | ||
| } | ||
|
|
||
| private static void validateMap(Map<Partition, SparkPartitionOffset> map) { | ||
| map.forEach( | ||
| (k, v) -> { | ||
| assert Objects.equals(k, v.partition()) | ||
| : "Key(Partition) and value(SparkPartitionOffset)'s partition don't match."; | ||
| }); | ||
| } | ||
|
|
||
| public static SparkSourceOffset merge(SparkSourceOffset o1, SparkSourceOffset o2) { | ||
| Map<Partition, SparkPartitionOffset> result = new HashMap<>(o1.partitionOffsetMap); | ||
| o2.partitionOffsetMap.forEach( | ||
| (k, v) -> | ||
| result.merge( | ||
| k, | ||
| v, | ||
| (v1, v2) -> | ||
| SparkPartitionOffset.builder() | ||
| .partition(Partition.of(k.value())) | ||
| .offset(Collections.max(ImmutableList.of(v1.offset(), v2.offset()))) | ||
| .build())); | ||
| return new SparkSourceOffset(result); | ||
| } | ||
|
|
||
| public static SparkSourceOffset merge(SparkPartitionOffset[] offsets) { | ||
| Map<Partition, SparkPartitionOffset> map = new HashMap<>(); | ||
| for (SparkPartitionOffset po : offsets) { | ||
| assert !map.containsKey(po.partition()) : "Multiple PslPartitionOffset has same partition."; | ||
| map.put( | ||
| po.partition(), | ||
| SparkPartitionOffset.builder().partition(po.partition()).offset(po.offset()).build()); | ||
| } | ||
| return new SparkSourceOffset(map); | ||
| } | ||
|
|
||
| @SuppressWarnings("unchecked") | ||
| public static SparkSourceOffset fromJson(String json) { | ||
| Map<String, Number> map; | ||
| try { | ||
| // TODO: Use TypeReference instead of Map.class, currently TypeReference breaks spark with | ||
| // java.lang.LinkageError: loader constraint violation: loader previously initiated loading | ||
| // for a different type. | ||
| map = objectMapper.readValue(json, Map.class); | ||
|
jiangmichaellll marked this conversation as resolved.
Resolved
|
||
| } catch (IOException e) { | ||
| throw new IllegalStateException("Unable to deserialize PslSourceOffset.", e); | ||
| } | ||
| Map<Partition, SparkPartitionOffset> partitionOffsetMap = | ||
| map.entrySet().stream() | ||
| .collect( | ||
| Collectors.toMap( | ||
| e -> Partition.of(Long.parseLong(e.getKey())), | ||
| e -> | ||
| SparkPartitionOffset.builder() | ||
| .partition(Partition.of(Long.parseLong(e.getKey()))) | ||
| .offset(e.getValue().longValue()) | ||
| .build())); | ||
| return new SparkSourceOffset(partitionOffsetMap); | ||
| } | ||
|
|
||
| public Map<Partition, SparkPartitionOffset> getPartitionOffsetMap() { | ||
| return this.partitionOffsetMap; | ||
| } | ||
|
|
||
| @Override | ||
| public String json() { | ||
| try { | ||
| Map<Long, Long> map = | ||
| partitionOffsetMap.entrySet().stream() | ||
| .collect(Collectors.toMap(e -> e.getKey().value(), e -> e.getValue().offset())); | ||
| return objectMapper.writeValueAsString(map); | ||
| } catch (JsonProcessingException e) { | ||
| throw new IllegalStateException("Unable to serialize PslSourceOffset.", e); | ||
| } | ||
| } | ||
| } | ||
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.
Uh oh!
There was an error while loading. Please reload this page.