/
BeamBagStateHandler.java
136 lines (118 loc) · 5.71 KB
/
BeamBagStateHandler.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
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
/*
* 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 org.apache.flink.streaming.api.runners.python.beam.state;
import org.apache.flink.api.common.state.ListState;
import org.apache.flink.api.common.typeutils.TypeSerializer;
import org.apache.flink.core.memory.ByteArrayInputStreamWithPos;
import org.apache.flink.core.memory.DataInputViewStreamWrapper;
import org.apache.flink.runtime.state.internal.InternalListState;
import org.apache.flink.util.Preconditions;
import org.apache.beam.model.fnexecution.v1.BeamFnApi;
import org.apache.beam.vendor.grpc.v1p43p2.com.google.protobuf.ByteString;
import javax.annotation.Nullable;
import java.util.HashSet;
import java.util.LinkedList;
import java.util.List;
import java.util.Set;
/** BeamBagStateHandler handles operations on {@link ListState}, which backs Beam bag states. */
public class BeamBagStateHandler implements BeamStateHandler<ListState<byte[]>> {
private static final String MERGE_NAMESPACES_MARK = "merge_namespaces";
@Nullable private final TypeSerializer<?> namespaceSerializer;
/** Reusable InputStream used to holding the elements to be deserialized. */
private final ByteArrayInputStreamWithPos bais;
/** InputStream Wrapper. */
private final DataInputViewStreamWrapper baisWrapper;
public BeamBagStateHandler(@Nullable TypeSerializer<?> namespaceSerializer) {
this.namespaceSerializer = namespaceSerializer;
this.bais = new ByteArrayInputStreamWithPos();
this.baisWrapper = new DataInputViewStreamWrapper(bais);
}
public BeamFnApi.StateResponse.Builder handle(
BeamFnApi.StateRequest request, ListState<byte[]> listState) throws Exception {
switch (request.getRequestCase()) {
case GET:
return handleBagGetRequest(request, listState);
case APPEND:
return handleBagAppendRequest(request, listState);
case CLEAR:
return handleBagClearRequest(request, listState);
default:
throw new RuntimeException(
String.format(
"Unsupported request type %s for user state.",
request.getRequestCase()));
}
}
private BeamFnApi.StateResponse.Builder handleBagGetRequest(
BeamFnApi.StateRequest request, ListState<byte[]> listState) throws Exception {
List<ByteString> byteStrings = convertToByteString(listState);
return BeamFnApi.StateResponse.newBuilder()
.setId(request.getId())
.setGet(
BeamFnApi.StateGetResponse.newBuilder()
.setData(ByteString.copyFrom(byteStrings)));
}
private BeamFnApi.StateResponse.Builder handleBagAppendRequest(
BeamFnApi.StateRequest request, ListState<byte[]> listState) throws Exception {
if (request.getStateKey()
.getBagUserState()
.getTransformId()
.equals(MERGE_NAMESPACES_MARK)) {
Preconditions.checkNotNull(namespaceSerializer);
// get namespaces to merge
byte[] namespacesBytes = request.getAppend().getData().toByteArray();
bais.setBuffer(namespacesBytes, 0, namespacesBytes.length);
int namespaceCount = baisWrapper.readInt();
Set<Object> namespaces = new HashSet<>();
for (int i = 0; i < namespaceCount; i++) {
namespaces.add(namespaceSerializer.deserialize(baisWrapper));
}
byte[] targetNamespaceByte =
request.getStateKey().getBagUserState().getWindow().toByteArray();
bais.setBuffer(targetNamespaceByte, 0, targetNamespaceByte.length);
Object targetNamespace = namespaceSerializer.deserialize(baisWrapper);
((InternalListState<Object, Object, byte[]>) listState)
.mergeNamespaces(targetNamespace, namespaces);
} else {
// get values
byte[] valueBytes = request.getAppend().getData().toByteArray();
listState.add(valueBytes);
}
return BeamFnApi.StateResponse.newBuilder()
.setId(request.getId())
.setAppend(BeamFnApi.StateAppendResponse.getDefaultInstance());
}
private BeamFnApi.StateResponse.Builder handleBagClearRequest(
BeamFnApi.StateRequest request, ListState<byte[]> listState) throws Exception {
listState.clear();
return BeamFnApi.StateResponse.newBuilder()
.setId(request.getId())
.setClear(BeamFnApi.StateClearResponse.getDefaultInstance());
}
private static List<ByteString> convertToByteString(ListState<byte[]> listState)
throws Exception {
List<ByteString> ret = new LinkedList<>();
if (listState.get() == null) {
return ret;
}
for (byte[] v : listState.get()) {
ret.add(ByteString.copyFrom(v));
}
return ret;
}
}