15

我最近迁移到 Play framework 2.0,关于控制器在游戏中的实际工作方式,我有一些疑问。

播放文档中提到:

由于 Play 2.0 的工作方式,动作代码必须尽可能快(即非阻塞)。

但是在文档的另一部分

            /actions {
                router = round-robin
                nr-of-instances = 24
            }

        actions-dispatcher = {
            fork-join-executor {
                parallelism-factor = 1.0
                parallelism-max = 24
            }
        }

似乎有 24 个参与者分配给控制器处理。我猜每个请求都会在请求的生命周期内分配其中一个参与者。那正确吗?

另外,是什么parallelism-factor意思,又有何fork-join-executor不同thread-pool

另外 - 文档应该说 Async 应该用于长时间计算。什么是长计算?100 毫秒?300毫秒?5秒?10秒?我的猜测会超过一秒钟,但如何确定呢?

这个问题的原因是测试异步控制器调用比常规调用更难。您必须启动一个假应用程序并执行一个完整的请求,而不是仅仅调用一个方法并检查它的返回值。

即使情况并非如此,我也怀疑将所有内容都包含在内Async 并且Akka.future是一种方式。

我已经在#playframework IRC 频道中提出了这个问题,但没有得到答复,而且似乎我不是唯一一个不确定应该如何做的人。

只是重申:

  1. 每个请求都从 /actions 池中分配一个演员是否正确?
  2. 是什么parallelism-factor意思,为什么是1?
  3. 与有何fork-join-executor不同?thread-pool-executor
  4. 计算应该包含多长时间Async
  5. 如果不启动假应用程序就不可能测试异步控制器方法吗?

提前致谢。

编辑:来自 IRC 的一些东西

来自 IRC 的一些东西。

<imeredith> arturaz: i cant be boethered writing up a full reply but here are key points
<imeredith> arturaz: i believe that some type of CPS goes on with async stuff which frees up request threads
<arturaz> CPS?
<imeredith> continuations
<imeredith> when the future is finished, or timedout, it then resumes the request
<imeredith> and returns data
<imeredith> arturaz: as for testing, you can do .await on the future and it will block until the data is ready
<imeredith> (i believe)
<imeredith> arturaz: as for "long" and parallelism - the longer you hold a request thread, the more parrellism you need
<imeredith> arturaz: ie servlets typically need a lot of threads because you have to hold the request thread open for a longer time then if you are using play async
<imeredith> "Is it right that every request allocates one actor from /actions pool?" - yes i belive so
<imeredith> "What does parallelism-factor mean and why is it 1?" - im guessing this is how many actors there are in the pool?
<imeredith> or not
<imeredith> "How does fork-join-executor differ from thread-pool-executor?" -no idea
<imeredith> "How long should a calculation be to become wrapped in Async?" - i think that is the same as asking "how long is a piece of string"
<imeredith> "Is is not possible to test async controller method without spinning up fake applications?" i think you should be able to get the result
<viktorklang> imeredith: A good idea is to read the documentation: http://doc.akka.io/docs/akka/2.0.3/general/configuration.html ( which says parallelism-factor is: # Parallelism (threads) ... ceil(available processors * factor))
<arturaz> viktorklang, don't get me wrong, but that's the problem - this is not documentation, it's a reminder to yourself.
<arturaz> I have absolutely no idea what that should mean
<viktorklang> arturaz: It's the number of processors available multiplied with the factor you give, and then rounded up using "ceil". I don't know how it could be more clear.
<arturaz> viktorklang, how about: This factor is used in calculation `ceil(number of processors * factor)` which describes how big is a thread pool given for your actors.
<viktorklang> arturaz: But that is not strictly true since the size is also guarded by your min and max values
<arturaz> then why is it there? :)
<viktorklang> arturaz: Parallelism (threads) ... ceil(available processors * factor) could be expanded by adding a big of conversational fluff: Parallelism ( in other words: number of threads), it is calculated using the given factor as: ceil(available processors * factor)
<viktorklang> arturaz: Because your program might not work with a parallelism less than X and you don't want to use more threads than X (i.e if you have a 48 core box and you have 4.0 as factor that'll be a crapload of threads)
<viktorklang> arturaz: I.e. scheduling overhead gives diminishing returns, especially if ctz switching is across physical slots.
<viktorklang> arturaz: Changing thread pool sizes will always require you to have at least basic understanding on Threads and thread scheduling
<viktorklang> arturaz: makes sense?
<arturaz> yes
<arturaz> and thank you
<arturaz> I'll add this to my question, but this kind of knowledge would be awesome docs ;)
4

2 回答 2

6
  1. 当消息到达一个actor 时,只要它需要处理该消息,它就会保留该actor。如果您同步处理请求(在处理该消息期间计算整个响应),则此参与者无法为其他请求提供服务,直到响应完成。相反,如果您可以在收到此请求后将工作发送给另一个参与者,则收到请求的参与者可以开始处理下一个请求,而其他参与者正在处理第一个请求。

  2. 用于actor的线程数是“num cpus * parallelism-factor”(但是您可以指定最小值和最大值)

  3. 不知道

  4. 除非进行真正的计算,否则我倾向于将任何与其他系统通信的东西都设为异步,比如使用数据库/文件系统进行 io。当然是任何可能阻塞线程的东西。但是,由于传递消息的开销很小,我认为将所有工作发送给其他参与者不会有问题。

  5. 请参阅有关如何测试控制器的功能测试的 Play 文档。

于 2012-09-25T13:15:40.827 回答
1

看来您可以这样做进行测试:

object ControllerHelpers {
  class ResultExtensions(result: Result) {
    /**
     * Retrieve Promise[Result] from AsyncResult
     * @return
     */
    def asyncResult = result match {
      case async: AsyncResult => async.result
      case _ => throw new IllegalArgumentException(
        "%s of type %s is not AsyncResult!".format(result, result.getClass)
      )
    }

    /**
     * Block until result is available.
     *
     * @return
     */
    def await = asyncResult.await

    /**
     * Block until result is available.
     *
     * @param timeout
     * @return
     */
    def await(timeout: Long) = asyncResult.await(timeout)

    /**
     * Block for max 5 seconds to retrieve result.
     * @return
     */
    def get = await.get
  }
}

  implicit def extendResult(result: Result) =
    new ControllerHelpers.ResultExtensions(result)


  val result = c.postcodeTimesCsv()(request(params)).get
  status(result) should be === OK
于 2012-09-26T09:49:53.480 回答