This is an automated email from the ASF dual-hosted git repository.
tysonnorris pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/incubator-openwhisk.git
The following commit(s) were added to refs/heads/master by this push:
new e97b6ce Fix netty leak in CosmosDBArtifactStore(#4126)
e97b6ce is described below
commit e97b6ce17f2bdc98c008d38c0219ae36c921ba27
Author: Chetan Mehrotra <[email protected]>
AuthorDate: Wed Nov 21 21:54:08 2018 +0530
Fix netty leak in CosmosDBArtifactStore(#4126)
In some cases where query has limit applied, some netty leak related
exceptions have been logged when using CosmosDBArtifactStore. This change
revises how the limits are applied, and adds tests for asserting the absence of
leaks.
---
.../database/cosmosdb/CosmosDBArtifactStore.scala | 9 +-
.../cosmosdb/CosmosDBArtifactStoreTests.scala | 23 +++++
.../core/database/cosmosdb/CosmosDBLeakTests.scala | 97 ++++++++++++++++++++++
.../database/cosmosdb/RecordingLeakDetector.java | 41 +++++++++
.../cosmosdb/RecordingLeakDetectorFactory.java | 34 ++++++++
5 files changed, 197 insertions(+), 7 deletions(-)
diff --git
a/common/scala/src/main/scala/org/apache/openwhisk/core/database/cosmosdb/CosmosDBArtifactStore.scala
b/common/scala/src/main/scala/org/apache/openwhisk/core/database/cosmosdb/CosmosDBArtifactStore.scala
index 09e8351..efa25f0 100644
---
a/common/scala/src/main/scala/org/apache/openwhisk/core/database/cosmosdb/CosmosDBArtifactStore.scala
+++
b/common/scala/src/main/scala/org/apache/openwhisk/core/database/cosmosdb/CosmosDBArtifactStore.scala
@@ -218,7 +218,7 @@ class CosmosDBArtifactStore[DocumentAbstraction <:
DocumentSerializer](protected
val publisher =
RxReactiveStreams.toPublisher(client.queryDocuments(collection.getSelfLink,
querySpec, newFeedOptions()))
- val s = Source
+ val f = Source
.fromPublisher(publisher)
.wireTap(Sink.foreach(r => collectMetrics(queryToken,
r.getRequestCharge)))
.mapConcat(asSeq)
@@ -229,14 +229,9 @@ class CosmosDBArtifactStore[DocumentAbstraction <:
DocumentSerializer](protected
.transformViewResult(ddoc, viewName, startKey, endKey,
realIncludeDocs, js, CosmosDBArtifactStore.this))
.mapAsync(1)(identity)
.mapConcat(identity)
-
- //If limit is specified then only take that many elements. With join its
possible that
- //number of entries become > limit
- val s2 = if (limit > 0) s.take(limit) else s
-
- val f = s2
.runWith(Sink.seq)
.map(_.toList)
+ .map(l => if (limit > 0) l.take(limit) else l)
f.foreach { out =>
transid.finished(this, start, s"[QUERY] '$collName' completed: matched
${out.size}")
diff --git
a/tests/src/test/scala/org/apache/openwhisk/core/database/cosmosdb/CosmosDBArtifactStoreTests.scala
b/tests/src/test/scala/org/apache/openwhisk/core/database/cosmosdb/CosmosDBArtifactStoreTests.scala
index ced5df3..6521d2c 100644
---
a/tests/src/test/scala/org/apache/openwhisk/core/database/cosmosdb/CosmosDBArtifactStoreTests.scala
+++
b/tests/src/test/scala/org/apache/openwhisk/core/database/cosmosdb/CosmosDBArtifactStoreTests.scala
@@ -17,6 +17,8 @@
package org.apache.openwhisk.core.database.cosmosdb
+import io.netty.util.ResourceLeakDetector
+import io.netty.util.ResourceLeakDetector.Level
import org.junit.runner.RunWith
import org.scalatest.FlatSpec
import org.scalatest.junit.JUnitRunner
@@ -27,6 +29,27 @@ import
org.apache.openwhisk.core.database.test.behavior.ArtifactStoreBehavior
class CosmosDBArtifactStoreTests extends FlatSpec with
CosmosDBStoreBehaviorBase with ArtifactStoreBehavior {
override protected def maxAttachmentSizeWithoutAttachmentStore = 1.MB
+ private var initialLevel: Level = _
+
+ override protected def beforeAll(): Unit = {
+ RecordingLeakDetectorFactory.register()
+ initialLevel = ResourceLeakDetector.getLevel
+ ResourceLeakDetector.setLevel(Level.PARANOID)
+ super.beforeAll()
+ }
+
+ override def afterAll(): Unit = {
+ super.afterAll()
+ ResourceLeakDetector.setLevel(initialLevel)
+
+ //Try triggering GC which may trigger leak detection logic
+ System.gc()
+
+ withClue("Recorded leak count should be zero") {
+ RecordingLeakDetectorFactory.counter.cur shouldBe 0
+ }
+ }
+
behavior of "CosmosDB Setup"
it should "be configured with default throughput" in {
diff --git
a/tests/src/test/scala/org/apache/openwhisk/core/database/cosmosdb/CosmosDBLeakTests.scala
b/tests/src/test/scala/org/apache/openwhisk/core/database/cosmosdb/CosmosDBLeakTests.scala
new file mode 100644
index 0000000..44efd15
--- /dev/null
+++
b/tests/src/test/scala/org/apache/openwhisk/core/database/cosmosdb/CosmosDBLeakTests.scala
@@ -0,0 +1,97 @@
+/*
+ * 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.openwhisk.core.database.cosmosdb
+import akka.stream.scaladsl.{Sink, Source}
+import io.netty.util.ResourceLeakDetector
+import io.netty.util.ResourceLeakDetector.Level
+import org.apache.openwhisk.common.TransactionId
+import org.apache.openwhisk.core.entity.{
+ BasicAuthenticationAuthKey,
+ Identity,
+ Namespace,
+ Secret,
+ Subject,
+ UUID,
+ WhiskAuth,
+ WhiskNamespace
+}
+import org.junit.runner.RunWith
+import org.scalatest.FlatSpec
+import org.scalatest.junit.JUnitRunner
+
+import scala.concurrent.duration.DurationInt
+
+/**
+ * Performs query flow which causes leak multiple times. Post fix this test
should always pass
+ * By default this test is disabled
+ */
+@RunWith(classOf[JUnitRunner])
+class CosmosDBLeakTests extends FlatSpec with CosmosDBStoreBehaviorBase {
+
+ behavior of s"CosmosDB leak"
+
+ private var initialLevel: Level = _
+
+ override protected def beforeAll(): Unit = {
+ RecordingLeakDetectorFactory.register()
+ initialLevel = ResourceLeakDetector.getLevel
+ ResourceLeakDetector.setLevel(Level.PARANOID)
+ super.beforeAll()
+ }
+
+ override def afterAll(): Unit = {
+ super.afterAll()
+ ResourceLeakDetector.setLevel(initialLevel)
+
+ withClue("Recorded leak count should be zero") {
+ RecordingLeakDetectorFactory.counter.cur shouldBe 0
+ }
+ }
+
+ it should "not happen in performing subject query" ignore {
+ implicit val tid: TransactionId = transid()
+ val uuid = UUID()
+ val ak = BasicAuthenticationAuthKey(uuid, Secret())
+ val ns = Namespace(aname(), uuid)
+ val subs =
+ Array(WhiskAuth(Subject(), Set(WhiskNamespace(ns, ak))))
+ subs foreach (put(authStore, _))
+
+ implicit val patienceConfig: PatienceConfig = PatienceConfig(timeout =
30.minutes)
+
+ Source(1 to 500)
+ .filter(_ => RecordingLeakDetectorFactory.counter.cur == 0)
+ .mapAsync(5) { i =>
+ if (i % 5 == 0) println(i)
+ queryName(ns)
+ }
+ .runWith(Sink.ignore)
+ .futureValue
+
+ System.gc()
+
+ withClue("Recorded leak count should be zero") {
+ RecordingLeakDetectorFactory.counter.cur shouldBe 0
+ }
+ }
+
+ def queryName(ns: Namespace)(implicit tid: TransactionId) = {
+ Identity.list(authStore, List(ns.name.asString), limit = 1)
+ }
+
+}
diff --git
a/tests/src/test/scala/org/apache/openwhisk/core/database/cosmosdb/RecordingLeakDetector.java
b/tests/src/test/scala/org/apache/openwhisk/core/database/cosmosdb/RecordingLeakDetector.java
new file mode 100644
index 0000000..2dbe767
--- /dev/null
+++
b/tests/src/test/scala/org/apache/openwhisk/core/database/cosmosdb/RecordingLeakDetector.java
@@ -0,0 +1,41 @@
+/*
+ * 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.openwhisk.core.database.cosmosdb;
+
+import io.netty.util.ResourceLeakDetector;
+import org.apache.openwhisk.common.Counter;
+
+public class RecordingLeakDetector<T> extends ResourceLeakDetector<T> {
+ private final Counter counter;
+ public RecordingLeakDetector(Counter counter, Class<?> resourceType, int
samplingInterval) {
+ super(resourceType, samplingInterval);
+ this.counter = counter;
+ }
+
+ @Override
+ protected void reportTracedLeak(String resourceType, String records) {
+ super.reportTracedLeak(resourceType, records);
+ counter.next();
+ }
+
+ @Override
+ protected void reportUntracedLeak(String resourceType) {
+ super.reportUntracedLeak(resourceType);
+ counter.next();
+ }
+}
diff --git
a/tests/src/test/scala/org/apache/openwhisk/core/database/cosmosdb/RecordingLeakDetectorFactory.java
b/tests/src/test/scala/org/apache/openwhisk/core/database/cosmosdb/RecordingLeakDetectorFactory.java
new file mode 100644
index 0000000..0a450dc
--- /dev/null
+++
b/tests/src/test/scala/org/apache/openwhisk/core/database/cosmosdb/RecordingLeakDetectorFactory.java
@@ -0,0 +1,34 @@
+/*
+ * 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.openwhisk.core.database.cosmosdb;
+
+import io.netty.util.ResourceLeakDetector;
+import io.netty.util.ResourceLeakDetectorFactory;
+import org.apache.openwhisk.common.Counter;
+
+public class RecordingLeakDetectorFactory extends ResourceLeakDetectorFactory {
+ static final Counter counter = new Counter();
+ @Override
+ public <T> ResourceLeakDetector<T> newResourceLeakDetector(Class<T>
resource, int samplingInterval, long maxActive) {
+ return new RecordingLeakDetector<T>(counter, resource,
samplingInterval);
+ }
+
+ public static void register() {
+ ResourceLeakDetectorFactory.setResourceLeakDetectorFactory(new
RecordingLeakDetectorFactory());
+ }
+}