[go: up one dir, main page]

CN111538763B - Method for determining master node in cluster, electronic equipment and storage medium - Google Patents

Method for determining master node in cluster, electronic equipment and storage medium Download PDF

Info

Publication number
CN111538763B
CN111538763B CN202010335432.3A CN202010335432A CN111538763B CN 111538763 B CN111538763 B CN 111538763B CN 202010335432 A CN202010335432 A CN 202010335432A CN 111538763 B CN111538763 B CN 111538763B
Authority
CN
China
Prior art keywords
node
master
information
cluster
time point
Prior art date
Legal status (The legal status is an assumption and is not a legal conclusion. Google has not performed a legal analysis and makes no representation as to the accuracy of the status listed.)
Active
Application number
CN202010335432.3A
Other languages
Chinese (zh)
Other versions
CN111538763A (en
Inventor
陈利乐
彭显根
Current Assignee (The listed assignees may be inaccurate. Google has not performed a legal analysis and makes no representation or warranty as to the accuracy of the list.)
China Mobile Communications Group Co Ltd
MIGU Culture Technology Co Ltd
Original Assignee
China Mobile Communications Group Co Ltd
MIGU Culture Technology Co Ltd
Priority date (The priority date is an assumption and is not a legal conclusion. Google has not performed a legal analysis and makes no representation as to the accuracy of the date listed.)
Filing date
Publication date
Application filed by China Mobile Communications Group Co Ltd, MIGU Culture Technology Co Ltd filed Critical China Mobile Communications Group Co Ltd
Priority to CN202010335432.3A priority Critical patent/CN111538763B/en
Publication of CN111538763A publication Critical patent/CN111538763A/en
Application granted granted Critical
Publication of CN111538763B publication Critical patent/CN111538763B/en
Active legal-status Critical Current
Anticipated expiration legal-status Critical

Links

Classifications

    • GPHYSICS
    • G06COMPUTING OR CALCULATING; COUNTING
    • G06FELECTRIC DIGITAL DATA PROCESSING
    • G06F16/00Information retrieval; Database structures therefor; File system structures therefor
    • G06F16/20Information retrieval; Database structures therefor; File system structures therefor of structured data, e.g. relational data
    • G06F16/24Querying
    • G06F16/245Query processing
    • G06F16/2458Special types of queries, e.g. statistical queries, fuzzy queries or distributed queries
    • G06F16/2471Distributed queries
    • GPHYSICS
    • G06COMPUTING OR CALCULATING; COUNTING
    • G06FELECTRIC DIGITAL DATA PROCESSING
    • G06F16/00Information retrieval; Database structures therefor; File system structures therefor
    • G06F16/20Information retrieval; Database structures therefor; File system structures therefor of structured data, e.g. relational data
    • G06F16/27Replication, distribution or synchronisation of data between databases or within a distributed database system; Distributed database system architectures therefor

Landscapes

  • Engineering & Computer Science (AREA)
  • Theoretical Computer Science (AREA)
  • Physics & Mathematics (AREA)
  • Databases & Information Systems (AREA)
  • Data Mining & Analysis (AREA)
  • General Physics & Mathematics (AREA)
  • General Engineering & Computer Science (AREA)
  • Mathematical Physics (AREA)
  • Computational Linguistics (AREA)
  • Software Systems (AREA)
  • Probability & Statistics with Applications (AREA)
  • Fuzzy Systems (AREA)
  • Computing Systems (AREA)
  • Information Retrieval, Db Structures And Fs Structures Therefor (AREA)

Abstract

The embodiment of the application provides a method, electronic equipment and storage medium for determining a master node in a cluster, wherein any target node in the cluster interacts with a database, whether the target node is used as the master node is determined from node information corresponding to each node in the database, and if so, first information for marking the target node as the master node is sent to the database. And when the master node does not exist, the master node is determined according to the node information every time, so that the existence of multiple master nodes is avoided, and the disorder of service order and the waste of resources are avoided. Meanwhile, node information is stored through the database, and special hardware equipment is not needed to be newly added in the determining process of the master node.

Description

一种确定集群中主节点的方法、电子设备和存储介质A method, electronic device and storage medium for determining a master node in a cluster

技术领域technical field

本发明涉及计算机技术领域,尤其是涉及一种确定集群中主节点的方法、电子设备和存储介质。The invention relates to the field of computer technology, in particular to a method for determining a master node in a cluster, an electronic device and a storage medium.

背景技术Background technique

随着互联网的发展,用户量越发庞大,普通单机应用已经无法满足高并发的应用,而为了解决高并发,高可用的场景,一般应用都使用集群来对外提供服务。然而集群中通常需要选举出一个master主机,用来执行一些特定的任务。With the development of the Internet, the number of users has become larger and larger, and ordinary stand-alone applications can no longer meet high-concurrency applications. In order to solve high-concurrency and high-availability scenarios, general applications use clusters to provide external services. However, a master host usually needs to be elected in the cluster to perform some specific tasks.

现有的方法一般都是基于zookeeper这种重量级的第三方或者使用主备机这种传统方式来实现master选举。其中,图1为主备机选举master的选举过程示意图,参见图1,在这种主备机的方式中,主机充当master角色,用来完成各种业务的处理,而备用结点(备机)则定期给当前主机结点发送Ping包,主节点接受到ping包后会向备用结点发送应答ack消息,当备用结点收到应答,就认为主机还活着,让他继续提供服务,否则就认为主机挂掉了,自己将开始行使主结点职责,充当master角色。Existing methods are generally based on a heavyweight third party like zookeeper or using the traditional method of master and backup machines to achieve master election. Among them, Figure 1 is a schematic diagram of the election process of master election for the master and backup machines. ) periodically sends Ping packets to the current host node. After receiving the ping packet, the master node will send a response ack message to the standby node. It is considered that the host is down, and I will start to exercise the responsibility of the master node and act as the master role.

然而,对于传统的主备master方式主要会存在如下隐患:当主机和备机的网络出现了问题,备机不能收到主机的ack回复消息时,备机会认为主节点挂掉,然后备机会自动启动自己的master实例。这样就会造成系统中有2个master主节点,也就是双master,这样整个服务就乱套了。同时传统的2个机器节点,从节点一般当作备机,也就是不提供服务,只有在主机宕机情况下,才会接管服务,这样就造成了系统资源浪费,闲置了一台机器。However, for the traditional active-standby master mode, there are mainly the following hidden dangers: when there is a problem with the network between the master and the standby machine, and the standby machine cannot receive the ack reply message from the master machine, the standby machine will think that the master node is down, and then the standby machine will automatically Start your own master instance. This will cause two master nodes in the system, that is, double masters, and the entire service will be messed up. At the same time, the traditional two machine nodes, the slave node is generally used as a backup machine, that is, it does not provide services, and only when the host machine goes down, it will take over the service, which causes a waste of system resources and leaves a machine idle.

对于传统应用来说增加zookeeper集群,相当于增加了一个重型装备,同时还要对zookeeper集群进行维护监控。且基于zookeeper的选举基本上都是依赖抢占和注册最小序号来实现master选举,选举的主机具有随机性,可控性较差。For traditional applications, adding a zookeeper cluster is equivalent to adding a piece of heavy equipment, and at the same time it is necessary to maintain and monitor the zookeeper cluster. Moreover, zookeeper-based elections basically rely on preemption and registration of the minimum serial number to achieve master election. The elected hosts are random and poorly controllable.

可见,现有的主节点确定过程要么存在多个主节点的隐患,造成服务秩序紊乱和资源浪费,要么需要增加相应的硬件设备。It can be seen that the existing master node determination process either has hidden dangers of multiple master nodes, resulting in disordered service order and waste of resources, or needs to increase corresponding hardware devices.

发明内容Contents of the invention

本发明实施例提供一种确定集群中主节点的方法、电子设备和存储介质,用以解决现有的主节点确定过程要么存在多个主节点的隐患,造成服务秩序紊乱和资源浪费,要么需要增加相应的硬件设备的问题。Embodiments of the present invention provide a method for determining a master node in a cluster, an electronic device, and a storage medium, to solve the problem of multiple master nodes in the existing master node determination process, resulting in disordered service order and waste of resources, or requiring Increase the problem of corresponding hardware devices.

针对以上技术问题,第一方面,本发明实施例提供一种确定集群中主节点的方法,包括:In view of the above technical problems, in the first aspect, the embodiment of the present invention provides a method for determining the master node in the cluster, including:

集群中任一目标节点若判断当前不存在主节点,则根据数据库中存储的集群各节点对应的节点信息,确定是否以所述目标节点作为主节点;If any target node in the cluster determines that there is currently no master node, then determine whether to use the target node as the master node according to the node information corresponding to each node of the cluster stored in the database;

