示例#1
0
def sample_slottable_2():
    slottable = SlotTable([(constants.RES_CPU,ResourceTuple.SINGLE_INSTANCE),(constants.RES_MEM,ResourceTuple.SINGLE_INSTANCE)])
    FULL_NODE, HALF_NODE, QRTR_NODE, EMPT_NODE = create_capacities(slottable)
    
    slottable.add_node(1, FULL_NODE)
    slottable.add_node(2, FULL_NODE)  
    slottable.add_node(3, FULL_NODE)  
    slottable.add_node(4, FULL_NODE)  

    lease1 = create_ar_lease(lease_id = 1,
                             submit_time = T1200,
                             start = T1330,
                             end   = T1345,
                             preemptible = False,
                             requested_resources = {1: FULL_NODE, 2: FULL_NODE})
    create_reservation_from_lease(lease1, {1:1,2:2}, slottable)
    
    lease2 = create_ar_lease(lease_id = 2,
                             submit_time = T1200,
                             start = T1315,
                             end   = T1330,
                             preemptible = False,
                             requested_resources = {1: HALF_NODE})
    create_reservation_from_lease(lease2, {1:1}, slottable)

    return slottable, [lease1,lease2]
示例#2
0
文件: common.py 项目: queba/haizea
def create_tmp_slottable(slottable):
    tmp_slottable = SlotTable(slottable.resource_types)
    tmp_slottable.nodes = slottable.nodes
    tmp_slottable.reservations_by_start = slottable.reservations_by_start[:]
    tmp_slottable.reservations_by_end = slottable.reservations_by_end[:]

    return tmp_slottable
示例#3
0
    def test_resource_tuple(self):
        
        multiinst = [(constants.RES_CPU,ResourceTuple.MULTI_INSTANCE),(constants.RES_MEM,ResourceTuple.SINGLE_INSTANCE)]
        
        self.slottable = SlotTable(multiinst)
                
        c1_100 = Capacity([constants.RES_CPU,constants.RES_MEM])
        c1_100.set_quantity(constants.RES_CPU, 100)
        c1_100.set_quantity(constants.RES_MEM, 1024)
        c1_100 = self.slottable.create_resource_tuple_from_capacity(c1_100)

        c2_100 = Capacity([constants.RES_CPU,constants.RES_MEM])
        c2_100.set_ninstances(constants.RES_CPU, 2)
        c2_100.set_quantity_instance(constants.RES_CPU, 1, 100)
        c2_100.set_quantity_instance(constants.RES_CPU, 2, 100)
        c2_100.set_quantity(constants.RES_MEM, 1024)
        c2_100 = self.slottable.create_resource_tuple_from_capacity(c2_100)

        c1_50 = Capacity([constants.RES_CPU,constants.RES_MEM])
        c1_50.set_quantity(constants.RES_CPU, 50)
        c1_50.set_quantity(constants.RES_MEM, 1024)
        c1_50 = self.slottable.create_resource_tuple_from_capacity(c1_50)

        c2_50 = Capacity([constants.RES_CPU,constants.RES_MEM])
        c2_50.set_ninstances(constants.RES_CPU, 2)
        c2_50.set_quantity_instance(constants.RES_CPU, 1, 50)
        c2_50.set_quantity_instance(constants.RES_CPU, 2, 50)
        c2_50.set_quantity(constants.RES_MEM, 1024)
        c2_50 = self.slottable.create_resource_tuple_from_capacity(c2_50)

        assert c1_100.fits_in(c2_100)
        assert not c1_100.fits_in(c1_50)
        assert not c1_100.fits_in(c2_50)

        assert not c2_100.fits_in(c1_100)
        assert not c2_100.fits_in(c1_50)
        assert not c2_100.fits_in(c2_50)

        assert c1_50.fits_in(c1_100)
        assert c1_50.fits_in(c2_100)
        assert c1_50.fits_in(c2_50)

        assert c2_50.fits_in(c1_100)
        assert c2_50.fits_in(c2_100)
        assert not c2_50.fits_in(c1_50)

        empty = self.slottable.create_empty_resource_tuple()
        empty.incr(c2_100)
        assert empty._single_instance[0] == 1024
        assert empty._multi_instance[1] == [100,100]
        
        empty = self.slottable.create_empty_resource_tuple()
        empty.incr(c1_100)
        assert empty._single_instance[0] == 1024
        assert empty._multi_instance[1] == [100]
        empty.incr(c1_100)
        assert empty._single_instance[0] == 2048
        assert empty._multi_instance[1] == [100,100]

        empty = self.slottable.create_empty_resource_tuple()
        empty.incr(c1_100)
        assert empty._single_instance[0] == 1024
        assert empty._multi_instance[1] == [100]
        empty.incr(c1_50)
        assert empty._single_instance[0] == 2048
        assert empty._multi_instance[1] == [100,50]
   
        c1_100a = ResourceTuple.copy(c1_100)
        c1_100a.decr(c1_50)
        assert c1_100a._single_instance[0] == 0
        assert c1_100a._multi_instance[1] == [50]

        c2_100a = ResourceTuple.copy(c2_100)
        c2_100a._single_instance[0] = 2048
        c2_100a.decr(c1_50)
        
        assert c2_100a._single_instance[0] == 1024
        assert c2_100a._multi_instance[1] == [50,100]
        c2_100a.decr(c1_50)
        assert c2_100a._single_instance[0] == 0
        assert c2_100a._multi_instance[1] == [0,100]

        c2_100a = ResourceTuple.copy(c2_100)
        c2_100a._single_instance[0] = 2048
        c2_100a.decr(c2_50)
        assert c2_100a._single_instance[0] == 1024
        assert c2_100a._multi_instance[1] == [0,100]
        c2_100a.decr(c2_50)
        assert c2_100a._single_instance[0] == 0
        assert c2_100a._multi_instance[1] == [0,0]
