Skip to content

Commit 1835768

Browse files
author
okay
committed
[query cache] cache queries via a per block query cache
This commit adds a per block query cache based on the contents of the querySpec. The query cache is enabled for table, time series and distribution queries. To cache a querySpec, its QueryDetails are hashed into an md5sum and checked for on disk. If they exist, we can re-use those results for the whole block, saving us some computation time. To enable the query cache, use the -cache-queries flag. The first query will be slightly slower because we write to disk, but the subsequent queries will be faster. Test Plan: Run a query with and without -cache-queries, plug in to running snorkel instance and watch dashboards load more quickly. Tests Added: see TESTPLAN.md
1 parent d916ca2 commit 1835768

23 files changed

Lines changed: 776 additions & 178 deletions

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ advantages
1919
* Lower disk usage through per column compression schemes
2020
* Serverless design with controlled memory usage
2121
* Per table retention policies (specify max age and/or size of tables)
22+
* Per block query cache (optional) that avoids recomputation
2223

2324
disadvantages
2425
-------------

docs/TESTPLAN.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,12 @@ Test Plan:
2929
* [ ] Open Partial Blocks and re-fill them
3030
* [ ] Auto Digest during ingestion
3131
* [ ] Digestion can fail gracefully
32+
* [ ] Per Block Query Cache
33+
* [x] Gets built
34+
* [x] Is used when supposed to
35+
* [x] Is ignored properly
36+
* [x] Gives consistent results
37+
* [x] Works with Basic Hist
3238

3339
Failure Plans
3440
-------------

