def update_obj_parent( storage_obj: m.SnapshotStorage, parent_storage_obj: m.SnapshotStorage) -> m.SnapshotStorage: _logger.info(f'alter [{storage_obj}] parent to <{parent_storage_obj}>') storage_obj.parent_ident = parent_storage_obj.ident s.get_scoped_session().flush() return storage_obj
def update_obj_parent( storage_obj: m.SnapshotStorage, parent_storage_obj: typing.Union[m.SnapshotStorage, None] ) -> m.SnapshotStorage: _logger.info(f'alter [{storage_obj}] parent to <{parent_storage_obj}>') storage_obj.parent_ident = parent_storage_obj.ident if parent_storage_obj else None s.get_scoped_session().flush() return storage_obj
def update_obj_status(storage_obj: m.SnapshotStorage, new_status) -> m.SnapshotStorage: assert storage_obj.status in _status_transition[new_status], ( '快照存储转移状态无效', f'update snapshot [{storage_obj}] status failed, want to <{m.SnapshotStorage.format_status(new_status)}>', 0) storage_obj.status = new_status s.get_scoped_session().flush() return storage_obj
def query_unconsumed_objs(journal_type=None, before_journal_obj: m.Journal = None): q = s.get_scoped_session().query(m.Journal).filter(m.Journal.consumed_timestamp.is_(None)) if journal_type: q = q.filter(m.Journal.operation_type == journal_type) if before_journal_obj: q = q.filter(m.Journal.id < before_journal_obj.id) return q.order_by(m.Journal.id).all()
def query_image_path_using_count(image_path: str) -> int: return (s.get_scoped_session().query(m.SnapshotStorage).filter( m.SnapshotStorage.image_path == image_path).filter( m.SnapshotStorage.status.notin_(( m.SnapshotStorage.STATUS_DELETED, m.SnapshotStorage.STATUS_RECYCLING, ))).count())
def query_valid_objs(tree_ident): """获取有效的快照存储""" return (s.get_scoped_session().query( m.SnapshotStorage ).filter(m.SnapshotStorage.tree_ident == tree_ident).filter( m.SnapshotStorage.status != m.SnapshotStorage.STATUS_DELETED).all())
def create_obj(token: str, operation_str: str, operation_type: str): new_journal_obj = m.Journal( token=token, operation_str=operation_str, operation_type=operation_type, produced_timestamp=xf.current_timestamp_float(), ) session = s.get_scoped_session() session.add(new_journal_obj) session.flush() _logger.info(f'create <{new_journal_obj}>') return new_journal_obj
def create_obj(storage_ident, parent_ident, parent_timestamp, storage_type, disk_bytes, status, image_path, tree_ident): new_storage_obj = m.SnapshotStorage( ident=storage_ident, parent_ident=parent_ident, parent_timestamp=parent_timestamp, type=storage_type, disk_bytes=disk_bytes, status=status, image_path=image_path, tree_ident=tree_ident, ) session = s.get_scoped_session() session.add(new_storage_obj) session.flush() return new_storage_obj
def consume(token: str, trace_msg: str, return_class: typing.Type[J]) -> J: """消费日志 :param token: :param trace_msg: :param return_class: :return: return_class类型的实例 """ with lm.get_journal_locker(trace_msg), s.transaction(): journal_obj = s.get_scoped_session().query( m.Journal).filter(m.Journal.token == token).first() assert journal_obj, ('磁盘快照日志令牌不存在', f'journal token [{token}] not exist', 0) assert not journal_obj.consumed_timestamp, ( '磁盘快照日志已被消费', f'journal has consumed {token}', 0) da_journal.consume(journal_obj) return return_class(journal_obj)
def get_storage_obj_by_ident(self): return s.get_scoped_session().query(m.SnapshotStorage).filter( m.SnapshotStorage.ident == self.ident).first()
def update_obj_values(storage_obj: m.SnapshotStorage, values: dict) -> m.SnapshotStorage: for k, v in values.items(): setattr(storage_obj, k, v) s.get_scoped_session().flush() return storage_obj
def get_obj_by_ident(storage_ident) -> m.SnapshotStorage: """获取指定快照存储""" return s.get_scoped_session().query(m.SnapshotStorage).filter( m.SnapshotStorage.ident == storage_ident).first()
def query_image_path_exist_count(image_path: str) -> int: return (s.get_scoped_session().query( m.SnapshotStorage ).filter(m.SnapshotStorage.image_path == image_path).filter( m.SnapshotStorage.status != m.SnapshotStorage.STATUS_DELETED).count())
def transform_unsafe_to_safe(self): session = s.get_scoped_session() ident_obj = session.query(m.CreatedIdents).filter( m.CreatedIdents.created_ident == self.ident).first() ident_obj.safe_status = m.CreatedIdents.SAFE session.commit()
def add_normal_ident(self): session = s.get_scoped_session() ident_obj = m.CreatedIdents(created_ident=self.ident) session.add(ident_obj) session.commit()
def alter_children(journal_obj, new_value: str): old_value = journal_obj.children_idents journal_obj.children_idents = new_value s.get_scoped_session().flush() _logger.info(f'change <{journal_obj}> children from [{old_value}] to [{new_value}]')
def get_ident_status_by_ident(self): return s.get_scoped_session().query(m.CreatedIdents).filter( m.CreatedIdents.created_ident == self.ident).first()
def transform_undestroy_to_destroy(self): session = s.get_scoped_session() ident_obj = session.query(m.CreatedIdents).filter( m.CreatedIdents.created_ident == self.ident).first() ident_obj.open_status = m.CreatedIdents.DESTROY session.commit()
def transform_open_to_unopen(self): session = s.get_scoped_session() ident_obj = session.query(m.CreatedIdents).filter( m.CreatedIdents.created_ident == self.ident).first() ident_obj.open_status = m.CreatedIdents.NOT_OPEN session.commit()
def consume(journal_obj): assert not journal_obj.consumed_timestamp, ('磁盘快照日志已被消费', f'journal has consumed {journal_obj}', 0) journal_obj.consumed_timestamp = xf.current_timestamp() s.get_scoped_session().flush() _logger.info(f'journal consumed : {journal_obj}') return journal_obj