Re: [PATCH] MEDIUM: lua: Add stick table support for Lua

2018-10-10 Thread bjun...@gmail.com
Am Sa., 29. Sep. 2018 um 20:18 Uhr schrieb Willy Tarreau :
>
> Hi Adis,
>
> On Thu, Sep 27, 2018 at 05:32:22PM +0200, Adis Nezirovic wrote:
> > On Thu, Sep 27, 2018 at 04:52:29PM +0200, Thierry Fournier wrote:
> > > I Adis,
> > >
> > > Sorry for the delay, I processed a quick review, and all seems to be ok 
> > > for me!
> > >
> > > BR,
> > > Thierry
> >
> > Great, happy to hear that, I hope guys will merge it soon.
>
> OK, just merged now.
>
> Thanks to you both!
>
> Willy
>

Hi Adis,

nice feature, thank you. Are there plans for adding write access also?

Currently i've a use case for this in Lua (get/set some sort of shared
lock) and i'm planning to use HAProxy maps as a workaround instead of
stick tables (or writing entries to stick tables from Lua via tcp to
admin socket).


Best Regards / Mit freundlichen Grüßen

Bjoern



Re: [PATCH] MEDIUM: lua: Add stick table support for Lua

2018-09-29 Thread Willy Tarreau
Hi Adis,

On Thu, Sep 27, 2018 at 05:32:22PM +0200, Adis Nezirovic wrote:
> On Thu, Sep 27, 2018 at 04:52:29PM +0200, Thierry Fournier wrote:
> > I Adis,
> >
> > Sorry for the delay, I processed a quick review, and all seems to be ok for 
> > me!
> >
> > BR,
> > Thierry
> 
> Great, happy to hear that, I hope guys will merge it soon.

OK, just merged now.

Thanks to you both!

Willy



Re: [PATCH] MEDIUM: lua: Add stick table support for Lua

2018-09-27 Thread Adis Nezirovic
On Thu, Sep 27, 2018 at 04:52:29PM +0200, Thierry Fournier wrote:
> I Adis,
>
> Sorry for the delay, I processed a quick review, and all seems to be ok for 
> me!
>
> BR,
> Thierry

Great, happy to hear that, I hope guys will merge it soon.

Best regards,
Adis



Re: [PATCH] MEDIUM: lua: Add stick table support for Lua

2018-09-27 Thread Thierry Fournier
I Adis,

Sorry for the delay, I processed a quick review, and all seems to be ok for me!

BR,
Thierry


> On 27 Sep 2018, at 14:02, Adis Nezirovic  wrote:
> 
> On Mon, Sep 03, 2018 at 12:09:47PM +0200, Adis Nezirovic wrote:
>> Hi Thierry,
>> 
>> Have you had the time to review my patches?
> 
> Thierry,
> 
> Reviving this thread, do you have any objections about latest version of
> the patch (Lua stick table patch)
> 
> Best regards,
> Adis




Re: [PATCH] MEDIUM: lua: Add stick table support for Lua

2018-09-27 Thread Adis Nezirovic
On Mon, Sep 03, 2018 at 12:09:47PM +0200, Adis Nezirovic wrote:
> Hi Thierry,
>
> Have you had the time to review my patches?

Thierry,

Reviving this thread, do you have any objections about latest version of
the patch (Lua stick table patch)

Best regards,
Adis



Re: [PATCH] MEDIUM: lua: Add stick table support for Lua

2018-09-03 Thread Adis Nezirovic
On Fri, Aug 24, 2018 at 11:40:51PM +0200, Adis Nezirovic wrote:
> Thierry,
>
> Something for Monday :-)
>
> Latest version of the patch in attachment:
>
> - Filter table format is flattened/simplified
> - I've tried to address filter table format error messages
>   (what is the error, and which filter entry is wrong)
> - Fixed one bug: stktable_get_data_type() return value can be < 0
>   (i.e. filter table contains unknown data column)

Hi Thierry,

Have you had the time to review my patches?

Best regards,
Adis



Re: [PATCH] MEDIUM: lua: Add stick table support for Lua

2018-08-24 Thread Adis Nezirovic
Thierry,

Something for Monday :-)

Latest version of the patch in attachment:

- Filter table format is flattened/simplified
- I've tried to address filter table format error messages
  (what is the error, and which filter entry is wrong)
- Fixed one bug: stktable_get_data_type() return value can be < 0
  (i.e. filter table contains unknown data column)

I've run a few unit tests for my Lua code using stick tables, hitting
all methods and handling regular return values and filter table errors.
So far so good, it looks good on my side.

