Finagle 没有异步执行 [英] Finagle No asyncronous executing

查看:50
本文介绍了Finagle 没有异步执行的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个简单的 finagle 节俭服务器:

import com.twitter.finagle.Thrift导入 scala.concurrent.Future导入 com.twitter.util.{ 等待,未来}对象主{def main(args: Array[String]) {变量计数 = 0val myserver = Thrift.serveIface("0.0.0.0:9090", new RealTimeDatabasePageImpressions[com.twitter.util.Future] {def saveOrUpdate(pageImpression: PageImpressions):com.twitter.util.Future[布尔] = {计数 += 1打印(计数)com.twitter.util.Future.value(真)}}等待.准备好(我的服务器)}}

这个服务器可以工作,但我有一个大问题:我写了一个带有 for 循环的 thrift nodejs 客户端.它执行 10.000 个节俭请求.但它不是异步的.它执行 500 个请求并停止.一段时间后,2 或 3 秒,将执行 300 个以上的请求.现在的问题是:为什么会发生这种情况?我的服务器或客户端有问题吗?我只使用 apache thrift 生成的 nodejs 代码.没有包装.该函数执行了 10.000 次.我认为 nodejs 不是问题:

函数 callFunc(i){console.log("开始执行:" + i);var connection = thrift.createConnection("IP", 9090, {运输:运输,协议:协议});connection.on('错误',函数(错误){控制台日志(错误);});//使用连接创建一个计算器客户端var client = thrift.createClient(Realtime_pageImpressions, connection);var rand = Math.random() * (20000 - 1);var trackId = trackIds[Math.round(Math.random() * 10)];var values = new PageImpressions({trackId:trackId,天:4,小时:4,分钟:13,pageId: 'blabla',uniqueImpressions: Math.random() * (13000 - 1),sumImpressions:Math.random() * (1000450 - 1)});client.saveOrUpdate(values, function (error, message) {如果(消息){console.log("成功,收到消息:" + message);} 别的 {console.log("错误信息:" + error);}});返回真;}for(var i = 0; i <10000; i++){callFunc(i);}

解决方案

您的 var count 未同步.这是一个非常大的问题,但可能与您的性能问题无关.您还阻塞了 finagle 线程,这也是一个大问题,但在您的模拟案例中无关紧要,因为没有等待时间.

这样想.假设您有一个 cpu(您可能有几个,但机器上还有其他东西),并且您要求它同时执行 10000 个操作.>

这怎么工作?它将必须执行其中一个请求,保存上下文,堆栈,刷新所有缓存,切换到下一个请求,执行该请求...

2 秒内 500 个请求是每个请求 4 毫秒.听起来没那么糟糕吧?

此外,您是否开启了 GC(在服务器和客户端上)?如果请求以突发方式处理,然后长时间停顿,这可能是 full GC 开始的迹象

i have a simple finagle thrift server:

import com.twitter.finagle.Thrift
import scala.concurrent.Future

import com.twitter.util.{ Await, Future }
object Main{

  def main(args: Array[String]) {

    var count = 0

    val myserver = Thrift.serveIface("0.0.0.0:9090", new RealTimeDatabasePageImpressions[com.twitter.util.Future] {

      def saveOrUpdate(pageImpression: PageImpressions):
      com.twitter.util.Future[Boolean] = {
    count += 1
    println(count)
    com.twitter.util.Future.value(true)
      }
    }

   Await.ready(myserver)
  }

}

This server works but i have one big problem: i wrote a thrift nodejs client with a for loop. It executes 10.000 thrift request. But it's not asynchronous. It executes 500 request and stops. After a while, 2 or 3 seconds, 300 more requests will executed. Now the question: Why this happen? Is something wrong with my server or client? I use only the apache thrift generated nodejs code. No wrapper. The function executed 10.000 times. I think the nodejs isn't the problem:

function callFunc(i){
    console.log("started executing: " + i);
    var connection = thrift.createConnection("IP", 9090, {
    transport: transport,
    protocol: protocol
    });

    connection.on('error', function (err) {
    console.log(err);
    });

    // Create a Calculator client with the connection
    var client = thrift.createClient(Realtime_pageImpressions, connection);


    var rand = Math.random() * (20000 - 1);

    var trackId = trackIds[Math.round(Math.random() * 10)];
    var values = new PageImpressions({
    trackId: trackId,
    day: 4,
    hour: 4,
    minute: 13,
    pageId: 'blabla',
    uniqueImpressions: Math.random() * (13000 - 1),
    sumImpressions: Math.random() * (1000450 - 1)
    });

    client.saveOrUpdate(values, function (error, message) {
    if (message) {
        console.log("Successful, got Message: " + message);
    } else {
        console.log("Error with Message: " + error);
    }
    });
    return true;
}
for(var i = 0; i < 10000; i++){
    callFunc(i);
}

解决方案

Your var count is unsynchronized. This is a very big problem, but, probably, not related to your performance issue. You are also blocking finagle thread, which is also a big problem, but does not matter in your mock case, because there is no wait time.

Think about it this way. Let's say, you have one cpu (you probably have several, but there are other things going on the machine as well), and you are asking it to execute 10000 operations all at the same time.

How can this work? It will have to execute one of the requests, save the context, the stack, flush all caches, switch to the next request, execute that one ...

500 requests in 2 seconds is 4 milliseconds per request. Does not sound that bad, does it?

Also, have you turned your GC (on both server and client)? If requests are processed in bursts followed by long pauses, that's probably a sign of full GC kicking in

这篇关于Finagle 没有异步执行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