Esempio n. 1
0
    def test_get_schedulers(self, leaf_scheduler_cls):
        leaf_scheduler = MagicMock()
        leaf_scheduler_cls.return_value = leaf_scheduler

        common.services.register(Host.Iface, MagicMock())
        scheduler_handler = SchedulerHandler()
        common.services.register(Scheduler.Iface, scheduler_handler)
        agent_config = MagicMock()
        agent_config.reboot_required = False
        common.services.register(ServiceName.AGENT_CONFIG, agent_config)
        common.services.register(ServiceName.REQUEST_ID, MagicMock())

        agent_control_handler = AgentControlHandler()
        request = GetSchedulersRequest()
        response = agent_control_handler.get_schedulers(request)

        child_host = ChildInfo(id="foo", address="address", port=12345)

        assert_that(response.schedulers, is_(empty()))
        leaf_scheduler_id = "leaf1"
        scheduler_handler.configure(
            [SchedulerRole(leaf_scheduler_id, "parent1",
                           host_children=[child_host])])
        leaf_scheduler._get_hosts.return_value = [child_host]

        response = agent_control_handler.get_schedulers(request)
        assert_that(response.schedulers, has_length(1))

        scheduler = response.schedulers[0]
        assert_that(scheduler.role.id, is_(leaf_scheduler_id))
        assert_that(scheduler.role.host_children[0], is_(child_host))
        assert_that(len(scheduler.role.host_children), is_(1))
        assert_that(response.result, is_(GetSchedulersResultCode.OK))
    def test_host_find(self):
        host_handler = common.services.get(Host.Iface)
        host_handler.find = MagicMock()

        handler = SchedulerHandler()
        request = FindRequest(scheduler_id="foo")
        handler.host_find(request)
        host_handler.find.assert_called_once_with(request)
    def test_host_place(self):
        host_handler = common.services.get(Host.Iface)
        host_handler.place = MagicMock()

        handler = SchedulerHandler()
        request = PlaceRequest(Resource(), "foo")
        handler.host_place(request)
        host_handler.place.assert_called_once_with(request)
    def test_find_system_error(self):
        scheduler = MagicMock()
        scheduler.find.side_effect = ValueError

        handler = SchedulerHandler()
        handler._schedulers["foo"] = scheduler
        response = handler.find(FindRequest(scheduler_id="foo"))

        assert_that(response.result, is_(FindResultCode.SYSTEM_ERROR))
    def test_place_system_error(self):
        scheduler = MagicMock()
        scheduler.place.side_effect = ValueError

        handler = SchedulerHandler()
        handler._schedulers["foo"] = scheduler
        response = handler.place(PlaceRequest(Resource(), "foo"))

        assert_that(response.result, is_(PlaceResultCode.SYSTEM_ERROR))
    def test_find_system_error(self):
        scheduler = MagicMock()
        scheduler.find.side_effect = ValueError

        handler = SchedulerHandler()
        handler._schedulers["foo"] = scheduler
        response = handler.find(FindRequest(scheduler_id="foo"))

        assert_that(response.result, is_(FindResultCode.SYSTEM_ERROR))
    def test_place_system_error(self):
        scheduler = MagicMock()
        scheduler.place.side_effect = ValueError

        handler = SchedulerHandler()
        handler._schedulers["foo"] = scheduler
        response = handler.place(PlaceRequest(Resource(), "foo"))

        assert_that(response.result, is_(PlaceResultCode.SYSTEM_ERROR))
    def test_find_response(self):
        response = FindResponse(FindResultCode.OK)

        scheduler = MagicMock()
        scheduler.find.return_value = response

        handler = SchedulerHandler()
        handler._schedulers["foo"] = scheduler
        actual_response = handler.find(FindRequest(Locator(), "foo"))

        assert_that(actual_response, is_(same_instance(response)))
    def test_place_response(self):
        response = PlaceResponse(PlaceResultCode.OK)

        scheduler = MagicMock()
        scheduler.place.return_value = response

        handler = SchedulerHandler()
        handler._schedulers["foo"] = scheduler
        actual_response = handler.place(PlaceRequest(Resource(), "foo"))

        assert_that(actual_response, is_(same_instance(response)))
    def test_place_response(self):
        response = PlaceResponse(PlaceResultCode.OK)

        scheduler = MagicMock()
        scheduler.place.return_value = response

        handler = SchedulerHandler()
        handler._schedulers["foo"] = scheduler
        actual_response = handler.place(PlaceRequest(Resource(), "foo"))

        assert_that(actual_response, is_(same_instance(response)))
    def test_find_response(self):
        response = FindResponse(FindResultCode.OK)

        scheduler = MagicMock()
        scheduler.find.return_value = response

        handler = SchedulerHandler()
        handler._schedulers["foo"] = scheduler
        actual_response = handler.find(FindRequest(Locator(), "foo"))

        assert_that(actual_response, is_(same_instance(response)))
    def test_configure_create_leaf_scheduler(self, leaf_scheduler_cls):
        leaf_scheduler = MagicMock()
        leaf_scheduler_cls.return_value = leaf_scheduler

        handler = SchedulerHandler()
        assert_that(handler._schedulers, is_(empty()))

        handler.configure(
            [SchedulerRole("leaf-scheduler", "parent-id",
                           host_children=[ChildInfo(id="foo")])])
        assert_that(handler._schedulers, has_length(1))

        scheduler = handler._schedulers["leaf-scheduler"]
        assert_that(scheduler.id, is_(leaf_scheduler.id))
        scheduler.configure.assert_called_once_with([ChildInfo(id="foo")])
    def test_configure_cleanup_scheduler(self, leaf_scheduler_cls):
        leaf_scheduler = MagicMock()
        leaf_scheduler_cls.return_value = leaf_scheduler

        handler = SchedulerHandler()

        handler.configure(
            [SchedulerRole("leaf-scheduler", "parent-id",
                           host_children=[ChildInfo(id="foo")])])
        scheduler = handler._schedulers["leaf-scheduler"]
        assert_that(scheduler, is_(leaf_scheduler))

        handler.configure([])

        assert_that(scheduler.cleanup.called, is_(True))
    def test_configure_update_scheduler(self):
        handler = SchedulerHandler()

        handler.configure(
            [SchedulerRole("leaf-scheduler", "parent-id",
                           host_children=[ChildInfo(id="foo")])], False)
        old_scheduler = handler._schedulers["leaf-scheduler"]

        handler.configure(
            [SchedulerRole("leaf-scheduler", "parent-id",
                           host_children=[ChildInfo(id="bar")])], False)
        new_scheduler = handler._schedulers["leaf-scheduler"]
        assert_that(new_scheduler._scheduler_id,
                    equal_to(old_scheduler._scheduler_id))
        assert_that(new_scheduler._hosts, has_length(1))
        assert_that(new_scheduler._hosts[0].id, equal_to("bar"))
    def test_configure_create_leaf_scheduler(self, leaf_scheduler_cls):
        leaf_scheduler = MagicMock()
        leaf_scheduler_cls.return_value = leaf_scheduler

        handler = SchedulerHandler()
        assert_that(handler._schedulers, is_(empty()))

        handler.configure([
            SchedulerRole("leaf-scheduler",
                          "parent-id",
                          host_children=[ChildInfo(id="foo")])
        ])
        assert_that(handler._schedulers, has_length(1))

        scheduler = handler._schedulers["leaf-scheduler"]
        assert_that(scheduler.id, is_(leaf_scheduler.id))
        scheduler.configure.assert_called_once_with([ChildInfo(id="foo")])
    def test_configure_bad_roles(self):
        handler = SchedulerHandler()

        self.assertRaises(ValueError, handler.configure,
                          [SchedulerRole("scheduler-id", "parent-id")])

        self.assertRaises(
            ValueError, handler.configure,
            [SchedulerRole("scheduler-id", "parent-id", ["foo"], ["bar"])])
    def test_place_during_reconfigure(self):
        # Create leaf scheduler which will return RESOURCE_CONSTRAINT
        scheduler = LeafScheduler("foo", 1, False)
        scheduler._placement_hosts = MagicMock()
        scheduler._placement_hosts.return_value = []

        # Create scheduler handler
        handler = SchedulerHandler()
        handler._create_scheduler = MagicMock()
        handler._create_scheduler.return_value = scheduler

        # Configure scheduler handler with leaf scheduler
        handler.configure([
            SchedulerRole("foo",
                          "parent-id",
                          scheduler_children=[ChildInfo(id="child")])
        ])
        assert_that(handler._schedulers, has_length(1))

        concurrency = 5
        threads = []
        results = {}
        done = [False]

        # Define the thread which keeps calling place, to make sure it
        # always return RESOURCE_CONSTRAINT
        def _loop():
            while True:
                actual_response = handler.place(PlaceRequest(
                    Resource(), "foo"))
                assert_that(actual_response.result,
                            is_not(PlaceResultCode.SYSTEM_ERROR))

                if done[0]:
                    results[threading.current_thread().name] = True
                    break

        for _ in xrange(concurrency):
            thread = threading.Thread(target=_loop)
            thread.start()
            threads.append(thread)

        # Reconfigure scheduler to demote it. To test when reconfiguring,
        # there is no race when new schedulers replacing the old ones.
        handler.configure([])
        assert_that(handler._schedulers, has_length(0))
        time.sleep(0.1)
        done[0] = True

        for thread in threads:
            thread.join()

        assert_that(len(results), equal_to(concurrency))
    def test_place_during_reconfigure(self):
        # Create leaf scheduler which will return RESOURCE_CONSTRAINT
        scheduler = LeafScheduler("foo", 1, False)
        scheduler._placement_hosts = MagicMock()
        scheduler._placement_hosts.return_value = []

        # Create scheduler handler
        handler = SchedulerHandler()
        handler._create_scheduler = MagicMock()
        handler._create_scheduler.return_value = scheduler

        # Configure scheduler handler with leaf scheduler
        handler.configure(
            [SchedulerRole("foo", "parent-id",
                           scheduler_children=[ChildInfo(id="child")])])
        assert_that(handler._schedulers, has_length(1))

        concurrency = 5
        threads = []
        results = {}
        done = [False]

        # Define the thread which keeps calling place, to make sure it
        # always return RESOURCE_CONSTRAINT
        def _loop():
            while True:
                actual_response = handler.place(
                    PlaceRequest(Resource(), "foo"))
                assert_that(actual_response.result,
                            is_not(PlaceResultCode.SYSTEM_ERROR))

                if done[0]:
                    results[threading.current_thread().name] = True
                    break

        for _ in xrange(concurrency):
            thread = threading.Thread(target=_loop)
            thread.start()
            threads.append(thread)

        # Reconfigure scheduler to demote it. To test when reconfiguring,
        # there is no race when new schedulers replacing the old ones.
        handler.configure([])
        assert_that(handler._schedulers, has_length(0))
        time.sleep(0.1)
        done[0] = True

        for thread in threads:
            thread.join()

        assert_that(len(results), equal_to(concurrency))
