# Buggy code
def upsert_monthly_active_user(self, user_id):
    """Updates or inserts the user into the monthly active user table, which
    is used to track the current MAU usage of the server

    Args:
        user_id (str): user to add/update
    """
    # Support user never to be included in MAU stats. Note I can't easily call this
    # from upsert_monthly_active_user_txn because then I need a _txn form of
    # is_support_user which is complicated because I want to cache the result.
    # Therefore I call it here and ignore the case where
    # upsert_monthly_active_user_txn is called directly from
    # _initialise_reserved_users reasoning that it would be very strange to
    #  include a support user in this context.

    is_support = yield self.is_support_user(user_id)
    if is_support:
        return

    yield self.db.runInteraction(
        "upsert_monthly_active_user", self.upsert_monthly_active_user_txn, user_id
    )

    user_in_mau = self.user_last_seen_monthly_active.cache.get(
        (user_id,), None, update_metrics=False
    )
    if user_in_mau is None:
        self.get_monthly_active_count.invalidate(())

    self.user_last_seen_monthly_active.invalidate((user_id,))


# Traceback (exceptions.AttributeError)
Feb 13 17:56:49 ip-10-1-2-232 matrix_synapse[3223]: 2019-02-13 17:56:49,005 - synapse.http.server - 112 - ERROR - PUT-362- Failed handle request via <function _async_render at 0x7ff0363d32a8>: <XForwardedForRequest at 0x7ff02cca4b48 method=u'PUT' uri=u'/_matrix/client/r0/rooms/<<ROOM ID>>/send/m.room.message/163970?user_id=<<USER ID>>&amp;access_token=<redacted>&amp;ts=1477692257000' clientproto=u'HTTP/1.1' site=18095>: Traceback (most recent call last):
Feb 13 17:56:49 ip-10-1-2-232 matrix_synapse[3223]:   File "/opt/synapse/env/local/lib/python2.7/site-packages/twisted/internet/defer.py", line 654, in _runCallbacks
Feb 13 17:56:49 ip-10-1-2-232 matrix_synapse[3223]:     current.result = callback(current.result, *args, **kw)
Feb 13 17:56:49 ip-10-1-2-232 matrix_synapse[3223]:   File "/opt/synapse/env/local/lib/python2.7/site-packages/twisted/internet/defer.py", line 1475, in gotResult
Feb 13 17:56:49 ip-10-1-2-232 matrix_synapse[3223]:     _inlineCallbacks(r, g, status)
Feb 13 17:56:49 ip-10-1-2-232 matrix_synapse[3223]:   File "/opt/synapse/env/local/lib/python2.7/site-packages/twisted/internet/defer.py", line 1416, in _inlineCallbacks
Feb 13 17:56:49 ip-10-1-2-232 matrix_synapse[3223]:     result = result.throwExceptionIntoGenerator(g)
Feb 13 17:56:49 ip-10-1-2-232 matrix_synapse[3223]:   File "/opt/synapse/env/local/lib/python2.7/site-packages/twisted/python/failure.py", line 491, in throwExceptionIntoGenerator
Feb 13 17:56:49 ip-10-1-2-232 matrix_synapse[3223]:     return g.throw(self.type, self.value, self.tb)
Feb 13 17:56:49 ip-10-1-2-232 matrix_synapse[3223]: --- <exception caught here> ---
Feb 13 17:56:49 ip-10-1-2-232 matrix_synapse[3223]:   File "/opt/synapse/env/local/lib/python2.7/site-packages/synapse/http/server.py", line 81, in wrapped_request_handler
Feb 13 17:56:49 ip-10-1-2-232 matrix_synapse[3223]:     yield h(self, request)
Feb 13 17:56:49 ip-10-1-2-232 matrix_synapse[3223]:   File "/opt/synapse/env/local/lib/python2.7/site-packages/twisted/internet/defer.py", line 1416, in _inlineCallbacks
Feb 13 17:56:49 ip-10-1-2-232 matrix_synapse[3223]:     result = result.throwExceptionIntoGenerator(g)
Feb 13 17:56:49 ip-10-1-2-232 matrix_synapse[3223]:   File "/opt/synapse/env/local/lib/python2.7/site-packages/twisted/python/failure.py", line 491, in throwExceptionIntoGenerator
Feb 13 17:56:49 ip-10-1-2-232 matrix_synapse[3223]:     return g.throw(self.type, self.value, self.tb)
Feb 13 17:56:49 ip-10-1-2-232 matrix_synapse[3223]:   File "/opt/synapse/env/local/lib/python2.7/site-packages/synapse/http/server.py", line 316, in _async_render
Feb 13 17:56:49 ip-10-1-2-232 matrix_synapse[3223]:     callback_return = yield callback(request, **kwargs)
Feb 13 17:56:49 ip-10-1-2-232 matrix_synapse[3223]:   File "/opt/synapse/env/local/lib/python2.7/site-packages/twisted/internet/defer.py", line 1416, in _inlineCallbacks
Feb 13 17:56:49 ip-10-1-2-232 matrix_synapse[3223]:     result = result.throwExceptionIntoGenerator(g)
Feb 13 17:56:49 ip-10-1-2-232 matrix_synapse[3223]:   File "/opt/synapse/env/local/lib/python2.7/site-packages/twisted/python/failure.py", line 491, in throwExceptionIntoGenerator
Feb 13 17:56:49 ip-10-1-2-232 matrix_synapse[3223]:     return g.throw(self.type, self.value, self.tb)
Feb 13 17:56:49 ip-10-1-2-232 matrix_synapse[3223]:   File "/opt/synapse/env/local/lib/python2.7/site-packages/synapse/rest/client/v1/room.py", line 217, in on_POST
Feb 13 17:56:49 ip-10-1-2-232 matrix_synapse[3223]:     txn_id=txn_id,
Feb 13 17:56:49 ip-10-1-2-232 matrix_synapse[3223]:   File "/opt/synapse/env/local/lib/python2.7/site-packages/twisted/internet/defer.py", line 1416, in _inlineCallbacks
Feb 13 17:56:49 ip-10-1-2-232 matrix_synapse[3223]:     result = result.throwExceptionIntoGenerator(g)
Feb 13 17:56:49 ip-10-1-2-232 matrix_synapse[3223]:   File "/opt/synapse/env/local/lib/python2.7/site-packages/twisted/python/failure.py", line 491, in throwExceptionIntoGenerator
Feb 13 17:56:49 ip-10-1-2-232 matrix_synapse[3223]:     return g.throw(self.type, self.value, self.tb)
Feb 13 17:56:49 ip-10-1-2-232 matrix_synapse[3223]:   File "/opt/synapse/env/local/lib/python2.7/site-packages/synapse/handlers/message.py", line 488, in create_and_send_nonmember_event
Feb 13 17:56:49 ip-10-1-2-232 matrix_synapse[3223]:     txn_id=txn_id
Feb 13 17:56:49 ip-10-1-2-232 matrix_synapse[3223]:   File "/opt/synapse/env/local/lib/python2.7/site-packages/twisted/internet/defer.py", line 1416, in _inlineCallbacks
Feb 13 17:56:49 ip-10-1-2-232 matrix_synapse[3223]:     result = result.throwExceptionIntoGenerator(g)
Feb 13 17:56:49 ip-10-1-2-232 matrix_synapse[3223]:   File "/opt/synapse/env/local/lib/python2.7/site-packages/twisted/python/failure.py", line 491, in throwExceptionIntoGenerator
Feb 13 17:56:49 ip-10-1-2-232 matrix_synapse[3223]:     return g.throw(self.type, self.value, self.tb)
Feb 13 17:56:49 ip-10-1-2-232 matrix_synapse[3223]:   File "/opt/synapse/env/local/lib/python2.7/site-packages/synapse/handlers/message.py", line 279, in create_event
Feb 13 17:56:49 ip-10-1-2-232 matrix_synapse[3223]:     yield self.auth.check_auth_blocking(requester.user.to_string())
Feb 13 17:56:49 ip-10-1-2-232 matrix_synapse[3223]:   File "/opt/synapse/env/local/lib/python2.7/site-packages/twisted/internet/defer.py", line 1418, in _inlineCallbacks
Feb 13 17:56:49 ip-10-1-2-232 matrix_synapse[3223]:     result = g.send(result)
Feb 13 17:56:49 ip-10-1-2-232 matrix_synapse[3223]:   File "/opt/synapse/env/local/lib/python2.7/site-packages/synapse/api/auth.py", line 812, in check_auth_blocking
Feb 13 17:56:49 ip-10-1-2-232 matrix_synapse[3223]:     timestamp = yield self.store.user_last_seen_monthly_active(user_id)
Feb 13 17:56:49 ip-10-1-2-232 matrix_synapse[3223]: exceptions.AttributeError: 'EventCreatorSlavedStore' object has no attribute 'user_last_seen_monthly_active'

# Fixed code
def upsert_monthly_active_user(self, user_id):
    """Updates or inserts the user into the monthly active user table, which
    is used to track the current MAU usage of the server

    Args:
        user_id (str): user to add/update
    """
    # Support user never to be included in MAU stats. Note I can't easily call this
    # from upsert_monthly_active_user_txn because then I need a _txn form of
    # is_support_user which is complicated because I want to cache the result.
    # Therefore I call it here and ignore the case where
    # upsert_monthly_active_user_txn is called directly from
    # _initialise_reserved_users reasoning that it would be very strange to
    #  include a support user in this context.

    is_support = yield self.is_support_user(user_id)
    if is_support:
        return

    yield self.db.runInteraction(
        "upsert_monthly_active_user", self.upsert_monthly_active_user_txn, user_id
    )