Skip to content

Commit 9318a50

Browse files
committed
Wsrep TLS service
This commit defines a TLS service interface. If the implementation is provided by the application when the provider is loaded, appropriate hooks are probed from the provider and the provider side hooks are initialized after the provider is loaded. A sample implementation to demostrate the use of TLS interface is provided in dbsim/db_tls.cpp. Also contains a change to thread service interface: The thread exit virtual method was changed to function pointer to allow thread exit path which does not involve C++.
1 parent a12b814 commit 9318a50

20 files changed

+1193
-79
lines changed

dbsim/CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ add_executable(dbsim
1313
db_simulator.cpp
1414
db_storage_engine.cpp
1515
db_threads.cpp
16+
db_tls.cpp
1617
dbsim.cpp
1718
)
1819

dbsim/db_params.cpp

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -87,6 +87,11 @@ db::params db::parse_args(int argc, char** argv)
8787
po::value<bool>(&params.cond_checks),
8888
"Enable checks for correct condition variable use. "
8989
" Effective only if thread-instrumentation is enabled")
90+
("tls-service",
91+
po::value<int>(&params.tls_service),
92+
"Configure TLS service stubs.\n0 default disabled\n1 enabled\n"
93+
"2 enabled with short read/write and renegotiation simulation\n"
94+
"3 enabled with error simulation.")
9095
;
9196
try
9297
{

dbsim/db_params.hpp

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,7 @@ namespace db
4242
int fast_exit;
4343
int thread_instrumentation;
4444
bool cond_checks;
45+
int tls_service;
4546
params()
4647
: n_servers(0)
4748
, n_clients(0)
@@ -58,6 +59,7 @@ namespace db
5859
, fast_exit(0)
5960
, thread_instrumentation()
6061
, cond_checks()
62+
, tls_service()
6163
{ }
6264
};
6365

dbsim/db_simulator.cpp

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,13 +20,15 @@
2020
#include "db_simulator.hpp"
2121
#include "db_client.hpp"
2222
#include "db_threads.hpp"
23+
#include "db_tls.hpp"
2324

2425
#include "wsrep/logger.hpp"
2526

2627
#include <boost/filesystem.hpp>
2728
#include <sstream>
2829

2930
static db::ti thread_instrumentation;
31+
static db::tls tls_service;
3032

3133
void db::simulator::run()
3234
{
@@ -36,6 +38,7 @@ void db::simulator::run()
3638
std::cout << "Results:\n";
3739
std::cout << stats() << std::endl;
3840
std::cout << db::ti::stats() << std::endl;
41+
std::cout << db::tls::stats() << std::endl;
3942
}
4043

