Search in sources :

Example 16 with PermissionService

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));
}
Also used : PermissionService(com.infiniteautomation.mango.spring.service.PermissionService) Test(org.junit.Test)

Example 17 with PermissionService

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));
}
Also used : PermissionService(com.infiniteautomation.mango.spring.service.PermissionService) Test(org.junit.Test)

Example 18 with PermissionService

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());
    }
}
Also used : DataSourceService(com.infiniteautomation.mango.spring.service.DataSourceService) Role(com.serotonin.m2m2.vo.role.Role) BeforeClass(org.junit.BeforeClass) EventHandlerService(com.infiniteautomation.mango.spring.service.EventHandlerService) ProcessEventHandlerDefinition(com.serotonin.m2m2.module.definitions.event.handlers.ProcessEventHandlerDefinition) EventTypeNames(com.serotonin.m2m2.rt.event.type.EventType.EventTypeNames) MockDataSourceVO(com.serotonin.m2m2.vo.dataSource.mock.MockDataSourceVO) ProcessEventHandlerVO(com.serotonin.m2m2.vo.event.ProcessEventHandlerVO) LoggerFactory(org.slf4j.LoggerFactory) DataSourceEventType(com.serotonin.m2m2.rt.event.type.DataSourceEventType) DataPointVO(com.serotonin.m2m2.vo.DataPointVO) ArrayList(java.util.ArrayList) ExtendedJdbcTemplate(com.serotonin.db.spring.ExtendedJdbcTemplate) HashSet(java.util.HashSet) EventTypeMatcher(com.serotonin.m2m2.rt.event.type.EventTypeMatcher) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) RoleVO(com.serotonin.m2m2.vo.role.RoleVO) DataSource(javax.sql.DataSource) MangoTestBase(com.serotonin.m2m2.MangoTestBase) DataPointService(com.infiniteautomation.mango.spring.service.DataPointService) Assert.fail(org.junit.Assert.fail) MutableObject(org.apache.commons.lang3.mutable.MutableObject) MockPointLocatorVO(com.serotonin.m2m2.vo.dataPoint.MockPointLocatorVO) Logger(org.slf4j.Logger) Common(com.serotonin.m2m2.Common) DatabaseProxy(com.serotonin.m2m2.db.DatabaseProxy) Set(java.util.Set) Test(org.junit.Test) UUID(java.util.UUID) AbstractEventHandlerVO(com.serotonin.m2m2.vo.event.AbstractEventHandlerVO) List(java.util.List) PlatformTransactionManager(org.springframework.transaction.PlatformTransactionManager) TransactionTemplate(org.springframework.transaction.support.TransactionTemplate) Collections(java.util.Collections) PermissionService(com.infiniteautomation.mango.spring.service.PermissionService) RoleService(com.infiniteautomation.mango.spring.service.RoleService) JdbcConnectionPool(org.h2.jdbcx.JdbcConnectionPool) ArrayList(java.util.ArrayList) ExtendedJdbcTemplate(com.serotonin.db.spring.ExtendedJdbcTemplate) TransactionTemplate(org.springframework.transaction.support.TransactionTemplate) AbstractEventHandlerVO(com.serotonin.m2m2.vo.event.AbstractEventHandlerVO) PermissionService(com.infiniteautomation.mango.spring.service.PermissionService) EventHandlerService(com.infiniteautomation.mango.spring.service.EventHandlerService) HashSet(java.util.HashSet) MutableObject(org.apache.commons.lang3.mutable.MutableObject) ProcessEventHandlerVO(com.serotonin.m2m2.vo.event.ProcessEventHandlerVO) MockDataSourceVO(com.serotonin.m2m2.vo.dataSource.mock.MockDataSourceVO) EventTypeMatcher(com.serotonin.m2m2.rt.event.type.EventTypeMatcher) DataSourceEventType(com.serotonin.m2m2.rt.event.type.DataSourceEventType) JdbcConnectionPool(org.h2.jdbcx.JdbcConnectionPool) DatabaseProxy(com.serotonin.m2m2.db.DatabaseProxy) PlatformTransactionManager(org.springframework.transaction.PlatformTransactionManager) DataSource(javax.sql.DataSource) DataSourceService(com.infiniteautomation.mango.spring.service.DataSourceService) Role(com.serotonin.m2m2.vo.role.Role) ProcessEventHandlerDefinition(com.serotonin.m2m2.module.definitions.event.handlers.ProcessEventHandlerDefinition) RoleVO(com.serotonin.m2m2.vo.role.RoleVO) RoleService(com.infiniteautomation.mango.spring.service.RoleService) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) MutableObject(org.apache.commons.lang3.mutable.MutableObject) Test(org.junit.Test)

