search
HomeDatabaseMysql TutorialMongoDB的选举过程

MongoDB的选举过程

Jun 07, 2016 pm 04:39 PM
mongodbPopular scienceprocess

科普文,MongoDB的选举过程. MongoDB的复制集具有自动容忍部分节点宕机的功能,在复制集出现问题时时,会触发选举相关的过程,完成主从节点自动切换. 每个复制集成员都会在后台运行与复制集所有节点的心跳线程,在两种情况下会触发状态检测过程: 复制集成员心跳检

科普文,MongoDB的选举过程.
MongoDB的复制集具有自动容忍部分节点宕机的功能,在复制集出现问题时时,会触发选举相关的过程,完成主从节点自动切换.
每个复制集成员都会在后台运行与复制集所有节点的心跳线程,在两种情况下会触发状态检测过程:

  • 复制集成员心跳检测结果发生变化,比如某个节点挂了或者新增节点.
  • 超过4s没有执行状态检测过程.

在状态检测过程大致包含以下步骤:

  • 检测自身是否处于选举过程,如果是,退出本次过程.
  • 维护一个主节点的备用列表,列表中所有节点都可能被选举为主节点,每个节点都会检测自身以及全局条件是否满足:
    • 是否看见复制集中是否有Majority在线.
    • 自身priority大于0.
    • 自身不为arbiter.
    • 自身opTime不能落后于最新节点10s以上.
    • 自身存储的集群程序按信息为最新.

如果所有条件满足,则将自身添加到主节点备用列表中,否则,将自身从列表中移除.

  • 检测以下条件,若都满足,将主节点将为从节点(如果要降级的主节点是自身,直接调用降级方法,如果不为自身,调用replSetStepDown命令将复制集主节点降级为从节点.):
    • 集群中主节点存在.
    • "主节点的备用列表"中存在比当前的主节点priority更高的节点.
    • "主节点的备用列表"中priority最高的节点,其opTime要比其他所有节点最新的opTime落后10s以内.
  • 检测自身是否为主,若为主,且自身无法看见复制集的Majority在线,将自身降级为从.
  • 如果看不见集群中有主节点存在,检测自身是否在"主节点的备用列表",若不在,打印log并退出此流程.
  • 若自身在"主节点的备用列表"中,开始判断自身可否向复制集中发送选举自身为主节点的通知,判断过程包含:
    • 自身是否可以看见复制集中的Majority在线.
    • 自身是否在"主节点的备用列表".
      若条件满足,则设置"自身已经在选举过程中"标识位为true,并进入"选举自身为主节点"方法.
  • 方法中会验证自身是否满足以下条件:
    • 此线程拿到了线程锁.
    • 此节点没有被配置slaveDelay选项或者配置的slaveDelay为0.
    • 此节点没有被配置为arbiter.
      若满足,则调用环境检测,若以下条件被触发,则不发送"选举我为主节点"投票:
    • 当前时间小于steppedDown的结束冻结时间(为执行steppedDown时的时间+冻结设定时间,内部调用为60s).
    • 自己的opTime不是所有节点最新的.
      • 若有节点opTime比自己新,直接退出此流程.
      • 如果其他最新的节点最多与自己一样新,每有一个这样的节点,随机sleep一段时间,之后继续判断.
    • 自己上线5分钟内且复制集中不是所有节点在线.
  • 如无其他问题,尝试获取自己进行投票时的票数,在此过程中,会判断自己在30s内是否进行过投票,如进行过,直接退出整个过程.
  • 经过以上种种复杂的检测,终于可以向复制集发送"选举我为主节点"的投票.
  • 发送之后,会接收来自所有节点的投票,若得票数小于等于一半,不将自己变为主节点,若超过一半,设置自己为主节点.
    投票结束后,设置"自身已经在选举过程中"标识位为false.
    可以看到,上面的判断逻辑有一些是重复判断,不过不影响最终结果,可能与判断逻辑较为复杂有关系,在每个决定之前都要验证所有条件是否满足,防止有条件被漏掉.
    在复制集中的节点收到其他节点发送的"选举我为主节点"投票信息时,会有以下的判断:
  • 若自身存储的复制集配置版本过低,不投票.
  • 若发起请求的节点存储的复制集配置版本过低,投反对票.
  • 如果自身所在的复制集没有发起投票的节点,投反对票.
  • 复制集中存在主节点,投反对票.
  • 可参与选举的节点中有priority高于请求为主的节点存在时,投反对票.
    如果所有条件通过,获取自身的投票数(同样会判断自身在30s内是否参加过投票,若参加过,不再投票),投出票数.
    需要说一下的是,一个反对会将最终票数减10000,即在绝大多数情况下,只要有节点反对,请求的节点就不能成为主节点.
    选举过程很复杂,实际使用中总结为几点:
  • 一般情况下需要5s左右进行选主.
  • 如果新选举出的主节点立马挂掉,至少需要30s时间重新选主.
Statement
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Explain the InnoDB Buffer Pool and its importance for performance.Explain the InnoDB Buffer Pool and its importance for performance.Apr 19, 2025 am 12:24 AM

