Пример #1
0
 def _query(self, sql, *params):
     """We are always using the same cursor, therefore this method is not thread-safe!!!
     You can call it from different threads only if you are holding explicit `AsyncExecutor` lock,
     because the main thread is always holding this lock when running HA cycle."""
     cursor = None
     try:
         cursor = self._connection.cursor()
         cursor.execute(sql, params)
         return cursor
     except psycopg2.Error as e:
         if cursor and cursor.connection.closed == 0:
             # When connected via unix socket, psycopg2 can't recoginze 'connection lost'
             # and leaves `_cursor_holder.connection.closed == 0`, but psycopg2.OperationalError
             # is still raised (what is correct). It doesn't make sense to continiue with existing
             # connection and we will close it, to avoid its reuse by the `cursor` method.
             if isinstance(e, psycopg2.OperationalError):
                 self._connection.close()
             else:
                 raise e
         if self.state == 'restarting':
             raise RetryFailedError('cluster is being restarted')
     except PostgresConnectionException as e:
         print('Connection problem')
         print('Hint: check if there is already a postgresql server running')
         # sys.exit('Connection problems')
         raise PostgresConnectionException('connection problems')
Пример #2
0
 def query(self, sql, *args, **kwargs):
     if not kwargs.get('retry', True):
         return self._query(sql, *args)
     try:
         return self.retry(self._query, sql, *args)
     except RetryFailedError as e:
         raise PostgresConnectionException(str(e))
Пример #3
0
 def query(self, sql, *params):
     cursor = None
     try:
         with self.patroni.postgresql.connection().cursor() as cursor:
             cursor.execute(sql, params)
             return [r for r in cursor]
     except psycopg2.Error as e:
         if cursor and cursor.connection.closed == 0:
             raise e
         raise PostgresConnectionException('connection problems')
Пример #4
0
 def _query(self, sql, *params):
     cursor = None
     try:
         cursor = self._cursor()
         cursor.execute(sql, params)
         return cursor
     except psycopg2.Error as e:
         if cursor and cursor.connection.closed == 0:
             raise e
         if self.state == 'restarting':
             raise RetryFailedError('cluster is being restarted')
         raise PostgresConnectionException('connection problems')
Пример #5
0
    def _cluster_info_state_get(self, name):
        if not self._cluster_info_state:
            try:
                result = self._is_leader_retry(self._query, self.cluster_info_query).fetchone()
                self._cluster_info_state = dict(zip(['timeline', 'wal_position'], result))
            except RetryFailedError as e:  # SELECT failed two times
                self._cluster_info_state = {'error': str(e)}
                if not self.is_starting() and self.pg_isready() == STATE_REJECT:
                    self.set_state('starting')

        if 'error' in self._cluster_info_state:
            raise PostgresConnectionException(self._cluster_info_state['error'])

        return self._cluster_info_state.get(name)
Пример #6
0
 def test_update_lock(self):
     self.p.last_operation = Mock(
         side_effect=PostgresConnectionException(''))
     self.assertTrue(self.ha.update_lock(True))
Пример #7
0
 def query(self, sql, *params):
     try:
         return self.retry(self._query, sql, *params)
     except RetryFailedError as e:
         raise PostgresConnectionException(str(e))