Best regards,
Adis
>From 6b702ff6f12f919ba4d2f42a7962fa2345272382 Mon Sep 17 00:00:00 2001
From: Adis Nezirovic 
Date: Fri, 13 Jul 2018 12:18:33 +0200
Subject: [PATCH] MEDIUM: lua: Add stick table support for Lua.

This ads support for accessing stick tables from Lua. The supported
operations are reading general table info, lookup by string/IP key, and
dumping the table.

Similar to "show table", a data filter is available during dump, and as
an improvement over "show table" it's possible to use up to 4 filter
expressions instead of just one (with implicit AND clause binding the
expressions). Dumping with/without filters can take a long time for
large tables, and should be used sparingly.
---
 doc/lua-api/index.rst |  72 
 include/types/hlua.h  |   1 +
 src/hlua_fcn.c| 399 ++
 3 files changed, 472 insertions(+)

diff --git a/doc/lua-api/index.rst b/doc/lua-api/index.rst
index 0c79766e..9a2e039a 100644
--- a/doc/lua-api/index.rst
+++ b/doc/lua-api/index.rst
@@ -852,6 +852,10 @@ Proxy class
   Contain a table with the attached servers. The table is indexed by server
   name, and each server entry is an object of type :ref:`server_class`.
 
+.. js:attribute:: Proxy.stktable
+
+  Contains a stick table object attached to the proxy.
+
 .. js:attribute:: Proxy.listeners
 
   Contain a table with the attached listeners. The table is indexed by listener
@@ -2489,6 +2493,74 @@ AppletTCP class
   :see: :js:func:`AppletTCP.unset_var`
   :see: :js:func:`AppletTCP.set_var`
 
+StickTable class
+
+
+.. js:class:: StickTable
+
+  **context**: task, action, sample-fetch
+
+  This class can be used to access the HAProxy stick tables from Lua.
+
+.. js:function:: StickTable.info()
+
+  Returns stick table attributes as a Lua table. See HAProxy documentation for
+  "stick-table" for canonical info, or check out example bellow.
+
+  :returns: Lua table
+
+  Assume our table has IPv4 key and gpc0 and conn_rate "columns":
+
+.. code-block:: lua
+
+  {
+expire=,  # Value in ms
+size=,# Maximum table size
+used=,# Actual number of entries in table
+data={ # Data columns, with types as key, and periods as values
+ (-1 if type is not rate counter)
+  conn_rate=,
+  gpc0=-1
+},
+length=,  # max string length for string table keys, key length
+   # otherwise
+nopurge=, # purge oldest entries when table is full
+type="ip"  # can be "ip", "ipv6", "integer", "string", "binary"
+  }
+
+.. js:function:: StickTable.lookup(key)
+
+   Returns stick table entry for given 
+
+   :param string key: Stick table key (IP addresses and strings are supported)
+   :returns: Lua table
+
+.. js:function:: StickTable.dump([filter])
+
+   Returns all entries in stick table. An optional filter can be used
+   to extract entries with specific data values. Filter is a table with valid
+   comparison operators as keys followed by data type name and value pairs.
+   Check out the HAProxy docs for "show table" for more details. For the
+   reference, the supported operators are:
+ "eq", "ne", "le", "lt", "ge", "gt"
+
+   For large tables, execution of this function can take a long time (for
+   HAProxy standards). That's also true when filter is used, so take care and
+   measure the impact.
+
+   :param table filter: Stick table filter
+   :returns: Stick table entries (table)
+
+   See below for example filter, which contains 4 entries (or comparisons).
+   (Maximum number of filter entries is 4, defined in the source code)
+
+.. code-block:: lua
+
+local filter = {
+  {"gpc0", "gt", 30}, {"gpc1", "gt", 20}}, {"conn_rate", "le", 10}
+}
+
+
 External Lua libraries
 ==
 
diff --git a/include/types/hlua.h b/include/types/hlua.h
index 5a8173f3..2e453351 100644
--- a/include/types/hlua.h
+++ b/include/types/hlua.h
@@ -25,6 +25,7 @@
 #define CLASS_SERVER   "Server"
 #define CLASS_LISTENER "Listener"
 #define CLASS_REGEX"Regex"
+#define CLASS_STKTABLE "StickTable"
 
 struct stream;
 
diff --git a/src/hlua_fcn.c b/src/hlua_fcn.c
index c

Re: [PATCH] MEDIUM: lua: Add stick table support for Lua

2018-08-23 Thread Adis Nezirovic
On Thu, Aug 23, 2018 at 03:43:59PM +0200, Willy Tarreau wrote:
> Does this mean I should merge Adis' patch or do you want to verify
> other things ? Just let me know.

Willy,

I'll submit new patch later today with simplified filter definitions and
then we can ask Thierry for final ack for the patch.

