Ejemplo n.º 1
0
    def test_agenda_with_multiple_jobs_in_one_host(self, mocker):
        s = SimulatorHandler()
        s.start("p", "w")

        e1 = BatsimEventAPI.get_job_submitted(
            job_id="w!0", res=1, walltime=100)
        e1 = JobSubmittedBatsimEvent(0, e1['data'])
        e2 = BatsimEventAPI.get_job_submitted(
            job_id="w!1", res=1, walltime=200)
        e2 = JobSubmittedBatsimEvent(0, e2['data'])
        msg = BatsimMessage(0, [e1, e2])
        mocker.patch.object(protocol.NetworkHandler, 'recv', return_value=msg)
        s.proceed_time()

        s.allocate(e1.job.id, [0])
        s.allocate(e2.job.id, [0])

        msg = BatsimMessage(10, [RequestedCallBatsimEvent(10)])
        mocker.patch.object(protocol.NetworkHandler, 'recv', return_value=msg)
        s.proceed_time()

        agenda = list(s.agenda)
        assert s.current_time == 10
        assert agenda[0].host.id == 0 and agenda[0].release_time == e2.job.walltime-10
        assert agenda[1].host.id == 1 and agenda[1].release_time == 0
Ejemplo n.º 2
0
    def test_on_batsim_job_completed_must_dispatch_event(self, mocker):
        def foo_j(j: Job):
            self.__j_called, self.__j_id = True, j.id

        def foo_h(h: Host):
            self.__h_called, self.__h_id = True, h.id

        self.__j_called = self.__h_called = False
        self.__j_id = self.__h_id = -1

        s = SimulatorHandler()
        s.start("p", "w")
        s.subscribe(HostEvent.STATE_CHANGED, foo_h)
        s.subscribe(JobEvent.COMPLETED, foo_j)

        # Setup Allocate
        e = BatsimEventAPI.get_job_submitted(res=1)
        job_id, job_alloc = e['data']['job_id'], [0]
        msg = BatsimMessage(150, [JobSubmittedBatsimEvent(150, e['data'])])
        mocker.patch.object(protocol.NetworkHandler, 'recv', return_value=msg)
        s.proceed_time()
        s.allocate(job_id, job_alloc)

        # Setup Completed
        mocker.patch.object(batsim_py.jobs.Job, '_terminate')
        e = BatsimEventAPI.get_job_completted(100, job_id, alloc=job_alloc)
        msg = BatsimMessage(150, [JobCompletedBatsimEvent(150, e['data'])])
        mocker.patch.object(protocol.NetworkHandler, 'recv', return_value=msg)
        s.proceed_time()

        assert self.__j_called and self.__j_id == job_id
        assert self.__h_called and self.__h_id == job_alloc[0]
Ejemplo n.º 3
0
    def test_kill_job_must_sync_with_batsim(self, mocker):
        mocker.patch("batsim_py.simulator.KillJobBatsimRequest")
        s = SimulatorHandler()
        s.start("p", "w")

        e = BatsimEventAPI.get_job_submitted(res=1)
        msg = BatsimMessage(150, [JobSubmittedBatsimEvent(150, e['data'])])
        mocker.patch.object(protocol.NetworkHandler, 'recv', return_value=msg)
        mocker.patch.object(batsim_py.jobs.Job, '_terminate')
        mocker.patch.object(batsim_py.resources.Host, '_release')
        s.proceed_time()

        mocker.patch("batsim_py.simulator.BatsimMessage")
        mocker.patch.object(batsim_py.jobs.Job,
                            'is_running', return_value=True)
        mocker.patch.object(
            protocol.NetworkHandler,
            'recv',
            return_value=BatsimMessage(s.current_time, []))

        job_id = s.jobs[0].id
        s.kill_job(job_id)

        assert s.jobs
        batsim_py.jobs.Job._terminate.assert_not_called()
        batsim_py.resources.Host._release.assert_not_called()
        simulator.KillJobBatsimRequest.assert_called_once_with(  # type: ignore
            150, job_id)
        assert simulator.NetworkHandler.send.call_count == 2
