Redis source code analysis (17) --- multi transaction operations

Source: Internet
Author: User

As a non-relational database, redis has the same transaction operations with RDBMS, which surprised me. A dedicated file in redis is used to perform transaction-related operations. We can also learn how to implement transaction operations in redis code. First, some apis under mulic. C are displayed.

/* ==================================== Multi/exec ==== =========================*/void initclientmultistate (redisclient * C) /* initialize the client operation */void freeclientmultistate (redisclient * C)/* release all client resources related to multi/exec */void queuemulticommand (redisclient * C) /* Add a new command to the Multi command queue of the client */void discardtransaction (redisclient * C)/* cancel transaction operation */void flagtransaction (redisclient * C) /* indicates that a transaction is in the dirty_exec state and the execution fails. This method is called when the command is inserted */void multicommand (redisclient * C) /* Add the multi command */void discardcommand (redisclient * C)/* undo command */void execcommandpropagatemulti (redisclient * C) /* Send the multi command to all slave clients and aof files */void Execcommand (redisclient * C)/* run the command on a single client */void watchforkey (redisclient * C, robj * Key)/* Add a key listener to the client */void unwatchallkeys (redisclient * C)/* remove all keys from the client */void touchwatchedkey (redisdb * dB, robj * key) /* indicates that the key is being monitored, and the next operation will fail. */void touchwatchedkeysonflush (INT dbid)/* indicates the database where the key is located, touch all the watched-keys in this dB */void watchcommand (redisclient * C)/* Watch key command method, use parameters in the client to pass the value */void unwatchcommand (redisclient * C)/* cancel the command Method for listening to keys */
There are not many methods, but there is a frequently used word "key" in it ". This key does play a key role here. The Muli code mainly contains commands, execution commands, and undo commands, such as the following undo transaction operations.

/* Cancel the transaction */void discardtransaction (redisclient * c) {freeclientmultistate (c); initclientmultistate (c); C-> flags & = ~ (Redis_multi | redis_dirty_cas | redis_dirty_exec); // The client cancels listening for all keys unwatchallkeys (c );}
There is an unwatchallkeys () method. The following are the key principles of transaction operations:

/* In transaction processing, there are two mapping types: Key --> client lists, which indicates that all the clients in the list are listening for this key. When the value of this key changes, it can mark these clients as dirty States and need to be updated. At the same time, a key of list is maintained inside the client, indicating all keys monitored by a client. When the client has a free operation, update the list of clients maintained in the key */

/* Indicates that the key is being monitored, and the next operation will fail */
That is to say, if the client is listening for the key, its next command will fail to be executed, achieving the synchronization effect,

/* "Touch" a key, so that if this key is being watched by some client the * Next exec will fail. * // * indicates that the key is being monitored, and the next operation will fail */void touchwatchedkey (redisdb * dB, robj * Key) {list * clients; listiter Li; listnode * ln; If (dictsize (db-> watched_keys) = 0) return; clients = dictfetchvalue (db-> watched_keys, key); If (! Clients) return;/* mark all the clients watching this key as redis_dirty_cas * // * check if we are already watching for this key */listrewind (clients, & Li ); while (Ln = listnext (& Li) {redisclient * c = listnodevalue (LN); // traverses the client owned by the key, mark flag as dirty_cas status C-> flags | = redis_dirty_cas ;}}
When the client tries to use the touch method to listen to the key, the flag status of the client is changed to dirty_cas. I cannot help but guess that the synchronous method is to use the CAS algorithm, if many clients use this algorithm, the CPU usage is indeed high. In general, the key maintains a client list, and a client also has a list of all its watch keys. The structure of the key is very simple:

/* Defines the watchedkey struct */typedef struct watchedkey {robj * key; redisdb * dB;} watchedkey;
The key contains the database to which it belongs. Therefore, if you just undo the transaction, you need to remove all the keys listened by the client.

Redis source code analysis (17) --- multi transaction operations

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.