| 10 | |
| 11 | |
| 12 | class WalSegment(object): |
| 13 | def __init__(self, seg_path, explicit=False): |
| 14 | self.path = seg_path |
| 15 | self.explicit = explicit |
| 16 | self.name = path.basename(self.path) |
| 17 | |
| 18 | # If possible, extract TLI and SegmentNumber information. |
| 19 | # Cases where this is not possible include a .history file. |
| 20 | self.tli = None |
| 21 | self.segment_number = None |
| 22 | match = re.match(storage.SEGMENT_REGEXP, self.name) |
| 23 | |
| 24 | if match is not None: |
| 25 | gd = match.groupdict() |
| 26 | self.tli = gd['tli'] |
| 27 | self.segment_number = storage.SegmentNumber(log=gd['log'], |
| 28 | seg=gd['seg']) |
| 29 | |
| 30 | def mark_done(self): |
| 31 | """Mark the archive status of this segment as 'done'. |
| 32 | |
| 33 | This is most useful when performing out-of-band parallel |
| 34 | uploads of segments, so that Postgres doesn't try to go and |
| 35 | upload them again. |
| 36 | |
| 37 | This amounts to messing with an internal bookkeeping mechanism |
| 38 | of Postgres, but that mechanism is not changing too fast over |
| 39 | the last five years and seems simple enough. |
| 40 | """ |
| 41 | |
| 42 | # Recheck that this is not an segment explicitly passed from Postgres |
| 43 | if self.explicit: |
| 44 | raise UserCritical( |
| 45 | msg='unexpected attempt to modify wal metadata detected', |
| 46 | detail=('Segments explicitly passed from postgres should not ' |
| 47 | 'engage in archiver metadata manipulation: {0}' |
| 48 | .format(self.path)), |
| 49 | hint='report a bug') |
| 50 | |
| 51 | # Attempt a rename of archiver metadata, wrapping unexpected |
| 52 | # raised exceptions into a UserCritical. |
| 53 | try: |
| 54 | status_dir = path.join(path.dirname(self.path), |
| 55 | 'archive_status') |
| 56 | |
| 57 | ready_metadata = path.join(status_dir, self.name + '.ready') |
| 58 | done_metadata = path.join(status_dir, self.name + '.done') |
| 59 | |
| 60 | os.rename(ready_metadata, done_metadata) |
| 61 | except Exception: |
| 62 | raise UserCritical( |
| 63 | msg='problem moving .ready archive status to .done', |
| 64 | detail='Traceback is: {0}'.format(traceback.format_exc()), |
| 65 | hint='report a bug') |
| 66 | |
| 67 | @staticmethod |
| 68 | def from_ready_archive_status(xlog_dir): |
| 69 | status_dir = path.join(xlog_dir, 'archive_status') |
no outgoing calls
no test coverage detected