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
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
use std::collections::HashMap;
use std::net::{Ipv4Addr, SocketAddrV4};
use std::sync::Arc;

use async_lib::AsyncRuntimeManager;
use async_std::sync::Arc as AsyncArc;
use config::Config;
use dashmap::DashMap;
use error::InternalError;
use fast_rsync::SignatureOptions;
#[cfg(feature = "observability")]
use lazy_static::lazy_static;
use net::data_exchange_client::location_mgr::data_exchange_server::DataExchangeServer as ProtoDataExchangeServer;
use net::data_exchange_client::DataExchangeClient;
use net::data_exchange_server::DataExchangeServer;
use net::data_request_client::location_mgr::data_request_server::DataRequestServer as ProtoDataRequestServer;
use net::data_request_client::DataRequestClient;
use net::data_request_server::DataRequestServer;
use object_lib::ObjectId;
use object_move_handle::ObjectMoveHandle;
use object_tracker::ObjectTracker;
#[cfg(feature = "observability")]
use prometheus::{register_histogram_vec, HistogramVec};
use tokio;
use tonic::transport::Server;

pub mod config;
mod error;
mod net;
mod object_move_handle;
mod rsync_lib;
mod util;

#[cfg(feature = "observability")]
lazy_static! {
    static ref OWNERSHIP_SIGNATURE_DESERIALIZATION_HISTOGRAM: HistogramVec =
        register_histogram_vec!(
            "location_manager_ownership_signature_deserialization_milliseconds",
            "Location Manager signature deserialization latencies in milliseconds",
            &[],
            vec![0.1, 1.0, 10.0, 100.0],
        )
        .unwrap();
}

pub type HostId = String;
pub type MoveHandleMap = DashMap<ObjectId, ObjectMoveHandle>;

pub struct LocationManager {
    // FIXME this should be replaced with a concurrent hashmap (like flurry, dashmap, etc.), or at
    // the very least protected by some kind of mutex.
    pub location_map: HashMap<ObjectId, HostId>,
    pub move_handles: Arc<MoveHandleMap>,

    async_rt_manager: Arc<AsyncRuntimeManager>,
    config: Config,
    object_tracker: Arc<ObjectTracker>,

    data_request_client: AsyncArc<DataRequestClient>,
    data_exchange_client: AsyncArc<DataExchangeClient>,
}

impl LocationManager {
    pub fn new(
        config: Config,
        async_rt_manager: Arc<AsyncRuntimeManager>,
        object_tracker: Arc<ObjectTracker>,
    ) -> Self {
        Self {
            config: config.clone(),
            location_map: HashMap::new(),
            async_rt_manager,
            object_tracker,
            move_handles: Arc::new(DashMap::new()),
            data_request_client: AsyncArc::new(DataRequestClient::new(config.data_server_port)),
            data_exchange_client: AsyncArc::new(DataExchangeClient::new(config.data_server_port)),
        }
    }

    pub fn get_current_host_id(&self) -> HostId {
        self.config.client_id.to_string()
    }

    pub async fn init_server(&self) -> Result<(), InternalError> {
        for runtime in self.async_rt_manager.runtime_iter() {
            let server_port = self.config.data_server_port;
            let socket_addr = SocketAddrV4::new(Ipv4Addr::new(0, 0, 0, 0), server_port);

            let socket = tokio::net::TcpSocket::new_v4().unwrap();
            socket.set_reuseaddr(true).expect("failed to set reuseaddr");
            socket.set_reuseport(true).expect("failed to set reuseport");
            socket
                .bind(socket_addr.into())
                .expect("failed to bind to port");

            let data_request_server = DataRequestServer::new(
                self.config.clone(),
                Arc::clone(&self.object_tracker),
                Arc::clone(&self.move_handles),
                AsyncArc::clone(&self.data_request_client),
                AsyncArc::clone(&self.data_exchange_client),
                &self.config.rsync_config,
                Arc::clone(&self.async_rt_manager),
            );
            let data_exchange_server = DataExchangeServer::new(
                self.config.clone(),
                Arc::clone(&self.object_tracker),
                Arc::clone(&self.move_handles),
            );

            runtime.spawn(async move {
                let listener = socket.listen(8192).unwrap();
                let incoming = tokio_stream::wrappers::TcpListenerStream::new(listener);

                let serve = Server::builder()
                    .tcp_nodelay(true)
                    .add_service(ProtoDataRequestServer::new(data_request_server).max_decoding_message_size(512 * 1024 * 1024).max_encoding_message_size(512 * 1024 * 1024)/*.accept_compressed(CompressionEncoding::Gzip).send_compressed(CompressionEncoding::Gzip)*/)
                    .add_service(ProtoDataExchangeServer::new(data_exchange_server).max_decoding_message_size(512 * 1024 * 1024).max_encoding_message_size(512 * 1024 * 1024)/*.accept_compressed(CompressionEncoding::Gzip).send_compressed(CompressionEncoding::Gzip)*/)
                    .serve_with_incoming(incoming);

                serve.await.unwrap()
            });
        }

        Ok(())
    }

