crushtool 0.3.10

A library to encode and decode Ceph's crushmap
Documentation
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
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
extern crate nom;
extern crate crushtool;
use crushtool::{CrushMap, BucketTypes, CrushBucketStraw, CrushBucketStraw2, OpCode, BucketAlg,
                CrushRuleStep, Bucket, CrushRuleMask, CrushHash, Rule, RuleType, decode_crushmap,
                encode_crushmap, set_tunables_jewel, set_tunables_argonaut, set_tunables_bobtail,
                set_tunables_firefly, set_tunables_hammer};

fn get_crushmap() -> CrushMap {
    CrushMap {
        magic: 65536,
        max_buckets: 8,
        max_rules: 1,
        max_devices: 3,
        buckets: vec![],
        rules: vec![],
        type_map: vec![(0, "osd".to_string()),
                       (1, "host".to_string()),
                       (2, "chassis".to_string()),
                       (3, "rack".to_string()),
                       (4, "row".to_string()),
                       (5, "pdu".to_string()),
                       (6, "pod".to_string()),
                       (7, "room".to_string()),
                       (8, "datacenter".to_string()),
                       (9, "region".to_string()),
                       (10, "root".to_string())],
        name_map: vec![],
        rule_name_map: vec![],
        choose_local_tries: Some(0),
        choose_local_fallback_tries: Some(0),
        choose_total_tries: Some(0),
        chooseleaf_descend_once: Some(0),
        chooseleaf_vary_r: Some(0),
        straw_calc_version: Some(0),
        allowed_bucket_algorithms: Some(0),
        chooseleaf_stable: Some(1),
    }
}

#[test]
fn it_sets_tunables_for_argonaut() {
    let mut crushmap = get_crushmap();
    let expected = CrushMap {
        magic: 65536,
        max_buckets: 8,
        max_rules: 1,
        max_devices: 3,
        buckets: vec![],
        rules: vec![],
        type_map: vec![(0, "osd".to_string()),
                       (1, "host".to_string()),
                       (2, "chassis".to_string()),
                       (3, "rack".to_string()),
                       (4, "row".to_string()),
                       (5, "pdu".to_string()),
                       (6, "pod".to_string()),
                       (7, "room".to_string()),
                       (8, "datacenter".to_string()),
                       (9, "region".to_string()),
                       (10, "root".to_string())],
        name_map: vec![],
        rule_name_map: vec![],
        choose_local_tries: Some(2),
        choose_local_fallback_tries: Some(5),
        choose_total_tries: Some(19),
        chooseleaf_descend_once: Some(0),
        chooseleaf_vary_r: Some(0),
        straw_calc_version: Some(0),
        allowed_bucket_algorithms: Some(22),
        chooseleaf_stable: Some(0),
    };
    set_tunables_argonaut(&mut crushmap);
    assert_eq!(expected, crushmap);
}

#[test]
fn it_sets_tunables_for_bobtail() {
    let mut crushmap = get_crushmap();
    let expected = CrushMap {
        magic: 65536,
        max_buckets: 8,
        max_rules: 1,
        max_devices: 3,
        buckets: vec![],
        rules: vec![],
        type_map: vec![(0, "osd".to_string()),
                       (1, "host".to_string()),
                       (2, "chassis".to_string()),
                       (3, "rack".to_string()),
                       (4, "row".to_string()),
                       (5, "pdu".to_string()),
                       (6, "pod".to_string()),
                       (7, "room".to_string()),
                       (8, "datacenter".to_string()),
                       (9, "region".to_string()),
                       (10, "root".to_string())],
        name_map: vec![],
        rule_name_map: vec![],
        choose_local_tries: Some(0),
        choose_local_fallback_tries: Some(0),
        choose_total_tries: Some(50),
        chooseleaf_descend_once: Some(1),
        chooseleaf_vary_r: Some(0),
        straw_calc_version: Some(0),
        allowed_bucket_algorithms: Some(22),
        chooseleaf_stable: Some(0),
    };
    set_tunables_bobtail(&mut crushmap);
    assert_eq!(expected, crushmap);
}

#[test]
fn it_sets_tunables_for_firefly() {
    let mut crushmap = get_crushmap();
    let expected = CrushMap {
        magic: 65536,
        max_buckets: 8,
        max_rules: 1,
        max_devices: 3,
        buckets: vec![],
        rules: vec![],
        type_map: vec![(0, "osd".to_string()),
                       (1, "host".to_string()),
                       (2, "chassis".to_string()),
                       (3, "rack".to_string()),
                       (4, "row".to_string()),
                       (5, "pdu".to_string()),
                       (6, "pod".to_string()),
                       (7, "room".to_string()),
                       (8, "datacenter".to_string()),
                       (9, "region".to_string()),
                       (10, "root".to_string())],
        name_map: vec![],
        rule_name_map: vec![],
        choose_local_tries: Some(0),
        choose_local_fallback_tries: Some(0),
        choose_total_tries: Some(50),
        chooseleaf_descend_once: Some(1),
        chooseleaf_vary_r: Some(1),
        straw_calc_version: Some(0),
        allowed_bucket_algorithms: Some(22),
        chooseleaf_stable: Some(0),
    };
    set_tunables_firefly(&mut crushmap);
    assert_eq!(expected, crushmap);
}

