1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
// Copyright (c) 2021, Facebook, Inc. and its affiliates
// Copyright (c) 2022, Mysten Labs, Inc.
// SPDX-License-Identifier: Apache-2.0

use config::{SharedCommittee, SharedWorkerCache, WorkerCache, WorkerIndex};

use network::P2pNetwork;
use primary::PrimaryWorkerMessage;
use std::{collections::BTreeMap, sync::Arc};
use store::Store;
use tap::TapOptional;
use tokio::{sync::watch, task::JoinHandle};
use tracing::{error, warn};
use types::{
    metered_channel::{Receiver, Sender},
    Batch, BatchDigest, ReconfigureNotification, WorkerPrimaryError, WorkerPrimaryMessage,
};

#[cfg(test)]
#[path = "tests/synchronizer_tests.rs"]
pub mod synchronizer_tests;

// The `Synchronizer` is responsible to keep the worker in sync with the others.
pub struct Synchronizer {
    /// The committee information.
    committee: SharedCommittee,
    /// The worker information cache.
    worker_cache: SharedWorkerCache,
    // The persistent storage.
    store: Store<BatchDigest, Batch>,
    /// Input channel to receive the commands from the primary.
    rx_message: Receiver<PrimaryWorkerMessage>,
    /// A network sender to send requests to the other workers.
    network: P2pNetwork,
    /// Send reconfiguration update to other tasks.
    tx_reconfigure: watch::Sender<ReconfigureNotification>,
    /// Output channel to send out the batch requests.
    tx_primary: Sender<WorkerPrimaryMessage>,
}

impl Synchronizer {
    #[must_use]
    pub fn spawn(
        committee: SharedCommittee,
        worker_cache: SharedWorkerCache,
        store: Store<BatchDigest, Batch>,
        rx_message: Receiver<PrimaryWorkerMessage>,
        tx_reconfigure: watch::Sender<ReconfigureNotification>,
        tx_primary: Sender<WorkerPrimaryMessage>,
        network: P2pNetwork,
    ) -> JoinHandle<()> {
        tokio::spawn(async move {
            Self {
                committee,
                worker_cache,
                store,
                rx_message,
                network,
                tx_reconfigure,
                tx_primary,
            }
            .run()
            .await;
        })
    }

    /// Main loop listening to the primary's messages.
    async fn run(&mut self) {
        loop {
            tokio::select! {
                // Handle primary's messages.
                Some(message) = self.rx_message.recv() => match message {
                    PrimaryWorkerMessage::Cleanup(_) => {
                        // TODO: this message does nothing, remove it.
                    },
                    PrimaryWorkerMessage::Reconfigure(message) => {
                        // Reconfigure this task and update the shared committee.
                        let shutdown = match &message {
                            ReconfigureNotification::NewEpoch(new_committee) => {
                                self.network.cleanup(self.worker_cache.load().network_diff(new_committee.keys()));
                                self.committee.swap(Arc::new(new_committee.clone()));

                                // Update the worker cache.
                                self.worker_cache.swap(Arc::new(WorkerCache {
                                    epoch: new_committee.epoch,
                                    workers: new_committee.keys().iter().map(|key|
                                        (
                                            (*key).clone(),
                                            self.worker_cache
                                                .load()
                                                .workers
                                                .get(key)
                                                .tap_none(||
                                                    warn!("Worker cache does not have a key for the new committee member"))
                                                .unwrap_or(&WorkerIndex(BTreeMap::new()))
                                                .clone()
                                        )).collect(),
                                }));

                                false
                            }
                            ReconfigureNotification::UpdateCommittee(new_committee) => {
                                self.network.cleanup(self.worker_cache.load().network_diff(new_committee.keys()));
                                self.committee.swap(Arc::new(new_committee.clone()));

                                // Update the worker cache.
                                self.worker_cache.swap(Arc::new(WorkerCache {
                                    epoch: new_committee.epoch,
                                    workers: new_committee.keys().iter().map(|key|
                                        (
                                            (*key).clone(),
                                            self.worker_cache
                                                .load()
                                                .workers
                                                .get(key)
                                                .tap_none(||
                                                    warn!("Worker cache does not have a key for the new committee member"))
                                                .unwrap_or(&WorkerIndex(BTreeMap::new()))
                                                .clone()
                                        )).collect(),
                                }));

                                tracing::debug!("Committee updated to {}", self.committee);
                                false
                            }
                            ReconfigureNotification::Shutdown => true
                        };

                        // Notify all other tasks.
                        self.tx_reconfigure.send(message).expect("All tasks dropped");

                        // Exit only when we are sure that all the other tasks received
                        // the shutdown message.
                        if shutdown {
                            self.tx_reconfigure.closed().await;
                            return;
                        }
                    }
                    PrimaryWorkerMessage::RequestBatch(digest) => {
                        self.handle_request_batch(digest).await;
                    },
                    PrimaryWorkerMessage::DeleteBatches(digests) => {
                        self.handle_delete_batches(digests).await;
                    }
                },
            }
        }
    }

    async fn handle_request_batch(&mut self, digest: BatchDigest) {
        let message = match self.store.read(digest).await {
            Ok(Some(batch)) => WorkerPrimaryMessage::RequestedBatch(digest, batch),
            _ => WorkerPrimaryMessage::Error(WorkerPrimaryError::RequestedBatchNotFound(digest)),
        };

        self.tx_primary
            .send(message)
            .await
            .expect("Failed to send message to primary channel");
    }

    async fn handle_delete_batches(&mut self, digests: Vec<BatchDigest>) {
        let message = match self.store.remove_all(digests.clone()).await {
            Ok(_) => WorkerPrimaryMessage::DeletedBatches(digests),
            Err(err) => {
                error!("{err}");
                WorkerPrimaryMessage::Error(WorkerPrimaryError::ErrorWhileDeletingBatches(
                    digests.clone(),
                ))
            }
        };

        self.tx_primary
            .send(message)
            .await
            .expect("Failed to send message to primary channel");
    }
}