zookeeper-cpp
ZooKeeper Client for C++
 All Classes Files Functions Variables Typedefs Enumerations Enumerator Friends Groups
zk::server::configuration Class Referencefinal

Represents a configuration which should be run by server instance. More...

#include <zk/server/configuration.hpp>

Public Types

using duration_type = std::chrono::milliseconds
 

Public Member Functions

const optional< std::string > & source_file () const
 Get the source file. This will only have a value if this was created by from_file. More...
 
bool is_minimal () const
 Check if this is a "minimal" configuration – meaning it only has a data_directory and client_port set. More...
 
std::map< server_id, std::string > servers () const
 Get the servers which are part of the ZooKeeper ensemble. More...
 
configurationadd_server (server_id id, std::string hostname, std::uint16_t peer_port=default_peer_port, std::uint16_t leader_port=default_leader_port)
 Add a new server to the configuration. More...
 
std::map< std::string,
std::string > 
unknown_settings () const
 Get settings that were in the configuration file (or manually added with add_setting) but unknown to this library. More...
 
configurationadd_setting (std::string key, std::string value)
 Add an arbitrary setting with the key and value. More...
 
void save (std::ostream &stream) const
 Write this configuration to the provided stream. More...
 
void save_file (std::string filename)
 Save this configuration to filename. More...
 
std::uint16_t client_port () const
 
configurationclient_port (optional< std::uint16_t > port)
 
const optional< std::string > & data_directory () const
 
configurationdata_directory (optional< std::string > path)
 
duration_type tick_time () const
 
configurationtick_time (optional< duration_type > time)
 
std::size_t init_limit () const
 
configurationinit_limit (optional< std::size_t > limit)
 
std::size_t sync_limit () const
 
configurationsync_limit (optional< std::size_t > limit)
 
optional< bool > leader_serves () const
 
configurationleader_serves (optional< bool > serve)
 

Static Public Member Functions

static configuration make_minimal (std::string data_directory, std::uint16_t client_port=default_client_port)
 Creates a minimal configuration, setting the four needed values. More...
 
static configuration from_file (std::string filename)
 Load the configuration from a file. More...
 
static configuration from_stream (std::istream &stream)
 Load configuration from the provided stream. More...
 
static configuration from_lines (std::vector< std::string > lines)
 Load configuration from the provided lines. More...
 
static configuration from_string (string_view value)
 Load configuration directly from the in-memory value. More...
 

Static Public Attributes

static std::uint16_t default_client_port = std::uint16_t(2181)
 The default value for client_port. More...
 
static std::uint16_t default_peer_port = std::uint16_t(2888)
 The default value for peer_port. More...
 
static std::uint16_t default_leader_port = std::uint16_t(3888)
 The default value for leader_port. More...
 
static duration_type default_tick_time = std::chrono::milliseconds(2000)
 The default value for tick_time. More...
 
static std::size_t default_init_limit = 10U
 The default value for init_limit. More...
 
static std::size_t default_sync_limit = 5U
 The default value for sync_limit. More...
 

Friends

bool operator== (const configuration &lhs, const configuration &rhs)
 
bool operator!= (const configuration &lhs, const configuration &rhs)
 

Detailed Description

Represents a configuration which should be run by server instance.

This can also be used to modify an existing ZooKeeper server configuration file in a safer manner than the unfortunate operating practice of sed, awk, and perl.

This can be used to quickly create a quorum peer, ready to connect to 3 servers.

auto config = server::configuration::make_minimal("zk-data", 2181)
.add_server(1, "192.168.1.101")
.add_server(2, "192.168.1.102")
.add_server(3, "192.168.1.103");
config.save_file("settings.cfg");
{
std::ofstream of("zk-data/myid");
// Assuming this is server 1
of << 1 << std::endl;
}
server::server svr(config);
See also
server For where this is used.
server_group An example of quickly creating multiple ZooKeeper servers on a single machine (for testing).

Definition at line 70 of file configuration.hpp.

Member Function Documentation

configuration & zk::server::configuration::add_server ( server_id  id,
std::string  hostname,
std::uint16_t  peer_port = default_peer_port,
std::uint16_t  leader_port = default_leader_port 
)

Add a new server to the configuration.

Parameters
idThe cluster unique ID of this server.
hostnameThe address of the server to connect to.
peer_portThe port used to move ZooKeeper data on.
leader_portThe port used for leader election.
Exceptions
std::out_of_rangeif the id is not in the valid range of IDs (see server_id).
std::runtime_errorif there is already a server with the given id.

Definition at line 296 of file configuration.cpp.

configuration & zk::server::configuration::add_setting ( std::string  key,
std::string  value 
)

Add an arbitrary setting with the key and value.

Note
You should not use this frequently – prefer the named settings.

