自拍偷在线精品自拍偷,亚洲欧美中文日韩v在线观看不卡

Redis超時(shí)問題分析匯總

運(yùn)維 系統(tǒng)運(yùn)維 Redis
Redis在分布式應(yīng)用中占據(jù)著越來越重要的地位,短短的幾萬(wàn)行代碼,實(shí)現(xiàn)了一個(gè)高性能的數(shù)據(jù)存儲(chǔ)服務(wù)。最近dump中心的cm8集群出現(xiàn)過幾次redis超時(shí)的情況,但是查看redis機(jī)器的相關(guān)內(nèi)存都沒有發(fā)現(xiàn)內(nèi)存不夠,或者內(nèi)存發(fā)生交換的情況,查看redis源碼之后,發(fā)現(xiàn)在某些情況下redis會(huì)出現(xiàn)超時(shí)的狀況,相關(guān)細(xì)節(jié)如下。

Redis在分布式應(yīng)用中占據(jù)著越來越重要的地位,短短的幾萬(wàn)行代碼,實(shí)現(xiàn)了一個(gè)高性能的數(shù)據(jù)存儲(chǔ)服務(wù)。最近dump中心的cm8集群出現(xiàn)過幾次redis超時(shí)的情況,但是查看redis機(jī)器的相關(guān)內(nèi)存都沒有發(fā)現(xiàn)內(nèi)存不夠,或者內(nèi)存發(fā)生交換的情況,查看redis源碼之后,發(fā)現(xiàn)在某些情況下redis會(huì)出現(xiàn)超時(shí)的狀況,相關(guān)細(xì)節(jié)如下。

1. 網(wǎng)絡(luò)。Redis的處理與網(wǎng)絡(luò)息息相關(guān),如果網(wǎng)絡(luò)出現(xiàn)閃斷則容易發(fā)生redis超時(shí)的狀況。如果出現(xiàn)這種狀況首先應(yīng)查看redis機(jī)器網(wǎng)絡(luò)帶寬信息,判斷是否有閃斷情況發(fā)生。

