Esempio n. 1
0
void CFormatMonitorToIoBuf::formatProxyToIoBuf(CProxyMonitor& proxyMonirot) {
    m_iobuf->append("[OneCache]\n");
    m_iobuf->appendFormatString("Version=%s\n", APP_VERSION);
    m_iobuf->appendFormatString("Port=%d\n",proxyMonirot.redisProxy()->address().port());
    m_iobuf->appendFormatString("StartTime=%s\n", CTimming::toString(proxyMonirot.proxyBeginTime()));
    time_t now = time(NULL);
    time_t upTime = now - proxyMonirot.proxyBeginTime();
    m_iobuf->appendFormatString("UpTime=%s", CTimming::secondToString((int)upTime));
    RedisProxy* proxy = proxyMonirot.redisProxy();
    m_iobuf->appendFormatString("GroupCount=%d\n", proxy->groupCount());
    m_iobuf->appendFormatString("VipEnabled=%s\n", proxy->vipEnabled()?"Yes":"No");
    m_iobuf->appendFormatString("TopKeyEnabled=%s\n", CRedisProxyCfg::instance()->topKeyEnable()?"Yes":"No");
    if (proxyMonirot.redisProxy()->vipEnabled()) {
        m_iobuf->appendFormatString("VipName=%s\n", proxy->vipName());
        m_iobuf->appendFormatString("VipAddress=%s\n", proxy->vipAddress());
    }
    m_iobuf->append("\n[Backends]\n");
    int groupCnt = proxy->groupCount();
    m_iobuf->append("[GROUP]                [IP]    [PORT] [CONNPOOL] [MASTER] [ACTIVE]      [REQUESTS]        [RECV SIZE]       [SEND SIZE]    [SEND>1KB]  [SEND>1MB]   [COMMANDS]\n");
    for (int  i = 0; i < groupCnt; i++) {
        RedisServantGroup* group = proxy->group(i);
        formatServants(proxyMonirot, group);
    }
    m_iobuf->append("\n");
}
Esempio n. 2
0
File: redis.cpp Progetto: RTS2/rts2
int main (int argc, char **argv)
{
    RedisProxy proxy (argc, argv);
    return proxy.run();
}
Esempio n. 3
0
void startOneCache(void)
{
    CRedisProxyCfg* cfg = CRedisProxyCfg::instance();
    setupSignal();

    EventLoop listenerLoop;
    bool twemproxyMode = cfg->isTwemproxyMode();
    const int points_per_server = 160;
    const int pointer_per_hash = 4;
    int slot_index = 0;

    RedisProxy proxy;
    proxy.setEventLoop(&listenerLoop);
    loop = &listenerLoop;
    CProxyMonitor monitor;
    proxy.setMonitor(&monitor);

    const int defaultPort = 8221;
    int port = cfg->port();
    if (port <= 0) {
        LOG(Logger::Debug, "Port %d invalid, use default port %d", defaultPort);
        port = defaultPort;
    }

    if (!proxy.run(HostAddress(port))) {
        exit(APP_EXIT_KEY);
    }

    proxy.setTwemproxyModeEnabled(twemproxyMode);

    EventLoopThreadPool pool;
    pool.start(cfg->threadNum());
    proxy.setEventLoopThreadPool(&pool);

    const SVipInfo* vipInfo = cfg->vipInfo();
    proxy.setVipName(vipInfo->if_alias_name);
    proxy.setVipAddress(vipInfo->vip_address);
    proxy.setVipEnabled(vipInfo->enable);

    const SHashInfo* sHashInfo = cfg->hashInfo();

    if (!twemproxyMode) {
        proxy.setSlotCount(sHashInfo->hash_value_max);
    } else {
        proxy.setSlotCount(cfg->groupCnt() * points_per_server);
    }

    const GroupOption* groupOption = cfg->groupOption();
    proxy.setGroupRetryTime(groupOption->group_retry_time);
    proxy.setAutoEjectGroupEnabled(groupOption->auto_eject_group);
    proxy.setEjectAfterRestoreEnabled(groupOption->eject_after_restore);
    proxy.setPassword(cfg->password());

    for (int i = 0; i < cfg->groupCnt(); ++i) {
        const CGroupInfo* info = cfg->group(i);

        RedisServantGroup* group = new RedisServantGroup;
        RedisServantGroupPolicy* policy = RedisServantGroupPolicy::createPolicy(info->groupPolicy());
        group->setGroupName(info->groupName());
        group->setPolicy(policy);

        const HostInfoList& hostList = info->hosts();
        HostInfoList::const_iterator itHost = hostList.begin();
        for (; itHost != hostList.end(); ++itHost) {
            const CHostInfo& hostInfo = (*itHost);
            RedisServant* servant = new RedisServant;
            RedisServant::Option opt;
            strcpy(opt.name, hostInfo.get_hostName().c_str());
            opt.poolSize = hostInfo.get_connectionNum();
            opt.reconnInterval = groupOption->backend_retry_interval;
            opt.maxReconnCount = groupOption->backend_retry_limit;
            servant->setOption(opt);
            servant->setRedisAddress(HostAddress(hostInfo.get_ip().c_str(), hostInfo.get_port()));
            servant->setEventLoop(proxy.eventLoop());
            if (hostInfo.get_master()) {
                group->addMasterRedisServant(servant);
            } else {
                group->addSlaveRedisServant(servant);
            }
            servant->connectionPool()->setPassword(itHost->passWord());
        }
        group->setEnabled(true);
        proxy.addRedisGroup(group);

        if (!twemproxyMode) {
            for (int i = info->hashMin(); i <= info->hashMax(); ++i) {
                proxy.setSlot(i, group);
            }

            for (int i = 0; i < cfg->hashMapCnt(); ++i) {
                const CHashMapping* mapping = cfg->hashMapping(i);
                RedisServantGroup* group = proxy.group(mapping->group_name);
                proxy.setSlot(mapping->hash_value, group);
            }
        } else {
            Slot* slot = proxy.slotData();
            for (int pointer_index = 1;
                    pointer_index <= points_per_server / pointer_per_hash;
                    pointer_index++) {

                char host[86];
                int hostlen = sprintf(host, "%s-%u", group->groupName(), pointer_index - 1);
                for (int x = 0; x < pointer_per_hash; x++) {
                    slot[slot_index].group = group;
                    slot[slot_index++].value = ketama_hash(host, hostlen, x);
                }
            }
        }
    }

    if (twemproxyMode) {
        LOG(Logger::Debug, "Twemproxy mode. dist=ketama slotcount=%d", proxy.slotCount());
        Slot* slot = proxy.slotData();
        int slotCount = proxy.slotCount();
        qsort(slot, slotCount, sizeof(Slot), slot_item_cmp);
    }

    HashFunc func = hashfuncFromName(cfg->hashFunctin().c_str());
    proxy.setHashFunction(func);

    for (int i = 0; i < cfg->keyMapCnt(); ++i) {
        const CKeyMapping* mapping = cfg->keyMapping(i);
        RedisServantGroup* group = proxy.group(mapping->group_name);
        if (group != NULL) {
            proxy.addGroupKeyMapping(mapping->key, strlen(mapping->key), group);
        }
    }

    LOG(Logger::Message, "Start the %s on port %d. PID: %d", APP_NAME, port, getpid());
    listenerLoop.exec();
}