Skip to content

Commit

Permalink
Merge pull request apache#48 from 106umao/ActionContextForTcc
Browse files Browse the repository at this point in the history
feature append action context for tcc
  • Loading branch information
AlexStocks authored Jul 12, 2022
2 parents 511859f + 12e2194 commit d130a9e
Show file tree
Hide file tree
Showing 3 changed files with 58 additions and 8 deletions.
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
# Idea configuration file
.idea/

# Binaries for programs and plugins
*.exe
*.exe~
Expand Down
27 changes: 19 additions & 8 deletions pkg/rm/tcc/tcc_resource.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,14 +19,15 @@ package tcc

import (
"context"
"encoding/json"
"fmt"
"sync"

"github.com/seata/seata-go/pkg/protocol/resource"
"github.com/seata/seata-go/pkg/tm"

"github.com/seata/seata-go/pkg/common"
"github.com/seata/seata-go/pkg/protocol/branch"
"github.com/seata/seata-go/pkg/protocol/resource"
"github.com/seata/seata-go/pkg/rm"
"github.com/seata/seata-go/pkg/tm"
)

var (
Expand Down Expand Up @@ -125,12 +126,22 @@ func (t *TCCResourceManager) BranchCommit(ctx context.Context, ranchType branch.
}

func (t *TCCResourceManager) getBusinessActionContext(xid string, branchID int64, resourceID string, applicationData []byte) tm.BusinessActionContext {
var actionContextMap = make(map[string]interface{}, 2)
if len(applicationData) > 0 {
var tccContext map[string]interface{}
if err := json.Unmarshal(applicationData, &tccContext); err != nil {
panic("application data failed to unmarshl as json")
}
if v, ok := tccContext[common.ActionContext]; ok {
actionContextMap = v.(map[string]interface{})
}
}

return tm.BusinessActionContext{
Xid: xid,
BranchId: branchID,
ActionName: resourceID,
// todo get ActionContext
//ActionContext:,
Xid: xid,
BranchId: branchID,
ActionName: resourceID,
ActionContext: &actionContextMap,
}
}

Expand Down
36 changes: 36 additions & 0 deletions pkg/rm/tcc/tcc_resource_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
/*
* 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 tcc

import (
"encoding/json"
"testing"

"github.com/stretchr/testify/assert"
)

func TestActionContext(t *testing.T) {
applicationData := `{"actionContext":{"zhangsan":"lisi"}}`
businessActionContext := GetTCCResourceManagerInstance().
getBusinessActionContext("1111111111", 2645276141, "TestActionContext", []byte(applicationData))

assert.NotEmpty(t, businessActionContext)
bytes, err := json.Marshal(businessActionContext.ActionContext)
assert.Nil(t, err)
assert.Equal(t, `{"zhangsan":"lisi"}`, string(bytes))
}

0 comments on commit d130a9e

Please sign in to comment.