use of com.infiniteautomation.mango.spring.service.PermissionService in project ma-core-public by MangoAutomation.
the class DataSourceDaoDeadlockDetection method detectDeadlockWithEventHandlerRoleMappingandDataSourceTablesExplicit.
@Test
public void detectDeadlockWithEventHandlerRoleMappingandDataSourceTablesExplicit() {
// This will create 2x threads for each operating as one of the desired problem scenarios
int numThreads = 5;
int numDataSources = 10;
AtomicInteger running = new AtomicInteger(numThreads * 2);
PermissionService permissionService = Common.getBean(PermissionService.class);
// Insert some roles
int roleCount = 0;
RoleService roleService = Common.getBean(RoleService.class);
List<RoleVO> roleVOs = new ArrayList<>();
Set<Role> roles = new HashSet<>();
for (int i = 0; i < roleCount; i++) {
RoleVO role = new RoleVO(Common.NEW_ID, Common.generateXid("ROLE_"), "Role " + i);
roleVOs.add(role);
roleService.insert(role);
roles.add(role.getRole());
}
DataSource dataSource = Common.getBean(DatabaseProxy.class).getDataSource();
JdbcConnectionPool pool = (JdbcConnectionPool) dataSource;
pool.setMaxConnections(numThreads * 2);
PlatformTransactionManager transactionManager = Common.getBean(DatabaseProxy.class).getTransactionManager();
AtomicInteger successes = new AtomicInteger();
AtomicInteger failures = new AtomicInteger();
MutableObject<Exception> failure = new MutableObject<>(null);
for (int i = 0; i < numThreads; i++) {
// #5 lock eventHandlerMappings and roleMappings and then try to lock dataSources
// Basically delete a data source
new Thread() {
@Override
public void run() {
try {
for (int i = 0; i < numDataSources; i++) {
// Insert an event handler
EventHandlerService eventHandlerService = Common.getBean(EventHandlerService.class);
ProcessEventHandlerVO eh = new ProcessEventHandlerVO();
eh.setDefinition(new ProcessEventHandlerDefinition());
eh.setName(Common.generateXid("Handler "));
eh.setActiveProcessCommand("ls");
eventHandlerService.insert(eh);
ExtendedJdbcTemplate ejt = new ExtendedJdbcTemplate(dataSource);
// Get event handler
AbstractEventHandlerVO myEventHandler = eventHandlerService.get(eh.getXid());
// Create data source
MockDataSourceVO ds = new MockDataSourceVO();
ds.setName(Common.generateXid("Mock "));
DataSourceService dataSourceService = Common.getBean(DataSourceService.class);
dataSourceService.insert(ds);
// Insert a mapping
myEventHandler.setEventTypes(Collections.singletonList(new EventTypeMatcher(new DataSourceEventType(ds.getId(), ds.getPollAbortedExceptionEventId()))));
eventHandlerService.update(eh.getXid(), myEventHandler);
new TransactionTemplate(transactionManager).execute((status) -> {
// The order of these statements matters for deadlock, we must always lock groups of tables in the same order
ejt.update("DELETE FROM dataSources WHERE id=?", new Object[] { ds.getId() });
ejt.update("DELETE FROM eventHandlersMapping WHERE eventTypeName=? AND eventTypeRef1=?", new Object[] { EventTypeNames.DATA_SOURCE, ds.getId() });
return null;
});
successes.getAndIncrement();
}
} catch (Exception e) {
e.printStackTrace();
failure.setValue(e);
failures.getAndIncrement();
} finally {
running.decrementAndGet();
}
}
}.start();
// #8 lock dataSources and try to lock roleMappings
// Basically update a data source
new Thread() {
@Override
public void run() {
try {
for (int i = 0; i < numDataSources; i++) {
ExtendedJdbcTemplate ejt = new ExtendedJdbcTemplate(dataSource);
// Insert an event handler
EventHandlerService eventHandlerService = Common.getBean(EventHandlerService.class);
ProcessEventHandlerVO eh = new ProcessEventHandlerVO();
eh.setDefinition(new ProcessEventHandlerDefinition());
eh.setName(Common.generateXid("Handler "));
eh.setActiveProcessCommand("ls");
eventHandlerService.insert(eh);
// Get event handler
AbstractEventHandlerVO myEventHandler = eventHandlerService.get(eh.getXid());
// Create data source
MockDataSourceVO ds = new MockDataSourceVO();
ds.setName(Common.generateXid("Mock "));
DataSourceService dataSourceService = Common.getBean(DataSourceService.class);
dataSourceService.insert(ds);
// Insert a mapping
myEventHandler.setEventTypes(Collections.singletonList(new EventTypeMatcher(new DataSourceEventType(ds.getId(), ds.getPollAbortedExceptionEventId()))));
eventHandlerService.update(eh.getXid(), myEventHandler);
new TransactionTemplate(transactionManager).execute((status) -> {
ejt.update("UPDATE dataSources SET xid=? WHERE id=?", new Object[] { ds.getXid() + "1", ds.getId() });
return null;
});
successes.getAndIncrement();
}
} catch (Exception e) {
e.printStackTrace();
failure.setValue(e);
failures.getAndIncrement();
} finally {
running.decrementAndGet();
}
}
}.start();
}
while (running.get() > 0) {
try {
Thread.sleep(100);
} catch (Exception e) {
}
}
if (failures.get() > 0) {
fail("Ran " + successes.get() + " queries: " + failure.getValue().getMessage());
}
}
use of com.infiniteautomation.mango.spring.service.PermissionService in project ma-core-public by MangoAutomation.
the class UserEventMulticasterTest method testMulticastEventsForUsersWithPermissions.
@Test
public void testMulticastEventsForUsersWithPermissions() {
PermissionService service = Common.getBean(PermissionService.class);
int dataPointId = 1;
int eventCount = 100;
int userCount = 5 * 6;
// Add them out of order so the tree is jumbled with permissions hither and yon
List<User> users = new ArrayList<>();
int added = 0;
for (int i = 0; i < (userCount / 6); i++) {
users.add(createUser("User" + added, "user" + added, "password", "user" + added + "@yourMangoDomain.com", PermissionHolder.SUPERADMIN_ROLE));
added++;
}
for (int i = 0; i < (userCount / 6); i++) {
users.add(createUser("User" + added, "user" + added, "password", "user" + added + "@yourMangoDomain.com", mockRole.getRole()));
added++;
}
for (int i = 0; i < (userCount / 6); i++) {
users.add(createUser("User" + added, "user" + added, "password", "user" + added + "@yourMangoDomain.com"));
added++;
}
for (int i = 0; i < (userCount / 6); i++) {
users.add(createUser("User" + added, "user" + added, "password", "user" + added + "@yourMangoDomain.com", PermissionHolder.SUPERADMIN_ROLE));
added++;
}
for (int i = 0; i < (userCount / 6); i++) {
users.add(createUser("User" + added, "user" + added, "password", "user" + added + "@yourMangoDomain.com", mockRole.getRole()));
added++;
}
for (int i = 0; i < (userCount / 6); i++) {
users.add(createUser("User" + added, "user" + added, "password", "user" + added + "@yourMangoDomain.com"));
added++;
}
List<Integer> idsToNotify = new ArrayList<>();
List<MockUserEventListener> listeners = new ArrayList<>();
UserEventListener multicaster = null;
MockEventType mockEventType = new MockEventType(DuplicateHandling.ALLOW, null, 0, dataPointId, this.mockRole.getRole());
for (User u : users) {
MockUserEventListener l = new MockUserEventListener(u);
if (// This work is normally done by the event manager handling the raiseEvent calls
mockEventType.hasPermission(u, service))
// through an EventNotifyWorkItem
idsToNotify.add(u.getId());
listeners.add(l);
multicaster = UserEventMulticaster.add(multicaster, l);
}
List<EventInstance> events = new ArrayList<>();
long time = 0;
for (int i = 0; i < eventCount; i++) {
EventInstance event = createMockEventInstance(i, dataPointId, time);
events.add(event);
event.setIdsToNotify(idsToNotify);
multicaster.raised(event);
time += 1;
}
// Ack
for (EventInstance e : events) multicaster.acknowledged(e);
// Rtn
for (EventInstance e : events) multicaster.returnToNormal(e);
// Confirm those with correct permissions permissions saw all raised
for (MockUserEventListener l : listeners) {
if (!(service.hasPermission(l.getUser(), MangoPermission.requireAnyRole(mockRole.getRole())) || service.hasAdminRole(l.getUser()))) {
assertEquals(0, l.getRaised().size());
} else {
assertEquals(eventCount, l.getRaised().size());
}
}
// Confirm those with permissions saw all acked
for (MockUserEventListener l : listeners) {
if (!(service.hasPermission(l.getUser(), MangoPermission.requireAnyRole(mockRole.getRole())) || service.hasAdminRole(l.getUser()))) {
assertEquals(0, l.getAcknowledged().size());
} else {
assertEquals(eventCount, l.getAcknowledged().size());
}
}
// Confirm those with permissions saw all rtned
for (MockUserEventListener l : listeners) {
if (!(service.hasPermission(l.getUser(), MangoPermission.requireAnyRole(mockRole.getRole())) || service.hasAdminRole(l.getUser()))) {
assertEquals(0, l.getReturned().size());
} else {
assertEquals(eventCount, l.getReturned().size());
}
}
}
Aggregations