Skip to content

MCR-3942 OCFL remote cache conflicts, GZIP head reads and stale file trees after purge - #3256

Open
Mewel wants to merge 2 commits into
mainfrom
issues/MCR-3942-ocfl-fixes
Open

Mewel wants to merge 2 commits into
mainfrom
issues/MCR-3942-ocfl-fixes

Conversation

@Mewel

@Mewel Mewel commented Nov 25, 2026 •

Copy link
Copy Markdown
Member

Link to jira.

  • MCROCFLDefaultRemoteTemporaryStorage writes cache files to a temporary file first and moves them to their final path atomically, replacing an existing file with the same digest. A file that exists on disk but not in the cache index no longer fails the import with FileAlreadyExistsException, and concurrent writes of the same content do not conflict
  • MCROCFLXMLMetadataManager.retrieveContent(MCRObjectID) uses getContent(), so MCRGZIPOCFLXMLMetadataManager decompresses the current version. The content is bound to the version that was checked, instead of resolving the head again when it is read
  • MCROCFLRepository notifies listeners about purged objects. The MCROCFLVirtualObjectProvider removes all cached versions of a purged object and does not cache versions loaded during a purge, so a reused id is not read from the purged object. Purges by another process using the same repository are not noticed
  • the read cache is no longer modified inside computeIfAbsent, and written objects are invalidated right after each persist

Pull Request Checklist (Author)

Please go through the following checklist before assigning the PR for review:

Ticket & Documentation

  • The issue in the ticket is clearly described and the solution is documented.
  • Design decisions (if any) are explained.
  • The ticket references the correct source and target branches.
  • The fixed-version is correctly set in the ticket and matches the PR's target branch (main).

Feature & Improvement Specific Checks

  • Instructions on how to test or use the feature are included or linked (e.g. to documentation).
  • For UI changes: before & after screenshots are attached.
  • New features or migrations are documented.
  • Does this change affect existing applications, data, or configurations?
    • Yes: Is a migration required? If yes, describe it.
    • Breaking change is marked in the commit message.

Bugfix-Specific Checks

  • Affected version is listed in the ticket.
  • Minimal code changes were made (no refactoring).
  • This PR truly fixes only the reported bug.
  • No breaking changes are introduced.
  • A relevant test was added (if feasible).

Testing

  • I have tested the changes locally.
  • The feature behaves as described in the ticket.
  • Were existing tests modified?
    • Yes: explain the changes for reviewers.

MCR Conventions & Metadata

  • MCR naming conventions are followed
  • If the public API has changed:
    • Old API is deprecated or a migration is documented.
    • If not, no action needed.
  • Java license headers are added where necessary.
  • Javadoc is written for non-self-explanatory classes/methods (Clean Code).
  • All configuration options are documented in Javadoc and mycore.properties.
  • No default properties are hardcoded — all set via mycore.properties.

Multi-Repo Considerations

  • Is an equivalent PR in MIR required?
    • If yes, is it already created?

@Mewel Mewel changed the title MCR-3942 Fix OCFL remote cache conflicts, GZIP head reads and stale file trees after purge MCR-3942 OCFL remote cache conflicts, GZIP head reads and stale file trees after purge Nov 25, 2026

@sebhofmann sebhofmann left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Merge assessment: Mergeable after the allocation accounting in cacheUpdate is fixed (see inline comment). The two nits are optional. CI is green.

This review was generated by an AI assistant (Claude) on behalf of sebhofmann.

