Thierry FOURNIER | 79c1051 | 2015-11-08 22:02:21 +0100 | [diff] [blame] | 1 | Lua: Architecture and first steps |
| 2 | ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ |
Willy Tarreau | 5a753bd | 2020-03-09 14:57:20 +0100 | [diff] [blame] | 3 | version 2.2 |
Thierry FOURNIER | 79c1051 | 2015-11-08 22:02:21 +0100 | [diff] [blame] | 4 | |
| 5 | author: Thierry FOURNIER |
| 6 | contact: tfournier at arpalert dot org |
| 7 | |
| 8 | |
| 9 | |
| 10 | HAProxy is a powerful load balancer. It embeds many options and many |
| 11 | configuration styles in order to give a solution to many load balancing |
| 12 | problems. However, HAProxy is not universal and some special or specific |
Godbach | 06c8099 | 2016-02-16 11:59:17 +0800 | [diff] [blame] | 13 | problems do not have solution with the native software. |
Thierry FOURNIER | 79c1051 | 2015-11-08 22:02:21 +0100 | [diff] [blame] | 14 | |
| 15 | This text is not a full explanation of the Lua syntax. |
| 16 | |
| 17 | This text is not a replacement of the HAProxy Lua API documentation. The API |
| 18 | documentation can be found at the project root, in the documentation directory. |
| 19 | The goal of this text is to discover how Lua is implemented in HAProxy and using |
| 20 | it efficiently. |
| 21 | |
| 22 | However, this can be read by Lua beginners. Some examples are detailed. |
| 23 | |
| 24 | Why a scripting language in HAProxy |
| 25 | =================================== |
| 26 | |
| 27 | HAProxy 1.5 makes at possible to do many things using samples, but some people |
Godbach | 06c8099 | 2016-02-16 11:59:17 +0800 | [diff] [blame] | 28 | want to more combining results of samples fetches, programming conditions and |
Ilya Shipitsin | 2075ca8 | 2020-03-06 23:22:22 +0500 | [diff] [blame] | 29 | loops which is not possible. Sometimes people implement these functionalities |
Thierry FOURNIER | 79c1051 | 2015-11-08 22:02:21 +0100 | [diff] [blame] | 30 | in patches which have no meaning outside their network. These people must |
| 31 | maintain these patches, or worse we must integrate them in the HAProxy |
| 32 | mainstream. |
| 33 | |
| 34 | Their need is to have an embedded programming language in order to no longer |
| 35 | modify the HAProxy source code, but to write their own control code. Lua is |
| 36 | encountered very often in the software industry, and in some open source |
| 37 | projects. It is easy to understand, efficient, light without external |
Joseph Herlant | 71b4b15 | 2018-11-13 16:55:16 -0800 | [diff] [blame] | 38 | dependencies, and leaves the resource control to the implementation. Its design |
Thierry FOURNIER | 79c1051 | 2015-11-08 22:02:21 +0100 | [diff] [blame] | 39 | is close to the HAProxy philosophy which uses components for what they do |
| 40 | perfectly. |
| 41 | |
| 42 | The HAProxy control block allows one to take a decision based on the comparison |
| 43 | between samples and patterns. The samples are extracted using fetch functions |
| 44 | easily extensible, and are used by actions which are also extensible. It seems |
| 45 | natural to allow Lua to give samples, modify them, and to be an action target. |
| 46 | So, Lua uses the same entities as the configuration language. This is the most |
Godbach | 06c8099 | 2016-02-16 11:59:17 +0800 | [diff] [blame] | 47 | natural and reliable way for the Lua integration. So, the Lua engine allows one |
Thierry FOURNIER | 79c1051 | 2015-11-08 22:02:21 +0100 | [diff] [blame] | 48 | to add new sample fetch functions, new converter functions and new actions. |
| 49 | These new entities can access the existing samples fetches and converters |
| 50 | allowing to extend them without rewriting them. |
| 51 | |
| 52 | The writing of the first Lua functions shows that implementing complex concepts |
| 53 | like protocol analysers is easy and can be extended to full services. It appears |
| 54 | that these services are not easy to implement with the HAProxy configuration |
Godbach | 06c8099 | 2016-02-16 11:59:17 +0800 | [diff] [blame] | 55 | model which is based on four steps: fetch, convert, compare and action. HAProxy |
Thierry FOURNIER | 79c1051 | 2015-11-08 22:02:21 +0100 | [diff] [blame] | 56 | is extended with a notion of services which are a formalisation of the existing |
| 57 | services like stats, cli and peers. The service is an autonomous entity with a |
| 58 | behaviour pattern close to that of an external client or server. The Lua engine |
| 59 | inherits from this new service and offers new possibilities for writing |
| 60 | services. |
| 61 | |
| 62 | This scripting language is useful for testing new features as proof of concept. |
| 63 | Later, if there is general interest, the proof of concept could be integrated |
| 64 | with C language in the HAProxy core. |
| 65 | |
Godbach | 06c8099 | 2016-02-16 11:59:17 +0800 | [diff] [blame] | 66 | The HAProxy Lua integration also provides a simple way for distributing Lua |
Thierry FOURNIER | 79c1051 | 2015-11-08 22:02:21 +0100 | [diff] [blame] | 67 | packages. The final user needs only to install the Lua file, load it in HAProxy |
| 68 | and follow the attached documentation. |
| 69 | |
| 70 | Design and technical things |
| 71 | =========================== |
| 72 | |
| 73 | Lua is integrated into the HAProxy event driven core. We want to preserve the |
| 74 | fast processing of HAProxy. To ensure this, we implement some technical concepts |
| 75 | between HAProxy and the Lua library. |
| 76 | |
| 77 | The following paragraph also describes the interactions between Lua and HAProxy |
| 78 | from a technical point of view. |
| 79 | |
| 80 | Prerequisite |
| 81 | ----------- |
| 82 | |
| 83 | Reading the following documentation links is required to understand the |
| 84 | current paragraph: |
| 85 | |
Willy Tarreau | daac1e4 | 2018-04-19 15:12:26 +0200 | [diff] [blame] | 86 | HAProxy doc: http://cbonte.github.io/haproxy-dconv/ |
Thierry FOURNIER | 79c1051 | 2015-11-08 22:02:21 +0100 | [diff] [blame] | 87 | Lua API: http://www.lua.org/manual/5.3/ |
Willy Tarreau | daac1e4 | 2018-04-19 15:12:26 +0200 | [diff] [blame] | 88 | HAProxy API: http://www.arpalert.org/src/haproxy-lua-api/1.9dev/index.html |
Thierry FOURNIER | 79c1051 | 2015-11-08 22:02:21 +0100 | [diff] [blame] | 89 | Lua guide: http://www.lua.org/pil/ |
| 90 | |
| 91 | more about Lua choice |
| 92 | --------------------- |
| 93 | |
| 94 | Lua language is very simple to extend. It is easy to add new functions written |
Godbach | 06c8099 | 2016-02-16 11:59:17 +0800 | [diff] [blame] | 95 | in C in the core language. It is not required to embed very intrusive libraries, |
| 96 | and we do not change compilation processes. |
Thierry FOURNIER | 79c1051 | 2015-11-08 22:02:21 +0100 | [diff] [blame] | 97 | |
| 98 | The amount of memory consumed can be controlled, and the issues due to lack of |
| 99 | memory are perfectly caught. The maximum amount of memory allowed for the Lua |
| 100 | processes is configurable. If some memory is missing, the current Lua action |
| 101 | fails, and the HAProxy processing flow continues. |
| 102 | |
| 103 | Lua provides a way for implementing event driven design. When the Lua code |
| 104 | wants to do a blocking action, the action is started, it executes non blocking |
| 105 | operations, and returns control to the HAProxy scheduler when it needs to wait |
| 106 | for some external event. |
| 107 | |
| 108 | The Lua process can be interrupted after a number of instructions executed. The |
| 109 | Lua execution will resume later. This is a useful way for controlling the |
| 110 | execution time. This system also keeps HAProxy responsive. When the Lua |
| 111 | execution is interrupted, HAProxy accepts some connections or transfers pending |
| 112 | data. The Lua execution does not block the main HAProxy processing, except in |
| 113 | some cases which we will see later. |
| 114 | |
| 115 | Lua function integration |
| 116 | ------------------------ |
| 117 | |
| 118 | The Lua actions, sample fetches, converters and services are integrated in |
| 119 | HAProxy with "register_*" functions. The register system is a choice for |
| 120 | providing HAProxy Lua packages easily. The register system adds new sample |
| 121 | fetches, converters, actions or services usable in the HAProxy configuration |
| 122 | file. |
| 123 | |
| 124 | The register system is defined in the "core" functions collection. This |
| 125 | collection is provided by HAProxy and is always available. Below, the list of |
| 126 | these functions: |
| 127 | |
| 128 | - core.register_action() |
| 129 | - core.register_converters() |
| 130 | - core.register_fetches() |
| 131 | - core.register_init() |
| 132 | - core.register_service() |
| 133 | - core.register_task() |
| 134 | |
| 135 | These functions are the execution entry points. |
| 136 | |
| 137 | HTTP action must be used for manipulating HTTP request headers. This action |
| 138 | can not manipulates HTTP content. It is dangerous to use the channel |
| 139 | manipulation object with an HTTP request in an HTTP action. The channel |
| 140 | manipulation can transform a valid request in an invalid request. In this case, |
| 141 | the action will never resume and the processing will be frozen. HAProxy |
| 142 | discards the request after the reception timeout. |
| 143 | |
| 144 | Non blocking design |
| 145 | ------------------- |
| 146 | |
| 147 | HAProxy is an event driven software, so blocking system calls are absolutely |
| 148 | forbidden. However, the Lua allows to do blocking actions. When an action |
| 149 | blocks, HAProxy is waiting and do nothing, so the basic functionalities like |
| 150 | accepting connections or forwarding data are blocked while the end of the system |
| 151 | call. In this case HAProxy will be less responsive. |
| 152 | |
| 153 | This is very insidious because when the developer tries to execute its Lua code |
| 154 | with only one stream, HAProxy seems to run fine. When the code is used with |
| 155 | production stream, HAProxy encounters some slow processing, and it cannot |
| 156 | hold the load. |
| 157 | |
| 158 | However, during the initialisation state, you can obviously using blocking |
| 159 | functions. There are typically used for loading files. |
| 160 | |
| 161 | The list of prohibited standard Lua functions during the runtime contains all |
| 162 | that do filesystem access: |
| 163 | |
| 164 | - os.remove() |
| 165 | - os.rename() |
| 166 | - os.tmpname() |
| 167 | - package.*() |
| 168 | - io.*() |
| 169 | - file.*() |
| 170 | |
| 171 | Some other functions are prohibited: |
| 172 | |
| 173 | - os.execute(), waits for the end of the required execution blocking HAProxy. |
| 174 | |
Godbach | 06c8099 | 2016-02-16 11:59:17 +0800 | [diff] [blame] | 175 | - os.exit(), is not really dangerous for the process, but it's not the good way |
Thierry FOURNIER | 79c1051 | 2015-11-08 22:02:21 +0100 | [diff] [blame] | 176 | for exiting the HAProxy process. |
| 177 | |
| 178 | - print(), writes data on stdout. In some cases these writes are blocking, the |
| 179 | best practice is reserving this call for debugging. We must prefer |
| 180 | to use core.log() or TXN.log() for sending messages. |
| 181 | |
| 182 | Some HAProxy functions have a blocking behaviour pattern in the Lua code, but |
| 183 | there are compatible with the non blocking design. These functions are: |
| 184 | |
| 185 | - All the socket class |
| 186 | - core.sleep() |
| 187 | |
| 188 | Responsive design |
| 189 | ----------------- |
| 190 | |
Godbach | 06c8099 | 2016-02-16 11:59:17 +0800 | [diff] [blame] | 191 | HAProxy must process connections accept, forwarding data and processing timeouts |
Thierry FOURNIER | 79c1051 | 2015-11-08 22:02:21 +0100 | [diff] [blame] | 192 | as soon as possible. The first thing is to believe that a Lua script with a long |
| 193 | execution time should impact the expected responsive behaviour. |
| 194 | |
| 195 | It is not the case, the Lua script execution are regularly interrupted, and |
| 196 | HAProxy can process other things. These interruptions are exprimed in number of |
| 197 | Lua instructions. The number of interruptions between two interrupts is |
| 198 | configured with the following "tune" option: |
| 199 | |
| 200 | tune.lua.forced-yield <nb> |
| 201 | |
| 202 | The default value is 10 000. For determining it, I ran benchmark on my laptop. |
Godbach | 06c8099 | 2016-02-16 11:59:17 +0800 | [diff] [blame] | 203 | I executed a Lua loop between 10 seconds with different values for the |
Thierry FOURNIER | 79c1051 | 2015-11-08 22:02:21 +0100 | [diff] [blame] | 204 | "tune.lua.forced-yield" option, and I noted the results: |
| 205 | |
| 206 | configured | Number of |
| 207 | instructions | loops executed |
Godbach | 06c8099 | 2016-02-16 11:59:17 +0800 | [diff] [blame] | 208 | between two | in millions |
Thierry FOURNIER | 79c1051 | 2015-11-08 22:02:21 +0100 | [diff] [blame] | 209 | forced yields | |
| 210 | ---------------+--------------- |
| 211 | 10 | 160 |
| 212 | 500 | 670 |
| 213 | 1000 | 680 |
| 214 | 5000 | 700 |
| 215 | 7000 | 700 |
| 216 | 8000 | 700 |
| 217 | 9000 | 710 <- ceil |
| 218 | 10000 | 710 |
| 219 | 100000 | 710 |
| 220 | 1000000 | 710 |
| 221 | |
| 222 | The result showed that from 9000 instructions between two interrupt, we reached |
| 223 | a ceil, so the default parameter is 10 000. |
| 224 | |
| 225 | When HAProxy interrupts the Lua processing, we have two states possible: |
| 226 | |
| 227 | - Lua is resumable, and it returns control to the HAProxy scheduler, |
| 228 | - Lua is not resumable, and we just check the execution timeout. |
| 229 | |
| 230 | The second case occurs if it is required by the HAProxy core. This state is |
| 231 | forced if the Lua is processed in a non resumable HAProxy part, like sample |
| 232 | fetches or converters. |
| 233 | |
| 234 | It occurs also if the Lua is non resumable. For example, if some code is |
| 235 | executed through the Lua pcall() function, the execution is not resumable. This |
| 236 | is explained later. |
| 237 | |
| 238 | So, the Lua code must be fast and simple when is executed as sample fetches and |
| 239 | converters, it could be slow and complex when is executed as actions and |
| 240 | services. |
| 241 | |
| 242 | Execution time |
| 243 | -------------- |
| 244 | |
Godbach | 06c8099 | 2016-02-16 11:59:17 +0800 | [diff] [blame] | 245 | The Lua execution time is measured and limited. Each group of functions has its |
Thierry FOURNIER | 79c1051 | 2015-11-08 22:02:21 +0100 | [diff] [blame] | 246 | own timeout configured. The time measured is the real Lua execution time, and |
| 247 | not the difference between the end time and the start time. The groups are: |
| 248 | |
| 249 | - main code and init are not submitted to the timeout, |
| 250 | - fetches, converters and action have a default timeout of 4s, |
| 251 | - task, by default does not have timeout, |
| 252 | - service have a default timeout of 4s. |
| 253 | |
Godbach | 06c8099 | 2016-02-16 11:59:17 +0800 | [diff] [blame] | 254 | The corresponding tune options are: |
Thierry FOURNIER | 79c1051 | 2015-11-08 22:02:21 +0100 | [diff] [blame] | 255 | |
| 256 | - tune.lua.session-timeout (fetches, converters and action) |
| 257 | - tune.lua.task-timeout (task) |
| 258 | - tune.lua.service-timeout (services) |
| 259 | |
Godbach | 06c8099 | 2016-02-16 11:59:17 +0800 | [diff] [blame] | 260 | The task does not have a timeout because it runs in background along the |
Thierry FOURNIER | 79c1051 | 2015-11-08 22:02:21 +0100 | [diff] [blame] | 261 | HAProxy process life. |
| 262 | |
Godbach | 06c8099 | 2016-02-16 11:59:17 +0800 | [diff] [blame] | 263 | For example, if an Lua script is executed during 1.1s and the script executes a |
| 264 | sleep of 1 second, the effective measured running time is 0.1s. |
Thierry FOURNIER | 79c1051 | 2015-11-08 22:02:21 +0100 | [diff] [blame] | 265 | |
| 266 | This timeout is useful for preventing infinite loops. During the runtime, it |
Godbach | 06c8099 | 2016-02-16 11:59:17 +0800 | [diff] [blame] | 267 | should be never triggered. |
Thierry FOURNIER | 79c1051 | 2015-11-08 22:02:21 +0100 | [diff] [blame] | 268 | |
| 269 | The stack and the coprocess |
| 270 | --------------------------- |
| 271 | |
| 272 | The Lua execution is organized around a stack. Each Lua action, even out of the |
| 273 | effective execution, affects the stack. HAProxy integration uses one main stack, |
| 274 | which is common for all the process, and a secondary one used as coprocess. |
| 275 | After the initialization, the main stack is no longer used by HAProxy, except |
| 276 | for global storage. The second type of stack is used by all the Lua functions |
| 277 | called from different Lua actions declared in HAProxy. The main stack permits |
| 278 | to store coroutines pointers, and some global variables. |
| 279 | |
| 280 | Do you want to see an example of how seems Lua C development around a stack ? |
| 281 | Some examples follows. This first one, is a simple addition: |
| 282 | |
| 283 | lua_pushnumber(L, 1) |
| 284 | lua_pushnumber(L, 2) |
| 285 | lua_arith(L, LUA_OPADD) |
| 286 | |
Godbach | 06c8099 | 2016-02-16 11:59:17 +0800 | [diff] [blame] | 287 | It's easy, we push 1 on the stack, after, we push 2, and finally, we perform an |
Ilya Shipitsin | 2075ca8 | 2020-03-06 23:22:22 +0500 | [diff] [blame] | 288 | addition. The two top entries of the stack are added, popped, and the result is |
Thierry FOURNIER | 79c1051 | 2015-11-08 22:02:21 +0100 | [diff] [blame] | 289 | pushed. It is a classic way with a stack. |
| 290 | |
Godbach | 06c8099 | 2016-02-16 11:59:17 +0800 | [diff] [blame] | 291 | Now an example for constructing array and objects. It's a little bit more |
Thierry FOURNIER | 79c1051 | 2015-11-08 22:02:21 +0100 | [diff] [blame] | 292 | complicated. The difficult consist to keep in mind the state of the stack while |
| 293 | we write the code. The goal is to create the entity described below. Note that |
| 294 | the notation "*1" is a metatable reference. The metatable will be explained |
| 295 | later. |
| 296 | |
| 297 | name*1 = { |
| 298 | [0] = <userdata>, |
| 299 | } |
| 300 | |
| 301 | *1 = { |
| 302 | "__index" = { |
| 303 | "method1" = <function>, |
| 304 | "method2" = <function> |
| 305 | } |
| 306 | "__gc" = <function> |
| 307 | } |
| 308 | |
| 309 | Let's go: |
| 310 | |
| 311 | lua_newtable() // The "name" table |
| 312 | lua_newtable() // The metatable *1 |
| 313 | lua_pushstring("__index") |
| 314 | lua_newtable() // The "__index" table |
| 315 | lua_pushstring("method1") |
| 316 | lua_pushfunction(function) |
| 317 | lua_settable(-3) // -3 is an index in the stack. insert method1 |
| 318 | lua_pushstring("method2") |
| 319 | lua_pushfunction(function) |
| 320 | lua_settable(-3) // insert method2 |
| 321 | lua_settable(-3) // insert "__index" |
| 322 | lua_pushstring("__gc") |
| 323 | lua_pushfunction(function) |
| 324 | lua_settable() // insert "__gc" |
| 325 | lua_setmetatable(-1) // attach metatable to "name" |
| 326 | lua_pushnumber(0) |
| 327 | lua_pushuserdata(userdata) |
| 328 | lua_settable(-3) |
| 329 | lua_setglobal("name") |
| 330 | |
| 331 | So, coding for Lua in C, is not complex, but it needs some mental gymnastic. |
| 332 | |
| 333 | The object concept and the HAProxy format |
| 334 | ----------------------------------------- |
| 335 | |
Godbach | 06c8099 | 2016-02-16 11:59:17 +0800 | [diff] [blame] | 336 | The object seems to be not a native concept. An Lua object is a table. We can |
Thierry FOURNIER | 79c1051 | 2015-11-08 22:02:21 +0100 | [diff] [blame] | 337 | note that the table notation accept three forms: |
| 338 | |
| 339 | 1. mytable["entry"](mytable, "param") |
| 340 | 2. mytable.entry(mytable, "param") |
| 341 | 3. mytable:entry("param") |
| 342 | |
| 343 | These three notation have the same behaviour pattern: a function is executed |
Godbach | 06c8099 | 2016-02-16 11:59:17 +0800 | [diff] [blame] | 344 | with the table itself as first parameter and string "param" as second parameter |
Thierry FOURNIER | 79c1051 | 2015-11-08 22:02:21 +0100 | [diff] [blame] | 345 | The notation with [] is commonly used for storing data in a hash table, and the |
| 346 | dotted notation is used for objects. The notation with ":" indicates that the |
| 347 | first parameter is the element at the left of the symbol ":". |
| 348 | |
| 349 | So, an object is a table and each entry of the table is a variable. A variable |
| 350 | can be a function. These are the first concepts of the object notation in the |
| 351 | Lua, but it is not the end. |
| 352 | |
| 353 | With the objects, we usually expect classes and inheritance. This is the role of |
| 354 | the metable. A metable is a table with predefined entries. These entries modify |
| 355 | the default behaviour of the table. The simplest example is the "__index" entry. |
| 356 | If this entry exists, it is called when a value is requested in the table. The |
| 357 | behaviour is the following: |
| 358 | |
| 359 | 1 - looks in the table if the entry exists, and if it the case, return it |
| 360 | |
| 361 | 2 - looks if a metatable exists, and if the "__index" entry exists |
| 362 | |
| 363 | 3 - if "__index" is a function, execute it with the key as parameter, and |
| 364 | returns the result of the function. |
| 365 | |
| 366 | 4 - if "__index" is a table, looks if the requested entry exists, and if |
| 367 | exists, return it. |
| 368 | |
| 369 | 5 - if not exists, return to step 2 |
| 370 | |
| 371 | The behaviour of the point 5 represents the inheritance. |
| 372 | |
| 373 | In HAProxy all the provided objects are tables, the entry "[0]" contains private |
Ilya Shipitsin | 11057a3 | 2020-06-21 21:18:27 +0500 | [diff] [blame] | 374 | data, there are often userdata or lightuserdata. The metatable is registered in |
Thierry FOURNIER | 79c1051 | 2015-11-08 22:02:21 +0100 | [diff] [blame] | 375 | the global part of the main Lua stack, and it is called with the case sensitive |
| 376 | class name. A great part of these class must not be used directly because it |
| 377 | requires an initialisation using the HAProxy internal structs. |
| 378 | |
Godbach | 06c8099 | 2016-02-16 11:59:17 +0800 | [diff] [blame] | 379 | The HAProxy objects use unified conventions. An Lua object is always a table. |
| 380 | In most cases, an HAProxy Lua object needs some private data. These are always |
Thierry FOURNIER | 79c1051 | 2015-11-08 22:02:21 +0100 | [diff] [blame] | 381 | set in the index [0] of the array. The metatable entry "__tostring" returns the |
| 382 | object name. |
| 383 | |
| 384 | The Lua developer can add entries to the HAProxy object. He just works carefully |
| 385 | and prevent to modify the index [0]. |
| 386 | |
| 387 | Common HAproxy objects are: |
| 388 | |
| 389 | - TXN : manipulates the transaction between the client and the server |
| 390 | - Channel : manipulates proxified data between the client and the server |
| 391 | - HTTP : manipulates HTTP between the client and the server |
| 392 | - Map : manipulates HAProxy maps. |
| 393 | - Fetches : access to all HAProxy sample fetches |
| 394 | - Converters : access to all HAProxy sample converters |
| 395 | - AppletTCP : process client request like a TCP server |
| 396 | - AppletHTTP : process client request like an HTTP server |
| 397 | - Socket : establish tcp connection to a server (ipv4/ipv6/socket/ssl/...) |
| 398 | |
| 399 | The garbage collector and the memory allocation |
| 400 | ----------------------------------------------- |
| 401 | |
| 402 | Lua doesn't really have a global memory limit, but HAProxy implements it. This |
| 403 | permits to control the amount of memory dedicated to the Lua processes. It is |
| 404 | specially useful with embedded environments. |
| 405 | |
| 406 | When the memory limit is reached, HAProxy refuses to give more memory to the Lua |
| 407 | scripts. The current Lua execution is terminated with an error and HAProxy |
Godbach | 06c8099 | 2016-02-16 11:59:17 +0800 | [diff] [blame] | 408 | continues its processing. |
Thierry FOURNIER | 79c1051 | 2015-11-08 22:02:21 +0100 | [diff] [blame] | 409 | |
| 410 | The max amount of memory is configured with the option: |
| 411 | |
| 412 | tune.lua.maxmem |
| 413 | |
| 414 | As many other script languages, Lua uses a garbage collector for reusing its |
Michael Prokop | 4438c60 | 2019-05-24 10:25:45 +0200 | [diff] [blame] | 415 | memory. The Lua developer can work without memory preoccupation. Usually, the |
Thierry FOURNIER | 79c1051 | 2015-11-08 22:02:21 +0100 | [diff] [blame] | 416 | garbage collector is controlled by the Lua core, but sometimes it will be useful |
| 417 | to run when the user/developer requires. So the garbage collector can be called |
| 418 | from C part or Lua part. |
| 419 | |
| 420 | Sometimes, objects using lightuserdata or userdata requires to free some memory |
| 421 | block or close filedescriptor not controlled by the Lua. A dedicated garbage |
Godbach | 06c8099 | 2016-02-16 11:59:17 +0800 | [diff] [blame] | 422 | collection function is provided through the metatable. It is referenced with the |
Thierry FOURNIER | 79c1051 | 2015-11-08 22:02:21 +0100 | [diff] [blame] | 423 | special entry "__gc". |
| 424 | |
| 425 | Generally, in HAProxy, the garbage collector does this job without any |
Godbach | 06c8099 | 2016-02-16 11:59:17 +0800 | [diff] [blame] | 426 | intervention. However some objects use a great amount of memory, and we want to |
| 427 | release as quickly as possible. The problem is that only the GC knows if the |
| 428 | object is in use or not. The reason is simple variable containing objects can be |
| 429 | shared between coroutines and the main thread, so an object can be used |
| 430 | everywhere in HAProxy. |
Thierry FOURNIER | 79c1051 | 2015-11-08 22:02:21 +0100 | [diff] [blame] | 431 | |
| 432 | The only one example is the HAProxy sockets. These are explained later, just for |
| 433 | understanding the GC issues, a quick overview of the socket follows. The HAProxy |
Godbach | 06c8099 | 2016-02-16 11:59:17 +0800 | [diff] [blame] | 434 | socket uses an internal session and stream, the session uses resources like |
Thierry FOURNIER | 79c1051 | 2015-11-08 22:02:21 +0100 | [diff] [blame] | 435 | memory and file descriptor and in some cases keeps a socket open while it is no |
Godbach | 06c8099 | 2016-02-16 11:59:17 +0800 | [diff] [blame] | 436 | longer used by Lua. |
Thierry FOURNIER | 79c1051 | 2015-11-08 22:02:21 +0100 | [diff] [blame] | 437 | |
| 438 | If the HAProxy socket is used, we forcing a garbage collector cycle after the |
| 439 | end of each function using HAProxy socket. The reason is simple: if the socket |
| 440 | is no longer used, we want to close the connection quickly. |
| 441 | |
| 442 | A special flag is used in HAProxy indicating that a HAProxy socket is created. |
| 443 | If this flag is set, a full GC cycle is started after each Lua action. This is |
| 444 | not free, we loose about 10% of performances, but it is the only way for closing |
| 445 | sockets quickly. |
| 446 | |
| 447 | The yield concept / longjmp issues |
| 448 | ---------------------------------- |
| 449 | |
| 450 | The "yield" is an action which does some Lua processing in pause and give back |
| 451 | the hand to the HAProxy core. This action is do when the Lua needs to wait about |
Godbach | 06c8099 | 2016-02-16 11:59:17 +0800 | [diff] [blame] | 452 | data or other things. The most basically example is the sleep() function. In an |
Thierry FOURNIER | 79c1051 | 2015-11-08 22:02:21 +0100 | [diff] [blame] | 453 | event driven software the code must not process blocking systems call, so the |
| 454 | sleep blocks the software between a lot of time. In HAProxy, an Lua sleep does a |
Godbach | 06c8099 | 2016-02-16 11:59:17 +0800 | [diff] [blame] | 455 | yield, and ask to the scheduler to be woken up in a required sleep time. |
| 456 | Meanwhile, the HAProxy scheduler does other things, like accepting new |
| 457 | connection or forwarding data. |
Thierry FOURNIER | 79c1051 | 2015-11-08 22:02:21 +0100 | [diff] [blame] | 458 | |
Godbach | 06c8099 | 2016-02-16 11:59:17 +0800 | [diff] [blame] | 459 | A yield is also executed regularly, after a lot of Lua instructions processed. |
Thierry FOURNIER | 79c1051 | 2015-11-08 22:02:21 +0100 | [diff] [blame] | 460 | This yield permits to control the effective execution time, and also give back |
Godbach | 06c8099 | 2016-02-16 11:59:17 +0800 | [diff] [blame] | 461 | the hand to the HAProxy core. When HAProxy finishes to process the pending jobs, |
| 462 | the Lua execution continues. |
Thierry FOURNIER | 79c1051 | 2015-11-08 22:02:21 +0100 | [diff] [blame] | 463 | |
| 464 | This special "yield" uses the Lua "debug" functions. Lua provides a debug method |
| 465 | called "lua_sethook()" which permits to interrupt the execution after some |
| 466 | configured condition and call a function. This condition used in HAProxy is |
Godbach | 06c8099 | 2016-02-16 11:59:17 +0800 | [diff] [blame] | 467 | a number of instructions processed and when a function returns. The function |
| 468 | called controls the effective execution time, and if it is possible to send a |
Thierry FOURNIER | 79c1051 | 2015-11-08 22:02:21 +0100 | [diff] [blame] | 469 | "yield". |
| 470 | |
| 471 | The yield system is based on a couple setjmp/longjmp. In brief, the setjmp() |
| 472 | stores a stack state, and the longjmp restores the stack in its state which had |
| 473 | before the last Lua execution. |
| 474 | |
Godbach | 06c8099 | 2016-02-16 11:59:17 +0800 | [diff] [blame] | 475 | Lua can immediately stop its execution if an error occurs. This system uses also |
Joseph Herlant | 71b4b15 | 2018-11-13 16:55:16 -0800 | [diff] [blame] | 476 | the longjmp system. In HAProxy, we try to use this system only for unrecoverable |
Godbach | 06c8099 | 2016-02-16 11:59:17 +0800 | [diff] [blame] | 477 | errors. Maybe some trivial errors target an exception, but we try to remove it. |
Thierry FOURNIER | 79c1051 | 2015-11-08 22:02:21 +0100 | [diff] [blame] | 478 | |
| 479 | It seems that Lua uses the longjmp system for having a behaviour like the java |
Godbach | 06c8099 | 2016-02-16 11:59:17 +0800 | [diff] [blame] | 480 | try / catch. We can use the function pcall() to execute some code. The function |
Thierry FOURNIER | 79c1051 | 2015-11-08 22:02:21 +0100 | [diff] [blame] | 481 | pcall() run a setjmp(). So, if any error occurs while the Lua code execution, |
Godbach | 06c8099 | 2016-02-16 11:59:17 +0800 | [diff] [blame] | 482 | the flow immediately returns from the pcall() with an error. |
Thierry FOURNIER | 79c1051 | 2015-11-08 22:02:21 +0100 | [diff] [blame] | 483 | |
| 484 | The big issue of this behaviour is that we cannot do a yield. So if some Lua code |
| 485 | executes a library using pcall for catching errors, HAProxy must be wait for the |
| 486 | end of execution without processing any accept or any stream. The cause is the |
Godbach | 06c8099 | 2016-02-16 11:59:17 +0800 | [diff] [blame] | 487 | yield must be jump to the root of execution. The intermediate setjmp() avoids |
Thierry FOURNIER | 79c1051 | 2015-11-08 22:02:21 +0100 | [diff] [blame] | 488 | this behaviour. |
| 489 | |
| 490 | |
| 491 | HAproxy start Lua execution |
| 492 | + Lua puts a setjmp() |
| 493 | + Lua executes code |
| 494 | + Some code is executed in a pcall() |
| 495 | + pcall() puts a setjmp() |
| 496 | + Lua executes code |
| 497 | + A yield is require for a sleep function |
| 498 | it cannot be jumps to the Lua root execution. |
| 499 | |
| 500 | |
| 501 | Another issue with the processing of strong errors is the manipulation of the |
| 502 | Lua stack outside of an Lua processing. If one of the functions called occurs a |
| 503 | strong error, the default behaviour is an abort(). It is not acceptable when |
| 504 | HAProxy is in runtime mode. The Lua documentation propose to use another |
Godbach | 06c8099 | 2016-02-16 11:59:17 +0800 | [diff] [blame] | 505 | setjmp/longjmp to avoid the abort(). The goal is to put a setjmp between |
Thierry FOURNIER | 79c1051 | 2015-11-08 22:02:21 +0100 | [diff] [blame] | 506 | manipulating the Lua stack and using an alternative "panic" function which jumps |
| 507 | to the setjmp() in error case. |
| 508 | |
| 509 | All of these behaviours are very dangerous for the stability, and the internal |
| 510 | HAProxy code must be modified with many precautions. |
| 511 | |
| 512 | For preserving a good behaviour of HAProxy, the yield is mandatory. |
Godbach | 06c8099 | 2016-02-16 11:59:17 +0800 | [diff] [blame] | 513 | Unfortunately, some HAProxy parts are not adapted for resuming an execution |
| 514 | after a yield. These parts are the sample fetches and the sample converters. So, |
| 515 | the Lua code written in these parts of HAProxy must be quickly executed, and can |
| 516 | not do actions which require yield like TCP connection or simple sleep. |
Thierry FOURNIER | 79c1051 | 2015-11-08 22:02:21 +0100 | [diff] [blame] | 517 | |
| 518 | HAproxy socket object |
| 519 | --------------------- |
| 520 | |
| 521 | The HAProxy design is optimized for the data transfers between a client and a |
| 522 | server, and processing the many errors which can occurs during these exchanges. |
| 523 | HAProxy is not designed for having a third connection established to a third |
| 524 | party server. |
| 525 | |
Godbach | 06c8099 | 2016-02-16 11:59:17 +0800 | [diff] [blame] | 526 | The solution consist to put the main stream in pause waiting for the end of the |
Thierry FOURNIER | 79c1051 | 2015-11-08 22:02:21 +0100 | [diff] [blame] | 527 | exchanges with the third connection. This is completed by a signal between |
| 528 | internal tasks. The following graph shows the HAProxy Lua socket: |
| 529 | |
| 530 | |
| 531 | +--------------------+ |
| 532 | | Lua processing | |
| 533 | ------------------\ | creates socket | ------------------\ |
| 534 | incoming request > | and puts the | Outgoing request > |
| 535 | ------------------/ | current processing | ------------------/ |
| 536 | Â Â | in pause waiting | |
| 537 | | for TCP applet | |
| 538 | +-----------------+--+ |
| 539 | ^ | |
| 540 | | | |
| 541 | | signal | read / write |
| 542 | | | data |
| 543 | | | |
| 544 | +-------------+---------+ v |
| 545 | | HAProxy internal +----------------+ |
| 546 | | applet send signals | | |
| 547 | | when data is received | | -------------------\ |
| 548 | | or some room is | Attached I/O | Client TCP stream > |
| 549 | | available | Buffers | -------------------/ |
| 550 | +--------------------+--+ | |
| 551 | | | |
| 552 | +-------------------+ |
| 553 | |
| 554 | |
| 555 | A more detailed graph is available in the "doc/internals" directory. |
| 556 | |
| 557 | The HAProxy Lua socket uses a full HAProxy session / stream for establishing the |
| 558 | connection. This mechanism provides all the facilities and HAProxy features, |
| 559 | like the SSL stack, many socket type, and support for namespaces. |
Godbach | 06c8099 | 2016-02-16 11:59:17 +0800 | [diff] [blame] | 560 | Technically it supports the proxy protocol, but there are no way to enable it. |
Thierry FOURNIER | 79c1051 | 2015-11-08 22:02:21 +0100 | [diff] [blame] | 561 | |
| 562 | How compiling HAProxy with Lua |
| 563 | ============================== |
| 564 | |
Godbach | 06c8099 | 2016-02-16 11:59:17 +0800 | [diff] [blame] | 565 | HAProxy 1.6 requires Lua 5.3. Lua 5.3 offers some features which make easy the |
Thierry FOURNIER | 79c1051 | 2015-11-08 22:02:21 +0100 | [diff] [blame] | 566 | integration. Lua 5.3 is young, and some distros do not distribute it. Luckily, |
| 567 | Lua is a great product because it does not require exotic dependencies, and its |
| 568 | build process is really easy. |
| 569 | |
| 570 | The compilation process for linux is easy: |
| 571 | |
| 572 | - download the source tarball |
| 573 | wget http://www.lua.org/ftp/lua-5.3.1.tar.gz |
| 574 | |
| 575 | - untar it |
| 576 | tar xf lua-5.3.1.tar.gz |
| 577 | |
| 578 | - enter the directory |
| 579 | cd lua-5.3.1 |
| 580 | |
| 581 | - build the library for linux |
| 582 | make linux |
| 583 | |
| 584 | - install it: |
Godbach | 06c8099 | 2016-02-16 11:59:17 +0800 | [diff] [blame] | 585 | sudo make INSTALL_TOP=/opt/lua-5.3.1 install |
Thierry FOURNIER | 79c1051 | 2015-11-08 22:02:21 +0100 | [diff] [blame] | 586 | |
| 587 | HAProxy builds with your favourite options, plus the following options for |
| 588 | embedding the Lua script language: |
| 589 | |
| 590 | - download the source tarball |
| 591 | wget http://www.haproxy.org/download/1.6/src/haproxy-1.6.2.tar.gz |
| 592 | |
| 593 | - untar it |
| 594 | tar xf haproxy-1.6.2.tar.gz |
| 595 | |
| 596 | - enter the directory |
| 597 | cd haproxy-1.6.2 |
| 598 | |
| 599 | - build HAProxy: |
Willy Tarreau | d254aa8 | 2019-06-14 18:40:48 +0200 | [diff] [blame] | 600 | make TARGET=linux-glibc \ |
Thierry FOURNIER | 79c1051 | 2015-11-08 22:02:21 +0100 | [diff] [blame] | 601 | USE_LUA=1 \ |
| 602 | LUA_LIB=/opt/lua-5.3.1/lib \ |
| 603 | LUA_INC=/opt/lua-5.3.1/include |
| 604 | |
| 605 | - install it: |
| 606 | sudo make PREFIX=/opt/haproxy-1.6.2 install |
| 607 | |
| 608 | First steps with Lua |
| 609 | ==================== |
| 610 | |
Godbach | 06c8099 | 2016-02-16 11:59:17 +0800 | [diff] [blame] | 611 | Now, it's time to use Lua in HAProxy. |
Thierry FOURNIER | 79c1051 | 2015-11-08 22:02:21 +0100 | [diff] [blame] | 612 | |
| 613 | Start point |
| 614 | ----------- |
| 615 | |
Godbach | 06c8099 | 2016-02-16 11:59:17 +0800 | [diff] [blame] | 616 | The HAProxy global directive "lua-load <file>" allows to load an Lua file. This |
Thierry FOURNIER | 79c1051 | 2015-11-08 22:02:21 +0100 | [diff] [blame] | 617 | is the entry point. This load become during the configuration parsing, and the |
| 618 | Lua file is immediately executed. |
| 619 | |
Godbach | 06c8099 | 2016-02-16 11:59:17 +0800 | [diff] [blame] | 620 | All the register_*() functions must be called at this time because they are used |
Thierry FOURNIER | 79c1051 | 2015-11-08 22:02:21 +0100 | [diff] [blame] | 621 | just after the processing of the global section, in the frontend/backend/listen |
| 622 | sections. |
| 623 | |
| 624 | The most simple "Hello world !" is the following line a loaded Lua file: |
| 625 | |
| 626 | core.Alert("Hello World !"); |
| 627 | |
Godbach | 06c8099 | 2016-02-16 11:59:17 +0800 | [diff] [blame] | 628 | It displays a log during the HAProxy startup: |
Thierry FOURNIER | 79c1051 | 2015-11-08 22:02:21 +0100 | [diff] [blame] | 629 | |
| 630 | [alert] 285/083533 (14465) : Hello World ! |
| 631 | |
| 632 | Default path and libraries |
| 633 | -------------------------- |
| 634 | |
| 635 | Lua can embed some libraries. These libraries can be included from different |
Godbach | 06c8099 | 2016-02-16 11:59:17 +0800 | [diff] [blame] | 636 | paths. It seems that Lua doesn't like subdirectories. In the following example, |
| 637 | I try to load a compiled library, so the first line is Lua code, the second line |
Thierry FOURNIER | 79c1051 | 2015-11-08 22:02:21 +0100 | [diff] [blame] | 638 | is an 'strace' extract proving that the library was opened. The next lines are |
| 639 | the associated error. |
| 640 | |
| 641 | require("luac/concat") |
| 642 | |
| 643 | open("./luac/concat.so", O_RDONLY|O_CLOEXEC) = 4 |
| 644 | |
| 645 | [ALERT] 293/175822 (22806) : parsing [commonstats.conf:15] : lua runtime |
| 646 | error: error loading module 'luac/concat' from file './luac/concat.so': |
| 647 | ./luac/concat.so: undefined symbol: luaopen_luac/concat |
| 648 | |
| 649 | Lua tries to load the C symbol 'luaopen_luac/concat'. When Lua tries to open a |
| 650 | library, it tries to execute the function associated to the symbol |
| 651 | "luaopen_<libname>". |
| 652 | |
| 653 | The variable "<libname>" is defined using the content of the variable |
| 654 | "package.cpath" and/or "package.path". The default definition of the |
| 655 | "package.cpath" (on my computer is ) variable is: |
| 656 | |
| 657 | /usr/local/lib/lua/5.3/?.so;/usr/local/lib/lua/5.3/loadall.so;./?.so |
| 658 | |
Godbach | 06c8099 | 2016-02-16 11:59:17 +0800 | [diff] [blame] | 659 | The "<libname>" is the content which replaces the symbol "<?>". In the previous |
Thierry FOURNIER | 79c1051 | 2015-11-08 22:02:21 +0100 | [diff] [blame] | 660 | example, its "luac/concat", and obviously the Lua core try to load the function |
| 661 | associated with the symbol "luaopen_luac/concat". |
| 662 | |
| 663 | My conclusion is that Lua doesn't support subdirectories. So, for loading |
| 664 | libraries in subdirectory, it must fill the variable with the name of this |
| 665 | subdirectory. The extension .so must disappear, otherwise Lua try to execute the |
| 666 | function associated with the symbol "luaopen_concat.so". The following syntax is |
| 667 | correct: |
| 668 | |
| 669 | package.cpath = package.cpath .. ";./luac/?.so" |
| 670 | require("concat") |
| 671 | |
| 672 | First useful example |
| 673 | -------------------- |
| 674 | |
| 675 | core.register_fetches("my-hash", function(txn, salt) |
| 676 | return txn.sc:sdbm(salt .. txn.sf:req_fhdr("host") .. txn.sf:path() .. txn.sf:src(), 1) |
| 677 | end) |
| 678 | |
Godbach | 06c8099 | 2016-02-16 11:59:17 +0800 | [diff] [blame] | 679 | You will see that these 3 lines can generate a lot of explanations :) |
Thierry FOURNIER | 79c1051 | 2015-11-08 22:02:21 +0100 | [diff] [blame] | 680 | |
| 681 | Core.register_fetches() is executed during the processing of the global section |
| 682 | by the HAProxy configuration parser. A new sample fetch is declared with name |
| 683 | "my-hash", this name is always prefixed by "lua.". So this new declared |
| 684 | sample fetch will be used calling "lua.my-hash" in the HAProxy configuration |
| 685 | file. |
| 686 | |
| 687 | The second parameter is an inline declared anonymous function. Note the closed |
Godbach | 06c8099 | 2016-02-16 11:59:17 +0800 | [diff] [blame] | 688 | parenthesis after the keyword "end" which ends the function. The first parameter |
| 689 | of this anonymous function is "txn". It is an object of class TXN. It provides |
Thierry FOURNIER | 79c1051 | 2015-11-08 22:02:21 +0100 | [diff] [blame] | 690 | access functions. The second parameter is an arbitrary value provided by the |
| 691 | HAProxy configuration file. This parameter is optional, the developer must |
Godbach | 06c8099 | 2016-02-16 11:59:17 +0800 | [diff] [blame] | 692 | check if it is present. |
Thierry FOURNIER | 79c1051 | 2015-11-08 22:02:21 +0100 | [diff] [blame] | 693 | |
| 694 | The anonymous function registration is executed when the HAProxy backend or |
| 695 | frontend configuration references the sample fetch "lua.my-hash". |
| 696 | |
Godbach | 06c8099 | 2016-02-16 11:59:17 +0800 | [diff] [blame] | 697 | This example can be written with another style, like below: |
Thierry FOURNIER | 79c1051 | 2015-11-08 22:02:21 +0100 | [diff] [blame] | 698 | |
| 699 | function my_hash(txn, salt) |
| 700 | return txn.sc:sdbm(salt .. txn.sf:req_fhdr("host") .. txn.sf:path() .. txn.sf:src(), 1) |
| 701 | end |
| 702 | |
| 703 | core.register_fetches("my-hash", my_hash) |
| 704 | |
| 705 | This second form is clearer, but the first one is compact. |
| 706 | |
Godbach | 06c8099 | 2016-02-16 11:59:17 +0800 | [diff] [blame] | 707 | The operator ".." is a string concatenation. If one of the two operands is not a |
Thierry FOURNIER | 79c1051 | 2015-11-08 22:02:21 +0100 | [diff] [blame] | 708 | string, an error occurs and the execution is immediately stopped. This is |
| 709 | important to keep in mind for the following things. |
| 710 | |
| 711 | Now I write the example on more than one line. Its an easiest way for commenting |
| 712 | the code: |
| 713 | |
| 714 | 1. function my_hash(txn, salt) |
| 715 | 2. local str = "" |
| 716 | 3. str = str .. salt |
| 717 | 4. str = str .. txn.sf:req_fhdr("host") |
| 718 | 5. str = str .. txn.sf:path() |
| 719 | 6. str = str .. txn.sf:src() |
| 720 | 7. local result = txn.sc:sdbm(str, 1) |
| 721 | 8. return result |
| 722 | 9. end |
| 723 | 10. |
| 724 | 11. core.register_fetches("my-hash", my_hash) |
| 725 | |
| 726 | local |
| 727 | ~~~~~ |
| 728 | |
| 729 | The first keyword is "local". This is a really important keyword. You must |
| 730 | understand that the function "my_hash" will be called for each HAProxy request |
| 731 | using the declared sample fetch. So, this function can be executed many times in |
| 732 | parallel. |
| 733 | |
Godbach | 06c8099 | 2016-02-16 11:59:17 +0800 | [diff] [blame] | 734 | By default, Lua uses global variables. So in this example, if the variable "str" |
Thierry FOURNIER | 79c1051 | 2015-11-08 22:02:21 +0100 | [diff] [blame] | 735 | is declared without the keyword "local", it will be shared by all the parallel |
| 736 | executions of the function and obviously, the content of the requests will be |
| 737 | shared. |
| 738 | |
| 739 | This warning is very important. I tried to write useful Lua code like a rewrite |
Godbach | 06c8099 | 2016-02-16 11:59:17 +0800 | [diff] [blame] | 740 | of the statistics page, and it is very hard thing to declare each variable as |
Thierry FOURNIER | 79c1051 | 2015-11-08 22:02:21 +0100 | [diff] [blame] | 741 | "local". |
| 742 | |
Godbach | 06c8099 | 2016-02-16 11:59:17 +0800 | [diff] [blame] | 743 | I guess that this behaviour will be the cause of many troubles on the mailing |
Thierry FOURNIER | 79c1051 | 2015-11-08 22:02:21 +0100 | [diff] [blame] | 744 | list. |
| 745 | |
| 746 | str = str .. |
| 747 | ~~~~~~~~~~~~ |
| 748 | |
Godbach | 06c8099 | 2016-02-16 11:59:17 +0800 | [diff] [blame] | 749 | Now a parenthesis about the form "str = str ..". This form allows to do string |
Thierry FOURNIER | 79c1051 | 2015-11-08 22:02:21 +0100 | [diff] [blame] | 750 | concatenations. Remember that Lua uses a garbage collector, so what happens when |
| 751 | we do "str = str .. 'another string'" ? |
| 752 | |
| 753 | str = str .. "another string" |
| 754 | ^ ^ ^ ^ |
| 755 | 1 2 3 4 |
| 756 | |
Godbach | 06c8099 | 2016-02-16 11:59:17 +0800 | [diff] [blame] | 757 | Lua executes first the concatenation operator (3), it allocates memory for the |
Thierry FOURNIER | 79c1051 | 2015-11-08 22:02:21 +0100 | [diff] [blame] | 758 | resulting string and fill this memory with the concatenation of the operands 2 |
Godbach | 06c8099 | 2016-02-16 11:59:17 +0800 | [diff] [blame] | 759 | and 4. Next, it frees the variable 1, now the old content of 1 can be garbage |
| 760 | collected. And finally, the new content of 1 is the concatenation. |
Thierry FOURNIER | 79c1051 | 2015-11-08 22:02:21 +0100 | [diff] [blame] | 761 | |
| 762 | what the matter ? when we do this operation many times, we consume a lot of |
| 763 | memory, and the string data is duplicated and move many times. So, this practice |
| 764 | is expensive in execution time and memory consumption. |
| 765 | |
| 766 | There are easy ways to prevent this behaviour. I guess that a C binding for |
| 767 | concatenation with chunks will be available ASAP (it is already written). I do |
| 768 | some benchmarks. I compare the execution time of 1 000 times, 1 000 |
| 769 | concatenation of 10 bytes written in pure Lua and with a C library. The result is |
| 770 | 10 times faster in C (1s in Lua, and 0.1s in C). |
| 771 | |
| 772 | txn |
| 773 | ~~~ |
| 774 | |
| 775 | txn is an HAProxy object of class TXN. The documentation is available in the |
| 776 | HAProxy Lua API reference. This class allow the access to the native HAProxy |
| 777 | sample fetches and converters. The object txn contains 2 members dedicated to |
| 778 | the sample fetches and 2 members dedicated to the converters. |
| 779 | |
| 780 | The sample fetches members are "f" (as sample-Fetch) and "sf" (as String |
Godbach | 06c8099 | 2016-02-16 11:59:17 +0800 | [diff] [blame] | 781 | sample-Fetch). These two members contain exactly the same functions. All the |
Thierry FOURNIER | 79c1051 | 2015-11-08 22:02:21 +0100 | [diff] [blame] | 782 | HAProxy native sample fetches are available, obviously, the Lua registered sample |
| 783 | fetches are not available. Unfortunately, HAProxy sample fetches names are not |
Godbach | 06c8099 | 2016-02-16 11:59:17 +0800 | [diff] [blame] | 784 | compatible with the Lua function names, and they are renamed. The rename |
Thierry FOURNIER | 79c1051 | 2015-11-08 22:02:21 +0100 | [diff] [blame] | 785 | convention is simple, we replace all the '.', '+' and '-' by '_'. The '.' is the |
| 786 | object member separator, and the "-" and "+" is math operator. |
| 787 | |
Godbach | 06c8099 | 2016-02-16 11:59:17 +0800 | [diff] [blame] | 788 | Now, that I'm writing this article, I know the Lua better than I wrote the |
Thierry FOURNIER | 79c1051 | 2015-11-08 22:02:21 +0100 | [diff] [blame] | 789 | sample-fetches wrapper. The original HAProxy sample-fetches name should be used |
| 790 | using alternative manner to call an object member, so the sample-fetch |
Godbach | 06c8099 | 2016-02-16 11:59:17 +0800 | [diff] [blame] | 791 | "req.fhdr" (actually renamed req_fhdr") should be used like this: |
Thierry FOURNIER | 79c1051 | 2015-11-08 22:02:21 +0100 | [diff] [blame] | 792 | |
| 793 | txn.f["req.fhdr"](txn.f, ...) |
| 794 | |
| 795 | However, I think that this form is not elegant. |
| 796 | |
| 797 | The "s" collection return a data with a type near to the original returned type. |
Godbach | 06c8099 | 2016-02-16 11:59:17 +0800 | [diff] [blame] | 798 | A string returns an Lua string, an integer returns an Lua integer and an IP |
Thierry FOURNIER | 79c1051 | 2015-11-08 22:02:21 +0100 | [diff] [blame] | 799 | address returns an Lua string. Sometime the data is not or not yet available, in |
| 800 | this case it returns the Lua nil value. |
| 801 | |
Godbach | 06c8099 | 2016-02-16 11:59:17 +0800 | [diff] [blame] | 802 | The "sf" collection guarantees that a string will be always returned. If the data |
Thierry FOURNIER | 79c1051 | 2015-11-08 22:02:21 +0100 | [diff] [blame] | 803 | is not available, an empty string is returned. The main usage of these collection |
| 804 | is to concatenate the returned sample-fetches without testing each function. |
| 805 | |
Godbach | 06c8099 | 2016-02-16 11:59:17 +0800 | [diff] [blame] | 806 | The parameters of the sample-fetches are according with the HAProxy |
Thierry FOURNIER | 79c1051 | 2015-11-08 22:02:21 +0100 | [diff] [blame] | 807 | documentation. |
| 808 | |
Godbach | 06c8099 | 2016-02-16 11:59:17 +0800 | [diff] [blame] | 809 | The converters run exactly with the same manner as the sample fetches. The |
| 810 | only one difference is that the first parameter is the converter entry element. |
Thierry FOURNIER | 79c1051 | 2015-11-08 22:02:21 +0100 | [diff] [blame] | 811 | The "c" collection returns a precise result, and the "sc" collection returns |
| 812 | always a string. |
| 813 | |
| 814 | The sample-fetches used in the example function are "txn.sf:req_fhdr()", |
Godbach | 06c8099 | 2016-02-16 11:59:17 +0800 | [diff] [blame] | 815 | "txn.sf:path()" and "txn.sf:src()". The converter is "txn.sc:sdbm()". The same |
Thierry FOURNIER | 79c1051 | 2015-11-08 22:02:21 +0100 | [diff] [blame] | 816 | function with the "s" collection of sample-fetches and the "c" collection of |
| 817 | converter should be written like this: |
| 818 | |
| 819 | 1. function my_hash(txn, salt) |
| 820 | 2. local str = "" |
| 821 | 3. str = str .. salt |
| 822 | 4. str = str .. tostring(txn.f:req_fhdr("host")) |
| 823 | 5. str = str .. tostring(txn.f:path()) |
| 824 | 6. str = str .. tostring(txn.f:src()) |
| 825 | 7. local result = tostring(txn.c:sdbm(str, 1)) |
| 826 | 8. return result |
| 827 | 9. end |
| 828 | 10. |
| 829 | 11. core.register_fetches("my-hash", my_hash) |
| 830 | |
| 831 | tostring |
| 832 | ~~~~~~~~ |
| 833 | |
Godbach | 06c8099 | 2016-02-16 11:59:17 +0800 | [diff] [blame] | 834 | The function tostring ensures that its parameter is returned as a string. If the |
Thierry FOURNIER | 79c1051 | 2015-11-08 22:02:21 +0100 | [diff] [blame] | 835 | parameter is a table or a thread or anything that will not have any sense as a |
| 836 | string, a form like the typename followed by a pointer is returned. For example: |
| 837 | |
| 838 | t = {} |
| 839 | print(tostring(t)) |
| 840 | |
| 841 | returns: |
| 842 | |
| 843 | table: 0x15facc0 |
| 844 | |
| 845 | For objects, if the special function __tostring() is registered in the attached |
| 846 | metatable, it will be called with the table itself as first argument. The |
Godbach | 06c8099 | 2016-02-16 11:59:17 +0800 | [diff] [blame] | 847 | HAProxy object returns its own type. |
Thierry FOURNIER | 79c1051 | 2015-11-08 22:02:21 +0100 | [diff] [blame] | 848 | |
| 849 | About the converters entry point |
| 850 | -------------------------------- |
| 851 | |
| 852 | In HAProxy, a converter is a stateless function that takes a data as entry and |
| 853 | returns a transformation of this data as output. In Lua it is exactly the same |
| 854 | behaviour. |
| 855 | |
Godbach | 06c8099 | 2016-02-16 11:59:17 +0800 | [diff] [blame] | 856 | So, the registered Lua function doesn't have any special parameters, just a |
Thierry FOURNIER | 79c1051 | 2015-11-08 22:02:21 +0100 | [diff] [blame] | 857 | variable as input which contains the value to convert, and it must return data. |
| 858 | |
| 859 | The data required as input by the Lua converter is a string. So HAProxy will |
| 860 | always provide a string as input. If the native sample fetch is not a string it |
Godbach | 06c8099 | 2016-02-16 11:59:17 +0800 | [diff] [blame] | 861 | will be converted in best effort. |
Thierry FOURNIER | 79c1051 | 2015-11-08 22:02:21 +0100 | [diff] [blame] | 862 | |
| 863 | The returned value will have anything type, it will be converted as sample of |
| 864 | the near HAProxy type. The conversion rules from Lua variables to HAProxy |
| 865 | samples are: |
| 866 | |
| 867 | Lua | HAProxy sample types |
| 868 | -----------+--------------------- |
| 869 | "number" | "sint" |
| 870 | "boolean" | "bool" |
| 871 | "string" | "str" |
| 872 | "userdata" | "bool" (false) |
| 873 | "nil" | "bool" (false) |
| 874 | "table" | "bool" (false) |
| 875 | "function" | "bool" (false) |
| 876 | "thread" | "bool" (false) |
| 877 | |
| 878 | The function used for registering a converter is: |
| 879 | |
| 880 | core.register_converters() |
| 881 | |
| 882 | The task entry point |
| 883 | -------------------- |
| 884 | |
| 885 | The function "core.register_task(fcn)" executes once the function "fcn" when the |
| 886 | scheduler starts. This way is used for executing background task. For example, |
Michael Prokop | 4438c60 | 2019-05-24 10:25:45 +0200 | [diff] [blame] | 887 | you can use this functionality for periodically checking the health of another |
Thierry FOURNIER | 79c1051 | 2015-11-08 22:02:21 +0100 | [diff] [blame] | 888 | service, and giving the result to each proxy needing it. |
| 889 | |
| 890 | The task is started once, if you want periodic actions, you can use the |
| 891 | "core.sleep()" or "core.msleep()" for waiting the next runtime. |
| 892 | |
| 893 | Storing Lua variable between function in the same session |
| 894 | --------------------------------------------------------- |
| 895 | |
| 896 | All the functions registered as action or sample fetch can share an Lua context. |
Godbach | 06c8099 | 2016-02-16 11:59:17 +0800 | [diff] [blame] | 897 | This context is a memory zone in the stack. sample fetch and action use the |
Thierry FOURNIER | 79c1051 | 2015-11-08 22:02:21 +0100 | [diff] [blame] | 898 | same stack, so both can access to the context. |
| 899 | |
| 900 | The context is accessible via the function get_priv and set_priv provided by an |
| 901 | object of class TXN. The value given to set_priv replaces the current stored |
| 902 | value. This value can be a table, it is useful if a lot of data can be shared. |
| 903 | |
| 904 | If the value stored is a table, you can add or remove entries from the table |
| 905 | without storing again the new table. Maybe an example will be clearer: |
| 906 | |
| 907 | local t = {} |
| 908 | txn:set_priv(t) |
| 909 | |
| 910 | t["entry1"] = "foo" |
| 911 | t["entry2"] = "bar" |
| 912 | |
| 913 | -- this will display "foo" |
| 914 | print(txn:get_priv()["entry1"]) |
| 915 | |
| 916 | HTTP actions |
| 917 | ============ |
| 918 | |
Joseph Herlant | 71b4b15 | 2018-11-13 16:55:16 -0800 | [diff] [blame] | 919 | ... coming soon ... |
Thierry FOURNIER | 79c1051 | 2015-11-08 22:02:21 +0100 | [diff] [blame] | 920 | |
| 921 | Lua is fast, but my service require more execution speed |
| 922 | ======================================================== |
| 923 | |
Godbach | 06c8099 | 2016-02-16 11:59:17 +0800 | [diff] [blame] | 924 | We can write C modules for Lua. These modules must run with HAProxy while they |
Thierry FOURNIER | 79c1051 | 2015-11-08 22:02:21 +0100 | [diff] [blame] | 925 | are compliant with the HAProxy Lua version. A simple example is the "concat" |
| 926 | module. |
| 927 | |
| 928 | It is very easy to write and compile a C Lua library, however, I don't see |
| 929 | documentation about this process. So the current chapter is a quick howto. |
| 930 | |
| 931 | The entry point |
| 932 | --------------- |
| 933 | |
| 934 | The entry point is called "luaopen_<name>", where <name> is the name of the ".so" |
| 935 | file. An hello world is like this: |
| 936 | |
| 937 | #include <stdio.h> |
| 938 | #include <lua.h> |
| 939 | #include <lauxlib.h> |
| 940 | |
| 941 | int luaopen_mymod(lua_State *L) |
| 942 | { |
| 943 | printf("Hello world\n"); |
| 944 | return 0; |
| 945 | } |
| 946 | |
| 947 | The build |
| 948 | --------- |
| 949 | |
| 950 | The compilation of the source file requires the Lua "include" directory. The |
Godbach | 06c8099 | 2016-02-16 11:59:17 +0800 | [diff] [blame] | 951 | compilation and the link of the object file requires the -fPIC option. That's |
Thierry FOURNIER | 79c1051 | 2015-11-08 22:02:21 +0100 | [diff] [blame] | 952 | all. |
| 953 | |
| 954 | cc -I/opt/lua/include -fPIC -shared -o mymod.so mymod.c |
| 955 | |
| 956 | Usage |
| 957 | ----- |
| 958 | |
| 959 | You can load this module with the following Lua syntax: |
| 960 | |
| 961 | require("mymod") |
| 962 | |
Godbach | 06c8099 | 2016-02-16 11:59:17 +0800 | [diff] [blame] | 963 | When you start HAProxy, this module just print "Hello world" when it is loaded. |
Thierry FOURNIER | 79c1051 | 2015-11-08 22:02:21 +0100 | [diff] [blame] | 964 | Please, remember that HAProxy doesn't allow blocking method, so if you write a |
| 965 | function doing filesystem access or synchronous network access, all the HAProxy |
| 966 | process will fail. |