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());
}
}
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());
}
}
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());
}
}
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()));
}
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);
}
}
Aggregations