Browse Source

Merge pull request #48 from 106umao/ActionContextForTcc

feature append action context for tcc
tags/v0.1.0-rc1
Xin.Zh GitHub 3 years ago
parent
commit
5ae225339e
No known key found for this signature in database GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 58 additions and 8 deletions
  1. +3
    -0
      .gitignore
  2. +19
    -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
*.exe
*.exe~


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

@@ -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 (
@@ -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,
}
}



+ 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