Skip to content

Commit

Permalink
Use proto hash and remove hash from db
Browse files Browse the repository at this point in the history
Signed-off-by: Terence <terencelimxp@gmail.com>
  • Loading branch information
terryyylim committed Nov 2, 2020
1 parent ccf4e9a commit cd50a3f
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 35 deletions.
60 changes: 29 additions & 31 deletions core/src/main/java/feast/core/model/FeatureTable.java
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,7 @@
*/
package feast.core.model;

import com.google.common.primitives.Longs;
import com.google.protobuf.ByteString;
import com.google.common.hash.Hashing;
import com.google.protobuf.Duration;
import com.google.protobuf.Timestamp;
import feast.core.dao.EntityRepository;
Expand All @@ -26,12 +25,7 @@
import feast.proto.core.FeatureProto.FeatureSpecV2;
import feast.proto.core.FeatureTableProto;
import feast.proto.core.FeatureTableProto.FeatureTableSpec;
import java.util.Collection;
import java.util.List;
import java.util.Map;
import java.util.Objects;
import java.util.Optional;
import java.util.Set;
import java.util.*;
import java.util.stream.Collectors;
import javax.persistence.CascadeType;
import javax.persistence.Column;
Expand Down Expand Up @@ -112,9 +106,6 @@ public class FeatureTable extends AbstractTimestampEntity {
@Column(name = "is_deleted", nullable = false)
private boolean isDeleted;

@Column(name = "metadata_hash", nullable = false)
private long metadataHash;

public FeatureTable() {};

/**
Expand Down Expand Up @@ -155,13 +146,6 @@ public static FeatureTable fromProto(
table.setStreamSource(DataSource.fromProto(spec.getStreamSource()));
}

table.setMetadataHash(
Objects.hash(
table.getEntities(),
table.getFeatures(),
table.getBatchSource(),
table.getStreamSource()));

return table;
}

Expand Down Expand Up @@ -217,11 +201,6 @@ public void updateFromProto(
this.streamSource = null;
}

// Update hash
this.setMetadataHash(
Objects.hash(
this.getEntities(), this.getFeatures(), this.getBatchSource(), this.getStreamSource()));

// Set isDeleted to false
this.setDeleted(false);

Expand All @@ -234,14 +213,7 @@ public FeatureTableProto.FeatureTable toProto() {
// Convert field types to Protobuf compatible types
Timestamp creationTime = TypeConversion.convertTimestamp(getCreated());
Timestamp updatedTime = TypeConversion.convertTimestamp(getLastUpdated());
ByteString metadataHashBytes =
ByteString.copyFrom(
Longs.toByteArray(
Objects.hash(
this.getEntities(),
this.getFeatures(),
this.getBatchSource(),
this.getStreamSource())));
String metadataHashBytes = this.protoHash();

List<FeatureSpecV2> featureSpecs =
getFeatures().stream().map(FeatureV2::toProto).collect(Collectors.toList());
Expand Down Expand Up @@ -316,6 +288,32 @@ public void delete() {
this.setRevision(0);
}

public String protoHash() {
List<String> sortedEntities =
this.getEntities().stream().map(entity -> entity.getName()).collect(Collectors.toList());
Collections.sort(sortedEntities);

List<FeatureV2> sortedFeatures = new ArrayList(this.getFeatures());
List<FeatureSpecV2> sortedFeatureSpecs =
sortedFeatures.stream().map(featureV2 -> featureV2.toProto()).collect(Collectors.toList());
sortedFeatures.sort(Comparator.comparing(FeatureV2::getName));

DataSourceProto.DataSource streamSource = DataSourceProto.DataSource.getDefaultInstance();
if (getStreamSource() != null) {
streamSource = getStreamSource().toProto();
}

FeatureTableSpec featureTableSpec =
FeatureTableSpec.newBuilder()
.addAllEntities(sortedEntities)
.addAllFeatures(sortedFeatureSpecs)
.setBatchSource(getBatchSource().toProto())
.setStreamSource(streamSource)
.setMaxAge(Duration.newBuilder().setSeconds(getMaxAgeSecs()).build())
.build();
return Hashing.murmur3_32().hashBytes(featureTableSpec.toByteArray()).toString();
}

@Override
public int hashCode() {
return Objects.hash(
Expand Down
Original file line number Diff line number Diff line change
@@ -1,3 +1 @@
ALTER TABLE feature_tables ADD COLUMN is_deleted boolean NOT NULL;

ALTER TABLE feature_tables ADD COLUMN metadata_hash bigint NOT NULL;
ALTER TABLE feature_tables ADD COLUMN is_deleted boolean NOT NULL;
2 changes: 1 addition & 1 deletion protos/feast/core/FeatureTable.proto
Original file line number Diff line number Diff line change
Expand Up @@ -79,5 +79,5 @@ message FeatureTableMeta {

// Hash entities, features, batch_source and stream_source to inform JobService if
// jobs should be restarted should hash change
bytes hash = 4;
string hash = 4;
}

0 comments on commit cd50a3f

Please sign in to comment.