Home Database Mysql Tutorial 硬盘写满后redis的处理机制

硬盘写满后redis的处理机制

Jun 07, 2016 pm 04:09 PM
redis deal with mechanism harddisk

前些天一台redis机器硬盘写满了,主要是由于程序bug导致备份量激增,而恰好监控程序的通知机制也罢工了,于是第一次体验到了redis的罢工(只读不写)。 现在我们来看下在磁盘写满后redis的处理机制: save流程:serverCron-rdbSaveBackground-rdbSave save后

前些天一台redis机器硬盘写满了,主要是由于程序bug导致备份量激增,而恰好监控程序的通知机制也罢工了,于是第一次体验到了redis的罢工(只读不写)。

现在我们来看下在磁盘写满后redis的处理机制:

save流程:serverCron->rdbSaveBackground->rdbSave
save后流程:serverCron->backgroundSaveDoneHandler
上述流程产生的结果就是server.lastbgsave_status = REDIS_ERR,

受其影响,processCommand和luaRedisGenericCommand中判断如果是写操作,则直接返回REDIS_OK,而没有实际写入

1.rdbSave所有的写出错都会返回REDIS_ERR

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

35

36

37

38

39

40

41

42

43

44

45

46

47

48

49

50

51

52

53

54

55

56

57

58

59

60

61

62

63

64

65

66

67

68

69

70

71

72

73

74

75

76

77

78

79

80

81

82

83

84

85

86

87

int rdbSave(char *filename) {

    dictIterator *di = NULL;

    dictEntry *de;

    char tmpfile[256];

    char magic[10];

    int j;

    long long now = mstime();

    FILE *fp;

    rio rdb;

    uint64_t cksum;

 

    snprintf(tmpfile,256,"temp-%d.rdb", (int) getpid());

    fp = fopen(tmpfile,"w");

    if (!fp) {

        redisLog(REDIS_WARNING, "Failed opening .rdb for saving: %s",

            strerror(errno));

        return REDIS_ERR;

    }

 

    rioInitWithFile(&rdb,fp);

    if (server.rdb_checksum)

        rdb.update_cksum = rioGenericUpdateChecksum;

    snprintf(magic,sizeof(magic),"REDIS%04d",REDIS_RDB_VERSION);

    if (rdbWriteRaw(&rdb,magic,9) == -1) goto werr;

 

    for (j = 0; j < server.dbnum; j++) {

        redisDb *db = server.db+j;

        dict *d = db->dict;

        if (dictSize(d) == 0) continue;

        di = dictGetSafeIterator(d);

        if (!di) {

            fclose(fp);

            return REDIS_ERR;

        }

 

        /* Write the SELECT DB opcode */

        if (rdbSaveType(&rdb,REDIS_RDB_OPCODE_SELECTDB) == -1) goto werr;

        if (rdbSaveLen(&rdb,j) == -1) goto werr;

 

        /* Iterate this DB writing every entry */

        while((de = dictNext(di)) != NULL) {

            sds keystr = dictGetKey(de);

            robj key, *o = dictGetVal(de);

            long long expire;

 

            initStaticStringObject(key,keystr);

            expire = getExpire(db,&key);

            if (rdbSaveKeyValuePair(&rdb,&key,o,expire,now) == -1) goto werr;

        }

        dictReleaseIterator(di);

    }

    di = NULL; /* So that we don&#39;t release it again on error. */

 

    /* EOF opcode */

    if (rdbSaveType(&rdb,REDIS_RDB_OPCODE_EOF) == -1) goto werr;

 

    /* CRC64 checksum. It will be zero if checksum computation is disabled, the

     * loading code skips the check in this case. */

    cksum = rdb.cksum;

    memrev64ifbe(&cksum);

    if (rioWrite(&rdb,&cksum,8) == 0) goto werr;

 

    /* Make sure data will not remain on the OS&#39;s output buffers */

    if (fflush(fp) == EOF) goto werr;

    if (fsync(fileno(fp)) == -1) goto werr;

    if (fclose(fp) == EOF) goto werr;

 

    /* Use RENAME to make sure the DB file is changed atomically only

     * if the generate DB file is ok. */

    if (rename(tmpfile,filename) == -1) {

        redisLog(REDIS_WARNING,"Error moving temp DB file on the final destination: %s", strerror(errno));

        unlink(tmpfile);

        return REDIS_ERR;

    }

    redisLog(REDIS_NOTICE,"DB saved on disk");

    server.dirty = 0;

    server.lastsave = time(NULL);

    server.lastbgsave_status = REDIS_OK;

    return REDIS_OK;

 

werr:

    fclose(fp);

    unlink(tmpfile);

    redisLog(REDIS_WARNING,"Write error saving DB on disk: %s", strerror(errno));

    if (di) dictReleaseIterator(di);

    return REDIS_ERR;

}

