Skip to content

sql: reset indexedTypeFormatter on FmtCtx close#73095

Merged
craig[bot] merged 1 commit intocockroachdb:masterfrom
RichardJCai:reset_indexed_type_formatter
Nov 24, 2021
Merged

sql: reset indexedTypeFormatter on FmtCtx close#73095
craig[bot] merged 1 commit intocockroachdb:masterfrom
RichardJCai:reset_indexed_type_formatter

Conversation

@RichardJCai
Copy link
Copy Markdown
Contributor

@RichardJCai RichardJCai commented Nov 23, 2021

Previously indexedTypeFormatter wouldn't be reset when
FmtCtx.Close() was called causing the indexedTypeFormatter
function to potentially be reused.

Release note (enterprise change): Fix a bug where restore can sometimes map OIDs
to invalid types in certain circumstances containing UDTs.

@RichardJCai RichardJCai requested review from a team and otan November 23, 2021 23:24
@cockroach-teamcity
Copy link
Copy Markdown
Member

This change is Reviewable

Copy link
Copy Markdown
Contributor

@otan otan left a comment

Choose a reason for hiding this comment

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

LGTM, you could optionally add a test to format_test.go that creates a new FmtCtx, uses indexedTypeFormatter, Close the FmtCtx, refetch from the FmtCtx pool and assert that it does not apply the same thing again.

@otan
Copy link
Copy Markdown
Contributor

otan commented Nov 23, 2021

Wonder how often this bug is triggered - perhaps worth a release note Fix a bug where restore can sometimes map OIDs to invalid types in certain circumstances containing UDTs.

@RichardJCai
Copy link
Copy Markdown
Contributor Author

Wonder how often this bug is triggered - perhaps worth a release note Fix a bug where restore can sometimes map OIDs to invalid types in certain circumstances containing UDTs.

I think the bug is easily reproducible if you just sequence the order of tables and udts are restored.

@RichardJCai RichardJCai force-pushed the reset_indexed_type_formatter branch from 31624bb to cc55cfa Compare November 23, 2021 23:59
@RichardJCai
Copy link
Copy Markdown
Contributor Author

Fix a bug where restore can sometimes map OIDs to invalid types in certain circumstances containing UDTs.

Updated the release note, gonna leave out the test since we can't test the unexported field anyway.

Copy link
Copy Markdown
Member

@yuzefovich yuzefovich 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 @RichardJCai and @yuzefovich)


pkg/sql/sem/tree/format.go, line 633 at r1 (raw file):

	ctx.tableNameFormatter = nil
	ctx.placeholderFormat = nil
	ctx.indexedTypeFormatter = nil

Consider replacing all this nilling out with something like

ctx.Buffer.Reset()
*ctx = FmtCtx {
  Buffer: ctx.Buffer,
   // other fields that we want to keep when putting ctx into the pool
}
fmtCtxPool.Put(ctx)

@otan
Copy link
Copy Markdown
Contributor

otan commented Nov 24, 2021


pkg/sql/sem/tree/format.go, line 633 at r1 (raw file):

Previously, yuzefovich (Yahor Yuzefovich) wrote…

Consider replacing all this nilling out with something like

ctx.Buffer.Reset()
*ctx = FmtCtx {
  Buffer: ctx.Buffer,
   // other fields that we want to keep when putting ctx into the pool
}
fmtCtxPool.Put(ctx)

Ooh nice

Previously indexedTypeFormatter wouldn't be reset when
FmtCtx.Close() was called causing the indexedTypeFormatter
function to potentially be reused.

Release note (enterprise change): Fix a bug where restore can sometimes map OIDs
to invalid types in certain circumstances containing UDTs.
@RichardJCai RichardJCai force-pushed the reset_indexed_type_formatter branch from cc55cfa to d0e09f1 Compare November 24, 2021 01:13
@RichardJCai
Copy link
Copy Markdown
Contributor Author

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

pkg/sql/sem/tree/format.go, line 633 at r1 (raw file):

	ctx.tableNameFormatter = nil
	ctx.placeholderFormat = nil
	ctx.indexedTypeFormatter = nil

Consider replacing all this nilling out with something like

ctx.Buffer.Reset()
*ctx = FmtCtx {
  Buffer: ctx.Buffer,
   // other fields that we want to keep when putting ctx into the pool
}
fmtCtxPool.Put(ctx)

Good idea - did that instead, there's nothing else that we want to keep other than ctx.Buffer I believe.

Copy link
Copy Markdown
Collaborator

@rafiss rafiss 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 @otan, @rafiss, and @yuzefovich)


pkg/sql/sem/tree/format.go, line 633 at r1 (raw file):

Previously, RichardJCai (Richard Cai) wrote…

Good idea - did that instead, there's nothing else that we want to keep other than ctx.Buffer I believe.

i guess technically there's a difference, since it changes the reference and the old one has to be garbage collected, but maybe that's fine? unless the whole reason we were pooling the FmtCtx's was to avoid having to do GCs?

Copy link
Copy Markdown
Member

@yuzefovich yuzefovich left a comment

Choose a reason for hiding this comment

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

Reviewed 1 of 1 files at r2, all commit messages.
Reviewable status: :shipit: complete! 0 of 0 LGTMs obtained (waiting on @otan, @rafiss, and @RichardJCai)


pkg/sql/sem/tree/format.go, line 633 at r1 (raw file):

Previously, rafiss (Rafi Shamim) wrote…

i guess technically there's a difference, since it changes the reference and the old one has to be garbage collected, but maybe that's fine? unless the whole reason we were pooling the FmtCtx's was to avoid having to do GCs?

My guess is that the pooling was introduced in order to keep the contents of ctx.Buffer (bytes.Buffer.buf) for reuse, and that is retained with this approach.

I believe because bytes.Buffer is stored by value in FmtCtx, then there is no "old reference" to be GCed too.

@RichardJCai
Copy link
Copy Markdown
Contributor Author

TFTR!
bors r+

@craig
Copy link
Copy Markdown
Contributor

craig bot commented Nov 24, 2021

Build failed:

@RichardJCai
Copy link
Copy Markdown
Contributor Author

bors r+

@craig
Copy link
Copy Markdown
Contributor

craig bot commented Nov 24, 2021

Build succeeded:

@craig craig bot merged commit 0331536 into cockroachdb:master Nov 24, 2021
@blathers-crl
Copy link
Copy Markdown

blathers-crl bot commented Nov 24, 2021

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 d0e09f1 to blathers/backport-release-21.1-73095: 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 21.1.x failed. See errors above.


🦉 Hoot! I am a Blathers, a bot for CockroachDB. My owner is otan.

@rafiss
Copy link
Copy Markdown
Collaborator

rafiss commented Nov 25, 2021

not important enough to change, but for the future IMO this should should be Release note (bug fix) not (enterprise change)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

5 participants