8000 Celery uses redis sentinel as backend exception · Issue #5142 · celery/celery · GitHub
[go: up one dir, main page]

Skip to content
Celery uses redis sentinel as backend exception #5142
@yoncan

Description

@yoncan

When redis sentinel is used as backend, the following exception occurs

Steps to reproduce

celery version:

[assistant@d3-dev1 app]$ celery report

software -> celery:4.2.1 (windowlicker) kombu:4.2.1 py:2.7.13
            billiard:3.5.0.4 py-amqp:2.3.2
platform -> system:Linux arch:64bit, ELF imp:CPython
loader   -> celery.loaders.default.Loader
settings -> transport:amqp results:disabled
The configuration information is as follows:

The parameter configuration is as follows:

BROKER_URL='sentinel://:password@10.22.0.150:26379;sentinel://:password@10.22.0.150:26380;sentinel://:password@10.22.0.150:26381'
BROKER_TRANSPORT_OPTIONS={ 'master_name': "mymaster" }

CELERY_RESULT_BACKEND='sentinel://:password@10.22.0.150:26379;sentinel://:password@10.22.0.150:26380;sentinel://:password@10.22.0.150:26381'
CELERY_RESULT_BACKEND_TRANSPORT_OPTIONS={ 'master_name': "mymaster" }

But the following error occurred:

[assistant@d3-dev1 app]$ celery  worker -A tt_worker -l DEBUG

[2018-10-24 18:12:32,709: DEBUG/MainProcess] | Worker: Preparing bootsteps.
[2018-10-24 18:12:32,715: DEBUG/MainProcess] | Worker: Building graph...
[2018-10-24 18:12:32,715: DEBUG/MainProcess] | Worker: New boot order: {Timer, Hub, Pool, Autoscaler, StateDB, Beat, Consumer}
[2018-10-24 18:12:32,742: DEBUG/MainProcess] | Consumer: Preparing bootsteps.
[2018-10-24 18:12:32,743: DEBUG/MainProcess] | Consumer: Building graph...
[2018-10-24 18:12:32,775: DEBUG/MainProcess] | Consumer: New boot order: {Connection, Events, Mingle, Tasks, Control, Heart, Agent, Gossip, event loop}
[2018-10-24 18:12:32,797: CRITICAL/MainProcess] Unrecoverable error: ValueError("invalid literal for int() with base 10: '26379;sentinel'",)
Traceback (most recent call last):
  File "/opt/edsp-assistant/python/lib/python2.7/site-packages/celery/worker/worker.py", line 205, in start
    self.blueprint.start(self)
  File "/opt/edsp-assistant/python/lib/python2.7/site-packages/celery/bootsteps.py", line 115, in start
    self.on_start()
  File "/opt/edsp-assistant/python/lib/python2.7/site-packages/celery/apps/worker.py", line 139, in on_start
    self.emit_banner()
  File "/opt/edsp-assistant/python/lib/python2.7/site-packages/celery/apps/worker.py", line 154, in emit_banner
    ' \n', self.startup_info(artlines=not use_image))),
  File "/opt/edsp-assistant/python/lib/python2.7/site-packages/celery/apps/worker.py", line 223, in startup_info
    results=self.app.backend.as_uri(),
  File "/opt/edsp-assistant/python/lib/python2.7/site-packages/celery/backends/base.py", line 135, in as_uri
    url = maybe_sanitize_url(self.url or '')
  File "/opt/edsp-assistant/python/lib/python2.7/site-packages/kombu/utils/url.py", line 92, in maybe_sanitize_url
    return sanitize_url(url, mask)
  File "/opt/edsp-assistant/python/lib/python2.7/site-packages/kombu/utils/url.py", line 85, in sanitize_url
    return as_url(*_parse_url(url), sanitize=True, mask=mask)
  File "/opt/edsp-assistant/python/lib/python2.7/site-packages/kombu/utils/url.py", line 52, in url_to_parts
    parts.port,
  File "/opt/edsp-assistant/python/lib/python2.7/urlparse.py", line 113, in port
    port = int(port, 10)
ValueError: invalid literal for int() with base 10: '26379;sentinel'

The problem occurred because self.app.backend.as_uri() was called when printing BANNER information.

celery/apps/worker.py

212         banner = BANNER.format(
213             app=appr,
214             hostname=safe_str(self.hostname),
215             timestamp=datetime.now().replace(microsecond=0),
216             version=VERSION_BANNER,
217             conninfo=self.app.connection().as_uri(),
218             results=self.app.backend.as_uri(),   <<<<------ here
219             concurrency=concurrency,
220             platform=safe_str(_platform.platform()),
221             events=events,
222             queues=app.amqp.queues.format(indent=0, indent_first=False),
223         ).splitlines()
		

Fix bug

Use the following method to fix the bug:

Add as_url() method in file Celery/backends/redis.py

 10 from kombu.utils.url import _parse_url, as_url, maybe_sanitize_url

     ......
	 
	 
429 class SentinelBackend(RedisBackend):
430     """Redis sentinel task result store."""
431 	
       ......
	   
488     def as_uri(self, include_password=False):
489         """Return the backend as an URI.
490 
491         Arguments:
492             include_password (bool): Password censored if disabled.
493         """
494         if not self.url:
495             return 'sentinel://'
496         if include_password:
497             return self.url
498 
499         urls = []
500         for h in self.connparams.get('hosts', []):
501             urls.append(maybe_sanitize_url(as_url('sentinel',
502                                                   host=h['host'],
503                                                   port=h['port'],
504                                                   password=h['password'])
505                                            )
506                         )
507         return ';'.join(urls)

Rerun the test

[assistant@d3-dev1 app]$ celery  worker -A tt_worker -l DEBUG
[2018-10-24 18:22:24,482: DEBUG/MainProcess] | Worker: Preparing bootsteps.
[2018-10-24 18:22:24,487: DEBUG/MainProcess] | Worker: Building graph...
[2018-10-24 18:22:24,487: DEBUG/MainProcess] | Worker: New boot order: {Timer, Hub, Pool, Autoscaler, StateDB, Beat, Consumer}
[2018-10-24 18:22:24,514: DEBUG/MainProcess] | Consumer: Preparing bootsteps.
[2018-10-24 18:22:24,514: DEBUG/MainProcess] | Consumer: Building graph...
[2018-10-24 18:22:24,546: DEBUG/MainProcess] | Consumer: New boot order: {Connection, Events, Mingle, Tasks, Control, Heart, Agent, Gossip, event loop}
 
 -------------- celery@d3-dev1.yfb.sunline.cn v4.2.1 (windowlicker)
---- **** ----- 
--- * ***  * -- Linux-3.10.0-693.2.2.el7.x86_64-x86_64-with-centos-7.4.1708-Core 2018-10-24 18:22:24
-- * - **** --- 
- ** ---------- [config]
- ** ---------- .> app:         test:0x7fd54c7bf410
- ** ---------- .> transport:   sentinel://:**@10.22.0.150:26379//
- ** ---------- .> results:     sentinel://:**@10.22.0.150:26379/;sentinel://:**@10.22.0.150:26380/;sentinel://:**@10.22.0.150:26381/
- *** --- * --- .> concurrency: 2 (prefork)
-- ******* ---- .> task events: OFF (enable -E to monitor tasks in this worker)
--- ***** ----- 
 -------------- [queues]
                .> celery           exchange=celery(direct) key=celery
                

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions

      0