Skip to content

Commit 56d66a4

Browse files
Fixes per PR review: refactored retry logic to eliminate code duplication.
1 parent 8801f9b commit 56d66a4

File tree

4 files changed

+46
-123
lines changed

4 files changed

+46
-123
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ config.h.in~
66
config.h
77
config.sub
88
configure
9+
configure~
910
compile
1011
depcomp
1112
install-sh

memtier_benchmark.cpp

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -76,6 +76,7 @@ static void sigint_handler(int signum)
7676
(void)signum; // unused parameter
7777
g_interrupted = 1;
7878
}
79+
7980
void benchmark_log_file_line(int level, const char *filename, unsigned int line, const char *fmt, ...)
8081
{
8182
if (level > log_level)
@@ -459,10 +460,6 @@ static void config_init_defaults(struct benchmark_config *cfg)
459460
if (!cfg->print_percentiles.is_defined())
460461
cfg->print_percentiles = config_quantiles("50,99,99.9");
461462

462-
// Set defaults for reconnection on error
463-
// Note: These defaults are only applied when reconnect_on_error is enabled
464-
// connection_timeout defaults to 0 (disabled) for backwards compatibility
465-
466463
#ifdef USE_TLS
467464
if (!cfg->tls_protocols)
468465
cfg->tls_protocols = REDIS_TLS_PROTO_DEFAULT;

shard_connection.cpp

Lines changed: 43 additions & 119 deletions
Original file line numberDiff line numberDiff line change
@@ -664,99 +664,62 @@ void shard_connection::handle_event(short events)
664664
benchmark_error_log("Connection error: %s\n", strerror(errno));
665665
}
666666

667-
// Update connection error statistics
668-
struct timeval now;
669-
gettimeofday(&now, NULL);
670-
client* c = static_cast<client*>(m_conns_manager);
671-
c->get_stats()->update_connection_error(&now);
672-
673-
// Attempt reconnection if enabled and not already reconnecting
674-
if (m_config->reconnect_on_error && !m_reconnecting &&
675-
(m_config->max_reconnect_attempts == 0 || m_reconnect_attempts < m_config->max_reconnect_attempts)) {
676-
677-
disconnect();
678-
m_reconnect_attempts++;
679-
if (m_config->reconnect_backoff_factor > 0.0) {
680-
m_current_backoff_delay *= m_config->reconnect_backoff_factor;
681-
}
682-
683-
if (m_config->max_reconnect_attempts == 0) {
684-
benchmark_error_log("Connection error, attempting reconnection %u (unlimited) in %.2f seconds...\n",
685-
m_reconnect_attempts, m_current_backoff_delay);
686-
} else {
687-
benchmark_error_log("Connection error, attempting reconnection %u/%u in %.2f seconds...\n",
688-
m_reconnect_attempts, m_config->max_reconnect_attempts, m_current_backoff_delay);
689-
}
690-
691-
// Schedule reconnection attempt
692-
struct timeval delay;
693-
delay.tv_sec = (long)m_current_backoff_delay;
694-
delay.tv_usec = (long)((m_current_backoff_delay - delay.tv_sec) * 1000000);
695-
696-
m_reconnect_timer = event_new(m_event_base, -1, 0, cluster_client_reconnect_timer_handler, (void *)this);
697-
event_add(m_reconnect_timer, &delay);
698-
m_reconnecting = true;
699-
} else {
700-
benchmark_error_log("Maximum reconnection attempts (%u) exceeded for connection error, triggering thread restart.\n",
701-
m_config->max_reconnect_attempts);
702-
disconnect();
703-
// Break the event loop to trigger thread restart
704-
event_base_loopbreak(m_event_base);
705-
}
706-
667+
attempt_reconnect("Connection error");
707668
return;
708669
}
709670

710671
if (events & BEV_EVENT_EOF) {
711672
benchmark_error_log("connection dropped.\n");
673+
attempt_reconnect("Connection dropped");
674+
return;
675+
}
676+
}
712677

