Skip to content

Commit

Permalink
feat: replace murmur3 with xxh3.
Browse files Browse the repository at this point in the history
Signed-off-by: Skye Gill <gill.skye95@gmail.com>
  • Loading branch information
skyerus committed Aug 23, 2022
1 parent 5201f6b commit 9a391ac
Show file tree
Hide file tree
Showing 5 changed files with 18 additions and 14 deletions.
2 changes: 1 addition & 1 deletion docs/fractional_evaluation.md
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
### Fractional Evaluation

The `fractionalEvaluation` operation is a custom JsonLogic operation which deterministically selects a variant based on
the defined distribution of each variant (as a percentage). This works by hashing ([murmur3](https://en.wikipedia.org/wiki/MurmurHash))
the defined distribution of each variant (as a percentage). This works by hashing ([xxHash](https://cyan4973.github.io/xxHash/))
the given data point, converting it into an int in the range [0, 99]. Whichever range this int falls in decides which variant
is selected. As hashing is deterministic we can be sure to get the same result every time for the same data point.

Expand Down
7 changes: 4 additions & 3 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -5,19 +5,21 @@ go 1.18
require (
github.com/deepmap/oapi-codegen v1.11.0
github.com/diegoholiveira/jsonlogic/v3 v3.2.3
github.com/dimiro1/banner v1.1.0
github.com/fsnotify/fsnotify v1.5.4
github.com/go-chi/chi/v5 v5.0.7
github.com/golang/mock v1.6.0
github.com/grpc-ecosystem/grpc-gateway/v2 v2.11.1
github.com/mattn/go-colorable v0.1.12
github.com/open-feature/schemas v0.0.0-20220809125026-ba7ea0aa4841
github.com/robfig/cron v1.2.0
github.com/sirupsen/logrus v1.8.1
github.com/soheilhy/cmux v0.1.5
github.com/spaolacci/murmur3 v1.1.0
github.com/spf13/cobra v1.4.0
github.com/spf13/viper v1.12.0
github.com/stretchr/testify v1.7.4
github.com/xeipuuv/gojsonschema v1.2.0
github.com/zeebo/xxh3 v1.0.2
go.buf.build/open-feature/flagd-server/open-feature/flagd v1.1.2
golang.org/x/sync v0.0.0-20220601150217-0de741cfad7f
google.golang.org/grpc v1.48.0
Expand All @@ -29,13 +31,12 @@ require gopkg.in/yaml.v2 v2.4.0 // indirect
require (
github.com/common-nighthawk/go-figure v0.0.0-20200609044655-c4b36f998cf2 // indirect
github.com/davecgh/go-spew v1.1.1 // indirect
github.com/dimiro1/banner v1.1.0 // indirect
github.com/golang/protobuf v1.5.2 // indirect
github.com/google/uuid v1.3.0 // indirect
github.com/hashicorp/hcl v1.0.0 // indirect
github.com/inconshreveable/mousetrap v1.0.0 // indirect
github.com/klauspost/cpuid/v2 v2.0.9 // indirect
github.com/magiconair/properties v1.8.6 // indirect
github.com/mattn/go-colorable v0.1.12 // indirect
github.com/mattn/go-isatty v0.0.14 // indirect
github.com/mitchellh/copystructure v1.2.0 // indirect
github.com/mitchellh/mapstructure v1.5.0 // indirect
Expand Down
7 changes: 5 additions & 2 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -233,6 +233,8 @@ github.com/json-iterator/go v1.1.12/go.mod h1:e30LSqwooZae/UwlEbR2852Gd8hjQvJoHm
github.com/jstemmer/go-junit-report v0.0.0-20190106144839-af01ea7f8024/go.mod h1:6v2b51hI/fHJwM22ozAgKL4VKDeJcHhJFhtBdhmNjmU=
github.com/jstemmer/go-junit-report v0.9.1/go.mod h1:Brl9GWCQeLvo8nXZwPNNblvFj/XSXhF0NWZEnDohbsk=
github.com/kisielk/gotool v1.0.0/go.mod h1:XhKaO+MFFWcvkIS/tQcRk01m1F5IRFswLeQ+oQHNcck=
github.com/klauspost/cpuid/v2 v2.0.9 h1:lgaqFMSdTdQYdZ04uHyN2d/eKdOMyi2YLSvlQIBFYa4=
github.com/klauspost/cpuid/v2 v2.0.9/go.mod h1:FInQzS24/EEf25PyTYn52gqo7WaD8xa0213Md/qVLRg=
github.com/kr/fs v0.1.0/go.mod h1:FFnZGqtBN9Gxj7eW1uZ42v5BccTP0vu6NEaFoC2HwRg=
github.com/kr/pretty v0.1.0/go.mod h1:dAy3ld7l9f0ibDNOQOHHMYYIIbhfbHSm3C4ZsoJORNo=
github.com/kr/pretty v0.2.1/go.mod h1:ipq/a2n7PKx3OHsz4KJII5eveXtPO4qwEXGdVfWzfnI=
Expand Down Expand Up @@ -308,8 +310,6 @@ github.com/sirupsen/logrus v1.8.1/go.mod h1:yWOB1SBYBC5VeMP7gHvWumXLIWorT60ONWic
github.com/soheilhy/cmux v0.1.5 h1:jjzc5WVemNEDTLwv9tlmemhC73tI08BNOIGwBOo10Js=
github.com/soheilhy/cmux v0.1.5/go.mod h1:T7TcVDs9LWfQgPlPsdngu6I6QIoyIFZDDC6sNE1GqG0=
github.com/spaolacci/murmur3 v0.0.0-20180118202830-f09979ecbc72/go.mod h1:JwIasOWyU6f++ZhiEuf87xNszmSA2myDM2Kzu9HwQUA=
github.com/spaolacci/murmur3 v1.1.0 h1:7c1g84S4BPRrfL5Xrdp6fOJ206sU9y293DDHaoy0bLI=
github.com/spaolacci/murmur3 v1.1.0/go.mod h1:JwIasOWyU6f++ZhiEuf87xNszmSA2myDM2Kzu9HwQUA=
github.com/spf13/afero v1.8.2 h1:xehSyVa0YnHWsJ49JFljMpg1HX19V6NDZ1fkm1Xznbo=
github.com/spf13/afero v1.8.2/go.mod h1:CtAatgMJh6bJEIs48Ay/FOnkljP3WeGUG0MC1RfAqwo=
github.com/spf13/cast v1.5.0 h1:rj3WzYc11XZaIZMPKmwP96zkFEnnAmV8s6XbB2aY32w=
Expand Down Expand Up @@ -353,6 +353,9 @@ github.com/yuin/goldmark v1.1.32/go.mod h1:3hX8gzYuyVAZsxl0MRgGTJEmQBFcNTphYh9de
github.com/yuin/goldmark v1.2.1/go.mod h1:3hX8gzYuyVAZsxl0MRgGTJEmQBFcNTphYh9decYSb74=
github.com/yuin/goldmark v1.3.5/go.mod h1:mwnBkeHKe2W/ZEtQ+71ViKU8L12m81fl3OWwC1Zlc8k=
github.com/yuin/goldmark v1.4.1/go.mod h1:mwnBkeHKe2W/ZEtQ+71ViKU8L12m81fl3OWwC1Zlc8k=
github.com/zeebo/assert v1.3.0 h1:g7C04CbJuIDKNPFHmsk4hwZDO5O+kntRxzaUoNXj+IQ=
github.com/zeebo/xxh3 v1.0.2 h1:xZmwmqxHZA8AI603jOQ0tMqmBr9lPeFwGg6d+xy9DC0=
github.com/zeebo/xxh3 v1.0.2/go.mod h1:5NWz9Sef7zIDm2JHfFlcQvNekmcEl9ekUZQQKCYaDcA=
go.buf.build/open-feature/flagd-server/open-feature/flagd v1.1.2 h1:GbrVEHuAp/3a6JpfoV8cusP4/CveInar997DCCbXSAg=
go.buf.build/open-feature/flagd-server/open-feature/flagd v1.1.2/go.mod h1:1gOZbwcemK1/404A8u4kXmzIn4DIIT1xIe8JuowznT8=
go.opencensus.io v0.21.0/go.mod h1:mSImk1erAIZhrmZN+AvHh14ztQfjbGwt4TtuofqLduU=
Expand Down
4 changes: 2 additions & 2 deletions pkg/eval/fractional_evaluation.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import (

"github.com/diegoholiveira/jsonlogic/v3"
log "github.com/sirupsen/logrus"
"github.com/spaolacci/murmur3"
"github.com/zeebo/xxh3"
)

func init() {
Expand Down Expand Up @@ -105,7 +105,7 @@ func parseFractionalEvaluationDistributions(values []interface{}) ([]fractionalE
}

func distributeValue(value string, feDistribution []fractionalEvaluationDistribution) string {
hashValue := murmur3.Sum64([]byte(value))
hashValue := xxh3.HashString(value)

hashRatio := float64(hashValue) / math.Pow(2, 64) // divide the hash value by the largest possible value, integer 2^64

Expand Down
12 changes: 6 additions & 6 deletions pkg/eval/fractional_evaluation_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -71,8 +71,8 @@ func TestFractionalEvaluation(t *testing.T) {
StringValue: "test@faas.com",
}},
}},
expectedVariant: "green",
expectedValue: "#00FF00",
expectedVariant: "red",
expectedValue: "#FF0000",
expectedReason: model.TargetingMatchReason,
},
"test2@faas.com": {
Expand All @@ -83,8 +83,8 @@ func TestFractionalEvaluation(t *testing.T) {
StringValue: "test2@faas.com",
}},
}},
expectedVariant: "red",
expectedValue: "#FF0000",
expectedVariant: "yellow",
expectedValue: "#FFFF00",
expectedReason: model.TargetingMatchReason,
},
"test3@faas.com": {
Expand All @@ -95,8 +95,8 @@ func TestFractionalEvaluation(t *testing.T) {
StringValue: "test3@faas.com",
}},
}},
expectedVariant: "yellow",
expectedValue: "#FFFF00",
expectedVariant: "red",
expectedValue: "#FF0000",
expectedReason: model.TargetingMatchReason,
},
"test4@faas.com": {
Expand Down

0 comments on commit 9a391ac

Please sign in to comment.