You are viewing a plain text version of this content. The canonical link for it is here.
Posted to dev@trafficserver.apache.org by Walt Karas <wk...@yahooinc.com.INVALID> on 2022/07/13 16:14:04 UTC

Re: [E] Fwd: Lua plugin development - Global Variables

There is a way to do this in the C API (global arguments with
TSUserArgSet/Get).  But it doesn't look like these functions are supported
in the Lua API.  Can you keep the data in a file, perhaps on a RAM disk to
make access fast?

On Wed, Jul 13, 2022 at 4:41 AM Vishal Garg <ga...@media.net.invalid>
wrote:

> Hey,
> ATS Version: v9.1
> OS: Ubuntu 22.04
>
> I am writing a Lua plugin to allow custom rate limiting logic.
> Right now each request has its own Lua state and thus no variables are
> shared.
>
> I want to have a piece of config loaded which is read only and shared
> across all requests coming in.
> Current implementation loads config from Redis on each request and stores
> it in ctx to be used in multiple stages of a request.
>
> Is there a global variable available in Lua which I can initialize for the
> entire lifetime of ATS running and can be accessed by all requests?
>
> Thanks in advance
> --
> *Vishal Garg*
> Site Reliability Engineer II
> Autoopt-3
>

Re: [E] Fwd: Lua plugin development - Global Variables

Posted by Vishal Garg <ga...@media.net.INVALID>.
Hey,
Oh I didn't realize that this was initialized once per VM.
I reduced the number of states to 1 and immediately saw it maintaining
state.

*tslua.so --states=1 /etc/trafficserver/script/global_ratelimiter.lua*

Earlier it was set at 64, and thus did not see it happening immediately.
This is amazing.

Thanks a ton for the help. I'd been stuck at it for so long.


On Fri, Jul 15, 2022 at 11:56 AM Shu Kit Chan <ch...@gmail.com> wrote:

> The global variable in the lua script is available for the lifetime of
> the ATS. If you use __init__() function to initialize it, you are only
> doing it once per VM, not once per request.
>
> e.g.
> https://docs.trafficserver.apache.org/en/latest/admin-guide/plugins/lua.en.html#example-scripts
> In "sethost.lua", "HOSTNAME" is set only once per VM when ATS starts.
> And each request doing "do_remap" will be getting the "HOSTNAME" to be
> used in a header.
>
> Perhaps you can elaborate more on your script and your use case. Maybe
> I am not fully clear on your needs.
>
> Thanks.
>
> Kit
>
> On Thu, Jul 14, 2022 at 11:15 PM Vishal Garg <ga...@media.net.invalid>
> wrote:
> >
> > Hi @wkaras & @chanshukit,
> > Thanks for your responses.
> >
> > @chanshukit what you've recommended solves one issue of keeping a
> variable
> > alive for the entire lifetime of a single request.
> > But this variable is not available for the lifetime of ATS itself. That
> > means each new request would initialize it fresh.
> >
> > @wkaras
> > We expect to serve around 300K QPM via ATS. Do you think it's prudent to
> > keep loading the same config over and over into the current lua context?
> > I am just trying to understand the impact of doing IO (faster because of
> > RAM) but still IO.
> > I tried mapping the file into memory and it sure works but I am concerned
> > if this cause memory issues down the line.
> >
> > Thanks
> >
> >
> > On Thu, Jul 14, 2022 at 1:10 AM Shu Kit Chan <ch...@gmail.com>
> wrote:
> >
> > > You can simply use a a global variable in the lua script to store the
> > > configuration information
> > >
> > > And you can initialize that in the __init__ function, which is run at
> > > the start of the server.
> > >
> > > You can see some examples.
> > > here -
> > >
> https://docs.trafficserver.apache.org/en/latest/admin-guide/plugins/lua.en.html#ts-stat-create
> > > and here -
> > >
> https://docs.trafficserver.apache.org/en/latest/admin-guide/plugins/lua.en.html#example-scripts
> > >
> > > On Wed, Jul 13, 2022 at 12:00 PM Walt Karas
> <wk...@yahooinc.com.invalid>
> > > wrote:
> > > >
> > > > There is a way to do this in the C API (global arguments with
> > > > TSUserArgSet/Get).  But it doesn't look like these functions are
> > > supported
> > > > in the Lua API.  Can you keep the data in a file, perhaps on a RAM
> disk
> > > to
> > > > make access fast?
> > > >
> > > > On Wed, Jul 13, 2022 at 4:41 AM Vishal Garg <garg.v@media.net.invalid
> >
> > > > wrote:
> > > >
> > > > > Hey,
> > > > > ATS Version: v9.1
> > > > > OS: Ubuntu 22.04
> > > > >
> > > > > I am writing a Lua plugin to allow custom rate limiting logic.
> > > > > Right now each request has its own Lua state and thus no variables
> are
> > > > > shared.
> > > > >
> > > > > I want to have a piece of config loaded which is read only and
> shared
> > > > > across all requests coming in.
> > > > > Current implementation loads config from Redis on each request and
> > > stores
> > > > > it in ctx to be used in multiple stages of a request.
> > > > >
> > > > > Is there a global variable available in Lua which I can initialize
> for
> > > the
> > > > > entire lifetime of ATS running and can be accessed by all requests?
> > > > >
> > > > > Thanks in advance
> > > > > --
> > > > > *Vishal Garg*
> > > > > Site Reliability Engineer II
> > > > > Autoopt-3
> > > > >
> > >
> >
> >
> > --
> > *Vishal Garg*
> > Site Reliability Engineer II
> > Autoopt-3
>