Esempio n. 19
0
    def init(self):
        # Load agent config
        config = common.services.get(ServiceName.AGENT_CONFIG)

        # Create scheduler handler
        scheduler_handler = SchedulerHandler(config.utilization_transfer_ratio)
        common.services.register(Scheduler.Iface, scheduler_handler)

        # Load num_threads
        num_threads = config.scheduler_service_threads

        # Define and add thrift service
        service = common.plugin.ThriftService(
            name="Scheduler",
            service=Scheduler,
            handler=scheduler_handler,
            num_threads=num_threads,
        )

        self.add_thrift_service(service)
    def test_configure_cleanup_scheduler(self, leaf_scheduler_cls):
        leaf_scheduler = MagicMock()
        leaf_scheduler_cls.return_value = leaf_scheduler

        handler = SchedulerHandler()

        handler.configure([
            SchedulerRole("leaf-scheduler",
                          "parent-id",
                          host_children=[ChildInfo(id="foo")])
        ])
        scheduler = handler._schedulers["leaf-scheduler"]
        assert_that(scheduler, is_(leaf_scheduler))

        handler.configure([])

        assert_that(scheduler.cleanup.called, is_(True))
    def test_configure_update_scheduler(self):
        handler = SchedulerHandler()

        handler.configure([
            SchedulerRole("leaf-scheduler",
                          "parent-id",
                          host_children=[ChildInfo(id="foo")])
        ], False)
        old_scheduler = handler._schedulers["leaf-scheduler"]

        handler.configure([
            SchedulerRole("leaf-scheduler",
                          "parent-id",
                          host_children=[ChildInfo(id="bar")])
        ], False)
        new_scheduler = handler._schedulers["leaf-scheduler"]
        assert_that(new_scheduler._scheduler_id,
                    equal_to(old_scheduler._scheduler_id))
        assert_that(new_scheduler._hosts, has_length(1))
        assert_that(new_scheduler._hosts[0].id, equal_to("bar"))
