Skip to content

[dbnode] Add index regexp DFA compilation cache to avoid allocating DFAs for same expressions#2814

Merged
robskillington merged 9 commits into
masterfrom
r/add-index-regexp-dfa-compilation-cache
Oct 28, 2020
Merged

[dbnode] Add index regexp DFA compilation cache to avoid allocating DFAs for same expressions#2814
robskillington merged 9 commits into
masterfrom
r/add-index-regexp-dfa-compilation-cache

Conversation

@robskillington

Copy link
Copy Markdown
Collaborator

What this PR does / why we need it:

This reduces allocations building regexp DFA for the same regexp queries one each index query, instead using an LRU to keep the most frequently queried regexp DFA's around for reuse. Also includes metrics.

Special notes for your reviewer:

Does this PR introduce a user-facing and/or backwards incompatible change?:

NONE

Does this PR require updating code package or user-facing documentation?:

NONE

Comment thread src/m3ninx/index/regexp.go Outdated
Comment thread src/x/cache/nop_cache.go Outdated
Comment thread src/x/cache/lru_cache.go
cacheErrors: opts.CacheErrorsByDefault,
concurrencyLeases: concurrencyLeases,
metrics: &lruCacheMetrics{
entries: tallyScope.Gauge(entriesGauge),

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

nit: may be better to have tallyScope be scoped to cache rather than requiring one that's scoped to be passed in

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

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

Yeah true, done 👍

@rallen090 rallen090 left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

LGTM

Comment thread src/x/cache/lru_cache.go
Comment on lines +466 to +472
select {
case <-ctx.Done():
return c.cacheLoadComplete(key, time.Time{}, nil, UncachedError{ctx.Err()})
case <-c.concurrencyLeases:
}

defer func() { c.concurrencyLeases <- struct{}{} }()

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

nit; maybe slightly safer as:

Suggested change
select {
case <-ctx.Done():
return c.cacheLoadComplete(key, time.Time{}, nil, UncachedError{ctx.Err()})
case <-c.concurrencyLeases:
}
defer func() { c.concurrencyLeases <- struct{}{} }()
select {
case <-ctx.Done():
return c.cacheLoadComplete(key, time.Time{}, nil, UncachedError{ctx.Err()})
case <-c.concurrencyLeases:
defer func() { c.concurrencyLeases <- struct{}{} }()
}

Comment thread src/x/cache/lru_cache.go
Comment on lines +76 to +83
// As returns true if the caller is asking for the error as an uncached error
func (e UncachedError) As(target interface{}) bool {
if uncached, ok := target.(*UncachedError); ok {
uncached.Err = e.Err
return true
}
return false
}

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

nit; dont think we use these

Comment thread src/x/cache/lru_cache.go
Comment on lines +102 to +109
// As returns true if the caller is asking for the error as an cached error
func (e CachedError) As(target interface{}) bool {
if cached, ok := target.(*CachedError); ok {
cached.Err = e.Err
return true
}
return false
}

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

nit; dont think we use these

Comment thread src/x/cache/lru_cache.go
}

// If the entry exists and has not expired, it's a hit - return it to the caller
if exists && entry.expiresAt.After(c.now()) {

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

Does this need to check that it's not expired? You'll likely be regenerating the same entry anyway here

Comment thread src/x/cache/lru_cache.go
}

// There is an entry and it's valid, return it.
return value, nil

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

Do we want this to update the entry TTL?

@arnikola arnikola left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

LGTM

Comment thread src/x/cache/nop_cache.go
@@ -0,0 +1,47 @@
// Copyright (c) 2020 Uber Technologies, Inc.

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

nit; update filename too

@robskillington
robskillington merged commit 7060ac0 into master Oct 28, 2020
@robskillington
robskillington deleted the r/add-index-regexp-dfa-compilation-cache branch October 28, 2020 19:35
soundvibe pushed a commit that referenced this pull request Oct 29, 2020
* master:
  Support dynamic namespace resolution for embedded coordinators (#2815)
  [dbnode] Add index regexp DFA compilation cache to avoid allocating DFAs for same expressions (#2814)
  [dbnode] Set default cache on retrieve to false, prepare testing with cache none (#2813)
  [tools] Output annotations as base64 (#2743)
  Add Reset Transformation (#2794)
  [large-tiles] Fix for a reverse index when querying downsampled namespace (#2808)
  [dbnode] Evict info files cache before index bootstrap in peers bootstrapper (#2802)
  [dbnode] Bump default filesystem persist rate limit (#2806)
  [coordinator] Add metrics and configurability of downsample and ingest writer worker pool (#2797)
  [dbnode] Fix concurrency granularity of seekerManager.UpdateOpenLease (#2790)
  [dbnode] Use correct import path for atomic library (#2801)
  Regenerate carbon automapping rules on namespaces changes (#2793)
  Enforce matching retention and index block size (#2783)
  [query] Add ClusterNamespacesWatcher and use to generate downsample automapper rules (#2782)
  [query][dbnode] Wire up etcd-backed Clusters implementation in coordinator (#2758)
  [query] Fix quantile() argument not being passed through (#2780)

# Conflicts:
#	src/query/parser/promql/matchers.go
soundvibe pushed a commit that referenced this pull request Oct 29, 2020
* master:
  [query] Improve precision for variance and stddev of equal values (#2799)
  Support dynamic namespace resolution for embedded coordinators (#2815)
  [dbnode] Add index regexp DFA compilation cache to avoid allocating DFAs for same expressions (#2814)
  [dbnode] Set default cache on retrieve to false, prepare testing with cache none (#2813)
  [tools] Output annotations as base64 (#2743)
  Add Reset Transformation (#2794)
  [large-tiles] Fix for a reverse index when querying downsampled namespace (#2808)
  [dbnode] Evict info files cache before index bootstrap in peers bootstrapper (#2802)
  [dbnode] Bump default filesystem persist rate limit (#2806)
  [coordinator] Add metrics and configurability of downsample and ingest writer worker pool (#2797)
  [dbnode] Fix concurrency granularity of seekerManager.UpdateOpenLease (#2790)
  [dbnode] Use correct import path for atomic library (#2801)
  Regenerate carbon automapping rules on namespaces changes (#2793)
  Enforce matching retention and index block size (#2783)
  [query] Add ClusterNamespacesWatcher and use to generate downsample automapper rules (#2782)
  [query][dbnode] Wire up etcd-backed Clusters implementation in coordinator (#2758)
  [query] Fix quantile() argument not being passed through (#2780)
  [query] Add "median" aggregation to Graphite aggregate() function (#2774)
  [r2] Ensure KeepOriginal is propagated when adding/reviving rules (#2796)
  [dbnode] Update default db read limits  (#2784)
rallen090 pushed a commit that referenced this pull request Nov 4, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants