/
SparkSourceOffset.java
106 lines (94 loc) · 3.83 KB
/
SparkSourceOffset.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
/*
* 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 static com.google.common.base.Preconditions.checkArgument;
import com.google.cloud.pubsublite.Partition;
import com.google.common.collect.ImmutableList;
import com.google.common.collect.ImmutableMap;
import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;
import java.util.Collections;
import java.util.HashMap;
import java.util.Map;
import java.util.Objects;
import java.util.TreeMap;
import java.util.stream.Collectors;
public final class SparkSourceOffset
extends org.apache.spark.sql.sources.v2.reader.streaming.Offset {
private static final Gson gson = new Gson();
// 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) ->
checkArgument(
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) {
checkArgument(
!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);
}
public static SparkSourceOffset fromJson(String json) {
Map<Long, Long> map = gson.fromJson(json, new TypeToken<Map<Long, Long>>() {}.getType());
Map<Partition, SparkPartitionOffset> partitionOffsetMap =
map.entrySet().stream()
.collect(
Collectors.toMap(
e -> Partition.of(e.getKey()),
e ->
SparkPartitionOffset.builder()
.partition(Partition.of(e.getKey()))
.offset(e.getValue())
.build()));
return new SparkSourceOffset(partitionOffsetMap);
}
public Map<Partition, SparkPartitionOffset> getPartitionOffsetMap() {
return this.partitionOffsetMap;
}
@Override
public String json() {
Map<Long, Long> map =
partitionOffsetMap.entrySet().stream()
.collect(Collectors.toMap(e -> e.getKey().value(), e -> e.getValue().offset()));
return gson.toJson(new TreeMap<>(map));
}
}