Skip to content

Commit

Permalink
#2748 Meta Data Sources run at the end during context initialization …
Browse files Browse the repository at this point in the history
…- queuing the order in which points from the context are included; refactor logging start application
  • Loading branch information
Limraj committed Nov 26, 2023
1 parent 193a4ec commit 29b028b
Show file tree
Hide file tree
Showing 2 changed files with 109 additions and 27 deletions.
10 changes: 8 additions & 2 deletions src/com/serotonin/mango/MangoContextListener.java
Original file line number Diff line number Diff line change
Expand Up @@ -176,13 +176,19 @@ private void initialized(ServletContextEvent evt) {
} catch (Exception e) {
log.error(e.getMessage(), e);
}

try {
ApplicationBeans.getViewDaoBean().init();
log.info("Cache views initialized");
} catch (Exception e) {
log.error(e.getMessage(), e);
}

try {
ViewHierarchyCache.getInstance();
log.info("Cache views hierarchy initialized");
} catch (Exception e) {
log.error(e.getMessage(), e);
log.error(e);
}

initSchedule();
Expand Down
126 changes: 101 additions & 25 deletions src/com/serotonin/mango/rt/RuntimeManager.java
Original file line number Diff line number Diff line change
Expand Up @@ -23,14 +23,18 @@
import java.util.concurrent.CopyOnWriteArrayList;
import java.util.stream.Collectors;

import com.serotonin.db.IntValuePair;
import com.serotonin.mango.db.dao.*;
import com.serotonin.mango.rt.dataImage.*;
import com.serotonin.mango.rt.event.*;
import com.serotonin.mango.rt.event.schedule.ResetDailyLimitSendingEventRT;
import com.serotonin.mango.rt.event.schedule.ScheduledExecuteInactiveEventRT;
import com.serotonin.mango.util.LoggingUtils;
import com.serotonin.mango.view.event.NoneEventRenderer;
import com.serotonin.mango.vo.User;
import com.serotonin.mango.vo.dataSource.PointLocatorVO;
import com.serotonin.mango.vo.dataSource.http.ICheckReactivation;
import com.serotonin.mango.vo.dataSource.meta.MetaPointLocatorVO;
import com.serotonin.mango.vo.mailingList.MailingList;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
Expand Down Expand Up @@ -117,7 +121,7 @@ public class RuntimeManager {
private final Map<Integer, ResetDailyLimitSendingEventRT> resetDailyLimitSentEmails = new ConcurrentHashMap<>();


private boolean started = false;
private volatile boolean started = false;

//
// Lifecycle
Expand All @@ -126,9 +130,6 @@ synchronized public void initialize(boolean safe) {
throw new ShouldNeverHappenException(
"RuntimeManager already started");

// Set the started indicator to true.
started = true;

ScheduledExecuteInactiveEventService service = ScheduledExecuteInactiveEventService.getInstance();
MailingListService mailingListService = new MailingListService();
List<MailingList> mailingLists = mailingListService.getMailingLists();
Expand All @@ -152,13 +153,27 @@ synchronized public void initialize(boolean safe) {
}

// Initialize data sources that are enabled.
DataSourceService dataSourceService = new DataSourceService();
List<DataSourceVO<?>> configs = dataSourceService.getDataSources();
List<DataSourceVO<?>> pollingRound = new ArrayList<>();
List<DataSourceVO<?>> nonMetaDataSources = configs.stream().filter(dataSource -> dataSource.getType() != DataSourceVO.Type.META).collect(Collectors.toList());
List<DataSourceVO<?>> metaDataSources = configs.stream().filter(dataSource -> dataSource.getType() == DataSourceVO.Type.META).collect(Collectors.toList());
initializeDataSources(safe, dataSourceService, nonMetaDataSources, pollingRound);
initializeDataSources(safe, dataSourceService, metaDataSources, pollingRound);
DataSourceDao dataSourceDao = new DataSourceDao();
List<DataSourceVO<?>> configs = dataSourceDao.getDataSources();
List<DataSourceVO<?>> pollingRound = new ArrayList<DataSourceVO<?>>();
for (DataSourceVO<?> config : configs) {

boolean isCheckToTrayEnableRun = (config instanceof ICheckReactivation);
boolean isToTrayEnable = false;
if (isCheckToTrayEnableRun) {
isToTrayEnable = ((ICheckReactivation) config).checkToTrayEnable();
}

if (config.isEnabled() || isToTrayEnable ) {
if (safe) {
config.setEnabled(false);
dataSourceDao.saveDataSource(config);
} else if (initializeDataSource(config))
pollingRound.add(config);
}
}

startPoints();

// Set up point links.
PointLinkDao pointLinkDao = new PointLinkDao();
Expand Down Expand Up @@ -232,6 +247,9 @@ synchronized public void initialize(boolean safe) {
startMaintenanceEvent(vo);
}
}

// Set the started indicator to true.
started = true;
}