Copy after login

2.rdbSaveBackground中,如果子进程调用rdbsave返回REDIS_ERR,那么子进程exit(1)

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

35

36

37

38

39

40

41

42

43

44

int rdbSaveBackground(char *filename) {

    pid_t childpid;

    long long start;

 

    if (server.rdb_child_pid != -1) return REDIS_ERR;

 

    server.dirty_before_bgsave = server.dirty;

    server.lastbgsave_try = time(NULL);

 

    start = ustime();

    if ((childpid = fork()) == 0) {

        int retval;

 

        /* Child */

        closeListeningSockets(0);

        redisSetProcTitle("redis-rdb-bgsave");

        retval = rdbSave(filename);

        if (retval == REDIS_OK) {

            size_t private_dirty = zmalloc_get_private_dirty();

 

            if (private_dirty) {

                redisLog(REDIS_NOTICE,

                    "RDB: %zu MB of memory used by copy-on-write",

                    private_dirty/(1024*1024));

            }

        }

        exitFromChild((retval == REDIS_OK) ? 0 : 1);       //进程退出时返回0/1

    } else {

        /* Parent */

        server.stat_fork_time = ustime()-start;

        if (childpid == -1) {

            server.lastbgsave_status = REDIS_ERR;

            redisLog(REDIS_WARNING,"Can&#39;t save in background: fork: %s",

                strerror(errno));

            return REDIS_ERR;

        }

        redisLog(REDIS_NOTICE,"Background saving started by pid %d",childpid);

        server.rdb_save_time_start = time(NULL);

        server.rdb_child_pid = childpid;

        updateDictResizePolicy();

        return REDIS_OK;

    }

    return REDIS_OK; /* unreached */

}

Copy after login
3.bgsave完成后,serverCron中得到bgsave子进程的返回码进行后续处理

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

/* Check if a background saving or AOF rewrite in progress terminated. */

if (server.rdb_child_pid != -1 || server.aof_child_pid != -1) {

    int statloc;

    pid_t pid;

 

    if ((pid = wait3(&statloc,WNOHANG,NULL)) != 0) {

        int exitcode = WEXITSTATUS(statloc);

        int bysignal = 0;

 

        if (WIFSIGNALED(statloc)) bysignal = WTERMSIG(statloc);

 

        if (pid == server.rdb_child_pid) {

            backgroundSaveDoneHandler(exitcode,bysignal);    //根据bgsave子进程的exitcode以及是否由信号结束的标签进行后续处理

        } else if (pid == server.aof_child_pid) {

            backgroundRewriteDoneHandler(exitcode,bysignal);

        } else {

            redisLog(REDIS_WARNING,

                "Warning, detected child with unmatched pid: %ld",

                (long)pid);

        }

        updateDictResizePolicy();

    }

}

Copy after login
4.如果子进程非信号结束,并且exitcode非0,那么设置bgsave状态为REDIS_ERR

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