Best regards,
Adis



Re: [PATCH] MEDIUM: lua: Add stick table support for Lua

2018-08-23 Thread Willy Tarreau
Hi Thierry,

On Thu, Aug 23, 2018 at 10:53:15AM +0200, Thierry Fournier wrote:
(...)
> Ok, it sounds good. I think this kind of syntax is easily understandable
> and it allow a good way for filtering values.

Does this mean I should merge Adis' patch or do you want to verify
other things ? Just let me know.

Thanks,
Willy



Re: [PATCH] MEDIUM: lua: Add stick table support for Lua

2018-08-23 Thread Thierry Fournier
Hi

[...]

>> I miss also the relation between oprators and between the content
>> of operators. I mean AND or OR. How I understand your example:
>> 
>> +local filter = {
>> +  lt={{"gpc0", 1}, {"gpc1", 2}},
>> +  gt={{"conn_rate", 3}},
>> +  eq={{"conn_cur", 4}}
>> +}
>> 
>> Are you sure that the syntax =
>> is a good format ? Maybe something like the following, with the operator
>> as argument between the two operands. lines are implicitly OR, and columns
>> are AND:
>> 
>> +local filter = {
>> +  {{"gpc0", "lt", 1}, {"gpc1", "lt", 2}},
>> +  {{"conn_rate", "gt", 3}},
>> +  {{"conn_cur", "eq", 4}}
>> +}
> Actually, I was playing with some other ideas, and it was useful to be
> able to "preselect" filter operators.
> However, the CLI doesn't even support more than one, maybe we don't need
> to complicate too much. Maybe we can simplify to this:
> 
>  local filter = {
>{"gpc0", "lt", 1},
>{"gpc1", "lt", 2},
>{"conn_rate", "gt", 3},
>{"conn_cur", "eq", 4}
>  }
> 
> The default operator would be AND, and we would not support other
> operators (to keep the things simple). e.g. example use case for the
> filter would be to filter out on gpc0 > X AND gpc1 > Y
> 
> If this sounds good, I can update/simplify the code.


Ok, it sounds good. I think this kind of syntax is easily understandable
and it allow a good way for filtering values.


>> Idea of extension for the future: Maybe it will be safe to compile
>> sticktable filter during the initialisation of the Lua code, to avoid
>> runtime errors ?
> I'm returning runtime errors since it can be easy to mix up data from
> the client side (most probably data would come as json table, then
> transformed to Lua table)


ok

[...]


>> Line 274 of your patch, I don't see any HA_SPIN_LOCK(STK_TABLE_LOCK
>> I don't known very well the thread, so maybe there are useles, maybe no.
> hlua_stktable_lookup() uses stktable_lookup_key() which does have locks,
> so I guess that it should be fine then?


sure !


[...]


>> l.365, 369: The user doesn't have context about the error. there are the
>> first entry of the table, the second ? Which operator doesn't exists ?
>> 
>> L.380, 384: Which line is wrong ?
> Yes, it is somwehat cryptic. I've tried to avoid returning user supplied
> data in the error messages. We can revisit this if/when we change the
> filter table format.


ok


>> L.431: Your release the lock, so the next element relative to the current
>> "n", can disappear and the ebmb_next() can return wrong memory.
> I was under impression that we only have to acquire lock and increment
> ref_cnt (so we can be sure our current node n is not deleted)
> ebmb_next() is called only when we're holding lock, first and every
> other iteration, i.e.
> 
>  HA_SPIN_LOCK(STK_TABLE_LOCK, >lock);
>  eb = ebmb_first(>keys);
>  for (n = eb; n; n = ebmb_next(n)) {
>  ...
>  ts->ref_cnt++;
>  HA_SPIN_UNLOCK(STK_TABLE_LOCK, >lock);
>  ...
> 
>  HA_SPIN_LOCK(STK_TABLE_LOCK, >lock);
>  }
> 
> Or I didn't get your point?


ok, you probably right. 


Thierry





Re: [PATCH] MEDIUM: lua: Add stick table support for Lua

2018-08-21 Thread Adis Nezirovic
rrent node n is not deleted)
ebmb_next() is called only when we're holding lock, first and every
other iteration, i.e.

  HA_SPIN_LOCK(STK_TABLE_LOCK, >lock);
  eb = ebmb_first(>keys);
  for (n = eb; n; n = ebmb_next(n)) {
  ...
  ts->ref_cnt++;
  HA_SPIN_UNLOCK(STK_TABLE_LOCK, >lock);
  ...

  HA_SPIN_LOCK(STK_TABLE_LOCK, >lock);
  }

