From bce8731e60c6d11721ddcb920016aa0bb8d62d04 Mon Sep 17 00:00:00 2001 From: Leo Yuriev Date: Tue, 13 Jun 2017 19:48:50 +0300 Subject: [PATCH] mdbx: fix mdbx_txn_straggler() for write-txn (backport from devel). --- mdbx.c | 29 +++++++++++++++++------------ 1 file changed, 17 insertions(+), 12 deletions(-) diff --git a/mdbx.c b/mdbx.c index 66c377d8..378beafa 100644 --- a/mdbx.c +++ b/mdbx.c @@ -140,7 +140,8 @@ int mdbx_txn_straggler(MDB_txn *txn, int *percent) { MDB_env *env; MDB_meta *meta; - txnid_t lag; + txnid_t recent, lag; + size_t maxpg; if(unlikely(!txn)) return -EINVAL; @@ -148,19 +149,23 @@ int mdbx_txn_straggler(MDB_txn *txn, int *percent) if(unlikely(txn->mt_signature != MDBX_MT_SIGNATURE)) return MDB_VERSION_MISMATCH; - if (unlikely(! txn->mt_u.reader)) - return -1; - env = txn->mt_env; - meta = mdb_meta_head_r(env); - if (percent) { - size_t maxpg = env->me_maxpg; - size_t last = meta->mm_last_pg + 1; - if (env->me_txn) - last = env->me_txn0->mt_next_pgno; - *percent = (last * 100ull + maxpg / 2) / maxpg; + maxpg = env->me_maxpg; + if (unlikely((txn->mt_flags & MDB_RDONLY) == 0)) { + *percent = (int)((txn->mt_next_pgno * 100ull + maxpg / 2) / maxpg); + return -1; } - lag = meta->mm_txnid - txn->mt_u.reader->mr_txnid; + + do { + meta = mdb_meta_head_r(env); + recent = meta->mm_txnid; + if (percent) { + pgno_t last = meta->mm_last_pg + 1; + *percent = (int)((last * 100ull + maxpg / 2) / maxpg); + } + } while (unlikely(recent != meta->mm_txnid)); + + lag = recent - txn->mt_u.reader->mr_txnid; return (0 > (long) lag) ? ~0u >> 1: lag; }