Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[dbnode/index] postingsList caching for FieldQuery #1530

Merged
merged 2 commits into from
Apr 4, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
77 changes: 54 additions & 23 deletions src/dbnode/storage/index/postings_list_cache.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,8 +43,11 @@ const (
PatternTypeRegexp PatternType = iota
// PatternTypeTerm indicates that the pattern is of type term.
PatternTypeTerm
// PatternTypeField indicates that the pattern is of type field.
PatternTypeField

reportLoopInterval = 10 * time.Second
emptyPattern = ""
)

// PostingsListCacheOptions is the options struct for the query cache.
Expand Down Expand Up @@ -92,11 +95,7 @@ func (q *PostingsListCache) GetRegexp(
field string,
pattern string,
) (postings.List, bool) {
return q.get(
segmentUUID,
field,
pattern,
PatternTypeRegexp)
return q.get(segmentUUID, field, pattern, PatternTypeRegexp)
}

// GetTerm returns the cached results for the provided term query, if any.
Expand All @@ -105,11 +104,15 @@ func (q *PostingsListCache) GetTerm(
field string,
pattern string,
) (postings.List, bool) {
return q.get(
segmentUUID,
field,
pattern,
PatternTypeTerm)
return q.get(segmentUUID, field, pattern, PatternTypeTerm)
}

// GetField returns the cached results for the provided field query, if any.
func (q *PostingsListCache) GetField(
segmentUUID uuid.UUID,
field string,
) (postings.List, bool) {
return q.get(segmentUUID, field, emptyPattern, PatternTypeField)
}

