Skip to content

Commit

Permalink
feat(server): support config set for some flags (#1624)
Browse files Browse the repository at this point in the history
1. add tcp_keepalive flag
2. support config set for the following flags: tcp_keepalive,dir,requirepass,masterauth
3. more print details when failing on dbfilename flag validation

Signed-off-by: adi_holden <adi@dragonflydb.io>
  • Loading branch information
adiholden committed Aug 9, 2023
1 parent 52442c4 commit 6d84515
Show file tree
Hide file tree
Showing 5 changed files with 18 additions and 9 deletions.
15 changes: 7 additions & 8 deletions src/facade/dragonfly_listener.cc
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@
#ifdef DFLY_USE_SSL
#include <openssl/ssl.h>
#endif

#include "base/flags.h"
#include "base/logging.h"
#include "facade/dragonfly_connection.h"
Expand All @@ -27,6 +26,9 @@ ABSL_FLAG(string, tls_cert_file, "", "cert file for tls connections");
ABSL_FLAG(string, tls_key_file, "", "key file for tls connections");
ABSL_FLAG(string, tls_ca_cert_file, "", "ca signed certificate to validate tls connections");
ABSL_FLAG(string, tls_ca_cert_dir, "", "ca signed certificates directory");
ABSL_FLAG(uint32_t, tcp_keepalive, 300,
"the period in seconds of inactivity after which keep-alives are triggerred,"
"the duration until an inactive connection is terminated is twice the specified time");

#if 0
enum TlsClientAuth {
Expand Down Expand Up @@ -99,21 +101,19 @@ SSL_CTX* CreateSslServerCntx() {
}
#endif

bool ConfigureKeepAlive(int fd, unsigned interval_sec) {
DCHECK_GT(interval_sec, 3u);

bool ConfigureKeepAlive(int fd) {
int val = 1;
if (setsockopt(fd, SOL_SOCKET, SO_KEEPALIVE, &val, sizeof(val)) < 0)
return false;

val = interval_sec;
val = absl::GetFlag(FLAGS_tcp_keepalive);
if (setsockopt(fd, IPPROTO_TCP, TCP_KEEPIDLE, &val, sizeof(val)) < 0)
return false;

/* Send next probes after the specified interval. Note that we set the
* delay as interval / 3, as we send three probes before detecting
* an error (see the next setsockopt call). */
val = interval_sec / 3;
val = std::max(val / 3, 1);
if (setsockopt(fd, IPPROTO_TCP, TCP_KEEPINTVL, &val, sizeof(val)) < 0)
return false;

Expand Down Expand Up @@ -153,12 +153,11 @@ util::Connection* Listener::NewConnection(ProactorBase* proactor) {

error_code Listener::ConfigureServerSocket(int fd) {
int val = 1;
constexpr int kInterval = 300; // 300 seconds is ok to start checking for liveness.

if (setsockopt(fd, SOL_SOCKET, SO_REUSEADDR, &val, sizeof(val)) < 0) {
LOG(WARNING) << "Could not set reuse addr on socket " << SafeErrorMessage(errno);
}
bool success = ConfigureKeepAlive(fd, kInterval);
bool success = ConfigureKeepAlive(fd);

if (!success) {
int myerr = errno;
Expand Down
4 changes: 4 additions & 0 deletions src/server/config_registry.cc
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,10 @@ ConfigRegistry& ConfigRegistry::Register(std::string_view name, WriteCb cb) {
return *this;
}

ConfigRegistry& ConfigRegistry::Register(std::string_view name) {
return Register(name, [](const absl::CommandLineFlag& flag) { return true; });
}

// Returns true if the value was updated.
bool ConfigRegistry::Set(std::string_view config_name, std::string_view value) {
unique_lock lk(mu_);
Expand Down
1 change: 1 addition & 0 deletions src/server/config_registry.h
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ class ConfigRegistry {
using WriteCb = std::function<bool(const absl::CommandLineFlag&)>;

ConfigRegistry& Register(std::string_view name, WriteCb cb);
ConfigRegistry& Register(std::string_view name);

// Returns true if the value was updated.
bool Set(std::string_view config_name, std::string_view value);
Expand Down
5 changes: 5 additions & 0 deletions src/server/main_service.cc
Original file line number Diff line number Diff line change
Expand Up @@ -622,6 +622,11 @@ void Service::Init(util::AcceptServer* acceptor, std::vector<facade::Listener*>
return true;
});

config_registry.Register("dir");
config_registry.Register("requirepass");
config_registry.Register("masterauth");
config_registry.Register("tcp_keepalive");

pp_.Await([](uint32_t index, ProactorBase* pb) { ServerState::Init(index); });

uint32_t shard_num = GetFlag(FLAGS_num_shards);
Expand Down
2 changes: 1 addition & 1 deletion src/server/server_family.cc
Original file line number Diff line number Diff line change
Expand Up @@ -388,7 +388,7 @@ GenericError ValidateFilename(const fs::path& filename, bool new_version) {

if (!filename.parent_path().empty() && !is_cloud_path) {
return {absl::StrCat("filename may not contain directory separators (Got \"", filename.c_str(),
"\")")};
"\"). dbfilename should specify the filename without the directory")};
}

if (!filename.has_extension()) {
Expand Down

0 comments on commit 6d84515

Please sign in to comment.