Example 19 with PermissionService

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());
    }
}
Also used : PermissionService(com.infiniteautomation.mango.spring.service.PermissionService) MockDataSourceVO(com.serotonin.m2m2.vo.dataSource.mock.MockDataSourceVO) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) MockPointLocatorVO(com.serotonin.m2m2.vo.dataPoint.MockPointLocatorVO) DataSourceService(com.infiniteautomation.mango.spring.service.DataSourceService) MutableObject(org.apache.commons.lang3.mutable.MutableObject) Test(org.junit.Test)

Example 20 with PermissionService

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());
    }
}
Also used : ProcessEventHandlerVO(com.serotonin.m2m2.vo.event.ProcessEventHandlerVO) MockDataSourceVO(com.serotonin.m2m2.vo.dataSource.mock.MockDataSourceVO) EventTypeMatcher(com.serotonin.m2m2.rt.event.type.EventTypeMatcher) DataSourceEventType(com.serotonin.m2m2.rt.event.type.DataSourceEventType) JdbcConnectionPool(org.h2.jdbcx.JdbcConnectionPool) ExtendedJdbcTemplate(com.serotonin.db.spring.ExtendedJdbcTemplate) DatabaseProxy(com.serotonin.m2m2.db.DatabaseProxy) AbstractEventHandlerVO(com.serotonin.m2m2.vo.event.AbstractEventHandlerVO) DataSource(javax.sql.DataSource) DataSourceService(com.infiniteautomation.mango.spring.service.DataSourceService) PermissionService(com.infiniteautomation.mango.spring.service.PermissionService) ProcessEventHandlerDefinition(com.serotonin.m2m2.module.definitions.event.handlers.ProcessEventHandlerDefinition) EventHandlerService(com.infiniteautomation.mango.spring.service.EventHandlerService) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) MutableObject(org.apache.commons.lang3.mutable.MutableObject) Test(org.junit.Test)

Aggregations

PermissionService (com.infiniteautomation.mango.spring.service.PermissionService)47 Test (org.junit.Test)32 Role (com.serotonin.m2m2.vo.role.Role)25 Set (java.util.Set)19 HashSet (java.util.HashSet)17 Common (com.serotonin.m2m2.Common)14 DatabaseProxy (com.serotonin.m2m2.db.DatabaseProxy)14 List (java.util.List)13 MangoTestBase (com.serotonin.m2m2.MangoTestBase)12 Collectors (java.util.stream.Collectors)12 PermissionsMinterms (com.infiniteautomation.mango.db.tables.PermissionsMinterms)10 DataSourceService (com.infiniteautomation.mango.spring.service.DataSourceService)10 RoleDao (com.serotonin.m2m2.db.dao.RoleDao)10 MockDataSourceVO (com.serotonin.m2m2.vo.dataSource.mock.MockDataSourceVO)10 Iterator (java.util.Iterator)10 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)10 MutableObject (org.apache.commons.lang3.mutable.MutableObject)10 DSLContext (org.jooq.DSLContext)10 Assert.assertEquals (org.junit.Assert.assertEquals)10 Assert.assertTrue (org.junit.Assert.assertTrue)10