Example #1
0
    def _reset_streaming_status(self, postgres_status, streaming_status):
        """
        Reset the status of receive-wal by removing the .partial file that
        is marking the current position and creating one that is current with
        the PostgreSQL insert location
        """
        current_wal = xlog.location_to_xlogfile_name_offset(
            postgres_status["current_lsn"],
            streaming_status["timeline"],
            postgres_status["xlog_segment_size"],
        )["file_name"]
        restart_wal = current_wal
        if (postgres_status["replication_slot"]
                and postgres_status["replication_slot"].restart_lsn):
            restart_wal = xlog.location_to_xlogfile_name_offset(
                postgres_status["replication_slot"].restart_lsn,
                streaming_status["timeline"],
                postgres_status["xlog_segment_size"],
            )["file_name"]
        restart_path = os.path.join(self.config.streaming_wals_directory,
                                    restart_wal)
        restart_partial_path = restart_path + ".partial"
        wal_files = sorted(glob(
            os.path.join(self.config.streaming_wals_directory, "*")),
                           reverse=True)

        # Pick the newer file
        last = None
        for last in wal_files:
            if xlog.is_wal_file(last) or xlog.is_partial_file(last):
                break

        # Check if the status is already up-to-date
        if not last or last == restart_partial_path or last == restart_path:
            output.info("Nothing to do. Position of receive-wal is aligned.")
            return

        if os.path.basename(last) > current_wal:
            output.error(
                "The receive-wal position is ahead of PostgreSQL "
                "current WAL lsn (%s > %s)",
                os.path.basename(last),
                postgres_status["current_xlog"],
            )
            return

        output.info("Resetting receive-wal directory status")
        if xlog.is_partial_file(last):
            output.info("Removing status file %s" % last)
            os.unlink(last)
        output.info("Creating status file %s" % restart_partial_path)
        open(restart_partial_path, "w").close()
Example #2
0
 def test_location_to_xlogfile_name_offset(self):
     result = xlog.location_to_xlogfile_name_offset(
         'A/12345678', 3, xlog.DEFAULT_XLOG_SEG_SIZE)
     assert result == {
         'file_name': '000000030000000A00000012',
         'file_offset': 0x345678
     }
Example #3
0
 def test_location_to_xlogfile_name_offset(self, size, name, offset, lsn):
     result = xlog.location_to_xlogfile_name_offset(
         lsn, 3, 1 << size)
     assert result == {
         'file_name': name,
         'file_offset': offset,
     }
Example #4
0
 def test_location_to_xlogfile_name_offset(self):
     assert xlog.location_to_xlogfile_name_offset('A/12345678', 3) == {
         'file_name': '000000030000000A00000012',
         'file_offset': 0x345678
     }
Example #5
0
 def test_location_to_xlogfile_name_offset(self):
     assert xlog.location_to_xlogfile_name_offset('A/12345678', 3) == {
         'file_name': '000000030000000A00000012',
         'file_offset': 0x345678
     }