Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

Redis-trib.rb parsing

2025-02-27 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Database >

Share

Shulou(Shulou.com)06/01 Report--

Analysis of Redis-trib.rb-create source code 1. Introduction

  Redis-trib.rb is the mainstream Redis management tool, which encapsulates a series of Redis commands and completes Create Cluster,Reshard Cluster,Add Slave,Remove Node and other functions through the ruby client. Let's take a look at how Create Cluster sets up a cluster cluster.

2. Process overview diagram

Third, node availability testing 1. Node connectivity 2. Whether the cluster mode (if cluster_enabled==1?) 3. Is it already a member of another cluster (if cluster_known_nodes==1?) 4. Check that there are at least three available nodes 5. Get node information:

Get statistics through the cluster nodes command

Name,addr,flags,master_id, ping_sent,ping_recv,config_epoch,link_status,slot information

Such as:

Fourth, master-slave pre-allocation and node pre-allocation instructions: Redis-trib.rb create can allocate different nodes of the master library to different machines as much as possible and separate the physical machines of the master-slave library. The implementation of this function calls the alloc_slots function, and we intercept part of it for analysis.

Line 559-line 562: classifies nodes according to ip, and the key of dict is the node information corresponding to ip,value for this ip.

Line 566-577: loop ip, extract one node of ip at a time and put it in interleaved. This allows for the separation of the same nodes as much as possible

Line 578: intercepting the first few nodes in interleaved as masters_count,master_count is determined by the number of node parameters and the number of replacas.

Line 585-594: the slot range corresponding to each master node is calculated according to the number of nodes.

Fifth, configure clusters

Redis-trib configures the cluster through the client of ruby and redis

Slot allocation (addslots) to the master node. Cluster_state become ok and cluster_slots_assigned become not null sets different config_epoch (set-config-epoch) nodes to join the cluster (CLUSTER MEET) for each node. Master-slave configuration (replicate) waits for all nodes to have the same cluster configuration information. 6. Cluster status detection checks whether there is open slot.

Openslot is defined as slot information as migrating or importing, marked as-> -.

two。 Check whether slot fully covers Redis-trib.rb-reshard source code parsing I. Overview

After the new master node is added to the cluster, it does not have any slot data and needs to migrate some slot from other nodes to the new node through reshard.

II. Flow chart

3. Explanation of key steps: compute_reshard_table method is mainly used to intelligently calculate how much slot each target node needs to migrate.

775 rows sort the source nodes by the number of slot, with most slot ranking first

Calculate the total number of slot of the source node from line 776to line 778

Line 779-785 you can see that the slot is migrated as a percentage of the total slot, and the more nodes are migrated, the more slot. You can also see that the node with the largest number of slot nodes is the largest integer of slot

Line 786line 791inserts the dispatch information of slot to the node into the moved variable

Description: slot importing status and migrating status need to be set before the migrate node

  redis supports dynamic migration. When the slot is set to the status migrating state, when the redis client queries the key corresponding to the node slot, if the node finds that the key does not exist in its own slot, it will send an asking request to the client to tell the migrated target node to check and do not change the slot distribution information maintained by the client. When the client requests the target node, the slot is redirected by refuse. When the request contains asking, he accepts the request and reads the key. When the migration is complete, he will set the setslot node, which will clear the slot migration state and set the new target node for the slot.

Redis-trib.rb-- del-node I. Overview

Del-node is used to weed out nodes in the cluster.

II. Flow chart

Third, key step description: get_master_with_least_replicas method if delete node has a slave library, he will specify his slave library to other master nodes.

Line 337 selects the master in the cluster

Line 338-line 341 is sorted according to the number of slave of master, and the master of at least slave is selected as the new master of the target slave library.

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Database

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report