|
| 1 | +""" |
| 2 | +This abstracts the "processing state" for a commit. |
| 3 | +
|
| 4 | +It takes care that each upload for a specific commit is going through the following |
| 5 | +states: |
| 6 | +
|
| 7 | +- "processing": when an upload was received and is being parsed/processed. |
| 8 | +- "processed": the upload has been processed and an "intermediate report" has been stored, |
| 9 | + the upload is now waiting to be merged into the "master report". |
| 10 | +- "merged": the upload was fully merged into the "master report". |
| 11 | +
|
| 12 | +The logic in this file also makes sure that processing and merging happens in an "optimal" way |
| 13 | +meaning that: |
| 14 | +
|
| 15 | +- "postprocessing", which means triggering notifications and other followup work |
| 16 | + only happens once for a commit. |
| 17 | +- merging should happen in batches, as that involves loading a bunch of "intermediate report"s |
| 18 | + into memory, which should be bounded. |
| 19 | +- (ideally in the future) an upload that has been processed into an "intermediate report" |
| 20 | + should be merged directly into the "master report" without doing a storage roundtrip for that |
| 21 | + "intermediate report". |
| 22 | +""" |
| 23 | + |
| 24 | +from dataclasses import dataclass |
| 25 | + |
| 26 | +from shared.metrics import Counter |
| 27 | + |
| 28 | +from services.redis import get_redis_connection |
| 29 | + |
| 30 | +MERGE_BATCH_SIZE = 5 |
| 31 | + |
| 32 | +CLEARED_UPLOADS = Counter( |
| 33 | + "worker_processing_cleared_uploads", |
| 34 | + "Number of uploads cleared from queue because of errors", |
| 35 | +) |
| 36 | + |
| 37 | + |
| 38 | +@dataclass |
| 39 | +class UploadNumbers: |
| 40 | + processing: int |
| 41 | + """ |
| 42 | + The number of uploads currently being processed. |
| 43 | + """ |
| 44 | + |
| 45 | + processed: int |
| 46 | + """ |
| 47 | + The number of uploads that have been processed, |
| 48 | + and are waiting on being merged into the "master report". |
| 49 | + """ |
| 50 | + |
| 51 | + |
| 52 | +def should_perform_merge(uploads: UploadNumbers) -> bool: |
| 53 | + """ |
| 54 | + Determines whether a merge should be performed. |
| 55 | +
|
| 56 | + This is the case when no more uploads are expected, |
| 57 | + or we reached the desired batch size for merging. |
| 58 | + """ |
| 59 | + return uploads.processing == 0 or uploads.processed >= MERGE_BATCH_SIZE |
| 60 | + |
| 61 | + |
| 62 | +def should_trigger_postprocessing(uploads: UploadNumbers) -> bool: |
| 63 | + """ |
| 64 | + Determines whether post-processing steps, such as notifications, etc, |
| 65 | + should be performed. |
| 66 | +
|
| 67 | + This is the case when no more uploads are expected, |
| 68 | + and all the processed uploads have been merged into the "master report". |
| 69 | + """ |
| 70 | + return uploads.processing == 0 and uploads.processed == 0 |
| 71 | + |
| 72 | + |
| 73 | +class ProcessingState: |
| 74 | + def __init__(self, repoid: int, commitsha: str) -> None: |
| 75 | + self._redis = get_redis_connection() |
| 76 | + self.repoid = repoid |
| 77 | + self.commitsha = commitsha |
| 78 | + |
| 79 | + def get_upload_numbers(self): |
| 80 | + processing = self._redis.scard(self._redis_key("processing")) |
| 81 | + processed = self._redis.scard(self._redis_key("processed")) |
| 82 | + return UploadNumbers(processing, processed) |
| 83 | + |
| 84 | + def mark_uploads_as_processing(self, upload_ids: list[int]): |
| 85 | + self._redis.sadd(self._redis_key("processing"), *upload_ids) |
| 86 | + |
| 87 | + def clear_in_progress_uploads(self, upload_ids: list[int]): |
| 88 | + removed_uploads = self._redis.srem(self._redis_key("processing"), *upload_ids) |
| 89 | + if removed_uploads > 0: |
| 90 | + # the normal flow would move the uploads from the "processing" set |
| 91 | + # to the "processed" set via `mark_upload_as_processed`. |
| 92 | + # this function here is only called in the error case and we don't expect |
| 93 | + # this to be triggered often, if at all. |
| 94 | + CLEARED_UPLOADS.inc(removed_uploads) |
| 95 | + |
| 96 | + def mark_upload_as_processed(self, upload_id: int): |
| 97 | + res = self._redis.smove( |
| 98 | + self._redis_key("processing"), self._redis_key("processed"), upload_id |
| 99 | + ) |
| 100 | + if not res: |
| 101 | + # this can happen when `upload_id` was never in the source set, |
| 102 | + # which probably is the case during initial deployment as |
| 103 | + # the code adding this to the initial set was not deployed yet |
| 104 | + # TODO: make sure to remove this code after a grace period |
| 105 | + self._redis.sadd(self._redis_key("processed"), upload_id) |
| 106 | + |
| 107 | + def mark_uploads_as_merged(self, upload_ids: list[int]): |
| 108 | + self._redis.srem(self._redis_key("processed"), *upload_ids) |
| 109 | + |
| 110 | + def get_uploads_for_merging(self) -> set[int]: |
| 111 | + return set( |
| 112 | + int(id) |
| 113 | + for id in self._redis.srandmember( |
| 114 | + self._redis_key("processed"), MERGE_BATCH_SIZE |
| 115 | + ) |
| 116 | + ) |
| 117 | + |
| 118 | + def _redis_key(self, state: str) -> str: |
| 119 | + return f"upload-processing-state/{self.repoid}/{self.commitsha}/{state}" |
0 commit comments