Skip to content

Commit

Permalink
feat(db):optimize cache settings (#5659)
Browse files Browse the repository at this point in the history
  • Loading branch information
o85372940 authored Jan 11, 2024
1 parent d910713 commit e14f3dd
Show file tree
Hide file tree
Showing 5 changed files with 31 additions and 30 deletions.
22 changes: 1 addition & 21 deletions chainbase/src/main/java/org/tron/core/store/WitnessStore.java
Original file line number Diff line number Diff line change
Expand Up @@ -11,25 +11,17 @@
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;
import org.tron.common.cache.CacheManager;
import org.tron.common.cache.CacheStrategies;
import org.tron.common.cache.CacheType;
import org.tron.common.cache.TronCache;
import org.tron.core.capsule.WitnessCapsule;
import org.tron.core.config.Parameter;
import org.tron.core.db.TronStoreWithRevoking;

@Slf4j(topic = "DB")
@Component
public class WitnessStore extends TronStoreWithRevoking<WitnessCapsule> {
// cache for 127 SR
private final TronCache<Integer, List<WitnessCapsule>> witnessStandbyCache;

@Autowired
protected WitnessStore(@Value("witness") String dbName) {
super(dbName);
String strategy = String.format(CacheStrategies.PATTERNS, 1, 1, "30s", 1);
witnessStandbyCache = CacheManager.allocate(CacheType.witnessStandby, strategy);
}

/**
Expand All @@ -48,19 +40,8 @@ public WitnessCapsule get(byte[] key) {
}

public List<WitnessCapsule> getWitnessStandby() {
List<WitnessCapsule> list =
witnessStandbyCache.getIfPresent(Parameter.ChainConstant.WITNESS_STANDBY_LENGTH);
if (list != null) {
return list;
}
return updateWitnessStandby(null);
}

public List<WitnessCapsule> updateWitnessStandby(List<WitnessCapsule> all) {
List<WitnessCapsule> ret;
if (all == null) {
all = getAllWitnesses();
}
List<WitnessCapsule> all = getAllWitnesses();
all.sort(Comparator.comparingLong(WitnessCapsule::getVoteCount)
.reversed().thenComparing(Comparator.comparingInt(
(WitnessCapsule w) -> w.getAddress().hashCode()).reversed()));
Expand All @@ -71,7 +52,6 @@ public List<WitnessCapsule> updateWitnessStandby(List<WitnessCapsule> all) {
}
// trim voteCount = 0
ret.removeIf(w -> w.getVoteCount() < 1);
witnessStandbyCache.put(Parameter.ChainConstant.WITNESS_STANDBY_LENGTH, ret);
return ret;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -108,10 +108,6 @@ public List<WitnessCapsule> getAllWitnesses() {
return witnessStore.getAllWitnesses();
}

public List<WitnessCapsule> updateWitnessStandby(List<WitnessCapsule> all) {
return witnessStore.updateWitnessStandby(all);
}

public void saveStateFlag(int flag) {
dynamicPropertiesStore.saveStateFlag(flag);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -151,13 +151,11 @@ public void doMaintenance() {
if (dynamicPropertiesStore.allowChangeDelegation()) {
long nextCycle = dynamicPropertiesStore.getCurrentCycleNumber() + 1;
dynamicPropertiesStore.saveCurrentCycleNumber(nextCycle);
List<WitnessCapsule> all = consensusDelegate.getAllWitnesses();
all.forEach(witness -> {
consensusDelegate.getAllWitnesses().forEach(witness -> {
delegationStore.setBrokerage(nextCycle, witness.createDbKey(),
delegationStore.getBrokerage(witness.createDbKey()));
delegationStore.setWitnessVote(nextCycle, witness.createDbKey(), witness.getVoteCount());
});
consensusDelegate.updateWitnessStandby(all);
}
}

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
package org.tron.common.cache;

import org.junit.Assert;
import org.junit.Test;

public class CacheManagerTest {

@Test
public void allocate() {
String strategy = String.format(CacheStrategies.PATTERNS, 1, 1, "30s", 1);
TronCache<String, String> cache = CacheManager.allocate(CacheType.witnessStandby, strategy);
Assert.assertNull(cache.getIfPresent("test"));
}
}
17 changes: 15 additions & 2 deletions framework/src/test/java/org/tron/core/db/ManagerTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -581,14 +581,24 @@ public void pushSwitchFork()
AccountResourceInsufficientException, EventBloomException {

String key = "f31db24bfbd1a2ef19beddca0a0fa37632eded9ac666a05d3bd925f01dde1f62";
String key2 = "c85ef7d79691fe79573b1a7064c19c1a9819ebdbd1faaab1a8ec92344438aaf4";
byte[] privateKey = ByteArray.fromHexString(key);
final ECKey ecKey = ECKey.fromPrivate(privateKey);
byte[] address = ecKey.getAddress();

WitnessCapsule sr1 = new WitnessCapsule(
ByteString.copyFrom(address), "www.tron.net/first");
sr1.setVoteCount(1000000000L);
byte[] privateKey2 = ByteArray.fromHexString(key2);
final ECKey ecKey2 = ECKey.fromPrivate(privateKey2);
byte[] address2 = ecKey2.getAddress();
WitnessCapsule sr2 = new WitnessCapsule(
ByteString.copyFrom(address2), "www.tron.net/second");
sr2.setVoteCount(100000L);
chainManager.getWitnessStore().put(address, sr1);
WitnessCapsule witnessCapsule = new WitnessCapsule(ByteString.copyFrom(address));
chainManager.getWitnessScheduleStore().saveActiveWitnesses(new ArrayList<>());
chainManager.addWitness(ByteString.copyFrom(address));

List<WitnessCapsule> witnessStandby1 = chainManager.getWitnessStore().getWitnessStandby();
Block block = getSignedBlock(witnessCapsule.getAddress(), 1533529947843L, privateKey);
dbManager.pushBlock(new BlockCapsule(block));

Expand Down Expand Up @@ -625,6 +635,9 @@ public void pushSwitchFork()
} catch (Exception e) {
Assert.assertTrue(e instanceof Exception);
}
chainManager.getWitnessStore().put(address, sr2);
List<WitnessCapsule> witnessStandby2 = chainManager.getWitnessStore().getWitnessStandby();
Assert.assertNotEquals(witnessStandby1, witnessStandby2);
}


Expand Down

0 comments on commit e14f3dd

Please sign in to comment.