Browse Source

feature add action context for tcc

tags/v0.1.0-rc1
juzimao 3 years ago
parent
commit
6008f73912
3 changed files with 56 additions and 8 deletions
  1. +3
    -0
      .gitignore
  2. +17
    -8
      pkg/rm/tcc/tcc_resource.go
  3. +36
    -0
      pkg/rm/tcc/tcc_resource_test.go

+ 3
- 0
.gitignore View File

@@ -1,3 +1,6 @@
# Idea configuration file
.idea/

# Binaries for programs and plugins # Binaries for programs and plugins
*.exe *.exe
*.exe~ *.exe~


+ 17
- 8
pkg/rm/tcc/tcc_resource.go View File

@@ -19,14 +19,15 @@ package tcc


import ( import (
"context" "context"
"encoding/json"
"fmt" "fmt"
"sync" "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/branch"
"github.com/seata/seata-go/pkg/protocol/resource"
"github.com/seata/seata-go/pkg/rm" "github.com/seata/seata-go/pkg/rm"
"github.com/seata/seata-go/pkg/tm"
) )


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


func (t *TCCResourceManager) getBusinessActionContext(xid string, branchID int64, resourceID string, applicationData []byte) tm.BusinessActionContext { 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{}
json.Unmarshal(applicationData, &tccContext)
if v, ok := tccContext[common.ActionContext]; ok {
actionContextMap = v.(map[string]interface{})
}
}

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




+ 36
- 0
pkg/rm/tcc/tcc_resource_test.go View File

@@ -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))
}

Loading…
Cancel
Save