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

Parametrize the transaction staging strategy & make it volatile by default #1131

Merged
merged 8 commits into from
Dec 22, 2020

Conversation

dahlia
Copy link
Contributor

@dahlia dahlia commented Dec 15, 2020

This patch parametrizes BlockChain<T>'s strategy to stage transactions through the new IStagePolicy<T> interface, and introduced VolatileStagePolicy<T>, which is an only built-in & default implementation.

See also the changelog.

@dahlia dahlia self-assigned this Dec 15, 2020
@dahlia dahlia force-pushed the stage-txs-on-memory branch from 324ac02 to 9d87645 Compare December 17, 2020 05:13
@dahlia dahlia changed the title WIP: Make staged transactions volatile and store them only on memory Make staged transactions volatile and store them only on memory Dec 17, 2020
@dahlia dahlia changed the title Make staged transactions volatile and store them only on memory Parametrize the transaction staging strategy & make it volatile by default Dec 17, 2020
@dahlia dahlia marked this pull request as ready for review December 17, 2020 05:18
@dahlia dahlia requested a review from longfin December 17, 2020 14:06
@codecov
Copy link

codecov bot commented Dec 17, 2020

Codecov Report

Merging #1131 (97263d7) into main (eb37cdf) will increase coverage by 0.08%.
The diff coverage is 97.50%.

@@            Coverage Diff             @@
##             main    #1131      +/-   ##
==========================================
+ Coverage   87.46%   87.55%   +0.08%     
==========================================
  Files         341      345       +4     
  Lines       29694    29956     +262     
==========================================
+ Hits        25972    26227     +255     
- Misses       2092     2097       +5     
- Partials     1630     1632       +2     
Impacted Files Coverage Δ
Libplanet/Net/Swarm.cs 82.03% <50.00%> (+0.41%) ⬆️
Libplanet/Net/Swarm.MessageHandlers.cs 84.57% <71.42%> (-0.86%) ⬇️
...ibplanet.Tests/Blockchain/BlockChainTest.Append.cs 96.78% <96.78%> (ø)
Libplanet/Blockchain/BlockChain.cs 90.81% <97.87%> (-0.22%) ⬇️
Libplanet.Tests/Blockchain/BlockChainTest.cs 98.62% <97.91%> (+0.32%) ⬆️
Libplanet.RocksDBStore.Tests/RocksDBStoreTest.cs 88.23% <100.00%> (+0.35%) ⬆️
...sts/Blockchain/BlockChainTest.ValidateNextBlock.cs 94.91% <100.00%> (+0.04%) ⬆️
...lanet.Tests/Blockchain/Policies/BlockPolicyTest.cs 79.25% <100.00%> (+0.31%) ⬆️
...lanet.Tests/Blockchain/Policies/StagePolicyTest.cs 100.00% <100.00%> (ø)
...sts/Blockchain/Policies/VolatileStagePolicyTest.cs 100.00% <100.00%> (ø)
... and 16 more

@dahlia
Copy link
Contributor Author

dahlia commented Dec 18, 2020

@planetarium/libplanet PTAL.

longfin
longfin previously approved these changes Dec 18, 2020
{
_rwlock.ExitWriteLock();
StagePolicy.Stage(this, transaction);
Copy link
Member

Choose a reason for hiding this comment

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

Is StagePolicy.Stage() thread-safe?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

It should be so (interface-wise), and I believe VolatileStagePolicy<T>, the only implementation, is so. (It uses ConcurrentDictionary<K, V> under the hood.)

Copy link
Member

Choose a reason for hiding this comment

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

OK, if .Unstage() is too, it seems that we can strip _rwlock in BlockChain<T>.UnstageTransaction().

.ToImmutableHashSet();
foreach (TxId txId in txIds)
{
StagePolicy.Unstage(this, txId);
Copy link
Member

Choose a reason for hiding this comment

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

Is StagePolicy.Unstage() also thread-safe?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I made every operation on IStagePolicy<T> (protocol-wise) & VolatileStagePolicy<T> (implementation-wise) sure they are thread safe: 131ed7d.

{
Transaction<T> tx = BlockChain.GetTransaction(txid);

if (tx is null)
Copy link
Member

Choose a reason for hiding this comment

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

Can BlockChain<T>.GetTransaction() return null?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

If txid is invalid there's the possibility.

Copy link
Member

Choose a reason for hiding this comment

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

🆗

dahlia added a commit to dahlia/libplanet that referenced this pull request Dec 18, 2020
@dahlia dahlia requested a review from longfin December 18, 2020 11:51
@dahlia
Copy link
Contributor Author

dahlia commented Dec 18, 2020

@planetarium/libplanet PTAL.

/// </summary>
public VolatileStagePolicy()
{
_set = new ConcurrentDictionary<TxId, Transaction<T>>();
Copy link
Contributor

Choose a reason for hiding this comment

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

It seems there is no code to delete _set's element. Is there a default capacity?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

No, we need to implement lifetiming, but I'm going to do it on a different PR.

longfin
longfin previously approved these changes Dec 21, 2020
Copy link
Member

@longfin longfin left a comment

Choose a reason for hiding this comment

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

There are comments about the style of the code left, but there seems to be no big problem with the behavior right now.

moreal
moreal previously approved these changes Dec 21, 2020
Copy link
Contributor

@moreal moreal left a comment

Choose a reason for hiding this comment

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

LGTM.

riemannulus
riemannulus previously approved these changes Dec 22, 2020
@dahlia dahlia dismissed stale reviews from riemannulus, moreal, and longfin via 97263d7 December 22, 2020 04:49
@dahlia dahlia force-pushed the stage-txs-on-memory branch from 131ed7d to 97263d7 Compare December 22, 2020 04:49
@dahlia
Copy link
Contributor Author

dahlia commented Dec 22, 2020

@planetarium/libplanet I rebased on the current main. Please review this again!

@longfin longfin self-requested a review December 22, 2020 04:55
@dahlia dahlia merged commit 64b0d03 into planetarium:main Dec 22, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

5 participants