Skip to content

Conversation

@rasom
Copy link
Contributor

@rasom rasom commented Nov 14, 2018

Summary:

Signing in is slow for account with tons of messages. This is caused by many reasons (see this note), this PR fixes some of them:

  • Remove necessity in the usage of deduplication-ids set (part of A). In order to tackle deduplication problem proper message-id (which will be calculated the same way regardless by which version of the app it has been received) can be calculated (this function was suggested) and updated in all existing messages via db migration. This is done in [slow sign in] fix slow sign in (first iteration) #6722
  • Remove necessity in the loading of all message-ids (another part of A). We can check if the message has been already received by making a direct query to db. While query which is done using .objects() with message-id=... condition is quite slow, objectForPrimaryKey works acceptably faster (~50ms fro 1000 queries). Implemented in [slow sign in] fix slow sign in (first iteration) #6722. Other suggestions on this are welcome.
  • Load part of data necessary for :home screen asynchronously. In this case we can go to 7 without doing 8. That's the only way to guaranty that sign in will have some likely constant regardless of db size. Done in [slow sign in] fix slow sign in (first iteration) #6722 (probably touches E as no need to calculate it before rendering screen)

Review notes (optional):

  • message-id is calculated as sha3(from + chat-id + clock-value) for each message
  • objectForPrimaryKey is used to check if message-id already exists in DB
  • :init-chats event is dispatched 100ms after :home's componentDidMount on mobile. The reason for this is that otherwise on Android event will handled before :home is actually rendered and it will be shown only after :init-chats.
  • migration resets messages-id in message (:messages-id, :reference-id in :content), messages-status (:status-id, :messages-id) entities, let me now if any other reference was missed
  • during transition period we will lost all refernces to messages which are done on older clients (as there is no way to calculate new message-id from the old one directly, without the message)

Testing notes (optional):

Platforms (optional)

  • All platforms

Areas that maybe impacted (optional)

Functional

  • signing in
  • 1-1 chats
  • public chats
  • group chats

Steps to test:

  • Open Status
  • create a n account
  • join several pub chats
  • send ~10000 messages in these pubchats (also it is slower if all these messages are unread)
  • logout
  • sign in back and check how fast it happens

status: ready

@ghost
Copy link

ghost commented Nov 14, 2018

Pull Request Checklist

  • Have you updated the documentation, if impacted (e.g. docs.status.im)?

@chadyj
Copy link
Contributor

chadyj commented Nov 16, 2018

What clients are slow to sign in?

My iOS is almost instant, but Mac desktop takes a long time.

@rasom
Copy link
Contributor Author

rasom commented Nov 16, 2018

@chadyj well, any of them can be slow right now because duration of sign in depends on iteration over ALL messages in db. So assuming you have enough messages it might be 20-30 seconds or even longer. At least @jarradh and @pombeirp are experiencing this problem, and it can be reproduced.

@pombeirp:

I'm seeing this long wait time both on my laptop (ThinkPad X1C6) and mobile phone (Huawei P20 Pro), so not slow devices. It only happens if I unlock an account that has tons of messages. If I unlock an account which has lots of channels open but no messages, it opens instantly (on same device and app instance), so it really feels like it is something tied with the handling of messages (maybe sorting?)

so the reason for the recent preformance regression is this code https://github.com/status-im/status-react/blob/26ec3f8cd7906a0a80cd75a937e8c8028a358fa9/src/status_im/data_store/messages.cljs#L65 , but also we iterate all messages here https://github.com/status-im/status-react/blob/26ec3f8cd7906a0a80cd75a937e8c8028a358fa9/src/status_im/data_store/messages.cljs#L43 and this happens during sign in as well

this PR already contains the fix for the first iteration mentioned, but i'm still working on the second.

Besides this problem, even on empty account sign in takes 3.2-3.5s on galaxy s9 (so it might be slower on many other android devices) which is not perfect, as i can say. There are ways to improve this duration as well, but i'll probably focus on this in another PR.

@rasom rasom self-assigned this Nov 16, 2018
@pedropombeiro
Copy link
Contributor

I downloaded the build to try it, but after 7 seconds I got the warning that it couldn't decrypt the db so I'll wait until it is rebased on latest develop.

@rasom
Copy link
Contributor Author

rasom commented Nov 16, 2018

thanks for trying, @pombeirp!
The first run will be slow because of migrations.
I will rebase it soon, but be careful: until we merge it to develop it might introduce some breaking changes so that you will not be able to upgrade to the next nightly build. If you want to test it make sure you have a backup of the database.

@rasom rasom force-pushed the fix/slow-sign-in branch 2 times, most recently from 82b7385 to 084ab9d Compare November 17, 2018 12:56
@status-comment-bot
Copy link

#19 CI BUILD SUCCESSFUL in 22 min and counting (a8ddff4)

