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

chore(deps): timed-map migration #2247

Open
wants to merge 3 commits into
base: dev
Choose a base branch
from
Open

chore(deps): timed-map migration #2247

wants to merge 3 commits into from

Conversation

shamardy
Copy link
Collaborator

@shamardy shamardy commented Oct 21, 2024

Complete timed-map migration from TimeCache, DuplicateCache, ExpirableMap and ExpirableEntry.

@shamardy shamardy changed the title chore(deps): use timed-map crate instead of internal ExpirableMap type chore(deps): use timed-map crate instead of internal ExpirableMap Oct 21, 2024
mm2src/coins/Cargo.toml Outdated Show resolved Hide resolved
@onur-ozkan onur-ozkan force-pushed the use-timed-map-crate branch from e2b814e to ff4038d Compare January 9, 2025 14:37
@onur-ozkan onur-ozkan marked this pull request as ready for review January 9, 2025 14:41
@onur-ozkan onur-ozkan changed the title chore(deps): use timed-map crate instead of internal ExpirableMap chore(deps): timed-map migration Jan 9, 2025
@@ -177,7 +177,7 @@ impl ElectrumConnection {
settings,
tx: Mutex::new(None),
establishing_connection: AsyncMutex::new(()),
responses: Mutex::new(JsonRpcPendingRequests::new()),
responses: Mutex::new(JsonRpcPendingRequests::new_with_map_kind(MapKind::BTreeMap)),
Copy link
Member

Choose a reason for hiding this comment

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

Picked BTreeMap intentionally to process entries in order.

Copy link
Collaborator

Choose a reason for hiding this comment

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

wdym by processing entries in order? what processing?

Copy link
Member

Choose a reason for hiding this comment

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

wdym by processing entries in order? what processing?

Making sure to process responses in the FIFO way.

Copy link
Collaborator

Choose a reason for hiding this comment

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

yeah where is that 😂

Copy link
Member

Choose a reason for hiding this comment

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

Fell into this terrible naming (there is no separation at all at the first glance)

for response in responses {
// `split` returns empty slice if it ends with separator which is our case.
if !response.is_empty() {
self.process_electrum_response(response, event_handlers)
}

thought this was the loop iterating over the responses map..

Copy link
Member

Choose a reason for hiding this comment

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

Honestly I still believe we should prefer BTreeMap here to avoid difficult-to-debug magical future bugs. If we add any logic with the expectation of running FIFO processing, it will accidentally run in a random order and that will be super annoying to catch on runtime. The fact that I already falled in this trap makes it very likely to happen again to me or anyone in the team.

Copy link
Collaborator

Choose a reason for hiding this comment

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

aha cool, im fine with BTree or others, i only nitted to make sure im not missing something with the in-order comment.

Copy link
Collaborator

@mariocynicys mariocynicys left a comment

Choose a reason for hiding this comment

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

Thanks!
One comment inline. LGTM otherwise.

mm2src/mm2_main/src/lp_swap/swap_watcher.rs Show resolved Hide resolved
@shamardy
Copy link
Collaborator Author

lp_ordermatch::ordermatch_tests::test_orderbook_order_pairs_trie_state_history_updates_expiration_on_insert and lp_ordermatch::ordermatch_tests::test_orderbook_sync_trie_diff_time_cache are both failing after the recent changes

@@ -251,7 +251,7 @@ impl ElectrumConnection {
self.responses
.lock()
.unwrap()
.insert(rpc_id, req_tx, Duration::from_secs_f64(timeout));
.insert_expirable_unchecked(rpc_id, req_tx, Duration::from_secs_f64(timeout));
Copy link
Collaborator

@mariocynicys mariocynicys Jan 10, 2025

Choose a reason for hiding this comment

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

the only insert being unchecked basically makes this map susceptible to memory leaks. this acts pretty much similar (with an *) to a normal hashmap at this point.

you can instead make it a checked insert but use a high tick cap.

there are other never-checked maps also introduced in this PR.

Copy link
Member

Choose a reason for hiding this comment

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

the only insert being unchecked basically makes this map susceptible to memory leaks. this acts pretty much similar (with an *) to a normal hashmap at this point.

you can instead make it a checked insert but use a high tick cap.

We don't need to include expiration overhead on inserts on electrum requests. The check is performed at end of process_electrum_response which should be enough.

there are other never-checked maps also introduced in this PR.

Can you mark them? I tried to minimize the expiration overhead in various places where it's unnecessary.

Copy link
Collaborator

Choose a reason for hiding this comment

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

We don't need to include expiration overhead on inserts on electrum requests. The check is performed at end of process_electrum_response which should be enough.

i can't find no check for outdated entries in process_electrum_response!
you aren't talking about the .remove right? .remove will not help with requests that we got no responses for, these request handles will reside in the timed map forever (until the connection terminates*).

Copy link
Collaborator

@mariocynicys mariocynicys left a comment

Choose a reason for hiding this comment

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

labeled the never checked maps below

) -> OuterAction {
match request {
Some(ControllerMessage::Request(WsRequest {
request_id,
serialized_request,
response_notifier,
})) => {
response_notifiers.insert(
response_notifiers.insert_expirable_unchecked(
Copy link
Collaborator

Choose a reason for hiding this comment

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

this

@@ -251,7 +251,7 @@ impl ElectrumConnection {
self.responses
.lock()
.unwrap()
.insert(rpc_id, req_tx, Duration::from_secs_f64(timeout));
.insert_expirable_unchecked(rpc_id, req_tx, Duration::from_secs_f64(timeout));
Copy link
Collaborator

Choose a reason for hiding this comment

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

this

let history = match pubkey_state.order_pairs_trie_state_history.get_mut(&alb_ordered) {
Some(t) => t,
None => {
pubkey_state.order_pairs_trie_state_history.insert_expirable_unchecked(
Copy link
Collaborator

Choose a reason for hiding this comment

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

and this

@onur-ozkan
Copy link
Member

onur-ozkan commented Jan 13, 2025

lp_ordermatch::ordermatch_tests::test_orderbook_order_pairs_trie_state_history_updates_expiration_on_insert and lp_ordermatch::ordermatch_tests::test_orderbook_sync_trie_diff_time_cache are both failing after the recent changes

The order trie logic highly depends on timecache internals, which is covered in the test_orderbook_order_pairs_trie_state_history_updates_expiration_on_insert test.

We have 3 options:

  1. Change the order trie expiration logic (timecache has quite different expiration logic (e.g., bulk mode expirations) and removes expired entries on remove and entry functions, not on inserts. Also, reading functions do not consider expirations.)
  2. Change the expectations on test_orderbook_order_pairs_trie_state_history_updates_expiration_on_insert flow.
  3. Stay with timecache for order trie (this requires to keep time_cache module with ExpirableEntry in the expirable_map module)

I am ok with any of them.

Signed-off-by: onur-ozkan <[email protected]>
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.

3 participants