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

sizegen: do not ignore type aliases #17556

Merged
merged 1 commit into from
Jan 16, 2025
Merged

sizegen: do not ignore type aliases #17556

merged 1 commit into from
Jan 16, 2025

Conversation

vmg
Copy link
Collaborator

@vmg vmg commented Jan 16, 2025

Description

Important fix for memory calculations of in-memory data structures. Was causing OOMs in production.

In Go 1.23 a new type alias node was introduced for reflection, see golang/go#63223. This initially caused the sizegen generation to crash, which was fixed by @systay in #16650 but that fix was not complete. It still creates a ton of warnings since that change that were never fixed:

2025/01/16 15:51:18 unhandled type: *types.Alias
2025/01/16 15:51:18 unhandled type: *types.Alias
2025/01/16 15:51:18 unhandled type: *types.Alias
2025/01/16 15:51:18 unhandled type: *types.Alias
2025/01/16 15:51:18 unhandled type: *types.Alias
2025/01/16 15:51:18 unhandled type: *types.Alias
2025/01/16 15:51:18 unhandled type: *types.Alias
2025/01/16 15:51:18 unhandled type: *types.Alias
2025/01/16 15:51:18 unhandled type: *types.Alias
2025/01/16 15:51:18 unhandled type: *types.Alias
2025/01/16 15:51:18 unhandled type: *types.Alias
2025/01/16 15:51:18 unhandled type: *types.Alias
2025/01/16 15:51:18 unhandled type: *types.Alias
2025/01/16 15:51:18 unhandled type: *types.Alias
2025/01/16 15:51:18 unhandled type: *types.Alias
2025/01/16 15:51:18 unhandled type: *types.Alias
2025/01/16 15:51:18 unhandled type: *types.Alias
2025/01/16 15:51:18 unhandled type: *types.Alias
2025/01/16 15:51:18 unhandled type: *types.Alias
2025/01/16 15:51:18 unhandled type: *types.Alias
2025/01/16 15:51:18 WARNING: size of external type regexp.Regexp cannot be fully calculated
2025/01/16 15:51:18 unhandled type: *types.Alias
2025/01/16 15:51:18 unhandled type: *types.Alias
2025/01/16 15:51:18 WARNING: size of external type math/big.Int cannot be fully calculated
2025/01/16 15:51:18 unhandled type: *types.Alias
2025/01/16 15:51:18 unhandled type: *types.Alias
2025/01/16 15:51:18 unhandled type: *types.Alias
2025/01/16 15:51:18 unhandled type: *types.Alias
2025/01/16 15:51:18 unhandled type: *types.Alias
2025/01/16 15:51:18 unhandled type: *types.Alias
2025/01/16 15:51:18 unhandled type: *types.Alias
2025/01/16 15:51:18 WARNING: size of external type regexp.Regexp cannot be fully calculated
2025/01/16 15:51:18 unhandled type: *types.Alias
2025/01/16 15:51:18 unhandled type: *types.Alias
2025/01/16 15:51:18 unhandled type: *types.Alias
2025/01/16 15:51:18 unhandled type: *types.Alias
2025/01/16 15:51:18 unhandled type: *types.Alias
2025/01/16 15:51:18 unhandled type: *types.Alias
2025/01/16 15:51:18 unhandled type: *types.Alias
2025/01/16 15:51:18 unhandled type: *types.Alias
2025/01/16 15:51:18 unhandled type: *types.Alias
2025/01/16 15:51:18 unhandled type: *types.Alias
2025/01/16 15:51:18 saved '/Users/dirkjan/code/vitessio/vitess/go/vt/key/cached_size.go'
2025/01/16 15:51:18 saved '/Users/dirkjan/code/vitessio/vitess/go/pools/smartconnpool/cached_size.go'
2025/01/16 15:51:18 saved '/Users/dirkjan/code/vitessio/vitess/go/vt/vtgate/engine/cached_size.go'
2025/01/16 15:51:18 saved '/Users/dirkjan/code/vitessio/vitess/go/mysql/collations/colldata/cached_size.go'
2025/01/16 15:51:18 saved '/Users/dirkjan/code/vitessio/vitess/go/vt/tableacl/cached_size.go'
2025/01/16 15:51:18 saved '/Users/dirkjan/code/vitessio/vitess/go/vt/schema/cached_size.go'
2025/01/16 15:51:18 saved '/Users/dirkjan/code/vitessio/vitess/go/sqltypes/cached_size.go'
2025/01/16 15:51:19 saved '/Users/dirkjan/code/vitessio/vitess/go/vt/vtgate/evalengine/cached_size.go'
2025/01/16 15:51:19 saved '/Users/dirkjan/code/vitessio/vitess/go/vt/vttablet/tabletserver/cached_size.go'
2025/01/16 15:51:19 saved '/Users/dirkjan/code/vitessio/vitess/go/vt/proto/query/cached_size.go'
2025/01/16 15:51:19 saved '/Users/dirkjan/code/vitessio/vitess/go/vt/srvtopo/cached_size.go'
2025/01/16 15:51:19 saved '/Users/dirkjan/code/vitessio/vitess/go/vt/sqlparser/cached_size.go'
2025/01/16 15:51:19 saved '/Users/dirkjan/code/vitessio/vitess/go/vt/vtenv/cached_size.go'
2025/01/16 15:51:19 saved '/Users/dirkjan/code/vitessio/vitess/go/vt/proto/topodata/cached_size.go'
2025/01/16 15:51:19 saved '/Users/dirkjan/code/vitessio/vitess/go/mysql/collations/cached_size.go'
2025/01/16 15:51:19 saved '/Users/dirkjan/code/vitessio/vitess/go/mysql/json/cached_size.go'
2025/01/16 15:51:19 saved '/Users/dirkjan/code/vitessio/vitess/go/mysql/decimal/cached_size.go'
2025/01/16 15:51:19 saved '/Users/dirkjan/code/vitessio/vitess/go/vt/vtgate/vindexes/cached_size.go'
2025/01/16 15:51:19 saved '/Users/dirkjan/code/vitessio/vitess/go/vt/vttablet/tabletserver/rules/cached_size.go'
2025/01/16 15:51:19 saved '/Users/dirkjan/code/vitessio/vitess/go/vt/vttablet/tabletserver/planbuilder/cached_size.go'
2025/01/16 15:51:19 saved '/Users/dirkjan/code/vitessio/vitess/go/vt/vttablet/tabletserver/schema/cached_size.go'
2025/01/16 15:51:19 saved '/Users/dirkjan/code/vitessio/vitess/go/vt/proto/vttime/cached_size.go'

