interesting bugs

收集工作、开发中遇到的bug

1

def get_es_client(self, namespace, endpoint_id=None):
        """
        Return elasticsearch client by config.

        :param namespace: namespace of build
        :type namespace: str
        :param endpoint_id: endpoint_id
        :type endpint_id: str
        :returns: elasticsearch client
        :rtype: ElasticSearchClient
        """
        data = []
        try:
            endpoint_result = RazzilUtil.get_endpoint(namespace=namespace,
                                                      endpoint_id=endpoint_id)
            if not endpoint_result.is_public:
                data = FurionClient.get_log_source(endpoint_result.region_id)['data']['read_log_source']
        except Exception as e:
            msg = 'get log source info error {}'.format(e.message)
            LOG.error(msg)

        # here we get three log sources, their priority: data(furion) > endpoint_result(razzil buildendpoint) > settings
        if data:
            if data[0]['type'] == 'default':
                return ElasticSearchClient()
            
            return ElasticSearchClient(data[0].get('query_address', None),
                                       data[0].get('username', None),
                                       data[0].get('password', None))

        if endpoint_result.es_host:
            return ElasticSearchClient(endpoint_result.es_host,
                                       endpoint_result.es_username,
                                       endpoint_result.es_password)

        """error case
        logger.info('there are no log sources,check your config!!!')
        return None
        """
        
        return ElasticSearchClient()

如果endpoint_result = RazzilUtil.get_endpoint报异常,导致data和endpoint_result都不满足if,这时漏掉了使用settings默认配置初始化es的情况。

2

def start(cls):
        while True:
            task = TresdinClient.get_task()
            if not task:
                time.sleep(1)
                continue
            
            logger.info('get task {}, will start it.'.format(task))
            new_task = EventTask.objects.filter(id=task['id']).latest()
            new_task.status = STATUS_IN_PROGRESS
            new_task.started_at = datetime.datetime.now()
            new_task.save()
            worker = EventHandler(task)
            worker.start()
            #注释掉防止任务堆积
            #time.sleep(1)

这个到不是bug,但是有优化空间,考虑如下情形:

在task很多的情况下,可以只在 if not task的时候sleep,有任务积压的时候sleep可能会产生更多堆积

3

Traceback (most recent call last):
  File "/usr/local/lib/python3.5/site-packages/django/db/backends/utils.py", line 65, in execute
    return self.cursor.execute(sql, params)
psycopg2.OperationalError: server closed the connection unexpectedly
	This probably means the server terminated abnormally
	before or while processing the request.

The above exception was the direct cause of the following exception:
Traceback (most recent call last):
  File "/usr/local/lib/python3.5/site-packages/django/core/handlers/base.py", line 185, in _get_response
    response = wrapped_callback(request, *callback_args, **callback_kwargs)
  File "/usr/local/lib/python3.5/site-packages/django/views/decorators/csrf.py", line 58, in wrapped_view
    return view_func(*args, **kwargs)
  File "/usr/local/lib/python3.5/site-packages/rest_framework/viewsets.py", line 83, in view
    return self.dispatch(request, *args, **kwargs)
  File "/usr/local/lib/python3.5/site-packages/rest_framework/views.py", line 477, in dispatch
    response = self.handle_exception(exc)
  File "/usr/local/lib/python3.5/site-packages/rest_framework/views.py", line 437, in handle_exception
    self.raise_uncaught_exception(exc)
  File "/usr/local/lib/python3.5/site-packages/rest_framework/views.py", line 474, in dispatch
    response = handler(request, *args, **kwargs)
  File "/app/instant_jobs/views.py", line 56, in create
    job_client.create_job(serializer.validated_data)
  File "/app/alauda_job/job_client.py", line 16, in create_job
    return self._scheduler.create_job(data)
  File "/app/alauda_job/kubernetes.py", line 57, in create_job
    event_task.save()
  File "/usr/local/lib/python3.5/site-packages/django/db/models/base.py", line 806, in save
    force_update=force_update, update_fields=update_fields)
  File "/usr/local/lib/python3.5/site-packages/django/db/models/base.py", line 836, in save_base
    updated = self._save_table(raw, cls, force_insert, force_update, using, update_fields)
  File "/usr/local/lib/python3.5/site-packages/django/db/models/base.py", line 903, in _save_table
    forced_update)
  File "/usr/local/lib/python3.5/site-packages/django/db/models/base.py", line 953, in _do_update
    return filtered._update(values) > 0
  File "/usr/local/lib/python3.5/site-packages/django/db/models/query.py", line 661, in _update
    return query.get_compiler(self.db).execute_sql(CURSOR)
  File "/usr/local/lib/python3.5/site-packages/django/db/models/sql/compiler.py", line 1191, in execute_sql
    cursor = super(SQLUpdateCompiler, self).execute_sql(result_type)
  File "/usr/local/lib/python3.5/site-packages/django/db/models/sql/compiler.py", line 876, in execute_sql
    cursor.execute(sql, params)
  File "/usr/local/lib/python3.5/site-packages/django/db/backends/utils.py", line 65, in execute
    return self.cursor.execute(sql, params)
  File "/usr/local/lib/python3.5/site-packages/django/db/utils.py", line 94, in __exit__
    six.reraise(dj_exc_type, dj_exc_value, traceback)
  File "/usr/local/lib/python3.5/site-packages/django/utils/six.py", line 685, in reraise
    raise value.with_traceback(tb)
  File "/usr/local/lib/python3.5/site-packages/django/db/backends/utils.py", line 65, in execute
    return self.cursor.execute(sql, params)