* from the digest of the content, so the existing file has the same content. It may exist on disk without
* being in the cache index, for example if another process using the same directory wrote it.
*/
private static void moveToFinalPath(Path tempFile, Path finalPath) throws IOException {

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Cache allocation drifts on concurrent writes of the same content

Before this change, the second writer of the same content failed with FileAlreadyExistsException. Now both writers reach cacheUpdate, which is not atomic:

FileInfo fileInfo = this.cache.get(digest);   // both threads see null
boolean newFile = fileInfo == null;
this.totalAllocation.addAndGet(newFile ? size : size - fileInfo.size);  // size is added twice
this.cache.put(...);

If two threads write the same content and both pass the containsKey check before either updates the cache, totalAllocation is permanently too high. The eviction strategy then evicts too early, and the journal gets two Add events.

Suggestion: use the return value of put:

FileInfo previous = this.cache.put(digest, new FileInfo(digest, size, originalFileName));
this.totalAllocation.addAndGet(previous == null ? size : size - previous.size());

This review was generated by an AI assistant (Claude) on behalf of sebhofmann.

// fill the read cache with v1
assertTrue(Files.exists(MCRVersionedPath.getPath(derivate, "v1", "/old.txt")));

if (purge) {

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nit: purge is reused here to choose between purging through the file system and through the metadata manager, while elsewhere it is the purge configuration. It works because removeRoot only purges with purge=true, but it is confusing to read. A short comment would help.

This review was generated by an AI assistant (Claude) on behalf of sebhofmann.

@Override
public void purgeObject(String objectId) {
base.purgeObject(objectId);
purgeListeners.forEach(listener -> listener.accept(objectId));

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nit: the listeners run after base.purgeObject. If a listener throws, the caller sees a failure although the object is already purged. Not relevant for invalidateAll, but a try/catch with LOGGER.error per listener would be more robust for future listeners.

This review was generated by an AI assistant (Claude) on behalf of sebhofmann.

@Mewel
Mewel requested a review from sebhofmann November 26, 2026 13:55
@Mewel Mewel mentioned this pull request Nov 26, 2026
7 of 29 tasks

@sebhofmann sebhofmann left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

All three points of the previous review are fixed.

Merge assessment: Mergeable after the journal compaction issue is fixed (see inline comment). CI is green.

This review was generated by an AI assistant (Claude) on behalf of sebhofmann.

// 4. Re-open the writer on the new, compacted journal file
this.journalWriter = Files.newBufferedWriter(journalFile, StandardCharsets.UTF_8,
StandardOpenOption.CREATE, StandardOpenOption.APPEND);
openWriter();

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Compaction on startup can leave the journal writer closed for good

Both weaknesses existed before, but they matter now that every startup may compact and several processes are explicitly supported:

  1. The temp file has a fixed name (cache.journal.tmp). If two processes start at the same time and both compact, they write into the same file, and the first move can move a half-written file. The next startup then fails with a DeserializationException and clears the whole cache.
  2. compact calls this.close() before the move. If the move fails, e.g. with NoSuchFileException because the other process already moved the temp file, journalWriter stays null. compactOnStartup only logs a warning, every following append fails with "Journal is not open", and cacheUpdate now only logs that. The process writes nothing to the journal until the next restart, and after it those files are unmanaged on disk.

Suggestion: create the temp file with Files.createTempFile(journalFile.getParent(), journalFile.getFileName().toString(), ".tmp") and call openWriter() in a finally, so the writer is open again after a failure.

This review was generated by an AI assistant (Claude) on behalf of sebhofmann.

@Mewel
Mewel added this pull request to stack #3263 November 27, 2026 09:40
…ile trees after purge

- MCROCFLDefaultRemoteTemporaryStorage writes cache files to a temporary
  file first and moves them to their final path atomically, replacing an
  existing file with the same digest. A file that exists on disk but not
  in the cache index no longer fails the import with
  FileAlreadyExistsException, and concurrent writes of the same content
  do not conflict
- MCROCFLXMLMetadataManager.retrieveContent(MCRObjectID) uses
  getContent(), so MCRGZIPOCFLXMLMetadataManager decompresses the
  current version. The content is bound to the version that was checked,
  instead of resolving the head again when it is read
- MCROCFLRepository notifies listeners about purged objects. The
  MCROCFLVirtualObjectProvider removes all cached versions of a purged
  object and does not cache versions loaded during a purge, so a reused
  id is not read from the purged object. Purges by another process using
  the same repository are not noticed
- the read cache is no longer modified inside computeIfAbsent, and
  written objects are invalidated right after each persist
Remote temporary storage:
- Count the allocation only once for concurrent writes of the same
  content and for duplicate add events in the journal
- Pass the size to cacheUpdate instead of reading it from the cache
  file, which a concurrent eviction may already have deleted
- Keep a valid cache entry if appending it to the journal fails, only
  log the error
- exists() no longer adds missing digests to the LRU queue
- synchronizeCache only removes the cache entry of a missing file and
  no longer deletes files another thread or process may have written
- Delete stale temporary files of interrupted downloads on startup
- Document the shared use of the directory by several processes

Journal:
- Do not compact the journal on close anymore. Another process, like
  the web application while a CLI run ends, may still append to it
- Compact on startup instead, right after the replay, if the journal
  contains more than twice as many events as cache entries
- Reopen the journal writer if another process replaced or deleted
  the journal file, otherwise its entries would be lost

File system provider and repository:
- Remove the purge listener of the previous virtual object provider
  when init() is called again
- A failing purge listener no longer makes a successful purge look
  failed to the caller
- invalidateAll ignores non-derivate objects
@Mewel
Mewel force-pushed the issues/MCR-3942-ocfl-fixes branch from 433b6f2 to 1d170c0 Compare November 27, 2026 09:41
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

Successfully merging this pull request may close these issues.

2 participants