若以所述目标节点作为主节点,则向所述数据库发送将所述目标节点标记为主节点的第一信息。If the target node is used as the master node, sending first information marking the target node as the master node to the database.

第二方面,本发明实施例提供一种确定集群中主节点的方法,包括:In a second aspect, an embodiment of the present invention provides a method for determining a master node in a cluster, including:

若接收到将集群中的目标节点标记为主节点的第一信息,则在所述目标节点的节点信息中添加主节点的标记。If the first information marking the target node in the cluster as the master node is received, the mark of the master node is added to the node information of the target node.

第三方面,本发明实施例提供一种电子设备,包括存储器、处理器及存储在存储器上并可在处理器上运行的计算机程序,所述处理器执行所述程序时实现以上所述的确定集群中主节点的方法的步骤。In a third aspect, an embodiment of the present invention provides an electronic device, including a memory, a processor, and a computer program stored in the memory and operable on the processor, and the processor implements the above-mentioned determination when executing the program The steps of the method of the master node in the cluster.

第四方面,本发明实施例提供一种非暂态可读存储介质,其上存储有计算机程序,该计算机程序被处理器执行时实现以上任一项所述的确定集群中主节点的方法的步骤。In a fourth aspect, an embodiment of the present invention provides a non-transitory readable storage medium, on which a computer program is stored. When the computer program is executed by a processor, the method for determining the master node in the cluster described in any one of the above items is implemented. step.

本发明的实施例提供了一种确定集群中主节点的方法、电子设备和存储介质,集群中的任一目标节点与数据库交互,从根据数据库中集群各节点对应的节点信息确定是否以自身作为主节点,若是,向数据库发送将所述目标节点标记为主节点的第一信息。每次在主节点不存在时,才根据节点信息确定主节点,不会导致多主节点的存在,避免了服务秩序紊乱和资源浪费。同时,通过数据库存储节点信息,主节点的确定过程无需新增专门的硬件设备。Embodiments of the present invention provide a method for determining the master node in the cluster, electronic equipment, and a storage medium. Any target node in the cluster interacts with the database, and determines whether to use itself as the node information corresponding to each node in the cluster in the database. If so, the master node sends first information marking the target node as the master node to the database. Every time the master node does not exist, the master node is determined according to the node information, which will not lead to the existence of multiple master nodes, and avoid service disorder and resource waste. At the same time, the node information is stored in the database, and the determination process of the master node does not need to add special hardware equipment.

附图说明Description of drawings

为了更清楚地说明本发明实施例或现有技术中的技术方案,下面将对实施例或现有技术描述中所需要使用的附图作一简单地介绍,显而易见地,下面描述中的附图是本发明的一些实施例,对于本领域普通技术人员来讲,在不付出创造性劳动的前提下,还可以根据这些附图获得其他的附图。In order to more clearly illustrate the technical solutions in the embodiments of the present invention or the prior art, the following will briefly introduce the drawings that need to be used in the description of the embodiments or the prior art. Obviously, the accompanying drawings in the following description These are some embodiments of the present invention. Those skilled in the art can also obtain other drawings based on these drawings without creative work.

图1是本发明实施例提供的主备机选举master的选举过程示意图;Fig. 1 is a schematic diagram of the election process of master and standby machine election master provided by the embodiment of the present invention;

图2是本发明另一实施例提供的适用于集群中任一主机的确定集群中主节点的方法的流程示意图;Fig. 2 is a schematic flowchart of a method for determining a master node in a cluster applicable to any host in the cluster provided by another embodiment of the present invention;

图3是本发明另一实施例提供的主节点选举过程中插队机制的实现过程示意图;Fig. 3 is a schematic diagram of the implementation process of the queue-jumping mechanism in the master node election process provided by another embodiment of the present invention;

图4是本发明另一实施例提供的主节点选举的流程示意图;Fig. 4 is a schematic flow diagram of master node election provided by another embodiment of the present invention;

图5是本发明另一实施例提供的适用于数据库的确定集群中主节点的方法的流程示意图;FIG. 5 is a schematic flowchart of a method for determining a master node in a cluster applicable to a database provided by another embodiment of the present invention;

图6是本发明另一实施例提供的对应于集群中任一节点的,确定集群中主节点的装置的结构框图;FIG. 6 is a structural block diagram of a device for determining the master node in the cluster corresponding to any node in the cluster provided by another embodiment of the present invention;

图7是本发明另一实施例提供的对应于数据库的,确定集群中主节点的装置的结构框图;FIG. 7 is a structural block diagram of a device for determining a master node in a cluster corresponding to a database provided by another embodiment of the present invention;

图8是本发明另一实施例提供的电子设备的实体结构示意图。Fig. 8 is a schematic diagram of the physical structure of an electronic device provided by another embodiment of the present invention.

具体实施方式Detailed ways

为使本发明实施例的目的、技术方案和优点更加清楚,下面将结合本发明实施例中的附图,对本发明实施例中的技术方案进行清楚、完整地描述,显然,所描述的实施例是本发明一部分实施例,而不是全部的实施例。基于本发明中的实施例,本领域普通技术人员在没有做出创造性劳动前提下所获得的所有其他实施例,都属于本发明保护的范围。In order to make the purpose, technical solutions and advantages of the embodiments of the present invention clearer, the technical solutions in the embodiments of the present invention will be clearly and completely described below in conjunction with the drawings in the embodiments of the present invention. Obviously, the described embodiments It is a part of embodiments of the present invention, but not all embodiments. Based on the embodiments of the present invention, all other embodiments obtained by persons of ordinary skill in the art without making creative efforts belong to the protection scope of the present invention.

本实施例提供一种确定集群中主节点的方法,由集群的任一主机和数据库配置实现。图2为本实施提供的适用于集群中任一主机的确定集群中主节点的方法的流程示意图,参见图2,该方法包括:This embodiment provides a method for determining the master node in the cluster, which is implemented by any host and database configuration of the cluster. Figure 2 is a schematic flow chart of the method for determining the master node in the cluster applicable to any host in the cluster provided by this implementation, referring to Figure 2, the method includes:

步骤201:集群中任一目标节点若判断当前不存在主节点,则根据数据库中存储的集群各节点对应的节点信息,确定是否以所述目标节点作为主节点。Step 201: If any target node in the cluster determines that there is currently no master node, it determines whether to use the target node as the master node according to the node information corresponding to each node of the cluster stored in the database.

集群为由多个节点组成的网络,每一节点为主机、服务器或者计算机,本实施例对此不作具体限制。A cluster is a network composed of multiple nodes, and each node is a host, server or computer, which is not specifically limited in this embodiment.

节点信息包括各节点执行预设指令的时间点,以及节点的基本信息;其中,节点的基本信息包括节点的机器IP、机器名、主键、操作系统类型、操作系统安装时间、刷新更新时间等。其中,预设指令包括节点实现某一功能执行的指令,例如,预设指令包括将节点对应的节点信息存储到所述数据库(即在数据库注册)、对节点进行初始化、节点启动某一应用程序、节点发送某一消息等,本实施例对此不做具体限制。The node information includes the time point when each node executes the preset command, and the basic information of the node; wherein, the basic information of the node includes the node's machine IP, machine name, primary key, operating system type, operating system installation time, refresh update time, etc. Wherein, the preset instruction includes an instruction for the node to implement a certain function execution, for example, the preset instruction includes storing the node information corresponding to the node in the database (that is, registering in the database), initializing the node, and starting a certain application program by the node , the node sends a certain message, etc., which are not specifically limited in this embodiment.

数据库为任意的关系型数据库,例如,mysql、sqlserver、oracle等。通过关系型数据库方便数据的读取和存储,只需要将关系型数据库的存储功能应用到本申请的技术方案中即可,不需要额外设置其它硬件设备。The database is any relational database, for example, mysql, sqlserver, oracle, and the like. To facilitate data reading and storage through a relational database, it is only necessary to apply the storage function of the relational database to the technical solution of the present application, and no additional hardware devices are required.

步骤202:若以所述目标节点作为主节点,则向所述数据库发送将所述目标节点标记为主节点的第一信息。Step 202: If the target node is used as the master node, send first information marking the target node as the master node to the database.

其中,还包括:通过所述主节点执行设定任务,或者对集群中的各节点进行协调。Wherein, it also includes: performing setting tasks through the master node, or coordinating each node in the cluster.

