You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@nlpcraft.apache.org by ar...@apache.org on 2020/12/03 05:14:25 UTC

[incubator-nlpcraft] branch NLPCRAFT-170 updated: WIP.

This is an automated email from the ASF dual-hosted git repository.

aradzinski pushed a commit to branch NLPCRAFT-170
in repository https://gitbox.apache.org/repos/asf/incubator-nlpcraft.git


The following commit(s) were added to refs/heads/NLPCRAFT-170 by this push:
     new c2de9d8  WIP.
c2de9d8 is described below

commit c2de9d8c36a9ddad6d887c3a30818610fef0835b
Author: Aaron Radzinski <ar...@datalingvo.com>
AuthorDate: Wed Dec 2 21:14:14 2020 -0800

    WIP.
---
 .../model/tools/cmdline/NCCliProbeBeacon.scala     | 38 +++++++++++
 .../org/apache/nlpcraft/probe/NCProbeBoot.scala    | 75 +++++++++++++++++++++-
 .../org/apache/nlpcraft/server/NCServer.scala      |  1 +
 3 files changed, 111 insertions(+), 3 deletions(-)

diff --git a/nlpcraft/src/main/scala/org/apache/nlpcraft/model/tools/cmdline/NCCliProbeBeacon.scala b/nlpcraft/src/main/scala/org/apache/nlpcraft/model/tools/cmdline/NCCliProbeBeacon.scala
new file mode 100644
index 0000000..8730016
--- /dev/null
+++ b/nlpcraft/src/main/scala/org/apache/nlpcraft/model/tools/cmdline/NCCliProbeBeacon.scala
@@ -0,0 +1,38 @@
+/*
+ * 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.nlpcraft.model.tools.cmdline
+
+/**
+ *
+ * @param pid
+ * @param id
+ * @param token
+ * @param upLink
+ * @param downLink
+ * @param jarsFolder
+ * @param models
+ */
+case class NCCliProbeBeacon (
+    pid: Long,
+    id: String,
+    token: String,
+    upLink: String,
+    downLink: String,
+    jarsFolder: String,
+    models: String
+)
diff --git a/nlpcraft/src/main/scala/org/apache/nlpcraft/probe/NCProbeBoot.scala b/nlpcraft/src/main/scala/org/apache/nlpcraft/probe/NCProbeBoot.scala
index 2de029f..1411fb4 100644
--- a/nlpcraft/src/main/scala/org/apache/nlpcraft/probe/NCProbeBoot.scala
+++ b/nlpcraft/src/main/scala/org/apache/nlpcraft/probe/NCProbeBoot.scala
@@ -17,11 +17,13 @@
 
 package org.apache.nlpcraft.probe
 
+import java.io._
 import java.util.concurrent.CompletableFuture
 
 import com.typesafe.config.{Config, ConfigFactory, ConfigValueFactory}
 import com.typesafe.scalalogging.LazyLogging
 import io.opencensus.trace.Span
+import org.apache.commons.lang3.SystemUtils
 import org.apache.nlpcraft.common.ascii.NCAsciiTable
 import org.apache.nlpcraft.common.config.NCConfigurable
 import org.apache.nlpcraft.common.nlp.core.NCNlpCoreManager
@@ -49,6 +51,8 @@ import org.apache.nlpcraft.probe.mgrs.nlp.enrichers.stopword.NCStopWordEnricher
 import org.apache.nlpcraft.probe.mgrs.nlp.enrichers.suspicious.NCSuspiciousNounsEnricher
 import org.apache.nlpcraft.probe.mgrs.nlp.validate.NCValidateManager
 import org.apache.nlpcraft.common._
+import org.apache.nlpcraft.model.tools.cmdline.NCCliProbeBeacon
+import resource.managed
 
 import scala.collection.JavaConverters._
 import scala.collection.mutable
