Redis Bio

作者: 烨哥 | 来源:发表于2018-03-21 15:05 被阅读14次

    简介

    Redis Bio 就是Background I/O service for Redis.
    他把那些耗时的io操作放到后台的线程来执行。主线程就专注服务。避免那些耗时的释放,写入操作等造成服务的等待。
    实现也特别的简单。采用新建处理线程。使用临界区投放任务的方式完成操作。

    核心的一些定义和方法

    static pthread_t bio_threads[BIO_NUM_OPS]; //保存pthread
    static pthread_mutex_t bio_mutex[BIO_NUM_OPS];//保存mutex
    static pthread_cond_t bio_newjob_cond[BIO_NUM_OPS];//保存新任务条件变量
    static pthread_cond_t bio_step_cond[BIO_NUM_OPS];//保存step条件变量
    static list *bio_jobs[BIO_NUM_OPS];//保存joblist
    
    static unsigned long long bio_pending[BIO_NUM_OPS];//保存pening数量
    
    struct bio_job {
        time_t time; /* Time at which the job was created. */ //创建时间
        /* Job specific arguments pointers. If we need to pass more than three
         * arguments we can just pass a pointer to a structure or alike. */
        void *arg1, *arg2, *arg3;//三个保存的参数
    };
    

    初始化bio

    /* Initialize the background system, spawning the thread. */
    void bioInit(void) {
        pthread_attr_t attr;
        pthread_t thread;
        size_t stacksize;
        int j;
    
        /* Initialization of state vars and objects */
        for (j = 0; j < BIO_NUM_OPS; j++) {//基本的初始化操作
            pthread_mutex_init(&bio_mutex[j],NULL);
            pthread_cond_init(&bio_newjob_cond[j],NULL);
            pthread_cond_init(&bio_step_cond[j],NULL);
            bio_jobs[j] = listCreate();//创建任务队列 
            bio_pending[j] = 0;//等待数量是0
        }
    
        /* Set the stack size as by default it may be small in some system */
        pthread_attr_init(&attr);
        pthread_attr_getstacksize(&attr,&stacksize);
        if (!stacksize) stacksize = 1; /* The world is full of Solaris Fixes */
        while (stacksize < REDIS_THREAD_STACK_SIZE) stacksize *= 2;
        pthread_attr_setstacksize(&attr, stacksize);//stack size
    
        /* Ready to spawn our threads. We use the single argument the thread
         * function accepts in order to pass the job ID the thread is
         * responsible of. */
        for (j = 0; j < BIO_NUM_OPS; j++) {
            void *arg = (void*)(unsigned long) j;
            if (pthread_create(&thread,&attr,bioProcessBackgroundJobs,arg) != 0) {//创建线程
                serverLog(LL_WARNING,"Fatal: Can't initialize Background Jobs.");
                exit(1);
            }
            bio_threads[j] = thread;
        }
    }
    

    整个创建过程是一个标准的初始化过程。没有什么特别的地方。

    线程函数

    void *bioProcessBackgroundJobs(void *arg) {//线程入口
        struct bio_job *job;
        unsigned long type = (unsigned long) arg;
        sigset_t sigset;
    
        /* Check that the type is within the right interval. */
        if (type >= BIO_NUM_OPS) {//type不对 
            serverLog(LL_WARNING,
                "Warning: bio thread started with wrong type %lu",type);
            return NULL;
        }
    
        /* Make the thread killable at any time, so that bioKillThreads()
         * can work reliably. */
        pthread_setcancelstate(PTHREAD_CANCEL_ENABLE, NULL);//设置响应cancel
        pthread_setcanceltype(PTHREAD_CANCEL_ASYNCHRONOUS, NULL);//设置立即结束
    
        pthread_mutex_lock(&bio_mutex[type]);//锁住互斥
        /* Block SIGALRM so we are sure that only the main thread will
         * receive the watchdog signal. */
        sigemptyset(&sigset);
        sigaddset(&sigset, SIGALRM);
        if (pthread_sigmask(SIG_BLOCK, &sigset, NULL))//只让主线程处理 SIGALRM
            serverLog(LL_WARNING,
                "Warning: can't mask SIGALRM in bio.c thread: %s", strerror(errno));
    
        while(1) {
            listNode *ln;
    
            /* The loop always starts with the lock hold. */
            if (listLength(bio_jobs[type]) == 0) {
                pthread_cond_wait(&bio_newjob_cond[type],&bio_mutex[type]);//释放mutex 并且进入阻塞等待
                continue;//防止意外唤醒
            }
            /* Pop the job from the queue. */
            ln = listFirst(bio_jobs[type]);// 获取新的任务
            job = ln->value;
            /* It is now possible to unlock the background system as we know have
             * a stand alone job structure to process.*/
            pthread_mutex_unlock(&bio_mutex[type]);//释放锁可以加入新的任务
    
            /* Process the job accordingly to its type. */
            if (type == BIO_CLOSE_FILE) {//判断自己的类型
                close((long)job->arg1);
            } else if (type == BIO_AOF_FSYNC) {
                aof_fsync((long)job->arg1);
            } else if (type == BIO_LAZY_FREE) {
                /* What we free changes depending on what arguments are set:
                 * arg1 -> free the object at pointer.
                 * arg2 & arg3 -> free two dictionaries (a Redis DB).
                 * only arg3 -> free the skiplist. */
                if (job->arg1)
                    lazyfreeFreeObjectFromBioThread(job->arg1);
                else if (job->arg2 && job->arg3)
                    lazyfreeFreeDatabaseFromBioThread(job->arg2,job->arg3);
                else if (job->arg3)
                    lazyfreeFreeSlotsMapFromBioThread(job->arg3);
            } else {
                serverPanic("Wrong job type in bioProcessBackgroundJobs().");
            }
            zfree(job);
    
            /* Unblock threads blocked on bioWaitStepOfType() if any. */
            pthread_cond_broadcast(&bio_step_cond[type]);//唤醒所有的 setp_cond
    
            /* Lock again before reiterating the loop, if there are no longer
             * jobs to process we'll block again in pthread_cond_wait(). */
            pthread_mutex_lock(&bio_mutex[type]);//锁住
            listDelNode(bio_jobs[type],ln);//删除完成的任务
            bio_pending[type]--;//pending --
        }
    }
    

    整理流程:
    1获取锁,判断任务是否存在,不存在则进入cond_wait,存在就提取出一个任务,然后根据任务类型,进行操作。
    2.执行结束后唤醒所有的step_cond,再次获取锁。删除完成的任务。进入循环

    创建一个任务

    void bioCreateBackgroundJob(int type, void *arg1, void *arg2, void *arg3) {//创建job
        struct bio_job *job = zmalloc(sizeof(*job));//分配内存
        job->time = time(NULL);//开始时间
        job->arg1 = arg1;//保存参数
        job->arg2 = arg2;
        job->arg3 = arg3;
        pthread_mutex_lock(&bio_mutex[type]);//获取锁
        listAddNodeTail(bio_jobs[type],job);//插入任务
        bio_pending[type]++;//pending++
        pthread_cond_signal(&bio_newjob_cond[type]);//通知等待
        pthread_mutex_unlock(&bio_mutex[type]);
    }
    

    关闭后台io线程

    void bioKillThreads(void) {
        int err, j;
    
        for (j = 0; j < BIO_NUM_OPS; j++) {
            if (pthread_cancel(bio_threads[j]) == 0) {//发送cancle
                if ((err = pthread_join(bio_threads[j],NULL)) != 0) {//join等待
                    serverLog(LL_WARNING,
                        "Bio thread for job type #%d can be joined: %s",
                            j, strerror(err));
                } else {
                    serverLog(LL_WARNING,
                        "Bio thread for job type #%d terminated",j);
                }
            }
        }
    }
    

    一些状态的获取

    unsigned long long bioWaitStepOfType(int type) {
        unsigned long long val;
        pthread_mutex_lock(&bio_mutex[type]);//加锁
        val = bio_pending[type];
        if (val != 0) {//
            pthread_cond_wait(&bio_step_cond[type],&bio_mutex[type]);//d等待通知
            val = bio_pending[type];
        }
        pthread_mutex_unlock(&bio_mutex[type]);//释放锁
        return val;
    }
    unsigned long long bioPendingJobsOfType(int type) {
        unsigned long long val;
        pthread_mutex_lock(&bio_mutex[type]);
        val = bio_pending[type];
        pthread_mutex_unlock(&bio_mutex[type]);
        return val;
    }
    

    相关文章

      网友评论

        本文标题:Redis Bio

        本文链接:https://www.haomeiwen.com/subject/eujgqftx.html