You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@superset.apache.org by mi...@apache.org on 2026/10/08 11:48:22 UTC
(superset) branch 6.2 updated (3d1bd4aa696 -> b87a79e0efb)
This is an automated email from the ASF dual-hosted git repository.
michael-s-molina pushed a change to branch 6.2
in repository https://gitbox.apache.org/repos/asf/superset.git
discard 3d1bd4aa696 fix(permalink): handle concurrent creation of identical dashboard permalinks (#45059)
new b87a79e0efb fix(permalink): handle concurrent creation of identical dashboard permalinks (#45059)
This update added new revisions after undoing existing revisions.
That is to say, some revisions that were in the old version of the
branch are not in the new version. This situation occurs
when a user --force pushes a change and generates a repository
containing something like this:
* -- * -- B -- O -- O -- O (3d1bd4aa696)
\
N -- N -- N refs/heads/6.2 (b87a79e0efb)
You should already have received notification emails for all of the O
revisions, and so the following emails describe only the N revisions
from the common base, B.
Any revisions marked "omit" are not gone; other references still
refer to them. Any revisions marked "discard" are gone forever.
The 1 revisions listed above as "new" are entirely new to this
repository and will be described in separate emails. The revisions
listed as "add" were already present in the repository and have only
been added to this reference.
Summary of changes:
superset/daos/key_value.py | 9 ++++++++-
1 file changed, 8 insertions(+), 1 deletion(-)
(superset) 01/01: fix(permalink): handle concurrent creation of identical dashboard permalinks (#45059)
Posted by mi...@apache.org.
This is an automated email from the ASF dual-hosted git repository.
michael-s-molina pushed a commit to branch 6.2
in repository https://gitbox.apache.org/repos/asf/superset.git
commit b87a79e0efbb018f51b2cfbf16c0fbdc36b0adf6
Author: Michael S. Molina <70...@users.noreply.github.com>
AuthorDate: Wed Oct 7 15:37:11 2026 -0300
fix(permalink): handle concurrent creation of identical dashboard permalinks (#45059)
Co-authored-by: Claude Sonnet 5.5 <no...@anthropic.com>
(cherry picked from commit ae297e910e785bc9da143d14cb8028c75923777b)
---
superset/commands/dashboard/permalink/create.py | 36 ++++--
superset/daos/key_value.py | 9 +-
.../commands/dashboard/permalink_create_test.py | 129 +++++++++++++++++++++
3 files changed, 164 insertions(+), 10 deletions(-)
diff --git a/superset/commands/dashboard/permalink/create.py b/superset/commands/dashboard/permalink/create.py
index 055cd7a9972..d8dafd8d90b 100644
--- a/superset/commands/dashboard/permalink/create.py
+++ b/superset/commands/dashboard/permalink/create.py
@@ -17,7 +17,7 @@
import logging
from functools import partial
-from sqlalchemy.exc import SQLAlchemyError
+from sqlalchemy.exc import IntegrityError, SQLAlchemyError
from superset import db
from superset.commands.dashboard.permalink.base import BaseDashboardPermalinkCommand
@@ -97,14 +97,32 @@ class CreateDashboardPermalinkCommand(BaseDashboardPermalinkCommand):
assert entry.id # for type checks
return encode_permalink_key(key=entry.id, salt=self.salt)
- # Create new entry with current algorithm
- entry = KeyValueDAO.create_entry(
- resource=self.resource,
- key=uuid_key,
- value=value,
- codec=self.codec,
- )
- db.session.flush()
+ # Create new entry with current algorithm.
+ #
+ # The uuid is deterministic, so concurrent identical requests (same user,
+ # dashboard and state) all try to insert the same uuid. The lookup above and
+ # this insert are not atomic, so the unique index is the ultimate arbiter:
+ # the insert runs inside a SAVEPOINT and, if a concurrent request won the
+ # race, we join the winner's entry instead of failing the request.
+ try:
+ with db.session.begin_nested():
+ entry = KeyValueDAO.create_entry(
+ resource=self.resource,
+ key=uuid_key,
+ value=value,
+ codec=self.codec,
+ )
+ db.session.flush()
+ except IntegrityError:
+ # The SAVEPOINT is rolled back and the session is still usable. The
+ # winner's row is committed by now, so re-read it. Use a locking read:
+ # under REPEATABLE READ (e.g. MySQL's default) a plain SELECT keeps
+ # using the snapshot taken by the lookup above and would not see the
+ # row the winner just committed. If nothing is found, this was not the
+ # expected duplicate, so re-raise.
+ entry = KeyValueDAO.get_entry(self.resource, uuid_key, for_update=True)
+ if entry is None:
+ raise
assert entry.id # for type checks
return encode_permalink_key(key=entry.id, salt=self.salt)
diff --git a/superset/daos/key_value.py b/superset/daos/key_value.py
index f15293abcab..a7b690f0e78 100644
--- a/superset/daos/key_value.py
+++ b/superset/daos/key_value.py
@@ -42,9 +42,16 @@ class KeyValueDAO(BaseDAO[KeyValueEntry]):
def get_entry(
resource: KeyValueResource,
key: Key,
+ for_update: bool = False,
) -> KeyValueEntry | None:
filter_ = get_filter(resource, key)
- return db.session.query(KeyValueEntry).filter_by(**filter_).first()
+ query = db.session.query(KeyValueEntry).filter_by(**filter_)
+ if for_update:
+ # Row-lock the entry for the rest of the transaction. A locking read
+ # also returns the latest committed row under REPEATABLE READ, where a
+ # plain SELECT would keep using the transaction's earlier snapshot.
+ query = query.with_for_update()
+ return query.first()
@classmethod
def get_value(
diff --git a/tests/unit_tests/commands/dashboard/permalink_create_test.py b/tests/unit_tests/commands/dashboard/permalink_create_test.py
new file mode 100644
index 00000000000..8c6a1f75d55
--- /dev/null
+++ b/tests/unit_tests/commands/dashboard/permalink_create_test.py
@@ -0,0 +1,129 @@
+# Licensed to the Apache Software Foundation (ASF) under one
+# or more contributor license agreements. See the NOTICE file
+# distributed with this work for additional information
+# regarding copyright ownership. The ASF licenses this file
+# to you under the Apache License, Version 2.0 (the
+# "License"); you may not use this file except in compliance
+# with the License. You may obtain a copy of the License at
+#
+# http://www.apache.org/licenses/LICENSE-2.0
+#
+# Unless required by applicable law or agreed to in writing,
+# software distributed under the License is distributed on an
+# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+# KIND, either express or implied. See the License for the
+# specific language governing permissions and limitations
+# under the License.
+from collections.abc import Iterator
+from contextlib import nullcontext
+from typing import Any
+from unittest.mock import MagicMock, patch, PropertyMock
+from uuid import UUID
+
+import pytest
+from sqlalchemy.exc import IntegrityError
+
+from superset.commands.dashboard.permalink.create import CreateDashboardPermalinkCommand
+
+MODULE = "superset.commands.dashboard.permalink.create"
+UUID_KEY = UUID("11111111-1111-1111-1111-111111111111")
+
+
+def _entry(entry_id: int) -> MagicMock:
+ entry = MagicMock()
+ entry.id = entry_id
+ return entry
+
+
+def _duplicate_key_error() -> IntegrityError:
+ return IntegrityError(
+ "INSERT INTO key_value",
+ {},
+ Exception("Duplicate entry for key 'key_value.ix_key_value_uuid'"),
+ )
+
+
+@pytest.fixture
+def mocks() -> Iterator[dict[str, Any]]:
+ """
+ Patch everything the command touches so ``run`` can be exercised without a
+ database. ``db.session.begin_nested()`` is a plain context manager that does not
+ swallow exceptions, like a real SAVEPOINT.
+ """
+ with (
+ patch(f"{MODULE}.db") as db,
+ patch(f"{MODULE}.KeyValueDAO") as key_value_dao,
+ patch(f"{MODULE}.DashboardDAO") as dashboard_dao,
+ patch(f"{MODULE}.get_user_id", return_value=1),
+ patch(f"{MODULE}.get_deterministic_uuid", return_value=UUID_KEY),
+ patch(f"{MODULE}.get_fallback_algorithms", return_value=[]),
+ patch(
+ f"{MODULE}.encode_permalink_key",
+ side_effect=lambda key, salt: f"permalink-{key}",
+ ),
+ patch.object(
+ CreateDashboardPermalinkCommand,
+ "salt",
+ new_callable=PropertyMock,
+ return_value="salt",
+ ),
+ ):
+ db.session.begin_nested.side_effect = lambda: nullcontext()
+ dashboard_dao.get_by_id_or_slug.return_value = MagicMock(uuid=UUID_KEY)
+ yield {"db": db, "dao": key_value_dao}
+
+
+def _run() -> str:
+ command = CreateDashboardPermalinkCommand(dashboard_id="1", state={})
+ # ``run`` is wrapped by ``@transaction``, which needs an app context and a real
+ # session; call the undecorated function to test the command's own logic.
+ return CreateDashboardPermalinkCommand.run.__wrapped__(command)
+
+
+def test_create_permalink_creates_new_entry(mocks: dict[str, Any]) -> None:
+ mocks["dao"].get_entry.return_value = None
+ mocks["dao"].create_entry.return_value = _entry(7)
+
+ assert _run() == "permalink-7"
+ mocks["dao"].create_entry.assert_called_once()
+ mocks["db"].session.flush.assert_called_once()
+
+
+def test_create_permalink_returns_existing_entry(mocks: dict[str, Any]) -> None:
+ mocks["dao"].get_entry.return_value = _entry(3)
+
+ assert _run() == "permalink-3"
+ mocks["dao"].create_entry.assert_not_called()
+
+
+def test_create_permalink_joins_concurrent_winner(mocks: dict[str, Any]) -> None:
+ """
+ Concurrent identical requests derive the same uuid. When the lookup misses but
+ another request inserts the entry first, the loser hits the unique index. It must
+ return the winner's permalink instead of failing.
+ """
+ winner = _entry(5)
+ mocks["dao"].get_entry.side_effect = [None, winner]
+ mocks["dao"].create_entry.return_value = _entry(99)
+ mocks["db"].session.flush.side_effect = _duplicate_key_error()
+
+ assert _run() == "permalink-5"
+ assert mocks["dao"].get_entry.call_count == 2
+ # The re-read must be a locking read so that it sees the winner's committed row
+ # even when the transaction runs under REPEATABLE READ.
+ assert mocks["dao"].get_entry.call_args.kwargs == {"for_update": True}
+
+
+def test_create_permalink_reraises_unexpected_integrity_error(
+ mocks: dict[str, Any],
+) -> None:
+ """
+ If the entry cannot be found after a unique violation, the error was not the
+ expected duplicate and must not be masked.
+ """
+ mocks["dao"].get_entry.return_value = None
+ mocks["dao"].create_entry.return_value = _entry(99)
+ mocks["db"].session.flush.side_effect = _duplicate_key_error()
+
+ with pytest.raises(IntegrityError):
+ _run()