本实施例提供了一种确定集群中主节点的方法,集群中的任一目标节点与数据库交互,从根据数据库中集群各节点对应的节点信息确定是否以自身作为主节点,若是,向数据库发送将所述目标节点标记为主节点的第一信息。每次在主节点不存在时,才根据节点信息确定主节点,不会导致多主节点的存在,避免了服务秩序紊乱和资源浪费。同时,通过数据库存储节点信息,主节点的确定过程无需新增专门的硬件设备。This embodiment provides a method for determining the master node in the cluster. Any target node in the cluster interacts with the database, and determines whether to use itself as the master node according to the node information corresponding to each node in the cluster in the database. If so, send the First information marking the target node as a master node. Every time the master node does not exist, the master node is determined according to the node information, which will not lead to the existence of multiple master nodes, and avoid service disorder and resource waste. At the same time, the node information is stored in the database, and the determination process of the master node does not need to add special hardware equipment.

进一步地,在上述实施例的基础上,所述根据数据库中存储的集群各节点对应的节点信息,确定是否以所述目标节点作为主节点,包括:Further, on the basis of the above embodiments, the determining whether to use the target node as the master node according to the node information corresponding to each node of the cluster stored in the database includes:

根据所述节点信息确定各节点对应的选举时间点,根据各选举时间点与当前时间点间隔的第一时长确定目标选举时间点;Determine the election time point corresponding to each node according to the node information, and determine the target election time point according to the first time interval between each election time point and the current time point;

若所述目标选举时间点对应的节点为所述目标节点,则以所述目标节点作为主节点;If the node corresponding to the target election time point is the target node, use the target node as the master node;

其中,选举时间点为节点执行预设指令的时间点,所述预设指令包括将节点对应的节点信息存储到所述数据库或对节点进行初始化。Wherein, the election time point is a time point when a node executes a preset instruction, and the preset instruction includes storing node information corresponding to the node in the database or initializing the node.

需要说明的是,选举时间点是从节点信息中存储的众多的时间点中确定的,用于进行主节点选举的时间点。当对每一节点均确定了选举时间点后,即可根据各节点对应的选举时间点确定出主节点。可以在每次判断当前不存在主节点后,均以节点执行某一相同的预设指令的时间点作为选举时间点(例如,每次均以对节点进行初始化的时间点作为选举时间点),也可以以节点执行不同预设指令的时间点作为选举时间点(例如,首次或前几次判断当前不存在主节点时,以将节点对应的节点信息存储到所述数据库的时间点作为选举之间点,在非首次或者前几次之后判断当前不存在主节点时,以对节点进行初始化的时间点作为选举时间点)。It should be noted that the election time point is determined from numerous time points stored in the node information, and is used for the time point of master node election. After the election time point is determined for each node, the master node can be determined according to the election time point corresponding to each node. After each judgment that there is currently no master node, the time point when the node executes the same preset instruction can be used as the election time point (for example, the time point when the node is initialized is used as the election time point each time), The time point when the node executes different preset instructions can also be used as the election time point (for example, when it is judged that there is no master node for the first time or the first few times, the time point when the node information corresponding to the node is stored in the database is used as the election time point When it is not the first time or after the first few times it is judged that there is currently no master node, the time point when the node is initialized is used as the election time point).

其中,根据各选举时间点与当前时间点间隔的第一时长确定目标选举时间点,包括:Among them, the target election time point is determined according to the first time interval between each election time point and the current time point, including:

根据各选举时间点与当前时间点间隔的第一时长,获取最长的第一时长或最短的第一时长对应的选举时间点,作为目标选举时间点;According to the first time interval between each election time point and the current time point, the election time point corresponding to the longest first time length or the shortest first time length is obtained as the target election time point;

其中,若最长的第一时长或最短的第一时长存在多个选举时间点,则从多个选举时间点中随机选取一个选举时间点,作为目标选举时间点。Wherein, if there are multiple election time points for the longest first duration or the shortest first duration, an election time point is randomly selected from the plurality of election time points as the target election time point.

本实施例在判断当前不存在主节点后,根据通过节点信息确定的选举时间点,实现了对主节点的选举。主节点的选举依据各节点的选举时间点唯一确定,避免了多主节点的产生,提高了主节点选举的合理性。In this embodiment, after judging that there is currently no master node, the election of the master node is implemented according to the election time point determined through the node information. The election of the master node is uniquely determined based on the election time of each node, which avoids the generation of multiple master nodes and improves the rationality of the master node election.

在本实施例中,可以使得每次以节点执行不同的预设指令的时间点作为选举时间点,进一步地,在上述各实施例的基础上,所述根据所述节点信息确定各节点对应的选举时间点,根据各选举时间点与当前时间点间隔的第一时长确定目标选举时间点,包括:In this embodiment, the time point at which a node executes a different preset instruction can be used as the election time point each time. Further, on the basis of the above-mentioned embodiments, the determination of the node corresponding to each node according to the node information Election time point, the target election time point is determined according to the first time interval between each election time point and the current time point, including:

根据预设对应关系,确定与当前判断不存在主节点的次数对应的预设指令,作为目标预设指令;According to the preset corresponding relationship, determine the preset command corresponding to the number of times that the master node is currently judged not to exist as the target preset command;

根据所述目标预设指令和所述节点信息,各节点对应的选举时间点,根据各选举时间点与当前时间点间隔的第一时长确定目标选举时间点。According to the target preset instruction and the node information, the election time point corresponding to each node is determined according to the first time interval between each election time point and the current time point.

其中,所述预设对应关系为预先存储的,与当前判断不存在主节点的次数对应的预设指令。预设对应关系可以根据需要设定,例如,设定首次判断当前不存在主节点时,以执行“将节点对应的节点信息存储到所述数据库”这一预设指令的时间点作为选举时间点。非首次判断当前不存在主节点时,以执行“对节点进行初始化”这一预设指令的时间点作为选举时间点。Wherein, the preset corresponding relationship is a pre-stored preset instruction corresponding to the number of times it is currently judged that there is no master node. The preset corresponding relationship can be set as needed, for example, when it is judged for the first time that there is currently no master node, the time point when the preset instruction "store the node information corresponding to the node into the database" is executed as the election time point . When it is not judged for the first time that there is no master node at present, the time point when the preset instruction "initialize the node" is executed is taken as the election time point.

具体地,首次判断当前不存在主节点时,根据所述节点信息获取各节点将节点信息存储到所述数据库的时间点,作为选举时间点,根据各选举时间点与当前时间点间隔的第一时长确定目标选举时间点;Specifically, when it is judged for the first time that there is currently no master node, the time point at which each node stores the node information in the database is obtained according to the node information as the election time point, and according to the first interval between each election time point and the current time point The duration determines the target election time point;

非首次判断当前不存在主节点时,根据所述节点信息获取各节点对节点进行初始化的时间点,作为选举时间点,根据各选举时间点与当前时间点间隔的第一时长确定目标选举时间点。When it is not judged for the first time that there is currently no master node, the time point at which each node initializes the node is obtained according to the node information as the election time point, and the target election time point is determined according to the first time interval between each election time point and the current time point .

本实施例根据当前判断不存在主节点的次数对应的预设指令,从节点信息中确定选举时间点,选举时间点的确定可以通过预设对应关系进行设置,增加了主节点选举的活性。In this embodiment, the election time point is determined from the node information according to the preset instructions corresponding to the number of times that no master node is currently judged.

在一些场景下,需要使得特定的某一节点被选举为主节点,即实现节点的“插队”,为了实现节点的“插队”,进一步地,在上述各实施例的基础上,还包括:In some scenarios, it is necessary to make a specific node elected as the master node, that is, to realize the "queue jumping" of the node. In order to realize the "queue jumping" of the node, further, on the basis of the above-mentioned embodiments, it also includes:

生成时间修改指令,时间修改指令用于对所述数据库中节点对应的选举时间点进行修改;Generate a time modification instruction, the time modification instruction is used to modify the election time point corresponding to the node in the database;

将所述时间修改指令发送到所述数据库,以使所述数据库根据所述时间修改指令对节点对应的选举时间点进行修改。The time modification instruction is sent to the database, so that the database modifies the election time point corresponding to the node according to the time modification instruction.

其中,目标节点在接收到集群中的节点对应的选举时间点进行修改的修改请求后,生成所述时间修改指令。修改请求可以为用户通过交互界面输入到目标节点的请求,本实施例对此不做具体限制。Wherein, the target node generates the time modification instruction after receiving a modification request for modifying the election time point corresponding to the nodes in the cluster. The modification request may be a request input by the user to the target node through an interactive interface, which is not specifically limited in this embodiment.

其中,目标节点生成的所述时间修改指令用于请求对目标节点对应的选举时间点进行修改,或者用于请求对集群节点中除了目标节点之外的节点对应的选举时间点进行修改。Wherein, the time modification instruction generated by the target node is used to request modification of the election time point corresponding to the target node, or to request modification of the election time point corresponding to nodes in the cluster nodes other than the target node.

