storage: add online Pebble compaction mechanism
We don't ever want to have to ask users to run a manual compaction as part of routine maintenance, but while we're investigating compaction issues, it can be helpful to be able to manually trigger a compaction of a specific key range.
This could be achieved with an internal crdb_internal.compact_span which has overloads (byte,byte) (for raw key range) and (id) (table ID prefix).
In an ideal world, this becomes traceable and tells us something useful, but that's definitely wishful thinking.
Would crdb_internal.compact_span compact on the local node only? Another approach would be syntax similar to SCATTER: ALTER TABLE ... EXPERIMENTAL_COMPACT FROM ... TO .... For the usage I was envisioning, forcing compaction on a single node would be more appropriate.
Yeah, I internally went through some version of ALTER TABLE too, but what we really want initially (generally?) is a targeted compaction on a single node.
@petermattis is this still relevant in a Pebble world?
Yes, this is still relevant. Being able to force a compaction while a node is online can be a useful debugging/hot-fixing tool.
@sumeerbhola and @jbowens I don't think this is particularly difficult and a tool like this would have been handy in a number of issues. @sumeerbhola Can you add some thoughts about how to implement this? I can see the crdb_internal function that Tobias mentions in the original message. I think I'd avoid the ALTER TABLE syntax as that promotes it to be a first class citizen. We could also use an RPC, though I don't see any benefit to that vs the SQL function.
In standup we talked about running EstimateDiskUsage on whatever node services the query. Does that preclude using the function on any multi-tenant kv nodes? I could imagine on multi-tenant cluster different tenants may have very different write patterns, and one tenant's keyspace might get neglected by compaction heuristics.
That's a good point. Do the KV nodes in multi-tenant still run the SQL subsystem? I think they do for access to the "host" cluster, but would be good to verify that.
They do.
This could be achieved with an internal crdb_internal.compact_span which has overloads (byte,byte) (for raw key range) and (id) (table ID prefix).
@tbg, some questions:
- Plumbing: The builtin has access to a
tree.EvalContextwhich has akv.DB. But does not have aserver.Nodeorkvserver.Store. I need to get to a store, when this is a KV node. Is there an existing way to get to one? I could add an interface field tokv.DB, which would be non-nil only for a KV node, that would allow me to do compactions. This field would be set inNewServer, where bothserver.Nodeandkv.DBare constructed. - Interface:
- Should we also require a StoreID param? Or make it optional, for the common case of a single store on the node?
- If we need to compact a tenant table we need the tenant codec. Looks like a
TenantIDis sufficient to construct that. So an optional TenantID param? This leads to the next question. - How do we expect someone to quickly produce a
(byte,byte)raw key range? I didn't spot anything when glancing at the cli. I can imagine someone being able to produce a pretty printed form like/Local/RangeID/1000001/r/RangeAppliedStateor/Local/Range/Tenant/5/Table/42. But our functionality to parse this seems incomplete based on readingprinter.go-- some parts of our key space have aKeyParserFuncbut some are marked asparseUnsupported. - If we expect folks to notice a problem range via the admin UI and want to compact it, we could expect them to provide the
RangeID. I could look that up in theStore.replicasmap and get to aRangeDescriptorand use theStartKey,EndKeyRKeys. Should we make this the only way to specify the compaction interval?
The question is how polished you want to make this. The easiest thing (also the most idiomatic, imo) is adding an RPC endpoint to the KV layer, similar to how it is done for CollectChecksum:
https://github.com/cockroachdb/cockroach/blob/477f02ac0b999b9fa62e95b14309e33ba32598cb/pkg/kv/kvserver/api.proto#L28-L39
This is then invoked like this (and you should have a node dialer in sql somewhere):
https://github.com/cockroachdb/cockroach/blob/32307340f401a2449d2d98f6f89a4efff6f20172/pkg/kv/kvserver/replica_consistency.go#L325-L333
and the server side is here:
https://github.com/cockroachdb/cockroach/blob/da03c9ff255699d2c4c5e0324fd344acdd7cdc66/pkg/kv/kvserver/stores_server.go#L50-L65
I would say that at the store level impl, the RPC should take in a key range, so that it neatly translates to a compaction. At the SQL level, you can do what you consider most ergonomic with overloads. For example:
- (int) --> rangeID on all stores with a replica for that range
- (bytes, bytes) --> key span on all stores
- (int, bytes, bytes) --> key span on store X
etc. For tenants, we can introduce a builtin crdb_internal.tenant_prefix(tenID) which returns their start_key and a corresponding _suffix, which then neatly allows using the (bytes,bytes) overload.
I think this is internally all powerable by crdb_internal.ranges_no_leases and the other crdb_internal tables that let you translate between storeIDs and replicas and key ranges as appropriate, i.e. every overload eventually delegates to (int,bytes,bytes) after having done some table yoga.
Of course there's no point pushing this tool to perfection as we don't hope to use it all that much.
Also, tagging @knz who on behalf of the server team is forming opinions on how to set things like these up. I believe he would recommend something similar, but it can't hurt to let him know this is happening.
:white_check_mark:
you should have a node dialer in sql somewhere
I assume you meant somewhere starting from tree.EvalContext. I see a nodedialer.Dialer in DistSender which implements Sender which is in CrossRangeTxnWrapperSender which is in kv.DB -- this seems hacky and fragile. Do you have a better suggestion?
For tenants, we can introduce a builtin crdb_internal.tenant_prefix(tenID) which returns their start_key and a corresponding _suffix, which then neatly allows using the (bytes,bytes) overload.
Why does one builtin need to call another builtin? At this point we can access all our existing Go code. Or maybe you are suggesting that the user would do this before calling crdb_internal.compact_span(byte, byte)?
I am leaning towards just doing rangeID to start with, since it is not clear to me how someone with limited time when stabilizing a system will use the other alternatives. We can extend later.
Maybe it's not plumbed to where you need it. Can you ask someone from sql? I'm out until Tuesday. Note that the dialer will not be available on tenants but that's fine. Alfonso is a good choice here but he's also in Europe.
Re: the calling each other, I was merely suggesting that they could share code. Starting with just range id is fine, but I think the store RPC should take keys.
On Fri, Dec 4, 2020, 17:54 sumeerbhola [email protected] wrote:
you should have a node dialer in sql somewhere
I assume you meant somewhere starting from tree.EvalContext. I see a nodedialer.Dialer in DistSender which implements Sender which is in CrossRangeTxnWrapperSender which is in kv.DB -- this seems hacky and fragile. Do you have a better suggestion?
For tenants, we can introduce a builtin crdb_internal.tenant_prefix(tenID) which returns their start_key and a corresponding _suffix, which then neatly allows using the (bytes,bytes) overload.
Why does one builtin need to call another builtin? At this point we can access all our existing Go code. Or maybe you are suggesting that the user would do this before calling crdb_internal.compact_span(byte, byte)? I am leaning towards just doing rangeID to start with, since it is not clear to me how someone with limited time when stabilizing a system will use the other alternatives. We can extend later.
— You are receiving this because you were mentioned. Reply to this email directly, view it on GitHub https://github.com/cockroachdb/cockroach/issues/26068#issuecomment-738893123, or unsubscribe https://github.com/notifications/unsubscribe-auth/ABGXPZHT3CYGJWDPKOPFPGTSTEH5NANCNFSM4FBUKTNQ .
I am leaning towards just doing rangeID to start with, since it is not clear to me how someone with limited time when stabilizing a system will use the other alternatives.
As someone who often has limited time stabilizing a system (HAHA), I think this is reasonable. Perhaps it should also be easy to compact all ranges? Though I guess you could call crdb_internal.ranges or similar in subquery to achieve this. So no syntactic sugar needed? OTOH, would that do the compactions in parallel possibly causing bad effects on cluster health? It seems like compact whole keyspace could be common enough need that it should be smooth to execute on.
Thanks for building this mechanism!!