### 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 now fetches the latest .compactor file (00005.compactor). 

2. Compactor fetches the latest .manifest file (00005.manifest).

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.
      ( The current active compactor Job would have updated the .compactor file)


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).