Android(e2e) iOS MacOS AppImage Windows

@rasom rasom changed the title [WIP] fix slow sign in fix slow sign in (first iteration) Nov 19, 2018
@status-im status-im deleted a comment from status-comment-bot Nov 19, 2018
@status-im status-im deleted a comment from status-comment-bot Nov 19, 2018
@status-im status-im deleted a comment from status-comment-bot Nov 19, 2018
@status-im status-im deleted a comment from status-comment-bot Nov 19, 2018
@status-im status-im deleted a comment from status-comment-bot Nov 19, 2018
@status-im status-im deleted a comment from status-comment-bot Nov 19, 2018
@status-im status-im deleted a comment from status-comment-bot Nov 19, 2018
@status-im status-im deleted a comment from status-comment-bot Nov 19, 2018
@status-im status-im deleted a comment from status-comment-bot Nov 19, 2018
@status-im status-im deleted a comment from status-comment-bot Nov 19, 2018
@status-im status-im deleted a comment from status-comment-bot Nov 19, 2018
@status-im status-im deleted a comment from status-comment-bot Nov 19, 2018
@status-im status-im deleted a comment from status-comment-bot Nov 19, 2018
@lukaszfryc
Copy link
Contributor

@lukaszfryc see #test-team chat

OK, so feel free to merge once review is done.

@yenda
Copy link
Contributor

yenda commented Nov 21, 2018

the place where collision might happen is send-membership-update and this message is not stored in realm db. So shouldn't be a problem.

I don't understand on the recipient end this message isn't this message stored? If that's the case is there a clock-value there to differentiate messages from the same sender and avoid overwritting the previous ones?

@yenda
Copy link
Contributor

yenda commented Nov 21, 2018

Also I agree about the rest it seems like a reasonable change and a broader fix for message-ids can be done later. If the group chat thing is a non-issue I'll remove by request for changes

@rasom
Copy link
Contributor Author

rasom commented Nov 21, 2018

if that's the case is there a clock-value there to differentiate messages from the same sender and avoid overwritting the previous ones?

the way to test it is to create a group chat, make sure that update was sent and check if there are messages with clock-value = 0 in db. Will check this later today.

@cammellos
Copy link
Contributor

cammellos commented Nov 21, 2018 via email

@rasom
Copy link
Contributor Author

rasom commented Nov 21, 2018

but why can't we just leave the code as it was?

do you mean the way how that id was generated for membership update or which part of code?

@rasom rasom force-pushed the fix/slow-sign-in branch 2 times, most recently from 05cedd3 to 9fcce33 Compare November 21, 2018 11:27
@rasom
Copy link
Contributor Author

rasom commented Nov 21, 2018

@lukaszfryc did you test migration from the older version of app?

@statustestbot
Copy link

97% of end-end tests have passed

Total executed tests: 58
Failed tests: 2
Passed tests: 56

Failed tests (2)

Click to expand
1. test_manage_assets

Device 1: Scrolling down to AssetCheckBox
Device 1: Scrolling down to AssetCheckBox

'NoneType' object has no attribute 'click'

Device sessions

2. test_network_mismatch_for_send_request_commands

Device 1: Tap on GotItButton
Device 1: Looking for a message by text: '0.01121111641'

Device 1: 'ChatElementByText' is not found on the screen

Device sessions

Passed tests (56)

Click to expand
1. test_create_account
Device sessions

2. test_user_can_switch_network
Device sessions

3. test_filters_from_daap
Device sessions

4. test_copy_and_paste_messages
Device sessions

5. test_send_transaction_from_daap
Device sessions

6. test_request_and_receive_tokens_in_1_1_chat
Device sessions

7. test_delete_cut_and_paste_messages
Device sessions

8. test_deploy_contract_from_daap
Device sessions

9. test_offline_login
Device sessions

10. test_pass_phrase_validation
Device sessions

11. test_public_chat_messaging
Device sessions

12. test_password_in_logcat_sign_in
Device sessions

13. test_set_profile_picture
Device sessions

14. test_text_message_1_1_chat
Device sessions

15. test_add_to_contacts
Device sessions

16. test_unread_messages_counter_1_1_chat
Device sessions

17. test_logcat_send_transaction_from_daap
Device sessions

18. test_onboarding_screen_when_requesting_tokens_for_new_account
Device sessions

19. test_logcat_send_transaction_from_wallet
Device sessions

20. test_send_token_with_7_decimals
Device sessions

21. test_modify_transaction_fee_values
Device sessions

22. test_token_with_more_than_allowed_decimals
Device sessions

23. test_send_eth_from_wallet_to_address
Device sessions

24. test_send_transaction_details_in_1_1_chat
Device sessions

25. test_wallet_set_up
Device sessions

26. test_logcat_send_transaction_in_1_1_chat
Device sessions