func (q *PostingsListCache) get(
Expand Down Expand Up @@ -152,6 +155,15 @@ func (q *PostingsListCache) PutTerm(
q.put(segmentUUID, field, pattern, PatternTypeTerm, pl)
}

// PutField updates the LRU with the result of the field query.
func (q *PostingsListCache) PutField(
segmentUUID uuid.UUID,
field string,
pl postings.List,
) {
q.put(segmentUUID, field, emptyPattern, PatternTypeField, pl)
}

func (q *PostingsListCache) put(
segmentUUID uuid.UUID,
field string,
Expand Down Expand Up @@ -220,29 +232,42 @@ func (q *PostingsListCache) Report() {
}

func (q *PostingsListCache) emitCacheGetMetrics(patternType PatternType, hit bool) {
switch {
case patternType == PatternTypeRegexp && hit:
q.metrics.regexp.hits.Inc(1)
case patternType == PatternTypeRegexp && !hit:
q.metrics.regexp.misses.Inc(1)
case patternType == PatternTypeTerm && hit:
q.metrics.term.hits.Inc(1)
case patternType == PatternTypeTerm && !hit:
q.metrics.term.misses.Inc(1)
var method *postingsListCacheMethodMetrics
switch patternType {
case PatternTypeRegexp:
method = q.metrics.regexp
case PatternTypeTerm:
method = q.metrics.term
case PatternTypeField:
method = q.metrics.field
default:
method = q.metrics.unknown // should never happen
}
if hit {
method.hits.Inc(1)
} else {
method.misses.Inc(1)
}
}

func (q *PostingsListCache) emitCachePutMetrics(patternType PatternType) {
if patternType == PatternTypeRegexp {
switch patternType {
case PatternTypeRegexp:
q.metrics.regexp.puts.Inc(1)
} else {
case PatternTypeTerm:
q.metrics.term.puts.Inc(1)
case PatternTypeField:
q.metrics.field.puts.Inc(1)
default:
q.metrics.unknown.puts.Inc(1) // should never happen
}
}

type postingsListCacheMetrics struct {
regexp *postingsListCacheMethodMetrics
term *postingsListCacheMethodMetrics
regexp *postingsListCacheMethodMetrics
term *postingsListCacheMethodMetrics
field *postingsListCacheMethodMetrics
unknown *postingsListCacheMethodMetrics

size tally.Gauge
capacity tally.Gauge
Expand All @@ -256,6 +281,12 @@ func newPostingsListCacheMetrics(scope tally.Scope) *postingsListCacheMetrics {
term: newPostingsListCacheMethodMetrics(scope.Tagged(map[string]string{
"query_type": "term",
})),
field: newPostingsListCacheMethodMetrics(scope.Tagged(map[string]string{
"query_type": "field",
})),
unknown: newPostingsListCacheMethodMetrics(scope.Tagged(map[string]string{
"query_type": "unknown",
})),

size: scope.Gauge("size"),
capacity: scope.Gauge("capacity"),
Expand Down
98 changes: 63 additions & 35 deletions src/dbnode/storage/index/postings_list_cache_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -61,9 +61,14 @@ func init() {
pl.Insert(postings.ID(i))

patternType := PatternTypeRegexp
if i%2 == 0 {
switch i % 3 {
case 0:
patternType = PatternTypeTerm
case 1:
patternType = PatternTypeField
pattern = "" // field queries don't have patterns
}

testPlEntries = append(testPlEntries, testEntry{
segmentUUID: segmentUUID,
key: newKey(field, pattern, patternType),
Expand Down Expand Up @@ -92,35 +97,35 @@ func TestSimpleLRUBehavior(t *testing.T) {
e4 = testPlEntries[4]
e5 = testPlEntries[5]
)
putEntry(plCache, 0)
putEntry(plCache, 1)
putEntry(plCache, 2)
putEntry(t, plCache, 0)
putEntry(t, plCache, 1)
putEntry(t, plCache, 2)
requireExpectedOrder(t, plCache, []testEntry{e0, e1, e2})

putEntry(plCache, 3)
putEntry(t, plCache, 3)
requireExpectedOrder(t, plCache, []testEntry{e1, e2, e3})

putEntry(plCache, 4)
putEntry(plCache, 4)
putEntry(plCache, 5)
putEntry(plCache, 5)
putEntry(plCache, 0)
putEntry(plCache, 0)
putEntry(t, plCache, 4)
putEntry(t, plCache, 4)
putEntry(t, plCache, 5)
putEntry(t, plCache, 5)
putEntry(t, plCache, 0)
putEntry(t, plCache, 0)
requireExpectedOrder(t, plCache, []testEntry{e4, e5, e0})

// Miss, no expected change.
getEntry(plCache, 100)
getEntry(t, plCache, 100)
requireExpectedOrder(t, plCache, []testEntry{e4, e5, e0})

// Hit.
getEntry(plCache, 4)
getEntry(t, plCache, 4)
requireExpectedOrder(t, plCache, []testEntry{e5, e0, e4})

// Multiple hits.
getEntry(plCache, 4)
getEntry(plCache, 0)
getEntry(plCache, 5)
getEntry(plCache, 5)
getEntry(t, plCache, 4)
getEntry(t, plCache, 0)
getEntry(t, plCache, 5)
getEntry(t, plCache, 5)
requireExpectedOrder(t, plCache, []testEntry{e4, e0, e5})
}

Expand Down Expand Up @@ -151,7 +156,7 @@ func TestPurgeSegment(t *testing.T) {

// Write the remaining entries.
for i := 100; i < len(testPlEntries); i++ {
putEntry(plCache, i)
putEntry(t, plCache, i)
}

// Purge all entries related to the segment.
Expand Down Expand Up @@ -179,7 +184,7 @@ func TestPurgeSegment(t *testing.T) {

// Remaining entries should still be present.
for i := 100; i < len(testPlEntries); i++ {
getEntry(plCache, i)
getEntry(t, plCache, i)
}
}

Expand All @@ -189,11 +194,11 @@ func TestEverthingInsertedCanBeRetrieved(t *testing.T) {
defer stopReporting()

for i := range testPlEntries {
putEntry(plCache, i)
putEntry(t, plCache, i)
}

for i, entry := range testPlEntries {
cached, ok := getEntry(plCache, i)
cached, ok := getEntry(t, plCache, i)
require.True(t, ok)
require.True(t, cached.Equal(entry.postingsList))
}
Expand All @@ -218,7 +223,7 @@ func testConcurrency(t *testing.T, size int, purge bool, verify bool) {
wg.Add(1)
go func(i int) {
for j := 0; j < 100; j++ {
putEntry(plCache, i)
putEntry(t, plCache, i)
}
wg.Done()
}(i)
Expand All @@ -229,7 +234,7 @@ func testConcurrency(t *testing.T, size int, purge bool, verify bool) {
wg.Add(1)
go func(i int) {
for j := 0; j < 100; j++ {
getEntry(plCache, j)
getEntry(t, plCache, j)
}
wg.Done()
}(i)
Expand Down Expand Up @@ -258,7 +263,7 @@ func testConcurrency(t *testing.T, size int, purge bool, verify bool) {
}

for i, entry := range testPlEntries {
cached, ok := getEntry(plCache, i)
cached, ok := getEntry(t, plCache, i)
if !ok {
// Debug.
printSortedKeys(t, plCache)
Expand All @@ -268,10 +273,11 @@ func testConcurrency(t *testing.T, size int, purge bool, verify bool) {
}
}

func putEntry(cache *PostingsListCache, i int) {
func putEntry(t *testing.T, cache *PostingsListCache, i int) {
// Do each put twice to test the logic that avoids storing
// multiple entries for the same value.
if testPlEntries[i].key.patternType == PatternTypeRegexp {
switch testPlEntries[i].key.patternType {
case PatternTypeRegexp:
cache.PutRegexp(
testPlEntries[i].segmentUUID,
testPlEntries[i].key.field,
Expand All @@ -284,7 +290,7 @@ func putEntry(cache *PostingsListCache, i int) {
testPlEntries[i].key.pattern,
testPlEntries[i].postingsList,
)
} else {
case PatternTypeTerm:
cache.PutTerm(
testPlEntries[i].segmentUUID,
testPlEntries[i].key.field,
Expand All @@ -297,23 +303,45 @@ func putEntry(cache *PostingsListCache, i int) {
testPlEntries[i].key.pattern,
testPlEntries[i].postingsList,
)
case PatternTypeField:
cache.PutField(
testPlEntries[i].segmentUUID,
testPlEntries[i].key.field,
testPlEntries[i].postingsList,
)
cache.PutField(
testPlEntries[i].segmentUUID,
testPlEntries[i].key.field,
testPlEntries[i].postingsList,
)
default:
require.FailNow(t, "unknown pattern type", testPlEntries[i].key.patternType)
}
}

func getEntry(cache *PostingsListCache, i int) (postings.List, bool) {
if testPlEntries[i].key.patternType == PatternTypeRegexp {
func getEntry(t *testing.T, cache *PostingsListCache, i int) (postings.List, bool) {
switch testPlEntries[i].key.patternType {
case PatternTypeRegexp:
return cache.GetRegexp(
testPlEntries[i].segmentUUID,
testPlEntries[i].key.field,
testPlEntries[i].key.pattern,
)
case PatternTypeTerm:
return cache.GetTerm(
testPlEntries[i].segmentUUID,
testPlEntries[i].key.field,
testPlEntries[i].key.pattern,
)
case PatternTypeField:
return cache.GetField(
testPlEntries[i].segmentUUID,
testPlEntries[i].key.field,
)
default:
require.FailNow(t, "unknown pattern type", testPlEntries[i].key.patternType)
}

return cache.GetTerm(
testPlEntries[i].segmentUUID,
testPlEntries[i].key.field,
testPlEntries[i].key.pattern,
)
return nil, false
}

func requireExpectedOrder(t *testing.T, plCache *PostingsListCache, expectedOrder []testEntry) {
Expand Down
20 changes: 17 additions & 3 deletions src/dbnode/storage/index/read_through_segment.go
Original file line number Diff line number Diff line change
Expand Up @@ -215,9 +215,23 @@ func (s *readThroughSegmentReader) MatchTerm(

// MatchField returns a cached posting list or queries the underlying
// segment if their is a cache miss.
func (s *readThroughSegmentReader) MatchField(f []byte) (postings.List, error) {
// TODO(prateek): wire up the postings list cache
return s.reader.MatchField(f)
func (s *readThroughSegmentReader) MatchField(field []byte) (postings.List, error) {
if s.postingsListCache == nil || !s.opts.CacheTerms {
return s.reader.MatchField(field)
}

// TODO(rartoul): Would be nice to not allocate strings here.
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

yeah making me look even lazier :P

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

;)

fieldStr := string(field)
pl, ok := s.postingsListCache.GetField(s.uuid, fieldStr)
if ok {
return pl, nil
}

pl, err := s.reader.MatchField(field)
if err == nil {
s.postingsListCache.PutField(s.uuid, fieldStr, pl)
}
return pl, err
}

// MatchAll is a pass through call, since there's no postings list to cache.
Expand Down
Loading