Or I didn't get your point?
>From 1f7912cbdf9a664a43ab64505d8dd0415984ca4e Mon Sep 17 00:00:00 2001
From: Adis Nezirovic 
Date: Fri, 13 Jul 2018 12:18:33 +0200
Subject: [PATCH] MEDIUM: lua: Add stick table support for Lua (read-only ops).

---
 doc/lua-api/index.rst |  74 
 include/types/hlua.h  |   1 +
 src/hlua_fcn.c| 399 ++
 3 files changed, 474 insertions(+)

diff --git a/doc/lua-api/index.rst b/doc/lua-api/index.rst
index 0c79766e..f54a37fd 100644
--- a/doc/lua-api/index.rst
+++ b/doc/lua-api/index.rst
@@ -852,6 +852,10 @@ Proxy class
   Contain a table with the attached servers. The table is indexed by server
   name, and each server entry is an object of type :ref:`server_class`.
 
+.. js:attribute:: Proxy.stktable
+
+  Contains a stick table object attached to the proxy.
+
 .. js:attribute:: Proxy.listeners
 
   Contain a table with the attached listeners. The table is indexed by listener
@@ -2489,6 +2493,76 @@ AppletTCP class
   :see: :js:func:`AppletTCP.unset_var`
   :see: :js:func:`AppletTCP.set_var`
 
+StickTable class
+
+
+.. js:class:: StickTable
+
+  **context**: task, action, sample-fetch, converter
+
+  This class can be used to access the HAProxy stick tables from Lua.
+
+.. js:function:: StickTable.info()
+
+  Returns stick table attributes as a Lua table. See HAProxy documentation for
+  "stick-table" for canonical info, or check out example bellow.
+
+  :returns: Lua table
+
+  Assume our table has IPv4 key and gpc0 and conn_rate "columns":
+
+.. code-block:: lua
+
+  {
+expire=,  # Value in ms
+size=,# Maximum table size
+used=,# Actual number of entries in table
+data={ # Data columns, with types as key, and periods as values
+ (-1 if type is not rate counter)
+  conn_rate=,
+  gpc0=-1
+},
+length=,  # max string length for string table keys, key length
+   # otherwise
+nopurge=,
+type="ip"  # can be "ip", "ipv6", "integer", "string", "binary"
+  }
+
+.. js:function:: StickTable.lookup(key)
+
+   Returns stick table entry for given 
+
+   :param string key: Stick table key (IP addresses and strings are supported)
+   :returns: Lua table
+
+.. js:function:: StickTable.dump([filter])
+
+   Returns all entries in stick table. An optional filter can be used
+   to extract entries with specific data values. Filter is a table with valid
+   comparison operators as keys followed by data type name and value pairs.
+   Check out the HAProxy docs for "show table" for more details. For the
+   reference, the supported operators are:
+ "eq", "ne", "le", "lt", "ge", "gt"
+
+   For large tables, execution of this function can take a long time (for
+   HAProxy standards). That's also true when filter is used, so take care and
+   measure the impact.
+
+   :param table filter: Stick table filter
+   :returns: Stick table entries (table)
+
+   See below for example filter, which contains 4 entries (or comparisons).
+   (Maximum number of filter entries is 4, hardcoded in the source code)
+
+.. code-block:: lua
+
+local filter = {
+  lt={{"gpc0", 1}, {"gpc1", 2}},
+  gt={{"conn_rate", 3}},
+  eq={{"conn_cur", 4}}
+}
+
+
 External Lua libraries
 ==
 
diff --git a/include/types/hlua.h b/include/types/hlua.h
index 5a8173f3..2e453351 100644
--- a/include/types/hlua.h
+++ b/include/types/hlua.h
@@ -25,6 +25,7 @@
 #define CLASS_SERVER   "Server"
 #define CLASS_LISTENER "Listener"
 #define CLASS_REGEX"Regex"
+#define CLASS_STKTABLE "StickTable"
 
 struct stream;
 
diff --git a/src/hlua_fcn.c b/src/hlua_fcn.c
index cebce224..6c5c8995 100644
--- a/src/hlua_fcn.c
+++ b/src/hlua_fcn.c
@@ -30,6 +30,7 @@
 #include 
 #include 
 #include 
+#include 
 
 /* Contains the class reference of the concat object. */
 static int class_concat_ref;
@@ -37,7 +38,9 @@ static int class_proxy_ref;
 static int class_server_ref;
 static int class_listener_ref;
 static int class_regex_ref;
+static int class_stktable_ref;
 
+#define MAX_STK_FILTER_LEN 4
 #define STATS_LEN (MAX((int)ST_F_TOTAL_FIELDS, (int)INF_TOTAL_FIELDS))
 
 static THREAD_LOCAL struct field stats[STATS_LEN];
