Skip to content

Commit 7d1eac9

Browse files
committed
avoid dumping a wrong cached data
1 parent 6cd5190 commit 7d1eac9

2 files changed

Lines changed: 12 additions & 1 deletion

File tree

chainbase/src/main/java/org/tron/core/db2/common/TxCacheDB.java

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
import java.util.Map.Entry;
2121
import java.util.Properties;
2222
import java.util.concurrent.CompletableFuture;
23+
import java.util.concurrent.atomic.AtomicBoolean;
2324
import lombok.extern.slf4j.Slf4j;
2425
import org.apache.commons.lang3.ArrayUtils;
2526
import org.bouncycastle.util.encoders.Hex;
@@ -76,6 +77,7 @@ public class TxCacheDB implements DB<byte[], byte[]>, Flusher {
7677
private final Path cacheFile1;
7778
private final Path cacheProperties;
7879
private final Path cacheDir;
80+
private AtomicBoolean isValid = new AtomicBoolean(false);
7981

8082
public TxCacheDB(String name, RecentTransactionStore recentTransactionStore) {
8183
this.name = name;
@@ -135,6 +137,7 @@ private void initCache() {
135137

136138
public void init() {
137139
if (recovery()) {
140+
isValid.set(true);
138141
return;
139142
}
140143
long size = recentTransactionStore.size();
@@ -156,6 +159,7 @@ public void init() {
156159
logger.info("Load cache from recentTransactionStore, filter: {}, filter-fpp: {}, cost: {} ms.",
157160
bloomFilters[1].approximateElementCount(), bloomFilters[1].expectedFpp(),
158161
System.currentTimeMillis() - start);
162+
isValid.set(true);
159163
}
160164

161165
@Override
@@ -235,8 +239,10 @@ public Iterator<Entry<byte[], byte[]>> iterator() {
235239
}
236240

237241
@Override
238-
public void flush(Map<WrappedByteArray, WrappedByteArray> batch) {
242+
public synchronized void flush(Map<WrappedByteArray, WrappedByteArray> batch) {
243+
isValid.set(false);
239244
batch.forEach((k, v) -> this.put(k.getBytes(), v.getBytes()));
245+
isValid.set(true);
240246
}
241247

242248
@Override
@@ -298,6 +304,9 @@ private boolean handleException(Throwable e) {
298304
}
299305

300306
private void dump() {
307+
if (!isValid.get()) {
308+
logger.info("bloomFilters is not valid.");
309+
}
301310
FileUtil.createDirIfNotExists(this.cacheDir.toString());
302311
logger.info("dump bloomFilters start.");
303312
CompletableFuture<Void> task0 = CompletableFuture.runAsync(

framework/src/test/java/org/tron/core/db/TxCacheDBInitTest.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,8 @@ public static void init() throws IOException {
4545

4646
@Test
4747
public void reload() {
48+
TransactionCache db = context.getBean(TransactionCache.class);
49+
db.initCache();
4850
putTransaction();
4951
DefaultListableBeanFactory defaultListableBeanFactory =
5052
(DefaultListableBeanFactory) context.getAutowireCapableBeanFactory();

0 commit comments

Comments
 (0)