Skip to content

memcached: trust a seen miss marker briefly instead of re-reading it - #108

Merged
antirotor merged 4 commits into
enhancement/featuriing-memcachedfrom
fix/memcached-known-miss-window
Nov 5, 2026
Merged

antirotor merged 4 commits into
enhancement/featuriing-memcachedfrom
fix/memcached-known-miss-window

Conversation

@antirotor

@antirotor antirotor commented Nov 4, 2026 •

Copy link
Copy Markdown
Member

Description

A stage with dangling references read memcached for every lookup of every missing URI. This PR makes a process remember a miss marker it has seen for a few seconds.

Follow-up to #104. #104, #105, #106, #110 and #112 are merged, and this branch now contains them. The one conflict was in batchWarm: observedAt moved in front of the getMany call.

Changelog

Miss markers stay out of the in-process caches so that a publish is seen. As a result, each lookup of a dangling URI read memcached again, and one Resolve looks a URI up more than once.

  • Trust window: a process trusts a marker it saw for up to 5 seconds (kKnownMissWindowSeconds), counted from when it read the marker. This covers both getAsset and batchWarm.
  • ClearCache() and DeleteFromCache() end the window. A lookup that read the marker before they finished cannot restore it: lookups take a generation from the new KnownMisses before reading, and invalidation deletes the marker first and advances the generation after.
  • A value found by add is not trusted: addMissMarker now reports Stored, Exists or Failed. Exists may be a path another process just stored, so it is read again instead of being treated as a miss.

Behaviour change: a process that has just seen a marker can miss a publish for up to 5 seconds, even if another process has already deleted the marker or the marker has expired. This affects only that process's view, not what memcached holds. The README states this. Capping the window at the marker's real expiry would take an extra read per marker, which this PR exists to avoid.

Linked issues/branches

Type of Change

  • Bug fix (non-breaking change which fixes an issue)

Additional info

Measured with the integration harness from #107 against a live AYON server, standalone OpenUSD 25.05 and memcached. It is a warm stage with 300 dangling references, median of 5, with the round trip simulated by a proxy.

Build get commands Open at 0 ms Open at 5 ms
#104 901 106.8 ms 4975.8 ms
#104 + this PR 301 50.7 ms 1665.0 ms
#105 603 82.0 ms 3343.7 ms
#106 + this PR (includes #105) 3 8.3 ms 25.2 ms

Dragonfly results are within a few ms of these. Warm and cold open times for a stage without dangling references did not change.

Testing

…g it

Miss markers stay out of the in-process caches so that a publish is seen, so
every lookup of a dangling URI read memcached again. One Resolve looks a URI up
more than once, which made 300 dangling references cost 901 gets on a warm
stage, about 5 s at a 5 ms round trip.

A process now trusts a marker it saw for up to 5 seconds, never longer than the
marker's TTL. ClearCache and DeleteFromCache end that immediately. When `add`
finds an existing value it may be a path another process just stored, so that
value is read again instead of being trusted as a miss.

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

🟡 Changes recommended

Miss-marker handling can reinstate stale misses after explicit invalidation and extend local miss windows beyond the marker’s expiry.

2 open issues
What changed in this PR

This PR reduces repeated memcached reads for unresolved AYON URIs by briefly remembering observed miss markers. The diff also includes the configurable-expiry and cache-invalidation changes from #104.

Changes:

  • Add a short local miss-trust window to single and batch lookups.
  • Distinguish stored markers from existing values to avoid trusting a concurrently published path as a miss.
  • Include configurable expiry, targeted invalidation, documentation, and helper tests.
File Description
tests/​memcachedExpiryTest.cpp Tests URI classification, expiry settings, and trust-window calculations.
src/​AyonUsdResolver/​config.h Defines expiry environment-variable names.
src/​AyonUsdResolver/​cache/​resolverContextCache.h Declares expiry settings and local miss tracking.
src/​AyonUsdResolver/​cache/​resolverContextCache.cpp Implements miss tracking, expiry selection, and targeted invalidation.
src/​AyonUsdResolver/​cache/​memcachedHandler.h Defines marker-write outcomes and updates cache APIs.
src/​AyonUsdResolver/​cache/​memcachedHandler.cpp Handles empty markers, conditional writes, and key-length limits.
src/​AyonUsdResolver/​cache/​memcachedExpiry.h Adds classification, expiry, and trust-window helpers.
README.md Documents expiry controls, invalidation, and delayed publish visibility.

