Search in sources :

Example 6 with DataSourceService

use of com.infiniteautomation.mango.spring.service.DataSourceService in project ma-core-public by infiniteautomation.

the class DataSourceDaoDeadlockDetection method detectDeadlockFromDataSourceDeleteDataPointInsertAndDelete.

/**
 * See the deadlock when you insert data source, then point,
 *  then delete point (outside of transaction) then delete source
 */
@Test
public void detectDeadlockFromDataSourceDeleteDataPointInsertAndDelete() {
    // Create data source
    // Add data point
    // Delete data source
    // Create data source
    // 25;
    int numThreads = 5;
    // 100;
    int numDataSources = 10;
    PermissionService permissionService = Common.getBean(PermissionService.class);
    DataSourceService dataSourceService = Common.getBean(DataSourceService.class);
    DataPointService dataPointService = Common.getBean(DataPointService.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
                        DataPointVO dp = createMockDataPoint(ds, new MockPointLocatorVO());
                        // Delete point
                        dataPointService.delete(dp.getId());
                        // Delete data source
                        dataSourceService.delete(ds.getId());
                        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 : DataPointVO(com.serotonin.m2m2.vo.DataPointVO) MockDataSourceVO(com.serotonin.m2m2.vo.dataSource.mock.MockDataSourceVO) MockPointLocatorVO(com.serotonin.m2m2.vo.dataPoint.MockPointLocatorVO) DataSourceService(com.infiniteautomation.mango.spring.service.DataSourceService) PermissionService(com.infiniteautomation.mango.spring.service.PermissionService) DataPointService(com.infiniteautomation.mango.spring.service.DataPointService) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) MutableObject(org.apache.commons.lang3.mutable.MutableObject) Test(org.junit.Test)

Example 7 with DataSourceService

use of com.infiniteautomation.mango.spring.service.DataSourceService 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)

Example 8 with DataSourceService

use of com.infiniteautomation.mango.spring.service.DataSourceService 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 9 with DataSourceService

use of com.infiniteautomation.mango.spring.service.DataSourceService in project ma-core-public by infiniteautomation.

the class DataPointEventsTest method stopAllDataSources.

@After
public void stopAllDataSources() {
    // Since we don't shutdown the runtime between tests we need to do this
    DataSourceService service = Common.getBean(DataSourceService.class);
    service.list(ds -> service.delete(ds.getId()));
}
Also used : DataSourceService(com.infiniteautomation.mango.spring.service.DataSourceService) After(org.junit.After)

Example 10 with DataSourceService

use of com.infiniteautomation.mango.spring.service.DataSourceService in project ma-modules-public by infiniteautomation.

the class MaintenanceEventType method getEventPermission.

@Override
public MangoPermission getEventPermission(Map<String, Object> context, PermissionService service) {
    DataSourceService dataSourceService = Common.getBean(DataSourceService.class);
    DataPointService dataPointService = Common.getBean(DataPointService.class);
    MaintenanceEventsService maintenanceEventService = Common.getBean(MaintenanceEventsService.class);
    Set<Role> allRequired = new HashSet<>();
    try {
        MaintenanceEventVO vo = maintenanceEventService.get(maintenanceId);
        try {
            for (int dsId : vo.getDataSources()) {
                MangoPermission read = dataSourceService.getReadPermission(dsId);
                read.getRoles().forEach(allRequired::addAll);
            }
        } catch (NotFoundException e) {
        // Ignore this item
        }
        try {
            for (int dpId : vo.getDataPoints()) {
                MangoPermission read = dataPointService.getReadPermission(dpId);
                read.getRoles().forEach(allRequired::addAll);
            }
        } catch (NotFoundException e) {
        // Ignore this item
        }
    } catch (NotFoundException e) {
    // Ignore all of it
    }
    if (allRequired.size() == 0) {
        return MangoPermission.superadminOnly();
    } else {
        return MangoPermission.requireAllRoles(allRequired);
    }
}
Also used : DataPointService(com.infiniteautomation.mango.spring.service.DataPointService) MaintenanceEventsService(com.infiniteautomation.mango.spring.service.maintenanceEvents.MaintenanceEventsService) Role(com.serotonin.m2m2.vo.role.Role) NotFoundException(com.infiniteautomation.mango.util.exception.NotFoundException) MangoPermission(com.infiniteautomation.mango.permission.MangoPermission) DataSourceService(com.infiniteautomation.mango.spring.service.DataSourceService) HashSet(java.util.HashSet)

Aggregations

DataSourceService (com.infiniteautomation.mango.spring.service.DataSourceService)17 MockDataSourceVO (com.serotonin.m2m2.vo.dataSource.mock.MockDataSourceVO)14 PermissionService (com.infiniteautomation.mango.spring.service.PermissionService)10 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)10 MutableObject (org.apache.commons.lang3.mutable.MutableObject)10 Test (org.junit.Test)10 MockPointLocatorVO (com.serotonin.m2m2.vo.dataPoint.MockPointLocatorVO)8 DataPointService (com.infiniteautomation.mango.spring.service.DataPointService)5 EventHandlerService (com.infiniteautomation.mango.spring.service.EventHandlerService)4 ExtendedJdbcTemplate (com.serotonin.db.spring.ExtendedJdbcTemplate)4 DatabaseProxy (com.serotonin.m2m2.db.DatabaseProxy)4 ProcessEventHandlerDefinition (com.serotonin.m2m2.module.definitions.event.handlers.ProcessEventHandlerDefinition)4 DataSourceEventType (com.serotonin.m2m2.rt.event.type.DataSourceEventType)4 EventTypeMatcher (com.serotonin.m2m2.rt.event.type.EventTypeMatcher)4 DataPointVO (com.serotonin.m2m2.vo.DataPointVO)4 AbstractEventHandlerVO (com.serotonin.m2m2.vo.event.AbstractEventHandlerVO)4 ProcessEventHandlerVO (com.serotonin.m2m2.vo.event.ProcessEventHandlerVO)4 DataSource (javax.sql.DataSource)4 JdbcConnectionPool (org.h2.jdbcx.JdbcConnectionPool)4 Role (com.serotonin.m2m2.vo.role.Role)3