-- 
*Vishal Garg*
Site Reliability Engineer II
Autoopt-3

Re: [E] Fwd: Lua plugin development - Global Variables

Posted by Shu Kit Chan <ch...@gmail.com>.
The global variable in the lua script is available for the lifetime of
the ATS. If you use __init__() function to initialize it, you are only
doing it once per VM, not once per request.

e.g. https://docs.trafficserver.apache.org/en/latest/admin-guide/plugins/lua.en.html#example-scripts
In "sethost.lua", "HOSTNAME" is set only once per VM when ATS starts.
And each request doing "do_remap" will be getting the "HOSTNAME" to be
used in a header.

Perhaps you can elaborate more on your script and your use case. Maybe
I am not fully clear on your needs.

Thanks.

Kit

On Thu, Jul 14, 2022 at 11:15 PM Vishal Garg <ga...@media.net.invalid> wrote:
>
> Hi @wkaras & @chanshukit,
> Thanks for your responses.
>
> @chanshukit what you've recommended solves one issue of keeping a variable
> alive for the entire lifetime of a single request.
> But this variable is not available for the lifetime of ATS itself. That
> means each new request would initialize it fresh.
>
> @wkaras
> We expect to serve around 300K QPM via ATS. Do you think it's prudent to
> keep loading the same config over and over into the current lua context?
> I am just trying to understand the impact of doing IO (faster because of
> RAM) but still IO.
> I tried mapping the file into memory and it sure works but I am concerned
> if this cause memory issues down the line.
>
> Thanks
>
>
> On Thu, Jul 14, 2022 at 1:10 AM Shu Kit Chan <ch...@gmail.com> wrote:
>
> > You can simply use a a global variable in the lua script to store the
> > configuration information
> >
> > And you can initialize that in the __init__ function, which is run at
> > the start of the server.
> >
> > You can see some examples.
> > here -
> > https://docs.trafficserver.apache.org/en/latest/admin-guide/plugins/lua.en.html#ts-stat-create
> > and here -
> > https://docs.trafficserver.apache.org/en/latest/admin-guide/plugins/lua.en.html#example-scripts
> >
> > On Wed, Jul 13, 2022 at 12:00 PM Walt Karas <wk...@yahooinc.com.invalid>
> > wrote:
> > >
> > > There is a way to do this in the C API (global arguments with
> > > TSUserArgSet/Get).  But it doesn't look like these functions are
> > supported
> > > in the Lua API.  Can you keep the data in a file, perhaps on a RAM disk
> > to
> > > make access fast?
> > >
> > > On Wed, Jul 13, 2022 at 4:41 AM Vishal Garg <ga...@media.net.invalid>
> > > wrote:
> > >
> > > > Hey,
> > > > ATS Version: v9.1
> > > > OS: Ubuntu 22.04
> > > >
> > > > I am writing a Lua plugin to allow custom rate limiting logic.
> > > > Right now each request has its own Lua state and thus no variables are
> > > > shared.
> > > >
> > > > I want to have a piece of config loaded which is read only and shared
> > > > across all requests coming in.
> > > > Current implementation loads config from Redis on each request and
> > stores
> > > > it in ctx to be used in multiple stages of a request.
> > > >
> > > > Is there a global variable available in Lua which I can initialize for
> > the
> > > > entire lifetime of ATS running and can be accessed by all requests?
> > > >
> > > > Thanks in advance
> > > > --
> > > > *Vishal Garg*
> > > > Site Reliability Engineer II
> > > > Autoopt-3
> > > >
> >
>
>
> --
> *Vishal Garg*
> Site Reliability Engineer II
> Autoopt-3

