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

log,server: avoid global variables to log/trace server IDs #73306

Merged
merged 1 commit into from
Dec 14, 2021

Conversation

knz
Copy link
Contributor

@knz knz commented Nov 30, 2021

First commit from #73678.
Fixes #58938.
(still incomplete - needs all the other PRs connected to that issue)

Prior to this change, the server identifiers (cluster ID, node ID etc)
were stored in global variables in the log package.
This was problematic when a single process contains multiple servers,
e.g. in tests, demo and multi-tenant CockroachDB.

This change switches the mechanism to use identifiers stored in the
go context. The disadvantage is that the server IDs are not any
more logged at the beginning of each log file (since a given log file
could report data from multiple servers).

Release note (cli change): The server identifiers (cluster ID, node
ID, tenant ID, instance ID) are not any more duplicated at the start
of every new log file (during log file rotations). They are now only
logged when known during server start-up.
(The copy of the identifiers is still included in per-event envelopes
for the various json output logging formats.)

@cockroach-teamcity
Copy link
Member

This change is Reviewable

@knz knz force-pushed the 20211130-server-ids branch from 3b5fc23 to 0d71bd9 Compare December 8, 2021 13:34
@knz knz force-pushed the 20211130-server-ids branch from 0d71bd9 to 57099da Compare December 10, 2021 12:29
@knz knz force-pushed the 20211130-server-ids branch from 57099da to 8f8e2e8 Compare December 10, 2021 14:06
@knz knz requested review from stevendanna and a team December 10, 2021 14:07
@knz knz marked this pull request as ready for review December 10, 2021 14:07
@knz knz requested a review from a team December 10, 2021 14:07
@knz knz requested review from a team as code owners December 10, 2021 14:07
@knz knz requested a review from a team December 10, 2021 14:07
@knz knz requested review from a team as code owners December 10, 2021 14:07
@knz knz force-pushed the 20211130-server-ids branch 3 times, most recently from f5b6004 to 04e979a Compare December 10, 2021 14:35
@knz knz force-pushed the 20211130-server-ids branch from 04e979a to f70a2b5 Compare December 13, 2021 11:19
@knz knz requested a review from RaduBerinde December 13, 2021 11:19
craig bot pushed a commit that referenced this pull request Dec 13, 2021
73678: cli: avoid using background ctx in server start r=RaduBerinde,cameronnunez a=knz

Needed for #73306. 
Informs #58938.

This change ensures that the entire server startup and shutdown logic
operates under a go context that is connected to the main config's
tracer and AmbientCtx (and its log tags, and server identifiers).

Co-authored-by: Raphael 'kena' Poss <[email protected]>
@knz knz force-pushed the 20211130-server-ids branch 3 times, most recently from 83a11c5 to fa38f41 Compare December 13, 2021 17:45
Copy link
Member

@RaduBerinde RaduBerinde 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 @knz and @stevendanna)