#[test]
fn it_sets_tunables_for_hammer() {
    let mut crushmap = get_crushmap();
    let expected = CrushMap {
        magic: 65536,
        max_buckets: 8,
        max_rules: 1,
        max_devices: 3,
        buckets: vec![],
        rules: vec![],
        type_map: vec![(0, "osd".to_string()),
                       (1, "host".to_string()),
                       (2, "chassis".to_string()),
                       (3, "rack".to_string()),
                       (4, "row".to_string()),
                       (5, "pdu".to_string()),
                       (6, "pod".to_string()),
                       (7, "room".to_string()),
                       (8, "datacenter".to_string()),
                       (9, "region".to_string()),
                       (10, "root".to_string())],
        name_map: vec![],
        rule_name_map: vec![],
        choose_local_tries: Some(0),
        choose_local_fallback_tries: Some(0),
        choose_total_tries: Some(50),
        chooseleaf_descend_once: Some(1),
        chooseleaf_vary_r: Some(1),
        straw_calc_version: Some(0),
        allowed_bucket_algorithms: Some(54),
        chooseleaf_stable: Some(0),
    };
    set_tunables_hammer(&mut crushmap);
    assert_eq!(expected, crushmap);
}

#[test]
fn it_sets_tunables_for_jewel() {
    let mut crushmap = get_crushmap();
    let expected = CrushMap {
        magic: 65536,
        max_buckets: 8,
        max_rules: 1,
        max_devices: 3,
        buckets: vec![],
        rules: vec![],
        type_map: vec![(0, "osd".to_string()),
                       (1, "host".to_string()),
                       (2, "chassis".to_string()),
                       (3, "rack".to_string()),
                       (4, "row".to_string()),
                       (5, "pdu".to_string()),
                       (6, "pod".to_string()),
                       (7, "room".to_string()),
                       (8, "datacenter".to_string()),
                       (9, "region".to_string()),
                       (10, "root".to_string())],
        name_map: vec![],
        rule_name_map: vec![],
        choose_local_tries: Some(0),
        choose_local_fallback_tries: Some(0),
        choose_total_tries: Some(50),
        chooseleaf_descend_once: Some(1),
        chooseleaf_vary_r: Some(1),
        straw_calc_version: Some(0),
        allowed_bucket_algorithms: Some(54),
        chooseleaf_stable: Some(1),
    };
    set_tunables_jewel(&mut crushmap);
    assert_eq!(expected, crushmap);
}

#[test]
fn it_gets_next_id_correctly() {
    let crushmap = CrushMap::default();
    assert_eq!(crushmap.next_bucket_id(), -1);
}

