Clio develop
The XRP Ledger API server.
Loading...
Searching...
No Matches
Cluster.hpp
1//------------------------------------------------------------------------------
2/*
3 This file is part of clio: https://github.com/XRPLF/clio
4 Copyright (c) 2023, the clio developers.
5
6 Permission to use, copy, modify, and distribute this software for any
7 purpose with or without fee is hereby granted, provided that the above
8 copyright notice and this permission notice appear in all copies.
9
10 THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
11 WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
12 MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
13 ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
14 WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
15 ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
16 OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
17*/
18//==============================================================================
19
20#pragma once
21
22#include "data/cassandra/impl/ManagedObject.hpp"
23#include "util/log/Logger.hpp"
24
25#include <cassandra.h>
26
27#include <chrono>
28#include <cstddef>
29#include <cstdint>
30#include <optional>
31#include <string>
32#include <string_view>
33#include <thread>
34#include <variant>
35
36namespace data::cassandra::impl {
37
38// TODO: move Settings to public interface, not impl
39
43struct Settings {
44 static constexpr std::size_t kDEFAULT_CONNECTION_TIMEOUT = 10000;
45 static constexpr uint32_t kDEFAULT_MAX_WRITE_REQUESTS_OUTSTANDING = 10'000;
46 static constexpr uint32_t kDEFAULT_MAX_READ_REQUESTS_OUTSTANDING = 100'000;
47 static constexpr std::size_t kDEFAULT_BATCH_SIZE = 20;
48
53 std::string contactPoints = "127.0.0.1"; // defaults to localhost
54 std::optional<uint16_t> port;
55 };
56
61 std::string bundle; // no meaningful default
62 };
63
65 bool enableLog = false;
66
68 std::chrono::milliseconds connectionTimeout = std::chrono::milliseconds{kDEFAULT_CONNECTION_TIMEOUT};
69
71 std::chrono::milliseconds requestTimeout = std::chrono::milliseconds{0}; // no timeout at all
72
74 std::variant<ContactPoints, SecureConnectionBundle> connectionInfo = ContactPoints{};
75
77 uint32_t threads = std::thread::hardware_concurrency();
78
80 uint32_t maxWriteRequestsOutstanding = kDEFAULT_MAX_WRITE_REQUESTS_OUTSTANDING;
81
83 uint32_t maxReadRequestsOutstanding = kDEFAULT_MAX_READ_REQUESTS_OUTSTANDING;
84
87
89 std::size_t writeBatchSize = kDEFAULT_BATCH_SIZE;
90
92 std::optional<uint32_t> queueSizeIO = std::nullopt; // NOLINT(readability-redundant-member-init)
93
95 std::optional<std::string> certificate = std::nullopt; // NOLINT(readability-redundant-member-init)
96
98 std::optional<std::string> username = std::nullopt; // NOLINT(readability-redundant-member-init)
99
101 std::optional<std::string> password = std::nullopt; // NOLINT(readability-redundant-member-init)
102
107 withContactPoints(std::string_view contactPoints)
108 {
109 auto tmp = *this;
110 tmp.connectionInfo = ContactPoints{.contactPoints = std::string{contactPoints}, .port = std::nullopt};
111 return tmp;
112 }
113
117 static Settings
119 {
120 return Settings();
121 }
122};
123
124class Cluster : public ManagedObject<CassCluster> {
125 util::Logger log_{"Backend"};
126
127public:
128 Cluster(Settings const& settings);
129
130private:
131 void
132 setupConnection(Settings const& settings);
133
134 void
135 setupContactPoints(Settings::ContactPoints const& points);
136
137 void
138 setupSecureBundle(Settings::SecureConnectionBundle const& bundle);
139
140 void
141 setupCertificate(Settings const& settings);
142
143 void
144 setupCredentials(Settings const& settings);
145};
146
147} // namespace data::cassandra::impl
Definition Cluster.hpp:124
Definition ManagedObject.hpp:28
A simple thread-safe logger for the channel specified in the constructor.
Definition Logger.hpp:111
Represents the configuration of contact points for cassandra.
Definition Cluster.hpp:52
Represents the configuration of a secure connection bundle.
Definition Cluster.hpp:60
Bundles all cassandra settings in one place.
Definition Cluster.hpp:43
std::chrono::milliseconds requestTimeout
Request timeout specified in milliseconds.
Definition Cluster.hpp:71
std::chrono::milliseconds connectionTimeout
Connect timeout specified in milliseconds.
Definition Cluster.hpp:68
std::optional< std::string > password
Password to match the username
Definition Cluster.hpp:101
std::optional< std::string > username
Username/login.
Definition Cluster.hpp:98
uint32_t threads
The number of threads for the driver to pool.
Definition Cluster.hpp:77
uint32_t maxReadRequestsOutstanding
The maximum number of outstanding read requests at any given moment.
Definition Cluster.hpp:83
uint32_t maxWriteRequestsOutstanding
The maximum number of outstanding write requests at any given moment.
Definition Cluster.hpp:80
std::optional< std::string > certificate
SSL certificate.
Definition Cluster.hpp:95
uint32_t coreConnectionsPerHost
The number of connection per host to always have active.
Definition Cluster.hpp:86
std::variant< ContactPoints, SecureConnectionBundle > connectionInfo
Connection information; either ContactPoints or SecureConnectionBundle.
Definition Cluster.hpp:74
bool enableLog
Enables or disables cassandra driver logger.
Definition Cluster.hpp:65
std::size_t writeBatchSize
Size of batches when writing.
Definition Cluster.hpp:89
std::optional< uint32_t > queueSizeIO
Size of the IO queue.
Definition Cluster.hpp:92
static Settings defaultSettings()
Returns the default settings.
Definition Cluster.hpp:118
Settings withContactPoints(std::string_view contactPoints)
Creates a new Settings object as a copy of the current one with overridden contact points.
Definition Cluster.hpp:107