Skip to content

Commit

Permalink
refactoring
Browse files Browse the repository at this point in the history
  • Loading branch information
yuezato committed Sep 30, 2019
1 parent ab2191b commit 6e6da2e
Show file tree
Hide file tree
Showing 7 changed files with 59 additions and 25 deletions.
26 changes: 20 additions & 6 deletions src/storage/builder.rs
Original file line number Diff line number Diff line change
Expand Up @@ -107,12 +107,26 @@ impl StorageBuilder {
self
}

/// ジャーナルバッファをsyncする際に
/// 安全な手順でsyncを行うようにする
/// ジャーナルバッファをdiskにflushする際に
/// atomicにdiskに永続化されるように安全な手順で書き出す
///
/// この詳細については wiki (https://github.com/frugalos/cannyls/wiki/Safe-Sync) を参照のこと。
pub fn journal_safe_sync(&mut self, safe_sync: bool) -> &mut Self {
self.journal.safe_sync = safe_sync;
/// これにより、
/// ジャーナルバッファを書き出している途中でプロセスがクラッシュしても
/// 再起動後には書き出し直前の状態に戻ることができる。
pub fn journal_safe_flush(&mut self, safe_flush: bool) -> &mut Self {
self.journal.buffer_options.safe_flush = safe_flush;
self
}

/// enqueue時にGoToFrontでEndOfRecordsを上書きする必要がある際は、
/// 先に新しいレコードとEndOfRecordsを書き込んでから、
/// その後にGoToFrontを書き込むようにする。
///
/// GoToFrontを書き込んでからジャーナル領域先頭にseekするという
/// 比較的長い処理をしている間にプロセスがクラッシュしても、
/// 再起動後には直前の状態の戻ることができる。
pub fn journal_safe_enqueue(&mut self, safe_enqueue: bool) -> &mut Self {
self.journal.buffer_options.safe_enqueue = safe_enqueue;
self
}

Expand Down Expand Up @@ -184,7 +198,7 @@ impl StorageBuilder {
header.block_size.contains(nvm.block_size()),
ErrorKind::InvalidInput
);
let mut journal_options = self.journal.clone();
let mut journal_options = self.journal;
journal_options.block_size = header.block_size;

// UUIDをチェック
Expand Down
2 changes: 1 addition & 1 deletion src/storage/journal/mod.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
pub use self::header::{JournalHeader, JournalHeaderRegion};
pub use self::nvm_buffer::JournalNvmBuffer;
pub use self::options::JournalRegionOptions;
pub use self::options::{JournalBufferOptions, JournalRegionOptions};
pub use self::record::{JournalEntry, JournalRecord};
pub use self::region::JournalRegion;

Expand Down
8 changes: 4 additions & 4 deletions src/storage/journal/nvm_buffer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ pub struct JournalNvmBuffer<N: NonVolatileMemory> {
// 内部NVMのブロック境界に合うようにアライメントするために使用される。
read_buf: AlignedBytes,

safe_sync: bool,
safe_flush: bool,
}
impl<N: NonVolatileMemory> JournalNvmBuffer<N> {
/// 新しい`JournalNvmBuffer`インスタンスを生成する.
Expand All @@ -67,7 +67,7 @@ impl<N: NonVolatileMemory> JournalNvmBuffer<N> {
///
/// ただし、シーク時には、シーク地点を含まない次のブロック境界までのデータは
/// 上書きされてしまうので注意が必要.
pub fn new(nvm: N, safe_sync: bool) -> Self {
pub fn new(nvm: N, safe_flush: bool) -> Self {
let block_size = nvm.block_size();
JournalNvmBuffer {
inner: nvm,
Expand All @@ -76,7 +76,7 @@ impl<N: NonVolatileMemory> JournalNvmBuffer<N> {
write_buf_offset: 0,
write_buf: AlignedBytes::new(0, block_size),
read_buf: AlignedBytes::new(0, block_size),
safe_sync,
safe_flush,
}
}

Expand Down Expand Up @@ -111,7 +111,7 @@ impl<N: NonVolatileMemory> JournalNvmBuffer<N> {
return Ok(());
}

if self.safe_sync {
if self.safe_flush {
/*
* issue 27(https://github.com/frugalos/cannyls/issues/27)を考慮した
* 順序づいた書き込みを行う。
Expand Down
20 changes: 17 additions & 3 deletions src/storage/journal/options.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,20 +3,34 @@ use block::BlockSize;
/// ジャーナル領域の挙動を調整するためのパラメータ群.
///
/// 各オプションの説明は`StorageBuilder'のドキュメントを参照のこと.
#[derive(Debug, Clone)]
#[derive(Debug, Clone, Copy)]
pub struct JournalRegionOptions {
pub gc_queue_size: usize,
pub sync_interval: usize,
pub block_size: BlockSize,
pub safe_sync: bool,
pub buffer_options: JournalBufferOptions,
}
impl Default for JournalRegionOptions {
fn default() -> Self {
JournalRegionOptions {
gc_queue_size: 0x1000,
sync_interval: 0x1000,
block_size: BlockSize::min(),
safe_sync: false,
buffer_options: Default::default(),
}
}
}

#[derive(Debug, Clone, Copy)]
pub struct JournalBufferOptions {
pub safe_flush: bool,
pub safe_enqueue: bool,
}
impl Default for JournalBufferOptions {
fn default() -> Self {
JournalBufferOptions {
safe_flush: false,
safe_enqueue: false,
}
}
}
2 changes: 1 addition & 1 deletion src/storage/journal/region.rs
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,7 @@ where
let ring_buffer = JournalRingBuffer::new(
ring_buffer_nvm,
header.ring_buffer_head,
options.safe_sync,
options.buffer_options,
metric_builder,
);

Expand Down
19 changes: 12 additions & 7 deletions src/storage/journal/ring_buffer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ use prometrics::metrics::MetricBuilder;
use std::io::{BufReader, Read, Seek, SeekFrom};

use super::record::{EMBEDDED_DATA_OFFSET, END_OF_RECORDS_SIZE};
use super::{JournalEntry, JournalNvmBuffer, JournalRecord};
use super::{JournalBufferOptions, JournalEntry, JournalNvmBuffer, JournalRecord};
use lump::LumpId;
use metrics::JournalQueueMetrics;
use nvm::NonVolatileMemory;
Expand Down Expand Up @@ -35,7 +35,7 @@ pub struct JournalRingBuffer<N: NonVolatileMemory> {

metrics: JournalQueueMetrics,

safe_sync: bool,
safe_enqueue: bool,
}
impl<N: NonVolatileMemory> JournalRingBuffer<N> {
pub fn head(&self) -> u64 {
Expand All @@ -53,16 +53,21 @@ impl<N: NonVolatileMemory> JournalRingBuffer<N> {
}

/// `JournalRingBuffer`インスタンスを生成する.
pub fn new(nvm: N, head: u64, safe_sync: bool, metric_builder: &MetricBuilder) -> Self {
pub fn new(
nvm: N,
head: u64,
options: JournalBufferOptions,
metric_builder: &MetricBuilder,
) -> Self {
let metrics = JournalQueueMetrics::new(metric_builder);
metrics.capacity_bytes.set(nvm.capacity() as f64);
JournalRingBuffer {
nvm: JournalNvmBuffer::new(nvm, safe_sync),
nvm: JournalNvmBuffer::new(nvm, options.safe_flush),
unreleased_head: head,
head,
tail: head,
metrics,
safe_sync,
safe_enqueue: options.safe_enqueue,
}
}

Expand Down Expand Up @@ -115,10 +120,10 @@ impl<N: NonVolatileMemory> JournalRingBuffer<N> {
&mut self,
record: &JournalRecord<B>,
) -> Result<Option<(LumpId, JournalPortion)>> {
if self.safe_sync {
if self.safe_enqueue {
self.safe_enqueue(record)
} else {
self.fast_aux(record)
self.fast_enqueue(record)
}
}

Expand Down
7 changes: 4 additions & 3 deletions src/storage/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -522,9 +522,10 @@ mod tests {

// マイナーバージョンを減らして、ヘッダを上書きする
{
header.minor_version = header.minor_version.checked_sub(1).expect(
"このテストは`MINOR_VERSION >= 1`であることを前提としている",
);
header.minor_version = header
.minor_version
.checked_sub(1)
.expect("このテストは`MINOR_VERSION >= 1`であることを前提としている");
let file = track_any_err!(OpenOptions::new().write(true).open(&path))?;
track!(header.write_to(file))?;
}
Expand Down

0 comments on commit 6e6da2e

Please sign in to comment.