示例#4
0
class TestSlotTable(object):
    def __init__(self):
        self.slottable = None
   
    def test_resource_tuple(self):
        
        multiinst = [(constants.RES_CPU,ResourceTuple.MULTI_INSTANCE),(constants.RES_MEM,ResourceTuple.SINGLE_INSTANCE)]
        
        self.slottable = SlotTable(multiinst)
                
        c1_100 = Capacity([constants.RES_CPU,constants.RES_MEM])
        c1_100.set_quantity(constants.RES_CPU, 100)
        c1_100.set_quantity(constants.RES_MEM, 1024)
        c1_100 = self.slottable.create_resource_tuple_from_capacity(c1_100)

        c2_100 = Capacity([constants.RES_CPU,constants.RES_MEM])
        c2_100.set_ninstances(constants.RES_CPU, 2)
        c2_100.set_quantity_instance(constants.RES_CPU, 1, 100)
        c2_100.set_quantity_instance(constants.RES_CPU, 2, 100)
        c2_100.set_quantity(constants.RES_MEM, 1024)
        c2_100 = self.slottable.create_resource_tuple_from_capacity(c2_100)

        c1_50 = Capacity([constants.RES_CPU,constants.RES_MEM])
        c1_50.set_quantity(constants.RES_CPU, 50)
        c1_50.set_quantity(constants.RES_MEM, 1024)
        c1_50 = self.slottable.create_resource_tuple_from_capacity(c1_50)

        c2_50 = Capacity([constants.RES_CPU,constants.RES_MEM])
        c2_50.set_ninstances(constants.RES_CPU, 2)
        c2_50.set_quantity_instance(constants.RES_CPU, 1, 50)
        c2_50.set_quantity_instance(constants.RES_CPU, 2, 50)
        c2_50.set_quantity(constants.RES_MEM, 1024)
        c2_50 = self.slottable.create_resource_tuple_from_capacity(c2_50)

        assert c1_100.fits_in(c2_100)
        assert not c1_100.fits_in(c1_50)
        assert not c1_100.fits_in(c2_50)

        assert not c2_100.fits_in(c1_100)
        assert not c2_100.fits_in(c1_50)
        assert not c2_100.fits_in(c2_50)

        assert c1_50.fits_in(c1_100)
        assert c1_50.fits_in(c2_100)
        assert c1_50.fits_in(c2_50)

        assert c2_50.fits_in(c1_100)
        assert c2_50.fits_in(c2_100)
        assert not c2_50.fits_in(c1_50)

        empty = self.slottable.create_empty_resource_tuple()
        empty.incr(c2_100)
        assert empty._single_instance[0] == 1024
        assert empty._multi_instance[1] == [100,100]
        
        empty = self.slottable.create_empty_resource_tuple()
        empty.incr(c1_100)
        assert empty._single_instance[0] == 1024
        assert empty._multi_instance[1] == [100]
        empty.incr(c1_100)
        assert empty._single_instance[0] == 2048
        assert empty._multi_instance[1] == [100,100]

        empty = self.slottable.create_empty_resource_tuple()
        empty.incr(c1_100)
        assert empty._single_instance[0] == 1024
        assert empty._multi_instance[1] == [100]
        empty.incr(c1_50)
        assert empty._single_instance[0] == 2048
        assert empty._multi_instance[1] == [100,50]
   
        c1_100a = ResourceTuple.copy(c1_100)
        c1_100a.decr(c1_50)
        assert c1_100a._single_instance[0] == 0
        assert c1_100a._multi_instance[1] == [50]

        c2_100a = ResourceTuple.copy(c2_100)
        c2_100a._single_instance[0] = 2048
        c2_100a.decr(c1_50)
        
        assert c2_100a._single_instance[0] == 1024
        assert c2_100a._multi_instance[1] == [50,100]
        c2_100a.decr(c1_50)
        assert c2_100a._single_instance[0] == 0
        assert c2_100a._multi_instance[1] == [0,100]

        c2_100a = ResourceTuple.copy(c2_100)
        c2_100a._single_instance[0] = 2048
        c2_100a.decr(c2_50)
        assert c2_100a._single_instance[0] == 1024
        assert c2_100a._multi_instance[1] == [0,100]
        c2_100a.decr(c2_50)
        assert c2_100a._single_instance[0] == 0
        assert c2_100a._multi_instance[1] == [0,0]
   
    def test_slottable(self):
        def assert_capacity(node, percent):
            assert node.capacity.get_by_type(constants.RES_CPU) == percent * 100
            assert node.capacity.get_by_type(constants.RES_MEM) == percent * 1024
            
        def reservations_1_assertions():
            assert not self.slottable.is_empty()
            nodes = self.slottable.get_availability(T1300)
            assert_capacity(nodes[1], 0.5)
            assert_capacity(nodes[2], 0.5)
            nodes = self.slottable.get_availability(T1330)
            assert_capacity(nodes[1], 1.0)
            assert_capacity(nodes[2], 1.0)
            
        def reservations_2_assertions():
            nodes = self.slottable.get_availability(T1300)
            assert_capacity(nodes[1], 0.5)
            assert_capacity(nodes[2], 0.5)
            rrs = self.slottable.get_reservations_at(T1300)
            assert len(rrs) == 1
            assert rrs[0] == rr1
    
            nodes = self.slottable.get_availability(T1330)
            assert_capacity(nodes[1], 0.75)
            assert_capacity(nodes[2], 0.5)
            rrs = self.slottable.get_reservations_at(T1330)
            assert len(rrs) == 1
            assert rrs[0] == rr2
    
            nodes = self.slottable.get_availability(T1400)
            assert_capacity(nodes[1], 1.0)
            assert_capacity(nodes[2], 1.0)
            rrs = self.slottable.get_reservations_at(T1400)
            assert len(rrs) == 0
            
        def reservations_3_assertions():
            nodes = self.slottable.get_availability(T1300)
            assert_capacity(nodes[1], 0.5)
            assert_capacity(nodes[2], 0.5)
            rrs = self.slottable.get_reservations_at(T1300)
            assert len(rrs) == 1
            assert rrs[0] == rr1
    
            nodes = self.slottable.get_availability(T1315)
            assert_capacity(nodes[1], 0.25)
            assert_capacity(nodes[2], 0.25)
            rrs = self.slottable.get_reservations_at(T1315)
            assert len(rrs) == 2
            assert rr1 in rrs and rr3 in rrs

            nodes = self.slottable.get_availability(T1330)
            assert_capacity(nodes[1], 0.5)
            assert_capacity(nodes[2], 0.25)
            rrs = self.slottable.get_reservations_at(T1330)
            assert len(rrs) == 2
            assert rr2 in rrs and rr3 in rrs

            nodes = self.slottable.get_availability(T1345)
            assert_capacity(nodes[1], 0.75)
            assert_capacity(nodes[2], 0.5)
            rrs = self.slottable.get_reservations_at(T1345)
            assert len(rrs) == 1
            assert rrs[0] == rr2
    
            nodes = self.slottable.get_availability(T1400)
            assert_capacity(nodes[1], 1.0)
            assert_capacity(nodes[2], 1.0)
            rrs = self.slottable.get_reservations_at(T1400)
            assert len(rrs) == 0

        def reservations_4_assertions():
            nodes = self.slottable.get_availability(T1300)
            assert_capacity(nodes[1], 0.5)
            assert_capacity(nodes[2], 0.5)
            rrs = self.slottable.get_reservations_at(T1300)
            assert len(rrs) == 1
            assert rrs[0] == rr1
    
            nodes = self.slottable.get_availability(T1315)
            assert_capacity(nodes[1], 0.25)
            assert_capacity(nodes[2], 0.25)
            rrs = self.slottable.get_reservations_at(T1315)
            assert len(rrs) == 2
            assert rr1 in rrs and rr3 in rrs

            nodes = self.slottable.get_availability(T1330)
            assert_capacity(nodes[1], 0)
            assert_capacity(nodes[2], 0)
            rrs = self.slottable.get_reservations_at(T1330)
            assert len(rrs) == 3
            assert rr4 in rrs and rr2 in rrs and rr3 in rrs

            nodes = self.slottable.get_availability(T1345)
            assert_capacity(nodes[1], 0.25)
            assert_capacity(nodes[2], 0.25)
            rrs = self.slottable.get_reservations_at(T1345)
            assert len(rrs) == 2
            assert rr2 in rrs and rr4 in rrs
    
            nodes = self.slottable.get_availability(T1400)
            assert_capacity(nodes[1], 0.5)
            assert_capacity(nodes[2], 0.75)
            rrs = self.slottable.get_reservations_at(T1400)
            assert len(rrs) == 1
            assert rrs[0] == rr4
    
            nodes = self.slottable.get_availability(T1415)
            assert_capacity(nodes[1], 1.0)
            assert_capacity(nodes[2], 1.0)
            rrs = self.slottable.get_reservations_at(T1415)
            assert len(rrs) == 0
        
        def reservations_5_assertions():
            nodes = self.slottable.get_availability(T1300)
            assert_capacity(nodes[1], 0.5)
            assert_capacity(nodes[2], 0.5)
            rrs = self.slottable.get_reservations_at(T1300)
            assert len(rrs) == 1
            assert rrs[0] == rr1
    
            nodes = self.slottable.get_availability(T1315)
            assert_capacity(nodes[1], 0.25)
            assert_capacity(nodes[2], 0.25)
            rrs = self.slottable.get_reservations_at(T1315)
            assert len(rrs) == 2
            assert set(rrs) == set([rr1,rr3])

            nodes = self.slottable.get_availability(T1330)
            assert_capacity(nodes[1], 0)
            assert_capacity(nodes[2], 0)
            rrs = self.slottable.get_reservations_at(T1330)
            assert len(rrs) == 3
            assert set(rrs) == set([rr2,rr3,rr4])

            nodes = self.slottable.get_availability(T1345)
            assert_capacity(nodes[1], 0.25)
            assert_capacity(nodes[2], 0)
            rrs = self.slottable.get_reservations_at(T1345)
            assert len(rrs) == 3
            assert set(rrs) == set([rr2,rr4,rr5])
    
            nodes = self.slottable.get_availability(T1400)
            assert_capacity(nodes[1], 0.5)
            assert_capacity(nodes[2], 0.5)
            rrs = self.slottable.get_reservations_at(T1400)
            assert len(rrs) == 2
            assert set(rrs) == set([rr4,rr5])
    
            nodes = self.slottable.get_availability(T1415)
            assert_capacity(nodes[1], 1.0)
            assert_capacity(nodes[2], 1.0)
            rrs = self.slottable.get_reservations_at(T1415)
            assert len(rrs) == 0
            
            rrs = self.slottable.get_reservations_starting_between(T1300, T1315)
            assert set(rrs) == set([rr1,rr3])
            rrs = self.slottable.get_reservations_starting_between(T1300, T1330)
            assert set(rrs) == set([rr1,rr2,rr3,rr4])
            rrs = self.slottable.get_reservations_starting_between(T1300, T1345)
            assert set(rrs) == set([rr1,rr2,rr3,rr4,rr5])
            rrs = self.slottable.get_reservations_starting_between(T1315, T1330)
            assert set(rrs) == set([rr2,rr3,rr4])
            rrs = self.slottable.get_reservations_starting_between(T1315, T1345)
            assert set(rrs) == set([rr2,rr3,rr4,rr5])
            rrs = self.slottable.get_reservations_starting_between(T1330, T1345)
            assert set(rrs) == set([rr2,rr4,rr5])
            rrs = self.slottable.get_reservations_starting_between(T1400, T1415)
            assert len(rrs) == 0
            rrs = self.slottable.get_reservations_starting_between(T1305, T1335)
            assert set(rrs) == set([rr3,rr2,rr4])

            rrs = self.slottable.get_reservations_ending_between(T1300, T1305)
            assert len(rrs) == 0
            rrs = self.slottable.get_reservations_ending_between(T1300, T1315)
            assert len(rrs) == 0
            rrs = self.slottable.get_reservations_ending_between(T1300, T1330)
            assert set(rrs) == set([rr1])
            rrs = self.slottable.get_reservations_ending_between(T1300, T1335)
            assert set(rrs) == set([rr1])
            rrs = self.slottable.get_reservations_ending_between(T1300, T1345)
            assert set(rrs) == set([rr1,rr3])
            rrs = self.slottable.get_reservations_ending_between(T1300, T1400)
            assert set(rrs) == set([rr1,rr2,rr3])
            rrs = self.slottable.get_reservations_ending_between(T1300, T1415)
            assert set(rrs) == set([rr1,rr2,rr3,rr4,rr5])
            rrs = self.slottable.get_reservations_ending_between(T1305, T1315)
            assert len(rrs) == 0
            rrs = self.slottable.get_reservations_ending_between(T1305, T1330)
            assert set(rrs) == set([rr1])
            rrs = self.slottable.get_reservations_ending_between(T1305, T1335)
            assert set(rrs) == set([rr1])
            rrs = self.slottable.get_reservations_ending_between(T1305, T1345)
            assert set(rrs) == set([rr1,rr3])
            rrs = self.slottable.get_reservations_ending_between(T1305, T1400)
            assert set(rrs) == set([rr1,rr2,rr3])
            rrs = self.slottable.get_reservations_ending_between(T1305, T1415)
            assert set(rrs) == set([rr1,rr2,rr3,rr4,rr5])
            rrs = self.slottable.get_reservations_ending_between(T1315, T1330)
            assert set(rrs) == set([rr1])
            rrs = self.slottable.get_reservations_ending_between(T1315, T1335)
            assert set(rrs) == set([rr1])
            rrs = self.slottable.get_reservations_ending_between(T1315, T1345)
            assert set(rrs) == set([rr1,rr3])
            rrs = self.slottable.get_reservations_ending_between(T1315, T1400)
            assert set(rrs) == set([rr1,rr2,rr3])
            rrs = self.slottable.get_reservations_ending_between(T1315, T1415)
            assert set(rrs) == set([rr1,rr2,rr3,rr4,rr5])
            rrs = self.slottable.get_reservations_ending_between(T1330, T1335)
            assert set(rrs) == set([rr1])
            rrs = self.slottable.get_reservations_ending_between(T1330, T1345)
            assert set(rrs) == set([rr1,rr3])
            rrs = self.slottable.get_reservations_ending_between(T1330, T1400)
            assert set(rrs) == set([rr1,rr2,rr3])
            rrs = self.slottable.get_reservations_ending_between(T1330, T1415)
            assert set(rrs) == set([rr1,rr2,rr3,rr4,rr5])
            rrs = self.slottable.get_reservations_ending_between(T1335, T1345)
            assert set(rrs) == set([rr3])
            rrs = self.slottable.get_reservations_ending_between(T1335, T1400)
            assert set(rrs) == set([rr2,rr3])
            rrs = self.slottable.get_reservations_ending_between(T1335, T1415)
            assert set(rrs) == set([rr2,rr3,rr4,rr5])
            rrs = self.slottable.get_reservations_ending_between(T1345, T1400)
            assert set(rrs) == set([rr2,rr3])
            rrs = self.slottable.get_reservations_ending_between(T1345, T1415)
            assert set(rrs) == set([rr2,rr3,rr4,rr5])
            rrs = self.slottable.get_reservations_ending_between(T1400, T1415)
            assert set(rrs) == set([rr2,rr4,rr5])
            
            rrs = self.slottable.get_reservations_starting_on_or_after(T1300)
            assert set(rrs) == set([rr1,rr2,rr3,rr4,rr5])
            rrs = self.slottable.get_reservations_starting_on_or_after(T1305)
            assert set(rrs) == set([rr2,rr3,rr4,rr5])
            rrs = self.slottable.get_reservations_starting_on_or_after(T1315)
            assert set(rrs) == set([rr2,rr3,rr4,rr5])
            rrs = self.slottable.get_reservations_starting_on_or_after(T1330)
            assert set(rrs) == set([rr2,rr4,rr5])
            rrs = self.slottable.get_reservations_starting_on_or_after(T1335)
            assert set(rrs) == set([rr5])
            rrs = self.slottable.get_reservations_starting_on_or_after(T1345)
            assert set(rrs) == set([rr5])
            rrs = self.slottable.get_reservations_starting_on_or_after(T1400)
            assert len(rrs) == 0
            rrs = self.slottable.get_reservations_starting_on_or_after(T1415)
            assert len(rrs) == 0
            
            rrs = self.slottable.get_reservations_ending_on_or_after(T1300)
            assert set(rrs) == set([rr1,rr2,rr3,rr4,rr5])
            rrs = self.slottable.get_reservations_ending_on_or_after(T1305)
            assert set(rrs) == set([rr1,rr2,rr3,rr4,rr5])
            rrs = self.slottable.get_reservations_ending_on_or_after(T1315)
            assert set(rrs) == set([rr1,rr2,rr3,rr4,rr5])
            rrs = self.slottable.get_reservations_ending_on_or_after(T1330)
            assert set(rrs) == set([rr1,rr2,rr3,rr4,rr5])
            rrs = self.slottable.get_reservations_ending_on_or_after(T1335)
            assert set(rrs) == set([rr2,rr3,rr4,rr5])
            rrs = self.slottable.get_reservations_ending_on_or_after(T1345)
            assert set(rrs) == set([rr2,rr3,rr4,rr5])
            rrs = self.slottable.get_reservations_ending_on_or_after(T1400)
            assert set(rrs) == set([rr2,rr4,rr5])
            rrs = self.slottable.get_reservations_ending_on_or_after(T1415)
            assert set(rrs) == set([rr4,rr5])
            
            assert self.slottable.get_next_changepoint(T1255) == T1300
            assert self.slottable.get_next_changepoint(T1300) == T1315
            assert self.slottable.get_next_changepoint(T1315) == T1330
            assert self.slottable.get_next_changepoint(T1330) == T1345
            assert self.slottable.get_next_changepoint(T1335) == T1345
            assert self.slottable.get_next_changepoint(T1345) == T1400
            assert self.slottable.get_next_changepoint(T1400) == T1415
            assert self.slottable.get_next_changepoint(T1415) == None
            assert self.slottable.get_next_changepoint(T1420) == None
        
        self.slottable = SlotTable([(constants.RES_CPU,ResourceTuple.SINGLE_INSTANCE),(constants.RES_MEM,ResourceTuple.SINGLE_INSTANCE)])
        FULL_NODE, HALF_NODE, QRTR_NODE, EMPT_NODE = create_capacities(self.slottable)
        
        self.slottable.add_node(1, FULL_NODE)
        self.slottable.add_node(2, FULL_NODE)  
        
        assert self.slottable.get_total_capacity(constants.RES_CPU) == 200
        assert self.slottable.get_total_capacity(constants.RES_MEM) == 2048
        assert self.slottable.is_empty()
    
        res1 = {1: HALF_NODE, 2: HALF_NODE}
        rr1 = ResourceReservation(None, T1300, T1330, res1)
        self.slottable.add_reservation(rr1)
        reservations_1_assertions()

        res2 = {1: QRTR_NODE, 2: HALF_NODE}
        rr2 = ResourceReservation(None, T1330, T1400, res2)
        self.slottable.add_reservation(rr2)
        reservations_2_assertions()

        res3 = {1: QRTR_NODE, 2: QRTR_NODE}
        rr3 = ResourceReservation(None, T1315, T1345, res3)
        self.slottable.add_reservation(rr3)
        reservations_3_assertions()

        res4 = {1: HALF_NODE, 2: QRTR_NODE}
        rr4 = ResourceReservation(None, T1330, T1415, res4)
        self.slottable.add_reservation(rr4)
        reservations_4_assertions()

        res5 = {2: QRTR_NODE}
        rr5 = ResourceReservation(None, T1345, T1415, res5)
        self.slottable.add_reservation(rr5)
        reservations_5_assertions()

        self.slottable.remove_reservation(rr5)
        reservations_4_assertions()
        self.slottable.remove_reservation(rr4)
        reservations_3_assertions()
        self.slottable.remove_reservation(rr3)
        reservations_2_assertions()
        self.slottable.remove_reservation(rr2)
        reservations_1_assertions()
        self.slottable.remove_reservation(rr1)
        
        assert self.slottable.is_empty()
        
    def test_availabilitywindow(self):
        def avail_node_assertions(time, avail, node_id, leases, next_cp):
            node = aw.changepoints[time].nodes[node_id]
            nleases = len(leases)
            assert(node.available == avail)
            
            assert(len(node.leases)==nleases)
            for l in leases:
                assert(l in node.leases)
            assert(len(node.available_if_preempting) == nleases)
            for l in leases:
                assert(node.available_if_preempting[l] == leases[l])
            assert(node.next_cp == next_cp)
            if next_cp != None:
                assert(node.next_nodeavail == aw.changepoints[next_cp].nodes[node_id])
        
        self.slottable, leases = sample_slottable_1()        
        FULL_NODE, HALF_NODE, QRTR_NODE, EMPT_NODE = create_capacities(self.slottable)
        
        lease1,lease2,lease3,lease4,lease5,lease6 = leases
        
        aw = self.slottable.get_availability_window(T1300)
        
        # TODO: Factor out data into a data structure so we can do more
        # elaborate assertions
        
        # 13:00
        avail_node_assertions(time = T1300, avail = EMPT_NODE, node_id = 1, 
                              leases = {lease6:FULL_NODE}, next_cp = T1305)
        avail_node_assertions(time = T1300, avail = FULL_NODE, node_id = 2, 
                              leases = {}, next_cp = T1315)
        avail_node_assertions(time = T1300, avail = FULL_NODE, node_id = 3, 
                              leases = {}, next_cp = T1330)
        avail_node_assertions(time = T1300, avail = FULL_NODE, node_id = 4, 
                              leases = {}, next_cp = T1330)


        # 13:05
        avail_node_assertions(time = T1305, avail = FULL_NODE, node_id = 1, 
                              leases = {}, next_cp = None)
        avail_node_assertions(time = T1305, avail = FULL_NODE, node_id = 2, 
                              leases = {}, next_cp = T1315)
        avail_node_assertions(time = T1305, avail = FULL_NODE, node_id = 3, 
                              leases = {}, next_cp = T1330)
        avail_node_assertions(time = T1305, avail = FULL_NODE, node_id = 4, 
                              leases = {}, next_cp = T1330)

        # 13:15
        avail_node_assertions(time = T1315, avail = FULL_NODE, node_id = 1, 
                              leases = {}, next_cp = None)
        avail_node_assertions(time = T1315, avail = HALF_NODE, node_id = 2, 
                              leases = {lease1:HALF_NODE}, next_cp = T1330)
        avail_node_assertions(time = T1315, avail = FULL_NODE, node_id = 3, 
                              leases = {}, next_cp = T1330)
        avail_node_assertions(time = T1315, avail = FULL_NODE, node_id = 4, 
                              leases = {}, next_cp = T1330)

        # 13:25
        avail_node_assertions(time = T1325, avail = FULL_NODE, node_id = 1, 
                              leases = {}, next_cp = None)
        avail_node_assertions(time = T1325, avail = HALF_NODE, node_id = 2, 
                              leases = {lease1:HALF_NODE}, next_cp = T1330)
        avail_node_assertions(time = T1325, avail = FULL_NODE, node_id = 3, 
                              leases = {}, next_cp = T1330)
        avail_node_assertions(time = T1325, avail = FULL_NODE, node_id = 4, 
                              leases = {}, next_cp = T1330)

        # 13:30
        avail_node_assertions(time = T1330, avail = FULL_NODE, node_id = 1, 
                              leases = {}, next_cp = None)
        avail_node_assertions(time = T1330, avail = EMPT_NODE, node_id = 2, 
                              leases = {lease2:FULL_NODE}, next_cp = T1345)
        avail_node_assertions(time = T1330, avail = EMPT_NODE, node_id = 3, 
                              leases = {lease2:FULL_NODE}, next_cp = T1345)
        avail_node_assertions(time = T1330, avail = EMPT_NODE, node_id = 4, 
                              leases = {lease3:FULL_NODE}, next_cp = T1400)

        # 13:45
        avail_node_assertions(time = T1345, avail = FULL_NODE, node_id = 1, 
                              leases = {}, next_cp = None)
        avail_node_assertions(time = T1345, avail = FULL_NODE, node_id = 2, 
                              leases = {}, next_cp = T1350)
        avail_node_assertions(time = T1345, avail = FULL_NODE, node_id = 3, 
                              leases = {}, next_cp = T1350)
        avail_node_assertions(time = T1345, avail = EMPT_NODE, node_id = 4, 
                              leases = {lease3:FULL_NODE}, next_cp = T1400)

        # 13:50
        avail_node_assertions(time = T1350, avail = FULL_NODE, node_id = 1, 
                              leases = {}, next_cp = None)
        avail_node_assertions(time = T1350, avail = HALF_NODE, node_id = 2, 
                              leases = {lease4:QRTR_NODE,lease5:QRTR_NODE}, next_cp = T1415)
        avail_node_assertions(time = T1350, avail = HALF_NODE, node_id = 3, 
                              leases = {lease4:HALF_NODE}, next_cp = T1415)
        avail_node_assertions(time = T1350, avail = EMPT_NODE, node_id = 4, 
                              leases = {lease3:FULL_NODE}, next_cp = T1400)

        # 13:55
        avail_node_assertions(time = T1355, avail = FULL_NODE, node_id = 1, 
                              leases = {}, next_cp = None)
        avail_node_assertions(time = T1355, avail = HALF_NODE, node_id = 2, 
                              leases = {lease4:QRTR_NODE,lease5:QRTR_NODE}, next_cp = T1415)
        avail_node_assertions(time = T1355, avail = HALF_NODE, node_id = 3, 
                              leases = {lease4:HALF_NODE}, next_cp = T1415)
        avail_node_assertions(time = T1355, avail = EMPT_NODE, node_id = 4, 
                              leases = {lease3:FULL_NODE}, next_cp = T1400)

        # 14:00
        avail_node_assertions(time = T1400, avail = FULL_NODE, node_id = 1, 
                              leases = {}, next_cp = None)
        avail_node_assertions(time = T1400, avail = HALF_NODE, node_id = 2, 
                              leases = {lease4:QRTR_NODE,lease5:QRTR_NODE}, next_cp = T1415)
        avail_node_assertions(time = T1400, avail = HALF_NODE, node_id = 3, 
                              leases = {lease4:HALF_NODE}, next_cp = T1415)
        avail_node_assertions(time = T1400, avail = FULL_NODE, node_id = 4, 
                              leases = {}, next_cp = None)

        # 14:15
        avail_node_assertions(time = T1415, avail = FULL_NODE, node_id = 1, 
                              leases = {}, next_cp = None)
        avail_node_assertions(time = T1415, avail = FULL_NODE, node_id = 2, 
                              leases = {}, next_cp = None)
        avail_node_assertions(time = T1415, avail = FULL_NODE, node_id = 3, 
                              leases = {}, next_cp = None)
        avail_node_assertions(time = T1415, avail = FULL_NODE, node_id = 4, 
                              leases = {}, next_cp = None)
        
        avail = aw.get_ongoing_availability(T1300, 1)
        assert(len(avail.avail_list)==1)
        assert(avail.avail_list[0].available == EMPT_NODE)
        assert(avail.avail_list[0].until     == None)

        avail = aw.get_ongoing_availability(T1300, 2)
        assert(len(avail.avail_list)==3)
        assert(avail.avail_list[0].available == FULL_NODE)
        assert(avail.avail_list[0].until     == T1315)
        assert(avail.avail_list[1].available == HALF_NODE)
        assert(avail.avail_list[1].until     == T1330)
        assert(avail.avail_list[2].available == EMPT_NODE)
        assert(avail.avail_list[2].until     == None)

        avail = aw.get_ongoing_availability(T1300, 3)
        assert(len(avail.avail_list)==2)
        assert(avail.avail_list[0].available == FULL_NODE)
        assert(avail.avail_list[0].until     == T1330)
        assert(avail.avail_list[1].available == EMPT_NODE)
        assert(avail.avail_list[1].until     == None)

        avail = aw.get_ongoing_availability(T1300, 4)
        assert(len(avail.avail_list)==2)
        assert(avail.avail_list[0].available == FULL_NODE)
        assert(avail.avail_list[0].until     == T1330)
        assert(avail.avail_list[1].available == EMPT_NODE)
        assert(avail.avail_list[1].until     == None)


        avail = aw.get_ongoing_availability(T1330, 1)
        assert(len(avail.avail_list)==1)
        assert(avail.avail_list[0].available == FULL_NODE)
        assert(avail.avail_list[0].until     == None)

        avail = aw.get_ongoing_availability(T1330, 2)
        assert(len(avail.avail_list)==1)
        assert(avail.avail_list[0].available == EMPT_NODE)
        assert(avail.avail_list[0].until     == None)

        avail = aw.get_ongoing_availability(T1330, 3)
        assert(len(avail.avail_list)==1)
        assert(avail.avail_list[0].available == EMPT_NODE)
        assert(avail.avail_list[0].until     == None)

        avail = aw.get_ongoing_availability(T1330, 4)
        assert(len(avail.avail_list)==1)
        assert(avail.avail_list[0].available == EMPT_NODE)
        assert(avail.avail_list[0].until     == None)
        
        
        avail = aw.get_ongoing_availability(T1345, 1)
        assert(len(avail.avail_list)==1)
        assert(avail.avail_list[0].available == FULL_NODE)
        assert(avail.avail_list[0].until     == None)

        avail = aw.get_ongoing_availability(T1345, 2)
        assert(len(avail.avail_list)==2)
        assert(avail.avail_list[0].available == FULL_NODE)
        assert(avail.avail_list[0].until     == T1350)
        assert(avail.avail_list[1].available == HALF_NODE)
        assert(avail.avail_list[1].until     == None)

        avail = aw.get_ongoing_availability(T1345, 3)
        assert(len(avail.avail_list)==2)
        assert(avail.avail_list[0].available == FULL_NODE)
        assert(avail.avail_list[0].until     == T1350)
        assert(avail.avail_list[1].available == HALF_NODE)
        assert(avail.avail_list[1].until     == None)

        avail = aw.get_ongoing_availability(T1345, 4)
        assert(len(avail.avail_list)==1)
        assert(avail.avail_list[0].available == EMPT_NODE)
        assert(avail.avail_list[0].until     == None)        

        self.slottable.awcache = None
        aw = self.slottable.get_availability_window(T1415)
        # 14:15
        avail_node_assertions(time = T1415, avail = FULL_NODE, node_id = 1, 
                              leases = {}, next_cp = None)
        avail_node_assertions(time = T1415, avail = FULL_NODE, node_id = 2, 
                              leases = {}, next_cp = None)
        avail_node_assertions(time = T1415, avail = FULL_NODE, node_id = 3, 
                              leases = {}, next_cp = None)
        avail_node_assertions(time = T1415, avail = FULL_NODE, node_id = 4, 
                              leases = {}, next_cp = None)

        avail = aw.get_ongoing_availability(T1415, 1)
        assert(len(avail.avail_list)==1)
        assert(avail.avail_list[0].available == FULL_NODE)
        assert(avail.avail_list[0].until     == None)
        
        avail = aw.get_ongoing_availability(T1415, 2)
        assert(len(avail.avail_list)==1)
        assert(avail.avail_list[0].available == FULL_NODE)
        assert(avail.avail_list[0].until     == None)
        
        avail = aw.get_ongoing_availability(T1415, 3)
        assert(len(avail.avail_list)==1)
        assert(avail.avail_list[0].available == FULL_NODE)
        assert(avail.avail_list[0].until     == None)
        
        avail = aw.get_ongoing_availability(T1415, 4)
        assert(len(avail.avail_list)==1)
        assert(avail.avail_list[0].available == FULL_NODE)
        assert(avail.avail_list[0].until     == None) 
        
        self.slottable, leases = sample_slottable_4()        
        FULL_NODE, HALF_NODE, QRTR_NODE, EMPT_NODE = create_capacities(self.slottable)
        
        lease1,lease2,lease3 = leases
        aw = self.slottable.get_availability_window(T1300)
        
        # 13:30
        avail_node_assertions(time = T1300, avail = FULL_NODE, node_id = 1, 
                              leases = {}, next_cp = T1315)
        avail_node_assertions(time = T1315, avail = HALF_NODE, node_id = 1, 
                              leases = {lease1:HALF_NODE}, next_cp = T1330)
        avail_node_assertions(time = T1330, avail = QRTR_NODE, node_id = 1, 
                              leases = {lease1:HALF_NODE,lease2:QRTR_NODE}, next_cp = T1415)
        
        avail = aw.get_ongoing_availability(T1300, 1)
        assert(len(avail.avail_list)==3)
        assert(avail.avail_list[0].available == FULL_NODE)
        assert(avail.avail_list[0].until     == T1315)
        assert(avail.avail_list[1].available == HALF_NODE)
        assert(avail.avail_list[1].until     == T1330)
        assert(avail.avail_list[2].available == QRTR_NODE)
        assert(avail.avail_list[2].until     == None)
        
                
