This is an automated email from the ASF dual-hosted git repository.
aradzinski pushed a commit to branch NLPCRAFT-41
in repository https://gitbox.apache.org/repos/asf/incubator-nlpcraft.git
The following commit(s) were added to refs/heads/NLPCRAFT-41 by this push:
new eae496a WIP.
eae496a is described below
commit eae496a5ae917a57038aa1e4655f094fb6b356bc
Author: Aaron Radzinski <[email protected]>
AuthorDate: Fri Sep 4 23:32:30 2020 -0700
WIP.
---
.../inspections/NCInspectionService.scala} | 36 ++++---
.../server/inspections/NCInspectionManager.scala | 109 ++++++++++-----------
.../inspectors/NCInspectorSuggestions.scala | 6 +-
.../nlpcraft/server/probe/NCProbeManager.scala | 17 ++--
.../nlpcraft/server/rest/NCBasicRestApi.scala | 2 +-
5 files changed, 91 insertions(+), 79 deletions(-)
diff --git
a/nlpcraft/src/main/scala/org/apache/nlpcraft/server/inspections/inspectors/NCInspector.scala
b/nlpcraft/src/main/scala/org/apache/nlpcraft/common/inspections/NCInspectionService.scala
similarity index 66%
rename from
nlpcraft/src/main/scala/org/apache/nlpcraft/server/inspections/inspectors/NCInspector.scala
rename to
nlpcraft/src/main/scala/org/apache/nlpcraft/common/inspections/NCInspectionService.scala
index 47d8bd4..63efeb9 100644
---
a/nlpcraft/src/main/scala/org/apache/nlpcraft/server/inspections/inspectors/NCInspector.scala
+++
b/nlpcraft/src/main/scala/org/apache/nlpcraft/common/inspections/NCInspectionService.scala
@@ -1,29 +1,39 @@
-package org.apache.nlpcraft.server.inspections.inspectors
+package org.apache.nlpcraft.common.inspections
import java.util.concurrent.{ExecutorService, Executors}
import io.opencensus.trace.Span
import org.apache.nlpcraft.common.NCService
-import org.apache.nlpcraft.common.inspections.NCInspectionResult
import org.apache.nlpcraft.common.util.NCUtils
import scala.concurrent.{ExecutionContext, ExecutionContextExecutor, Future}
-// TODO: duplicated with probe component (same API) etc - should we move it
into common?
-private[inspections] trait NCInspector extends NCService {
+/**
+ * Base trait for inspection implementation.
+ */
+trait NCInspectionService extends NCService {
+ /** */
@volatile private var pool: ExecutorService = _
- @volatile protected var executor: ExecutionContextExecutor = _
+
+ /** */
+ @volatile private var executor: ExecutionContextExecutor = _
/**
- *
- * @param mdlId
- * @param inspId
- * @param args
- * @param parent
- * @return
- */
+ *
+ * @param mdlId
+ * @param inspId
+ * @param args
+ * @param parent
+ * @return
+ */
def inspect(mdlId: String, inspId: String, args: Option[String], parent:
Span = null): Future[NCInspectionResult]
+ /**
+ *
+ * @return
+ */
+ def getExecutor = executor
+
override def start(parent: Span): NCService =
startScopedSpan("start", parent) { _ ⇒
pool = Executors.newCachedThreadPool()
@@ -37,6 +47,8 @@ private[inspections] trait NCInspector extends NCService {
super.stop(parent)
NCUtils.shutdownPools(pool)
+
+ pool = null
executor = null
}
}
diff --git
a/nlpcraft/src/main/scala/org/apache/nlpcraft/server/inspections/NCInspectionManager.scala
b/nlpcraft/src/main/scala/org/apache/nlpcraft/server/inspections/NCInspectionManager.scala
index a95c841..fff9a2e 100644
---
a/nlpcraft/src/main/scala/org/apache/nlpcraft/server/inspections/NCInspectionManager.scala
+++
b/nlpcraft/src/main/scala/org/apache/nlpcraft/server/inspections/NCInspectionManager.scala
@@ -20,7 +20,7 @@ package org.apache.nlpcraft.server.inspections
import io.opencensus.trace.Span
import org.apache.nlpcraft.common.NCService
import org.apache.nlpcraft.common.inspections.impl.{NCInspectionImpl,
NCInspectionParameterImpl}
-import org.apache.nlpcraft.common.inspections.{NCInspection,
NCInspectionResult}
+import org.apache.nlpcraft.common.inspections.{NCInspection,
NCInspectionResult, NCInspectionService}
import org.apache.nlpcraft.server.inspections.inspectors.NCInspectorSuggestions
import org.apache.nlpcraft.server.probe.NCProbeManager
@@ -28,63 +28,59 @@ import scala.collection.Map
import scala.concurrent.Future
/**
- * TODO:
+ * Model inspection manager.
*/
object NCInspectionManager extends NCService {
- private final val INSPECTIONS: Seq[NCInspection] = {
- // TODO:
- Seq(
- NCInspectionImpl(
- id = "macros",
- name = "macros",
- synopsis = "macros",
- parameters = Seq.empty,
- description = "macros",
- isServerSide = false
+ private final val ALL_INSPECTIONS: Seq[NCInspection] = Seq(
+ NCInspectionImpl(
+ id = "macros",
+ name = "macros",
+ synopsis = "macros",
+ parameters = Seq.empty,
+ description = "macros",
+ isServerSide = false
+ ),
+ NCInspectionImpl(
+ id = "intents",
+ name = "intents",
+ synopsis = "intents",
+ parameters = Seq.empty,
+ description = "intents",
+ isServerSide = false
+ ),
+ NCInspectionImpl(
+ id = "synonyms",
+ name = "synonyms",
+ synopsis = "synonyms",
+ parameters = Seq.empty,
+ description = "synonyms",
+ isServerSide = false
+ ),
+ NCInspectionImpl(
+ id = "suggestions",
+ name = "suggestions",
+ synopsis = "suggestions",
+ parameters = Seq(
+ NCInspectionParameterImpl(
+ id = "minScore",
+ name = "minScore",
+ value = "minScore",
+ valueType = "double",
+ synopsis = "minScore, range between 0 and 1",
+ description = "minScore"
+ )
),
- NCInspectionImpl(
- id = "intents",
- name = "intents",
- synopsis = "intents",
- parameters = Seq.empty,
- description = "intents",
- isServerSide = false
- ),
- NCInspectionImpl(
- id = "synonyms",
- name = "synonyms",
- synopsis = "synonyms",
- parameters = Seq.empty,
- description = "synonyms",
- isServerSide = false
- ),
- NCInspectionImpl(
- id = "suggestions",
- name = "suggestions",
- synopsis = "suggestions",
- parameters = Seq(
- NCInspectionParameterImpl(
- id = "minScore",
- name = "minScore",
- value = "minScore",
- valueType = "double",
- synopsis = "minScore, range between 0 and 1",
- description = "minScore"
- )
- ),
- description = "suggestions",
- isServerSide = true
- )
+ description = "suggestions",
+ isServerSide = true
)
- }
+ )
- private final val SRV_INSPECTORS =
- Map(
- "suggestions" → NCInspectorSuggestions
- )
+ private final val SRV_INSPECTIONS = Map[String, NCInspectionService](
+ "suggestions" → NCInspectorSuggestions
+ )
override def start(parent: Span): NCService = startScopedSpan("start",
parent) { _ ⇒
- SRV_INSPECTORS.values.foreach(_.start())
+ SRV_INSPECTIONS.values.foreach(_.start())
super.start(parent)
}
@@ -92,7 +88,7 @@ object NCInspectionManager extends NCService {
override def stop(parent: Span): Unit = startScopedSpan("stop", parent) {
_ ⇒
super.stop()
- SRV_INSPECTORS.values.foreach(_.stop(parent))
+ SRV_INSPECTIONS.values.foreach(_.stop(parent))
}
/**
@@ -103,15 +99,16 @@ object NCInspectionManager extends NCService {
* @param parent Optional parent trace span.
*/
def inspect(mdlId: String, inspId: String, args: Option[String], parent:
Span = null): Future[NCInspectionResult] =
- SRV_INSPECTORS.get(inspId) match {
- case Some(inspector) ⇒ inspector.inspect(mdlId, inspId, args,
parent)
- case None ⇒ NCProbeManager.getProbeInspection(mdlId, inspId, args,
parent)
+ SRV_INSPECTIONS.get(inspId) match {
+ case Some(insp) ⇒ insp.inspect(mdlId, inspId, args, parent)
+ case None ⇒ NCProbeManager.runInspection(mdlId, inspId, args,
parent)
}
+
/**
* Gets all supported server and probe inspections.
*
* @param parent
* @return
*/
- def getInspections(parent: Span = null): Seq[NCInspection] = INSPECTIONS
+ def allInspections(parent: Span = null): Seq[NCInspection] =
ALL_INSPECTIONS
}
diff --git
a/nlpcraft/src/main/scala/org/apache/nlpcraft/server/inspections/inspectors/NCInspectorSuggestions.scala
b/nlpcraft/src/main/scala/org/apache/nlpcraft/server/inspections/inspectors/NCInspectorSuggestions.scala
index 17258ed..ec73e3d 100644
---
a/nlpcraft/src/main/scala/org/apache/nlpcraft/server/inspections/inspectors/NCInspectorSuggestions.scala
+++
b/nlpcraft/src/main/scala/org/apache/nlpcraft/server/inspections/inspectors/NCInspectorSuggestions.scala
@@ -32,7 +32,7 @@ import org.apache.http.impl.client.HttpClients
import org.apache.http.util.EntityUtils
import org.apache.nlpcraft.common.NCE
import org.apache.nlpcraft.common.config.NCConfigurable
-import org.apache.nlpcraft.common.inspections.NCInspectionResult
+import org.apache.nlpcraft.common.inspections._
import org.apache.nlpcraft.common.inspections.impl.NCInspectionResultImpl
import org.apache.nlpcraft.common.makro.NCMacroParser
import org.apache.nlpcraft.common.nlp.core.NCNlpPorterStemmer
@@ -44,7 +44,7 @@ import scala.collection.{Seq, mutable}
import scala.concurrent.{ExecutionContextExecutor, Future, Promise}
// TODO: Possible parameter 'minScore' (double 0 .. 1)
-object NCInspectorSuggestions extends NCInspector {
+object NCInspectorSuggestions extends NCInspectionService {
// For context word server requests.
private final val MAX_LIMIT: Int = 10000
private final val BATCH_SIZE = 20
@@ -471,7 +471,7 @@ object NCInspectorSuggestions extends NCInspector {
promise.failure(e)
- }(executor)
+ }(getExecutor)
promise.future
}
diff --git
a/nlpcraft/src/main/scala/org/apache/nlpcraft/server/probe/NCProbeManager.scala
b/nlpcraft/src/main/scala/org/apache/nlpcraft/server/probe/NCProbeManager.scala
index f619d7a..8a30e63 100644
---
a/nlpcraft/src/main/scala/org/apache/nlpcraft/server/probe/NCProbeManager.scala
+++
b/nlpcraft/src/main/scala/org/apache/nlpcraft/server/probe/NCProbeManager.scala
@@ -147,7 +147,7 @@ object NCProbeManager extends NCService {
@volatile private var pool: ExecutorService = _
@volatile private var isStopping: AtomicBoolean = _
- @volatile private var probeInspecs: ConcurrentHashMap[String,
Promise[NCInspectionResult]] = _
+ @volatile private var probeInsp: ConcurrentHashMap[String,
Promise[NCInspectionResult]] = _
@volatile private var modelsInfo: ConcurrentHashMap[String,
Promise[java.util.Map[String, AnyRef]]] = _
/**
@@ -169,7 +169,7 @@ object NCProbeManager extends NCService {
isStopping = new AtomicBoolean(false)
- probeInspecs = new ConcurrentHashMap[String,
Promise[NCInspectionResult]]()
+ probeInsp = new ConcurrentHashMap[String,
Promise[NCInspectionResult]]()
modelsInfo = new ConcurrentHashMap[String,
Promise[java.util.Map[String, AnyRef]]]()
pool = Executors.newFixedThreadPool(Config.poolSize)
@@ -210,7 +210,7 @@ object NCProbeManager extends NCService {
U.stopThread(dnSrv)
U.stopThread(upSrv)
- probeInspecs = null
+ probeInsp = null
modelsInfo = null
super.stop()
@@ -412,6 +412,7 @@ object NCProbeManager extends NCService {
*/
@throws[NCE]
@throws[IOException]
+ //noinspection DuplicatedCode
private def downLinkHandler(sock: NCSocket): Unit = {
// Read header token hash message.
val tokHash = sock.read[String]()
@@ -533,6 +534,7 @@ object NCProbeManager extends NCService {
*/
@throws[NCE]
@throws[IOException]
+ //noinspection DuplicatedCode
private def upLinkHandler(sock: NCSocket): Unit = {
// Read header probe token hash message.
val tokHash = sock.read[String]()
@@ -700,7 +702,7 @@ object NCProbeManager extends NCService {
typ match {
case "P2S_PING" ⇒ ()
- case "P2S_PROBE_INSPECTION" ⇒ processPromise(probeInspecs,
TYPE_INSPECTION_RESP)
+ case "P2S_PROBE_INSPECTION" ⇒ processPromise(probeInsp,
TYPE_INSPECTION_RESP)
case "P2S_MODEL_INFO" ⇒ processPromise(modelsInfo,
TYPE_MODEL_INFO_RESP)
case "P2S_ASK_RESULT" ⇒
val srvReqId = probeMsg.data[String]("srvReqId")
@@ -983,14 +985,14 @@ object NCProbeManager extends NCService {
* @param parent
* @return
*/
- def getProbeInspection(mdlId: String, inspId: String, args:
Option[String], parent: Span = null): Future[NCInspectionResult] =
- startScopedSpan("inspect", parent, "modelId" → mdlId, "inspId" →
inspId) { _ ⇒
+ def runInspection(mdlId: String, inspId: String, args: Option[String],
parent: Span = null): Future[NCInspectionResult] =
+ startScopedSpan("runInspection", parent, "modelId" → mdlId, "inspId" →
inspId) { _ ⇒
val params = mutable.HashMap.empty[String, Serializable] ++
Map("mdlId" → mdlId, "inspId" → inspId)
if (args.isDefined)
params += "args" → GSON.toJson(args.get)
- probePromise(parent, mdlId, probeInspecs, "S2P_PROBE_INSPECTION",
params.toSeq :_*)
+ probePromise(parent, mdlId, probeInsp, "S2P_PROBE_INSPECTION",
params.toSeq :_*)
}
/**
@@ -1036,6 +1038,7 @@ object NCProbeManager extends NCService {
sendToProbe(probe.probeKey, msg, parent)
promise.future
+
case None ⇒ throw new NCE(s"Probe not found for model: '$mdlId''")
}
}
diff --git
a/nlpcraft/src/main/scala/org/apache/nlpcraft/server/rest/NCBasicRestApi.scala
b/nlpcraft/src/main/scala/org/apache/nlpcraft/server/rest/NCBasicRestApi.scala
index 06fe21a..b070836 100644
---
a/nlpcraft/src/main/scala/org/apache/nlpcraft/server/rest/NCBasicRestApi.scala
+++
b/nlpcraft/src/main/scala/org/apache/nlpcraft/server/rest/NCBasicRestApi.scala
@@ -784,7 +784,7 @@ class NCBasicRestApi extends NCRestApi with LazyLogging
with NCOpenCensusTrace w
authenticateAsAdmin(req.acsTok)
- val inspections =
NCInspectionManager.getInspections(span).map(i ⇒ Inspection_Inspection$All(
+ val inspections =
NCInspectionManager.allInspections(span).map(i ⇒ Inspection_Inspection$All(
id = i.id(),
name = i.name(),
synopsis = i.synopsis(),