https://slatedb.io logo
@criccomini thanks for adding the
# general
s
@criccomini thanks for adding the detailed protocol. The approach looks fine to me for ensuring compactors always update the .manifest in order( i.e fenced compactors don't update the .manifest) I am thinking more on this part of the race condition: the question seems to be: if we don't have a compactor_epoch in the manifest, what do we do when the manifest is updated by the compactors out of order? Does out of order updates diverge the manifest from the actual state. I will dry run some examples and post it here. Do let me know if there are any cases that would diverge manifest in any race conditions.
c
In the proposed protocol, I don't think this is a problem since the protocol defines all mutations as compactor state first, then manifest state update.
I think these are the possible permutations:
Copy code
old (c1), old (m1), new (c2), new (m2)
old (c1), new (c2), old (m1), new (m1)
old (c1), new (c2), new (m1), old (m1)
(notation is old updates 00001.compactor, etc)
If new updates c1 first, old is immediately fenced, so those sequences don't need to be considered.
In the second line (new fences old, but old manages to update the manifest first), then new will retry refresh and retry since the compactor epoch in the manifest < the new epoch. In the third line (new fully fences old), old would fail the CAS for the m1 write (the last step) and shut down as fenced.
s
I am dry running with some compaction examples to understand the nuances between earlier approach and the proposed protocol. Am I assuming something incorrect in the above case? I am trying to see if the earlier discussed logic of having compactor_epoch in compactorState diverge the manifest in case of out of order updates. Case: 2 in the PR
c
In T0 and T2 did you intentionally drop SR0 from the .compactor file list?
IIUC, the timeline you illustrate above allows two different processes to modify the .compactor file simultaneously? In T5, it seems that you allow compactor B to mutate the .compactor file even though there is state (SR5 (merged)) that hasn't yet made it into the manifest file.
IIRC, distributed compaction (which the sequence you listed seems to suggest) is out of scope for this RFC
(we could of course reconsider that.. just want to make sure i'm understanding this timeline correctly)
> I am trying to see if the earlier discussed logic of having compactor_epoch in compactorState diverge the manifest in case of out of order updates. The problem with having the compactor_epoch only in the compactor state is that the source of truth for the SSTs/SRs is in the manifest. So, no matter what, the manifest is going to need to get updated by the compactor when it completes a SR. If there's no compactor_epoch in the manifest when a compactor goes to update the SRs, it has to have some other mechanism to determine if another compactor won the race (to prevent adding duplicate SRs and to know if it's been fenced). I don't see how this is possible without adding something to the manifest to signal to the compactor who last updated the SRs. Even if SR IDs are deterministic, since we allow overwriting them, using SR IDs isn't enough. Perhaps we could use the checksum of the SR manifest state (its id and all sst ids or something) to determine if it has been mutated, but that seems very brittle, which is what @Rohan was getting at in his comments on the GH RFC PR
s
> In T0 and T2 did you intentionally drop SR0 from the .compactor file list? > IIUC, the timeline you illustrate above allows two different processes to modify the .compactor file simultaneously? In T5, it seems that you allow compactor B to mutate the .compactor file even though there is state (SR5 (merged)) that hasn't yet made it into the manifest file. Apologies, there was a typo. I have added the updated file. This is the same use-case as mentioned in the Case 2 of the PR. Compactor A gets fenced once Comapactor B starts.
> The problem with having the compactor_epoch only in the compactor state is that the source of truth for the SSTs/SRs is in the manifest. I was treating compactorState as the source of SRs since the compactor was responsible for creating it. If we look at it this way, the manifest would eventually sync with the SRs in the .compactor file?
c
> I was treating compactorState as the source of SRs since the compactor was responsible for creating it. If we look at it this way, the manifest would eventually sync with the SRs in the .compactor file? Who does the syncing? And can there be split brain/race conditions when syncing?
I see, so anyone can sync the SRs to the manifest at any time
essentially it becomes idempotent
🤔 that's interesting
So, in this model, the compactor_epoch lives only in the .compactor file, right?
s
> So, in this model, the compactor_epoch lives only in the .compactor file, right? Yes
> I see, so anyone can sync the SRs to the manifest at any time > essentially it becomes idempotent Yes. That's the idea
c
Yea.. I like that idea quite a bit
(thinking about fencing protocol on .compactor file)
Do we have a race condition on out of order manifest updates, though?
In the following condition:
Ah wait, nm 😝
This seems nice..
I think it might make distributed compactor execution easier as well..
compactor state -> manifest syncing can be completely separate..
s
> I think it might make distributed compactor execution easier as well.. > compactor state -> manifest syncing can be completely separate.. Yes. That's what I had in mind.
c
Though, if that's the case, it could lead to compactors seeing stale data in the manifest (i.e. SSTs that are actually compacted out already)
The L0's source of truth is in the manifest, still, too....
(sorry, thinking out loud)
s
> Though, if that's the case, it could lead to compactors seeing stale data in the manifest (i.e. SSTs that are actually compacted out already) For compactors, they are the source of truth for SRs. I still need to think about corner cases in handling L0 SSTs. But, I feel this pattern is quite nimble if it satisfies for all cases.
c
Yea, I agree.
I'm trying to figure out if we even need compactor_epoch at all haha
I think we still do
s
I think we will need it coordinate the compactors.
c
To detect whether compactor has been fenced, or whether the old writer just beat the new writer (and new writer needs to retry fence)
yep
Yes, this is a neat idea. Mind writing up a protocol description for it so we can reason about it fully?
s
Yes. I will try to come up with a protocol in few days.
c
Awesome! Lookin' forward to it. I will continue to think about this in the background 😛
r
Still need to go over the specific examples above, but I'm not seeing how we can do this without some sort of mutually exclusive access to the main manifest. Without this how can we support syncing the compactor state to the manifest safely? There's always the possibility of some zombie compactor syncing stale state
c
If you: 1. get max manifest file id 2. read latest compactor file 3. write to manifest file id + 1 I think it’s safe?
You’re always syncing the latest state to the next manifest. If that manifest already exists, you have to go back to the compactor file and either die (newer epoch) or resync again.
r
ah so a conflict on the manifest version forces a reload of the compactor state
so
T = 7
in the example above would actually fail because Compactor A's manifest write uses a stale version, and then it reloads the compactor file and discovers its fenced
c
Ya, that’s what I was thinking
I suppose that makes it not totally idempotent, but it means any process can update as long as the manifest isn’t written already
r
I think it kind of distills down to the same basic mechanism. A process that wants to write one of the files does: 1. read both the compactor state and the manifest 2. write either the manifest or the compactor state. If there is a version conflict, go back to (1) and check epoch A process that wants to fence, does: 1. read both the compactor state and manifest 2. write a new epoch to one of the files 3. update the other file to a new version On top of this we can either do tracking of the full set of SRs in the compactor state and syncing this to the manifest, or tracking only ongoing compactions in the compactor state and then applying completed ones to the manifest.
c
That sounds right. Regarding (3), I don’t think we need to update the other file to fence in @Sujeet ‘s protocol. Once the new process writes a copy of the latest compactor file to the + 1 compactor file, all older compactor writers are fenced at that point.
Any lingering writes to the manifest from the old writer would be allowed to go through since it would still be a valid update and the new compactor writer would do a follow up to resync any newer state.
(I think this is cuz the compactor acts as both the fence file and stores the epoch, which is different from how the WAL fence happens).
Ah nm even though the fence is complete on 2 it still has to migrate any missing sr/SSTs to the manifest…
Yea these seem more or less the same..
s
(Thinking out loud) 1. Isn't compactor writing to manifest similar to a reader updating the manifest with updated checkpoint? Isn't the only diff would be merging and writing the latest .manifest and the latest .compactor files rather than just writing the latest .manifest in case of a reader? 2. When a compactor(stale or new) is trying to update the manifest and get a
ManifestVersionExists
error, it would fetch the latest .compactor and .manifest files. In case, the compactor is fenced( updated compactor_epoch in latest .compactor), does it need to stop? Can't it still go ahead and retry writing to the manifest? This fenced compactor won't be able to do anyways update the compactionState and would shutdown on the fence error while compacting.
r
I guess what you're saying is why even bother with fencing at all, and just apply mutations to the manifest if it looks like the state of the manifest is consistent with the state of the manifest that is the basis for the mutation. The difference in my mind is that (1) the consequence of a bug is minor for checkpoints - you might end up with an extra checkpoint. Whereas a compactor can corrupt the state of the db. (2) it's relatively tricky (e.g. bug-prone) to make the mutation that the compactor is doing safe, whereas in the case of a process creating a checkpoint it's straightforward - just add the checkpoint to the list of checkpoints. For the compactor, it needs to ensure that the source sorted runs are still present, and there is no conflict with the destination sorted run in the sense that it's still ordered correctly with respect to the way the sources were ordered relative to the other sorted runs. It's definitely possible but I'd say bug-prone to write this check. In fact its' probably worthwhile to do this check even with mutual exclusive access to manifest/compactor, as an additional failsafe. But it's easier to write the check under the assumption that only one process is mutating the state. You can just assert that either all the source sorted runs exist, and the destination doesnt or vice versa. And panic if the db isn't in one of those 2 states. Basically it would make it safer and easier to think about if we could just rule out the possibility of multiple compactors racing. (3) There is obvious benefit to allowing multiple processes to checkpoint. I can't think of any benefit to having multiple active compaction coordinators.
s
> I guess what you're saying is why even bother with fencing at all, and just apply mutations to the manifest if it looks like the state of the manifest is consistent with the state of the manifest that is the basis for the mutation. In my model, SortedRuns are owned and managed by the compactor. CompactionState is the source of truth for all the compactor processes( stale and new) for sortedRuns. So, on high level the SRs in .manifest will always represent some version of .compactor file and the final list of SRs in manifest should correspond to the list of SRs in compactionState I am saying whenever a process tries to update the
compactionState
by writing a new .compactor file it would be fenced or it will succeed. In a single processing leg of a compactor process, the update order would be: 1. Update the compactionState by writing a new .compactor file( first during startup and then during persisting SRs) 2. Update the manifest by writing a new .manifest file(last step). So, even if we don't bother to fence a old compactor process at Step (2), it will not be able to diverge the compactionState thereby the state of SRs since it will get fenced in the next processing leg. Based on the code reference in the
finishCompaction()
, older SRs and older L0 SSTs are removed from the in-memory state and then manifest is persisted. The mutation(merge of manifest and compactionState) for a in-memory manifest would be done as follows based on my current understanding of the code: 1. Pick SRs only from the .compactor file. 2. For L0 SSTs, merge the newly added L0 SSTs in .manifest file to the .compactor file to create the L0 SSTs in the in-memory manifest. Wdyt? Could you also let me know the bottlenecks/concerns you perceive with this approach so that I can handle them while writing the protocol?
> It's definitely possible but I'd say bug-prone to write this check. In fact its' probably worthwhile to do this check even with mutual exclusive access to manifest/compactor, as an additional failsafe. But it's easier to write the check under the assumption that only one process is mutating the state. You can just assert that either all the source sorted runs exist, and the destination doesnt or vice versa. And panic if the db isn't in one of those 2 states. Basically it would make it safer and easier to think about if we could just rule out the possibility of multiple compactors racing.  Yes, we also have the flexibility to stop the stale compactor from writing the manifest as you mentioned in your earlier comments. However, the allowing the compactors to update the manifest as mentioned above I think should also be fine.
r
> So, even if we don't bother to fence a old compactor process at Step (2), it will not be able to diverge the compactionState thereby the state of SRs since it will get fenced in the next processing leg. Ok so in this this step (Step 2), the compactor is updating the manifest. What happens if it detects that the manifest version has changed? Does it first go and re-check the compactor state to see if it's fenced or does it just go ahead and update the manifest anyway? If it's the latter then the concern is that it would be updating the manifest with a stale view of the SRs, which will cause the main db to return invalid reads
s
If it detects manifest version has changed, it could be due to two possibilities: 1. Writer has written a new manifest. 2. Comapctor has written a new manifest. In both the cases, it will fetch the latest manifest and latest compactorState, merge them and retry. Here, while merging, we have two possibilities, 1. Check the compactor_epoch and drop the fenced process and stop merge. If no compactor_epoch change, go to step 2. 2. Let the merge continue and allow the process to complete the manifest write.
r
Yes. What I'm saying is I think it's much safer to go with option 1 in your second list below, because it guarantees we don't have a split-brain view of the set of sorted runs. And there isn't really any meaningful drawback from doing so
s
My only far fetched thinking was in case we want to enable distributed compaction do you see any difference if we go with option 1 or option 2. Or should we think about it later?
r
I think distributed compaction is a separate problem of assigning work to a set of workers. To solve that we could model it as registering compaction tasks somewhere (maybe the compaction state), and come up with some protocol to allow workers to claim ownership over compaction tasks. The protocol can be looser because it's not the end of the world if multiple workers claim ownership over the same task for some duration. But we'd still have a single owner for the set of SRs that make up the db.
I don't think there is any benefit to having a distributed coordinator/orchestrator that's actually deciding what to compact and handling the results of completed compactions
s
> But we'd still have a single owner for the set of SRs that make up the db. Yes, this is the reason I am thinking of a separating the ownership of SRs from manifest to compactionState so that it would only be the compactors that update compactionState. With this model, we can leverage the
compactor_epoch
to coordinate a single active compactor to write compactionState.
> I don't think there is any benefit to having a distributed coordinator/orchestrator that's actually deciding what to compact and handling the results of completed compactions Makes sense.
> The mutation(merge of manifest and compactionState) for a in-memory manifest would be done as follows based on my current understanding of the code: > 1. Pick SRs only from the .compactor file. > 2. For L0 SSTs, merge the newly added L0 SSTs in .manifest file to the .compactor file to create the L0 SSTs in the in-memory manifest. With this model, I think the above changes are required during merging. Do you see any other mutations required while merging remote manifest and compactionState in-memory?
r
I think assuming we go with option (1) above (compactor checks if its fenced on a manifest version conflict), then this is sufficient - it's equivalent to what the compactor is doing today
s
I think we would need to add the merging of compactor logic to the present form of compactor, if we go with option (1), right?
Would the above 2 merging mutations be sufficient?
r
not sure I follow - what do you mean by merging of compactor logic?
s
Pulling the latest compactionState from .compactor file and merging with the latest .manifest file to make an in-memory manifest for remote write.
r
we should only need to do this during initialization right? After that (with option (1)), we're guaranteed that no other process could have updated the compactor state
s
> we should only need to do this during initialization right? Yes. The active compactor would also need to do the above when publishing a new manifest after completion of the CompactionJob, right?
(Just thinking out loud) During persisting partial SRs, compactor would only be updating the compactionState by writing a new .compactor file. (No manifest interactions here).
r
yeah, but if it's updating the compactor state then it can keep it's in-memory view consistent with those updates. I'm just saying that it doesn't need to synchronize its in-memory view with writes from some other writer.
that's how I interpreted merge at least - there's potentially some new updates that I don't know about so I have to merge them with my local view
s
> yeah, but if it's updating the compactor state then it can keep it's in-memory view consistent with those updates. I'm just saying that it doesn't need to synchronize its in-memory view with writes from some other writer. I didn't follow this. Is this about the active compactor writing a new .manifest file (using the same writer epoch) or you are talking about multiple compactors trying to update the compactionState by writing new .compactor file?
r
The former. Also the compactor doesn't touch the writer epoch (not sure if you meant compactor epoch)
s
> Also the compactor doesn't touch the writer epoch (not sure if you meant compactor epoch) I mean the active compactor makes a request to the active writer to write the manifest. It doesn't touch the writer_epoch
r
It doesn't do that - it writes the manifest directly
s
I mean the writer_epoch should be the same as in the earlier manifest, right? Similar to how reader write the manifest( treating it as the writes are going through the active writer)
r
yep that's right
s
So, to summarise the manifest update flow for an active compactor: 1. When the active compactor tries to write a manifest file, it will fetch the latest .compactor and .manifest files and create a merged in-memory view. In case of compactor_epoch changes, it will fence and stop. ( so that it merges the new L0 SST writes from writers). 2. Once this in-memory view is updated, it will try to publish a new .manifest file. If it succeeds the compaction leg completes.In case of failure, go back to step 1
r
In (1) it needs to read the latest manifest file before reading the latest .compactor file. I think you could also "simplify" it if the compactor observes that the .manifest has not changed. In that case, it shouldn't need to refresh the .compactor file, since it then knows its not fenced and so no other active .compactor could have written it. And when initializing, it needs to first write a new .compactor with a bumped epoch, and then write a new .manifest version.
s
> In (1) it needs to read the latest manifest file before reading the latest .compactor file. I think you could also "simplify" it if the compactor observes that the .manifest has not changed. Why would the order matter? > In that case, it shouldn't need to refresh the .compactor file, since it then knows its not fenced and so no other active .compactor could have written it. We would be maintaining the compactor_epoch only in .compactor file. So, we would need to refresh the .compactor file in case of failure during manifest write to check if this compactor is fenced. Manifest won't be updated during the compactor startup.
r
If it reads the compactor before the manifest, it's prone to a race condition like:
Copy code
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
> Manifest won't be updated during the compactor startup. I think this is ok, since the compactor is always syncing the compactor state. So if there's no state to sync, then the zombie compactor will detect its fenced when it tries to update the compactor state before updating the manifest. And if there is state to sync, then the new compactor will write the manifest. Seems harmless to just write a new manifest version in either case to force the old compactor to be fenced
s
Won't the Compactor 1 fail to write the .manifest file with the error
ManifestVersionExists
since the manifest file it planned to write to has already been written and retry? During retry, it would fetch the latest compactionState with configuration (compactor_epoch=2, [SR2]) and get to know that it has been fenced due to change in compactor_epoch
r
In the example Compactor 1 reads the manifest after compactor 2 updates it, so it sees the latest version. You can prevent this by requiring the manifest to be read before the compactor state
c
^ yep, I think this is critical
(catching up on this thread 😅 )
s
Ah..got it.
The doc contains the proposal of State Management of compaction State and Manifest. This proposal tries to decouple manifest and compaction state from each other by moving the ownership of SRs and compaction_epoch to .compactor files.
c
@Sujeet this looks excellent. I have some minor quibbles that I think are most likley typos, but this makes sense to me. As @Rohan said, it's largely the same protocol as what I had proposed, just inverted. I don't have a strong preference for one vs. the other. It does seem intuitive to me to keep all the compactor-related stuff in the .compactor file.
In some ways, the compactor syncing reminds me of actual vs. ideal config/state syncing
I also wonder if it's necessary for the .compactor file to keep a copy of L0 as you suggest. It seems to me that we might be able to get away without this. When a compaction SR completes, any sources that are L0's (as defined by the manifest) can be removed from the dirty manifest, right?
Mind putting this in the RFC so I can leave comments? 😅
s
Yes. I actually copied it from the RFC to see if it looks good before putting it to the RFC
> I also wonder if it's necessary for the .compactor file to keep a copy of L0 as you suggest. It seems to me that we might be able to get away without this. When a compaction SR completes, any sources that are L0's (as defined by the manifest) can be removed from the dirty manifest, right? Yes, we should be able to get rid of the L0 from the .compactor file when the SR completes because the .compactor file will always be in sync. However, need to consider the corner cases (if any)
> As @Rohan said, it's largely the same protocol as what I had proposed, just inverted. I don't have a strong preference for one vs. the other. It does seem intuitive to me to keep all the compactor-related stuff in the .compactor file. I feel easier to comprehend this protocol since it works in tandem with the existing compaction logic. The separation of concerns it offers I think can be leveraged for distributed compaction coordination as well.
c
Yea, it feels intuitive to me
s
> Mind putting this in the RFC so I can leave comments? 😅 Done
r
This protocol takes me a minute every time I have to go back and read it. Any reason not to just include the compactor epoch in both files? That seems really easy to reason about. So when initializing, the compactor increments the epoch in either the manifest or the compactor state, and then all subsequent writes to either file use the bumped epoch. If it ever sees that one of the files uses a larger epoch, it decides its fenced and exits. We don't have to be super careful about the order in which we read the files. And as a side benefit it might also be nice to be able to pull the set of manifests that were modified by a given compactor
s
> Why do we need to be careful about the order in which we read the files Is it specific to this protocol or in general regarding reading .compactor and .manifest files? I feel it easier to reason about manifest and compactor state independently with this protocol. It seems intuitive to me to keep all the compactor related stuff in .compactor file. In my view, having compactor_epoch in both files could add to confusion unless someone has complete understanding of how this sync between manifest and compactor state works. How would one reason if the compactor_epoch is out of sync temporarily due to write failures, etc? We might also be adding extra writes in the above protocols to keep both manifests and compactor state in sync. I think the current logic, we are assured that the manifest and compactor would eventually sync( though data consistency would always remain) and the number of object store operations are also less. However, I am fine to add the compactor_epoch to the .manifest file if we have a consensus around it.
r
> Is it specific to this protocol or in general regarding reading .compactor and .manifest files? The protocol. I'm fine with having separate .compactor/.manifest files to track the db writer/reader state vs the state of ongoing compactions. That said TBH I'm unconvinced on maintaining the SRs in both the .compactor and .manifest. I don't really see a strong reason to maintain it twice. But it's more or less just an implementation detail, so I don't feel too strongly about that.
> I feel it easier to reason about manifest and compactor state independently with this protocol. It seems intuitive to me to keep all the compactor related stuff in .compactor file. They are not really independent though. The compactor must synchronize the two in a way that is always consistent (e.g. the manifest should not reflect an older view of the compaction state) . It's pretty easy to do this if the compactor has exclusive access to both files. This is what the proposed protocol is trying to do, but it's hard to follow. It relies on always reading the manifest before the compactor file, and it's hard to understand why without thinking about it deeply. Initialization may or may not require writing out the manifest, but again it's hard to understand why without sitting down and thinking about it. Using epochs in both places seems really easy to understand to me: (1) it builds on the existing atomic update protocol of each individual file (as the protocol in the rfc does) (2) the process first needs to establish an epoch. It does so by bumping the compactor epoch in the manifest (or compactor state file, as long as its always the same place) (3) it then records the new epoch in the other file, as long as the epoch is higher than the epoch recorded in that file. It can technically skip this step and just write the epoch as part of the next write it would naturally do, but its easier to reason about if it just does this when establishing the epoch. (4) at this point, the compactor has exclusive access to both files. It just needs to the epoch whenever there's a version conflict. On top of this the compactor can safely sychronize between the compactor state and manifest since it has exclusive access to both.
> In my view, having compactor_epoch in both files could add to confusion unless someone has complete understanding of how this sync between manifest and compactor state works. How would one reason if the compactor_epoch is out of sync temporarily due to write failures, etc? I don't really understand this concern. I don't think makin the manifest/compactor state self-explanatory is really a design goal - you have to take some time to understand their contents. That said, I think the concept of an epoch is pretty well understood in distributed systems. There is a single epoch for the compactor - the field just records which compactor epoch each file is currently on.
> We might also be adding extra writes in the above protocols to keep both manifests and compactor state in sync. I think the current logic, we are assured that the manifest and compactor would eventually sync( though data consistency would always remain) and the number of object store operations are also less. Yes, there is an extra write during initialization (though as explained above you can avoid it). I'm not worried about that overhead at all if it yields an easier to understand protocol.
s
> That said TBH I'm unconvinced on maintaining the SRs in both the .compactor and .manifest. I don't really see a strong reason to maintain it twice. But it's more or less just an implementation detail, so I don't feel too strongly about that. I had thought about it. I think keeping the SRs only in .compactor further simplifies the protocol as we would not need to maintain the order of reading and writing the manifest since there won't be any syncing issues between SR state. We could also get rid of compactor_epoch from .manifest itself without losing on simplicity. However, my only concern is the backward compatibility with the existing code. I am unsure the amount of change required in read workflows, etc.
r
Yeah that's another option. The DB would then need to periodically refresh both the compactor state and the manifest and merge the two together. We'd also need to maintain references to both when taking a checkpoint. At first glance I'd be ok with this approach too.
s
> They are not really independent though. The compactor must synchronize the two in a way that is always consistent (e.g. the manifest should not reflect an older view of the compaction state) . It's pretty easy to do this if the compactor has exclusive access to both files. This is what the proposed protocol is trying to do, but it's hard to follow. It relies on always reading the manifest before the compactor file, and it's hard to understand why without thinking about it deeply. Initialization may or may not require writing out the manifest, but again it's hard to understand why without sitting down and thinking about it. By independent I mean on the SR creation/compaction side, it doesn't need to bother about interacting with manifest. > Using epochs in both places seems really easy to understand to me: > (1) it builds on the existing atomic update protocol of each individual file (as the protocol in the rfc does) > (2) the process first needs to establish an epoch. It does so by bumping the compactor epoch in the manifest (or compactor state file, as long as its always the same place) > (3) it then records the new epoch in the other file, as long as the epoch is higher than the epoch recorded in that file. It can technically skip this step and just write the epoch as part of the next write it would naturally do, but its easier to reason about if it just does this when establishing the epoch. > (4) at this point, the compactor has exclusive access to both files. It just needs to the epoch whenever there's a version conflict. I agree that it would be easier to understand why we need to read the manifest first and then the compactor due to difference in compactor_epoch if the manifest is updated.
> Yeah that's another option. The DB would then need to periodically refresh both the compactor state and the manifest and merge the two together. We'd also need to maintain references to both when taking a checkpoint. At first glance I'd be ok with this approach too. Merging would be required to remove the old L0 SSTs from manifest, right? Couldn't we do it in the final CompactionJob completion step as we are doing now? But, I think it would not require any synchronisation.
> We'd also need to maintain references to both when taking a checkpoint. At first glance I'd be ok with this approach too. Yes. Even the read workflow would need some code change.
r
> Merging would be required to remove the old L0 SSTs, right? Couldn't we do it in the final CompactionJob completion step as we are doing now? But, I think it would not require any synchronisation. Well the whole point of doing it this way would be to have a single writer for each of the manifest and compactor. So the writer writes the manifest, and the compactor writes the compactor state. So in that world the compactor would just record the last compacted l0 in compactor state, and the writer would read that and update the L0s in the manifest accordingly
s
> Well the whole point of doing it this way would be to have a single writer for each of the manifest and compactor. So the writer writes the manifest, and the compactor writes the compactor state. So in that world the compactor would just record the last compacted l0 in compactor state, and the writer would read that and update the L0s in the manifest accordingly My point is since there is a single compactor state updating process, last compacted l0 would always be in sync. And since L0 Id won't go backward, the compactor process in an idempotent way remove the old L0s. I think were are now discussing on the mechanism pull vs push for updating the manifest. I also think removing the SR from manifest eliminates the whole synchronisation thing.
r
> last compacted l0 would always be in sync what does this mean?
s
> last compacted l0 would always be in sync > what does this mean? The ULID of the last compacted L0. We can remove all the l0 SSTs prior to this. The value of this won't go backwards.
r
which process will remove them from where?
all I'm saying is the writer has to be the one to remove them from the manifest in this model
s
> which process will remove them from where? In my earlier comment, it was compactor process using the active writer_epoch to update the manifest or a signal to writer process to update the manifest during the compactionJob completion (sort of a push based mechanism). But, I am fine with both poll vs push till the time the sync issue is getting resolved with this new change. However, do you see any issues with backward code compatibility? > all I'm saying is the writer has to be the one to remove them from the manifest in this model You mean the writer process or any process writing using same the writer_epoch?
r
> You mean the writer process or any process writing using same the writer_epoch? I don't understand the question. There is only 1 process that can write using a given writer epoch, and that's the writer that is on that epoch
> But, I am fine with both poll vs push till the time the sync issue is getting resolved with this new change. However, do you see any issues with backward code compatibility? What sync issue? I don't really see a code compatibility problem. The bigger problem is the compatibility of the files themselves. So there would need to be a migration path, which is a bit complicated.
a
Still catching up on this discussion (lots to read!) but just wanted to drop a note while I do that for https://github.com/slatedb/slatedb/pull/695#discussion_r2261086165 which I commented before seeing this discussion. May be an interesting thing. Some of my instincts: 1. I like having manifests be self contained, so references from the manifest -> the compaction state seems undesirable (re: removing SRs from the manifest) 2. I like having the compactor epoch in the manifest because the most critical part that we want to fence is updating the manifest with the new SR/SST information. We never want a zombie accidentally doing that, whereas a zombie accidentally messing with the unpersisted compaction state will just get GC'd at some point 3. Maintaining consistent, mutable state in a distributed system is hard. Currently the manifest is the only thing that can do that, everything else depends on that for the sync barrier. I'd like to keep it that way if we can (hence see suggestion in my comment on the PR)
Also re: distributed compaction I agree with Rohan's suggestion that coordination should still be kept centralized instead of interact with this system. I see that as two orthogonal designs (a fleet of compactors can elect a leader and use that leader to do the coordination, but from Slate's perspective it's just one coordinator)
I also wasn't sure I understood how external-triggered compactions work in the proposed design, but maybe that's for another thread
c
Oh, boy. Got some reading to do. 😅
s
> I don't understand the question. There is only 1 process that can write using a given writer epoch, and that's the writer that is on that epoch I mean how readers write using the active writer epoch( one can think it is the writer writing on behalf of the reader) > What sync issue? I don't really see a code compatibility problem. The bigger problem is the compatibility of the files themselves. So there would need to be a migration path, which is a bit complicated. Do you mean folks already using SlateDb would need to migrate data to the new construct? How is that a problem? The structure of the file will eventually be consistent, right? But, logic wise it will solve the biggest coupling problem and also make the compactor idempotent.
> I like having manifests be self contained, so references from the manifest -> the compaction state seems undesirable (re: removing SRs from the manifest) I feel like SRs are never an outcome of a client write. It is our internal file handling construct. So, why couple it with manifest. Similarly, having details like active_compactions, references to CompactionJob in manifest doesn't seem right to me.( if we plan to have all the details in .manifest) > I like having the compactor epoch in the manifest because the most critical part that we want to fence is updating the manifest with the new SR/SST information. We never want a zombie accidentally doing that, whereas a zombie accidentally messing with the unpersisted compaction state will just get GC'd at some point The proposed protocol handles this. We are persisting a compaction to compactionState as soon as it is scheduled by the scheduler. The protocol also extends the existing logic of compaction without requring major code changes to it.
> Also re: distributed compaction I agree with Rohan's suggestion that coordination should still be kept centralized instead of interact with this system. I see that as two orthogonal designs (a fleet of compactors can elect a leader and use that leader to do the coordination, but from Slate's perspective it's just one coordinator) The coordination would be handled using
compactor_epoch
and it is the execution of compactionJob that would be distributed. In the proposed protocol, one would need to move the CompactionJob execution to a separate process and the compaction scheduler would be able distribute compactions to these processes. However, for reliability and availability purposes we would need some kind of a coordination protocol to let say elect a compactor scheduler, etc in any protocol which would operate over the above construct. > I also wasn't sure I understood how external-triggered compactions work in the proposed design, but maybe that's for another thread Gist, external triggered compaction need to run where the compactor process is running. The request would be sent to a channel on which the eventloop is listening for compaction events and would trigger the initiation of the compaction with the existing flow.
Based on the discussion so far, I am inclined to removing the SRs from .manifest files altogether. The biggest advantage is the requirement of sync between .manifest and .compactor files. Updating the L0 SSTs in manifest would be a idempotent operation. However, I need to understand the touch point of these changes to the existing flows. The places I see is where changes would be required: 1. checkpoints 2. snapshots 3. Range scans and reads
We should also take into account scenarios wherein the writer process may be shutdown but compaction needs to happen. Keeping everything in manifest with the writer process responsible to write the state won't work in this case. However, a separate compactor process can continue compacting and persisting the state in .compactor file with sync between .manifest and .compactor state happening eventually.( case with the proposed protocol)
c
> Also re: distributed compaction I agree with Rohan's suggestion that coordination should still be kept centralized instead of interact with this system +1 to this as well. In talking with Sujeet yesterday, I proposed the idea of writing a SlateDB operator for K8s that could handle orchestration. Sujeet also proposed the idea of a chitchat-based (gossip) distributed compactor. IMO, the target for our protocol should be to treat the compactor state file as single writer, and if applications want distributed compaction, they need to handle their own leadership election, scheduling, orchestration, execution etc. The "API" between SlateDB and that is the .compactor state file protocol we're defining. > I mean how readers write using the active writer epoch( one can think it is the writer writing on behalf of the reader) Only one writer writes using the writer epoch, and that is the writer process. I think there is some confusion about your comment because you were suggesting that the compactor could use the writer's epoch. In a sense, this is what the protocol you prooposed does: it has a compactor epoch only in the compactor file. So, when it updates the manifest, it simply writes to the file without any ownership of it. This works because we agree to always a) read manifest, b) read compactor state c) bump compactor epoch. I think this gets at the core of Rohan's concern about the proposed protocol. This ordering dependency is non-obvious and if broken can lead to corruption.
> Do you mean folks already using SlateDb would need to migrate data to the new construct? How is that a problem? The structure of the file will eventually be consistent, right? But, logic wise it will solve the biggest coupling problem and also make the compactor idempotent. IIRC, Rohan's concern was if we eliminated the SRs from the manifest. In such a case, the clients will need migrate their SRs out of the manifest into the .compactor file when it's first started. No biggy, though. But it is custom code to handle the migration. > However, for reliability and availability purposes we would need some kind of a coordination protocol to let say elect a compactor scheduler, etc in any protocol which would operate over the above construct. I think this is where you and I diverge. From SlateDB's perspective, IMO, the compactor should always appear as a single writer. All coordination/failure detection/execution/scheduling should happen outside of SlateDB. The external (distributed) compactor can do whatever it wants as long as it implements our protocol for updating the compactor state file.
> Gist, external triggered compaction need to run where the compactor process is running. The request would be sent to a channel on which the eventloop is listening for compaction events and would trigger the initiation of the compaction with the existing flow. Not sure I follow this. I agree that an external triggered compaction can only actually run when the compactor is running. But I don't think external compactions should be triggered through a channel (tokio/oneshot). IMO, the external process should write an unstarted job (or job request) to the .compactor file. The compactor would refresh and see this new job and execute it up as it would any other.
I need to think about this approach. It's a significant change. I'm a bit uncomfortable with it at first blush. I like the idea of having the manifest be the source of truth for everything that clients need to read and write. I had been thinking of the .compactor file as an implementation detail of the compactor. The removal of SRs from the manifest would be a significant divergence from this view.
tl;dr right now, I feel like: (1) .manifest should be source of truth for reader and writer clients (and should thus contain SRs) (2) .compactor file should be an implementation detail of the compactor, not something the DB needs to pay any attention to. With this view, I feel like the entire .compactor file is really just a single implementation for our built-in (non-distributed) compactor. In fact, for distributed compaction, it need not matter at all. The core of our compaction protocol is simply: the owner of the
compactor_epoch
may manipulate the SRs and L0s in the manifest. This RFC is simply how our current compactor manages resuming on failed compactions. If a distributed compactor wants to store their compactor state in etcd (k8s), that's great. If they want to use gossip and store state on local disks, that's great, too. If they want to store in object storage, awesome. We don't care as long as they have a single writer (compactor_epoch) updating our manifest.
s
@criccomini I am a bit confused. What are you proposing? 1. Should we have a .compactor file 2. Should we have everything in .manifest file 3. Should we have compactor_epoch in both .manifest file and .compactor file 4. Should we decouple .manifest file and .compactor by moving SRs and compactor_epoch
c
(1) yes (2) yes, except for the scheduled/in-process/complete compaction jobs (3) manifest yes, compactor file I could go either way (4) moving them to compactor file (and out of manifest)? No, i feel SRs should live in the manifest.
s
Ok. So, only update required in the above protocol would be to update compactor_epoch in manifest as well and maintain it in both places, right?
c
I believe so, yes. There might be some quibbles about what (duplicate) data we keep in the .compactor file, but I think your protocol is mostly compatible with my viewpoint.
I kinda feel we shouldn’t bother with keeping SRs in the compactor file, for example.
If we can get consensus around my view of things, I think we can work through the details of your protocol more easily.
(I’m open to alternative view of course)
s
Folks can we get an alignment so that we can finalise and close on the approach. cc: @almog @Rohan
r
> I need to think about this approach. It's a significant change. I'm a bit uncomfortable with it at fir> I need to think about this approach. It's a significant change. I'm a bit uncomfortable with it at first blush. I like the idea of having the manifest be the source of truth for everything that clients need to read and write. Yeah this was my gut reaction as well. I think its possible to model it with the SRs in a separate file, and this does make the boundaries of what is allowed to write what parts of the metadata more clear. On the other hand, having a single atomically updated object with all the metadata required for reading i s really nice. It makes it really simple to get a consistent view of the db for reading, checkpointing, and garbage collection. IMO this is a worthwhile property to retain.
> > I had been thinking of the .compactor file as an implementation detail of the compactor. The removal of SRs from the manifest would be a significant divergence from this view. > tl;dr right now, I feel like: > > (1) .manifest should be source of truth for reader and writer clients (and should thus contain SRs) > (2) .compactor file should be an implementation detail of the compactor, not something the DB needs to pay any attention to. > > With this view, I feel like the entire .compactor file is really just a single implementation for our built-in (non-distributed) compactor. In fact, for distributed compaction, it need not matter at all. The core of our compaction protocol is simply: the owner of the compactor_epoch may manipulate the SRs and L0s in the manifest. > > This RFC is simply how our current compactor manages resuming on failed compactions. If a distributed compactor wants to store their compactor state in etcd (k8s), that's great. If they want to use gossip and store state on local disks, that's great, too. If they want to store in object storage, awesome. We don't care as long as they have a single writer (compactor_epoch) updating our manifest. +1. This is totally aligned with the way I view the compactor.
> There might be some quibbles about what (duplicate) data we keep in the .compactor file, but I think your protocol is mostly compatible with my viewpoint. I kinda feel we shouldn’t bother with keeping SRs in the compactor file, for example. + 1
c
@Sujeet I’ll take another pass at the RFC tomorrow and leave feedback on the protocol with this viewpoint in mind. I don’t think we’re far off, but it probably needs some tweaks since the compactor epoch would be in (at least) the manifest now.. and SRs will probably live there as well (need to think on that).
I need to think about third party compaction requests too…
a
I'm +1 on this as well! We should summarize that and put that "philosophy" on the top of the RFC for future readers. I think that heps frame it a lot
c
I left a review and high-level plan on how we cam move forward with this RFC: https://github.com/slatedb/slatedb/pull/695#pullrequestreview-3101779741 I think the next steps right now are: 1. @Sujeet Update "Protocol for State Management of Manifest and CompactionState" and "Summarised protocol" to reflect protocol based on design goals. I left some high-level comments in the RFC. 2. @criccomini Start discussion on external process integration design.
Regarding (2), an open question is how we handle third party compaction requests given the agreed upon vision for compaction in SlateDB (https://discord.com/channels/1232385660460204122/1401210792778535055/1403219350969909258). My proposal is that we think of the
.compactor
file protocol as part of the public interface for compactors. So the agreement is a compactor that owns the latest
compactor_epoch
may: 1. Update SRs, LOs, and related compactor fields in
.manifest
, provided it adheres to the manifest update protocol. 2. Whatever it sees fit with the
.compactor
file, provided it adheres to the protocol defined in our RFC. Our current compactor would be a single node (non distributed, non-fault tolerant) implementation of the compactor protocol, which handles both .manifest and .compactor updates and syncing. Other implementations (k8s, chitchat, kafka based, object store-based, etc) can do whatever they want, provided they follow the compactor protocol defined in this PR. This approach is appealing to me because it allows us to keep SlateDB agnostic to the compactor implementation, while still having a standard way to track job progress, request compactions, and so on. We can have a single unified set of CLI and UI tooling for running things like
slatedb compaction status
that will work whether we have a k8s distributed compactor, our simple local compactor, or whatever other compactors people cook up.
(i had a lot of coffee today, so LMK if this is gibberish)
s
I was revisiting the protocol, I was thinking if the above condition possible because before
Compactor 1 writes .manifest ([SR1, SR2])
, Compactor 1 would try to persist the state and get fenced( Step (4) of the startup protocol). Hence, it would not go ahead and write manifest during the completion of CompactionJob. cc: @criccomini
Copy code
text
#### 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)
I checked the code and there would be concurrent CompactionJob executing, trying to write their compactionState to the .compactor file. Hence, I think we would need the protocol to try write to the next sequential .compactor file by merging with the existing state in case of file conflict and if the compactor is not fenced.
c
Kid soccer tourney stuff today, but I'll try and get back to you by EOD 😅
Need think make sure I understand what you're saying
r
@criccomini just to make sure I'm understanding your proposal correctly. The .compactor file would not just be an implementation detail of the compactor. Rather, any compactor implementation must maintain a .compactor file with the contractual schema, which will function as the interface between the compactor and stuff outside it. This makes sense to me.
@Sujeet I'm assuming you're referencing the version of the protocol where we only track the epoch in the compactor state. I think the problem is that Compactor 1's write to the .compactor state which does the final update for the compaction can be ordered before all of Compactor 2's writes to the .compactor state. But it's interactions with the manifest can be ordered after Compactor 2's writes to the manifest. Hence the potential race condition in the protocol if you don't require that the manifest read happens before the corresponding compactor state update.
> I checked the code and there would be concurrent CompactionJob executing, trying to write their compactionState to the .compactor file. Hence, I think we would need the protocol to try write to the next sequential .compactor file by merging with the existing state in case of file conflict and if the compactor is not fenced. Yes. It seems simpler for the current non-distributed implementation to just do the compactor state writes through the event handler. So the jobs can just report status back over the channel and let the event handler maintain the .compactor file
s
> Yes. It seems simpler for the current non-distributed implementation to just do the compactor state writes through the event handler. So the jobs can just report status back over the channel and let the event handler maintain the .compactor file Yes, this would make it centralised. However, the version exists condition in case of a file conflict needs to be updated in the protocol as mentioned above so that the state of the .compactor file remains consistent.
> @Sujeet I'm assuming you're referencing the version of the protocol where we only track the epoch in the compactor state. Yes. I was revisiting our earlier discussions.
> @Sujeet I'm assuming you're referencing the version of the protocol where we only track the epoch in the compactor state. I think the problem is that Compactor 1's write to the .compactor state which does the final update for the compaction can be ordered before all of Compactor 2's writes to the .compactor state. But it's interactions with the manifest can be ordered after Compactor 2's writes to the manifest. Hence the potential race condition in the protocol if you don't require that the manifest read happens before the corresponding compactor state update. I am finding it difficult to think about this case due to the following reasons: 1. Latest manifest fetch is followed by the compactor state update to the .compactor file during startup. Hence, old compactor would get fenced whenever it tries to update the .compactor file 2. Now, the only other possibility of the race condition of out of order update of .manifest file are the other race conditions mentioned in the protocol. However, synced state of .compactor file ensures that the .manifest will eventually sync with the state of .compactor file. Do you see any change in the protocol after adding the compactor_epoch in .manifest file?
r
> Latest manifest fetch is followed by the compactor state update to the .compactor file during startup. Hence, old compactor would get fenced whenever it tries to update the .compactor file Here's what I think you're trying to say (correct me if I'm wrong). Compactor 1 will eventually go to write the compactor state file as part of some later update iteration, and then get fenced. The problem is that, in the race condition I described, Compactor 1 will have already written out the manifest with the wrong SRs as part of the current update, because it read and wrote the manifest after updating the compactor state. To prevent this condition, you would need to make sure to read the manifest before updating the compactor state in the current iteration. > Now, the only other possibility of the race condition of out of order update of .manifest file are the other race conditions mentioned in the protocol. However, synced state of .compactor file ensures that the .manifest will eventually sync with the state of .compactor file. Eventually consistency is not sufficient. The race condition we're discussing results in a manifest that contains the wrong SRs for some period of time. We cannot allow this as it could mess up reads that use that manifest. > Do you see any change in the protocol after adding the compactor_epoch in .manifest file? I'm saying we should put the compactor epoch in the .manifest file, and use it as the synchronization point for bumping the epoch. And we should write the epoch in the .compactor file as well. So when initializing the compactor should first bump the epoch in the .manifest file, and then rewrite the .compactor file with the bumped epoch. It then has exclusive access to both the .manifest and .compactor and can freely sync from the .compactor to the .manifest.
c
Yea.. that's kinda where I landed. The issue is, if we don't do that, then every compactor is going to have a different way to check the current compactions, add/request new ones, etc. Treating the .compactor file as an API allows us to unify these operations. WDYT?
r
that makes sense to me. The only drawback I can think of is that if a compactor impl needs some custom state it'll have to track that in some other file. I can't see a great way to model that with flatbuffers
but we can cross that bridge when we come to it. We need an api somewhere.
c
@Sujeet @Rohan let me attempt to clarify a bit. I think Rohan's sequence of event is missing some info that might help get us on the same page.
Copy code
Compactor 1 reads 00000.compactor(compactor_epoch=1, [SR0, SR1, SR2])
Compactor 2 updates 00000.compactor(compactor_epoch=2, [SR0, SR1, SR2])
Compactor 2 updates 00001.compactor(compactor_epoch=2, [SR2])
Compactor 2 updates 00023.manifest(compactor_epoch=2, [SR2])
Compactor 1 reads 00023.manifest ([SR2])
Compactor 1 writes 00024.manifest ([SR1, SR2]) // undoes Compactor 2's change when it should be fenced
In this sequence, compactor 1 reads the latest manifest after compactor 2 has updated it, then writes to 00024.manifest (the next manifest file). And compactor 1 can't know it was fenced yet because in the context of this discussion, we were saying the compactor epoch was only in the
.compactor
file. In this sequence, compactor 1 has overwritten compactor 2's update in the manifest for at least some period of time.
"meta" field of type bytes? 😅 We can also just punt on it and each impl can do whatever they want. since it's out of band.
@Rohan regarding the timeline, wouldn't c1 need to write to a .compactor file before it updates the manifest? that step seems to be missing in the sequence.
r
We also need to have some protocol for communicating compaction status back. Basically from the compactor's POV when it's done with a compaction it doesn't care about it anymore and can drop it from state. But other clients might be interested in the status. So we probably need to keep it around for some time (like k8s status fields in its objects)
c
Yea.. seems like we can let jobs hang around in the .compactor file for some max_age before cleaning them up
r
> @Rohan regarding the timeline, wouldn't c1 need to write to a .compactor file before it updates the manifest? that step seems to be missing in the sequence. Yeah you could just swap "Compactor 1 reads" in the first line with "Compactor 1 writes"
c
Copy code
Compactor 1 writes 00000.compactor(compactor_epoch=1, [SR0, SR1])
Compactor 2 reads 00000.compactor(compactor_epoch=1, [SR0, SR1]) // fencing
Compactor 2 writes 00001.compactor(compactor_epoch=2, [SR0, SR1]) // fencing
Compactor 2 writes 00002.compactor(compactor_epoch=2, [SR2]) // compacted out sr0+sr1
Compactor 2 writes 00023.manifest([SR2]) // compacted out sr0+sr1
Compactor 1 writes 00024.manifest ([SR0, SR1]) // undoes Compactor 2's change when it should be fenced
@Rohan Does this look accurate?
r
Copy code
Compactor 1 writes 00000.compactor(compactor_epoch=1, [SR0, SR1])
Compactor 2 reads 00000.compactor(compactor_epoch=1, [SR0, SR1]) // fencing
Compactor 2 writes 00001.compactor(compactor_epoch=2, [SR0, SR1]) // fencing
Compactor 2 writes 00002.compactor(compactor_epoch=2, [SR2]) // compacted out sr0+sr1
Compactor 2 writes 00023.manifest([SR2]) // compacted out sr0+sr1
Compactor 1 reads 00023.manifest([SR2]) <--- if it read this before it's .compactor write, then the write below would fail. Alternatively, if the compactor epoch was in the manifest, then it would observe it was bumped and fail
Compactor 1 writes 00024.manifest ([SR0, SR1]) // undoes Compactor 2's change when it should be fenced
c
Gotcha.
s
> The problem is that, in the race condition I described, Compactor 1 will have already written out the manifest with the wrong SRs as part of the current update, because it read and wrote the manifest after updating the compactor state. @Rohan Consider the protocol attached, I have just swapped step(1) and step(2) in the startup phase. Latest Manifest read during startup -> compaction_state update during startup -> Merged Manifest write during compaction completion. So, how will it read and write manifest after compaction state update based on the above protocol? Could you let me know the order of steps that need to be followed in the above protocol for this condition to take place?
r
The startup looks good to me, because it's reading the manifest before making the compactor state update. The "on compaction job complete" phase is underspecified. It's not clear when the read of the manifest happens. As long as it happens before the first step in the doc then it should be fine
Basically with your protocol as long as you're always reading both files first, and then updating the compactor state, and then the manifest state I think its fine.
s
> The "on compaction job complete" phase is underspecified. Let me update this part with more details and come back.
Copy code
text
#### 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) by reconciling it with the current compactor state.

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

    - Check if there is a change in the `compactor_epoch` between local and latest compactionState.

    - If yes, the compactor is stale and is fenced. Else reconcile the in-memory manifest state L0 SSTs and SRs with the latest manifest and the compactionState and go back to Step (3).
I have added more details for the compaction Job completion part. The protocol follows these steps in general read both files -> update compaction state -> update manifest. The above approach is a lazy sync approach wherein there could be period wherein manifest is not having the latest compactor state. However, it would eventually get synced during the next manifest write. The compaction state file Version Conflicts can be due to: 1. Concurrent CompactionJob trying to update the compaction state by writing .compactor file. 2. Old compactor process writing a .compactor file that is intended to be written by the active compactor during startup. The manifest file Version Conflicts can be due to: 1.Updating the manifest( checkpoints added by readers, flushing L0 SSTs, etc) 2. Old compactor process writing a .manifest file that is intended to be written by the active compactor during job completion. Does the above look good? Wdyt?
r
> I have added more details for the compaction Job completion part. The protocol follows these steps in general > read both files -> update compaction state -> update manifest. the excerpt you pasted above doesn't specify when the manifest is read > The above approach is a lazy sync approach wherein there could be period wherein manifest is not having the latest compactor state. However, it would eventually get synced during the next manifest write. I think this is fine, but what's the benefit? When a compaction finishes, why not just update the compactor state and then immediately the manifest to try to keep them in sync. > Concurrent CompactionJob trying to update the compaction state by writing .compactor file. This is a bit of an implementation detail, but I'd rather just have all updates go through the event handler for now so we don't have to deal with this case. > Old compactor process writing a .manifest file that is intended to be written by the active compactor during job completion I'd prefer we just force a write of the manifest during init so we don't have to worry about this case. > Does the above look good? Wdyt? I'd still strongly prefer an approach where we use the manifest to bump the compactor epoch, and store the epoch in the compactor state file as part of initialization as well. It just reduces the number of cases we have to think about because we know once the init is done an old compactor can never write either file
s
> the excerpt you pasted above doesn't specify when the manifest is read The manifest is read during the startup phase.
> I think this is fine, but what's the benefit? When a compaction finishes, why not just update the compactor state and then immediately the manifest to try to keep them in sync. Yes, this is what we are trying to do as mentioned in the protocol above. However, there are some corner cases( mentioned in the RFC) that could lead to above scenario.
> I'd still strongly prefer an approach where we use the manifest to bump the compactor epoch, and store the epoch in the compactor state file as part of initialization as well. It just reduces the number of cases we have to think about because we know once the init is done an old compactor can never write either file You mean to say we update the .manifest file during the startup as well with the
compactor_epoch
of the written .compactor file?
r
> The manifest is read during the startup phase. The compactor will read the manifest multiple times in its lifetime. For example, it needs to poll the manifest to discover new L0s. In your protocol, it's worth calling out that the version of the manifest being updated was read before the compactor state write. > However, there are some corner cases( mentioned in the RFC) that could lead to above scenario. gotcha. I'm assuming you mean in failure cases where we cannot update the manifest because we crash. In these cases we'd just try to update it when the compactor restarts. > You mean to say we update the .manifest file during the startup as well with the compactor_epoch of the written .compactor file? Kind of. What I'm saying is both .manifest and .compactor have a field called compactor_epoch. Initialization always starts by establishing a new compactor_epoch by reading the manifest, incrementing the compactor_epoch in the manifest, and then writing back the manifest. The compactor then writes this same compactor_epoch into the .compactor file. At that point it has exclusive access to both files and knows that it's impossible for any older compactor to update either file.
s
@Rohan I have updated the protocol with compactor_epoch in both .manifest and .compactor files.
> gotcha. I'm assuming you mean in failure cases where we cannot update the manifest because we crash. In these cases we'd just try to update it when the compactor restarts. Yes.
> The compactor will read the manifest multiple times in its lifetime. For example, it needs to poll the manifest to discover new L0s. In your protocol, it's worth calling out that the version of the manifest being updated was read before the compactor state write. Added the file version in the updated protocol and summarised protocol.
c
Looking at this now
In this section:
Copy code
#### 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)
> If latest .compactor compactor_epoch == current compactor epoch, goto Step (2) Does the compactor need to refresh the compactor state from the .compactor file? I'm assuming if 00007.compactor exists and the compactor didn't write it, then some out of band process did (e.g. a CLI requesting a new compaction). If so, shouldn't the compactor refresh to take that into account?
Copy code
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.
Nit: technically, a reader could also have created a checkpoint, which requires writing a new manifest. Just wanted to make a note of this, but I don't think it's important to this protocol.
s
> Does the compactor need to refresh the compactor state from the .compactor file? I'm assuming if 00007.compactor exists and the compactor didn't write it, then some out of band process did (e.g. a CLI requesting a new compaction). If so, shouldn't the compactor refresh to take that into account? Yes. It would be refreshing the state. Missed to update it like "reconcile the compactor state and go to step (2)"
c
It seems this protocol does not address resuming partially completed jobs on restart (AFAICT). I think that would happen during the initiation phase, but isn't mentioned. I think it's pretty straight forward, though. Not really related to the protocol, per-se.
Also, this protocol uses lazy recovery for syncing completed SRs to the manifest. I think that's totally fine. Just want to make sure I'm reading it correctly. I think you and Rohan agreed to this earlier, anyway.
s
> Nit: technically, a reader could also have created a checkpoint, which requires writing a new manifest. Just wanted to make a note of this, but I don't think it's important to this protocol. I didn't add the reader because I was thinking in terms writer_epoch and compactor_epoch changes. Will update it as well.
c
This protocol LGTM.
Let’s see if @Rohan or @almog have any feedback
Thanks for all the hard work 🙂
s
> It seems this protocol does not address resuming partially completed jobs on restart (AFAICT). I think that would happen during the initiation phase, but isn't mentioned. I think it's pretty straight forward, though. Not really related to the protocol, per-se. This protocol is to maintain the correctness between .compactor and .manifest file. Since this protocol is inline with the existing compaction scheduling and execution logic, resuming partially completed SRs would be a low level detail on how we handle it in the initiation phase of the next manifest poll. I think till the time the compactions are persisted, the existing logic should be able to handle the resuming flow.
c
Agreed
s
Cool. I will update the RFC once I get an approval from either @Rohan or @almog
> Also, this protocol uses lazy recovery for syncing completed SRs to the manifest. I think that's totally fine. Just want to make sure I'm reading it correctly. I think you and Rohan agreed to this earlier, anyway. Yes, we had an alignment on this in earlier discussion in this thread. In case of failures to write .manifest file, it would essentially end up being a lazy update to the manifest in the next iteration.
c
😀
r
Sorry for the delay! This LGTM to me. Couple minor quibbles, but nothing blocking: > #### On startup... > ... > 3. Compactor increments
compactor_epoch
and writes the dirty CompactionState to the next sequential .compactor position.(00006.compactor). Not super important to the protocol as long as we pick one of the two, but it might be easier to use the manifest as the place where we bump the epoch. Would make for a cleaner upgrade path since its consistent with the current behavior. Also what does dirty CompactionState mean here? The only "dirty" bit at this point should be the bumped epoch right? > #### On startup... > ... > - If latest .manifest compactor_epoch == current compactor epoch, die (fenced) Technically this should be impossible, so we should panic and log a critical error.
s
> Not super important to the protocol as long as we pick one of the two, but it might be easier to use the manifest as the place where we bump the epoch. The current order of events work well with my mental model 😃 . I think we could move updating .manifest compactor_epoch before .compactor compactor_epoch, however it could increase the possibility of failing the manifest write of the completed compactions in the Job completion phase due to fencing old compactor( though nothing critical, it would happen in the next iteration). The list of compactions in .compactor file would at any point of time represent the next state of SRs in SlateDB( as in we are copying SRs state to manifest). Hence, in my mental model, I think of the order: .compactor -> .manifest though the source of truth of a database view would be manifest. The manifest would always be lazily updated with the compaction changes. > > Also what does dirty CompactionState mean here? The only "dirty" bit at this point should be the bumped epoch right? Yes. > Technically this should be impossible, so we should panic and log a critical error. Yes. I will update while making the changes to RFC.
I was thinking of this case during the compactor startup.
Copy code
text
Compactor 1 reads latest .compactor file (00005.compactor, compactor_epoch = 1)
Compactor 1 reads latest .manifest file (00005.manifest, compactor_epoch = 1)
Compactor 1 writes .compactor file (00006.compactor, compactor_epoch = 2)
Compactor 2 reads latest .compactor file (00006.compactor, compactor_epoch = 2)
Compactor 2 reads latest .manifest file (00005.manifest, compactor_epoch = 1)
Compactor 2 writes .compactor file (00007.compactor, compactor_epoch = 3)
Compactor 2 writes .manifest file (00006.manifest, compactor_epoch = 3)
Compactor 1 writes .manifest file (00006.manifest, compactor_epoch = 2) (fenced)
Though everything is consistent, there could be gaps in the compactor_epoch in .manifest in the above approach. If I write .manifest before .compactor, the same gaps in compactor_epoch can be seen in .compactor file. I think as we have manifest as the source of truth for compactor_epoch, I will better bump the epoch in .manifest than in .compactor as Rohan suggested.
Copy code
text
Compactor 1 reads latest .manifest file (00005.manifest, compactor_epoch = 1)
Compactor 1 reads latest .compactor file (00005.compactor, compactor_epoch = 1)
Compactor 1 writes .manifest file (00006.manifest, compactor_epoch = 2)
Compactor 2 reads latest .manifest file (00006.manifest, compactor_epoch = 2)
Compactor 2 reads latest .compactor file (00005.compactor, compactor_epoch = 1)
Compactor 1 writes .manifest file (00007.manifest, compactor_epoch = 3)
Compactor 2 writes .compactor file (00006.compactor, compactor_epoch = 3)
Compactor 1 writes .compactor file (00006.compactor, compactor_epoch = 2) (fenced)
I was also thinking if I can remove step(4) in the startup phase after this change because the condition would always be false. Any thoughts?
a
This LGTM as well, clear enough to follow and work through the situations. re: "If latest .manifest compactor_epoch == current compactor epoch, die (fenced)" couldn't that happen if:
Copy code
1. C1 reads manifest.001 (c_epoch = 2) and compactor.001
2. C2 reads manifest.001 (c_epoch = 2) and compactor.001
3. C1 writes compactor.002 (c_epoch = 3)
4. C2 writes compactor.002 (c_epoch = 3) // !! writes compactor with same epoch should be fenced
That would be possible, right? (same thing could happen if we flip the order of writing to manifest first)
s
In the original protocol,
latest .manifest compactor_epoch == current compactor epoch, die (fenced)
Step(5), couldn't happen because it would get fenced during compactor_epoch update in .compactor file.
The example you mentioned is for compactor_epoch write in .compactor file where fencing could happen in Step(3)
https://discord.com/channels/1232385660460204122/1401210792778535055/1405134106303529032 @criccomini @Rohan @almog does anyine have any thoughts on this? Else I will go ahead and update the RFC with the original protocol.
c
Compactor first sounds good to me 🙂
s
Nice! I will update the RFC with the proposed protocol then.
#### CompactionState Structure The persistent state contains the complete view of all compaction activity:
Copy code
rust

pub(crate) struct CompactionJob {
    pub(crate) id: Uuid,
    pub(crate) destination: u32,
    pub(crate) ssts: Vec<SsTableHandle>,
    pub(crate) sorted_runs: Vec<SortedRun>,
    pub(crate) compaction_ts: i64,
    pub(crate) is_dest_last_run: bool,
    pub(crate) completed_input_sst_ids: Vec<Ulid>;
    pub(crate) completed_input_sr_ids: Vec<u32>;
    pub(crate) output_sr: SortedRun;
}

pub(crate) enum CompactionType {
    Internal,
    External,
}

pub struct Compaction {
    pub(crate) status: CompactionStatus,
    pub(crate) sources: Vec<SourceId>,
    pub(crate) destination: u32,
    pub(crate) compaction_id: Uuid,
    pub(crate) compaction_type: CompactionType,
    pub(crate) job_attempts: Vec<CompactionJob>;
}

pub(crate) CompactorState {
    manifest: DirtyManifest
    compaction_state: DirtyCompactionState
}

pub(crate) struct CompactionState {
    compactor_epoch: u64,
    // active_compactions queued, in-progress and completed
    compactions: HashMap<Uuid, Compaction>,
}

pub(crate) struct DirtyCompactionState {
    id: u64,
    compactor_epoch: u64,
    compaction_state: CompactionState,
}

pub(crate) struct StoredCompactionState {
    id: u64,
    compaction_state: CompactionState,
    compaction_state_store: Arc<CompactionStateStore>,
}

pub(crate) struct FenceableCompactionState {
    compaction_state: StoredCompactionState,
    local_epoch: u64,
    stored_epoch: fn(&CompactionState) -> u64,
}
Does the above in-memory structure look good?
c
Can we do: > compactions: BTreeMap, The reason is that ULID will then be sorted by create time.
(I know we do UUID for the in-memory compaction stuff now)
The state stuff looks like it mirrors the manifest stuff, which sounds good to me. I think it might be a good idea to provide a single implementation/abstraction for this pattern, though, rather than duplicating it. I think it'd be a good idea to refactor the manifest stuff to be abstract before doing the compactor state changes.
s
@Rohan am I good to merge the RFC, Chris has approved it.
Responded to the comments