synchronized public void terminate() {
Expand Down Expand Up @@ -356,7 +374,7 @@ private boolean initializeDataSource(DataSourceVO<?> vo) {
List<DataPointVO> dataSourcePoints = new DataPointDao()
.getDataPoints(vo.getId(), null);
for (DataPointVO dataPoint : dataSourcePoints) {
if (dataPoint.isEnabled())
if (dataPoint.isEnabled() && started)
startDataPointSafe(dataPoint);
}

Expand Down Expand Up @@ -467,6 +485,8 @@ private void startDataPoint(DataPointVO vo) {

// Add/update it in the data source.
ds.addDataPoint(dataPoint);

LOG.info("Data point '" + vo.getExtendedName() + "' initialized");
}
}
}
Expand Down Expand Up @@ -1046,22 +1066,78 @@ private void stopResetDailyLimitSentEmails(int mailingListId) {
resetDailyLimitSentEmails.remove(mailingListId);
}

private void initializeDataSources(boolean safe, DataSourceService dataSourceService,
List<DataSourceVO<?>> configs, List<DataSourceVO<?>> pollingRound) {
for (DataSourceVO<?> config : configs) {
public boolean isStarted() {
return started;
}

boolean isCheckToTrayEnableRun = (config instanceof ICheckReactivation);
boolean isToTrayEnable = false;
if (isCheckToTrayEnableRun) {
isToTrayEnable = ((ICheckReactivation) config).checkToTrayEnable();
private void startPoints() {

DataPointService dataPointService = new DataPointService();
List<DataPointVO> allPoints = dataPointService.getDataPoints(null, true);
List<DataPointVO> nonMetaDataPoints = allPoints.stream()
.filter(a -> !(a.getPointLocator() instanceof MetaPointLocatorVO))
.collect(Collectors.toList());
List<DataPointVO> metaDataPoints = allPoints.stream()
.filter(a -> a.getPointLocator() instanceof MetaPointLocatorVO)
.collect(Collectors.toList());

run(nonMetaDataPoints);

List<DataPointVO> toRunning = new ArrayList<>();
Set<Integer> toCheck = new HashSet<>();
int safe = 10;
for(DataPointVO dataPoint: metaDataPoints) {
collectMetaDataPointsFromContext(toCheck, toRunning, dataPoint, safe, allPoints);
}

run(toRunning);
run(metaDataPoints);
}

private void run(List<DataPointVO> dataPoints) {
for(DataPointVO dataPoint: dataPoints) {
if(dataPoint.isEnabled()) {
DataPointRT dataPointRT = getDataPoint(dataPoint.getId());
if(dataPointRT == null) {
startDataPointSafe(dataPoint);
}
}
}
}

if (config.isEnabled() || isToTrayEnable ) {
if (safe) {
config.setEnabled(false);
dataSourceService.saveDataSource(config);
} else if (initializeDataSource(config))
pollingRound.add(config);
private void collectMetaDataPointsFromContext(Set<Integer> toCheck, List<DataPointVO> toRunning,
DataPointVO dataPoint, int safe,
List<DataPointVO> allPoints) {
if(safe < 0) {
LOG.error("Recursion level exceeded: " + LoggingUtils.dataPointInfo(dataPoint));
return;
}
if(dataPoint.isEnabled()) {
PointLocatorVO pointLocator = dataPoint.getPointLocator();
if(pointLocator instanceof MetaPointLocatorVO) {
MetaPointLocatorVO metaPointLocator = (MetaPointLocatorVO) pointLocator;
if (metaPointLocator.getContext() != null && !metaPointLocator.getContext().isEmpty()) {
for (IntValuePair intValuePair : metaPointLocator.getContext()) {
if(intValuePair.getKey() > 0) {
DataPointRT dataPointRT = getDataPoint(intValuePair.getKey());
if (dataPointRT == null) {
DataPointVO fromContextDataPoint = allPoints.stream()
.filter(a -> a.getId() == intValuePair.getKey())
.findAny()
.orElse(null);
if (fromContextDataPoint != null) {
if (fromContextDataPoint.getPointLocator() instanceof MetaPointLocatorVO) {
collectMetaDataPointsFromContext(toCheck, toRunning, fromContextDataPoint, --safe, allPoints);
}
if (!toCheck.contains(fromContextDataPoint.getId())) {
toCheck.add(fromContextDataPoint.getId());
toRunning.add(fromContextDataPoint);
}
}
}
}
}
}
}
}
}
Expand Down

0 comments on commit 29b028b

Please sign in to comment.