Skip to content

Commit 0e54a8a

Browse files
author
Brendan Plougonven
committed
Fix bug when reconnecting to Sparrow
1 parent 4b82f47 commit 0e54a8a

File tree

6 files changed

+62
-15
lines changed

6 files changed

+62
-15
lines changed

storage/sparrow/api/thread.h

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,8 @@ class Thread {
6565
Guard guard(lock_);
6666
stop_ = true;
6767
notifyStop();
68-
stopCond_.wait(2000, true);
68+
join();
69+
stop_ = false;
6970
}
7071
}
7172

@@ -88,6 +89,7 @@ class Thread {
8889
private:
8990

9091
static void* handler(void *p) {
92+
PRINT_DBUG("Thread started");
9193
Thread* thread = (Thread*)p;
9294
thread->running_ = true;
9395
thread->threadId_ = my_thread_self();

storage/sparrow/api_test/all_types.cpp

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,10 @@ void TestAlltypes::run() {
2323
printf("OK\n");
2424

2525
runMasterFileTest( table.get() );
26+
if (reconnect_) {
27+
disconnect();
28+
connect();
29+
}
2630

2731
runGetTableTest( table.get() );
2832

storage/sparrow/api_test/all_types.h

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -231,6 +231,8 @@ class TestAlltypes : public Test {
231231
void run();
232232

233233
public:
234+
void setReconnect(bool reconnect) { reconnect_ = reconnect; }
235+
234236
void runMasterFileTest(const Table*);
235237
void runGetTableTest(const Table* table);
236238
void runDisableCoalescingGlobTest(bool loop=false);
@@ -239,6 +241,9 @@ class TestAlltypes : public Test {
239241
void sendSampleData(const Table* table);
240242
void sendErrorData(const Table* table);
241243
void sendDataFlow(const Table* table);
244+
245+
private:
246+
bool reconnect_{ false };
242247
};
243248

244249
#endif// _spw_test_all_types_h

storage/sparrow/api_test/common.cpp

Lines changed: 43 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,8 @@ using namespace Sparrow;
1414
//////////////////////////////////////////////////////////////////////////////////////////////////////
1515
//Test
1616

17-
Test::Test(const SQLparams& sql_params) : sql_params_(sql_params) {
17+
Test::Test(const SQLparams& sql_params) : sql_params_(sql_params)
18+
{
1819
try
1920
{
2021
initialize();
@@ -29,21 +30,52 @@ Test::Test(const SQLparams& sql_params) : sql_params_(sql_params) {
2930
}
3031
}
3132

32-
Test::~Test() {
33-
if ( connect_ ) {
34-
if ( !connect_->isClosed() ) {
35-
printf( "Disconnecting..." );
36-
connect_->disconnect();
37-
printf( "done\n" );
33+
34+
Test::~Test()
35+
{
36+
disconnect(true);
37+
}
38+
39+
40+
void Test::connect()
41+
{
42+
try
43+
{
44+
if (connect_ == nullptr) {
45+
printf("Creating connection object.\n");
46+
if (!(connect_ = createConnect()))
47+
throw MyException::create(false, "Failed to get Connection object.");
3848
}
39-
printf( "Deleting connection object..." );
40-
delete connect_;
41-
connect_= NULL;
42-
printf( "done\n" );
49+
printf("Setting connection properties.\n");
50+
if (connect_->setProperties(sql_params_.getHost(), sql_params_.getLogin(), sql_params_.getPsswd(), sql_params_.getMySQLPort(), sql_params_.getSpwPort()) < 0)
51+
throw MyException::create(false, "Failed to set Properties.");
52+
printf("Connecting...\n");
53+
if (connect_->connect() < 0)
54+
throw MyException::create(false, "Failed to set Connect to Sparrow.");
55+
printf("Connected.\n");
4356
}
57+
catch (const MyException& e) {
58+
printf("Failed to connect to Sparrow: %s : %s\n", e.getText(), errmsg());
59+
}
60+
}
4461

62+
void Test::disconnect(bool free_object)
63+
{
64+
if (connect_ != nullptr) {
65+
if (!connect_->isClosed()) {
66+
printf("Disconnecting...\n");
67+
connect_->disconnect();
68+
printf("Disconnected.\n");
69+
}
70+
if (free_object) {
71+
delete connect_;
72+
connect_ = nullptr;
73+
printf("Deleted connection object.\n");
74+
}
75+
}
4576
}
4677

78+
4779
void Test::dropTable(const char* table_name) {
4880
printf( "Dropping Sparrow table '%s'.'%s' ...", sql_params_.getSchema(), table_name);
4981
MySQLGuardmysql(sql_params_.getLogin(), sql_params_.getPsswd(), sql_params_.getMySQLPort());

storage/sparrow/api_test/common.h

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,12 +13,15 @@ class Test {
1313

1414
protected:
1515
SQLparams sql_params_;
16-
Sparrow::Connection*connect_;
16+
Sparrow::Connection* connect_{ nullptr };
1717

1818
public:
1919
Test(const SQLparams& sql_params);
2020
virtual ~Test();
2121

22+
void connect();
23+
void disconnect(bool free_object=false);
24+
2225
void reset(Sparrow::Table*& table);
2326
void dropTable(const char* table_name);
2427
uint getFlushInterval();

storage/sparrow/api_test/sparrow_api_test.cpp

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@
1818
#include "too_many_columns.h"
1919
#include "vl.h"
2020

21-
//#define TEST_BASIC
21+
#define TEST_BASIC
2222
//#define TEST_CREATETABLE_TOO_MANY_COLS
2323
//#define TEST_COALESCING
2424
//#define TEST_CREATETABLE
@@ -31,7 +31,7 @@
3131
//#define TEST_INSERT_INTERLACED_TS_MASS
3232
//#define TEST_INSERT_SELECT_COLUMNS
3333
//#define TEST_INSERT_SELECT_COLUMNS_MASSIVE
34-
#define TEST_COLUMN_OPTIM
34+
//#define TEST_COLUMN_OPTIM
3535
//#define TEST_ERRORS
3636
//#define TEST_DISABLE_COALESCING
3737

@@ -70,6 +70,7 @@ int main (int argc, char* argv[])
7070

7171
#ifdef TEST_BASIC
7272
TestAlltypestest_all(sql_params);
73+
test_all.setReconnect(true);
7374
test_all.run();
7475
#endif
7576

0 commit comments

Comments
 (0)