Skip to content

Commit

Permalink
Merge branch 'semver' into feat/proxy_semver_support
Browse files Browse the repository at this point in the history
  • Loading branch information
mredolatti authored May 2, 2024
2 parents 8053764 + 797a135 commit 59410e1
Show file tree
Hide file tree
Showing 9 changed files with 76 additions and 28 deletions.
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ require (
github.com/gin-gonic/gin v1.9.1
github.com/google/uuid v1.3.0
github.com/splitio/gincache v1.0.1
github.com/splitio/go-split-commons/v5 v5.2.1
github.com/splitio/go-split-commons/v5 v5.2.2-0.20240502153343-221c57a08ca1
github.com/splitio/go-toolkit/v5 v5.4.0
github.com/stretchr/testify v1.8.4
go.etcd.io/bbolt v1.3.6
Expand Down
8 changes: 2 additions & 6 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -90,12 +90,8 @@ github.com/rogpeppe/go-internal v1.8.0 h1:FCbCCtXNOY3UtUuHUYaghJg4y7Fd14rXifAYUA
github.com/rogpeppe/go-internal v1.8.0/go.mod h1:WmiCO8CzOY8rg0OYDC4/i/2WRWAB6poM+XZ2dLUbcbE=
github.com/splitio/gincache v1.0.1 h1:dLYdANY/BqH4KcUMCe/LluLyV5WtuE/LEdQWRE06IXU=
github.com/splitio/gincache v1.0.1/go.mod h1:CcgJDSM9Af75kyBH0724v55URVwMBuSj5x1eCWIOECY=
github.com/splitio/go-split-commons/v5 v5.1.1 h1:lLOqNQMdZA5Z7FBBh4YODWdE2QFgxSPMptX9ty14x4c=
github.com/splitio/go-split-commons/v5 v5.1.1/go.mod h1:9vAZrlhKvhensyRC11hyVFdgLIBrkX9D5vdYc9qB13w=
github.com/splitio/go-split-commons/v5 v5.2.0 h1:1P66JdUV1Fj1DUeWU1rwkeObqinl9AecRxDsktBsx0g=
github.com/splitio/go-split-commons/v5 v5.2.0/go.mod h1:m1Od/jxiSUJXpdbRvRxTaKeSAdQVem5AZr7AjI4xXn8=
github.com/splitio/go-split-commons/v5 v5.2.1 h1:h8Up3Jk6NFkHSYCj4Sr15uuoxQwFqPr3gn0G4vghM/8=
github.com/splitio/go-split-commons/v5 v5.2.1/go.mod h1:344KP05ULARzjRfnC4VtGSyu5l3kmIM375WUIzrURs0=
github.com/splitio/go-split-commons/v5 v5.2.2-0.20240502153343-221c57a08ca1 h1:xAayvpboEuhr/vX8DOnNqVLNTsR0mWHcbVxwkEI780g=
github.com/splitio/go-split-commons/v5 v5.2.2-0.20240502153343-221c57a08ca1/go.mod h1:344KP05ULARzjRfnC4VtGSyu5l3kmIM375WUIzrURs0=
github.com/splitio/go-toolkit/v5 v5.4.0 h1:g5WFpRhQomnXCmvfsNOWV4s5AuUrWIZ+amM68G8NBKM=
github.com/splitio/go-toolkit/v5 v5.4.0/go.mod h1:xYhUvV1gga9/1029Wbp5pjnR6Cy8nvBpjw99wAbsMko=
github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
Expand Down
2 changes: 1 addition & 1 deletion splitio/commitversion.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,4 +5,4 @@ This file is created automatically, please do not edit
*/

// CommitVersion is the version of the last commit previous to release
const CommitVersion = "94c6f52"
const CommitVersion = "94c6f52"
1 change: 1 addition & 0 deletions splitio/producer/conf/sections.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ type Main struct {
Integrations conf.Integrations `json:"integrations" s-nested:"true"`
Logging conf.Logging `json:"logging" s-nested:"true"`
Healthcheck Healthcheck `json:"healthcheck" s-nested:"true"`
SpecVersion string
}

// BuildAdvancedConfig generates a commons-compatible advancedconfig with default + overriden parameters
Expand Down
3 changes: 3 additions & 0 deletions splitio/producer/initialization.go
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,10 @@ const (
// Start initialize the producer mode
func Start(logger logging.LoggerInterface, cfg *conf.Main) error {
// Getting initial config data
cfg.SpecVersion = "1.0" // @TODO Until is implemented
advanced := cfg.BuildAdvancedConfig()
advanced.AuthSpecVersion = cfg.SpecVersion
advanced.FlagsSpecVersion = cfg.SpecVersion
advanced.FlagSetsFilter = cfg.FlagSetsFilter
metadata := util.GetMetadata(false, cfg.IPAddressEnabled)

Expand Down
66 changes: 58 additions & 8 deletions splitio/producer/initialization_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ func TestIsApikeyValidOk(t *testing.T) {
os.Setenv("SPLITIO_EVENTS_URL", ts.URL)

httpSplitFetcher := mocks.MockSplitFetcher{
FetchCall: func(changeNumber int64, fetchOptions *service.FetchOptions) (*dtos.SplitChangesDTO, error) {
FetchCall: func(fetchOptions *service.FlagRequestParams) (*dtos.SplitChangesDTO, error) {
return nil, nil
},
}
Expand All @@ -68,7 +68,7 @@ func TestIsApikeyValidNotOk(t *testing.T) {
os.Setenv("SPLITIO_EVENTS_URL", ts.URL)

httpSplitFetcher := mocks.MockSplitFetcher{
FetchCall: func(changeNumber int64, fetchOptions *service.FetchOptions) (*dtos.SplitChangesDTO, error) {
FetchCall: func(fetchOptions *service.FlagRequestParams) (*dtos.SplitChangesDTO, error) {
return nil, errors.New("Some")
},
}
Expand All @@ -81,6 +81,7 @@ func TestIsApikeyValidNotOk(t *testing.T) {
func TestSanitizeRedisWithForcedCleanup(t *testing.T) {
cfg := getDefaultConf()
cfg.Apikey = "983564etyrudhijfgknf9i08euh"
cfg.SpecVersion = "1.0"
cfg.Initialization.ForceFreshStartup = true

logger := logging.NewLogger(nil)
Expand All @@ -99,13 +100,12 @@ func TestSanitizeRedisWithForcedCleanup(t *testing.T) {
if err != nil {
t.Error("It should be nil")
}
value, err := redisClient.Get("SPLITIO.test1")
value, _ := redisClient.Get("SPLITIO.test1")
if value != "123" {
t.Error("Value should have been set properly")
}

miscStorage := predis.NewMiscStorage(redisClient, logger)
value, err = redisClient.Get("SPLITIO.test1")
err = sanitizeRedis(cfg, miscStorage, logger)
if err != nil {
t.Error("It should be nil", err)
Expand All @@ -116,8 +116,8 @@ func TestSanitizeRedisWithForcedCleanup(t *testing.T) {
t.Error("Value should have been null, and was ", value)
}

value, err = redisClient.Get("SPLITIO.hash")
if value != "1497926959" {
value, _ = redisClient.Get("SPLITIO.hash")
if value != "2298020180" {
t.Error("Incorrect apikey hash set in redis after sanitization operation.", value)
}

Expand Down Expand Up @@ -225,14 +225,64 @@ func TestSanitizeRedisWithForcedCleanupByFlagSets(t *testing.T) {
if err != nil {
t.Error("It should be nil")
}
value, err := redisClient.Get("SPLITIO.test1")
value, _ := redisClient.Get("SPLITIO.test1")
if value != "123" {
t.Error("Value should have been set properly")
}

cfg.FlagSetsFilter = []string{"flagset7"}
miscStorage := predis.NewMiscStorage(redisClient, logger)
value, err = redisClient.Get("SPLITIO.test1")
err = sanitizeRedis(cfg, miscStorage, logger)
if err != nil {
t.Error("It should be nil", err)
}

value, _ = redisClient.Get("SPLITIO.test1")
if value != "" {
t.Error("Value should have been removed.")
}

val, _ := redisClient.Get("SPLITIO.hash")
parsedHash, _ := strconv.ParseUint(val, 10, 64)
if uint32(parsedHash) == hash {
t.Error("ApiHash should have been updated.")
}
redisClient.Del("SPLITIO.hash")
redisClient.Del("SPLITIO.test1")
}

func TestSanitizeRedisWithForcedCleanupBySpecVersion(t *testing.T) {
cfg := getDefaultConf()
cfg.Apikey = "983564etyrudhijfgknf9i08euh"
cfg.Initialization.ForceFreshStartup = true
cfg.SpecVersion = "1.0"

hash := util.HashAPIKey(cfg.Apikey + cfg.SpecVersion + strings.Join(cfg.FlagSetsFilter, "::"))

logger := logging.NewLogger(nil)

redisClient, err := predis.NewRedisClient(&config.RedisConfig{
Host: "localhost",
Port: 6379,
Prefix: "some_prefix",
Database: 1,
}, logger)
if err != nil {
t.Error("It should be nil")
}

err = redisClient.Set("SPLITIO.test1", "123", 0)
redisClient.Set("SPLITIO.hash", hash, 0)
if err != nil {
t.Error("It should be nil")
}
value, _ := redisClient.Get("SPLITIO.test1")
if value != "123" {
t.Error("Value should have been set properly")
}

cfg.SpecVersion = "1.1"
miscStorage := predis.NewMiscStorage(redisClient, logger)
err = sanitizeRedis(cfg, miscStorage, logger)
if err != nil {
t.Error("It should be nil", err)
Expand Down
9 changes: 4 additions & 5 deletions splitio/producer/util.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@ import (
"strings"
"time"

cconf "github.com/splitio/go-split-commons/v5/conf"
config "github.com/splitio/go-split-commons/v5/conf"
"github.com/splitio/go-split-commons/v5/provisional"
"github.com/splitio/go-split-commons/v5/provisional/strategy"
Expand Down Expand Up @@ -116,15 +115,15 @@ func parseRedisOptions(cfg *conf.Redis) (*config.RedisConfig, error) {
}

func isValidApikey(splitFetcher service.SplitFetcher) bool {
_, err := splitFetcher.Fetch(time.Now().UnixNano()/int64(time.Millisecond), &service.FetchOptions{CacheControlHeaders: false})
_, err := splitFetcher.Fetch(service.MakeFlagRequestParams().WithCacheControl(false).WithChangeNumber(time.Now().UnixNano() / int64(time.Millisecond)))
return err == nil
}

func sanitizeRedis(cfg *conf.Main, miscStorage *redis.MiscStorage, logger logging.LoggerInterface) error {
if miscStorage == nil {
return errors.New("Could not sanitize redis")
return errors.New("could not sanitize redis")
}
currentHash := util.HashAPIKey(cfg.Apikey + strings.Join(cfg.FlagSetsFilter, "::"))
currentHash := util.HashAPIKey(cfg.Apikey + cfg.SpecVersion + strings.Join(cfg.FlagSetsFilter, "::"))
currentHashAsStr := strconv.Itoa(int(currentHash))
defer miscStorage.SetApikeyHash(currentHashAsStr)

Expand Down Expand Up @@ -166,7 +165,7 @@ func getAppCounterConfigs(storage storageCommon.SplitStorage) (hcAppCounter.Thre
return splitsConfig, segmentsConfig, storageConfig
}

func getServicesCountersConfig(advanced *cconf.AdvancedConfig) []hcServicesCounter.Config {
func getServicesCountersConfig(advanced *config.AdvancedConfig) []hcServicesCounter.Config {
var cfgs []hcServicesCounter.Config

apiConfig := hcServicesCounter.DefaultConfig("API", advanced.SdkURL, "/version")
Expand Down
5 changes: 2 additions & 3 deletions splitio/proxy/controllers/sdk.go
Original file line number Diff line number Diff line change
Expand Up @@ -140,7 +140,6 @@ func (c *SdkServerController) fetchSplitChangesSince(since int64, sets []string)

// perform a fetch to the BE using the supplied `since`, have the storage process it's response &, retry
// TODO(mredolatti): implement basic collapsing here to avoid flooding the BE with requests
fetchOptions := service.NewFetchOptions(true, nil)
fetchOptions.FlagSetsFilter = strings.Join(sets, ",") // at this point the sets have been sanitized & sorted
return c.fetcher.Fetch(since, &fetchOptions)
fetchOptions := service.MakeFlagRequestParams().WithChangeNumber(since).WithFlagSetsFilter(strings.Join(sets, ",")) // at this point the sets have been sanitized & sorted
return c.fetcher.Fetch(fetchOptions)
}
8 changes: 4 additions & 4 deletions splitio/proxy/controllers/sdk_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@ func TestSplitChangesOlderSince(t *testing.T) {
Once()

var splitFetcher splitFetcherMock
splitFetcher.On("Fetch", int64(-1), ref(service.NewFetchOptions(true, nil))).
splitFetcher.On("Fetch", ref(*service.MakeFlagRequestParams().WithChangeNumber(-1))).
Return(&dtos.SplitChangesDTO{Since: -1, Till: 1, Splits: []dtos.SplitDTO{{Name: "s1", Status: "ACTIVE"}, {Name: "s2", Status: "ACTIVE"}}}, nil).
Once()

Expand Down Expand Up @@ -126,7 +126,7 @@ func TestSplitChangesOlderSinceFetchFails(t *testing.T) {
Once()

var splitFetcher splitFetcherMock
splitFetcher.On("Fetch", int64(-1), ref(service.NewFetchOptions(true, nil))).
splitFetcher.On("Fetch", ref(*service.MakeFlagRequestParams().WithChangeNumber(-1))).
Return((*dtos.SplitChangesDTO)(nil), errors.New("something")).
Once()

Expand Down Expand Up @@ -404,8 +404,8 @@ type splitFetcherMock struct {
}

// Fetch implements service.SplitFetcher
func (s *splitFetcherMock) Fetch(changeNumber int64, fetchOptions *service.FetchOptions) (*dtos.SplitChangesDTO, error) {
args := s.Called(changeNumber, fetchOptions)
func (s *splitFetcherMock) Fetch(fetchOptions *service.FlagRequestParams) (*dtos.SplitChangesDTO, error) {
args := s.Called(fetchOptions)
return args.Get(0).(*dtos.SplitChangesDTO), args.Error(1)
}

Expand Down

0 comments on commit 59410e1

Please sign in to comment.