Skip to content

streamingccl: create new tenant automatically for replication stream#83646

Merged
craig[bot] merged 1 commit intocockroachdb:masterfrom
samiskin:replication-stream-create-tenant
Jul 12, 2022
Merged

streamingccl: create new tenant automatically for replication stream#83646
craig[bot] merged 1 commit intocockroachdb:masterfrom
samiskin:replication-stream-create-tenant

Conversation

@samiskin
Copy link
Copy Markdown
Contributor

@samiskin samiskin commented Jun 30, 2022

Resolves #76952

Originally tenants had to be created manually prior to replication
stream creation. This change instead creates a new tenant upon calling
the RESTORE INTO command using the tenant ID that was provided. The
tenant is considered inactive until the cutover point, at which the
tenant is activated.

Release note: None

@cockroach-teamcity
Copy link
Copy Markdown
Member

This change is Reviewable

@samiskin
Copy link
Copy Markdown
Contributor Author

samiskin commented Jun 30, 2022

I purposefully did not include the necessary OnFailOrCancel behaviour of tearing down the tenant just yet since #83310 is still in-flight as of this comment. I'll add tenant teardown after it merges.

The code to validate that the tenant is inactive is commented out until #83650 is resolved. Right now the tenant is marked inactive but its still possible to open a SQL connection to it and query its tables.

@samiskin samiskin force-pushed the replication-stream-create-tenant branch 5 times, most recently from 1632472 to 7bdf88d Compare June 30, 2022 14:41
@samiskin samiskin marked this pull request as ready for review June 30, 2022 14:42
@samiskin samiskin requested a review from a team June 30, 2022 14:42
@samiskin samiskin requested a review from a team as a code owner June 30, 2022 14:42
@samiskin samiskin requested review from a team, HonoreDB, gh-casper and msbutler and removed request for a team June 30, 2022 14:42
if _, err := sql.GetTenantRecord(ctx, p.ExecCfg(), nil, newTenantID.ToUint64()); err == nil {
return errors.Newf("tenant with id %s already exists", newTenantID)
}
if err := p.ExtendedEvalContext().Tenant.CreateInactiveTenant(ctx, newTenantID.ToUint64()); err != nil {
Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

I elected to use the full CreateTenant code rather than just creating a record like Restore does because it seemed like just creating a record did eventually initialize the rest of the tenant such that I run queries against it, but prior to that point a sql connection would error and ActivateTenant didn't look like it would necessarily force the initialization so I was concerned about a stream that was created and then completed very quickly, activating the tenant prior to it being actually ready. I didn't end up looking into the mechanism for this eventual initialization.

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

Coming back to this. I think in the long run we want to do something a little different.

The problem with initialising the tenant is that it means that the keyspace we are writing into isn't completely empty. Namely it will have entries schema entries for the default database, the cluster version setting, the id sequence populated. If this destination tenant is created after the source tenant, then the timestamp on those KVs will likely be above whatever gets replicated to us.

because it seemed like just creating a record did eventually initialize the rest of the tenant such that I run queries against it

Did you see this outside of the streaming replication tests? If you only saw it in the streaming replication tests, my guess for what you were seeing is that eventually enough of the source cluster's tenant data is replicated to it for it to be viable.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

I think your theory on "the tenant works when enough has been streamed in" is true, since attempting to connect right after stream creation doesn't work, connecting after waiting 2 seconds but without cutover still works, and connecting after cutover always works.

@samiskin samiskin force-pushed the replication-stream-create-tenant branch from 7bdf88d to bf84b8a Compare June 30, 2022 14:58
@msbutler msbutler requested review from stevendanna and removed request for msbutler June 30, 2022 18:17
@samiskin samiskin force-pushed the replication-stream-create-tenant branch from bf84b8a to ac4af26 Compare June 30, 2022 18:35
Copy link
Copy Markdown
Collaborator

@stevendanna stevendanna left a comment

Choose a reason for hiding this comment

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

Thanks for jumping into this. This looks reasonable to me. I've left some comments but nothing blocking.

@samiskin samiskin force-pushed the replication-stream-create-tenant branch 4 times, most recently from 5e692c0 to 267427e Compare July 4, 2022 01:12
@samiskin samiskin force-pushed the replication-stream-create-tenant branch 2 times, most recently from 0617def to 53c05df Compare July 11, 2022 19:19
Originally tenants had to be created manually prior to replication
stream creation.  This change instead creates a new tenant upon calling
the RESTORE INTO command using the tenant ID that was provided.  The
tenant is considered inactive until the cutover point, at which the
tenant is activated.

Release note: None
@samiskin samiskin force-pushed the replication-stream-create-tenant branch from 53c05df to e9fe630 Compare July 11, 2022 20:58
@samiskin
Copy link
Copy Markdown
Contributor Author

bors r+

@craig
Copy link
Copy Markdown
Contributor

craig bot commented Jul 12, 2022

Build succeeded:

@craig craig bot merged commit 0aa3c44 into cockroachdb:master Jul 12, 2022
@shermanCRL shermanCRL added the A-tenant-streaming Including cluster streaming label Jul 29, 2022
@shermanCRL shermanCRL added this to the 22.2 milestone Jul 29, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

A-tenant-streaming Including cluster streaming

Projects

None yet

Development

Successfully merging this pull request may close these issues.

streamingccl: ingestion job should takes the control of dest tenant state during stream replication

4 participants