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)
 
bool leader_serves () const
 
configurationleader_serves (optional< bool > serve)
 
const std::set< std::string > & four_letter_word_whitelist () const
 
configurationfour_letter_word_whitelist (optional< std::set< std::string >> words)
 

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 const std::uint16_t default_client_port = std::uint16_t(2181)
 The default value for client_port. More...
 
static const std::uint16_t default_peer_port = std::uint16_t(2888)
 The default value for peer_port. More...
 
static const std::uint16_t default_leader_port = std::uint16_t(3888)
 The default value for leader_port. More...
 
static const duration_type default_tick_time = std::chrono::milliseconds(2000)
 The default value for tick_time. More...
 
static const std::size_t default_init_limit = 10U
 The default value for init_limit. More...
 
static const std::size_t default_sync_limit = 5U
 The default value for sync_limit. More...
 
static const std::set
< std::string > 
default_four_letter_word_whitelist = { "srvr" }
 The default value for four_letter_word_whitelist. More...
 
static const std::set
< std::string > 
all_four_letter_word_whitelist = { "*" }
 A value for four_letter_word_whitelist that enables all commands. More...
 
static const std::set
< std::string > 
known_four_letter_word_whitelist
 All known values allowed in four_letter_word_whitelist. 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 71 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 368 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 398 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 259 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 270 of file configuration.cpp.

const std::set< std::string > & zk::server::configuration::four_letter_word_whitelist ( ) const

A list of comma separated four letter words commands that user wants to use. A valid four letter words command must be put in this list or the ZooKeeper server will not enable the command. If unspecified, the whitelist only contains "srvr" command (default_four_letter_word_whitelist).

Note
It is planned that the ZooKeeper server will deprecate this whitelist in preference of using a JSON REST API for health checks. It is unlikely to be deprecated any time in the near future and will likely remain in the product for a very long time.
Parameters
wordsis the list of four letter words to allow or nullopt to clear the setting. If specified as an empty set, this explicitly disables all words, which is different than setting this value to nullopt.
Exceptions
std::invalid_argumentif words contains the all value ("*") but it is not the only value in the set.

default_four_letter_word_whitelist all_four_letter_word_whitelist known_four_letter_word_whitelist

Definition at line 325 of file configuration.cpp.

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

Load the configuration from a file.

Definition at line 201 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 118 of file configuration.cpp.

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

Load configuration from the provided stream.

Definition at line 185 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 209 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 292 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 216 of file configuration.cpp.

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 314 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 77 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 413 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 421 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 359 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 126 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 303 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 281 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 389 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 431 of file configuration.cpp.

Member Data Documentation

const std::set< std::string > zk::server::configuration::all_four_letter_word_whitelist = { "*" }
static

A value for four_letter_word_whitelist that enables all commands.

Note that this is not a list of all allowed words, but simply the string "*".

Definition at line 100 of file configuration.hpp.

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

The default value for client_port.

Definition at line 78 of file configuration.hpp.

const std::set< std::string > zk::server::configuration::default_four_letter_word_whitelist = { "srvr" }
static

The default value for four_letter_word_whitelist.

Definition at line 96 of file configuration.hpp.

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

The default value for init_limit.

Definition at line 90 of file configuration.hpp.

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

The default value for leader_port.

Definition at line 84 of file configuration.hpp.

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

The default value for peer_port.

Definition at line 81 of file configuration.hpp.

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

The default value for sync_limit.

Definition at line 93 of file configuration.hpp.

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

The default value for tick_time.

Definition at line 87 of file configuration.hpp.

const std::set<std::string> zk::server::configuration::known_four_letter_word_whitelist
static

All known values allowed in four_letter_word_whitelist.

This set comes from what ZooKeeper server 3.5.3 supported, so it is possible the version of ZooKeeper you are running supports a different set.

Definition at line 104 of file configuration.hpp.


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