Repository navigation
fix: make lazily populated runtime caches thread-safe - #1470
Draft
myronmarston wants to merge 1 commit into
Draft
myronmarston wants to merge 1 commit into
myronmarston wants to merge 1 commit into
Conversation
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
Collaborator
Author
|
This change is part of the following stack: Change managed by git-spice. |
This branch has not been deployed
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Stacked on #1469.
On JRuby, concurrent writes to a plain
Hashcan 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
Monitor, not aMutex.Schema#type_frombuilds aType, and that build callstype_fromfor the wrapped type while the lock is held.nilandfalseare cached:fetchcheckskey?, so a cached falsy value doesn't trigger a rebuild, as it would with||=.JSONSchema::ValidatorFactory#validator_for(#1469)JSONIngestion::EnvelopeValidatorvalidator factories (#1469)JSONIngestion::RecordPreparerFactory#for_json_schema_versionSchemaArtifacts::FromDisk#json_schemas_forRuntimeMetadata::ExtensionLoader#loadIndexDefinition::Base#list_counts_field_paths_for_sourceGraphQL::Schema#type_fromGraphQL::Schema::Type#enum_value_namedRecordPreparerFactoryused 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.rbruns a deterministic race. The test holds thread 1 inside the block until thread 2 is asleep, then counts block calls.||=. Two examples failed: "evaluates the block only once when another thread requests the same key..." (expected: 1, got: 2), and "cachesnilandfalsevalues" (expected: 2, got: 4).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
Monitorfor each cache it reads.🤖 Generated with Claude Code