713-
// Update connection error statistics
714-
struct timeval now;
715-
gettimeofday(&now, NULL);
716-
client* c = static_cast<client*>(m_conns_manager);
717-
c->get_stats()->update_connection_error(&now);
718-
719-
// Attempt reconnection if enabled and not already reconnecting
720-
if (m_config->reconnect_on_error && !m_reconnecting &&
721-
(m_config->max_reconnect_attempts == 0 || m_reconnect_attempts < m_config->max_reconnect_attempts)) {
678+
void shard_connection::handle_timer_event() {
679+
m_request_per_cur_interval = m_config->request_per_interval;
680+
fill_pipeline();
681+
}
722682

723-
disconnect();
724-
m_reconnect_attempts++;
725-
if (m_config->reconnect_backoff_factor > 0.0) {
726-
m_current_backoff_delay *= m_config->reconnect_backoff_factor;
727-
}
683+
void shard_connection::attempt_reconnect(const char* error_context) {
684+
// Update connection error statistics
685+
struct timeval now;
686+
gettimeofday(&now, NULL);
687+
client* c = static_cast<client*>(m_conns_manager);
688+
c->get_stats()->update_connection_error(&now);
728689

729-
if (m_config->max_reconnect_attempts == 0) {
730-
benchmark_error_log("Connection dropped, attempting reconnection %u (unlimited) in %.2f seconds...\n",
731-
m_reconnect_attempts, m_current_backoff_delay);
732-
} else {
733-
benchmark_error_log("Connection dropped, attempting reconnection %u/%u in %.2f seconds...\n",
734-
m_reconnect_attempts, m_config->max_reconnect_attempts, m_current_backoff_delay);
735-
}
690+
// Attempt reconnection if enabled and not already reconnecting
691+
if (m_config->reconnect_on_error && !m_reconnecting &&
692+
(m_config->max_reconnect_attempts == 0 || m_reconnect_attempts < m_config->max_reconnect_attempts)) {
736693

737-
// Schedule reconnection attempt
738-
struct timeval delay;
739-
delay.tv_sec = (long)m_current_backoff_delay;
740-
delay.tv_usec = (long)((m_current_backoff_delay - delay.tv_sec) * 1000000);
694+
disconnect();
695+
m_reconnect_attempts++;
696+
if (m_config->reconnect_backoff_factor > 0.0) {
697+
m_current_backoff_delay *= m_config->reconnect_backoff_factor;
698+
}
741699

742-
m_reconnect_timer = event_new(m_event_base, -1, 0, cluster_client_reconnect_timer_handler, (void *)this);
743-
event_add(m_reconnect_timer, &delay);
744-
m_reconnecting = true;
700+
if (m_config->max_reconnect_attempts == 0) {
701+
benchmark_error_log("%s, attempting reconnection %u (unlimited) in %.2f seconds...\n",
702+
error_context, m_reconnect_attempts, m_current_backoff_delay);
745703
} else {
746-
benchmark_error_log("Maximum reconnection attempts (%u) exceeded for connection drop, triggering thread restart.\n",
747-
m_config->max_reconnect_attempts);
748-
disconnect();
749-
// Break the event loop to trigger thread restart
750-
event_base_loopbreak(m_event_base);
704+
benchmark_error_log("%s, attempting reconnection %u/%u in %.2f seconds...\n",
705+
error_context, m_reconnect_attempts, m_config->max_reconnect_attempts, m_current_backoff_delay);
751706
}
752707

753-
return;
754-
}
755-
}
708+
// Schedule reconnection attempt
709+
struct timeval delay;
710+
delay.tv_sec = (long)m_current_backoff_delay;
711+
delay.tv_usec = (long)((m_current_backoff_delay - delay.tv_sec) * 1000000);
756712

757-
void shard_connection::handle_timer_event() {
758-
m_request_per_cur_interval = m_config->request_per_interval;
759-
fill_pipeline();
713+
m_reconnect_timer = event_new(m_event_base, -1, 0, cluster_client_reconnect_timer_handler, (void *)this);
714+
event_add(m_reconnect_timer, &delay);
715+
m_reconnecting = true;
716+
} else {
717+
benchmark_error_log("Maximum reconnection attempts (%u) exceeded for %s, triggering thread restart.\n",
718+
m_config->max_reconnect_attempts, error_context);
719+
disconnect();
720+
// Break the event loop to trigger thread restart
721+
event_base_loopbreak(m_event_base);
722+
}
760723
}
761724

762725
void shard_connection::handle_reconnect_timer_event() {
@@ -815,46 +778,7 @@ void shard_connection::handle_connection_timeout_event() {
815778
}
816779

817780
benchmark_error_log("Connection timeout after %u seconds.\n", m_config->connection_timeout);
818-
819-
// Update connection error statistics
820-
struct timeval now;
821-
gettimeofday(&now, NULL);
822-
client* c = static_cast<client*>(m_conns_manager);
823-
c->get_stats()->update_connection_error(&now);
824-
825-
// Treat timeout as a connection error and attempt reconnection if enabled
826-
if (m_config->reconnect_on_error && !m_reconnecting &&
827-
(m_config->max_reconnect_attempts == 0 || m_reconnect_attempts < m_config->max_reconnect_attempts)) {
828-
829-
disconnect();
830-
m_reconnect_attempts++;
831-
if (m_config->reconnect_backoff_factor > 0.0) {
832-
m_current_backoff_delay *= m_config->reconnect_backoff_factor;
833-
}
834-
835-
if (m_config->max_reconnect_attempts == 0) {
836-
benchmark_error_log("Connection timeout, attempting reconnection %u (unlimited) in %.2f seconds...\n",
837-
m_reconnect_attempts, m_current_backoff_delay);
838-
} else {
839-
benchmark_error_log("Connection timeout, attempting reconnection %u/%u in %.2f seconds...\n",
840-
m_reconnect_attempts, m_config->max_reconnect_attempts, m_current_backoff_delay);
841-
}
842-
843-
// Schedule reconnection attempt
844-
struct timeval delay;
845-
delay.tv_sec = (long)m_current_backoff_delay;
846-
delay.tv_usec = (long)((m_current_backoff_delay - delay.tv_sec) * 1000000);
847-
848-
m_reconnect_timer = event_new(m_event_base, -1, 0, cluster_client_reconnect_timer_handler, (void *)this);
849-
event_add(m_reconnect_timer, &delay);
850-
m_reconnecting = true;
851-
} else {
852-
benchmark_error_log("Maximum reconnection attempts (%u) exceeded for connection timeout, triggering thread restart.\n",
853-
m_config->max_reconnect_attempts);
854-
disconnect();
855-
// Break the event loop to trigger thread restart
856-
event_base_loopbreak(m_event_base);
857-
}
781+
attempt_reconnect("Connection timeout");
858782
}
859783

860784
void shard_connection::send_wait_command(struct timeval* sent_time,

shard_connection.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -153,6 +153,7 @@ class shard_connection {
153153

154154
void handle_event(short evtype);
155155
void handle_timer_event();
156+
void attempt_reconnect(const char* error_context);
156157

157158
unsigned int m_id;
158159
connections_manager* m_conns_manager;

0 commit comments

Comments
 (0)