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
use bytes::Bytes;
use futures::{stream, StreamExt};
use std::{collections::HashMap, error::Error, sync::Arc};
use tokio::{
    sync::Mutex,
    time::{self, Duration},
};
use tonic::{transport::Channel, Request};

use crate::{
    dstore_proto::{dstore_client::DstoreClient, Byte, KeyValue},
    DstoreError, MAX_BYTE_SIZE,
};

/// Maintain state of Local cache
pub struct Local {
    /// Local, cached in-memory database
    db: HashMap<Bytes, Bytes>,
    /// Stores client connection with Global
    global: DstoreClient<Channel>,
    /// Using an address as UID
    pub addr: String,
}

impl Local {
    /// Generate reference counted pointer to datastructure maintaining Local state
    pub async fn new(
        global_addr: String,
        addr: String,
    ) -> Result<Arc<Mutex<Self>>, Box<dyn Error>> {
        // Client connection to Global server
        let mut global = DstoreClient::connect(format!("http://{}", global_addr)).await?;

        // Check if Local is allowed to join Global's cluster
        match global
            .join(Request::new(Byte {
                body: addr.as_bytes().to_vec(),
            }))
            .await
        {
            Ok(_) => {
                // If able to join, create reference counted pointer to Local state
                let node = Arc::new(Mutex::new(Self {
                    db: HashMap::new(),
                    global,
                    addr,
                }));

                // Start a timer at intervals of 5 seconds, create clone of Local pointer
                let mut timer = time::interval(Duration::from_secs(5));
                let updater = node.clone();

                // Start thread to concurrently update cache by refering Global invalidation queue
                tokio::spawn(async move {
                    loop {
                        timer.tick().await;
                        updater.lock().await.update().await;
                    }
                });

                Ok(node)
            }
            Err(_) => Err(Box::new(DstoreError("Couldn't join cluster".to_string()))),
        }
    }

    /// Remove cached mappings as per directions from Global Invalidation queue
    pub async fn update(&mut self) {
        while let Ok(key) = self
            .global
            .update(Request::new(Byte {
                body: self.addr.as_bytes().to_vec(),
            }))
            .await
        {
            self.db.remove(&key.into_inner().body[..]);
        }
    }

    /// Insert VALUEs onto Global in either a single packet or as a stream as per it's size
    pub async fn insert(&mut self, key: Bytes, value: Bytes) -> Result<(), Box<dyn Error>> {
        if value.len() < MAX_BYTE_SIZE {
            self.insert_single(key, value).await
        } else {
            self.insert_file(key, value).await
        }
    }

    /// Insert a single packet sized KEY->VALUE mapping onto Global and store in cache
    pub async fn insert_single(&mut self, key: Bytes, value: Bytes) -> Result<(), Box<dyn Error>> {
        // Check if LOCAL already contains KEY
        if self.db.contains_key(&key) {
            return Err(Box::new(DstoreError("Key occupied!".to_string())));
        } else {
            // If not, consult Global
            let req = Byte { body: key.to_vec() };
            match self.global.contains(Request::new(req)).await {
                Ok(size) => {
                    // If Global contains KEY, update LOCAL cache
                    if size.into_inner().size as usize > MAX_BYTE_SIZE {
                        self.get_file(&key).await?;
                    } else {
                        self.get_single(&key).await?;
                    }
                    Err(Box::new(DstoreError(
                        "Local updated, Key occupied!".to_string(),
                    )))
                }
                Err(_) => {
                    // Else push a single packet KEY -> VALUE to update GLOBAL
                    let req = Request::new(KeyValue {
                        key: key.to_vec(),
                        value: value.to_vec(),
                    });
                    let res = self.global.push(req).await;

                    if let Err(e) = res {
                        Err(Box::new(DstoreError(format!(
                            "Couldn't update Global: {}",
                            e
                        ))))
                    } else {
                        // If Global updated successfully, add mapping to cache
                        self.db.insert(key, value);
                        Ok(eprintln!("Database updated"))
                    }
                }
            }
        }
    }