举例来说,当需要使得目标节点被选为主节点,则可以向数据库发送时间修改指令,对该目标节点对应的选举时间点进行修改,使得根据选举时间点与当前时间点的第一时长能够将该目标节点确定为主节点。例如,当需要选取最短的第一时长对应的节点作为主节点,则可以通过时间修改指令对某一节点的选举时间点进行修改,使得修改之后该目标节点对应的第一时长最短,从而可以将该目标节点选举为主节点。For example, when the target node needs to be selected as the master node, a time modification command can be sent to the database to modify the election time point corresponding to the target node, so that the election time point and the first duration of the current time point can be Determine the target node as the primary node. For example, when the node corresponding to the shortest first duration needs to be selected as the master node, the election time point of a certain node can be modified through the time modification command, so that the first duration corresponding to the target node is the shortest after modification, so that the The target node is elected as the master node.

本实施例通过时间修改指令对集群各节点的选举时间点进行修改,实现了主节点选举过程中,对特定的节点进行“插队”,使得该特定的节点被选举为主节点。“插队”机制增加了主节点选举的可控性。In this embodiment, the election time point of each node of the cluster is modified through a time modification instruction, so that during the master node election process, a specific node is "jumped in the queue", so that the specific node is elected as the master node. The "queue jumping" mechanism increases the controllability of the master node election.

具体来说,本实施例通过数据库来记录节点信息,同时对选举过程使用选举时间点(选举时间点为执行预设指令的时间点或者根据时间设置指令对节点设置的时间点)以及增加合理有序的“插队”机制(即通过时间修改指令修改选举时间点)来保证master被合理的主机来充当,这样就优化了整个机制,不再是随机或者按序的变化master,同时优先级可以根据各主机进行配置。Specifically, this embodiment uses the database to record node information, and uses the election time point for the election process (the election time point is the time point when the preset instruction is executed or the time point set for the node according to the time setting instruction) and increases the reasonable and effective time point. The sequential "queue jumping" mechanism (that is, modifying the election time point through time modification instructions) ensures that the master is acted by a reasonable host, which optimizes the entire mechanism and no longer changes the master randomly or sequentially. At the same time, the priority can be changed according to Each host is configured.

图3为本实施例提供的主节点选举过程中“插队”机制的实现过程示意图,参见图3,“插队”机制的实现过程包括如下内容:Figure 3 is a schematic diagram of the implementation process of the "queue jumping" mechanism in the master node election process provided by this embodiment. Referring to Figure 3, the implementation process of the "queue jumping" mechanism includes the following contents:

(1)获取机器的操作系统初始化时间。(1) Get the operating system initialization time of the machine.

(2)判断master主机宕机后,删除原有master的记录信息。(2) After judging that the master host is down, delete the record information of the original master.

(3)根据server_info表的init_time的值(即操作系统初始化时间,也就是对节点进行初始化的时间),选举最新的机器修改注册时间为所有机器最小的注册时间。(3) According to the value of init_time in the server_info table (that is, the operating system initialization time, that is, the time to initialize the node), select the latest machine to modify the registration time to be the minimum registration time of all machines.

(4)系统将重新选举主机,判断最小注册时间的机器为master。(4) The system will re-elect the host, and judge the machine with the minimum registration time as the master.

进一步地,在上述各实施例的基础上,还包括:Further, on the basis of the above-mentioned embodiments, it also includes:

根据所述数据库中各节点周期性上报的心跳时间点,判断是否存在异常节点,其中,异常节点为最近一次上报的心跳时间点与当前时间点间隔的第二时长大于预设时长的节点;According to the heartbeat time point periodically reported by each node in the database, it is judged whether there is an abnormal node, wherein the abnormal node is a node whose second time interval between the heartbeat time point reported last time and the current time point is greater than the preset time length;

若存在异常节点,则向所述数据库发送将异常节点的节点信息删除的第二信息。If there is an abnormal node, sending second information to the database to delete the node information of the abnormal node.

其中,预设时长为1分钟。Wherein, the preset duration is 1 minute.

其中,各节点每隔30秒上报一次当前时间点,作为心跳时间点。Wherein, each node reports the current time point every 30 seconds as the heartbeat time point.

本实施例通过心跳时间点对各主机是否存在宕机进行识别,能够及时排除宕机节点参与主节点的选举,有效避免了多个主节点的情况出现。In this embodiment, the heartbeat time point is used to identify whether each host is down, and the downtime node can be excluded in time to participate in the election of the master node, effectively avoiding the occurrence of multiple master nodes.

进一步地,在上述各实施例的基础上,还包括:Further, on the basis of the above-mentioned embodiments, it also includes:

根据数据库中当前存储的各节点的节点信息,判断是否存在被标记为主节点的节点信息,若否,则集群各节点中当前不存在主节点。According to the node information of each node currently stored in the database, it is judged whether there is node information marked as a master node, and if not, there is no master node in each node of the cluster.

其中,若判断存在被标记为主节点的节点信息,则不需进行主节点的选举。Wherein, if it is judged that there is node information marked as the master node, no election of the master node is required.

本实施例通过对主节点的标记实现了对集群中当前是否存在主节点的判断。In this embodiment, it is realized whether a master node currently exists in the cluster is judged by marking the master node.

图4为本实施例提供的主节点选举的流程示意图,参见图4,该流程具体包括如下内容:Fig. 4 is a schematic flow diagram of the master node election provided in this embodiment, referring to Fig. 4, the flow specifically includes the following contents:

(1)每台主机启动,获取自己的业务IP,将IP写入到数据库表server_master中,register_time为当前软件启动时间,刷新时间和注册时间一样,将is_master字段标识为否。(1) Each host starts, obtains its own business IP, and writes the IP into the database table server_master, register_time is the current software startup time, the refresh time is the same as the registration time, and the is_master field is marked as No.

(2)每台机器在启动时同时检查操作系统的初始化安装时间,可以根据系统文件的修改时间进行检测,同时获取操作系统的类型和机器名称。将这些信息写入到server_info主机信息表中。(2) Each machine checks the initial installation time of the operating system at the same time when starting, and can detect according to the modification time of the system file, and obtain the type of the operating system and the machine name at the same time. Write this information into the server_info host information table.

(3)每台主机间隔30秒更新数据库的server_master表的refresh_time字段,表示主机的心跳。(3) Each host updates the refresh_time field of the server_master table of the database every 30 seconds, indicating the heartbeat of the host.

(4)在每台主机第一次更新心跳的时候,判断自己是否能成为master;遍历server_master表的所有记录,获取注册时间register_time最早的机器的记录,检查该记录的IP和自己的机器IP是否匹配,如果匹配则将自己标识为master角色,同时更新记录中的is_master字段为true。(4) When each host updates its heartbeat for the first time, judge whether it can become a master; traverse all records in the server_master table, obtain the record of the machine with the earliest registration time register_time, and check whether the IP of the record and its own machine IP are Match, if it matches, identify yourself as the master role, and update the is_master field in the record to true.

(5)获取master权限的机器则执行属于master的任务。(5) The machine that obtains the master authority executes the tasks belonging to the master.

(6)在之后的心跳检测如果有refresh_time减去当前时间大于1分钟还未更新的主机,则删除该条记录,表示该主机已经断线。如果是宕机的是master角色,在心跳失败的时候也会释放自己master的权限,相应master任务就会释放。(6) In the subsequent heartbeat detection, if there is a host whose refresh_time minus the current time is greater than 1 minute and has not been updated, the record will be deleted, indicating that the host has been disconnected. If it is the master role that is down, it will also release its own master permissions when the heartbeat fails, and the corresponding master tasks will be released.

(7)在master主机释放后,系统需要重新选举master,这个时候,每台主机则会读取server_info的信息,根据init_time(系统安装初始化时间)来判断机器的使用时长,如果init_time距离现在的时间最短,则表示该主机最新,那么和自己IP相同的机器就会取更新server_master表的注册时间(根据IP修改),将它更新为所有注册时间中最小的。(7) After the master host is released, the system needs to re-elect the master. At this time, each host will read the server_info information, and judge the use time of the machine according to the init_time (system installation initialization time). If the init_time is from the current time The shortest means that the host is up-to-date, then the machine with the same IP as its own will take the registration time of updating the server_master table (modified according to the IP), and update it to the smallest of all registration times.

(8)在后续的心跳中,所有机器检测注册时间的时候,那么最小的注册时间的机器,即刚才最新的那台机器就会顺利被选举为master主机。(8) In the subsequent heartbeat, when all machines detect the registration time, the machine with the smallest registration time, that is, the latest machine just now, will be successfully elected as the master host.

