def __init__(self, source, dest, replay_indexes=True, ts=None, poll_time=1.0): # Create a one-time connection to source, to determine replicaset. c = pymongo.Connection(source) try: obj = c.local.system.replset.find_one() replicaset = obj['_id'] except: raise ValueError('Could not determine replicaset for %r' % source) # Mongo source is a replica set, connect to it as such. self.source = pymongo.Connection(source, replicaset=replicaset) # Use ReadPreference.SECONDARY because we can afford to read oplogs # from secondaries: even if they're behind, everything will work # correctly because the oplog order will always be preserved. self.source.read_preference = pymongo.ReadPreference.SECONDARY self._lastts_id = '%s-lastts' % replicaset self.dest = pymongo.Connection(dest) self.replay_indexes = replay_indexes # When no ts argument is supplied, get the last timestamp from dest. if ts is None: ts = self._get_lastts() self._replay_count = 0 # Compute velocity every few ops. self._started_at = self._last_velocity_at = time.time() self._last_replay_count = 0 OplogWatcher.__init__(self, self.source, ts=ts, poll_time=poll_time)
def process_op(self, ns, raw): if not self.replay_indexes and OplogReplayer.is_index_operation(raw): # Do not replay index operations. pass else: # Treat "drop index" operations separately. if OplogReplayer.is_drop_index(raw): self.drop_index(raw) else: OplogWatcher.process_op(self, ns, raw) # Update the lastts on the destination self._update_lastts() self._replay_count += 1 self.print_replication_info()