Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -1111,11 +1111,16 @@ private void updateLogicalTestSuite(LogicalSuiteRelationshipChange relationshipC
List.of(testSuite),
null,
null,
new EntityUpdateContext(
Map.of(testSuite.getId(), relationshipChange.relationshipRevision())));
revisionContext(testSuite.getId(), relationshipChange.relationshipRevision()));
RdfUpdater.updateEntity(testSuite);
}

private static EntityUpdateContext revisionContext(UUID entityId, Long revision) {
return revision == null
? EntityUpdateContext.empty()
: new EntityUpdateContext(Map.of(entityId, revision));
}

@Override
protected void deleteChildren(
List<CollectionDAO.EntityRelationshipRecord> children, boolean hardDelete, String updatedBy) {
Expand Down Expand Up @@ -1264,8 +1269,10 @@ public RestUtil.PutResponse<TestSuite> addAllTestCasesToLogicalTestSuite(
Lists.partition(relationshipChange.get().testCaseReferences(), 100)) {
Map<UUID, Long> batchRevisions = new HashMap<>();
for (EntityReference testCase : batch) {
batchRevisions.put(
testCase.getId(), relationshipChange.get().testCaseRevisions().get(testCase.getId()));
Long revision = relationshipChange.get().testCaseRevisions().get(testCase.getId());
if (revision != null) {
batchRevisions.put(testCase.getId(), revision);
}
}
postLogicalSuiteRelationshipUpdate(List.copyOf(batch), batchRevisions);
}
Expand Down Expand Up @@ -1347,7 +1354,7 @@ private List<TestCase> getLogicalSuiteUpdatedTestCase(List<EntityReference> test
return testCases;
}

private LogicalSuiteRelationshipChange prepareLogicalSuiteRelationshipChange(
LogicalSuiteRelationshipChange prepareLogicalSuiteRelationshipChange(
UUID testSuiteId, List<EntityReference> testCaseReferences) {
Comment on lines +1357 to 1358
if (nullOrEmpty(testCaseReferences)) {
return LogicalSuiteRelationshipChange.empty();
Expand All @@ -1366,19 +1373,39 @@ private LogicalSuiteRelationshipChange prepareLogicalSuiteRelationshipChange(
TestSuiteRepository.TESTS_REVISION_EXTENSION,
TestSuiteRepository.getTestsRevisionSchema());
Map<UUID, Long> testCaseRevisions = getTestSuiteRelationshipRevisions(testCaseIds);
if (testCaseRevisions.size() != testCaseIds.size()) {
throw new IllegalStateException("Failed to persist every test suite relationship revision");
}
Long relationshipRevision =
TestSuiteRepository.getTestsRelationshipRevisions(List.of(testSuiteId)).get(testSuiteId);
if (relationshipRevision == null) {
throw new IllegalStateException("Failed to persist the test suite tests revision");
}

logUnresolvedRevisions(testSuiteId, testCaseIds, testCaseRevisions, relationshipRevision);
return new LogicalSuiteRelationshipChange(
testSuiteId, testCaseReferences, testCaseRevisions, relationshipRevision);
}

/**
* The read-back above is a snapshot read issued inside the flush transaction, so under concurrent
* writers on the same revision rows it can return fewer rows than were just advanced — MySQL's
* default REPEATABLE READ makes that far likelier than Postgres' READ COMMITTED. Revisions only
* order concurrent search writes, and both index builders already re-read a revision they were not
* handed ({@link org.openmetadata.service.search.indexes.TestCaseIndex}, {@link
* org.openmetadata.service.search.indexes.TestSuiteIndex}), so an unresolved revision costs one
* extra read at index time. Failing the request instead would roll back membership rows that
* persisted correctly, which is how contention here used to surface as a 500.
*/
private void logUnresolvedRevisions(
UUID testSuiteId,
List<UUID> testCaseIds,
Map<UUID, Long> testCaseRevisions,
Long relationshipRevision) {
if (testCaseRevisions.size() != testCaseIds.size()) {
LOG.warn(
"Logical test suite {} advanced test case revisions that did not read back: {}",
testSuiteId,
testCaseIds.stream().filter(id -> !testCaseRevisions.containsKey(id)).toList());
}
Comment thread
mohityadav766 marked this conversation as resolved.
Resolved
if (relationshipRevision == null) {
LOG.warn("Logical test suite {} tests revision did not read back", testSuiteId);
}
}

public static Map<UUID, Long> getTestSuiteRelationshipRevisions(List<UUID> testCaseIds) {
if (nullOrEmpty(testCaseIds)) {
return Map.of();
Expand Down Expand Up @@ -1438,12 +1465,12 @@ private List<TestCase> postLogicalSuiteRelationshipUpdate(

private record TestSuiteRelationshipRevision(long revision) {}

private record LogicalSuiteRelationshipChange(
record LogicalSuiteRelationshipChange(
UUID testSuiteId,
List<EntityReference> testCaseReferences,
Map<UUID, Long> testCaseRevisions,
long relationshipRevision) {
private LogicalSuiteRelationshipChange {
Long relationshipRevision) {
LogicalSuiteRelationshipChange {
testCaseReferences = List.copyOf(testCaseReferences);
testCaseRevisions = Map.copyOf(testCaseRevisions);
}
Expand All @@ -1453,7 +1480,7 @@ private boolean isEmpty() {
}

private static LogicalSuiteRelationshipChange empty() {
return new LogicalSuiteRelationshipChange(null, List.of(), Map.of(), 0L);
return new LogicalSuiteRelationshipChange(null, List.of(), Map.of(), null);
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import static org.awaitility.Awaitility.await;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertNotNull;
import static org.junit.jupiter.api.Assertions.assertNull;
import static org.junit.jupiter.api.Assertions.assertTrue;
import static org.mockito.ArgumentMatchers.anyMap;
import static org.mockito.ArgumentMatchers.anyString;
Expand Down Expand Up @@ -66,6 +67,51 @@ void readsPersistedTestSuiteRelationshipRevisions() {
}
}

/**
* The revision read-back runs inside the flush transaction and can return fewer rows than were
* just advanced when other writers touch the same rows. Treating that as a persistence failure
* rolled back membership rows that had persisted correctly and answered the caller with a 500.
*/
@Test
void unresolvedRevisionsKeepTheMembershipChange() {
UUID testSuiteId = UUID.randomUUID();
List<EntityReference> testCases =
List.of(
entityReference(Entity.TEST_CASE, "readsBack"),
entityReference(Entity.TEST_CASE, "doesNotReadBack"));
UUID resolvedId = testCases.getFirst().getId();
List<String> requestedIds =
testCases.stream().map(EntityReference::getId).sorted().map(UUID::toString).toList();

CollectionDAO collectionDAO = mock(CollectionDAO.class);
CollectionDAO.EntityExtensionDAO extensionDAO = mock(CollectionDAO.EntityExtensionDAO.class);
when(collectionDAO.testCaseDAO()).thenReturn(mock(CollectionDAO.TestCaseDAO.class));
when(collectionDAO.entityExtensionDAO()).thenReturn(extensionDAO);
when(extensionDAO.getExtensionBatch(
requestedIds, TestCaseRepository.TEST_SUITES_REVISION_EXTENSION))
.thenReturn(
List.of(
new CollectionDAO.ExtensionRecordWithId(
resolvedId,
TestCaseRepository.TEST_SUITES_REVISION_EXTENSION,
"{\"revision\":4}")));
when(extensionDAO.getExtensionBatch(
List.of(testSuiteId.toString()), TestSuiteRepository.TESTS_REVISION_EXTENSION))
.thenReturn(List.of());

try (MockedStatic<Entity> entity = Mockito.mockStatic(Entity.class)) {
entity.when(Entity::getCollectionDAO).thenReturn(collectionDAO);
entity.when(() -> Entity.getEntityFields(TestCase.class)).thenCallRealMethod();

TestCaseRepository.LogicalSuiteRelationshipChange change =
new TestCaseRepository().prepareLogicalSuiteRelationshipChange(testSuiteId, testCases);

assertEquals(Map.of(resolvedId, 4L), change.testCaseRevisions());
assertNull(change.relationshipRevision(), "an unread suite revision must not gate indexing");
assertEquals(testCases, change.testCaseReferences());
}
}

@Test
void postUpdateHydratesTestSuitesBeforeLifecycleUpdate() {
CollectionDAO collectionDAO = mock(CollectionDAO.class);
Expand Down
Loading