This might have looked innocent, but it certainly is not. The problem we found here specifically starts showing up in the vttablet consolidation logic. The problem is that we need these sizes to properly compute the cache. We have a type alias for Row to []Value for the actual row storage which is now ignored.

This means that the cache in use size for the consolidator did not actually count the row data! This is basically the main part of what it needs to measure. This in turns leads to excessive memory usage by the consolidator.

This also applies to the normal query consolidator, to the query cache, and any other piece of code that was depending on CachedSize to be accurate in order to limit vttablet memory usage. We're hoping this will cut down on the amount of OOMs we see in production.

cc @dbussink

Related Issue(s)

Checklist

  • "Backport to:" labels have been added if this change should be back-ported to release branches
  • If this change is to be back-ported to previous releases, a justification is included in the PR description
  • Tests were added or are not required
  • Did the new or modified tests pass consistently locally and on CI?
  • Documentation was added or is not required

Deployment Notes

Copy link
Contributor

vitess-bot bot commented Jan 16, 2025

Review Checklist

Hello reviewers! 👋 Please follow this checklist when reviewing this Pull Request.

General

  • Ensure that the Pull Request has a descriptive title.
  • Ensure there is a link to an issue (except for internal cleanup and flaky test fixes), new features should have an RFC that documents use cases and test cases.

Tests

  • Bug fixes should have at least one unit or end-to-end test, enhancement and new features should have a sufficient number of tests.

Documentation

  • Apply the release notes (needs details) label if users need to know about this change.
  • New features should be documented.
  • There should be some code comments as to why things are implemented the way they are.
  • There should be a comment at the top of each new or modified test to explain what the test does.

New flags

  • Is this flag really necessary?
  • Flag names must be clear and intuitive, use dashes (-), and have a clear help text.

