-
Notifications
You must be signed in to change notification settings - Fork 1.4k
Expand file tree
/
Copy pathOnlineTransformationService.java
More file actions
325 lines (303 loc) · 13.8 KB
/
Copy pathOnlineTransformationService.java
File metadata and controls
325 lines (303 loc) · 13.8 KB
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
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
/*
* SPDX-License-Identifier: Apache-2.0
* Copyright 2018-2021 The Feast Authors
*
* 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
*
* https://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 feast.serving.service;
import com.google.common.collect.Lists;
import com.google.protobuf.ByteString;
import com.google.protobuf.Timestamp;
import feast.proto.core.*;
import feast.proto.serving.ServingAPIProto;
import feast.proto.serving.TransformationServiceAPIProto.TransformFeaturesRequest;
import feast.proto.serving.TransformationServiceAPIProto.TransformFeaturesResponse;
import feast.proto.serving.TransformationServiceAPIProto.ValueType;
import feast.proto.serving.TransformationServiceGrpc;
import feast.proto.types.ValueProto;
import feast.serving.registry.RegistryRepository;
import io.grpc.ManagedChannel;
import io.grpc.ManagedChannelBuilder;
import io.grpc.Status;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.nio.channels.Channels;
import java.util.*;
import org.apache.arrow.memory.BufferAllocator;
import org.apache.arrow.memory.RootAllocator;
import org.apache.arrow.vector.*;
import org.apache.arrow.vector.ipc.ArrowFileReader;
import org.apache.arrow.vector.ipc.ArrowFileWriter;
import org.apache.arrow.vector.types.FloatingPointPrecision;
import org.apache.arrow.vector.types.pojo.ArrowType;
import org.apache.arrow.vector.types.pojo.Field;
import org.apache.arrow.vector.types.pojo.Schema;
import org.apache.arrow.vector.util.ByteArrayReadableSeekableByteChannel;
import org.apache.commons.lang3.tuple.Pair;
import org.slf4j.Logger;
public class OnlineTransformationService implements TransformationService {
private static final Logger log =
org.slf4j.LoggerFactory.getLogger(OnlineTransformationService.class);
private final TransformationServiceGrpc.TransformationServiceBlockingStub stub;
private final RegistryRepository registryRepository;
static final int INT64_BITWIDTH = 64;
static final int INT32_BITWIDTH = 32;
public OnlineTransformationService(
String transformationServiceEndpoint, RegistryRepository registryRepository) {
if (transformationServiceEndpoint != null) {
final ManagedChannel channel =
ManagedChannelBuilder.forTarget(transformationServiceEndpoint).usePlaintext().build();
this.stub = TransformationServiceGrpc.newBlockingStub(channel);
} else {
this.stub = null;
}
this.registryRepository = registryRepository;
}
/** {@inheritDoc} */
@Override
public TransformFeaturesResponse transformFeatures(
TransformFeaturesRequest transformFeaturesRequest) {
if (this.stub == null) {
throw new RuntimeException(
"Transformation service endpoint must be configured to enable this functionality.");
}
return this.stub.transformFeatures(transformFeaturesRequest);
}
/** {@inheritDoc} */
@Override
public List<ServingAPIProto.FeatureReferenceV2> extractOnDemandFeaturesDependencies(
List<ServingAPIProto.FeatureReferenceV2> onDemandFeatureReferences) {
List<ServingAPIProto.FeatureReferenceV2> onDemandFeatureSources = new ArrayList<>();
for (ServingAPIProto.FeatureReferenceV2 featureReference : onDemandFeatureReferences) {
OnDemandFeatureViewProto.OnDemandFeatureViewSpec onDemandFeatureViewSpec =
this.registryRepository.getOnDemandFeatureViewSpec(featureReference);
Map<String, OnDemandFeatureViewProto.OnDemandSource> sources =
onDemandFeatureViewSpec.getSourcesMap();
for (OnDemandFeatureViewProto.OnDemandSource source : sources.values()) {
OnDemandFeatureViewProto.OnDemandSource.SourceCase sourceCase = source.getSourceCase();
switch (sourceCase) {
case REQUEST_DATA_SOURCE:
// Do nothing. The value should be provided as dedicated request parameter
break;
case FEATURE_VIEW_PROJECTION:
FeatureReferenceProto.FeatureViewProjection projection =
source.getFeatureViewProjection();
for (FeatureProto.FeatureSpecV2 featureSpec : projection.getFeatureColumnsList()) {
String featureName = featureSpec.getName();
ServingAPIProto.FeatureReferenceV2 onDemandFeatureSource =
ServingAPIProto.FeatureReferenceV2.newBuilder()
.setFeatureViewName(projection.getFeatureViewName())
.setFeatureName(featureName)
.build();
onDemandFeatureSources.add(onDemandFeatureSource);
}
break;
case FEATURE_VIEW:
FeatureViewProto.FeatureView featureView = source.getFeatureView();
FeatureViewProto.FeatureViewSpec featureViewSpec = featureView.getSpec();
String featureViewName = featureViewSpec.getName();
for (FeatureProto.FeatureSpecV2 featureSpec : featureViewSpec.getFeaturesList()) {
String featureName = featureSpec.getName();
ServingAPIProto.FeatureReferenceV2 onDemandFeatureSource =
ServingAPIProto.FeatureReferenceV2.newBuilder()
.setFeatureViewName(featureViewName)
.setFeatureName(featureName)
.build();
onDemandFeatureSources.add(onDemandFeatureSource);
}
break;
default:
throw Status.INTERNAL
.withDescription(
"OnDemandSource proto source field has an unexpected type: " + sourceCase)
.asRuntimeException();
}
}
}
return onDemandFeatureSources;
}
/** {@inheritDoc} */
public void processTransformFeaturesResponse(
feast.proto.serving.TransformationServiceAPIProto.TransformFeaturesResponse
transformFeaturesResponse,
String onDemandFeatureViewName,
Set<String> onDemandFeatureStringReferences,
ServingAPIProto.GetOnlineFeaturesResponse.Builder responseBuilder) {
try {
BufferAllocator allocator = new RootAllocator(Long.MAX_VALUE);
ArrowFileReader reader =
new ArrowFileReader(
new ByteArrayReadableSeekableByteChannel(
transformFeaturesResponse
.getTransformationOutput()
.getArrowValue()
.toByteArray()),
allocator);
reader.loadNextBatch();
VectorSchemaRoot readBatch = reader.getVectorSchemaRoot();
Schema responseSchema = readBatch.getSchema();
List<Field> responseFields = responseSchema.getFields();
Timestamp now = Timestamp.newBuilder().setSeconds(System.currentTimeMillis() / 1000).build();
for (Field field : responseFields) {
String columnName = field.getName();
String fullFeatureName = columnName.replace("__", ":");
ArrowType columnType = field.getType();
// The response will contain all features for the specified ODFV, so we
// skip the features that were not requested.
if (!onDemandFeatureStringReferences.contains(fullFeatureName)) {
continue;
}
FieldVector fieldVector = readBatch.getVector(field);
int valueCount = fieldVector.getValueCount();
ServingAPIProto.GetOnlineFeaturesResponse.FeatureVector.Builder vectorBuilder =
responseBuilder.addResultsBuilder();
List<ValueProto.Value> valueList = Lists.newArrayListWithExpectedSize(valueCount);
// TODO: support all Feast types
// TODO: clean up the switch statement
if (columnType instanceof ArrowType.Int) {
int bitWidth = ((ArrowType.Int) columnType).getBitWidth();
switch (bitWidth) {
case INT64_BITWIDTH:
for (int i = 0; i < valueCount; i++) {
long int64Value = ((BigIntVector) fieldVector).get(i);
valueList.add(ValueProto.Value.newBuilder().setInt64Val(int64Value).build());
}
break;
case INT32_BITWIDTH:
for (int i = 0; i < valueCount; i++) {
int int32Value = ((IntVector) fieldVector).get(i);
valueList.add(ValueProto.Value.newBuilder().setInt32Val(int32Value).build());
}
break;
default:
throw Status.INTERNAL
.withDescription(
"Column "
+ columnName
+ " is of type ArrowType.Int but has bitWidth "
+ bitWidth
+ " which cannot be handled.")
.asRuntimeException();
}
} else if (columnType instanceof ArrowType.FloatingPoint) {
FloatingPointPrecision precision = ((ArrowType.FloatingPoint) columnType).getPrecision();
switch (precision) {
case DOUBLE:
for (int i = 0; i < valueCount; i++) {
double doubleValue = ((Float8Vector) fieldVector).get(i);
valueList.add(ValueProto.Value.newBuilder().setDoubleVal(doubleValue).build());
}
break;
case SINGLE:
for (int i = 0; i < valueCount; i++) {
float floatValue = ((Float4Vector) fieldVector).get(i);
valueList.add(ValueProto.Value.newBuilder().setFloatVal(floatValue).build());
}
break;
default:
throw Status.INTERNAL
.withDescription(
"Column "
+ columnName
+ " is of type ArrowType.FloatingPoint but has precision "
+ precision
+ " which cannot be handled.")
.asRuntimeException();
}
}
for (ValueProto.Value v : valueList) {
vectorBuilder.addValues(v);
vectorBuilder.addStatuses(ServingAPIProto.FieldStatus.PRESENT);
vectorBuilder.addEventTimestamps(now);
}
responseBuilder.getMetadataBuilder().getFeatureNamesBuilder().addVal(fullFeatureName);
}
} catch (IOException e) {
log.info(e.toString());
throw Status.INTERNAL
.withDescription("Unable to correctly process transform features response: " + e)
.asRuntimeException();
}
}
/** {@inheritDoc} */
public ValueType serializeValuesIntoArrowIPC(List<Pair<String, List<ValueProto.Value>>> values) {
// In order to be serialized correctly, the data must be packaged in a VectorSchemaRoot.
// We first construct all the columns.
BufferAllocator allocator = new RootAllocator(Long.MAX_VALUE);
List<Field> columnFields = new ArrayList<>();
List<FieldVector> columns = new ArrayList<>();
for (Pair<String, List<ValueProto.Value>> columnEntry : values) {
// The Python FTS does not expect full feature names, so we extract the feature name.
String columnName = columnEntry.getKey();
List<ValueProto.Value> columnValues = columnEntry.getValue();
FieldVector column;
ValueProto.Value.ValCase valCase = columnValues.get(0).getValCase();
// TODO: support all Feast types
switch (valCase) {
case INT32_VAL:
column = new IntVector(columnName, allocator);
column.setValueCount(columnValues.size());
for (int idx = 0; idx < columnValues.size(); idx++) {
((IntVector) column).set(idx, columnValues.get(idx).getInt32Val());
}
break;
case INT64_VAL:
column = new BigIntVector(columnName, allocator);
column.setValueCount(columnValues.size());
for (int idx = 0; idx < columnValues.size(); idx++) {
((BigIntVector) column).set(idx, columnValues.get(idx).getInt64Val());
}
break;
case DOUBLE_VAL:
column = new Float8Vector(columnName, allocator);
column.setValueCount(columnValues.size());
for (int idx = 0; idx < columnValues.size(); idx++) {
((Float8Vector) column).set(idx, columnValues.get(idx).getDoubleVal());
}
break;
case FLOAT_VAL:
column = new Float4Vector(columnName, allocator);
column.setValueCount(columnValues.size());
for (int idx = 0; idx < columnValues.size(); idx++) {
((Float4Vector) column).set(idx, columnValues.get(idx).getFloatVal());
}
break;
default:
throw Status.INTERNAL
.withDescription(
"Column " + columnName + " has a type that is currently not handled: " + valCase)
.asRuntimeException();
}
columns.add(column);
columnFields.add(column.getField());
}
VectorSchemaRoot schemaRoot = new VectorSchemaRoot(columnFields, columns);
// Serialize the VectorSchemaRoot into Arrow IPC format.
ByteArrayOutputStream out = new ByteArrayOutputStream();
ArrowFileWriter writer = new ArrowFileWriter(schemaRoot, null, Channels.newChannel(out));
try {
writer.start();
writer.writeBatch();
writer.end();
} catch (IOException e) {
log.info(e.toString());
throw Status.INTERNAL
.withDescription("ArrowFileWriter could not write properly; failed with error: " + e)
.asRuntimeException();
}
byte[] byteData = out.toByteArray();
ByteString sourceData = ByteString.copyFrom(byteData);
ValueType transformationInput = ValueType.newBuilder().setArrowValue(sourceData).build();
return transformationInput;
}
}