Skip to content

Commit 2b332fc

Browse files
committed
{182782396} Look up peer's short hostname
This patch includes a slight refactoring around hostname lookup, also fixes incorrect IP address when peer drops. Signed-off-by: Rivers Zhang <hzhang320@bloomberg.net>
1 parent 8edf868 commit 2b332fc

File tree

13 files changed

+55
-76
lines changed

13 files changed

+55
-76
lines changed

bdb/bdb_net.c

Lines changed: 6 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,7 @@
4141
#include "printformats.h"
4242
#include "crc32c.h"
4343
#include <timer_util.h>
44+
#include <hostname_support.h>
4445

4546
#undef UDP_DEBUG
4647
#undef UDP_TRACE
@@ -200,33 +201,23 @@ void comdb2_early_ack(DB_ENV *dbenv, DB_LSN permlsn, uint32_t commit_gen, uint32
200201
char *print_addr(struct sockaddr_in *addr, char *buf)
201202
{
202203
buf[0] = '\0';
204+
short port = ntohs(addr->sin_port);
203205
if (addr == NULL) {
204206
return buf;
205207
}
206208
if(addr->sin_addr.s_addr == htonl(INADDR_ANY)) {
207-
sprintf(buf, "[0.0.0.0 0.0.0.0:%d ]", ntohs(addr->sin_port));
209+
sprintf(buf, "[0.0.0.0 0.0.0.0:%d ]", port);
208210
return buf;
209211
}
210212
char ip[32] = {0};
211-
char name[256] = {0};
212-
char service[256] = {0};
213+
const char *name;
213214
char errbuf[256] = {0};
214-
socklen_t len;
215215

216-
len = sizeof(*addr);
217-
int rc = getnameinfo((struct sockaddr *)addr, len, name, sizeof(name),
218-
service, sizeof(service), 0);
219-
if (rc) {
220-
#pragma GCC diagnostic push
221-
#pragma GCC diagnostic ignored "-Wunused-result"
222-
strerror_r(errno, errbuf, sizeof(errbuf));
223-
#pragma GCC diagnostic pop
224-
sprintf(buf, "%s:getnameinfo errbuf=%s", __func__, errbuf);
216+
if ((name = get_cached_hostname_by_addr(addr)) == NULL)
225217
return buf;
226-
}
227218

228219
if (inet_ntop(addr->sin_family, &addr->sin_addr.s_addr, ip, sizeof(ip))) {
229-
sprintf(buf, "[%s %s:%s] ", name, ip, service);
220+
sprintf(buf, "[%s %s:%hd] ", name, ip, port);
230221
} else {
231222
#pragma GCC diagnostic push
232223
#pragma GCC diagnostic ignored "-Wunused-result"

db/comdb2.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3021,8 +3021,8 @@ void process_nodestats(void);
30213021
void nodestats_report(FILE *fh, const char *prefix, int disp_rates);
30223022
void nodestats_node_report(FILE *fh, const char *prefix, int disp_rates,
30233023
char *host);
3024-
struct rawnodestats *get_raw_node_stats(const char *task, const char *stack,
3025-
char *host, int fd, int is_ssl);
3024+
struct rawnodestats *get_raw_node_stats(const char *task, const char *stack, char *host, struct sockaddr_in *addr,
3025+
int is_ssl);
30263026
int release_node_stats(const char *task, const char *stack, char *host);
30273027
struct summary_nodestats *get_nodestats_summary(unsigned *nodes_cnt,
30283028
int disp_rates);

db/config.c

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -458,7 +458,8 @@ static char *legacy_options[] = {
458458
"recovery_ckp 0",
459459
"sc_current_version 3",
460460
"disable_sql_table_replacement 1",
461-
"endianize_locklist 0"
461+
"endianize_locklist 0",
462+
"get_peer_fqdn 0"
462463
};
463464
// clang-format on
464465

db/db_tunables.c

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -624,6 +624,7 @@ int gbl_nudge_replication_when_idle = 100;
624624

625625
extern int gbl_new_connection_grace_ms;
626626
extern int gbl_accept_headroom;
627+
extern int gbl_get_peer_fqdn;
627628

628629
int parse_int64(const char *value, int64_t *num);
629630

db/db_tunables.h

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2607,5 +2607,6 @@ REGISTER_TUNABLE("new_connection_grace_ms", "Time (in ms) before new connection
26072607
TUNABLE_INTEGER, &gbl_new_connection_grace_ms, INTERNAL, NULL, NULL, NULL, NULL);
26082608
REGISTER_TUNABLE("accept_headroom", "", TUNABLE_INTEGER, &gbl_accept_headroom, INTERNAL, NULL, NULL, NULL, NULL);
26092609
REGISTER_TUNABLE("simpleauth", NULL, TUNABLE_BOOLEAN, &gbl_uses_simpleauth, NOARG | READEARLY, NULL, NULL, NULL, NULL);
2610-
2610+
REGISTER_TUNABLE("get_peer_fqdn", "When set, use peer's FQDN", TUNABLE_BOOLEAN, &gbl_get_peer_fqdn, INTERNAL, NULL,
2611+
NULL, NULL, NULL);
26112612
#endif /* _DB_TUNABLES_H */

db/reqlog.c

Lines changed: 14 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -2508,7 +2508,7 @@ void release_clientstats_lock()
25082508
Pthread_rwlock_unlock(&clientstats_lk);
25092509
}
25102510

2511-
static void init_clientstats(nodestats_t *entry, int task_len, char *host, int fd)
2511+
static void init_clientstats(nodestats_t *entry, int task_len, char *host, struct sockaddr_in *addr)
25122512
{
25132513
entry->task = entry->mem;
25142514
entry->stack = entry->mem + task_len;
@@ -2521,21 +2521,10 @@ static void init_clientstats(nodestats_t *entry, int task_len, char *host, int f
25212521
Pthread_mutex_init(&entry->mtx, 0);
25222522
Pthread_mutex_init(&entry->rawtotals.lk, NULL);
25232523

2524-
if (fd < 0) {
2524+
if (addr == NULL) {
25252525
bzero(&(entry->addr), sizeof(struct in_addr));
25262526
} else {
2527-
struct sockaddr_in peeraddr;
2528-
socklen_t len = sizeof(peeraddr);
2529-
bzero(&peeraddr, sizeof(peeraddr));
2530-
if (getpeername(fd, (struct sockaddr *)&peeraddr, &len) < 0) {
2531-
if (errno != ENOTCONN)
2532-
logmsg(LOGMSG_ERROR, "%s: getpeername failed fd %d: %d %s\n", __func__, fd, errno,
2533-
strerror(errno));
2534-
bzero(&(entry->addr), sizeof(struct in_addr));
2535-
} else {
2536-
memcpy(&(entry->addr), &peeraddr.sin_addr,
2537-
sizeof(struct in_addr));
2538-
}
2527+
memcpy(&(entry->addr), &addr->sin_addr, sizeof(struct in_addr));
25392528
}
25402529
}
25412530

@@ -2549,9 +2538,8 @@ static void update_clientstats_cache(nodestats_t *entry) {
25492538
Pthread_mutex_unlock(&clientstats_cache_mtx);
25502539
}
25512540

2552-
static nodestats_t *add_clientstats(unsigned checksum,
2553-
const char *task_and_stack, int task_len,
2554-
int stack_len, char *host, int node, int fd)
2541+
static nodestats_t *add_clientstats(unsigned checksum, const char *task_and_stack, int task_len, int stack_len,
2542+
char *host, int node, struct sockaddr_in *addr)
25552543
{
25562544
nodestats_t *entry = calloc(1, offsetof(nodestats_t, mem) + task_len + stack_len);
25572545
if (entry == NULL) {
@@ -2606,15 +2594,15 @@ static nodestats_t *add_clientstats(unsigned checksum,
26062594
goto done;
26072595
}
26082596

2609-
init_clientstats(entry, task_len, host, fd);
2597+
init_clientstats(entry, task_len, host, addr);
26102598
hash_add(clientstats, entry);
26112599

26122600
done:
26132601
release_clientstats_lock();
26142602
return entry;
26152603
}
26162604

2617-
static nodestats_t *find_clientstats(unsigned checksum, int node, int fd)
2605+
static nodestats_t *find_clientstats(unsigned checksum, int node, struct sockaddr_in *addr)
26182606
{
26192607
nodestats_t key;
26202608
key.checksum = checksum;
@@ -2630,19 +2618,8 @@ static nodestats_t *find_clientstats(unsigned checksum, int node, int fd)
26302618
entry->ref++;
26312619
update_clientstats_cache(entry);
26322620

2633-
if (*(unsigned *)&(entry->addr) == 0 && fd > 0) {
2634-
struct sockaddr_in peeraddr;
2635-
socklen_t len = sizeof(peeraddr);
2636-
bzero(&peeraddr, sizeof(peeraddr));
2637-
if (getpeername(fd, (struct sockaddr *)&peeraddr, &len) < 0) {
2638-
if (errno != ENOTCONN)
2639-
logmsg(LOGMSG_ERROR, "%s: getpeername failed fd %d: %d %s\n", __func__, fd, errno,
2640-
strerror(errno));
2641-
bzero(&(entry->addr), sizeof(struct in_addr));
2642-
} else {
2643-
memcpy(&(entry->addr), &peeraddr.sin_addr,
2644-
sizeof(struct in_addr));
2645-
}
2621+
if (*(unsigned *)&(entry->addr) == 0 && addr != NULL) {
2622+
memcpy(&(entry->addr), &addr->sin_addr, sizeof(struct in_addr));
26462623
}
26472624
Pthread_mutex_unlock(&entry->mtx);
26482625

@@ -2692,8 +2669,8 @@ nodestats_t *get_next_clientstats_entry(void **curr, unsigned int *iter)
26922669
return entry;
26932670
}
26942671

2695-
struct rawnodestats *get_raw_node_stats(const char *task, const char *stack,
2696-
char *host, int fd, int is_ssl)
2672+
struct rawnodestats *get_raw_node_stats(const char *task, const char *stack, char *host, struct sockaddr_in *addr,
2673+
int is_ssl)
26972674
{
26982675
unsigned checksum;
26992676
int namelen, node;
@@ -2718,10 +2695,10 @@ struct rawnodestats *get_raw_node_stats(const char *task, const char *stack,
27182695
memcpy(tmp, task, task_len);
27192696
memcpy(tmp + task_len, stack, stack_len);
27202697
checksum = crc32c((const uint8_t *)tmp, namelen);
2721-
2722-
nodestats_t *nodestats = find_clientstats(checksum, node, fd);
2698+
2699+
nodestats_t *nodestats = find_clientstats(checksum, node, addr);
27232700
if (!nodestats) {
2724-
nodestats = add_clientstats(checksum, tmp, task_len, stack_len, host, node, fd);
2701+
nodestats = add_clientstats(checksum, tmp, task_len, stack_len, host, node, addr);
27252702
}
27262703

27272704
if (nodestats) {

db/sltdbt.c

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -427,9 +427,12 @@ int handle_ireq(struct ireq *iq)
427427
reqlog_pushprefixf(iq->reqlogger, "%s:REQ %s ", getorigin(iq),
428428
req2a(iq->opcode));
429429

430-
if (!iq->sorese) /* don't count osql */
431-
iq->rawnodestats =
432-
get_raw_node_stats(NULL, NULL, iq->frommach, cdb2buf_fileno(iq->sb), 0 /* tag does not support ssl */);
430+
if (!iq->sorese) { /* don't count osql */
431+
struct sockaddr_in addr;
432+
socklen_t len = sizeof(addr);
433+
getpeername(cdb2buf_fileno(iq->sb), (struct sockaddr *)&addr, &len);
434+
iq->rawnodestats = get_raw_node_stats(NULL, NULL, iq->frommach, &addr, 0 /* tag does not support ssl */);
435+
}
433436
if (iq->rawnodestats && iq->opcode >= 0 && iq->opcode < MAXTYPCNT)
434437
iq->rawnodestats->opcode_counts[iq->opcode]++;
435438
if (gbl_print_deadlock_cycles && IQ_HAS_SNAPINFO(iq))

db/sql.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -843,6 +843,7 @@ struct sqlclntstate {
843843
int authgen;
844844

845845
char *origin;
846+
struct sockaddr_in addr;
846847

847848
TAILQ_HEAD(, session_tbl) session_tbls;
848849

net/net.c

Lines changed: 6 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -73,6 +73,7 @@
7373
#include "thread_util.h"
7474
#include <timer_util.h>
7575
#include <comdb2_atomic.h>
76+
#include <hostname_support.h>
7677

7778
#ifdef UDP_DEBUG
7879
static int curr_udp_cnt = 0;
@@ -2524,23 +2525,19 @@ static int get_subnet_incomming_syn(host_node_type *host_node_ptr)
25242525
return 0;
25252526
}
25262527

2527-
char host[NI_MAXHOST], service[NI_MAXSERV];
25282528
/* get our host name for local _into_ address of connection */
2529-
int s = getnameinfo((struct sockaddr *)&lcl_addr_inet, lcl_len, host,
2530-
NI_MAXHOST, service, NI_MAXSERV, 0);
2531-
2532-
if (s != 0) {
2533-
logmsg(LOGMSG_WARN, "Incoming connection into unknown (%s:%u): %s\n",
2534-
inet_ntoa(lcl_addr_inet.sin_addr),
2535-
(unsigned)ntohs(lcl_addr_inet.sin_port), gai_strerror(s));
2529+
const char *host = get_cached_hostname_by_addr(&lcl_addr_inet);
2530+
if (host == NULL) {
2531+
logmsg(LOGMSG_WARN, "Incoming connection into unknown (%s:%u)\n", inet_ntoa(lcl_addr_inet.sin_addr),
2532+
(unsigned)ntohs(lcl_addr_inet.sin_port));
25362533
return 0;
25372534
}
25382535

25392536
/* extract the suffix of subnet ex. '_n3' in name node1_n3 */
25402537
int myh_len = strlen(host_node_ptr->netinfo_ptr->myhostname);
25412538
if (strncmp(host_node_ptr->netinfo_ptr->myhostname, host, myh_len) == 0) {
25422539
assert(myh_len <= sizeof(host));
2543-
char *subnet = &host[myh_len];
2540+
const char *subnet = &host[myh_len];
25442541
if (subnet[0])
25452542
strncpy0(host_node_ptr->subnet, subnet, HOSTNAME_LEN);
25462543
}

plugins/newsql/newsql.c

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2456,8 +2456,7 @@ newsql_loop_result newsql_loop(struct sqlclntstate *clnt, CDB2SQLQUERY *sql_quer
24562456
}
24572457
if (clnt->rawnodestats == NULL) {
24582458
clnt->rawnodestats =
2459-
get_raw_node_stats(clnt->argv0, clnt->stack, clnt->origin,
2460-
clnt->plugin.get_fileno(clnt), clnt->plugin.has_ssl(clnt));
2459+
get_raw_node_stats(clnt->argv0, clnt->stack, clnt->origin, &clnt->addr, clnt->plugin.has_ssl(clnt));
24612460
}
24622461
if (process_set_commands(clnt, sql_query)) {
24632462
return NEWSQL_ERROR;

0 commit comments

Comments
 (0)