Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
36 changes: 20 additions & 16 deletions client/session.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@ package client

import (
"bytes"
"container/list"
"context"
"encoding/binary"
"errors"
Expand Down Expand Up @@ -63,15 +62,14 @@ type Session struct {
trans thrift.TTransport
requestStatementId int64
protocolFactory thrift.TProtocolFactory
endPointList []endPoint
}

type endPoint struct {
Host string
Port string
}

var endPointList = list.New()

func (s *Session) Open(enableRPCCompression bool, connectionTimeoutInMs int) error {
if s.config.FetchSize <= 0 {
s.config.FetchSize = DefaultFetchSize
Expand Down Expand Up @@ -1078,24 +1076,29 @@ func (s *Session) GetSessionId() int64 {
}

func NewSession(config *Config) Session {
endPoint := endPoint{}
endPoint.Host = config.Host
endPoint.Port = config.Port
endPointList.PushBack(endPoint)
return Session{config: config}
endPointList := []endPoint{{
Host: config.Host,
Port: config.Port,
}}
return Session{
config: config,
endPointList: endPointList,
}
}

func NewClusterSession(clusterConfig *ClusterConfig) (Session, error) {
session := Session{}
node := endPoint{}
session.endPointList = make([]endPoint, len(clusterConfig.NodeUrls))
for i := 0; i < len(clusterConfig.NodeUrls); i++ {
node := endPoint{}
node.Host = strings.Split(clusterConfig.NodeUrls[i], ":")[0]
node.Port = strings.Split(clusterConfig.NodeUrls[i], ":")[1]
endPointList.PushBack(node)
session.endPointList[i] = node
}
var err error
for e := endPointList.Front(); e != nil; e = e.Next() {
session.trans = thrift.NewTSocketConf(net.JoinHostPort(e.Value.(endPoint).Host, e.Value.(endPoint).Port), &thrift.TConfiguration{
for i := range session.endPointList {
ep := session.endPointList[i]
session.trans = thrift.NewTSocketConf(net.JoinHostPort(ep.Host, ep.Port), &thrift.TConfiguration{
ConnectTimeout: time.Duration(0), // Use 0 for no timeout
})
// session.trans = thrift.NewTFramedTransport(session.trans) // deprecated
Expand All @@ -1106,7 +1109,7 @@ func NewClusterSession(clusterConfig *ClusterConfig) (Session, error) {
if err != nil {
log.Println(err)
} else {
session.config = getConfig(e.Value.(endPoint).Host, e.Value.(endPoint).Port,
session.config = getConfig(ep.Host, ep.Port,
clusterConfig.UserName, clusterConfig.Password, clusterConfig.FetchSize, clusterConfig.TimeZone, clusterConfig.ConnectRetryMax)
break
}
Expand Down Expand Up @@ -1181,13 +1184,14 @@ func (s *Session) reconnect() bool {
var connectedSuccess = false

for i := 0; i < s.config.ConnectRetryMax; i++ {
for e := endPointList.Front(); e != nil; e = e.Next() {
err = s.initClusterConn(e.Value.(endPoint))
for i := range s.endPointList {
ep := s.endPointList[i]
err = s.initClusterConn(ep)
if err == nil {
connectedSuccess = true
break
} else {
log.Println("Connection refused:", e.Value.(endPoint))
log.Println("Connection refused:", ep)
}
}
if connectedSuccess {
Expand Down
10 changes: 10 additions & 0 deletions test/e2e/e2e_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,16 @@ func (s *e2eTestSuite) checkError(status *common.TSStatus, err error) {
}
}

func (s *e2eTestSuite) Test_WrongURL() {
clusterConfig := client.ClusterConfig{
NodeUrls: strings.Split("iotdb1:6667", ","),
UserName: "root",
Password: "root",
}
_, err := client.NewClusterSession(&clusterConfig)
s.Require().Error(err)
}

func (s *e2eTestSuite) Test_CreateTimeseries() {
var (
path = "root.tsg1.dev1.status"
Expand Down