图5为本实施提供的适用于数据库的确定集群中主节点的方法的流程示意图,参见图5,该方法包括:Fig. 5 is a schematic flow chart of the method for determining the master node in the cluster applicable to the database provided by this implementation. Referring to Fig. 5, the method includes:

步骤501:若接收到将集群中的目标节点标记为主节点的第一信息,则在所述目标节点的节点信息中添加主节点的标记。Step 501: If the first information marking the target node in the cluster as the master node is received, add the mark of the master node to the node information of the target node.

第一信息为集群中任一节点(例如,目标节点),发送的用于指示将目标节点标记为主节点的信息。The first information is information sent by any node (for example, the target node) in the cluster to indicate that the target node is marked as the master node.

本实施例提供了一种确定集群中主节点的方法,集群中的任一目标节点与数据库交互,从根据数据库中集群各节点对应的节点信息确定是否以自身作为主节点,若是,向数据库发送将所述目标节点标记为主节点的第一信息。每次在主节点不存在时,才根据节点信息确定主节点,不会导致多主节点的存在,避免了服务秩序紊乱和资源浪费。同时,通过数据库存储节点信息,主节点的确定过程无需新增专门的硬件设备。This embodiment provides a method for determining the master node in the cluster. Any target node in the cluster interacts with the database, and determines whether to use itself as the master node according to the node information corresponding to each node in the cluster in the database. If so, send the First information marking the target node as a master node. Every time the master node does not exist, the master node is determined according to the node information, which will not lead to the existence of multiple master nodes, and avoid service disorder and resource waste. At the same time, the node information is stored in the database, and the determination process of the master node does not need to add special hardware equipment.

进一步地,在上述实施例的基础上,还包括:Further, on the basis of the above-mentioned embodiments, it also includes:

若接收到时间修改指令,则根据所述时间修改指令对节点对应的选举时间点进行修改。If the time modification instruction is received, the election time point corresponding to the node is modified according to the time modification instruction.

其中,还包括;Among them, also include;

若接收到节点发送的节点信息和/或各节点周期性上报的心跳时间点,则存储接收到的节点信息和/或心跳时间点;If the node information sent by the node and/or the heartbeat time point periodically reported by each node is received, the received node information and/or heartbeat time point are stored;

若接收到将异常节点的节点信息删除的第二信息,则将异常节点的节点信息删除。If the second information for deleting the node information of the abnormal node is received, the node information of the abnormal node is deleted.

需要说明的是,在本实施例提供的方法中,数据库中对于集群各节点为了两张列表,一张列表用于存取各节点的节点信息,另一张列表用于存取节点的心跳时间点。在数据库中创建了两个表,其中一个表存取各节点的基本信息和选举时间点,在维护节点信息的列表中,包括了各节点对应的选举时间点,当数据库接收到对某一节点的选举时间点进行修改的时间修改指令,则对该节点对应的选举时间点进行修改。It should be noted that, in the method provided in this embodiment, there are two lists for each node in the database in the database, one list is used to access the node information of each node, and the other list is used to access the heartbeat time of the node point. Two tables are created in the database, one of which accesses the basic information of each node and the election time point. In the list of maintenance node information, the election time point corresponding to each node is included. When the database receives a request for a node The time modification instruction for modifying the election time point of the node will modify the election time point corresponding to the node.

本实施例提供的确定集群中主节点的方法使用关系型数据库进行主机联网信息的存储,根据注册时间、操作系统的安装初始化时间进行master的选举,通过能够对操作系统的安装初始化时间进行修改,使得选举中实现插队,增加了选举的可控性。具有的优点如下:相对于传统的主备模式来说,杜绝了双master的隐患。利用数据库来进行注册信息,不必安装zookeeper重量级软件。充分考虑各种情况的master选举,比如按时间顺序选举,调整时间机制进行插队选举。The method for determining the master node in the cluster provided by this embodiment uses a relational database to store the networking information of the host, and elects the master according to the registration time and the installation and initialization time of the operating system. By being able to modify the installation and initialization time of the operating system, It enables queue jumping during the election and increases the controllability of the election. The advantages are as follows: Compared with the traditional master-standby mode, it eliminates the hidden danger of dual masters. Use the database to register information without installing zookeeper heavyweight software. Fully consider the master elections in various situations, such as elections in chronological order, and adjust the time mechanism for queue-jumping elections.

在上述各实施例提供的方法中,数据库用于存储节点信息和节点发送的心跳时间点,并向节点发送节点需要的信息。这一过程由节点执行了从数据库读取节点信息,选举出主节点,并向数据库发送时间修改指令的操作,数据库不主动参与主节点的选举过程和对选举时间点的主动修改过程。因此,对于这一过程开发者只需要在节点侧开发,直接调用数据库的存取功能即可。然而,实际上对于一些方便开发者进行开发的数据库,也可以通过如下过程实现主节点的选举:In the methods provided in the foregoing embodiments, the database is used to store node information and heartbeat time points sent by the nodes, and send information required by the nodes to the nodes. In this process, the node performs the operation of reading node information from the database, electing the master node, and sending time modification instructions to the database. The database does not actively participate in the election process of the master node and the active modification process of the election time point. Therefore, for this process, developers only need to develop on the node side and directly call the access function of the database. However, in fact, for some databases that are convenient for developers to develop, the election of the master node can also be achieved through the following process:

数据库判断当前是否存在标记为主节点的节点,若否,则根据各节点对应的选举时间点确定主节点,将确定的主节点对应的节点标识发送到集群各节点。The database judges whether there is currently a node marked as the master node. If not, the master node is determined according to the election time point corresponding to each node, and the node identifier corresponding to the determined master node is sent to each node of the cluster.

还包括:接收到对目标节点对应的选举时间点进行修改的修改请求后,根据所述修改请求,对目标节点对应的选举时间点进行修改。The method further includes: after receiving a modification request for modifying the election time point corresponding to the target node, modifying the election time point corresponding to the target node according to the modification request.

还包括:接收集群各节点周期性上报的心跳时间点,存储该节点对应的心跳时间点。It also includes: receiving the heartbeat time point periodically reported by each node of the cluster, and storing the heartbeat time point corresponding to the node.

还包括:根据各节点对应的心跳时间点判断是否存在异常节点,若是,删除异常节点对应的节点信息。It also includes: judging whether there is an abnormal node according to the heartbeat time point corresponding to each node, and if so, deleting the node information corresponding to the abnormal node.

其中,异常节点为最近一次上报的心跳时间点与当前时间点间隔的第二时长大于预设时长的节点。Wherein, the abnormal node is a node whose second time interval between the last reported heartbeat time point and the current time point is longer than the preset time length.

图6为本实施例提供的对应于集群中任一节点的,确定集群中主节点的装置的结构框图,参见图6,该确定集群中主节点的装置包括确定模块601和发送模块602,其中,Fig. 6 is a structural block diagram of the device for determining the master node in the cluster corresponding to any node in the cluster provided by this embodiment. Referring to Fig. 6, the device for determining the master node in the cluster includes a determination module 601 and a sending module 602, wherein ,

确定模块601,用于若判断当前不存在主节点,则根据数据库中存储的集群各节点对应的节点信息,确定是否以所述目标节点作为主节点;The determining module 601 is configured to determine whether to use the target node as the master node according to the node information corresponding to each node of the cluster stored in the database if it is judged that there is currently no master node;

发送模块602,用于若以所述目标节点作为主节点,则向所述数据库发送将所述目标节点标记为主节点的第一信息。The sending module 602 is configured to send first information marking the target node as the master node to the database if the target node is used as the master node.

本实施例提供的确定集群中主节点的装置适用于上述各实施例提供的对应于集群中任一节点的,确定集群中主节点的方法,在此不再赘述。The apparatus for determining the master node in the cluster provided in this embodiment is applicable to the methods for determining the master node in the cluster corresponding to any node in the cluster provided in the above embodiments, and details are not repeated here.

本发明的实施例提供了一种确定集群中主节点的装置,集群中的任一目标节点与数据库交互,从根据数据库中集群各节点对应的节点信息确定是否以自身作为主节点,若是,向数据库发送将所述目标节点标记为主节点的第一信息。每次在主节点不存在时,才根据节点信息确定主节点,不会导致多主节点的存在,避免了服务秩序紊乱和资源浪费。同时,通过数据库存储节点信息,主节点的确定过程无需新增专门的硬件设备。The embodiment of the present invention provides a device for determining the master node in the cluster. Any target node in the cluster interacts with the database, and determines whether to use itself as the master node according to the node information corresponding to each node in the cluster in the database. If so, send The database sends first information marking the target node as a master node. Every time the master node does not exist, the master node is determined according to the node information, which will not lead to the existence of multiple master nodes, and avoid service disorder and waste of resources. At the same time, the node information is stored in the database, and the determination process of the master node does not need to add special hardware equipment.

