Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Catch Redis server errors #1119

Merged
merged 12 commits into from
Feb 22, 2021
51 changes: 34 additions & 17 deletions gnocchi/incoming/redis.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,10 +17,13 @@
import uuid

import daiquiri
from redis.exceptions import ConnectionError
import six
import tenacity

from gnocchi.common import redis
from gnocchi import incoming
from gnocchi import utils


LOG = daiquiri.getLogger(__name__)
Expand Down Expand Up @@ -93,6 +96,10 @@ def add_measures_batch(self, metrics_and_measures):
notified_sacks.add(sack_name)
pipe.execute()

@tenacity.retry(
wait=utils.wait_exponential,
# Never retry except when explicitly asked by raising TryAgain
retry=tenacity.retry_if_exception_type(ConnectionError))
def _build_report(self, details):
report_vars = {'measures': 0, 'metric_details': {}}

Expand All @@ -106,20 +113,23 @@ def update_report(results, m_list):
metrics = 0
m_list = []
pipe = self._client.pipeline()
for key in self._client.scan_iter(match=match, count=1000):
metrics += 1
pipe.llen(key)
if details:
m_list.append(key.split(redis.SEP)[1].decode("utf8"))
# group 100 commands/call
if metrics % 100 == 0:
try:
for key in self._client.scan_iter(match=match, count=1000):
metrics += 1
pipe.llen(key)
if details:
m_list.append(key.split(redis.SEP)[1].decode("utf8"))
# group 100 commands/call
if metrics % 100 == 0:
results = pipe.execute()
update_report(results, m_list)
m_list = []
pipe = self._client.pipeline()
else:
results = pipe.execute()
update_report(results, m_list)
m_list = []
pipe = self._client.pipeline()
else:
results = pipe.execute()
update_report(results, m_list)
except ConnectionError:
LOG.debug("Redis Server closed connection. Retrying.")
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

you don't need this anymore (the try/except)

return (metrics, report_vars['measures'],
report_vars['metric_details'] if details else None)

Expand Down Expand Up @@ -176,18 +186,25 @@ def process_measures_for_sack(self, sack):
pipe.ltrim(key, item_len + 1, -1)
pipe.execute()

@tenacity.retry(
wait=utils.wait_exponential,
# Never retry except when explicitly asked by raising TryAgain
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

wrong comment

retry=tenacity.retry_if_exception_type(ConnectionError))
jd marked this conversation as resolved.
Show resolved Hide resolved
def iter_on_sacks_to_process(self):
self._client.config_set("notify-keyspace-events", "K$")
p = self._client.pubsub()
db = self._client.connection_pool.connection_kwargs['db']
keyspace = b"__keyspace@" + str(db).encode() + b"__:"
pattern = keyspace + self._get_sack_name("*").encode()
p.psubscribe(pattern)
for message in p.listen():
if message['type'] == 'pmessage' and message['pattern'] == pattern:
# FIXME(jd) This is awful, we need a better way to extract this
# Format is defined by _get_sack_name: incoming128-17
yield self._make_sack(int(message['channel'].split(b"-")[-1]))
try:
for message in p.listen():
if message['type'] == 'pmessage' and message['pattern'] == pattern:
# FIXME(jd) This is awful, we need a better way to extract this
# Format is defined by _get_sack_name: incoming128-17
yield self._make_sack(int(message['channel'].split(b"-")[-1]))
except ConnectionError:
LOG.debug("Redis Server closed connection. Retrying.")
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ditto


def finish_sack_processing(self, sack):
# Delete the sack key which handles no data but is used to get a SET
Expand Down
2 changes: 1 addition & 1 deletion setup.cfg
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ s3 =
boto3
botocore>=1.5
redis =
redis>=2.10.0 # MIT
redis >= 3.2.0 # MIT
hiredis
swift =
python-swiftclient>=3.1.0
Expand Down