You are viewing a plain text version of this content. The canonical link for it is here.
Posted to notifications@dubbo.apache.org by GitBox <gi...@apache.org> on 2021/11/01 01:56:50 UTC

[GitHub] [dubbo-go-pixiu] ztelur commented on a change in pull request #261: integrated seata-golang filter, support tcc transaction mode

ztelur commented on a change in pull request #261:
URL: https://github.com/apache/dubbo-go-pixiu/pull/261#discussion_r739908190



##########
File path: pkg/filter/seata/branch_transaction_service.go
##########
@@ -0,0 +1,240 @@
+/*
+ * 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 seata
+
+import (
+	"context"
+	"io"
+	"net/http"
+	"net/url"
+	"time"
+)
+
+import (
+	"github.com/go-resty/resty/v2"
+	"github.com/gogo/protobuf/types"
+	"github.com/opentrx/seata-golang/v2/pkg/apis"
+	"github.com/opentrx/seata-golang/v2/pkg/util/runtime"
+	"google.golang.org/grpc/metadata"
+)
+
+import (
+	"github.com/apache/dubbo-go-pixiu/pkg/logger"
+)
+
+const (
+	CommitRequestPath   = "tcc_commit_request_path"
+	RollbackRequestPath = "tcc_rollback_request_path"
+)
+
+func (f *Filter) branchCommunicate() {
+	for {
+		ctx := metadata.AppendToOutgoingContext(context.Background(), "addressing", f.conf.Addressing)
+		stream, err := f.resourceClient.BranchCommunicate(ctx)
+		if err != nil {
+			time.Sleep(time.Second)
+			continue
+		}
+
+		done := make(chan bool)
+		runtime.GoWithRecover(func() {
+			for {
+				select {
+				case _, ok := <-done:
+					if !ok {
+						return
+					}
+				case msg := <-f.branchMessages:
+					err := stream.Send(msg)
+					if err != nil {
+						return

Review comment:
       是否需要重试,否则commit msg 等会丢失 ?

##########
File path: pkg/filter/seata/filter.go
##########
@@ -0,0 +1,136 @@
+/*
+ * 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 seata
+
+import (
+	netHttp "net/http"
+	"strings"
+)
+
+import (
+	"github.com/opentrx/seata-golang/v2/pkg/apis"
+	"github.com/opentrx/seata-golang/v2/pkg/util/runtime"
+	"google.golang.org/grpc"
+)
+
+import (
+	"github.com/apache/dubbo-go-pixiu/pkg/common/extension/filter"
+	"github.com/apache/dubbo-go-pixiu/pkg/context/http"
+)
+
+const (
+	Kind = "dgp.filter.http.seata"
+
+	SEATA    = "seata"
+	XID      = "x_seata_xid"
+	BranchID = "x_seata_branch_id"
+)
+
+func init() {
+	filter.RegisterHttpFilter(&Plugin{})
+}
+
+type (
+	// MetricFilter is http Filter plugin.
+	Plugin struct {
+	}
+	// Filter is http Filter instance
+	Filter struct {
+		conf              *Seata
+		transactionInfos  map[string]*TransactionInfo
+		tccResources      map[string]*TCCResource
+		transactionClient apis.TransactionManagerServiceClient
+		resourceClient    apis.ResourceManagerServiceClient
+		branchMessages    chan *apis.BranchMessage
+	}
+)
+
+func (ap *Plugin) Kind() string {
+	return Kind
+}
+
+func (ap *Plugin) CreateFilter() (filter.HttpFilter, error) {
+	return &Filter{
+		conf:             &Seata{},
+		transactionInfos: make(map[string]*TransactionInfo),
+		tccResources:     make(map[string]*TCCResource),
+		branchMessages:   make(chan *apis.BranchMessage),
+	}, nil
+}
+
+func (f *Filter) Config() interface{} {
+	return f.conf
+}
+
+func (f *Filter) Apply() error {
+	conn, err := grpc.Dial(f.conf.ServerAddressing,
+		grpc.WithInsecure(),
+		grpc.WithKeepaliveParams(f.conf.GetClientParameters()))
+	if err != nil {
+		return err
+	}
+
+	f.transactionClient = apis.NewTransactionManagerServiceClient(conn)
+	f.resourceClient = apis.NewResourceManagerServiceClient(conn)
+
+	runtime.GoWithRecover(func() {
+		f.branchCommunicate()
+	}, nil)
+
+	for _, ti := range f.conf.TransactionInfos {
+		f.transactionInfos[ti.RequestPath] = ti
+	}
+
+	for _, r := range f.conf.TCCResources {
+		f.tccResources[r.PrepareRequestPath] = r
+	}
+	return nil
+}
+
+func (f *Filter) PrepareFilterChain(ctx *http.HttpContext) error {
+	ctx.AppendFilterFunc(f.Handle)
+	return nil
+}
+
+func (f *Filter) Handle(ctx *http.HttpContext) {
+	path := ctx.Request.URL.Path
+	method := ctx.Request.Method
+
+	if method != netHttp.MethodPost {
+		ctx.Next()
+		return
+	}
+
+	transactionInfo, found := f.transactionInfos[strings.ToLower(path)]
+	if found {
+		result := f.handleHttp1GlobalBegin(ctx, transactionInfo)
+		ctx.Next()
+		if result {
+			f.handleHttp1GlobalEnd(ctx)
+		}
+	}
+
+	tccResource, exists := f.tccResources[strings.ToLower(path)]
+	if exists {
+		result := f.handleHttp1BranchRegister(ctx, tccResource)
+		ctx.Next()
+		if result {
+			f.handleHttp1BranchEnd(ctx)
+		}
+	}

Review comment:
       是否存在 not found 和 not exists 的场景,需要ctx.Next直接放行

##########
File path: pkg/filter/seata/branch_transaction_service.go
##########
@@ -0,0 +1,240 @@
+/*
+ * 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 seata
+
+import (
+	"context"
+	"io"
+	"net/http"
+	"net/url"
+	"time"
+)
+
+import (
+	"github.com/go-resty/resty/v2"
+	"github.com/gogo/protobuf/types"
+	"github.com/opentrx/seata-golang/v2/pkg/apis"
+	"github.com/opentrx/seata-golang/v2/pkg/util/runtime"
+	"google.golang.org/grpc/metadata"
+)
+
+import (
+	"github.com/apache/dubbo-go-pixiu/pkg/logger"
+)
+
+const (
+	CommitRequestPath   = "tcc_commit_request_path"
+	RollbackRequestPath = "tcc_rollback_request_path"
+)
+
+func (f *Filter) branchCommunicate() {
+	for {
+		ctx := metadata.AppendToOutgoingContext(context.Background(), "addressing", f.conf.Addressing)
+		stream, err := f.resourceClient.BranchCommunicate(ctx)
+		if err != nil {
+			time.Sleep(time.Second)

Review comment:
       连接服务器异常,不断重试,是否要打印 warn 日志

##########
File path: pkg/filter/seata/branch_transaction_service.go
##########
@@ -0,0 +1,240 @@
+/*
+ * 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 seata
+
+import (
+	"context"
+	"io"
+	"net/http"
+	"net/url"
+	"time"
+)
+
+import (
+	"github.com/go-resty/resty/v2"
+	"github.com/gogo/protobuf/types"
+	"github.com/opentrx/seata-golang/v2/pkg/apis"
+	"github.com/opentrx/seata-golang/v2/pkg/util/runtime"
+	"google.golang.org/grpc/metadata"
+)
+
+import (
+	"github.com/apache/dubbo-go-pixiu/pkg/logger"
+)
+
+const (
+	CommitRequestPath   = "tcc_commit_request_path"
+	RollbackRequestPath = "tcc_rollback_request_path"
+)
+
+func (f *Filter) branchCommunicate() {
+	for {
+		ctx := metadata.AppendToOutgoingContext(context.Background(), "addressing", f.conf.Addressing)
+		stream, err := f.resourceClient.BranchCommunicate(ctx)
+		if err != nil {
+			time.Sleep(time.Second)
+			continue
+		}
+
+		done := make(chan bool)
+		runtime.GoWithRecover(func() {
+			for {
+				select {
+				case _, ok := <-done:
+					if !ok {
+						return
+					}
+				case msg := <-f.branchMessages:
+					err := stream.Send(msg)
+					if err != nil {
+						return
+					}
+				}
+			}
+		}, nil)
+
+		for {
+			msg, err := stream.Recv()
+			if err == io.EOF {
+				close(done)
+				break
+			}
+			if err != nil {
+				close(done)
+				break
+			}
+			switch msg.BranchMessageType {
+			case apis.TypeBranchCommit:
+				request := &apis.BranchCommitRequest{}
+				data := msg.GetMessage().GetValue()
+				err := request.Unmarshal(data)
+				if err != nil {
+					logger.Errorf(err.Error(), nil)
+					continue
+				}
+				response, err := branchCommit(context.Background(), request)

Review comment:
       看着branchCommit将error都转成response的code了,是否可以将error 返回值去掉




-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: notifications-unsubscribe@dubbo.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org



---------------------------------------------------------------------
To unsubscribe, e-mail: notifications-unsubscribe@dubbo.apache.org
For additional commands, e-mail: notifications-help@dubbo.apache.org