Skip to content

fix: make lazily populated runtime caches thread-safe - #1470

Draft
myronmarston wants to merge 1 commit into
joshuaw/threadsafe-validator-cachesfrom
myron/threadsafe-runtime-caches
Draft

myronmarston wants to merge 1 commit into
joshuaw/threadsafe-validator-cachesfrom
myron/threadsafe-runtime-caches

Conversation

@myronmarston

@myronmarston myronmarston commented Nov 24, 2026 •

Copy link
Copy Markdown
Collaborator

Stacked on #1469.

On JRuby, concurrent writes to a plain Hash can lose entries. #1469 guards 2 lazily populated caches with a mutex. 7 more caches have the same race: each lives on an object built once per process and gets entries while events are indexed or GraphQL requests run. This PR adds one shared cache type and uses it for all 9.

Summary

# elasticgraph-support: Support::ThreadSafeCache
def fetch_or_store(key)
  @monitor.synchronize do
    @values_by_key.fetch(key) { @values_by_key[key] = yield }
  end
end
  • Reentrant lock: a Monitor, not a Mutex. Schema#type_from builds a Type, and that build calls type_from for the wrapped type while the lock is held.
  • nil and false are cached: fetch checks key?, so a cached falsy value doesn't trigger a rebuild, as it would with ||=.
Cache Path
JSONSchema::ValidatorFactory#validator_for (#1469) indexing
JSONIngestion::EnvelopeValidator validator factories (#1469) indexing
JSONIngestion::RecordPreparerFactory#for_json_schema_version indexing
SchemaArtifacts::FromDisk#json_schemas_for indexing
RuntimeMetadata::ExtensionLoader#load indexing
IndexDefinition::Base#list_counts_field_paths_for_source indexing
GraphQL::Schema#type_from GraphQL
GraphQL::Schema::Type#enum_value_named GraphQL

RecordPreparerFactory used to load each scalar type's indexing preparer lazily. Now it builds that map in its constructor, because the scalar types are known then. A misconfigured preparer therefore fails at boot, not on the first event that uses it.

Evidence

thread_safe_cache_spec.rb runs a deterministic race. The test holds thread 1 inside the block until thread 2 is asleep, then counts block calls.

  • Before: the stub had no lock and used ||=. Two examples failed: "evaluates the block only once when another thread requests the same key..." (expected: 1, got: 2), and "caches nil and false values" (expected: 2, got: 4).
  • After: all examples pass, including the test where the block fetches a different key from the same cache.

Per-gem suites (support, schema_artifacts, json_ingestion, datastore_core, graphql, indexer) pass with 100% line and branch coverage. Steep is clean.

Merge Danger

Door: two-way

Blast Radius: small. Every indexed event and every GraphQL request now acquires an uncontended Monitor for each cache it reads.

🤖 Generated with Claude Code

On JRuby, concurrent writes to a plain `Hash` can lose entries. These caches live on long-lived objects and get filled while events are indexed or GraphQL requests run.

- Add `Support::ThreadSafeCache`, a lazily populated cache guarded by a reentrant `Monitor`.
- Use it for the JSON schema validator caches (`ValidatorFactory`, `EnvelopeValidator`), the JSON schemas by version in `SchemaArtifacts::FromDisk`, the record preparers by JSON schema version, `ExtensionLoader`, `IndexDefinition::Base#list_counts_field_paths_for_source`, and the GraphQL `Schema` type and `Type` enum value caches.
- Build `RecordPreparerFactory`'s scalar indexing preparer map eagerly, since the scalar types are known up front.

Generated with Claude Code
@myronmarston

Copy link
Copy Markdown
Collaborator Author

This change is part of the following stack:

Change managed by git-spice.

This branch has not been deployed

No deployments
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.

1 participant