1
0
mirror of https://github.com/codership/wsrep-lib.git synced 2025-07-28 20:02:00 +03:00

Implemented thread service support.

Added a wsrep::thread_service interface to allow application to
inject instrumented thread, mutex and condition variable implementation
for provider.

The interface is defined in include/wsrep/thread_service.hpp.
Sample implementation is provided in dbsim/db_threads.[h|c]pp.

This patch will also clean up some remaining dependencies to
wsrep-API compilation units so that the dependency to wsrep-API
is header only. This will extending the provider support to
later wsrep-API versions.
This commit is contained in:
Teemu Ollakka
2019-02-16 17:09:18 +02:00
parent 477a71dd46
commit eb4cf86c1e
33 changed files with 1821 additions and 58 deletions

View File

@ -307,6 +307,10 @@ namespace wsrep
/**
* Append a key into transaction write set.
*
* @param key Key to be appended
*
* @return Zero on success, non-zero on failure.
*/
int append_key(const wsrep::key& key)
{
@ -315,6 +319,27 @@ namespace wsrep
return transaction_.append_key(key);
}
/**
* Append keys in key_array into transaction write set.
*
* @param keys Array of keys to be appended
*
* @return Zero in case of success, non-zero on failure.
*/
int append_keys(const wsrep::key_array& keys)
{
assert(mode_ == m_local || mode_ == m_toi);
assert(state_ == s_exec);
for (auto i(keys.begin()); i != keys.end(); ++i)
{
if (transaction_.append_key(*i))
{
return 1;
}
}
return 0;
}
/**
* Append data into transaction write set.
*/

View File

@ -20,12 +20,30 @@
/** @file compiler.hpp
*
* Compiler specific options.
* Compiler specific macro definitions.
*
* WSREP_NOEXCEPT - Specifies that the method/function does not throw. If
* and exception is thrown inside, std::terminate is called
* without propagating the exception.
* Set to "noexcept" if the compiler supports it, otherwise
* left empty.
* WSREP_NORETURN - Indicates that the method/function does not return.
* Set to attribute "[[noreturn]]" if the compiler supports,
* it, otherwise "__attribute__((noreturn))".
* WSREP_OVERRIDE - Set to "override" if the compiler supports it, otherwise
* left empty.
* WSREP_UNUSED - Can be used to mark variables which may be present in
* debug builds but not in release builds.
*/
#define WSREP_UNUSED __attribute__((unused))
#if __cplusplus >= 201103L
#define WSREP_NOEXCEPT noexcept
#define WSREP_NORETURN [[noreturn]]
#define WSREP_OVERRIDE override
#else
#define WSREP_NOEXCEPT
#define WSREP_NORETURN __attribute__((noreturn))
#define WSREP_OVERRIDE
#endif // __cplusplus >= 201103L
#define WSREP_UNUSED __attribute__((unused))

View File

@ -22,9 +22,16 @@
#include "id.hpp"
#include "seqno.hpp"
#include "compiler.hpp"
#include <iosfwd>
/**
* Minimum number of bytes guaratneed to store GTID string representation,
* terminating '\0' not included (36 + 1 + 20).
*/
#define WSREP_LIB_GTID_C_STR_LEN 57
namespace wsrep
{
class gtid
@ -61,6 +68,18 @@ namespace wsrep
wsrep::seqno seqno_;
};
/**
* Scan a GTID from C string.
*
* @param buf Buffer containing the string
* @param len Length of buffer
* @param[out] gtid Gtid to be printed to
*
* @return Number of bytes scanned, negative value on error.
*/
ssize_t scan_from_c_str(const char* buf, size_t buf_len,
wsrep::gtid& gtid);
/**
* Print a GTID into character buffer.
* @param buf Pointer to the beginning of the buffer
@ -68,16 +87,16 @@ namespace wsrep
*
* @return Number of characters printed or negative value for error
*/
ssize_t gtid_print_to_c_str(const wsrep::gtid&, char* buf, size_t buf_len);
ssize_t print_to_c_str(const wsrep::gtid&, char* buf, size_t buf_len);
/**
* Return minimum number of bytes guaranteed to store GTID string
* representation, terminating '\0' not included (36 + 1 + 20)
* Overload for ostream operator<<.
*/
static inline size_t gtid_c_str_len()
{
return 57;
}
std::ostream& operator<<(std::ostream&, const wsrep::gtid&);
/**
* Overload for istream operator>>.
*/
std::istream& operator>>(std::istream&, wsrep::gtid&);
}

