|
| 1 | +# -*- coding: utf-8 -*- |
| 2 | +""" |
| 3 | +Keep a long-running website's table metadata in sync with the database. |
| 4 | +
|
| 5 | +psycodict reads each table's columns, types and sort information from the |
| 6 | +database once, when the connection is created. A website process therefore |
| 7 | +does not see schema changes made elsewhere: after a column is dropped its |
| 8 | +queries still mention the column and fail, and a newly added column stays |
| 9 | +invisible, until every worker process is restarted. |
| 10 | +
|
| 11 | +psycodict provides the two halves of the remedy: |
| 12 | +
|
| 13 | +- ``db.refresh_tables()`` re-reads the schema and updates the table objects |
| 14 | + in place, so references held by application code stay valid |
| 15 | + (roed314/psycodict#99); |
| 16 | +- schema-changing operations (``create_table``, ``drop_table``, |
| 17 | + ``rename_table``, ``add_column``, ``drop_column`` and the reload swap) |
| 18 | + announce themselves via PostgreSQL's LISTEN/NOTIFY on the channel |
| 19 | + ``psycodict_schema``, with the affected table's name as payload, and |
| 20 | + ``db.listener()`` subscribes to that channel (roed314/psycodict#111). |
| 21 | +
|
| 22 | +This module ties the two together for the website. Each worker process owns |
| 23 | +a :class:`SchemaRefresher`, driven by a non-blocking ``check()`` from a |
| 24 | +``before_request`` hook: when a schema-change notification has arrived, the |
| 25 | +worker refreshes its table metadata before handling the request. |
| 26 | +
|
| 27 | +psycodict deliberately ships the notification API without threads, callbacks |
| 28 | +or automatic reconnection, leaving those policies to the application. The |
| 29 | +policies chosen here: |
| 30 | +
|
| 31 | +- **No background thread.** Web workers spend their life handling requests, |
| 32 | + so polling at request boundaries is both sufficient and free of the races a |
| 33 | + refresh-from-another-thread would invite. An idle worker can lag behind |
| 34 | + until its next request, which is harmless: with no requests there are no |
| 35 | + queries to fail. A non-blocking poll on an idle connection is just a |
| 36 | + socket read, so doing it every request costs nothing measurable. |
| 37 | +- **Reconnect with catch-up.** If the listening connection is lost, any |
| 38 | + notifications sent before a new ``LISTEN`` is issued are gone (PostgreSQL |
| 39 | + delivers only what is sent after). So the refresher backs off briefly, |
| 40 | + builds a fresh listener, and then does a full refresh to cover whatever it |
| 41 | + may have missed -- including the window between process start and the |
| 42 | + first subscription. |
| 43 | +- **Refresh everything, not just the named table.** The payload names the |
| 44 | + affected table, but ``refresh_tables()`` re-reads all metadata anyway; a |
| 45 | + whole-catalog refresh is cheap relative to how rarely schemas change, and |
| 46 | + it handles creates, drops and renames without special cases. The payload |
| 47 | + is still used for logging and for collapsing a burst of notifications into |
| 48 | + a single refresh. |
| 49 | +
|
| 50 | +If psycodict does not provide the notification API (any release before 1.0), |
| 51 | +the refresher logs once and disables itself, so this module is safe to |
| 52 | +deploy against current psycodict. |
| 53 | +""" |
| 54 | +import os |
| 55 | +import threading |
| 56 | +import time |
| 57 | +from logging import getLogger |
| 58 | + |
| 59 | +try: |
| 60 | + from psycodict.notifications import SCHEMA_CHANNEL |
| 61 | +except ImportError: |
| 62 | + # psycodict without LISTEN/NOTIFY support; the refresher will disable |
| 63 | + # itself, but the channel name is part of psycodict's contract either way. |
| 64 | + SCHEMA_CHANNEL = "psycodict_schema" |
| 65 | + |
| 66 | +logger = getLogger("lmfdb.schema_refresh") |
| 67 | + |
| 68 | + |
| 69 | +class SchemaRefresher: |
| 70 | + """ |
| 71 | + Refresh ``db``'s table metadata when a schema change is announced. |
| 72 | +
|
| 73 | + Drive it by calling :meth:`check` regularly -- the LMFDB app does so in a |
| 74 | + ``before_request`` hook. ``check`` never blocks and never raises, so it |
| 75 | + cannot take a request down with it. |
| 76 | +
|
| 77 | + INPUT: |
| 78 | +
|
| 79 | + - ``db`` -- the database whose metadata to refresh; defaults to lmfdb's |
| 80 | + ``db``, imported lazily so this module stays import-light |
| 81 | + - ``retry_interval`` -- seconds to wait before rebuilding the listener |
| 82 | + after a failure (default 30) |
| 83 | + """ |
| 84 | + |
| 85 | + def __init__(self, db=None, retry_interval=30.0): |
| 86 | + self._db = db |
| 87 | + self.retry_interval = retry_interval |
| 88 | + self._listener = None |
| 89 | + self._pid = None |
| 90 | + self._next_attempt = 0.0 |
| 91 | + self._logged_unavailable = False |
| 92 | + # before_request hooks may run concurrently under threaded or gevent |
| 93 | + # servers; one poller at a time is plenty, so extra callers just skip. |
| 94 | + self._lock = threading.Lock() |
| 95 | + |
| 96 | + @property |
| 97 | + def db(self): |
| 98 | + if self._db is None: |
| 99 | + from lmfdb import db |
| 100 | + self._db = db |
| 101 | + return self._db |
| 102 | + |
| 103 | + def available(self): |
| 104 | + """ |
| 105 | + Whether psycodict provides both the notification API (``listener``) |
| 106 | + and the refresh API (``refresh_tables``). |
| 107 | + """ |
| 108 | + return hasattr(self.db, "listener") and hasattr(self.db, "refresh_tables") |
| 109 | + |
| 110 | + def check(self): |
| 111 | + """ |
| 112 | + Poll for schema-change notifications, refreshing metadata if any arrived. |
| 113 | + """ |
| 114 | + if not self._lock.acquire(blocking=False): |
| 115 | + # Another thread is polling; it will see anything we would have. |
| 116 | + return |
| 117 | + try: |
| 118 | + self._check() |
| 119 | + except Exception: |
| 120 | + # A refresher bug must never take down the request that ran it. |
| 121 | + logger.exception("Unexpected error while checking for schema changes") |
| 122 | + finally: |
| 123 | + self._lock.release() |
| 124 | + |
| 125 | + def _check(self): |
| 126 | + if not self.available(): |
| 127 | + if not self._logged_unavailable: |
| 128 | + logger.info( |
| 129 | + "psycodict does not provide schema-change notifications; " |
| 130 | + "table metadata will refresh only on restart" |
| 131 | + ) |
| 132 | + self._logged_unavailable = True |
| 133 | + return |
| 134 | + if self._listener is not None and self._pid != os.getpid(): |
| 135 | + # This process was forked (gunicorn --preload) after the listener |
| 136 | + # was built, so the socket is shared with the parent. Abandon it |
| 137 | + # without closing -- a close would corrupt the parent's copy -- |
| 138 | + # and build our own below. |
| 139 | + self._listener = None |
| 140 | + if self._listener is None: |
| 141 | + if time.monotonic() < self._next_attempt: |
| 142 | + return |
| 143 | + try: |
| 144 | + self._listener = self.db.listener() |
| 145 | + self._pid = os.getpid() |
| 146 | + except Exception as err: |
| 147 | + self._next_attempt = time.monotonic() + self.retry_interval |
| 148 | + logger.warning( |
| 149 | + "Could not subscribe to schema-change notifications (%s); will retry", err |
| 150 | + ) |
| 151 | + return |
| 152 | + # Notifications sent while we were not subscribed are lost, so |
| 153 | + # catch up with a full refresh on every (re)subscription. |
| 154 | + self._refresh("subscribed to schema-change notifications") |
| 155 | + return |
| 156 | + try: |
| 157 | + notifications = self._listener.poll() |
| 158 | + except Exception as err: |
| 159 | + self._drop_listener() |
| 160 | + self._next_attempt = time.monotonic() + self.retry_interval |
| 161 | + logger.warning("Lost the schema-change listener (%s); will resubscribe", err) |
| 162 | + return |
| 163 | + tables = sorted({payload for channel, payload in notifications if channel == SCHEMA_CHANNEL}) |
| 164 | + if tables: |
| 165 | + self._refresh("schema changed for %s" % (", ".join(tables))) |
| 166 | + |
| 167 | + def _refresh(self, reason): |
| 168 | + try: |
| 169 | + self.db.refresh_tables() |
| 170 | + except Exception as err: |
| 171 | + # Staying subscribed with stale metadata would silently swallow |
| 172 | + # the failure; drop the listener so the next check resubscribes |
| 173 | + # and the catch-up refresh retries this one. |
| 174 | + self._drop_listener() |
| 175 | + self._next_attempt = time.monotonic() + self.retry_interval |
| 176 | + logger.warning("Failed to refresh table metadata (%s): %s; will retry", reason, err) |
| 177 | + else: |
| 178 | + logger.info("Refreshed table metadata: %s", reason) |
| 179 | + |
| 180 | + def _drop_listener(self): |
| 181 | + if self._listener is not None: |
| 182 | + try: |
| 183 | + self._listener.close() |
| 184 | + except Exception: |
| 185 | + pass |
| 186 | + self._listener = None |
| 187 | + |
| 188 | + def close(self): |
| 189 | + """ |
| 190 | + Close the listener (if any); a later ``check`` subscribes anew. |
| 191 | + """ |
| 192 | + self._drop_listener() |
| 193 | + |
| 194 | + |
| 195 | +schema_refresher = SchemaRefresher() |
0 commit comments