    /// Insert large KEY -> VALUE mappings on Global and store in cache
    pub async fn insert_file(&mut self, key: Bytes, value: Bytes) -> Result<(), Box<dyn Error>> {
        // Check if LOCAL already contains KEY
        if self.db.contains_key(&key) {
            return Err(Box::new(DstoreError("Key occupied!".to_string())));
        } else {
            // If not, consult Global
            let req = Byte { body: key.to_vec() };
            match self.global.contains(Request::new(req.clone())).await {
                Ok(size) => {
                    // If Global contains KEY, update LOCAL cache
                    if size.into_inner().size as usize > MAX_BYTE_SIZE {
                        self.get_file(&key).await?;
                    } else {
                        self.get_single(&key).await?;
                    }
                    Err(Box::new(DstoreError(
                        "Local updated, Key occupied!".to_string(),
                    )))
                }
                Err(_) => {
                    // Else push steam of packets ordered as `KEY, VALUE(1), VALUE(2)..` frames, to update GLOBAL
                    let mut frames = vec![Byte { body: key.to_vec() }];
                    // Size each frame upto MAX_BYTE_SIZE
                    for i in 0..value.len() / MAX_BYTE_SIZE {
                        frames.push(Byte {
                            body: value[i * MAX_BYTE_SIZE..(i + 1) * MAX_BYTE_SIZE].to_vec(),
                        })
                    }

                    // If global accepts stream, update cache, else fail task
                    match self
                        .global
                        .push_file(Request::new(stream::iter(frames)))
                        .await
                    {
                        Ok(_) => {
                            self.db.insert(key, value);
                            Ok(eprintln!("Database updated"))
                        }
                        Err(e) => Err(Box::new(DstoreError(format!(
                            "Couldn't update Global: {}",
                            e
                        )))),
                    }
                }
            }
        }
    }

    /// Get VALUE associated with KEY from system
    pub async fn get(&mut self, key: &Bytes) -> Result<Bytes, Box<dyn Error>> {
        // Check cache for KEY, if it exists, return associated VALUE
        match self.db.get(key) {
            Some(value) => Ok(value.clone()),
            None => {
                // If KEY in Global, extract VALUE byte size
                let size = match self
                    .global
                    .contains(Request::new(Byte { body: key.to_vec() }))
                    .await
                {
                    Ok(res) => res.into_inner().size,
                    Err(e) => return Err(Box::new(DstoreError(format!("Global: {}", e)))),
                } as usize;
                // If VALUE sized larger than single packet transportable, use get_file(), else use get_single()
                if size < MAX_BYTE_SIZE {
                    self.get_single(key).await
                } else {
                    self.get_file(key).await
                }
            }
        }
    }

    /// Get VALUES that can fit in a single packet
    pub async fn get_single(&mut self, key: &Bytes) -> Result<Bytes, Box<dyn Error>> {
        // Check if KEY is present in cache, else consult Global
        match self.db.get(key) {
            Some(value) => Ok(value.clone()),
            None => {
                // Send pull request to Global, update cache if successful
                let req = Request::new(Byte { body: key.to_vec() });
                match self.global.pull(req).await {
                    Ok(res) => {
                        let res = res.into_inner();
                        eprintln!("Updating Local");
                        self.db.insert(key.clone(), Bytes::from(res.body.clone()));
                        Ok(Bytes::from(res.body))
                    }
                    Err(_) => Err(Box::new(DstoreError(
                        "Key-Value mapping doesn't exist".to_string(),
                    ))),
                }
            }
        }
    }

    /// Get VALUES that don't fit in a single packet
    pub async fn get_file(&mut self, key: &Bytes) -> Result<Bytes, Box<dyn Error>> {
        // Check if KEY is present in cache, else consult Global
        match self.db.get(key) {
            Some(value) => Ok(value.clone()),
            None => {
                // Send pull_file request to Global, update cache with streamed response
                let req = Request::new(Byte { body: key.to_vec() });
                let mut stream = self.global.pull_file(req).await.unwrap().into_inner();
                eprintln!("Updating Local");
                let mut value = vec![];
                while let Some(frame) = stream.next().await {
                    let mut frame = frame?;
                    value.append(&mut frame.body);
                }
                self.db.insert(key.clone(), Bytes::from(value.clone()));
                Ok(Bytes::from(value))
            }
        }
    }

    /// Remove a KEY from the system
    pub async fn remove(&mut self, key: &Bytes) -> Result<(), Box<dyn Error>> {
        // Store erroring locations to write error message
        let mut err = vec![];
        // Send remove request to Global
        let req = Request::new(Byte { body: key.to_vec() });
        match self.global.remove(req).await {
            Ok(_) => eprintln!("Global mapping removed!"),
            Err(_) => err.push("global"),
        }

        // Check if mapping exists locally, let `update()` remove KEY if it does
        match self.db.contains_key(key) {
            true => eprintln!("Local mapping will soon be removed!"),
            false => err.push("local"),
        }

        // Return error message based on contents of err
        match err.len() {
            0 => Ok(()),
            _ => Err(Box::new(DstoreError(format!(
                "Key missing from {}!",
                err.join(" and ")
            )))),
        }
    }
}