@@ -49,6 +52,38 @@ int hlua_checkboolean(lua_State *L, int index)
return lua_toboolean(L, index);
 }
 
+/* Helper 

Re: [PATCH] MEDIUM: lua: Add stick table support for Lua

2018-08-21 Thread Thierry Fournier
Hi Adis,

Thanks for this patch, it is a very useful class.

Some remark about the documentation and the formats:


js:function:: StickTable.info()
js:function:: StickTable.lookup(key)

Maybe the specification and an example of the returnes values
will be welcome, because I guess that the table keys are hardcoded.
Something like :

   {
  type =  -- list of given string,
  length = ,
  ...
   }

or

   {
  expire =  -- Specifiy the unit (ms or ?)
   }

js:function:: StickTable.dump([filter])

The exact list of allowed operators will helps the user to use
your class. It seems that string or regexes operators are not
allowed, only the integer operator are taken in account. This
list is "eq", "ne", "le", "lt", "ge", "gt".

Same remarqk for allowed data type. Maybe a link to the HAProxy
documentation will be sufficient for the data type.

I see in the code that the filters can exceed 4 entries. This
limitation must be written in the doc.

I miss also the relation between oprators and between the content
of operators. I mean AND or OR. How I understand your example:

+local filter = {
+  lt={{"gpc0", 1}, {"gpc1", 2}},
+  gt={{"conn_rate", 3}},
+  eq={{"conn_cur", 4}}
+}

( gpc0 >= 1 AND gpc1 >= 2 ) OR (conn_rate > 3) OR (conn_cur = 4)
   or
( gpc0 >= 1 OR gpc1 >= 2 ) OR (conn_rate > 3) OR (conn_cur = 4)
   or 
( gpc0 >= 1 AND gpc1 >= 2 ) AND (conn_rate > 3) AND (conn_cur = 4)
   or
( gpc0 >= 1 OR gpc1 >= 2 ) AND (conn_rate > 3) AND (conn_cur = 4)

Are you sure that the syntax =
is a good format ? Maybe something like the following, with the operator
as argument between the two operands. lines are implicitly OR, and columns
are AND:

+local filter = {
+  {{"gpc0", "lt", 1}, {"gpc1", "lt", 2}},
+  {{"conn_rate", "gt", 3}},
+  {{"conn_cur", "eq", 4}}
+}

It is read:

   ( gpc0 >= 1 OR gpc1 >= 2 ) AND (conn_rate > 3) AND (conn_cur = 4)

You can also implement a parser for natural language, in order to understand
directly the previous string ? This algoritm is very simple to implement:

   https://en.wikipedia.org/wiki/Shunting-yard_algorithm

Idea of extension for the future: Maybe it will be safe to compile
sticktable filter during the initialisation of the Lua code, to avoid
runtime errors ?


Other point with the doc of this function, you must specify that the
execution of this function can be very long for millions of entry, even
if a filter is specified because the stick table is entirely scanned.


some remarks about the code and the logs:
-

The line 182 of the patch contains space in place of tab.

Line 274 of your patch, I don't see any HA_SPIN_LOCK(STK_TABLE_LOCK
I don't known very well the thread, so maybe there are useles, maybe no.

Line 311: I see the decrement of the refcount without ATOMIC function
and whitout lock. Once again, I don't known very well the thread but
I send a warning. Maybe locks are useles, maybe no.

Line 286 of your patch. It seems that smp.flags is used uninitialized.
Maybe you should apply this change:

   -smp.flags |= SMP_F_CONST;
   +smp.flags = SMP_F_CONST;

l.365, 369: The user doesn't have context about the error. there are the
first entry of the table, the second ? Which operator doesn't exists ?

L.380, 384: Which line is wrong ?

L.431: Your release the lock, so the next element relative to the current
"n", can disappear and the ebmb_next() can return wrong memory.

That's all.
Thierry





> On 20 Aug 2018, at 15:15, Adis Nezirovic  wrote:
> 
> On Mon, Aug 20, 2018 at 02:11:13PM +0200, Adis Nezirovic wrote:
>> Hi guys,
>> 
>> I've attached a patch to add stick table support to Lua. Operations are
>> mostly similar to "show table" functionality from admin socket, namely:
>> 
>> - Show basic table info
>> - Key lookup
>> - Table dump, optionally using data/column filter
>> 
>> One side note, the code provides support for multiple filters
>> (4 by default) while CLI doesn't support that, nor it complains about
>> multiple "  " clauses.
>> 
>> Also, if this patch is accepted, maybe we can use provided helper
>> functions in other places in the code.
>> 
> 
> It's always funny to reply to self, right sending email to public I've
> spotted a bug. New patch attached.
> 
> SMT_T_SINT should be treated as ordinary signed integer, and shoud use
> lua_pushinteger() on it?
> 
> On many places in the code it is noted as "64" bit integer, but in
> stick_table.c it is defined as 32bit integer:
> 
> struct stktable_type stktable_types[SMP_TYPES] = {
>   [SMP_T_SINT] = { "integer", 0, 4 },
> 
> 
> Best regards,
> Adis
> <0001-MEDIUM-lua-Add-stick-table-support-for-Lua-read-only.patch>




Re: [PATCH] MEDIUM: lua: Add stick table support for Lua

2018-08-21 Thread Thierry Fournier


> On 20 Aug 2018, at 15:15, Adis Nezirovic  wrote:
> 
> On Mon, Aug 20, 2018 at 02:11:13PM +0200, Adis Nezirovic wrote:
>> Hi guys,
>> 
>> I've attached a patch to add stick table support to Lua. Operations are
>> mostly similar to "show table" functionality from admin socket, namely:
>> 
>> - Show basic table info
>> - Key lookup
>> - Table dump, optionally using data/column filter
>> 
>> One side note, the code provides support for multiple filters
>> (4 by default) while CLI doesn't support that, nor it complains about
>> multiple "  " clauses.


Hi Adis,

This is a great feature. I look this ASAP.


>> Also, if this patch is accepted, maybe we can use provided helper
>> functions in other places in the code.
>> 
> 
> It's always funny to reply to self, right sending email to public I've
> spotted a bug. New patch attached.
> 
> SMT_T_SINT should be treated as ordinary signed integer, and shoud use
> lua_pushinteger() on it?


There are a function which convert haproxy type in Lua types: hlua_arg2lua(),
and SINT is converted with lua_pushinteger(). I guess that stick tables SINT
are the same.


> On many places in the code it is noted as "64" bit integer, but in
> stick_table.c it is defined as 32bit integer:
> 
> struct stktable_type stktable_types[SMP_TYPES] = {
>   [SMP_T_SINT] = { "integer", 0, 4 },


The lua_pushinteger function takes a lua_Integer which is defined as known C
type (int, long or long long). So, when the function lua_pushinteger() is
called, the compilator perform a conversion between the type passed as argument
and the expected type. So I’m confident with the good behavior.

br,
Thierry


> Best regards,
> Adis
> <0001-MEDIUM-lua-Add-stick-table-support-for-Lua-read-only.patch>




Re: [PATCH] MEDIUM: lua: Add stick table support for Lua

2018-08-20 Thread Adis Nezirovic
On Mon, Aug 20, 2018 at 02:11:13PM +0200, Adis Nezirovic wrote:
> Hi guys,
>
> I've attached a patch to add stick table support to Lua. Operations are
> mostly similar to "show table" functionality from admin socket, namely:
>
> - Show basic table info
> - Key lookup
> - Table dump, optionally using data/column filter
>
> One side note, the code provides support for multiple filters
> (4 by default) while CLI doesn't support that, nor it complains about
> multiple "  " clauses.
>
> Also, if this patch is accepted, maybe we can use provided helper
> functions in other places in the code.
>

It's always funny to reply to self, right sending email to public I've
spotted a bug. New patch attached.

SMT_T_SINT should be treated as ordinary signed integer, and shoud use
lua_pushinteger() on it?

On many places in the code it is noted as "64" bit integer, but in
stick_table.c it is defined as 32bit integer:

struct stktable_type stktable_types[SMP_TYPES] = {
[SMP_T_SINT] = { "integer", 0, 4 },


Best regards,
Adis
>From 4c699b0d57ba5d6d5463595a4bdaa2f2ae8c2c56 Mon Sep 17 00:00:00 2001
From: Adis Nezirovic 
Date: Fri, 13 Jul 2018 12:18:33 +0200
Subject: [PATCH] MEDIUM: lua: Add stick table support for Lua (read-only ops).

---
 doc/lua-api/index.rst |  46 +
 include/types/hlua.h  |   1 +
 src/hlua_fcn.c| 401 ++
 3 files changed, 448 insertions(+)

diff --git a/doc/lua-api/index.rst b/doc/lua-api/index.rst
index 0c79766e..63848661 100644
--- a/doc/lua-api/index.rst
+++ b/doc/lua-api/index.rst
@@ -852,6 +852,10 @@ Proxy class
   Contain a table with the attached servers. The table is indexed by server
   name, and each server entry is an object of type :ref:`server_class`.
 
+.. js:attribute:: Proxy.stktable
+
+  Contains a stick table object attached to the proxy.
+
 .. js:attribute:: Proxy.listeners
 
   Contain a table with the attached listeners. The table is indexed by listener
@@ -2489,6 +2493,48 @@ AppletTCP class
   :see: :js:func:`AppletTCP.unset_var`
   :see: :js:func:`AppletTCP.set_var`
 
+StickTable class
+
+
+.. js:class:: StickTable
+
+  This class is used with applets that requires the 'tcp' mode. The tcp applet
+  can be registered with the *core.register_service()* function. They are used
+  for processing a tcp stream like a server in back of HAProxy.
+
+.. js:function:: StickTable.info()
+
+  Returns relevant stick table attributes: type, length, size, used, nopurge,
+  expire and exact interval values for frequency data columns.
+
+  :returns: Lua table
+
+.. js:function:: StickTable.lookup(key)
+
+   Returns stick table entry for given 
+
+   :param string key: Stick table key (IP addresses and strings are supported)
+   :returns: Lua table
+
+.. js:function:: StickTable.dump([filter])
+
+   Returns all entries in stick table. An optional filter can be used
+   to extract entries with specific data values. Filter is a table with valid
+   comparison operators as keys followed by data type name and value pairs.
+   e.g.
+
+   :param table filter: Stick table filter
+   :returns: Stick table entries (table)
+
+.. code-block:: lua
+
+local filter = {
+  lt={{"gpc0", 1}, {"gpc1", 2}},
+  gt={{"conn_rate", 3}},
+  eq={{"conn_cur", 4}}
+}
+
+
 External Lua libraries
 ==
 
diff --git a/include/types/hlua.h b/include/types/hlua.h
index 5a8173f3..2e453351 100644
--- a/include/types/hlua.h
+++ b/include/types/hlua.h
@@ -25,6 +25,7 @@
 #define CLASS_SERVER   "Server"
 #define CLASS_LISTENER "Listener"
 #define CLASS_REGEX"Regex"
+#define CLASS_STKTABLE "StickTable"
 
 struct stream;
 
diff --git a/src/hlua_fcn.c b/src/hlua_fcn.c
index cebce224..f38b9f37 100644
--- a/src/hlua_fcn.c
+++ b/src/hlua_fcn.c
@@ -30,6 +30,7 @@
 #include 
 #include 
 #include 
+#include 
 
 /* Contains the class reference of the concat object. */
 static int class_concat_ref;
