-
Notifications
You must be signed in to change notification settings - Fork 455
/
results_cache.go
631 lines (544 loc) · 21.8 KB
/
results_cache.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
// SPDX-License-Identifier: AGPL-3.0-only
// Provenance-includes-location: https://github.com/cortexproject/cortex/blob/master/pkg/querier/queryrange/results_cache.go
// Provenance-includes-license: Apache-2.0
// Provenance-includes-copyright: The Cortex Authors.
package querymiddleware
import (
"context"
"encoding/hex"
"flag"
"fmt"
"hash/fnv"
"net/http"
"sort"
"strings"
"time"
"github.com/go-kit/log"
"github.com/go-kit/log/level"
"github.com/gogo/protobuf/types"
"github.com/grafana/dskit/cache"
"github.com/grafana/dskit/tenant"
"github.com/opentracing/opentracing-go"
"github.com/pkg/errors"
"github.com/prometheus/client_golang/prometheus"
"github.com/prometheus/client_golang/prometheus/promauto"
"github.com/prometheus/common/model"
"github.com/prometheus/prometheus/model/timestamp"
"github.com/prometheus/prometheus/promql"
"github.com/prometheus/prometheus/promql/parser"
"github.com/uber/jaeger-client-go"
"github.com/grafana/mimir/pkg/mimirpb"
"github.com/grafana/mimir/pkg/util"
"github.com/grafana/mimir/pkg/util/math"
)
const (
// resultsCacheVersion should be increased every time cache should be invalidated (after a bugfix or cache format change).
resultsCacheVersion = 1
// cacheControlHeader is the name of the cache control header.
cacheControlHeader = "Cache-Control"
// noStoreValue is the value that cacheControlHeader has if the response indicates that the results should not be cached.
noStoreValue = "no-store"
)
var (
supportedResultsCacheBackends = []string{cache.BackendMemcached, cache.BackendRedis}
errUnsupportedBackend = errors.New("unsupported cache backend")
)
// ResultsCacheConfig is the config for the results cache.
type ResultsCacheConfig struct {
cache.BackendConfig `yaml:",inline"`
Compression cache.CompressionConfig `yaml:",inline"`
}
// RegisterFlags registers flags.
func (cfg *ResultsCacheConfig) RegisterFlags(f *flag.FlagSet) {
f.StringVar(&cfg.Backend, "query-frontend.results-cache.backend", "", fmt.Sprintf("Backend for query-frontend results cache, if not empty. Supported values: %s.", strings.Join(supportedResultsCacheBackends, ", ")))
cfg.Memcached.RegisterFlagsWithPrefix("query-frontend.results-cache.memcached.", f)
cfg.Redis.RegisterFlagsWithPrefix("query-frontend.results-cache.redis.", f)
cfg.Compression.RegisterFlagsWithPrefix(f, "query-frontend.results-cache.")
}
func (cfg *ResultsCacheConfig) Validate() error {
if cfg.Backend != "" && !util.StringsContain(supportedResultsCacheBackends, cfg.Backend) {
return errUnsupportedResultsCacheBackend(cfg.Backend)
}
switch cfg.Backend {
case cache.BackendMemcached:
if err := cfg.Memcached.Validate(); err != nil {
return errors.Wrap(err, "query-frontend results cache")
}
case cache.BackendRedis:
if err := cfg.Redis.Validate(); err != nil {
return errors.Wrap(err, "query-frontend results cache")
}
}
if err := cfg.Compression.Validate(); err != nil {
return errors.Wrap(err, "query-frontend results cache")
}
return nil
}
func errUnsupportedResultsCacheBackend(backend string) error {
return fmt.Errorf("%w: %q, supported values: %v", errUnsupportedBackend, backend, supportedResultsCacheBackends)
}
type resultsCacheMetrics struct {
cacheRequests prometheus.Counter
cacheHits prometheus.Counter
}
func newResultsCacheMetrics(requestType string, reg prometheus.Registerer) *resultsCacheMetrics {
return &resultsCacheMetrics{
cacheRequests: promauto.With(reg).NewCounter(prometheus.CounterOpts{
Name: "cortex_frontend_query_result_cache_requests_total",
Help: "Total number of requests (or partial requests) looked up in the results cache.",
ConstLabels: map[string]string{"request_type": requestType},
}),
cacheHits: promauto.With(reg).NewCounter(prometheus.CounterOpts{
Name: "cortex_frontend_query_result_cache_hits_total",
Help: "Total number of requests (or partial requests) fetched from the results cache.",
ConstLabels: map[string]string{"request_type": requestType},
}),
}
}
// newResultsCache creates a new results cache based on the input configuration.
func newResultsCache(cfg ResultsCacheConfig, logger log.Logger, reg prometheus.Registerer) (cache.Cache, error) {
// Add the "component" label similarly to other components, so that metrics don't clash and have the same labels set
// when running in monolithic mode.
reg = prometheus.WrapRegistererWith(prometheus.Labels{"component": "query-frontend"}, reg)
client, err := cache.CreateClient("frontend-cache", cfg.BackendConfig, logger, prometheus.WrapRegistererWithPrefix("thanos_", reg))
if err != nil {
return nil, err
} else if client == nil {
return nil, errUnsupportedResultsCacheBackend(cfg.Backend)
}
return cache.NewVersioned(
cache.NewSpanlessTracingCache(client, logger, tenant.NewMultiResolver()),
resultsCacheVersion,
), nil
}
// Extractor is used by the cache to extract a subset of a response from a cache entry.
type Extractor interface {
// Extract extracts a subset of a response from the `start` and `end` timestamps in milliseconds in the `from` response.
Extract(start, end int64, from Response) Response
ResponseWithoutHeaders(resp Response) Response
}
// PrometheusResponseExtractor helps extracting specific info from Query Response.
type PrometheusResponseExtractor struct{}
// Extract extracts response for specific a range from a response.
func (PrometheusResponseExtractor) Extract(start, end int64, from Response) Response {
promRes := from.(*PrometheusResponse)
var data *PrometheusData
if promRes.Data != nil {
data = &PrometheusData{
ResultType: promRes.Data.ResultType,
Result: extractMatrix(start, end, promRes.Data.Result),
}
}
return &PrometheusResponse{
Status: promRes.Status,
Data: data,
Headers: promRes.Headers,
Warnings: promRes.Warnings,
}
}
// ResponseWithoutHeaders is useful in caching data without headers since
// we anyways do not need headers for sending back the response so this saves some space by reducing size of the objects.
func (PrometheusResponseExtractor) ResponseWithoutHeaders(resp Response) Response {
promRes := resp.(*PrometheusResponse)
var data *PrometheusData
if promRes.Data != nil {
data = &PrometheusData{
ResultType: promRes.Data.ResultType,
Result: promRes.Data.Result,
}
}
return &PrometheusResponse{
Status: promRes.Status,
Data: data,
Warnings: promRes.Warnings,
}
}
// ErrUnsupportedRequest is intended to be used with CacheKeyGenerator
var ErrUnsupportedRequest = errors.New("request is not cacheable")
// CacheKeyGenerator generates cache keys. This is a useful interface for downstream
// consumers who wish to implement their own strategies.
type CacheKeyGenerator interface {
// QueryRequest should generate a cache key based on the tenant ID and MetricsQueryRequest.
QueryRequest(ctx context.Context, tenantID string, r MetricsQueryRequest) string
// LabelValues should return a cache key for a label values request. The cache key does not need to contain the tenant ID.
// LabelValues can return ErrUnsupportedRequest, in which case the response won't be treated as an error, but the item will still not be cached.
// LabelValues should return a nil *GenericQueryCacheKey when it returns an error and
// should always return non-nil *GenericQueryCacheKey when the returned error is nil.
LabelValues(r *http.Request) (*GenericQueryCacheKey, error)
// LabelValuesCardinality should return a cache key for a label values cardinality request. The cache key does not need to contain the tenant ID.
// LabelValuesCardinality can return ErrUnsupportedRequest, in which case the response won't be treated as an error, but the item will still not be cached.
// LabelValuesCardinality should return a nil *GenericQueryCacheKey when it returns an error and
// should always return non-nil *GenericQueryCacheKey when the returned error is nil.
LabelValuesCardinality(r *http.Request) (*GenericQueryCacheKey, error)
}
type DefaultCacheKeyGenerator struct {
codec Codec
// interval is a constant split interval when determining cache keys for QueryRequest.
interval time.Duration
}
func NewDefaultCacheKeyGenerator(codec Codec, interval time.Duration) DefaultCacheKeyGenerator {
return DefaultCacheKeyGenerator{
codec: codec,
interval: interval,
}
}
// QueryRequest generates a cache key based on the userID, MetricsQueryRequest and interval.
func (g DefaultCacheKeyGenerator) QueryRequest(_ context.Context, userID string, r MetricsQueryRequest) string {
startInterval := r.GetStart() / g.interval.Milliseconds()
stepOffset := r.GetStart() % r.GetStep()
// Use original format for step-aligned request, so that we can use existing cached results for such requests.
if stepOffset == 0 {
return fmt.Sprintf("%s:%s:%d:%d", userID, r.GetQuery(), r.GetStep(), startInterval)
}
return fmt.Sprintf("%s:%s:%d:%d:%d", userID, r.GetQuery(), r.GetStep(), startInterval, stepOffset)
}
// shouldCacheFn checks whether the current request should go to cache
// or not. If not, just send the request to next handler.
type shouldCacheFn func(r MetricsQueryRequest) bool
// resultsCacheAlwaysEnabled is a shouldCacheFn function always returning true.
var resultsCacheAlwaysEnabled = func(_ MetricsQueryRequest) bool { return true }
// isRequestCachable says whether the request is eligible for caching.
func isRequestCachable(req MetricsQueryRequest, maxCacheTime int64, cacheUnalignedRequests bool, logger log.Logger) (cachable bool, reason string) {
// We can run with step alignment disabled because Grafana does it already. Mimir automatically aligning start and end is not
// PromQL compatible. But this means we cannot cache queries that do not have their start and end aligned.
if !cacheUnalignedRequests && !isRequestStepAligned(req) {
return false, notCachableReasonUnalignedTimeRange
}
// Do not cache it at all if the query time range is more recent than the configured max cache freshness.
if req.GetStart() > maxCacheTime {
return false, notCachableReasonTooNew
}
if !areEvaluationTimeModifiersCachable(req, maxCacheTime, logger) {
return false, notCachableReasonModifiersNotCachable
}
return true, ""
}
// isResponseCachable says whether the response should be cached or not.
func isResponseCachable(r Response, logger log.Logger) bool {
headerValues := getHeaderValuesWithName(r, cacheControlHeader)
for _, v := range headerValues {
if v == noStoreValue {
level.Debug(logger).Log("msg", fmt.Sprintf("%s header in response is equal to %s, not caching the response", cacheControlHeader, noStoreValue))
return false
}
}
return true
}
var (
errAtModifierAfterEnd = errors.New("at modifier after end")
errNegativeOffset = errors.New("negative offset")
)
// areEvaluationTimeModifiersCachable returns true if the @ modifier and the offset modifier results are safe to cache.
func areEvaluationTimeModifiersCachable(r MetricsQueryRequest, maxCacheTime int64, logger log.Logger) bool {
// There are 3 cases when evaluation time modifiers are not safe to cache:
// 1. When @ modifier points to time beyond the maxCacheTime.
// 2. If the @ modifier time is > the query range end while being
// below maxCacheTime. In such cases if any tenant is intentionally
// playing with old data, we could cache empty result if we look
// beyond query end.
// 3. When query contains a negative offset.
query := r.GetQuery()
if !strings.Contains(query, "@") && !strings.Contains(query, "offset") {
return true
}
expr, err := parser.ParseExpr(query)
if err != nil {
// We are being pessimistic in such cases.
level.Warn(logger).Log("msg", "failed to parse query, considering @ modifier as not cachable", "query", query, "err", err)
return false
}
// This resolves the start() and end() used with the @ modifier.
expr = promql.PreprocessExpr(expr, timestamp.Time(r.GetStart()), timestamp.Time(r.GetEnd()))
end := r.GetEnd()
cachable := true
check := func(ts *int64, offset time.Duration) error {
if offset < 0 {
cachable = false
return errNegativeOffset
}
if ts != nil && (*ts > end || *ts > maxCacheTime) {
cachable = false
return errAtModifierAfterEnd
}
return nil
}
parser.Inspect(expr, func(n parser.Node, _ []parser.Node) error {
switch e := n.(type) {
case *parser.VectorSelector:
return check(e.Timestamp, e.OriginalOffset)
case *parser.SubqueryExpr:
return check(e.Timestamp, e.OriginalOffset)
}
return nil
})
return cachable
}
func getHeaderValuesWithName(r Response, headerName string) (headerValues []string) {
for _, hv := range r.GetHeaders() {
if hv.GetName() != headerName {
continue
}
headerValues = append(headerValues, hv.GetValues()...)
}
return
}
// mergeCacheExtentsForRequest merges the provided cache extents for the input request and returns merged extents.
// The input extents can be overlapping and are not required to be sorted.
func mergeCacheExtentsForRequest(ctx context.Context, r MetricsQueryRequest, merger Merger, extents []Extent) ([]Extent, error) {
// Fast path.
if len(extents) <= 1 {
return extents, nil
}
sort.Slice(extents, func(i, j int) bool {
if extents[i].Start == extents[j].Start {
// as an optimization, for two extents starts at the same time, we
// put bigger extent at the front of the slice, which helps
// to reduce the amount of merge we have to do later.
return extents[i].End > extents[j].End
}
return extents[i].Start < extents[j].Start
})
// Merge any extents - potentially overlapping
accumulator, err := newAccumulator(extents[0])
if err != nil {
return nil, err
}
mergedExtents := make([]Extent, 0, len(extents))
for i := 1; i < len(extents); i++ {
if accumulator.End+r.GetStep() < extents[i].Start {
mergedExtents, err = mergeCacheExtentsWithAccumulator(mergedExtents, accumulator)
if err != nil {
return nil, err
}
accumulator, err = newAccumulator(extents[i])
if err != nil {
return nil, err
}
continue
}
if accumulator.End >= extents[i].End {
continue
}
accumulator.TraceId = jaegerTraceID(ctx)
accumulator.End = extents[i].End
currentRes, err := extents[i].toResponse()
if err != nil {
return nil, err
}
merged, err := merger.MergeResponse(accumulator.Response, currentRes)
if err != nil {
return nil, err
}
accumulator.Response = merged
if accumulator.QueryTimestampMs > 0 && extents[i].QueryTimestampMs > 0 {
// Keep older (minimum) timestamp.
accumulator.QueryTimestampMs = math.Min(accumulator.QueryTimestampMs, extents[i].QueryTimestampMs)
} else {
// Some old extents may have zero timestamps. In that case we keep the non-zero one.
// (Hopefully one of them is not zero, since we're only merging if there are some new extents.)
accumulator.QueryTimestampMs = math.Max(accumulator.QueryTimestampMs, extents[i].QueryTimestampMs)
}
}
return mergeCacheExtentsWithAccumulator(mergedExtents, accumulator)
}
type accumulator struct {
Response
Extent
}
func mergeCacheExtentsWithAccumulator(extents []Extent, acc *accumulator) ([]Extent, error) {
marshalled, err := types.MarshalAny(acc.Response)
if err != nil {
return nil, err
}
return append(extents, Extent{
Start: acc.Extent.Start,
End: acc.Extent.End,
Response: marshalled,
TraceId: acc.Extent.TraceId,
QueryTimestampMs: acc.QueryTimestampMs,
}), nil
}
func newAccumulator(base Extent) (*accumulator, error) {
res, err := base.toResponse()
if err != nil {
return nil, err
}
return &accumulator{
Response: res,
Extent: base,
}, nil
}
func toExtent(ctx context.Context, req MetricsQueryRequest, res Response, queryTime time.Time) (Extent, error) {
marshalled, err := types.MarshalAny(res)
if err != nil {
return Extent{}, err
}
return Extent{
Start: req.GetStart(),
End: req.GetEnd(),
Response: marshalled,
TraceId: jaegerTraceID(ctx),
QueryTimestampMs: queryTime.UnixMilli(),
}, nil
}
// partitionCacheExtents calculates the required requests to satisfy req given the cached data.
// extents must be in order by start time.
func partitionCacheExtents(req MetricsQueryRequest, extents []Extent, minCacheExtent int64, extractor Extractor) ([]MetricsQueryRequest, []Response, error) {
var requests []MetricsQueryRequest
var cachedResponses []Response
start := req.GetStart()
for _, extent := range extents {
// If there is no overlap, ignore this extent.
if extent.GetEnd() < start || extent.Start > req.GetEnd() {
continue
}
// If this extent is tiny and request is not tiny, discard it: more efficient to do a few larger queries.
// Hopefully tiny request can make tiny extent into not-so-tiny extent.
// However if the step is large enough, the split_query_by_interval middleware would generate a query with same start and end.
// For example, if the step size is more than 12h and the interval is 24h.
// This means the extent's start and end time would be same, even if the timerange covers several hours.
if (req.GetStart() != req.GetEnd()) && (req.GetEnd()-req.GetStart() > minCacheExtent) && (extent.End-extent.Start < minCacheExtent) {
continue
}
// If there is a bit missing at the front, make a request for that.
if start < extent.Start {
r := req.WithStartEnd(start, extent.Start)
requests = append(requests, r)
}
res, err := extent.toResponse()
if err != nil {
return nil, nil, err
}
// extract the overlap from the cached extent.
cachedResponses = append(cachedResponses, extractor.Extract(start, req.GetEnd(), res))
// We want next request to start where extent ends, but we must make sure that
// next start also has the same offset into the step as original request had, ie.
// "start % req.Step" must be the same as "req.GetStart() % req.GetStep()".
// We do that by computing "adjustment". Go's % operator is a "remainder" operator
// and not "modulo" operator, which means it returns negative numbers in our case or zero
// (because request.GetStart <= extent.End), and we need to adjust it by one step forward.
// We don't do adjustments if extent.End is already on the same step-offset as request.Start,
// although technically we could. But existing unit tests expect existing behaviour.
adjust := (req.GetStart() - extent.End) % req.GetStep()
if adjust < 0 {
adjust += req.GetStep()
}
start = extent.End + adjust
}
// Lastly, make a request for any data missing at the end.
if start < req.GetEnd() {
r := req.WithStartEnd(start, req.GetEnd())
requests = append(requests, r)
}
// If start and end are the same (valid in promql), start == req.GetEnd() and we won't do the query.
// But we should only do the request if we don't have a valid cached response for it.
if req.GetStart() == req.GetEnd() && len(cachedResponses) == 0 {
requests = append(requests, req)
}
return requests, cachedResponses, nil
}
func filterRecentCacheExtents(req MetricsQueryRequest, maxCacheFreshness time.Duration, extractor Extractor, extents []Extent) ([]Extent, error) {
maxCacheTime := (int64(model.Now().Add(-maxCacheFreshness)) / req.GetStep()) * req.GetStep()
for i := range extents {
// Never cache data for the latest freshness period.
if extents[i].End > maxCacheTime {
extents[i].End = maxCacheTime
res, err := extents[i].toResponse()
if err != nil {
return nil, err
}
extracted := extractor.Extract(extents[i].Start, maxCacheTime, res)
marshalled, err := types.MarshalAny(extracted)
if err != nil {
return nil, err
}
extents[i].Response = marshalled
}
}
return extents, nil
}
func jaegerTraceID(ctx context.Context) string {
span := opentracing.SpanFromContext(ctx)
if span == nil {
return ""
}
spanContext, ok := span.Context().(jaeger.SpanContext)
if !ok {
return ""
}
return spanContext.TraceID().String()
}
func extractMatrix(start, end int64, matrix []SampleStream) []SampleStream {
result := make([]SampleStream, 0, len(matrix))
for _, stream := range matrix {
extracted, ok := extractSampleStream(start, end, stream)
if ok {
result = append(result, extracted)
}
}
return result
}
func filterFloatStream(start, end int64, streamSamples []mimirpb.Sample) []mimirpb.Sample {
result := make([]mimirpb.Sample, 0, len(streamSamples))
for _, sample := range streamSamples {
if start <= sample.TimestampMs && sample.TimestampMs <= end {
result = append(result, sample)
}
}
return result
}
func filterHistogramStream(start, end int64, streamSamples []mimirpb.FloatHistogramPair) []mimirpb.FloatHistogramPair {
result := make([]mimirpb.FloatHistogramPair, 0, len(streamSamples))
for _, sample := range streamSamples {
if start <= sample.TimestampMs && sample.TimestampMs <= end {
result = append(result, sample)
}
}
return result
}
func extractSampleStream(start, end int64, stream SampleStream) (SampleStream, bool) {
result := SampleStream{
Labels: stream.Labels,
}
gotSamples := false
gotHistograms := false
if len(stream.Histograms) > 0 {
histograms := filterHistogramStream(start, end, stream.Histograms)
if len(histograms) > 0 {
result.Histograms = histograms
gotHistograms = true
}
}
if len(stream.Samples) > 0 {
samples := filterFloatStream(start, end, stream.Samples)
if len(samples) > 0 {
result.Samples = samples
gotSamples = true
}
}
if !gotHistograms && !gotSamples {
return SampleStream{}, false
}
return result, true
}
func (e *Extent) toResponse() (Response, error) {
msg, err := types.EmptyAny(e.Response)
if err != nil {
return nil, err
}
if err := types.UnmarshalAny(e.Response, msg); err != nil {
return nil, err
}
resp, ok := msg.(Response)
if !ok {
return nil, fmt.Errorf("bad cached type")
}
return resp, nil
}
// cacheHashKey hashes key into something you can store in the results cache.
func cacheHashKey(key string) string {
hasher := fnv.New64a()
_, _ = hasher.Write([]byte(key)) // This'll never error.
// Hex because memcache errors for the bytes produced by the hash.
return hex.EncodeToString(hasher.Sum(nil))
}