4144
void db::simulator::sst(db::server& server,
@@ -114,6 +117,7 @@ void db::simulator::start()
114117
{
115118
thread_instrumentation.level(params_.thread_instrumentation);
116119
thread_instrumentation.cond_checks(params_.cond_checks);
120+
tls_service.init(params_.tls_service);
117121
wsrep::log_info() << "Provider: " << params_.wsrep_provider;
118122

119123
std::string cluster_address(build_cluster_address());
@@ -149,6 +153,9 @@ void db::simulator::start()
149153
services.thread_service = params_.thread_instrumentation
150154
? &thread_instrumentation
151155
: nullptr;
156+
services.tls_service = params_.tls_service
157+
? &tls_service
158+
: nullptr;
152159
if (server.server_state().load_provider(params_.wsrep_provider,
153160
server_options, services))
154161
{

dbsim/db_threads.cpp

Lines changed: 38 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@
3535
#include <unordered_map>
3636
#include <vector>
3737

38+
extern "C" { static void* start_thread(void* args_ptr); }
3839
namespace
3940
{
4041
struct ti_obj
@@ -186,7 +187,6 @@ namespace
186187
}
187188
};
188189

189-
void* thread_start_fn(void* args_ptr);
190190

191191
class ti_thread : public ti_obj
192192
{
@@ -209,7 +209,7 @@ namespace
209209
int run(void* (*fn)(void *), void* args)
210210
{
211211
auto ta(new thread_args{this, fn, args});
212-
return pthread_create(&th_, nullptr, thread_start_fn, ta);
212+
return pthread_create(&th_, nullptr, start_thread, ta);
213213
}
214214

215215
int detach()
@@ -254,24 +254,6 @@ namespace
254254
bool detached_;
255255
};
256256

257-
void* thread_start_fn(void* args_ptr)
258-
{
259-
thread_args* ta(reinterpret_cast<thread_args*>(args_ptr));
260-
ti_thread* thread = reinterpret_cast<ti_thread*>(ta->this_thread);
261-
pthread_setspecific(this_thread_key, thread);
262-
void* (*fn)(void*) = ta->fn;
263-
void* args = ta->args;
264-
delete ta;
265-
void* ret((*fn)(args));
266-
pthread_setspecific(this_thread_key, nullptr);
267-
268-
// If we end here the thread returned instead of calling
269-
// pthread_exit()
270-
if (thread->detached())
271-
delete thread;
272-
return ret;
273-
}
274-
275257
class ti_mutex : public ti_obj
276258
{
277259
public:
@@ -482,6 +464,42 @@ int db::ti::after_init()
482464
// Thread //
483465
//////////////////////////////////////////////////////////////////////////////
484466

467+
extern "C"
468+
{
469+
static void* start_thread(void* args_ptr)
470+
{
471+
thread_args* ta(reinterpret_cast<thread_args*>(args_ptr));
472+
ti_thread* thread = reinterpret_cast<ti_thread*>(ta->this_thread);
473+
pthread_setspecific(this_thread_key, thread);
474+
void* (*fn)(void*) = ta->fn;
475+
void* args = ta->args;
476+
delete ta;
477+
void* ret = (*fn)(args);
478+
pthread_setspecific(this_thread_key, nullptr);
479+
// If we end here the thread returned instead of calling
480+
// pthread_exit()
481+
if (thread->detached())
482+
delete thread;
483+
return ret;
484+
}
485+
486+
WSREP_NORETURN
487+
static void exit_thread(wsrep::thread_service::thread* thread, void* retval)
488+
{
489+
pthread_setspecific(this_thread_key, nullptr);
490+
ti_thread* th(reinterpret_cast<ti_thread*>(thread));
491+
th->retval(retval);
492+
if (th->detached())
493+
delete th;
494+
pthread_exit(retval);
495+
}
496+
} // extern "C"
497+
498+
db::ti::ti()
499+
{
500+
thread_service::exit = exit_thread;
501+
}
502+
485503
const wsrep::thread_service::thread_key*
486504
db::ti::create_thread_key(const char* name) WSREP_NOEXCEPT
487505
{
@@ -513,15 +531,6 @@ int db::ti::detach(wsrep::thread_service::thread* thread) WSREP_NOEXCEPT
513531
return reinterpret_cast<ti_thread*>(thread)->detach();
514532
}
515533

516-
void db::ti::exit(wsrep::thread_service::thread* thread, void* retval) WSREP_NOEXCEPT
517-
{
518-
ti_thread* th(reinterpret_cast<ti_thread*>(thread));
519-
th->retval(retval);
520-
if (th->detached())
521-
delete th;
522-
pthread_exit(retval);
523-
}
524-
525534
int db::ti::equal(wsrep::thread_service::thread* thread_1,
526535
wsrep::thread_service::thread* thread_2) WSREP_NOEXCEPT
527536
{

dbsim/db_threads.hpp

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ namespace db
2828
class ti : public wsrep::thread_service
2929
{
3030
public:
31-
ti() { }
31+
ti();
3232
int before_init() override;
3333
int after_init() override;
3434

@@ -41,7 +41,6 @@ namespace db
4141
int detach(wsrep::thread_service::thread*) WSREP_NOEXCEPT override;
4242
int equal(wsrep::thread_service::thread*,
4343
wsrep::thread_service::thread*) WSREP_NOEXCEPT override;
44-
void exit(wsrep::thread_service::thread*, void*) WSREP_NOEXCEPT override;
4544
int join(wsrep::thread_service::thread*, void**) WSREP_NOEXCEPT override;
4645
wsrep::thread_service::thread* self() WSREP_NOEXCEPT override;
4746
int setschedparam(wsrep::thread_service::thread*, int,

0 commit comments

Comments
 (0)