use of com.infiniteautomation.mango.spring.service.PermissionService in project ma-core-public by infiniteautomation.
the class RoleInheritanceTest method test3.
/**
* Test a slightly more complex tree hierarchy with multiple inheritance
*
* <pre>{@code
* gasTechnician -> gasRead <- gasEdit <- gasAdmin
* technician -> solidTechnician -> solidRead <- solidEdit <- solidAdmin <- admin
* fluidTechnician -> fluidRead <- fluidEdit <- fluidAdmin
* }</pre>
*/
@Test
public void test3() {
PermissionService service = Common.getBean(PermissionService.class);
// Fluid roles
RoleVOHierarchy fluidRead = createRoleVOHierarchy("fluidRead", "fluidRead");
MangoPermission fluidReadPermission = MangoPermission.requireAnyRole(fluidRead.getRole());
RoleVOHierarchy fluidEdit = createRoleVOHierarchy("fluidEdit", "fluidEdit", fluidRead);
MangoPermission fluidEditPermission = MangoPermission.requireAnyRole(fluidEdit.getRole());
RoleVOHierarchy fluidTechnician = createRoleVOHierarchy("fluidTechnician", "fluidTechnician", fluidRead);
RoleVOHierarchy fluidAdmin = createRoleVOHierarchy("fluidAdmin", "fluidAdmin", fluidEdit);
// Solid roles
RoleVOHierarchy solidRead = createRoleVOHierarchy("solidRead", "solidRead");
MangoPermission solidReadPermission = MangoPermission.requireAnyRole(solidRead.getRole());
RoleVOHierarchy solidEdit = createRoleVOHierarchy("solidEdit", "solidEdit", solidRead);
MangoPermission solidEditPermission = MangoPermission.requireAnyRole(solidEdit.getRole());
RoleVOHierarchy solidTechnician = createRoleVOHierarchy("solidTechnician", "solidTechnician", solidRead);
RoleVOHierarchy solidAdmin = createRoleVOHierarchy("solidAdmin", "solidAdmin", solidEdit);
// Gas roles
RoleVOHierarchy gasRead = createRoleVOHierarchy("gasRead", "gasRead");
MangoPermission gasReadPermission = MangoPermission.requireAnyRole(gasRead.getRole());
RoleVOHierarchy gasEdit = createRoleVOHierarchy("gasEdit", "gasEdit", gasRead);
MangoPermission gasEditPermission = MangoPermission.requireAnyRole(gasEdit.getRole());
RoleVOHierarchy gasTechnician = createRoleVOHierarchy("gasTechnician", "gasTechnician", gasRead);
RoleVOHierarchy gasAdmin = createRoleVOHierarchy("gasAdmin", "gasAdmin", gasEdit);
RoleVOHierarchy technician = createRoleVOHierarchy("technician", "technician", fluidTechnician, solidTechnician, gasTechnician);
RoleVOHierarchy admin = createRoleVOHierarchy("admin", "admin", fluidAdmin, solidAdmin, gasAdmin);
// Who can read fluid
assertTrue(service.hasPermission(createPermissionHolder(fluidRead), fluidReadPermission));
assertTrue(service.hasPermission(createPermissionHolder(fluidEdit), fluidReadPermission));
assertTrue(service.hasPermission(createPermissionHolder(fluidTechnician), fluidReadPermission));
assertTrue(service.hasPermission(createPermissionHolder(fluidAdmin), fluidReadPermission));
assertTrue(service.hasPermission(createPermissionHolder(technician), fluidReadPermission));
assertTrue(service.hasPermission(createPermissionHolder(admin), fluidReadPermission));
// Who cannot read fluid
assertFalse(service.hasPermission(createPermissionHolder(gasRead), fluidReadPermission));
assertFalse(service.hasPermission(createPermissionHolder(gasEdit), fluidReadPermission));
assertFalse(service.hasPermission(createPermissionHolder(gasTechnician), fluidReadPermission));
assertFalse(service.hasPermission(createPermissionHolder(gasAdmin), fluidReadPermission));
assertFalse(service.hasPermission(createPermissionHolder(solidRead), fluidReadPermission));
assertFalse(service.hasPermission(createPermissionHolder(solidEdit), fluidReadPermission));
assertFalse(service.hasPermission(createPermissionHolder(solidTechnician), fluidReadPermission));
assertFalse(service.hasPermission(createPermissionHolder(solidAdmin), fluidReadPermission));
// Who can edit fluid
assertTrue(service.hasPermission(createPermissionHolder(fluidEdit), fluidEditPermission));
assertTrue(service.hasPermission(createPermissionHolder(fluidAdmin), fluidEditPermission));
assertTrue(service.hasPermission(createPermissionHolder(admin), fluidEditPermission));
// Who cannot edit fluid
assertFalse(service.hasPermission(createPermissionHolder(fluidRead), fluidEditPermission));
assertFalse(service.hasPermission(createPermissionHolder(gasRead), fluidEditPermission));
assertFalse(service.hasPermission(createPermissionHolder(gasEdit), fluidEditPermission));
assertFalse(service.hasPermission(createPermissionHolder(gasTechnician), fluidEditPermission));
assertFalse(service.hasPermission(createPermissionHolder(solidRead), fluidEditPermission));
assertFalse(service.hasPermission(createPermissionHolder(solidEdit), fluidEditPermission));
assertFalse(service.hasPermission(createPermissionHolder(solidTechnician), fluidEditPermission));
assertFalse(service.hasPermission(createPermissionHolder(technician), fluidEditPermission));
// Who can read solid
assertTrue(service.hasPermission(createPermissionHolder(solidRead), solidReadPermission));
assertTrue(service.hasPermission(createPermissionHolder(solidEdit), solidReadPermission));
assertTrue(service.hasPermission(createPermissionHolder(solidTechnician), solidReadPermission));
assertTrue(service.hasPermission(createPermissionHolder(solidAdmin), solidReadPermission));
assertTrue(service.hasPermission(createPermissionHolder(technician), solidReadPermission));
assertTrue(service.hasPermission(createPermissionHolder(admin), solidReadPermission));
// Who cannot read solid
assertFalse(service.hasPermission(createPermissionHolder(gasRead), solidReadPermission));
assertFalse(service.hasPermission(createPermissionHolder(gasEdit), solidReadPermission));
assertFalse(service.hasPermission(createPermissionHolder(gasTechnician), solidReadPermission));
assertFalse(service.hasPermission(createPermissionHolder(gasAdmin), solidReadPermission));
assertFalse(service.hasPermission(createPermissionHolder(fluidRead), solidReadPermission));
assertFalse(service.hasPermission(createPermissionHolder(fluidEdit), solidReadPermission));
assertFalse(service.hasPermission(createPermissionHolder(fluidTechnician), solidReadPermission));
assertFalse(service.hasPermission(createPermissionHolder(fluidAdmin), solidReadPermission));
// Who can edit solid
assertTrue(service.hasPermission(createPermissionHolder(solidEdit), solidEditPermission));
assertTrue(service.hasPermission(createPermissionHolder(solidAdmin), solidEditPermission));
assertTrue(service.hasPermission(createPermissionHolder(admin), solidEditPermission));
// Who cannot edit solid
assertFalse(service.hasPermission(createPermissionHolder(solidRead), solidEditPermission));
assertFalse(service.hasPermission(createPermissionHolder(gasRead), solidEditPermission));
assertFalse(service.hasPermission(createPermissionHolder(gasEdit), solidEditPermission));
assertFalse(service.hasPermission(createPermissionHolder(gasTechnician), solidEditPermission));
assertFalse(service.hasPermission(createPermissionHolder(fluidRead), solidEditPermission));
assertFalse(service.hasPermission(createPermissionHolder(fluidEdit), solidEditPermission));
assertFalse(service.hasPermission(createPermissionHolder(fluidTechnician), solidEditPermission));
assertFalse(service.hasPermission(createPermissionHolder(technician), solidEditPermission));
// Who can read gas
assertTrue(service.hasPermission(createPermissionHolder(gasRead), gasReadPermission));
assertTrue(service.hasPermission(createPermissionHolder(gasEdit), gasReadPermission));
assertTrue(service.hasPermission(createPermissionHolder(gasTechnician), gasReadPermission));
assertTrue(service.hasPermission(createPermissionHolder(gasAdmin), gasReadPermission));
assertTrue(service.hasPermission(createPermissionHolder(technician), gasReadPermission));
assertTrue(service.hasPermission(createPermissionHolder(admin), gasReadPermission));
// Who cannot read gas
assertFalse(service.hasPermission(createPermissionHolder(solidRead), gasReadPermission));
assertFalse(service.hasPermission(createPermissionHolder(solidEdit), gasReadPermission));
assertFalse(service.hasPermission(createPermissionHolder(solidTechnician), gasReadPermission));
assertFalse(service.hasPermission(createPermissionHolder(solidAdmin), gasReadPermission));
assertFalse(service.hasPermission(createPermissionHolder(fluidRead), gasReadPermission));
assertFalse(service.hasPermission(createPermissionHolder(fluidEdit), gasReadPermission));
assertFalse(service.hasPermission(createPermissionHolder(fluidTechnician), gasReadPermission));
assertFalse(service.hasPermission(createPermissionHolder(fluidAdmin), gasReadPermission));
// Who can edit gas
assertTrue(service.hasPermission(createPermissionHolder(gasEdit), gasEditPermission));
assertTrue(service.hasPermission(createPermissionHolder(gasAdmin), gasEditPermission));
assertTrue(service.hasPermission(createPermissionHolder(admin), gasEditPermission));
// Who cannot edit gas
assertFalse(service.hasPermission(createPermissionHolder(gasRead), gasEditPermission));
assertFalse(service.hasPermission(createPermissionHolder(solidRead), gasEditPermission));
assertFalse(service.hasPermission(createPermissionHolder(solidEdit), gasEditPermission));
assertFalse(service.hasPermission(createPermissionHolder(solidTechnician), gasEditPermission));
assertFalse(service.hasPermission(createPermissionHolder(fluidRead), gasEditPermission));
assertFalse(service.hasPermission(createPermissionHolder(fluidEdit), gasEditPermission));
assertFalse(service.hasPermission(createPermissionHolder(fluidTechnician), gasEditPermission));
assertFalse(service.hasPermission(createPermissionHolder(technician), gasEditPermission));
}
use of com.infiniteautomation.mango.spring.service.PermissionService in project ma-core-public by infiniteautomation.
the class RoleInheritanceTest method test2.
/**
* Test a tree hierarchy with multiple inheritance
*
* <pre>{@code
* -> gasTechnician -> gasEdit -> gasRead
* technician -> solidTechnician -> solidEdit -> solidRead
* -> fluidTechnician -> fluidEdit -> fluidRead
* }</pre>
*/
@Test
public void test2() {
PermissionService service = Common.getBean(PermissionService.class);
RoleVOHierarchy fluidRead = createRoleVOHierarchy("fluidRead", "fluidRead");
MangoPermission fluidReadPermission = MangoPermission.requireAnyRole(fluidRead.getRole());
RoleVOHierarchy fluidEdit = createRoleVOHierarchy("fluidEdit", "fluidEdit", fluidRead);
MangoPermission fluidEditPermission = MangoPermission.requireAnyRole(fluidEdit.getRole());
RoleVOHierarchy solidRead = createRoleVOHierarchy("solidRead", "solidRead");
MangoPermission solidReadPermission = MangoPermission.requireAnyRole(solidRead.getRole());
RoleVOHierarchy solidEdit = createRoleVOHierarchy("solidEdit", "solidEdit", solidRead);
MangoPermission solidEditPermission = MangoPermission.requireAnyRole(solidEdit.getRole());
RoleVOHierarchy gasRead = createRoleVOHierarchy("gasRead", "gasRead");
MangoPermission gasReadPermission = MangoPermission.requireAnyRole(gasRead.getRole());
RoleVOHierarchy gasEdit = createRoleVOHierarchy("gasEdit", "gasEdit", gasRead);
MangoPermission gasEditPermission = MangoPermission.requireAnyRole(gasEdit.getRole());
RoleVOHierarchy fluidTechnician = createRoleVOHierarchy("fluidTechnician", "fluidTechnician", fluidEdit);
RoleVOHierarchy solidTechnician = createRoleVOHierarchy("solidTechnician", "solidTechnician", solidEdit);
RoleVOHierarchy gasTechnician = createRoleVOHierarchy("gasTechnician", "gasTechnician", gasEdit);
RoleVOHierarchy technician = createRoleVOHierarchy("technician", "technician", fluidTechnician, solidTechnician, gasTechnician);
// Who can read fluid
assertTrue(service.hasPermission(createPermissionHolder(fluidRead), fluidReadPermission));
assertTrue(service.hasPermission(createPermissionHolder(fluidEdit), fluidReadPermission));
assertTrue(service.hasPermission(createPermissionHolder(fluidTechnician), fluidReadPermission));
assertTrue(service.hasPermission(createPermissionHolder(technician), fluidReadPermission));
// Who can edit fluid
assertTrue(service.hasPermission(createPermissionHolder(fluidEdit), fluidEditPermission));
assertTrue(service.hasPermission(createPermissionHolder(fluidTechnician), fluidEditPermission));
assertTrue(service.hasPermission(createPermissionHolder(technician), fluidEditPermission));
// Who cannot read fluid
assertFalse(service.hasPermission(createPermissionHolder(gasRead), fluidReadPermission));
assertFalse(service.hasPermission(createPermissionHolder(gasEdit), fluidReadPermission));
assertFalse(service.hasPermission(createPermissionHolder(gasTechnician), fluidReadPermission));
assertFalse(service.hasPermission(createPermissionHolder(solidRead), fluidReadPermission));
assertFalse(service.hasPermission(createPermissionHolder(solidEdit), fluidReadPermission));
assertFalse(service.hasPermission(createPermissionHolder(solidTechnician), fluidReadPermission));
// Who cannot edit fluid
assertFalse(service.hasPermission(createPermissionHolder(fluidRead), fluidEditPermission));
assertFalse(service.hasPermission(createPermissionHolder(gasRead), fluidEditPermission));
assertFalse(service.hasPermission(createPermissionHolder(gasEdit), fluidEditPermission));
assertFalse(service.hasPermission(createPermissionHolder(gasTechnician), fluidEditPermission));
assertFalse(service.hasPermission(createPermissionHolder(solidRead), fluidEditPermission));
assertFalse(service.hasPermission(createPermissionHolder(solidEdit), fluidEditPermission));
assertFalse(service.hasPermission(createPermissionHolder(solidTechnician), fluidEditPermission));
// Who can read solid
assertTrue(service.hasPermission(createPermissionHolder(solidRead), solidReadPermission));
assertTrue(service.hasPermission(createPermissionHolder(solidEdit), solidReadPermission));
assertTrue(service.hasPermission(createPermissionHolder(solidTechnician), solidReadPermission));
assertTrue(service.hasPermission(createPermissionHolder(technician), solidReadPermission));
// Who can edit solid
assertTrue(service.hasPermission(createPermissionHolder(solidEdit), solidEditPermission));
assertTrue(service.hasPermission(createPermissionHolder(solidTechnician), solidEditPermission));
assertTrue(service.hasPermission(createPermissionHolder(technician), solidEditPermission));
// Who cannot read solid
assertFalse(service.hasPermission(createPermissionHolder(gasRead), solidReadPermission));
assertFalse(service.hasPermission(createPermissionHolder(gasEdit), solidReadPermission));
assertFalse(service.hasPermission(createPermissionHolder(gasTechnician), solidReadPermission));
assertFalse(service.hasPermission(createPermissionHolder(fluidRead), solidReadPermission));
assertFalse(service.hasPermission(createPermissionHolder(fluidEdit), solidReadPermission));
assertFalse(service.hasPermission(createPermissionHolder(fluidTechnician), solidReadPermission));
// Who cannot edit solid
assertFalse(service.hasPermission(createPermissionHolder(solidRead), solidEditPermission));
assertFalse(service.hasPermission(createPermissionHolder(gasRead), solidEditPermission));
assertFalse(service.hasPermission(createPermissionHolder(gasEdit), solidEditPermission));
assertFalse(service.hasPermission(createPermissionHolder(gasTechnician), solidEditPermission));
assertFalse(service.hasPermission(createPermissionHolder(fluidRead), solidEditPermission));
assertFalse(service.hasPermission(createPermissionHolder(fluidEdit), solidEditPermission));
assertFalse(service.hasPermission(createPermissionHolder(fluidTechnician), solidEditPermission));
// Who can read gas
assertTrue(service.hasPermission(createPermissionHolder(gasRead), gasReadPermission));
assertTrue(service.hasPermission(createPermissionHolder(gasEdit), gasReadPermission));
assertTrue(service.hasPermission(createPermissionHolder(gasTechnician), gasReadPermission));
assertTrue(service.hasPermission(createPermissionHolder(technician), gasReadPermission));
// Who can edit gas
assertTrue(service.hasPermission(createPermissionHolder(gasEdit), gasEditPermission));
assertTrue(service.hasPermission(createPermissionHolder(gasTechnician), gasEditPermission));
assertTrue(service.hasPermission(createPermissionHolder(technician), gasEditPermission));
// Who cannot read gas
assertFalse(service.hasPermission(createPermissionHolder(fluidRead), gasReadPermission));
assertFalse(service.hasPermission(createPermissionHolder(fluidEdit), gasReadPermission));
assertFalse(service.hasPermission(createPermissionHolder(fluidTechnician), gasReadPermission));
assertFalse(service.hasPermission(createPermissionHolder(solidRead), gasReadPermission));
assertFalse(service.hasPermission(createPermissionHolder(solidEdit), gasReadPermission));
assertFalse(service.hasPermission(createPermissionHolder(solidTechnician), gasReadPermission));
// Who cannot edit gas
assertFalse(service.hasPermission(createPermissionHolder(gasRead), gasEditPermission));
assertFalse(service.hasPermission(createPermissionHolder(fluidRead), gasEditPermission));
assertFalse(service.hasPermission(createPermissionHolder(fluidEdit), gasEditPermission));
assertFalse(service.hasPermission(createPermissionHolder(fluidTechnician), gasEditPermission));
assertFalse(service.hasPermission(createPermissionHolder(solidRead), gasEditPermission));
assertFalse(service.hasPermission(createPermissionHolder(solidEdit), gasEditPermission));
assertFalse(service.hasPermission(createPermissionHolder(solidTechnician), gasEditPermission));
}
use of com.infiniteautomation.mango.spring.service.PermissionService in project ma-core-public by infiniteautomation.
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 infiniteautomation.
the class DataSourceDaoDeadlockDetection method detectDeadlockFromDataSourceDataPointInsert.
@Test
public void detectDeadlockFromDataSourceDataPointInsert() {
// 25;
int numThreads = 5;
// 100;
int numDataSources = 10;
PermissionService permissionService = Common.getBean(PermissionService.class);
DataSourceService dataSourceService = Common.getBean(DataSourceService.class);
AtomicInteger successes = new AtomicInteger();
AtomicInteger failures = new AtomicInteger();
AtomicInteger running = new AtomicInteger(numThreads);
MutableObject<Exception> failure = new MutableObject<>(null);
for (int i = 0; i < numThreads; i++) {
new Thread() {
@Override
public void run() {
try {
for (int i = 0; i < numDataSources; i++) {
// Create data source
MockDataSourceVO ds = new MockDataSourceVO();
ds.setName(Common.generateXid("Mock "));
dataSourceService.insert(ds);
// Create and save point
createMockDataPoint(ds, new MockPointLocatorVO());
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 infiniteautomation.
the class DataSourceDaoDeadlockDetection method detectDeadlockWithEventHandlerRoleMappingandDataSourceTablesUsingDaos.
@Test
public void detectDeadlockWithEventHandlerRoleMappingandDataSourceTablesUsingDaos() {
// This will create 2x threads for each operating as one of the desired problem scenarios
// 25;
int numThreads = 5;
// 100;
int numDataSources = 10;
AtomicInteger running = new AtomicInteger(numThreads * 2);
PermissionService permissionService = Common.getBean(PermissionService.class);
DataSource dataSource = Common.getBean(DatabaseProxy.class).getDataSource();
JdbcConnectionPool pool = (JdbcConnectionPool) dataSource;
pool.setMaxConnections(numThreads * 100);
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);
dataSourceService.delete(ds.getId());
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);
ds.setXid(ds.getXid() + 1);
dataSourceService.update(ds.getId(), ds);
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());
}
}
Aggregations