2. 內(nèi)存。redis所有的數(shù)據(jù)都放在內(nèi)存里,當(dāng)物理內(nèi)存不夠時(shí),linux os會(huì)使用swap內(nèi)存,導(dǎo)致內(nèi)存交換發(fā)生,這時(shí)如果有redis調(diào)用命令就會(huì)產(chǎn)生redis超時(shí)。這里可以通過調(diào)整/proc/sys/vm/swappiness參數(shù),來設(shè)置物理內(nèi)存使用超過多少就會(huì)進(jìn)行swap。

  1. int rdbSaveBackground(char *filename) { 
  2.     pid_t childpid; 
  3.     long long start; 
  4.   
  5.     if (server.rdb_child_pid != -1) return REDIS_ERR; 
  6.     serverserver.dirty_before_bgsave = server.dirty; 
  7.     server.lastbgsave_try = time(NULL); 
  8.     start = ustime(); 
  9.     if ((childpid = fork()) == 0) { 
  10.         int retval; 
  11.         /* Child */ 
  12.         if (server.ipfd > 0) close(server.ipfd); 
  13.         if (server.sofd > 0) close(server.sofd); 
  14.         retval = rdbSave(filename); 
  15.         if (retval == REDIS_OK) { 
  16.             size_t private_dirty = zmalloc_get_private_dirty(); 
  17.             if (private_dirty) { 
  18.                 redisLog(REDIS_NOTICE, 
  19.                     "RDB: %zu MB of memory used by copy-on-write", 
  20.                     private_dirty/(1024*1024)); 
  21.             } 
  22.         } 
  23.         exitFromChild((retval == REDIS_OK) ? 0 : 1); 
  24.     } else { 
  25.         /* Parent */ 
  26.         server.stat_fork_time = ustime()-start; 
  27.         if (childpid == -1) { 
  28.             server.lastbgsave_status = REDIS_ERR
  29.             redisLog(REDIS_WARNING,"Can't save in background: fork: %s", 
  30.                 strerror(errno)); 
  31.             return REDIS_ERR; 
  32.         } 
  33.         redisLog(REDIS_NOTICE,"Background saving started by pid %d",childpid); 
  34.         server.rdb_save_time_start = time(NULL); 
  35.         server.rdb_child_pid = childpid
  36.         updateDictResizePolicy(); 
  37.         return REDIS_OK; 
  38.     } 
  39.     return REDIS_OK; /* unreached */ 

 

程序1

另外還有一些特殊情況也會(huì)導(dǎo)致swap發(fā)生。當(dāng)我們使用rdb做為redis集群持久化時(shí)可能會(huì)發(fā)生物理內(nèi)存不夠的情況(aof持久化只是保持支持不斷的追加redis集群變化操作,不太容易引起swap)。當(dāng)使用rdb持久化時(shí),如程序1所示主進(jìn)程會(huì)fork一個(gè)子進(jìn)程去dump redis中所有的數(shù)據(jù),主進(jìn)程依然為客戶端服務(wù)。此時(shí)主進(jìn)程和子進(jìn)程共享同一塊內(nèi)存區(qū)域, linux內(nèi)核采用寫時(shí)復(fù)制來保證數(shù)據(jù)的安全性。在這種模式下如果客戶端發(fā)來寫請(qǐng)求,內(nèi)核將該頁(yè)賦值到一個(gè)新的頁(yè)面上并標(biāo)記為寫,在將寫請(qǐng)求寫入該頁(yè)面。因此,在rdb持久化時(shí),如果有其他請(qǐng)求,那么redis會(huì)使用更多的內(nèi)存,更容易發(fā)生swap,因此在可以快速恢復(fù)的場(chǎng)景下盡量少使用rdb持久化可以將rdb dump的條件設(shè)的苛刻一點(diǎn),當(dāng)然也可以選擇aof,但是aof也有他自身的缺點(diǎn)。另外也可以使用2.6以后的主從結(jié)構(gòu),將讀寫分離,這樣不會(huì)出現(xiàn)server進(jìn)程上又讀又寫的情景發(fā)生 3. Redis單進(jìn)程處理命令。Redis支持udp和tcp兩種連接,redis客戶端向redis服務(wù)器發(fā)送包含redis命令的信息,redis服務(wù)器收到信息后解析命令后執(zhí)行相應(yīng)的操作,redis處理命令是串行的具體流程如下。首先服務(wù)端建立連接如程序2所示,在創(chuàng)建socket,bind,listen后返回文件描述符:

 

  1. server.ipfd = anetTcpServer(server.neterr,server.port,server.bindaddr); 

 

程序2

對(duì)于redis這種服務(wù)來說,它需要處理成千上萬(wàn)個(gè)連接(***達(dá)到655350),需要使用多路復(fù)用來處理多個(gè)連接。這里redis提供了epoll,select, kqueue來實(shí)現(xiàn),這里在默認(rèn)使用epoll(ae.c)。拿到listen函數(shù)返回的文件描述符fd后,redis將fd和其處理acceptTcpHandler函數(shù)加入到事件驅(qū)動(dòng)的鏈表中.實(shí)際上在加入事件隊(duì)列中,程序4事件驅(qū)動(dòng)程序?qū)⑻捉幼窒嚓P(guān)的fd文件描述符加入到epoll的監(jiān)聽事件中。

  1.  if (server.ipfd > 0 && aeCreateFileEvent(server.el,server.ipfd,AE_READABLE, 
  2.         acceptTcpHandler,NULL) == AE_ERR) redisPanic("Unrecoverable error creating server.ipfd file event."); 
  3.   
  4. int aeCreateFileEvent(aeEventLoop *eventLoop, int fd, int mask, 
  5.         aeFileProc *proc, void *clientData) 
  6.     if (fd >= eventLoop->setsize) { 
  7.         errno = ERANGE
  8.         return AE_ERR; 
  9.     } 
  10.     aeFileEvent *fe = &eventLoop->events[fd]; 
  11.   
  12.     if (aeApiAddEvent(eventLoop, fd, mask) == -1) 
  13.         return AE_ERR; 
  14.     fe->mask |= mask; 
  15.     if (mask & AE_READABLE) fe->rfileProc = proc
  16.     if (mask & AE_WRITABLE) fe->wfileProc = proc
  17.     fe->clientDataclientData = clientData; 
  18.     if (fd > eventLoop->maxfd) 
  19.         eventLoop->maxfd = fd; 
  20.     return AE_OK; 

 