Definition at line 326 of file configuration.cpp.

std::uint16_t zk::server::configuration::client_port ( ) const

The port a client should use to connect to this server.

Definition at line 221 of file configuration.cpp.

const optional< std::string > & zk::server::configuration::data_directory ( ) const

The directory for "myid" file and "version-2" directory (containing the log, snapshot, and epoch files).

Definition at line 232 of file configuration.cpp.

configuration zk::server::configuration::from_file ( std::string  filename)
static

Load the configuration from a file.

Definition at line 163 of file configuration.cpp.

configuration zk::server::configuration::from_lines ( std::vector< std::string >  lines)
static

Load configuration from the provided lines.

Definition at line 84 of file configuration.cpp.

configuration zk::server::configuration::from_stream ( std::istream &  stream)
static

Load configuration from the provided stream.

Definition at line 147 of file configuration.cpp.

configuration zk::server::configuration::from_string ( string_view  value)
static

Load configuration directly from the in-memory value.

Definition at line 171 of file configuration.cpp.

std::size_t zk::server::configuration::init_limit ( ) const

The number of ticks that the initial synchronization phase can take. This limits the length of time the ZooKeeper servers in quorum have to connect to a leader.

Definition at line 254 of file configuration.cpp.

bool zk::server::configuration::is_minimal ( ) const

Check if this is a "minimal" configuration – meaning it only has a data_directory and client_port set.

Configurations which are minimal can be started directly from the command line.

Definition at line 178 of file configuration.cpp.

optional< bool > zk::server::configuration::leader_serves ( ) const

Should an elected leader accepts client connections? For higher update throughput at the slight expense of read latency, the leader can be configured to not accept clients and focus on coordination. The default to this value is true, which means that a leader will accept client connections.

Definition at line 276 of file configuration.cpp.

configuration zk::server::configuration::make_minimal ( std::string  data_directory,
std::uint16_t  client_port = default_client_port 
)
static

Creates a minimal configuration, setting the four needed values.

The resulting configuration can be run through a file with save or it can run directly from the command line.

Definition at line 73 of file configuration.cpp.

void zk::server::configuration::save ( std::ostream &  stream) const

Write this configuration to the provided stream.

See also
save_file

Definition at line 341 of file configuration.cpp.

void zk::server::configuration::save_file ( std::string  filename)

Save this configuration to filename.

On successful save, source_file will but updated to reflect the new file.

Definition at line 349 of file configuration.cpp.

std::map< server_id, std::string > zk::server::configuration::servers ( ) const

Get the servers which are part of the ZooKeeper ensemble.

Definition at line 287 of file configuration.cpp.

const optional<std::string>& zk::server::configuration::source_file ( ) const
inline

Get the source file. This will only have a value if this was created by from_file.

Definition at line 114 of file configuration.hpp.

std::size_t zk::server::configuration::sync_limit ( ) const

Limits how far out of date a server can be from a leader.

Definition at line 265 of file configuration.cpp.

configuration::duration_type zk::server::configuration::tick_time ( ) const

The time between server "ticks." This value is used to translate init_limit and sync_limit into clock times.

Definition at line 243 of file configuration.cpp.

std::map< std::string, std::string > zk::server::configuration::unknown_settings ( ) const

Get settings that were in the configuration file (or manually added with add_setting) but unknown to this library.

Definition at line 317 of file configuration.cpp.

Friends And Related Function Documentation

bool operator== ( const configuration lhs,
const configuration rhs 
)
friend

Check for equality of configuration. This does not check the specification in lines, but the values of the settings. In other words, two configurations with tick_time set to 1 second are equal, even if the source files are different and "tickTime=1000" was set on different lines.

Definition at line 359 of file configuration.cpp.

Member Data Documentation

std::uint16_t zk::server::configuration::default_client_port = std::uint16_t(2181)
static

The default value for client_port.

Definition at line 77 of file configuration.hpp.

std::size_t zk::server::configuration::default_init_limit = 10U
static

The default value for init_limit.

Definition at line 89 of file configuration.hpp.

std::uint16_t zk::server::configuration::default_leader_port = std::uint16_t(3888)
static

The default value for leader_port.

Definition at line 83 of file configuration.hpp.

std::uint16_t zk::server::configuration::default_peer_port = std::uint16_t(2888)
static

The default value for peer_port.

Definition at line 80 of file configuration.hpp.

std::size_t zk::server::configuration::default_sync_limit = 5U
static

The default value for sync_limit.

Definition at line 92 of file configuration.hpp.

configuration::duration_type zk::server::configuration::default_tick_time = std::chrono::milliseconds(2000)
static

The default value for tick_time.

Definition at line 86 of file configuration.hpp.


The documentation for this class was generated from the following files: