Skip to content

Commit

Permalink
fix: decode clickhouse exception when http protocol
Browse files Browse the repository at this point in the history
  • Loading branch information
YenchangChan committed Dec 6, 2023
1 parent 3ae2501 commit 024c26c
Show file tree
Hide file tree
Showing 5 changed files with 39 additions and 0 deletions.
29 changes: 29 additions & 0 deletions common/ck.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import (
"encoding/hex"
"encoding/json"
"fmt"
"regexp"
"strconv"
"strings"
"sync"
Expand Down Expand Up @@ -339,3 +340,31 @@ WHERE match(engine, 'Distributed') AND (database = '%s') AND ((dist = '%s') OR (

return local, dist, nil
}

func ClikHouseExceptionDecode(err error) error {
var e *clickhouse.Exception
// TCP protocol
if errors.As(err, &e) {
return e
}
// HTTP protocol
if strings.HasPrefix(err.Error(), "clickhouse [execute]::") {
r := regexp.MustCompile(`.*Code:\s+(\d+)\.\s+(.*)`)
matchs := r.FindStringSubmatch(err.Error())
if len(matchs) != 3 {
return err
}
code, err2 := strconv.Atoi(matchs[1])
if err2 != nil {
return err
}
message := matchs[2]
e = &clickhouse.Exception{
Code: int32(code),
Message: message,
}
return e
}

return err
}
5 changes: 5 additions & 0 deletions controller/clickhouse.go
Original file line number Diff line number Diff line change
Expand Up @@ -441,6 +441,7 @@ func (controller *ClickHouseController) CreateDistTableOnLogic(c *gin.Context) {
LogicCluster: *conf.LogicCluster,
}
if err = ckService.CreateDistTblOnLogic(&params); err != nil {
err = common.ClikHouseExceptionDecode(err)
var exception *client.Exception
if errors.As(err, &exception) {
if exception.Code == 60 && cluster != conf.Cluster {
Expand Down Expand Up @@ -1769,6 +1770,7 @@ func (controller *ClickHouseController) StartNode(c *gin.Context) {
ckService.InitCkService()
err := ckService.FetchSchemerFromOtherNode(host, conf.Password)
if err != nil {
err = common.ClikHouseExceptionDecode(err)
var exception *client.Exception
if errors.As(err, &exception) {
if exception.Code == 253 {
Expand Down Expand Up @@ -1964,6 +1966,7 @@ func (controller *ClickHouseController) GetTableMetric(c *gin.Context) {
metrics, err := clickhouse.GetCkTableMetrics(&conf, database, cols)
if err != nil {
gotError = true
err = common.ClikHouseExceptionDecode(err)
var exception *client.Exception
if errors.As(err, &exception) {
if exception.Code == 60 {
Expand Down Expand Up @@ -2011,6 +2014,7 @@ func (controller *ClickHouseController) GetOpenSessions(c *gin.Context) {
sessions, err := clickhouse.GetCkOpenSessions(&conf, limit)
if err != nil {
gotError = true
err = common.ClikHouseExceptionDecode(err)
var exception *client.Exception
if errors.As(err, &exception) {
if exception.Code == 60 {
Expand Down Expand Up @@ -2104,6 +2108,7 @@ func (controller *ClickHouseController) GetSlowSessions(c *gin.Context) {
sessions, err := clickhouse.GetCkSlowSessions(&conf, cond)
if err != nil {
gotError = true
err = common.ClikHouseExceptionDecode(err)
var exception *client.Exception
if errors.As(err, &exception) {
if exception.Code == 60 {
Expand Down
2 changes: 2 additions & 0 deletions router/v1.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import (

"github.com/ClickHouse/clickhouse-go/v2"
"github.com/gin-gonic/gin"
"github.com/housepower/ckman/common"
"github.com/housepower/ckman/config"
"github.com/housepower/ckman/controller"
"github.com/housepower/ckman/log"
Expand All @@ -29,6 +30,7 @@ func WrapMsg(c *gin.Context, retCode string, entity interface{}) {
if retCode != model.E_SUCCESS {
log.Logger.Errorf("%s %s return %s, %v", c.Request.Method, c.Request.RequestURI, retCode, entity)
if err, ok := entity.(error); ok {
err = common.ClikHouseExceptionDecode(err)
var exception *clickhouse.Exception
if errors.As(err, &exception) {
retCode = fmt.Sprintf("%04d", exception.Code)
Expand Down
2 changes: 2 additions & 0 deletions router/v2.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import (

"github.com/ClickHouse/clickhouse-go/v2"
"github.com/gin-gonic/gin"
"github.com/housepower/ckman/common"
"github.com/housepower/ckman/config"
"github.com/housepower/ckman/controller"
"github.com/housepower/ckman/log"
Expand All @@ -29,6 +30,7 @@ func WrapMsg2(c *gin.Context, retCode string, entity interface{}) {
if retCode != model.E_SUCCESS {
log.Logger.Errorf("%s %s return %s, %v", c.Request.Method, c.Request.RequestURI, retCode, entity)
if err, ok := entity.(error); ok {
err = common.ClikHouseExceptionDecode(err)
var exception *clickhouse.Exception
if errors.As(err, &exception) {
retCode = fmt.Sprintf("%04d", exception.Code)
Expand Down
1 change: 1 addition & 0 deletions service/cron/clickhouse.go
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,7 @@ AND (cluster != '%s')`, cluster)
_ = rows.Scan(&database, &table, &logic, &local)
err = syncLogicbyTable(clusters, database, local)
if err != nil {
err = common.ClikHouseExceptionDecode(err)
var exception *client.Exception
if errors.As(err, &exception) {
if exception.Code == 60 {
Expand Down

0 comments on commit 024c26c

Please sign in to comment.