### Protocol for State Management of Manifest and CompactionState This a proposol for Statement Management of Manifest and CompactionState. The protocol is based on the following principals: - Compaction is an entity owned by the Compactor. Therefore, the `compactor_epoch` and the compacted `SortedRuns` are also owned by the Compactor. - CAS based update of the .compactor file ensures consistent view of the compactionState to all the compactor processes. #### On startup... 1. Compactor fetches the latest .manifest file (00005.manifest). 2. Compactor now fetches the latest .compactor file (00005.compactor). 3. It builds a dirty compactionState by merging L0 SSTs and SortedRuns across both files. - Copy SortedRuns from .compactor file to compactionState - Add and delete L0 SSTs of .manifest in compactionState 4. Compactor increments `compactor_epoch` in the dirty CompactionState and writes the dirty CompactionState to the next sequential .compactor position.(00006.compactor). Two level validation: Epoch Check fails, compactor is fenced File version check (in-memory and remote object store), If 00006.compactor exists, - If latest .compactor compactor_epoch > current compactor's epoch, die (fenced) - If latest .compactor compactor_epoch == current compactor's epoch, die (fenced) ( in case of external processes like CLI sending a compaction request, the compaction persistence in the CompactionState would be done by the active Compactor) - If latest .compactor compactor_epoch < current compactor's epoch, increment the .compactor file ID by 1 and retry. This process would continue until successful compactor write. At this point, the compactor has been successfully initialised. Any updates to write a new .compactor file in our case (00006.compactor) by stale compactors would fence them. #### On compaction initiation... 1. Compactor writes to the next .compactor file the list of scheduled compactions with the empty JobAttempts (00007.compactor in our example). If the file exists, die (fenced) #### On compaction job progress... 1. Compactor writes to the next .compactor file the compactionState(persist when an SST is added to SR) with the latest progress (00008.compactor in our example). If the file exists, die (fenced) #### On compaction job complete... 1. Write the current compactor state (including the completed compaction job) to the next .compactor file (steps (1) and (2) in the "progress" section, above). 2. Update in-memory .manifest state to reflect the latest SRs/SSTs that were created (and remove old SRs/SSTs). 3. Write the in-memory .manifest state to the next sequential .manifest file. If the file exists... If it detects manifest version has changed, it could be due to two possibilities: - Writer has written a new manifest. - Compactor has written a new manifest. In both the cases, - Fetch the latest compactorState. - Check if there is a change in the `compactor_epoch` between local and latest compactorState. - If yes, the compactor is stale and is fenced. Else go back to Step (2). ### Summarised Protocol ``` 1. Compactor A starts(compactor_epoch = 1) 2. At T = 1, Compactor A fetches the latest .manifest file 3. At T = 2, Compactor A fetches the latest .compactor file 4. At T = 3, Compactor A creates an in-memory dirty manifest by merging fetched .manifest and .compactor file 5. At T = 4, Compactor A updates .compactor file by creating a new sequential file (On this step, Stale compactors get fenced) 6. At T = 5, Update in-memory .manifest state to reflect the latest SRs/SSTs that were created (and remove old SRs/SSTs). 7. At T = 5, Compactor A tries updating .manifest by creating a new sequential file 8. At T = 6, On file version exists error, fetch both latest .compactor and .manifest file. Check the compactor_epoch and fence the current compactor process else retry Step (6) ``` ### Possible Race conditions #### Incorrect Read order of manifest and compactionState ``` Compactor 1 reads .compactor(compactor_epoch=1, [SR0, SR1, SR2]) Compactor 2 updates .compactor(compactor_epoch=2, [SR0, SR1, SR2]) Compactor 2 updates .compactor(compactor_epoch=2, [SR2]) Compactor 2 updates .manifest(compactor_epoch=2, [SR2]) Compactor 1 reads .manifest ([SR2]) Compactor 1 writes .manifest ([SR1, SR2]) // undoes Compactor 2's change when it should be fenced ``` ### Fenced Compactor Process trying to update manifest ``` .manifest file : [SR7, SR6, SR5, SR4, SR3, SR2, SR1, SR0], .compactor file : [SR7, SR6, SR5, SR4, SR3, SR2, SR1, SR0] At T = 0, Compactor A starts(compactor_epoch = 1), .manifest file: [SR7, SR6, SR5, SR4, SR3, SR2, SR1, SR0], .compactor file : [SR7, SR6, SR5, SR4, SR3, SR2, SR1, SR0] At T = 1, Compactor A (compactor_epoch = 1), updates .compactor file .manifest file: [SR7, SR6, SR5, SR4, SR3, SR2, SR1, SR0], .compactor file : [SR7(merged), SR3, SR2, SR1, SR0] At T = 3, Compactor B starts(compactor_epoch = 2), .manifest file: [SR7, SR6, SR5, SR4, SR3, SR2, SR1, SR0], .compactor file : [SR7(merged), SR3, SR2, SR1, SR0] (Comapactor A is fenced) At T = 4, Compactor B updates (compactor_epoch = 2), updates .compactor file .manifest file: [SR7, SR6, SR5, SR4, SR3, SR2, SR1, SR0], .compactor file : [SR5(merged), SR3(merged)] At T = 5, Compactor B updates .manifest file At T = 6, Compactor A updates .manifest file (Fenced Compactor updating Manifest) ``` Note: The protocol still allows fenced compactor to update the manifest if they are in order because compactor is always syncing compaction state. However, it would get fenced if the file already exists. Consider the following case: ### Fenced Compactor Process trying to update manifest ``` .manifest file : [SR7, SR6, SR5, SR4, SR3, SR2, SR1, SR0], .compactor file : [SR7, SR6, SR5, SR4, SR3, SR2, SR1, SR0] At T = 0, Compactor A starts(compactor_epoch = 1), .manifest file: [SR7, SR6, SR5, SR4, SR3, SR2, SR1, SR0], .compactor file : [SR7, SR6, SR5, SR4, SR3, SR2, SR1, SR0] At T = 1, Compactor A (compactor_epoch = 1), updates .compactor file .manifest file: [SR7, SR6, SR5, SR4, SR3, SR2, SR1, SR0], .compactor file : [SR7(merged), SR3, SR2, SR1, SR0] At T = 3, Compactor B starts(compactor_epoch = 2), .manifest file: [SR7, SR6, SR5, SR4, SR3, SR2, SR1, SR0], .compactor file : [SR7(merged), SR3, SR2, SR1, SR0] (Comapactor A is fenced) At T = 4, Compactor B updates (compactor_epoch = 2), updates .compactor file .manifest file: [SR7, SR6, SR5, SR4, SR3, SR2, SR1, SR0], .compactor file : [SR5(merged), SR3(merged)] At T = 5, Compactor A updates .manifest file [Compactor is fenced but can still update manifest] At T = 6, Compactor B updates .manifest file ``` Note: The above protocol enables us to use the existing compaction logic for merging L0 SSTs/SRs between manifest and compactionState. Hence, that is not added as part of this protocol.