https://slatedb.io logo
Discuss handling external compaction requests
# general
s
@criccomini Starting this thread to discuss handling external compaction requests. Moved your thoughts here. https://discord.com/channels/1232385660460204122/1401210792778535055/1403454066910298194 cc: @Rohan @almog
Copy code
text
#### 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)
I also think in the case
If latest .compactor compactor_epoch == current compactor's epoch
, we would need to think how we handle this case in the external compaction request scenario.
I have based my approach in the RFC keeping in mind the above compactor startup condition. This won't happen since external process would be using the same compactor_epoch as the active compactor.
@criccomini @Rohan How do we want this flow to be synchronous or asynchronous? Asynchronous meaning provide the compaction_id as the response to the request despite the compaction is not persisted to the .compactor file. Client can check the status using the status api, if the process restarts the request could be lost or can also get error out due to any failures.. Synchronous meaning provide the compaction_id as the response to the request only after the compaction is persisted to the .compactor file. Client can check the status using the status api. In case of a failure to persist the compaction, client is provided with a suitable error response.
a
I'm wondering if for the external compaction requests we can add a field to
.compactor
that is append-only for external writers and on conflict they retry. A compactor that owns the epoch lock can remove from the
requested
. We would need to change the protocol for seeing new
.compactor
files with epochs we've already seen (see below) Example:
Copy code
compactor.0001 {
  c_epoch: 10,
  requested: [request1]
}
A. Clean Path
Copy code
External 1: read compactor.0001
External 1: writes compactor.0002 {requested = [request1, request2]}
B. External Race
Copy code
External 1: read compactor.0001
External 2: read compactor.0001
External 2: writes compactor.0002 {requested = [request1, request2]}
External 2: attempt write compactor.0002 | exists => fenced
External 2: writes compactor.0003 {requested = [request1, request2, request3]}
C. Compactor Race (External Wins)
Copy code
External: read compactor.0001
Compactor: read compactor.0001
External: writes compactor.0002 {requested = [request1, request2]}
Compactor: writes compactor.002 | CONFLICT
Compactor: attempt epoch bump protocol on new .compactor file with same epoch, write compactor.003 with new epoch
D. Compactor Race (Compactor Wins)
Copy code
External: read compactor.0001
Compactor: read compactor.0001
Compactor: writes compactor.002
External: writes compactor.002 | CONFLICT FAIL
External: writes compactor.003 {requested = [request1]} SUCCESS
s
I was thinking of having an
active_compaction
list persisted that would hold compactions that were being executed and would be moved to the manifest compacted SRs.
Copy code
text
table CompactionState {
    // Identifier of the compaction state. Compactor file name would be derived from this field
    compaction_state_id: uint64;

    // Fencing token to ensure single active compactor
    compactor_epoch: uint64;

    // Compactions in scheduled, in_progress and recently completed compactions
    active_compactions: [Compaction];

    // Creation Timestamp of CompactionState
    created_at: uint64
}
Would the request_id be same as the compactor_id or something that the client would provide? Also, would you return the request_id once it is persisted or as soon as the compaction struct is created?
c
Synchronous sounds more straight forward to me. If clients want it async, they can always wrap the call in an async spawn.
Does it make sense to store requested compactions in the
active_compactions
as well? I can't recall how many fields
Compaction
has, or which specifc fields a request needs.
a
seems to me it's easier to store requested ones separately, that way it's clear that the only process that can modify
active_compactions
is the active compactor. Now that I'm thinking about it I'd be tempted to even have a requested compaction dir that's totally separate and append only entirely (doesn't modify the compactor file at all)
c
If the request data model is significantly different from
Compaction
, then that makes sense. To me, a request is just a scheduled compaction, though. (we would just need the compactor to verify it doesn't conflict before executing it.) OTOH, I saw that @Sujeet had added a lot of fields like priority and stuff... if that's the case, then keeping separate makes sense.
(If we keep separate, I don't feel strongly one way or the other about whether it's a separate dir or a field in the .compactor file)
s
Based on earlier suggestions, the idea I had was to build a
compaction
struct based on the request parameters and persist it as part of the
active_compaction
with no job_attempts(similar to how I was planning to update the existing compaction workflow). As a result, we would not need a separate handling of these external requests and can be accomodated in the existing compaction workflow. I had added a few more
CompactionStatus
states and
CompactionType
. I think we can drop off
CompactionPriority
to start with.
c
SGTM
@almog that work for you?
a
works for me 👍 how are we going to make the coordination with the active compactor work? I'm not sure I got that (may have been discussed but there's lots of dangling discussions)
c
external processes can write to the active_compaction on N+1.compactor. They keep the compactor_epoch the same. On conflict, the compactor/external writer (whichever failed to write the .compactor) refreshes and retries their write.
Much the same as checkpoints in the .manifest
a
got it, makes sense to me
s
Cool. I will structure the low level design on this aligned approach
Can we accept compaction requests if the number of ongoing compactions are at the threshold? Should be persist them as part of
active_compaction
list/map. Currently, we only submit_compaction if the threshold is not breached.
Copy code
rust
async fn maybe_schedule_compactions(&mut self) -> Result<(), SlateDBError> {
        let compactions = self.scheduler.maybe_schedule_compaction(&self.state);
        for compaction in compactions.iter() {
            if self.state.num_compactions() >= self.options.max_concurrent_compactions {
                info!(
                    "already running {} compactions, which is at the max {}. Won't run compaction {:?}",
                    self.state.num_compactions(),
                    self.options.max_concurrent_compactions,
                    compaction
                );
                break;
            }
            self.submit_compaction(compaction.clone()).await?;
        }
        Ok(())
    }
There could also be a possibility that we accept a compaction request but reject it during the validations before starting the compaction.
@Rohan I also had a question on why do we have validations in both ConflictChecker and submit_compaction?
r
ConflictChecker
is just an implementation detail of the size-tiered scheduler. It skips over compactions that migth conflict and moves on to other compactions.
submit_compaction
is the final decider of what compactions actually execute. We could probably change size-tiered scheduler to just return a list of all possible compactions and have the event handler skip over ones that conflict. Not sure its worth doing
> There could also be a possibility that we accept a compaction request but reject it during the validations before starting the compaction. This shouldn't be possible. The definition of accepting a compaction should be writing it to the compactions in compaction state. We should just run all validation before writing it
s
> ConflictChecker is just an implementation detail of the size-tiered scheduler. It skips over compactions that migth conflict and moves on to other compactions. submit_compaction is the final decider of what compactions actually execute. We could probably change size-tiered scheduler to just return a list of all possible compactions and have the event handler skip over ones that conflict. Not sure its worth doing If we allow external compaction requests to be persisted out of Compaction Event Loop, we would need to validate the request before persisting it. So, having some public endpoint that internally validates against
ConflictChecker
and
submit_compaction
would be requried. Wdyt?
c
> So, having some public endpoint that internally validates against ConflictChecker and submit_compaction would be requried. Not 100% clear to me what "public endpoint" means here, but if you're suggesting a remote API (HTTP, gRPC, etc), I would be strongly opposed to that. I don't want any servers or direct communication between SlateDB components.
If you just mean the client is using the same library/code to write directly to the object store .compactor file, that I'm OK with (but I still think the compactor would need to validate before actually executing). TBH, I'm having a bit of trouble following these parallel conversations. Can you summarize what the current proposal is for "Discuss/agree on external process integration design." ?
s
> Can you summarize what the current proposal is for "Discuss/agree on external process integration design." ? Yes. I will write a protocol and share in some time that would help better explain my approach. I think this is intertwined in some way how we plan to add persistence of compactions.