Skip to content

Commit

Permalink
Rollback1 (apache#360)
Browse files Browse the repository at this point in the history
* feat: rollback

* feat: add at rollback sampel

* feat: update
  • Loading branch information
georgehao committed Dec 5, 2022
1 parent 050b13d commit 9e10939
Show file tree
Hide file tree
Showing 7 changed files with 18 additions and 15 deletions.
6 changes: 4 additions & 2 deletions sample/at/gin/client/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,10 @@ package main

import (
"context"
"errors"
"flag"
"fmt"
"net/http"
"time"

"github.com/parnurzeal/gorequest"
Expand Down Expand Up @@ -57,8 +59,8 @@ func updateData(ctx context.Context) (re error) {
request.Post(serverIpPort+"/updateDataSuccess").
Set(constant.XidKey, tm.GetXID(ctx)).
End(func(response gorequest.Response, body string, errs []error) {
if len(errs) != 0 {
re = errs[0]
if response.StatusCode != http.StatusOK {
re = errors.New("update data fail")
}
})
return
Expand Down
2 changes: 1 addition & 1 deletion sample/at/gin/server/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ func main() {
r.POST("/updateDataSuccess", func(c *gin.Context) {
log.Infof("get tm updateData")
if err := updateDataSuccess(c); err != nil {
c.JSON(http.StatusOK, "updateData failure")
c.JSON(http.StatusBadRequest, "updateData failure")
return
}
c.JSON(http.StatusOK, "updateData ok")
Expand Down
13 changes: 7 additions & 6 deletions sample/at/rollback/client/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,10 @@ package main

import (
"context"
"errors"
"flag"
"fmt"
"net/http"
"time"

"github.com/parnurzeal/gorequest"
Expand Down Expand Up @@ -58,18 +60,17 @@ func updateData(ctx context.Context) (re error) {
request.Post(serverIpPort+"/updateDataSuccess").
Set(constant.XidKey, tm.GetXID(ctx)).
End(func(response gorequest.Response, body string, errs []error) {
if len(errs) != 0 {
re = errs[0]
if response.StatusCode != http.StatusOK {
re = errors.New("update data fail")
}
})

request.Post(serverIpPort2+"/updateDataFail").
Set(constant.XidKey, tm.GetXID(ctx)).
End(func(response gorequest.Response, body string, errs []error) {
if len(errs) != 0 {
re = errs[0]
End(func(response gorequest.Response, body string, errs1 []error) {
if response.StatusCode != http.StatusOK {
re = errors.New("update data fail")
}
})

return
}
2 changes: 1 addition & 1 deletion sample/at/rollback/server/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ func main() {
r.POST("/updateDataSuccess", func(c *gin.Context) {
log.Infof("get tm updateData")
if err := updateDataSuccess(c); err != nil {
c.JSON(http.StatusOK, "updateData failure")
c.JSON(http.StatusBadRequest, "updateData failure")
return
}
c.JSON(http.StatusOK, "updateData ok")
Expand Down
4 changes: 2 additions & 2 deletions sample/at/rollback/server/service.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,13 +43,13 @@ func updateDataSuccess(ctx context.Context) error {
ret, err := db.ExecContext(ctx, sql, fmt.Sprintf("NewDescs100-%d", time.Now().UnixMilli()), 1)
if err != nil {
fmt.Printf("update failed, err:%v\n", err)
return nil
return err
}

rows, err := ret.RowsAffected()
if err != nil {
fmt.Printf("update failed, err:%v\n", err)
return nil
return err
}
fmt.Printf("update success: %d.\n", rows)
return nil
Expand Down
2 changes: 1 addition & 1 deletion sample/at/rollback/server2/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ func main() {
r.POST("/updateDataFail", func(c *gin.Context) {
log.Infof("get tm updateData")
if err := updateDataFail(c); err != nil {
c.JSON(http.StatusOK, "updateData failure")
c.JSON(http.StatusBadRequest, "updateData failure")
return
}
c.JSON(http.StatusOK, "updateData ok")
Expand Down
4 changes: 2 additions & 2 deletions sample/at/rollback/server2/service.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,13 +43,13 @@ func updateDataFail(ctx context.Context) error {
ret, err := db.ExecContext(ctx, sql, fmt.Sprintf("NewDescs1-%d", time.Now().UnixMilli()), 10000)
if err != nil {
fmt.Printf("update failed, err:%v\n", err)
return nil
return err
}

rows, err := ret.RowsAffected()
if err != nil {
fmt.Printf("update failed, err:%v\n", err)
return nil
return err
}
fmt.Printf("update success: %d.\n", rows)
return nil
Expand Down

0 comments on commit 9e10939

Please sign in to comment.