|
| 1 | +/* |
| 2 | + * SPDX-License-Identifier: Apache-2.0 |
| 3 | + * Copyright 2018-2020 The Feast Authors |
| 4 | + * |
| 5 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 6 | + * you may not use this file except in compliance with the License. |
| 7 | + * You may obtain a copy of the License at |
| 8 | + * |
| 9 | + * https://www.apache.org/licenses/LICENSE-2.0 |
| 10 | + * |
| 11 | + * Unless required by applicable law or agreed to in writing, software |
| 12 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 13 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 14 | + * See the License for the specific language governing permissions and |
| 15 | + * limitations under the License. |
| 16 | + */ |
| 17 | +package feast.store.serving.redis; |
| 18 | + |
| 19 | +import static org.junit.Assert.*; |
| 20 | + |
| 21 | +import com.google.protobuf.Timestamp; |
| 22 | +import feast.core.FeatureSetProto; |
| 23 | +import feast.core.FeatureSetProto.EntitySpec; |
| 24 | +import feast.core.FeatureSetProto.FeatureSetSpec; |
| 25 | +import feast.core.FeatureSetProto.FeatureSpec; |
| 26 | +import feast.storage.RedisProto.RedisKey; |
| 27 | +import feast.store.serving.redis.RedisCustomIO.RedisMutation; |
| 28 | +import feast.types.FeatureRowProto.FeatureRow; |
| 29 | +import feast.types.FieldProto.Field; |
| 30 | +import feast.types.ValueProto.Value; |
| 31 | +import feast.types.ValueProto.ValueType.Enum; |
| 32 | +import java.util.Arrays; |
| 33 | +import java.util.Collections; |
| 34 | +import java.util.HashMap; |
| 35 | +import java.util.Map; |
| 36 | +import org.apache.beam.sdk.extensions.protobuf.ProtoCoder; |
| 37 | +import org.apache.beam.sdk.testing.PAssert; |
| 38 | +import org.apache.beam.sdk.testing.TestPipeline; |
| 39 | +import org.apache.beam.sdk.transforms.Create; |
| 40 | +import org.apache.beam.sdk.transforms.ParDo; |
| 41 | +import org.apache.beam.sdk.transforms.SerializableFunction; |
| 42 | +import org.apache.beam.sdk.values.PCollection; |
| 43 | +import org.junit.Rule; |
| 44 | +import org.junit.Test; |
| 45 | + |
| 46 | +public class FeatureRowToRedisMutationDoFnTest { |
| 47 | + |
| 48 | + @Rule public transient TestPipeline p = TestPipeline.create(); |
| 49 | + |
| 50 | + private FeatureSetProto.FeatureSet fs = |
| 51 | + FeatureSetProto.FeatureSet.newBuilder() |
| 52 | + .setSpec( |
| 53 | + FeatureSetSpec.newBuilder() |
| 54 | + .setName("feature_set") |
| 55 | + .setVersion(1) |
| 56 | + .addEntities( |
| 57 | + EntitySpec.newBuilder() |
| 58 | + .setName("entity_id_primary") |
| 59 | + .setValueType(Enum.INT32) |
| 60 | + .build()) |
| 61 | + .addEntities( |
| 62 | + EntitySpec.newBuilder() |
| 63 | + .setName("entity_id_secondary") |
| 64 | + .setValueType(Enum.STRING) |
| 65 | + .build()) |
| 66 | + .addFeatures( |
| 67 | + FeatureSpec.newBuilder() |
| 68 | + .setName("feature_1") |
| 69 | + .setValueType(Enum.STRING) |
| 70 | + .build()) |
| 71 | + .addFeatures( |
| 72 | + FeatureSpec.newBuilder() |
| 73 | + .setName("feature_2") |
| 74 | + .setValueType(Enum.INT64) |
| 75 | + .build())) |
| 76 | + .build(); |
| 77 | + |
| 78 | + @Test |
| 79 | + public void shouldConvertRowWithDuplicateEntitiesToValidKey() { |
| 80 | + Map<String, FeatureSetProto.FeatureSet> featureSets = new HashMap<>(); |
| 81 | + featureSets.put("feature_set", fs); |
| 82 | + |
| 83 | + FeatureRow offendingRow = |
| 84 | + FeatureRow.newBuilder() |
| 85 | + .setFeatureSet("feature_set") |
| 86 | + .setEventTimestamp(Timestamp.newBuilder().setSeconds(10)) |
| 87 | + .addFields( |
| 88 | + Field.newBuilder() |
| 89 | + .setName("entity_id_primary") |
| 90 | + .setValue(Value.newBuilder().setInt32Val(1))) |
| 91 | + .addFields( |
| 92 | + Field.newBuilder() |
| 93 | + .setName("entity_id_primary") |
| 94 | + .setValue(Value.newBuilder().setInt32Val(2))) |
| 95 | + .addFields( |
| 96 | + Field.newBuilder() |
| 97 | + .setName("entity_id_secondary") |
| 98 | + .setValue(Value.newBuilder().setStringVal("a"))) |
| 99 | + .build(); |
| 100 | + |
| 101 | + PCollection<RedisMutation> output = |
| 102 | + p.apply(Create.of(Collections.singletonList(offendingRow))) |
| 103 | + .setCoder(ProtoCoder.of(FeatureRow.class)) |
| 104 | + .apply(ParDo.of(new FeatureRowToRedisMutationDoFn(featureSets))); |
| 105 | + |
| 106 | + RedisKey expectedKey = |
| 107 | + RedisKey.newBuilder() |
| 108 | + .setFeatureSet("feature_set") |
| 109 | + .addEntities( |
| 110 | + Field.newBuilder() |
| 111 | + .setName("entity_id_primary") |
| 112 | + .setValue(Value.newBuilder().setInt32Val(1))) |
| 113 | + .addEntities( |
| 114 | + Field.newBuilder() |
| 115 | + .setName("entity_id_secondary") |
| 116 | + .setValue(Value.newBuilder().setStringVal("a"))) |
| 117 | + .build(); |
| 118 | + |
| 119 | + PAssert.that(output) |
| 120 | + .satisfies( |
| 121 | + (SerializableFunction<Iterable<RedisMutation>, Void>) |
| 122 | + input -> { |
| 123 | + input.forEach( |
| 124 | + rm -> { |
| 125 | + assert (Arrays.equals(rm.getKey(), expectedKey.toByteArray())); |
| 126 | + assert (Arrays.equals(rm.getValue(), offendingRow.toByteArray())); |
| 127 | + }); |
| 128 | + return null; |
| 129 | + }); |
| 130 | + p.run(); |
| 131 | + } |
| 132 | + |
| 133 | + @Test |
| 134 | + public void shouldConvertRowWithOutOfOrderEntitiesToValidKey() { |
| 135 | + Map<String, FeatureSetProto.FeatureSet> featureSets = new HashMap<>(); |
| 136 | + featureSets.put("feature_set", fs); |
| 137 | + |
| 138 | + FeatureRow offendingRow = |
| 139 | + FeatureRow.newBuilder() |
| 140 | + .setFeatureSet("feature_set") |
| 141 | + .setEventTimestamp(Timestamp.newBuilder().setSeconds(10)) |
| 142 | + .addFields( |
| 143 | + Field.newBuilder() |
| 144 | + .setName("entity_id_secondary") |
| 145 | + .setValue(Value.newBuilder().setStringVal("a"))) |
| 146 | + .addFields( |
| 147 | + Field.newBuilder() |
| 148 | + .setName("entity_id_primary") |
| 149 | + .setValue(Value.newBuilder().setInt32Val(1))) |
| 150 | + .build(); |
| 151 | + |
| 152 | + PCollection<RedisMutation> output = |
| 153 | + p.apply(Create.of(Collections.singletonList(offendingRow))) |
| 154 | + .setCoder(ProtoCoder.of(FeatureRow.class)) |
| 155 | + .apply(ParDo.of(new FeatureRowToRedisMutationDoFn(featureSets))); |
| 156 | + |
| 157 | + RedisKey expectedKey = |
| 158 | + RedisKey.newBuilder() |
| 159 | + .setFeatureSet("feature_set") |
| 160 | + .addEntities( |
| 161 | + Field.newBuilder() |
| 162 | + .setName("entity_id_primary") |
| 163 | + .setValue(Value.newBuilder().setInt32Val(1))) |
| 164 | + .addEntities( |
| 165 | + Field.newBuilder() |
| 166 | + .setName("entity_id_secondary") |
| 167 | + .setValue(Value.newBuilder().setStringVal("a"))) |
| 168 | + .build(); |
| 169 | + |
| 170 | + PAssert.that(output) |
| 171 | + .satisfies( |
| 172 | + (SerializableFunction<Iterable<RedisMutation>, Void>) |
| 173 | + input -> { |
| 174 | + input.forEach( |
| 175 | + rm -> { |
| 176 | + assert (Arrays.equals(rm.getKey(), expectedKey.toByteArray())); |
| 177 | + assert (Arrays.equals(rm.getValue(), offendingRow.toByteArray())); |
| 178 | + }); |
| 179 | + return null; |
| 180 | + }); |
| 181 | + p.run(); |
| 182 | + } |
| 183 | +} |
0 commit comments