def save_snapshot(tega_id): ''' Take a snapshot of _idb and saves it to the hard disk. ''' global _log_fd _idb_snapshot = {} for root_oid in _idb: _idb_snapshot[root_oid] = _idb[root_oid].serialize_( internal=True, serialize_ephemeral=False) num = commit_log_number(server_tega_id, _log_dir) filename = _commit_log_filename(server_tega_id, num + 1) log_file = os.path.join(_log_dir, filename) if _log_fd: _log_fd.close() _log_fd = open(log_file, 'a+') # append-only file finish_marker = COMMIT_FINISH_MARKER + '{}'.format(now()) _log_fd.write(COMMIT_START_MARKER + '\n') # commit start for root_oid, instance in _idb_snapshot.items(): log = log_entry(ope=OPE.SS.name, path=root_oid, tega_id=tega_id, instance=instance) log = str(log) _log_fd.write(log + '\n') _log_fd.write(finish_marker + '\n') # commit finish marker _log_fd.flush() os.fsync(_log_fd)
def save_snapshot(tega_id): ''' Take a snapshot of _idb and saves it to the hard disk. ''' global _log_fd _idb_snapshot = {} for root_oid in _idb: _idb_snapshot[root_oid] = _idb[root_oid].serialize_(internal=True, serialize_ephemeral=False) num = commit_log_number(server_tega_id, _log_dir) filename = _commit_log_filename(server_tega_id, num+1) log_file = os.path.join(_log_dir, filename) if _log_fd: _log_fd.close() _log_fd = open(log_file, 'a+') # append-only file finish_marker = COMMIT_FINISH_MARKER+'{}'.format(now()) _log_fd.write(COMMIT_START_MARKER+'\n') # commit start for root_oid, instance in _idb_snapshot.items(): log = log_entry(ope=OPE.SS.name, path=root_oid, tega_id=tega_id, instance=instance) log = str(log) _log_fd.write(log+'\n') _log_fd.write(finish_marker+'\n') # commit finish marker _log_fd.flush() os.fsync(_log_fd)
def start(data_dir, tega_id, maxlen=OLD_ROOTS_LEN): ''' Starts tega db ''' global _log_fd global _log_dir global server_tega_id server_tega_id = tega_id _log_dir = os.path.join(os.path.expanduser(data_dir)) num = commit_log_number(server_tega_id, _log_dir) filename = _commit_log_filename(server_tega_id, num) log_file = os.path.join(_log_dir, filename) try: _log_fd = open(log_file, 'a+') # append-only file except FileNotFoundError: raise old_roots_len = maxlen
def start(data_dir, tega_id, maxlen=OLD_ROOTS_LEN): ''' Starts tega db ''' global _log_fd global _log_dir global server_tega_id server_tega_id = tega_id _log_dir = os.path.join(os.path.expanduser(data_dir)) num = commit_log_number(server_tega_id, _log_dir) filename = _commit_log_filename(server_tega_id, num) log_file = os.path.join(_log_dir, filename) try: _log_fd = open(log_file, 'a+') # append-only file except FileNotFoundError: raise old_roots_len = maxlen
def clear(): ''' Empties tega-db file and in-memory DB ''' global _log_fd, _log_dir, _idb, _old_roots, server_tega_id # Clears idb _idb = {} _old_roots = {} # Removes commit log files _log_fd.close() max_ = commit_log_number(server_tega_id, _log_dir) for i in range(0, max_ + 1): log_file = os.path.join(_log_dir, _commit_log_filename(server_tega_id, i)) if os.path.exists(log_file): os.remove(log_file) # Reopens an empty commit log file log_file = os.path.join(_log_dir, _commit_log_filename(server_tega_id, 0)) _log_fd = open(log_file, 'a+') # append-onlyfile
def clear(): ''' Empties tega-db file and in-memory DB ''' global _log_fd, _log_dir, _idb, _old_roots, server_tega_id # Clears idb _idb = {} _old_roots = {} # Removes commit log files _log_fd.close() max_ = commit_log_number(server_tega_id, _log_dir) for i in range(0, max_ + 1): log_file = os.path.join(_log_dir, _commit_log_filename(server_tega_id, i)) if os.path.exists(log_file): os.remove(log_file) # Reopens an empty commit log file log_file = os.path.join(_log_dir, _commit_log_filename(server_tega_id, 0)) _log_fd = open(log_file, 'a+') # append-onlyfile
def loglist_for_sync(root_oid, version): ''' Accumulates log entries until the version. log.global.0 log.global.l log.global.n +------------+ +------------+ +------------+ ^ | | | SS | | SS | | | | |------------| | | | | | | | ^ | | | | | | | | | | | +------------+ +------------+ | +------------+ | ''' multi = [] notifications = [] if version < 0: version = -1 try: version_ = _idb[root_oid]['_version'] except KeyError: return multi if version_ <= version: return multi max_ = commit_log_number(server_tega_id, _log_dir) continue_ = True begin = False match = False while continue_ and max_ >= 0: filename = _commit_log_filename(server_tega_id, max_) log_file = os.path.join(_log_dir, filename) max_ -= 1 with open(log_file, 'r') as fd: g = readline_reverse(fd) while True: try: line = next(g).rstrip('\n') except StopIteration: break if line == '': pass elif line.startswith(COMMIT_FINISH_MARKER): begin = True elif line.startswith(COMMIT_START_MARKER): begin = False if match: version_ -= 1 match = False multi.insert(0, notifications) notifications = [] if version_ <= version: continue_ = False break elif line.startswith(ROLLBACK_MARKER): args = line.split(' ') root_oid_ = args[2] if root_oid_ == root_oid: backto = args[0] tega_id = args[1] log = log_entry(ope=OPE.ROLLBACK.name, path=root_oid, tega_id=tega_id, instance=None, backto=int(backto)) if notifications: multi.insert(0, notifications) notifications = [log] multi.insert(0, notifications) notifications = [] version_ -= 1 if version_ <= version: continue_ = False break elif begin: log = eval(line) ope = log['ope'] path = log['path'] tega_id = log['tega_id'] instance = log['instance'] if ope == OPE.SS.name: pass elif path.split('.')[0] == root_oid: match = True log = log_entry(ope=ope, path=path, tega_id=tega_id, instance=instance) notifications.insert(0, log) return multi
def loglist_for_sync(root_oid, version): ''' Accumulates log entries until the version. log.global.0 log.global.l log.global.n +------------+ +------------+ +------------+ ^ | | | SS | | SS | | | | |------------| | | | | | | | ^ | | | | | | | | | | | +------------+ +------------+ | +------------+ | ''' multi = [] notifications = [] if version < 0: version = -1 try: version_ = _idb[root_oid]['_version'] except KeyError: return multi if version_ <= version: return multi max_ = commit_log_number(server_tega_id, _log_dir) continue_ = True begin = False match = False while continue_ and max_ >= 0: filename = _commit_log_filename(server_tega_id, max_) log_file = os.path.join(_log_dir, filename) max_ -= 1 with open(log_file, 'r') as fd: g = readline_reverse(fd) while True: try: line = next(g).rstrip('\n') except StopIteration: break if line == '': pass elif line.startswith(COMMIT_FINISH_MARKER): begin = True elif line.startswith(COMMIT_START_MARKER): begin = False if match: version_ -= 1 match = False multi.insert(0, notifications) notifications = [] if version_ <= version: continue_ = False break elif line.startswith(ROLLBACK_MARKER): args = line.split(' ') root_oid_ = args[2] if root_oid_ == root_oid: backto = args[0] tega_id = args[1] log = log_entry(ope=OPE.ROLLBACK.name, path=root_oid, tega_id=tega_id, instance=None, backto=int(backto)) if notifications: multi.insert(0, notifications) notifications = [log] multi.insert(0, notifications) notifications = [] version_ -= 1 if version_ <= version: continue_ = False break elif begin: log = eval(line) ope = log['ope'] path = log['path'] tega_id = log['tega_id'] instance = log['instance'] if ope == OPE.SS.name: pass elif path.split('.')[0] == root_oid: match = True log = log_entry(ope=ope, path=path, tega_id=tega_id, instance=instance) notifications.insert(0, log) return multi