~ubuntu-branches/ubuntu/vivid/swift/vivid-updates

« back to all changes in this revision

Viewing changes to test/unit/common/ring/test_builder.py

  • Committer: Package Import Robot
  • Author(s): James Page, Chuck Short, James Page
  • Date: 2014-10-06 10:06:11 UTC
  • mfrom: (1.2.31)
  • Revision ID: package-import@ubuntu.com-20141006100611-wdzkkuoru7ubtlml
Tags: 2.1.0-0ubuntu1
[ Chuck Short ]
* debian/patches/fix-doc-no-network.patch: Refreshed.
* debian/control: Add python-oslosphinx as a build dependency.

[ James Page ]
* New upstream release for OpenStack Juno.
* d/copyright: Add linebreaks to fixup file-without-copyright-
  information warning.

Show diffs side-by-side

added added

removed removed

Lines of Context:
35
35
    def tearDown(self):
36
36
        rmtree(self.testdir, ignore_errors=1)
37
37
 
 
38
    def _get_population_by_region(self, builder):
 
39
        """
 
40
        Returns a dictionary mapping region to number of partitions in that
 
41
        region.
 
42
        """
 
43
        population_by_region = defaultdict(int)
 
44
        r = builder.get_ring()
 
45
        for part2dev_id in r._replica2part2dev_id:
 
46
            for dev_id in part2dev_id:
 
47
                dev = r.devs[dev_id]
 
48
                population_by_region[dev['region']] += 1
 
49
        return dict(population_by_region.items())
 
50
 
38
51
    def test_init(self):
39
52
        rb = ring.RingBuilder(8, 3, 1)
40
53
        self.assertEquals(rb.part_power, 8)
641
654
 
642
655
        rb.rebalance()
643
656
 
 
657
    def test_region_fullness_with_balanceable_ring(self):
 
658
        rb = ring.RingBuilder(8, 3, 1)
 
659
        rb.add_dev({'id': 0, 'region': 0, 'zone': 0, 'weight': 1,
 
660
                    'ip': '127.0.0.1', 'port': 10000, 'device': 'sda1'})
 
661
        rb.add_dev({'id': 1, 'region': 0, 'zone': 1, 'weight': 1,
 
662
                    'ip': '127.0.0.1', 'port': 10001, 'device': 'sda1'})
 
663
 
 
664
        rb.add_dev({'id': 2, 'region': 1, 'zone': 0, 'weight': 1,
 
665
                    'ip': '127.0.0.1', 'port': 10003, 'device': 'sda1'})
 
666
        rb.add_dev({'id': 3, 'region': 1, 'zone': 1, 'weight': 1,
 
667
                    'ip': '127.0.0.1', 'port': 10004, 'device': 'sda1'})
 
668
 
 
669
        rb.add_dev({'id': 4, 'region': 2, 'zone': 0, 'weight': 1,
 
670
                    'ip': '127.0.0.1', 'port': 10005, 'device': 'sda1'})
 
671
        rb.add_dev({'id': 5, 'region': 2, 'zone': 1, 'weight': 1,
 
672
                    'ip': '127.0.0.1', 'port': 10006, 'device': 'sda1'})
 
673
 
 
674
        rb.add_dev({'id': 6, 'region': 3, 'zone': 0, 'weight': 1,
 
675
                    'ip': '127.0.0.1', 'port': 10007, 'device': 'sda1'})
 
676
        rb.add_dev({'id': 7, 'region': 3, 'zone': 1, 'weight': 1,
 
677
                    'ip': '127.0.0.1', 'port': 10008, 'device': 'sda1'})
 
678
        rb.rebalance(seed=2)
 
679
 
 
680
        population_by_region = self._get_population_by_region(rb)
 
681
        self.assertEquals(population_by_region,
 
682
                          {0: 192, 1: 192, 2: 192, 3: 192})
 
683
 
 
684
    def test_region_fullness_with_unbalanceable_ring(self):
 
685
        rb = ring.RingBuilder(8, 3, 1)
 
686
        rb.add_dev({'id': 0, 'region': 0, 'zone': 0, 'weight': 2,
 
687
                    'ip': '127.0.0.1', 'port': 10000, 'device': 'sda1'})
 
