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
284
285
286
287
288
use super::error::{StorageError, StorageResult};
use super::merge::merge_iter;
use super::sstable::SSTable;
use crate::log::{JudgeReal, LogManager};
use crate::storage::SyncDatabase;
use crate::MemDatabase;

use agilulf_protocol::Slice;
use crossbeam::sync::ShardedLock;
use futures::channel::mpsc::{unbounded, UnboundedSender};
use futures::executor::LocalPool;
use futures::stream::StreamExt;
use futures::task::LocalSpawnExt;

use std::collections::{BTreeMap, VecDeque};
use std::path::Path;

use std::sync::atomic::AtomicUsize;
use std::sync::atomic::Ordering;
use std::sync::Arc;

#[repr(packed)]
#[derive(Clone)]
pub struct RawManifestLogEntry {
    pub real_flag: u8,
    pub add_flag: u8,
    pub level: u8,
    pub id: u8,
}

impl JudgeReal for RawManifestLogEntry {
    fn is_real(&self) -> bool {
        self.real_flag == 1
    }

    fn set_real(&mut self, real: bool) {
        if real {
            self.real_flag = 1;
        } else {
            self.real_flag = 0;
        }
    }
}

pub struct ManifestManager {
    base_dir: String,
    log_manager: Arc<LogManager<RawManifestLogEntry>>,
    frozen_databases: Arc<ShardedLock<VecDeque<Arc<MemDatabase>>>>,
    sstables: Arc<[ShardedLock<BTreeMap<usize, SSTable>>; 6]>, // TODO: a concurrent RwLock may be better
    level_counter: Arc<[AtomicUsize; 6]>,
}

impl ManifestManager {
    pub fn create_new(
        base_dir: &str,
        frozen_databases: Arc<ShardedLock<VecDeque<Arc<MemDatabase>>>>,
    ) -> StorageResult<ManifestManager> {
        let base_path = Path::new(base_dir);
        let manifest_path = base_path.join("MANIFEST");

        let manifest_path = match manifest_path.to_str() {
            Some(str) => str,
            None => {
                log::error!("Manifest path is not UTF-8: {:#?}", manifest_path);
                return Err(StorageError::UnicodeError);
            }
        };

        Ok(ManifestManager {
            base_dir: base_dir.to_string(),
            log_manager: Arc::new(LogManager::create_new(manifest_path, 4 * 1024)?),
            frozen_databases,
            sstables: Arc::new([
                ShardedLock::new(BTreeMap::new()),
                ShardedLock::new(BTreeMap::new()),
                ShardedLock::new(BTreeMap::new()),
                ShardedLock::new(BTreeMap::new()),
                ShardedLock::new(BTreeMap::new()),
                ShardedLock::new(BTreeMap::new()),
            ]),
            level_counter: Arc::new([
                AtomicUsize::new(0),
                AtomicUsize::new(0),
                AtomicUsize::new(0),
                AtomicUsize::new(0),
                AtomicUsize::new(0),
                AtomicUsize::new(0),
            ]), // TODO: use macro to avoid these redundant codes
        })
    }

    pub fn open(
        base_dir: &str,
        frozen_databases: Arc<ShardedLock<VecDeque<Arc<MemDatabase>>>>,
    ) -> StorageResult<ManifestManager> {
        let base_path = Path::new(base_dir);
        let manifest_path = base_path.join("MANIFEST");

        let manifest_path = match manifest_path.to_str() {
            Some(str) => str,
            None => {
                log::error!("Manifest path is not UTF-8: {:#?}", manifest_path);
                return Err(StorageError::UnicodeError);
            }
        };
        let log_manager: Arc<LogManager<RawManifestLogEntry>> =
            Arc::new(LogManager::open(manifest_path, 4 * 1024)?);

        let sstables = Arc::new([
            ShardedLock::new(BTreeMap::new()),
            ShardedLock::new(BTreeMap::new()),
            ShardedLock::new(BTreeMap::new()),
            ShardedLock::new(BTreeMap::new()),
            ShardedLock::new(BTreeMap::new()),
            ShardedLock::new(BTreeMap::new()),
        ]);

        let level_counter = Arc::new([
            AtomicUsize::new(0),
            AtomicUsize::new(0),
            AtomicUsize::new(0),
            AtomicUsize::new(0),
            AtomicUsize::new(0),
            AtomicUsize::new(0),
        ]);

        for log in log_manager.iter() {
            match log.add_flag {
                1 => {
                    if log.level >= 6 {
                        return Err(StorageError::ManifestLogFormatError);
                    }

                    let table_path = base_path.join(format!("sstable_{}_{}", log.level, log.id));
                    log::info!("Restoring sstable from {:#?}", table_path);
                    let sstable_file = std::fs::OpenOptions::new()
                        .read(true)
                        .write(true)
                        .open(table_path)?;
                    let sstable = SSTable::open(sstable_file)?;
                    sstables
                        .get(log.level as usize)
                        .unwrap() // unwrap here is totally safe
                        .write()
                        .unwrap()
                        .insert(log.id as usize, sstable);
                    level_counter
                        .get(log.level as usize)
                        .unwrap() // unwrap here is totally safe
                        .fetch_max(log.id as usize, Ordering::SeqCst);
                }
                0 => {
                    if log.level >= 6 {
                        return Err(StorageError::ManifestLogFormatError);
                    }

                    sstables
                        .get(log.level as usize)
                        .unwrap() // unwrap here is totally safe
                        .write()
                        .unwrap()
                        .remove(&(log.id as usize));
                }
                _ => unreachable!(),
            }
        }

        Ok(ManifestManager {
            base_dir: base_dir.to_string(),
            log_manager,
            frozen_databases,
            sstables,
            level_counter,
        })
    }

    //    fn compact<S: Spawn>(&self, _spawner: S) {
    //        unimplemented!()
    //    }

    pub fn background_work(&self) -> StorageResult<UnboundedSender<usize>> {
        let frozen_databases = self.frozen_databases.clone();

        let (freeze_sender, freeze_receiver) = unbounded::<usize>();
        let mut freeze_receiver = freeze_receiver.fuse();

        let base_dir = self.base_dir.clone();
        let level_counter = self.level_counter.clone();
        let sstables = self.sstables.clone();
        let log_manager = self.log_manager.clone();

        std::thread::Builder::new()
            .name("background_worker".to_string())
            .spawn(move || {
                let mut local_pool = LocalPool::new();
                let spawn_result = local_pool.spawner().spawn_local(async move {
                    let base_path = Path::new(&base_dir);
                    loop {
                        let newest_log_id = match freeze_receiver.next().await {
                            Some(id) => id,
                            None => {
                                break;
                            }
                        };
                        let new_log_path = base_path.join(format!("log.{}", newest_log_id));

                        let db_guard = frozen_databases.read().unwrap();
                        match db_guard.back() {
                            Some(db) => {
                                let sstable = SSTable::from(db.clone());
                                drop(db_guard);

                                let id = level_counter[0].fetch_add(1, Ordering::SeqCst);

                                let table_path = base_path.join(format! {"sstable_0_{}", id});
                                let table_path = match table_path.to_str() {
                                    Some(str) => str,
                                    None => {
                                        log::error!("Table path is not UTF-8: {:#?}", table_path);
                                        continue;
                                    }
                                };
                                match sstable.save(table_path).await {
                                    Ok(()) => {}
                                    Err(err) => {
                                        log::error!("Error while storing SSTable: {}", err);
                                        continue;
                                    }
                                }

                                log_manager.add_entry(RawManifestLogEntry {
                                    real_flag: 1,
                                    add_flag: 1,
                                    level: 0,
                                    id: id as u8,
                                });

                                sstables[0].write().unwrap().insert(id, sstable);
                                frozen_databases.write().unwrap().pop_back();

                                match std::fs::remove_file(new_log_path) {
                                    Ok(()) => {}
                                    Err(err) => {
                                        log::error!("Error while remove log: {}", err);
                                        continue;
                                    }
                                };
                            }
                            None => {}
                        }
                    }
                });

                match spawn_result {
                    Ok(()) => {}
                    Err(err) => log::error!("Error while spawning: {}", err),
                }

                local_pool.run();
            })?;

        Ok(freeze_sender)
    }

    pub fn find_key(&self, key: Slice) -> Option<Slice> {
        for level in 0..6 {
            let level = self.sstables[level].read().unwrap();
            for (_id, table) in level.iter() {
                match table.get_sync(key.clone()) {
                    Ok(value) => return Some(value),
                    Err(_) => {}
                }
            }
        }
        None
    }

    pub fn scan(&self, start: Slice, end: Slice) -> impl Iterator<Item = (Slice, Slice)> {
        let mut merge_vec = Vec::new();
        for level in 0..6 {
            let level = self.sstables[level].read().unwrap();
            for (_id, table) in level.iter() {
                merge_vec.push(table.scan_sync(start.clone(), end.clone()).into_iter())
            }
        }
        merge_iter(merge_vec)
    }
}