From f1d81b9405567f76ce1bc5eb60b1af9eb465f37c Mon Sep 17 00:00:00 2001 From: Aliaksandr Valialkin Date: Wed, 14 Aug 2019 01:50:20 +0300 Subject: [PATCH] lib/storage: compress contents of cache for tagFilters -> TSIDs This should increase cache capacity --- lib/storage/index_db.go | 26 +++++++++++++++++++++----- 1 file changed, 21 insertions(+), 5 deletions(-) diff --git a/lib/storage/index_db.go b/lib/storage/index_db.go index 0eb63d31bf..c0664f48bb 100644 --- a/lib/storage/index_db.go +++ b/lib/storage/index_db.go @@ -303,20 +303,36 @@ func (db *indexDB) decRef() { } func (db *indexDB) getFromTagCache(key []byte) ([]TSID, bool) { - value := db.tagCache.GetBig(nil, key) - if len(value) == 0 { + compressedBuf := tagBufPool.Get() + defer tagBufPool.Put(compressedBuf) + compressedBuf.B = db.tagCache.GetBig(compressedBuf.B[:0], key) + if len(compressedBuf.B) == 0 { return nil, false } - tsids, err := unmarshalTSIDs(nil, value) + buf := tagBufPool.Get() + defer tagBufPool.Put(buf) + var err error + buf.B, err = encoding.DecompressZSTD(buf.B[:0], compressedBuf.B) + if err != nil { + logger.Panicf("FATAL: cannot decompress tsids from tagCache: %s", err) + } + tsids, err := unmarshalTSIDs(nil, buf.B) if err != nil { logger.Panicf("FATAL: cannot unmarshal tsids from tagCache: %s", err) } return tsids, true } +var tagBufPool bytesutil.ByteBufferPool + func (db *indexDB) putToTagCache(tsids []TSID, key []byte) { - value := marshalTSIDs(nil, tsids) - db.tagCache.SetBig(key, value) + buf := tagBufPool.Get() + buf.B = marshalTSIDs(buf.B[:0], tsids) + compressedBuf := tagBufPool.Get() + compressedBuf.B = encoding.CompressZSTDLevel(compressedBuf.B[:0], buf.B, 1) + tagBufPool.Put(buf) + db.tagCache.SetBig(key, compressedBuf.B) + tagBufPool.Put(compressedBuf) } func (db *indexDB) getFromMetricIDCache(dst *TSID, metricID uint64) error {