Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

sql: create new slice when removing backreference items #97631

Conversation

chengxiong-ruan
Copy link
Contributor

@chengxiong-ruan chengxiong-ruan commented Feb 24, 2023

Fixes: #97546
Fixes: #96368

Previously we reused the underlying slots of the DependedOnBy slice when removing items from it. This is problematic because we normally have to iterate the same slice to figure out what to drop. Iterating on a slice which is rewritten can be problematic unless we always know how to smartly move the cursor backward. This commit changes it to just create a new slice to avoid the hazzard.

Release note: None.

@chengxiong-ruan chengxiong-ruan requested a review from a team February 24, 2023 16:43
@cockroach-teamcity
Copy link
Member

This change is Reviewable

@chengxiong-ruan chengxiong-ruan force-pushed the create-new-slice-when-removing-dependencies branch from 21cf090 to fe645a5 Compare February 24, 2023 17:13
Copy link
Collaborator

@fqazi fqazi left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Reviewed all commit messages.
Reviewable status: :shipit: complete! 0 of 0 LGTMs obtained (waiting on @chengxiong-ruan)


pkg/sql/alter_table.go line 1615 at r1 (raw file):

			return nil, err
		}
		if depDesc.Dropped() {

Is there another bug we are fixing here, we should maybe have a commit message and release not if it was public.


pkg/sql/drop_table.go line 650 at r1 (raw file):

	refs []descpb.TableDescriptor_Reference, id descpb.ID,
) []descpb.TableDescriptor_Reference {
	var updatedRefs []descpb.TableDescriptor_Reference

Pre-allocate based on the size of the old refs, incase nothing gets removed. That way we don't keep on growing.

Copy link
Contributor

@ajwerner ajwerner left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Reviewable status: :shipit: complete! 0 of 0 LGTMs obtained (waiting on @chengxiong-ruan and @fqazi)


pkg/sql/drop_table.go line 650 at r1 (raw file):

Previously, fqazi (Faizan Qazi) wrote…

Pre-allocate based on the size of the old refs, incase nothing gets removed. That way we don't keep on growing.

This can be N^2 now in the worst case, right? Some other options are to collect the ids to remove or to clone the slice before iterating. This is yet another reason why the legacy schema changer is bad. I imagine the recursion makes this all tricky. What is the loop we were messing up?

@chengxiong-ruan
Copy link
Contributor Author

pkg/sql/drop_table.go line 650 at r1 (raw file):

Previously, ajwerner wrote…

This can be N^2 now in the worst case, right? Some other options are to collect the ids to remove or to clone the slice before iterating. This is yet another reason why the legacy schema changer is bad. I imagine the recursion makes this all tricky. What is the loop we were messing up?

Yeah, N^2 can be bad if the dep graph is large.
The loops under concern here is DROP INDEX CASCADE and DROP COLUMN CASCADE
But yeah, I think collecting ids to drop is a good idea here.

@chengxiong-ruan chengxiong-ruan force-pushed the create-new-slice-when-removing-dependencies branch from fe645a5 to b6680ab Compare February 24, 2023 18:59
Copy link
Contributor Author

@chengxiong-ruan chengxiong-ruan left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Reviewable status: :shipit: complete! 0 of 0 LGTMs obtained (waiting on @ajwerner and @fqazi)


pkg/sql/alter_table.go line 1615 at r1 (raw file):

Previously, fqazi (Faizan Qazi) wrote…

Is there another bug we are fixing here, we should maybe have a commit message and release not if it was public.

Yeah, another bug was accidentally fixed here...I added both bugs to commit message and added release note.


pkg/sql/drop_table.go line 650 at r1 (raw file):

Previously, chengxiong-ruan (Chengxiong Ruan) wrote…

Yeah, N^2 can be bad if the dep graph is large.
The loops under concern here is DROP INDEX CASCADE and DROP COLUMN CASCADE
But yeah, I think collecting ids to drop is a good idea here.

Done

Copy link
Contributor

@ajwerner ajwerner left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I like this much more

@chengxiong-ruan chengxiong-ruan force-pushed the create-new-slice-when-removing-dependencies branch from b6680ab to b2d689e Compare February 27, 2023 17:37
Copy link
Collaborator

@fqazi fqazi left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This looks much nicer :lgtm:

Reviewed 1 of 3 files at r2, 4 of 4 files at r3, all commit messages.
Reviewable status: :shipit: complete! 1 of 0 LGTMs obtained (waiting on @chengxiong-ruan)

@chengxiong-ruan chengxiong-ruan force-pushed the create-new-slice-when-removing-dependencies branch from b2d689e to bf5d0f3 Compare February 28, 2023 02:31
@chengxiong-ruan chengxiong-ruan force-pushed the create-new-slice-when-removing-dependencies branch from bf5d0f3 to 181008f Compare February 28, 2023 03:44
Copy link
Contributor

@ajwerner ajwerner left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The version gate feels like it should have been a separate PR.

Reviewed 1 of 3 files at r2, 1 of 4 files at r3.
Reviewable status: :shipit: complete! 0 of 0 LGTMs obtained (and 1 stale) (waiting on @chengxiong-ruan and @fqazi)


pkg/sql/schemachanger/scdecomp/decomp.go line 123 at r4 (raw file):

			panic(
				scerrors.NotImplementedErrorf(
					nil,

nit: can you name the nil with a comment?

@chengxiong-ruan
Copy link
Contributor Author

TFTRs!
bors r+

@chengxiong-ruan
Copy link
Contributor Author

bors r-

@craig
Copy link
Contributor

craig bot commented Feb 28, 2023

Canceled.

@chengxiong-ruan
Copy link
Contributor Author

The version gate feels like it should have been a separate PR.
Actually it's needed in this PR... the mixed-22.2-23.1 logictest nicely failed the tests I added because it can't do drop column which cascade drop the function.

@chengxiong-ruan
Copy link
Contributor Author

The version gate feels like it should have been a separate PR.

Sorry.. terrible formatting....
Actually it's needed in this PR... the mixed-22.2-23.1 logictest nicely failed the tests I added because it can't do drop column which cascade drop the function.

Fixes: cockroachdb#97546
Fixes: cockroachdb#96368

We are reusing the underlying slots of the `DependedOnBy`
slice when removing items from it. This is problematic because
we normally have to iterate the same slice to figure out what
to drop. Iterating on a slice which is rewritten can be problematic
unless we always know how to smartly move the cursor backward.
This commit remove such kind of hazard by collecting descriptor
IDs to drop before actually dropping everything.

Releaste note (sql change): This commit fixes two bugs prevent
users from doing `DROP COLUMN CASCADE` and `DROP INDEX CASCADE`
when dependecies on the Table from Views and Functions get complex.
They all happened When a table is referenced multiple times in
more than one Views and Functions.

Release note: None.
@chengxiong-ruan chengxiong-ruan force-pushed the create-new-slice-when-removing-dependencies branch from 181008f to 2e05878 Compare February 28, 2023 04:44
@chengxiong-ruan
Copy link
Contributor Author

pkg/sql/schemachanger/scdecomp/decomp.go line 123 at r4 (raw file):

Previously, ajwerner wrote…

nit: can you name the nil with a comment?

done.

@chengxiong-ruan
Copy link
Contributor Author

TFTRs!
bors r+

@craig
Copy link
Contributor

craig bot commented Feb 28, 2023

Build succeeded:

@craig craig bot merged commit 46b1afa into cockroachdb:master Feb 28, 2023
@blathers-crl
Copy link

blathers-crl bot commented Feb 28, 2023

Encountered an error creating backports. Some common things that can go wrong:

  1. The backport branch might have already existed.
  2. There was a merge conflict.
  3. The backport branch contained merge commits.

You might need to create your backport manually using the backport tool.


error creating merge commit from 2e05878 to blathers/backport-release-22.2-97631: POST https://api.github.com/repos/cockroachdb/cockroach/merges: 409 Merge conflict []

you may need to manually resolve merge conflicts with the backport tool.

Backport to branch 22.2.x failed. See errors above.


🦉 Hoot! I am a Blathers, a bot for CockroachDB. My owner is dev-inf.

chengxiong-ruan added a commit to chengxiong-ruan/cockroach that referenced this pull request Feb 28, 2023
In cockroachdb#97631 we refactor the method to drorp index and column
cascade dependents. But we didn't return a correct droppedViews,
instead, we returned a nil. This wasn't caught by the `event_log`
logic test because we skip `local-legacy-schema-changer` test config
in v23.1. Luckily,this was caught when backporting to v22.2 because
there was a fallback.

Epic: None

Release note: None
chengxiong-ruan added a commit to chengxiong-ruan/cockroach that referenced this pull request Feb 28, 2023
In cockroachdb#97631 we refactor the method to drorp index and column
cascade dependents. But we didn't return a correct droppedViews,
instead, we returned a nil. This wasn't caught by the `event_log`
logic test because we skip `local-legacy-schema-changer` test config
in v23.1. Luckily,this was caught when backporting to v22.2 because
there was a fallback.

Epic: None

Release note: None
chengxiong-ruan added a commit to chengxiong-ruan/cockroach that referenced this pull request Feb 28, 2023
In cockroachdb#97631 we refactor the method to drorp index and column
cascade dependents. But we didn't return a correct droppedViews,
instead, we returned a nil. This wasn't caught by the `event_log`
logic test because we skip `local-legacy-schema-changer` test config
in v23.1. Luckily,this was caught when backporting to v22.2 because
there was a fallback.

Epic: None

Release note: None
craig bot pushed a commit that referenced this pull request Feb 28, 2023
96382: jobs: add VIEWJOB role option r=HonoreDB a=jayshrivastava

#### jobs: allow CONTROLJOB users to view all jobs 
This is a preliminary change before adding the `VIEWJOB` role
option. The purpose of the `VIEWJOB` role option
will be to allow TSEs to view jobs in admin clusters without
modifying them. This requires access to view all jobs, including ones
owned by admins.

Adding `VIEWJOB` will conflict with the present `CONTROLJOB` implementation.
It will be stronger than `CONTROLJOB` because it lets one view admin jobs when
`CONTROLJOB` doesn't, yet it will be weaker because it only allows viewing
jobs and not pause/cancel/resume-ing them.

This change is introduced to make `CONTROLJOB` at least as strong
as `VIEWJOB`: it now allows for all jobs to be viewed. In other words,
`VIEWJOB` lets you do a subset of things `CONTROLJOB` lets you do.

Also, the reason that `CONTROLJOB` prevents access to admin-owned
jobs is not well defined. This is tracked in #96432.

Release note (general change):
Previously, users with the `CONTROLJOB` role option could not view
owned by admins (ex. via `SHOW JOBS`). Now, they can.

Epic: none

---

#### jobs: add VIEWJOB role option
Release note (general change):
Users can now be granted the `VIEWJOB` role option
to be able to view all jobs (ex. via `SHOW JOBS`).
This role can be revoked using `NOVIEWJOB`.

Epic: none

97746: sql,schemachanger: ADD PRIMARY KEY NOT VALID returns an error r=Xiang-Gu a=Xiang-Gu

Fixes #96729

Release note (sql change): When we add an unvalidated PK constraint to a table, assuming its PK was on the implicit "rowid" column, will return an error. This is compatible to what PG will do.

97802: sql: return droppedView instead of nil r=chengxiong-ruan a=chengxiong-ruan

In #97631 we refactor the method to drorp index and column cascade dependents. But we didn't return a correct droppedViews for event logging, instead, we returned a nil. This wasn't caught by the `event_log` logic test because we `local-legacy-schema-changer` test config. Though this was caught when backporting to v22.2 because there was a fallback.

Epic: None

Release note: None

Co-authored-by: Jayant Shrivastava <[email protected]>
Co-authored-by: Xiang Gu <[email protected]>
Co-authored-by: Chengxiong Ruan <[email protected]>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
4 participants