Re: [E] Fwd: Lua plugin development - Global Variables

Posted by Vishal Garg <ga...@media.net.INVALID>.
Hi @wkaras & @chanshukit,
Thanks for your responses.

@chanshukit what you've recommended solves one issue of keeping a variable
alive for the entire lifetime of a single request.
But this variable is not available for the lifetime of ATS itself. That
means each new request would initialize it fresh.

@wkaras
We expect to serve around 300K QPM via ATS. Do you think it's prudent to
keep loading the same config over and over into the current lua context?
I am just trying to understand the impact of doing IO (faster because of
RAM) but still IO.
I tried mapping the file into memory and it sure works but I am concerned
if this cause memory issues down the line.

Thanks


On Thu, Jul 14, 2022 at 1:10 AM Shu Kit Chan <ch...@gmail.com> wrote:

> You can simply use a a global variable in the lua script to store the
> configuration information
>
> And you can initialize that in the __init__ function, which is run at
> the start of the server.
>
> You can see some examples.
> here -
> https://docs.trafficserver.apache.org/en/latest/admin-guide/plugins/lua.en.html#ts-stat-create
> and here -
> https://docs.trafficserver.apache.org/en/latest/admin-guide/plugins/lua.en.html#example-scripts
>
> On Wed, Jul 13, 2022 at 12:00 PM Walt Karas <wk...@yahooinc.com.invalid>
> wrote:
> >
> > There is a way to do this in the C API (global arguments with
> > TSUserArgSet/Get).  But it doesn't look like these functions are
> supported
> > in the Lua API.  Can you keep the data in a file, perhaps on a RAM disk
> to
> > make access fast?
> >
> > On Wed, Jul 13, 2022 at 4:41 AM Vishal Garg <ga...@media.net.invalid>
> > wrote:
> >
> > > Hey,
> > > ATS Version: v9.1
> > > OS: Ubuntu 22.04
> > >
> > > I am writing a Lua plugin to allow custom rate limiting logic.
> > > Right now each request has its own Lua state and thus no variables are
> > > shared.
> > >
> > > I want to have a piece of config loaded which is read only and shared
> > > across all requests coming in.
> > > Current implementation loads config from Redis on each request and
> stores
> > > it in ctx to be used in multiple stages of a request.
> > >
> > > Is there a global variable available in Lua which I can initialize for
> the
> > > entire lifetime of ATS running and can be accessed by all requests?
> > >
> > > Thanks in advance
> > > --
> > > *Vishal Garg*
> > > Site Reliability Engineer II
> > > Autoopt-3
> > >
>


-- 
*Vishal Garg*
Site Reliability Engineer II
Autoopt-3

Re: [E] Fwd: Lua plugin development - Global Variables

Posted by Shu Kit Chan <ch...@gmail.com>.
You can simply use a a global variable in the lua script to store the
configuration information

And you can initialize that in the __init__ function, which is run at
the start of the server.

You can see some examples.
here - https://docs.trafficserver.apache.org/en/latest/admin-guide/plugins/lua.en.html#ts-stat-create
and here - https://docs.trafficserver.apache.org/en/latest/admin-guide/plugins/lua.en.html#example-scripts

On Wed, Jul 13, 2022 at 12:00 PM Walt Karas <wk...@yahooinc.com.invalid> wrote:
>
> There is a way to do this in the C API (global arguments with
> TSUserArgSet/Get).  But it doesn't look like these functions are supported
> in the Lua API.  Can you keep the data in a file, perhaps on a RAM disk to
> make access fast?
>
> On Wed, Jul 13, 2022 at 4:41 AM Vishal Garg <ga...@media.net.invalid>
> wrote:
>
> > Hey,
> > ATS Version: v9.1
> > OS: Ubuntu 22.04
> >
> > I am writing a Lua plugin to allow custom rate limiting logic.
> > Right now each request has its own Lua state and thus no variables are
> > shared.
> >
> > I want to have a piece of config loaded which is read only and shared
> > across all requests coming in.
> > Current implementation loads config from Redis on each request and stores
> > it in ctx to be used in multiple stages of a request.
> >
> > Is there a global variable available in Lua which I can initialize for the
> > entire lifetime of ATS running and can be accessed by all requests?
> >
> > Thanks in advance
> > --
> > *Vishal Garg*
> > Site Reliability Engineer II
> > Autoopt-3
> >