If a workflow is added or modified:

  • Each item in Jobs should be named in order to mark it as required.
  • If the workflow needs to be marked as required, the maintainer team must be notified.

Backward compatibility

  • Protobuf changes should be wire-compatible.
  • Changes to _vt tables and RPCs need to be backward compatible.
  • RPC changes should be compatible with vitess-operator
  • If a flag is removed, then it should also be removed from vitess-operator and arewefastyet, if used there.
  • vtctl command output order should be stable and awk-able.

@vitess-bot vitess-bot bot added NeedsBackportReason If backport labels have been applied to a PR, a justification is required NeedsDescriptionUpdate The description is not clear or comprehensive enough, and needs work NeedsIssue A linked issue is missing for this Pull Request NeedsWebsiteDocsUpdate What it says labels Jan 16, 2025
@dbussink dbussink added Type: Bug Component: Query Serving Backport to: release-21.0 Needs to be backport to release-21.0 and removed NeedsDescriptionUpdate The description is not clear or comprehensive enough, and needs work NeedsWebsiteDocsUpdate What it says NeedsIssue A linked issue is missing for this Pull Request NeedsBackportReason If backport labels have been applied to a PR, a justification is required labels Jan 16, 2025
@@ -163,6 +163,8 @@ func (sizegen *sizegen) generateTyp(tt types.Type) {
sizegen.generateKnownType(tt)
case *types.Alias:
sizegen.generateTyp(types.Unalias(tt))
default:
panic(fmt.Sprintf("unhandled type: %v (%T)", tt, tt))
Copy link
Contributor

Choose a reason for hiding this comment

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

We now make sure we panic here on unexpected values as well.

default:
log.Printf("unhandled type: %T", node)
return nil, 0
panic(fmt.Sprintf("unhandled type: %v (%T)", node, node))
Copy link
Contributor

Choose a reason for hiding this comment

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

We should not just print a warning here, but actively panic. This is so that if we have other new types in the future, we don't ignore this again but actively investigate.

Comment on lines +40 to +47
for _, elem := range cached.Rows {
{
size += hack.RuntimeAllocSize(int64(cap(elem)) * int64(32))
for _, elem := range elem {
size += elem.CachedSize(false)
}
}
}
Copy link
Collaborator Author

Choose a reason for hiding this comment

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

This was the big missing memory calculation for the size of in-memory query results.

@github-actions github-actions bot added this to the v22.0.0 milestone Jan 16, 2025
Copy link
Member

@GuptaManan100 GuptaManan100 left a comment

Choose a reason for hiding this comment

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

LGTM! Excellent find!

@dbussink dbussink merged commit 71ccd6d into main Jan 16, 2025
212 checks passed
@dbussink dbussink deleted the vmg/sizegen-fix branch January 16, 2025 15:46
vitess-bot pushed a commit that referenced this pull request Jan 16, 2025
Copy link

codecov bot commented Jan 16, 2025

Codecov Report

Attention: Patch coverage is 0% with 5 lines in your changes missing coverage. Please review.

Project coverage is 67.72%. Comparing base (b103492) to head (755e0e1).
Report is 1 commits behind head on main.

Files with missing lines Patch % Lines
go/tools/sizegen/sizegen.go 0.00% 5 Missing ⚠️
Additional details and impacted files
@@            Coverage Diff             @@
##             main   #17556      +/-   ##
==========================================
+ Coverage   67.68%   67.72%   +0.03%     
==========================================
  Files        1584     1584              
  Lines      254718   254721       +3     
==========================================
+ Hits       172414   172508      +94     
+ Misses      82304    82213      -91     

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

@deepthi
Copy link
Member

deepthi commented Jan 16, 2025

Isn't there a unit test that could have caught this? We should add one.

@dbussink
Copy link
Contributor

Isn't there a unit test that could have caught this? We should add one.

That's why we changed this to panic instead of only printing a warning. That way we'd hard crash / fail instead of it being possible to ignore the warning.

dbussink pushed a commit that referenced this pull request Jan 16, 2025
Signed-off-by: Vicent Marti <[email protected]>
Co-authored-by: vitess-bot[bot] <108069721+vitess-bot[bot]@users.noreply.github.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Bug Report: Sizegen is broken for cached results on v21 and later
5 participants