示例#1
0
    def test_job_handler_with_version(self, mock_next_time):
        db_func = self.create_function()
        function_id = db_func.id
        new_version = db_api.increase_function_version(function_id, 0)

        self.assertEqual(0, new_version.count)

        now = datetime.utcnow()
        db_job = self.create_job(
            function_id,
            function_version=1,
            status=status.RUNNING,
            next_execution_time=now,
            count=2
        )
        job_id = db_job.id

        e_client = mock.Mock()
        # It doesn't matter what's the returned value, but need to be in
        # datetime type.
        mock_next_time.return_value = now + timedelta(seconds=1)

        periodics.handle_job(e_client)
        context.set_ctx(self.ctx)

        db_job = db_api.get_job(job_id)
        self.assertEqual(1, db_job.count)
        db_func = db_api.get_function(function_id)
        self.assertEqual(0, db_func.count)
        db_version = db_api.get_function_version(function_id, 1)
        self.assertEqual(1, db_version.count)
        db_execs = db_api.get_executions(function_id=function_id,
                                         function_version=1)
        self.assertEqual(1, len(db_execs))

        periodics.handle_job(e_client)
        context.set_ctx(self.ctx)

        db_job = db_api.get_job(job_id)
        self.assertEqual(0, db_job.count)
        self.assertEqual(status.DONE, db_job.status)
        db_func = db_api.get_function(function_id)
        self.assertEqual(0, db_func.count)
        db_version = db_api.get_function_version(function_id, 1)
        self.assertEqual(2, db_version.count)
        db_execs = db_api.get_executions(function_id=function_id,
                                         function_version=1)
        self.assertEqual(2, len(db_execs))
示例#2
0
    def test_job_handler_with_alias(self, mock_next_time):
        e_client = mock.Mock()
        now = datetime.utcnow()
        # It doesn't matter what's the returned value, but need to be in
        # datetime type.
        mock_next_time.return_value = now + timedelta(seconds=1)

        # Create a alias for a function.
        alias_name = self.rand_name(name="alias", prefix=self.prefix)
        db_func = self.create_function()
        function_id = db_func.id
        db_api.create_function_alias(name=alias_name, function_id=function_id)

        self.create_job(
            function_alias=alias_name,
            status=status.RUNNING,
            next_execution_time=now,
        )

        periodics.handle_job(e_client)
        context.set_ctx(self.ctx)

        # Create function version 1 and update the alias.
        db_api.increase_function_version(function_id, 0)
        db_api.update_function_alias(alias_name, function_version=1)

        periodics.handle_job(e_client)
        context.set_ctx(self.ctx)

        db_func = db_api.get_function(function_id)
        self.assertEqual(1, db_func.count)
        db_version = db_api.get_function_version(function_id, 1)
        self.assertEqual(1, db_version.count)
        db_execs = db_api.get_executions(function_id=function_id)
        self.assertEqual(2, len(db_execs))
示例#3
0
    def get_all(self):
        LOG.info("Get all %ss.", self.type)

        executions = [resources.Execution.from_dict(db_model.to_dict())
                      for db_model in db_api.get_executions()]

        return resources.Executions(executions=executions)
示例#4
0
    def function_load_check(self, function_id, version, runtime_id):
        """Check function load and scale the workers if needed.

        :return: None if no need to scale up otherwise return the service url
        """
        with etcd_util.get_worker_lock(function_id, version) as lock:
            if not lock.is_acquired():
                raise exc.EtcdLockException(
                    'Etcd: failed to get worker lock for function %s'
                    '(version %s).' % (function_id, version))

            workers = etcd_util.get_workers(function_id, version)
            running_execs = db_api.get_executions(function_id=function_id,
                                                  function_version=version,
                                                  status=status.RUNNING)
            concurrency = (len(running_execs) or 1) / (len(workers) or 1)
            if (len(workers) == 0
                    or concurrency > CONF.engine.function_concurrency):
                LOG.info(
                    'Scale up function %s(version %s). Current concurrency: '
                    '%s, execution number %s, worker number %s', function_id,
                    version, concurrency, len(running_execs), len(workers))

                # NOTE(kong): The increase step could be configurable
                return self.scaleup_function(None, function_id, version,
                                             runtime_id, 1)
示例#5
0
    def get_all(self, function_id=None, all_projects=False, project_id=None,
                status=None, description=None):
        """Return a list of executions.

        :param function_id: Optional. Filtering executions by function_id.
        :param project_id: Optional. Admin user can query other projects
            resources, the param is ignored for normal user.
        :param all_projects: Optional. Get resources of all projects.
        :param status: Optional. Filter by execution status.
        :param description: Optional. Filter by description.
        """
        ctx = context.get_ctx()
        if project_id and not ctx.is_admin:
            project_id = context.ctx().projectid
        if project_id and ctx.is_admin:
            all_projects = True

        if all_projects:
            acl.enforce('execution:get_all:all_projects', ctx)

        filters = rest_utils.get_filters(
            function_id=function_id,
            project_id=project_id,
            status=status,
            description=description
        )
        LOG.info("Get all %ss. filters=%s", self.type, filters)

        db_execs = db_api.get_executions(insecure=all_projects, **filters)
        executions = [resources.Execution.from_dict(db_model.to_dict())
                      for db_model in db_execs]

        return resources.Executions(executions=executions)
示例#6
0
    def test_job_handler(self, mock_get_next):
        db_func = self.create_function()
        function_id = db_func.id

        self.assertEqual(0, db_func.count)

        now = datetime.utcnow()
        db_job = self.create_job(
            function_id,
            status=status.RUNNING,
            next_execution_time=now,
            count=2
        )
        job_id = db_job.id

        e_client = mock.Mock()
        mock_get_next.return_value = now + timedelta(seconds=1)

        periodics.handle_job(e_client)
        context.set_ctx(self.ctx)

        db_job = db_api.get_job(job_id)
        self.assertEqual(1, db_job.count)
        db_func = db_api.get_function(function_id)
        self.assertEqual(1, db_func.count)
        db_execs = db_api.get_executions(function_id=function_id)
        self.assertEqual(1, len(db_execs))

        periodics.handle_job(e_client)
        context.set_ctx(self.ctx)

        db_job = db_api.get_job(job_id)
        self.assertEqual(0, db_job.count)
        self.assertEqual(status.DONE, db_job.status)
        db_func = db_api.get_function(function_id)
        self.assertEqual(2, db_func.count)
        db_execs = db_api.get_executions(function_id=function_id)
        self.assertEqual(2, len(db_execs))
示例#7
0
    def function_load_check(self, function_id, runtime_id):
        with etcd_util.get_worker_lock() as lock:
            if not lock.is_acquired():
                return False

            workers = etcd_util.get_workers(function_id)
            running_execs = db_api.get_executions(function_id=function_id,
                                                  status=status.RUNNING)
            concurrency = (len(running_execs) or 1) / (len(workers) or 1)
            if (len(workers) == 0
                    or concurrency > CONF.engine.function_concurrency):
                LOG.info(
                    'Scale up function %s. Current concurrency: %s, execution '
                    'number %s, worker number %s', function_id, concurrency,
                    len(running_execs), len(workers))

                # NOTE(kong): The increase step could be configurable
                return self.scaleup_function(None, function_id, runtime_id, 1)