prometheus/promql
beorn7 0e202dacb4 Streamline series iterator creation
This will fix issue #1035 and will also help to make issue #1264 less
bad.

The fundamental problem in the current code:

In the preload phase, we quite accurately determine which chunks will
be used for the query being executed. However, in the subsequent step
of creating series iterators, the created iterators are referencing
_all_ in-memory chunks in their series, even the un-pinned ones. In
iterator creation, we copy a pointer to each in-memory chunk of a
series into the iterator. While this creates a certain amount of
allocation churn, the worst thing about it is that copying the chunk
pointer out of the chunkDesc requires a mutex acquisition. (Remember
that the iterator will also reference un-pinned chunks, so we need to
acquire the mutex to protect against concurrent eviction.) The worst
case happens if a series doesn't even contain any relevant samples for
the query time range. We notice that during preloading but then we
will still create a series iterator for it. But even for series that
do contain relevant samples, the overhead is quite bad for instant
queries that retrieve a single sample from each series, but still go
through all the effort of series iterator creation. All of that is
particularly bad if a series has many in-memory chunks.

This commit addresses the problem from two sides:

First, it merges preloading and iterator creation into one step,
i.e. the preload call returns an iterator for exactly the preloaded
chunks.

Second, the required mutex acquisition in chunkDesc has been greatly
reduced. That was enabled by a side effect of the first step, which is
that the iterator is only referencing pinned chunks, so there is no
risk of concurrent eviction anymore, and chunks can be accessed
without mutex acquisition.

To simplify the code changes for the above, the long-planned change of
ValueAtTime to ValueAtOrBefore time was performed at the same
time. (It should have been done first, but it kind of accidentally
happened while I was in the middle of writing the series iterator
changes. Sorry for that.) So far, we actively filtered the up to two
values that were returned by ValueAtTime, i.e. we invested work to
retrieve up to two values, and then we invested more work to throw one
of them away.

The SeriesIterator.BoundaryValues method can be removed once #1401 is
fixed. But I really didn't want to load even more changes into this
PR.

Benchmarks:

The BenchmarkFuzz.* benchmarks run 83% faster (i.e. about six times
faster) and allocate 95% fewer bytes. The reason for that is that the
benchmark reads one sample after another from the time series and
creates a new series iterator for each sample read.

To find out how much these improvements matter in practice, I have
mirrored a beefy Prometheus server at SoundCloud that suffers from
both issues #1035 and #1264. To reach steady state that would be
comparable, the server needs to run for 15d. So far, it has run for
1d. The test server currently has only half as many memory time series
and 60% of the memory chunks the main server has. The 90th percentile
rule evaluation cycle time is ~11s on the main server and only ~3s on
the test server. However, these numbers might get much closer over
time.

In addition to performance improvements, this commit removes about 150
LOC.
2016-02-19 16:24:38 +01:00
..
testdata Add without aggregator modifier. 2016-02-08 14:05:33 +00:00
analyzer.go Streamline series iterator creation 2016-02-19 16:24:38 +01:00
ast.go Fix various typos in comments. 2016-02-10 03:47:00 +01:00
engine.go Streamline series iterator creation 2016-02-19 16:24:38 +01:00
engine_test.go promql: Reduce flakiness of concurrency test 2015-09-23 10:07:30 +01:00
functions.go Streamline series iterator creation 2016-02-19 16:24:38 +01:00
lex.go Add without aggregator modifier. 2016-02-08 14:05:33 +00:00
lex_test.go Add without aggregator modifier. 2016-02-08 14:05:33 +00:00
parse.go Fix various typos in comments. 2016-02-10 03:47:00 +01:00
parse_test.go Add without aggregator modifier. 2016-02-08 14:05:33 +00:00
printer.go Add without aggregator modifier. 2016-02-08 14:05:33 +00:00
printer_test.go Add without aggregator modifier. 2016-02-08 14:05:33 +00:00
promql_test.go Fix most golint warnings. 2015-08-26 12:44:46 +02:00
quantile.go Switch promql types to common/model 2015-08-25 13:49:14 +02:00
test.go Unify duration parsing 2016-01-29 15:41:50 +01:00