Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(no-rows-updated): fix no rows updated #12530

Merged
Merged
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 @@ -1031,7 +1031,17 @@
if (!upsertResults.isEmpty()) {
// commit upserts prior to retention or kafka send, if supported by impl
if (txContext != null) {
txContext.commitAndContinue();
try {
txContext.commitAndContinue();
} catch (EntityNotFoundException e) {
if (e.getMessage() != null
&& e.getMessage().contains("No rows updated")) {
log.debug("Ignoring no rows updated condition for metadata update", e);
MetricUtils.counter(EntityServiceImpl.class, "no_rows_updated").inc();
return TransactionResult.rollback();
}
throw e;

Check warning on line 1043 in metadata-io/src/main/java/com/linkedin/metadata/entity/EntityServiceImpl.java

View check run for this annotation

Codecov / codecov/patch

metadata-io/src/main/java/com/linkedin/metadata/entity/EntityServiceImpl.java#L1043

Added line #L1043 was not covered by tests
}
}

// Retention optimization and tx
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,21 @@
import static com.linkedin.metadata.Constants.CORP_USER_ENTITY_NAME;
import static com.linkedin.metadata.Constants.STATUS_ASPECT_NAME;
import static com.linkedin.metadata.entity.ebean.EbeanAspectDao.TX_ISOLATION;
import static org.mockito.ArgumentMatchers.any;
import static org.mockito.ArgumentMatchers.anyBoolean;
import static org.mockito.ArgumentMatchers.anyInt;
import static org.mockito.ArgumentMatchers.anyLong;
import static org.mockito.ArgumentMatchers.anyString;
import static org.mockito.ArgumentMatchers.eq;
import static org.mockito.Mockito.doAnswer;
import static org.mockito.Mockito.doNothing;
import static org.mockito.Mockito.doReturn;
import static org.mockito.Mockito.doThrow;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.spy;
import static org.mockito.Mockito.verify;
import static org.testng.Assert.assertEquals;
import static org.testng.Assert.assertFalse;
import static org.testng.Assert.assertNotNull;
import static org.testng.Assert.assertTrue;

Expand Down Expand Up @@ -41,6 +54,7 @@
import io.ebean.Database;
import io.ebean.Transaction;
import io.ebean.TxScope;
import jakarta.persistence.EntityNotFoundException;
import java.net.URISyntaxException;
import java.sql.Timestamp;
import java.time.Instant;
Expand All @@ -50,10 +64,11 @@
import java.util.Objects;
import java.util.Set;
import java.util.concurrent.LinkedBlockingQueue;
import java.util.concurrent.atomic.AtomicReference;
import java.util.function.Function;
import java.util.stream.Collectors;
import java.util.stream.IntStream;
import org.apache.commons.lang3.tuple.Triple;
import org.testng.Assert;
import org.testng.annotations.BeforeMethod;
import org.testng.annotations.Test;

Expand Down Expand Up @@ -112,14 +127,98 @@ public void setupTest() {
null);
}

/**
* Ideally, all tests would be in the base class, so they're reused between all implementations.
* When that's the case - test runner will ignore this class (and its base!) so we keep this dummy
* test to make sure this class will always be discovered.
*/
@Test
public void obligatoryTest() throws AssertionError {
Assert.assertTrue(true);
public void testNoRowsUpdatedErrorHandling() throws Exception {
// Setup test data
Urn entityUrn = UrnUtils.getUrn("urn:li:corpuser:testUser");
SystemMetadata systemMetadata = AspectGenerationUtils.createSystemMetadata();
CorpUserInfo writeAspect = AspectGenerationUtils.createCorpUserInfo("[email protected]");
String aspectName = PegasusUtils.getAspectNameFromSchema(writeAspect.schema());

// Create database and spy on aspectDao
Database server = EbeanTestUtils.createTestServer(EbeanEntityServiceTest.class.getSimpleName());
EbeanAspectDao aspectDao = spy(new EbeanAspectDao(server, EbeanConfiguration.testDefault));

// Prevent actual saves
doNothing().when(aspectDao).saveAspect(any(), any(), anyBoolean());
doReturn(0L)
.when(aspectDao)
.saveLatestAspect(
any(),
anyString(),
anyString(),
any(),
any(),
any(),
any(),
any(),
anyString(),
anyString(),
any(),
any(),
any(),
anyLong());

// Create spied transaction context that throws on commitAndContinue
AtomicReference<TransactionContext> capturedTxContext = new AtomicReference<>();
AtomicReference<TransactionResult<?>> capturedResult = new AtomicReference<>();

doAnswer(
invocation -> {
Function<TransactionContext, TransactionResult<?>> block = invocation.getArgument(0);
Integer maxTransactionRetry = invocation.getArgument(2);

TransactionContext txContext = spy(TransactionContext.empty(maxTransactionRetry));
capturedTxContext.set(txContext);

doThrow(new EntityNotFoundException("No rows updated"))
.when(txContext)
.commitAndContinue();

TransactionResult<?> result = block.apply(txContext);
capturedResult.set(result);
return result.getResults();
})
.when(aspectDao)
.runInTransactionWithRetry(any(), any(), anyInt());

// Create the service with our spied dao
PreProcessHooks preProcessHooks = new PreProcessHooks();
preProcessHooks.setUiEnabled(false);
EntityServiceImpl entityService =
new EntityServiceImpl(aspectDao, _mockProducer, false, preProcessHooks, true);

// Create the test batch
List<ChangeItemImpl> items =
List.of(
ChangeItemImpl.builder()
.urn(entityUrn)
.aspectName(aspectName)
.recordTemplate(writeAspect)
.systemMetadata(systemMetadata)
.auditStamp(TEST_AUDIT_STAMP)
.build(TestOperationContexts.emptyActiveUsersAspectRetriever(null)));

AspectsBatchImpl batch =
AspectsBatchImpl.builder()
.retrieverContext(opContext.getRetrieverContext())
.items(items)
.build();

// Execute the test
List<UpdateAspectResult> results = entityService.ingestAspects(opContext, batch, false, true);

// Verify results
assertEquals(results.size(), 0, "Expected no results for rolled back transaction");

// Verify transaction behavior
verify(aspectDao).runInTransactionWithRetry(any(), eq(batch), anyInt());
verify(capturedTxContext.get()).commitAndContinue();

// Verify the transaction result was a rollback
TransactionResult<?> result = capturedResult.get();
assertNotNull(result, "Expected a transaction result");
assertFalse(result.isCommitOrRollback(), "Expected a rollback result");
}

@Override
Expand Down
Loading