File: DrbdVolume.cpp

package info (click to toggle)
drbd-utils 9.5.0-1
  • links: PTS, VCS
  • area: main
  • in suites: buster
  • size: 5,388 kB
  • sloc: ansic: 43,698; xml: 15,968; cpp: 7,783; sh: 3,699; makefile: 1,353; perl: 353
file content (640 lines) | stat: -rw-r--r-- 18,390 bytes parent folder | download
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
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
#include <DrbdVolume.h>
#include <DrbdConnection.h>
#include <utils.h>

const std::string DrbdVolume::PROP_KEY_VOL_NR      = "volume";
const std::string DrbdVolume::PROP_KEY_MINOR       = "minor";
const std::string DrbdVolume::PROP_KEY_DISK        = "disk";
const std::string DrbdVolume::PROP_KEY_PEER_DISK   = "peer-disk";
const std::string DrbdVolume::PROP_KEY_REPLICATION = "replication";
const std::string DrbdVolume::PROP_KEY_CLIENT      = "client";
const std::string DrbdVolume::PROP_KEY_PEER_CLIENT = "peer-client";
const std::string DrbdVolume::PROP_KEY_QUORUM      = "quorum";

const char* DrbdVolume::DS_LABEL_DISKLESS     = "Diskless";
const char* DrbdVolume::DS_LABEL_ATTACHING    = "Attaching";
const char* DrbdVolume::DS_LABEL_DETACHING    = "Detaching";
const char* DrbdVolume::DS_LABEL_FAILED       = "Failed";
const char* DrbdVolume::DS_LABEL_NEGOTIATING  = "Negotiating";
const char* DrbdVolume::DS_LABEL_INCONSISTENT = "Inconsistent";
const char* DrbdVolume::DS_LABEL_OUTDATED     = "Outdated";
const char* DrbdVolume::DS_LABEL_UNKNOWN      = "DUnknown";
const char* DrbdVolume::DS_LABEL_CONSISTENT   = "Consistent";
const char* DrbdVolume::DS_LABEL_UP_TO_DATE   = "UpToDate";

const char* DrbdVolume::RS_LABEL_OFF                  = "Off";
const char* DrbdVolume::RS_LABEL_ESTABLISHED          = "Established";
const char* DrbdVolume::RS_LABEL_STARTING_SYNC_SOURCE = "StartingSyncS";
const char* DrbdVolume::RS_LABEL_STARTING_SYNC_TARGET = "StartingSyncT";
const char* DrbdVolume::RS_LABEL_WF_BITMAP_SOURCE     = "WFBitMapS";
const char* DrbdVolume::RS_LABEL_WF_BITMAP_TARGET     = "WFBitMapT";
const char* DrbdVolume::RS_LABEL_WF_SYNC_UUID         = "WFSyncUUID";
const char* DrbdVolume::RS_LABEL_SYNC_SOURCE          = "SyncSource";
const char* DrbdVolume::RS_LABEL_SYNC_TARGET          = "SyncTarget";
const char* DrbdVolume::RS_LABEL_PAUSED_SYNC_SOURCE   = "PausedSyncS";
const char* DrbdVolume::RS_LABEL_PAUSED_SYNC_TARGET   = "PausedSyncT";
const char* DrbdVolume::RS_LABEL_VERIFY_SOURCE        = "VerifyS";
const char* DrbdVolume::RS_LABEL_VERIFY_TARGET        = "VerifyT";
const char* DrbdVolume::RS_LABEL_AHEAD                = "Ahead";
const char* DrbdVolume::RS_LABEL_BEHIND               = "Behind";
const char* DrbdVolume::RS_LABEL_UNKNOWN              = "Unknown";

const char* DrbdVolume::CS_LABEL_ENABLED              = "yes";
const char* DrbdVolume::CS_LABEL_DISABLED             = "no";
const char* DrbdVolume::CS_LABEL_UNKNOWN              = "unknown";

const char* DrbdVolume::QU_LABEL_PRESENT              = "yes";
const char* DrbdVolume::QU_LABEL_LOST                 = "no";

DrbdVolume::DrbdVolume(uint16_t volume_nr) :
    vol_nr(volume_nr)
{
    minor_nr = -1;
    vol_disk_state = DrbdVolume::disk_state::UNKNOWN;
    vol_repl_state = DrbdVolume::repl_state::UNKNOWN;
    vol_client_state = DrbdVolume::client_state::UNKNOWN;
}

