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
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
#![allow(clippy::type_complexity)]
#![allow(clippy::needless_range_loop)]
#![allow(clippy::single_match)]
use crate::sys::votequorum as ffi;
use std::os::raw::{c_void, c_int};
use std::slice;
use std::collections::HashMap;
use std::sync::Mutex;
use std::ffi::CString;
use std::fmt;
use crate::{CsError, DispatchFlags, TrackFlags, Result, NodeId};
use crate::string_from_bytes;
pub struct RingId {
pub nodeid: NodeId,
pub seq: u64,
}
lazy_static! {
static ref HANDLE_HASH: Mutex<HashMap<u64, Handle>> = Mutex::new(HashMap::new());
}
pub enum NodeState
{
Member,
Dead,
Leaving,
Unknown,
}
impl NodeState {
pub fn new(state: u32) -> NodeState
{
match state {
1 => NodeState::Member,
2 => NodeState::Dead,
3 => NodeState::Leaving,
_ => NodeState::Unknown,
}
}
}
impl fmt::Debug for NodeState {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
match self {
NodeState::Member => write!(f, "Member"),
NodeState::Dead => write!(f, "Dead"),
NodeState::Leaving => write!(f, "Leaving"),
_ => write!(f, "Unknown"),
}
}
}
pub struct Node
{
nodeid: NodeId,
state: NodeState
}
impl fmt::Debug for Node {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
write!(f, "nodeid: {}, state: {:?}", self.nodeid, self.state)
}
}
bitflags! {
pub struct NodeInfoFlags: u32
{
const VOTEQUORUM_INFO_TWONODE = 1;
const VOTEQUORUM_INFO_QUORATE = 2;
const VOTEQUORUM_INFO_WAIT_FOR_ALL = 4;
const VOTEQUORUM_INFO_LAST_MAN_STANDING = 8;
const VOTEQUORUM_INFO_AUTO_TIE_BREAKER = 16;
const VOTEQUORUM_INFO_ALLOW_DOWNSCALE = 32;
const VOTEQUORUM_INFO_QDEVICE_REGISTERED = 64;
const VOTEQUORUM_INFO_QDEVICE_ALIVE = 128;
const VOTEQUORUM_INFO_QDEVICE_CAST_VOTE = 256;
const VOTEQUORUM_INFO_QDEVICE_MASTER_WINS = 512;
}
}
pub struct NodeInfo
{
pub node_id: NodeId,
pub node_state: NodeState,
pub node_votes: u32,
pub node_expected_votes: u32,
pub highest_expected: u32,
pub quorum: u32,
pub flags: NodeInfoFlags,
pub qdevice_votes: u32,
pub qdevice_name: String,
}
fn list_to_vec(list_entries: u32, list: *const u32) -> Vec<NodeId>
{
let mut r_member_list = Vec::<NodeId>::new();
let temp_members: &[u32] = unsafe { slice::from_raw_parts(list, list_entries as usize) };
for i in 0..list_entries as usize {
r_member_list.push(NodeId::from(temp_members[i]));
}
r_member_list
}
extern "C" fn rust_expectedvotes_notification_fn(
handle: ffi::votequorum_handle_t,
context: u64,
expected_votes: u32)
{
if let Some(h) = HANDLE_HASH.lock().unwrap().get(&handle) {
if let Some(cb) = h.callbacks.expectedvotes_notification_fn {
(cb)(h,
context,
expected_votes);
}
}
}
extern "C" fn rust_quorum_notification_fn(
handle: ffi::votequorum_handle_t,
context: u64,
quorate: u32,
node_list_entries: u32,
node_list: *mut ffi::votequorum_node_t)
{
if let Some(h) = HANDLE_HASH.lock().unwrap().get(&handle) {
let r_quorate = match quorate {
0 => false,
1 => true,
_ => false,
};
let mut r_node_list = Vec::<Node>::new();
let temp_members: &[ffi::votequorum_node_t] =
unsafe { slice::from_raw_parts(node_list, node_list_entries as usize) };
for i in 0..node_list_entries as usize {
r_node_list.push(Node{nodeid: NodeId::from(temp_members[i].nodeid),
state: NodeState::new(temp_members[i].state)} );
}
if let Some(cb) = h.callbacks.quorum_notification_fn {
(cb)(h,
context,
r_quorate,
r_node_list);
}
}
}
extern "C" fn rust_nodelist_notification_fn(
handle: ffi::votequorum_handle_t,
context: u64,
ring_id: ffi::votequorum_ring_id_t,
node_list_entries: u32,
node_list: *mut u32)
{
if let Some(h) = HANDLE_HASH.lock().unwrap().get(&handle) {
let r_ring_id = RingId{nodeid: NodeId::from(ring_id.nodeid),
seq: ring_id.seq};
let r_node_list = list_to_vec(node_list_entries, node_list);
if let Some(cb) = h.callbacks.nodelist_notification_fn {
(cb)(h,
context,
r_ring_id,
r_node_list);
}
}
}
#[derive(Copy, Clone)]
pub struct Callbacks {
pub quorum_notification_fn: Option<fn(hande: &Handle,
context: u64,
quorate: bool,
node_list: Vec<Node>)>,
pub nodelist_notification_fn: Option<fn(hande: &Handle,
context: u64,
ring_id: RingId,
node_list: Vec<NodeId>)>,
pub expectedvotes_notification_fn: Option<fn(handle: &Handle,
context: u64,
expected_votes: u32)>,
}
#[derive(Copy, Clone)]
pub struct Handle {
votequorum_handle: u64,
callbacks: Callbacks
}
pub fn initialize(callbacks: &Callbacks) -> Result<Handle>
{
let mut handle: ffi::votequorum_handle_t = 0;
let mut c_callbacks = ffi::votequorum_callbacks_t {
votequorum_quorum_notify_fn: Some(rust_quorum_notification_fn),
votequorum_nodelist_notify_fn: Some(rust_nodelist_notification_fn),
votequorum_expectedvotes_notify_fn: Some(rust_expectedvotes_notification_fn),
};
unsafe {
let res = ffi::votequorum_initialize(&mut handle,
&mut c_callbacks);
if res == ffi::CS_OK {
let rhandle = Handle{votequorum_handle: handle, callbacks: *callbacks};
HANDLE_HASH.lock().unwrap().insert(handle, rhandle);
Ok(rhandle)
} else {
Err(CsError::from_c(res))
}
}
}
pub fn finalize(handle: Handle) -> Result<()>
{
let res =
unsafe {
ffi::votequorum_finalize(handle.votequorum_handle)
};
if res == ffi::CS_OK {
HANDLE_HASH.lock().unwrap().remove(&handle.votequorum_handle);
Ok(())
} else {
Err(CsError::from_c(res))
}
}
pub fn fd_get(handle: Handle) -> Result<i32>
{
let c_fd: *mut c_int = &mut 0 as *mut _ as *mut c_int;
let res =
unsafe {
ffi::votequorum_fd_get(handle.votequorum_handle, c_fd)
};
if res == ffi::CS_OK {
Ok(c_fd as i32)
} else {
Err(CsError::from_c(res))
}
}
const VOTEQUORUM_QDEVICE_MAX_NAME_LEN : usize = 255;
pub fn get_info(handle: Handle, nodeid: NodeId) -> Result<NodeInfo>
{
let mut c_info = ffi::votequorum_info {
node_id: 0,
node_state:0,
node_votes: 0,
node_expected_votes:0,
highest_expected:0,
total_votes:0,
quorum:0,
flags:0,
qdevice_votes:0,
qdevice_name: [0; 255usize]
};
let res =
unsafe {
ffi::votequorum_getinfo(handle.votequorum_handle, u32::from(nodeid), &mut c_info)
};
if res == ffi::CS_OK {
let info = NodeInfo {
node_id : NodeId::from(c_info.node_id),
node_state : NodeState::new(c_info.node_state),
node_votes : c_info.node_votes,
node_expected_votes : c_info.node_expected_votes,
highest_expected : c_info.highest_expected,
quorum : c_info.quorum,
flags : NodeInfoFlags{bits: c_info.flags},
qdevice_votes : c_info.qdevice_votes,
qdevice_name : match string_from_bytes(c_info.qdevice_name.as_ptr(), VOTEQUORUM_QDEVICE_MAX_NAME_LEN) {
Ok(s) => s,
Err(_) => String::new()
},
};
Ok(info)
} else {
Err(CsError::from_c(res))
}
}
pub fn dispatch(handle: Handle, flags: DispatchFlags) -> Result<()>
{
let res =
unsafe {
ffi::votequorum_dispatch(handle.votequorum_handle, flags as u32)
};
if res == ffi::CS_OK {
Ok(())
} else {
Err(CsError::from_c(res))
}
}
pub fn trackstart(handle: Handle, context: u64, flags: TrackFlags) -> Result<()>
{
let res =
unsafe {
ffi::votequorum_trackstart(handle.votequorum_handle, context, flags as u32)
};
if res == ffi::CS_OK {
Ok(())
} else {
Err(CsError::from_c(res))
}
}
pub fn trackstop(handle: Handle) -> Result<()>
{
let res =
unsafe {
ffi::votequorum_trackstop(handle.votequorum_handle)
};
if res == ffi::CS_OK {
Ok(())
} else {
Err(CsError::from_c(res))
}
}
pub fn context_get(handle: Handle) -> Result<u64>
{
let (res, context) =
unsafe {
let mut c_context: *mut c_void = &mut 0u64 as *mut _ as *mut c_void;
let r = ffi::votequorum_context_get(handle.votequorum_handle, &mut c_context);
let context: u64 = c_context as u64;
(r, context)
};
if res == ffi::CS_OK {
Ok(context)
} else {
Err(CsError::from_c(res))
}
}
pub fn context_set(handle: Handle, context: u64) -> Result<()>
{
let res =
unsafe {
let c_context = context as *mut c_void;
ffi::votequorum_context_set(handle.votequorum_handle, c_context)
};
if res == ffi::CS_OK {
Ok(())
} else {
Err(CsError::from_c(res))
}
}
pub fn set_expected(handle: Handle, expected_votes: u32) -> Result<()>
{
let res =
unsafe {
ffi::votequorum_setexpected(handle.votequorum_handle, expected_votes)
};
if res == ffi::CS_OK {
Ok(())
} else {
Err(CsError::from_c(res))
}
}
pub fn set_votes(handle: Handle, nodeid: NodeId, votes: u32) -> Result<()>
{
let res =
unsafe {
ffi::votequorum_setvotes(handle.votequorum_handle, u32::from(nodeid), votes)
};
if res == ffi::CS_OK {
Ok(())
} else {
Err(CsError::from_c(res))
}
}
pub fn qdevice_register(handle: Handle, name: &str) -> Result<()>
{
let c_string = {
match CString::new(name) {
Ok(cs) => cs,
Err(_) => return Err(CsError::CsErrInvalidParam),
}
};
let res =
unsafe {
ffi::votequorum_qdevice_register(handle.votequorum_handle, c_string. as_ptr())
};
if res == ffi::CS_OK {
Ok(())
} else {
Err(CsError::from_c(res))
}
}
pub fn qdevice_unregister(handle: Handle, name: &str) -> Result<()>
{
let c_string = {
match CString::new(name) {
Ok(cs) => cs,
Err(_) => return Err(CsError::CsErrInvalidParam),
}
};
let res =
unsafe {
ffi::votequorum_qdevice_unregister(handle.votequorum_handle, c_string. as_ptr())
};
if res == ffi::CS_OK {
Ok(())
} else {
Err(CsError::from_c(res))
}
}
pub fn qdevice_update(handle: Handle, oldname: &str, newname: &str) -> Result<()>
{
let on_string = {
match CString::new(oldname) {
Ok(cs) => cs,
Err(_) => return Err(CsError::CsErrInvalidParam),
}
};
let nn_string = {
match CString::new(newname) {
Ok(cs) => cs,
Err(_) => return Err(CsError::CsErrInvalidParam),
}
};
let res =
unsafe {
ffi::votequorum_qdevice_update(handle.votequorum_handle, on_string.as_ptr(), nn_string.as_ptr())
};
if res == ffi::CS_OK {
Ok(())
} else {
Err(CsError::from_c(res))
}
}
pub fn qdevice_poll(handle: Handle, name: &str, cast_vote: bool, ring_id: &RingId) -> Result<()>
{
let c_string = {
match CString::new(name) {
Ok(cs) => cs,
Err(_) => return Err(CsError::CsErrInvalidParam),
}
};
let c_cast_vote : u32 = if cast_vote {1} else {0};
let c_ring_id = ffi::votequorum_ring_id_t {
nodeid: u32::from(ring_id.nodeid),
seq: ring_id.seq};
let res =
unsafe {
ffi::votequorum_qdevice_poll(handle.votequorum_handle, c_string.as_ptr(), c_cast_vote, c_ring_id)
};
if res == ffi::CS_OK {
Ok(())
} else {
Err(CsError::from_c(res))
}
}
pub fn qdevice_master_wins(handle: Handle, name: &str, master_wins: bool) -> Result<()>
{
let c_string = {
match CString::new(name) {
Ok(cs) => cs,
Err(_) => return Err(CsError::CsErrInvalidParam),
}
};
let c_master_wins : u32 = if master_wins {1} else {0};
let res =
unsafe {
ffi::votequorum_qdevice_master_wins(handle.votequorum_handle, c_string.as_ptr(), c_master_wins)
};
if res == ffi::CS_OK {
Ok(())
} else {
Err(CsError::from_c(res))
}
}