From ea3850e86a0b16ee0f0fa6ac0a6041315341e234 Mon Sep 17 00:00:00 2001 From: rjl493456442 Date: Wed, 25 Sep 2019 17:29:15 +0800 Subject: [PATCH] ethdb/leveldb: adjust write buffer size --- ethdb/leveldb/leveldb.go | 30 ++++++++++++++++++++++++++++-- 1 file changed, 28 insertions(+), 2 deletions(-) diff --git a/ethdb/leveldb/leveldb.go b/ethdb/leveldb/leveldb.go index 71ad8cd227..a954731ee4 100644 --- a/ethdb/leveldb/leveldb.go +++ b/ethdb/leveldb/leveldb.go @@ -43,9 +43,17 @@ const ( degradationWarnInterval = time.Minute // minCache is the minimum amount of memory in megabytes to allocate to leveldb - // read and write caching, split half and half. + // read and write caching. minCache = 16 + // minWriteBufferCache is the minimum amount of memory in megabytes to allocate + // to leveldb's write buffer. + minWriteBufferCache = 4 + + // minWriteBufferCache is the maximum amount of memory in megabytes to allocate + // to leveldb's write buffer. + maxWriteBufferCache = 256 + // minHandles is the minimum number of files handles to allocate to the open // database files. minHandles = 16 @@ -94,6 +102,23 @@ func New(file string, cache int, handles int, namespace string) (*Database, erro logger := log.New("database", file) logger.Info("Allocated cache and file handles", "cache", common.StorageSize(cache*1024*1024), "handles", handles) + // Assign write buffer with a "suitable" capacity. There are two internal + // write buffers(one live memdb, one frozen memdb), so the total capacity + // of write buffer is 1/8 cache. + // + // The capacity of write buffer can't be too large or too small. If the size + // is too large, then we will accumulate lots of data in memory and then + // dump them all at some time, which creates disk spike since lots of compaction + // tasks are created. Also if the size of write buffer is too small, then + // we will dump lots of small files at low level and they will be compacted + // with new files later. Unnecessary compaction is introduced. + writeBuffer := cache / 16 + if writeBuffer >= maxWriteBufferCache { + writeBuffer = maxWriteBufferCache + } + if writeBuffer <= minWriteBufferCache { + writeBuffer = minWriteBufferCache + } // Open the db and recover any potential corruptions db, err := leveldb.OpenFile(file, &opt.Options{ OpenFilesCacheCapacity: handles, @@ -101,7 +126,8 @@ func New(file string, cache int, handles int, namespace string) (*Database, erro // BlockCache can be used for caching: data block, index block as well as // filter block. The more capacity block cache has, the less disk hit we // have. - BlockCacheCapacity: cache * opt.MiB, + BlockCacheCapacity: 7 * cache / 8 * opt.MiB, + WriteBuffer: writeBuffer * opt.MiB, Filter: filter.NewBloomFilter(10), DisableSeeksCompaction: true, })