@@ -37,7 +38,9 @@ static int class_proxy_ref;
 static int class_server_ref;
 static int class_listener_ref;
 static int class_regex_ref;
+static int class_stktable_ref;
 
+#define MAX_STK_FILTER_LEN 4
 #define STATS_LEN (MAX((int)ST_F_TOTAL_FIELDS, (int)INF_TOTAL_FIELDS))
 
 static THREAD_LOCAL struct field stats[STATS_LEN];
@@ -49,6 +52,38 @@ int hlua_checkboolean(lua_State *L, int index)
return lua_toboolean(L, index);
 }
 
+/* Helper to push unsigned integers to Lua stack, respecting Lua limitations  
*/
+static int hlua_fcn_pushunsigned(lua_State *L, unsigned int val)
+{
+#if (LUA_MAXINTEGER == LLONG_MAX || ((LUA_MAXINTEGER == LONG_MAX) && 
(__WORDSIZE == 64)))
+   lua_pushinteger(L, val);
+#else
+   if (val > INT_MAX)
+   lua_pushnumber(L, (lua_Number)val);
+   else
+   lua_pushi

[PATCH] MEDIUM: lua: Add stick table support for Lua

2018-08-20 Thread Adis Nezirovic
Hi guys,

I've attached a patch to add stick table support to Lua. Operations are
mostly similar to "show table" functionality from admin socket, namely:

