-
Notifications
You must be signed in to change notification settings - Fork 97
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Make ID generation thread-safe (#1472)
Resolves #1473 While there is currently a lock for ID generation, it is not thread-safe as the start value of ID values can be modified by different threads. e.g. thread A starts generating IDs, thread B resets the ID generation start value, thread A repeats IDs. This PR: * Adds a test to verify consistent ID generation for SQL with multiple threads. * Changes the ID generator to use thread-local state. * Removes patching of state and replaces it with an explicit stack. * Assorted organizational / naming changes.
- Loading branch information
Showing
8 changed files
with
109 additions
and
36 deletions.
There are no files selected for viewing
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
kind: Fixes | ||
body: Make ID generation thread-safe | ||
time: 2024-10-21T12:07:48.313324-07:00 | ||
custom: | ||
Author: plypaul | ||
Issue: "1473" |
This file contains 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
This file contains 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
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,7 +1,7 @@ | ||
# These imports are required to properly set up pytest fixtures. | ||
from __future__ import annotations | ||
|
||
from metricflow_semantics.test_helpers.id_helpers import patch_id_generators # noqa: F401 | ||
from metricflow_semantics.test_helpers.id_helpers import setup_id_generators # noqa: F401 | ||
|
||
from tests_metricflow_semantics.fixtures.manifest_fixtures import * # noqa: F401, F403 | ||
from tests_metricflow_semantics.fixtures.setup_fixtures import * # noqa: F401, F403 |
This file contains 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
Empty file.
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
from __future__ import annotations | ||
|
||
from concurrent.futures import Future, ThreadPoolExecutor | ||
from typing import Mapping, Sequence | ||
|
||
from metricflow.engine.metricflow_engine import MetricFlowEngine, MetricFlowExplainResult, MetricFlowQueryRequest | ||
from tests_metricflow.fixtures.manifest_fixtures import MetricFlowEngineTestFixture, SemanticManifestSetup | ||
|
||
|
||
def _explain_one_query(mf_engine: MetricFlowEngine) -> str: | ||
explain_result: MetricFlowExplainResult = mf_engine.explain( | ||
MetricFlowQueryRequest.create_with_random_request_id(saved_query_name="p0_booking") | ||
) | ||
return explain_result.rendered_sql.sql_query | ||
|
||
|
||
def test_concurrent_explain_consistency( | ||
mf_engine_test_fixture_mapping: Mapping[SemanticManifestSetup, MetricFlowEngineTestFixture] | ||
) -> None: | ||
"""Tests that concurrent requests for the same query generate the same SQL. | ||
Prior to consistency fixes for ID generation, this test would fail due to issues with sequentially numbered aliases. | ||
""" | ||
mf_engine = mf_engine_test_fixture_mapping[SemanticManifestSetup.SIMPLE_MANIFEST].metricflow_engine | ||
|
||
request_count = 4 | ||
with ThreadPoolExecutor(max_workers=2) as executor: | ||
futures: Sequence[Future] = [executor.submit(_explain_one_query, mf_engine) for _ in range(request_count)] | ||
results = [future.result() for future in futures] | ||
for result in results: | ||
assert result == results[0], "Expected only one unique result / results to be the same" |
This file contains 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