Skip to content
This repository has been archived by the owner on Apr 26, 2024. It is now read-only.

Commit

Permalink
Track command processing as a background process (#7879)
Browse files Browse the repository at this point in the history
I'm going to be doing more stuff synchronously, and I don't want to lose the
CPU metrics down the sofa.
  • Loading branch information
richvdh committed Jul 21, 2020
1 parent 1599761 commit 05060e0
Show file tree
Hide file tree
Showing 4 changed files with 40 additions and 3 deletions.
1 change: 1 addition & 0 deletions changelog.d/7879.feature
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Report CPU metrics to prometheus for time spent processing replication commands.
1 change: 1 addition & 0 deletions stubs/txredisapi.pyi
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ class RedisProtocol:
def publish(self, channel: str, message: bytes): ...

class SubscriberProtocol:
def __init__(self, *args, **kwargs): ...
password: Optional[str]
def subscribe(self, channels: Union[str, List[str]]): ...
def connectionMade(self): ...
Expand Down
19 changes: 18 additions & 1 deletion synapse/replication/tcp/protocol.py
Original file line number Diff line number Diff line change
Expand Up @@ -57,8 +57,12 @@
from twisted.protocols.basic import LineOnlyReceiver
from twisted.python.failure import Failure

from synapse.logging.context import PreserveLoggingContext
from synapse.metrics import LaterGauge
from synapse.metrics.background_process_metrics import run_as_background_process
from synapse.metrics.background_process_metrics import (
BackgroundProcessLoggingContext,
run_as_background_process,
)
from synapse.replication.tcp.commands import (
VALID_CLIENT_COMMANDS,
VALID_SERVER_COMMANDS,
Expand Down Expand Up @@ -160,6 +164,12 @@ def __init__(self, clock: Clock, handler: "ReplicationCommandHandler"):
# The LoopingCall for sending pings.
self._send_ping_loop = None

# a logcontext which we use for processing incoming commands. We declare it as a
# background process so that the CPU stats get reported to prometheus.
self._logging_context = BackgroundProcessLoggingContext(
"replication_command_handler-%s" % self.conn_id
)

def connectionMade(self):
logger.info("[%s] Connection established", self.id())

Expand Down Expand Up @@ -210,6 +220,10 @@ def send_ping(self):
def lineReceived(self, line: bytes):
"""Called when we've received a line
"""
with PreserveLoggingContext(self._logging_context):
self._parse_and_dispatch_line(line)

def _parse_and_dispatch_line(self, line: bytes):
if line.strip() == "":
# Ignore blank lines
return
Expand Down Expand Up @@ -397,6 +411,9 @@ def on_connection_closed(self):
if self.transport:
self.transport.unregisterProducer()

# mark the logging context as finished
self._logging_context.__exit__(None, None, None)

def __str__(self):
addr = None
if self.transport:
Expand Down
22 changes: 20 additions & 2 deletions synapse/replication/tcp/redis.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,11 @@

import txredisapi

from synapse.logging.context import make_deferred_yieldable
from synapse.metrics.background_process_metrics import run_as_background_process
from synapse.logging.context import PreserveLoggingContext, make_deferred_yieldable
from synapse.metrics.background_process_metrics import (
BackgroundProcessLoggingContext,
run_as_background_process,
)
from synapse.replication.tcp.commands import (
Command,
ReplicateCommand,
Expand Down Expand Up @@ -66,6 +69,15 @@ class RedisSubscriber(txredisapi.SubscriberProtocol, AbstractConnection):
stream_name = None # type: str
outbound_redis_connection = None # type: txredisapi.RedisProtocol

def __init__(self, *args, **kwargs):
super().__init__(*args, **kwargs)

# a logcontext which we use for processing incoming commands. We declare it as a
# background process so that the CPU stats get reported to prometheus.
self._logging_context = BackgroundProcessLoggingContext(
"replication_command_handler"
)

def connectionMade(self):
logger.info("Connected to redis")
super().connectionMade()
Expand All @@ -92,7 +104,10 @@ async def _send_subscribe(self):
def messageReceived(self, pattern: str, channel: str, message: str):
"""Received a message from redis.
"""
with PreserveLoggingContext(self._logging_context):
self._parse_and_dispatch_message(message)

def _parse_and_dispatch_message(self, message: str):
if message.strip() == "":
# Ignore blank lines
return
Expand Down Expand Up @@ -145,6 +160,9 @@ def connectionLost(self, reason):
super().connectionLost(reason)
self.handler.lost_connection(self)

# mark the logging context as finished
self._logging_context.__exit__(None, None, None)

def send_command(self, cmd: Command):
"""Send a command if connection has been established.
Expand Down

0 comments on commit 05060e0

Please sign in to comment.