-
Notifications
You must be signed in to change notification settings - Fork 329
feature/datasource_at merge to master #213
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
Merged
Merged
Changes from all commits
Commits
Show all changes
13 commits
Select commit
Hold shift + click to select a range
84146a5
(WIP) Feat transaction at datasource (#127)
chuntaojun 58ba55a
Feature/datasource at (#151)
liiibpm 2425256
optimize datasource usage of driver (#207)
luky116 9e6f8c0
refactor: modify datasource/sql struct
f10b32b
fix: fix wrong code
chuntaojun 7e70da4
fix: fix golang-lint check bugs
chuntaojun 150d6cc
fix: ignore datasource test
chuntaojun e89a078
style: use gofumpt to format code
chuntaojun 56e45a6
fix: nil pointer bug
chuntaojun cc5793f
style:fix code style
chuntaojun b618598
style: fix code style
chuntaojun 18432e1
fix: fix code style
chuntaojun 7df840a
fix: fix code style
chuntaojun File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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 |
|---|---|---|
|
|
@@ -19,3 +19,6 @@ dist/ | |
|
|
||
| # Dependency directories (remove the comment below to include it) | ||
| # vendor/ | ||
| .vscode | ||
| .codecc | ||
| vendor | ||
This file contains hidden or 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
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains hidden or 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,270 @@ | ||
| /* | ||
| * Licensed to the 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. | ||
| * The 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 sql | ||
|
|
||
| import ( | ||
| "context" | ||
| "database/sql" | ||
| "fmt" | ||
| "os" | ||
| "strconv" | ||
| "sync" | ||
| "time" | ||
|
|
||
| "github.com/seata/seata-go/pkg/datasource/sql/datasource" | ||
| "github.com/seata/seata-go/pkg/datasource/sql/types" | ||
| "github.com/seata/seata-go/pkg/datasource/sql/undo" | ||
| "github.com/seata/seata-go/pkg/protocol/branch" | ||
| "github.com/seata/seata-go/pkg/protocol/message" | ||
| "github.com/seata/seata-go/pkg/rm" | ||
| ) | ||
|
|
||
| const ( | ||
| _defaultResourceSize = 16 | ||
| _undoLogDeleteLimitSize = 1000 | ||
| ) | ||
|
|
||
| func init() { | ||
| datasource.RegisterResourceManager(branch.BranchTypeAT, | ||
| &ATSourceManager{ | ||
| resourceCache: sync.Map{}, | ||
| basic: datasource.NewBasicSourceManager(), | ||
| }) | ||
| } | ||
|
|
||
| type ATSourceManager struct { | ||
| resourceCache sync.Map | ||
| worker *asyncATWorker | ||
| basic *datasource.BasicSourceManager | ||
| } | ||
|
|
||
| // Register a Resource to be managed by Resource Manager | ||
| func (mgr *ATSourceManager) RegisterResource(res rm.Resource) error { | ||
| mgr.resourceCache.Store(res.GetResourceId(), res) | ||
|
|
||
| return mgr.basic.RegisterResource(res) | ||
| } | ||
|
|
||
| // Unregister a Resource from the Resource Manager | ||
| func (mgr *ATSourceManager) UnregisterResource(res rm.Resource) error { | ||
| return mgr.basic.UnregisterResource(res) | ||
| } | ||
|
|
||
| // Get all resources managed by this manager | ||
| func (mgr *ATSourceManager) GetManagedResources() map[string]rm.Resource { | ||
| ret := make(map[string]rm.Resource) | ||
|
|
||
| mgr.resourceCache.Range(func(key, value interface{}) bool { | ||
| ret[key.(string)] = value.(rm.Resource) | ||
| return true | ||
| }) | ||
|
|
||
| return ret | ||
| } | ||
|
|
||
| // BranchRollback Rollback the corresponding transactions according to the request | ||
| func (mgr *ATSourceManager) BranchRollback(ctx context.Context, req message.BranchRollbackRequest) (branch.BranchStatus, error) { | ||
| val, ok := mgr.resourceCache.Load(req.ResourceId) | ||
|
|
||
| if !ok { | ||
| return branch.BranchStatusPhaseoneFailed, fmt.Errorf("resource %s not found", req.ResourceId) | ||
| } | ||
|
|
||
| res := val.(*DBResource) | ||
|
|
||
| undoMgr, err := undo.GetUndoLogManager(res.dbType) | ||
| if err != nil { | ||
| return branch.BranchStatusUnknown, err | ||
| } | ||
|
|
||
| conn, err := res.target.Conn(ctx) | ||
| if err != nil { | ||
| return branch.BranchStatusUnknown, err | ||
| } | ||
|
|
||
| if err := undoMgr.RunUndo(req.Xid, req.BranchId, conn); err != nil { | ||
| transErr, ok := err.(*types.TransactionError) | ||
| if !ok { | ||
| return branch.BranchStatusPhaseoneFailed, err | ||
| } | ||
|
|
||
| if transErr.Code() == types.ErrorCodeBranchRollbackFailedUnretriable { | ||
| return branch.BranchStatusPhasetwoRollbackFailedUnretryable, nil | ||
| } | ||
|
|
||
| return branch.BranchStatusPhasetwoRollbackFailedRetryable, nil | ||
| } | ||
|
|
||
| return branch.BranchStatusPhasetwoRollbacked, nil | ||
| } | ||
|
|
||
| // BranchCommit | ||
| func (mgr *ATSourceManager) BranchCommit(ctx context.Context, req message.BranchCommitRequest) (branch.BranchStatus, error) { | ||
| mgr.worker.branchCommit(ctx, req) | ||
| return branch.BranchStatusPhaseoneDone, nil | ||
| } | ||
|
|
||
| // LockQuery | ||
| func (mgr *ATSourceManager) LockQuery(ctx context.Context, req message.GlobalLockQueryRequest) (bool, error) { | ||
| return false, nil | ||
| } | ||
|
|
||
| // BranchRegister | ||
| func (mgr *ATSourceManager) BranchRegister(ctx context.Context, clientId string, req message.BranchRegisterRequest) (int64, error) { | ||
| return 0, nil | ||
| } | ||
|
|
||
| // BranchReport | ||
| func (mgr *ATSourceManager) BranchReport(ctx context.Context, req message.BranchReportRequest) error { | ||
| return nil | ||
| } | ||
|
|
||
| // CreateTableMetaCache | ||
| func (mgr *ATSourceManager) CreateTableMetaCache(ctx context.Context, resID string, dbType types.DBType, | ||
| db *sql.DB) (datasource.TableMetaCache, error) { | ||
| return mgr.basic.CreateTableMetaCache(ctx, resID, dbType, db) | ||
| } | ||
|
|
||
| type asyncATWorker struct { | ||
| asyncCommitBufferLimit int64 | ||
| commitQueue chan phaseTwoContext | ||
| resourceMgr datasource.DataSourceManager | ||
| } | ||
|
|
||
| func newAsyncATWorker() *asyncATWorker { | ||
| asyncCommitBufferLimit := int64(10000) | ||
|
|
||
| val := os.Getenv("CLIENT_RM_ASYNC_COMMIT_BUFFER_LIMIT") | ||
| if val != "" { | ||
| limit, _ := strconv.ParseInt(val, 10, 64) | ||
| if limit != 0 { | ||
| asyncCommitBufferLimit = limit | ||
| } | ||
| } | ||
|
|
||
| worker := &asyncATWorker{ | ||
| commitQueue: make(chan phaseTwoContext, asyncCommitBufferLimit), | ||
| } | ||
|
|
||
| return worker | ||
| } | ||
|
|
||
| func (w *asyncATWorker) doBranchCommitSafely() { | ||
| batchSize := 64 | ||
|
|
||
| ticker := time.NewTicker(1 * time.Second) | ||
| phaseCtxs := make([]phaseTwoContext, 0, batchSize) | ||
|
|
||
| for { | ||
chuntaojun marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| select { | ||
| case phaseCtx := <-w.commitQueue: | ||
| phaseCtxs = append(phaseCtxs, phaseCtx) | ||
| if len(phaseCtxs) == batchSize { | ||
| tmp := phaseCtxs | ||
| w.doBranchCommit(tmp) | ||
| phaseCtxs = make([]phaseTwoContext, 0, batchSize) | ||
| } | ||
| case <-ticker.C: | ||
| tmp := phaseCtxs | ||
| w.doBranchCommit(tmp) | ||
|
|
||
| phaseCtxs = make([]phaseTwoContext, 0, batchSize) | ||
| } | ||
| } | ||
| } | ||
|
|
||
| func (w *asyncATWorker) doBranchCommit(phaseCtxs []phaseTwoContext) { | ||
| groupCtxs := make(map[string][]phaseTwoContext, _defaultResourceSize) | ||
|
|
||
| for i := range phaseCtxs { | ||
| if phaseCtxs[i].ResourceID == "" { | ||
| continue | ||
| } | ||
|
|
||
| if _, ok := groupCtxs[phaseCtxs[i].ResourceID]; !ok { | ||
| groupCtxs[phaseCtxs[i].ResourceID] = make([]phaseTwoContext, 0, 4) | ||
| } | ||
|
|
||
| ctxs := groupCtxs[phaseCtxs[i].ResourceID] | ||
| ctxs = append(ctxs, phaseCtxs[i]) | ||
|
|
||
| groupCtxs[phaseCtxs[i].ResourceID] = ctxs | ||
| } | ||
|
|
||
| for k := range groupCtxs { | ||
| w.dealWithGroupedContexts(k, groupCtxs[k]) | ||
| } | ||
| } | ||
|
|
||
| func (w *asyncATWorker) dealWithGroupedContexts(resID string, phaseCtxs []phaseTwoContext) { | ||
| val, ok := w.resourceMgr.GetManagedResources()[resID] | ||
| if !ok { | ||
| for i := range phaseCtxs { | ||
| w.commitQueue <- phaseCtxs[i] | ||
| } | ||
| return | ||
| } | ||
|
|
||
| res := val.(*DBResource) | ||
|
|
||
| conn, err := res.target.Conn(context.Background()) | ||
| if err != nil { | ||
| for i := range phaseCtxs { | ||
| w.commitQueue <- phaseCtxs[i] | ||
| } | ||
| } | ||
|
|
||
| defer conn.Close() | ||
|
|
||
| undoMgr, err := undo.GetUndoLogManager(res.dbType) | ||
| if err != nil { | ||
| for i := range phaseCtxs { | ||
| w.commitQueue <- phaseCtxs[i] | ||
| } | ||
|
|
||
| return | ||
| } | ||
|
|
||
| for i := range phaseCtxs { | ||
| phaseCtx := phaseCtxs[i] | ||
| if err := undoMgr.DeleteUndoLogs([]string{phaseCtx.Xid}, []int64{phaseCtx.BranchID}, conn); err != nil { | ||
| w.commitQueue <- phaseCtx | ||
| } | ||
| } | ||
| } | ||
|
|
||
| func (w *asyncATWorker) branchCommit(ctx context.Context, req message.BranchCommitRequest) { | ||
| phaseCtx := phaseTwoContext{ | ||
| Xid: req.Xid, | ||
| BranchID: req.BranchId, | ||
| ResourceID: req.ResourceId, | ||
| } | ||
|
|
||
| select { | ||
| case w.commitQueue <- phaseCtx: | ||
| case <-ctx.Done(): | ||
| } | ||
|
|
||
| return | ||
| } | ||
|
|
||
| type phaseTwoContext struct { | ||
| Xid string | ||
| BranchID int64 | ||
| ResourceID string | ||
| } | ||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
这里的err, 尽量都改成 error.Warp 吧
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
这里没有将另外一个错误包装返回,这里没必要这个吧