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
//! Durability and sync policies for transactions.
//!
use noxu_dbi::ReplicaAckPolicyKind;
/// Sync policy for local commit synchronization.
///
/// Determines how transaction commits are synchronized to stable storage.
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, Default)]
pub enum SyncPolicy {
/// Write and fsync to disk on commit.
///
/// Maximum durability, but slowest performance. Guarantees that committed
/// data is written to stable storage.
#[default]
Sync,
/// Write to OS buffers on commit (no fsync).
///
/// Data is written to OS buffers but not necessarily to disk. Faster than
/// Sync but less durable in case of OS crash.
WriteNoSync,
/// No fsync on commit; the commit record is written only to the log
/// buffer in application memory (not to the OS).
///
/// Maximum performance, minimum durability. The commit stays in the
/// in-memory log buffer until a later write flushes it to the OS, so a
/// process crash before that flush loses the commit. (Contrast
/// [`SyncPolicy::WriteNoSync`], which does write to OS buffers and so
/// survives a process crash, losing data only on OS/power failure.)
NoSync,
}
/// Acknowledgment policy for replicated environments.
///
/// Determines how many replicas must acknowledge a transaction before
/// the commit returns to the application.
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, Default)]
pub enum ReplicaAckPolicy {
/// All replicas must acknowledge.
All,
/// No acknowledgment required.
None,
/// Simple majority must acknowledge.
#[default]
SimpleMajority,
}
/// Durability characteristics for a transaction.
///
/// Specifies the durability guarantees associated with a transaction when
/// it's committed. The durability policy consists of:
/// - Local sync policy: how the master node synchronizes
/// - Replica sync policy: how replica nodes synchronize
/// - Replica acknowledgment policy: how many replicas must acknowledge
///
///
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub struct Durability {
/// Sync policy for the local (master) node.
pub local_sync: SyncPolicy,
/// Sync policy for replica nodes.
pub replica_sync: SyncPolicy,
/// Acknowledgment policy for replicas.
pub replica_ack: ReplicaAckPolicy,
}
impl ReplicaAckPolicy {
/// Convert this policy to the dependency-free `ReplicaAckPolicyKind`
/// used by the `ReplicaAckCoordinator` trait in `noxu-dbi`.
pub fn as_kind(self) -> ReplicaAckPolicyKind {
match self {
ReplicaAckPolicy::All => ReplicaAckPolicyKind::All,
ReplicaAckPolicy::SimpleMajority => {
ReplicaAckPolicyKind::SimpleMajority
}
ReplicaAckPolicy::None => ReplicaAckPolicyKind::None,
}
}
}
impl Durability {
/// Creates a new Durability with the specified policies.
pub fn new(
local_sync: SyncPolicy,
replica_sync: SyncPolicy,
replica_ack: ReplicaAckPolicy,
) -> Self {
Self { local_sync, replica_sync, replica_ack }
}
/// Convenience constant matching JE `Durability.COMMIT_SYNC`: the master
/// fsyncs on commit, replicas do not fsync, and a simple majority must
/// acknowledge. (JE `Durability.java`: `SYNC` / `NO_SYNC` /
/// `SIMPLE_MAJORITY`.)
pub const COMMIT_SYNC: Self = Self {
local_sync: SyncPolicy::Sync,
replica_sync: SyncPolicy::NoSync,
replica_ack: ReplicaAckPolicy::SimpleMajority,
};
/// Convenience constant matching JE `Durability.COMMIT_NO_SYNC`: neither
/// the master nor replicas fsync on commit, but a simple majority must
/// still acknowledge. (JE `Durability.java`: `NO_SYNC` / `NO_SYNC` /
/// `SIMPLE_MAJORITY`.) The acknowledgment guarantee is preserved even
/// though the sync policy is relaxed.
pub const COMMIT_NO_SYNC: Self = Self {
local_sync: SyncPolicy::NoSync,
replica_sync: SyncPolicy::NoSync,
replica_ack: ReplicaAckPolicy::SimpleMajority,
};
/// Convenience constant matching JE `Durability.COMMIT_WRITE_NO_SYNC`: the
/// master writes to OS buffers without fsync, replicas do not fsync, and a
/// simple majority must acknowledge. (JE `Durability.java`:
/// `WRITE_NO_SYNC` / `NO_SYNC` / `SIMPLE_MAJORITY`.)
pub const COMMIT_WRITE_NO_SYNC: Self = Self {
local_sync: SyncPolicy::WriteNoSync,
replica_sync: SyncPolicy::NoSync,
replica_ack: ReplicaAckPolicy::SimpleMajority,
};
}
impl Default for Durability {
fn default() -> Self {
Self::COMMIT_SYNC
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn test_sync_policy_default() {
assert_eq!(SyncPolicy::default(), SyncPolicy::Sync);
}
#[test]
fn test_sync_policy_equality() {
assert_eq!(SyncPolicy::Sync, SyncPolicy::Sync);
assert_ne!(SyncPolicy::Sync, SyncPolicy::NoSync);
}
#[test]
fn test_replica_ack_policy_default() {
assert_eq!(
ReplicaAckPolicy::default(),
ReplicaAckPolicy::SimpleMajority
);
}
#[test]
fn test_replica_ack_policy_equality() {
assert_eq!(ReplicaAckPolicy::All, ReplicaAckPolicy::All);
assert_ne!(ReplicaAckPolicy::All, ReplicaAckPolicy::None);
}
#[test]
fn test_durability_new() {
let d = Durability::new(
SyncPolicy::Sync,
SyncPolicy::WriteNoSync,
ReplicaAckPolicy::All,
);
assert_eq!(d.local_sync, SyncPolicy::Sync);
assert_eq!(d.replica_sync, SyncPolicy::WriteNoSync);
assert_eq!(d.replica_ack, ReplicaAckPolicy::All);
}
#[test]
fn test_durability_commit_sync() {
// JE Durability.COMMIT_SYNC = (SYNC, NO_SYNC, SIMPLE_MAJORITY).
let d = Durability::COMMIT_SYNC;
assert_eq!(d.local_sync, SyncPolicy::Sync);
assert_eq!(d.replica_sync, SyncPolicy::NoSync);
assert_eq!(d.replica_ack, ReplicaAckPolicy::SimpleMajority);
}
#[test]
fn test_durability_commit_no_sync() {
// JE Durability.COMMIT_NO_SYNC = (NO_SYNC, NO_SYNC, SIMPLE_MAJORITY):
// the majority-ack guarantee is retained even with sync relaxed.
let d = Durability::COMMIT_NO_SYNC;
assert_eq!(d.local_sync, SyncPolicy::NoSync);
assert_eq!(d.replica_sync, SyncPolicy::NoSync);
assert_eq!(d.replica_ack, ReplicaAckPolicy::SimpleMajority);
}
#[test]
fn test_durability_commit_write_no_sync() {
// JE Durability.COMMIT_WRITE_NO_SYNC = (WRITE_NO_SYNC, NO_SYNC,
// SIMPLE_MAJORITY).
let d = Durability::COMMIT_WRITE_NO_SYNC;
assert_eq!(d.local_sync, SyncPolicy::WriteNoSync);
assert_eq!(d.replica_sync, SyncPolicy::NoSync);
assert_eq!(d.replica_ack, ReplicaAckPolicy::SimpleMajority);
}
#[test]
fn test_durability_default() {
let d = Durability::default();
assert_eq!(d, Durability::COMMIT_SYNC);
}
#[test]
fn test_durability_equality() {
let d1 = Durability::new(
SyncPolicy::Sync,
SyncPolicy::NoSync,
ReplicaAckPolicy::SimpleMajority,
);
let d2 = Durability::COMMIT_SYNC;
assert_eq!(d1, d2);
}
#[test]
fn test_durability_clone() {
let d1 = Durability::COMMIT_SYNC;
let d2 = d1;
assert_eq!(d1, d2);
}
#[test]
fn test_sync_policy_copy() {
let s1 = SyncPolicy::Sync;
let s2 = s1;
assert_eq!(s1, s2);
}
#[test]
fn test_replica_ack_policy_copy() {
let r1 = ReplicaAckPolicy::All;
let r2 = r1;
assert_eq!(r1, r2);
}
}