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 TimeoutException(s"Request to ${endpoint} could 
not be completed in time."))
+    }
 
 Review comment:
   So I tried to do this using 
https://github.com/paypal/squbs/blob/master/squbs-ext/src/main/scala/org/squbs/streams/Timeout.scala#L268
   
   I'm not wild about:
   * dragging in squbs artifacts (seems heave handed for "just" adding a 
timeout)
   * the `Try[HttpResponse]` is wrapped as a `Try[Try[HttpResponse]]` this 
seems awkward.
   
   

----------------------------------------------------------------
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

Reply via email to