You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@nlpcraft.apache.org by se...@apache.org on 2021/01/20 14:36:22 UTC
[incubator-nlpcraft] 02/02: WIP.
This is an automated email from the ASF dual-hosted git repository.
sergeykamov pushed a commit to branch NLPCRAFT-111
in repository https://gitbox.apache.org/repos/asf/incubator-nlpcraft.git
commit 2f1a98e2f5cb143f9ac7b84952c73991391018ef
Author: Sergey Kamov <sk...@gmail.com>
AuthorDate: Wed Jan 20 17:35:58 2021 +0300
WIP.
---
.../nlpcraft/common/pool/NCPoolManager.scala | 82 ++++++++++++++++++++++
.../apache/nlpcraft/common/pool/NcPoolFactory.java | 7 ++
.../probe/mgrs/pool/NCProbePoolContext.scala | 27 +++++++
.../probe/mgrs/pool/NCProbePoolManager.scala | 26 +++++++
.../nlpcraft/server/pool/NCServerPoolContext.scala | 27 +++++++
.../nlpcraft/server/pool/NCServerPoolManager.scala | 25 +++++++
6 files changed, 194 insertions(+)
diff --git a/nlpcraft/src/main/scala/org/apache/nlpcraft/common/pool/NCPoolManager.scala b/nlpcraft/src/main/scala/org/apache/nlpcraft/common/pool/NCPoolManager.scala
new file mode 100644
index 0000000..19a9d09
--- /dev/null
+++ b/nlpcraft/src/main/scala/org/apache/nlpcraft/common/pool/NCPoolManager.scala
@@ -0,0 +1,82 @@
+/*
+ * 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.common.pool
+
+import io.opencensus.trace.Span
+import org.apache.nlpcraft.common.config.NCConfigurable
+import org.apache.nlpcraft.common.{NCService, U}
+
+import java.util.concurrent.{ConcurrentHashMap, ExecutorService}
+import scala.collection.JavaConverters._
+import scala.concurrent.ExecutionContext
+
+/**
+ *
+ * @param cfg
+ */
+abstract class NCPoolManager(cfg: String) extends NCService {
+ @volatile private var data: ConcurrentHashMap[String, Holder] = new ConcurrentHashMap
+
+ private case class Holder(context: ExecutionContext, pool: Option[ExecutorService])
+
+ private object Config extends NCConfigurable {
+ val factories: Map[String, NcPoolFactory] = {
+ val m: Option[Map[String, String]] = getMapOpt(cfg)
+
+ m.getOrElse(Map.empty).map(p ⇒ p._1 → U.mkObject(p._2))
+ }
+ }
+
+ def getContext(name: String): ExecutionContext =
+ data.computeIfAbsent(
+ name,
+ (_: String) ⇒
+ Config.factories.get(name) match {
+ case Some(f) ⇒
+ val p = f.mkExecutorService()
+
+ logger.info(s"Executor service created with factory '${f.getClass.getName}' for '$name'")
+
+ Holder(ExecutionContext.fromExecutor(p), Some(p))
+ case None ⇒
+ logger.info(s"System executor service used for '$name'")
+
+ Holder(scala.concurrent.ExecutionContext.Implicits.global, None)
+ }
+ ).context
+
+ override def start(parent: Span): NCService = startScopedSpan("start", parent) { _ ⇒
+ ackStarting()
+
+ data = new ConcurrentHashMap
+
+ ackStarted()
+ }
+
+ override def stop(parent: Span): Unit = startScopedSpan("stop", parent) { _ ⇒
+ ackStopping()
+
+ data.values().asScala.flatMap(_.pool).foreach(U.shutdownPool)
+
+ data.clear()
+
+ data = null
+
+ ackStopped()
+ }
+}
diff --git a/nlpcraft/src/main/scala/org/apache/nlpcraft/common/pool/NcPoolFactory.java b/nlpcraft/src/main/scala/org/apache/nlpcraft/common/pool/NcPoolFactory.java
new file mode 100644
index 0000000..e5b99a0
--- /dev/null
+++ b/nlpcraft/src/main/scala/org/apache/nlpcraft/common/pool/NcPoolFactory.java
@@ -0,0 +1,7 @@
+package org.apache.nlpcraft.common.pool;
+
+import java.util.concurrent.ExecutorService;
+
+public interface NcPoolFactory {
+ ExecutorService mkExecutorService();
+}
diff --git a/nlpcraft/src/main/scala/org/apache/nlpcraft/probe/mgrs/pool/NCProbePoolContext.scala b/nlpcraft/src/main/scala/org/apache/nlpcraft/probe/mgrs/pool/NCProbePoolContext.scala
new file mode 100644
index 0000000..0cc2691
--- /dev/null
+++ b/nlpcraft/src/main/scala/org/apache/nlpcraft/probe/mgrs/pool/NCProbePoolContext.scala
@@ -0,0 +1,27 @@
+/*
+ * 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.probe.mgrs.pool
+
+/**
+ * Probe pool context trait.
+ */
+trait NCProbePoolContext {
+ def getName: String = getClass.getName
+
+ implicit def getContext = NCProbePoolManager.getContext(getName)
+}
\ No newline at end of file
diff --git a/nlpcraft/src/main/scala/org/apache/nlpcraft/probe/mgrs/pool/NCProbePoolManager.scala b/nlpcraft/src/main/scala/org/apache/nlpcraft/probe/mgrs/pool/NCProbePoolManager.scala
new file mode 100644
index 0000000..68fe2e5
--- /dev/null
+++ b/nlpcraft/src/main/scala/org/apache/nlpcraft/probe/mgrs/pool/NCProbePoolManager.scala
@@ -0,0 +1,26 @@
+/*
+ * 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.probe.mgrs.pool
+
+import org.apache.nlpcraft.common.pool.NCPoolManager
+
+/**
+ * Probe pool manager.
+ */
+object NCProbePoolManager extends NCPoolManager("nlpcraft.probe.pools")
+
diff --git a/nlpcraft/src/main/scala/org/apache/nlpcraft/server/pool/NCServerPoolContext.scala b/nlpcraft/src/main/scala/org/apache/nlpcraft/server/pool/NCServerPoolContext.scala
new file mode 100644
index 0000000..b0800c8
--- /dev/null
+++ b/nlpcraft/src/main/scala/org/apache/nlpcraft/server/pool/NCServerPoolContext.scala
@@ -0,0 +1,27 @@
+/*
+ * 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.server.pool
+
+/**
+ * Server pool context trait.
+ */
+trait NCServerPoolContext {
+ def getName: String = getClass.getName
+
+ implicit def getContext = NCServerPoolManager.getContext(getName)
+}
\ No newline at end of file
diff --git a/nlpcraft/src/main/scala/org/apache/nlpcraft/server/pool/NCServerPoolManager.scala b/nlpcraft/src/main/scala/org/apache/nlpcraft/server/pool/NCServerPoolManager.scala
new file mode 100644
index 0000000..2e0f0e9
--- /dev/null
+++ b/nlpcraft/src/main/scala/org/apache/nlpcraft/server/pool/NCServerPoolManager.scala
@@ -0,0 +1,25 @@
+/*
+ * 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.server.pool
+
+import org.apache.nlpcraft.common.pool.NCPoolManager
+
+/**
+ * Server pool manager.
+ */
+object NCServerPoolManager extends NCPoolManager("nlpcraft.server.pools")
\ No newline at end of file