|
| 1 | +import logging |
| 2 | +from utils import setup_logging, log_since, batch_range |
| 3 | +import argparse |
| 4 | +from connections import connect_to_fossildb, assert_grpc_success |
| 5 | +import time |
| 6 | +import fossildbapi_pb2 as proto |
| 7 | +from typing import List, Tuple |
| 8 | +import msgspec |
| 9 | + |
| 10 | +logger = logging.getLogger("migration-logs") |
| 11 | + |
| 12 | + |
| 13 | +def main(): |
| 14 | + logger.info("Hello from repair_editable_mapping_updates") |
| 15 | + setup_logging() |
| 16 | + parser = argparse.ArgumentParser() |
| 17 | + parser.add_argument("--fossil", type=str, help="Fossildb host and port. Example: localhost:7155", required=True) |
| 18 | + parser.add_argument("--id_mapping", type=str, help="json file containing the id mapping determined by find_mapping_tracing_mapping.py", required=True) |
| 19 | + args = parser.parse_args() |
| 20 | + before = time.time() |
| 21 | + stub = connect_to_fossildb(args.fossil, "target") |
| 22 | + |
| 23 | + json_encoder = msgspec.json.Encoder() |
| 24 | + json_decoder = msgspec.json.Decoder() |
| 25 | + with open(args.id_mapping, "rb") as infile: |
| 26 | + id_mapping = json_decoder.decode(infile.read()) |
| 27 | + for annotation_id in id_mapping.keys(): |
| 28 | + repair_updates_of_annotation(stub, annotation_id, id_mapping[annotation_id], json_encoder, json_decoder) |
| 29 | + |
| 30 | + log_since(before, f"Repairing all {len(id_mapping)} annotations") |
| 31 | + |
| 32 | + |
| 33 | +def repair_updates_of_annotation(stub, annotation_id, id_mapping_for_annotation, json_encoder, json_decoder): |
| 34 | + get_batch_size = 100 # in update groups |
| 35 | + put_buffer_size = 100 # in update groups |
| 36 | + |
| 37 | + before = time.time() |
| 38 | + put_buffer = [] |
| 39 | + changed_update_count = 0 |
| 40 | + newest_version = get_newest_version(stub, annotation_id, "annotationUpdates") |
| 41 | + if newest_version > 10000: |
| 42 | + logger.info(f"Newest version of {annotation_id} is {newest_version}. This may take some time...") |
| 43 | + for batch_start, batch_end in list(batch_range(newest_version + 1, get_batch_size)): |
| 44 | + update_groups_batch = get_update_batch(stub, annotation_id, batch_start, batch_end - 1) |
| 45 | + for version, update_group_bytes in update_groups_batch: |
| 46 | + update_group = json_decoder.decode(update_group_bytes) |
| 47 | + group_changed = False |
| 48 | + for update in update_group: |
| 49 | + if "value" in update: |
| 50 | + update_value = update["value"] |
| 51 | + if "actionTracingId" in update_value and update_value["actionTracingId"] in id_mapping_for_annotation: |
| 52 | + update_value["actionTracingId"] = id_mapping_for_annotation[update_value["actionTracingId"]] |
| 53 | + group_changed = True |
| 54 | + changed_update_count += 1 |
| 55 | + if group_changed: |
| 56 | + versioned_key_value_pair = proto.VersionedKeyValuePairProto() |
| 57 | + versioned_key_value_pair.key = annotation_id |
| 58 | + versioned_key_value_pair.version = version |
| 59 | + versioned_key_value_pair.value = json_encoder.encode(update_group) |
| 60 | + put_buffer.append(versioned_key_value_pair) |
| 61 | + if len(put_buffer) >= put_buffer_size: |
| 62 | + put_multiple_keys_versions(stub, "annotationUpdates", put_buffer) |
| 63 | + put_buffer = [] |
| 64 | + if len(put_buffer) > 0: |
| 65 | + put_multiple_keys_versions(stub, "annotationUpdates", put_buffer) |
| 66 | + log_since(before, f"Repaired {changed_update_count} updates of annotation {annotation_id},") |
| 67 | + |
| 68 | + |
| 69 | +def put_multiple_keys_versions(stub, collection: str, to_put) -> None: |
| 70 | + reply = stub.PutMultipleKeysWithMultipleVersions(proto.PutMultipleKeysWithMultipleVersionsRequest(collection=collection, versionedKeyValuePairs = to_put)) |
| 71 | + assert_grpc_success(reply) |
| 72 | + |
| 73 | + |
| 74 | +def get_update_batch(stub, annotation_id: str, batch_start: int, batch_end_inclusive: int) -> List[Tuple[int, bytes]]: |
| 75 | + reply = stub.GetMultipleVersions( |
| 76 | + proto.GetMultipleVersionsRequest(collection="annotationUpdates", key=annotation_id, oldestVersion=batch_start, newestVersion=batch_end_inclusive) |
| 77 | + ) |
| 78 | + assert_grpc_success(reply) |
| 79 | + return list(zip(reply.versions, reply.values)) |
| 80 | + |
| 81 | + |
| 82 | +def get_newest_version(stub, tracing_id: str, collection: str) -> int: |
| 83 | + reply = stub.Get( |
| 84 | + proto.GetRequest(collection=collection, key=tracing_id, mayBeEmpty=True) |
| 85 | + ) |
| 86 | + assert_grpc_success(reply) |
| 87 | + return reply.actualVersion |
| 88 | + |
| 89 | + |
| 90 | +if __name__ == '__main__': |
| 91 | + main() |
0 commit comments