forked from arana-db/arana
-
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.
feat: implement sharding update (arana-db#107)
* feat: implement sharding update * fix
- Loading branch information
Showing
9 changed files
with
270 additions
and
1 deletion.
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,15 @@ | ||
set dotenv-load := true | ||
|
||
alias r := run | ||
alias c := cli | ||
|
||
default: | ||
@just --list | ||
|
||
run: | ||
@go run ./cmd/... start -c ./docker/conf/config.yaml | ||
|
||
cli: | ||
@mycli -h127.0.0.1 -P13306 -udksl employees -p123456 | ||
cli-raw: | ||
@mycli -h127.0.0.1 -uroot employees -p123456 |
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 |
---|---|---|
|
@@ -27,6 +27,7 @@ import ( | |
|
||
import ( | ||
"github.com/golang/mock/gomock" | ||
|
||
"github.com/stretchr/testify/assert" | ||
) | ||
|
||
|
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,44 @@ | ||
// Licensed to Apache Software Foundation (ASF) under one or more contributor | ||
// license agreements. See the NOTICE file distributed with | ||
// this work for additional information regarding copyright | ||
// ownership. Apache Software Foundation (ASF) licenses this file to you under | ||
// the Apache License, Version 2.0 (the "License"); you may | ||
// not use this file except in compliance with the License. | ||
// You may obtain a copy of the License at | ||
// | ||
// http://www.apache.org/licenses/LICENSE-2.0 | ||
// | ||
// Unless required by applicable law or agreed to in writing, | ||
// software distributed under the License is distributed on an | ||
// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY | ||
// KIND, either express or implied. See the License for the | ||
// specific language governing permissions and limitations | ||
// under the License. | ||
// | ||
|
||
package plan | ||
|
||
import ( | ||
"context" | ||
) | ||
|
||
import ( | ||
"github.com/arana-db/arana/pkg/mysql" | ||
"github.com/arana-db/arana/pkg/proto" | ||
) | ||
|
||
var _ proto.Plan = (*AlwaysEmptyExecPlan)(nil) | ||
|
||
var _emptyResult mysql.Result | ||
|
||
// AlwaysEmptyExecPlan represents an exec plan which affects nothing. | ||
type AlwaysEmptyExecPlan struct { | ||
} | ||
|
||
func (a AlwaysEmptyExecPlan) Type() proto.PlanType { | ||
return proto.PlanTypeExec | ||
} | ||
|
||
func (a AlwaysEmptyExecPlan) ExecIn(ctx context.Context, conn proto.VConn) (proto.Result, error) { | ||
return &_emptyResult, nil | ||
} |
File renamed without changes.
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,129 @@ | ||
// Licensed to Apache Software Foundation (ASF) under one or more contributor | ||
// license agreements. See the NOTICE file distributed with | ||
// this work for additional information regarding copyright | ||
// ownership. Apache Software Foundation (ASF) licenses this file to you under | ||
// the Apache License, Version 2.0 (the "License"); you may | ||
// not use this file except in compliance with the License. | ||
// You may obtain a copy of the License at | ||
// | ||
// http://www.apache.org/licenses/LICENSE-2.0 | ||
// | ||
// Unless required by applicable law or agreed to in writing, | ||
// software distributed under the License is distributed on an | ||
// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY | ||
// KIND, either express or implied. See the License for the | ||
// specific language governing permissions and limitations | ||
// under the License. | ||
// | ||
|
||
package plan | ||
|
||
import ( | ||
"context" | ||
"strings" | ||
) | ||
|
||
import ( | ||
"github.com/pkg/errors" | ||
|
||
uatomic "go.uber.org/atomic" | ||
|
||
"golang.org/x/sync/errgroup" | ||
) | ||
|
||
import ( | ||
"github.com/arana-db/arana/pkg/mysql" | ||
"github.com/arana-db/arana/pkg/proto" | ||
"github.com/arana-db/arana/pkg/proto/rule" | ||
"github.com/arana-db/arana/pkg/runtime/ast" | ||
"github.com/arana-db/arana/pkg/util/log" | ||
) | ||
|
||
var _ proto.Plan = (*UpdatePlan)(nil) | ||
|
||
// UpdatePlan represents a plan to execute sharding-update. | ||
type UpdatePlan struct { | ||
basePlan | ||
stmt *ast.UpdateStatement | ||
shards rule.DatabaseTables | ||
} | ||
|
||
// NewUpdatePlan creates a sharding-update plan. | ||
func NewUpdatePlan(stmt *ast.UpdateStatement) *UpdatePlan { | ||
return &UpdatePlan{stmt: stmt} | ||
} | ||
|
||
func (up *UpdatePlan) Type() proto.PlanType { | ||
return proto.PlanTypeExec | ||
} | ||
|
||
func (up *UpdatePlan) ExecIn(ctx context.Context, conn proto.VConn) (proto.Result, error) { | ||
if up.shards == nil { | ||
var sb strings.Builder | ||
if err := up.stmt.Restore(ast.RestoreDefault, &sb, nil); err != nil { | ||
return nil, err | ||
} | ||
return conn.Exec(ctx, "", sb.String(), up.args...) | ||
} | ||
|
||
var ( | ||
affects = uatomic.NewUint64(0) | ||
cnt = uatomic.NewUint32(0) | ||
) | ||
|
||
var g errgroup.Group | ||
|
||
// TODO: should wrap with tx in the future | ||
for k, v := range up.shards { | ||
// do copy for goroutine-safe | ||
var ( | ||
db = k | ||
tables = v | ||
) | ||
// execute concurrent for each phy database | ||
g.Go(func() error { | ||
var ( | ||
sb strings.Builder | ||
args []int | ||
res proto.Result | ||
err error | ||
) | ||
|
||
sb.Grow(256) | ||
|
||
for _, table := range tables { | ||
if err = up.stmt.ResetTable(table).Restore(ast.RestoreDefault, &sb, &args); err != nil { | ||
return errors.WithStack(err) | ||
} | ||
|
||
if res, err = conn.Exec(ctx, db, sb.String(), up.toArgs(args)...); err != nil { | ||
return errors.WithStack(err) | ||
} | ||
|
||
n, _ := res.RowsAffected() | ||
affects.Add(n) | ||
cnt.Inc() | ||
|
||
// cleanup | ||
if len(args) > 0 { | ||
args = args[:0] | ||
} | ||
sb.Reset() | ||
} | ||
|
||
return nil | ||
}) | ||
} | ||
|
||
if err := g.Wait(); err != nil { | ||
return nil, err | ||
} | ||
|
||
log.Debugf("sharding update success: batch=%d, affects=%d", cnt.Load(), affects.Load()) | ||
|
||
return &mysql.Result{AffectedRows: affects.Load()}, nil | ||
} | ||
|
||
func (up *UpdatePlan) SetShards(shards rule.DatabaseTables) { | ||
up.shards = shards | ||
} |