688
        rb.add_dev({'id': 1, 'region': 0, 'zone': 1, 'weight': 2,
 
689
                    'ip': '127.0.0.1', 'port': 10001, 'device': 'sda1'})
 
690
 
 
691
        rb.add_dev({'id': 2, 'region': 1, 'zone': 0, 'weight': 1,
 
692
                    'ip': '127.0.0.1', 'port': 10003, 'device': 'sda1'})
 
693
        rb.add_dev({'id': 3, 'region': 1, 'zone': 1, 'weight': 1,
 
694
                    'ip': '127.0.0.1', 'port': 10004, 'device': 'sda1'})
 
695
        rb.rebalance(seed=2)
 
696
 
 
697
        population_by_region = self._get_population_by_region(rb)
 
698
        self.assertEquals(population_by_region, {0: 512, 1: 256})
 
699
 
 
700
    def test_adding_region_slowly_with_unbalanceable_ring(self):
 
701
        rb = ring.RingBuilder(8, 3, 1)
 
702
        rb.add_dev({'id': 0, 'region': 0, 'zone': 0, 'weight': 2,
 
703
                    'ip': '127.0.0.1', 'port': 10000, 'device': 'sda1'})
 
704
        rb.add_dev({'id': 1, 'region': 0, 'zone': 1, 'weight': 2,
 
705
                    'ip': '127.0.0.1', 'port': 10001, 'device': 'sda1'})
 
706
        rb.rebalance()
 
707
 
 
708
        rb.add_dev({'id': 2, 'region': 1, 'zone': 0, 'weight': 0.25,
 
709
                    'ip': '127.0.0.1', 'port': 10003, 'device': 'sda1'})
 
710
        rb.add_dev({'id': 3, 'region': 1, 'zone': 1, 'weight': 0.25,
 
711
                    'ip': '127.0.0.1', 'port': 10004, 'device': 'sda1'})
 
712
        rb.pretend_min_part_hours_passed()
 
713
        rb.rebalance(seed=2)
 
714
 
 
715
        # there's not enough room in r1 for every partition to have a replica
 
716
        # in it, so only 86 assignments occur in r1 (that's ~1/5 of the total,
 
717
        # since r1 has 1/5 of the weight).
 
718
        population_by_region = self._get_population_by_region(rb)
 
719
        self.assertEquals(population_by_region, {0: 682, 1: 86})
 
720
 
 
721
        # and since there's not enough room, subsequent rebalances will not
 
722
        # cause additional assignments to r1
 
723
        rb.pretend_min_part_hours_passed()
 
724
        rb.rebalance(seed=2)
 
725
        population_by_region = self._get_population_by_region(rb)
 
726
        self.assertEquals(population_by_region, {0: 682, 1: 86})
 
727
 
 
728
        # after you add more weight, more partition assignments move
 
729
        rb.set_dev_weight(2, 0.5)
 
730
        rb.set_dev_weight(3, 0.5)
 
731
        rb.pretend_min_part_hours_passed()
 
732
        rb.rebalance(seed=2)
 
733
        population_by_region = self._get_population_by_region(rb)
 
734
        self.assertEquals(population_by_region, {0: 614, 1: 154})
 
735
 
 
736
        rb.set_dev_weight(2, 1.0)
 
737
        rb.set_dev_weight(3, 1.0)
 
738
        rb.pretend_min_part_hours_passed()
 
739
        rb.rebalance(seed=2)
 
740
        population_by_region = self._get_population_by_region(rb)
 
741
        self.assertEquals(population_by_region, {0: 512, 1: 256})
 
742
 
644
743
    def test_set_replicas_increase(self):
645
744
        rb = ring.RingBuilder(8, 2, 0)