Ejemplo n.º 4
0
    def test_allocate_start_must_dispatch_events(self, mocker):
        def foo_j(j: Job):
            self.__j_called, self.__j_id = True, j.id

        def foo_h(h: Host):
            self.__h_called, self.__h_id = True, h.id

        self.__j_called = self.__h_called = False
        self.__j_id = self.__h_id = -1
        s = SimulatorHandler()
        s.start("p", "w")
        s.subscribe(JobEvent.STARTED, foo_j)
        s.subscribe(HostEvent.STATE_CHANGED, foo_h)

        e = BatsimEventAPI.get_job_submitted(res=1)
        msg = BatsimMessage(150, [JobSubmittedBatsimEvent(150, e['data'])])
        mocker.patch.object(protocol.NetworkHandler, 'recv', return_value=msg)
        s.proceed_time()

        assert s.queue

        job = s.jobs[0]
        s.allocate(job.id, [0])
        assert self.__j_called and self.__j_id == job.id
        assert self.__h_called and self.__h_id == 0
Ejemplo n.º 5
0
    def test_on_batsim_job_completed_must_terminate_job_and_release_resources(self, mocker):
        s = SimulatorHandler()
        s.start("p", "w")

        # Setup Allocate
        profile = BatsimJobProfileAPI.get_data_staging("a", "b", 10)
        e = BatsimEventAPI.get_job_submitted(res=1, profile=profile)
        job_id, job_alloc = e['data']['job_id'], [0]
        msg = BatsimMessage(150, [JobSubmittedBatsimEvent(150, e['data'])])
        mocker.patch.object(protocol.NetworkHandler, 'recv', return_value=msg)

        storage = list(s.platform.storages)[0]
        s.proceed_time()
        s.allocate(job_id, job_alloc, {"a": storage.id, "b": storage.id})

        # Setup Completed
        mocker.patch.object(batsim_py.jobs.Job, '_terminate')
        mocker.patch.object(batsim_py.resources.Host, '_release')
        mocker.patch.object(batsim_py.resources.Storage, '_release')

        e = BatsimEventAPI.get_job_completted(100, job_id, alloc=job_alloc)
        msg = BatsimMessage(150, [JobCompletedBatsimEvent(150, e['data'])])
        mocker.patch.object(protocol.NetworkHandler, 'recv', return_value=msg)
        s.proceed_time()

        batsim_py.jobs.Job._terminate.assert_called_once()
        batsim_py.resources.Host._release.assert_called_once_with(job_id)
        batsim_py.resources.Storage._release.assert_called_once_with(job_id)
        assert not s.jobs
Ejemplo n.º 6
0
    def test_queue(self, mocker):
        s = SimulatorHandler()
        s.start("p", "w")

        e = [
            JobSubmittedBatsimEvent(
                0, BatsimEventAPI.get_job_submitted(job_id="w!0")['data']),
            JobSubmittedBatsimEvent(
                0, BatsimEventAPI.get_job_submitted(job_id="w!1")['data']),
        ]
        msg = BatsimMessage(150, e)
        mocker.patch.object(protocol.NetworkHandler, 'recv', return_value=msg)
        s.proceed_time()

        assert s.queue and len(s.queue) == 2
        s.allocate("w!1", [0])
        assert s.queue and len(s.queue) == 1
Ejemplo n.º 7
0
    def test_job_parallel_homogeneous_total_submitted_event(self):
        profile = BatsimJobProfileAPI.get_parallel_homogeneous_total(0, 500000)
        event = BatsimEventAPI.get_job_submitted(10, "dyn!my_new_job",
                                                 "delay_10s", 1, 12.0, profile)
        e = JobSubmittedBatsimEvent(10, event["data"])

        assert isinstance(e.job.profile, ParallelHomogeneousTotalJobProfile)
        assert e.job.profile.cpu == profile['cpu']
        assert e.job.profile.com == profile['com']
Ejemplo n.º 8
0
    def test_job_delay_submitted_event(self):
        event = BatsimEventAPI.get_job_submitted(
            10, "dyn!my_new_job", "delay_10s", 1, 12.0,
            BatsimJobProfileAPI.get_delay(10))
        e = JobSubmittedBatsimEvent(10, event["data"])

        assert isinstance(e.job.profile, DelayJobProfile)
        assert e.job.profile.delay == 10
        assert e.job.profile.name == "delay_10s"
Ejemplo n.º 9
0
    def test_job_data_staging_submitted_event(self):
        profile = BatsimJobProfileAPI.get_data_staging("nfs", "pfs", 1000)
        event = BatsimEventAPI.get_job_submitted(10, "dyn!my_new_job",
                                                 "delay_10s", 1, 12.0, profile)
        e = JobSubmittedBatsimEvent(10, event["data"])

        assert isinstance(e.job.profile, DataStagingJobProfile)
        assert e.job.profile.nb_bytes == 1000
        assert e.job.profile.src == "nfs"
        assert e.job.profile.dest == "pfs"
