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

trie: stop indexing empty directory nodes #445

Merged
merged 2 commits into from
Jan 17, 2022
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
37 changes: 22 additions & 15 deletions carbonserver/carbonserver.go
Original file line number Diff line number Diff line change
Expand Up @@ -975,6 +975,7 @@ func (listener *CarbonserverListener) updateFileList(dir string, cacheMetricName
trimmedName := strings.TrimPrefix(p, listener.whisperData)
filesLen++
if flc != nil {
// TODO: include metadata like physical/logical size, data points
if err := flc.write(trimmedName); err != nil {
logger.Error("failed to write to file list cache", zap.Error(err))
if err := flc.close(); err != nil {
Expand All @@ -990,21 +991,27 @@ func (listener *CarbonserverListener) updateFileList(dir string, cacheMetricName
delete(cacheMetricNames, trimmedName)
} else {
if listener.trieIndex {
var dataPoints int64
if isFullMetric && listener.estimateSize != nil {
m := strings.ReplaceAll(trimmedName, "/", ".")
m = m[1 : len(m)-4]
_, dataPoints = listener.estimateSize(m)
}

var physicalSize = info.Size()
if stat, ok := info.Sys().(*syscall.Stat_t); ok {
physicalSize = stat.Blocks * 512
}

if err := trieIdx.insert(trimmedName, info.Size(), physicalSize, dataPoints); err != nil {
// It's better to just log an error than stop indexing
listener.logTrieInsertError(logger, "updateFileList.trie: failed to index path", trimmedName, err)
// WHY:
// * this would only affects empty directories
// * indexing empty directories causes an strange bug in trie index
// * empty dir isn't useful (at least most of the time)?
if isFullMetric {
var dataPoints int64
if listener.estimateSize != nil {
m := strings.ReplaceAll(trimmedName, "/", ".")
m = m[1 : len(m)-4]
_, dataPoints = listener.estimateSize(m)
}

var physicalSize = info.Size()
if stat, ok := info.Sys().(*syscall.Stat_t); ok {
physicalSize = stat.Blocks * 512
}

if err := trieIdx.insert(trimmedName, info.Size(), physicalSize, dataPoints); err != nil {
// It's better to just log an error than stop indexing
listener.logTrieInsertError(logger, "updateFileList.trie: failed to index path", trimmedName, err)
}
}
} else {
files = append(files, trimmedName)
Expand Down
4 changes: 3 additions & 1 deletion carbonserver/trie.go
Original file line number Diff line number Diff line change
Expand Up @@ -633,13 +633,15 @@ outer:

if !isFile {
if cur.dir() {
cur.gen = ti.root.gen
return nil
}

var newDir = true
for _, child := range *cur.childrens {
if child.dir() {
cur = child
cur.gen = ti.root.gen
newDir = false
break
}
Expand Down Expand Up @@ -745,7 +747,7 @@ func (ti *trieIndex) query(expr string, limit int, expand func(globs []string) (
}

if cur.dir() {
if mindex+1 >= len(matchers) || !curm.dstate().matched() {
if mindex+1 >= len(matchers) || !curm.dstate().matched() || len(curChildrens) == 0 {
goto parent
}

Expand Down
13 changes: 13 additions & 0 deletions carbonserver/trie_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -692,6 +692,19 @@ func TestTrieIndex(t *testing.T) {
},
expectLeafs: []bool{false, false, false, false},
},
{
input: []string{
"zk/kafka_xxx/by_node/node_0/status/health.wsp",
"zk/streaming_yyy/by_node/node_0/status/health.wsp",
"zk/kafka_zzz/by_node/node_0/status", // intentionally empty directory node
},
query: "zk.*.by_node.*.status.health",
expect: []string{
"zk.kafka_xxx.by_node.node_0.status.health",
"zk.streaming_yyy.by_node.node_0.status.health",
},
expectLeafs: []bool{true, true},
},
}

for _, c := range cases {
Expand Down