InnoDBBufferPool reduces disk I/O by caching data and indexing pages, improving database performance. Its working principle includes: 1. Data reading: Read data from BufferPool; 2. Data writing: After modifying the data, write to BufferPool and refresh it to disk regularly; 3. Cache management: Use the LRU algorithm to manage cache pages; 4. Reading mechanism: Load adjacent data pages in advance. By sizing the BufferPool and using multiple instances, database performance can be optimized.

MySQL vs. Other Programming Languages: A ComparisonMySQL vs. Other Programming Languages: A ComparisonApr 19, 2025 am 12:22 AM

Compared with other programming languages, MySQL is mainly used to store and manage data, while other languages ​​such as Python, Java, and C are used for logical processing and application development. MySQL is known for its high performance, scalability and cross-platform support, suitable for data management needs, while other languages ​​have advantages in their respective fields such as data analytics, enterprise applications, and system programming.

Learning MySQL: A Step-by-Step Guide for New UsersLearning MySQL: A Step-by-Step Guide for New UsersApr 19, 2025 am 12:19 AM

MySQL is worth learning because it is a powerful open source database management system suitable for data storage, management and analysis. 1) MySQL is a relational database that uses SQL to operate data and is suitable for structured data management. 2) The SQL language is the key to interacting with MySQL and supports CRUD operations. 3) The working principle of MySQL includes client/server architecture, storage engine and query optimizer. 4) Basic usage includes creating databases and tables, and advanced usage involves joining tables using JOIN. 5) Common errors include syntax errors and permission issues, and debugging skills include checking syntax and using EXPLAIN commands. 6) Performance optimization involves the use of indexes, optimization of SQL statements and regular maintenance of databases.

MySQL: Essential Skills for Beginners to MasterMySQL: Essential Skills for Beginners to MasterApr 18, 2025 am 12:24 AM

MySQL is suitable for beginners to learn database skills. 1. Install MySQL server and client tools. 2. Understand basic SQL queries, such as SELECT. 3. Master data operations: create tables, insert, update, and delete data. 4. Learn advanced skills: subquery and window functions. 5. Debugging and optimization: Check syntax, use indexes, avoid SELECT*, and use LIMIT.

MySQL: Structured Data and Relational DatabasesMySQL: Structured Data and Relational DatabasesApr 18, 2025 am 12:22 AM

MySQL efficiently manages structured data through table structure and SQL query, and implements inter-table relationships through foreign keys. 1. Define the data format and type when creating a table. 2. Use foreign keys to establish relationships between tables. 3. Improve performance through indexing and query optimization. 4. Regularly backup and monitor databases to ensure data security and performance optimization.

MySQL: Key Features and Capabilities ExplainedMySQL: Key Features and Capabilities ExplainedApr 18, 2025 am 12:17 AM

MySQL is an open source relational database management system that is widely used in Web development. Its key features include: 1. Supports multiple storage engines, such as InnoDB and MyISAM, suitable for different scenarios; 2. Provides master-slave replication functions to facilitate load balancing and data backup; 3. Improve query efficiency through query optimization and index use.

The Purpose of SQL: Interacting with MySQL DatabasesThe Purpose of SQL: Interacting with MySQL DatabasesApr 18, 2025 am 12:12 AM

SQL is used to interact with MySQL database to realize data addition, deletion, modification, inspection and database design. 1) SQL performs data operations through SELECT, INSERT, UPDATE, DELETE statements; 2) Use CREATE, ALTER, DROP statements for database design and management; 3) Complex queries and data analysis are implemented through SQL to improve business decision-making efficiency.

MySQL for Beginners: Getting Started with Database ManagementMySQL for Beginners: Getting Started with Database ManagementApr 18, 2025 am 12:10 AM

The basic operations of MySQL include creating databases, tables, and using SQL to perform CRUD operations on data. 1. Create a database: CREATEDATABASEmy_first_db; 2. Create a table: CREATETABLEbooks(idINTAUTO_INCREMENTPRIMARYKEY, titleVARCHAR(100)NOTNULL, authorVARCHAR(100)NOTNULL, published_yearINT); 3. Insert data: INSERTINTObooks(title, author, published_year)VA

See all articles

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Tools

DVWA

DVWA

Damn Vulnerable Web App (DVWA) is a PHP/MySQL web application that is very vulnerable. Its main goals are to be an aid for security professionals to test their skills and tools in a legal environment, to help web developers better understand the process of securing web applications, and to help teachers/students teach/learn in a classroom environment Web application security. The goal of DVWA is to practice some of the most common web vulnerabilities through a simple and straightforward interface, with varying degrees of difficulty. Please note that this software

VSCode Windows 64-bit Download

VSCode Windows 64-bit Download

A free and powerful IDE editor launched by Microsoft

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

SAP NetWeaver Server Adapter for Eclipse

SAP NetWeaver Server Adapter for Eclipse

Integrate Eclipse with SAP NetWeaver application server.

Dreamweaver Mac version

Dreamweaver Mac version

Visual web development tools