Ejemplo n.º 10
0
    def test_job_composed_submitted_event(self):
        profile = BatsimJobProfileAPI.get_composed(4,
                                                   ["prof1", "prof2", "prof1"])
        event = BatsimEventAPI.get_job_submitted(10, "dyn!my_new_job",
                                                 "delay_10s", 1, 12.0, profile)
        e = JobSubmittedBatsimEvent(10, event["data"])

        assert isinstance(e.job.profile, ComposedJobProfile)
        assert e.job.profile.repeat == 4
        assert e.job.profile.profiles == ["prof1", "prof2", "prof1"]
Ejemplo n.º 11
0
    def test_proceed_time_without_time_must_go_to_next_event(self, mocker):
        s = SimulatorHandler()
        s.start("p", "w")

        e = BatsimEventAPI.get_job_submitted()
        msg = BatsimMessage(150, [JobSubmittedBatsimEvent(150, e['data'])])
        mocker.patch.object(protocol.NetworkHandler, 'recv', return_value=msg)
        mocker.patch.object(SimulatorHandler, 'set_callback')
        s.proceed_time()
        SimulatorHandler.set_callback.assert_not_called()
        assert s.current_time == 150
Ejemplo n.º 12
0
    def test_job_parallel_homogeneous_pfs_submitted_event(self):
        profile = BatsimJobProfileAPI.get_parallel_homogeneous_pfs(
            "nfs", 100, 150)
        event = BatsimEventAPI.get_job_submitted(10, "dyn!my_new_job",
                                                 "delay_10s", 1, 12.0, profile)
        e = JobSubmittedBatsimEvent(10, event["data"])

        assert isinstance(e.job.profile, ParallelHomogeneousPFSJobProfile)
        assert e.job.profile.bytes_to_read == 100
        assert e.job.profile.bytes_to_write == 150
        assert e.job.profile.storage == "nfs"
Ejemplo n.º 13
0
    def test_event_with_metadata(self):
        event = BatsimEventAPI.get_job_submitted(
            10,
            "dyn!my_new_job",
            "delay_10s",
            1,
            12.0,
            BatsimJobProfileAPI.get_delay(10),
            extra_property=True)
        e = JobSubmittedBatsimEvent(10, event["data"])

        assert e.job.metadata.get('extra_property', False) == True
Ejemplo n.º 14
0
    def test_on_batsim_job_submitted_must_append_in_queue(self, mocker):
        s = SimulatorHandler()
        s.start("p", "w")

        # Setup Allocate
        e = BatsimEventAPI.get_job_submitted(res=1)
        job_id, job_alloc = e['data']['job_id'], [0]
        msg = BatsimMessage(150, [JobSubmittedBatsimEvent(150, e['data'])])
        mocker.patch.object(protocol.NetworkHandler, 'recv', return_value=msg)
        s.proceed_time()

        assert s.queue and s.queue[0].id == job_id
Ejemplo n.º 15
0
    def test_allocate_invalid_host_must_raise(self, mocker):
        s = SimulatorHandler()
        s.start("p", "w")

        e = BatsimEventAPI.get_job_submitted(res=1)
        msg = BatsimMessage(150, [JobSubmittedBatsimEvent(150, e['data'])])
        mocker.patch.object(protocol.NetworkHandler, 'recv', return_value=msg)
        s.proceed_time()

        with pytest.raises(LookupError) as excinfo:
            s.allocate(e['data']['job_id'], [3])

        assert "resources" in str(excinfo.value)
Ejemplo n.º 16
0
    def test_kill_job_not_running_must_raise(self, mocker):
        mocker.patch("batsim_py.simulator.KillJobBatsimRequest")
        s = SimulatorHandler()
        s.start("p", "w")

        e = BatsimEventAPI.get_job_submitted(res=1)
        msg = BatsimMessage(150, [JobSubmittedBatsimEvent(150, e['data'])])
        mocker.patch.object(protocol.NetworkHandler, 'recv', return_value=msg)
        s.proceed_time()

        with pytest.raises(RuntimeError) as excinfo:
            s.kill_job(s.jobs[0].id)

        assert "not running" in str(excinfo.value)