View File

@ -42,10 +42,11 @@ namespace wsrep
class id
{
public:
typedef struct native_type { unsigned char buf[16]; } native_type;
/**
* Default constructor. Constructs an empty identifier.
*/
id() : data_() { std::memset(data_, 0, sizeof(data_)); }
id() : data_() { std::memset(data_.buf, 0, sizeof(data_.buf)); }
/**
* Construct from string. The input string may contain either
@ -62,24 +63,24 @@ namespace wsrep
{
throw wsrep::runtime_error("Too long identifier");
}
std::memset(data_, 0, sizeof(data_));
std::memcpy(data_, data, size);
std::memset(data_.buf, 0, sizeof(data_.buf));
std::memcpy(data_.buf, data, size);
}
bool operator<(const id& other) const
{
return (std::memcmp(data_, other.data_, sizeof(data_)) < 0);
return (std::memcmp(data_.buf, other.data_.buf, sizeof(data_.buf)) < 0);
}
bool operator==(const id& other) const
{
return (std::memcmp(data_, other.data_, sizeof(data_)) == 0);
return (std::memcmp(data_.buf, other.data_.buf, sizeof(data_.buf)) == 0);
}
bool operator!=(const id& other) const
{
return !(*this == other);
}
const void* data() const { return data_; }
const void* data() const { return data_.buf; }
size_t size() const { return sizeof(data_); }
@ -94,7 +95,7 @@ namespace wsrep
}
private:
static const wsrep::id undefined_;
unsigned char data_[16];
native_type data_;
};
std::ostream& operator<<(std::ostream&, const wsrep::id& id);

View File

@ -25,6 +25,7 @@
#include "buffer.hpp"
#include "client_id.hpp"
#include "transaction_id.hpp"
#include "compiler.hpp"
#include <cassert>
#include <cstring>
@ -33,10 +34,17 @@
#include <vector>
#include <ostream>
/**
* Empty provider magic. If none provider is passed to make_provider(),
* a dummy provider is loaded.
*/
#define WSREP_LIB_PROVIDER_NONE "none"
namespace wsrep
{
class server_state;
class high_priority_service;
class thread_service;
class stid
{
public:
@ -186,7 +194,6 @@ namespace wsrep
class provider
{
public:
class status_variable
{
public:
@ -404,16 +411,31 @@ namespace wsrep
*/
virtual void* native() const = 0;
/**
* Services argument passed to make_provider. This struct contains
* optional services which are passed to the provider.
*/
struct services
{
wsrep::thread_service* thread_service;
services()
: thread_service()
{
}
};
/**
* Create a new provider.
*
* @param provider_spec Provider specification
* @param provider_options Initial options to provider
* @param thread_service Optional thread service implementation.
*/
static provider* make_provider(
wsrep::server_state&,
const std::string& provider_spec,
const std::string& provider_options);
static provider* make_provider(wsrep::server_state&,
const std::string& provider_spec,
const std::string& provider_options,
const wsrep::provider::services& services
= wsrep::provider::services());
protected:
wsrep::server_state& server_state_;
};

View File

@ -20,8 +20,6 @@
#ifndef WSREP_SEQNO_HPP
#define WSREP_SEQNO_HPP
#include "exception.hpp"
#include <iosfwd>
namespace wsrep
@ -33,6 +31,8 @@ namespace wsrep
class seqno
{
public:
typedef long long native_type;
seqno()
: seqno_(-1)
{ }
@ -77,8 +77,9 @@ namespace wsrep
return (*this + seqno(other));
}
static seqno undefined() { return seqno(-1); }
private:
long long seqno_;
native_type seqno_;
};
std::ostream& operator<<(std::ostream& os, wsrep::seqno seqno);

View File

@ -95,6 +95,13 @@
#include <string>
#include <map>
/**
* Magic string to tell provider to engage into trivial (empty)
* state transfer. No data will be passed, but the node shall be
* considered joined.
*/
#define WSREP_LIB_SST_TRIVIAL "trivial"
namespace wsrep
{
// Forward declarations
@ -179,7 +186,6 @@ namespace wsrep
rm_sync
};
virtual ~server_state();
wsrep::encryption_service* encryption_service()
@ -262,11 +268,15 @@ namespace wsrep
* @param provider WSRep provider library to be loaded.
* @param provider_options Provider specific options string
* to be passed for provider during initialization.
* @param services Application defined services passed to
* the provider.
*
* @return Zero on success, non-zero on error.
*/
int load_provider(const std::string& provider,
const std::string& provider_options);
const std::string& provider_options,
const wsrep::provider::services& services
= wsrep::provider::services());
void unload_provider();

View File

@ -0,0 +1,100 @@
/*
* Copyright (C) 2019 Codership Oy <info@codership.com>
*
* This file is part of wsrep-lib.
*
* Wsrep-lib is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 2 of the License, or
* (at your option) any later version.
*
* Wsrep-lib is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with wsrep-lib. If not, see <https://www.gnu.org/licenses/>.
*/
/** @file thread_service.hpp
*
* Service interface for threads and synchronization primitives.
* The purpose of this interface is to provider provider implementations
* means to integrate with application thread implementation.
*
* Interface is designed to resemble POSIX threads, mutexes and
* condition variables.
*/
#include <cstddef> // size_t
#include "compiler.hpp"
struct timespec;
struct sched_param;
namespace wsrep
{
class thread_service
{
public:
virtual ~thread_service() { }
class thread_key { };
class thread { };
class mutex_key { };
class mutex { };
class cond_key { };
class cond { };
/**
* Method will be called before library side thread
* service initialization.
*/
virtual int before_init() = 0;
/**
* Method will be called after library side thread service
* has been initialized.
*/
virtual int after_init() = 0;
/* Thread */
virtual const thread_key* create_thread_key(const char* name) WSREP_NOEXCEPT
= 0;
virtual int create_thread(const thread_key*, thread**,
void* (*fn)(void*), void*) WSREP_NOEXCEPT
= 0;
virtual int detach(thread*) WSREP_NOEXCEPT = 0;
virtual int equal(thread*, thread*) WSREP_NOEXCEPT = 0;
WSREP_NORETURN virtual void exit(thread*, void* retval) WSREP_NOEXCEPT = 0;
virtual int join(thread*, void** retval) WSREP_NOEXCEPT = 0;
virtual thread* self() WSREP_NOEXCEPT = 0;
virtual int setschedparam(thread*, int,
const struct sched_param*) WSREP_NOEXCEPT
= 0;
virtual int getschedparam(thread*, int*, struct sched_param*) WSREP_NOEXCEPT
= 0;
/* Mutex */
virtual const mutex_key* create_mutex_key(const char* name) WSREP_NOEXCEPT
= 0;
virtual mutex* init_mutex(const mutex_key*, void*, size_t) WSREP_NOEXCEPT = 0;
virtual int destroy(mutex*) WSREP_NOEXCEPT = 0;
virtual int lock(mutex*) WSREP_NOEXCEPT = 0;
virtual int trylock(mutex*) WSREP_NOEXCEPT = 0;
virtual int unlock(mutex*) WSREP_NOEXCEPT = 0;
/* Condition variable */
virtual const cond_key* create_cond_key(const char* name) WSREP_NOEXCEPT = 0;
virtual cond* init_cond(const cond_key*, void*, size_t) WSREP_NOEXCEPT = 0;
virtual int destroy(cond*) WSREP_NOEXCEPT = 0;
virtual int wait(cond*, mutex*) WSREP_NOEXCEPT = 0;
virtual int timedwait(cond*, mutex*, const struct timespec*) WSREP_NOEXCEPT
= 0;
virtual int signal(cond*) WSREP_NOEXCEPT = 0;
virtual int broadcast(cond*) WSREP_NOEXCEPT = 0;
};
} // namespace wsrep