Skip to content

MVStore used as standalone, as on-disk map provider, keeps failing with "Chunk not found" #4218

Closed

Description

@marco-brandizi

I'm having a hard time trying to use a couple of MVMap instances from an MVStore, as an on-disk implementation of Map. As you can imagine, I'd like to do so because I don't have enough RAM to process a quite big dataset.

My code works like this (in pseudo-code):

  store = MVStore.open ( "file.mvstore" ) // so, auto-commit enabled
  scoresMap = store.openMap ( "scores" )

  // Stream -> best scores
  // stream railed over about 16 parallel threads
  for each key, score in externalParallelStream:
    oldScore = scoresMap.get ( key )
    if ( oldScore != null && score <= oldScore ) continue // skip some unneded writes
    
    // check again, since we're multi-threading
    scoresMap.compute ( key, (k, s) -> s == null || score > s ? score : s )
  
  // Best scores => category counts
  countsMap = store.openMap ( "counts" )
  scoresMap.entrySet ()
  .parallelStream ()
  .forEach ( entry -> {
    category = getCategory ( entry.key () ) // bins each entry into a category
    countsMap.merge ( category, 1, (ct, one) -> ++ct )
  })

As you can see, I'm trying to compute the best scores for a set of items and then a category-based aggregation of scored items. Both the involved maps are rather big, scoresMap has something like 100M entries and countsMap has about 0.5M categories/entries, taking around 64GB of data.

The whole thing constantly fails with: "Chunk NNNN not found [2.3.232/9]".

I get this from the stack trace:

Caused by: org.h2.mvstore.MVStoreException: Chunk 1408 not found [2.3.232/9]
	at org.h2.mvstore.DataUtils.newMVStoreException(DataUtils.java:996)
	at org.h2.mvstore.FileStore.getChunk(FileStore.java:2014)
	at org.h2.mvstore.FileStore.readPage(FileStore.java:1961)
	at org.h2.mvstore.MVStore.readPage(MVStore.java:1021)
	at org.h2.mvstore.MVMap.readPage(MVMap.java:632)
	at org.h2.mvstore.Page$NonLeaf.getChildPage(Page.java:1158)
	at org.h2.mvstore.Cursor.hasNext(Cursor.java:64)
	at java.base/java.util.Iterator.forEachRemaining(Iterator.java:132)
	at java.base/java.util.Spliterators$IteratorSpliterator.forEachRemaining(Spliterators.java:1939)
	at java.base/java.util.stream.AbstractPipeline.copyInto(AbstractPipeline.java:509)
	at java.base/java.util.stream.AbstractPipeline.wrapAndCopyInto(AbstractPipeline.java:499)
	at java.base/java.util.stream.ForEachOps$ForEachOp.evaluateSequential(ForEachOps.java:151)
	at java.base/java.util.stream.ForEachOps$ForEachOp$OfRef.evaluateSequential(ForEachOps.java:174)
	at java.base/java.util.stream.AbstractPipeline.evaluate(AbstractPipeline.java:234)
	at java.base/java.util.stream.ReferencePipeline.forEach(ReferencePipeline.java:596)

That happens either upon starting the counting stream or at about 10% of it. I've tried with closing/reopening the MVStore after the first stream, I've added a store.commit() before closing it, but I keep getting the same error.

I've also increased store.setAutoCommitDelay() to 1 min, which improves things a bit, but not so much, it goes a bit further with the second stream.

I've also tried to make the counting stream sequential, but no luck.

The same procedure works perfectly fine when the maps are common HashMap instances (but takes around 64GB of heap).

To me, the problem looks similar to #4012, but I don't have the expertise to say much more.

As you see, I'm using H2 2.3.232/9, with JDK 21, on an AWS EC2 instance (ie, virtual server based on Ubuntu). I'm using an attached volume to manage the MVStore file.

