Skip to content

Commit

Permalink
Merge pull request #415 from SCADA-LTS/feature/#410_Expand_the_cache_…
Browse files Browse the repository at this point in the history
…DataPointWithDataSource1

#410 Correcting DataSourcePointsCache
  • Loading branch information
grzesiekb authored Nov 2, 2017
2 parents 70ffa62 + 28c4f03 commit 45992b1
Show file tree
Hide file tree
Showing 11 changed files with 239 additions and 157 deletions.
12 changes: 11 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -77,4 +77,14 @@ seroUtils.jar
###########################
# docker
###########################
docker/app/**
docker/app/**

###########################
# intelij
###########################
.idea

###########################
# jrebel
###########################
catalina-jrebel.bat
5 changes: 3 additions & 2 deletions WebContent/WEB-INF/classes/env.properties
Original file line number Diff line number Diff line change
Expand Up @@ -66,8 +66,9 @@ abilit.MILLIS_SECONDS_PERIOD_UPDATE_EVENT_DETECTORS=1000
abilit.MILLIS_SECONDS_PERIOD_UPDATE_PENDING_EVENTS=1000
abilit.CRONE_UPDATE_CACHE_POINT_HIERARCHY=0 0/10 * * * ?



# every minute
abilit.CRONE_UPDATE_DATA_SOURCES_POINTS=0 0/1 * * * ?
abilit.USE_CACHE_DATA_SOURCES_POINTS_WHEN_THE_SYSTEM_IS_READY=true

#security.hashAlgorithm=NONE
#grove.url=http://mango.serotoninsoftware.com/servlet
8 changes: 5 additions & 3 deletions build.xml
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,9 @@
update="true"/>
</target>

<target name="copyToDeploy" >
<target name="clearAndCopyToDeploy" >
<delete file="${env.CATALINA_HOME}/webapps/ScadaBR.war"></delete>
<delete dir="${env.CATALINA_HOME}/webapps/ScadaBR"></delete>
<copy file="ScadaBR.war" todir="${env.CATALINA_HOME}/webapps" overwrite="true">
</copy>
</target>
Expand Down Expand Up @@ -278,11 +280,11 @@
haltonfailure="yes">
</test>
-->
<test name="org.scada_lts.cached.DataPointsCacheComposeDataTest"
<test name="org.scada_lts.cached.DataPointsCacheComposeDataSourcesDataTest"
haltonfailure="yes">
</test>

<test name="org.scada_lts.cached.DataPointsCacheMockitoTest"
<test name="org.scada_lts.cached.DataSourcePointsCacheMockitoTest"
haltonfailure="yes">
</test>

Expand Down
57 changes: 27 additions & 30 deletions src/com/serotonin/mango/MangoContextListener.java
Original file line number Diff line number Diff line change
Expand Up @@ -19,32 +19,7 @@

package com.serotonin.mango;

import java.io.File;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.concurrent.SynchronousQueue;
import java.util.concurrent.ThreadPoolExecutor;
import java.util.concurrent.TimeUnit;

import javax.servlet.ServletContext;
import javax.servlet.ServletContextEvent;
import javax.servlet.ServletContextListener;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.mozilla.javascript.ContextFactory;
import org.scada_lts.cache.DataPointsCache;
import org.scada_lts.cache.EventDetectorsCache;
import org.scada_lts.cache.PointHierarchyCache;
import org.scada_lts.cache.ViewHierarchyCache;
import org.scada_lts.dao.SystemSettingsDAO;
import org.scada_lts.mango.adapter.MangoScadaConfig;
import org.scada_lts.scripting.SandboxContextFactory;

import br.org.scadabr.api.utils.APIUtils;
import com.serotonin.ShouldNeverHappenException;
import com.serotonin.mango.db.DatabaseAccess;
import com.serotonin.mango.db.dao.DataPointDao;
Expand Down Expand Up @@ -75,13 +50,35 @@
import com.serotonin.mango.web.dwr.BaseDwr;
import com.serotonin.util.StringUtils;
import com.serotonin.web.i18n.LocalizableMessage;

import br.org.scadabr.api.utils.APIUtils;
import freemarker.cache.FileTemplateLoader;
import freemarker.cache.MultiTemplateLoader;
import freemarker.cache.TemplateLoader;
import freemarker.template.Configuration;
import freemarker.template.DefaultObjectWrapper;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.mozilla.javascript.ContextFactory;
import org.scada_lts.cache.DataSourcePointsCache;
import org.scada_lts.cache.EventDetectorsCache;
import org.scada_lts.cache.PointHierarchyCache;
import org.scada_lts.cache.ViewHierarchyCache;
import org.scada_lts.dao.SystemSettingsDAO;
import org.scada_lts.mango.adapter.MangoScadaConfig;
import org.scada_lts.scripting.SandboxContextFactory;

import javax.servlet.ServletContext;
import javax.servlet.ServletContextEvent;
import javax.servlet.ServletContextListener;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.concurrent.SynchronousQueue;
import java.util.concurrent.ThreadPoolExecutor;
import java.util.concurrent.TimeUnit;

public class MangoContextListener implements ServletContextListener {
private final Log log = LogFactory.getLog(MangoContextListener.class);
Expand Down Expand Up @@ -132,15 +129,15 @@ public void contextInitialized(ServletContextEvent evt) {
}

try {
DataPointsCache.getInstance().cacheInitialize();
DataSourcePointsCache.getInstance().cacheInitialize();
log.info("Cache data points initialized");

runtimeManagerInitialize(ctx);

} catch (Exception e) {
log.error(e);
} finally {
DataPointsCache.getInstance().cacheFinalized();
DataSourcePointsCache.getInstance().cacheFinalized();
}


Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,13 +18,6 @@
*/
package com.serotonin.mango.rt.dataSource.modbus;

