关键词搜索

源码搜索 ×
×

漫话Redis源码之七十六

发布2022-02-13浏览558次

详情内容

处理非阻塞的client, 整个实现还是比较简单的:

  1. /* This function is called in the beforeSleep() function of the event loop
  2. * in order to process the pending input buffer of clients that were
  3. * unblocked after a blocking operation. */
  4. void processUnblockedClients(void) {
  5. listNode *ln;
  6. client *c;
  7. while (listLength(server.unblocked_clients)) {
  8. ln = listFirst(server.unblocked_clients);
  9. serverAssert(ln != NULL);
  10. c = ln->value;
  11. listDelNode(server.unblocked_clients,ln);
  12. c->flags &= ~CLIENT_UNBLOCKED;
  13. /* Process remaining data in the input buffer, unless the client
  14. * is blocked again. Actually processInputBuffer() checks that the
  15. * client is not blocked before to proceed, but things may change and
  16. * the code is conceptually more correct this way. */
  17. if (!(c->flags & CLIENT_BLOCKED)) {
  18. /* If we have a queued command, execute it now. */
  19. if (processPendingCommandsAndResetClient(c) == C_ERR) {
  20. continue;
  21. }
  22. /* Then process client if it has more data in it's buffer. */
  23. if (c->querybuf && sdslen(c->querybuf) > 0) {
  24. processInputBuffer(c);
  25. }
  26. }
  27. }
  28. }
  29. /* This function will schedule the client for reprocessing at a safe time.
  30. *
  31. * This is useful when a client was blocked for some reason (blocking operation,
  32. * CLIENT PAUSE, or whatever), because it may end with some accumulated query
  33. * buffer that needs to be processed ASAP:
  34. *
  35. * 1. When a client is blocked, its readable handler is still active.
  36. * 2. However in this case it only gets data into the query buffer, but the
  37. * query is not parsed or executed once there is enough to proceed as
  38. * usually (because the client is blocked... so we can't execute commands).
  39. * 3. When the client is unblocked, without this function, the client would
  40. * have to write some query in order for the readable handler to finally
  41. * call processQueryBuffer*() on it.
  42. * 4. With this function instead we can put the client in a queue that will
  43. * process it for queries ready to be executed at a safe time.
  44. */
  45. void queueClientForReprocessing(client *c) {
  46. /* The client may already be into the unblocked list because of a previous
  47. * blocking operation, don't add back it into the list multiple times. */
  48. if (!(c->flags & CLIENT_UNBLOCKED)) {
  49. c->flags |= CLIENT_UNBLOCKED;
  50. listAddNodeTail(server.unblocked_clients,c);
  51. }
  52. }
  53. /* Unblock a client calling the right function depending on the kind
  54. * of operation the client is blocking for. */
  55. void unblockClient(client *c) {
  56. if (c->btype == BLOCKED_LIST ||
  57. c->btype == BLOCKED_ZSET ||
  58. c->btype == BLOCKED_STREAM) {
  59. unblockClientWaitingData(c);
  60. } else if (c->btype == BLOCKED_WAIT) {
  61. unblockClientWaitingReplicas(c);
  62. } else if (c->btype == BLOCKED_MODULE) {
  63. if (moduleClientIsBlockedOnKeys(c)) unblockClientWaitingData(c);
  64. unblockClientFromModule(c);
  65. } else if (c->btype == BLOCKED_PAUSE) {
  66. listDelNode(server.paused_clients,c->paused_list_node);
  67. c->paused_list_node = NULL;
  68. } else {
  69. serverPanic("Unknown btype in unblockClient().");
  70. }
  71. /* Reset the client for a new query since, for blocking commands
  72. * we do not do it immediately after the command returns (when the
  73. * client got blocked) in order to be still able to access the argument
  74. * vector from module callbacks and updateStatsOnUnblock. */
  75. if (c->btype != BLOCKED_PAUSE) {
  76. freeClientOriginalArgv(c);
  77. resetClient(c);
  78. }
  79. /* Clear the flags, and put the client in the unblocked list so that
  80. * we'll process new commands in its query buffer ASAP. */
  81. server.blocked_clients--;
  82. server.blocked_clients_by_type[c->btype]--;
  83. c->flags &= ~CLIENT_BLOCKED;
  84. c->btype = BLOCKED_NONE;
  85. removeClientFromTimeoutTable(c);
  86. queueClientForReprocessing(c);
  87. }
  88. /* This function gets called when a blocked client timed out in order to
  89. * send it a reply of some kind. After this function is called,
  90. * unblockClient() will be called with the same client as argument. */
  91. void replyToBlockedClientTimedOut(client *c) {
  92. if (c->btype == BLOCKED_LIST ||
  93. c->btype == BLOCKED_ZSET ||
  94. c->btype == BLOCKED_STREAM) {
  95. addReplyNullArray(c);
  96. } else if (c->btype == BLOCKED_WAIT) {
  97. addReplyLongLong(c,replicationCountAcksByOffset(c->bpop.reploffset));
  98. } else if (c->btype == BLOCKED_MODULE) {
  99. moduleBlockedClientTimedOut(c);
  100. } else {
  101. serverPanic("Unknown btype in replyToBlockedClientTimedOut().");
  102. }
  103. }
  104. /* Mass-unblock clients because something changed in the instance that makes
  105. * blocking no longer safe. For example clients blocked in list operations
  106. * in an instance which turns from master to slave is unsafe, so this function
  107. * is called when a master turns into a slave.
  108. *
  109. * The semantics is to send an -UNBLOCKED error to the client, disconnecting
  110. * it at the same time. */
  111. void disconnectAllBlockedClients(void) {
  112. listNode *ln;
  113. listIter li;
  114. listRewind(server.clients,&li);
  115. while((ln = listNext(&li))) {
  116. client *c = listNodeValue(ln);
  117. if (c->flags & CLIENT_BLOCKED) {
  118. /* PAUSED clients are an exception, when they'll be unblocked, the
  119. * command processing will start from scratch, and the command will
  120. * be either executed or rejected. (unlike LIST blocked clients for
  121. * which the command is already in progress in a way. */
  122. if (c->btype == BLOCKED_PAUSE)
  123. continue;
  124. addReplyError(c,
  125. "-UNBLOCKED force unblock from blocking operation, "
  126. "instance state changed (master -> replica?)");
  127. unblockClient(c);
  128. c->flags |= CLIENT_CLOSE_AFTER_REPLY;
  129. }
  130. }
  131. }
  132. /* Helper function for handleClientsBlockedOnKeys(). This function is called
  133. * when there may be clients blocked on a list key, and there may be new
  134. * data to fetch (the key is ready). */
  135. void serveClientsBlockedOnListKey(robj *o, readyList *rl) {
  136. /* We serve clients in the same order they blocked for
  137. * this key, from the first blocked to the last. */
  138. dictEntry *de = dictFind(rl->db->blocking_keys,rl->key);
  139. if (de) {
  140. list *clients = dictGetVal(de);
  141. int numclients = listLength(clients);
  142. while(numclients--) {
  143. listNode *clientnode = listFirst(clients);
  144. client *receiver = clientnode->value;
  145. if (receiver->btype != BLOCKED_LIST) {
  146. /* Put at the tail, so that at the next call
  147. * we'll not run into it again. */
  148. listRotateHeadToTail(clients);
  149. continue;
  150. }
  151. robj *dstkey = receiver->bpop.target;
  152. int wherefrom = receiver->bpop.listpos.wherefrom;
  153. int whereto = receiver->bpop.listpos.whereto;
  154. robj *value = listTypePop(o, wherefrom);
  155. if (value) {
  156. /* Protect receiver->bpop.target, that will be
  157. * freed by the next unblockClient()
  158. * call. */
  159. if (dstkey) incrRefCount(dstkey);
  160. monotime replyTimer;
  161. elapsedStart(&replyTimer);
  162. if (serveClientBlockedOnList(receiver,
  163. rl->key,dstkey,rl->db,value,
  164. wherefrom, whereto) == C_ERR)
  165. {
  166. /* If we failed serving the client we need
  167. * to also undo the POP operation. */
  168. listTypePush(o,value,wherefrom);
  169. }
  170. updateStatsOnUnblock(receiver, 0, elapsedUs(replyTimer));
  171. unblockClient(receiver);
  172. if (dstkey) decrRefCount(dstkey);
  173. decrRefCount(value);
  174. } else {
  175. break;
  176. }
  177. }
  178. }
  179. if (listTypeLength(o) == 0) {
  180. dbDelete(rl->db,rl->key);
  181. notifyKeyspaceEvent(NOTIFY_GENERIC,"del",rl->key,rl->db->id);
  182. }
  183. /* We don't call signalModifiedKey() as it was already called
  184. * when an element was pushed on the list. */
  185. }
  186. /* Helper function for handleClientsBlockedOnKeys(). This function is called
  187. * when there may be clients blocked on a sorted set key, and there may be new
  188. * data to fetch (the key is ready). */
  189. void serveClientsBlockedOnSortedSetKey(robj *o, readyList *rl) {
  190. /* We serve clients in the same order they blocked for
  191. * this key, from the first blocked to the last. */
  192. dictEntry *de = dictFind(rl->db->blocking_keys,rl->key);
  193. if (de) {
  194. list *clients = dictGetVal(de);
  195. int numclients = listLength(clients);
  196. unsigned long zcard = zsetLength(o);
  197. while(numclients-- && zcard) {
  198. listNode *clientnode = listFirst(clients);
  199. client *receiver = clientnode->value;
  200. if (receiver->btype != BLOCKED_ZSET) {
  201. /* Put at the tail, so that at the next call
  202. * we'll not run into it again. */
  203. listRotateHeadToTail(clients);
  204. continue;
  205. }
  206. int where = (receiver->lastcmd &&
  207. receiver->lastcmd->proc == bzpopminCommand)
  208. ? ZSET_MIN : ZSET_MAX;
  209. monotime replyTimer;
  210. elapsedStart(&replyTimer);
  211. genericZpopCommand(receiver,&rl->key,1,where,1,NULL);
  212. updateStatsOnUnblock(receiver, 0, elapsedUs(replyTimer));
  213. unblockClient(receiver);
  214. zcard--;
  215. /* Replicate the command. */
  216. robj *argv[2];
  217. struct redisCommand *cmd = where == ZSET_MIN ?
  218. server.zpopminCommand :
  219. server.zpopmaxCommand;
  220. argv[0] = createStringObject(cmd->name,strlen(cmd->name));
  221. argv[1] = rl->key;
  222. incrRefCount(rl->key);
  223. propagate(cmd,receiver->db->id,
  224. argv,2,PROPAGATE_AOF|PROPAGATE_REPL);
  225. decrRefCount(argv[0]);
  226. decrRefCount(argv[1]);
  227. }
  228. }
  229. }
  230. /* Helper function for handleClientsBlockedOnKeys(). This function is called
  231. * when there may be clients blocked on a stream key, and there may be new
  232. * data to fetch (the key is ready). */
  233. void serveClientsBlockedOnStreamKey(robj *o, readyList *rl) {
  234. dictEntry *de = dictFind(rl->db->blocking_keys,rl->key);
  235. stream *s = o->ptr;
  236. /* We need to provide the new data arrived on the stream
  237. * to all the clients that are waiting for an offset smaller
  238. * than the current top item. */
  239. if (de) {
  240. list *clients = dictGetVal(de);
  241. listNode *ln;
  242. listIter li;
  243. listRewind(clients,&li);
  244. while((ln = listNext(&li))) {
  245. client *receiver = listNodeValue(ln);
  246. if (receiver->btype != BLOCKED_STREAM) continue;
  247. bkinfo *bki = dictFetchValue(receiver->bpop.keys,rl->key);
  248. streamID *gt = &bki->stream_id;
  249. /* If we blocked in the context of a consumer
  250. * group, we need to resolve the group and update the
  251. * last ID the client is blocked for: this is needed
  252. * because serving other clients in the same consumer
  253. * group will alter the "last ID" of the consumer
  254. * group, and clients blocked in a consumer group are
  255. * always blocked for the ">" ID: we need to deliver
  256. * only new messages and avoid unblocking the client
  257. * otherwise. */
  258. streamCG *group = NULL;
  259. if (receiver->bpop.xread_group) {
  260. group = streamLookupCG(s,
  261. receiver->bpop.xread_group->ptr);
  262. /* If the group was not found, send an error
  263. * to the consumer. */
  264. if (!group) {
  265. addReplyError(receiver,
  266. "-NOGROUP the consumer group this client "
  267. "was blocked on no longer exists");
  268. unblockClient(receiver);
  269. continue;
  270. } else {
  271. *gt = group->last_id;
  272. }
  273. }
  274. if (streamCompareID(&s->last_id, gt) > 0) {
  275. streamID start = *gt;
  276. streamIncrID(&start);
  277. /* Lookup the consumer for the group, if any. */
  278. streamConsumer *consumer = NULL;
  279. int noack = 0;
  280. if (group) {
  281. int created = 0;
  282. consumer =
  283. streamLookupConsumer(group,
  284. receiver->bpop.xread_consumer->ptr,
  285. SLC_NONE,
  286. &created);
  287. noack = receiver->bpop.xread_group_noack;
  288. if (created && noack) {
  289. streamPropagateConsumerCreation(receiver,rl->key,
  290. receiver->bpop.xread_group,
  291. consumer->name);
  292. }
  293. }
  294. monotime replyTimer;
  295. elapsedStart(&replyTimer);
  296. /* Emit the two elements sub-array consisting of
  297. * the name of the stream and the data we
  298. * extracted from it. Wrapped in a single-item
  299. * array, since we have just one key. */
  300. if (receiver->resp == 2) {
  301. addReplyArrayLen(receiver,1);
  302. addReplyArrayLen(receiver,2);
  303. } else {
  304. addReplyMapLen(receiver,1);
  305. }
  306. addReplyBulk(receiver,rl->key);
  307. streamPropInfo pi = {
  308. rl->key,
  309. receiver->bpop.xread_group
  310. };
  311. streamReplyWithRange(receiver,s,&start,NULL,
  312. receiver->bpop.xread_count,
  313. 0, group, consumer, noack, &pi);
  314. updateStatsOnUnblock(receiver, 0, elapsedUs(replyTimer));
  315. /* Note that after we unblock the client, 'gt'
  316. * and other receiver->bpop stuff are no longer
  317. * valid, so we must do the setup above before
  318. * this call. */
  319. unblockClient(receiver);
  320. }
  321. }
  322. }
  323. }
  324. /* Helper function for handleClientsBlockedOnKeys(). This function is called
  325. * in order to check if we can serve clients blocked by modules using
  326. * RM_BlockClientOnKeys(), when the corresponding key was signaled as ready:
  327. * our goal here is to call the RedisModuleBlockedClient reply() callback to
  328. * see if the key is really able to serve the client, and in that case,
  329. * unblock it. */
  330. void serveClientsBlockedOnKeyByModule(readyList *rl) {
  331. dictEntry *de;
  332. /* Optimization: If no clients are in type BLOCKED_MODULE,
  333. * we can skip this loop. */
  334. if (!server.blocked_clients_by_type[BLOCKED_MODULE]) return;
  335. /* We serve clients in the same order they blocked for
  336. * this key, from the first blocked to the last. */
  337. de = dictFind(rl->db->blocking_keys,rl->key);
  338. if (de) {
  339. list *clients = dictGetVal(de);
  340. int numclients = listLength(clients);
  341. while(numclients--) {
  342. listNode *clientnode = listFirst(clients);
  343. client *receiver = clientnode->value;
  344. /* Put at the tail, so that at the next call
  345. * we'll not run into it again: clients here may not be
  346. * ready to be served, so they'll remain in the list
  347. * sometimes. We want also be able to skip clients that are
  348. * not blocked for the MODULE type safely. */
  349. listRotateHeadToTail(clients);
  350. if (receiver->btype != BLOCKED_MODULE) continue;
  351. /* Note that if *this* client cannot be served by this key,
  352. * it does not mean that another client that is next into the
  353. * list cannot be served as well: they may be blocked by
  354. * different modules with different triggers to consider if a key
  355. * is ready or not. This means we can't exit the loop but need
  356. * to continue after the first failure. */
  357. monotime replyTimer;
  358. elapsedStart(&replyTimer);
  359. if (!moduleTryServeClientBlockedOnKey(receiver, rl->key)) continue;
  360. updateStatsOnUnblock(receiver, 0, elapsedUs(replyTimer));
  361. moduleUnblockClient(receiver);
  362. }
  363. }
  364. }
  365. /* This function should be called by Redis every time a single command,
  366. * a MULTI/EXEC block, or a Lua script, terminated its execution after
  367. * being called by a client. It handles serving clients blocked in
  368. * lists, streams, and sorted sets, via a blocking commands.
  369. *
  370. * All the keys with at least one client blocked that received at least
  371. * one new element via some write operation are accumulated into
  372. * the server.ready_keys list. This function will run the list and will
  373. * serve clients accordingly. Note that the function will iterate again and
  374. * again as a result of serving BLMOVE we can have new blocking clients
  375. * to serve because of the PUSH side of BLMOVE.
  376. *
  377. * This function is normally "fair", that is, it will server clients
  378. * using a FIFO behavior. However this fairness is violated in certain
  379. * edge cases, that is, when we have clients blocked at the same time
  380. * in a sorted set and in a list, for the same key (a very odd thing to
  381. * do client side, indeed!). Because mismatching clients (blocking for
  382. * a different type compared to the current key type) are moved in the
  383. * other side of the linked list. However as long as the key starts to
  384. * be used only for a single type, like virtually any Redis application will
  385. * do, the function is already fair. */
  386. void handleClientsBlockedOnKeys(void) {
  387. while(listLength(server.ready_keys) != 0) {
  388. list *l;
  389. /* Point server.ready_keys to a fresh list and save the current one
  390. * locally. This way as we run the old list we are free to call
  391. * signalKeyAsReady() that may push new elements in server.ready_keys
  392. * when handling clients blocked into BLMOVE. */
  393. l = server.ready_keys;
  394. server.ready_keys = listCreate();
  395. while(listLength(l) != 0) {
  396. listNode *ln = listFirst(l);
  397. readyList *rl = ln->value;
  398. /* First of all remove this key from db->ready_keys so that
  399. * we can safely call signalKeyAsReady() against this key. */
  400. dictDelete(rl->db->ready_keys,rl->key);
  401. /* Even if we are not inside call(), increment the call depth
  402. * in order to make sure that keys are expired against a fixed
  403. * reference time, and not against the wallclock time. This
  404. * way we can lookup an object multiple times (BLMOVE does
  405. * that) without the risk of it being freed in the second
  406. * lookup, invalidating the first one.
  407. * See https://github.com/redis/redis/pull/6554. */
  408. server.fixed_time_expire++;
  409. updateCachedTime(0);
  410. /* Serve clients blocked on the key. */
  411. robj *o = lookupKeyWrite(rl->db,rl->key);
  412. if (o != NULL) {
  413. if (o->type == OBJ_LIST)
  414. serveClientsBlockedOnListKey(o,rl);
  415. else if (o->type == OBJ_ZSET)
  416. serveClientsBlockedOnSortedSetKey(o,rl);
  417. else if (o->type == OBJ_STREAM)
  418. serveClientsBlockedOnStreamKey(o,rl);
  419. /* We want to serve clients blocked on module keys
  420. * regardless of the object type: we don't know what the
  421. * module is trying to accomplish right now. */
  422. serveClientsBlockedOnKeyByModule(rl);
  423. }
  424. server.fixed_time_expire--;
  425. /* Free this item. */
  426. decrRefCount(rl->key);
  427. zfree(rl);
  428. listDelNode(l,ln);
  429. }
  430. listRelease(l); /* We have the new list on place at this point. */
  431. }
  432. }
  433. /* This is how the current blocking lists/sorted sets/streams work, we use
  434. * BLPOP as example, but the concept is the same for other list ops, sorted
  435. * sets and XREAD.
  436. * - If the user calls BLPOP and the key exists and contains a non empty list
  437. * then LPOP is called instead. So BLPOP is semantically the same as LPOP
  438. * if blocking is not required.
  439. * - If instead BLPOP is called and the key does not exists or the list is
  440. * empty we need to block. In order to do so we remove the notification for
  441. * new data to read in the client socket (so that we'll not serve new
  442. * requests if the blocking request is not served). Also we put the client
  443. * in a dictionary (db->blocking_keys) mapping keys to a list of clients
  444. * blocking for this keys.
  445. * - If a PUSH operation against a key with blocked clients waiting is
  446. * performed, we mark this key as "ready", and after the current command,
  447. * MULTI/EXEC block, or script, is executed, we serve all the clients waiting
  448. * for this list, from the one that blocked first, to the last, accordingly
  449. * to the number of elements we have in the ready list.
  450. */
  451. /* Set a client in blocking mode for the specified key (list, zset or stream),
  452. * with the specified timeout. The 'type' argument is BLOCKED_LIST,
  453. * BLOCKED_ZSET or BLOCKED_STREAM depending on the kind of operation we are
  454. * waiting for an empty key in order to awake the client. The client is blocked
  455. * for all the 'numkeys' keys as in the 'keys' argument. When we block for
  456. * stream keys, we also provide an array of streamID structures: clients will
  457. * be unblocked only when items with an ID greater or equal to the specified
  458. * one is appended to the stream. */
  459. void blockForKeys(client *c, int btype, robj **keys, int numkeys, mstime_t timeout, robj *target, struct listPos *listpos, streamID *ids) {
  460. dictEntry *de;
  461. list *l;
  462. int j;
  463. c->bpop.timeout = timeout;
  464. c->bpop.target = target;
  465. if (listpos != NULL) c->bpop.listpos = *listpos;
  466. if (target != NULL) incrRefCount(target);
  467. for (j = 0; j < numkeys; j++) {
  468. /* Allocate our bkinfo structure, associated to each key the client
  469. * is blocked for. */
  470. bkinfo *bki = zmalloc(sizeof(*bki));
  471. if (btype == BLOCKED_STREAM)
  472. bki->stream_id = ids[j];
  473. /* If the key already exists in the dictionary ignore it. */
  474. if (dictAdd(c->bpop.keys,keys[j],bki) != DICT_OK) {
  475. zfree(bki);
  476. continue;
  477. }
  478. incrRefCount(keys[j]);
  479. /* And in the other "side", to map keys -> clients */
  480. de = dictFind(c->db->blocking_keys,keys[j]);
  481. if (de == NULL) {
  482. int retval;
  483. /* For every key we take a list of clients blocked for it */
  484. l = listCreate();
  485. retval = dictAdd(c->db->blocking_keys,keys[j],l);
  486. incrRefCount(keys[j]);
  487. serverAssertWithInfo(c,keys[j],retval == DICT_OK);
  488. } else {
  489. l = dictGetVal(de);
  490. }
  491. listAddNodeTail(l,c);
  492. bki->listnode = listLast(l);
  493. }
  494. blockClient(c,btype);
  495. }
  496. /* Unblock a client that's waiting in a blocking operation such as BLPOP.
  497. * You should never call this function directly, but unblockClient() instead. */
  498. void unblockClientWaitingData(client *c) {
  499. dictEntry *de;
  500. dictIterator *di;
  501. list *l;
  502. serverAssertWithInfo(c,NULL,dictSize(c->bpop.keys) != 0);
  503. di = dictGetIterator(c->bpop.keys);
  504. /* The client may wait for multiple keys, so unblock it for every key. */
  505. while((de = dictNext(di)) != NULL) {
  506. robj *key = dictGetKey(de);
  507. bkinfo *bki = dictGetVal(de);
  508. /* Remove this client from the list of clients waiting for this key. */
  509. l = dictFetchValue(c->db->blocking_keys,key);
  510. serverAssertWithInfo(c,key,l != NULL);
  511. listDelNode(l,bki->listnode);
  512. /* If the list is empty we need to remove it to avoid wasting memory */
  513. if (listLength(l) == 0)
  514. dictDelete(c->db->blocking_keys,key);
  515. }
  516. dictReleaseIterator(di);
  517. /* Cleanup the client structure */
  518. dictEmpty(c->bpop.keys,NULL);
  519. if (c->bpop.target) {
  520. decrRefCount(c->bpop.target);
  521. c->bpop.target = NULL;
  522. }
  523. if (c->bpop.xread_group) {
  524. decrRefCount(c->bpop.xread_group);
  525. decrRefCount(c->bpop.xread_consumer);
  526. c->bpop.xread_group = NULL;
  527. c->bpop.xread_consumer = NULL;
  528. }
  529. }

相关技术文章

点击QQ咨询
开通会员
返回顶部
×
微信扫码支付
微信扫码支付
确定支付下载
请使用微信描二维码支付
×

提示信息

×

选择支付方式

  • 微信支付
  • 支付宝付款
确定支付下载