pkg/ccl/backupccl/backup_test.go, line 6876 at r3 (raw file):

	makeTenant := func(srv serverutils.TestServerInterface, tenant uint64) (*sqlutils.SQLRunner, func()) {
		// Prevent a logging assertion that the server ID is initialized multiple times.

🎉


pkg/server/config.go, line 781 at r3 (raw file):

type idProvider struct {
	clusterID *base.ClusterIDContainer
	clusterS  atomic.Value

[nit] is S for string? Then make it Str so it's more clear. Also a comment somewhere mentioning that the Str fields cache tha values of their corresponding IDs, once those IDs are set.


pkg/server/config.go, line 783 at r3 (raw file):

	clusterS  atomic.Value

	isTenant bool

[nit] deserves a comment


pkg/server/config.go, line 822 at r3 (raw file):

	case log.IdentifyInstanceID:
		if !s.isTenant {

[nit] a comment would help here and below (eg "if this is not a tenant, the serverID is a node ID and not an instance ID").


pkg/server/config.go, line 839 at r3 (raw file):

// SetTenant informs the provider that it provides data for
// a SQL server.
func (s *idProvider) SetTenant(tenantID roachpb.TenantID) {

[nit] mention that this cannot be called after or concurrently with ServerIdentityString


pkg/server/config.go, line 841 at r3 (raw file):

func (s *idProvider) SetTenant(tenantID roachpb.TenantID) {
	s.isTenant = true
	s.tenantID = tenantID

This should assert that the tenantID is not set to anything else, no? (otherwise the memoized string could be out of sync)


pkg/server/config.go, line 844 at r3 (raw file):

}

func (s *idProvider) maybeMemoizeServerID() string {

[nit] could use a comment


pkg/util/log/server_ident.go, line 36 at r3 (raw file):

	IdentifyClusterID ServerIdentificationKey = iota
	// IdentifyNodeID retrieves the KV node ID of the server.
	IdentifyNodeID

[nit] consider using KVNodeID in places like this, just so there's less confusion

Copy link
Contributor Author

@knz knz 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 @knz and @stevendanna)


pkg/server/config.go, line 781 at r3 (raw file):

Previously, RaduBerinde wrote…

[nit] is S for string? Then make it Str so it's more clear. Also a comment somewhere mentioning that the Str fields cache tha values of their corresponding IDs, once those IDs are set.

Done.


pkg/server/config.go, line 783 at r3 (raw file):

Previously, RaduBerinde wrote…

[nit] deserves a comment

Done.


pkg/server/config.go, line 822 at r3 (raw file):

Previously, RaduBerinde wrote…

[nit] a comment would help here and below (eg "if this is not a tenant, the serverID is a node ID and not an instance ID").

Done.


pkg/server/config.go, line 839 at r3 (raw file):

Previously, RaduBerinde wrote…

[nit] mention that this cannot be called after or concurrently with ServerIdentityString

Done.


pkg/server/config.go, line 841 at r3 (raw file):

Previously, RaduBerinde wrote…

This should assert that the tenantID is not set to anything else, no? (otherwise the memoized string could be out of sync)

Done.


pkg/server/config.go, line 844 at r3 (raw file):

Previously, RaduBerinde wrote…

[nit] could use a comment

Done.

@knz knz force-pushed the 20211130-server-ids branch 2 times, most recently from 5df5106 to b23d5ca Compare December 14, 2021 12:39
Copy link
Contributor Author

@knz knz 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 @stevendanna)


pkg/util/log/server_ident.go, line 36 at r3 (raw file):

Previously, RaduBerinde wrote…

[nit] consider using KVNodeID in places like this, just so there's less confusion

Done.

@knz knz force-pushed the 20211130-server-ids branch from b23d5ca to 6c9d6c6 Compare December 14, 2021 12:40
Copy link
Member

@RaduBerinde RaduBerinde left a comment

Choose a reason for hiding this comment

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

:lgtm: Very nice comments, thank you!!

Reviewed 33 of 35 files at r3, 2 of 2 files at r4, all commit messages.
Reviewable status: :shipit: complete! 1 of 0 LGTMs obtained (waiting on @knz and @stevendanna)


pkg/server/config.go, line 867 at r4 (raw file):

// interface.
func (s *idProvider) SetTenant(tenantID roachpb.TenantID) {
	var defaultT roachpb.TenantID

[nit] can just use s.tenantID.IsSet(). By the way, couldn't we use that instead of the separate isTenant flag as well?


pkg/server/config.go, line 876 at r4 (raw file):

// maybeMemoizeServerID saves the representation of serverID to
// serverStr.

[nit] "if the former is initialized".

Prior to this change, the server identifiers (cluster ID, node ID etc)
were stored in global variables in the `log` package.
This was problematic when a single process contains multiple servers,
e.g. in tests, `demo` and multi-tenant CockroachDB.

This change switches the mechanism to use identifiers stored in the
go context. The disadvantage is that the server IDs are not any
more logged at the beginning of each log file (since a given log file
could report data from multiple servers).

Release note (cli change): The server identifiers (cluster ID, node
ID, tenant ID, instance ID) are not any more duplicated at the start
of every new log file (during log file rotations). They are now only
logged when known during server start-up.
(The copy of the identifiers is still included in per-event envelopes
for the various `json` output logging formats.)
@knz knz force-pushed the 20211130-server-ids branch from 6c9d6c6 to 0e6abb5 Compare December 14, 2021 21:23
Copy link
Contributor Author

@knz knz 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! 1 of 0 LGTMs obtained (waiting on @RaduBerinde and @stevendanna)


pkg/server/config.go, line 867 at r4 (raw file):

Previously, RaduBerinde wrote…

[nit] can just use s.tenantID.IsSet(). By the way, couldn't we use that instead of the separate isTenant flag as well?

Good idea.
Done.


pkg/server/config.go, line 876 at r4 (raw file):

Previously, RaduBerinde wrote…

[nit] "if the former is initialized".

Done.

@knz
Copy link
Contributor Author

knz commented Dec 14, 2021

TFYRs!

bors r=RaduBerinde,cameronnunez

@craig
Copy link
Contributor

craig bot commented Dec 14, 2021

Build succeeded:

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.

server,log: enable different server identifiers and other common logging in different in-memory processes
4 participants