import java.util.ArrayList;
import java.util.List;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.scada_lts.cache.DataPointsCache;

import com.serotonin.mango.Common;
import com.serotonin.mango.db.dao.DataPointDao;
import com.serotonin.mango.rt.dataImage.DataPointRT;
Expand All @@ -38,6 +31,12 @@
import com.serotonin.modbus4j.ModbusFactory;
import com.serotonin.modbus4j.ModbusMaster;
import com.serotonin.modbus4j.ip.IpParameters;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.scada_lts.cache.DataSourcePointsCache;

import java.util.ArrayList;
import java.util.List;

public class ModbusIpDataSource extends ModbusDataSource {
private final ModbusIpDataSourceVO configuration;
Expand Down Expand Up @@ -69,8 +68,8 @@ public void addDataPoint(DataPointRT dataPoint) {
boolean found = false;

List<DataPointVO> points;
if (DataPointsCache.getInstance().isStart()) {
points = DataPointsCache.getInstance().getDataPoints((long) configuration.getId());
if (DataSourcePointsCache.getInstance().isCacheEnabled()) {
points = DataSourcePointsCache.getInstance().getDataPoints((long) configuration.getId());
} else {
points = dataPointDao.getDataPoints(
configuration.getId(), null);
Expand Down
81 changes: 0 additions & 81 deletions src/org/scada_lts/cache/DataPointsCache.java

This file was deleted.

120 changes: 120 additions & 0 deletions src/org/scada_lts/cache/DataSourcePointsCache.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,120 @@
package org.scada_lts.cache;

import com.serotonin.mango.vo.DataPointVO;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.quartz.*;
import org.quartz.impl.StdSchedulerFactory;
import org.scada_lts.config.ScadaConfig;
import org.scada_lts.dao.DataPointDAO;

import java.io.IOException;
import java.text.ParseException;
import java.util.*;

public class DataSourcePointsCache implements IDataPointsCacheWhenStart {

private static final Log LOG = LogFactory.getLog(DataSourcePointsCache.class);

private boolean cacheEnabled = false;

private static DataSourcePointsCache instance = null;

private Map<Long, List<DataPointVO>> dss = new TreeMap<>();

private DataSourcePointsCache() {

}

public static DataSourcePointsCache getInstance() {
if (instance==null) {
instance = new DataSourcePointsCache();
}
return instance;
}

@Override
public List<DataPointVO> getDataPoints(Long dataSourceId) {

LOG.info("I'm using a cache with datasources points");

if (cacheEnabled) {
return dss.get(dataSourceId);
} else {
throw new RuntimeException("Cache may work only when scada cacheEnabled");
}
}

public void setData(Map<Long, List<DataPointVO>> dss) {
this.dss = dss;
}

@Override
public void cacheFinalized() {
try {
if (ScadaConfig.getInstance().getBoolean(ScadaConfig.USE_CACHE_DATA_SOURCES_POINTS_WHEN_THE_SYSTEM_IS_READY, false)) {
cronInitialize();
cacheEnabled = true;
} else {
cacheEnabled = false;
instance = null;
}
} catch (IOException | SchedulerException | ParseException e) {
LOG.error(e);
}
}

public boolean isCacheEnabled() {
return cacheEnabled;
}

@Override
public void cacheInitialize() {

List<DataPointVO> dps = new DataPointDAO().getDataPoints();

dss = composeCashData(dps);

cacheEnabled = true;
}

public Map<Long, List<DataPointVO>> composeCashData(List<DataPointVO> dps) {

Map<Long, List<DataPointVO>> dss = new TreeMap<>();
if (dps != null && dps.size()>0) {
for (DataPointVO dp : dps) {
List<DataPointVO> cacheDs = dss.get((long)dp.getDataSourceId());
if (cacheDs==null) {
cacheDs = new ArrayList<>();
cacheDs.add(dp);
dss.put((long) dp.getDataSourceId(), cacheDs);
} else {
cacheDs.add(dp);
}
}
}
return dss;
}

private void cronInitialize() throws java.io.IOException, SchedulerException, ParseException {

if (LOG.isTraceEnabled()) {
LOG.trace("cacheInitialize");
}

JobDetail job = new JobDetail();
job.setName("UpdateDataSourcesPoints");
job.setJobClass(UpdateDataSourcesPoints.class);


CronTrigger trigger = new CronTrigger();
trigger.setName("Quartz - trigger-DataSourcePointsCache");
String cronExpression = ScadaConfig.getInstance().getProperty(ScadaConfig.CRONE_UPDATE_CACHE_DATA_SOURCES_POINTS);
trigger.setCronExpression(cronExpression);//"0 15 1 ? * *"
Scheduler scheduler = new StdSchedulerFactory().getScheduler();
scheduler.start();
scheduler.scheduleJob(job, trigger);

}

}
Loading

0 comments on commit 45992b1

Please sign in to comment.