可选地,所述根据数据库中存储的集群各节点对应的节点信息,确定是否以所述目标节点作为主节点,包括:Optionally, the determining whether to use the target node as the master node according to the node information corresponding to each node of the cluster stored in the database includes:

根据所述节点信息确定各节点对应的选举时间点,根据各选举时间点与当前时间点间隔的第一时长确定目标选举时间点;Determine the election time point corresponding to each node according to the node information, and determine the target election time point according to the first time interval between each election time point and the current time point;

若所述目标选举时间点对应的节点为所述目标节点,则以所述目标节点作为主节点;If the node corresponding to the target election time point is the target node, use the target node as the master node;

其中,选举时间点为节点执行预设指令的时间点,所述预设指令包括将节点对应的节点信息存储到所述数据库或对节点进行初始化。Wherein, the election time point is a time point when a node executes a preset instruction, and the preset instruction includes storing node information corresponding to the node in the database or initializing the node.

可选地,还包括:Optionally, also include:

生成时间修改指令,时间修改指令用于对所述数据库中节点对应的选举时间点进行修改;Generate a time modification instruction, the time modification instruction is used to modify the election time point corresponding to the node in the database;

将所述时间修改指令发送到所述数据库,以使所述数据库根据所述时间修改指令对节点对应的选举时间点进行修改。The time modification instruction is sent to the database, so that the database modifies the election time point corresponding to the node according to the time modification instruction.

可选地,还包括:Optionally, also include:

根据所述数据库中各节点周期性上报的心跳时间点,判断是否存在异常节点,其中,异常节点为最近一次上报的心跳时间点与当前时间点间隔的第二时长大于预设时长的节点;According to the heartbeat time point periodically reported by each node in the database, it is judged whether there is an abnormal node, wherein the abnormal node is a node whose second time interval between the heartbeat time point reported last time and the current time point is greater than the preset time length;

若存在异常节点,则向所述数据库发送将异常节点的节点信息删除的第二信息。If there is an abnormal node, sending second information to the database to delete the node information of the abnormal node.

可选地,还包括:Optionally, also include:

根据数据库中当前存储的各节点的节点信息,判断是否存在被标记为主节点的节点信息,若否,则集群各节点中当前不存在主节点。According to the node information of each node currently stored in the database, it is judged whether there is node information marked as a master node, and if not, there is no master node in each node of the cluster.

可选地,所述根据所述节点信息确定各节点对应的选举时间点,根据各选举时间点与当前时间点间隔的第一时长确定目标选举时间点,包括:Optionally, the determining the election time point corresponding to each node according to the node information, and determining the target election time point according to the first time interval between each election time point and the current time point include:

根据预设对应关系,确定与当前判断不存在主节点的次数对应的预设指令,作为目标预设指令;According to the preset corresponding relationship, determine the preset command corresponding to the number of times that the master node is currently judged not to exist as the target preset command;

根据所述目标预设指令和所述节点信息,各节点对应的选举时间点,根据各选举时间点与当前时间点间隔的第一时长确定目标选举时间点。According to the target preset instruction and the node information, the election time point corresponding to each node is determined according to the first time interval between each election time point and the current time point.

图7为本实施例提供的对应于数据库的,确定集群中主节点的装置的结构框图,参见图7,该确定集群中主节点的装置包括标记模块701,其中,FIG. 7 is a structural block diagram of the device for determining the master node in the cluster corresponding to the database provided in this embodiment. Referring to FIG. 7, the device for determining the master node in the cluster includes a marking module 701, wherein,

标记模块701,用于若接收到将集群中的目标节点标记为主节点的第一信息,则在所述目标节点的节点信息中添加主节点的标记。The marking module 701 is configured to add the marking of the master node to the node information of the target node if the first information marking the target node in the cluster as the master node is received.

本实施例提供的确定集群中主节点的装置适用于上述各实施例提供的对应于数据库的,确定集群中主节点的方法,在此不再赘述。The apparatus for determining the master node in the cluster provided in this embodiment is applicable to the methods for determining the master node in the cluster corresponding to the databases provided in the above embodiments, and will not be repeated here.

本发明的实施例提供了一种确定集群中主节点的装置,集群中的任一目标节点与数据库交互,从根据数据库中集群各节点对应的节点信息确定是否以自身作为主节点,若是,向数据库发送将所述目标节点标记为主节点的第一信息。每次在主节点不存在时,才根据节点信息确定主节点,不会导致多主节点的存在,避免了服务秩序紊乱和资源浪费。同时,通过数据库存储节点信息,主节点的确定过程无需新增专门的硬件设备。The embodiment of the present invention provides a device for determining the master node in the cluster. Any target node in the cluster interacts with the database, and determines whether to use itself as the master node according to the node information corresponding to each node in the cluster in the database. If so, send The database sends first information marking the target node as a master node. Every time the master node does not exist, the master node is determined according to the node information, which will not lead to the existence of multiple master nodes, and avoid service disorder and resource waste. At the same time, the node information is stored in the database, and the determination process of the master node does not need to add special hardware equipment.

可选地,还包括:Optionally, also include:

若接收到时间修改指令,则根据所述时间修改指令对节点对应的选举时间点进行修改。If the time modification instruction is received, the election time point corresponding to the node is modified according to the time modification instruction.

图8示例了一种电子设备的实体结构示意图,如图8所示,该电子设备可以包括:处理器(processor)801、通信接口(Communications Interface)802、存储器(memory)803和通信总线804,其中,处理器801,通信接口802,存储器803通过通信总线804完成相互间的通信。处理器801可以调用存储器803中的逻辑指令,以执行如下方法:集群中任一目标节点若判断当前不存在主节点,则根据数据库中存储的集群各节点对应的节点信息,确定是否以所述目标节点作为主节点;若以所述目标节点作为主节点,则向所述数据库发送将所述目标节点标记为主节点的第一信息,或者若接收到将集群中的目标节点标记为主节点的第一信息,则在所述目标节点的节点信息中添加主节点的标记。FIG. 8 illustrates a schematic diagram of the physical structure of an electronic device. As shown in FIG. 8, the electronic device may include: a processor (processor) 801, a communication interface (Communications Interface) 802, a memory (memory) 803, and a communication bus 804, Wherein, the processor 801 , the communication interface 802 , and the memory 803 communicate with each other through a communication bus 804 . The processor 801 can call the logical instructions in the memory 803 to execute the following method: if any target node in the cluster judges that there is no master node currently, then according to the node information corresponding to each node of the cluster stored in the database, determine whether to use the The target node is used as the master node; if the target node is used as the master node, the first information marking the target node as the master node is sent to the database, or if the target node in the cluster is marked as the master node if received the first information of the target node, add the mark of the master node to the node information of the target node.

此外,上述的存储器803中的逻辑指令可以通过软件功能单元的形式实现并作为独立的产品销售或使用时,可以存储在一个计算机可读取存储介质中。基于这样的理解,本发明的技术方案本质上或者说对现有技术做出贡献的部分或者该技术方案的部分可以以软件产品的形式体现出来,该计算机软件产品存储在一个存储介质中,包括若干指令用以使得一台计算机设备(可以是个人计算机,服务器,或者网络设备等)执行本发明各个实施例所述方法的全部或部分步骤。而前述的存储介质包括:U盘、移动硬盘、只读存储器(ROM,Read-Only Memory)、随机存取存储器(RAM,Random Access Memory)、磁碟或者光盘等各种可以存储程序代码的介质。In addition, the above logic instructions in the memory 803 may be implemented in the form of software function units and when sold or used as an independent product, may be stored in a computer-readable storage medium. Based on this understanding, the essence of the technical solution of the present invention or the part that contributes to the prior art or the part of the technical solution can be embodied in the form of a software product, and the computer software product is stored in a storage medium, including Several instructions are used to make a computer device (which may be a personal computer, a server, or a network device, etc.) execute all or part of the steps of the methods described in various embodiments of the present invention. The aforementioned storage medium includes: U disk, mobile hard disk, read-only memory (ROM, Read-Only Memory), random access memory (RAM, Random Access Memory), magnetic disk or optical disk and other media that can store program codes. .