#[test]
fn test_decode_crushmap() {
    let crushmap_compiled: &[u8] =
        &[0x00, 0x00, 0x01, 0x00, 0x08, 0x00, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00, 0x03, 0x00,
          0x00, 0x00, 0x04, 0x00, 0x00, 0x00, 0xff, 0xff, 0xff, 0xff, 0x0a, 0x00, 0x04, 0x00,
          0x00, 0x00, 0x00, 0x00, 0x03, 0x00, 0x00, 0x00, 0xfe, 0xff, 0xff, 0xff, 0xfd, 0xff,
          0xff, 0xff, 0xfc, 0xff, 0xff, 0xff, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
          0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
          0x00, 0x00, 0x04, 0x00, 0x00, 0x00, 0xfe, 0xff, 0xff, 0xff, 0x01, 0x00, 0x04, 0x00,
          0x00, 0x00, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
          0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x04, 0x00, 0x00, 0x00, 0xfd, 0xff, 0xff, 0xff,
          0x01, 0x00, 0x04, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00, 0x01, 0x00,
          0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x04, 0x00, 0x00, 0x00,
          0xfc, 0xff, 0xff, 0xff, 0x01, 0x00, 0x04, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01, 0x00,
          0x00, 0x00, 0x02, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
          0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
          0x00, 0x00, 0x01, 0x00, 0x00, 0x00, 0x03, 0x00, 0x00, 0x00, 0x00, 0x01, 0x01, 0x0a,
          0x01, 0x00, 0x00, 0x00, 0xff, 0xff, 0xff, 0xff, 0x00, 0x00, 0x00, 0x00, 0x06, 0x00,
          0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00, 0x04, 0x00, 0x00, 0x00,
          0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x0b, 0x00, 0x00, 0x00, 0x00, 0x00,
          0x00, 0x00, 0x03, 0x00, 0x00, 0x00, 0x6f, 0x73, 0x64, 0x01, 0x00, 0x00, 0x00, 0x04,
          0x00, 0x00, 0x00, 0x68, 0x6f, 0x73, 0x74, 0x02, 0x00, 0x00, 0x00, 0x07, 0x00, 0x00,
          0x00, 0x63, 0x68, 0x61, 0x73, 0x73, 0x69, 0x73, 0x03, 0x00, 0x00, 0x00, 0x04, 0x00,
          0x00, 0x00, 0x72, 0x61, 0x63, 0x6b, 0x04, 0x00, 0x00, 0x00, 0x03, 0x00, 0x00, 0x00,
          0x72, 0x6f, 0x77, 0x05, 0x00, 0x00, 0x00, 0x03, 0x00, 0x00, 0x00, 0x70, 0x64, 0x75,
          0x06, 0x00, 0x00, 0x00, 0x03, 0x00, 0x00, 0x00, 0x70, 0x6f, 0x64, 0x07, 0x00, 0x00,
          0x00, 0x04, 0x00, 0x00, 0x00, 0x72, 0x6f, 0x6f, 0x6d, 0x08, 0x00, 0x00, 0x00, 0x0a,
          0x00, 0x00, 0x00, 0x64, 0x61, 0x74, 0x61, 0x63, 0x65, 0x6e, 0x74, 0x65, 0x72, 0x09,
          0x00, 0x00, 0x00, 0x06, 0x00, 0x00, 0x00, 0x72, 0x65, 0x67, 0x69, 0x6f, 0x6e, 0x0a,
          0x00, 0x00, 0x00, 0x04, 0x00, 0x00, 0x00, 0x72, 0x6f, 0x6f, 0x74, 0x07, 0x00, 0x00,
          0x00, 0xfc, 0xff, 0xff, 0xff, 0x0e, 0x00, 0x00, 0x00, 0x69, 0x70, 0x2d, 0x31, 0x37,
          0x32, 0x2d, 0x33, 0x31, 0x2d, 0x34, 0x2d, 0x35, 0x36, 0xfd, 0xff, 0xff, 0xff, 0x0e,
          0x00, 0x00, 0x00, 0x69, 0x70, 0x2d, 0x31, 0x37, 0x32, 0x2d, 0x33, 0x31, 0x2d, 0x32,
          0x32, 0x2d, 0x32, 0xfe, 0xff, 0xff, 0xff, 0x10, 0x00, 0x00, 0x00, 0x69, 0x70, 0x2d,
          0x31, 0x37, 0x32, 0x2d, 0x33, 0x31, 0x2d, 0x34, 0x33, 0x2d, 0x31, 0x34, 0x37, 0xff,
          0xff, 0xff, 0xff, 0x07, 0x00, 0x00, 0x00, 0x64, 0x65, 0x66, 0x61, 0x75, 0x6c, 0x74,
          0x00, 0x00, 0x00, 0x00, 0x05, 0x00, 0x00, 0x00, 0x6f, 0x73, 0x64, 0x2e, 0x30, 0x01,
          0x00, 0x00, 0x00, 0x05, 0x00, 0x00, 0x00, 0x6f, 0x73, 0x64, 0x2e, 0x31, 0x02, 0x00,
          0x00, 0x00, 0x05, 0x00, 0x00, 0x00, 0x6f, 0x73, 0x64, 0x2e, 0x32, 0x01, 0x00, 0x00,
          0x00, 0x00, 0x00, 0x00, 0x00, 0x12, 0x00, 0x00, 0x00, 0x72, 0x65, 0x70, 0x6c, 0x69,
          0x63, 0x61, 0x74, 0x65, 0x64, 0x5f, 0x72, 0x75, 0x6c, 0x65, 0x73, 0x65, 0x74, 0x00,
          0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x32, 0x00, 0x00, 0x00, 0x01, 0x00, 0x00,
          0x00, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00];

    println!("crushmap compiled len {}", crushmap_compiled.len());
    let expected_result = CrushMap {
        magic: 65536,
        max_buckets: 8,
        max_rules: 1,
        max_devices: 3,
        buckets: vec![BucketTypes::Straw(CrushBucketStraw {
                          bucket: Bucket {
                              id: -1,
                              bucket_type: OpCode::SetChooseLocalTries,
                              alg: BucketAlg::Straw,
                              hash: CrushHash::RJenkins1,
                              weight: 0,
                              size: 3,
                              items: vec![(-2, Some("ip-172-31-43-147".to_string())),
                                          (-3, Some("ip-172-31-22-2".to_string())),
                                          (-4, Some("ip-172-31-4-56".to_string()))],
                              perm_n: 0,
                              perm: 3,
                          },
                          item_weights: vec![(0, 0), (0, 0), (0, 0)],
                      }),
                      BucketTypes::Straw(CrushBucketStraw {
                          bucket: Bucket {
                              id: -2,
                              bucket_type: OpCode::Take,
                              alg: BucketAlg::Straw,
                              hash: CrushHash::RJenkins1,
                              weight: 0,
                              size: 1,
                              items: vec![(0, Some("osd.0".to_string()))],
                              perm_n: 0,
                              perm: 1,
                          },
                          item_weights: vec![(0, 0)],
                      }),
                      BucketTypes::Straw(CrushBucketStraw {
                          bucket: Bucket {
                              id: -3,
                              bucket_type: OpCode::Take,
                              alg: BucketAlg::Straw,
                              hash: CrushHash::RJenkins1,
                              weight: 0,
                              size: 1,
                              items: vec![(1, Some("osd.1".to_string()))],
                              perm_n: 0,
                              perm: 1,
                          },
                          item_weights: vec![(0, 0)],
                      }),
                      BucketTypes::Straw(CrushBucketStraw {
                          bucket: Bucket {
                              id: -4,
                              bucket_type: OpCode::Take,
                              alg: BucketAlg::Straw,
                              hash: CrushHash::RJenkins1,
                              weight: 0,
                              size: 1,
                              items: vec![(2, Some("osd.2".to_string()))],
                              perm_n: 0,
                              perm: 1,
                          },
                          item_weights: vec![(0, 0)],
                      }),
                      BucketTypes::Unknown,
                      BucketTypes::Unknown,
                      BucketTypes::Unknown,
                      BucketTypes::Unknown],
        rules: vec![Some(Rule {
                        mask: CrushRuleMask {
                            ruleset: 0,
                            rule_type: RuleType::Replicated,
                            min_size: 1,
                            max_size: 10,
                        },
                        steps: vec![CrushRuleStep {
                                        op: OpCode::Take,
                                        arg1: (-1, None),
                                        arg2: (0, Some("osd".to_string())),
                                    },
                                    CrushRuleStep {
                                        op: OpCode::ChooseLeafFirstN,
                                        arg1: (0, Some("osd".to_string())),
                                        arg2: (1, Some("host".to_string())),
                                    },
                                    CrushRuleStep {
                                        op: OpCode::Emit,
                                        arg1: (0, Some("osd".to_string())),
                                        arg2: (0, Some("osd".to_string())),
                                    }],
                    })],
        type_map: vec![(0, "osd".to_string()),
                       (1, "host".to_string()),
                       (2, "chassis".to_string()),
                       (3, "rack".to_string()),
                       (4, "row".to_string()),
                       (5, "pdu".to_string()),
                       (6, "pod".to_string()),
                       (7, "room".to_string()),
                       (8, "datacenter".to_string()),
                       (9, "region".to_string()),
                       (10, "root".to_string())],
        name_map: vec![(-4, "ip-172-31-4-56".to_string()),
                       (-3, "ip-172-31-22-2".to_string()),
                       (-2, "ip-172-31-43-147".to_string()),
                       (-1, "default".to_string()),
                       (0, "osd.0".to_string()),
                       (1, "osd.1".to_string()),
                       (2, "osd.2".to_string())],
        rule_name_map: vec![(0, "replicated_ruleset".to_string())],
        choose_local_tries: Some(0),
        choose_local_fallback_tries: Some(0),
        choose_total_tries: Some(50),
        chooseleaf_descend_once: Some(1),
        chooseleaf_vary_r: Some(1),
        straw_calc_version: Some(0),
        allowed_bucket_algorithms: Some(0),
        chooseleaf_stable: Some(0),
    };
    let result = decode_crushmap(&crushmap_compiled);
    println!("crushmap {:?}", result);
    assert_eq!(Ok(expected_result), result);
}

