Skip to content

A long time before jobs start running when reading parquet files #4701

Closed

Description

@nmerket

I am using dask-yarn on Amazon EMR almost exactly as described in the documentation including your handy bootstrap script (thanks).

image

I have ~16,000 parquet files (~110 GB on disk) that I'm trying to load into a dask dataframe from s3. The operation

import dask.dataframe as dd
dd.from_parquet('s3://bucket/path/to/parquet-files')

hangs longer than I have the patience to wait for (more than an hour). The workers never light up in the dask dashboard.

So, to try to get some insight, I wrote a function that just loads each parquet file into a pandas dataframe and returns its memory usage. I then randomly selected 100 of the files and submitted that function as a dask.delayed ie.

dask.compute(map(dask.delayed(get_memory_usage_of_parquet_file), subset_of_100_parquet_s3_paths))

This takes about 3 minutes to run. About 5 seconds of that is the actual work being done by the workers. Each file takes up about 70-80 MB in memory, so it's not a memory overrun. (That's why I was checking this.)

What am I missing? Is there some configuration change that will make this more performant?

Activity

  1. martindurant commented on Apr 15, 2019

    @martindurant
    Member

    This is not an issue with dask-yarn. I strongly suspect that you have no _metadata file in your parquet file hierarchy. Which parquet backend are you using? With fastparquet if you do have that metadata file or if you pass infer_divisions=False, you should be able to skip fetching details of every file from the main client thread before handing work off to the cluster.

    Note that, even in the best case, simply listing 16000 files takes some time, just bear this in mind.

  2. jcrist commented on Apr 15, 2019

    @jcrist
    Member

    Yeah, this is hitting several different (potential) issues with dask's parquet reader. I'm going to transfer this issue over to dask/dask as that location is a better fit.

  3. transferred this issue fromdask/dask-yarnon Apr 15, 2019
  4. changed the title [-]A long time before jobs start running on cluster on EMR[/-] [+]A long time before jobs start running when reading parquet files[/+] on Apr 15, 2019
  5. nmerket commented on Apr 15, 2019

    @nmerket
    Author

    I suspected that it was dask's parquet reader as well, which is why I did something like this to get around it by using pandas parquet reader directly

    fs = s3fs.S3FileSystem()
    
    def read_parquet_file_from_s3(filename):
        with fs.open(filename, mode='rb') as f:
            return pd.read_parquet(f, engine='pyarrow') # also tried fastparquet, pyarrow was faster
    
    df = dd.from_delayed(map(dask.delayed(read_parquet_file_from_s3), parquet_file_list))

    Running that function takes about 300 ms in the main thread. Mapping it to 100 files takes ~3 minutes. 2:55 of that the workers weren't doing anything.

  6. jcrist commented on Apr 15, 2019

    @jcrist
    Member

    Running that function takes about 300 ms in the main thread. Mapping it to 100 files takes ~3 minutes. 2:55 of that the workers weren't doing anything.

    Hmmm, that is odd. That still has nothing to do with dask-yarn (which only sets up the workers). You might look at the profile tab of the dashboard, or using Client.profile to collect some profiling information. Are the workers idle for 2:55 and then do work for the last 0:05? Is there any way you could build a reproducible example showing this issue? This is not something I've seen before when using parquet with dask.

  7. nmerket commented on Apr 15, 2019

    @nmerket
    Author

    When I hook it up to a profiler (snakeviz) what I'm seeing is that 383/387 seconds running were spent pickling.

  8. martindurant commented on Apr 15, 2019

    @martindurant
    Member

    Is that also true for your delayed way? The only thing I see there that needs pickling is the fs instance, which would surprise me a lot. (You could create the instance in the function rather than include it in the closure for pickling)

  9. nmerket commented on Apr 15, 2019

    @nmerket
    Author

    Yes, that was for my delayed way. Does it try to pickle all of pandas as well or is it smart enough to know that it can import that?

  10. nmerket commented on Apr 15, 2019

    @nmerket
    Author

    Whoa, when I move the fs instance creation inside the function, it's almost immediate now.

  11. jcrist commented on Apr 15, 2019

    @jcrist
    Member

    Does it try to pickle all of pandas as well or is it smart enough to know that it can import that?

    cloudpickle will only serialize functions as bytecode for functions defined in __main__ (e.g. in an interpreter or script). Anything imported will be assumed to have the same import paths on the workers.

    What it looks like you're seeing here is that fs is included in the closure of your function (instead of creating it in the function or using it as a parameter), leading to a potentially slower pickling path. I'm not sure why this would lead to that much of a slowdown though - cloudpickle is usually pretty smart about these things. Do you still get the speedup if you use fs as a parameter instead?

    fs = s3fs.S3FileSystem()
    
    @dask.delayed
    def read_parquet_file_from_s3(fs, filename):
        with fs.open(filename, mode='rb') as f:
            return pd.read_parquet(f, engine='pyarrow')
    
    df = dd.from_delayed(read_parquet_file_from_s3(fs, fn) for fn in parquet_file_list)

    Could also try serializing the filesystem separately once to the cluster:

    fs = dask.delayed(s3fs.S3FileSystem())  # keeps this as a separate value in the grapu
    
    @dask.delayed
    def read_parquet_file_from_s3(fs, filename):
        with fs.open(filename, mode='rb') as f:
            return pd.read_parquet(f, engine='pyarrow')
    
    df = dd.from_delayed(read_parquet_file_from_s3(fs, fn) for fn in parquet_file_list)

    If these are both fast, something may be odd with cloudpickle, otherwise something is maybe odd with s3fs.

  12. birdsarah commented on Apr 28, 2019

    @birdsarah
    Contributor

    I've been having the same problem. My data is ~5000 files. The above pasted code was really helpful for being able to take a look at my data, but I was having the same problem when I tried to write my processed data out.

    I was regularly seeing errors about too many files open which I never got to the bottom of fixing.

    In the end what I did was this:

    • read my data in with spark and then out again - this assembled all my data in one directory (previously it was nested into many smaller subdirectories) and also merged it into a smaller number ~500 file (this was unintentional but seemed to help)
    • I then went back to dask, read my data in, repartitioned to 5000 partitions, performed all my processing and saved, and everything's working.
  13. birdsarah commented on Apr 28, 2019

    @birdsarah
    Contributor

    It turned out to be too slow to work with 5000 files, I'm down to 1200 which is the smallest I can get away with for the size of my data. Even still something as simple as df.head() on 1200 files which do have _common_metadata is taking minutes.

  14. martindurant commented on Apr 28, 2019

    @martindurant
    Member

    Are you using infer_divisions=False?

  15. birdsarah commented on Apr 28, 2019

    @birdsarah
    Contributor

    Are you using infer_divisions=False?

    No, should I?

  16. 4 remaining items

  17. birdsarah commented on May 1, 2019

    @birdsarah
    Contributor

    Thanks to some help from @mrocklin on how to go about profiling, I've so far learned the following:

    When reading from local disk, the top of my profile is:

    465073 function calls (462713 primitive calls) in 2.781 seconds
    
       Ordered by: internal time
    
       ncalls  tottime  percall  cumtime  percall filename:lineno(function)
         2501    1.356    0.001    1.390    0.001 {method 'open' of 'pyarrow._parquet.ParquetReader' objects}
        67527    0.264    0.000    0.400    0.000 parquet.py:137(_visit_piece)
         2501    0.251    0.000    0.871    0.000 parquet.py:132(_build_nested_paths)
        67635    0.141    0.000    0.221    0.000 compat.py:142(frombytes)
    

    when reading from s3 it is:

             12844488 function calls (12794206 primitive calls) in 392.863 seconds
    
       Ordered by: internal time
    
       ncalls  tottime  percall  cumtime  percall filename:lineno(function)
        25235  343.676    0.014  343.676    0.014 {method 'read' of '_ssl._SSLSocket' objects}
        25163    5.563    0.000   10.744    0.000 hooks.py:390(_alias_event_name)
           27    3.684    0.136    3.684    0.136 {method 'do_handshake' of '_ssl._SSLSocket' objects}
      1459059    2.647    0.000    2.647    0.000 {method 'index' of 'list' objects}
      1870260    2.616    0.000    2.616    0.000 {method 'split' of 'str' objects}
           27    1.809    0.067    1.809    0.067 {method 'connect' of '_socket.socket' objects}
       919944    1.087    0.000    1.087    0.000 {method 'lower' of 'str' objects}
       152646    0.850    0.000    2.328    0.000 awsrequest.py:633(__getitem__)
         2501    0.782    0.000  388.640    0.155 {method 'open' of 'pyarrow._parquet.ParquetReader' objects}
    

    First thing to note is that _parquet.ParquetReader is getting called 2x as many partitions as I have (in both s3 and local)

    After some more digging:

    • the same problem seems to persist between fastparquet and pyarrow.
    • I'm wondering if the problem is with scheduling a lot of small boto3 sessions / tasks - but i don't really know how to dig into this more.
  18. birdsarah commented on May 1, 2019

    @birdsarah
    Contributor

    @martindurant can you explain the mechanism to me a little.

    I'm doing my profiling on just df = dd.read_parquet('s3://file', ....) This line is taking about 5 minutes to run on for a 1200 partition parquet file on s3. I haven't tried to compute anything.

    Why does dask need to read every parquet partition in order to compute start building the graph? I understand reading the directory or needing to gather all the file names, but that's different than opening every file. If it's to check metadata, could a user be able to supply a schema to bypass this step (at the user's own risk?). Or do you need to construct an index or something?

    My frustration is that my parallelized operation takes seconds with the size of cluster i have, which includes the read, but getting started takes minutes. Now it may be that my read is benefit from some cacheing that's happening after the initial graph reading. Oddly though i don't benefit from any cacheing in the future. If data has been loaded on first read, I would have thought second read would be quicker.

  19. martindurant commented on May 1, 2019

    @martindurant
    Member

    Parquet maintains more information in its metadata besides the schema: it includes the max and min of the values of each column within each chunk. Dask uses this information to construct the "divisions" of a dataframe, if possible, so that when you do a selection df.loc[..], Dask can figure out which of the partitions don't need to be loaded. This in general is a very useful optimisation, especially when all of the max/min values are held in the special _metadata file, which can be read in a single operation.

    Using dd.read_parquet(..., infer_divisions=False), tells Dask that you don't care about the divisions and don't expect to gain from this sort of optimisation - so it can skip reading the metadata parts of all of the files.

  20. birdsarah commented on Jul 23, 2019

    @birdsarah
    Contributor
  21. mrocklin commented on Jul 23, 2019

    @mrocklin
    Member
  22. birdsarah commented on Jul 23, 2019

    @birdsarah
    Contributor

    :) I'm just reading the PR.

  23. birdsarah commented on Jul 26, 2019

    @birdsarah
    Contributor

    With #5157 I believe this is done for fastparquet.

    Am I right in thinking @martindurant @pitrou that this fast load cannot be achieved with pyarrow as pyarrow insists on doing it's own on-load checks on parquet parts without metadata?

  24. rjzamora commented on Jul 26, 2019

    @rjzamora
    Member

    @birdsarah I think we can probably tweak the pyarrow engine to do something similar to the fastparquet engine in #5157. Hopefully I’ll have time to test this tomorrow.

  25. martindurant commented on Jul 26, 2019

    @martindurant
    Member

    Agree, definitely think that pyarrow should be able to do this too

  26. pitrou commented on Jul 26, 2019

    @pitrou
    Member

    I'm not competent on this, but @wesm may have an answer.

  27. wesm commented on Jul 26, 2019

    @wesm
    Contributor

    Readability issues with https://github.com/dask/dask/blob/master/dask/dataframe/io/parquet/fastparquet.py aside (I had to spend some time parsing what is going on in read_metadata -- I would personally refactor to make some helper functions to help with readability) I don't see a problem with doing the same work for the Arrow engine. I believe there are still some follow up items having to do with the _metadata and _common_metadata files but I have not kept track of what they are. If you run into issues please do open JIRA issues so we don't lose track of the work

  28. jcrist commented on Apr 13, 2022

    @jcrist
    Member

    Closing this as stale and unactionable. Parquet performance has improved considerably in the last few years, and there are several other issues tracking parquet performance and heuristics that are better scoped. Closing.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions