[GitHub] tysonnorris commented on a change in pull request #3812: ContainerClient + akka http alternative to HttpUtils

2018-07-20 Thread GitBox
tysonnorris commented on a change in pull request #3812: ContainerClient + akka 
http alternative to HttpUtils
URL: 
https://github.com/apache/incubator-openwhisk/pull/3812#discussion_r204137242
 
 

 ##
 File path: 
tests/src/test/scala/whisk/core/containerpool/kubernetes/test/KubernetesClientTests.scala
 ##
 @@ -188,6 +189,7 @@ object KubernetesClientTests {
   implicit def strToInstant(str: String): Instant =
 strToDate(str).get
 
+  implicit val as = ActorSystem("kubernetes-client-tests-actor-system")
 
 Review comment:
   The `TestKubernetesClient` is shared with `KubernetesContainerTests` - so 
for now, changed to implicit ActorSystem (and left object/classes in current 
places). Good?


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


[GitHub] tysonnorris commented on a change in pull request #3812: ContainerClient + akka http alternative to HttpUtils

2018-07-20 Thread GitBox
tysonnorris commented on a change in pull request #3812: ContainerClient + akka 
http alternative to HttpUtils
URL: 
https://github.com/apache/incubator-openwhisk/pull/3812#discussion_r204126416
 
 

 ##
 File path: 
tests/src/test/scala/whisk/core/containerpool/docker/test/AkkaContainerClientTests.scala
 ##
 @@ -0,0 +1,213 @@
+/*
+ * 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 whisk.core.containerpool.docker.test
+
+import common.StreamLogging
+import common.WskActorSystem
+import java.nio.charset.StandardCharsets
+import java.time.Instant
+import org.apache.http.HttpRequest
+import org.apache.http.HttpResponse
+import org.apache.http.entity.StringEntity
+import org.apache.http.localserver.LocalServerTestBase
+import org.apache.http.protocol.HttpContext
+import org.apache.http.protocol.HttpRequestHandler
+import org.junit.runner.RunWith
+import org.scalatest.BeforeAndAfter
+import org.scalatest.BeforeAndAfterAll
+import org.scalatest.FlatSpec
+import org.scalatest.Matchers
+import org.scalatest.junit.JUnitRunner
+import scala.concurrent.Await
+import scala.concurrent.TimeoutException
+import scala.concurrent.duration._
+import spray.json.JsObject
+import whisk.common.TransactionId
+import whisk.core.containerpool.AkkaContainerClient
+import whisk.core.entity.ActivationResponse._
+import whisk.core.entity.size._
+
+/**
+ * Unit tests for AkkaContainerClientTests which communicate with containers.
+ */
+@RunWith(classOf[JUnitRunner])
+class AkkaContainerClientTests
+extends FlatSpec
+with Matchers
+with BeforeAndAfter
+with BeforeAndAfterAll
+with StreamLogging
+with WskActorSystem {
+
+  implicit val transid = TransactionId.testing
+  implicit val ec = actorSystem.dispatcher
+
+  var testHang: FiniteDuration = 0.second
+  var testStatusCode: Int = 200
+  var testResponse: String = null
+  var testConnectionFailCount: Int = 0
+
+  val mockServer = new LocalServerTestBase {
+var failcount = 0
+override def setUp() = {
+  super.setUp()
+  this.serverBootstrap
+.registerHandler(
+  "/init",
+  new HttpRequestHandler() {
+override def handle(request: HttpRequest, response: HttpResponse, 
context: HttpContext) = {
+  if (testHang.length > 0) {
+Thread.sleep(testHang.toMillis)
+  }
+  if (testConnectionFailCount > 0 && failcount < 
testConnectionFailCount) {
+failcount += 1
+println("failing in test")
+throw new RuntimeException("failing...")
+  }
+  response.setStatusCode(testStatusCode);
+  if (testResponse != null) {
+response.setEntity(new StringEntity(testResponse, 
StandardCharsets.UTF_8))
+  }
+}
+  })
+}
+  }
+
+  mockServer.setUp()
+  val httpHost = mockServer.start()
+  val hostWithPort = s"${httpHost.getHostName}:${httpHost.getPort}"
+
+  before {
+testHang = 0.second
+testStatusCode = 200
+testResponse = null
+testConnectionFailCount = 0
+stream.reset()
+  }
+
+  override def afterAll = {
+mockServer.shutDown()
+  }
+
+  behavior of "PoolingContainerClient"
+
+  it should "not wait longer than set timeout" in {
+val timeout = 5.seconds
+val connection = new AkkaContainerClient(httpHost.getHostName, 
httpHost.getPort, timeout, 1.B, 100)
+testHang = timeout * 2
+val start = Instant.now()
+val result = Await.result(connection.post("/init", JsObject.empty, retry = 
true), 10.seconds)
+
+val end = Instant.now()
+val waited = end.toEpochMilli - start.toEpochMilli
+result shouldBe 'left
+waited should be > timeout.toMillis
+waited should be < (timeout * 2).toMillis
+  }
+
+  it should "handle empty entity response" in {
+val timeout = 5.seconds
+val connection = new AkkaContainerClient(httpHost.getHostName, 
httpHost.getPort, timeout, 1.B, 100)
+testStatusCode = 204
+val result = Await.result(connection.post("/init", JsObject.empty, retry = 
true), 10.seconds)
+result shouldBe Left(NoResponseReceived())
+  }
+
+  it should "retry till timeout on StreamTcpException" in {
+val timeout = 

[GitHub] tysonnorris commented on a change in pull request #3812: ContainerClient + akka http alternative to HttpUtils

2018-07-20 Thread GitBox
tysonnorris commented on a change in pull request #3812: ContainerClient + akka 
http alternative to HttpUtils
URL: 
https://github.com/apache/incubator-openwhisk/pull/3812#discussion_r204125651
 
 

 ##
 File path: 
tests/src/test/scala/whisk/core/containerpool/docker/test/AkkaContainerClientTests.scala
 ##
 @@ -0,0 +1,213 @@
+/*
+ * 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 whisk.core.containerpool.docker.test
+
+import common.StreamLogging
+import common.WskActorSystem
+import java.nio.charset.StandardCharsets
+import java.time.Instant
+import org.apache.http.HttpRequest
+import org.apache.http.HttpResponse
+import org.apache.http.entity.StringEntity
+import org.apache.http.localserver.LocalServerTestBase
+import org.apache.http.protocol.HttpContext
+import org.apache.http.protocol.HttpRequestHandler
+import org.junit.runner.RunWith
+import org.scalatest.BeforeAndAfter
+import org.scalatest.BeforeAndAfterAll
+import org.scalatest.FlatSpec
+import org.scalatest.Matchers
+import org.scalatest.junit.JUnitRunner
+import scala.concurrent.Await
+import scala.concurrent.TimeoutException
+import scala.concurrent.duration._
+import spray.json.JsObject
+import whisk.common.TransactionId
+import whisk.core.containerpool.AkkaContainerClient
+import whisk.core.entity.ActivationResponse._
+import whisk.core.entity.size._
+
+/**
+ * Unit tests for AkkaContainerClientTests which communicate with containers.
+ */
+@RunWith(classOf[JUnitRunner])
+class AkkaContainerClientTests
+extends FlatSpec
+with Matchers
+with BeforeAndAfter
+with BeforeAndAfterAll
+with StreamLogging
+with WskActorSystem {
+
+  implicit val transid = TransactionId.testing
+  implicit val ec = actorSystem.dispatcher
+
+  var testHang: FiniteDuration = 0.second
+  var testStatusCode: Int = 200
+  var testResponse: String = null
+  var testConnectionFailCount: Int = 0
+
+  val mockServer = new LocalServerTestBase {
+var failcount = 0
+override def setUp() = {
+  super.setUp()
+  this.serverBootstrap
+.registerHandler(
+  "/init",
+  new HttpRequestHandler() {
+override def handle(request: HttpRequest, response: HttpResponse, 
context: HttpContext) = {
+  if (testHang.length > 0) {
+Thread.sleep(testHang.toMillis)
+  }
+  if (testConnectionFailCount > 0 && failcount < 
testConnectionFailCount) {
+failcount += 1
+println("failing in test")
+throw new RuntimeException("failing...")
+  }
+  response.setStatusCode(testStatusCode);
+  if (testResponse != null) {
+response.setEntity(new StringEntity(testResponse, 
StandardCharsets.UTF_8))
+  }
+}
+  })
+}
+  }
+
+  mockServer.setUp()
+  val httpHost = mockServer.start()
+  val hostWithPort = s"${httpHost.getHostName}:${httpHost.getPort}"
+
+  before {
+testHang = 0.second
+testStatusCode = 200
+testResponse = null
+testConnectionFailCount = 0
+stream.reset()
+  }
+
+  override def afterAll = {
+mockServer.shutDown()
+  }
+
+  behavior of "PoolingContainerClient"
+
+  it should "not wait longer than set timeout" in {
+val timeout = 5.seconds
+val connection = new AkkaContainerClient(httpHost.getHostName, 
httpHost.getPort, timeout, 1.B, 100)
+testHang = timeout * 2
+val start = Instant.now()
+val result = Await.result(connection.post("/init", JsObject.empty, retry = 
true), 10.seconds)
+
+val end = Instant.now()
+val waited = end.toEpochMilli - start.toEpochMilli
+result shouldBe 'left
+waited should be > timeout.toMillis
+waited should be < (timeout * 2).toMillis
+  }
+
+  it should "handle empty entity response" in {
+val timeout = 5.seconds
+val connection = new AkkaContainerClient(httpHost.getHostName, 
httpHost.getPort, timeout, 1.B, 100)
+testStatusCode = 204
+val result = Await.result(connection.post("/init", JsObject.empty, retry = 
true), 10.seconds)
+result shouldBe Left(NoResponseReceived())
+  }
+
+  it should "retry till timeout on StreamTcpException" in {
+val timeout = 

[GitHub] tysonnorris commented on a change in pull request #3812: ContainerClient + akka http alternative to HttpUtils

2018-07-20 Thread GitBox
tysonnorris commented on a change in pull request #3812: ContainerClient + akka 
http alternative to HttpUtils
URL: 
https://github.com/apache/incubator-openwhisk/pull/3812#discussion_r204125226
 
 

 ##
 File path: 
tests/src/test/scala/whisk/core/containerpool/docker/test/ApacheBlockingContainerClientTests.scala
 ##
 @@ -106,20 +113,41 @@ class ContainerConnectionTests
 
   it should "handle empty entity response" in {
 val timeout = 5.seconds
-val connection = new HttpUtils(hostWithPort, timeout, 1.B)
+val connection = new ApacheBlockingContainerClient(hostWithPort, timeout, 
1.B)
 testStatusCode = 204
-val result = connection.post("/init", JsObject.empty, retry = true)
+val result = Await.result(connection.post("/init", JsObject.empty, retry = 
true), 10.seconds)
 result shouldBe Left(NoResponseReceived())
   }
 
+  it should "retry till timeout on HttpHostConnectException" in {
+val timeout = 5.seconds
+val badHostAndPort = "0.0.0.0:12345"
+val connection = new ApacheBlockingContainerClient(badHostAndPort, 
timeout, 1.B)
+testStatusCode = 204
+val start = Instant.now()
+val result = Await.result(connection.post("/init", JsObject.empty, retry = 
true), 10.seconds)
+val end = Instant.now()
+val waited = end.toEpochMilli - start.toEpochMilli
+result should be('left)
+result.left.get shouldBe a[Timeout]
+result.left.get.asInstanceOf[Timeout].t shouldBe 
a[RetryableConnectionError]
+result.left.get
+  .asInstanceOf[Timeout]
+  .t
+  .asInstanceOf[RetryableConnectionError]
+  .t shouldBe a[HttpHostConnectException]
 
 Review comment:
   nice - much better! (worked for me)


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


[GitHub] tysonnorris commented on a change in pull request #3812: ContainerClient + akka http alternative to HttpUtils

2018-07-20 Thread GitBox
tysonnorris commented on a change in pull request #3812: ContainerClient + akka 
http alternative to HttpUtils
URL: 
https://github.com/apache/incubator-openwhisk/pull/3812#discussion_r204123577
 
 

 ##
 File path: 
tests/src/test/scala/whisk/core/containerpool/kubernetes/test/KubernetesClientTests.scala
 ##
 @@ -188,6 +189,7 @@ object KubernetesClientTests {
   implicit def strToInstant(str: String): Instant =
 strToDate(str).get
 
+  implicit val as = ActorSystem("kubernetes-client-tests-actor-system")
 
 Review comment:
   I will give it a try, but it isn't clear why this test was setup this way? 
@dgrove-oss @jcrossley3 ?


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


[GitHub] tysonnorris commented on a change in pull request #3812: ContainerClient + akka http alternative to HttpUtils

2018-07-20 Thread GitBox
tysonnorris commented on a change in pull request #3812: ContainerClient + akka 
http alternative to HttpUtils
URL: 
https://github.com/apache/incubator-openwhisk/pull/3812#discussion_r204115351
 
 

 ##
 File path: 
core/invoker/src/main/scala/whisk/core/containerpool/docker/DockerContainer.scala
 ##
 @@ -190,29 +190,33 @@ class DockerContainer(protected val id: ContainerId,
 implicit transid: TransactionId): Future[RunResult] = {
 val started = Instant.now()
 val http = httpConnection.getOrElse {
-  val conn = new HttpUtils(s"${addr.host}:${addr.port}", timeout, 
ActivationEntityLimit.MAX_ACTIVATION_ENTITY_LIMIT)
+  val conn = new ApacheBlockingContainerClient(
+s"${addr.host}:${addr.port}",
+timeout,
+ActivationEntityLimit.MAX_ACTIVATION_ENTITY_LIMIT)
 
 Review comment:
   Also noticed that `ActivationEntityLimit.MAX_ACTIVATION_ENTITY_LIMIT` is not 
used in `Container.scala`... These are both 1mb, but docs for 
`MAX_ACTIVATION_LIMIT` says _This refers to the invoke-time parameters_ - but 
in this case we are limiting the response size (and I don't see any assertion 
of limit on the request entity size in former HttpUtils?).
   WDYT?


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


[GitHub] tysonnorris commented on a change in pull request #3812: ContainerClient + akka http alternative to HttpUtils

2018-07-20 Thread GitBox
tysonnorris commented on a change in pull request #3812: ContainerClient + akka 
http alternative to HttpUtils
URL: 
https://github.com/apache/incubator-openwhisk/pull/3812#discussion_r204112297
 
 

 ##
 File path: 
common/scala/src/main/scala/whisk/core/containerpool/AkkaContainerClient.scala
 ##
 @@ -0,0 +1,216 @@
+/*
+ * 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 whisk.core.containerpool
+
+import akka.actor.ActorSystem
+import akka.http.scaladsl.marshallers.sprayjson.SprayJsonSupport._
+import akka.http.scaladsl.marshalling.Marshal
+import akka.http.scaladsl.model.HttpMethods
+import akka.http.scaladsl.model.HttpRequest
+import akka.http.scaladsl.model.HttpResponse
+import akka.http.scaladsl.model.MediaTypes
+import akka.http.scaladsl.model.MessageEntity
+import akka.http.scaladsl.model.StatusCodes
+import akka.http.scaladsl.model.headers.Accept
+import akka.http.scaladsl.model.headers.Connection
+import akka.http.scaladsl.unmarshalling.Unmarshal
+import akka.stream.StreamTcpException
+import akka.stream.scaladsl.Sink
+import akka.stream.scaladsl.Source
+import akka.util.ByteString
+import scala.concurrent.Await
+import scala.concurrent.ExecutionContext
+import scala.concurrent.Future
+import scala.concurrent.TimeoutException
+import scala.concurrent.duration._
+import scala.util.Try
+import scala.util.control.NonFatal
+import spray.json._
+import whisk.common.Logging
+import whisk.common.TransactionId
+import whisk.core.entity.ActivationResponse.ContainerHttpError
+import whisk.core.entity.ActivationResponse._
+import whisk.core.entity.ByteSize
+import whisk.core.entity.size.SizeLong
+import whisk.http.PoolingRestClient
+
+/**
+ * This HTTP client is used only in the invoker to communicate with the action 
container.
+ * It allows to POST a JSON object and receive JSON object back; that is the
+ * content type and the accept headers are both 'application/json.
+ * This implementation uses the akka http host-level client API.
+ *
+ * @param hostname the host name
+ * @param port the port
+ * @param timeout the timeout in msecs to wait for a response
+ * @param maxResponse the maximum size in bytes the connection will accept
+ * @param queueSize once all connections are used, how big of queue to allow 
for additional requests
+ * @param retryInterval duration between retries for TCP connection errors
+ */
+protected class AkkaContainerClient(
+  hostname: String,
+  port: Int,
+  timeout: FiniteDuration,
+  maxResponse: ByteSize,
+  queueSize: Int,
+  retryInterval: FiniteDuration = 100.milliseconds)(implicit logging: Logging, 
as: ActorSystem)
+extends PoolingRestClient("http", hostname, port, queueSize, timeout = 
Some(timeout))
+with ContainerClient {
+
+  def close() = Await.result(shutdown(), 30.seconds)
+
+  /**
+   * Posts to hostname/endpoint the given JSON object.
+   * Waits up to timeout before aborting on a good connection.
+   * If the endpoint is not ready, retry up to timeout.
+   * Every retry reduces the available timeout so that this method should not
+   * wait longer than the total timeout (within a small slack allowance).
+   *
+   * @param endpoint the path the api call relative to hostname
+   * @param body the JSON value to post (this is usually a JSON objecT)
+   * @param retry whether or not to retry on connection failure
+   * @return Left(Error Message) or Right(Status Code, Response as UTF-8 
String)
+   */
+  def post(endpoint: String, body: JsValue, retry: Boolean)(
+implicit tid: TransactionId): Future[Either[ContainerHttpError, 
ContainerResponse]] = {
+
+//create the request
+val req = Marshal(body).to[MessageEntity].map { b =>
+  //DO NOT reuse the connection
+  //For details on Connection: Close handling, see:
+  // - 
https://doc.akka.io/docs/akka-http/current/common/http-model.html#http-headers
+  // - 
http://github.com/akka/akka-http/tree/v10.1.3/akka-http-core/src/test/scala/akka/http/impl/engine/rendering/ResponseRendererSpec.scala#L470-L571
+  HttpRequest(HttpMethods.POST, endpoint, entity = b)
+.withHeaders(Connection("close"), 
Accept(MediaTypes.`application/json`))
+}
+
+retryingRequest(req, timeout, retry)
+  .flatMap 

[GitHub] tysonnorris commented on a change in pull request #3812: ContainerClient + akka http alternative to HttpUtils

2018-07-19 Thread GitBox
tysonnorris commented on a change in pull request #3812: ContainerClient + akka 
http alternative to HttpUtils
URL: 
https://github.com/apache/incubator-openwhisk/pull/3812#discussion_r203930332
 
 

 ##
 File path: common/scala/src/main/scala/whisk/http/PoolingRestClient.scala
 ##
 @@ -43,18 +44,24 @@ class PoolingRestClient(
   host: String,
   port: Int,
   queueSize: Int,
-  httpFlow: Option[Flow[(HttpRequest, Promise[HttpResponse]), 
(Try[HttpResponse], Promise[HttpResponse]), Any]] = None)(
-  implicit system: ActorSystem) {
+  httpFlow: Option[Flow[(HttpRequest, Promise[HttpResponse]), 
(Try[HttpResponse], Promise[HttpResponse]), Any]] = None,
+  timeout: Option[FiniteDuration] = None)(implicit system: ActorSystem) {
   require(protocol == "http" || protocol == "https", "Protocol must be one of 
{ http, https }.")
 
   protected implicit val context: ExecutionContext = system.dispatcher
   protected implicit val materializer: ActorMaterializer = ActorMaterializer()
 
+  //if specified, override the ClientConnection idle-timeout value
+  private val timeoutSettings = {
+val ps = ConnectionPoolSettings(system.settings.config)
+timeout.map(t => 
ps.withUpdatedConnectionSettings(_.withIdleTimeout(t))).getOrElse(ps)
+  }
 
 Review comment:
   Thanks @chetanmeh !


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


[GitHub] tysonnorris commented on a change in pull request #3812: ContainerClient + akka http alternative to HttpUtils

2018-07-19 Thread GitBox
tysonnorris commented on a change in pull request #3812: ContainerClient + akka 
http alternative to HttpUtils
URL: 
https://github.com/apache/incubator-openwhisk/pull/3812#discussion_r203843977
 
 

 ##
 File path: 
common/scala/src/main/scala/whisk/core/containerpool/ContainerClient.scala
 ##
 @@ -0,0 +1,212 @@
+/*
+ * 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 whisk.core.containerpool
+
+import akka.actor.ActorSystem
+import akka.http.scaladsl.marshallers.sprayjson.SprayJsonSupport._
+import akka.http.scaladsl.marshalling.Marshal
+import akka.http.scaladsl.model.HttpMethods
+import akka.http.scaladsl.model.HttpRequest
+import akka.http.scaladsl.model.HttpResponse
+import akka.http.scaladsl.model.MessageEntity
+import akka.http.scaladsl.model.StatusCodes
+import akka.http.scaladsl.model.headers.Connection
+import akka.http.scaladsl.unmarshalling.Unmarshal
+import akka.stream.StreamTcpException
+import akka.stream.scaladsl.Sink
+import akka.stream.scaladsl.Source
+import akka.util.ByteString
+import scala.concurrent.Await
+import scala.concurrent.ExecutionContext
+import scala.concurrent.Future
+import scala.concurrent.TimeoutException
+import scala.concurrent.duration._
+import scala.util.Try
+import spray.json._
+import whisk.common.Logging
+import whisk.common.TransactionId
+import whisk.core.entity.ActivationResponse.ContainerHttpError
+import whisk.core.entity.ActivationResponse._
+import whisk.core.entity.ByteSize
+import whisk.core.entity.size.SizeLong
+import whisk.http.PoolingRestClient
+
+trait ContainerClient {
+  def close(): Unit
+  def post(endpoint: String, body: JsValue, retry: Boolean)(
+implicit tid: TransactionId): Future[Either[ContainerHttpError, 
ContainerResponse]]
+
+}
+
+/**
+ * This HTTP client is used only in the invoker to communicate with the action 
container.
+ * It allows to POST a JSON object and receive JSON object back; that is the
+ * content type and the accept headers are both 'application/json.
+ * The reason we still use this class for the action container is a mysterious 
hang
+ * in the Akka http client where a future fails to properly timeout and we 
have not
+ * determined why that is.
+ *
+ * @param hostname the host name
+ * @param timeout the timeout in msecs to wait for a response
+ * @param maxResponse the maximum size in bytes the connection will accept
+ * @param queueSize once all connections are used, how big of queue to allow 
for additional requests
+ * @param retryInterval duration between retries for TCP connection errors
+ */
+protected class PoolingContainerClient(
+  hostname: String,
+  port: Int,
+  timeout: FiniteDuration,
+  maxResponse: ByteSize,
+  queueSize: Int,
+  retryInterval: FiniteDuration = 100.milliseconds)(implicit logging: Logging, 
as: ActorSystem)
+extends PoolingRestClient("http", hostname, port, queueSize, timeout = 
Some(timeout))
+with ContainerClient
+with AutoCloseable {
+
+  def close() = shutdown()
+
+  /**
+   * Posts to hostname/endpoint the given JSON object.
+   * Waits up to timeout before aborting on a good connection.
+   * If the endpoint is not ready, retry up to timeout.
+   * Every retry reduces the available timeout so that this method should not
+   * wait longer than the total timeout (within a small slack allowance).
+   *
+   * @param endpoint the path the api call relative to hostname
+   * @param body the JSON value to post (this is usually a JSON objecT)
+   * @param retry whether or not to retry on connection failure
+   * @return Left(Error Message) or Right(Status Code, Response as UTF-8 
String)
+   */
+  def post(endpoint: String, body: JsValue, retry: Boolean)(
+implicit tid: TransactionId): Future[Either[ContainerHttpError, 
ContainerResponse]] = {
+
+//create the request
+val req = Marshal(body).to[MessageEntity].map { b =>
+  //DO NOT reuse the connection (in case of paused containers)
+  //For details on Connection: Close handling, see:
+  // - 
https://doc.akka.io/docs/akka-http/current/common/http-model.html#http-headers
+  // - 

[GitHub] tysonnorris commented on a change in pull request #3812: ContainerClient + akka http alternative to HttpUtils

2018-07-19 Thread GitBox
tysonnorris commented on a change in pull request #3812: ContainerClient + akka 
http alternative to HttpUtils
URL: 
https://github.com/apache/incubator-openwhisk/pull/3812#discussion_r203783691
 
 

 ##
 File path: 
common/scala/src/main/scala/whisk/core/containerpool/ContainerClient.scala
 ##
 @@ -0,0 +1,223 @@
+/*
+ * 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 whisk.core.containerpool
+
+import akka.actor.ActorSystem
+import akka.http.scaladsl.marshallers.sprayjson.SprayJsonSupport._
+import akka.http.scaladsl.marshalling.Marshal
+import akka.http.scaladsl.model.HttpMethods
+import akka.http.scaladsl.model.HttpRequest
+import akka.http.scaladsl.model.HttpResponse
+import akka.http.scaladsl.model.MessageEntity
+import akka.http.scaladsl.model.StatusCodes
+import akka.http.scaladsl.model.headers.Connection
+import akka.http.scaladsl.unmarshalling.Unmarshal
+import akka.stream.StreamTcpException
+import akka.stream.scaladsl.Sink
+import akka.stream.scaladsl.Source
+import akka.util.ByteString
+import scala.concurrent.Await
+import scala.concurrent.ExecutionContext
+import scala.concurrent.Future
+import scala.concurrent.TimeoutException
+import scala.concurrent.duration._
+import scala.util.Try
+import scala.util.control.NonFatal
+import spray.json._
+import whisk.common.Logging
+import whisk.common.TransactionId
+import whisk.core.entity.ActivationResponse.ContainerHttpError
+import whisk.core.entity.ActivationResponse._
+import whisk.core.entity.ByteSize
+import whisk.core.entity.size.SizeLong
+import whisk.http.PoolingRestClient
+
+trait ContainerClient {
+  def close(): Unit
+  def post(endpoint: String, body: JsValue, retry: Boolean)(
+implicit tid: TransactionId): Future[Either[ContainerHttpError, 
ContainerResponse]]
+
+}
+
+/**
+ * This HTTP client is used only in the invoker to communicate with the action 
container.
+ * It allows to POST a JSON object and receive JSON object back; that is the
+ * content type and the accept headers are both 'application/json.
+ * The reason we still use this class for the action container is a mysterious 
hang
+ * in the Akka http client where a future fails to properly timeout and we 
have not
+ * determined why that is.
+ *
+ * @param hostname the host name
+ * @param timeout the timeout in msecs to wait for a response
+ * @param maxResponse the maximum size in bytes the connection will accept
+ * @param queueSize once all connections are used, how big of queue to allow 
for additional requests
+ * @param retryInterval duration between retries for TCP connection errors
+ */
+protected class PoolingContainerClient(
+  hostname: String,
+  port: Int,
+  timeout: FiniteDuration,
+  maxResponse: ByteSize,
+  queueSize: Int,
+  retryInterval: FiniteDuration = 100.milliseconds)(implicit logging: Logging, 
as: ActorSystem)
+extends PoolingRestClient("http", hostname, port, queueSize, timeout = 
Some(timeout))
+with ContainerClient
+with AutoCloseable {
+
+  def close() = shutdown()
+
+  /**
+   * Posts to hostname/endpoint the given JSON object.
+   * Waits up to timeout before aborting on a good connection.
+   * If the endpoint is not ready, retry up to timeout.
+   * Every retry reduces the available timeout so that this method should not
+   * wait longer than the total timeout (within a small slack allowance).
+   *
+   * @param endpoint the path the api call relative to hostname
+   * @param body the JSON value to post (this is usually a JSON objecT)
+   * @param retry whether or not to retry on connection failure
+   * @return Left(Error Message) or Right(Status Code, Response as UTF-8 
String)
+   */
+  def post(endpoint: String, body: JsValue, retry: Boolean)(
+implicit tid: TransactionId): Future[Either[ContainerHttpError, 
ContainerResponse]] = {
+
+//create the request
+val req = Marshal(body).to[MessageEntity].map { b =>
+  //DO NOT reuse the connection (in case of paused containers)
+  //For details on Connection: Close handling, see:
+  // - 
https://doc.akka.io/docs/akka-http/current/common/http-model.html#http-headers
+  // - 

[GitHub] tysonnorris commented on a change in pull request #3812: ContainerClient + akka http alternative to HttpUtils

2018-07-19 Thread GitBox
tysonnorris commented on a change in pull request #3812: ContainerClient + akka 
http alternative to HttpUtils
URL: 
https://github.com/apache/incubator-openwhisk/pull/3812#discussion_r203773479
 
 

 ##
 File path: 
common/scala/src/main/scala/whisk/core/containerpool/ContainerClient.scala
 ##
 @@ -0,0 +1,212 @@
+/*
+ * 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 whisk.core.containerpool
+
+import akka.actor.ActorSystem
+import akka.http.scaladsl.marshallers.sprayjson.SprayJsonSupport._
+import akka.http.scaladsl.marshalling.Marshal
+import akka.http.scaladsl.model.HttpMethods
+import akka.http.scaladsl.model.HttpRequest
+import akka.http.scaladsl.model.HttpResponse
+import akka.http.scaladsl.model.MessageEntity
+import akka.http.scaladsl.model.StatusCodes
+import akka.http.scaladsl.model.headers.Connection
+import akka.http.scaladsl.unmarshalling.Unmarshal
+import akka.stream.StreamTcpException
+import akka.stream.scaladsl.Sink
+import akka.stream.scaladsl.Source
+import akka.util.ByteString
+import scala.concurrent.Await
+import scala.concurrent.ExecutionContext
+import scala.concurrent.Future
+import scala.concurrent.TimeoutException
+import scala.concurrent.duration._
+import scala.util.Try
+import spray.json._
+import whisk.common.Logging
+import whisk.common.TransactionId
+import whisk.core.entity.ActivationResponse.ContainerHttpError
+import whisk.core.entity.ActivationResponse._
+import whisk.core.entity.ByteSize
+import whisk.core.entity.size.SizeLong
+import whisk.http.PoolingRestClient
+
+trait ContainerClient {
+  def close(): Unit
+  def post(endpoint: String, body: JsValue, retry: Boolean)(
+implicit tid: TransactionId): Future[Either[ContainerHttpError, 
ContainerResponse]]
+
+}
+
+/**
+ * This HTTP client is used only in the invoker to communicate with the action 
container.
+ * It allows to POST a JSON object and receive JSON object back; that is the
+ * content type and the accept headers are both 'application/json.
+ * The reason we still use this class for the action container is a mysterious 
hang
+ * in the Akka http client where a future fails to properly timeout and we 
have not
+ * determined why that is.
+ *
+ * @param hostname the host name
+ * @param timeout the timeout in msecs to wait for a response
+ * @param maxResponse the maximum size in bytes the connection will accept
+ * @param queueSize once all connections are used, how big of queue to allow 
for additional requests
+ * @param retryInterval duration between retries for TCP connection errors
+ */
+protected class PoolingContainerClient(
+  hostname: String,
+  port: Int,
+  timeout: FiniteDuration,
+  maxResponse: ByteSize,
+  queueSize: Int,
+  retryInterval: FiniteDuration = 100.milliseconds)(implicit logging: Logging, 
as: ActorSystem)
+extends PoolingRestClient("http", hostname, port, queueSize, timeout = 
Some(timeout))
+with ContainerClient
+with AutoCloseable {
+
+  def close() = shutdown()
+
+  /**
+   * Posts to hostname/endpoint the given JSON object.
+   * Waits up to timeout before aborting on a good connection.
+   * If the endpoint is not ready, retry up to timeout.
+   * Every retry reduces the available timeout so that this method should not
+   * wait longer than the total timeout (within a small slack allowance).
+   *
+   * @param endpoint the path the api call relative to hostname
+   * @param body the JSON value to post (this is usually a JSON objecT)
+   * @param retry whether or not to retry on connection failure
+   * @return Left(Error Message) or Right(Status Code, Response as UTF-8 
String)
+   */
+  def post(endpoint: String, body: JsValue, retry: Boolean)(
+implicit tid: TransactionId): Future[Either[ContainerHttpError, 
ContainerResponse]] = {
+
+//create the request
+val req = Marshal(body).to[MessageEntity].map { b =>
+  //DO NOT reuse the connection (in case of paused containers)
+  //For details on Connection: Close handling, see:
+  // - 
https://doc.akka.io/docs/akka-http/current/common/http-model.html#http-headers
+  // - 

[GitHub] tysonnorris commented on a change in pull request #3812: ContainerClient + akka http alternative to HttpUtils

2018-07-18 Thread GitBox
tysonnorris commented on a change in pull request #3812: ContainerClient + akka 
http alternative to HttpUtils
URL: 
https://github.com/apache/incubator-openwhisk/pull/3812#discussion_r203404011
 
 

 ##
 File path: common/scala/src/main/scala/whisk/core/containerpool/HttpUtils.scala
 ##
 @@ -80,20 +85,17 @@ protected class HttpUtils(hostname: String, timeout: 
FiniteDuration, maxResponse
* @return Left(Error Message) or Right(Status Code, Response as UTF-8 
String)
*/
   def post(endpoint: String, body: JsValue, retry: Boolean)(
-implicit tid: TransactionId): Either[ContainerHttpError, 
ContainerResponse] = {
+implicit tid: TransactionId): Future[Either[ContainerHttpError, 
ContainerResponse]] = {
 val entity = new StringEntity(body.compactPrint, StandardCharsets.UTF_8)
 entity.setContentType("application/json")
 
 val request = new HttpPost(baseUri.setPath(endpoint).build)
 request.addHeader(HttpHeaders.ACCEPT, "application/json")
 request.setEntity(entity)
 
-execute(request, timeout, maxConcurrent, retry)
+Future { execute(request, timeout, maxConcurrent, retry) }
 
 Review comment:
   Instead of `Future.successful`?


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


[GitHub] tysonnorris commented on a change in pull request #3812: ContainerClient + akka http alternative to HttpUtils

2018-07-18 Thread GitBox
tysonnorris commented on a change in pull request #3812: ContainerClient + akka 
http alternative to HttpUtils
URL: 
https://github.com/apache/incubator-openwhisk/pull/3812#discussion_r203402781
 
 

 ##
 File path: 
common/scala/src/main/scala/whisk/core/containerpool/ContainerClient.scala
 ##
 @@ -0,0 +1,223 @@
+/*
+ * 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 whisk.core.containerpool
+
+import akka.actor.ActorSystem
+import akka.http.scaladsl.marshallers.sprayjson.SprayJsonSupport._
+import akka.http.scaladsl.marshalling.Marshal
+import akka.http.scaladsl.model.HttpMethods
+import akka.http.scaladsl.model.HttpRequest
+import akka.http.scaladsl.model.HttpResponse
+import akka.http.scaladsl.model.MessageEntity
+import akka.http.scaladsl.model.StatusCodes
+import akka.http.scaladsl.model.headers.Connection
+import akka.http.scaladsl.unmarshalling.Unmarshal
+import akka.stream.StreamTcpException
+import akka.stream.scaladsl.Sink
+import akka.stream.scaladsl.Source
+import akka.util.ByteString
+import scala.concurrent.Await
+import scala.concurrent.ExecutionContext
+import scala.concurrent.Future
+import scala.concurrent.TimeoutException
+import scala.concurrent.duration._
+import scala.util.Try
+import scala.util.control.NonFatal
+import spray.json._
+import whisk.common.Logging
+import whisk.common.TransactionId
+import whisk.core.entity.ActivationResponse.ContainerHttpError
+import whisk.core.entity.ActivationResponse._
+import whisk.core.entity.ByteSize
+import whisk.core.entity.size.SizeLong
+import whisk.http.PoolingRestClient
+
+trait ContainerClient {
+  def close(): Unit
+  def post(endpoint: String, body: JsValue, retry: Boolean)(
+implicit tid: TransactionId): Future[Either[ContainerHttpError, 
ContainerResponse]]
+
+}
+
+/**
+ * This HTTP client is used only in the invoker to communicate with the action 
container.
+ * It allows to POST a JSON object and receive JSON object back; that is the
+ * content type and the accept headers are both 'application/json.
+ * The reason we still use this class for the action container is a mysterious 
hang
+ * in the Akka http client where a future fails to properly timeout and we 
have not
+ * determined why that is.
+ *
+ * @param hostname the host name
+ * @param timeout the timeout in msecs to wait for a response
+ * @param maxResponse the maximum size in bytes the connection will accept
+ * @param queueSize once all connections are used, how big of queue to allow 
for additional requests
+ * @param retryInterval duration between retries for TCP connection errors
+ */
+protected class PoolingContainerClient(
+  hostname: String,
+  port: Int,
+  timeout: FiniteDuration,
+  maxResponse: ByteSize,
+  queueSize: Int,
+  retryInterval: FiniteDuration = 100.milliseconds)(implicit logging: Logging, 
as: ActorSystem)
+extends PoolingRestClient("http", hostname, port, queueSize, timeout = 
Some(timeout))
+with ContainerClient
+with AutoCloseable {
+
+  def close() = shutdown()
+
+  /**
+   * Posts to hostname/endpoint the given JSON object.
+   * Waits up to timeout before aborting on a good connection.
+   * If the endpoint is not ready, retry up to timeout.
+   * Every retry reduces the available timeout so that this method should not
+   * wait longer than the total timeout (within a small slack allowance).
+   *
+   * @param endpoint the path the api call relative to hostname
+   * @param body the JSON value to post (this is usually a JSON objecT)
+   * @param retry whether or not to retry on connection failure
+   * @return Left(Error Message) or Right(Status Code, Response as UTF-8 
String)
+   */
+  def post(endpoint: String, body: JsValue, retry: Boolean)(
+implicit tid: TransactionId): Future[Either[ContainerHttpError, 
ContainerResponse]] = {
+
+//create the request
+val req = Marshal(body).to[MessageEntity].map { b =>
+  //DO NOT reuse the connection (in case of paused containers)
+  //For details on Connection: Close handling, see:
+  // - 
https://doc.akka.io/docs/akka-http/current/common/http-model.html#http-headers
+  // - 

[GitHub] tysonnorris commented on a change in pull request #3812: ContainerClient + akka http alternative to HttpUtils

2018-07-17 Thread GitBox
tysonnorris commented on a change in pull request #3812: ContainerClient + akka 
http alternative to HttpUtils
URL: 
https://github.com/apache/incubator-openwhisk/pull/3812#discussion_r203219670
 
 

 ##
 File path: 
common/scala/src/main/scala/whisk/core/containerpool/ContainerClient.scala
 ##
 @@ -0,0 +1,212 @@
+/*
+ * 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 whisk.core.containerpool
+
+import akka.actor.ActorSystem
+import akka.http.scaladsl.marshallers.sprayjson.SprayJsonSupport._
+import akka.http.scaladsl.marshalling.Marshal
+import akka.http.scaladsl.model.HttpMethods
+import akka.http.scaladsl.model.HttpRequest
+import akka.http.scaladsl.model.HttpResponse
+import akka.http.scaladsl.model.MessageEntity
+import akka.http.scaladsl.model.StatusCodes
+import akka.http.scaladsl.model.headers.Connection
+import akka.http.scaladsl.unmarshalling.Unmarshal
+import akka.stream.StreamTcpException
+import akka.stream.scaladsl.Sink
+import akka.stream.scaladsl.Source
+import akka.util.ByteString
+import scala.concurrent.Await
+import scala.concurrent.ExecutionContext
+import scala.concurrent.Future
+import scala.concurrent.TimeoutException
+import scala.concurrent.duration._
+import scala.util.Try
+import spray.json._
+import whisk.common.Logging
+import whisk.common.TransactionId
+import whisk.core.entity.ActivationResponse.ContainerHttpError
+import whisk.core.entity.ActivationResponse._
+import whisk.core.entity.ByteSize
+import whisk.core.entity.size.SizeLong
+import whisk.http.PoolingRestClient
+
+trait ContainerClient {
+  def close(): Unit
+  def post(endpoint: String, body: JsValue, retry: Boolean)(
+implicit tid: TransactionId): Future[Either[ContainerHttpError, 
ContainerResponse]]
+
+}
+
+/**
+ * This HTTP client is used only in the invoker to communicate with the action 
container.
+ * It allows to POST a JSON object and receive JSON object back; that is the
+ * content type and the accept headers are both 'application/json.
+ * The reason we still use this class for the action container is a mysterious 
hang
+ * in the Akka http client where a future fails to properly timeout and we 
have not
+ * determined why that is.
+ *
+ * @param hostname the host name
+ * @param timeout the timeout in msecs to wait for a response
+ * @param maxResponse the maximum size in bytes the connection will accept
+ * @param queueSize once all connections are used, how big of queue to allow 
for additional requests
+ * @param retryInterval duration between retries for TCP connection errors
+ */
+protected class PoolingContainerClient(
+  hostname: String,
+  port: Int,
+  timeout: FiniteDuration,
+  maxResponse: ByteSize,
+  queueSize: Int,
+  retryInterval: FiniteDuration = 100.milliseconds)(implicit logging: Logging, 
as: ActorSystem)
+extends PoolingRestClient("http", hostname, port, queueSize, timeout = 
Some(timeout))
+with ContainerClient
+with AutoCloseable {
+
+  def close() = shutdown()
 
 Review comment:
   @markusthoemmes WDYT? This may affect use of `AutoCloseable` - for now I 
will return a Unit


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


[GitHub] tysonnorris commented on a change in pull request #3812: ContainerClient + akka http alternative to HttpUtils

2018-07-17 Thread GitBox
tysonnorris commented on a change in pull request #3812: ContainerClient + akka 
http alternative to HttpUtils
URL: 
https://github.com/apache/incubator-openwhisk/pull/3812#discussion_r203219371
 
 

 ##
 File path: common/scala/src/main/scala/whisk/http/PoolingRestClient.scala
 ##
 @@ -43,29 +45,36 @@ class PoolingRestClient(
   host: String,
   port: Int,
   queueSize: Int,
-  httpFlow: Option[Flow[(HttpRequest, Promise[HttpResponse]), 
(Try[HttpResponse], Promise[HttpResponse]), Any]] = None)(
-  implicit system: ActorSystem) {
+  httpFlow: Option[Flow[(HttpRequest, Promise[HttpResponse]), 
(Try[HttpResponse], Promise[HttpResponse]), Any]] = None,
+  timeout: Option[FiniteDuration] = None)(implicit system: ActorSystem) {
   require(protocol == "http" || protocol == "https", "Protocol must be one of 
{ http, https }.")
 
   protected implicit val context: ExecutionContext = system.dispatcher
   protected implicit val materializer: ActorMaterializer = ActorMaterializer()
 
+  //if specified, override the ClientConnection idle-timeout value
+  private val timeoutSettings =
+ConnectionPoolSettings(system.settings.config)
+  .withConnectionSettings(if (timeout.isDefined) {
+ClientConnectionSettings(system.settings.config)
+  .withIdleTimeout(timeout.get)
+  } else { ClientConnectionSettings(system.settings.config) })
 
 Review comment:
   nice


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


[GitHub] tysonnorris commented on a change in pull request #3812: ContainerClient + akka http alternative to HttpUtils

2018-07-17 Thread GitBox
tysonnorris commented on a change in pull request #3812: ContainerClient + akka 
http alternative to HttpUtils
URL: 
https://github.com/apache/incubator-openwhisk/pull/3812#discussion_r203218558
 
 

 ##
 File path: 
common/scala/src/main/scala/whisk/core/database/CouchDbRestStore.scala
 ##
 @@ -513,7 +512,7 @@ class CouchDbRestStore[DocumentAbstraction <: 
DocumentSerializer](dbProtocol: St
   .getOrElse(Future.successful(true)) // For CouchDB it is expected that 
the entire document is deleted.
 
   override def shutdown(): Unit = {
-Await.ready(client.shutdown(), 1.minute)
+client.shutdown()
 
 Review comment:
   Yes! 


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


[GitHub] tysonnorris commented on a change in pull request #3812: ContainerClient + akka http alternative to HttpUtils

2018-07-17 Thread GitBox
tysonnorris commented on a change in pull request #3812: ContainerClient + akka 
http alternative to HttpUtils
URL: 
https://github.com/apache/incubator-openwhisk/pull/3812#discussion_r203218116
 
 

 ##
 File path: 
common/scala/src/main/scala/whisk/core/containerpool/ContainerClient.scala
 ##
 @@ -0,0 +1,212 @@
+/*
+ * 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 whisk.core.containerpool
+
+import akka.actor.ActorSystem
+import akka.http.scaladsl.marshallers.sprayjson.SprayJsonSupport._
+import akka.http.scaladsl.marshalling.Marshal
+import akka.http.scaladsl.model.HttpMethods
+import akka.http.scaladsl.model.HttpRequest
+import akka.http.scaladsl.model.HttpResponse
+import akka.http.scaladsl.model.MessageEntity
+import akka.http.scaladsl.model.StatusCodes
+import akka.http.scaladsl.model.headers.Connection
+import akka.http.scaladsl.unmarshalling.Unmarshal
+import akka.stream.StreamTcpException
+import akka.stream.scaladsl.Sink
+import akka.stream.scaladsl.Source
+import akka.util.ByteString
+import scala.concurrent.Await
+import scala.concurrent.ExecutionContext
+import scala.concurrent.Future
+import scala.concurrent.TimeoutException
+import scala.concurrent.duration._
+import scala.util.Try
+import spray.json._
+import whisk.common.Logging
+import whisk.common.TransactionId
+import whisk.core.entity.ActivationResponse.ContainerHttpError
+import whisk.core.entity.ActivationResponse._
+import whisk.core.entity.ByteSize
+import whisk.core.entity.size.SizeLong
+import whisk.http.PoolingRestClient
+
+trait ContainerClient {
+  def close(): Unit
+  def post(endpoint: String, body: JsValue, retry: Boolean)(
+implicit tid: TransactionId): Future[Either[ContainerHttpError, 
ContainerResponse]]
+
+}
+
+/**
+ * This HTTP client is used only in the invoker to communicate with the action 
container.
+ * It allows to POST a JSON object and receive JSON object back; that is the
+ * content type and the accept headers are both 'application/json.
+ * The reason we still use this class for the action container is a mysterious 
hang
+ * in the Akka http client where a future fails to properly timeout and we 
have not
+ * determined why that is.
+ *
+ * @param hostname the host name
+ * @param timeout the timeout in msecs to wait for a response
+ * @param maxResponse the maximum size in bytes the connection will accept
+ * @param queueSize once all connections are used, how big of queue to allow 
for additional requests
+ * @param retryInterval duration between retries for TCP connection errors
+ */
+protected class PoolingContainerClient(
+  hostname: String,
+  port: Int,
+  timeout: FiniteDuration,
+  maxResponse: ByteSize,
+  queueSize: Int,
+  retryInterval: FiniteDuration = 100.milliseconds)(implicit logging: Logging, 
as: ActorSystem)
+extends PoolingRestClient("http", hostname, port, queueSize, timeout = 
Some(timeout))
+with ContainerClient
+with AutoCloseable {
+
+  def close() = shutdown()
+
+  /**
+   * Posts to hostname/endpoint the given JSON object.
+   * Waits up to timeout before aborting on a good connection.
+   * If the endpoint is not ready, retry up to timeout.
+   * Every retry reduces the available timeout so that this method should not
+   * wait longer than the total timeout (within a small slack allowance).
+   *
+   * @param endpoint the path the api call relative to hostname
+   * @param body the JSON value to post (this is usually a JSON objecT)
+   * @param retry whether or not to retry on connection failure
+   * @return Left(Error Message) or Right(Status Code, Response as UTF-8 
String)
+   */
+  def post(endpoint: String, body: JsValue, retry: Boolean)(
+implicit tid: TransactionId): Future[Either[ContainerHttpError, 
ContainerResponse]] = {
+
+//create the request
+val req = Marshal(body).to[MessageEntity].map { b =>
+  //DO NOT reuse the connection (in case of paused containers)
+  //For details on Connection: Close handling, see:
+  // - 
https://doc.akka.io/docs/akka-http/current/common/http-model.html#http-headers
+  // - 

[GitHub] tysonnorris commented on a change in pull request #3812: ContainerClient + akka http alternative to HttpUtils

2018-07-17 Thread GitBox
tysonnorris commented on a change in pull request #3812: ContainerClient + akka 
http alternative to HttpUtils
URL: 
https://github.com/apache/incubator-openwhisk/pull/3812#discussion_r203217847
 
 

 ##
 File path: 
common/scala/src/main/scala/whisk/core/containerpool/ContainerClient.scala
 ##
 @@ -0,0 +1,212 @@
+/*
+ * 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 whisk.core.containerpool
+
+import akka.actor.ActorSystem
+import akka.http.scaladsl.marshallers.sprayjson.SprayJsonSupport._
+import akka.http.scaladsl.marshalling.Marshal
+import akka.http.scaladsl.model.HttpMethods
+import akka.http.scaladsl.model.HttpRequest
+import akka.http.scaladsl.model.HttpResponse
+import akka.http.scaladsl.model.MessageEntity
+import akka.http.scaladsl.model.StatusCodes
+import akka.http.scaladsl.model.headers.Connection
+import akka.http.scaladsl.unmarshalling.Unmarshal
+import akka.stream.StreamTcpException
+import akka.stream.scaladsl.Sink
+import akka.stream.scaladsl.Source
+import akka.util.ByteString
+import scala.concurrent.Await
+import scala.concurrent.ExecutionContext
+import scala.concurrent.Future
+import scala.concurrent.TimeoutException
+import scala.concurrent.duration._
+import scala.util.Try
+import spray.json._
+import whisk.common.Logging
+import whisk.common.TransactionId
+import whisk.core.entity.ActivationResponse.ContainerHttpError
+import whisk.core.entity.ActivationResponse._
+import whisk.core.entity.ByteSize
+import whisk.core.entity.size.SizeLong
+import whisk.http.PoolingRestClient
+
+trait ContainerClient {
+  def close(): Unit
+  def post(endpoint: String, body: JsValue, retry: Boolean)(
+implicit tid: TransactionId): Future[Either[ContainerHttpError, 
ContainerResponse]]
+
+}
+
+/**
+ * This HTTP client is used only in the invoker to communicate with the action 
container.
+ * It allows to POST a JSON object and receive JSON object back; that is the
+ * content type and the accept headers are both 'application/json.
+ * The reason we still use this class for the action container is a mysterious 
hang
+ * in the Akka http client where a future fails to properly timeout and we 
have not
+ * determined why that is.
+ *
+ * @param hostname the host name
+ * @param timeout the timeout in msecs to wait for a response
+ * @param maxResponse the maximum size in bytes the connection will accept
+ * @param queueSize once all connections are used, how big of queue to allow 
for additional requests
+ * @param retryInterval duration between retries for TCP connection errors
+ */
+protected class PoolingContainerClient(
+  hostname: String,
+  port: Int,
+  timeout: FiniteDuration,
+  maxResponse: ByteSize,
+  queueSize: Int,
+  retryInterval: FiniteDuration = 100.milliseconds)(implicit logging: Logging, 
as: ActorSystem)
+extends PoolingRestClient("http", hostname, port, queueSize, timeout = 
Some(timeout))
+with ContainerClient
+with AutoCloseable {
+
+  def close() = shutdown()
+
+  /**
+   * Posts to hostname/endpoint the given JSON object.
+   * Waits up to timeout before aborting on a good connection.
+   * If the endpoint is not ready, retry up to timeout.
+   * Every retry reduces the available timeout so that this method should not
+   * wait longer than the total timeout (within a small slack allowance).
+   *
+   * @param endpoint the path the api call relative to hostname
+   * @param body the JSON value to post (this is usually a JSON objecT)
+   * @param retry whether or not to retry on connection failure
+   * @return Left(Error Message) or Right(Status Code, Response as UTF-8 
String)
+   */
+  def post(endpoint: String, body: JsValue, retry: Boolean)(
+implicit tid: TransactionId): Future[Either[ContainerHttpError, 
ContainerResponse]] = {
+
+//create the request
+val req = Marshal(body).to[MessageEntity].map { b =>
+  //DO NOT reuse the connection (in case of paused containers)
+  //For details on Connection: Close handling, see:
+  // - 
https://doc.akka.io/docs/akka-http/current/common/http-model.html#http-headers
+  // - 

[GitHub] tysonnorris commented on a change in pull request #3812: ContainerClient + akka http alternative to HttpUtils

2018-07-17 Thread GitBox
tysonnorris commented on a change in pull request #3812: ContainerClient + akka 
http alternative to HttpUtils
URL: 
https://github.com/apache/incubator-openwhisk/pull/3812#discussion_r203217369
 
 

 ##
 File path: 
common/scala/src/main/scala/whisk/core/containerpool/ContainerClient.scala
 ##
 @@ -0,0 +1,212 @@
+/*
+ * 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 whisk.core.containerpool
+
+import akka.actor.ActorSystem
+import akka.http.scaladsl.marshallers.sprayjson.SprayJsonSupport._
+import akka.http.scaladsl.marshalling.Marshal
+import akka.http.scaladsl.model.HttpMethods
+import akka.http.scaladsl.model.HttpRequest
+import akka.http.scaladsl.model.HttpResponse
+import akka.http.scaladsl.model.MessageEntity
+import akka.http.scaladsl.model.StatusCodes
+import akka.http.scaladsl.model.headers.Connection
+import akka.http.scaladsl.unmarshalling.Unmarshal
+import akka.stream.StreamTcpException
+import akka.stream.scaladsl.Sink
+import akka.stream.scaladsl.Source
+import akka.util.ByteString
+import scala.concurrent.Await
+import scala.concurrent.ExecutionContext
+import scala.concurrent.Future
+import scala.concurrent.TimeoutException
+import scala.concurrent.duration._
+import scala.util.Try
+import spray.json._
+import whisk.common.Logging
+import whisk.common.TransactionId
+import whisk.core.entity.ActivationResponse.ContainerHttpError
+import whisk.core.entity.ActivationResponse._
+import whisk.core.entity.ByteSize
+import whisk.core.entity.size.SizeLong
+import whisk.http.PoolingRestClient
+
+trait ContainerClient {
+  def close(): Unit
+  def post(endpoint: String, body: JsValue, retry: Boolean)(
+implicit tid: TransactionId): Future[Either[ContainerHttpError, 
ContainerResponse]]
+
+}
+
+/**
+ * This HTTP client is used only in the invoker to communicate with the action 
container.
+ * It allows to POST a JSON object and receive JSON object back; that is the
+ * content type and the accept headers are both 'application/json.
+ * The reason we still use this class for the action container is a mysterious 
hang
+ * in the Akka http client where a future fails to properly timeout and we 
have not
+ * determined why that is.
+ *
+ * @param hostname the host name
+ * @param timeout the timeout in msecs to wait for a response
+ * @param maxResponse the maximum size in bytes the connection will accept
+ * @param queueSize once all connections are used, how big of queue to allow 
for additional requests
+ * @param retryInterval duration between retries for TCP connection errors
+ */
+protected class PoolingContainerClient(
+  hostname: String,
+  port: Int,
+  timeout: FiniteDuration,
+  maxResponse: ByteSize,
+  queueSize: Int,
+  retryInterval: FiniteDuration = 100.milliseconds)(implicit logging: Logging, 
as: ActorSystem)
+extends PoolingRestClient("http", hostname, port, queueSize, timeout = 
Some(timeout))
+with ContainerClient
+with AutoCloseable {
+
+  def close() = shutdown()
+
+  /**
+   * Posts to hostname/endpoint the given JSON object.
+   * Waits up to timeout before aborting on a good connection.
+   * If the endpoint is not ready, retry up to timeout.
+   * Every retry reduces the available timeout so that this method should not
+   * wait longer than the total timeout (within a small slack allowance).
+   *
+   * @param endpoint the path the api call relative to hostname
+   * @param body the JSON value to post (this is usually a JSON objecT)
+   * @param retry whether or not to retry on connection failure
+   * @return Left(Error Message) or Right(Status Code, Response as UTF-8 
String)
+   */
+  def post(endpoint: String, body: JsValue, retry: Boolean)(
+implicit tid: TransactionId): Future[Either[ContainerHttpError, 
ContainerResponse]] = {
+
+//create the request
+val req = Marshal(body).to[MessageEntity].map { b =>
+  //DO NOT reuse the connection (in case of paused containers)
+  //For details on Connection: Close handling, see:
+  // - 
https://doc.akka.io/docs/akka-http/current/common/http-model.html#http-headers
+  // - 

[GitHub] tysonnorris commented on a change in pull request #3812: ContainerClient + akka http alternative to HttpUtils

2018-07-16 Thread GitBox
tysonnorris commented on a change in pull request #3812: ContainerClient + akka 
http alternative to HttpUtils
URL: 
https://github.com/apache/incubator-openwhisk/pull/3812#discussion_r202899653
 
 

 ##
 File path: 
common/scala/src/main/scala/whisk/core/containerpool/ContainerClient.scala
 ##
 @@ -0,0 +1,213 @@
+/*
+ * 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 whisk.core.containerpool
+
+import akka.actor.ActorSystem
+import akka.http.scaladsl.marshallers.sprayjson.SprayJsonSupport._
+import akka.http.scaladsl.marshalling.Marshal
+import akka.http.scaladsl.model.HttpMethods
+import akka.http.scaladsl.model.HttpRequest
+import akka.http.scaladsl.model.HttpResponse
+import akka.http.scaladsl.model.MessageEntity
+import akka.http.scaladsl.model.StatusCodes
+import akka.http.scaladsl.model.headers.Connection
+import akka.http.scaladsl.unmarshalling.Unmarshal
+import akka.stream.StreamTcpException
+import akka.stream.scaladsl.Sink
+import akka.stream.scaladsl.Source
+import akka.util.ByteString
+import scala.concurrent.Await
+import scala.concurrent.ExecutionContext
+import scala.concurrent.Future
+import scala.concurrent.TimeoutException
+import scala.concurrent.duration._
+import scala.util.Try
+import scala.util.control.NonFatal
+import spray.json._
+import whisk.common.Logging
+import whisk.common.TransactionId
+import whisk.core.entity.ActivationResponse.ContainerHttpError
+import whisk.core.entity.ActivationResponse._
+import whisk.core.entity.ByteSize
+import whisk.core.entity.size.SizeLong
+import whisk.http.PoolingRestClient
+
+trait ContainerClient {
+  def close(): Unit
+  def post(endpoint: String, body: JsValue, retry: Boolean)(
+implicit tid: TransactionId): Future[Either[ContainerHttpError, 
ContainerResponse]]
+
+}
+
+/**
+ * This HTTP client is used only in the invoker to communicate with the action 
container.
+ * It allows to POST a JSON object and receive JSON object back; that is the
+ * content type and the accept headers are both 'application/json.
+ * The reason we still use this class for the action container is a mysterious 
hang
+ * in the Akka http client where a future fails to properly timeout and we 
have not
+ * determined why that is.
+ *
+ * @param hostname the host name
+ * @param timeout the timeout in msecs to wait for a response
+ * @param maxResponse the maximum size in bytes the connection will accept
+ * @param queueSize once all connections are used, how big of queue to allow 
for additional requests
+ * @param retryInterval duration between retries for TCP connection errors
+ */
+protected class PoolingContainerClient(
+  hostname: String,
+  port: Int,
+  timeout: FiniteDuration,
+  maxResponse: ByteSize,
+  queueSize: Int,
+  retryInterval: FiniteDuration = 100.milliseconds)(implicit logging: Logging, 
as: ActorSystem)
+extends PoolingRestClient("http", hostname, port, queueSize, timeout = 
Some(timeout))
+with ContainerClient
+with AutoCloseable {
+
+  def close() = shutdown()
+
+  /**
+   * Posts to hostname/endpoint the given JSON object.
+   * Waits up to timeout before aborting on a good connection.
+   * If the endpoint is not ready, retry up to timeout.
+   * Every retry reduces the available timeout so that this method should not
+   * wait longer than the total timeout (within a small slack allowance).
+   *
+   * @param endpoint the path the api call relative to hostname
+   * @param body the JSON value to post (this is usually a JSON objecT)
+   * @param retry whether or not to retry on connection failure
+   * @return Left(Error Message) or Right(Status Code, Response as UTF-8 
String)
+   */
+  def post(endpoint: String, body: JsValue, retry: Boolean)(
+implicit tid: TransactionId): Future[Either[ContainerHttpError, 
ContainerResponse]] = {
+
+//create the request
+val req = Marshal(body).to[MessageEntity].map { b =>
+  //DO NOT reuse the connection (in case of paused containers)
+  //For details on Connection: Close handling, see:
+  // - 
https://doc.akka.io/docs/akka-http/current/common/http-model.html#http-headers
+  // - 

[GitHub] tysonnorris commented on a change in pull request #3812: ContainerClient + akka http alternative to HttpUtils

2018-07-16 Thread GitBox
tysonnorris commented on a change in pull request #3812: ContainerClient + akka 
http alternative to HttpUtils
URL: 
https://github.com/apache/incubator-openwhisk/pull/3812#discussion_r202899222
 
 

 ##
 File path: 
common/scala/src/main/scala/whisk/core/containerpool/ContainerClient.scala
 ##
 @@ -0,0 +1,212 @@
+/*
+ * 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 whisk.core.containerpool
+
+import akka.actor.ActorSystem
+import akka.http.scaladsl.marshallers.sprayjson.SprayJsonSupport._
+import akka.http.scaladsl.marshalling.Marshal
+import akka.http.scaladsl.model.HttpMethods
+import akka.http.scaladsl.model.HttpRequest
+import akka.http.scaladsl.model.HttpResponse
+import akka.http.scaladsl.model.MessageEntity
+import akka.http.scaladsl.model.StatusCodes
+import akka.http.scaladsl.model.headers.Connection
+import akka.http.scaladsl.unmarshalling.Unmarshal
+import akka.stream.StreamTcpException
+import akka.stream.scaladsl.Sink
+import akka.stream.scaladsl.Source
+import akka.util.ByteString
+import scala.concurrent.Await
+import scala.concurrent.ExecutionContext
+import scala.concurrent.Future
+import scala.concurrent.TimeoutException
+import scala.concurrent.duration._
+import scala.util.Try
+import spray.json._
+import whisk.common.Logging
+import whisk.common.TransactionId
+import whisk.core.entity.ActivationResponse.ContainerHttpError
+import whisk.core.entity.ActivationResponse._
+import whisk.core.entity.ByteSize
+import whisk.core.entity.size.SizeLong
+import whisk.http.PoolingRestClient
+
+trait ContainerClient {
+  def close(): Unit
+  def post(endpoint: String, body: JsValue, retry: Boolean)(
+implicit tid: TransactionId): Future[Either[ContainerHttpError, 
ContainerResponse]]
+
+}
+
+/**
+ * This HTTP client is used only in the invoker to communicate with the action 
container.
+ * It allows to POST a JSON object and receive JSON object back; that is the
+ * content type and the accept headers are both 'application/json.
+ * The reason we still use this class for the action container is a mysterious 
hang
+ * in the Akka http client where a future fails to properly timeout and we 
have not
+ * determined why that is.
+ *
+ * @param hostname the host name
+ * @param timeout the timeout in msecs to wait for a response
+ * @param maxResponse the maximum size in bytes the connection will accept
+ * @param queueSize once all connections are used, how big of queue to allow 
for additional requests
+ * @param retryInterval duration between retries for TCP connection errors
+ */
+protected class PoolingContainerClient(
+  hostname: String,
+  port: Int,
+  timeout: FiniteDuration,
+  maxResponse: ByteSize,
+  queueSize: Int,
+  retryInterval: FiniteDuration = 100.milliseconds)(implicit logging: Logging, 
as: ActorSystem)
+extends PoolingRestClient("http", hostname, port, queueSize, timeout = 
Some(timeout))
+with ContainerClient
+with AutoCloseable {
+
+  def close() = shutdown()
+
+  /**
+   * Posts to hostname/endpoint the given JSON object.
+   * Waits up to timeout before aborting on a good connection.
+   * If the endpoint is not ready, retry up to timeout.
+   * Every retry reduces the available timeout so that this method should not
+   * wait longer than the total timeout (within a small slack allowance).
+   *
+   * @param endpoint the path the api call relative to hostname
+   * @param body the JSON value to post (this is usually a JSON objecT)
+   * @param retry whether or not to retry on connection failure
+   * @return Left(Error Message) or Right(Status Code, Response as UTF-8 
String)
+   */
+  def post(endpoint: String, body: JsValue, retry: Boolean)(
+implicit tid: TransactionId): Future[Either[ContainerHttpError, 
ContainerResponse]] = {
+
+//create the request
+val req = Marshal(body).to[MessageEntity].map { b =>
+  //DO NOT reuse the connection (in case of paused containers)
+  //For details on Connection: Close handling, see:
+  // - 
https://doc.akka.io/docs/akka-http/current/common/http-model.html#http-headers
+  // - 

[GitHub] tysonnorris commented on a change in pull request #3812: ContainerClient + akka http alternative to HttpUtils

2018-07-16 Thread GitBox
tysonnorris commented on a change in pull request #3812: ContainerClient + akka 
http alternative to HttpUtils
URL: 
https://github.com/apache/incubator-openwhisk/pull/3812#discussion_r202895105
 
 

 ##
 File path: 
common/scala/src/main/scala/whisk/core/containerpool/ContainerClient.scala
 ##
 @@ -0,0 +1,240 @@
+/*
+ * 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 whisk.core.containerpool
+
+import akka.actor.ActorSystem
+import akka.http.scaladsl.marshallers.sprayjson.SprayJsonSupport._
+import akka.http.scaladsl.marshalling.Marshal
+import akka.http.scaladsl.model.HttpMethods
+import akka.http.scaladsl.model.HttpRequest
+import akka.http.scaladsl.model.HttpResponse
+import akka.http.scaladsl.model.MessageEntity
+import akka.http.scaladsl.model.StatusCodes
+import akka.http.scaladsl.model.headers.Connection
+import akka.http.scaladsl.unmarshalling.Unmarshal
+import akka.stream.scaladsl.Sink
+import akka.stream.scaladsl.Source
+import akka.util.ByteString
+import scala.concurrent.Await
+import scala.concurrent.ExecutionContext
+import scala.concurrent.Future
+import scala.concurrent.Promise
+import scala.concurrent.TimeoutException
+import scala.concurrent.duration._
+import scala.util.Try
+import scala.util.control.NoStackTrace
+import spray.json._
+import whisk.common.Logging
+import whisk.common.TransactionId
+import whisk.core.entity.ActivationResponse.ContainerHttpError
+import whisk.core.entity.ActivationResponse._
+import whisk.core.entity.ByteSize
+import whisk.core.entity.size.SizeLong
+import whisk.http.PoolingRestClient
+
+trait ContainerClient {
+  def close(): Unit
+  def post(endpoint: String, body: JsValue, retry: Boolean)(
+implicit tid: TransactionId): Future[Either[ContainerHttpError, 
ContainerResponse]]
+
+}
+
+/**
+ * This HTTP client is used only in the invoker to communicate with the action 
container.
+ * It allows to POST a JSON object and receive JSON object back; that is the
+ * content type and the accept headers are both 'application/json.
+ * The reason we still use this class for the action container is a mysterious 
hang
+ * in the Akka http client where a future fails to properly timeout and we 
have not
+ * determined why that is.
+ *
+ * @param hostname the host name
+ * @param timeout the timeout in msecs to wait for a response
+ * @param maxResponse the maximum size in bytes the connection will accept
+ * @param queueSize once all connections are used, how big of queue to allow 
for additional requests
+ * @param retryInterval duration between retries for TCP connection errors
+ */
+protected class PoolingContainerClient(
+  hostname: String,
+  port: Int,
+  timeout: FiniteDuration,
+  maxResponse: ByteSize,
+  queueSize: Int,
+  retryInterval: FiniteDuration = 100.milliseconds)(implicit logging: Logging, 
as: ActorSystem)
+extends PoolingRestClient("http", hostname, port, queueSize)
+with ContainerClient {
+
+  def close() = shutdown()
+
+  /**
+   * Posts to hostname/endpoint the given JSON object.
+   * Waits up to timeout before aborting on a good connection.
+   * If the endpoint is not ready, retry up to timeout.
+   * Every retry reduces the available timeout so that this method should not
+   * wait longer than the total timeout (within a small slack allowance).
+   *
+   * @param endpoint the path the api call relative to hostname
+   * @param body the JSON value to post (this is usually a JSON objecT)
+   * @param retry whether or not to retry on connection failure
+   * @return Left(Error Message) or Right(Status Code, Response as UTF-8 
String)
+   */
+  def post(endpoint: String, body: JsValue, retry: Boolean)(
+implicit tid: TransactionId): Future[Either[ContainerHttpError, 
ContainerResponse]] = {
+
+//create the request
+val req = Marshal(body).to[MessageEntity].map { b =>
+  //DO NOT reuse the connection (in case of paused containers)
+  //For details on Connection: Close handling, see:
+  // - 
https://doc.akka.io/docs/akka-http/current/common/http-model.html#http-headers
+  // - 
http://github.com/akka/akka-http/tree/v10.1.3/akka-http-core/src/test/scala/akka/http/impl/engine/rendering/ResponseRendererSpec.scala#L470-L571
+  

[GitHub] tysonnorris commented on a change in pull request #3812: ContainerClient + akka http alternative to HttpUtils

2018-07-16 Thread GitBox
tysonnorris commented on a change in pull request #3812: ContainerClient + akka 
http alternative to HttpUtils
URL: 
https://github.com/apache/incubator-openwhisk/pull/3812#discussion_r202885345
 
 

 ##
 File path: 
common/scala/src/main/scala/whisk/core/containerpool/ContainerClient.scala
 ##
 @@ -0,0 +1,240 @@
+/*
+ * 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 whisk.core.containerpool
+
+import akka.actor.ActorSystem
+import akka.http.scaladsl.marshallers.sprayjson.SprayJsonSupport._
+import akka.http.scaladsl.marshalling.Marshal
+import akka.http.scaladsl.model.HttpMethods
+import akka.http.scaladsl.model.HttpRequest
+import akka.http.scaladsl.model.HttpResponse
+import akka.http.scaladsl.model.MessageEntity
+import akka.http.scaladsl.model.StatusCodes
+import akka.http.scaladsl.model.headers.Connection
+import akka.http.scaladsl.unmarshalling.Unmarshal
+import akka.stream.scaladsl.Sink
+import akka.stream.scaladsl.Source
+import akka.util.ByteString
+import scala.concurrent.Await
+import scala.concurrent.ExecutionContext
+import scala.concurrent.Future
+import scala.concurrent.Promise
+import scala.concurrent.TimeoutException
+import scala.concurrent.duration._
+import scala.util.Try
+import scala.util.control.NoStackTrace
+import spray.json._
+import whisk.common.Logging
+import whisk.common.TransactionId
+import whisk.core.entity.ActivationResponse.ContainerHttpError
+import whisk.core.entity.ActivationResponse._
+import whisk.core.entity.ByteSize
+import whisk.core.entity.size.SizeLong
+import whisk.http.PoolingRestClient
+
+trait ContainerClient {
+  def close(): Unit
+  def post(endpoint: String, body: JsValue, retry: Boolean)(
+implicit tid: TransactionId): Future[Either[ContainerHttpError, 
ContainerResponse]]
+
+}
+
+/**
+ * This HTTP client is used only in the invoker to communicate with the action 
container.
+ * It allows to POST a JSON object and receive JSON object back; that is the
+ * content type and the accept headers are both 'application/json.
+ * The reason we still use this class for the action container is a mysterious 
hang
+ * in the Akka http client where a future fails to properly timeout and we 
have not
+ * determined why that is.
+ *
+ * @param hostname the host name
+ * @param timeout the timeout in msecs to wait for a response
+ * @param maxResponse the maximum size in bytes the connection will accept
+ * @param queueSize once all connections are used, how big of queue to allow 
for additional requests
+ * @param retryInterval duration between retries for TCP connection errors
+ */
+protected class PoolingContainerClient(
+  hostname: String,
+  port: Int,
+  timeout: FiniteDuration,
+  maxResponse: ByteSize,
+  queueSize: Int,
+  retryInterval: FiniteDuration = 100.milliseconds)(implicit logging: Logging, 
as: ActorSystem)
+extends PoolingRestClient("http", hostname, port, queueSize)
+with ContainerClient {
+
+  def close() = shutdown()
+
+  /**
+   * Posts to hostname/endpoint the given JSON object.
+   * Waits up to timeout before aborting on a good connection.
+   * If the endpoint is not ready, retry up to timeout.
+   * Every retry reduces the available timeout so that this method should not
+   * wait longer than the total timeout (within a small slack allowance).
+   *
+   * @param endpoint the path the api call relative to hostname
+   * @param body the JSON value to post (this is usually a JSON objecT)
+   * @param retry whether or not to retry on connection failure
+   * @return Left(Error Message) or Right(Status Code, Response as UTF-8 
String)
+   */
+  def post(endpoint: String, body: JsValue, retry: Boolean)(
+implicit tid: TransactionId): Future[Either[ContainerHttpError, 
ContainerResponse]] = {
+
+//create the request
+val req = Marshal(body).to[MessageEntity].map { b =>
+  //DO NOT reuse the connection (in case of paused containers)
+  //For details on Connection: Close handling, see:
+  // - 
https://doc.akka.io/docs/akka-http/current/common/http-model.html#http-headers
+  // - 
http://github.com/akka/akka-http/tree/v10.1.3/akka-http-core/src/test/scala/akka/http/impl/engine/rendering/ResponseRendererSpec.scala#L470-L571
+  

[GitHub] tysonnorris commented on a change in pull request #3812: ContainerClient + akka http alternative to HttpUtils

2018-07-16 Thread GitBox
tysonnorris commented on a change in pull request #3812: ContainerClient + akka 
http alternative to HttpUtils
URL: 
https://github.com/apache/incubator-openwhisk/pull/3812#discussion_r202884192
 
 

 ##
 File path: 
common/scala/src/main/scala/whisk/core/containerpool/ContainerClient.scala
 ##
 @@ -0,0 +1,205 @@
+/*
+ * 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 whisk.core.containerpool
+
+import akka.actor.ActorSystem
+import akka.http.scaladsl.marshallers.sprayjson.SprayJsonSupport._
+import akka.http.scaladsl.marshalling.Marshal
+import akka.http.scaladsl.model.HttpMethods
+import akka.http.scaladsl.model.HttpRequest
+import akka.http.scaladsl.model.HttpResponse
+import akka.http.scaladsl.model.MessageEntity
+import akka.http.scaladsl.unmarshalling.Unmarshal
+import scala.concurrent.Await
+import scala.concurrent.ExecutionContext
+import scala.concurrent.Future
+import scala.concurrent.Promise
+import scala.concurrent.TimeoutException
+import scala.concurrent.duration._
+import scala.util.Try
+import scala.util.control.NoStackTrace
+import spray.json._
+import whisk.common.Logging
+import whisk.common.TransactionId
+import whisk.core.entity.ActivationResponse.ContainerHttpError
+import whisk.core.entity.ActivationResponse._
+import whisk.core.entity.ByteSize
+import whisk.core.entity.size.SizeLong
+import whisk.http.PoolingRestClient
+
+trait ContainerClient {
+  def close(): Unit
+  def post(endpoint: String, body: JsValue, retry: Boolean)(
+implicit tid: TransactionId,
+ec: ExecutionContext): Future[Either[ContainerHttpError, 
ContainerResponse]]
+
+}
+
+/**
+ * This HTTP client is used only in the invoker to communicate with the action 
container.
+ * It allows to POST a JSON object and receive JSON object back; that is the
+ * content type and the accept headers are both 'application/json.
+ * The reason we still use this class for the action container is a mysterious 
hang
+ * in the Akka http client where a future fails to properly timeout and we 
have not
+ * determined why that is.
+ *
+ * @param hostname the host name
+ * @param timeout the timeout in msecs to wait for a response
+ * @param maxResponse the maximum size in bytes the connection will accept
+ * @param queueSize once all connections are used, how big of queue to allow 
for additional requests
+ * @param retryInterval duration between retries for TCP connection errors
+ */
+protected class PoolingContainerClient(
+  hostname: String,
+  port: Int,
+  timeout: FiniteDuration,
+  maxResponse: ByteSize,
+  queueSize: Int,
+  retryInterval: FiniteDuration = 100.milliseconds)(implicit logging: Logging, 
as: ActorSystem)
+extends PoolingRestClient("http", hostname, port, queueSize)
+with ContainerClient {
+
+  def close() = shutdown()
+
+  /**
+   * Posts to hostname/endpoint the given JSON object.
+   * Waits up to timeout before aborting on a good connection.
+   * If the endpoint is not ready, retry up to timeout.
+   * Every retry reduces the available timeout so that this method should not
+   * wait longer than the total timeout (within a small slack allowance).
+   *
+   * @param endpoint the path the api call relative to hostname
+   * @param body the JSON value to post (this is usually a JSON objecT)
+   * @param retry whether or not to retry on connection failure
+   * @return Left(Error Message) or Right(Status Code, Response as UTF-8 
String)
+   */
+  def post(endpoint: String, body: JsValue, retry: Boolean)(
+implicit tid: TransactionId,
+ec: ExecutionContext): Future[Either[ContainerHttpError, 
ContainerResponse]] = {
+
+//create the request
+val req = Marshal(body).to[MessageEntity].map { b =>
+  HttpRequest(HttpMethods.POST, endpoint, entity = b)
+}
+
+//Begin retry handling
+
+//Handle retries by:
+// - tracking request as a promise
+// - attaching a timeout to fail the promise
+// - create a function to enqueue the request
+// - retry (using same function) on StreamTcpException (only if retry == 
true)
+
+val promise = Promise[HttpResponse]
+
+// Timeout includes all retries.
+as.scheduler.scheduleOnce(timeout) {
+  promise.tryFailure(new 

[GitHub] tysonnorris commented on a change in pull request #3812: ContainerClient + akka http alternative to HttpUtils

2018-07-16 Thread GitBox
tysonnorris commented on a change in pull request #3812: ContainerClient + akka 
http alternative to HttpUtils
URL: 
https://github.com/apache/incubator-openwhisk/pull/3812#discussion_r202883397
 
 

 ##
 File path: 
common/scala/src/main/scala/whisk/core/containerpool/ContainerClient.scala
 ##
 @@ -0,0 +1,205 @@
+/*
+ * 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 whisk.core.containerpool
+
+import akka.actor.ActorSystem
+import akka.http.scaladsl.marshallers.sprayjson.SprayJsonSupport._
+import akka.http.scaladsl.marshalling.Marshal
+import akka.http.scaladsl.model.HttpMethods
+import akka.http.scaladsl.model.HttpRequest
+import akka.http.scaladsl.model.HttpResponse
+import akka.http.scaladsl.model.MessageEntity
+import akka.http.scaladsl.unmarshalling.Unmarshal
+import scala.concurrent.Await
+import scala.concurrent.ExecutionContext
+import scala.concurrent.Future
+import scala.concurrent.Promise
+import scala.concurrent.TimeoutException
+import scala.concurrent.duration._
+import scala.util.Try
+import scala.util.control.NoStackTrace
+import spray.json._
+import whisk.common.Logging
+import whisk.common.TransactionId
+import whisk.core.entity.ActivationResponse.ContainerHttpError
+import whisk.core.entity.ActivationResponse._
+import whisk.core.entity.ByteSize
+import whisk.core.entity.size.SizeLong
+import whisk.http.PoolingRestClient
+
+trait ContainerClient {
+  def close(): Unit
+  def post(endpoint: String, body: JsValue, retry: Boolean)(
+implicit tid: TransactionId,
+ec: ExecutionContext): Future[Either[ContainerHttpError, 
ContainerResponse]]
+
+}
+
+/**
+ * This HTTP client is used only in the invoker to communicate with the action 
container.
+ * It allows to POST a JSON object and receive JSON object back; that is the
+ * content type and the accept headers are both 'application/json.
+ * The reason we still use this class for the action container is a mysterious 
hang
+ * in the Akka http client where a future fails to properly timeout and we 
have not
+ * determined why that is.
+ *
+ * @param hostname the host name
+ * @param timeout the timeout in msecs to wait for a response
+ * @param maxResponse the maximum size in bytes the connection will accept
+ * @param queueSize once all connections are used, how big of queue to allow 
for additional requests
+ * @param retryInterval duration between retries for TCP connection errors
+ */
+protected class PoolingContainerClient(
+  hostname: String,
+  port: Int,
+  timeout: FiniteDuration,
+  maxResponse: ByteSize,
+  queueSize: Int,
+  retryInterval: FiniteDuration = 100.milliseconds)(implicit logging: Logging, 
as: ActorSystem)
+extends PoolingRestClient("http", hostname, port, queueSize)
+with ContainerClient {
+
+  def close() = shutdown()
+
+  /**
+   * Posts to hostname/endpoint the given JSON object.
+   * Waits up to timeout before aborting on a good connection.
+   * If the endpoint is not ready, retry up to timeout.
+   * Every retry reduces the available timeout so that this method should not
+   * wait longer than the total timeout (within a small slack allowance).
+   *
+   * @param endpoint the path the api call relative to hostname
+   * @param body the JSON value to post (this is usually a JSON objecT)
+   * @param retry whether or not to retry on connection failure
+   * @return Left(Error Message) or Right(Status Code, Response as UTF-8 
String)
+   */
+  def post(endpoint: String, body: JsValue, retry: Boolean)(
+implicit tid: TransactionId,
+ec: ExecutionContext): Future[Either[ContainerHttpError, 
ContainerResponse]] = {
+
+//create the request
+val req = Marshal(body).to[MessageEntity].map { b =>
+  HttpRequest(HttpMethods.POST, endpoint, entity = b)
+}
+
+//Begin retry handling
+
+//Handle retries by:
+// - tracking request as a promise
+// - attaching a timeout to fail the promise
+// - create a function to enqueue the request
+// - retry (using same function) on StreamTcpException (only if retry == 
true)
+
+val promise = Promise[HttpResponse]
+
+// Timeout includes all retries.
+as.scheduler.scheduleOnce(timeout) {
+  promise.tryFailure(new 

[GitHub] tysonnorris commented on a change in pull request #3812: ContainerClient + akka http alternative to HttpUtils

2018-07-15 Thread GitBox
tysonnorris commented on a change in pull request #3812: ContainerClient + akka 
http alternative to HttpUtils
URL: 
https://github.com/apache/incubator-openwhisk/pull/3812#discussion_r202558847
 
 

 ##
 File path: 
common/scala/src/main/scala/whisk/core/containerpool/ContainerClient.scala
 ##
 @@ -0,0 +1,205 @@
+/*
+ * 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 whisk.core.containerpool
+
+import akka.actor.ActorSystem
+import akka.http.scaladsl.marshallers.sprayjson.SprayJsonSupport._
+import akka.http.scaladsl.marshalling.Marshal
+import akka.http.scaladsl.model.HttpMethods
+import akka.http.scaladsl.model.HttpRequest
+import akka.http.scaladsl.model.HttpResponse
+import akka.http.scaladsl.model.MessageEntity
+import akka.http.scaladsl.unmarshalling.Unmarshal
+import scala.concurrent.Await
+import scala.concurrent.ExecutionContext
+import scala.concurrent.Future
+import scala.concurrent.Promise
+import scala.concurrent.TimeoutException
+import scala.concurrent.duration._
+import scala.util.Try
+import scala.util.control.NoStackTrace
+import spray.json._
+import whisk.common.Logging
+import whisk.common.TransactionId
+import whisk.core.entity.ActivationResponse.ContainerHttpError
+import whisk.core.entity.ActivationResponse._
+import whisk.core.entity.ByteSize
+import whisk.core.entity.size.SizeLong
+import whisk.http.PoolingRestClient
+
+trait ContainerClient {
+  def close(): Unit
+  def post(endpoint: String, body: JsValue, retry: Boolean)(
+implicit tid: TransactionId,
+ec: ExecutionContext): Future[Either[ContainerHttpError, 
ContainerResponse]]
+
+}
+
+/**
+ * This HTTP client is used only in the invoker to communicate with the action 
container.
+ * It allows to POST a JSON object and receive JSON object back; that is the
+ * content type and the accept headers are both 'application/json.
+ * The reason we still use this class for the action container is a mysterious 
hang
+ * in the Akka http client where a future fails to properly timeout and we 
have not
+ * determined why that is.
+ *
+ * @param hostname the host name
+ * @param timeout the timeout in msecs to wait for a response
+ * @param maxResponse the maximum size in bytes the connection will accept
+ * @param queueSize once all connections are used, how big of queue to allow 
for additional requests
+ * @param retryInterval duration between retries for TCP connection errors
+ */
+protected class PoolingContainerClient(
+  hostname: String,
+  port: Int,
+  timeout: FiniteDuration,
+  maxResponse: ByteSize,
+  queueSize: Int,
+  retryInterval: FiniteDuration = 100.milliseconds)(implicit logging: Logging, 
as: ActorSystem)
+extends PoolingRestClient("http", hostname, port, queueSize)
+with ContainerClient {
+
+  def close() = shutdown()
+
+  /**
+   * Posts to hostname/endpoint the given JSON object.
+   * Waits up to timeout before aborting on a good connection.
+   * If the endpoint is not ready, retry up to timeout.
+   * Every retry reduces the available timeout so that this method should not
+   * wait longer than the total timeout (within a small slack allowance).
+   *
+   * @param endpoint the path the api call relative to hostname
+   * @param body the JSON value to post (this is usually a JSON objecT)
+   * @param retry whether or not to retry on connection failure
+   * @return Left(Error Message) or Right(Status Code, Response as UTF-8 
String)
+   */
+  def post(endpoint: String, body: JsValue, retry: Boolean)(
+implicit tid: TransactionId,
+ec: ExecutionContext): Future[Either[ContainerHttpError, 
ContainerResponse]] = {
+
+//create the request
+val req = Marshal(body).to[MessageEntity].map { b =>
+  HttpRequest(HttpMethods.POST, endpoint, entity = b)
+}
+
+//Begin retry handling
+
+//Handle retries by:
+// - tracking request as a promise
+// - attaching a timeout to fail the promise
+// - create a function to enqueue the request
+// - retry (using same function) on StreamTcpException (only if retry == 
true)
+
+val promise = Promise[HttpResponse]
+
+// Timeout includes all retries.
+as.scheduler.scheduleOnce(timeout) {
+  promise.tryFailure(new 

[GitHub] tysonnorris commented on a change in pull request #3812: ContainerClient + akka http alternative to HttpUtils

2018-07-15 Thread GitBox
tysonnorris commented on a change in pull request #3812: ContainerClient + akka 
http alternative to HttpUtils
URL: 
https://github.com/apache/incubator-openwhisk/pull/3812#discussion_r202549203
 
 

 ##
 File path: 
common/scala/src/main/scala/whisk/core/containerpool/ContainerClient.scala
 ##
 @@ -0,0 +1,205 @@
+/*
+ * 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 whisk.core.containerpool
+
+import akka.actor.ActorSystem
+import akka.http.scaladsl.marshallers.sprayjson.SprayJsonSupport._
+import akka.http.scaladsl.marshalling.Marshal
+import akka.http.scaladsl.model.HttpMethods
+import akka.http.scaladsl.model.HttpRequest
+import akka.http.scaladsl.model.HttpResponse
+import akka.http.scaladsl.model.MessageEntity
+import akka.http.scaladsl.unmarshalling.Unmarshal
+import scala.concurrent.Await
+import scala.concurrent.ExecutionContext
+import scala.concurrent.Future
+import scala.concurrent.Promise
+import scala.concurrent.TimeoutException
+import scala.concurrent.duration._
+import scala.util.Try
+import scala.util.control.NoStackTrace
+import spray.json._
+import whisk.common.Logging
+import whisk.common.TransactionId
+import whisk.core.entity.ActivationResponse.ContainerHttpError
+import whisk.core.entity.ActivationResponse._
+import whisk.core.entity.ByteSize
+import whisk.core.entity.size.SizeLong
+import whisk.http.PoolingRestClient
+
+trait ContainerClient {
+  def close(): Unit
+  def post(endpoint: String, body: JsValue, retry: Boolean)(
+implicit tid: TransactionId,
+ec: ExecutionContext): Future[Either[ContainerHttpError, 
ContainerResponse]]
+
+}
+
+/**
+ * This HTTP client is used only in the invoker to communicate with the action 
container.
+ * It allows to POST a JSON object and receive JSON object back; that is the
+ * content type and the accept headers are both 'application/json.
+ * The reason we still use this class for the action container is a mysterious 
hang
+ * in the Akka http client where a future fails to properly timeout and we 
have not
+ * determined why that is.
+ *
+ * @param hostname the host name
+ * @param timeout the timeout in msecs to wait for a response
+ * @param maxResponse the maximum size in bytes the connection will accept
+ * @param queueSize once all connections are used, how big of queue to allow 
for additional requests
+ * @param retryInterval duration between retries for TCP connection errors
+ */
+protected class PoolingContainerClient(
+  hostname: String,
+  port: Int,
+  timeout: FiniteDuration,
+  maxResponse: ByteSize,
+  queueSize: Int,
+  retryInterval: FiniteDuration = 100.milliseconds)(implicit logging: Logging, 
as: ActorSystem)
+extends PoolingRestClient("http", hostname, port, queueSize)
+with ContainerClient {
+
+  def close() = shutdown()
+
+  /**
+   * Posts to hostname/endpoint the given JSON object.
+   * Waits up to timeout before aborting on a good connection.
+   * If the endpoint is not ready, retry up to timeout.
+   * Every retry reduces the available timeout so that this method should not
+   * wait longer than the total timeout (within a small slack allowance).
+   *
+   * @param endpoint the path the api call relative to hostname
+   * @param body the JSON value to post (this is usually a JSON objecT)
+   * @param retry whether or not to retry on connection failure
+   * @return Left(Error Message) or Right(Status Code, Response as UTF-8 
String)
+   */
+  def post(endpoint: String, body: JsValue, retry: Boolean)(
+implicit tid: TransactionId,
+ec: ExecutionContext): Future[Either[ContainerHttpError, 
ContainerResponse]] = {
+
+//create the request
+val req = Marshal(body).to[MessageEntity].map { b =>
+  HttpRequest(HttpMethods.POST, endpoint, entity = b)
+}
+
+//Begin retry handling
+
+//Handle retries by:
+// - tracking request as a promise
+// - attaching a timeout to fail the promise
+// - create a function to enqueue the request
+// - retry (using same function) on StreamTcpException (only if retry == 
true)
+
+val promise = Promise[HttpResponse]
+
+// Timeout includes all retries.
+as.scheduler.scheduleOnce(timeout) {
+  promise.tryFailure(new 

[GitHub] tysonnorris commented on a change in pull request #3812: ContainerClient + akka http alternative to HttpUtils

2018-07-15 Thread GitBox
tysonnorris commented on a change in pull request #3812: ContainerClient + akka 
http alternative to HttpUtils
URL: 
https://github.com/apache/incubator-openwhisk/pull/3812#discussion_r202549203
 
 

 ##
 File path: 
common/scala/src/main/scala/whisk/core/containerpool/ContainerClient.scala
 ##
 @@ -0,0 +1,205 @@
+/*
+ * 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 whisk.core.containerpool
+
+import akka.actor.ActorSystem
+import akka.http.scaladsl.marshallers.sprayjson.SprayJsonSupport._
+import akka.http.scaladsl.marshalling.Marshal
+import akka.http.scaladsl.model.HttpMethods
+import akka.http.scaladsl.model.HttpRequest
+import akka.http.scaladsl.model.HttpResponse
+import akka.http.scaladsl.model.MessageEntity
+import akka.http.scaladsl.unmarshalling.Unmarshal
+import scala.concurrent.Await
+import scala.concurrent.ExecutionContext
+import scala.concurrent.Future
+import scala.concurrent.Promise
+import scala.concurrent.TimeoutException
+import scala.concurrent.duration._
+import scala.util.Try
+import scala.util.control.NoStackTrace
+import spray.json._
+import whisk.common.Logging
+import whisk.common.TransactionId
+import whisk.core.entity.ActivationResponse.ContainerHttpError
+import whisk.core.entity.ActivationResponse._
+import whisk.core.entity.ByteSize
+import whisk.core.entity.size.SizeLong
+import whisk.http.PoolingRestClient
+
+trait ContainerClient {
+  def close(): Unit
+  def post(endpoint: String, body: JsValue, retry: Boolean)(
+implicit tid: TransactionId,
+ec: ExecutionContext): Future[Either[ContainerHttpError, 
ContainerResponse]]
+
+}
+
+/**
+ * This HTTP client is used only in the invoker to communicate with the action 
container.
+ * It allows to POST a JSON object and receive JSON object back; that is the
+ * content type and the accept headers are both 'application/json.
+ * The reason we still use this class for the action container is a mysterious 
hang
+ * in the Akka http client where a future fails to properly timeout and we 
have not
+ * determined why that is.
+ *
+ * @param hostname the host name
+ * @param timeout the timeout in msecs to wait for a response
+ * @param maxResponse the maximum size in bytes the connection will accept
+ * @param queueSize once all connections are used, how big of queue to allow 
for additional requests
+ * @param retryInterval duration between retries for TCP connection errors
+ */
+protected class PoolingContainerClient(
+  hostname: String,
+  port: Int,
+  timeout: FiniteDuration,
+  maxResponse: ByteSize,
+  queueSize: Int,
+  retryInterval: FiniteDuration = 100.milliseconds)(implicit logging: Logging, 
as: ActorSystem)
+extends PoolingRestClient("http", hostname, port, queueSize)
+with ContainerClient {
+
+  def close() = shutdown()
+
+  /**
+   * Posts to hostname/endpoint the given JSON object.
+   * Waits up to timeout before aborting on a good connection.
+   * If the endpoint is not ready, retry up to timeout.
+   * Every retry reduces the available timeout so that this method should not
+   * wait longer than the total timeout (within a small slack allowance).
+   *
+   * @param endpoint the path the api call relative to hostname
+   * @param body the JSON value to post (this is usually a JSON objecT)
+   * @param retry whether or not to retry on connection failure
+   * @return Left(Error Message) or Right(Status Code, Response as UTF-8 
String)
+   */
+  def post(endpoint: String, body: JsValue, retry: Boolean)(
+implicit tid: TransactionId,
+ec: ExecutionContext): Future[Either[ContainerHttpError, 
ContainerResponse]] = {
+
+//create the request
+val req = Marshal(body).to[MessageEntity].map { b =>
+  HttpRequest(HttpMethods.POST, endpoint, entity = b)
+}
+
+//Begin retry handling
+
+//Handle retries by:
+// - tracking request as a promise
+// - attaching a timeout to fail the promise
+// - create a function to enqueue the request
+// - retry (using same function) on StreamTcpException (only if retry == 
true)
+
+val promise = Promise[HttpResponse]
+
+// Timeout includes all retries.
+as.scheduler.scheduleOnce(timeout) {
+  promise.tryFailure(new