|
|
@ -6,6 +6,7 @@ import ( |
|
|
"strings" |
|
|
"strings" |
|
|
|
|
|
|
|
|
"github.com/gin-gonic/gin" |
|
|
"github.com/gin-gonic/gin" |
|
|
|
|
|
"github.com/go-resty/resty/v2" |
|
|
"github.com/yedf/dtm/common" |
|
|
"github.com/yedf/dtm/common" |
|
|
) |
|
|
) |
|
|
|
|
|
|
|
|
@ -13,18 +14,43 @@ type M = map[string]interface{} |
|
|
|
|
|
|
|
|
var e2p = common.E2P |
|
|
var e2p = common.E2P |
|
|
|
|
|
|
|
|
type XaGlobalFunc func(gid string) error |
|
|
type XaGlobalFunc func(xa *Xa) error |
|
|
|
|
|
|
|
|
type XaLocalFunc func(db *common.DB) error |
|
|
type XaLocalFunc func(db *common.DB, xa *Xa) error |
|
|
|
|
|
|
|
|
type Xa struct { |
|
|
type XaClient struct { |
|
|
Server string |
|
|
Server string |
|
|
Conf map[string]string |
|
|
Conf map[string]string |
|
|
CallbackUrl string |
|
|
CallbackUrl string |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
func NewXa(server string, mysqlConf map[string]string, app *gin.Engine, callbackUrl string) *Xa { |
|
|
type Xa struct { |
|
|
xa := &Xa{ |
|
|
IDGenerator |
|
|
|
|
|
Gid string |
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
func (x *Xa) GetParams(branchID string) common.MS { |
|
|
|
|
|
return common.MS{ |
|
|
|
|
|
"gid": x.Gid, |
|
|
|
|
|
"trans_type": "xa", |
|
|
|
|
|
"branch_id": branchID, |
|
|
|
|
|
"branch_type": "action", |
|
|
|
|
|
} |
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
func XaFromReq(c *gin.Context) *Xa { |
|
|
|
|
|
return &Xa{ |
|
|
|
|
|
Gid: c.Query("gid"), |
|
|
|
|
|
IDGenerator: IDGenerator{parentID: c.Query("branch_id")}, |
|
|
|
|
|
} |
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
func (x *Xa) NewXaBranchID() string { |
|
|
|
|
|
return x.Gid + "-" + x.NewBranchID() |
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
func NewXaClient(server string, mysqlConf map[string]string, app *gin.Engine, callbackUrl string) *XaClient { |
|
|
|
|
|
xa := &XaClient{ |
|
|
Server: server, |
|
|
Server: server, |
|
|
Conf: mysqlConf, |
|
|
Conf: mysqlConf, |
|
|
CallbackUrl: callbackUrl, |
|
|
CallbackUrl: callbackUrl, |
|
|
@ -43,10 +69,11 @@ func NewXa(server string, mysqlConf map[string]string, app *gin.Engine, callback |
|
|
common.MustUnmarshal(b, &req) |
|
|
common.MustUnmarshal(b, &req) |
|
|
tx, my := common.DbAlone(xa.Conf) |
|
|
tx, my := common.DbAlone(xa.Conf) |
|
|
defer my.Close() |
|
|
defer my.Close() |
|
|
|
|
|
branchID := req.Gid + "-" + req.BranchID |
|
|
if req.Action == "commit" { |
|
|
if req.Action == "commit" { |
|
|
tx.Must().Exec(fmt.Sprintf("xa commit '%s'", req.BranchID)) |
|
|
tx.Must().Exec(fmt.Sprintf("xa commit '%s'", branchID)) |
|
|
} else if req.Action == "rollback" { |
|
|
} else if req.Action == "rollback" { |
|
|
tx.Must().Exec(fmt.Sprintf("xa rollback '%s'", req.BranchID)) |
|
|
tx.Must().Exec(fmt.Sprintf("xa rollback '%s'", branchID)) |
|
|
} else { |
|
|
} else { |
|
|
panic(fmt.Errorf("unknown action: %s", req.Action)) |
|
|
panic(fmt.Errorf("unknown action: %s", req.Action)) |
|
|
} |
|
|
} |
|
|
@ -55,28 +82,31 @@ func NewXa(server string, mysqlConf map[string]string, app *gin.Engine, callback |
|
|
return xa |
|
|
return xa |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
func (xa *Xa) XaLocalTransaction(gid string, transFunc XaLocalFunc) (rerr error) { |
|
|
func (xc *XaClient) XaLocalTransaction(c *gin.Context, transFunc XaLocalFunc) (rerr error) { |
|
|
defer common.P2E(&rerr) |
|
|
defer common.P2E(&rerr) |
|
|
branchID := GenGid(xa.Server) |
|
|
xa := XaFromReq(c) |
|
|
tx, my := common.DbAlone(xa.Conf) |
|
|
branchId := xa.NewBranchID() |
|
|
|
|
|
xaBranch := xa.Gid + "-" + branchId |
|
|
|
|
|
tx, my := common.DbAlone(xc.Conf) |
|
|
defer func() { my.Close() }() |
|
|
defer func() { my.Close() }() |
|
|
tx.Must().Exec(fmt.Sprintf("XA start '%s'", branchID)) |
|
|
tx.Must().Exec(fmt.Sprintf("XA start '%s'", xaBranch)) |
|
|
err := transFunc(tx) |
|
|
err := transFunc(tx, xa) |
|
|
e2p(err) |
|
|
e2p(err) |
|
|
resp, err := common.RestyClient.R(). |
|
|
resp, err := common.RestyClient.R(). |
|
|
SetBody(&M{"gid": gid, "branch_id": branchID, "trans_type": "xa", "status": "prepared", "url": xa.CallbackUrl}). |
|
|
SetBody(&M{"gid": xa.Gid, "branch_id": branchId, "trans_type": "xa", "status": "prepared", "url": xc.CallbackUrl}). |
|
|
Post(xa.Server + "/registerXaBranch") |
|
|
Post(xc.Server + "/registerXaBranch") |
|
|
e2p(err) |
|
|
e2p(err) |
|
|
if !strings.Contains(resp.String(), "SUCCESS") { |
|
|
if !strings.Contains(resp.String(), "SUCCESS") { |
|
|
e2p(fmt.Errorf("unknown server response: %s", resp.String())) |
|
|
e2p(fmt.Errorf("unknown server response: %s", resp.String())) |
|
|
} |
|
|
} |
|
|
tx.Must().Exec(fmt.Sprintf("XA end '%s'", branchID)) |
|
|
tx.Must().Exec(fmt.Sprintf("XA end '%s'", xaBranch)) |
|
|
tx.Must().Exec(fmt.Sprintf("XA prepare '%s'", branchID)) |
|
|
tx.Must().Exec(fmt.Sprintf("XA prepare '%s'", xaBranch)) |
|
|
return nil |
|
|
return nil |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
func (xa *Xa) XaGlobalTransaction(transFunc XaGlobalFunc) (gid string, rerr error) { |
|
|
func (xc *XaClient) XaGlobalTransaction(transFunc XaGlobalFunc) (gid string, rerr error) { |
|
|
gid = GenGid(xa.Server) |
|
|
xa := Xa{IDGenerator: IDGenerator{}, Gid: GenGid(xc.Server)} |
|
|
|
|
|
gid = xa.Gid |
|
|
data := &M{ |
|
|
data := &M{ |
|
|
"gid": gid, |
|
|
"gid": gid, |
|
|
"trans_type": "xa", |
|
|
"trans_type": "xa", |
|
|
@ -84,21 +114,34 @@ func (xa *Xa) XaGlobalTransaction(transFunc XaGlobalFunc) (gid string, rerr erro |
|
|
defer func() { |
|
|
defer func() { |
|
|
x := recover() |
|
|
x := recover() |
|
|
if x != nil { |
|
|
if x != nil { |
|
|
_, _ = common.RestyClient.R().SetBody(data).Post(xa.Server + "/abort") |
|
|
_, _ = common.RestyClient.R().SetBody(data).Post(xc.Server + "/abort") |
|
|
rerr = x.(error) |
|
|
rerr = x.(error) |
|
|
} |
|
|
} |
|
|
}() |
|
|
}() |
|
|
resp, rerr := common.RestyClient.R().SetBody(data).Post(xa.Server + "/prepare") |
|
|
resp, rerr := common.RestyClient.R().SetBody(data).Post(xc.Server + "/prepare") |
|
|
e2p(rerr) |
|
|
e2p(rerr) |
|
|
if !strings.Contains(resp.String(), "SUCCESS") { |
|
|
if !strings.Contains(resp.String(), "SUCCESS") { |
|
|
panic(fmt.Errorf("unexpected result: %s", resp.String())) |
|
|
panic(fmt.Errorf("unexpected result: %s", resp.String())) |
|
|
} |
|
|
} |
|
|
rerr = transFunc(gid) |
|
|
rerr = transFunc(&xa) |
|
|
e2p(rerr) |
|
|
e2p(rerr) |
|
|
resp, rerr = common.RestyClient.R().SetBody(data).Post(xa.Server + "/submit") |
|
|
resp, rerr = common.RestyClient.R().SetBody(data).Post(xc.Server + "/submit") |
|
|
e2p(rerr) |
|
|
e2p(rerr) |
|
|
if !strings.Contains(resp.String(), "SUCCESS") { |
|
|
if !strings.Contains(resp.String(), "SUCCESS") { |
|
|
panic(fmt.Errorf("unexpected result: %s", resp.String())) |
|
|
panic(fmt.Errorf("unexpected result: %s", resp.String())) |
|
|
} |
|
|
} |
|
|
return |
|
|
return |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
func (xa *Xa) CallBranch(body interface{}, url string) (*resty.Response, error) { |
|
|
|
|
|
branchID := xa.NewBranchID() |
|
|
|
|
|
return common.RestyClient.R(). |
|
|
|
|
|
SetBody(body). |
|
|
|
|
|
SetQueryParams(common.MS{ |
|
|
|
|
|
"gid": xa.Gid, |
|
|
|
|
|
"branch_id": branchID, |
|
|
|
|
|
"trans_type": "xa", |
|
|
|
|
|
"branch_type": "action", |
|
|
|
|
|
}). |
|
|
|
|
|
Post(url) |
|
|
|
|
|
} |
|
|
|