Skip to content

Commit

Permalink
Merge #3391
Browse files Browse the repository at this point in the history
3391: improve sync header map r=driftluo a=zhangsoledad

<!--
Thank you for contributing to nervosnetwork/ckb!

If you haven't already, please read [CONTRIBUTING](https://github.com/nervosnetwork/ckb/blob/develop/CONTRIBUTING.md) document.

If you're unsure about anything, just ask; somebody should be along to answer within a day or two.

PR Title Format:
1. module [, module2, module3]: what's changed
2. *: what's changed
-->

### What problem does this PR solve?

* Optimize the implementation of header map to improve the speed of header synchronization from 11924/s to 14377/s, about 20% improvement.
* Human-friendly configuration

### What is changed and how it works?
- Asynchronous batch write to hard disk when memory limit is exceeded.
- Replace the implementation of header map bankend. Rocksdb has some drawbacks in this scenario, Rocksdb cannot give feedback on whether the key is overwritten when writing, nor can it distinguish between successful or not found when deleting, it's inefficient and looks silly to retrieve a value just to check if it exists or not. 
```
RocksDBBackend::contains_key duration 4041 ns
RocksDBBackend::remove duration 7000 ns
RocksDBBackend::insert duration 10750 ns

Sometimes it deteriorates into the following, because of the inconvenience of implementation:
RocksDBBackend::contains_key duration 10750 ns
RocksDBBackend::remove duration 13000 ns
RocksDBBackend::insert duration 21583 ns

-----------------------------
SledBackend::contains_key duration 2666 ns
SledBackend::remove duration 2583 ns
SledBackend::insert_batch 35503541/10028 ns (3540 ns)
```


### Check List <!--REMOVE the items that are not applicable-->

Tests <!-- At least one of them must be included. -->

- Unit test
- Integration test
- Manual test (add detailed scripts or steps below)
- No code ci-runs-only: [ quick_checks,linters ]


### Release note <!-- Choose from None, Title Only and Note. Bugfixes or new features need a release note. -->

```release-note
Title Only: Include only the PR title in the release note.
```



Co-authored-by: zhangsoledad <[email protected]>
  • Loading branch information
bors[bot] and zhangsoledad authored Jun 24, 2022
2 parents d28d334 + d5ada78 commit bb7f720
Show file tree
Hide file tree
Showing 12 changed files with 357 additions and 367 deletions.
39 changes: 38 additions & 1 deletion Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 3 additions & 0 deletions resource/ckb.toml
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,9 @@ discovery_local_address = false # {{
# Ensure that itself can continue to serve as a bootnode node
bootnode_mode = false

# [network.sync.header_map]
# memory_limit = "600MB"

[rpc]
# By default RPC only binds to localhost, thus it only allows accessing from the same machine.
#
Expand Down
7 changes: 4 additions & 3 deletions sync/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@ repository = "https://github.com/nervosnetwork/ckb"
ckb-chain = { path = "../chain", version = "= 0.104.0-pre" }
ckb-shared = { path = "../shared", version = "= 0.104.0-pre" }
ckb-store = { path = "../store", version = "= 0.104.0-pre" }
ckb-db = { path = "../db", version = "= 0.104.0-pre" }
ckb-app-config = {path = "../util/app-config", version = "= 0.104.0-pre"}
ckb-types = {path = "../util/types", version = "= 0.104.0-pre"}
ckb-network = { path = "../network", version = "= 0.104.0-pre" }
Expand All @@ -28,6 +27,9 @@ ckb-error = {path = "../error", version = "= 0.104.0-pre"}
ckb-tx-pool = { path = "../tx-pool", version = "= 0.104.0-pre" }
sentry = { version = "0.23.0", optional = true }
ckb-constant = { path = "../util/constant", version = "= 0.104.0-pre" }
ckb-async-runtime = { path = "../util/runtime", version = "= 0.104.0-pre" }
ckb-stop-handler = { path = "../util/stop-handler", version = "= 0.104.0-pre" }
tokio = { version = "1", features = ["sync"] }
lru = "0.7.1"
futures = "0.3"
governor = "0.3.1"
Expand All @@ -36,6 +38,7 @@ faketime = "0.2.0"
bitflags = "1.0"
dashmap = "4.0"
keyed_priority_queue = "0.3"
sled = "0.34.7"

[dev-dependencies]
ckb-test-chain-utils = { path = "../util/test-chain-utils", version = "= 0.104.0-pre" }
Expand All @@ -46,8 +49,6 @@ ckb-reward-calculator = { path = "../util/reward-calculator", version = "= 0.104
ckb-chain = { path = "../chain", version = "= 0.104.0-pre", features = ["mock"] }
ckb-launcher = { path = "../util/launcher", version = "= 0.104.0-pre" }
faux = "^0.1"
tokio = "1"
tempfile = "3.0"
once_cell = "1.8.0"

[features]
Expand Down
10 changes: 4 additions & 6 deletions sync/src/types/header_map/backend.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,12 +14,10 @@ pub(crate) trait KeyValueBackend {
self.len() == 0
}

fn is_opened(&self) -> bool;
fn open(&mut self);
fn try_close(&mut self) -> bool;

fn contains_key(&self, key: &Byte32) -> bool;
fn get(&self, key: &Byte32) -> Option<HeaderView>;
fn insert(&mut self, value: &HeaderView) -> Option<HeaderView>;
fn remove(&mut self, key: &Byte32) -> Option<HeaderView>;
fn insert(&self, value: &HeaderView) -> Option<()>;
fn insert_batch(&self, values: &[HeaderView]);
fn remove(&self, key: &Byte32) -> Option<HeaderView>;
fn remove_no_return(&self, key: &Byte32);
}
168 changes: 0 additions & 168 deletions sync/src/types/header_map/backend_rocksdb.rs

This file was deleted.

Loading

0 comments on commit bb7f720

Please sign in to comment.