Ejemplo n.º 17
0
    def test_proceed_time_with_simulation_time_must_force_close(self, mocker):
        s = SimulatorHandler()
        s.start("p2", "w2", simulation_time=10)

        # setup
        e = BatsimEventAPI.get_job_submitted(res=1)
        events = [
            JobSubmittedBatsimEvent(5, e['data']),
            RequestedCallBatsimEvent(10)
        ]
        msg = BatsimMessage(10, events)
        mocker.patch.object(protocol.NetworkHandler, 'recv', return_value=msg)

        s.proceed_time()
        assert not s.is_running
Ejemplo n.º 18
0
    def test_event(self):
        event = BatsimEventAPI.get_job_submitted(
            10, "dyn!my_new_job", "delay_10s", 1, 12.0,
            BatsimJobProfileAPI.get_delay(10))
        e = JobSubmittedBatsimEvent(10, event["data"])

        assert e.type == BatsimEventType.JOB_SUBMITTED
        assert e.timestamp == 10
        assert e.job.id == "dyn!my_new_job"
        assert e.job.name == "my_new_job"
        assert e.job.workload == "dyn"
        assert e.job.res == 1
        assert isinstance(e.job.profile, JobProfile)
        assert e.job.subtime == 10.0
        assert e.job.walltime == 12.0
        assert e.job.user_id is None
Ejemplo n.º 19
0
    def test_on_batsim_job_submitted_must_dispatch_event(self, mocker):
        def foo(j: Job):
            self.__called, self.__j_id = True, j.id

        self.__called, self.__j_id = False, -1
        s = SimulatorHandler()
        s.start("p", "w")
        s.subscribe(JobEvent.SUBMITTED, foo)

        # Setup Allocate
        e = BatsimEventAPI.get_job_submitted(res=1)
        job_id = e['data']['job_id']
        msg = BatsimMessage(150, [JobSubmittedBatsimEvent(150, e['data'])])
        mocker.patch.object(protocol.NetworkHandler, 'recv', return_value=msg)
        s.proceed_time()

        assert self.__called and self.__j_id == job_id
Ejemplo n.º 20
0
    def test_reject_job(self, mocker):
        mocker.patch("batsim_py.simulator.RejectJobBatsimRequest")
        s = SimulatorHandler()
        s.start("p", "w")

        e = BatsimEventAPI.get_job_submitted(res=1)
        msg = BatsimMessage(150, [JobSubmittedBatsimEvent(150, e['data'])])
        mocker.patch.object(protocol.NetworkHandler, 'recv', return_value=msg)
        mocker.patch.object(batsim_py.jobs.Job, '_reject')
        s.proceed_time()
        job_id = e['data']['job_id']
        s.reject_job(job_id)

        assert not s.jobs
        batsim_py.jobs.Job._reject.assert_called_once()
        simulator.RejectJobBatsimRequest.assert_called_once_with(  # type: ignore
            150, job_id)
Ejemplo n.º 21
0
    def test_allocate_with_switching_off_host_must_not_start_job(self, mocker):
        mocker.patch("batsim_py.protocol.ExecuteJobBatsimRequest")
        s = SimulatorHandler()
        s.start("p", "w")

        # setup
        host = s.platform.get_host(0)
        host._switch_off()
        e = BatsimEventAPI.get_job_submitted(res=1)
        msg = BatsimMessage(150, [JobSubmittedBatsimEvent(150, e['data'])])
        mocker.patch.object(protocol.NetworkHandler, 'recv', return_value=msg)
        s.proceed_time()
        s.allocate(e['data']['job_id'], [0])

        assert s.jobs[0].is_runnable
        assert host.is_switching_off
        protocol.ExecuteJobBatsimRequest.assert_not_called()  # type: ignore
Ejemplo n.º 22
0
    def test_allocate_must_dispatch_job_event(self, mocker):
        def foo(j: Job):
            self.__called = True
            self.__job_id = j.id
        self.__called, self.__job_id = False, -1

        s = SimulatorHandler()
        s.start("p", "w")

        e = BatsimEventAPI.get_job_submitted(res=1)
        msg = BatsimMessage(150, [JobSubmittedBatsimEvent(150, e['data'])])
        mocker.patch.object(protocol.NetworkHandler, 'recv', return_value=msg)
        s.proceed_time()
        s.subscribe(JobEvent.ALLOCATED, foo)
        job = s.jobs[0]
        s.allocate(job.id, [0])

        assert self.__called and self.__job_id == job.id
