SEARCH-2782 commit time as event time (#377)

This commit is contained in:
Davide
2021-04-20 16:32:41 +02:00
committed by GitHub
parent 8a6a76d191
commit 9d711213cc
9 changed files with 246 additions and 95 deletions

View File

@@ -92,12 +92,10 @@ public class NodeDAOTest extends TestCase
public void testTransaction() throws Throwable
{
final boolean[] newTxn = new boolean[] {false};
RetryingTransactionCallback<Long> getTxnIdCallback = new RetryingTransactionCallback<Long>()
{
public Long execute() throws Throwable
{
return nodeDAO.getCurrentTransactionId(newTxn[0]);
}
RetryingTransactionCallback<Pair<Long, Long>> getTxnIdCallback = () -> {
Long currentTransactionId = nodeDAO.getCurrentTransactionId(newTxn[0]);
Long currentTransactionCommitTime = nodeDAO.getCurrentTransactionCommitTime();
return new Pair<>(currentTransactionId, currentTransactionCommitTime);
};
// No txn
try
@@ -110,14 +108,24 @@ public class NodeDAOTest extends TestCase
// Expected
}
// Read-only
assertNull("No Txn ID should be present in read-only txn", txnHelper.doInTransaction(getTxnIdCallback, true));
Pair<Long, Long> txn0 = txnHelper.doInTransaction(getTxnIdCallback);
Long txnId0 = txn0.getFirst();
Long commitTime0 = txn0.getSecond();
assertNull("No Txn ID should be present in read-only txn", txnId0);
assertNull("No Txn Commit time should be present in read-only txn", commitTime0);
// First success
Long txnId1 = txnHelper.doInTransaction(getTxnIdCallback);
Pair<Long, Long> txn1 = txnHelper.doInTransaction(getTxnIdCallback);
Long txnId1 = txn1.getFirst();
Long commitTime1 = txn1.getSecond();
assertNull("No Txn ID should be present in untouched txn", txnId1);
assertNull("No Txn Commit time should be present in untouched txn", commitTime1);
// Second success
newTxn[0] = true;
Long txnId2 = txnHelper.doInTransaction(getTxnIdCallback);
Pair<Long, Long> txn2 = txnHelper.doInTransaction(getTxnIdCallback);
Long txnId2 = txn2.getFirst();
Long commitTime2 = txn2.getSecond();
assertNotNull("Txn ID should be present by forcing it", txnId2);
assertNotNull("Txn commit time should be present by forcing it", commitTime2);
}
public void testSelectNodePropertiesByTypes() throws Exception

View File

@@ -26,9 +26,14 @@
package org.alfresco.repo.event2;
import java.time.Instant;
import java.time.ZoneOffset;
import java.time.ZonedDateTime;
import java.util.List;
import org.alfresco.model.ContentModel;
import org.alfresco.repo.domain.node.NodeDAO;
import org.alfresco.repo.domain.node.Transaction;
import org.alfresco.repo.event.v1.model.EventData;
import org.alfresco.repo.event.v1.model.EventType;
import org.alfresco.repo.event.v1.model.NodeResource;
@@ -38,6 +43,7 @@ import org.alfresco.service.namespace.QName;
import org.alfresco.util.GUID;
import org.alfresco.util.PropertyMap;
import org.junit.Test;
import org.springframework.beans.factory.annotation.Autowired;
/**
* @author Iulian Aftene
@@ -45,6 +51,9 @@ import org.junit.Test;
public class CreateRepoEventIT extends AbstractContextAwareRepoEvent
{
@Autowired
private NodeDAO nodeDAO;
@Test
public void testCreateEvent()
{
@@ -149,9 +158,32 @@ public class CreateRepoEventIT extends AbstractContextAwareRepoEvent
assertTrue("isFile flag should be TRUE for nodeType=cm:content. ", resource.isFile());
assertFalse("isFolder flag should be FALSE for nodeType=cm:content. ", resource.isFolder());
}
@Test
public void testEventTimestampEqualsToTransactionCommitTime()
{
String name = "TestFile-" + System.currentTimeMillis() + ".txt";
PropertyMap propertyMap = new PropertyMap();
propertyMap.put(ContentModel.PROP_NAME, name);
//create a node and return the transaction id required later
Long transactionId = retryingTransactionHelper.doInTransaction(() -> {
nodeService.createNode(rootNodeRef, ContentModel.ASSOC_CHILDREN,
QName.createQName(TEST_NAMESPACE, GUID.generate()), ContentModel.TYPE_CONTENT, propertyMap).getChildRef();
return nodeDAO.getCurrentTransactionId(false);
});
RepoEvent<EventData<NodeResource>> resultRepoEvent = getRepoEvent(1);
Transaction transaction = nodeDAO.getTxnById(transactionId);
Instant commitTimeMs = Instant.ofEpochMilli(transaction.getCommitTimeMs());
ZonedDateTime timestamp = ZonedDateTime.ofInstant(commitTimeMs, ZoneOffset.UTC);
assertEquals(timestamp, resultRepoEvent.getTime());
}
@Test
public void testCteateMultipleNodesInTheSameTransaction()
public void testCreateMultipleNodesInTheSameTransaction()
{
retryingTransactionHelper.doInTransaction(() -> {
for (int i = 0; i < 3; i++)

View File

@@ -31,8 +31,16 @@ import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.concurrent.CountDownLatch;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.atomic.AtomicBoolean;
import javax.transaction.HeuristicMixedException;
import javax.transaction.HeuristicRollbackException;
import javax.transaction.NotSupportedException;
import javax.transaction.RollbackException;
import javax.transaction.Status;
import javax.transaction.SystemException;
import javax.transaction.UserTransaction;
import junit.framework.TestCase;
@@ -54,6 +62,7 @@ import org.alfresco.repo.security.permissions.SimpleAccessControlEntry;
import org.alfresco.repo.security.permissions.SimpleAccessControlListProperties;
import org.alfresco.repo.transaction.AlfrescoTransactionSupport;
import org.alfresco.repo.transaction.AlfrescoTransactionSupport.TxnReadState;
import org.alfresco.repo.transaction.TransactionListener;
import org.alfresco.service.ServiceRegistry;
import org.alfresco.service.cmr.dictionary.DictionaryService;
import org.alfresco.service.cmr.repository.NodeRef;
@@ -71,9 +80,14 @@ import org.alfresco.test_category.OwnJVMTestsCategory;
import org.alfresco.util.ApplicationContextHelper;
import org.alfresco.util.EqualsHelper;
import org.alfresco.util.testing.category.DBTests;
import org.alfresco.util.transaction.TransactionListenerAdapter;
import org.awaitility.Awaitility;
import org.junit.experimental.categories.Category;
import org.springframework.context.ApplicationContext;
import static org.awaitility.Awaitility.await;
import static org.hamcrest.CoreMatchers.equalTo;
@Category({OwnJVMTestsCategory.class, DBTests.class})
public class AclDaoComponentTest extends TestCase
{
@@ -244,6 +258,37 @@ public class AclDaoComponentTest extends TestCase
assertEquals(aclProps.getInherits(), Boolean.TRUE);
assertEquals(aclDaoComponent.getAccessControlListProperties(aclProps.getId()), aclProps);
}
public void testGetCurrentACLChangeSet()
throws HeuristicRollbackException, RollbackException, HeuristicMixedException, SystemException
{
SimpleAccessControlListProperties properties = new SimpleAccessControlListProperties();
properties.setAclType(ACLType.DEFINING);
properties.setVersioned(true);
Long id = aclDaoComponent.createAccessControlList(properties).getId();
AccessControlListProperties aclProps = aclDaoComponent.getAccessControlListProperties(id);
assertEquals(aclProps.getAclType(), ACLType.DEFINING);
assertEquals(aclProps.getAclVersion(), Long.valueOf(1l));
assertEquals(aclProps.getInherits(), Boolean.TRUE);
AtomicBoolean afterCommit = new AtomicBoolean();
AlfrescoTransactionSupport.bindListener(new TransactionListenerAdapter() {
@Override
public void afterCommit()
{
//The commit time is available only after a transaction is committed
assertNotNull(aclDaoComponent.getCurrentChangeSetCommitTime());
afterCommit.set(true);
}
});
testTX.commit();
await("Commit time not null")
.atMost(3, TimeUnit.SECONDS)
.untilAtomic(afterCommit, equalTo(true));
}
public void testCreateShared()
{