Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Concurrent file write causes content overlap #630

Open
mykytamykhailenko opened this issue Sep 17, 2023 · 1 comment
Open

Concurrent file write causes content overlap #630

mykytamykhailenko opened this issue Sep 17, 2023 · 1 comment

Comments

@mykytamykhailenko
Copy link

mykytamykhailenko commented Sep 17, 2023

I have developed an app, which executes a few heavy queries, which yield a lot of data so I decided to write the query results to multiple files concurrently.

I create a FileChannel for each separate query and write the results concurrently.

    def acquireOutputStream(path: Path) = ZIO.acquireRelease(FileChannel.open(path, options: _*)) { stream =>
      stream.flatMapBlocking(_.force(metadata = true)).orDie *>
        stream.close.orDie
    }

    // Writes data to file 
    def writeStream(stream: Stream[Throwable, Result], os: FileChannel) = ???

    def runQuery(query: String, path: Path): RIO[DatabaseProvider, Path] = ZIO.scoped {
        acquireOutputStream(path).flatMap { os =>
            ZIO.fromStreamingDBIO(SQLActionBuilder(query, (_, _) => ()).as[Result]).map(writeStream(_, os)).runDrain
        }
    }

   // Some code
   val concurrentQueries: List[RIO[DatabaseProvider, Path]] = ???

   ZIO.collectAllPar(concurrentQueries)

When I execute the code sequentially, everything works as expected (using ZIO.collectAll), but once I use ZIO.collectAllPar, content from one file can appear in the other.

The question is: even though I do not leak FileChannel anywhere, the content from one file can appear in there other (they will be mixed together as if a few streams use the same FileChannel), how can it be fixed?

Thanks for help in advance!

@mykytamykhailenko
Copy link
Author

@pablf if you have a spare minute, could you help please🙏

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant