tysonnorris closed pull request #4126: Fix netty leak in CosmosDBArtifactStore` URL: https://github.com/apache/incubator-openwhisk/pull/4126
This is a PR merged from a forked repository. As GitHub hides the original diff on merge, it is displayed below for the sake of provenance: As this is a foreign pull request (from a fork), the diff is supplied below (as it won't show otherwise due to GitHub magic): 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 09e8351c6b..efa25f022f 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 ced5df3a0a..6521d2c3c1 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 0000000000..44efd15bfc --- /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 0000000000..2dbe767e04 --- /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 0000000000..0a450dc93c --- /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()); + } +} ---------------------------------------------------------------- This is an automated message from the Apache Git Service. To respond to the message, please log on GitHub and use the URL above to go to the specific comment. For queries about this service, please contact Infrastructure at: us...@infra.apache.org With regards, Apache Git Services