Skip to content

Commit

Permalink
Merge pull request #367 from taosdata/enh/xftan/TD-33164-3.0
Browse files Browse the repository at this point in the history
enh: stmt2 use result response change result_id to id and fix version action
  • Loading branch information
zitsen authored Dec 10, 2024
2 parents 13e5919 + 8fd893f commit 1b2f232
Show file tree
Hide file tree
Showing 6 changed files with 50 additions and 12 deletions.
14 changes: 13 additions & 1 deletion controller/ws/ws/handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -208,7 +208,19 @@ func (h *messageHandler) handleMessage(session *melody.Session, data []byte) {
// no need connection actions
switch request.Action {
case wstool.ClientVersion:
wstool.WSWriteVersion(session, h.logger)
action = wstool.ClientVersion
var req versionRequest
if err := json.Unmarshal(request.Args, &req); err != nil {
h.logger.Errorf("unmarshal version request error, request:%s, err:%s", request.Args, err)
reqID := getReqID(request.Args)
commonErrorResponse(ctx, session, h.logger, action, reqID, 0xffff, "unmarshal version request error")
return
}
logger := h.logger.WithFields(logrus.Fields{
actionKey: action,
config.ReqIDKey: req.ReqID,
})
h.version(ctx, session, action, req, logger, log.IsDebug())
return
case Connect:
action = Connect
Expand Down
6 changes: 6 additions & 0 deletions controller/ws/ws/handler_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,12 @@ func Test_WrongJsonProtocol(t *testing.T) {
args: nil,
errorPrefix: "request no action",
},
{
name: "version with wrong args",
action: wstool.ClientVersion,
args: "wrong",
errorPrefix: "unmarshal version request error",
},
{
name: "connect with wrong args",
action: Connect,
Expand Down
25 changes: 25 additions & 0 deletions controller/ws/ws/misc.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import (
"github.com/taosdata/taosadapter/v3/db/syncinterface"
errors2 "github.com/taosdata/taosadapter/v3/driver/errors"
"github.com/taosdata/taosadapter/v3/tools/melody"
"github.com/taosdata/taosadapter/v3/version"
)

type getCurrentDBRequest struct {
Expand Down Expand Up @@ -65,3 +66,27 @@ func (h *messageHandler) getServerInfo(ctx context.Context, session *melody.Sess
}
wstool.WSWriteJson(session, logger, resp)
}

type versionRequest struct {
ReqID uint64 `json:"req_id"`
}

type versionResp struct {
Code int `json:"code"`
Message string `json:"message"`
Action string `json:"action"`
ReqID uint64 `json:"req_id"`
Timing int64 `json:"timing"`
Version string `json:"version"`
}

func (h *messageHandler) version(ctx context.Context, session *melody.Session, action string, req versionRequest, logger *logrus.Entry, isDebug bool) {
logger.Trace("get version")
resp := &versionResp{
Action: action,
ReqID: req.ReqID,
Timing: wstool.GetDuration(ctx),
Version: version.TaosClientVersion,
}
wstool.WSWriteJson(session, logger, resp)
}
4 changes: 2 additions & 2 deletions controller/ws/ws/stmt2.go
Original file line number Diff line number Diff line change
Expand Up @@ -200,7 +200,7 @@ type stmt2UseResultResponse struct {
ReqID uint64 `json:"req_id"`
Timing int64 `json:"timing"`
StmtID uint64 `json:"stmt_id"`
ResultID uint64 `json:"result_id"`
ID uint64 `json:"id"`
FieldsCount int `json:"fields_count"`
FieldsNames []string `json:"fields_names"`
FieldsTypes jsontype.JsonUint8 `json:"fields_types"`
Expand All @@ -227,7 +227,7 @@ func (h *messageHandler) stmt2UseResult(ctx context.Context, session *melody.Ses
ReqID: req.ReqID,
Timing: wstool.GetDuration(ctx),
StmtID: req.StmtID,
ResultID: idx,
ID: idx,
FieldsCount: fieldsCount,
FieldsNames: rowsHeader.ColNames,
FieldsTypes: rowsHeader.ColTypes,
Expand Down
6 changes: 3 additions & 3 deletions controller/ws/ws/stmt2_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -510,7 +510,7 @@ func Stmt2Query(t *testing.T, db string, prepareDataSql []string) {
assert.Equal(t, 0, useResultResp.Code, useResultResp.Message)

// fetch
fetchReq := fetchRequest{ReqID: 8, ID: useResultResp.ResultID}
fetchReq := fetchRequest{ReqID: 8, ID: useResultResp.ID}
resp, err = doWebSocket(ws, WSFetch, &fetchReq)
assert.NoError(t, err)
var fetchResp fetchResponse
Expand All @@ -521,7 +521,7 @@ func Stmt2Query(t *testing.T, db string, prepareDataSql []string) {
assert.Equal(t, 1, fetchResp.Rows)

// fetch block
fetchBlockReq := fetchBlockRequest{ReqID: 9, ID: useResultResp.ResultID}
fetchBlockReq := fetchBlockRequest{ReqID: 9, ID: useResultResp.ID}
fetchBlockResp, err := doWebSocket(ws, WSFetchBlock, &fetchBlockReq)
assert.NoError(t, err)
_, blockResult := parseblock.ParseBlock(fetchBlockResp[8:], useResultResp.FieldsTypes, fetchResp.Rows, useResultResp.Precision)
Expand All @@ -531,7 +531,7 @@ func Stmt2Query(t *testing.T, db string, prepareDataSql []string) {
assert.Equal(t, float32(0.31), blockResult[0][3])

// free result
freeResultReq, _ := json.Marshal(freeResultRequest{ReqID: 10, ID: useResultResp.ResultID})
freeResultReq, _ := json.Marshal(freeResultRequest{ReqID: 10, ID: useResultResp.ID})
a, _ := json.Marshal(Request{Action: WSFreeResult, Args: freeResultReq})
err = ws.WriteMessage(websocket.TextMessage, a)
assert.NoError(t, err)
Expand Down
7 changes: 1 addition & 6 deletions controller/ws/ws/ws_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -131,11 +131,6 @@ func doWebSocketWithoutResp(ws *websocket.Conn, action string, arg interface{})
return nil
}

type versionResponse struct {
commonResp
Version string
}

func TestVersion(t *testing.T) {
s := httptest.NewServer(router)
defer s.Close()
Expand All @@ -150,7 +145,7 @@ func TestVersion(t *testing.T) {
}()
resp, err := doWebSocket(ws, wstool.ClientVersion, nil)
assert.NoError(t, err)
var versionResp versionResponse
var versionResp versionResp
err = json.Unmarshal(resp, &versionResp)
assert.NoError(t, err)
assert.Equal(t, 0, versionResp.Code, versionResp.Message)
Expand Down

0 comments on commit 1b2f232

Please sign in to comment.