示例#5
0
    def test_slottable(self):
        def assert_capacity(node, percent):
            assert node.capacity.get_by_type(constants.RES_CPU) == percent * 100
            assert node.capacity.get_by_type(constants.RES_MEM) == percent * 1024
            
        def reservations_1_assertions():
            assert not self.slottable.is_empty()
            nodes = self.slottable.get_availability(T1300)
            assert_capacity(nodes[1], 0.5)
            assert_capacity(nodes[2], 0.5)
            nodes = self.slottable.get_availability(T1330)
            assert_capacity(nodes[1], 1.0)
            assert_capacity(nodes[2], 1.0)
            
        def reservations_2_assertions():
            nodes = self.slottable.get_availability(T1300)
            assert_capacity(nodes[1], 0.5)
            assert_capacity(nodes[2], 0.5)
            rrs = self.slottable.get_reservations_at(T1300)
            assert len(rrs) == 1
            assert rrs[0] == rr1
    
            nodes = self.slottable.get_availability(T1330)
            assert_capacity(nodes[1], 0.75)
            assert_capacity(nodes[2], 0.5)
            rrs = self.slottable.get_reservations_at(T1330)
            assert len(rrs) == 1
            assert rrs[0] == rr2
    
            nodes = self.slottable.get_availability(T1400)
            assert_capacity(nodes[1], 1.0)
            assert_capacity(nodes[2], 1.0)
            rrs = self.slottable.get_reservations_at(T1400)
            assert len(rrs) == 0
            
        def reservations_3_assertions():
            nodes = self.slottable.get_availability(T1300)
            assert_capacity(nodes[1], 0.5)
            assert_capacity(nodes[2], 0.5)
            rrs = self.slottable.get_reservations_at(T1300)
            assert len(rrs) == 1
            assert rrs[0] == rr1
    
            nodes = self.slottable.get_availability(T1315)
            assert_capacity(nodes[1], 0.25)
            assert_capacity(nodes[2], 0.25)
            rrs = self.slottable.get_reservations_at(T1315)
            assert len(rrs) == 2
            assert rr1 in rrs and rr3 in rrs

            nodes = self.slottable.get_availability(T1330)
            assert_capacity(nodes[1], 0.5)
            assert_capacity(nodes[2], 0.25)
            rrs = self.slottable.get_reservations_at(T1330)
            assert len(rrs) == 2
            assert rr2 in rrs and rr3 in rrs

            nodes = self.slottable.get_availability(T1345)
            assert_capacity(nodes[1], 0.75)
            assert_capacity(nodes[2], 0.5)
            rrs = self.slottable.get_reservations_at(T1345)
            assert len(rrs) == 1
            assert rrs[0] == rr2
    
            nodes = self.slottable.get_availability(T1400)
            assert_capacity(nodes[1], 1.0)
            assert_capacity(nodes[2], 1.0)
            rrs = self.slottable.get_reservations_at(T1400)
            assert len(rrs) == 0

        def reservations_4_assertions():
            nodes = self.slottable.get_availability(T1300)
            assert_capacity(nodes[1], 0.5)
            assert_capacity(nodes[2], 0.5)
            rrs = self.slottable.get_reservations_at(T1300)
            assert len(rrs) == 1
            assert rrs[0] == rr1
    
            nodes = self.slottable.get_availability(T1315)
            assert_capacity(nodes[1], 0.25)
            assert_capacity(nodes[2], 0.25)
            rrs = self.slottable.get_reservations_at(T1315)
            assert len(rrs) == 2
            assert rr1 in rrs and rr3 in rrs

            nodes = self.slottable.get_availability(T1330)
            assert_capacity(nodes[1], 0)
            assert_capacity(nodes[2], 0)
            rrs = self.slottable.get_reservations_at(T1330)
            assert len(rrs) == 3
            assert rr4 in rrs and rr2 in rrs and rr3 in rrs

            nodes = self.slottable.get_availability(T1345)
            assert_capacity(nodes[1], 0.25)
            assert_capacity(nodes[2], 0.25)
            rrs = self.slottable.get_reservations_at(T1345)
            assert len(rrs) == 2
            assert rr2 in rrs and rr4 in rrs
    
            nodes = self.slottable.get_availability(T1400)
            assert_capacity(nodes[1], 0.5)
            assert_capacity(nodes[2], 0.75)
            rrs = self.slottable.get_reservations_at(T1400)
            assert len(rrs) == 1
            assert rrs[0] == rr4
    
            nodes = self.slottable.get_availability(T1415)
            assert_capacity(nodes[1], 1.0)
            assert_capacity(nodes[2], 1.0)
            rrs = self.slottable.get_reservations_at(T1415)
            assert len(rrs) == 0
        
        def reservations_5_assertions():
            nodes = self.slottable.get_availability(T1300)
            assert_capacity(nodes[1], 0.5)
            assert_capacity(nodes[2], 0.5)
            rrs = self.slottable.get_reservations_at(T1300)
            assert len(rrs) == 1
            assert rrs[0] == rr1
    
            nodes = self.slottable.get_availability(T1315)
            assert_capacity(nodes[1], 0.25)
            assert_capacity(nodes[2], 0.25)
            rrs = self.slottable.get_reservations_at(T1315)
            assert len(rrs) == 2
            assert set(rrs) == set([rr1,rr3])

            nodes = self.slottable.get_availability(T1330)
            assert_capacity(nodes[1], 0)
            assert_capacity(nodes[2], 0)
            rrs = self.slottable.get_reservations_at(T1330)
            assert len(rrs) == 3
            assert set(rrs) == set([rr2,rr3,rr4])

            nodes = self.slottable.get_availability(T1345)
            assert_capacity(nodes[1], 0.25)
            assert_capacity(nodes[2], 0)
            rrs = self.slottable.get_reservations_at(T1345)
            assert len(rrs) == 3
            assert set(rrs) == set([rr2,rr4,rr5])
    
            nodes = self.slottable.get_availability(T1400)
            assert_capacity(nodes[1], 0.5)
            assert_capacity(nodes[2], 0.5)
            rrs = self.slottable.get_reservations_at(T1400)
            assert len(rrs) == 2
            assert set(rrs) == set([rr4,rr5])
    
            nodes = self.slottable.get_availability(T1415)
            assert_capacity(nodes[1], 1.0)
            assert_capacity(nodes[2], 1.0)
            rrs = self.slottable.get_reservations_at(T1415)
            assert len(rrs) == 0
            
            rrs = self.slottable.get_reservations_starting_between(T1300, T1315)
            assert set(rrs) == set([rr1,rr3])
            rrs = self.slottable.get_reservations_starting_between(T1300, T1330)
            assert set(rrs) == set([rr1,rr2,rr3,rr4])
            rrs = self.slottable.get_reservations_starting_between(T1300, T1345)
            assert set(rrs) == set([rr1,rr2,rr3,rr4,rr5])
            rrs = self.slottable.get_reservations_starting_between(T1315, T1330)
            assert set(rrs) == set([rr2,rr3,rr4])
            rrs = self.slottable.get_reservations_starting_between(T1315, T1345)
            assert set(rrs) == set([rr2,rr3,rr4,rr5])
            rrs = self.slottable.get_reservations_starting_between(T1330, T1345)
            assert set(rrs) == set([rr2,rr4,rr5])
            rrs = self.slottable.get_reservations_starting_between(T1400, T1415)
            assert len(rrs) == 0
            rrs = self.slottable.get_reservations_starting_between(T1305, T1335)
            assert set(rrs) == set([rr3,rr2,rr4])

            rrs = self.slottable.get_reservations_ending_between(T1300, T1305)
            assert len(rrs) == 0
            rrs = self.slottable.get_reservations_ending_between(T1300, T1315)
            assert len(rrs) == 0
            rrs = self.slottable.get_reservations_ending_between(T1300, T1330)
            assert set(rrs) == set([rr1])
            rrs = self.slottable.get_reservations_ending_between(T1300, T1335)
            assert set(rrs) == set([rr1])
            rrs = self.slottable.get_reservations_ending_between(T1300, T1345)
            assert set(rrs) == set([rr1,rr3])
            rrs = self.slottable.get_reservations_ending_between(T1300, T1400)
            assert set(rrs) == set([rr1,rr2,rr3])
            rrs = self.slottable.get_reservations_ending_between(T1300, T1415)
            assert set(rrs) == set([rr1,rr2,rr3,rr4,rr5])
            rrs = self.slottable.get_reservations_ending_between(T1305, T1315)
            assert len(rrs) == 0
            rrs = self.slottable.get_reservations_ending_between(T1305, T1330)
            assert set(rrs) == set([rr1])
            rrs = self.slottable.get_reservations_ending_between(T1305, T1335)
            assert set(rrs) == set([rr1])
            rrs = self.slottable.get_reservations_ending_between(T1305, T1345)
            assert set(rrs) == set([rr1,rr3])
            rrs = self.slottable.get_reservations_ending_between(T1305, T1400)
            assert set(rrs) == set([rr1,rr2,rr3])
            rrs = self.slottable.get_reservations_ending_between(T1305, T1415)
            assert set(rrs) == set([rr1,rr2,rr3,rr4,rr5])
            rrs = self.slottable.get_reservations_ending_between(T1315, T1330)
            assert set(rrs) == set([rr1])
            rrs = self.slottable.get_reservations_ending_between(T1315, T1335)
            assert set(rrs) == set([rr1])
            rrs = self.slottable.get_reservations_ending_between(T1315, T1345)
            assert set(rrs) == set([rr1,rr3])
            rrs = self.slottable.get_reservations_ending_between(T1315, T1400)
            assert set(rrs) == set([rr1,rr2,rr3])
            rrs = self.slottable.get_reservations_ending_between(T1315, T1415)
            assert set(rrs) == set([rr1,rr2,rr3,rr4,rr5])
            rrs = self.slottable.get_reservations_ending_between(T1330, T1335)
            assert set(rrs) == set([rr1])
            rrs = self.slottable.get_reservations_ending_between(T1330, T1345)
            assert set(rrs) == set([rr1,rr3])
            rrs = self.slottable.get_reservations_ending_between(T1330, T1400)
            assert set(rrs) == set([rr1,rr2,rr3])
            rrs = self.slottable.get_reservations_ending_between(T1330, T1415)
            assert set(rrs) == set([rr1,rr2,rr3,rr4,rr5])
            rrs = self.slottable.get_reservations_ending_between(T1335, T1345)
            assert set(rrs) == set([rr3])
            rrs = self.slottable.get_reservations_ending_between(T1335, T1400)
            assert set(rrs) == set([rr2,rr3])
            rrs = self.slottable.get_reservations_ending_between(T1335, T1415)
            assert set(rrs) == set([rr2,rr3,rr4,rr5])
            rrs = self.slottable.get_reservations_ending_between(T1345, T1400)
            assert set(rrs) == set([rr2,rr3])
            rrs = self.slottable.get_reservations_ending_between(T1345, T1415)
            assert set(rrs) == set([rr2,rr3,rr4,rr5])
            rrs = self.slottable.get_reservations_ending_between(T1400, T1415)
            assert set(rrs) == set([rr2,rr4,rr5])
            
            rrs = self.slottable.get_reservations_starting_on_or_after(T1300)
            assert set(rrs) == set([rr1,rr2,rr3,rr4,rr5])
            rrs = self.slottable.get_reservations_starting_on_or_after(T1305)
            assert set(rrs) == set([rr2,rr3,rr4,rr5])
            rrs = self.slottable.get_reservations_starting_on_or_after(T1315)
            assert set(rrs) == set([rr2,rr3,rr4,rr5])
            rrs = self.slottable.get_reservations_starting_on_or_after(T1330)
            assert set(rrs) == set([rr2,rr4,rr5])
            rrs = self.slottable.get_reservations_starting_on_or_after(T1335)
            assert set(rrs) == set([rr5])
            rrs = self.slottable.get_reservations_starting_on_or_after(T1345)
            assert set(rrs) == set([rr5])
            rrs = self.slottable.get_reservations_starting_on_or_after(T1400)
            assert len(rrs) == 0
            rrs = self.slottable.get_reservations_starting_on_or_after(T1415)
            assert len(rrs) == 0
            
            rrs = self.slottable.get_reservations_ending_on_or_after(T1300)
            assert set(rrs) == set([rr1,rr2,rr3,rr4,rr5])
            rrs = self.slottable.get_reservations_ending_on_or_after(T1305)
            assert set(rrs) == set([rr1,rr2,rr3,rr4,rr5])
            rrs = self.slottable.get_reservations_ending_on_or_after(T1315)
            assert set(rrs) == set([rr1,rr2,rr3,rr4,rr5])
            rrs = self.slottable.get_reservations_ending_on_or_after(T1330)
            assert set(rrs) == set([rr1,rr2,rr3,rr4,rr5])
            rrs = self.slottable.get_reservations_ending_on_or_after(T1335)
            assert set(rrs) == set([rr2,rr3,rr4,rr5])
            rrs = self.slottable.get_reservations_ending_on_or_after(T1345)
            assert set(rrs) == set([rr2,rr3,rr4,rr5])
            rrs = self.slottable.get_reservations_ending_on_or_after(T1400)
            assert set(rrs) == set([rr2,rr4,rr5])
            rrs = self.slottable.get_reservations_ending_on_or_after(T1415)
            assert set(rrs) == set([rr4,rr5])
            
            assert self.slottable.get_next_changepoint(T1255) == T1300
            assert self.slottable.get_next_changepoint(T1300) == T1315
            assert self.slottable.get_next_changepoint(T1315) == T1330
            assert self.slottable.get_next_changepoint(T1330) == T1345
            assert self.slottable.get_next_changepoint(T1335) == T1345
            assert self.slottable.get_next_changepoint(T1345) == T1400
            assert self.slottable.get_next_changepoint(T1400) == T1415
            assert self.slottable.get_next_changepoint(T1415) == None
            assert self.slottable.get_next_changepoint(T1420) == None
        
        self.slottable = SlotTable([(constants.RES_CPU,ResourceTuple.SINGLE_INSTANCE),(constants.RES_MEM,ResourceTuple.SINGLE_INSTANCE)])
        FULL_NODE, HALF_NODE, QRTR_NODE, EMPT_NODE = create_capacities(self.slottable)
        
        self.slottable.add_node(1, FULL_NODE)
        self.slottable.add_node(2, FULL_NODE)  
        
        assert self.slottable.get_total_capacity(constants.RES_CPU) == 200
        assert self.slottable.get_total_capacity(constants.RES_MEM) == 2048
        assert self.slottable.is_empty()
    
        res1 = {1: HALF_NODE, 2: HALF_NODE}
        rr1 = ResourceReservation(None, T1300, T1330, res1)
        self.slottable.add_reservation(rr1)
        reservations_1_assertions()

        res2 = {1: QRTR_NODE, 2: HALF_NODE}
        rr2 = ResourceReservation(None, T1330, T1400, res2)
        self.slottable.add_reservation(rr2)
        reservations_2_assertions()

        res3 = {1: QRTR_NODE, 2: QRTR_NODE}
        rr3 = ResourceReservation(None, T1315, T1345, res3)
        self.slottable.add_reservation(rr3)
        reservations_3_assertions()

        res4 = {1: HALF_NODE, 2: QRTR_NODE}
        rr4 = ResourceReservation(None, T1330, T1415, res4)
        self.slottable.add_reservation(rr4)
        reservations_4_assertions()

        res5 = {2: QRTR_NODE}
        rr5 = ResourceReservation(None, T1345, T1415, res5)
        self.slottable.add_reservation(rr5)
        reservations_5_assertions()

        self.slottable.remove_reservation(rr5)
        reservations_4_assertions()
        self.slottable.remove_reservation(rr4)
        reservations_3_assertions()
        self.slottable.remove_reservation(rr3)
        reservations_2_assertions()
        self.slottable.remove_reservation(rr2)
        reservations_1_assertions()
        self.slottable.remove_reservation(rr1)
        
        assert self.slottable.is_empty()
