squid-2.6.STABLE13 修改手记

来源:互联网 发布:淘宝图片保护在哪里看 编辑:程序博客网 时间:2024/05/16 13:58

squid 2.6.STABLE13 中cache_peer 中weight分配有问题,不能达到预期效果,这些算法很简单,不知道为什么开发者会出这样的问题,而一直也没有修正,网上一直也没有出一个完整的解决方法,以下是魔鬼对源代码的修改
修改 cache_cf.c中parse_peer函数,让weight可以为0,原来weigth小于1就赋值为1, 效果是当weight<=0,关闭cache_peer:
static void
parse_peer(peer ** head)
{
...
    if (peerFindByName(p->name))
        fatalf("ERROR: cache_peer %s specified twice/n", p->name);
    / * 注释掉原来的分配算法 * /
    / *
    if (p->weight < 1)
        p->weight = 1;
    * /
    / * 注释代码结束 * /
    / * 加入下列代码 * /
    / *
    * change by devil, weight=0, the peer is disable
    * /
    if (p->weight < 0)
        p->weight = 0;
    / * 加入代码结束 * /
    p->icp.version = ICP_VERSION_CURRENT;
    p->test_fd = -1;
#if USE_CACHE_DIGESTS
    if (!p->options.no_digest) {
        p->digest = peerDigestCreate(p);
        cbdataLock(p->digest); / * so we know when/if digest disappears * /
    }
#endif
...
}

修改neighbors.c中getRoundRobinParent函数,修正不正确的weight分配:
peer *
getRoundRobinParent(request_t * request)
{
    peer *p;
    peer *q = NULL;
    for (p = Config.peers; p; p = p->next) {
        if (!p->options.roundrobin)
             continue;
        if (neighborType(p, request) != PEER_PARENT)
             continue;
        if (!peerHTTPOkay(p, request))
             continue;
        / * 注释原有算法 * /
        / *
        if (p->weight == 1) { / * round-bin * /
             / * 当前选中的peer分配count小于当前peer分配count则继续匹配下一个peer * /
             if (q && q->rr_count < p->rr_count)
                 continue;
        } else if (p->weight == 0 || (q && q->rr_count < (p->rr_count / p->weight))) {
             / * q->weight 应该与p->weight对应才能正确选择 * /
             continue;
        }
        * /
        / * 注释结束 * /
        / * 加入如下代码 * /
        / *
        * Change by devil
        * /
        if (p->weight > 0) {
                 if (q && (q->rr_count / q->weight) < (p->rr_count / p->weight))
                         continue;
         } else
                 continue;
        / *  加入代码结束 * /
         q = p;
     }
     if (q)
         q->rr_count++;
     debug(15, 3) ("getRoundRobinParent: returning %s/n", q ? q->host : "NULL");
     return q;
}

重新编译安装过后使用squidclient  mgr:server_list 查看Peer Cache Statistics中peer分配状态