-
Notifications
You must be signed in to change notification settings - Fork 3.8k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
revertccl: ALTER VIRTUAL CLUSTER RESET DATA
This enables resetting a virtual cluster's data to a prior timestamp. This is possible if the prior timestamp is still retained in the mvcc history of the virtual cluster, the virtual cluster has stopped service, and is run by a user with the MANAGEVIRTUALCLUSTER (or admin) privilege in the system tenant. Revisions of data in the system tenant newer than the target time to which it is being reset are destroyed, reverting the tenant to the state it was in as of the time reverted to. Destroyed revisions are not recoverable; once a tenant has been reset to a timestamp, it cannot be 'un-reset' back to a higher timestamp. Release note (cluster virtualization): Added a new 'flashback' command to revert a virtual cluster to an earlier state using ALTER VIRTUAL CLUSTER RESET DATA. Epic: CRDB-34233.
- Loading branch information
Showing
4 changed files
with
96 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,84 @@ | ||
// Copyright 2024 The Cockroach Authors. | ||
// | ||
// Licensed as a CockroachDB Enterprise file under the Cockroach Community | ||
// License (the "License"); you may not use this file except in compliance with | ||
// the License. You may obtain a copy of the License at | ||
// | ||
// https://github.com/cockroachdb/cockroach/blob/master/licenses/CCL.txt | ||
|
||
package revertccl | ||
|
||
import ( | ||
"context" | ||
|
||
"github.com/cockroachdb/cockroach/pkg/ccl/utilccl" | ||
"github.com/cockroachdb/cockroach/pkg/sql" | ||
"github.com/cockroachdb/cockroach/pkg/sql/catalog/colinfo" | ||
"github.com/cockroachdb/cockroach/pkg/sql/exprutil" | ||
"github.com/cockroachdb/cockroach/pkg/sql/pgwire/pgcode" | ||
"github.com/cockroachdb/cockroach/pkg/sql/pgwire/pgerror" | ||
"github.com/cockroachdb/cockroach/pkg/sql/sem/asof" | ||
"github.com/cockroachdb/cockroach/pkg/sql/sem/tree" | ||
) | ||
|
||
const ( | ||
alterTenantResetOp = "ALTER VIRTUAL CLUSTER RESET" | ||
) | ||
|
||
func alterTenantResetHook( | ||
ctx context.Context, stmt tree.Statement, p sql.PlanHookState, | ||
) (sql.PlanHookRowFn, colinfo.ResultColumns, []sql.PlanNode, bool, error) { | ||
alterTenantStmt, ok := stmt.(*tree.AlterTenantReset) | ||
if !ok { | ||
return nil, nil, nil, false, nil | ||
} | ||
if !p.ExecCfg().Codec.ForSystemTenant() { | ||
return nil, nil, nil, false, pgerror.Newf(pgcode.InsufficientPrivilege, "only the system tenant can alter tenant") | ||
} | ||
|
||
timestamp, err := asof.EvalSystemTimeExpr(ctx, &p.ExtendedEvalContext().Context, p.SemaCtx(), alterTenantStmt.Timestamp, | ||
alterTenantResetOp, asof.ReplicationCutover) | ||
if err != nil { | ||
return nil, nil, nil, false, err | ||
} | ||
|
||
fn := func(ctx context.Context, _ []sql.PlanNode, resultsCh chan<- tree.Datums) error { | ||
if err := sql.CanManageTenant(ctx, p); err != nil { | ||
return err | ||
} | ||
if err := utilccl.CheckEnterpriseEnabled(p.ExecCfg().Settings, alterTenantResetOp); err != nil { | ||
return err | ||
} | ||
|
||
tenInfo, err := p.LookupTenantInfo(ctx, alterTenantStmt.TenantSpec, alterTenantResetOp) | ||
if err != nil { | ||
return err | ||
} | ||
return RevertTenantToTimestamp(ctx, &p.ExtendedEvalContext().Context, tenInfo.Name, timestamp, p.ExtendedEvalContext().SessionID) | ||
} | ||
return fn, nil, nil, false, nil | ||
} | ||
|
||
func alterTenantResetHookTypeCheck( | ||
ctx context.Context, stmt tree.Statement, p sql.PlanHookState, | ||
) (bool, colinfo.ResultColumns, error) { | ||
alterStmt, ok := stmt.(*tree.AlterTenantReset) | ||
if !ok { | ||
return false, nil, nil | ||
} | ||
if err := exprutil.TypeCheck( | ||
ctx, alterTenantResetOp, p.SemaCtx(), exprutil.TenantSpec{TenantSpec: alterStmt.TenantSpec}, | ||
); err != nil { | ||
return false, nil, err | ||
} | ||
if _, err := asof.TypeCheckSystemTimeExpr( | ||
ctx, p.SemaCtx(), alterStmt.Timestamp, alterTenantResetOp, | ||
); err != nil { | ||
return false, nil, err | ||
} | ||
return true, nil, nil | ||
} | ||
|
||
func init() { | ||
sql.AddPlanHook("alter virtual cluster reset", alterTenantResetHook, alterTenantResetHookTypeCheck) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters