Re: Adding Serial Number to POST Requests

2014-07-18 Thread Baptiste
On Fri, Jul 18, 2014 at 9:12 AM, Neil - HAProxy List
maillist-hapr...@iamafreeman.com wrote:
 Hi

 Using peers should prevent the reset when restarting?

 Neil


With Willy's solution, yes, it should do the job.

Baptiste



Re: Adding Serial Number to POST Requests

2014-07-16 Thread Zuoning Yin
We later also got the help from Willy.  He provided us a configuration
which solved our problem. To benefit other people,  I just posted it here.

 Willy's response ###


 I
 actually asked this question in gmane and Baptiste also suggested that we
 could do this in header.

 However, we failed to find an example config about this issue after quite
a
 lot of googling. If possible, would you please provide a config snippet
 which can do our job?

 What we need is that:

 1)  Have a counter to count the POST requests
 2)  When forwarding http requests (both POST and GET), add the value of
 this counter as a header

OK so it's not exactly a request ID since multiple requests will have the
same ID, it's a POST counter.

 Baptiste suggested that we should use some stick table along with some
 counters, but we can't figure out how to do it exactly (Sorry, we are
still
 new to Haproxy).

I understand why he suggested a stick-table : stick-tables allow you to
store a few metrics among which a general purpose counter that you can
increment when you want (gpc0).

 Another question, I found the counter gpc0 is just 32 bit. Is there any
 chance we can have a 64 bit counter?

No, but in a more ugly fashion, what could be done would be to track two
gpc0 (one per stick table) and send them both.

Let's try something like this :

# this is ugly, but we need only one key for the lower 32-bit,
# and one for the higher 32-bit. So we track two constants, one
# which is a boolean false, cast to integer 0, and one which is
# boolean true, cast to integer 1.
tcp-request connection track-sc0 always_false table counter
tcp-request connection track-sc1 always_true table counter

# increment sc0_gpc0 on POST, increment sc1_gpc0 when sc0_gpc0
overflows,
# and send both values as a header in hex form.
http-request set-header X-post-counter %[sc1_get_gpc0]:%[sc0_get_gpc0]
if METH_POST { sc0_inc_gpc0 gt 0 } || METH_POST { sc1_inc_gpc0 gt 0 }

backend counter
stick-table type integer size 2 store gpc0

It will produce an output looking like this :

   GET / HTTP/1.1
   Host: foo
   X-post-counter: 0:521

Here the counter should be interpreted as (0  32) + 521. The right word
(521) will wrap after 2^32-1 and the left one will be incremented by one
upon every wrapping.

Regards,
Willy

##


Thanks both Willy and Baptiste!

--Zuoning


On Thu, Jul 10, 2014 at 8:21 PM, Zuoning Yin zuon...@graphsql.com wrote:

 Hi Baptiste,
  Thanks so much for the reply. It is good to know stick table can help
 with my case. I had tried some further googling, but can't find a similar
 example that I can follow. Sorry, I am still new to HAProxy.
  It will be highly appreciated if you could provide some sample config
 snippet about this particular issue.
  Before that, please allow me reiterate the goal that we want to
 achieve (I omitted some details in previous POST).
  We want a global counter for POST requests (I guess we can use gpc0
 here). For every POST request, we need to increase the counter by 1. We
 will also have GET requests, but we don't do anything with the counter for
 GET.  Then when we forward requests to backends, we want to append this
 global counter to the request URL.
  For example, assume the value of current global counter is 1001, we
 need:

  curl -X POST -H 'Content-Type: application/json' -d
  '{key1:value1}'  http://localhost:9000/update
  ==
  curl -X POST -H 'Content-Type: application/json' -d
  '{key1:value1}'  http://localhost:9000/update/1001

  curl -X GET  http://localhost:9000/query
  ==
  curl -X GET  http://localhost:9000/query/1001

  I guess I need to define a counter in a stick table. Then define some
 acl to increase the counter. Then some rewrite rules to use this counter.
 However, I just don't know how to write the config for these tasks.

 Thanks,
 --Zuoning










 On Thu, Jul 10, 2014 at 6:16 PM, Baptiste bed...@gmail.com wrote:

 On Thu, Jul 10, 2014 at 11:27 PM, Zuoning Yin zuon...@graphsql.com
 wrote:
  Hi All,
   We recently used haproxy as the load balancer in our system and
  it really worked great. However,  we still need one extra feature here.
   For every POST request, we want to be able to append an id (or
  serial number) to it. Essentially, we are trying to serializing the POST
  requests.
   For example,  for the following POST requests,
 
   a.1) curl -X POST -H 'Content-Type: application/json' -d
  '{key1:value1}'
  http://localhost:9000/update
   a.2) curl -X POST -H 'Content-Type: application/json' -d
  '{key2:value2}'
  http://localhost:9000/update
   a.3) curl -X POST -H 'Content-Type: application/json' -d
  '{key3:value3}'
  http://localhost:9000/update
 
  What we want is that haproxy could append a serial number to them.
  Therefore, they will 

