Page MenuHomePhabricator

[Tunnelbroker] always persist WebSocket messages in database
ClosedPublic

Authored by kamil on Sep 28 2023, 7:39 AM.
Tags
None
Referenced Files
F2553259: D9316.diff
Tue, Aug 27, 1:22 AM
Unknown Object (File)
Sun, Aug 25, 12:15 PM
Unknown Object (File)
Sun, Aug 25, 12:15 PM
Unknown Object (File)
Sun, Aug 25, 12:15 PM
Unknown Object (File)
Sun, Aug 25, 12:09 PM
Unknown Object (File)
Sun, Aug 25, 7:21 AM
Unknown Object (File)
Sun, Aug 25, 2:21 AM
Unknown Object (File)
Sat, Aug 24, 11:39 PM
Subscribers

Details

Summary
  • We need to make sure that each message is delivered to client.
  • We need to persist it immediately after receiving
  • Message will be removed after confirmation from client

Depends on D9314

Test Plan
  1. Restart RabbitMQ
  2. Restart localstack
  3. Run this code:
#[tokio::test]
async fn send_websocket() {
  let device_info = create_device(None).await;
  let mut socket = create_socket(&device_info).await;

  let request = WebsocketMessageToDevice {
    device_id: device_info.device_id.to_string(),
    payload: "message from test".to_string(),
  };

  let serialized_request = serde_json::to_string(&request)
    .expect("Failed to serialize connection request");
  println!("request: {}", serialized_request);

  socket
    .send(Message::Text(serialized_request))
    .await
    .expect("Failed to send message");
}
  1. Open RabbitMQ management console, there should be one dropped message (there is no device with test_persist ID)

Screenshot 2023-09-28 at 14.51.24.png (356×1 px, 70 KB)

  1. Open DynamoDB console, message should be persisted:

Screenshot 2023-09-28 at 14.53.32.png (1×5 px, 222 KB)

  1. Additionally, check TB logs for:
2023-09-28T12:52:08.492774Z DEBUG tunnelbroker::websockets::session: Received message for ZXx1ADCFxFm6P+UmVhX0A1tuqUoBU7lYjig/gMzSEJI
2023-09-28T12:52:08.492789Z DEBUG tunnelbroker::database: Persisting message to device: ZXx1ADCFxFm6P+UmVhX0A1tuqUoBU7lYjig/gMzSEJI
  1. Run:

cd services/commtest && cargo test --test tunnelbroker_persist_tests -- --nocapture

Diff Detail

Repository
rCOMM Comm
Lint
Lint Not Applicable
Unit
Tests Not Applicable

Event Timeline

kamil held this revision as a draft.
kamil published this revision for review.Sep 28 2023, 8:09 AM
services/commtest/tests/tunnelbroker_persist_tests.rs
83 ↗(On Diff #31526)

comment says 50 while in code there's 500

services/commtest/tests/tunnelbroker_persist_tests.rs
83 ↗(On Diff #31526)

whoops, I changed it to check something and forgot to restore correct value, I will update before landing

services/commtest/tests/tunnelbroker_persist_tests.rs
83 ↗(On Diff #31526)

It's better to avoid repeating what is already in the code so that we don't have to remember to update both values simultaneously. In this case, we can say e.g.

Wait a specified duration to ensure that message had time to persist

85 ↗(On Diff #31526)

Doesn't look like ten_millis. Maybe wait_duration?

services/commtest/tests/tunnelbroker_persist_tests.rs
86 ↗(On Diff #31526)

This is a test so probably doesn't matter but we should use tokio::sleep in async contexts.

services/commtest/tests/tunnelbroker_persist_tests.rs
86 ↗(On Diff #31526)

Good call. We should replace it with tokio::sleep because thread::sleep might block the whole runtime (all tasks), so it also blocks other tests if they run concurrently.

From my experience, even with rt_multi_thread (Tokio default), it tends to spawn all tasks on one thread so if it gets blocked, it effectively blocks the whole runtime.

use tokio::sleep
(sorry for responding to review late)

This revision is now accepted and ready to land.Oct 11 2023, 8:37 AM