const uint16_t DrbdVolume::get_volume_nr() const
{
    return vol_nr;
}

// @throws EventMessageException
void DrbdVolume::update(PropsMap& event_props)
{
    std::string* prop_disk = event_props.get(&PROP_KEY_DISK);
    if (prop_disk == nullptr)
    {
        prop_disk = event_props.get(&PROP_KEY_PEER_DISK);
    }

    if (prop_disk != nullptr)
    {
        vol_disk_state = parse_disk_state(*prop_disk);
    }

    std::string* prop_replication = event_props.get(&PROP_KEY_REPLICATION);
    if (prop_replication != nullptr)
    {
        vol_repl_state = parse_repl_state(*prop_replication);
    }

    std::string* minor_nr_str = event_props.get(&PROP_KEY_MINOR);
    if (minor_nr_str != nullptr)
    {
        try
        {
            minor_nr = DrbdVolume::parse_minor_nr(*minor_nr_str);
        }
        catch (NumberFormatException& nf_exc)
        {
            throw EventMessageException();
        }
    }

    std::string* prop_client = event_props.get(&PROP_KEY_CLIENT);
    if (prop_client == nullptr)
    {
        prop_client = event_props.get(&PROP_KEY_PEER_CLIENT);
    }

    if (prop_client != nullptr)
    {
        vol_client_state = parse_client_state(*prop_client);
    }

    std::string* prop_quorum = event_props.get(&PROP_KEY_QUORUM);
    if (prop_quorum != nullptr)
    {
        quorum_alert = !parse_quorum_state(*prop_quorum);
    }
}

int32_t DrbdVolume::get_minor_nr() const
{
    return minor_nr;
}

// @throws EventMessageException
void DrbdVolume::set_minor_nr(int32_t value)
{
    if (value >= -1 && value < 0x100000)
    {
        minor_nr = value;
    }
    else
    {
        throw EventMessageException();
    }
}

DrbdVolume::disk_state DrbdVolume::get_disk_state() const
{
    return vol_disk_state;
}

const char* DrbdVolume::get_disk_state_label() const
{
    const char* label = DS_LABEL_UNKNOWN;
    switch (vol_disk_state)
    {
        case DrbdVolume::disk_state::ATTACHING:
            label = DS_LABEL_ATTACHING;
            break;
        case DrbdVolume::disk_state::DETACHING:
            label = DS_LABEL_DETACHING;
            break;
        case DrbdVolume::disk_state::CONSISTENT:
            label = DS_LABEL_CONSISTENT;
            break;
        case DrbdVolume::disk_state::DISKLESS:
            label = DS_LABEL_DISKLESS;
            break;
        case DrbdVolume::disk_state::FAILED:
            label = DS_LABEL_FAILED;
            break;
        case DrbdVolume::disk_state::INCONSISTENT:
            label = DS_LABEL_INCONSISTENT;
            break;
        case DrbdVolume::disk_state::NEGOTIATING:
            label = DS_LABEL_NEGOTIATING;
            break;
        case DrbdVolume::disk_state::OUTDATED:
            label = DS_LABEL_OUTDATED;
            break;
        case DrbdVolume::disk_state::UP_TO_DATE:
            label = DS_LABEL_UP_TO_DATE;
            break;
        case DrbdVolume::disk_state::UNKNOWN:
            // fall-through
        default:
            break;
    }
    return label;
}

DrbdVolume::repl_state DrbdVolume::get_replication_state() const
{
    return vol_repl_state;
}