Review effort: Balanced

Comment on lines +766 to +769
const int64_t window
= trust ? MemcachedExpiry::knownMissWindowFor(MemcachedExpiry::missTtlFor(_MemcachedTtl(uri), m_memcachedMissExpire))
: 0;
const auto until = std::chrono::steady_clock::now() + std::chrono::seconds(window);

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

Correct, and the documentation was wrong: it said the trust is never longer than the marker's TTL, but the window starts when the marker is read, so it can end up to 5 seconds after the marker expired. Fixed the wording in the README and memcachedExpiry.h in 212d650.

The code stays as it is on purpose. The overrun is the same 5 seconds the README already accepts when another process deletes a marker. Knowing the real expiry would take an extra read per marker or a timestamp in the item flags, and not trusting read markers would undo this PR (300 dangling references: 901 gets instead of 301).

Comment on lines +770 to +771
std::lock_guard<std::mutex> lock(m_missKeysMutex);
m_missKeys[uri] = until;

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

Correct, fixed in 212d650. Lookups now take a generation from KnownMisses before reading, and an older observation is kept for ClearCache but not trusted. The same applies to _StoreMiss.

A generation alone would not have been enough, because a lookup could take the new generation and still read a marker not yet deleted. So invalidation now deletes the marker first and advances the generation after. Reproduced with the read of a marker held in a proxy while the invalidation starts: 16 of 16 runs failed before the change, 16 of 16 pass with it.

A lookup that read a marker could record it after ClearCache or
DeleteFromCache had finished, which hid the URI for up to 5 seconds after
an explicit invalidation. ClearCache also emptied the list before deleting
the markers, so with many markers this window lasted seconds.

Lookups now take a generation before reading memcached. Invalidation deletes
the marker first and then advances the generation, and an older observation
is remembered for ClearCache but not trusted.

Also correct the documented limit: the 5 seconds count from when the marker
was read, so the trust can end up to 5 seconds after the marker expired.
@DexterSchenk

Copy link
Copy Markdown
Contributor

Heads-up: since 212d650 (and the base merge 5caf3bc), this PR conflicts with #110 in batchWarm(). This PR edits the per-key get loop that #105/#110 replace with getMany. We resolved it in our fork by keeping the mget loop and reading m_knownMisses.generation() once, just before getMany, then passing it to _TrackMiss(uriPath, observedAt) for each marker it returns:

// Taken before the markers are read, so an invalidation during the mget is not undone (see KnownMisses).
const uint64_t observedAt = m_knownMisses.generation();
std::unordered_map<std::string, std::string> cachedValues;
{
    std::lock_guard<std::mutex> lock(s_memcachedMutex);
    cachedValues = m_memcached->get()->getMany(eligible);
}

With #108, #109, #110 and #106 merged on the base in your order, the build passes our smoke tests on Houdini 21.0/22.0, Maya 2026 and standalone USD. Our behaviour probe also picked up the documented trade-off: after a miss, a publish is seen in the same process only once the 5 s window has passed.

@antirotor
antirotor merged commit 86ead5f into enhancement/featuriing-memcached Nov 5, 2026
1 check passed
@antirotor
antirotor deleted the fix/memcached-known-miss-window branch November 5, 2026 15:26
antirotor added a commit that referenced this pull request Nov 5, 2026
Add the scenarios that fail on builds without #108, #109 and #110, now
that those are merged:

- miss markers: trust window, ClearCache and DeleteFromCache ending it,
  an invalidation during a read, a path stored during the lookup
- malformed AYON answers write no marker; an empty answer still does
- dangling references are read once, and their open time
- one dead server keeps the hits from the others

The proxy can now hold the reads of one key, and the probe can open a
stage in the background, to keep the races open.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

type: bug Something isn't working

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants