Skip to content
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

Add validation when feature type is changed #1102

Merged
merged 4 commits into from
Oct 28, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@

import com.google.protobuf.Duration;
import feast.common.models.FeatureV2;
import feast.proto.core.FeatureProto;
import feast.proto.core.FeatureTableProto.FeatureTableSpec;
import feast.proto.serving.ServingAPIProto.FeatureReferenceV2;
import feast.proto.serving.ServingAPIProto.GetOnlineFeaturesRequestV2;
Expand Down Expand Up @@ -102,14 +103,20 @@ public GetOnlineFeaturesResponse getOnlineFeatures(GetOnlineFeaturesRequestV2 re

FeatureTableSpec featureTableSpec =
specService.getFeatureTableSpec(projectName, feature.get().getFeatureReference());
FeatureProto.FeatureSpecV2 featureSpec =
specService.getFeatureSpec(projectName, feature.get().getFeatureReference());
ValueProto.ValueType.Enum valueTypeEnum = featureSpec.getValueType();
ValueProto.Value.ValCase valueCase = feature.get().getFeatureValue().getValCase();
boolean isMatchingFeatureSpec = checkSameFeatureSpec(valueTypeEnum, valueCase);

boolean isOutsideMaxAge = checkOutsideMaxAge(featureTableSpec, entityRow, feature);
Map<String, ValueProto.Value> valueMap = unpackValueMap(feature, isOutsideMaxAge);
Map<String, ValueProto.Value> valueMap =
unpackValueMap(feature, isOutsideMaxAge, isMatchingFeatureSpec);
allValueMaps.putAll(valueMap);

// Generate metadata for feature values and merge into entityFieldsMap
Map<String, GetOnlineFeaturesResponse.FieldStatus> statusMap =
getMetadataMap(valueMap, false, isOutsideMaxAge);
getMetadataMap(valueMap, !isMatchingFeatureSpec, isOutsideMaxAge);
allStatusMaps.putAll(statusMap);

// Populate metrics/log request
Expand Down Expand Up @@ -153,6 +160,34 @@ public GetOnlineFeaturesResponse getOnlineFeatures(GetOnlineFeaturesRequestV2 re
}
}

private boolean checkSameFeatureSpec(
ValueProto.ValueType.Enum valueTypeEnum, ValueProto.Value.ValCase valueCase) {
HashMap<ValueProto.ValueType.Enum, ValueProto.Value.ValCase> typingMap =
new HashMap<>() {
{
put(ValueProto.ValueType.Enum.BYTES, ValueProto.Value.ValCase.BYTES_VAL);
put(ValueProto.ValueType.Enum.STRING, ValueProto.Value.ValCase.STRING_VAL);
put(ValueProto.ValueType.Enum.INT32, ValueProto.Value.ValCase.INT32_VAL);
put(ValueProto.ValueType.Enum.INT64, ValueProto.Value.ValCase.INT64_VAL);
put(ValueProto.ValueType.Enum.DOUBLE, ValueProto.Value.ValCase.DOUBLE_VAL);
put(ValueProto.ValueType.Enum.FLOAT, ValueProto.Value.ValCase.FLOAT_VAL);
put(ValueProto.ValueType.Enum.BOOL, ValueProto.Value.ValCase.BOOL_VAL);
put(ValueProto.ValueType.Enum.BYTES_LIST, ValueProto.Value.ValCase.BYTES_LIST_VAL);
put(ValueProto.ValueType.Enum.STRING_LIST, ValueProto.Value.ValCase.STRING_LIST_VAL);
put(ValueProto.ValueType.Enum.INT32_LIST, ValueProto.Value.ValCase.INT32_LIST_VAL);
put(ValueProto.ValueType.Enum.INT64_LIST, ValueProto.Value.ValCase.INT64_LIST_VAL);
put(ValueProto.ValueType.Enum.DOUBLE_LIST, ValueProto.Value.ValCase.DOUBLE_LIST_VAL);
put(ValueProto.ValueType.Enum.FLOAT_LIST, ValueProto.Value.ValCase.FLOAT_LIST_VAL);
put(ValueProto.ValueType.Enum.BOOL_LIST, ValueProto.Value.ValCase.BOOL_LIST_VAL);
}
};
if (valueCase.equals(ValueProto.Value.ValCase.VAL_NOT_SET)) {
return true;
}

return typingMap.get(valueTypeEnum).equals(valueCase);
}

private static Map<FeatureReferenceV2, Optional<Feature>> getFeatureRefFeatureMap(
List<Optional<Feature>> features) {
Map<FeatureReferenceV2, Optional<Feature>> featureReferenceFeatureMap = new HashMap<>();
Expand Down Expand Up @@ -196,11 +231,11 @@ private static Map<String, GetOnlineFeaturesResponse.FieldStatus> getMetadataMap
}

private static Map<String, ValueProto.Value> unpackValueMap(
Optional<Feature> feature, boolean isOutsideMaxAge) {
Optional<Feature> feature, boolean isOutsideMaxAge, boolean isMatchingFeatureSpec) {
Map<String, ValueProto.Value> valueMap = new HashMap<>();

if (feature.isPresent()) {
if (!isOutsideMaxAge) {
if (!isOutsideMaxAge && isMatchingFeatureSpec) {
valueMap.put(
FeatureV2.getFeatureStringRef(feature.get().getFeatureReference()),
feature.get().getFeatureValue());
Expand Down
61 changes: 57 additions & 4 deletions serving/src/main/java/feast/serving/specs/CachedSpecService.java
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@
import feast.proto.core.CoreServiceProto.ListFeatureTablesRequest;
import feast.proto.core.CoreServiceProto.ListFeatureTablesResponse;
import feast.proto.core.CoreServiceProto.ListProjectsRequest;
import feast.proto.core.FeatureProto;
import feast.proto.core.FeatureSetProto.FeatureSet;
import feast.proto.core.FeatureSetProto.FeatureSetSpec;
import feast.proto.core.FeatureSetProto.FeatureSpec;
Expand All @@ -51,6 +52,7 @@
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.ExecutionException;
import java.util.stream.Collectors;
import org.apache.commons.lang3.tuple.ImmutablePair;
import org.apache.commons.lang3.tuple.Pair;
import org.slf4j.Logger;

Expand Down Expand Up @@ -92,6 +94,10 @@ public class CachedSpecService {
.help("number of feature sets served by this instance")
.register();

private final LoadingCache<
String, Map<ServingAPIProto.FeatureReferenceV2, FeatureProto.FeatureSpecV2>>
featureCache;

public CachedSpecService(CoreSpecService coreService, StoreProto.Store store) {
this.coreService = coreService;
this.store = coreService.registerStore(store);
Expand All @@ -104,12 +110,19 @@ public CachedSpecService(CoreSpecService coreService, StoreProto.Store store) {
CacheBuilder.newBuilder().maximumSize(MAX_SPEC_COUNT).build(featureSetCacheLoader);
featureSetCache.putAll(featureSets);

Map<String, FeatureTableSpec> featureTables = getFeatureTableMap();
Map<String, FeatureTableSpec> featureTables = getFeatureTableMap().getLeft();
CacheLoader<String, FeatureTableSpec> featureTableCacheLoader =
CacheLoader.from(featureTables::get);
featureTableCache =
CacheBuilder.newBuilder().maximumSize(MAX_SPEC_COUNT).build(featureTableCacheLoader);
featureTableCache.putAll(featureTables);

Map<String, Map<ServingAPIProto.FeatureReferenceV2, FeatureProto.FeatureSpecV2>> features =
getFeatureTableMap().getRight();
CacheLoader<String, Map<ServingAPIProto.FeatureReferenceV2, FeatureProto.FeatureSpecV2>>
featureCacheLoader = CacheLoader.from(features::get);
featureCache = CacheBuilder.newBuilder().build(featureCacheLoader);
featureCache.putAll(features);
}

/**
Expand Down Expand Up @@ -241,12 +254,18 @@ public void populateCache() {

featureSetsCount.set(featureSetCache.size());

Map<String, FeatureTableSpec> featureTableMap = getFeatureTableMap();
Map<String, FeatureTableSpec> featureTableMap = getFeatureTableMap().getLeft();

featureTableCache.invalidateAll();
featureTableCache.putAll(featureTableMap);

featureTablesCount.set(featureTableCache.size());

Map<String, Map<ServingAPIProto.FeatureReferenceV2, FeatureProto.FeatureSpecV2>> featureMap =
getFeatureTableMap().getRight();
featureCache.invalidateAll();
featureCache.putAll(featureMap);

cacheLastUpdated.set(System.currentTimeMillis());
}

Expand Down Expand Up @@ -369,8 +388,13 @@ private Pair<String, String> generateFeatureToFeatureSetMapping(
*
* @return Map in the format of <project/featuretable_name: FeatureTableSpec>
*/
private Map<String, FeatureTableSpec> getFeatureTableMap() {
private ImmutablePair<
Map<String, FeatureTableSpec>,
Map<String, Map<ServingAPIProto.FeatureReferenceV2, FeatureProto.FeatureSpecV2>>>
getFeatureTableMap() {
HashMap<String, FeatureTableSpec> featureTables = new HashMap<>();
HashMap<String, Map<ServingAPIProto.FeatureReferenceV2, FeatureProto.FeatureSpecV2>> features =
new HashMap<>();

List<String> projects =
coreService.listProjects(ListProjectsRequest.newBuilder().build()).getProjectsList();
Expand All @@ -382,17 +406,31 @@ private Map<String, FeatureTableSpec> getFeatureTableMap() {
ListFeatureTablesRequest.newBuilder()
.setFilter(ListFeatureTablesRequest.Filter.newBuilder().setProject(project))
.build());
Map<ServingAPIProto.FeatureReferenceV2, FeatureProto.FeatureSpecV2> featureRefSpecMap =
new HashMap<>();
for (FeatureTable featureTable : featureTablesResponse.getTablesList()) {
FeatureTableSpec spec = featureTable.getSpec();
// Key of Map is in the form of <project/featuretable_name>
featureTables.put(getFeatureTableStringRef(project, spec), spec);

String featureTableName = spec.getName();
List<FeatureProto.FeatureSpecV2> featureSpecs = spec.getFeaturesList();
for (FeatureProto.FeatureSpecV2 featureSpec : featureSpecs) {
ServingAPIProto.FeatureReferenceV2 featureReference =
ServingAPIProto.FeatureReferenceV2.newBuilder()
.setFeatureTable(featureTableName)
.setName(featureSpec.getName())
.build();
featureRefSpecMap.put(featureReference, featureSpec);
}
}
features.put(project, featureRefSpecMap);
} catch (StatusRuntimeException e) {
throw new RuntimeException(
String.format("Unable to retrieve specs matching project %s", project), e);
}
}
return featureTables;
return ImmutablePair.of(featureTables, features);
}

public FeatureTableSpec getFeatureTableSpec(
Expand All @@ -408,4 +446,19 @@ public FeatureTableSpec getFeatureTableSpec(

return featureTableSpec;
}

public FeatureProto.FeatureSpecV2 getFeatureSpec(
String project, ServingAPIProto.FeatureReferenceV2 featureReference) {
FeatureProto.FeatureSpecV2 featureSpec;
try {
Map<ServingAPIProto.FeatureReferenceV2, FeatureProto.FeatureSpecV2> featureRefSpecMap =
featureCache.get(project);
featureSpec = featureRefSpecMap.get(featureReference);
} catch (ExecutionException e) {
throw new SpecRetrievalException(
String.format("Unable to find Feature with name: %s", featureReference.getName()), e);
}

return featureSpec;
}
}
77 changes: 69 additions & 8 deletions serving/src/test/java/feast/serving/it/ServingServiceIT.java
Original file line number Diff line number Diff line change
Expand Up @@ -127,16 +127,23 @@ static void globalSetup() {
DataGenerator.createFeatureReference("rides", "trip_distance");
ServingAPIProto.FeatureReferenceV2 feature3Reference =
DataGenerator.createFeatureReference("rides", "trip_empty");
ServingAPIProto.FeatureReferenceV2 feature4Reference =
DataGenerator.createFeatureReference("rides", "trip_wrong_type");

// Event Timestamp
String eventTimestampKey = timestampPrefix + ":" + featureTableName;
Timestamp eventTimestampValue = Timestamp.newBuilder().setSeconds(100).build();

ImmutableMap<String, ValueProto.ValueType.Enum> features =
ImmutableMap.of(
"trip_cost", ValueProto.ValueType.Enum.INT64,
"trip_distance", ValueProto.ValueType.Enum.DOUBLE,
"trip_empty", ValueProto.ValueType.Enum.DOUBLE);
"trip_cost",
ValueProto.ValueType.Enum.INT64,
"trip_distance",
ValueProto.ValueType.Enum.DOUBLE,
"trip_empty",
ValueProto.ValueType.Enum.DOUBLE,
"trip_wrong_type",
ValueProto.ValueType.Enum.STRING);

TestUtils.applyFeatureTable(
coreClient, projectName, featureTableName, entities, features, 7200);
Expand All @@ -151,9 +158,14 @@ static void globalSetup() {

ImmutableMap<ServingAPIProto.FeatureReferenceV2, ValueProto.Value> featureReferenceValueMap =
ImmutableMap.of(
feature1Reference, DataGenerator.createDoubleValue(42.2),
feature2Reference, DataGenerator.createInt64Value(42),
feature3Reference, DataGenerator.createEmptyValue());
feature1Reference,
DataGenerator.createInt64Value(42),
feature2Reference,
DataGenerator.createDoubleValue(42.2),
feature3Reference,
DataGenerator.createEmptyValue(),
feature4Reference,
DataGenerator.createDoubleValue(42.2));

// Insert timestamp into Redis and isTimestampMap only once
syncCommands.hset(
Expand Down Expand Up @@ -228,7 +240,7 @@ public void shouldRegisterAndGetOnlineFeatures() {
entityName,
entityValue,
FeatureV2.getFeatureStringRef(feature1Reference),
DataGenerator.createDoubleValue(42.2));
DataGenerator.createInt64Value(42));

ImmutableMap<String, GetOnlineFeaturesResponse.FieldStatus> expectedStatusMap =
ImmutableMap.of(
Expand Down Expand Up @@ -282,7 +294,7 @@ public void shouldRegisterAndGetOnlineFeaturesWithNotFound() {
entityName,
entityValue,
FeatureV2.getFeatureStringRef(featureReference),
DataGenerator.createDoubleValue(42.2),
DataGenerator.createInt64Value(42),
FeatureV2.getFeatureStringRef(notFoundFeatureReference),
DataGenerator.createEmptyValue(),
FeatureV2.getFeatureStringRef(emptyFeatureReference),
Expand Down Expand Up @@ -358,4 +370,53 @@ public void shouldGetOnlineFeaturesOutsideMaxAge() {

assertEquals(expectedFieldValuesList, featureResponse.getFieldValuesList());
}

@Test
public void shouldReturnNotFoundForDiffType() {
String projectName = "default";
String entityName = "driver_id";
ValueProto.Value entityValue = ValueProto.Value.newBuilder().setInt64Val(1).build();

// Instantiate EntityRows
GetOnlineFeaturesRequestV2.EntityRow entityRow1 =
DataGenerator.createEntityRow(entityName, DataGenerator.createInt64Value(1), 100);
ImmutableList<GetOnlineFeaturesRequestV2.EntityRow> entityRows = ImmutableList.of(entityRow1);

// Instantiate FeatureReferences
ServingAPIProto.FeatureReferenceV2 featureReference =
DataGenerator.createFeatureReference("rides", "trip_wrong_type");

ImmutableList<ServingAPIProto.FeatureReferenceV2> featureReferences =
ImmutableList.of(featureReference);

// Build GetOnlineFeaturesRequestV2
GetOnlineFeaturesRequestV2 onlineFeatureRequest =
TestUtils.createOnlineFeatureRequest(projectName, featureReferences, entityRows);
GetOnlineFeaturesResponse featureResponse =
servingStub.getOnlineFeaturesV2(onlineFeatureRequest);

ImmutableMap<String, ValueProto.Value> expectedValueMap =
ImmutableMap.of(
entityName,
entityValue,
FeatureV2.getFeatureStringRef(featureReference),
DataGenerator.createEmptyValue());

ImmutableMap<String, GetOnlineFeaturesResponse.FieldStatus> expectedStatusMap =
ImmutableMap.of(
entityName,
GetOnlineFeaturesResponse.FieldStatus.PRESENT,
FeatureV2.getFeatureStringRef(featureReference),
GetOnlineFeaturesResponse.FieldStatus.NOT_FOUND);

GetOnlineFeaturesResponse.FieldValues expectedFieldValues =
GetOnlineFeaturesResponse.FieldValues.newBuilder()
.putAllFields(expectedValueMap)
.putAllStatuses(expectedStatusMap)
.build();
ImmutableList<GetOnlineFeaturesResponse.FieldValues> expectedFieldValuesList =
ImmutableList.of(expectedFieldValues);

assertEquals(expectedFieldValuesList, featureResponse.getFieldValuesList());
}
}