646
745
        rb.add_dev({'id': 0, 'region': 0, 'zone': 0, 'weight': 1,
698
797
        self.assertEqual([len(p2d) for p2d in rb._replica2part2dev],
699
798
                         [256, 256, 128])
700
799
 
 
800
    def test_add_replicas_then_rebalance_respects_weight(self):
 
801
        rb = ring.RingBuilder(8, 3, 1)
 
802
        rb.add_dev({'id': 0, 'region': 0, 'region': 0, 'zone': 0, 'weight': 3,
 
803
                    'ip': '127.0.0.1', 'port': 10000, 'device': 'sda'})
 
804
        rb.add_dev({'id': 1, 'region': 0, 'region': 0, 'zone': 0, 'weight': 3,
 
805
                    'ip': '127.0.0.1', 'port': 10000, 'device': 'sdb'})
 
806
        rb.add_dev({'id': 2, 'region': 0, 'region': 0, 'zone': 0, 'weight': 1,
 
807
                    'ip': '127.0.0.1', 'port': 10000, 'device': 'sdc'})
 
808
        rb.add_dev({'id': 3, 'region': 0, 'region': 0, 'zone': 0, 'weight': 1,
 
809
                    'ip': '127.0.0.1', 'port': 10000, 'device': 'sdd'})
 
810
 
 
811
        rb.add_dev({'id': 4, 'region': 0, 'region': 0, 'zone': 1, 'weight': 3,
 
812
                    'ip': '127.0.0.1', 'port': 10000, 'device': 'sde'})
 
813
        rb.add_dev({'id': 5, 'region': 0, 'region': 0, 'zone': 1, 'weight': 3,
 
814
                    'ip': '127.0.0.1', 'port': 10000, 'device': 'sdf'})
 
815
        rb.add_dev({'id': 6, 'region': 0, 'region': 0, 'zone': 1, 'weight': 1,
 
816
                    'ip': '127.0.0.1', 'port': 10000, 'device': 'sdg'})
 
817
        rb.add_dev({'id': 7, 'region': 0, 'region': 0, 'zone': 1, 'weight': 1,
 
818
                    'ip': '127.0.0.1', 'port': 10000, 'device': 'sdh'})
 
819
 
 
820
        rb.add_dev({'id': 8, 'region': 0, 'region': 0, 'zone': 2, 'weight': 3,
 
821
                    'ip': '127.0.0.1', 'port': 10000, 'device': 'sdi'})
 
822
        rb.add_dev({'id': 9, 'region': 0, 'region': 0, 'zone': 2, 'weight': 3,
 
823
                    'ip': '127.0.0.1', 'port': 10000, 'device': 'sdj'})
 
824
        rb.add_dev({'id': 10, 'region': 0, 'region': 0, 'zone': 2, 'weight': 1,
 
825
                    'ip': '127.0.0.1', 'port': 10000, 'device': 'sdk'})
 
826
        rb.add_dev({'id': 11, 'region': 0, 'region': 0, 'zone': 2, 'weight': 1,
 
827
                    'ip': '127.0.0.1', 'port': 10000, 'device': 'sdl'})
 
828
 
 
829
        rb.rebalance(seed=1)
 
830
 
 
831
        r = rb.get_ring()
 
832
        counts = {}
 
833
        for part2dev_id in r._replica2part2dev_id:
 
834
            for dev_id in part2dev_id:
 
835
                counts[dev_id] = counts.get(dev_id, 0) + 1
 
836
        self.assertEquals(counts, {0: 96, 1: 96,
 
837
                                   2: 32, 3: 32,
 
838
                                   4: 96, 5: 96,
 
839
                                   6: 32, 7: 32,
 
840
                                   8: 96, 9: 96,
 
841
                                   10: 32, 11: 32})
 
842
 
 
843
        rb.replicas *= 2
 
844
        rb.rebalance(seed=1)
 
845
 
 
846
        r = rb.get_ring()
 
847
        counts = {}
 
848
        for part2dev_id in r._replica2part2dev_id:
 
849
            for dev_id in part2dev_id:
 
850
                counts[dev_id] = counts.get(dev_id, 0) + 1
 
851
        self.assertEquals(counts, {0: 192, 1: 192,
 
852
                                   2: 64, 3: 64,
 
853
                                   4: 192, 5: 192,
 
854
                                   6: 64, 7: 64,
 
855
                                   8: 192, 9: 192,
 
856
                                   10: 64, 11: 64})
 
857
 
701
858
    def test_load(self):
702
859
        rb = ring.RingBuilder(8, 3, 1)
703
860
        devs = [{'id': 0, 'region': 0, 'zone': 0, 'weight': 1,