- Show basic table info
- Key lookup
- Table dump, optionally using data/column filter

One side note, the code provides support for multiple filters
(4 by default) while CLI doesn't support that, nor it complains about
multiple "  " clauses.

Also, if this patch is accepted, maybe we can use provided helper
functions in other places in the code.

Best regards,
Adis
>From 27d0c08a85e8a997fb96e65937c70e983e31c21e Mon Sep 17 00:00:00 2001
From: Adis Nezirovic 
Date: Fri, 13 Jul 2018 12:18:33 +0200
Subject: [PATCH] MEDIUM: lua: Add stick table support for Lua (read-only ops).

---
 doc/lua-api/index.rst |  46 +
 include/types/hlua.h  |   1 +
 src/hlua_fcn.c| 401 ++
 3 files changed, 448 insertions(+)

diff --git a/doc/lua-api/index.rst b/doc/lua-api/index.rst
index 0c79766e..63848661 100644
--- a/doc/lua-api/index.rst
+++ b/doc/lua-api/index.rst
@@ -852,6 +852,10 @@ Proxy class
   Contain a table with the attached servers. The table is indexed by server
   name, and each server entry is an object of type :ref:`server_class`.
 
+.. js:attribute:: Proxy.stktable
+
+  Contains a stick table object attached to the proxy.
+
 .. js:attribute:: Proxy.listeners
 
   Contain a table with the attached listeners. The table is indexed by listener