django.db.utils.OperationalError: server closed the connection unexpectedly
	This probably means the server terminated abnormally
	before or while processing the request.
2018-03-27 05:04:38,653 [WARNING][MainThread][mekansm.contrib.django.middleware:27] the failed request info:
{
    "user": "None",
    "remote_host": null,
    "path": "/v1/instant-jobs/",
    "method": "POST",
    "remote_addr": ""
}
2018-03-27 05:04:38,653 [ERROR][MainThread][django_config.middleware:16] Exception was not wrapped: server closed the connection unexpectedly
	This probably means the server terminated abnormally
	before or while processing the request.
2018-03-27 05:04:38,654 [ERROR][MainThread][django_config.middleware:17] Traceback (most recent call last):
  File "/usr/local/lib/python3.5/site-packages/django/db/backends/utils.py", line 65, in execute
    return self.cursor.execute(sql, params)
psycopg2.OperationalError: server closed the connection unexpectedly
	This probably means the server terminated abnormally
	before or while processing the request.

The above exception was the direct cause of the following exception:
Traceback (most recent call last):
  File "/usr/local/lib/python3.5/site-packages/django/core/handlers/base.py", line 185, in _get_response
    response = wrapped_callback(request, *callback_args, **callback_kwargs)
  File "/usr/local/lib/python3.5/site-packages/django/views/decorators/csrf.py", line 58, in wrapped_view
    return view_func(*args, **kwargs)
  File "/usr/local/lib/python3.5/site-packages/rest_framework/viewsets.py", line 83, in view
    return self.dispatch(request, *args, **kwargs)
  File "/usr/local/lib/python3.5/site-packages/rest_framework/views.py", line 477, in dispatch
    response = self.handle_exception(exc)
  File "/usr/local/lib/python3.5/site-packages/rest_framework/views.py", line 437, in handle_exception
    self.raise_uncaught_exception(exc)
  File "/usr/local/lib/python3.5/site-packages/rest_framework/views.py", line 474, in dispatch
    response = handler(request, *args, **kwargs)
  File "/app/instant_jobs/views.py", line 56, in create
    job_client.create_job(serializer.validated_data)
  File "/app/alauda_job/job_client.py", line 16, in create_job
    return self._scheduler.create_job(data)
  File "/app/alauda_job/kubernetes.py", line 57, in create_job
    event_task.save()
  File "/usr/local/lib/python3.5/site-packages/django/db/models/base.py", line 806, in save
    force_update=force_update, update_fields=update_fields)
  File "/usr/local/lib/python3.5/site-packages/django/db/models/base.py", line 836, in save_base
    updated = self._save_table(raw, cls, force_insert, force_update, using, update_fields)
  File "/usr/local/lib/python3.5/site-packages/django/db/models/base.py", line 903, in _save_table
    forced_update)
  File "/usr/local/lib/python3.5/site-packages/django/db/models/base.py", line 953, in _do_update
    return filtered._update(values) > 0
  File "/usr/local/lib/python3.5/site-packages/django/db/models/query.py", line 661, in _update
    return query.get_compiler(self.db).execute_sql(CURSOR)
  File "/usr/local/lib/python3.5/site-packages/django/db/models/sql/compiler.py", line 1191, in execute_sql
    cursor = super(SQLUpdateCompiler, self).execute_sql(result_type)
  File "/usr/local/lib/python3.5/site-packages/django/db/models/sql/compiler.py", line 876, in execute_sql
    cursor.execute(sql, params)
  File "/usr/local/lib/python3.5/site-packages/django/db/backends/utils.py", line 65, in execute
    return self.cursor.execute(sql, params)
  File "/usr/local/lib/python3.5/site-packages/django/db/utils.py", line 94, in __exit__
    six.reraise(dj_exc_type, dj_exc_value, traceback)
  File "/usr/local/lib/python3.5/site-packages/django/utils/six.py", line 685, in reraise
    raise value.with_traceback(tb)
  File "/usr/local/lib/python3.5/site-packages/django/db/backends/utils.py", line 65, in execute
    return self.cursor.execute(sql, params)
django.db.utils.OperationalError: server closed the connection unexpectedly
	This probably means the server terminated abnormally
	before or while processing the request.

Django models数据库连接超时问题,待解决分析

4

def trigger(self):
        if self.is_git:
            data = self.generate_git_data()
        else:
            data = self.generate_svn_data()

        if data:
            logger.info("start trigger build for {}, payload is {}".format(self.repo_path, data))            
            self.build(data)
            # return

        logger.info('repo {} is not update, skip trigger'.format(self.repo_path))

if data调用完成后忘记写return了,产生了垃圾log

5

Django migrate does not work?

Operations to perform:
  Apply all migrations: ci_envs, contenttypes, dockerfile_manage, private_build_configs, private_builds, private_code_clients
Running migrations:
  No migrations to apply.

最后通过python manage.py sqlmigrate命令,将获取的SQL在PostgreSQL中执行解决问题。原因是自己之前测试的时候没有把migrate file放到版本库里,而是用python manage.py makemigrations生成后执行的,导致管理混乱,开发不应该随意动数据库。

6

TBD...

comments powered by Disqus