redis debug命令詳解

chenfeng發表於2018-12-24

redis debug命令提供了幾個非常實用的debug功能,本文介紹下redis下的debug命令。


debug segment

讓redis發生段錯誤,如果開啟了coredump,則會產生core檔案。這個命令實現很簡單,直接往一個非法地址上寫資料。


*((char*)-1) = 'x';

debug oom

申請一大片記憶體,直接讓zmalloc觸發oom錯誤


void *ptr = zmalloc(ULONG_MAX); /* Should trigger an out of memory. */

zfree(ptr);

addReply(c,shared.ok);

debug assert

不多解釋


redisAssertWithInfo(c,c->argv[0],1 == 2);


debug reload

save當前的rdb檔案,並清空當前資料庫,重新載入rdb,載入與啟動時載入類似,載入過程中只能服務部分只讀請求(比如info、ping等)


rdbSave();

emptyDb();

rdbLoad();

debug loadaof

清空當前資料庫,重新從aof檔案里載入資料庫


emptyDb();

loadAppendOnlyFile();

debug object

檢視一個key內部資訊,比如refcount、encoding、serializedlength等,結果如下


Value at:0x7f21b9479850 refcount:1 encoding:raw serializedlength:6 lru:8462202 lru_seconds_idle:215

debug sdslen

檢視某個sds當前的資訊,當前sds長度,以及可用記憶體長度,結果如下


key_sds_len:3, key_sds_avail:0, val_sds_len:5, val_sds_avail:0

debug populate

測試利器,快速產生大量的key


127.0.0.1:6379> debug populate 10000

OK

127.0.0.1:6379> dbsize

(integer) 10000

debug digest

對整個資料庫的資料,產生一個摘要,可用於驗證兩個redis資料庫資料是否一致


127.0.0.1:6379> debug digest


7164ae8b6730c8bcade46532e5e4a8015d4cccfb


127.0.0.1:6379> debug digest


7164ae8b6730c8bcade46532e5e4a8015d4cccfb


debug sleep

測試利器,用於模擬某個時間開銷的命令,比如debug sleep 0.1就相當於執行了一條開銷為100ms的命令。


127.0.0.1:6379> debug sleep 1

OK

(1.00s)

debug error

測試利器,模擬一條命令執行失敗,傳送debug error,redis直接會返回一個錯誤應答


127.0.0.1:6379> debug error "test"

(error) test


來自 “ ITPUB部落格 ” ,連結:http://blog.itpub.net/15498/viewspace-2286238/,如需轉載,請註明出處,否則將追究法律責任。