Welcome! Log In Create A New Profile

Advanced

Re: cache: move open to thread pool

Maxim Dounin
September 13, 2018 02:12PM
Hello!

On Tue, Sep 04, 2018 at 04:58:05PM -0700, Ka-Hing Cheung via nginx-devel wrote:

> On Mon, Sep 3, 2018 at 9:09 AM, Maxim Dounin <mdounin@mdounin.ru> wrote:
> > The biggest problem with in-place solution is that it requires
> > in-place coding for all the places where we want to use aio_open.
> > Currently we use ngx_open_cached_file() to open files everywhere,
> > and it would be a good idea to preserve it as a starting point for
> > all file open operations.
> >
> > This should be beneficial even if aio_open is only used when open
> > file cache is actually disabled. And I'm actually fine with
> > implementing this only when open file cache is disabled - but
> > implementing this in ngx_open_cached_file(), to make it easier to
> > add more places which use aio_open, and also makeing it possible
> > to further improve things by making it compatible with
> > open_file_cache.
>
> The most difficult thing to switch to threaded open isn't which method
> to call, but to expect NGX_AGAIN and have the necessary bookkeeping in
> place to resume, so imo this is a pretty weak argument.

Sure. But adding an alternative interface and dozens of lines of
the otherwise non-needed code everywhere we want to support
aio_open is something we can easily avoid, and we should do it.

> > Adding a code path somewhere in ngx_open_file_wrapper() should be
> > simple enough. Note that probably there is no need to cover
> > stat() as well, as well as to address various symlinks cases - I
> > think it would be fine if aio_open will only work when there is no
> > need to disable symlinks, and will only work for open(), as stat()
> > after an open is expected to be fast.
>
> A problem is that someone need to own the thread task (which for other
> aio is owned by ngx_file, and someone needs to own the ngx_file).
> ngx_open_cached_file doesn't care about anything beyond the file name.
> That can of course change, but changing API is subjective and
> difficult for me as an external contributor to decide on. I can try to
> find time to do the change if you want to comment on how it should
> look like.

The ngx_open_file_cache() function takes a ngx_open_file_info_t
structure with various arguments, and it would be logical to
extend this structure to keep thread task as well.

> >> > - The code calls ngx_open_and_stat_file() whithin a thread, which
> >> > is relatively complex function never designed to be thread safe.
> >> > While it looks like currently it is, a better solution would be to
> >> > introduce a separate simple function to execute within a thread,
> >> > similar to ngx_thread_read_handler().
> >>
> >> I kept using ngx_open_and_stat_file() as we are looking into moving
> >> other types of open into thread pool, so we will probably need to have
> >> similar logic anyway.
> >
> > The problem is that ngx_open_and_stat_file() is never coded to be
> > thread safe, and this is expected to cause problems sooner or
>
> switched to use ngx_open_file_wrapper

You probably didn't get the point. You shouldn't use existing
functions - all them were not designed to be thread safe.
Instead, write a simple function using only low-level calls. Please
take a look at ngx_thread_read_handler() for an example.

> > While this looks slightly better than in the previous iteration,
> > this still leaves a lot to be desired. In particular, this still
> > uses duplicate "rv == NGX_DECLINED" checks.
>
> I poked around at it and thinks this one line duplication is the
> cleanest. ngx_http_file_cache_read can also return DECLINED and
> cleaning up nginx return code seems to be beyond the scope of this
> work.

We can live with some duplication, but the whole thing needs to
be improved somehow to be more readable. The thing which bugs me
most is an opaque "c->open_rv" field, which is also passed as a
separate argument into the ngx_http_file_cache_aio_open()
function. Using some more descriptive name and saving it always
to avoid separate argument might be a way to go.

> >> + ngx_open_file_info_t of;
> >> + ngx_int_t rv;
> >> +} ngx_thread_file_open_ctx_t;
> >> +
> >> +typedef struct {
> >> + union {
> >> + ngx_thread_file_open_ctx_t open;
> >> + ngx_thread_file_read_ctx_t read;
> >> + };
> >> } ngx_thread_file_ctx_t;
> >
> > Please keep things in a single structure instead. This will allow
> > to avoid many coresponding changes in the file.
>
> Is 72 bytes per request not worth saving?

To save bytes, consider re-thinking the data used for open (and
you anyway has to do it when re-implementing the in-thread code as
a simple function using only low-level calls). Just a file name,
fd, and a return code should be enough here - and most of these
fields are already available in the existing ngx_thread_file_ctx_t
structure.

> >> typedef int ngx_fd_t;
> >> typedef struct stat ngx_file_info_t;
> >> typedef ino_t ngx_file_uniq_t;
> >>
> >> +#include <ngx_open_file_cache.h>
> >>
> >
> > This introduces a dependency of low-level code from high-level
> > OS-independant structures in ngx_open_file_cache.h. Instead, some
> > low-level interface should be used.
>
> Do you have a more concrete suggestion?

See above.

[...]

