diff --git a/native/src/snappyclient/cpp/impl/ControlConnection.cpp b/native/src/snappyclient/cpp/impl/ControlConnection.cpp index 324665957..1a11d1520 100644 --- a/native/src/snappyclient/cpp/impl/ControlConnection.cpp +++ b/native/src/snappyclient/cpp/impl/ControlConnection.cpp @@ -335,7 +335,7 @@ void ControlConnection::failoverToAvailableHost( protocolFactory = 0; break; } - } catch (const TException& te) { + } catch (const TException&) { failedServers.insert(controlAddr); if (outTransport != nullptr) { outTransport->close(); diff --git a/native/tests/failoverTest_AddServer_AfterConn.cpp b/native/tests/failoverTest_AddServer_AfterConn.cpp new file mode 100644 index 000000000..76674ca01 --- /dev/null +++ b/native/tests/failoverTest_AddServer_AfterConn.cpp @@ -0,0 +1,104 @@ +#include "Connection.h" +#include +//#include +#include +using namespace io::snappydata::client; +using namespace std; + +void stopServer(string command) { + system(command.c_str()); +} + +int main(int argc, char **argv) { + Connection conn; + string snappyHomeDir(argv[1]); + string serverStopScript; + serverStopScript.append("cd ").append(snappyHomeDir).append( + "; ./sbin/snappy-server.sh stop -dir="); + string serverStartScript; + serverStartScript.append("cd ").append(snappyHomeDir).append( + "; ./sbin/snappy-server.sh start -locators=localhost:10334 -dir="); + try { + std::map properties; + properties.insert( + std::pair("load-balance", "true")); + conn.open("localhost", 1527, "app", "app", properties); + std::cout << "before stopping server- connected to :" + << conn.getCurrentHostAddress() << std::endl; + // creating dummy data + conn.execute("drop table if exists FailOverTest.test"); + conn.execute("drop schema if exists FailOverTest"); + conn.execute("create schema FailOverTest"); + conn.execute( + "create table FailOverTest.test(id int) as select id from range(120000)"); + auto count = conn.executeQuery("select * from FailOverTest.test"); + if (count > 0) { + std::cout << "Query execute successfully before server stop" + << std::endl; + } + //create directory for a new server 2 + string createServerDir; + createServerDir.append("cd ").append(snappyHomeDir).append( + "; mkdir ./work/localhost-server-2"); + system(createServerDir.c_str()); + //start a server 2 + string startNewServer; + startNewServer.append(serverStartScript).append( + "./work/localhost-server-2"); + system(startNewServer.c_str()); + //put on sleep + std::this_thread::sleep_for(std::chrono::seconds(50)); + // stop the connected server 1 + string stopRunningServer; + stopRunningServer.append(serverStopScript).append( + "./work/localhost-server-1"); + //stop the server + std::thread t1(stopServer, stopRunningServer); + t1.join(); + + conn.executeQuery("insert into FailOverTest.test (id) values(120001)"); + conn.execute("drop table if exists FailOverTest.test2"); + conn.execute( + "create table FailOverTest.test2(id int) as select id from range(120000)"); + count = conn.executeQuery("select * from FailOverTest.test2"); + if (count > 0) { + std::cout << "Query execute successfully after server stop" + << std::endl; + } + std::cout << "Test executed successfully connected to :" + << conn.getCurrentHostAddress() << std::endl; + conn.execute("drop table if exists FailOverTest.test"); + conn.execute("drop table if exists FailOverTest.test2"); + conn.execute("drop schema if exists FailOverTest"); + conn.close(); + //start the server 1 again + serverStartScript.append("./work/localhost-server-1"); + system(serverStartScript.c_str()); + std::this_thread::sleep_for(std::chrono::seconds(50)); + //stop server 2 + serverStopScript.append("./work/localhost-server-2"); + system(serverStopScript.c_str()); + //remove directory for a new server 2 + string removeServerDir; + removeServerDir.append("cd ").append(snappyHomeDir).append( + "; rm -r ./work/localhost-server-2"); + system(removeServerDir.c_str()); + } catch (SQLException& sqle) { + if (conn.isOpen()) conn.close(); + //start the server 1 again + serverStartScript.append("./work/localhost-server-1"); + system(serverStartScript.c_str()); + std::this_thread::sleep_for(std::chrono::seconds(50)); + //stop server 2 + serverStopScript.append("./work/localhost-server-2"); + system(serverStopScript.c_str()); + //remove directory for a new server 2 + string removeServerDir; + removeServerDir.append("cd ").append(snappyHomeDir).append( + "; rm -r ./work/localhost-server-2"); + system(removeServerDir.c_str()); + std::cout << "ExecuteQuery failed, throws exception" << std::endl; + sqle.printStackTrace(std::cout); + } +} + diff --git a/native/tests/failoverTest_AddServer_AfterConn.sh b/native/tests/failoverTest_AddServer_AfterConn.sh new file mode 100755 index 000000000..f0f163517 --- /dev/null +++ b/native/tests/failoverTest_AddServer_AfterConn.sh @@ -0,0 +1,38 @@ +#!/bin/sh + +SNAPPY_HOME_DIR="$1" +_testDir="$2" +_snappyNativeDir="$(dirname "$_testDir")" + +#echo "current working dir $_testDir" +#echo "Snappy Native dir- $_snappyNativeDir" + +distDir=${_snappyNativeDir}/dist + +THRIFT_VERSION=1.0.0-2 +BOOST_VERSION=1.65.1 + +thrftLibPath=${distDir}/thrift-${THRIFT_VERSION}/lin64/lib +boostLibPath=${distDir}/boost-${BOOST_VERSION}/lin64/lib +snappClientLibPath=${_snappyNativeDir}/build-artifacts/lin/libs/snappyclient/static/release + +LD_LIBRARY_PATH=$LD_LIBRARY_PATH:${thrftLibPath}:${boostLibPath}:${snappClientLibPath} + +echo "$LD_LIBRARY_PATH" +export LD_LIBRARY_PATH + +headerLoc=${_snappyNativeDir}/build-artifacts/lin/snappyclient/include +boostHeadeLoc=${distDir}/boost-${BOOST_VERSION}/include +thriftHeaderLoc=${distDir}/thrift-${THRIFT_VERSION}/include + +g++ -std=c++11 -I${headerLoc} -I${boostHeadeLoc} -I${thriftHeaderLoc} -O0 -g3 -Wall -c ${_testDir}/failoverTest_AddServer_AfterConn.cpp -o ${_testDir}/failoverTest_AddServer_AfterConn.o + +chmod 777 ${_testDir}/failoverTest_AddServer_AfterConn.o + +g++ -L${boostLibPath} -L${thrftLibPath} -L${snappClientLibPath} -o ${_testDir}/failoverTest_AddServer_AfterConn ${_testDir}/failoverTest_AddServer_AfterConn.o -lsnappyclient -lcrypto -lodbc -lpthread -lssl -lboost_chrono -lboost_date_time -lboost_filesystem -lboost_log -lboost_log_setup -lboost_system -lboost_thread -lthrift -lpthread -lrt -lgmp + +chmod 777 ${_testDir}/failoverTest_AddServer_AfterConn +cd ${_testDir} +./failoverTest_AddServer_AfterConn $SNAPPY_HOME_DIR + +rm failoverTest_AddServer_AfterConn failoverTest_AddServer_AfterConn.o diff --git a/native/tests/failoverTest_AddServer_BeforeConn.cpp b/native/tests/failoverTest_AddServer_BeforeConn.cpp new file mode 100644 index 000000000..a17e34a50 --- /dev/null +++ b/native/tests/failoverTest_AddServer_BeforeConn.cpp @@ -0,0 +1,115 @@ +#include "Connection.h" +#include +#include +#include +using namespace io::snappydata::client; +using namespace std; + +void stopServer(string command) { + system(command.c_str()); +} + +int main(int argc, char **argv) { + Connection conn; + string snappyHomeDir(argv[1]); + string serverStopScript; + serverStopScript.append("cd ").append(snappyHomeDir).append( + "; ./sbin/snappy-server.sh stop -dir="); + string serverStartScript; + serverStartScript.append("cd ").append(snappyHomeDir).append( + "; ./sbin/snappy-server.sh start -locators=localhost:10334 -dir="); + try { + //create directory for a new server 2 + string createServerDir; + createServerDir.append("cd ").append(snappyHomeDir).append( + "; mkdir ./work/localhost-server-2"); + system(createServerDir.c_str()); + //start a server 2 + string startNewServer; + startNewServer.append(serverStartScript).append( + "./work/localhost-server-2"); + system(startNewServer.c_str()); + + std::map properties; + properties.insert( + std::pair("load-balance", "true")); + + conn.open("localhost", 1527, "app", "app", properties); + std::cout << "before stopping server connected to :" + << conn.getCurrentHostAddress() << std::endl; + + // creating dummy data + conn.execute("drop table if exists FailOverTest.test"); + conn.execute("drop schema if exists FailOverTest"); + conn.execute("create schema FailOverTest"); + conn.execute( + "create table FailOverTest.test(id int) as select id from range(120000)"); + auto count = conn.executeQuery("select count(*) from FailOverTest.test"); + if (count > 0) { + std::cout << "Query execute successfully before server stop" + << std::endl; + } + + // stop the connected server 1 + int connectedPort = conn.getCurrentHostAddress().port; + string stopRunningServer; + if (connectedPort == 1529) { + stopRunningServer.append(serverStopScript).append( + "./work/localhost-server-2"); + } else { + stopRunningServer.append(serverStopScript).append( + "./work/localhost-server-1"); + } + + for (int i = 0; i < 5; ++i) { + if (i == 3) { + std::thread t1(stopServer, stopRunningServer); + t1.join(); + } + count = conn.executeQuery("select count(*) from FailOverTest.test"); + if (count > 0 && i > 3) { + std::cout << "Query execute successfully after server stop" + << std::endl; + + } else if (count > 0 && i > 3) { + std::cout << "Query execute successfully before server stop" + << std::endl; + } + + } + std::cout << "Test executed successfully connected to :" + << conn.getCurrentHostAddress() << std::endl; + + conn.close(); + //start the server 1 again + serverStartScript.append("./work/localhost-server-1"); + system(serverStartScript.c_str()); + std::this_thread::sleep_for(std::chrono::seconds(50)); + //stop server 2 + serverStopScript.append("./work/localhost-server-2"); + system(serverStopScript.c_str()); + //remove directory for a new server 2 + string removeServerDir; + removeServerDir.append("cd ").append(snappyHomeDir).append( + "; rm -r ./work/localhost-server-2"); + system(removeServerDir.c_str()); + } catch (SQLException& sqle) { + if (conn.isOpen()) conn.close(); + //start the server 1 again + serverStartScript.append("./work/localhost-server-1"); + system(serverStartScript.c_str()); + std::this_thread::sleep_for(std::chrono::seconds(50)); + //stop server 2 + serverStopScript.append("./work/localhost-server-2"); + system(serverStopScript.c_str()); + + //create directory for a new server 2 + string removeServerDir; + removeServerDir.append("cd ").append(snappyHomeDir).append( + "; rm -r ./work/localhost-server-2"); + system(removeServerDir.c_str()); + std::cout << "ExecuteQuery failed, throws exception" << std::endl; + sqle.printStackTrace(std::cout); + } +} + diff --git a/native/tests/failoverTest_AddServer_BeforeConn.sh b/native/tests/failoverTest_AddServer_BeforeConn.sh new file mode 100755 index 000000000..39cd81824 --- /dev/null +++ b/native/tests/failoverTest_AddServer_BeforeConn.sh @@ -0,0 +1,33 @@ +#!/bin/sh + +SNAPPY_HOME_DIR="$1" +_testDir="$2" +_snappyNativeDir="$(dirname "$_testDir")" + +distDir=${_snappyNativeDir}/dist +THRIFT_VERSION=1.0.0-2 +BOOST_VERSION=1.65.1 + +thrftLibPath=${distDir}/thrift-${THRIFT_VERSION}/lin64/lib +boostLibPath=${distDir}/boost-${BOOST_VERSION}/lin64/lib +snappClientLibPath=${_snappyNativeDir}/build-artifacts/lin/libs/snappyclient/static/release + +LD_LIBRARY_PATH=$LD_LIBRARY_PATH:${thrftLibPath}:${boostLibPath}:${snappClientLibPath} + +export LD_LIBRARY_PATH + +headerLoc=${_snappyNativeDir}/src/snappyclient/headers +boostHeadeLoc=${_snappyNativeDir}/dist/boost-${BOOST_VERSION}/include +thriftHeaderLoc=${distDir}/thrift-${THRIFT_VERSION}/include + +g++ -std=c++11 -I${headerLoc} -I${boostHeadeLoc} -I${thriftHeaderLoc} -O0 -g3 -Wall -c ${_testDir}/failoverTest_AddServer_BeforeConn.cpp -o ${_testDir}/failoverTest_AddServer_BeforeConn.o + +chmod 777 ${_testDir}/failoverTest_AddServer_BeforeConn.o + +g++ -L${boostLibPath} -L${thrftLibPath} -L${snappClientLibPath} -o ${_testDir}/failoverTest_AddServer_BeforeConn ${_testDir}/failoverTest_AddServer_BeforeConn.o -lsnappyclient -lcrypto -lodbc -lpthread -lssl -lboost_chrono -lboost_date_time -lboost_filesystem -lboost_log -lboost_log_setup -lboost_system -lboost_thread -lthrift -lpthread -lrt -lgmp + +chmod 777 ${_testDir}/failoverTest_AddServer_BeforeConn +cd ${_testDir} +./failoverTest_AddServer_BeforeConn $SNAPPY_HOME_DIR + +rm failoverTest_AddServer_BeforeConn failoverTest_AddServer_BeforeConn.o diff --git a/native/tests/failoverTest_NewServer.cpp b/native/tests/failoverTest_NewServer.cpp deleted file mode 100644 index 8f1748482..000000000 --- a/native/tests/failoverTest_NewServer.cpp +++ /dev/null @@ -1,58 +0,0 @@ -#include "Connection.h" -#include -#include -using namespace io::snappydata::client; -using namespace std; - -void stopServer(string command){ - system(command.c_str()); -} - -int main(int argc, char **argv) { - Connection conn; - try { - string snappyHomeDir(argv[1]); - string serverStopScript ; - serverStopScript.append("cd ").append(snappyHomeDir).append("; ./sbin/snappy-server.sh stop -dir="); - std::map properties; - properties.insert(std::pair("load-balance","true")); - - conn.open("localhost", 1527,"app","app",properties); - std::cout << "before stopping server connected to :"<< conn.getCurrentHostAddress() < 0 ) - { - std::cout << "Query execute successfully stopping server"< 0 ) - { - std::cout << "Query execute successfully after server stop"< -//#include -#include -using namespace io::snappydata::client; -using namespace std; - -void stopServer(string command){ - system(command.c_str()); -} - -int main(int argc, char **argv) { - Connection conn; - try { - string snappyHomeDir(argv[1]); - string serverStopScript ; - serverStopScript.append("cd ").append(snappyHomeDir).append("; ./sbin/snappy-server.sh stop -dir="); - std::map properties; - properties.insert(std::pair("load-balance","true")); - - conn.open("localhost", 1527,"app","app",properties); - std::cout << "before stopping server- connected to :"<< conn.getCurrentHostAddress() < 0 ) - { - std::cout << "Query execute successfully before server stop"< properties; - properties.insert(std::pair("load-balance","true")); - properties.insert(std::pair("route-query","false")); + properties.insert( + std::pair("load-balance", "true")); + properties.insert( + std::pair("route-query", "false")); + + conn.open(locatorIpAddr, stoi(locatorPort), "app", "app", properties); + std::cout << "before stopping server- connected to :" + << conn.getCurrentHostAddress() << std::endl; + + for (int i = 0; i < 10; ++i) { + try { + std::this_thread::sleep_for(std::chrono::seconds(stoi(timeDuration))); + + auto count = conn.executeQuery("select * from app.orders"); - conn.open(locatorIpAddr, stoi(locatorPort),"app","app",properties); - std::cout << "before stopping server- connected to :"<< conn.getCurrentHostAddress() <