main.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -93,7 +93,7 @@ func main() {
9393
first_arg := os.Args[1]
9494
os.Args = os.Args[1:]
9595

96-
sybil.SetDefaults()
96+
sybil.Startup()
9797

9898
handler, ok := CMD_FUNCS[first_arg]
9999
if !ok {

src/cmd/cmd_query.go

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,8 @@ func addQueryFlags() {
6363
TIME_FORMAT = flag.String("time-format", "", "time format to use")
6464
NO_RECYCLE_MEM = flag.Bool("no-recycle-mem", false, "don't recycle memory slabs (use Go GC instead)")
6565

66+
sybil.FLAGS.CACHED_QUERIES = flag.Bool("cache-queries", false, "Cache query results per block")
67+
6668
}
6769

6870
func RunQueryCmdLine() {
@@ -166,7 +168,8 @@ func RunQueryCmdLine() {
166168
filterSpec := sybil.FilterSpec{Int: *sybil.FLAGS.INT_FILTERS, Str: *sybil.FLAGS.STR_FILTERS, Set: *sybil.FLAGS.SET_FILTERS}
167169
filters := sybil.BuildFilters(t, &loadSpec, filterSpec)
168170

169-
querySpec := sybil.QuerySpec{Groups: groupings, Filters: filters, Aggregations: aggs}
171+
query_params := sybil.QueryParams{Groups: groupings, Filters: filters, Aggregations: aggs}
172+
querySpec := sybil.QuerySpec{QueryParams: query_params}
170173

171174
for _, v := range groups {
172175
switch t.GetColumnType(v) {

src/cmd/cmd_sessionize.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -84,7 +84,8 @@ func RunSessionizeCmdLine() {
8484
filters := []sybil.Filter{}
8585
groupings := []sybil.Grouping{}
8686
aggs := []sybil.Aggregation{}
87-
querySpec := sybil.QuerySpec{Groups: groupings, Filters: filters, Aggregations: aggs}
87+
query_params := sybil.QueryParams{Groups: groupings, Filters: filters, Aggregations: aggs}
88+
querySpec := sybil.QuerySpec{QueryParams: query_params}
8889

8990
querySpec.Limit = int16(*sybil.FLAGS.LIMIT)
9091

src/lib/aggregate.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -186,7 +186,7 @@ func FilterAndAggRecords(querySpec *QuerySpec, recordsPtr *RecordList) int {
186186
case INT_VAL:
187187
val := int64(r.Ints[a.name_id])
188188

189-
hist, ok := added_record.Hists[a.name]
189+
hist, ok := added_record.Hists[a.Name]
190190

191191
if !ok {
192192
if *FLAGS.HDR_HIST && ENABLE_HDR {
@@ -196,7 +196,7 @@ func FilterAndAggRecords(querySpec *QuerySpec, recordsPtr *RecordList) int {
196196
} else {
197197
hist = r.block.table.NewHist(r.block.table.get_int_info(a.name_id))
198198
}
199-
added_record.Hists[a.name] = hist
199+
added_record.Hists[a.Name] = hist
200200
}
201201

202202
hist.RecordValues(val, weight)

src/lib/cmd_flags.go

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,8 @@ type FlagDefs struct {
4343
PROFILE *bool
4444
PROFILE_MEM *bool
4545

46-
RECYCLE_MEM *bool
46+
RECYCLE_MEM *bool
47+
CACHED_QUERIES *bool
4748

4849
WEIGHT_COL *string
4950

@@ -106,7 +107,7 @@ var FLAGS = FlagDefs{}
106107
var OPTS = OptionDefs{}
107108
var EMPTY = ""
108109

109-
func SetDefaults() {
110+
func setDefaults() {
110111
OPTS.SORT_COUNT = "$COUNT"
111112
OPTS.SAMPLES = false
112113
OPTS.WEIGHT_COL = false
@@ -144,6 +145,7 @@ func SetDefaults() {
144145
FLAGS.LUAFILE = &EMPTY
145146

146147
FLAGS.RECYCLE_MEM = &TRUE
148+
FLAGS.CACHED_QUERIES = &FALSE
147149

148150
FLAGS.HDR_HIST = &FALSE
149151
FLAGS.LOG_HIST = &FALSE
@@ -159,4 +161,5 @@ func SetDefaults() {
159161
}
160162

161163
initLua()
164+
162165
}

src/lib/filter.go

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -118,7 +118,7 @@ type StrFilter struct {
118118
FieldId int16
119119
Op string
120120
Value string
121-
Regex *regexp.Regexp
121+
regex *regexp.Regexp
122122

123123
table *Table
124124
}
@@ -185,11 +185,11 @@ func (filter StrFilter) Filter(r *Record) bool {
185185
ret, ok = col.RCache[int(val)]
186186
if !ok {
187187
str_val := col.get_string_for_val(int32(val))
188-
ret = filter.Regex.MatchString(str_val)
188+
ret = filter.regex.MatchString(str_val)
189189
}
190190
} else {
191191
str_val := col.get_string_for_val(int32(val))
192-
ret = filter.Regex.MatchString(str_val)
192+
ret = filter.regex.MatchString(str_val)
193193
}
194194

195195
if cardinality < REGEX_CACHE_SIZE && !ok {
@@ -262,7 +262,7 @@ func (t *Table) StrFilter(name string, op string, value string) StrFilter {
262262

263263
var err error
264264
if op == "re" || op == "nre" {
265-
strFilter.Regex, err = regexp.Compile(value)
265+
strFilter.regex, err = regexp.Compile(value)
266266
if err != nil {
267267
Debug("REGEX ERROR", err, "WITH", value)
268268
}

src/lib/filter_test.go

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@ func testIntLt(test *testing.T) {
4949
aggs := []sybil.Aggregation{}
5050
aggs = append(aggs, nt.Aggregation("age", "avg"))
5151

52-
querySpec := sybil.QuerySpec{Filters: filters, Aggregations: aggs}
52+
querySpec := sybil.QuerySpec{QueryParams: sybil.QueryParams{Filters: filters, Aggregations: aggs}}
5353

5454
nt.MatchAndAggregate(&querySpec)
5555

@@ -75,7 +75,7 @@ func testIntGt(test *testing.T) {
7575
aggs := []sybil.Aggregation{}
7676
aggs = append(aggs, nt.Aggregation("age", "avg"))
7777

78-
querySpec := sybil.QuerySpec{Filters: filters, Aggregations: aggs}
78+
querySpec := sybil.QuerySpec{QueryParams: sybil.QueryParams{Filters: filters, Aggregations: aggs}}
7979

8080
nt.MatchAndAggregate(&querySpec)
8181

@@ -104,7 +104,7 @@ func testIntNeq(test *testing.T) {
104104
groupings := []sybil.Grouping{}
105105
groupings = append(groupings, nt.Grouping("age"))
106106

107-
querySpec := sybil.QuerySpec{Filters: filters, Aggregations: aggs, Groups: groupings}
107+
querySpec := sybil.QuerySpec{QueryParams: sybil.QueryParams{Filters: filters, Aggregations: aggs, Groups: groupings}}
108108

109109
nt.MatchAndAggregate(&querySpec)
110110

@@ -135,7 +135,7 @@ func testIntEq(test *testing.T) {
135135
aggs := []sybil.Aggregation{}
136136
aggs = append(aggs, nt.Aggregation("age", "avg"))
137137

138-
querySpec := sybil.QuerySpec{Filters: filters, Aggregations: aggs}
138+
querySpec := sybil.QuerySpec{QueryParams: sybil.QueryParams{Filters: filters, Aggregations: aggs}}
139139

140140
nt.MatchAndAggregate(&querySpec)
141141

@@ -164,7 +164,7 @@ func testStrEq(test *testing.T) {
164164
groupings := []sybil.Grouping{}
165165
groupings = append(groupings, nt.Grouping("age"))
166166

167-
querySpec := sybil.QuerySpec{Filters: filters, Aggregations: aggs, Groups: groupings}
167+
querySpec := sybil.QuerySpec{QueryParams: sybil.QueryParams{Filters: filters, Aggregations: aggs, Groups: groupings}}
168168

169169
Debug("QUERY SPEC", querySpec.Results)
170170

@@ -195,7 +195,7 @@ func testStrNeq(test *testing.T) {
195195
groupings := []sybil.Grouping{}
196196
groupings = append(groupings, nt.Grouping("age"))
197197

198-
querySpec := sybil.QuerySpec{Filters: filters, Aggregations: aggs}
198+
querySpec := sybil.QuerySpec{QueryParams: sybil.QueryParams{Filters: filters, Aggregations: aggs}}
199199

200200
nt.MatchAndAggregate(&querySpec)
201201

@@ -224,7 +224,7 @@ func testStrRe(test *testing.T) {
224224
groupings := []sybil.Grouping{}
225225
groupings = append(groupings, nt.Grouping("age"))
226226

227-
querySpec := sybil.QuerySpec{Filters: filters, Aggregations: aggs, Groups: groupings}
227+
querySpec := sybil.QuerySpec{QueryParams: sybil.QueryParams{Filters: filters, Aggregations: aggs, Groups: groupings}}
228228

229229
nt.MatchAndAggregate(&querySpec)
230230

@@ -255,7 +255,7 @@ func testSetIn(test *testing.T) {
255255
groupings := []sybil.Grouping{}
256256
groupings = append(groupings, nt.Grouping("age"))
257257

258-
querySpec := sybil.QuerySpec{Filters: filters, Aggregations: aggs, Groups: groupings}
258+
querySpec := sybil.QuerySpec{QueryParams: sybil.QueryParams{Filters: filters, Aggregations: aggs, Groups: groupings}}
259259

260260
nt.MatchAndAggregate(&querySpec)
261261

@@ -278,7 +278,7 @@ func testSetIn(test *testing.T) {
278278
// TODO: MULTIPLE SET VALUE FILTER
279279
// filters = []sybil.Filter{}
280280
// filters = append(filters, nt.SetFilter("age_set", "in", "20,21,22"))
281-
// querySpec = sybil.QuerySpec{Filters: filters, Aggregations: aggs, Groups: groupings}
281+
// querySpec = sybil.QuerySpec{QueryParams: sybil.QueryParams{Filters: filters, Aggregations: aggs, Groups: groupings}}
282282
//
283283
// if len(querySpec.Results) != 3 {
284284
// test.Error("Set Filter for nin returned more (or less) than three results", len(querySpec.Results), querySpec.Results)
@@ -297,7 +297,7 @@ func testSetNin(test *testing.T) {
297297
groupings := []sybil.Grouping{}
298298
groupings = append(groupings, nt.Grouping("age"))
299299

300-
querySpec := sybil.QuerySpec{Filters: filters, Aggregations: aggs, Groups: groupings}
300+
querySpec := sybil.QuerySpec{QueryParams: sybil.QueryParams{Filters: filters, Aggregations: aggs, Groups: groupings}}
301301

302302
nt.MatchAndAggregate(&querySpec)
303303

src/lib/helpers_test.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ func run_tests(m *testing.M) {
2323
}
2424

2525
func setup_test_vars(chunk_size int) {
26-
sybil.SetDefaults()
26+
sybil.Startup()
2727
sybil.FLAGS.TABLE = &TEST_TABLE_NAME
2828

2929
sybil.TEST_MODE = true
@@ -82,7 +82,7 @@ func new_query_spec() *sybil.QuerySpec {
8282
aggs := []sybil.Aggregation{}
8383
groupings := []sybil.Grouping{}
8484

85-
querySpec := sybil.QuerySpec{Groups: groupings, Filters: filters, Aggregations: aggs}
85+
querySpec := sybil.QuerySpec{QueryParams: sybil.QueryParams{Groups: groupings, Filters: filters, Aggregations: aggs}}
8686

8787
return &querySpec
8888
}

0 commit comments

Comments
 (0)