Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(db): optimize old rewards withdrawal #5406

Merged

Conversation

halibobo1205
Copy link
Contributor

close #5354

@halibobo1205 halibobo1205 added this to the GreatVoyage-v4.7.3 milestone Aug 9, 2023
@halibobo1205
Copy link
Contributor Author

@wangzichichi PTAL, https://buildkite.com/tronprotocol/build-on-centos-linux-release-7-dot-4-1708-core/builds/5978

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Are these removals part of the optimization reward calculation? If not, better fix it in another PR, just suggestion

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

There's a class conflict.

Metrics.histogramObserve(requestTimer);
}

private Protocol.Account getAccount(byte[] address) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is it possible to get the account using accountStore.getFromRoot() directly?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

DB Cache pollution due to bulk queries, like accountStore.getFromRoot()

default UnmodifiableIterator<Entry<byte[], byte[]>> prefixQueryAfterThat
(byte[] key, byte[] afterThat) {
this.seek(afterThat == null ? key : afterThat);
return Iterators.filter(this, entry -> Bytes.indexOf(entry.getKey(), key) == 0);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Here I have doubt, whether this iterator will still access all data in the database but just return the data that matched the filter.
If so, it will do a lot of useless read-op?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not All, iterates over all data after the prefix. I can't think of a better way for now. It takes 2 minutes to iterate the database.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Use the default iterator and move the filter logic into startRewardCal, or re-implement a prefix iterator.
Seems 2 minutes is not a big deal, depends on you

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Prefix does not work is fixed.

@halibobo1205
Copy link
Contributor Author

   System.out.println((long) 128.9999999999999857891452847979962825775146484375); // will print 129
   System.out.println((long) 128.9999999999999857891452847979962825775146484374999999999999999999); // will print 128


private long getOldReward(long beginCycle, long oldEndCycle, AccountCapsule accountCapsule) {
long cacheData = rewardCalService.getReward(accountCapsule.createDbKey(), beginCycle);
long reward = 0;

This comment was marked as resolved.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Phase I only calculates without using, cache enabled for the next release.

return null;
}

long computeReward(long cycle, Protocol.Account account) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why rewrite this method instead of calling the original method?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Avoid circular dependencies.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

done

@codecov-commenter
Copy link

codecov-commenter commented Aug 30, 2023

Codecov Report

Merging #5406 (00e349b) into develop (f9c42d6) will increase coverage by 0.08%.
Report is 1 commits behind head on develop.
The diff coverage is 75.00%.

❗ Your organization is not using the GitHub App Integration. As a result you may experience degraded service beginning May 15th. Please install the GitHub App Integration for your organization. Read more.

@@              Coverage Diff              @@
##             develop    #5406      +/-   ##
=============================================
+ Coverage      61.23%   61.32%   +0.08%     
- Complexity      9323     9374      +51     
=============================================
  Files            842      841       -1     
  Lines          50147    50275     +128     
  Branches        5580     5591      +11     
=============================================
+ Hits           30710    30831     +121     
+ Misses         17029    17028       -1     
- Partials        2408     2416       +8     
Files Changed Coverage Δ
...in/java/org/tron/common/prometheus/MetricKeys.java 0.00% <ø> (ø)
...a/org/tron/core/db/common/iterator/DBIterator.java 40.00% <40.00%> (ø)
...in/java/org/tron/core/service/MortgageService.java 63.94% <60.00%> (-2.94%) ⬇️
...n/java/org/tron/core/service/RewardCalService.java 68.13% <68.13%> (ø)
...ron/core/db/common/iterator/RockStoreIterator.java 78.94% <80.00%> (+5.41%) ⬆️
...ain/java/org/tron/core/store/RewardCacheStore.java 85.71% <85.71%> (ø)
...rg/tron/core/db/common/iterator/StoreIterator.java 91.66% <96.55%> (+3.66%) ⬆️
...a/org/tron/common/prometheus/MetricsHistogram.java 83.33% <100.00%> (+0.35%) ⬆️

... and 8 files with indirect coverage changes

📣 We’re building smart automated test selection to slash your CI/CD build times. Learn more

@@ -13,23 +14,22 @@ public final class StoreIterator implements org.tron.core.db.common.iterator.DBI
private final DBIterator dbIterator;
private boolean first = true;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is there a simpler way by just setting first false when invoking seek() & seekToLast() to solve the problem of pointer confusion?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, that's exactly how it's handled.

@@ -2523,6 +2525,10 @@ public boolean useNewRewardAlgorithm() {
return getNewRewardAlgorithmEffectiveCycle() != Long.MAX_VALUE;
}

public boolean useNewRewardAlgorithmFromStart() {
return getNewRewardAlgorithmEffectiveCycle() == 1;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can this value possibly equal 1, even on a new chain?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, It can be turned on anytime.

try {
lock.await();
} catch (InterruptedException e) {
logger.error("rewardViCalService lock error: {}", e.getMessage());
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If an InterruptedException occurs, the program will continue to execute, but with an additional error log. Is this the expected behavior?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fixed!

this.clearUp(true);
logger.info("rewardViCalService is already done");
} else {
if (this.getLatestBlockHeaderNumber() > lastBlockNumber) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is getLatestSolidifiedBlockNum & lastSolidifiedBlockNumber better?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This service is depend on root dbs.

Copy link
Contributor

@yanghang8612 yanghang8612 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM

@halibobo1205 halibobo1205 merged commit 52b3327 into tronprotocol:develop Jan 5, 2024
7 checks passed
@halibobo1205
Copy link
Contributor Author

Review task on 2.28:

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Improve the performance of vote reward withdrawal
6 participants