进一步地,本发明实施例公开一种计算机程序产品,所述计算机程序产品包括存储在非暂态可读存储介质上的计算机程序,所述计算机程序包括程序指令,当所述程序指令被计算机执行时,计算机能够执行上述各方法实施例所提供的方法,例如包括:集群中任一目标节点若判断当前不存在主节点,则根据数据库中存储的集群各节点对应的节点信息,确定是否以所述目标节点作为主节点;若以所述目标节点作为主节点,则向所述数据库发送将所述目标节点标记为主节点的第一信息,或者若接收到将集群中的目标节点标记为主节点的第一信息,则在所述目标节点的节点信息中添加主节点的标记。Furthermore, the embodiment of the present invention discloses a computer program product, the computer program product includes a computer program stored on a non-transitory readable storage medium, the computer program includes program instructions, when the program instructions are executed by the computer , the computer can execute the methods provided by the above method embodiments, for example, including: if any target node in the cluster judges that there is no master node currently, then according to the node information corresponding to each node of the cluster stored in the database, determine whether to use the The target node is used as the master node; if the target node is used as the master node, then the first information to mark the target node as the master node is sent to the database, or if the target node in the cluster is marked as the master node If the first information of the node is the first information of the target node, the mark of the master node is added to the node information of the target node.

另一方面,本发明实施例还提供一种非暂态可读存储介质,其上存储有计算机程序,该计算机程序被处理器执行时实现以执行上述各实施例提供的传输方法,例如包括:集群中任一目标节点若判断当前不存在主节点,则根据数据库中存储的集群各节点对应的节点信息,确定是否以所述目标节点作为主节点;若以所述目标节点作为主节点,则向所述数据库发送将所述目标节点标记为主节点的第一信息,或者若接收到将集群中的目标节点标记为主节点的第一信息,则在所述目标节点的节点信息中添加主节点的标记。On the other hand, an embodiment of the present invention also provides a non-transitory readable storage medium, on which a computer program is stored, and when the computer program is executed by a processor, the transmission method provided by the above-mentioned embodiments is implemented, for example, including: If any target node in the cluster judges that there is currently no master node, then determine whether to use the target node as the master node according to the node information corresponding to each node of the cluster stored in the database; if the target node is used as the master node, then Sending the first information marking the target node as the master node to the database, or if receiving the first information marking the target node in the cluster as the master node, adding the master node to the node information of the target node The label of the node.

以上所描述的装置实施例仅仅是示意性的,其中所述作为分离部件说明的单元可以是或者也可以不是物理上分开的,作为单元显示的部件可以是或者也可以不是物理单元,即可以位于一个地方,或者也可以分布到多个网络单元上。可以根据实际的需要选择其中的部分或者全部模块来实现本实施例方案的目的。本领域普通技术人员在不付出创造性的劳动的情况下,即可以理解并实施。The device embodiments described above are only illustrative, and the units described as separate components may or may not be physically separated, and the components shown as units may or may not be physical units, that is, they may be located in One place, or it can be distributed to multiple network elements. Part or all of the modules can be selected according to actual needs to achieve the purpose of the solution of this embodiment. It can be understood and implemented by those skilled in the art without any creative efforts.

通过以上的实施方式的描述,本领域的技术人员可以清楚地了解到各实施方式可借助软件加必需的通用硬件平台的方式来实现,当然也可以通过硬件。基于这样的理解,上述技术方案本质上或者说对现有技术做出贡献的部分可以以软件产品的形式体现出来,该计算机软件产品可以存储在可读存储介质中,如ROM/RAM、磁碟、光盘等,包括若干指令用以使得一台计算机设备(可以是个人计算机,服务器,或者网络设备等)执行各个实施例或者实施例的某些部分所述的方法。Through the above description of the implementations, those skilled in the art can clearly understand that each implementation can be implemented by means of software plus a necessary general-purpose hardware platform, and of course also by hardware. Based on this understanding, the essence of the above technical solution or the part that contributes to the prior art can be embodied in the form of software products, and the computer software products can be stored in readable storage media, such as ROM/RAM, disk , CD, etc., including several instructions to make a computer device (which may be a personal computer, server, or network device, etc.) execute the methods described in each embodiment or some parts of the embodiments.

最后应说明的是:以上实施例仅用以说明本发明的技术方案,而非对其限制;尽管参照前述实施例对本发明进行了详细的说明,本领域的普通技术人员应当理解:其依然可以对前述各实施例所记载的技术方案进行修改,或者对其中部分技术特征进行等同替换;而这些修改或者替换,并不使相应技术方案的本质脱离本发明各实施例技术方案的精神和范围。Finally, it should be noted that: the above embodiments are only used to illustrate the technical solutions of the present invention, rather than to limit them; although the present invention has been described in detail with reference to the foregoing embodiments, those of ordinary skill in the art should understand that: it can still be Modifications are made to the technical solutions described in the foregoing embodiments, or equivalent replacements are made to some of the technical features; and these modifications or replacements do not make the essence of the corresponding technical solutions deviate from the spirit and scope of the technical solutions of the various embodiments of the present invention.

Claims (9)

1. A method for determining master nodes in a cluster, comprising:
if any target node in the cluster judges that the master node does not exist currently, determining whether the target node is used as the master node according to node information corresponding to each node of the cluster stored in a database; the database is used for storing node information and heartbeat time points sent by the nodes and sending information needed by the nodes to the nodes;
if the target node is used as a main node, first information for marking the target node as the main node is sent to the database;
judging whether an abnormal node exists according to the heartbeat time points periodically reported by all the nodes in the database, wherein the abnormal node is a node with a second time length longer than a preset time length between the latest reported heartbeat time point and the current time point;
and if the abnormal node exists, sending second information for deleting the node information of the abnormal node to the database.
2. The method for determining a master node in a cluster according to claim 1, wherein determining whether to use the target node as the master node according to node information corresponding to each node of the cluster stored in a database includes:
determining election time points corresponding to the nodes according to the node information, and determining target election time points according to first time intervals between the election time points and the current time points;
if the node corresponding to the target election time point is the target node, the target node is used as a main node;
the election time point is a time point when the node executes a preset instruction, and the preset instruction comprises storing node information corresponding to the node into the database or initializing the node.
3. The method of determining a master node in a cluster of claim 2, further comprising:
generating a time modification instruction, wherein the time modification instruction is used for modifying an election time point corresponding to a node in the database;
and sending the time modification instruction to the database so that the database modifies the election time point corresponding to the node according to the time modification instruction.
4. The method of determining a master node in a cluster of claim 1, further comprising:
judging whether node information marked as a master node exists according to the node information of each node currently stored in the database, and if not, judging that the master node does not exist in each node of the cluster currently.
5. The method for determining a master node in a cluster according to claim 2, wherein determining an election time point corresponding to each node according to the node information, and determining a target election time point according to a first time interval between each election time point and a current time point, includes:
determining a preset instruction corresponding to the number of times that the main node does not exist at present according to the preset corresponding relation, and taking the preset instruction as a target preset instruction;
and determining a target election time point according to the target preset instruction and the node information, and the election time point corresponding to each node according to the first time interval between each election time point and the current time point.
6. A method for determining master nodes in a cluster, comprising:
the database is used for storing node information and heartbeat time points sent by the nodes and sending information needed by the nodes to the nodes;
if first information of marking a target node in a cluster as a main node is received, adding the mark of the main node into the node information of the target node;
if node information sent by the nodes and/or heartbeat time points periodically reported by the nodes are received, storing the received node information and/or the heartbeat time points;
and if the second information for deleting the node information of the abnormal node is received, deleting the node information of the abnormal node.
7. The method of determining master nodes in a cluster of claim 6, further comprising:
and if the time modification instruction is received, modifying the election time point corresponding to the node according to the time modification instruction.
8. An electronic device comprising a memory, a processor and a computer program stored on the memory and executable on the processor, characterized in that the processor implements the steps of the method of determining master nodes in a cluster according to any of claims 1 to 7 when the program is executed.
9. A non-transitory readable storage medium having stored thereon a computer program, which when executed by a processor, implements the steps of the method of determining master nodes in a cluster according to any of claims 1 to 7.
CN202010335432.3A 2020-04-24 2020-04-24 Method for determining master node in cluster, electronic equipment and storage medium Active CN111538763B (en)

Priority Applications (1)

Application Number Priority Date Filing Date Title
CN202010335432.3A CN111538763B (en) 2020-04-24 2020-04-24 Method for determining master node in cluster, electronic equipment and storage medium

Applications Claiming Priority (1)

Application Number Priority Date Filing Date Title
CN202010335432.3A CN111538763B (en) 2020-04-24 2020-04-24 Method for determining master node in cluster, electronic equipment and storage medium

Publications (2)

