cockroach icon indicating copy to clipboard operation
cockroach copied to clipboard

jobs: Use separate tables for cancellation and progress

Open bdarnell opened this issue 7 years ago • 8 comments

We sometimes see issues like #36601 in which a job runs away updating its progress and renders the jobs table unwriteable. This is especially problematic because canceling a job requires a write to the jobs table. We should refactor things so that cancellation is managed through a separate table from the one used for progress updates (and/or provide a cancellation signal that's not table-based at all, such as using gossip).

I understand that this wouldn't help with #36601 because schema changes are not currently cancelable, but since issues like this have come up a few times it seems like a good idea to make this more robust for the future.

CC @dt @rolandcrosby

Jira issue: CRDB-5571

bdarnell avatar Aug 12 '19 18:08 bdarnell

cc @spaskob - something to think about as part of the jobs refactor. As David mentioned today, we already put progress in a separate column family to minimize impact on the main KV for the job record, but that doesn't help us with cancellation.

[email protected]:59936/defaultdb> show create table system.jobs;
      table_name     |                              create_statement
+--------------------+----------------------------------------------------------------------------+
  system.public.jobs | CREATE TABLE jobs (
                     |     id INT8 NOT NULL DEFAULT unique_rowid(),
                     |     status STRING NOT NULL,
                     |     created TIMESTAMP NOT NULL DEFAULT now():::TIMESTAMP,
                     |     payload BYTES NOT NULL,
                     |     progress BYTES NULL,
                     |     CONSTRAINT "primary" PRIMARY KEY (id ASC),
                     |     INDEX jobs_status_created_idx (status ASC, created ASC),
                     |     FAMILY fam_0_id_status_created_payload (id, status, created, payload),
                     |     FAMILY progress (progress)
                     | )
(1 row)

rolandcrosby avatar Aug 12 '19 18:08 rolandcrosby

I like the idea of two tables: job definition and running status in one, job state/progress in another. I think the current jobs table could be the former and we already have a lot of the code setup to handle writing progress separately, but we'd need to be very disciplined about when we allow updating the definition. Even since splitting progress we still see some jobs that choose to update Details (definition) instead of Progress to document what is left to do.

@knz suggested a similar, even more radical shift, not just going to two tables, but having potentially many rows in the progress one, s.t. each update just appends a new row instead of editing one big row. I'm not sure if this would actually be that easy in practice as we'd need to aggregate all the rows to be useful, and we can/should be doing that aggregation of updates by the coordinator running the job anyway, but it is something to consider.

(@knz and I have also been talking about making new system tables less "special": some of the system tables need hardcoded protos and hardcoded IDs so that we can bootstrap SQL (e.g. we store table descriptors in a table, including the descriptor for the descriptor table). However many internal tables that happen to live in the "system" db do not need to be special -- we can just create them, with dynamically allocated IDs, like any normal table, since all traffic to them is just normal SQL traffic. Jobs falls in this category (as could things like comments or locations). Currently we treat all the system tables the same, and make brittle, hand-written descriptors for them and assign them fixed IDs from a finite pool, but making the ones that don't need it more like regular tables would a) make things easier and b) leave the special IDs for the tables that need them (we have 50 of which ~24 have been used).

dt avatar Aug 12 '19 19:08 dt

leave the special IDs for the tables that need them (we have 50 of which ~24 have been used

The only ones that really need reserved IDs are the "system config" tables (the first 10 of the 50 reserved IDs, of which 6 have been used). For the rest, I don't think there's any great reason for them to reserve IDs - it was just an attempt to minimize the differences between clusters that had gone through the migration path and those that were bootstrapped at a new version. (and "50 system tables ought to be enough for anybody")

So go ahead and use those last 26 table IDs; there's no reason to save them for a special purpose. We'll need to move to non-reserved ID assignments when we get that far (if we haven't already to simplify the migration code), but there's no need to rush that step.

bdarnell avatar Aug 12 '19 20:08 bdarnell

Hm, I think there is a chance that at some point in the future there is a new table where we want to be able to have deterministic IDs / use raw KVs to interact with it even if it isn't in the config range? If so, it'd be nice if there were still available constants left to assign, so I didn't see a reason to use the constants for tables that have exclusively normal SQL name resolution and interaction.

dt avatar Aug 12 '19 20:08 dt

Hm, I think there is a chance that at some point in the future there is a new table where we want to be able to have deterministic IDs / use raw KVs to interact with it even if it isn't in the config range?

I suppose there's always a chance. But even when/if that occurs, will we know that at table creation time? One of the few dependencies we have on the table ID constants is actually in the default zone config we've been (ineffectively: #39605) setting on the jobs table since 2.0. Prior to the introduction of that zone config the jobs table was one of those sql-only tables that might have been given a non-reserved ID.

bdarnell avatar Aug 12 '19 21:08 bdarnell

Fair enough. I care more about the use of normal table creation of the descriptor and such than I do about the dynamic vs static ID allocation. My preference for not exhausting the available static IDs when it seems inevitable we'll switch to dynamic at some point anyway is a weak one.

dt avatar Aug 12 '19 21:08 dt

fyi @spaskob

ajwerner avatar Oct 11 '19 21:10 ajwerner

Closing this issue, we're planning on rewriting/working the jobs system during the 22.2 release cycle using lessons learned from this and similar problems that have popped up over the last few years

amruss avatar Mar 30 '22 16:03 amruss