Skip to content

Commit

Permalink
use memory based cost model for events cache
Browse files Browse the repository at this point in the history
  • Loading branch information
gernest committed Feb 8, 2024
1 parent 6d7c7c7 commit d05e666
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 2 deletions.
4 changes: 2 additions & 2 deletions session/session.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ func New(mem memory.Allocator, resource string, storage db.Storage,
primary index.Primary, opts ...lsm.Option) *Session {
cache, err := ristretto.NewCache(&ristretto.Config{
NumCounters: 1e7,
MaxCost: 2 << 20,
MaxCost: 100 << 20, // 100MiB
BufferItems: 64,
OnEvict: func(item *ristretto.Item) {
item.Value.(*staples.Event).Release()
Expand Down Expand Up @@ -98,7 +98,7 @@ func (s *Session) process(ctx context.Context, req *v1.Event) {
s.mu.Lock()
s.build.Append(e)
s.mu.Unlock()
s.cache.SetWithTTL(e.ID, e, 1, DefaultSession)
s.cache.SetWithTTL(e.ID, e, int64(e.Size()), DefaultSession)
}

func (s *Session) Scan(ctx context.Context, start, end int64, fs *v1.Filters) (arrow.Record, error) {
Expand Down
31 changes: 31 additions & 0 deletions staples/event.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import (
"strings"
"sync"
"time"
"unsafe"

"github.com/cespare/xxhash/v2"
v1 "github.com/vinceanalytics/vince/gen/go/staples/v1"
Expand Down Expand Up @@ -56,6 +57,36 @@ type Event struct {
UtmTerm string
}

// Size without strings
var baseSize = unsafe.Sizeof(Event{})

// Size in bytes of e in memory. We use this as a cost to control cache size.
func (e *Event) Size() (n int) {
n = int(baseSize)
n += len(e.Browser)
n += len(e.BrowserVersion)
n += len(e.City)
n += len(e.Country)
n += len(e.Domain)
n += len(e.EntryPage)
n += len(e.ExitPage)
n += len(e.Host)
n += len(e.Event)
n += len(e.Os)
n += len(e.OsVersion)
n += len(e.Path)
n += len(e.Referrer)
n += len(e.ReferrerSource)
n += len(e.Region)
n += len(e.Screen)
n += len(e.UtmCampaign)
n += len(e.UtmContent)
n += len(e.UtmMedium)
n += len(e.UtmSource)
n += len(e.UtmTerm)
return
}

var eventsPool = &sync.Pool{New: func() any { return new(Event) }}

func NewEvent() *Event {
Expand Down

0 comments on commit d05e666

Please sign in to comment.