@@ -59,7 +63,9 @@ import scala.util.control.Exception.{catching, ignoring}
   * Probe loader.
   */
 private [probe] object NCProbeBoot extends LazyLogging with NCOpenCensusTrace {
-    private final val executionStart = System.currentTimeMillis()
+    private final val BEACON_PATH = ".nlpcraft/probe_beacon"
+
+    private final val execStart = System.currentTimeMillis()
 
     private val startedMgrs = mutable.Buffer.empty[NCService]
 
@@ -169,7 +175,7 @@ private [probe] object NCProbeBoot extends LazyLogging with NCOpenCensusTrace {
       * @param fut
       */
     private def start0(cfg: ProbeConfig, fut: CompletableFuture[Integer]): Unit = {
-        // Check version.
+        // Record an anonymous screenview.
         new Thread() {
             override def run(): Unit = U.gaScreenView("probe")
         }
@@ -189,6 +195,9 @@ private [probe] object NCProbeBoot extends LazyLogging with NCOpenCensusTrace {
                 fut.complete(1)
             
             case _ ⇒ // Managers started OK.
+                // Store beacon file once all managers started OK.
+                storeBeacon(cfg)
+
                 shutdownHook = new Thread() {
                     override def run(): Unit = {
                         logger.info("Executing shutdown hook...")
@@ -216,6 +225,66 @@ private [probe] object NCProbeBoot extends LazyLogging with NCOpenCensusTrace {
     
         logger.trace("Probe thread stopped OK.")
     }
+
+    /**
+     *
+     * @param cfg
+     */
+    private def storeBeacon(cfg: ProbeConfig): Unit = {
+        val path = new File(SystemUtils.getUserHome, BEACON_PATH)
+
+        /**
+         *
+         */
+        def save() = {
+            try {
+                managed(new ObjectOutputStream(new FileOutputStream(path))) acquireAndGet { stream ⇒
+                    stream.writeObject(NCCliProbeBeacon(
+                        pid = ProcessHandle.current().pid(),
+                        id = cfg.id,
+                        token = cfg.token,
+                        upLink = s"${cfg.upLink._1}:${cfg.upLink._2}",
+                        downLink = s"${cfg.downLink._1}:${cfg.downLink._2}",
+                        jarsFolder = cfg.jarsFolder.orNull,
+                        models = cfg.models
+                    ))
+
+                    stream.flush()
+                }
+
+                // Make sure beacon is deleted when server process exits.
+                path.deleteOnExit()
+            }
+            catch {
+                case e: IOException ⇒ U.prettyError(logger, "Failed to save probe beacon.", e)
+            }
+        }
+
+        if (path.exists())
+            catching(classOf[IOException]) either {
+                managed(new ObjectInputStream(new FileInputStream(path))) acquireAndGet { _.readObject() }
+            } match {
+                case Left(e) ⇒
+                    logger.trace(s"Failed to read existing probe beacon: ${path.getAbsolutePath}", e)
+                    logger.trace(s"Overriding failed probe beacon: ${path.getAbsolutePath}")
+
+                    save()
+
+                case Right(rawObj) ⇒
+                    val beacon = rawObj.asInstanceOf[NCCliProbeBeacon]
+
+                    if (ProcessHandle.of(beacon.pid).isPresent)
+                        logger.error(s"Cannot save probe beacon file as another live local probe detected [pid=${beacon.pid}]")
+                    else {
+                        logger.trace(s"Overriding probe beacon for a phantom process [pid=${beacon.pid}]")
+
+                        save()
+                    }
+            }
+        else
+            // No existing beacon file detected.
+            save()
+    }
     
     /**
       * 
@@ -386,7 +455,7 @@ private [probe] object NCProbeBoot extends LazyLogging with NCOpenCensusTrace {
       * Asks server start.
       */
     private def ackStart() {
-        val dur = s"[${U.format((currentTime - executionStart) / 1000.0, 2)} sec]"
+        val dur = s"[${U.format((currentTime - execStart) / 1000.0, 2)} sec]"
         
         val tbl = NCAsciiTable()
         
diff --git a/nlpcraft/src/main/scala/org/apache/nlpcraft/server/NCServer.scala b/nlpcraft/src/main/scala/org/apache/nlpcraft/server/NCServer.scala
index 52b47bd..2c8105e 100644
--- a/nlpcraft/src/main/scala/org/apache/nlpcraft/server/NCServer.scala
+++ b/nlpcraft/src/main/scala/org/apache/nlpcraft/server/NCServer.scala
@@ -285,6 +285,7 @@ object NCServer extends App with NCIgniteInstance with LazyLogging with NCOpenCe
                         beaconPath = path.getAbsolutePath,
                         startMs = currentTime
                     ))
+
                     stream.flush()
                 }