@@ -2489,6 +2493,48 @@ AppletTCP class
   :see: :js:func:`AppletTCP.unset_var`
   :see: :js:func:`AppletTCP.set_var`
 
+StickTable class
+
+
+.. js:class:: StickTable
+
+  This class is used with applets that requires the 'tcp' mode. The tcp applet
+  can be registered with the *core.register_service()* function. They are used
+  for processing a tcp stream like a server in back of HAProxy.
+
+.. js:function:: StickTable.info()
+
+  Returns relevant stick table attributes: type, length, size, used, nopurge,
+  expire and exact interval values for frequency data columns.
+
+  :returns: Lua table
+
+.. js:function:: StickTable.lookup(key)
+
+   Returns stick table entry for given 
+
+   :param string key: Stick table key (IP addresses and strings are supported)
+   :returns: Lua table
+
+.. js:function:: StickTable.dump([filter])
+
+   Returns all entries in stick table. An optional filter can be used
+   to extract entries with specific data values. Filter is a table with valid
+   comparison operators as keys followed by data type name and value pairs.
+   e.g.
+
+   :param table filter: Stick table filter
+   :returns: Stick table entries (table)
+
+.. code-block:: lua
+
+local filter = {
+  lt={{"gpc0", 1}, {"gpc1", 2}},
+  gt={{"conn_rate", 3}},
+  eq={{"conn_cur", 4}}
+}
+
+
 External Lua libraries
 ==
 
diff --git a/include/types/hlua.h b/include/types/hlua.h
index 5a8173f3..2e453351 100644
--- a/include/types/hlua.h
+++ b/include/types/hlua.h
@@ -25,6 +25,7 @@
 #define CLASS_SERVER   "Server"
 #define CLASS_LISTENER "Listener"
 #define CLASS_REGEX"Regex"
+#define CLASS_STKTABLE "StickTable"
 
 struct stream;
 
diff --git a/src/hlua_fcn.c b/src/hlua_fcn.c
index cebce224..49f7ef03 100644
--- a/src/hlua_fcn.c
+++ b/src/hlua_fcn.c
@@ -30,6 +30,7 @@
 #include 
 #include 
 #include 
+#include 
 
 /* Contains the class reference of the concat object. */
 static int class_concat_ref;
@@ -37,7 +38,9 @@ static int class_proxy_ref;
 static int class_server_ref;
 static int class_listener_ref;
 static int class_regex_ref;
+static int class_stktable_ref;
 
+#define MAX_STK_FILTER_LEN 4
 #define STATS_LEN (MAX((int)ST_F_TOTAL_FIELDS, (int)INF_TOTAL_FIELDS))
 
 static THREAD_LOCAL struct field stats[STATS_LEN];
@@ -49,6 +52,38 @@ int hlua_checkboolean(lua_State *L, int index)
return lua_toboolean(L, index);
 }
 
+/* Helper to push unsigned integers to Lua stack, respecting Lua limitations  
*/
+static int hlua_fcn_pushunsigned(lua_State *L, unsigned int val)
+{
+#if (LUA_MAXINTEGER == LLONG_MAX || ((LUA_MAXINTEGER == LONG_MAX) && 
(__WORDSIZE == 64)))
+   lua_pushinteger(L, val);
+#else
+   if (val > INT_MAX)
+   lua_pushnumber(L, (lua_Number)val);
+   else
+   lua_pushinteger(L, (int)val);
+#endif
+   return 1;
+}
+
+/* Helper to push unsigned long long to Lua stack, respecting Lua limitations  
*/
+static int hlua_fcn_pushunsigned_ll(lua_State *L, unsigned long long val) {
+#if (LUA_MAXINTEGER == LLONG_MAX || ((LUA_MAXINTEGER == LONG_MAX) && 
(__WORDSIZE == 64)))
+   /* 64 bits case, U64 is supported until LLONG_MAX */
+   if (val > LLONG_MAX)
+   lua_pushnumber(L, (lua_Number)val);
+   else
+   lua_pushinteger(L, val);
+#else
+   /* 32 bits case, U64 is supported until INT_MAX */
+   if