### 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 .compactor file (00005.compactor). 

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

3. Compactor increments `compactor_epoch` and writes the dirty CompactionState to the next sequential .compactor position.(00006.compactor).

    File version check (in-memory and remote object store), If 00006.compactor exists, 

      - If latest .compactor compactor_epoch > current compactor epoch, die (fenced)

      - If latest .compactor compactor_epoch == current compactor epoch, die (fenced)

      - If latest .compactor compactor_epoch < current compactor 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)

4. If compactor_epoch in in-memory manifest(00005.manifest) >= `compactor_epoch`, the compactor is fenced.

5. Try writing the above `compaction_epoch` to the .manifest file (00006.manifest)

    File version check (in-memory and remote object store), If 00006.manifest exists, 

      - If latest .manifest compactor_epoch > current compactor epoch, die (fenced)

      - If latest .manifest compactor_epoch == current compactor epoch, die (fenced)

      - If latest .manifest compactor_epoch < current compactor epoch, increment the .manifest 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(00006.compactor) or .manifest file(00006.manifest) by stale compactors would fence them.

#### On compaction initiation...

(Manifest is polled periodically to get the list of L0 SSTs created. The scheduler would create a list of new compactions for these L0 SSTs as well)

1. Compactor Fetches the latest .manifest file during the manifest poll.(00006.manifest)

2. Compactor writes to the next .compactor file the list of scheduled compactions with the empty JobAttempts (00007.compactor in our example).

    If the file(00007.compactor) exists, 

      - If latest .compactor compactor_epoch > current compactor epoch, die (fenced)

      - If latest .compactor compactor_epoch == current compactor epoch, goto Step (2)

      - If latest .compactor compactor_epoch < current compactor epoch, panic
        (compactor_epoch going backwards)

#### 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(00008.compactor) exists, 

      - If latest .compactor compactor_epoch > current compactor epoch, die (fenced)

      - If latest .compactor compactor_epoch == current compactor epoch, goto Step (2)

      - If latest .compactor compactor_epoch < current compactor epoch, panic
        (compactor_epoch going backwards)

#### On compaction job complete...

1. Write the current compactor state (including the completed compaction job) to the next sequential .compactor file(00009.compactor) (steps (1) and (2) in the "progress" section, above).

2. Update in-memory .manifest state (fetched in compaction initiation phase) with the compaction 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(00007.manifest) exists, it could be due to two possibilities:

    - Writer has written a new manifest.

    - Compactor has written a new manifest.

    In both the cases, 

      - If latest .manifest compactor_epoch > current manifest epoch, die (fenced)

      - If latest .manifest compactor_epoch == current manifest epoch, reconcile with the latest manifest and write to the next sequential(00008.manifest) .manifest file

      (Writer would have flushed L0 SSTs or updated checkpoint to the manifest)

      - If latest .manifest compactor_epoch < current manifest epoch, panic
        (compactor_epoch going backwards)

### Summarised Protocol

```
1. Compactor A fetches the latest .manifest file(00001.manifest)

2. Compactor A fetches the latest .compactor file(00001.compactor)

3. Compactor A writes .compactor file(00002.compactor) with compactor_epoch(compactor_epoch = 1)

4. If compactor_epoch in .manifest file(00001.manifest) >= compactor_epoch(compactor_epoch = 1), fenced

5. Compactor A writes .manifest file(00002.manifest) with compactor_epoch(compactor_epoch = 1)

(Compactions are scheduled based on the latest manifest poll and CompactionJob updates the .compactor file with the in progress SR state) 

After compactionJob completion...,

6. Update in-memory .manifest(00002.manifest) state to reflect the latest SRs/SSTs that were created (and remove old SRs/SSTs) from the latest .compactor file.

7. Write the in-memory .manifest state to the next sequential .manifest file.

8. If the file(00007.manifest) exists, it could be due to two possibilities:
     - Writer has written a new manifest.

     - Compactor has written a new manifest.

 In both cases, compare the `compactor_epoch` in both .manifest file and the local manifest file and write manifest to next sequential file if the compactor is not fenced.