Ejemplo n.º 23
0
    def test_allocate_must_start_job_and_host(self, mocker):
        mocker.patch("batsim_py.simulator.ExecuteJobBatsimRequest")
        s = SimulatorHandler()
        s.start("p", "w")

        e = BatsimEventAPI.get_job_submitted(res=1)
        msg = BatsimMessage(150, [JobSubmittedBatsimEvent(150, e['data'])])
        mocker.patch.object(protocol.NetworkHandler, 'recv', return_value=msg)
        s.proceed_time()

        assert s.queue

        job = s.jobs[0]
        s.allocate(job.id, [0])

        assert job.is_running
        assert s.platform.get_host(0).is_computing
        simulator.ExecuteJobBatsimRequest.assert_called_once_with(  # type: ignore
            150, job.id, job.allocation, job.storage_mapping)
Ejemplo n.º 24
0
    def test_proceed_time_with_is_submitter_finished_and_queue_must_allow_callback(self, mocker):
        s = SimulatorHandler()
        s.start("p", "w")

        e = BatsimEventAPI.get_notify_no_more_static_job_to_submit(10)
        e2 = BatsimEventAPI.get_job_submitted()
        events = [
            JobSubmittedBatsimEvent(10, e2['data']),
            NotifyBatsimEvent(10, e['data']),
        ]
        msg = BatsimMessage(10, events)
        mocker.patch.object(protocol.NetworkHandler, 'recv', return_value=msg)
        s.proceed_time()
        assert s.is_submitter_finished

        msg = BatsimMessage(50, [SimulationEndsBatsimEvent(50)])
        mocker.patch.object(protocol.NetworkHandler, 'recv', return_value=msg)
        mocker.patch.object(SimulatorHandler, 'set_callback')
        s.proceed_time(50)
        SimulatorHandler.set_callback.assert_called_once()
Ejemplo n.º 25
0
    def test_agenda_with_job_not_running(self, mocker):
        s = SimulatorHandler()
        s.start("p", "w")

        s.switch_off([h.id for h in s.platform.hosts])

        e = BatsimEventAPI.get_job_submitted(res=1, walltime=100)
        e = JobSubmittedBatsimEvent(0, e['data'])
        msg = BatsimMessage(0, [e])
        mocker.patch.object(protocol.NetworkHandler, 'recv', return_value=msg)
        s.proceed_time()

        s.allocate(e.job.id, [0])

        msg = BatsimMessage(10, [RequestedCallBatsimEvent(10)])
        mocker.patch.object(protocol.NetworkHandler, 'recv', return_value=msg)
        s.proceed_time()

        agenda = list(s.agenda)
        assert s.current_time == 10
        assert agenda[0].host.id == 0 and agenda[0].release_time == e.job.walltime
Ejemplo n.º 26
0
    def test_agenda_with_job_without_walltime(self, mocker):
        s = SimulatorHandler()
        s.start("p", "w")

        e = BatsimEventAPI.get_job_submitted(res=1)
        e = JobSubmittedBatsimEvent(0, e['data'])
        msg = BatsimMessage(0, [e])
        mocker.patch.object(protocol.NetworkHandler, 'recv', return_value=msg)
        s.proceed_time()

        s.allocate(e.job.id, [0])

        msg = BatsimMessage(10, [RequestedCallBatsimEvent(10)])
        mocker.patch.object(protocol.NetworkHandler, 'recv', return_value=msg)
        s.proceed_time()

        agenda = list(s.agenda)

        assert s.current_time == 10
        assert agenda[0].host.id == 0 and agenda[0].release_time == np.inf
        assert agenda[1].host.id == 1 and agenda[1].release_time == 0
Ejemplo n.º 27
0
    def test_allocate_with_pfs_job_must_allocate_storages(self, mocker):
        mocker.patch("batsim_py.simulator.ExecuteJobBatsimRequest")
        s = SimulatorHandler()
        s.start("p", "w")

        profile = BatsimJobProfileAPI.get_parallel_homogeneous_pfs("a", 1, 2)
        e = BatsimEventAPI.get_job_submitted(res=1, profile=profile)
        msg = BatsimMessage(150, [JobSubmittedBatsimEvent(150, e['data'])])
        mocker.patch.object(protocol.NetworkHandler, 'recv', return_value=msg)
        s.proceed_time()

        assert s.queue

        job = s.jobs[0]
        storage = list(s.platform.storages)[0]
        s.allocate(job.id, [0], {"a": storage.id})

        assert job.is_running
        assert s.platform.get_host(0).is_computing
        assert storage.jobs and storage.jobs[0] == job.id
        simulator.ExecuteJobBatsimRequest.assert_called_once_with(  # type: ignore
            150, job.id, job.allocation, job.storage_mapping)