Re: Adding Serial Number to POST Requests

2014-07-16 Thread Jonathan Matthews
On 16 Jul 2014 16:56, Zuoning Yin zuon...@graphsql.com wrote:

 We later also got the help from Willy.  He provided us a configuration
which solved our problem. To benefit other people,  I just posted it here.

I had meant to chime in on this thread earlier.

What happens when your HAProxy layer loses state - be it reboot, service
restart or  data centre power cut? Are you risking resetting the counter
and overwriting existing data on the backend? Are you in fact treating HAP
as a single point of truth?

J


Re: Adding Serial Number to POST Requests

2014-07-16 Thread Baptiste
On Wed, Jul 16, 2014 at 7:04 PM, Jonathan Matthews
cont...@jpluscplusm.com wrote:
 On 16 Jul 2014 16:56, Zuoning Yin zuon...@graphsql.com wrote:

 We later also got the help from Willy.  He provided us a configuration
 which solved our problem. To benefit other people,  I just posted it here.

 I had meant to chime in on this thread earlier.

 What happens when your HAProxy layer loses state - be it reboot, service
 restart or  data centre power cut? Are you risking resetting the counter and
 overwriting existing data on the backend? Are you in fact treating HAP as a
 single point of truth?

 J

That's why I propose to use the unique-id format as well.
You can combine a timestamp with your unique id.
So going back to 0 is not a problem anymore.

Baptiste



Re: Adding Serial Number to POST Requests

2014-07-10 Thread Baptiste
On Thu, Jul 10, 2014 at 11:27 PM, Zuoning Yin zuon...@graphsql.com wrote:
 Hi All,
  We recently used haproxy as the load balancer in our system and
 it really worked great. However,  we still need one extra feature here.
  For every POST request, we want to be able to append an id (or
 serial number) to it. Essentially, we are trying to serializing the POST
 requests.
  For example,  for the following POST requests,

  a.1) curl -X POST -H 'Content-Type: application/json' -d
 '{key1:value1}'
 http://localhost:9000/update
  a.2) curl -X POST -H 'Content-Type: application/json' -d
 '{key2:value2}'
 http://localhost:9000/update
  a.3) curl -X POST -H 'Content-Type: application/json' -d
 '{key3:value3}'
 http://localhost:9000/update

 What we want is that haproxy could append a serial number to them.
 Therefore, they will become

  b.1) curl -X POST -H 'Content-Type: application/json' -d
 '{key1:value1}'
 http://localhost:9000/update/10001
  b.2) curl -X POST -H 'Content-Type: application/json' -d
 '{key2:value2}'
 http://localhost:9000/update/10002
  b.3) curl -X POST -H 'Content-Type: application/json' -d
 '{key3:value3}'
 http://localhost:9000/update/10003

  Here, 10001 , 10002 and 10003  are the serial numbers appended
 to the original URL. Our backend servers will consume these serial numbers.

  I did some googling, but failed to find any reference about what we
 want
 to achieve. I am guessing we may need to tweak the src code a little bit to
 achieve
 the goal.
  So here are my questions:
  1) is there any configuration change which can achieve our goal?
  2) if 1) is not possible, what part of the code (which file/function) I
 need to
 look at to implement the hack? I guess there might be some global variables
 that relate to this issue.
  3) if we are doing 2), what level of performance overhead will be
 expected?

 Thanks so much,
 --Zuoning