void backgroundSaveDoneHandler(int exitcode, int bysignal) {

    if (!bysignal && exitcode == 0) {

        redisLog(REDIS_NOTICE,

            "Background saving terminated with success");

        server.dirty = server.dirty - server.dirty_before_bgsave;

        server.lastsave = time(NULL);

        server.lastbgsave_status = REDIS_OK;

    } else if (!bysignal && exitcode != 0) {

        redisLog(REDIS_WARNING, "Background saving error");

        server.lastbgsave_status = REDIS_ERR;      //状态转换

    } else {

        mstime_t latency;

 

        redisLog(REDIS_WARNING,

            "Background saving terminated by signal %d", bysignal);

        latencyStartMonitor(latency);

        rdbRemoveTempFile(server.rdb_child_pid);

        latencyEndMonitor(latency);

        latencyAddSampleIfNeeded("rdb-unlink-temp-file",latency);

        /* SIGUSR1 is whitelisted, so we have a way to kill a child without

         * tirggering an error conditon. */

        if (bysignal != SIGUSR1)

            server.lastbgsave_status = REDIS_ERR;

    }

    server.rdb_child_pid = -1;

    server.rdb_save_time_last = time(NULL)-server.rdb_save_time_start;

    server.rdb_save_time_start = -1;

    /* Possibly there are slaves waiting for a BGSAVE in order to be served

     * (the first stage of SYNC is a bulk transfer of dump.rdb) */

    updateSlavesWaitingBgsave((!bysignal && exitcode == 0) ? REDIS_OK : REDIS_ERR);

}

Copy after login
5.processCommand中判定cmd是写操作的话,直接返回REDIS_OK

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

/* Don&#39;t accept write commands if there are problems persisting on disk

 * and if this is a master instance. */

if (((server.stop_writes_on_bgsave_err &&

      server.saveparamslen > 0 &&

      server.lastbgsave_status == REDIS_ERR) ||

      server.aof_last_write_status == REDIS_ERR) &&

    server.masterhost == NULL &&

    (c->cmd->flags & REDIS_CMD_WRITE ||

     c->cmd->proc == pingCommand))

{

    flagTransaction(c);

    if (server.aof_last_write_status == REDIS_OK)

        addReply(c, shared.bgsaveerr);

    else

        addReplySds(c,

            sdscatprintf(sdsempty(),

            "-MISCONF Errors writing to the AOF file: %s\r\n",

            strerror(server.aof_last_write_errno)));

    return REDIS_OK;

}

Copy after login
6.luaRedisGenericCommand中判定cmd是写操作的话,屏蔽

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

35

36

37

38

39

40

41

42

43

44

45

46

47

48

49

50

51

52

53

54

55

56

57

58

59

60

61

    /* Write commands are forbidden against read-only slaves, or if a

     * command marked as non-deterministic was already called in the context

     * of this script. */

    if (cmd->flags & REDIS_CMD_WRITE) {

        if (server.lua_random_dirty) {

            luaPushError(lua,

                "Write commands not allowed after non deterministic commands");

            goto cleanup;

        } else if (server.masterhost && server.repl_slave_ro &&

                   !server.loading &&

                   !(server.lua_caller->flags & REDIS_MASTER))

        {

            luaPushError(lua, shared.roslaveerr->ptr);

            goto cleanup;

        } else if (server.stop_writes_on_bgsave_err &&

                   server.saveparamslen > 0 &&

                   server.lastbgsave_status == REDIS_ERR)

        {

            luaPushError(lua, shared.bgsaveerr->ptr);

            goto cleanup;

        }

    }

 

cleanup:

    /* Clean up. Command code may have changed argv/argc so we use the

     * argv/argc of the client instead of the local variables. */

    for (j = 0; j < c->argc; j++) {

        robj *o = c->argv[j];

 

        /* Try to cache the object in the cached_objects array.

         * The object must be small, SDS-encoded, and with refcount = 1

         * (we must be the only owner) for us to cache it. */

        if (j < LUA_CMD_OBJCACHE_SIZE &&

            o->refcount == 1 &&

            o->encoding == REDIS_ENCODING_RAW &&

            sdslen(o->ptr) <= LUA_CMD_OBJCACHE_MAX_LEN)

        {

            struct sdshdr *sh = (void*)(((char*)(o->ptr))-(sizeof(struct sdshdr)));

 

            if (cached_objects[j]) decrRefCount(cached_objects[j]);

            cached_objects[j] = o;

            cached_objects_len[j] = sh->free + sh->len;

        } else {

            decrRefCount(o);

        }

    }

 

    if (c->argv != argv) {

        zfree(c->argv);

        argv = NULL;

    }

 

    if (raise_error) {

        /* If we are here we should have an error in the stack, in the

         * form of a table with an "err" field. Extract the string to

         * return the plain error. */

        lua_pushstring(lua,"err");

        lua_gettable(lua,-2);

        return lua_error(lua);

    }

    return 1;

