diff --git a/carbonserver/carbonserver.go b/carbonserver/carbonserver.go index 546e6d223..64b69e6c4 100644 --- a/carbonserver/carbonserver.go +++ b/carbonserver/carbonserver.go @@ -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 { @@ -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) diff --git a/carbonserver/trie.go b/carbonserver/trie.go index b4a04a26c..62d7e9cc8 100644 --- a/carbonserver/trie.go +++ b/carbonserver/trie.go @@ -633,6 +633,7 @@ outer: if !isFile { if cur.dir() { + cur.gen = ti.root.gen return nil } @@ -640,6 +641,7 @@ outer: for _, child := range *cur.childrens { if child.dir() { cur = child + cur.gen = ti.root.gen newDir = false break } @@ -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 } diff --git a/carbonserver/trie_test.go b/carbonserver/trie_test.go index 1b525f907..f63ea74f6 100644 --- a/carbonserver/trie_test.go +++ b/carbonserver/trie_test.go @@ -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 {