plc4x-dev mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From GitBox <...@apache.org>
Subject [GitHub] [incubator-plc4x] JulianFeinauer commented on a change in pull request #46: PLC4X-88: Add Triggering to PLC Scraper
Date Wed, 06 Mar 2019 09:12:50 GMT
JulianFeinauer commented on a change in pull request #46: PLC4X-88: Add Triggering to PLC Scraper
URL: https://github.com/apache/incubator-plc4x/pull/46#discussion_r262823550
 
 

 ##########
 File path: plc4j/utils/scraper/src/main/java/org/apache/plc4x/java/scraper/triggeredscraper/TriggeredScraperImpl.java
 ##########
 @@ -0,0 +1,186 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License.  You may obtain a copy of the License at
+ *
+ *   http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied.  See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+
+package org.apache.plc4x.java.scraper.triggeredscraper;
+
+import org.apache.commons.collections4.MultiValuedMap;
+import org.apache.commons.collections4.multimap.ArrayListValuedHashMap;
+import org.apache.commons.lang3.Validate;
+import org.apache.commons.lang3.concurrent.BasicThreadFactory;
+import org.apache.commons.lang3.tuple.Triple;
+import org.apache.commons.math3.stat.descriptive.DescriptiveStatistics;
+import org.apache.commons.pool2.impl.GenericKeyedObjectPool;
+import org.apache.commons.pool2.impl.GenericKeyedObjectPoolConfig;
+import org.apache.plc4x.java.PlcDriverManager;
+import org.apache.plc4x.java.api.PlcConnection;
+import org.apache.plc4x.java.scraper.*;
+import org.apache.plc4x.java.scraper.exception.ScraperException;
+import org.apache.plc4x.java.scraper.config.triggeredscraper.TriggeredScraperConfiguration;
+import org.apache.plc4x.java.scraper.util.PercentageAboveThreshold;
+import org.apache.plc4x.java.utils.connectionpool.PooledPlcDriverManager;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+import java.util.List;
+import java.util.Locale;
+import java.util.Map;
+import java.util.concurrent.*;
+
+/**
+ * replaces the old Scraper that only could do scheduled scraping jobs
+ * Triggers have been introduced, so that in configuration "scrapeTime" has been exchanged
by "triggerConfig"
+ *
+ * Some example:
+ *   - 200ms scheduling is now performed by "triggerConfig: (SCHEDULED,200)" in scraper-configuration
+ *   - a triggered S7 variable can be used as follows:
+ *     "triggerConfig: (S7_TRIGGER_VAR,10,(%M0.3:BOOL)==(true))" meaning that Boolean in
Marker-Block in Byte-Offset 0, Bit-Offset 3 is scanned every 10ms, when trigger has a rising-edge
the acquirement of data-block is triggered
+ *     the trigger variable must be a valid address as defined with PLC4X-S7-Driver
+ *     right now boolean variables as well as numeric variables could be used as data-types
+ *     available comparators are ==,!= for all data-types and &gt;,&gt;=,&lt;,&lt;=
for numeric data-types
+ */
+public class TriggeredScraperImpl implements Scraper {
+
+    private static final Logger LOGGER = LoggerFactory.getLogger(TriggeredScraperImpl.class);
+
+    private final ScheduledExecutorService scheduler = Executors.newScheduledThreadPool(10,
+        new BasicThreadFactory.Builder()
+            .namingPattern("scheduler-thread-%d")
+            .daemon(false)
+            .build()
+    );
+    private final ExecutorService handlerPool = Executors.newFixedThreadPool(4,
+        new BasicThreadFactory.Builder()
+            .namingPattern("handler-thread-%d")
+            .daemon(true)
+            .build()
+    );
+
+    private final ResultHandler resultHandler;
+
+    private final MultiValuedMap<ScrapeJob, ScraperTask> tasks = new ArrayListValuedHashMap<>();
+    private final MultiValuedMap<ScraperTask, ScheduledFuture<?>> futures = new
ArrayListValuedHashMap<>();
+    private final PlcDriverManager driverManager;
+    private final List<ScrapeJob> jobs;
+
+    /**
+     * Creates a Scraper instance from a configuration.
+     * By default a {@link PooledPlcDriverManager} is used.
+     * @param config Configuration to use.
+     * @param resultHandler
+     */
+    public TriggeredScraperImpl(TriggeredScraperConfiguration config, ResultHandler resultHandler)
throws ScraperException {
+        this(resultHandler, createPooledDriverManager(), config.getJobs());
+    }
+
+    /**
+     * Min Idle per Key is set to 1 for situations where the network is broken.
+     * Then, on reconnect we can fail all getConnection calls (in the ScraperTask) fast until
+     * (in the background) the idle connection is created and the getConnection call returns
fast.
+     */
+    private static PooledPlcDriverManager createPooledDriverManager() {
+        return new PooledPlcDriverManager(pooledPlcConnectionFactory -> {
+            GenericKeyedObjectPoolConfig<PlcConnection> poolConfig = new GenericKeyedObjectPoolConfig<>();
+            poolConfig.setMinIdlePerKey(1);  // This should avoid problems with long running
connect attempts??
+            poolConfig.setTestOnBorrow(true);
+            poolConfig.setTestOnReturn(true);
+            return new GenericKeyedObjectPool<>(pooledPlcConnectionFactory, poolConfig);
+        });
+    }
+
+
+    public TriggeredScraperImpl(ResultHandler resultHandler, PlcDriverManager driverManager,
List<ScrapeJob> jobs) {
+        this.resultHandler = resultHandler;
+        Validate.notEmpty(jobs);
+        this.driverManager = driverManager;
+        this.jobs = jobs;
+    }
+
+    /**
+     * Start the scraping.
+     */
+    @Override
+    public void start() {
+        // Schedule all jobs
+        LOGGER.info("Starting jobs...");
+        jobs.stream()
+            .flatMap(job -> job.getSourceConnections().entrySet().stream()
+                .map(entry -> Triple.of(job, entry.getKey(), entry.getValue()))
+            )
+            .forEach(
+                tuple -> {
 
 Review comment:
   This part here is sooo dirty (was done by me so... no offense). Could you add a todo that
we need to refactor this and add some more tests?

----------------------------------------------------------------
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
 
For queries about this service, please contact Infrastructure at:
users@infra.apache.org


With regards,
Apache Git Services

Mime
View raw message