示例#6
0
def sample_slottable_1():
    slottable = SlotTable([(constants.RES_CPU,ResourceTuple.SINGLE_INSTANCE),(constants.RES_MEM,ResourceTuple.SINGLE_INSTANCE)])
    FULL_NODE, HALF_NODE, QRTR_NODE, EMPT_NODE = create_capacities(slottable)
    
    slottable.add_node(1, FULL_NODE)
    slottable.add_node(2, FULL_NODE)  
    slottable.add_node(3, FULL_NODE)  
    slottable.add_node(4, FULL_NODE)  

    lease1 = Lease.create_new(None,{},None,None,None,1,None)
    lease1.id = 1
    res1 = {2: HALF_NODE}
    rr1_1 = ResourceReservation(lease1, T1315, T1325, res1)
    rr1_2 = ResourceReservation(lease1, T1325, T1330, res1)
    slottable.add_reservation(rr1_1)
    slottable.add_reservation(rr1_2)

    lease2 = Lease.create_new(None,{},None,None,None,2,None)
    lease2.id = 2
    res2 = {2: FULL_NODE, 3: FULL_NODE}
    rr2 = ResourceReservation(lease2, T1330, T1345, res2)
    slottable.add_reservation(rr2)

    lease3 = Lease.create_new(None,{},None,None,None,1,None)
    lease3.id = 3
    res3 = {4: FULL_NODE}
    rr3_1 = ResourceReservation(lease3, T1330, T1355, res3)
    rr3_2 = ResourceReservation(lease3, T1355, T1400, res3)
    slottable.add_reservation(rr3_1)
    slottable.add_reservation(rr3_2)

    lease4 = Lease.create_new(None,{},None,None,None,1,None)
    lease4.id = 4
    res4 = {2: QRTR_NODE, 3: HALF_NODE}
    rr4 = ResourceReservation(lease4, T1350, T1415, res4)
    slottable.add_reservation(rr4)

    lease5 = Lease.create_new(None,{},None,None,None,1,None)
    lease5.id = 5
    res5 = {2: QRTR_NODE}
    rr5 = ResourceReservation(lease5, T1350, T1415, res5)
    slottable.add_reservation(rr5)
    
    lease6 = Lease.create_new(None,{},None,None,None,1,None)
    lease6.id = 6
    res6 = {1: FULL_NODE}
    rr6 = ResourceReservation(lease6, T1255, T1305, res6)
    slottable.add_reservation(rr6)      
    
    return slottable, [lease1,lease2,lease3,lease4,lease5,lease6]
