Ejemplo n.º 1
0
 def recover_snapshot(self):
     file = open(path.join(self.basedir,fu.last_snapshot_file(self.basedir)),"rb")
     if not file:
         return None
     logger.debug("Recovering snapshot from: " + file.name)
     unpickler = Unpickler(file)
     return unpickler.load()
Ejemplo n.º 2
0
def init_persistent_system(obj, basedir=None):
    # a system object is needed in order to coopy work
    if not obj:
        raise Exception(CORE_LOG_PREFIX +
                "Must input a valid object if there's no snapshot files")

    # if obj is a class, change obj to an insance
    if isinstance(obj, type):
        obj = obj()

    validate_system(obj)

    # first step is to check basedir argument. if isn't defined
    # coopy will create a directory name based on system class
    if not basedir:
        basedir = fileutils.obj_to_dir_name(obj)

    # convert some string to a valid directory name
    basedir = fileutils.name_to_dir(basedir)

    system_data_path = os.getcwd()

    # check if basedir exists, if not, create it
    try:
        os.listdir(basedir)
    except os.error:
        os.mkdir(basedir)

    # if no snapshot files, create first one with a 'empty' system
    if not fileutils.last_snapshot_file(basedir):
        logger.info(CORE_LOG_PREFIX + "No snapshot files..")
        SnapshotManager(basedir).take_snapshot(obj)

    # measure restore time
    start = datetime.utcnow()
    logger.info(CORE_LOG_PREFIX + "coopy init....")

    # inject clock on system object
    inject(obj, '_clock', RecordClock())
    inject(obj, '_system_data_path', system_data_path)

    # RestoreHelper will recover a system state based on snapshots and
    # transations files extracting actions executed previously and
    # re-executing them again
    obj = restore(obj, basedir)

    end = datetime.utcnow()
    delta = end - start
    logger.info(CORE_LOG_PREFIX + "spent " + str(delta) + "microseconds")

    journal = DiskJournal(basedir, system_data_path)
    journal.setup()

    snapshot_manager = SnapshotManager(basedir)

    return CoopyProxy(obj, [journal], snapshot_manager=snapshot_manager)
Ejemplo n.º 3
0
    def test_last_snapshot_file(self):
        open(TEST_DIR + 'transaction_000000000000001.log', 'w')
        open(TEST_DIR + 'transaction_000000000000002.log', 'w')
        open(TEST_DIR + 'transaction_000000000000003.log', 'w')
        open(TEST_DIR + 'snapshot_000000000000004.dat', 'w')
        open(TEST_DIR + 'transaction_000000000000005.log', 'w')
        result = fu.last_snapshot_file(TEST_DIR)
        self.assertEqual('snapshot_000000000000004.dat',result)

        self.tearDown()
        self.setUp()

        open(TEST_DIR + 'transaction_000000000000001.log', 'w')
        open(TEST_DIR + 'transaction_000000000000002.log', 'w')
        open(TEST_DIR + 'transaction_000000000000003.log', 'w')
        open(TEST_DIR + 'snapshot_000000000000004.dat', 'w')
        open(TEST_DIR + 'transaction_000000000000005.log', 'w')
        open(TEST_DIR + 'snapshot_000000000000006.dat', 'w')
        result = fu.last_snapshot_file(TEST_DIR)
        self.assertEqual('snapshot_000000000000006.dat',result)