From 26765592fda5d899b5c0f1948d373763517014e4 Mon Sep 17 00:00:00 2001
From: Tomas Hozza <thozza@redhat.com>
Date: Wed, 30 Apr 2014 22:00:40 +0200
Subject: [PATCH] Add lwres options for setting number of workers and clients

Add two new options into 'lwres' configuration statement:
'lwresd-tasks' - for setting number of workers
               - the default is number of CPUs (ns_g_cpus)
'lwresd-clients' - for setting number of client objects per worker
                 - the default is 256 if running as a part of named
                 - the default is 1024 if not running as a part of named
                 - the maximum is 32768

Added documentation into ARM (ch05 and ch06 - lwres grammar) and
named.conf manual pages.

Signed-off-by: Tomas Hozza <thozza@redhat.com>
---
 bin/named/include/named/lwresd.h |  2 ++
 bin/named/lwresd.c               | 39 ++++++++++++++++++++++++++++++++-------
 bin/named/named.conf.5           |  2 ++
 bin/named/named.conf.docbook     |  2 ++
 bin/named/named.conf.html        |  2 ++
 doc/arm/Bv9ARM.ch05.html         |  5 +++++
 doc/arm/Bv9ARM.ch06.html         | 23 +++++++++++++++++++++++
 doc/misc/options                 |  2 ++
 lib/isccfg/namedconf.c           |  2 ++
 9 files changed, 72 insertions(+), 7 deletions(-)

diff --git a/bin/named/include/named/lwresd.h b/bin/named/include/named/lwresd.h
index 565e58d..f5cef7b 100644
--- a/bin/named/include/named/lwresd.h
+++ b/bin/named/include/named/lwresd.h
@@ -36,6 +36,8 @@ struct ns_lwresd {
 	dns_view_t *view;
 	ns_lwsearchlist_t *search;
 	unsigned int ndots;
+	unsigned int ntasks;
+	unsigned int nclients;
 	isc_mem_t *mctx;
 	isc_boolean_t shutting_down;
 	unsigned int refs;
diff --git a/bin/named/lwresd.c b/bin/named/lwresd.c
index 8a9ecb6..5fdc6ee 100644
--- a/bin/named/lwresd.c
+++ b/bin/named/lwresd.c
@@ -60,11 +60,7 @@
 #define LWRESLISTENER_MAGIC	ISC_MAGIC('L', 'W', 'R', 'L')
 #define VALID_LWRESLISTENER(l)	ISC_MAGIC_VALID(l, LWRESLISTENER_MAGIC)
 
-/*!
- * The total number of clients we can handle will be NTASKS * NRECVS.
- */
-#define NTASKS		2	/*%< tasks to create to handle lwres queries */
-#define NRECVS		2	/*%< max clients per task */
+#define LWRESD_NCLIENTS_MAX		32768	/*%< max clients per task */
 
 typedef ISC_LIST(ns_lwreslistener_t) ns_lwreslistenerlist_t;
 
@@ -395,6 +391,25 @@ ns_lwdmanager_create(isc_mem_t *mctx, const cfg_obj_t *lwres,
 		}
 	}
 
+	obj = NULL;
+	(void)cfg_map_get(lwres, "lwres-tasks", &obj);
+	if (obj != NULL)
+		lwresd->ntasks = cfg_obj_asuint32(obj);
+	else
+		lwresd->ntasks = ns_g_cpus;
+
+	obj = NULL;
+	(void)cfg_map_get(lwres, "lwres-clients", &obj);
+	if (obj != NULL) {
+		lwresd->nclients = cfg_obj_asuint32(obj);
+		if (lwresd->nclients > LWRESD_NCLIENTS_MAX)
+			lwresd->nclients = LWRESD_NCLIENTS_MAX;
+	} else if (ns_g_lwresdonly) {
+		lwresd->nclients = 1024;
+	} else {
+		lwresd->nclients = 256;
+	}
+
 	lwresd->magic = LWRESD_MAGIC;
 
 	*lwresdp = lwresd;
