Skip to content

Commit 2aa0b70

Browse files
authored
feat(server): Support replica-announce-ip/port (#3421)
* feat: Support `replica-announce-ip`/`port` Before this PR, we only supported `cluster_announce_ip`. It's basically the same feature, but used for cluster announcements instead of replication. This PR adds support for `replica-announce-ip` and `replica-announce-port`, which can be set via new flags `--announce_ip=` and `--announce_port=`. These flags apply to both cluster and replica announcements. Tested via running Sentinel, and making sure it is able to connect to announced ip+port, while it can't connect to announced false / unavailable ip+port. Note: this PR deprecates `--cluster_announce_ip`, but continues to support it. We will remove it in a future version. Fixes #3380 * fix failing test * destructure
1 parent c9ed3f7 commit 2aa0b70

13 files changed

+78
-15
lines changed

README.ja-JP.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -113,7 +113,7 @@ Dragonfly 特有の議論もある:
113113
* `admin_bind`: 管理コンソールの TCP 接続を指定されたアドレスにバインドする(`default: any`)。HTTP と RESP の両方のプロトコルをサポートする。
114114
* `admin_nopass`: 割り当てられたポートで、認証トークンなしでコンソールへのオープン管理アクセスを有効にする (`default: false`)。HTTP と RESP の両方のプロトコルをサポートする。
115115
* `cluster_mode`: サポートするクラスターモード (`default: ""`)。現在は `emulated` のみをサポートしている。
116-
* `cluster_announce_ip`: クラスタコマンドがクライアントにアナウンスする IP。
116+
* `announce_ip`: クラスタコマンドがクライアントにアナウンスする IP。
117117

118118
### 一般的なオプションを使用した開始スクリプトの例:
119119

README.ko-KR.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -111,7 +111,7 @@ Dragonfly는 현재 아래와 같은 Redis 인수들을 지원합니다 :
111111
* `admin_bind`: 주어진 주소에 관리자 콘솔 TCP 연결을 바인딩합니다. (`기본값: any`). HTTP와 RESP 프로토콜 모두를 지원합니다.
112112
* `admin_nopass`: 할당된 포트에 대해서 인증 토큰 없이 관리자 콘솔 접근을 활성화합니다. (`default: false`). HTTP와 RESP 프로토콜 모두를 지원합니다.
113113
* `cluster_mode`: 클러스터 모드가 지원됩니다. (`기본값: ""`). 현재는`emulated` 만 지원합니다.
114-
* `cluster_announce_ip`: 클러스터 명령을 클라이언트에게 알리는 IP 주소.
114+
* `announce_ip`: 클러스터 명령을 클라이언트에게 알리는 IP 주소.
115115

116116

117117
### 주요 옵션을 활용한 실행 스크립트 예시:

README.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -166,7 +166,8 @@ There are also some Dragonfly-specific arguments:
166166
* `admin_bind`: To bind the admin console TCP connection to a given address (`default: any`). Supports both HTTP and RESP protocols.
167167
* `admin_nopass`: To enable open admin access to console on the assigned port, without auth token needed (`default: false`). Supports both HTTP and RESP protocols.
168168
* `cluster_mode`: Cluster mode supported (`default: ""`). Currently supports only `emulated`.
169-
* `cluster_announce_ip`: The IP that cluster commands announce to the client.
169+
* `announce_ip`: The IP that cluster commands announce to the client, and to replication master.
170+
* `announce_port`: The port that cluster commands announce to the client, and to replication master.
170171

171172
### Example start script with popular options:
172173

README.zh-CN.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -135,7 +135,7 @@ Dragonfly 支持 Redis 的常见参数。
135135

136136
* `cluster_mode`:支持集群模式。目前仅支持 `emulated`。默认为空 `""`
137137

138-
* `cluster_announce_ip`:集群模式下向客户端公开的 IP。
138+
* `announce_ip`:集群模式下向客户端公开的 IP。
139139

140140
### 启动脚本示例,包含常用选项:
141141

src/server/cluster/cluster_family.cc

Lines changed: 20 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -25,12 +25,15 @@
2525
#include "server/server_family.h"
2626
#include "server/server_state.h"
2727

28-
ABSL_FLAG(std::string, cluster_announce_ip, "", "ip that cluster commands announce to the client");
28+
ABSL_FLAG(std::string, cluster_announce_ip, "", "DEPRECATED: use --announce_ip");
29+
2930
ABSL_FLAG(std::string, cluster_node_id, "",
3031
"ID within a cluster, used for slot assignment. MUST be unique. If empty, uses master "
3132
"replication ID (random string)");
3233

3334
ABSL_DECLARE_FLAG(int32_t, port);
35+
ABSL_DECLARE_FLAG(std::string, announce_ip);
36+
ABSL_DECLARE_FLAG(uint16_t, announce_port);
3437

3538
namespace dfly {
3639
namespace acl {
@@ -66,6 +69,16 @@ ClusterFamily::ClusterFamily(ServerFamily* server_family) : server_family_(serve
6669

6770
InitializeCluster();
6871

72+
// TODO: Remove flag cluster_announce_ip in v1.23+
73+
if (!absl::GetFlag(FLAGS_cluster_announce_ip).empty()) {
74+
CHECK(absl::GetFlag(FLAGS_announce_ip).empty())
75+
<< "Can't use both --cluster_announce_ip and --announce_ip";
76+
77+
LOG(WARNING) << "WARNING: Flag --cluster_announce_ip is deprecated in favor of --announce_ip. "
78+
"Use the latter, as the former will be removed in a future release.";
79+
absl::SetFlag(&FLAGS_announce_ip, absl::GetFlag(FLAGS_cluster_announce_ip));
80+
}
81+
6982
id_ = absl::GetFlag(FLAGS_cluster_node_id);
7083
if (id_.empty()) {
7184
id_ = server_family_->master_replid();
@@ -104,13 +117,15 @@ ClusterShardInfo ClusterFamily::GetEmulatedShardInfo(ConnectionContext* cntx) co
104117
ServerState& etl = *ServerState::tlocal();
105118
if (!replication_info.has_value()) {
106119
DCHECK(etl.is_master);
107-
std::string cluster_announce_ip = absl::GetFlag(FLAGS_cluster_announce_ip);
120+
std::string cluster_announce_ip = absl::GetFlag(FLAGS_announce_ip);
108121
std::string preferred_endpoint =
109122
cluster_announce_ip.empty() ? cntx->conn()->LocalBindAddress() : cluster_announce_ip;
123+
uint16_t cluster_announce_port = absl::GetFlag(FLAGS_announce_port);
124+
uint16_t preferred_port = cluster_announce_port == 0
125+
? static_cast<uint16_t>(absl::GetFlag(FLAGS_port))
126+
: cluster_announce_port;
110127

111-
info.master = {.id = id_,
112-
.ip = preferred_endpoint,
113-
.port = static_cast<uint16_t>(absl::GetFlag(FLAGS_port))};
128+
info.master = {.id = id_, .ip = preferred_endpoint, .port = preferred_port};
114129

115130
for (const auto& replica : server_family_->GetDflyCmd()->GetReplicasRoleInfo()) {
116131
info.replicas.push_back({.id = replica.id,

src/server/cluster/cluster_family_test.cc

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -696,7 +696,7 @@ class ClusterFamilyEmulatedTest : public ClusterFamilyTest {
696696
public:
697697
ClusterFamilyEmulatedTest() {
698698
SetTestFlag("cluster_mode", "emulated");
699-
SetTestFlag("cluster_announce_ip", "fake-host");
699+
SetTestFlag("announce_ip", "fake-host");
700700
}
701701
};
702702

src/server/conn_context.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -131,6 +131,7 @@ struct ConnectionState {
131131
// then it holds positive sync session id.
132132
uint32_t repl_session_id = 0;
133133
uint32_t repl_flow_id = UINT32_MAX;
134+
std::string repl_ip_address;
134135
uint32_t repl_listening_port = 0;
135136
DflyVersion repl_version = DflyVersion::VER0;
136137
};

src/server/dflycmd.cc

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -563,7 +563,7 @@ auto DflyCmd::CreateSyncSession(ConnectionContext* cntx)
563563
fb2::Fiber("stop_replication", &DflyCmd::StopReplication, this, sync_id).Detach();
564564
};
565565

566-
string address = cntx->conn()->RemoteEndpointAddress();
566+
string address = cntx->conn_state.replication_info.repl_ip_address;
567567
uint32_t port = cntx->conn_state.replication_info.repl_listening_port;
568568

569569
LOG(INFO) << "Registered replica " << address << ":" << port;

src/server/main_service.cc

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,11 @@ using facade::ErrorReply;
6767
ABSL_FLAG(int32_t, port, 6379,
6868
"Redis port. 0 disables the port, -1 will bind on a random available port.");
6969

70+
ABSL_FLAG(std::string, announce_ip, "",
71+
"IP address that Dragonfly announces to cluster clients and replication master");
72+
ABSL_FLAG(uint16_t, announce_port, 0,
73+
"Port that Dragonfly announces to cluster clients and replication master");
74+
7075
ABSL_FLAG(uint32_t, memcached_port, 0, "Memcached port");
7176

7277
ABSL_FLAG(uint32_t, num_shards, 0, "Number of database shards, 0 - to choose automatically");

src/server/replica.cc

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,8 @@ ABSL_FLAG(bool, break_replication_on_master_restart, false,
4242
"When in replica mode, and master restarts, break replication from master to avoid "
4343
"flushing the replica's data.");
4444
ABSL_DECLARE_FLAG(int32_t, port);
45+
ABSL_DECLARE_FLAG(uint16_t, announce_port);
46+
ABSL_DECLARE_FLAG(std::string, announce_ip);
4547
ABSL_FLAG(
4648
int, replica_priority, 100,
4749
"Published by info command for sentinel to pick replica based on score during a failover");
@@ -266,10 +268,19 @@ error_code Replica::Greet() {
266268
PC_RETURN_ON_BAD_RESPONSE(CheckRespIsSimpleReply("PONG"));
267269

268270
// Corresponds to server.repl_state == REPL_STATE_SEND_HANDSHAKE condition in replication.c
269-
auto port = absl::GetFlag(FLAGS_port);
271+
uint16_t port = absl::GetFlag(FLAGS_announce_port);
272+
if (port == 0) {
273+
port = static_cast<uint16_t>(absl::GetFlag(FLAGS_port));
274+
}
270275
RETURN_ON_ERR(SendCommandAndReadResponse(StrCat("REPLCONF listening-port ", port)));
271276
PC_RETURN_ON_BAD_RESPONSE(CheckRespIsSimpleReply("OK"));
272277

278+
auto announce_ip = absl::GetFlag(FLAGS_announce_ip);
279+
if (!announce_ip.empty()) {
280+
RETURN_ON_ERR(SendCommandAndReadResponse(StrCat("REPLCONF ip-address ", announce_ip)));
281+
PC_RETURN_ON_BAD_RESPONSE(CheckRespIsSimpleReply("OK"));
282+
}
283+
273284
// Corresponds to server.repl_state == REPL_STATE_SEND_CAPA
274285
RETURN_ON_ERR(SendCommandAndReadResponse("REPLCONF capa eof capa psync2"));
275286
PC_RETURN_ON_BAD_RESPONSE(CheckRespIsSimpleReply("OK"));

0 commit comments

Comments
 (0)