Esempio n. 22
0
import common

from common.service_name import ServiceName
from gen.scheduler import Scheduler
from scheduler.scheduler_handler import SchedulerHandler


# Load agent config and registrant
try:
    config = common.services.get(ServiceName.AGENT_CONFIG)
except Exception as e:
    raise ImportError(e)

# Create scheduler handler
scheduler_handler = SchedulerHandler(config.utilization_transfer_ratio)
common.services.register(Scheduler.Iface, scheduler_handler)

# Load num_threads
try:
    num_threads = config.scheduler_service_threads
except Exception as e:
    raise ImportError(e)


# Define scheduler plugin
plugin = common.plugin.Plugin(
    name="Scheduler",
    service=Scheduler,
    handler=scheduler_handler,
    num_threads=num_threads,
    def test_find_missing_scheduler(self):
        handler = SchedulerHandler()
        response = handler.find(FindRequest(Locator()))

        assert_that(response.result, is_(FindResultCode.INVALID_SCHEDULER))
    def test_find_missing_scheduler(self):
        handler = SchedulerHandler()
        response = handler.find(FindRequest(Locator()))

        assert_that(response.result, is_(FindResultCode.INVALID_SCHEDULER))
    def test_place_missing_scheduler(self):
        handler = SchedulerHandler()
        response = handler.place(PlaceRequest(Resource()))

        assert_that(response.result, is_(PlaceResultCode.INVALID_SCHEDULER))
    def test_place_missing_scheduler(self):
        handler = SchedulerHandler()
        response = handler.place(PlaceRequest(Resource()))

        assert_that(response.result, is_(PlaceResultCode.INVALID_SCHEDULER))