Hi Zuoning,

You can create a stick table in which you store URLs and count the
number of hit per URL.
Then when forwarding POST to the server, you can append the number of
hit for this particular URL.
Note that after a reload / restart of HAProxy, the number will come back to 0.
An other way could be using the unique-id feature. I let you read the
man about it.

That said, I'm not fan of integrating application logic in the LB layer...

Baptiste



Re: Adding Serial Number to POST Requests

2014-07-10 Thread Zuoning Yin
Hi Baptiste,
 Thanks so much for the reply. It is good to know stick table can help
with my case. I had tried some further googling, but can't find a similar
example that I can follow. Sorry, I am still new to HAProxy.
 It will be highly appreciated if you could provide some sample config
snippet about this particular issue.
 Before that, please allow me reiterate the goal that we want to
achieve (I omitted some details in previous POST).
 We want a global counter for POST requests (I guess we can use gpc0
here). For every POST request, we need to increase the counter by 1. We
will also have GET requests, but we don't do anything with the counter for
GET.  Then when we forward requests to backends, we want to append this
global counter to the request URL.
 For example, assume the value of current global counter is 1001, we
need:

 curl -X POST -H 'Content-Type: application/json' -d
 '{key1:value1}'  http://localhost:9000/update
 ==
 curl -X POST -H 'Content-Type: application/json' -d
 '{key1:value1}'  http://localhost:9000/update/1001

 curl -X GET  http://localhost:9000/query
 ==
 curl -X GET  http://localhost:9000/query/1001

 I guess I need to define a counter in a stick table. Then define some
acl to increase the counter. Then some rewrite rules to use this counter.
However, I just don't know how to write the config for these tasks.

Thanks,
--Zuoning










On Thu, Jul 10, 2014 at 6:16 PM, Baptiste bed...@gmail.com wrote:

 On Thu, Jul 10, 2014 at 11:27 PM, Zuoning Yin zuon...@graphsql.com
 wrote:
  Hi All,
   We recently used haproxy as the load balancer in our system and
  it really worked great. However,  we still need one extra feature here.
   For every POST request, we want to be able to append an id (or
  serial number) to it. Essentially, we are trying to serializing the POST
  requests.
   For example,  for the following POST requests,
 
   a.1) curl -X POST -H 'Content-Type: application/json' -d
  '{key1:value1}'
  http://localhost:9000/update
   a.2) curl -X POST -H 'Content-Type: application/json' -d
  '{key2:value2}'
  http://localhost:9000/update
   a.3) curl -X POST -H 'Content-Type: application/json' -d
  '{key3:value3}'
  http://localhost:9000/update
 
  What we want is that haproxy could append a serial number to them.
  Therefore, they will become
 
   b.1) curl -X POST -H 'Content-Type: application/json' -d
  '{key1:value1}'
  http://localhost:9000/update/10001
   b.2) curl -X POST -H 'Content-Type: application/json' -d
  '{key2:value2}'
  http://localhost:9000/update/10002
   b.3) curl -X POST -H 'Content-Type: application/json' -d
  '{key3:value3}'
  http://localhost:9000/update/10003
 
   Here, 10001 , 10002 and 10003  are the serial numbers
 appended
  to the original URL. Our backend servers will consume these serial
 numbers.
 
   I did some googling, but failed to find any reference about what we
  want
  to achieve. I am guessing we may need to tweak the src code a little bit
 to
  achieve
  the goal.
   So here are my questions:
   1) is there any configuration change which can achieve our goal?
   2) if 1) is not possible, what part of the code (which
 file/function) I
  need to
  look at to implement the hack? I guess there might be some global
 variables
  that relate to this issue.
   3) if we are doing 2), what level of performance overhead will be
  expected?
 
  Thanks so much,
  --Zuoning
 

 Hi Zuoning,

 You can create a stick table in which you store URLs and count the
 number of hit per URL.
 Then when forwarding POST to the server, you can append the number of
 hit for this particular URL.
 Note that after a reload / restart of HAProxy, the number will come back
 to 0.
 An other way could be using the unique-id feature. I let you read the
 man about it.

 That said, I'm not fan of integrating application logic in the LB layer...

 Baptiste