#[test]
fn test_encode_crushmap() {
    let expected_result: Vec<u8> =
        vec![0x00, 0x00, 0x01, 0x00, 0x08, 0x00, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00, 0x03, 0x00,
             0x00, 0x00, 0x04, 0x00, 0x00, 0x00, 0xff, 0xff, 0xff, 0xff, 0x0a, 0x00, 0x04, 0x00,
             0x00, 0x00, 0x00, 0x00, 0x03, 0x00, 0x00, 0x00, 0xfe, 0xff, 0xff, 0xff, 0xfd, 0xff,
             0xff, 0xff, 0xfc, 0xff, 0xff, 0xff, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
             0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
             0x00, 0x00, 0x04, 0x00, 0x00, 0x00, 0xfe, 0xff, 0xff, 0xff, 0x01, 0x00, 0x04, 0x00,
             0x00, 0x00, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
             0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x04, 0x00, 0x00, 0x00, 0xfd, 0xff, 0xff, 0xff,
             0x01, 0x00, 0x04, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00, 0x01, 0x00,
             0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x04, 0x00, 0x00, 0x00,
             0xfc, 0xff, 0xff, 0xff, 0x01, 0x00, 0x04, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01, 0x00,
             0x00, 0x00, 0x02, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
             0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
             0x00, 0x00, 0x01, 0x00, 0x00, 0x00, 0x03, 0x00, 0x00, 0x00, 0x00, 0x01, 0x01, 0x0a,
             0x01, 0x00, 0x00, 0x00, 0xff, 0xff, 0xff, 0xff, 0x00, 0x00, 0x00, 0x00, 0x06, 0x00,
             0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00, 0x04, 0x00, 0x00, 0x00,
             0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x0b, 0x00, 0x00, 0x00, 0x00, 0x00,
             0x00, 0x00, 0x03, 0x00, 0x00, 0x00, 0x6f, 0x73, 0x64, 0x01, 0x00, 0x00, 0x00, 0x04,
             0x00, 0x00, 0x00, 0x68, 0x6f, 0x73, 0x74, 0x02, 0x00, 0x00, 0x00, 0x07, 0x00, 0x00,
             0x00, 0x63, 0x68, 0x61, 0x73, 0x73, 0x69, 0x73, 0x03, 0x00, 0x00, 0x00, 0x04, 0x00,
             0x00, 0x00, 0x72, 0x61, 0x63, 0x6b, 0x04, 0x00, 0x00, 0x00, 0x03, 0x00, 0x00, 0x00,
             0x72, 0x6f, 0x77, 0x05, 0x00, 0x00, 0x00, 0x03, 0x00, 0x00, 0x00, 0x70, 0x64, 0x75,
             0x06, 0x00, 0x00, 0x00, 0x03, 0x00, 0x00, 0x00, 0x70, 0x6f, 0x64, 0x07, 0x00, 0x00,
             0x00, 0x04, 0x00, 0x00, 0x00, 0x72, 0x6f, 0x6f, 0x6d, 0x08, 0x00, 0x00, 0x00, 0x0a,
             0x00, 0x00, 0x00, 0x64, 0x61, 0x74, 0x61, 0x63, 0x65, 0x6e, 0x74, 0x65, 0x72, 0x09,
             0x00, 0x00, 0x00, 0x06, 0x00, 0x00, 0x00, 0x72, 0x65, 0x67, 0x69, 0x6f, 0x6e, 0x0a,
             0x00, 0x00, 0x00, 0x04, 0x00, 0x00, 0x00, 0x72, 0x6f, 0x6f, 0x74, 0x07, 0x00, 0x00,
             0x00, 0xfc, 0xff, 0xff, 0xff, 0x0e, 0x00, 0x00, 0x00, 0x69, 0x70, 0x2d, 0x31, 0x37,
             0x32, 0x2d, 0x33, 0x31, 0x2d, 0x34, 0x2d, 0x35, 0x36, 0xfd, 0xff, 0xff, 0xff, 0x0e,
             0x00, 0x00, 0x00, 0x69, 0x70, 0x2d, 0x31, 0x37, 0x32, 0x2d, 0x33, 0x31, 0x2d, 0x32,
             0x32, 0x2d, 0x32, 0xfe, 0xff, 0xff, 0xff, 0x10, 0x00, 0x00, 0x00, 0x69, 0x70, 0x2d,
             0x31, 0x37, 0x32, 0x2d, 0x33, 0x31, 0x2d, 0x34, 0x33, 0x2d, 0x31, 0x34, 0x37, 0xff,
             0xff, 0xff, 0xff, 0x07, 0x00, 0x00, 0x00, 0x64, 0x65, 0x66, 0x61, 0x75, 0x6c, 0x74,
             0x00, 0x00, 0x00, 0x00, 0x05, 0x00, 0x00, 0x00, 0x6f, 0x73, 0x64, 0x2e, 0x30, 0x01,
             0x00, 0x00, 0x00, 0x05, 0x00, 0x00, 0x00, 0x6f, 0x73, 0x64, 0x2e, 0x31, 0x02, 0x00,
             0x00, 0x00, 0x05, 0x00, 0x00, 0x00, 0x6f, 0x73, 0x64, 0x2e, 0x32, 0x01, 0x00, 0x00,
             0x00, 0x00, 0x00, 0x00, 0x00, 0x12, 0x00, 0x00, 0x00, 0x72, 0x65, 0x70, 0x6c, 0x69,
             0x63, 0x61, 0x74, 0x65, 0x64, 0x5f, 0x72, 0x75, 0x6c, 0x65, 0x73, 0x65, 0x74, 0x00,
             0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x32, 0x00, 0x00, 0x00, 0x01, 0x00, 0x00,
             0x00, 0x01, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00];
    let crushmap = CrushMap {
        magic: 65536,
        max_buckets: 8,
        max_rules: 1,
        max_devices: 3,
        buckets: vec![BucketTypes::Straw(CrushBucketStraw {
                          bucket: Bucket {
                              id: -1,
                              bucket_type: OpCode::SetChooseLocalTries,
                              alg: BucketAlg::Straw,
                              hash: CrushHash::RJenkins1,
                              weight: 0,
                              size: 3,
                              items: vec![(-2, None), (-3, None), (-4, None)],
                              perm_n: 0,
                              perm: 3,
                          },
                          item_weights: vec![(0, 0), (0, 0), (0, 0)],
                      }),
                      BucketTypes::Straw(CrushBucketStraw {
                          bucket: Bucket {
                              id: -2,
                              bucket_type: OpCode::Take,
                              alg: BucketAlg::Straw,
                              hash: CrushHash::RJenkins1,
                              weight: 0,
                              size: 1,
                              items: vec![(0, None)],
                              perm_n: 0,
                              perm: 1,
                          },
                          item_weights: vec![(0, 0)],
                      }),
                      BucketTypes::Straw(CrushBucketStraw {
                          bucket: Bucket {
                              id: -3,
                              bucket_type: OpCode::Take,
                              alg: BucketAlg::Straw,
                              hash: CrushHash::RJenkins1,
                              weight: 0,
                              size: 1,
                              items: vec![(1, None)],
                              perm_n: 0,
                              perm: 1,
                          },
                          item_weights: vec![(0, 0)],
                      }),
                      BucketTypes::Straw(CrushBucketStraw {
                          bucket: Bucket {
                              id: -4,
                              bucket_type: OpCode::Take,
                              alg: BucketAlg::Straw,
                              hash: CrushHash::RJenkins1,
                              weight: 0,
                              size: 1,
                              items: vec![(2, None)],
                              perm_n: 0,
                              perm: 1,
                          },
                          item_weights: vec![(0, 0)],
                      }),
                      BucketTypes::Unknown,
                      BucketTypes::Unknown,
                      BucketTypes::Unknown,
                      BucketTypes::Unknown],
        rules: vec![Some(Rule {
                        mask: CrushRuleMask {
                            ruleset: 0,
                            rule_type: RuleType::Replicated,
                            min_size: 1,
                            max_size: 10,
                        },
                        steps: vec![CrushRuleStep {
                                        op: OpCode::Take,
                                        arg1: (-1, None),
                                        arg2: (0, None),
                                    },
                                    CrushRuleStep {
                                        op: OpCode::ChooseLeafFirstN,
                                        arg1: (0, None),
                                        arg2: (1, None),
                                    },
                                    CrushRuleStep {
                                        op: OpCode::Emit,
                                        arg1: (0, None),
                                        arg2: (0, None),
                                    }],
                    })],
        type_map: vec![(0, "osd".to_string()),
                       (1, "host".to_string()),
                       (2, "chassis".to_string()),
                       (3, "rack".to_string()),
                       (4, "row".to_string()),
                       (5, "pdu".to_string()),
                       (6, "pod".to_string()),
                       (7, "room".to_string()),
                       (8, "datacenter".to_string()),
                       (9, "region".to_string()),
                       (10, "root".to_string())],
        name_map: vec![(-4, "ip-172-31-4-56".to_string()),
                       (-3, "ip-172-31-22-2".to_string()),
                       (-2, "ip-172-31-43-147".to_string()),
                       (-1, "default".to_string()),
                       (0, "osd.0".to_string()),
                       (1, "osd.1".to_string()),
                       (2, "osd.2".to_string())],
        rule_name_map: vec![(0, "replicated_ruleset".to_string())],
        choose_local_tries: Some(0),
        choose_local_fallback_tries: Some(0),
        choose_total_tries: Some(50),
        chooseleaf_descend_once: Some(1),
        chooseleaf_vary_r: Some(1),
        straw_calc_version: Some(1),
        allowed_bucket_algorithms: Some(0),
        chooseleaf_stable: Some(0),
    };
    let result = encode_crushmap(crushmap);
    assert_eq!(expected_result, result.unwrap());
}

