Skip to content

Commit

Permalink
adding purge stragegy
Browse files Browse the repository at this point in the history
  • Loading branch information
sanzmauro committed Nov 26, 2024
1 parent 01b0079 commit 569561b
Show file tree
Hide file tree
Showing 5 changed files with 18 additions and 31 deletions.
4 changes: 1 addition & 3 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -8,15 +8,13 @@ require (
github.com/gin-gonic/gin v1.10.0
github.com/google/uuid v1.3.0
github.com/splitio/gincache v1.0.1
github.com/splitio/go-split-commons/v6 v6.0.2-0.20241125153044-959311072c68
github.com/splitio/go-split-commons/v6 v6.0.2-0.20241126190617-03ffbb3a100f
github.com/splitio/go-toolkit/v5 v5.4.0
github.com/stretchr/testify v1.9.0
go.etcd.io/bbolt v1.3.6
golang.org/x/exp v0.0.0-20231006140011-7918f672742d
)

replace github.com/splitio/go-split-commons/v6 => /Users/maurosanz/go/src/github/splitio/go-split-commons

require (
github.com/bits-and-blooms/bitset v1.3.1 // indirect
github.com/bits-and-blooms/bloom/v3 v3.3.1 // indirect
Expand Down
2 changes: 2 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,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/v6 v6.0.2-0.20241126190617-03ffbb3a100f h1:FbR7KHn2kdtBda7CZVxut2qfFH7ZI4XW+FV6B0JpWgM=
github.com/splitio/go-split-commons/v6 v6.0.2-0.20241126190617-03ffbb3a100f/go.mod h1:D/XIY/9Hmfk9ivWsRsJVp439kEdmHbzUi3PKzQQDOXY=
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
18 changes: 3 additions & 15 deletions splitio/proxy/caching/caching.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,21 +19,17 @@ const (
// SplitSurrogate key (we only need one, since all splitChanges should be expired when an update is processed)
SplitSurrogate = "sp"

// LargeSegmentSurrogate key (we only need one, since all memberships should be expired when an update is processed)
LargeSegmentSurrogate = "ls"

// AuthSurrogate key (having push disabled, it's safe to cache this and return it on all requests)
AuthSurrogate = "au"

segmentPrefix = "se::"

largeSegmentPrefix = "ls::"
)

const cacheSize = 1000000

// MakeSurrogateForSegmentChanges creates a surrogate key for the segment being queried
func MakeSurrogateForLargeSegmentChanges(name string) string {
return largeSegmentPrefix + name
}

// MakeSurrogateForSegmentChanges creates a surrogate key for the segment being queried
func MakeSurrogateForSegmentChanges(segmentName string) string {
return segmentPrefix + segmentName
Expand All @@ -45,14 +41,6 @@ func MakeSurrogateForMySegments(mysegments []dtos.MySegmentDTO) []string {
return nil
}

// MakeMembershipsEntries create a cache entry key for Memberships
func MakeMembershipsEntries(key string) []string {
return []string{
"/api/memberships/" + key,
"gzip::/api/memberships/" + key,
}
}

// MakeMySegmentsEntry create a cache entry key for mysegments
func MakeMySegmentsEntries(key string) []string {
return []string{
Expand Down
20 changes: 10 additions & 10 deletions splitio/proxy/caching/workers.go
Original file line number Diff line number Diff line change
Expand Up @@ -182,7 +182,7 @@ func (c *CacheAwareLargeSegmentSynchronizer) SynchronizeLargeSegment(name string
previous := c.largeSegmentStorage.ChangeNumber(name)
newCN, err := c.wrapped.SynchronizeLargeSegment(name, till)

c.shouldEvictBySurrogate(name, previous, *newCN)
c.shouldEvictBySurrogate(previous, *newCN)

return newCN, err
}
Expand All @@ -198,19 +198,13 @@ func (c *CacheAwareLargeSegmentSynchronizer) SynchronizeLargeSegments() (map[str
}

results, err := c.wrapped.SynchronizeLargeSegments()
for name, res := range results {
c.shouldEvictBySurrogate(name, previousCNs[name], *res)
for name, currentCN := range results {
c.shouldEvictBySurrogate(previousCNs[name], *currentCN)
}

return results, err
}

func (c *CacheAwareLargeSegmentSynchronizer) shouldEvictBySurrogate(name string, previousCN int64, currentCN int64) {
if currentCN > previousCN || (previousCN != -1 && currentCN == -1) {
c.cacheFlusher.EvictBySurrogate(MakeSurrogateForLargeSegmentChanges(name))
}
}

func (c *CacheAwareLargeSegmentSynchronizer) IsCached(name string) bool {
return c.wrapped.IsCached(name)
}
Expand All @@ -219,7 +213,13 @@ func (c *CacheAwareLargeSegmentSynchronizer) SynchronizeLargeSegmentUpdate(lsRFD
previous := c.largeSegmentStorage.ChangeNumber(lsRFDResponseDTO.Name)
newCN, err := c.wrapped.SynchronizeLargeSegmentUpdate(lsRFDResponseDTO)

c.shouldEvictBySurrogate(lsRFDResponseDTO.Name, previous, *newCN)
c.shouldEvictBySurrogate(previous, *newCN)

return newCN, err
}

func (c *CacheAwareLargeSegmentSynchronizer) shouldEvictBySurrogate(previousCN int64, currentCN int64) {
if currentCN > previousCN || currentCN == -1 {
c.cacheFlusher.EvictBySurrogate(LargeSegmentSurrogate)
}
}
5 changes: 2 additions & 3 deletions splitio/proxy/caching/workers_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -240,7 +240,7 @@ func TestSynchronizeLargeSegment(t *testing.T) {

var splitStorage mocks.SplitStorageMock
var cacheFlusher mocks.CacheFlusherMock
cacheFlusher.On("EvictBySurrogate", MakeSurrogateForLargeSegmentChanges(lsName)).Once()
cacheFlusher.On("EvictBySurrogate", LargeSegmentSurrogate).Once()

var largeSegmentStorage mocks.LargeSegmentStorageMock
largeSegmentStorage.On("ChangeNumber", lsName).Return(int64(-1)).Once()
Expand Down Expand Up @@ -310,8 +310,7 @@ func TestSynchronizeLargeSegments(t *testing.T) {
splitStorage.On("LargeSegmentNames").Return(set.NewSet("ls1", "ls2"))

var cacheFlusher mocks.CacheFlusherMock
cacheFlusher.On("EvictBySurrogate", MakeSurrogateForLargeSegmentChanges("ls1")).Once()
cacheFlusher.On("EvictBySurrogate", MakeSurrogateForLargeSegmentChanges("ls2")).Once()
cacheFlusher.On("EvictBySurrogate", LargeSegmentSurrogate).Times(2)

var cn1 int64 = 100
var cn2 int64 = 200
Expand Down

0 comments on commit 569561b

Please sign in to comment.