~launchpad-pqm/launchpad/devel

« back to all changes in this revision

Viewing changes to database/schema/preflight.py

  • Committer: Stuart Bishop
  • Date: 2011-07-25 12:52:15 UTC
  • mto: (7675.1045.660 db-devel)
  • mto: This revision was merged to the branch mainline in revision 13516.
  • Revision ID: stuart.bishop@canonical.com-20110725125215-3bikrwvdyamkwdql
Fragile user check for connections we don't want to risk interrupting

Show diffs side-by-side

added added

removed removed

Lines of Context:
36
36
# Ignore connections by these users.
37
37
SYSTEM_USERS = frozenset(['postgres', 'slony', 'nagios', 'lagmon'])
38
38
 
 
39
# Fail checks if these users are connected. If a process should not be
 
40
# interrupted by a rollout, the database user it connects as should be
 
41
# added here. The preflight check will fail if any of these users are
 
42
# connected, so these systems will need to be shut down manually before
 
43
# a database update.
 
44
FRAGILE_USERS = frozenset(['archivepublisher'])
 
45
 
39
46
# How lagged the cluster can be before failing the preflight check.
40
47
MAX_LAG = timedelta(seconds=45)
41
48
 
128
135
            self.log.info("Only system users connected to the cluster")
129
136
        return success
130
137
 
 
138
    def check_fragile_connections(self):
 
139
        """Fail if any FRAGILE_USERS are connected to the cluster.
 
140
 
 
141
        If we interrupt these processes, we may have a mess to clean
 
142
        up. If they are connected, the preflight check should fail.
 
143
        """
 
144
        success = True
 
145
        for node in self.lpmain_nodes:
 
146
            cur = node.con.cursor()
 
147
            cur.execute("""
 
148
                SELECT datname, usename, COUNT(*) AS num_connections
 
149
                FROM pg_stat_activity
 
150
                WHERE
 
151
                    datname=current_database()
 
152
                    AND procpid <> pg_backend_pid()
 
153
                    AND usename IN %s
 
154
                GROUP BY datname, usename
 
155
                """ % sqlvalues(FRAGILE_USERS))
 
156
            for datname, usename, num_connections in cur.fetchall():
 
157
                self.log.fatal(
 
158
                    "Fragile system %s running. %s has %d connections.",
 
159
                    usename, datname, num_connections)
 
160
                success = False
 
161
        if success:
 
162
            self.log.info(
 
163
                "No fragile systems connected to the cluster (%s)"
 
164
                % ', '.join(FRAGILE_USERS))
 
165
        return success
 
166
 
131
167
    def check_long_running_transactions(self, max_secs=10):
132
168
        """Return False if any nodes have long running transactions open.
133
169
 
218
254
            return False
219
255
 
220
256
        success = True
 
257
        if not self.check_replication_lag():
 
258
            success = False
 
259
        if not self.check_can_sync():
 
260
            success = False
221
261
        if not self.check_open_connections():
222
262
            success = False
223
263
        if not self.check_long_running_transactions():
224
264
            success = False
225
 
        if not self.check_replication_lag():
226
 
            success = False
227
 
        if not self.check_can_sync():
 
265
        if not self.check_fragile_connections():
228
266
            success = False
229
267
        return success
230
268