@@ -606,12 +621,22 @@ listener_startclients(ns_lwreslistener_t *listener) {
 	unsigned int i;
 	isc_result_t result;
 
+	isc_log_write(ns_g_lctx,
+				  NS_LOGCATEGORY_GENERAL,
+				  NS_LOGMODULE_LWRESD,
+				  ISC_LOG_DEBUG(6),
+				  "listener_startclients: creating %d managers with %d clients each",
+				  listener->manager->ntasks,
+				  listener->manager->nclients);
+
 	/*
 	 * Create the client managers.
 	 */
 	result = ISC_R_SUCCESS;
-	for (i = 0; i < NTASKS && result == ISC_R_SUCCESS; i++)
-		result = ns_lwdclientmgr_create(listener, NRECVS,
+	for (i = 0;
+		 i < listener->manager->ntasks && result == ISC_R_SUCCESS;
+		 i++)
+		result = ns_lwdclientmgr_create(listener, listener->manager->nclients,
 						ns_g_taskmgr);
 
 	/*
diff --git a/bin/named/named.conf.5 b/bin/named/named.conf.5
index 479aab7..eeaa51b 100644
--- a/bin/named/named.conf.5
+++ b/bin/named/named.conf.5
@@ -154,6 +154,8 @@ lwres {
 	view \fIstring\fR \fIoptional_class\fR;
 	search { \fIstring\fR; ... };
 	ndots \fIinteger\fR;
+	lwres-tasks \fIinteger\fR;
+	lwres-clients \fIinteger\fR;
 };
 .fi
 .RE
diff --git a/bin/named/named.conf.docbook b/bin/named/named.conf.docbook
index 4c99a61..cf3d949 100644
--- a/bin/named/named.conf.docbook
+++ b/bin/named/named.conf.docbook
@@ -187,6 +187,8 @@ lwres {
 	view <replaceable>string</replaceable> <replaceable>optional_class</replaceable>;
 	search { <replaceable>string</replaceable>; ... };
 	ndots <replaceable>integer</replaceable>;
+	lwres-tasks <replaceable>integer</replaceable>;
+	lwres-clients <replaceable>integer</replaceable>;
 };
 </literallayout>
   </refsect1>
diff --git a/bin/named/named.conf.html b/bin/named/named.conf.html
index 5873ad2..ba02799 100644
--- a/bin/named/named.conf.html
+++ b/bin/named/named.conf.html
@@ -152,6 +152,8 @@ lwres
 	view <em class="replaceable"><code>string</code></em> <em class="replaceable"><code>optional_class</code></em>;<br>
 	search { <em class="replaceable"><code>string</code></em>; ... };<br>
 	ndots <em class="replaceable"><code>integer</code></em>;<br>
+	lwres-tasks <em class="replaceable"><code>integer</code></em>;<br>
+	lwres-clients <em class="replaceable"><code>integer</code></em>;<br>
 };<br>
 </p></div>
 </div>
diff --git a/doc/arm/Bv9ARM.ch05.html b/doc/arm/Bv9ARM.ch05.html
index bfe7395..b706538 100644
--- a/doc/arm/Bv9ARM.ch05.html
+++ b/doc/arm/Bv9ARM.ch05.html
@@ -120,6 +120,11 @@
         be configured to act as a lightweight resolver daemon using the
         <span><strong class="command">lwres</strong></span> statement in <code class="filename">named.conf</code>.
       </p>
+<p>
+        The number of client queries that the <span><strong class="command">lwresd</strong></span>
+        daemon is able to serve can be set using <span><strong class="command">lwres-tasks</strong></span>
+        and <span><strong class="command">lwres-clients</strong></span> statements in the configuration.
+      </p>
 </div>
 </div>
 <div class="navfooter">
diff --git a/doc/arm/Bv9ARM.ch06.html b/doc/arm/Bv9ARM.ch06.html
index 3ec7b8e..a920d0b 100644
--- a/doc/arm/Bv9ARM.ch06.html
+++ b/doc/arm/Bv9ARM.ch06.html
@@ -2127,6 +2127,8 @@ badresp:1,adberr:0,findfail:0,valfail:0]
     [<span class="optional"> view <em class="replaceable"><code>view_name</code></em>; </span>]
     [<span class="optional"> search { <em class="replaceable"><code>domain_name</code></em> ; [<span class="optional"> <em class="replaceable"><code>domain_name</code></em> ; ... </span>] }; </span>]
     [<span class="optional"> ndots <em class="replaceable"><code>number</code></em>; </span>]
+    [<span class="optional"> lwresd-tasks <em class="replaceable"><code>number</code></em>; </span>]
+    [<span class="optional"> lwresd-clients <em class="replaceable"><code>number</code></em>; </span>]
 };
 </pre>
 </div>
@@ -2180,6 +2182,27 @@ badresp:1,adberr:0,findfail:0,valfail:0]
           number of dots in a relative domain name that should result in an
           exact match lookup before search path elements are appended.
         </p>
+<p>
+          The <span><strong class="command">lwres-tasks</strong></span> statement is used to
+          specify how many worker threads will the lightweight resolver create to serve clients.
+          By default the number is the same as number of CPUs on the system (which can
+          be overridden using the <span><strong class="command">-n</strong></span> command
+          line option when starting the server.
+        </p>
+<p>
+          The <span><strong class="command">lwres-clients</strong></span> statement is used to
+          specify how many client objects will the lightweight resolver created per worker thread
+          to serve queries from clients. By default, if the lightweight resolver runs as a part
+          of named 256 client objects are created, if not running as a part of named 1024 client
+          objects are created. The maximum value is 32768. If the user specifies greater number,
+          the lightweight resolver will silently ignore it and use the maximum value instead.
+          Note that by setting the value too high you may run out of system resources.
+        </p>
+<p>
+          The maximum number of client queries that the lightweight resolver can handle equals to
+          <span><strong class="command">lwres-tasks</strong></span> value times
+          <span><strong class="command">lwres-clients</strong></span> value.
+        </p>
 </div>
 <div class="sect2" lang="en">
 <div class="titlepage"><div><div><h3 class="title">
diff --git a/doc/misc/options b/doc/misc/options
index 0238356..bfd3aa4 100644
--- a/doc/misc/options
+++ b/doc/misc/options
@@ -43,6 +43,8 @@ lwres {
         ndots <integer>;
         search { <string>; ... };
         view <string> <optional_class>;
+        lwres-tasks <integer>;
+        lwres-clients <integer>;
 };
 
 managed-keys { <string> <string> <integer> <integer> <integer>
diff --git a/lib/isccfg/namedconf.c b/lib/isccfg/namedconf.c
index cd5be19..96ce6c1 100644
--- a/lib/isccfg/namedconf.c
+++ b/lib/isccfg/namedconf.c
@@ -2925,6 +2925,8 @@ lwres_clauses[] = {
 	{ "view", &cfg_type_lwres_view, 0 },
 	{ "search", &cfg_type_lwres_searchlist, 0 },
 	{ "ndots", &cfg_type_uint32, 0 },
+	{ "lwres-tasks", &cfg_type_uint32, 0},
+	{ "lwres-clients", &cfg_type_uint32, 0},
 	{ NULL, NULL, 0 }
 };
 
-- 
1.9.0

