forked from cockroachdb/cockroach
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
changefeedccl: support ALTER CHANGEFEED statement for
adding/dropping targets References issue cockroachdb#75895 This PR introduces a new SQL statement called ALTER CHANGEFEED, which allows users to add/drop targets for an existing changefeed. Note that the changefeed job must be paused in order for the alterations to be applied to the changefeed. The syntax of the statement is as follows: ALTER CHANGEFEED <job_id> {{ADD|DROP} <targets>}... Where there can be an arbitrary number of ADD/ DROP commands in any order. Once a user executes this statement, the targets will be added/dropped, and the statement will return the job id and the new job description of the changefeed job. It is also important to note that a user cannot drop all targets in a changefeed. Example: ALTER CHANGEFEED 123 ADD foo,bar DROP baz; Release note (enterprise change): Added support for a new SQL statement called ALTER CHANGEFEED, which allows users to add/drop targets for an existing changefeed. The syntax of the statement is as follows: ALTER CHANGEFEED <job_id> {{ADD|DROP} <targets>}... Where there can be an abritrary number of ADD/DROP commands in any order. The following is an example of this statement: ALTER CHANGEFEED 123 ADD foo,bar DROP baz; With this statement, users can avoid going through the process of altering a changefeed on their own, and rely on CRDB to carry out this task.
- Loading branch information
Sherman Grewal
committed
Feb 9, 2022
1 parent
a5158c4
commit 9d40c8e
Showing
18 changed files
with
831 additions
and
99 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,2 @@ | ||
alter_changefeed_stmt ::= | ||
'ALTER' 'CHANGEFEED' a_expr alter_changefeed_cmds |
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 |
---|---|---|
|
@@ -9,3 +9,4 @@ alter_ddl_stmt ::= | |
| alter_schema_stmt | ||
| alter_type_stmt | ||
| alter_default_privileges_stmt | ||
| alter_changefeed_stmt |
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
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,202 @@ | ||
// Copyright 2022 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 changefeedccl | ||
|
||
import ( | ||
"context" | ||
|
||
"github.com/cockroachdb/cockroach/pkg/jobs" | ||
"github.com/cockroachdb/cockroach/pkg/jobs/jobspb" | ||
"github.com/cockroachdb/cockroach/pkg/kv" | ||
"github.com/cockroachdb/cockroach/pkg/sql" | ||
"github.com/cockroachdb/cockroach/pkg/sql/catalog" | ||
"github.com/cockroachdb/cockroach/pkg/sql/catalog/colinfo" | ||
"github.com/cockroachdb/cockroach/pkg/sql/catalog/descpb" | ||
"github.com/cockroachdb/cockroach/pkg/sql/parser" | ||
"github.com/cockroachdb/cockroach/pkg/sql/privilege" | ||
"github.com/cockroachdb/cockroach/pkg/sql/sem/tree" | ||
"github.com/cockroachdb/cockroach/pkg/sql/types" | ||
"github.com/cockroachdb/cockroach/pkg/util/hlc" | ||
"github.com/cockroachdb/errors" | ||
) | ||
|
||
func init() { | ||
sql.AddPlanHook(alterChangefeedPlanHook) | ||
} | ||
|
||
type alterChangefeedOpts struct { | ||
AddTargets []tree.TargetList | ||
DropTargets []tree.TargetList | ||
} | ||
|
||
// alterChangefeedPlanHook implements sql.PlanHookFn. | ||
func alterChangefeedPlanHook( | ||
ctx context.Context, stmt tree.Statement, p sql.PlanHookState, | ||
) (sql.PlanHookRowFn, colinfo.ResultColumns, []sql.PlanNode, bool, error) { | ||
alterChangefeedStmt, ok := stmt.(*tree.AlterChangefeed) | ||
if !ok { | ||
return nil, nil, nil, false, nil | ||
} | ||
|
||
header := colinfo.ResultColumns{ | ||
{Name: "job_id", Typ: types.Int}, | ||
{Name: "job_description", Typ: types.String}, | ||
} | ||
lockForUpdate := false | ||
|
||
fn := func(ctx context.Context, _ []sql.PlanNode, resultsCh chan<- tree.Datums) error { | ||
if err := validateSettings(ctx, p); err != nil { | ||
return err | ||
} | ||
|
||
typedExpr, err := alterChangefeedStmt.Jobs.TypeCheck(ctx, p.SemaCtx(), types.Int) | ||
if err != nil { | ||
return err | ||
} | ||
jobID := jobspb.JobID(tree.MustBeDInt(typedExpr)) | ||
|
||
job, err := p.ExecCfg().JobRegistry.LoadJobWithTxn(ctx, jobID, p.ExtendedEvalContext().Txn) | ||
if err != nil { | ||
err = errors.Wrapf(err, `could not load job with job id %d`, jobID) | ||
return err | ||
} | ||
|
||
details, ok := job.Details().(jobspb.ChangefeedDetails) | ||
if !ok { | ||
return errors.Errorf(`job %d is not changefeed job`, jobID) | ||
} | ||
|
||
if job.Status() != jobs.StatusPaused { | ||
return errors.Errorf(`job %d is not paused`, jobID) | ||
} | ||
|
||
var opts alterChangefeedOpts | ||
for _, cmd := range alterChangefeedStmt.Cmds { | ||
switch v := cmd.(type) { | ||
case *tree.AlterChangefeedAddTarget: | ||
opts.AddTargets = append(opts.AddTargets, v.Targets) | ||
case *tree.AlterChangefeedDropTarget: | ||
opts.DropTargets = append(opts.DropTargets, v.Targets) | ||
} | ||
} | ||
|
||
var initialHighWater hlc.Timestamp | ||
statementTime := hlc.Timestamp{ | ||
WallTime: p.ExtendedEvalContext().GetStmtTimestamp().UnixNano(), | ||
} | ||
|
||
if opts.AddTargets != nil { | ||
var targetDescs []catalog.Descriptor | ||
|
||
for _, targetList := range opts.AddTargets { | ||
descs, err := getTableDescriptors(ctx, p, &targetList, statementTime, initialHighWater) | ||
if err != nil { | ||
return err | ||
} | ||
targetDescs = append(targetDescs, descs...) | ||
} | ||
|
||
newTargets, err := getTargets(ctx, p, targetDescs, details.Opts) | ||
if err != nil { | ||
return err | ||
} | ||
// add old targets | ||
for id, target := range details.Targets { | ||
newTargets[id] = target | ||
} | ||
details.Targets = newTargets | ||
} | ||
|
||
if opts.DropTargets != nil { | ||
var targetDescs []catalog.Descriptor | ||
|
||
for _, targetList := range opts.DropTargets { | ||
descs, err := getTableDescriptors(ctx, p, &targetList, statementTime, initialHighWater) | ||
if err != nil { | ||
return err | ||
} | ||
targetDescs = append(targetDescs, descs...) | ||
} | ||
|
||
for _, desc := range targetDescs { | ||
if table, isTable := desc.(catalog.TableDescriptor); isTable { | ||
if err := p.CheckPrivilege(ctx, desc, privilege.SELECT); err != nil { | ||
return err | ||
} | ||
delete(details.Targets, table.GetID()) | ||
} | ||
} | ||
} | ||
|
||
if len(details.Targets) == 0 { | ||
return errors.Errorf("cannot drop all targets for changefeed job %d", jobID) | ||
} | ||
|
||
if err := validateSink(ctx, p, jobID, details, details.Opts); err != nil { | ||
return err | ||
} | ||
|
||
oldStmt, err := parser.ParseOne(job.Payload().Description) | ||
if err != nil { | ||
return err | ||
} | ||
oldChangefeedStmt, ok := oldStmt.AST.(*tree.CreateChangefeed) | ||
if !ok { | ||
return errors.Errorf(`could not parse create changefeed statement for job %d`, jobID) | ||
} | ||
|
||
var targets tree.TargetList | ||
for _, target := range details.Targets { | ||
targetName := tree.MakeTableNameFromPrefix(tree.ObjectNamePrefix{}, tree.Name(target.StatementTimeName)) | ||
targets.Tables = append(targets.Tables, &targetName) | ||
} | ||
|
||
oldChangefeedStmt.Targets = targets | ||
jobDescription := tree.AsString(oldChangefeedStmt) | ||
|
||
newPayload := job.Payload() | ||
newPayload.Description = jobDescription | ||
newPayload.Details = jobspb.WrapPayloadDetails(details) | ||
|
||
finalDescs, err := getTableDescriptors(ctx, p, &targets, statementTime, initialHighWater) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
newPayload.DescriptorIDs = func() (sqlDescIDs []descpb.ID) { | ||
for _, desc := range finalDescs { | ||
sqlDescIDs = append(sqlDescIDs, desc.GetID()) | ||
} | ||
return sqlDescIDs | ||
}() | ||
|
||
err = p.ExecCfg().JobRegistry.UpdateJobWithTxn(ctx, jobID, p.ExtendedEvalContext().Txn, lockForUpdate, func( | ||
txn *kv.Txn, md jobs.JobMetadata, ju *jobs.JobUpdater, | ||
) error { | ||
ju.UpdatePayload(&newPayload) | ||
return nil | ||
}) | ||
|
||
if err != nil { | ||
return err | ||
} | ||
|
||
select { | ||
case <-ctx.Done(): | ||
return ctx.Err() | ||
case resultsCh <- tree.Datums{ | ||
tree.NewDInt(tree.DInt(jobID)), | ||
tree.NewDString(jobDescription), | ||
}: | ||
return nil | ||
} | ||
} | ||
|
||
return fn, header, nil, false, nil | ||
} |
Oops, something went wrong.