Activity

  1. katzyn commented on Mar 29, 2025

    @katzyn
    Contributor

    When you use MVStore directly, you need to wrap slow operations with registerVersionUsage() / deregisterVersionUsage():

    TxCounter version = mvStore.registerVersionUsage();
    // slow read operation
    mvStore.deregisterVersionUsage(version);
  2. andreitokar commented on Mar 29, 2025

    @andreitokar
    Contributor

    Slow or not,

      scoresMap.entrySet ()
      .parallelStream ()
      .forEach ( entry -> {
        category = getCategory ( entry.key () ) // bins each entry into a category
        countsMap.merge ( category, 1, (ct, one) -> ++ct )
      })
    
    

    has to be wrapped, as described by @katzyn above.
    Otherwise, store version (snapshot), used by the cursor (iterator), will be considered as dead and might be overwritten by later store changes.
    Ideally, in case of concurrent read / writes, every read has to by protected by this pair of calls, but it might be prohibitively expensive to do it separately for each read operation, so some judgement need to be made about granularity.

  3. marco-brandizi commented on Mar 31, 2025

    @marco-brandizi
    Author

    Thanks, @katzyn, @andreitokar. Do you mean wrapping both of the above main loops? Does this have an impact on performance or heap consumption?

  4. andreitokar commented on Mar 31, 2025

    @andreitokar
    Contributor

    Definitely the code mentioned in my post above, because it is concurrent. First loop is fine, because it's single-threaded.
    Of course it does have an impact. The version of a store at the time of mvStore.registerVersionUsage() call (and all after that) will be preserved until mvStore.deregisterVersionUsage(). This may require a considerable disk space (and some related memory too) and efforts to clean it up afterwards (on-disk garbage collection). Unbalanced calls will cause unbounded file growth.

  5. marco-brandizi commented on Mar 31, 2025

    @marco-brandizi
    Author

    OK, thanks again. Due to that, I'm thinking of periodically de-registering and registering again, done in a synchronized block, but not sure how it will work.

  6. andreitokar commented on Mar 31, 2025

    @andreitokar
    Contributor

    Sorry, i missed the fact that your first loop is multi-threaded too. It has to be wrapped too. And you do not need synchronized block, since those wrapping calls are handling concurrency internally, in a non-blocking fashion.
    "Periodically" will not cut it, because you need to hold on to the version, throughout duration of the stream iterator.

  7. marco-brandizi commented on Apr 2, 2025

    @marco-brandizi
    Author

    Thanks again. I tried registering the version before the loops, and indeed, it worked, but as said above, it took a very large amount of disk space. I guess there isn't much to do about it.

  8. clausnagel commented on Jun 27, 2025

    @clausnagel

    Just stumbled across this issue. I am also using MVStore standalone and facing the same "chunk not found" issue. Wrapping the corresponding part of my code with registerVersionUsage and deregisterVersionUsage also helps in my case. But also disabling auto-commit makes the error go away. Is this just by chance or would this also be a stable solution?

  9. marco-brandizi commented on Jun 27, 2025

    @marco-brandizi
    Author

    @clausnagel I had to give up with this approach, since it doesn't seem good when the store is continuously read and updated, concurrently and at a fast speed. I've tried the operations in my original post with Apache Spark, which is designed for such use cases, and indeed it seems to work. You might be luckier with a different use case. MVStore and H2 are excellent software anyway.

  10. clausnagel commented on Jun 27, 2025

    @clausnagel

    Thanks, @marco-brandizi, for your quick reply. What I actually need for my application is a map supporting highly-concurrent reads and writes and a disk-based storage because the map will quickly grow beyond heap space limits. I was hoping to use MVStore for this purpose because it is small and lightweight. Now, can someone please confirm that - in such a highly-concurrent scenario - I always have to wrap each read with registerVersionUsage and deregisterVersionUsage? This is how I read the answers above, but I am not sure.

  11. andreitokar commented on Jun 27, 2025

    @andreitokar
    Contributor

    Yes, each access call has to be protected by being in between those registerVersionUsage / deregisterVersionUsage calls, but it does not have to be around each call separately. Group your access calls as you see fit. Downsize of having too big groups is the fact that version of the store from registerVersionUsage has to be preserved up until deregisterVersionUsage is called, which causes extra disk space consumption and subsequent efforts to reclaim that space later.

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

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions