From 76deaa986ec40b746f47e336598b1b4e583377ca Mon Sep 17 00:00:00 2001 From: zjubfd <296179868@qq.com> Date: Mon, 4 Nov 2019 16:37:18 +0800 Subject: [PATCH] state: txindex/kv: fsync data to disk immediately after receiving it (#4104) ## Issue Why this pr: When restarting chain node, sometimes we lost tx index about recent(around 80)blocks, and some client complains that they can't find the tx by RPC call(tx_search) when the tx do exist in the block. I try to partially fix this issue in a simple way by writing the index data in a sync way. There is no performance difference under 1K TPS according to our test. It is still possible that lost index data after restarting the node, but only 2 block data will lost at most. I try to totally fix this in https://github.com/tendermint/tendermint/pull/3847/files, but this one is simple and can solve most part of the issue. Please review first, thks. ## Comments Anton: BEFORE: BenchmarkTxIndex1-2 100000 12434 ns/op BenchmarkTxIndex500-2 300 5151564 ns/op BenchmarkTxIndex1000-2 100 15053910 ns/op BenchmarkTxIndex2000-2 100 18238892 ns/op BenchmarkTxIndex10000-2 20 124287930 ns/op AFTER: BenchmarkTxIndex1-2 2000 795431 ns/op BenchmarkTxIndex500-2 200 6385124 ns/op BenchmarkTxIndex1000-2 100 11388219 ns/op BenchmarkTxIndex2000-2 100 20514873 ns/op BenchmarkTxIndex10000-2 20 107456004 ns/op Performance drop is pretty steep, but I think it's the right thing to do UNTIL we have a WAL. --- CHANGELOG_PENDING.md | 1 + state/txindex/kv/kv.go | 4 ++-- 2 files changed, 3 insertions(+), 2 deletions(-) diff --git a/CHANGELOG_PENDING.md b/CHANGELOG_PENDING.md index 58ac79bfd..e33081dd4 100644 --- a/CHANGELOG_PENDING.md +++ b/CHANGELOG_PENDING.md @@ -30,3 +30,4 @@ program](https://hackerone.com/tendermint). ### BUG FIXES: - [tools] [\#4023](https://github.com/tendermint/tendermint/issues/4023) Refresh `tm-monitor` health when validator count is updated (@erikgrinaker) +- [state] [\#4104](https://github.com/tendermint/tendermint/pull/4104) txindex/kv: Fsync data to disk immediately after receiving it (@guagualvcha) diff --git a/state/txindex/kv/kv.go b/state/txindex/kv/kv.go index 8fcce4862..dd9c5f300 100644 --- a/state/txindex/kv/kv.go +++ b/state/txindex/kv/kv.go @@ -103,7 +103,7 @@ func (txi *TxIndex) AddBatch(b *txindex.Batch) error { storeBatch.Set(hash, rawBytes) } - storeBatch.Write() + storeBatch.WriteSync() return nil } @@ -132,7 +132,7 @@ func (txi *TxIndex) Index(result *types.TxResult) error { } b.Set(hash, rawBytes) - b.Write() + b.WriteSync() return nil }