|
| 1 | +""" |
| 2 | +This migration checks all the files stored in local storage (=GridFS) and compares them to the list |
| 3 | +of messages already on the node. The files that are not linked to any message are scheduled for |
| 4 | +deletion. |
| 5 | +""" |
| 6 | + |
| 7 | +import asyncio |
| 8 | +import datetime as dt |
| 9 | +import logging |
| 10 | +from dataclasses import asdict |
| 11 | +from typing import Optional, FrozenSet, Any, List |
| 12 | + |
| 13 | +from aleph_message.models import MessageType |
| 14 | +from configmanager import Config |
| 15 | + |
| 16 | +import aleph.model |
| 17 | +from aleph.config import get_defaults |
| 18 | +from aleph.model import init_db_globals |
| 19 | +from aleph.model.messages import Message |
| 20 | +from aleph.model.scheduled_deletions import ScheduledDeletion, ScheduledDeletionInfo |
| 21 | + |
| 22 | +logger = logging.getLogger() |
| 23 | + |
| 24 | + |
| 25 | +async def async_upgrade(config_file: Optional[str], **kwargs): |
| 26 | + config = Config(schema=get_defaults()) |
| 27 | + if config_file is not None: |
| 28 | + config.yaml.load(config_file) |
| 29 | + |
| 30 | + init_db_globals(config=config) |
| 31 | + collections = await aleph.model.db.list_collection_names() |
| 32 | + if ScheduledDeletion.COLLECTION in collections: |
| 33 | + logging.info( |
| 34 | + "%s collection is already present. Skipping migration.", |
| 35 | + ScheduledDeletion.COLLECTION, |
| 36 | + ) |
| 37 | + return |
| 38 | + |
| 39 | + # Get a set of all the files currently in GridFS |
| 40 | + gridfs_files = frozenset( |
| 41 | + [ |
| 42 | + file["filename"] |
| 43 | + async for file in aleph.model.db["fs.files"].find( |
| 44 | + projection={"filename": 1}, batch_size=1000 |
| 45 | + ) |
| 46 | + ] |
| 47 | + ) |
| 48 | + |
| 49 | + print(len(gridfs_files)) |
| 50 | + |
| 51 | + # Get all the messages that potentially store data in local storage: |
| 52 | + # * AGGREGATEs with item_type=="storage" |
| 53 | + # * POSTs with item_type=="storage" |
| 54 | + # * STOREs with content.item_type=="storage" |
| 55 | + async def get_hashes( |
| 56 | + msg_type: MessageType, item_type_field: str, item_hash_field: str |
| 57 | + ) -> FrozenSet[str]: |
| 58 | + def rgetitem(dictionary: Any, fields: List[str]) -> Any: |
| 59 | + value = dictionary[fields[0]] |
| 60 | + if len(fields) > 1: |
| 61 | + return rgetitem(value, fields[1:]) |
| 62 | + return value |
| 63 | + |
| 64 | + return frozenset( |
| 65 | + [ |
| 66 | + rgetitem(msg, item_hash_field.split(".")) |
| 67 | + async for msg in Message.collection.find( |
| 68 | + {"type": msg_type, item_type_field: "storage"}, |
| 69 | + {item_hash_field: 1}, |
| 70 | + batch_size=1000, |
| 71 | + ) |
| 72 | + ] |
| 73 | + ) |
| 74 | + |
| 75 | + aggregates = await get_hashes(MessageType.aggregate, "item_type", "item_hash") |
| 76 | + posts = await get_hashes(MessageType.post, "item_type", "item_hash") |
| 77 | + stores = await get_hashes( |
| 78 | + MessageType.store, "content.item_type", "content.item_hash" |
| 79 | + ) |
| 80 | + |
| 81 | + files_to_preserve = aggregates | posts | stores |
| 82 | + files_to_delete = gridfs_files - files_to_preserve |
| 83 | + delete_by = dt.datetime.utcnow() |
| 84 | + |
| 85 | + await ScheduledDeletion.collection.insert_many( |
| 86 | + [ |
| 87 | + asdict(ScheduledDeletionInfo(filename=file_to_delete, delete_by=delete_by)) |
| 88 | + for file_to_delete in files_to_delete |
| 89 | + ] |
| 90 | + ) |
| 91 | + |
| 92 | + |
| 93 | +def upgrade(config_file: str, **kwargs): |
| 94 | + asyncio.run(async_upgrade(config_file=config_file, **kwargs)) |
| 95 | + |
| 96 | + |
| 97 | +def downgrade(**kwargs): |
| 98 | + # Nothing to do, processing the chain data multiple times only adds some load on the node. |
| 99 | + pass |
0 commit comments