const char* DrbdVolume::get_replication_state_label() const
{
    const char* label = RS_LABEL_UNKNOWN;
    switch (vol_repl_state)
    {
        case DrbdVolume::repl_state::AHEAD:
            label = RS_LABEL_AHEAD;
            break;
        case DrbdVolume::repl_state::BEHIND:
            label = RS_LABEL_BEHIND;
            break;
        case DrbdVolume::repl_state::ESTABLISHED:
            label = RS_LABEL_ESTABLISHED;
            break;
        case DrbdVolume::repl_state::OFF:
            label = RS_LABEL_OFF;
            break;
        case DrbdVolume::repl_state::PAUSED_SYNC_SOURCE:
            label = RS_LABEL_PAUSED_SYNC_SOURCE;
            break;
        case DrbdVolume::repl_state::PAUSED_SYNC_TARGET:
            label = RS_LABEL_PAUSED_SYNC_TARGET;
            break;
        case DrbdVolume::repl_state::STARTING_SYNC_SOURCE:
            label = RS_LABEL_STARTING_SYNC_SOURCE;
            break;
        case DrbdVolume::repl_state::STARTING_SYNC_TARGET:
            label = RS_LABEL_STARTING_SYNC_TARGET;
            break;
        case DrbdVolume::repl_state::SYNC_SOURCE:
            label = RS_LABEL_SYNC_SOURCE;
            break;
        case DrbdVolume::repl_state::SYNC_TARGET:
            label = RS_LABEL_SYNC_TARGET;
            break;
        case DrbdVolume::repl_state::VERIFY_SOURCE:
            label = RS_LABEL_VERIFY_SOURCE;
            break;
        case DrbdVolume::repl_state::VERIFY_TARGET:
            label = RS_LABEL_VERIFY_TARGET;
            break;
        case DrbdVolume::repl_state::WF_BITMAP_SOURCE:
            label = RS_LABEL_WF_BITMAP_SOURCE;
            break;
        case DrbdVolume::repl_state::WF_BITMAP_TARGET:
            label = RS_LABEL_WF_BITMAP_TARGET;
            break;
        case DrbdVolume::repl_state::WF_SYNC_UUID:
            label = RS_LABEL_WF_SYNC_UUID;
            break;
        case DrbdVolume::repl_state::UNKNOWN:
            // fall-through
        default:
            break;
    }
    return label;
}

void DrbdVolume::set_connection(DrbdConnection* conn)
{
    connection = conn;
}

bool DrbdVolume::has_disk_alert()
{
    return disk_alert;
}

bool DrbdVolume::has_replication_warning()
{
    return repl_warn || repl_alert;
}

bool DrbdVolume::has_replication_alert()
{
    return repl_alert;
}

bool DrbdVolume::has_quorum_alert()
{
    return quorum_alert;
}

void DrbdVolume::clear_state_flags()
{
    disk_alert = false;
    repl_warn  = false;
    repl_alert = false;
    StateFlags::clear_state_flags();
}

StateFlags::state DrbdVolume::update_state_flags()
{
    // Reset the state to normal
    // quorum_alert is set directly by update()
    StateFlags::clear_state_flags();
    disk_alert = false;
    repl_warn  = false;
    repl_alert = false;

    // Check the volume's disk state
    switch (vol_disk_state)
    {
        case DrbdVolume::disk_state::UP_TO_DATE:
            // UpToDate disk, no alert
            break;
        case DrbdVolume::disk_state::DISKLESS:
            // If the volume is not configured as a diskless DRBD client,
            // then trigger a disk alert
            if (vol_client_state != DrbdVolume::client_state::ENABLED)
            {
                disk_alert = true;
                set_alert();
            }
            break;
        case DrbdVolume::disk_state::UNKNOWN:
            if (connection == nullptr)
            {
                // Volume is local, always issue an alert for
                // an unknown disk state
                disk_alert = true;
                set_alert();
            }
            else
            {
                // Volume is a peer volume, if the connection
                // to the peer is faulty, do not issue an alert for
                // an unknown disk state
                if (!connection->has_warn_state())
                {
                    disk_alert = true;
                    set_alert();
                }
            }
            break;
        case DrbdVolume::disk_state::ATTACHING:
            // fall-through
        case DrbdVolume::disk_state::CONSISTENT:
            // fall-through
        case DrbdVolume::disk_state::DETACHING:
            // fall-through
        case DrbdVolume::disk_state::FAILED:
            // fall-through
        case DrbdVolume::disk_state::INCONSISTENT:
            // fall-through
        case DrbdVolume::disk_state::NEGOTIATING:
            // fall-through
        case DrbdVolume::disk_state::OUTDATED:
            // fall-through
        default:
            disk_alert = true;
            set_alert();
            break;
    }

    // Check the volume's replication state
    switch (vol_repl_state)
    {
        case DrbdVolume::repl_state::ESTABLISHED:
            // no warning, no alert
            break;
        case DrbdVolume::repl_state::PAUSED_SYNC_SOURCE:
            // fall-through
        case DrbdVolume::repl_state::PAUSED_SYNC_TARGET:
            // fall-through
        case DrbdVolume::repl_state::STARTING_SYNC_SOURCE:
            // fall-through
        case DrbdVolume::repl_state::STARTING_SYNC_TARGET:
            // fall-through
        case DrbdVolume::repl_state::SYNC_SOURCE:
            // fall-through
        case DrbdVolume::repl_state::SYNC_TARGET:
            // fall-through
        case DrbdVolume::repl_state::VERIFY_SOURCE:
            // fall-through
        case DrbdVolume::repl_state::VERIFY_TARGET:
            // fall-through
        case DrbdVolume::repl_state::WF_BITMAP_SOURCE:
            // fall-through
        case DrbdVolume::repl_state::WF_BITMAP_TARGET:
            // fall-through
        case DrbdVolume::repl_state::WF_SYNC_UUID:
            repl_warn = true;
            set_warn();
            break;
        case DrbdVolume::repl_state::UNKNOWN:
            // fall-through
        case DrbdVolume::repl_state::OFF:
            if (connection != nullptr)
            {
                // Volume is a peer volume, if the connection
                // to the peer is faulty, do not issue alerts for
                // inoperative replication
                if (!connection->has_warn_state())
                {
                    repl_alert = true;
                    set_alert();
                }
            }
            // No alert is issued for local (non-peer) volumes,
            // because those always have 'Unknown' replication state
            break;
        case DrbdVolume::repl_state::AHEAD:
            // fall-through
        case DrbdVolume::repl_state::BEHIND:
            // fall-through
        default:
            repl_alert = true;
            set_alert();
            break;
    }

    // Set alert status on the volume if the quorum has been lost
    if (quorum_alert)
    {
        set_alert();
    }

    return obj_state;
}

StateFlags::state DrbdVolume::child_state_flags_changed()
{
    // No-op, the DrbdVolume does not have child objects

    return StateFlags::state::NORM;
}

// @throws EventMessageException
DrbdVolume::disk_state DrbdVolume::parse_disk_state(std::string& state_name)
{
    DrbdVolume::disk_state state = DrbdVolume::disk_state::UNKNOWN;

    if (state_name == DS_LABEL_DISKLESS)
    {
        state = DrbdVolume::disk_state::DISKLESS;
    }
    else
    if (state_name == DS_LABEL_ATTACHING)
    {
        state = DrbdVolume::disk_state::ATTACHING;
    }
    else
    if (state_name == DS_LABEL_DETACHING)
    {
        state = DrbdVolume::disk_state::DETACHING;
    }
    else
    if (state_name == DS_LABEL_FAILED)
    {
        state = DrbdVolume::disk_state::FAILED;
    }
    else
    if (state_name == DS_LABEL_NEGOTIATING)
    {
        state = DrbdVolume::disk_state::NEGOTIATING;
    }
    else
    if (state_name == DS_LABEL_INCONSISTENT)
    {
        state = DrbdVolume::disk_state::INCONSISTENT;
    }
    else
    if (state_name == DS_LABEL_OUTDATED)
    {
        state = DrbdVolume::disk_state::OUTDATED;
    }
    else
    if (state_name == DS_LABEL_CONSISTENT)
    {
        state = DrbdVolume::disk_state::CONSISTENT;
    }
    else
    if (state_name == DS_LABEL_UP_TO_DATE)
    {
        state = DrbdVolume::disk_state::UP_TO_DATE;
    }
    else
    if (state_name != DS_LABEL_UNKNOWN)
    {
        throw EventMessageException();
    }

    return state;
}