示例#7
0
class Manager(object):
    """The root of Haizea
    
    This class is the root of Haizea. Pretty much everything else (scheduler,
    enactment modules, etc.) is contained in this class. The Manager
    class is meant to be a singleton.
    
    """
    
    __metaclass__ = Singleton
    
    def __init__(self, config, daemon=False, pidfile=None, logging_handler=None, site=None):
        """Initializes the manager.
        
        Argument:site
        config -- a populated instance of haizea.common.config.RMConfig
        daemon -- True if Haizea must run as a daemon, False if it must
                  run in the foreground
        pidfile -- When running as a daemon, file to save pid to
        """
        self.config = config
        self.logging_handler = logging_handler
        # Create the RM components
        
        mode = config.get("mode")
        
        self.daemon = daemon
        self.pidfile = pidfile

        if mode == "simulated":
            # Simulated-time simulations always run in the foreground
            clock = self.config.get("clock")
            if clock == constants.CLOCK_SIMULATED:
                self.daemon = False
        elif mode == "opennebula":
            clock = constants.CLOCK_REAL        
        
        self.init_logging()
                
        if clock == constants.CLOCK_SIMULATED:
            starttime = self.config.get("starttime")
            self.clock = SimulatedClock(self, starttime)
            self.rpc_server = None
        elif clock == constants.CLOCK_REAL:
            wakeup_interval = self.config.get("wakeup-interval")
            non_sched = self.config.get("non-schedulable-interval")
            if mode == "opennebula":
                fastforward = self.config.get("dry-run")
            else:
                fastforward = False
            self.clock = RealClock(self, wakeup_interval, non_sched, fastforward)
            if fastforward:
                # No need for an RPC server when doing a dry run
                self.rpc_server = None
            else:
                self.rpc_server = RPCServer(self)
                    
        # Create the RPC singleton client for OpenNebula mode
        if mode == "opennebula":
            host = self.config.get("one.host")
            port = self.config.get("one.port")
            rv = OpenNebulaXMLRPCClient.get_userpass_from_env()
            if rv == None:
                print "ONE_AUTH environment variable is not set"
                exit(1)
            else:
                user, passw = rv[0], rv[1]
                try:
                    OpenNebulaXMLRPCClientSingleton(host, port, user, passw)
                except socket.error, e:
                    print "Unable to connect to OpenNebula"
                    print "Reason: %s" % e
                    exit(1)
                    
        # Enactment modules
        if mode == "simulated":
            if not site:
                resources = self.config.get("simul.resources")
                if resources == "in-tracefile":
                    tracefile = os.path.expanduser(self.config.get("tracefile"))
                    self.site = Site.from_lwf_file(tracefile)
                elif resources.startswith("file:"):
                    sitefile = resources.split(":")
                    self.site = Site.from_xml_file(sitefile)
                else:
                    self.site = Site.from_resources_string(resources)
            else:
                self.site = site
            
            deploy_bandwidth = config.get("imagetransfer-bandwidth")
            info_enact = SimulatedResourcePoolInfo(self.site)
            vm_enact = SimulatedVMEnactment()
            deploy_enact = SimulatedDeploymentEnactment(deploy_bandwidth)
        elif mode == "opennebula":
            # Enactment modules
            info_enact = OpenNebulaResourcePoolInfo()
            vm_enact = OpenNebulaVMEnactment()
            # No deployment in OpenNebula. Using dummy one for now.
            deploy_enact = OpenNebulaDummyDeploymentEnactment()            

        if mode == "simulated":
            preparation_type = self.config.get("lease-preparation")
        elif mode == "opennebula":
            # No deployment in OpenNebula.
            preparation_type = constants.PREPARATION_UNMANAGED

        # Resource pool
        if preparation_type == constants.PREPARATION_TRANSFER:
            if self.config.get("diskimage-reuse") == constants.REUSE_IMAGECACHES:
                resourcepool = ResourcePoolWithReusableImages(info_enact, vm_enact, deploy_enact)
            else:
                resourcepool = ResourcePool(info_enact, vm_enact, deploy_enact)
        else:
            resourcepool = ResourcePool(info_enact, vm_enact, deploy_enact)
    
        # Slot table
        slottable = SlotTable(info_enact.get_resource_types())
        for n in resourcepool.get_nodes() + resourcepool.get_aux_nodes():
            rt = slottable.create_resource_tuple_from_capacity(n.capacity)
            slottable.add_node(n.id, rt)

        # Policy manager
        admission = self.config.get("policy.admission")
        admission = admission_class_mappings.get(admission, admission)
        admission = import_class(admission)
        admission = admission(slottable)
        
        preemption = self.config.get("policy.preemption")
        preemption = preemption_class_mappings.get(preemption, preemption)
        preemption = import_class(preemption)
        preemption = preemption(slottable)

        host_selection = self.config.get("policy.host-selection")
        host_selection = host_class_mappings.get(host_selection, host_selection)
        host_selection = import_class(host_selection)
        host_selection = host_selection(slottable)
        
        matchmaking = self.config.get("policy.matchmaking")
        matchmaking = matchmaking_class_mappings.get(matchmaking, matchmaking)
        matchmaking = import_class(matchmaking)
        matchmaking = matchmaking(slottable)
        
        self.policy = PolicyManager(admission, preemption, host_selection, matchmaking)

        # Preparation scheduler
        if preparation_type == constants.PREPARATION_UNMANAGED:
            preparation_scheduler = UnmanagedPreparationScheduler(slottable, resourcepool, deploy_enact)
        elif preparation_type == constants.PREPARATION_TRANSFER:
            preparation_scheduler = ImageTransferPreparationScheduler(slottable, resourcepool, deploy_enact)    
    
        # VM mapper and scheduler
        mapper = self.config.get("mapper")
        mapper = mapper_mappings.get(mapper, mapper)
        mapper = import_class(mapper)
        mapper = mapper(slottable, self.policy)
        
        # When using backfilling, set the number of leases that can be
        # scheduled in the future.
        backfilling = self.config.get("backfilling")
        if backfilling == constants.BACKFILLING_OFF:
            max_in_future = 0
        elif backfilling == constants.BACKFILLING_AGGRESSIVE:
            max_in_future = 1
        elif backfilling == constants.BACKFILLING_CONSERVATIVE:
            max_in_future = -1 # Unlimited
        elif backfilling == constants.BACKFILLING_INTERMEDIATE:
            max_in_future = self.config.get("backfilling-reservations")
        vm_scheduler = VMScheduler(slottable, resourcepool, mapper, max_in_future)
    
        # Statistics collection 
        attrs = dict([(attr, self.config.get_attr(attr)) for attr in self.config.get_attrs()])    
        self.accounting = AccountingDataCollection(os.path.expanduser(self.config.get("datafile")), attrs)
        # Load probes
        probes = self.config.get("accounting-probes")
        probes = probes.split()
        for probe in probes:
            probe_class = probe_class_mappings.get(probe, probe)
            probe_class = import_class(probe_class)
            probe_obj = probe_class(self.accounting)
            self.accounting.add_probe(probe_obj)    
    
        # Lease Scheduler
        self.scheduler = LeaseScheduler(vm_scheduler, preparation_scheduler, slottable, self.accounting)
                                
        # Lease request frontends
        if mode == "simulated":
            if clock == constants.CLOCK_SIMULATED:
                # In pure simulation, we can only use the tracefile frontend
                self.frontends = [TracefileFrontend(self.clock.get_start_time())]
            elif clock == constants.CLOCK_REAL:
                # In simulation with a real clock, only the RPC frontend can be used
                self.frontends = [RPCFrontend()]             
        elif mode == "opennebula":
            self.frontends = [OpenNebulaFrontend()]               

        persistence_file = self.config.get("persistence-file")
        if persistence_file == "none":
            persistence_file = None
        
        self.persistence = PersistenceManager(persistence_file)
        
        self.logger = logging.getLogger("RM")