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/Assert.hpp"
24#include "util/log/Logger.hpp"
25
26#include <cassandra.h>
27
28#include <chrono>
29#include <cstddef>
30#include <cstdint>
31#include <optional>
32#include <string>
33#include <string_view>
34#include <thread>
35#include <variant>
36
37namespace data::cassandra::impl {
38
39enum class Provider { Cassandra, Keyspace };
40
41inline Provider
42providerFromString(std::string const& provider)
43{
44 ASSERT(
45 provider == "cassandra" || provider == "aws_keyspace",
46 "Provider type must be one of 'cassandra' or 'aws_keyspace'"
47 );
48 return provider == "cassandra" ? Provider::Cassandra : Provider::Keyspace;
49}
50
51// TODO: move Settings to public interface, not impl
52
56struct Settings {
57 static constexpr std::size_t kDEFAULT_CONNECTION_TIMEOUT = 10000;
58 static constexpr uint32_t kDEFAULT_MAX_WRITE_REQUESTS_OUTSTANDING = 10'000;
59 static constexpr uint32_t kDEFAULT_MAX_READ_REQUESTS_OUTSTANDING = 100'000;
60 static constexpr std::size_t kDEFAULT_BATCH_SIZE = 20;
61 static constexpr Provider kDEFAULT_PROVIDER = Provider::Cassandra;
62
67 std::string contactPoints = "127.0.0.1"; // defaults to localhost
68 std::optional<uint16_t> port;
69 };
70
75 std::string bundle; // no meaningful default
76 };
77
79 bool enableLog = false;
80
82 std::chrono::milliseconds connectionTimeout = std::chrono::milliseconds{kDEFAULT_CONNECTION_TIMEOUT};
83
85 std::chrono::milliseconds requestTimeout = std::chrono::milliseconds{0}; // no timeout at all
86
88 std::variant<ContactPoints, SecureConnectionBundle> connectionInfo = ContactPoints{};
89
91 uint32_t threads = std::thread::hardware_concurrency();
92
94 uint32_t maxWriteRequestsOutstanding = kDEFAULT_MAX_WRITE_REQUESTS_OUTSTANDING;
95
97 uint32_t maxReadRequestsOutstanding = kDEFAULT_MAX_READ_REQUESTS_OUTSTANDING;
98
101
103 std::size_t writeBatchSize = kDEFAULT_BATCH_SIZE;
104
106 Provider provider = kDEFAULT_PROVIDER;
107
109 std::optional<uint32_t> queueSizeIO = std::nullopt; // NOLINT(readability-redundant-member-init)
110
112 std::optional<std::string> certificate = std::nullopt; // NOLINT(readability-redundant-member-init)
113
115 std::optional<std::string> username = std::nullopt; // NOLINT(readability-redundant-member-init)
116
118 std::optional<std::string> password = std::nullopt; // NOLINT(readability-redundant-member-init)
119
124 withContactPoints(std::string_view contactPoints)
125 {
126 auto tmp = *this;
127 tmp.connectionInfo = ContactPoints{.contactPoints = std::string{contactPoints}, .port = std::nullopt};
128 return tmp;
129 }
130
134 static Settings
136 {
137 return Settings();
138 }
139};
140
141class Cluster : public ManagedObject<CassCluster> {
142 util::Logger log_{"Backend"};
143
144public:
145 Cluster(Settings const& settings);
146
147private:
148 void
149 setupConnection(Settings const& settings);
150
151 void
152 setupContactPoints(Settings::ContactPoints const& points);
153
154 void
155 setupSecureBundle(Settings::SecureConnectionBundle const& bundle);
156
157 void
158 setupCertificate(Settings const& settings);
159
160 void
161 setupCredentials(Settings const& settings);
162};
163
164} // namespace data::cassandra::impl
A simple thread-safe logger for the channel specified in the constructor.
Definition Logger.hpp:95
Represents the configuration of contact points for cassandra.
Definition Cluster.hpp:66
Represents the configuration of a secure connection bundle.
Definition Cluster.hpp:74
Bundles all cassandra settings in one place.
Definition Cluster.hpp:56
std::chrono::milliseconds requestTimeout
Request timeout specified in milliseconds.
Definition Cluster.hpp:85
std::chrono::milliseconds connectionTimeout
Connect timeout specified in milliseconds.
Definition Cluster.hpp:82
Provider provider
Provider to know if we are using scylladb or keyspace.
Definition Cluster.hpp:106
std::optional< std::string > password
Password to match the username.
Definition Cluster.hpp:118
std::optional< std::string > username
Username/login.
Definition Cluster.hpp:115
uint32_t threads
The number of threads for the driver to pool.
Definition Cluster.hpp:91
uint32_t maxReadRequestsOutstanding
The maximum number of outstanding read requests at any given moment.
Definition Cluster.hpp:97
uint32_t maxWriteRequestsOutstanding
The maximum number of outstanding write requests at any given moment.
Definition Cluster.hpp:94
std::optional< std::string > certificate
SSL certificate.
Definition Cluster.hpp:112
uint32_t coreConnectionsPerHost
The number of connection per host to always have active.
Definition Cluster.hpp:100
std::variant< ContactPoints, SecureConnectionBundle > connectionInfo
Connection information; either ContactPoints or SecureConnectionBundle.
Definition Cluster.hpp:88
bool enableLog
Enables or disables cassandra driver logger.
Definition Cluster.hpp:79
std::size_t writeBatchSize
Size of batches when writing.
Definition Cluster.hpp:103
std::optional< uint32_t > queueSizeIO
Size of the IO queue.
Definition Cluster.hpp:109
static Settings defaultSettings()
Returns the default settings.
Definition Cluster.hpp:135
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:124