// @throws EventMessageException
DrbdVolume::repl_state DrbdVolume::parse_repl_state(std::string& state_name)
{
    DrbdVolume::repl_state state = DrbdVolume::repl_state::UNKNOWN;

    if (state_name == RS_LABEL_AHEAD)
    {
        state = DrbdVolume::repl_state::AHEAD;
    }
    else
    if (state_name == RS_LABEL_BEHIND)
    {
        state = DrbdVolume::repl_state::BEHIND;
    }
    else
    if (state_name == RS_LABEL_ESTABLISHED)
    {
        state = DrbdVolume::repl_state::ESTABLISHED;
    }
    else
    if (state_name == RS_LABEL_OFF)
    {
        state = DrbdVolume::repl_state::OFF;
    }
    else
    if (state_name == RS_LABEL_PAUSED_SYNC_SOURCE)
    {
        state = DrbdVolume::repl_state::PAUSED_SYNC_SOURCE;
    }
    else
    if (state_name == RS_LABEL_PAUSED_SYNC_TARGET)
    {
        state = DrbdVolume::repl_state::PAUSED_SYNC_TARGET;
    }
    else
    if (state_name == RS_LABEL_STARTING_SYNC_SOURCE)
    {
        state = DrbdVolume::repl_state::STARTING_SYNC_SOURCE;
    }
    else
    if (state_name == RS_LABEL_STARTING_SYNC_TARGET)
    {
        state = DrbdVolume::repl_state::STARTING_SYNC_TARGET;
    }
    else
    if (state_name == RS_LABEL_SYNC_SOURCE)
    {
        state = DrbdVolume::repl_state::SYNC_SOURCE;
    }
    else
    if (state_name == RS_LABEL_SYNC_TARGET)
    {
        state = DrbdVolume::repl_state::SYNC_TARGET;
    }
    else
    if (state_name == RS_LABEL_VERIFY_SOURCE)
    {
        state = DrbdVolume::repl_state::VERIFY_SOURCE;
    }
    else
    if (state_name == RS_LABEL_VERIFY_TARGET)
    {
        state = DrbdVolume::repl_state::VERIFY_TARGET;
    }
    else
    if (state_name == RS_LABEL_WF_BITMAP_SOURCE)
    {
        state = DrbdVolume::repl_state::WF_BITMAP_SOURCE;
    }
    else
    if (state_name == RS_LABEL_WF_BITMAP_TARGET)
    {
        state = DrbdVolume::repl_state::WF_BITMAP_TARGET;
    }
    else
    if (state_name == RS_LABEL_WF_SYNC_UUID)
    {
        state = DrbdVolume::repl_state::WF_SYNC_UUID;
    }
    else
    if (state_name != RS_LABEL_UNKNOWN)
    {
        throw EventMessageException();
    }

    return state;
}

// @throws EventMessageException
DrbdVolume::client_state DrbdVolume::parse_client_state(std::string& value_str)
{
    DrbdVolume::client_state state = DrbdVolume::client_state::UNKNOWN;

    if (value_str == CS_LABEL_DISABLED)
    {
        state = DrbdVolume::client_state::DISABLED;
    }
    else
    if (value_str == CS_LABEL_ENABLED)
    {
        state = DrbdVolume::client_state::ENABLED;
    }
    else
    if (value_str != CS_LABEL_UNKNOWN)
    {
        throw EventMessageException();
    }

    return state;
}

// @throws EventMessageException
bool DrbdVolume::parse_quorum_state(std::string& value_str)
{
    bool quorum_present {false};

    if (value_str == QU_LABEL_PRESENT)
    {
        quorum_present = true;
    }
    else
    if (value_str != QU_LABEL_LOST)
    {
        throw EventMessageException();
    }

    return quorum_present;
}


// @throws NumberFormatException
uint16_t DrbdVolume::parse_volume_nr(std::string& value_str)
{
    uint16_t value {0};
    value = NumberParser::parse_uint16(value_str);
    return value;
}

// @throws NumberFormatException
int32_t DrbdVolume::parse_minor_nr(std::string& value_str)
{
    int32_t value {-1};
    value = NumberParser::parse_int32(value_str);
    return value;
}

// Creates (allocates and initializes) a new DrbdVolume object from a map of properties
//
// @param event_props Reference to the map of properties from a 'drbdsetup events2' line
// @return Pointer to a newly created DrbdVolume object
// @throws std::bad_alloc, EventMessageException
DrbdVolume* DrbdVolume::new_from_props(PropsMap& event_props)
{
    DrbdVolume* vol {nullptr};
    std::string* number_str = event_props.get(&PROP_KEY_VOL_NR);
    if (number_str != nullptr)
    {
        try
        {
            uint16_t vol_nr = NumberParser::parse_uint16(*number_str);
            vol = new DrbdVolume(vol_nr);
        }
        catch (NumberFormatException& nf_exc)
        {
            // no-op
        }
    }
    if (vol == nullptr)
    {
        throw EventMessageException();
    }
    return vol;
}