Publication Number Publication Date
CN111538763A CN111538763A (en) 2020-08-14
CN111538763B true CN111538763B (en) 2023-08-15

Family

ID=71967566

Family Applications (1)

Application Number Title Priority Date Filing Date
CN202010335432.3A Active CN111538763B (en) 2020-04-24 2020-04-24 Method for determining master node in cluster, electronic equipment and storage medium

Country Status (1)

Country Link
CN (1) CN111538763B (en)

Families Citing this family (12)

* Cited by examiner, † Cited by third party
Publication number Priority date Publication date Assignee Title
CN112003755B (en) * 2020-08-25 2022-12-23 北京浪潮数据技术有限公司 Method, device, equipment and medium for selecting cluster master nodes
CN112035326A (en) * 2020-09-03 2020-12-04 中国银行股份有限公司 Abnormal node task processing method and device based on cluster node mutual detection
CN112286738B (en) * 2020-11-19 2024-01-26 政采云有限公司 A method, system, device and readable storage medium for database access
CN113220464B (en) * 2021-05-31 2025-05-02 平安科技(深圳)有限公司 Distributed application method, device, computer equipment, and storage medium
CN113922988B (en) * 2021-09-16 2023-07-18 苏州浪潮智能科技有限公司 A network-based host security policy detection method and system
CN114422521B (en) * 2022-01-12 2024-04-05 北京京东振世信息技术有限公司 Method and device for determining master node, electronic equipment and storage medium
CN115499447B (en) * 2022-09-15 2024-10-29 北京天融信网络安全技术有限公司 Cluster master node confirmation method and device, electronic equipment and storage medium
CN115329151B (en) * 2022-10-17 2023-03-14 北方健康医疗大数据科技有限公司 Graph database optimization method and device, electronic equipment and storage medium
CN116303279A (en) * 2023-02-08 2023-06-23 北京思特奇信息技术股份有限公司 Concurrent file acquisition method, concurrent file acquisition device, concurrent file acquisition equipment and storage medium
CN115811520B (en) * 2023-02-08 2023-04-07 天翼云科技有限公司 Election method, device and electronic equipment for master node in distributed system
CN116319789A (en) * 2023-03-01 2023-06-23 中国建设银行股份有限公司 Distributed cluster master node election method, device and computer equipment
CN119128018B (en) * 2024-11-08 2025-03-11 宁波菊风系统软件有限公司 Master-slave node election method, device, equipment and storage medium in distributed system

Citations (15)

* Cited by examiner, † Cited by third party
Publication number Priority date Publication date Assignee Title
US6199169B1 (en) * 1998-03-31 2001-03-06 Compaq Computer Corporation System and method for synchronizing time across a computer cluster
CN101754271A (en) * 2008-12-09 2010-06-23 华为终端有限公司 Method and device for negotiation of master node
CN102904752A (en) * 2012-09-25 2013-01-30 新浪网技术(中国)有限公司 Node selection method, node equipment and node system
CN104679796A (en) * 2013-12-03 2015-06-03 方正信息产业控股有限公司 Selecting method, selecting device and database mirror image cluster node
CN104933132A (en) * 2015-06-12 2015-09-23 广州巨杉软件开发有限公司 Distributed database weighted voting method based on operating sequence number
CN106911524A (en) * 2017-04-27 2017-06-30 紫光华山信息技术有限公司 A kind of HA implementation methods and device
CN107257332A (en) * 2011-12-13 2017-10-17 迈克菲公司 Time control in large-scale firewall cluster
CN107800737A (en) * 2016-09-05 2018-03-13 中国移动通信有限公司研究院 The determination method, apparatus and server cluster of host node in a kind of server cluster
CN108780346A (en) * 2015-11-16 2018-11-09 东芝存储器株式会社 Host equipment and expanding unit
CN109379238A (en) * 2018-12-14 2019-02-22 郑州云海信息技术有限公司 A CTDB master node election method, device and system for distributed cluster
CN109951331A (en) * 2019-03-15 2019-06-28 北京百度网讯科技有限公司 Method, apparatus and computing cluster for sending information
CN109995835A (en) * 2017-12-29 2019-07-09 浙江宇视科技有限公司 Host node electoral machinery, device and distributed memory system
CN110062027A (en) * 2019-03-18 2019-07-26 中科恒运股份有限公司 Load-balancing method, central server and terminal device
CN110300023A (en) * 2019-06-28 2019-10-01 上海智臻智能网络科技股份有限公司 A kind of state switching method, device, node, node group and storage medium
CN110431533A (en) * 2016-12-30 2019-11-08 华为技术有限公司 Method, device and system for failure recovery

Patent Citations (15)

* Cited by examiner, † Cited by third party
Publication number Priority date Publication date Assignee Title
US6199169B1 (en) * 1998-03-31 2001-03-06 Compaq Computer Corporation System and method for synchronizing time across a computer cluster
CN101754271A (en) * 2008-12-09 2010-06-23 华为终端有限公司 Method and device for negotiation of master node
CN107257332A (en) * 2011-12-13 2017-10-17 迈克菲公司 Time control in large-scale firewall cluster
CN102904752A (en) * 2012-09-25 2013-01-30 新浪网技术(中国)有限公司 Node selection method, node equipment and node system
CN104679796A (en) * 2013-12-03 2015-06-03 方正信息产业控股有限公司 Selecting method, selecting device and database mirror image cluster node
CN104933132A (en) * 2015-06-12 2015-09-23 广州巨杉软件开发有限公司 Distributed database weighted voting method based on operating sequence number
CN108780346A (en) * 2015-11-16 2018-11-09 东芝存储器株式会社 Host equipment and expanding unit
CN107800737A (en) * 2016-09-05 2018-03-13 中国移动通信有限公司研究院 The determination method, apparatus and server cluster of host node in a kind of server cluster
CN110431533A (en) * 2016-12-30 2019-11-08 华为技术有限公司 Method, device and system for failure recovery
CN106911524A (en) * 2017-04-27 2017-06-30 紫光华山信息技术有限公司 A kind of HA implementation methods and device
CN109995835A (en) * 2017-12-29 2019-07-09 浙江宇视科技有限公司 Host node electoral machinery, device and distributed memory system
CN109379238A (en) * 2018-12-14 2019-02-22 郑州云海信息技术有限公司 A CTDB master node election method, device and system for distributed cluster
CN109951331A (en) * 2019-03-15 2019-06-28 北京百度网讯科技有限公司 Method, apparatus and computing cluster for sending information
CN110062027A (en) * 2019-03-18 2019-07-26 中科恒运股份有限公司 Load-balancing method, central server and terminal device
CN110300023A (en) * 2019-06-28 2019-10-01 上海智臻智能网络科技股份有限公司 A kind of state switching method, device, node, node group and storage medium

Non-Patent Citations (1)

* Cited by examiner, † Cited by third party
Title
Effect of network topology on the controllability of voter model dynamics using biased nodes;Aravinda R. Srinivasan;《2014 American Control Conference》;全文 *

Also Published As

Publication number Publication date
CN111538763A (en) 2020-08-14

Similar Documents

Publication Publication Date Title
CN111538763B (en) Method for determining master node in cluster, electronic equipment and storage medium
US11899684B2 (en) System and method for maintaining a master replica for reads and writes in a data store
US12316489B2 (en) System and method for data replication using a single master failover protocol
US10929240B2 (en) System and method for adjusting membership of a data replication group
US9411873B2 (en) System and method for splitting a replicated data partition
US9489434B1 (en) System and method for replication log branching avoidance using post-failover rejoin
US8719225B1 (en) System and method for log conflict detection and resolution in a data store
US9367261B2 (en) Computer system, data management method and data management program
CN102012944B (en) Distributed NOSQL (not only structured query language) database capable of providing replication property
US11895185B2 (en) Node synchronization method and apparatus, device and storage medium
CN104793981B (en) A kind of online snapshot management method and device of cluster virtual machine
CN106331081B (en) A kind of information synchronization method and device
US11397632B2 (en) Safely recovering workloads within a finite timeframe from unhealthy cluster nodes
CN108509296B (en) Method and system for processing equipment fault
CN110351107B (en) Configuration management method and device
CN116346834A (en) Session synchronization method, device, computing equipment and computer storage medium
US11757733B2 (en) Parallel service invocation in a network
CN104506429A (en) Router backup set managing method, router and system
CN116132277A (en) Subnet configuration method, device, computer equipment and storage medium
CN115242820A (en) A cluster node fault processing method, device, equipment and medium

Legal Events

Date Code Title Description
PB01 Publication
PB01 Publication
SE01 Entry into force of request for substantive examination
SE01 Entry into force of request for substantive examination
GR01 Patent grant
GR01 Patent grant