[ 
https://issues.apache.org/jira/browse/ZOOKEEPER-2965?page=com.atlassian.jira.plugin.system.issuetabpanels:all-tabpanel
 ]

Philippe Serreault updated ZOOKEEPER-2965:
------------------------------------------
    Comment: was deleted

(was: From f293df54d7890f1870c1460a4d7c0c4850065548 Mon Sep 17 00:00:00 2001
From: Philippe Serreault <[email protected]>
Date: Tue, 9 Jan 2018 18:14:23 +0100
Subject: [PATCH] Prevent DNS queries spam.

---
 src/c/src/zk_adaptor.h |  2 ++
 src/c/src/zookeeper.c  | 28 ++++++++++++++++++++++++----
 2 files changed, 26 insertions(+), 4 deletions(-)

diff --git a/src/c/src/zk_adaptor.h b/src/c/src/zk_adaptor.h
index 97995e36..8487dee9 100644
--- a/src/c/src/zk_adaptor.h
+++ b/src/c/src/zk_adaptor.h
@@ -268,6 +268,8 @@ struct _zhandle {
     char allow_read_only;
     /** Indicates if we connected to a majority server before */
     char seen_rw_server_before;
+    /** Indicates hostname modification and triggers a DNS resolve query */
+    char hostname_modified;
 };
 
 
diff --git a/src/c/src/zookeeper.c b/src/c/src/zookeeper.c
index 47e2ee19..74319439 100644
--- a/src/c/src/zookeeper.c
+++ b/src/c/src/zookeeper.c
@@ -904,9 +904,15 @@ int update_addrs(zhandle_t *zh)
         return ZSYSTEMERROR;
     }
 
-    // NOTE: guard access to {hostname, addr_cur, addrs, addrs_old, addrs_new}
+    // NOTE: guard access to {hostname, addr_cur, addrs, addrs_old, addrs_new, 
hostname_modified}
     lock_reconfig(zh);
 
+    // zh->hostname was unchanged, no need to resolve_hosts
+    if (!zh->hostname_modified)
+    {
+        goto fail;
+    }
+
     // Copy zh->hostname for local use
     hosts = strdup(zh->hostname);
     if (hosts == NULL) {
@@ -919,6 +925,11 @@ int update_addrs(zhandle_t *zh)
     {
         goto fail;
     }
+    else
+    {
+        // resolve successful, prevent further resolution of same hostname
+        zh->hostname_modified = 0;
+    }
 
     // If the addrvec list is identical to last time we ran don't do anything
     if (addrvec_eq(&zh->addrs, &resolved))
@@ -1159,6 +1170,7 @@ static zhandle_t *zookeeper_init_internal(const char 
*host, watcher_fn watcher,
     zh->allow_read_only = flags & ZOO_READONLY;
     // non-zero clientid implies we've seen r/w server already
     zh->seen_rw_server_before = (clientid != 0 && clientid->client_id != 0);
+    zh->hostname_modified = 1;
     init_auth_info(&zh->auth_h);
     if (watcher) {
        zh->watcher = watcher;
@@ -1258,15 +1270,23 @@ int zoo_set_servers(zhandle_t *zh, const char *hosts)
         return ZBADARGUMENTS;
     }
 
-    // NOTE: guard access to {hostname, addr_cur, addrs, addrs_old, addrs_new}
+    // NOTE: guard access to {hostname, addr_cur, addrs, addrs_old, addrs_new, 
hostname_modified}
     lock_reconfig(zh);
 
+    // No need to update_addrs when new zk servers list is identical to 
previous
+    if (zh->hostname && hosts && !strcmp(zh->hostname, hosts))
+    {
+        unlock_reconfig(zh);
+        return ZOK;
+    }
+
     // Reset hostname to new set of hosts to connect to
     if (zh->hostname) {
         free(zh->hostname);
     }
 
     zh->hostname = strdup(hosts);
+    zh->hostname_modified = 1;
 
     unlock_reconfig(zh);
 
@@ -1335,7 +1355,7 @@ static int get_next_server_in_reconfig(zhandle_t *zh)
  */
 void zoo_cycle_next_server(zhandle_t *zh)
 {
-    // NOTE: guard access to {hostname, addr_cur, addrs, addrs_old, addrs_new}
+    // NOTE: guard access to {hostname, addr_cur, addrs, addrs_old, addrs_new, 
hostname_modified}
     lock_reconfig(zh);
 
     memset(&zh->addr_cur, 0, sizeof(zh->addr_cur));
@@ -1367,7 +1387,7 @@ const char* zoo_get_current_server(zhandle_t* zh)
 {
     const char *endpoint_info = NULL;
 
-    // NOTE: guard access to {hostname, addr_cur, addrs, addrs_old, addrs_new}
+    // NOTE: guard access to {hostname, addr_cur, addrs, addrs_old, addrs_new, 
hostname_modified}
     // Need the lock here as it is changed in update_addrs()
     lock_reconfig(zh);
 
-- 
2.13.6

)

> prevent DNS queries spam
> ------------------------
>
>                 Key: ZOOKEEPER-2965
>                 URL: https://issues.apache.org/jira/browse/ZOOKEEPER-2965
>             Project: ZooKeeper
>          Issue Type: Improvement
>          Components: c client
>            Reporter: Philippe Serreault
>            Priority: Minor
>
> Hello,
> First of all, some context about the issue and why it became quite apparent 
> to me:
> * I'm using the native zookeeper client on linux
> * I'm not declaring -DTHREADED
> * My zookeeper ensemble is made of server names that need to be resolved
> * The ensemble and DNS servers are "next" to each other
> * My client is "far" and uses an unreliable network path that can drop UDP 
> requests
> For each run in client's main loop, all servers in ensemble are resolved, 
> even if no change in servers list occurred (zookeeper_interest .. 
> update_addrs .. resolve_hosts).
> In my situation, DNS requests could timeout and would trigger a reconnection 
> to ensemble.
> Please find attached a patch that would prevent DNS queries when hostname was 
> not changed.
> Best regards,



--
This message was sent by Atlassian JIRA
(v6.4.14#64029)

Reply via email to