#[test]
fn test_decode_straw2_crushmap() {
    let crushmap_compiled: &[u8] =
        &[0x00, 0x00, 0x01, 0x00, 0x08, 0x00, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00, 0x03, 0x00,
          0x00, 0x00, 0x04, 0x00, 0x00, 0x00, 0xff, 0xff, 0xff, 0xff, 0x0a, 0x00, 0x04, 0x00,
          0x3a, 0x02, 0x00, 0x00, 0x03, 0x00, 0x00, 0x00, 0xfe, 0xff, 0xff, 0xff, 0xfd, 0xff,
          0xff, 0xff, 0xfc, 0xff, 0xff, 0xff, 0xbe, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01, 0x00,
          0xbe, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01, 0x00, 0xbe, 0x00, 0x00, 0x00, 0x00, 0x00,
          0x01, 0x00, 0x04, 0x00, 0x00, 0x00, 0xfe, 0xff, 0xff, 0xff, 0x01, 0x00, 0x04, 0x00,
          0xbe, 0x00, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xbe, 0x00,
          0x00, 0x00, 0x00, 0x00, 0x01, 0x00, 0x04, 0x00, 0x00, 0x00, 0xfd, 0xff, 0xff, 0xff,
          0x01, 0x00, 0x04, 0x00, 0xbe, 0x00, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00, 0x01, 0x00,
          0x00, 0x00, 0xbe, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01, 0x00, 0x04, 0x00, 0x00, 0x00,
          0xfc, 0xff, 0xff, 0xff, 0x01, 0x00, 0x04, 0x00, 0xbe, 0x00, 0x00, 0x00, 0x01, 0x00,
          0x00, 0x00, 0x02, 0x00, 0x00, 0x00, 0xbe, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01, 0x00,
          0x05, 0x00, 0x00, 0x00, 0xfb, 0xff, 0xff, 0xff, 0x03, 0x00, 0x05, 0x00, 0x00, 0x00,
          0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
          0x00, 0x00, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00, 0x03, 0x00, 0x00, 0x00, 0x00, 0x01,
          0x01, 0x0a, 0x01, 0x00, 0x00, 0x00, 0xff, 0xff, 0xff, 0xff, 0x00, 0x00, 0x00, 0x00,
          0x06, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00, 0x04, 0x00,
          0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x0b, 0x00, 0x00, 0x00,
          0x00, 0x00, 0x00, 0x00, 0x03, 0x00, 0x00, 0x00, 0x6f, 0x73, 0x64, 0x01, 0x00, 0x00,
          0x00, 0x04, 0x00, 0x00, 0x00, 0x68, 0x6f, 0x73, 0x74, 0x02, 0x00, 0x00, 0x00, 0x07,
          0x00, 0x00, 0x00, 0x63, 0x68, 0x61, 0x73, 0x73, 0x69, 0x73, 0x03, 0x00, 0x00, 0x00,
          0x04, 0x00, 0x00, 0x00, 0x72, 0x61, 0x63, 0x6b, 0x04, 0x00, 0x00, 0x00, 0x03, 0x00,
          0x00, 0x00, 0x72, 0x6f, 0x77, 0x05, 0x00, 0x00, 0x00, 0x03, 0x00, 0x00, 0x00, 0x70,
          0x64, 0x75, 0x06, 0x00, 0x00, 0x00, 0x03, 0x00, 0x00, 0x00, 0x70, 0x6f, 0x64, 0x07,
          0x00, 0x00, 0x00, 0x04, 0x00, 0x00, 0x00, 0x72, 0x6f, 0x6f, 0x6d, 0x08, 0x00, 0x00,
          0x00, 0x0a, 0x00, 0x00, 0x00, 0x64, 0x61, 0x74, 0x61, 0x63, 0x65, 0x6e, 0x74, 0x65,
          0x72, 0x09, 0x00, 0x00, 0x00, 0x06, 0x00, 0x00, 0x00, 0x72, 0x65, 0x67, 0x69, 0x6f,
          0x6e, 0x0a, 0x00, 0x00, 0x00, 0x04, 0x00, 0x00, 0x00, 0x72, 0x6f, 0x6f, 0x74, 0x08,
          0x00, 0x00, 0x00, 0xfb, 0xff, 0xff, 0xff, 0x04, 0x00, 0x00, 0x00, 0x74, 0x65, 0x73,
          0x74, 0xfc, 0xff, 0xff, 0xff, 0x10, 0x00, 0x00, 0x00, 0x69, 0x70, 0x2d, 0x31, 0x37,
          0x32, 0x2d, 0x33, 0x31, 0x2d, 0x31, 0x39, 0x2d, 0x32, 0x30, 0x35, 0xfd, 0xff, 0xff,
          0xff, 0x10, 0x00, 0x00, 0x00, 0x69, 0x70, 0x2d, 0x31, 0x37, 0x32, 0x2d, 0x33, 0x31,
          0x2d, 0x33, 0x34, 0x2d, 0x31, 0x34, 0x30, 0xfe, 0xff, 0xff, 0xff, 0x0f, 0x00, 0x00,
          0x00, 0x69, 0x70, 0x2d, 0x31, 0x37, 0x32, 0x2d, 0x33, 0x31, 0x2d, 0x39, 0x2d, 0x31,
          0x31, 0x30, 0xff, 0xff, 0xff, 0xff, 0x07, 0x00, 0x00, 0x00, 0x64, 0x65, 0x66, 0x61,
          0x75, 0x6c, 0x74, 0x00, 0x00, 0x00, 0x00, 0x05, 0x00, 0x00, 0x00, 0x6f, 0x73, 0x64,
          0x2e, 0x30, 0x01, 0x00, 0x00, 0x00, 0x05, 0x00, 0x00, 0x00, 0x6f, 0x73, 0x64, 0x2e,
          0x31, 0x02, 0x00, 0x00, 0x00, 0x05, 0x00, 0x00, 0x00, 0x6f, 0x73, 0x64, 0x2e, 0x32,
          0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x12, 0x00, 0x00, 0x00, 0x72, 0x65,
          0x70, 0x6c, 0x69, 0x63, 0x61, 0x74, 0x65, 0x64, 0x5f, 0x72, 0x75, 0x6c, 0x65, 0x73,
          0x65, 0x74, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x32, 0x00, 0x00, 0x00,
          0x01, 0x00, 0x00, 0x00, 0x01, 0x01, 0x36, 0x00, 0x00, 0x00, 0x01];

    let expected_result = CrushMap {
        magic: 65536,
        max_buckets: 8,
        max_rules: 1,
        max_devices: 3,
        buckets: vec![BucketTypes::Straw(CrushBucketStraw {
                          bucket: Bucket {
                              id: -1,
                              bucket_type: OpCode::SetChooseLocalTries,
                              alg: BucketAlg::Straw,
                              hash: CrushHash::RJenkins1,
                              weight: 570,
                              size: 3,
                              items: vec![(-2, Some("ip-172-31-9-110".to_string())),
                                          (-3, Some("ip-172-31-34-140".to_string())),
                                          (-4, Some("ip-172-31-19-205".to_string()))],
                              perm_n: 0,
                              perm: 3,
                          },
                          item_weights: vec![(190, 65536), (190, 65536), (190, 65536)],
                      }),
                      BucketTypes::Straw(CrushBucketStraw {
                          bucket: Bucket {
                              id: -2,
                              bucket_type: OpCode::Take,
                              alg: BucketAlg::Straw,
                              hash: CrushHash::RJenkins1,
                              weight: 190,
                              size: 1,
                              items: vec![(0, Some("osd.0".to_string()))],
                              perm_n: 0,
                              perm: 1,
                          },
                          item_weights: vec![(190, 65536)],
                      }),
                      BucketTypes::Straw(CrushBucketStraw {
                          bucket: Bucket {
                              id: -3,
                              bucket_type: OpCode::Take,
                              alg: BucketAlg::Straw,
                              hash: CrushHash::RJenkins1,
                              weight: 190,
                              size: 1,
                              items: vec![(1, Some("osd.1".to_string()))],
                              perm_n: 0,
                              perm: 1,
                          },
                          item_weights: vec![(190, 65536)],
                      }),
                      BucketTypes::Straw(CrushBucketStraw {
                          bucket: Bucket {
                              id: -4,
                              bucket_type: OpCode::Take,
                              alg: BucketAlg::Straw,
                              hash: CrushHash::RJenkins1,
                              weight: 190,
                              size: 1,
                              items: vec![(2, Some("osd.2".to_string()))],
                              perm_n: 0,
                              perm: 1,
                          },
                          item_weights: vec![(190, 65536)],
                      }),
                      BucketTypes::Straw2(CrushBucketStraw2 {
                          bucket: Bucket {
                              id: -5,
                              bucket_type: OpCode::ChooseIndep,
                              alg: BucketAlg::Straw2,
                              hash: CrushHash::RJenkins1,
                              weight: 0,
                              size: 0,
                              items: vec![],
                              perm_n: 0,
                              perm: 0,
                          },
                          item_weights: vec![],
                      }),
                      BucketTypes::Unknown,
                      BucketTypes::Unknown,
                      BucketTypes::Unknown],
        rules: vec![Some(Rule {
                        mask: CrushRuleMask {
                            ruleset: 0,
                            rule_type: RuleType::Replicated,
                            min_size: 1,
                            max_size: 10,
                        },
                        steps: vec![CrushRuleStep {
                                        op: OpCode::Take,
                                        arg1: (-1, None),
                                        arg2: (0, Some("osd".to_string())),
                                    },
                                    CrushRuleStep {
                                        op: OpCode::ChooseLeafFirstN,
                                        arg1: (0, Some("osd".to_string())),
                                        arg2: (1, Some("host".to_string())),
                                    },
                                    CrushRuleStep {
                                        op: OpCode::Emit,
                                        arg1: (0, Some("osd".to_string())),
                                        arg2: (0, Some("osd".to_string())),
                                    }],
                    })],
        type_map: vec![(0, "osd".to_string()),
                       (1, "host".to_string()),
                       (2, "chassis".to_string()),
                       (3, "rack".to_string()),
                       (4, "row".to_string()),
                       (5, "pdu".to_string()),
                       (6, "pod".to_string()),
                       (7, "room".to_string()),
                       (8, "datacenter".to_string()),
                       (9, "region".to_string()),
                       (10, "root".to_string())],
        name_map: vec![(-5, "test".to_string()),
                       (-4, "ip-172-31-19-205".to_string()),
                       (-3, "ip-172-31-34-140".to_string()),
                       (-2, "ip-172-31-9-110".to_string()),
                       (-1, "default".to_string()),
                       (0, "osd.0".to_string()),
                       (1, "osd.1".to_string()),
                       (2, "osd.2".to_string())],
        rule_name_map: vec![(0, "replicated_ruleset".to_string())],
        choose_local_tries: Some(0),
        choose_local_fallback_tries: Some(0),
        choose_total_tries: Some(50),
        chooseleaf_descend_once: Some(1),
        chooseleaf_vary_r: Some(1),
        straw_calc_version: Some(1),
        allowed_bucket_algorithms: Some(54),
        chooseleaf_stable: Some(1),
    };
    let result = decode_crushmap(&crushmap_compiled);
    println!("straw2 crushmap {:?}", result);
    assert_eq!(Ok(expected_result), result);
}