    pub async fn move_object_to(
        &self,
        object_id: ObjectId,
        target_host: HostId,
    ) -> Result<(), anyhow::Error> {
        let source_host_id = self.config.client_id;
        let source_host = self.config.data_server_hosts[source_host_id as usize].clone();
        if source_host == target_host {
            return Err(anyhow::Error::msg(
                "Cannot be the target of requested object move",
            ));
        }

        self.data_request_client
            .initiate_move(object_id, source_host, target_host)
            .await
            .expect("failed to initiate move");

        return Ok(());
    }

    pub async fn move_object(
        &self,
        object_id: ObjectId,
        source_host_id: u16,
        target_host_id: u16,
    ) -> Result<(), anyhow::Error> {
        let client_id = self.config.client_id;
        let source_host = self.config.data_server_hosts[source_host_id as usize].clone();
        let target_host = self.config.data_server_hosts[target_host_id as usize].clone();

        // If the current host is not the recipient in this data movement, we only need to notify
        // the remote recipient to trigger the data movement.
        if client_id == source_host_id || client_id != target_host_id {
            self.data_request_client
                .initiate_move(object_id, source_host, target_host)
                .await
                .expect("failed to initiate move");

            return Ok(());
        }

        // This host is the recipient, so first we need to do some bookkeeping.
        self.move_handles
            .insert(object_id, ObjectMoveHandle::new(object_id, None));

        let signature = {
            let rsync_config = self.config.rsync_config.clone();
            let object_tracker = Arc::clone(&self.object_tracker);
            rsync_lib::calculate_signature(
                object_id,
                object_tracker,
                rsync_lib::SignatureCalculationConfig::SignatureCalculationConfig(&rsync_config),
            )
        };

        let data_request_client = AsyncArc::clone(&self.data_request_client);
        self.async_rt_manager.spawn(async move {
            data_request_client
                .move_object(object_id, source_host, target_host, &signature)
                .await
                .expect(&format!("failed to move object {}", object_id));
        });

        Ok(())
    }

    pub async fn wait_for_object(&self, object_id: ObjectId) {
        match self.object_tracker.get(object_id).is_none() {
            true => {
                let move_handle = self
                    .move_handles
                    .get(&object_id)
                    .expect(&format!("object {} is not in flight", object_id))
                    .clone();
                move_handle.wait_until_done().await;
            }
            false => (),
        }
    }

    /// Unconditionally waits for an in-flight object without first checking for residency.
    ///
    /// It is up to the caller of this function to ensure that the right wait semantics are
    /// enforced.
    pub async fn wait_for_object_or_update(&self, object_id: ObjectId) -> Option<()> {
        let move_handle = match self.move_handles.get(&object_id) {
            None => return None,
            Some(h) => h.clone(),
        };
        move_handle.wait_until_done().await;

        Some(())
    }

    pub fn get_object_tracker(&self) -> Arc<ObjectTracker> {
        self.object_tracker.clone()
    }

    pub async fn calculate_signature(&self, object_id: ObjectId) -> Vec<u8> {
        let rsync_config = self.config.rsync_config.clone();
        let object_tracker = Arc::clone(&self.object_tracker);
        rsync_lib::calculate_signature(
            object_id,
            object_tracker,
            rsync_lib::SignatureCalculationConfig::SignatureCalculationConfig(&rsync_config),
        )
    }

    pub async fn insert_move_handle(&self, object_id: ObjectId) {
        self.move_handles
            .insert(object_id, ObjectMoveHandle::new(object_id, None));
    }

    pub async fn trigger_move(
        &self,
        object_id: ObjectId,
        foreign_signature: &Vec<u8>,
        target_host_id: String,
    ) {
        let object_bytes: Vec<u8> =
            match util::get_object_bytes(object_id, Arc::clone(&self.object_tracker)) {
                Some(ob) => ob,
                None => vec![],
            };

        #[cfg(feature = "observability")]
        let signature_deserialization_start = tokio::time::Instant::now();
        let foreign_signature = rsync_lib::deserialize_signature(
            foreign_signature,
            rsync_lib::SignatureCalculationConfig::SignatureCalculationOptions(SignatureOptions {
                block_size: self.config.rsync_config.block_size,
                crypto_hash_size: self.config.rsync_config.hash_size,
            }),
        )
        .unwrap();
        #[cfg(feature = "observability")]
        {
            let signature_deserialization_duration = signature_deserialization_start.elapsed();
            OWNERSHIP_SIGNATURE_DESERIALIZATION_HISTOGRAM
                .with_label_values(&[])
                .observe(signature_deserialization_duration.as_micros() as f64 / 1000.0);
        }

        util::trigger_object_move(
            object_id,
            Arc::clone(&self.move_handles),
            target_host_id,
            &foreign_signature,
            &object_bytes,
            AsyncArc::clone(&self.data_exchange_client),
            Arc::clone(&self.async_rt_manager),
        )
        .await;
    }
}