use of org.opencastproject.scheduler.api.SchedulerTransactionLockException in project opencast by opencast.
the class SchedulerServiceImpl method addMultipleEventInternal.
private Map<String, Period> addMultipleEventInternal(List<Period> periods, String captureAgentId, Set<String> userIds, MediaPackage templateMp, Map<String, String> wfProperties, Map<String, String> caMetadata, String modificationOrigin, Opt<Boolean> optOutStatus, Opt<String> schedulingSource, Opt<String> trxId) throws SchedulerException {
notNull(periods, "periods");
requireTrue(periods.size() > 0, "periods");
notEmpty(captureAgentId, "captureAgentId");
notNull(userIds, "userIds");
notNull(templateMp, "mediaPackages");
notNull(wfProperties, "wfProperties");
notNull(caMetadata, "caMetadata");
notEmpty(modificationOrigin, "modificationOrigin");
notNull(optOutStatus, "optOutStatus");
notNull(schedulingSource, "schedulingSource");
notNull(trxId, "trxId");
Map<String, Period> scheduledEvents = new LinkedHashMap<>();
try {
LinkedList<Id> ids = new LinkedList<>();
AQueryBuilder qb = assetManager.createQuery();
Predicate p = null;
// While we don't have a list of IDs equal to the number of periods
while (ids.size() <= periods.size()) {
// Create a list of IDs equal to the number of periods, along with a set of AM predicates
while (ids.size() <= periods.size()) {
Id id = new IdImpl(UUID.randomUUID().toString());
ids.add(id);
Predicate np = qb.mediaPackageId(id.compact());
// Haha, p = np jokes with the AM query language. Ha. Haha. Ha. (Sob...)
if (null == p) {
p = np;
} else {
p = p.or(np);
}
}
// Select the list of ids which alread exist. Hint: this needs to be zero
AResult result = qb.select(qb.nothing()).where(withOrganization(qb).and(p).and(qb.version().isLatest())).run();
// If there is conflict, clear the list and start over
if (result.getTotalSize() > 0) {
ids.clear();
}
}
Opt<String> seriesId = Opt.nul(StringUtils.trimToNull(templateMp.getSeries()));
// Get opt out status
boolean optOut = getOptOutStatus(seriesId, optOutStatus);
if (trxId.isNone()) {
// Check for locked transactions
if (schedulingSource.isSome() && persistence.hasTransaction(schedulingSource.get())) {
logger.warn("Unable to add events, source '{}' is currently locked due to an active transaction!", schedulingSource.get());
throw new SchedulerTransactionLockException("Unable to add event, locked source " + schedulingSource.get());
}
// Check for conflicting events if not opted out
if (!optOut) {
List<MediaPackage> conflictingEvents = findConflictingEvents(periods, captureAgentId, TimeZone.getDefault());
if (conflictingEvents.size() > 0) {
logger.info("Unable to add events, conflicting events found: {}", conflictingEvents);
throw new SchedulerConflictException("Unable to add event, conflicting events found");
}
}
}
// counter for index into the list of mediapackages
int counter = 0;
for (Period event : periods) {
MediaPackage mediaPackage = (MediaPackage) templateMp.clone();
Date startDate = new Date(event.getStart().getTime());
Date endDate = new Date(event.getEnd().getTime());
Id id = ids.get(counter);
// Get, or make, the DC catalog
DublinCoreCatalog dc;
Opt<DublinCoreCatalog> dcOpt = DublinCoreUtil.loadEpisodeDublinCore(workspace, templateMp);
if (dcOpt.isSome()) {
dc = dcOpt.get();
dc = (DublinCoreCatalog) dc.clone();
// make sure to bind the OC_PROPERTY namespace
dc.addBindings(XmlNamespaceContext.mk(XmlNamespaceBinding.mk(DublinCores.OC_PROPERTY_NS_PREFIX, DublinCores.OC_PROPERTY_NS_URI)));
} else {
dc = DublinCores.mkOpencastEpisode().getCatalog();
}
// Set the new media package identifier
mediaPackage.setIdentifier(id);
// Update dublincore title and temporal
String newTitle = dc.getFirst(DublinCore.PROPERTY_TITLE) + String.format(" %0" + Integer.toString(periods.size()).length() + "d", ++counter);
dc.set(DublinCore.PROPERTY_TITLE, newTitle);
DublinCoreValue eventTime = EncodingSchemeUtils.encodePeriod(new DCMIPeriod(startDate, endDate), Precision.Second);
dc.set(DublinCore.PROPERTY_TEMPORAL, eventTime);
mediaPackage = updateDublincCoreCatalog(mediaPackage, dc);
mediaPackage.setTitle(newTitle);
String mediaPackageId = mediaPackage.getIdentifier().compact();
// Converting from iCal4j DateTime objects to plain Date objects to prevent AMQ issues below
Calendar cal = Calendar.getInstance(TimeZone.getTimeZone("UTC"));
cal.setTime(event.getStart());
Date startDateTime = cal.getTime();
cal.setTime(event.getEnd());
Date endDateTime = cal.getTime();
// Load dublincore and acl for update
Opt<DublinCoreCatalog> dublinCore = DublinCoreUtil.loadEpisodeDublinCore(workspace, mediaPackage);
Option<AccessControlList> acl = authorizationService.getAcl(mediaPackage, AclScope.Episode);
// Get updated agent properties
Map<String, String> finalCaProperties = getFinalAgentProperties(caMetadata, wfProperties, captureAgentId, seriesId, dublinCore);
// Persist asset
String checksum = calculateChecksum(workspace, getEventCatalogUIAdapterFlavors(), startDateTime, endDateTime, captureAgentId, userIds, mediaPackage, dublinCore, wfProperties, finalCaProperties, optOut, acl.toOpt().getOr(new AccessControlList()));
persistEvent(mediaPackageId, modificationOrigin, checksum, Opt.some(startDateTime), Opt.some(endDateTime), Opt.some(captureAgentId), Opt.some(userIds), Opt.some(mediaPackage), Opt.some(wfProperties), Opt.some(finalCaProperties), Opt.some(optOut), schedulingSource, trxId);
if (trxId.isNone()) {
// Send updates
sendUpdateAddEvent(mediaPackageId, acl.toOpt(), dublinCore, Opt.some(startDateTime), Opt.some(endDateTime), Opt.some(userIds), Opt.some(captureAgentId), Opt.some(finalCaProperties), Opt.some(optOut));
// Update last modified
touchLastEntry(captureAgentId);
}
scheduledEvents.put(mediaPackageId, event);
for (MediaPackageElement mediaPackageElement : mediaPackage.getElements()) {
try {
workspace.delete(mediaPackage.getIdentifier().toString(), mediaPackageElement.getIdentifier());
} catch (NotFoundException | IOException e) {
logger.warn("Failed to delete media package element", e);
}
}
}
return scheduledEvents;
} catch (SchedulerException e) {
throw e;
} catch (Exception e) {
logger.error("Failed to create events: {}", getStackTrace(e));
throw new SchedulerException(e);
}
}
use of org.opencastproject.scheduler.api.SchedulerTransactionLockException in project opencast by opencast.
the class SchedulerServiceImpl method removeEvent.
@Override
public synchronized void removeEvent(String mediaPackageId) throws NotFoundException, UnauthorizedException, SchedulerException {
notEmpty(mediaPackageId, "mediaPackageId");
try {
// Check if there are properties only from scheduler
AQueryBuilder query = assetManager.createQuery();
Props p = new Props(query);
AResult result = query.select(p.agent().target(), p.source().target()).where(withOrganization(query).and(query.mediaPackageId(mediaPackageId)).and(query.version().isLatest()).and(query.hasPropertiesOf(p.namespace()))).run();
Opt<ARecord> recordOpt = result.getRecords().head();
long deletedProperties = 0;
if (recordOpt.isSome()) {
// Check for locked transactions
Opt<String> source = recordOpt.get().getProperties().apply(Properties.getStringOpt(SOURCE_CONFIG));
if (source.isSome() && persistence.hasTransaction(source.get())) {
logger.warn("Unable to remove event '{}', source '{}' is currently locked due to an active transaction!", mediaPackageId, source.get());
throw new SchedulerTransactionLockException("Unable to remove event, locked source " + source.get());
}
String agentId = recordOpt.get().getProperties().apply(Properties.getString(AGENT_CONFIG));
// Delete all properties
deletedProperties = query.delete(SNAPSHOT_OWNER, query.propertiesOf(p.namespace(), WORKFLOW_NAMESPACE, CA_NAMESPACE)).where(withOrganization(query).and(query.mediaPackageId(mediaPackageId))).name("delete all properties").run();
if (StringUtils.isNotEmpty(agentId))
touchLastEntry(agentId);
}
// Delete scheduler snapshot
long deletedSnapshots = query.delete(SNAPSHOT_OWNER, query.snapshot()).where(withOrganization(query).and(query.mediaPackageId(mediaPackageId))).name("delete episode").run();
if (deletedProperties + deletedSnapshots == 0)
throw new NotFoundException();
messageSender.sendObjectMessage(SchedulerItem.SCHEDULER_QUEUE, MessageSender.DestinationType.Queue, SchedulerItem.delete(mediaPackageId));
} catch (NotFoundException | SchedulerException e) {
throw e;
} catch (Exception e) {
logger.error("Could not remove event '{}' from persistent storage: {}", mediaPackageId, e);
throw new SchedulerException(e);
}
}
use of org.opencastproject.scheduler.api.SchedulerTransactionLockException in project opencast by opencast.
the class SchedulerServiceImplTest method testTransactionCommit.
@Test
public void testTransactionCommit() throws Exception {
Date start = new Date();
Date end = new Date(System.currentTimeMillis() + 60000);
String captureDeviceID = "demo";
String seriesId = "series1";
Set<String> userIds = new HashSet<>();
userIds.add("user1");
userIds.add("user2");
MediaPackage mp = generateEvent(Opt.<String>none());
mp.setSeries(seriesId);
DublinCoreCatalog event = generateEvent(captureDeviceID, start, end);
addDublinCore(Opt.<String>none(), mp, event);
Map<String, String> caProperties = generateCaptureAgentMetadata("demo");
try {
schedSvc.addEvent(start, end, captureDeviceID, userIds, mp, wfProperties, caProperties, Opt.<Boolean>none(), Opt.some("new"), "new");
} catch (SchedulerTransactionLockException e) {
fail("Transaction create lock not working!");
}
SchedulerTransaction trx = schedSvc.createTransaction("new");
assertEquals("new", trx.getSource());
AQueryBuilder query = assetManager.createQuery();
AResult result = query.select(query.snapshot(), query.properties()).where(query.organizationId().eq(new DefaultOrganization().getId()).and(query.mediaPackageId(mp.getIdentifier().compact()))).run();
assertEquals(1, result.getSize());
trx.addEvent(start, end, captureDeviceID, userIds, mp, wfPropertiesUpdated, caProperties, Opt.<Boolean>none());
query = assetManager.createQuery();
result = query.select(query.snapshot(), query.properties()).where(query.organizationId().eq(new DefaultOrganization().getId()).and(query.mediaPackageId(mp.getIdentifier().compact()))).run();
assertEquals(2, result.getSize());
trx.commit();
query = assetManager.createQuery();
result = query.select(query.snapshot(), query.properties()).where(query.organizationId().eq(new DefaultOrganization().getId()).and(query.mediaPackageId(mp.getIdentifier().compact())).and(query.hasPropertiesOf(SchedulerServiceImpl.TRX_NAMESPACE).not())).run();
assertEquals(1, result.getSize());
ARecord record = result.getRecords().head2();
assertEquals(VersionImpl.mk(1), record.getSnapshot().get().getVersion());
try {
schedSvc.getTransaction(trx.getId());
fail("Deleted transaction found!");
} catch (NotFoundException e) {
assertNotNull(e);
}
}
use of org.opencastproject.scheduler.api.SchedulerTransactionLockException in project opencast by opencast.
the class SchedulerServiceImplTest method testTransactionRollback.
@Test
public void testTransactionRollback() throws Exception {
Date start = new Date();
Date end = new Date(System.currentTimeMillis() + 60000);
String captureDeviceID = "demo";
String seriesId = "series1";
Set<String> userIds = new HashSet<>();
userIds.add("user1");
userIds.add("user2");
MediaPackage mp = generateEvent(Opt.<String>none());
mp.setSeries(seriesId);
DublinCoreCatalog event = generateEvent(captureDeviceID, start, end);
addDublinCore(Opt.<String>none(), mp, event);
Map<String, String> caProperties = generateCaptureAgentMetadata("demo");
try {
schedSvc.addEvent(start, end, captureDeviceID, userIds, mp, wfPropertiesUpdated, caProperties, Opt.<Boolean>none(), Opt.some(SchedulerService.ORIGIN), SchedulerService.ORIGIN);
} catch (SchedulerTransactionLockException e) {
fail("Unable to add event!");
}
SchedulerTransaction trx = schedSvc.createTransaction("new");
assertEquals("new", trx.getSource());
AQueryBuilder query = assetManager.createQuery();
AResult result = query.select(query.snapshot(), query.properties()).where(query.organizationId().eq(new DefaultOrganization().getId()).and(query.mediaPackageId(mp.getIdentifier().compact()))).run();
assertEquals(1, result.getSize());
trx.addEvent(start, end, captureDeviceID, userIds, mp, wfPropertiesUpdated, caProperties, Opt.<Boolean>none());
query = assetManager.createQuery();
result = query.select(query.snapshot(), query.properties()).where(query.organizationId().eq(new DefaultOrganization().getId()).and(query.mediaPackageId(mp.getIdentifier().compact()))).run();
assertEquals(2, result.getSize());
trx.rollback();
query = assetManager.createQuery();
result = query.select(query.snapshot(), query.properties()).where(query.organizationId().eq(new DefaultOrganization().getId()).and(query.mediaPackageId(mp.getIdentifier().compact()))).run();
assertEquals(1, result.getSize());
try {
schedSvc.getTransaction(trx.getId());
fail("Deleted transaction found!");
} catch (NotFoundException e) {
assertNotNull(e);
}
trx = schedSvc.createTransaction("new");
trx.addEvent(start, end, captureDeviceID, userIds, mp, wfPropertiesUpdated, caProperties, Opt.<Boolean>none());
query = assetManager.createQuery();
result = query.select(query.snapshot(), query.properties()).where(query.organizationId().eq(new DefaultOrganization().getId()).and(query.mediaPackageId(mp.getIdentifier().compact()))).run();
assertEquals(2, result.getSize());
assertEquals(new VersionImpl(2), result.getRecords().tail().head2().getSnapshot().get().getVersion());
assertEquals(new VersionImpl(0), result.getRecords().head2().getSnapshot().get().getVersion());
try {
schedSvc.getMediaPackage(mp.getIdentifier().compact());
} catch (NotFoundException e) {
fail("Detection of second last element doesn't work!");
}
}
use of org.opencastproject.scheduler.api.SchedulerTransactionLockException in project opencast by opencast.
the class SchedulerServiceImplTest method testTransactionCommitCollision.
@Test
public void testTransactionCommitCollision() throws Exception {
Date start = new Date();
Date end = new Date(System.currentTimeMillis() + 60000);
String captureDeviceID = "demo";
String seriesId = "series1";
Set<String> userIds = new HashSet<>();
userIds.add("user1");
userIds.add("user2");
MediaPackage mp = generateEvent(Opt.<String>none());
mp.setSeries(seriesId);
DublinCoreCatalog event = generateEvent(captureDeviceID, start, end);
addDublinCore(Opt.<String>none(), mp, event);
Map<String, String> caProperties = generateCaptureAgentMetadata("demo");
MediaPackage mp2 = generateEvent(Opt.<String>none());
try {
schedSvc.addEvent(start, end, captureDeviceID, userIds, mp2, wfPropertiesUpdated, caProperties, Opt.some(false), Opt.some("existing"), SchedulerService.ORIGIN);
} catch (SchedulerTransactionLockException e) {
fail("Transaction create lock not working!");
}
/* Test transaction collision of already existing event and new transaction event */
SchedulerTransaction trx = schedSvc.createTransaction("new");
assertEquals("new", trx.getSource());
AQueryBuilder query = assetManager.createQuery();
AResult result = query.select(query.snapshot(), query.properties()).where(query.organizationId().eq(new DefaultOrganization().getId()).and(query.mediaPackageId(mp.getIdentifier().compact()))).run();
Opt<ARecord> record = result.getRecords().head();
assertFalse(record.isSome());
trx.addEvent(start, end, captureDeviceID, userIds, mp, wfPropertiesUpdated, caProperties, Opt.some(false));
query = assetManager.createQuery();
result = query.select(query.snapshot(), query.properties()).where(query.organizationId().eq(new DefaultOrganization().getId()).and(query.mediaPackageId(mp.getIdentifier().compact()))).run();
record = result.getRecords().head();
assertTrue(record.isSome());
try {
trx.commit();
fail("Pre-conflict detection not working!");
} catch (SchedulerConflictException e) {
assertNotNull(e);
}
query = assetManager.createQuery();
result = query.select(query.snapshot(), query.properties()).where(query.organizationId().eq(new DefaultOrganization().getId()).and(query.mediaPackageId(mp.getIdentifier().compact()))).run();
record = result.getRecords().head();
assertTrue(record.isSome());
try {
schedSvc.getTransaction(trx.getId());
} catch (NotFoundException e) {
fail("Transaction found!");
}
/* Test transaction collision of already existing event and new opted out transaction event */
MediaPackage mp4 = (MediaPackage) mp.clone();
mp4.setIdentifier(new IdImpl("newuuid"));
SchedulerTransaction trx2 = schedSvc.createTransaction("optedout");
assertEquals("optedout", trx2.getSource());
query = assetManager.createQuery();
result = query.select(query.snapshot(), query.properties()).where(query.organizationId().eq(new DefaultOrganization().getId()).and(query.mediaPackageId(mp4.getIdentifier().compact()))).run();
record = result.getRecords().head();
assertFalse(record.isSome());
trx2.addEvent(start, end, captureDeviceID, userIds, mp4, wfPropertiesUpdated, caProperties, Opt.some(true));
query = assetManager.createQuery();
result = query.select(query.snapshot(), query.properties()).where(query.organizationId().eq(new DefaultOrganization().getId()).and(query.mediaPackageId(mp4.getIdentifier().compact()))).run();
record = result.getRecords().head();
assertTrue(record.isSome());
try {
trx2.commit();
} catch (SchedulerConflictException e) {
fail("Pre-conflict detection not working!");
}
/* Test transaction collision of two transaction events */
schedSvc.removeEvent(mp2.getIdentifier().compact());
MediaPackage mp3 = generateEvent(Opt.<String>none());
trx.addEvent(start, end, captureDeviceID, userIds, mp3, wfPropertiesUpdated, caProperties, Opt.some(false));
try {
trx.commit();
fail("Pre-conflict detection not working!");
} catch (SchedulerConflictException e) {
assertNotNull(e);
}
query = assetManager.createQuery();
result = query.select(query.snapshot(), query.properties()).where(query.organizationId().eq(new DefaultOrganization().getId()).and(query.mediaPackageId(mp.getIdentifier().compact()))).run();
record = result.getRecords().head();
assertTrue(record.isSome());
try {
schedSvc.getTransaction(trx.getId());
} catch (NotFoundException e) {
fail("Transaction found!");
}
/* Test transaction collision of two transaction events but one opted out */
MediaPackage mp5 = (MediaPackage) mp.clone();
mp5.setIdentifier(new IdImpl("newuuid2"));
SchedulerTransaction trx3 = schedSvc.createTransaction("optedout2");
assertEquals("optedout2", trx3.getSource());
query = assetManager.createQuery();
result = query.select(query.snapshot(), query.properties()).where(query.organizationId().eq(new DefaultOrganization().getId()).and(query.mediaPackageId(mp5.getIdentifier().compact()))).run();
record = result.getRecords().head();
assertFalse(record.isSome());
trx3.addEvent(start, end, captureDeviceID, userIds, mp5, wfPropertiesUpdated, caProperties, Opt.some(false));
query = assetManager.createQuery();
result = query.select(query.snapshot(), query.properties()).where(query.organizationId().eq(new DefaultOrganization().getId()).and(query.mediaPackageId(mp5.getIdentifier().compact()))).run();
record = result.getRecords().head();
assertTrue(record.isSome());
MediaPackage mp6 = generateEvent(Opt.<String>none());
trx.addEvent(start, end, captureDeviceID, userIds, mp6, wfPropertiesUpdated, caProperties, Opt.some(true));
try {
trx3.commit();
} catch (SchedulerConflictException e) {
fail("Pre-conflict detection not working!");
}
}
Aggregations