• Varnish升级到2.0.3 - [技术笔记]

    2009-02-26

    分类: 技术笔记

    版权声明:转载时请以超链接形式标明文章原始出处和作者信息及本声明
    http://iyubo.blogbus.com/logs/35752419.html

    Varnish在2月12日又更新了版本,更新日志:

    Varnish 2.0.3 has just been released. This release contains multiple changes, amongst them:

    • Support for backend timeouts
    • Multiple fixes in how we process ESI
    • restart in vcl_hit is now supported
    • Documentation has been updated
    • Expiry processing is now more scalable
    • The default session workspace is now 16k instead of 8k
    • More graceful handling of too many headers from the client or the server.
    • More expressive purges

    之前一直使用的还是2.0的一个trunk的R2860版本,因为只有这个版本我从1.1.2升级上来以后没有慢的问题⊙﹏⊙。但是看着新版却一直不能用,实在是让人心里痒。于是抱着死磕到底的态度,在检查了n+1遍配置文件和修改启动参数重启了n+1遍Varnish以后终于找到了问题的所在,即启动参数的-w这个上面。那么这个参数是干什么用的呢?

        -w int[,int[,int]]           # Number of worker threads
                                     #   -w <fixed_count>
                                     #   -w min,max
                                     #   -w min,max,timeout [default: -w2,500,300]

    可以看出这个参数是控制每个进程的线程数的,1.1.2版本的时候这个参数我配置的是-w30000,51200,10,貌似到了2.0版以后这个最小启动的线程数不能设定过大,于是在进行了几次调试以后最终将参数定为了-w5,51200,30后终于解决了2.0版本Varnish慢的问题。具体原因到底是什么目前还没有时间去仔细琢磨,但总算是解决了这个最大的疑问。

    我的线上服务器已全部升级至2.0.3版,目前运行良好,推荐大家更新。

    BTW: 2.0版本相对1.1版本的vcl语法有一些小的改动,原来的insert改名成了deliver,2.0.3版本需要使用return来调用lookup,pass,pipe等功能。


    收藏到:Del.icio.us




    评论

  • 谢谢,确实是-w参数会有问题,调整了就可以了!
    thx
  • backend 搞好复杂啊@不方便管理
  • haproxy将cgi-bin的url匹配后都转发到varnish,不是很会配varnish的cache规则,上面题的vcl就是我的v_recv的,麻烦帮我看下哈
    Kevin回复pippen说:
    最好贴一个具体url的例子
    2009-03-04 17:21:34
  • vcl贴得太长了,不好意思
    观察stat时发现hits很少很少
    Hitrate ratio: 10 100 1000
    Hitrate avg: 0.1732 0.0574 0.0604

    18375 20.00 19.57 Client connections accepted
    15557 19.00 16.57 Client requests received
    23 0.00 0.02 Cache hits
    1 0.00 0.00 Cache hits for pass
    599 2.00 0.64 Cache misses
    15533 19.00 16.54 Backend connections success
    0 0.00 0.00 Backend connections failures
    11454 17.00 12.20 Backend connections reuses
    15487 22.00 1***9 Backend connections recycles
    3 . . N struct srcaddr
    0 . . N active struct srcaddr
    23 . . N struct sess_mem
    1 . . N struct sess
    70 . . N struct object
    90 . . N struct objecthead
    157 . . N struct smf
    12 . . N small free smf
    5 . . N large free smf
    6 . . N struct vbe_conn
    19 . . N struct bereq
    20 . . N worker threads
    23 0.00 0.02 N worker threads created
    0 0.00 0.00 N queued work requests
    19 0.00 0.02 N overflowed work requests
    4 . . N backends
    529 . . N expired objects
    17 . . N LRU moved objects
    15563 22.00 16.57 Objects sent with write
    18375 23.00 19.57 Total Sessions
    15557 22.00 16.57 Total Requests
    0 0.00 0.00 Total pipe
    14934 19.00 15.90 Total pass
    15533 22.00 16.54 Total fetch
    4270247 6042.67 4547.65 Total header bytes
    158054370 251058.41 168322.01 Total body bytes
    Kevin回复pippen说:
    命中率确实很低,但是你的请求也很少啊。
    另外,你的URL都是什么样的?是都是带?跟参数的么?
    建议只使用url来做hash进行cache,在vcl_fetch里面可以加上cache过期时间观察一下效果。
    只能一点一点调了
    2009-03-04 12:29:24
  • 我的想法是这样:目前每天有500W的PV,将大部分的cgi请求都经由varnish缓存。
    架构是:
    用户->haproxy->varnish->apache
    vcl配置:
    #backend vhost
    backend cgi1 {
    .host = "172.23.8.140";
    .port = "80";
    }

    backend cgi2 {
    .host = "172.16.81.202";
    .port = "80";
    }

    backend cgi3 {
    .host = "172.16.195.12";
    .port = "80";
    }

    backend cgi4 {
    .host = "172.23.154.41";
    .port = "80";
    }

    director default random {
    .retries = 5;
    /* We can refer to named backends */
    {
    .backend = cgi1;
    .weight = 10;
    }

    {
    .backend = cgi2;
    .weight = 5;
    }
    {
    .backend = cgi3;
    .weight = 5;
    }
    {
    .backend = cgi4;
    .weight = 10;
    }
    }

    sub vcl_recv {
    # remove req.http.X-Forwarded-For;
    # set req.http.X-Forwarded-For = client.ip;

    remove req.http.X-Forwarded-For;
    remove req.http.X-Forwarded-for;
    remove req.http.X-Forwarded-Host;
    remove req.http.X-Forwarded-Server;
    set req.http.X-Forwarded-For = client.ip;

    # if (req.http.host ~ "^x.soso.com") {
    if (req.restarts == 0) {
    set req.backend = default;
    } else if(req.restarts == 1) {
    set req.backend = cgi1;
    } else if(req.restarts == 2) {
    set req.backend = cgi2;
    } else if(req.restarts == 3) {
    set req.backend = cgi3;
    } else if(req.restarts == 4) {
    set req.backend = cgi4;
    }

    if (req.http.host ~ "^x.soso.com") {
    set req.backend = default;
    }else{
    error 404 "wrong ip";
    }

    if (req.request != "GET" && req.request != "HEAD") {
    pipe;
    }

    if (req.http.Expect) {
    pipe;
    }

    if (req.http.Authenticate || req.http.Cookie) {
    pass;
    }
    lookup;

    # } elseif (req.http.host ~ "^(www)|(bbs)|(doc).linuxtone.org") {
    # set req.backend = www;
    # } elseif (req.http.host ~ ".netseek.com") {
    # set req.backend = netseek;
    # } else {
    # error 404 "the server is wrong!";
    # }

    # if (req.request != "GET" && req.request != "HEAD" && req.request != "POST") {
    # pipe;
    # } elseif (req.url ~ "\.(php|cgi)($|\?)") {
    # pass;
    # }
    # lookup;
    }

    sub vcl_hit {
    if (req.request == "PURGE") {
    set obj.ttl = 0s;
    error 200 "Purged.";
    }

    if (!obj.cacheable) {
    pass;
    }
    deliver;
    }

    sub vcl_miss {
    if (req.request == "PURGE") {
    error 404 "Not in cache.";
    }
    fetch;
    }

    sub vcl_fetch {
    if (!obj.cacheable) {
    pass;
    }
    if (obj.http.Set-Cookie) {
    pass;
    }
    if (obj.status != 200 && req.restarts < 5) {
    restart;
    }

    set obj.prefetch = -30s;
    deliver;
    }

    sub vcl_pipe {
    pipe;
    }

    sub vcl_pass {
    pass;
    }

    sub vcl_hash {
    set req.hash += req.url;
    if (req.http.host) {
    set req.hash += req.http.host;
    } else {
    set req.hash += server.ip;
    }
    hash;
    }

    sub vcl_deliver {
    deliver;
    }
  • 楼主,您好
    想看看您的vcl配置,另外问个问题,为什么我的varnishstat看到的cache hits和misses差这么多,能否加下你的qq或msn具体聊一下呢

    我的qq 78961257
    msn pippen__2333@hotmail.com
    gtalk pippenpp@gmail.com
    Kevin回复pippen说:
    呵呵,可以直接在这里讨论,大家都能看到。Cache命中率不高的情况需要结合实际业务需求来进行配置文件调整,你可以大概描述一下你的需求。
    另外,如果Varnish运行的不稳定也有可能造成cache命中率低的情况。
    2009-03-04 12:03:46
  • 呵呵,远远不只这些机器咯
  • 呵呵,速度之快

    我现在是没有机器测试了,所有一百来台机器跑了N多服务.新机器还没到货.
    Kevin回复freeke说:
    呵呵,你比我管理的机器多的多撒:)
    2009-02-26 12:03:08