程序3

 

  1. static int aeApiAddEvent(aeEventLoop *eventLoop, int fd, int mask) {  
  2.     aeApiState *state = eventLoop->apidata;  
  3.     struct epoll_event ee;  
  4.     /* If the fd was already monitored for some event, we need a MOD  
  5.      * operation. Otherwise we need an ADD operation. */  
  6.     int op = eventLoop->events[fd].mask == AE_NONE ?  
  7.             EPOLL_CTL_ADD : EPOLL_CTL_MOD;  
  8.     ee.events = 0;  
  9.     mask |= eventLoop->events[fd].mask; /* Merge old events */  
  10.     if (mask & AE_READABLE) ee.events |= EPOLLIN;  
  11.     if (mask & AE_WRITABLE) ee.events |= EPOLLOUT;  
  12.     ee.data.u64 = 0; /* avoid valgrind warning */  
  13.     ee.data.fd = fd;  
  14.     if (epoll_ctl(state->epfd,op,fd,&ee) == -1) return -1;  
  15.     return 0;  
  16. }  

 

程序4

在初始話完所有事件驅(qū)動(dòng)后,如程序5所示主進(jìn)程根據(jù)numevents = aeApiPoll(eventLoop, tvp)獲得io就緒的文件描述符和其對(duì)應(yīng)的處理程序,并對(duì)fd進(jìn)行處理。大致流程是accept()->createclient()->readQueryFromClient()。其中readQueryFromClient()讀取信息中的redis命令-> processInputBuffer()->call()***完成命令。

  1. void aeMain(aeEventLoop *eventLoop) { 
  2.     eventLoop->stop = 0
  3.     while (!eventLoop->stop) { 
  4.         if (eventLoop->beforesleep != NULL) 
  5.             eventLoop->beforesleep(eventLoop); 
  6.         aeProcessEvents(eventLoop, AE_ALL_EVENTS); 
  7.     } 
  8. int aeProcessEvents(aeEventLoop *eventLoop, int flags) 
  9. {------------------------------- 
  10.  numevents = aeApiPoll(eventLoop, tvp); 
  11.         for (j = 0; j < numevents; j++) {             aeFileEvent *fe = &eventLoop->events[eventLoop->fired[j].fd]; 
  12.             int mask = eventLoop->fired[j].mask; 
  13.             int fd = eventLoop->fired[j].fd; 
  14.             int rfired = 0
  15.   
  16.             /* note the fe->mask & mask & ... code: maybe an already processed 
  17.              * event removed an element that fired and we still didn't 
  18.              * processed, so we check if the event is still valid. */ 
  19.             if (fe->mask & mask & AE_READABLE) { 
  20.                 rfired = 1
  21.                 fe->rfileProc(eventLoop,fd,fe->clientData,mask); 
  22.             } 
  23.             if (fe->mask & mask & AE_WRITABLE) { 
  24.                 if (!rfired || fe->wfileProc != fe->rfileProc) 
  25.                     fe->wfileProc(eventLoop,fd,fe->clientData,mask); 
  26.             } 
  27.             processed++; 
  28.         } 

 

程序5

從上述代碼可以看出redis利用ae事件驅(qū)動(dòng)結(jié)合epoll多路復(fù)用實(shí)現(xiàn)了串行式的命令處理。所以一些慢命令例如sort,hgetall,union,mget都會(huì)使得單命令處理時(shí)間較長(zhǎng),容易引起后續(xù)命令time out.所以我們***需要從業(yè)務(wù)上盡量避免使用慢命令,如將hash格式改為kv自行解析,第二增加redis實(shí)例個(gè)數(shù),每個(gè)redis服務(wù)器調(diào)用盡量少的慢命令。

責(zé)任編輯:黃丹 來源: 搜索技術(shù)博客
相關(guān)推薦

2014-07-01 11:18:37

Android Stu問題匯總

2022-09-28 14:13:03

Linux工具

2010-09-15 09:20:40

2011-08-12 09:52:35

iPhone開發(fā)TableviewUITextField

2021-11-15 12:42:25

C# 定位gRPC

2010-11-25 11:15:11

MySQL查詢超時(shí)

2011-04-02 10:29:20

Linux工具

2009-07-22 08:59:01

Windows xp升Windows 7系統(tǒng)升級(jí)

2010-05-13 13:27:23

2013-08-21 14:57:42

objective-c問題

2009-09-22 09:22:03

.NET常見問題

2012-02-06 10:37:07

Java

2017-05-17 15:09:46

Linux分析性能工具

2010-10-14 09:15:20

MySQL查詢

2010-08-17 09:16:23

2020-10-18 12:00:27

前端開發(fā)架構(gòu)

2010-02-03 16:32:13

2020-12-28 11:08:18

MySQL數(shù)據(jù)庫(kù)服務(wù)器

2010-07-21 09:29:33

Perl常見問題

2010-01-05 09:36:40

ADO超時(shí)
點(diǎn)贊
收藏

51CTO技術(shù)棧公眾號(hào)