Copy after login
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

How to build the redis cluster mode How to build the redis cluster mode Apr 10, 2025 pm 10:15 PM

Redis cluster mode deploys Redis instances to multiple servers through sharding, improving scalability and availability. The construction steps are as follows: Create odd Redis instances with different ports; Create 3 sentinel instances, monitor Redis instances and failover; configure sentinel configuration files, add monitoring Redis instance information and failover settings; configure Redis instance configuration files, enable cluster mode and specify the cluster information file path; create nodes.conf file, containing information of each Redis instance; start the cluster, execute the create command to create a cluster and specify the number of replicas; log in to the cluster to execute the CLUSTER INFO command to verify the cluster status; make

How to clear redis data How to clear redis data Apr 10, 2025 pm 10:06 PM

How to clear Redis data: Use the FLUSHALL command to clear all key values. Use the FLUSHDB command to clear the key value of the currently selected database. Use SELECT to switch databases, and then use FLUSHDB to clear multiple databases. Use the DEL command to delete a specific key. Use the redis-cli tool to clear the data.

How to read redis queue How to read redis queue Apr 10, 2025 pm 10:12 PM

To read a queue from Redis, you need to get the queue name, read the elements using the LPOP command, and process the empty queue. The specific steps are as follows: Get the queue name: name it with the prefix of "queue:" such as "queue:my-queue". Use the LPOP command: Eject the element from the head of the queue and return its value, such as LPOP queue:my-queue. Processing empty queues: If the queue is empty, LPOP returns nil, and you can check whether the queue exists before reading the element.

How to use the redis command How to use the redis command Apr 10, 2025 pm 08:45 PM

Using the Redis directive requires the following steps: Open the Redis client. Enter the command (verb key value). Provides the required parameters (varies from instruction to instruction). Press Enter to execute the command. Redis returns a response indicating the result of the operation (usually OK or -ERR).

How to use redis lock How to use redis lock Apr 10, 2025 pm 08:39 PM

Using Redis to lock operations requires obtaining the lock through the SETNX command, and then using the EXPIRE command to set the expiration time. The specific steps are: (1) Use the SETNX command to try to set a key-value pair; (2) Use the EXPIRE command to set the expiration time for the lock; (3) Use the DEL command to delete the lock when the lock is no longer needed.

How to read the source code of redis How to read the source code of redis Apr 10, 2025 pm 08:27 PM

The best way to understand Redis source code is to go step by step: get familiar with the basics of Redis. Select a specific module or function as the starting point. Start with the entry point of the module or function and view the code line by line. View the code through the function call chain. Be familiar with the underlying data structures used by Redis. Identify the algorithm used by Redis.

How to use the redis command line How to use the redis command line Apr 10, 2025 pm 10:18 PM

Use the Redis command line tool (redis-cli) to manage and operate Redis through the following steps: Connect to the server, specify the address and port. Send commands to the server using the command name and parameters. Use the HELP command to view help information for a specific command. Use the QUIT command to exit the command line tool.

How to solve data loss with redis How to solve data loss with redis Apr 10, 2025 pm 08:24 PM

Redis data loss causes include memory failures, power outages, human errors, and hardware failures. The solutions are: 1. Store data to disk with RDB or AOF persistence; 2. Copy to multiple servers for high availability; 3. HA with Redis Sentinel or Redis Cluster; 4. Create snapshots to back up data; 5. Implement best practices such as persistence, replication, snapshots, monitoring, and security measures.

See all articles