27. test_request_and_receive_eth_in_1_1_chat
Device sessions

28. test_swipe_to_delete_public_chat
Device sessions

29. test_passphrase_whitespaces_ignored_while_recovering_access
Device sessions

30. test_send_emoji
Device sessions

31. test_add_contact_by_pasting_public_key
Device sessions

32. test_logcat_recovering_account
Device sessions

33. test_messaging_in_different_networks
Device sessions

34. test_send_tokens_in_1_1_chat
Device sessions

35. test_logcat_sign_message_from_daap
Device sessions

36. test_swipe_to_delete_1_1_chat
Device sessions

37. test_switch_users_and_add_new_account
Device sessions

38. test_send_stt_from_wallet
Device sessions

39. test_send_eth_in_1_1_chat
Device sessions

40. test_login_with_new_account
Device sessions

41. test_send_eth_from_wallet_to_contact
Device sessions

42. test_add_contact_from_public_chat
Device sessions

43. test_send_request_not_enabled_tokens
Device sessions

44. test_send_message_to_newly_added_contact
Device sessions

45. test_password_in_logcat_creating_account
Device sessions

46. test_backup_recovery_phrase
Device sessions

47. test_offline_status
Device sessions

48. test_open_google_com_via_open_dapp
Device sessions

49. test_unread_messages_counter_public_chat
Device sessions

50. test_sign_message_from_daap
Device sessions

51. test_user_can_remove_profile_picture
Device sessions

52. test_share_contact_code_and_wallet_address
Device sessions

53. test_request_eth_in_wallet
Device sessions

54. test_refresh_button_browsing_app_webview
Device sessions

55. test_backup_recovery_phrase_warning_from_wallet
Device sessions

56. test_recover_account
Device sessions

@antdanchenko
Copy link
Contributor

there are 2 tests failed in latest test run and both of them are known issues:
#6825
#5975

@status-comment-bot
Copy link

#34 CI BUILD SUCCESSFUL in 38 min and counting (9fcce33)

Android(e2e) iOS MacOS AppImage Windows

@lukaszfryc
Copy link
Contributor

@rasom yes, and I just re-tested it on Android. Works fine.

@rasom
Copy link
Contributor Author

rasom commented Nov 21, 2018

thanks @lukaszfryc

fix iterating over all messages from realm db (was done for deduplication)
async loading of chats (:init-chats event)
@rasom rasom merged commit d66198a into develop Nov 21, 2018
@rasom rasom deleted the fix/slow-sign-in branch November 21, 2018 16:22
@status-comment-bot
Copy link

#35 CI BUILD SUCCESSFUL in 22 min and counting (d66198a)

Android(e2e) iOS MacOS AppImage Windows

rasom added a commit that referenced this pull request Nov 23, 2018
rcullito pushed a commit that referenced this pull request Nov 23, 2018
rasom added a commit that referenced this pull request Dec 5, 2018
Issue was caused by #6722

Implementation:
1. `old-message-id` field (indexed) was introduced in `message` entity
   and is calculated as `message-id` was calculated in `0.9.31`
```clojure
(defn old-message-id
  [message]
  (sha3 (pr-str message)))
```
2. When a reply message is sent from the PR version of app both `response-to`
   and `response-to-v2` fields are sent as a part of `message`'s `content`
   field, so that it can be recognized by `0.9.31`.
3. When PR version of app receives reply from `0.9.31` we check whether
   message's `content` contains `response-to` but doesn't contain
   `response-to-v2`, and if so we check whether DB contains message with
   `old-message-id=response-to`. If such message has been found we assoc
   `response-to-v2` to content.
4. If message from DB contains only `response-to` but not `response-to-v2`
   attempt to fetch the message by `old-message-id` is done.
rasom added a commit that referenced this pull request Dec 5, 2018
Issue was caused by #6722

Implementation:
1. `old-message-id` field (indexed) was introduced in `message` entity
   and is calculated as `message-id` was calculated in `0.9.31`
```clojure
(defn old-message-id
  [message]
  (sha3 (pr-str message)))
```
2. When a reply message is sent from the PR version of app both `response-to`
   and `response-to-v2` fields are sent as a part of `message`'s `content`
   field, so that it can be recognized by `0.9.31`.
3. When PR version of app receives reply from `0.9.31` we check whether
   message's `content` contains `response-to` but doesn't contain
   `response-to-v2`, and if so we check whether DB contains message with
   `old-message-id=response-to`. If such message has been found we assoc
   `response-to-v2` to content.
4. If message from DB contains only `response-to` but not `response-to-v2`
   attempt to fetch the message by `old-message-id` is done.
@rasom rasom changed the title fix slow sign in (first iteration) [slow sign in] fix slow sign in (first iteration) Dec 10, 2018
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

No open projects
Archived in project

Development

Successfully merging this pull request may close these issues.