> diff --git src/core/ngx_open_file_cache.h src/core/ngx_open_file_cache.h
> index d119c129..9c1c6bdc 100644
> --- src/core/ngx_open_file_cache.h
> +++ src/core/ngx_open_file_cache.h
> @@ -7,6 +7,7 @@
>
> #include <ngx_config.h>
> #include <ngx_core.h>
> +#include <ngx_queue.h>
>
>
> #ifndef _NGX_OPEN_FILE_CACHE_H_INCLUDED_

This looks like an unrelated change.

[...]

> @@ -398,7 +356,6 @@ done:
> return rv;
> }
>
> -
> static ngx_int_t
> ngx_http_file_cache_lock(ngx_http_request_t *r, ngx_http_cache_t *c)
> {

As previously pointed out, this is an unrelated change and a
style issue.

[...]

--
Maxim Dounin
http://mdounin.ru/
_______________________________________________
nginx-devel mailing list
nginx-devel@nginx.org
http://mailman.nginx.org/mailman/listinfo/nginx-devel
Subject Author Views Posted

cache: move open to thread pool

Ka-Hing Cheung via nginx-devel 2823 August 07, 2018 05:28PM

Re: cache: move open to thread pool

Maxim Dounin 846 August 08, 2018 02:18PM

RE: cache: move open to thread pool

erankor 804 August 08, 2018 03:46PM

Re: cache: move open to thread pool

Ka-Hing Cheung via nginx-devel 632 August 27, 2018 06:00PM

RE: cache: move open to thread pool

erankor 739 August 28, 2018 05:18AM

Re: cache: move open to thread pool

Ka-Hing Cheung via nginx-devel 634 August 09, 2018 05:44PM

Re: cache: move open to thread pool

Maxim Dounin 1098 August 10, 2018 07:42AM

Re: cache: move open to thread pool

Ka-Hing Cheung via nginx-devel 642 August 27, 2018 05:40PM

Re: cache: move open to thread pool

Maxim Dounin 758 September 03, 2018 12:10PM

Re: cache: move open to thread pool

Ka-Hing Cheung via nginx-devel 688 September 04, 2018 08:00PM

Re: cache: move open to thread pool

Maxim Dounin 648 September 13, 2018 02:12PM

Re: cache: move open to thread pool

Roman Arutyunyan 760 October 04, 2018 05:34AM

Re: cache: move open to thread pool

Roman Arutyunyan 749 November 01, 2018 08:30AM

Re: cache: move open to thread pool

Maxim Konovalov 631 November 01, 2018 12:08PM

Re: cache: move open to thread pool

Roman Arutyunyan 465 August 03, 2021 04:52AM

Re: cache: move open to thread pool

Maxim Konovalov 629 November 08, 2018 09:20AM

Re: cache: move open to thread pool

Ka-Hing Cheung via nginx-devel 667 November 15, 2018 05:54PM

Re: cache: move open to thread pool

Maxim Konovalov 703 November 16, 2018 04:12AM

Re: cache: move open to thread pool

Maxim Konovalov 672 December 05, 2018 04:44AM

Re: cache: move open to thread pool

Maxim Konovalov 615 January 10, 2019 03:32AM

Re: cache: move open to thread pool

Ka-Hing Cheung via nginx-devel 666 January 14, 2019 08:54PM

Re: cache: move open to thread pool

Ka-Hing Cheung via nginx-devel 585 January 22, 2019 01:22PM

Re: cache: move open to thread pool

Ka-Hing Cheung via nginx-devel 662 January 22, 2019 02:36PM

Re: cache: move open to thread pool

Maxim Konovalov 626 January 23, 2019 07:40AM

Re: cache: move open to thread pool

Ka-Hing Cheung via nginx-devel 609 February 06, 2019 04:20PM

Re: cache: move open to thread pool

Maxim Konovalov 649 February 07, 2019 08:40AM

Re: cache: move open to thread pool

Ka-Hing Cheung via nginx-devel 695 February 08, 2019 05:42PM

Re: cache: move open to thread pool

karton 530 July 19, 2019 11:28AM

Re: cache: move open to thread pool

Maxim Konovalov 573 July 19, 2019 02:44PM

Re: cache: move open to thread pool

karton 498 July 19, 2019 09:46PM

Re: cache: move open to thread pool

Roman Arutyunyan 549 July 22, 2019 10:06AM

Re: cache: move open to thread pool

karton 524 July 22, 2019 11:40AM

Re: cache: move open to thread pool

karton 540 July 23, 2019 11:48PM

Re: cache: move open to thread pool

Roman Arutyunyan 607 August 16, 2019 10:26AM

Re: cache: move open to thread pool

karton 664 August 16, 2019 10:52AM

Re: cache: move open to thread pool

NCviQwilt 516 September 02, 2021 12:22PM

Re: cache: move open to thread pool

Roman Arutyunyan 559 September 09, 2021 06:50AM



Sorry, you do not have permission to post/reply in this forum.

Online Users

Guests: 327
Record Number of Users: 8 on April 13, 2023
Record Number of Guests: 421 on December 02, 2018
Powered by nginx      Powered by FreeBSD      PHP Powered      Powered by MariaDB      ipv6 ready