CN112954008B - Distributed task processing method and device, electronic equipment and storage medium - Google Patents

Distributed task processing method and device, electronic equipment and storage medium Download PDF

Info

Publication number
CN112954008B
CN112954008B CN202110105312.9A CN202110105312A CN112954008B CN 112954008 B CN112954008 B CN 112954008B CN 202110105312 A CN202110105312 A CN 202110105312A CN 112954008 B CN112954008 B CN 112954008B
Authority
CN
China
Prior art keywords
node
value pair
key value
redis database
control node
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
CN202110105312.9A
Other languages
Chinese (zh)
Other versions
CN112954008A (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.)
Wangsu Science and Technology Co Ltd
Original Assignee
Wangsu Science and 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 Wangsu Science and Technology Co Ltd filed Critical Wangsu Science and Technology Co Ltd
Priority to CN202110105312.9A priority Critical patent/CN112954008B/en
Publication of CN112954008A publication Critical patent/CN112954008A/en
Application granted granted Critical
Publication of CN112954008B publication Critical patent/CN112954008B/en
Active legal-status Critical Current
Anticipated expiration legal-status Critical

Links

Images

Classifications

    • HELECTRICITY
    • H04ELECTRIC COMMUNICATION TECHNIQUE
    • H04LTRANSMISSION OF DIGITAL INFORMATION, e.g. TELEGRAPHIC COMMUNICATION
    • H04L67/00Network arrangements or protocols for supporting network services or applications
    • H04L67/01Protocols
    • H04L67/10Protocols in which an application is distributed across nodes in the network
    • HELECTRICITY
    • H04ELECTRIC COMMUNICATION TECHNIQUE
    • H04LTRANSMISSION OF DIGITAL INFORMATION, e.g. TELEGRAPHIC COMMUNICATION
    • H04L67/00Network arrangements or protocols for supporting network services or applications
    • H04L67/01Protocols
    • H04L67/02Protocols based on web technology, e.g. hypertext transfer protocol [HTTP]
    • HELECTRICITY
    • H04ELECTRIC COMMUNICATION TECHNIQUE
    • H04LTRANSMISSION OF DIGITAL INFORMATION, e.g. TELEGRAPHIC COMMUNICATION
    • H04L67/00Network arrangements or protocols for supporting network services or applications
    • H04L67/01Protocols
    • H04L67/10Protocols in which an application is distributed across nodes in the network
    • H04L67/1001Protocols in which an application is distributed across nodes in the network for accessing one among a plurality of replicated servers
    • H04L67/1034Reaction to server failures by a load balancer
    • HELECTRICITY
    • H04ELECTRIC COMMUNICATION TECHNIQUE
    • H04LTRANSMISSION OF DIGITAL INFORMATION, e.g. TELEGRAPHIC COMMUNICATION
    • H04L67/00Network arrangements or protocols for supporting network services or applications
    • H04L67/50Network services
    • H04L67/56Provisioning of proxy services
    • H04L67/568Storing data temporarily at an intermediate stage, e.g. caching

Landscapes

  • Engineering & Computer Science (AREA)
  • Computer Networks & Wireless Communication (AREA)
  • Signal Processing (AREA)
  • Information Retrieval, Db Structures And Fs Structures Therefor (AREA)

Abstract

The invention discloses a distributed task processing method, a distributed task processing device, electronic equipment and a storage medium, which are used for solving the problem that distributed tasks cannot be normally completed if server nodes in a distributed system are abnormal when the existing distributed system performs distributed task processing, wherein the distributed task processing method is applied to the distributed system, the distributed system comprises at least two nodes, each node caches the tasks to be processed by adopting the same Redis database, and the distributed task processing method comprises the following steps: any node in the distributed system sends a competition request message to a Redis database; when the competition success message returned by the Redis database is determined to be received, determining that the Redis database is a main node; when determining that a competition failure message returned by the Redis database is received, determining that the Redis database is a control node; when the control node is a main node, when the control node in the distributed system is determined to be abnormal, the task to be processed in the control node is obtained for processing.

Description

Distributed task processing method and device, electronic equipment and storage medium
Technical Field
The present invention relates to the field of computer application technologies, and in particular, to a distributed task processing method and apparatus, an electronic device, and a storage medium.
Background
In internet applications, more and more enterprises and organizations adopt distributed systems (or clusters) to provide various services, and when a batch task is processed, the batch task needs to be distributed to all server nodes in the distributed systems to be completed together, however, when a server node in the distributed systems is abnormal, the tasks on the abnormal server nodes are lost, and the batch task cannot be completed normally.
Disclosure of Invention
In order to solve the problem that when an existing distributed system performs distributed task processing, if a server node in the distributed system is abnormal, the distributed task cannot be completed normally, embodiments of the present invention provide a distributed task processing method and apparatus, an electronic device, and a storage medium.
In a first aspect, an embodiment of the present invention provides a distributed task processing method, which is applied to a distributed system, where the distributed system includes at least two nodes, each node caches a task to be processed by using a same remote dictionary service Redis database, where:
any node in the distributed system sends a competition request message to the Redis database;
when the competition success message returned by the Redis database is determined to be received, determining that the Redis database is a main node; when determining that the competition failure message returned by the Redis database is received, determining that the Redis database is a control node;
when the control node is a main node, when the control node in the distributed system is determined to be abnormal, the task to be processed in the control node is obtained for processing.
The distributed task processing method provided by the embodiment of the invention is applied to a distributed system comprising at least two nodes, each node caches a task to be processed by adopting the same Redis database, any node in the distributed system sends a competition request message to the Redis database, if any node receives a competition success message returned by the Redis database, the node is determined to be a master node, if a competition failure message returned by the Redis database is received, the node is determined to be a control node, and when any node is the master node, the control node in the distributed system is determined to be abnormal, the task to be processed in the abnormal control node is obtained to be processed.
Preferably, any node sends a contention request message to the Redis database, which specifically includes:
setting a designated key value pair by any node by utilizing an SETnx command of a Redis database;
determining that the competition success message returned by the Redis database is received, specifically including:
when the received return value is determined to be the first parameter value, determining that a competition success message returned by the Redis database is received;
determining that the competition failure message returned by the Redis database is received, specifically including:
and when the received return value is determined to be the second parameter value, determining that a competition failure message returned by the Redis database is received.
In the above preferred embodiment, any node in the distributed system competes for the master node through SETnx (SET if Not ) command of the Redis database, the Redis database is a high-performance Key-Value database, the SETnx command is used to SET a specified Value (Value) for the Key when the specified Key does Not exist, if the Key already eXists, the SETnx command does Not perform any action, the return Value is the first parameter Value, the setting is successful, the return Value is the second parameter Value, the setting is failed, in the embodiment of the present invention, each node SETs the same specified Key Value pair by using the SETnx command, the node whose received return Value is the first parameter Value is determined as the node, once one node SETs the specified Key Value pair successfully, the other nodes cannot SET the specified Key Value pair successfully, the return values received by the other nodes are the second parameter Value, the node whose return Value is the second parameter Value is determined as the control node, and the master node can be controlled more effectively by competing for only one master node.
Optionally, when it is determined that any node is a master node, the method further includes:
creating a first key value pair and a second key value pair, setting a value in the first key value pair as the main node identification, setting a value in the second key value pair as each control node identification, and setting the expiration time of the first key value pair through an exit command of a Redis database;
and updating the expiration time of the first key value pair according to a first preset time period, wherein the duration corresponding to the first preset time period is less than the duration corresponding to the expiration time of the first key value pair.
In the above optional embodiment, when it is determined that any node is the master node, the master node creates a first key-value pair and a second key-value pair, sets a value in the first key-value pair as the master node identifier, sets a value in the second key-value pair as each control node identifier, sets expiration time of the first key-value pair by using an exit command of the Redis database, and updates the expiration time of the first key-value pair by using the expiration time smaller than the expiration time of the first key-value pair as a period, so as to ensure that the set first key-value pair is not automatically deleted by the Redis database.
Optionally, when it is determined that any node is a control node, the method further includes:
creating a third key value pair and a fourth key value pair, setting the value in the third key value pair as the host name of the control node, setting the expiration time of the third key value pair, and setting the value in the fourth key value pair as the task identifier to be processed of the control node;
and updating the expiration time of the third key value pair according to a second preset time period, wherein the duration corresponding to the second preset time period is less than the duration corresponding to the expiration time of the third key value pair.
In the above optional embodiment, when any node is a control node, the control node creates a third key value pair and a fourth key value pair, sets a value in the third key value pair as a host name of the control node, sets an expiration time of the third key value pair, and sets a value in the fourth key value pair as a to-be-processed task identifier of the control node, so that the master node determines whether each control node is abnormal by obtaining the third key value pair of each control node, and if the control node is abnormal, the master node may obtain the to-be-processed task identifier of the abnormal control node from the fourth key value pair of the abnormal control node, and further obtain a to-be-processed task according to the to-be-processed task identifier to process the to-be-processed task, thereby implementing takeover of the abnormal control node.
And each control node updates the expiration time of the corresponding third key value pair by taking the expiration time less than the respective set third key value pair as a period, so as to ensure that the respective set third key value pair is not automatically deleted by the Redis database, if the control node is abnormal, the expiration time of the third key value pair cannot be updated, and when the expiration time arrives, the third key value pair is cleared by the Redis database, so that the master node can judge whether each control node is abnormal according to whether the third key value pair of each control node exists or not.
Preferably, the determining that the control node in the distributed system is abnormal specifically includes:
for each control node, scanning a third key value pair created by the control node in the Redis database according to a third preset time period;
and when the third key value pair created by the control node is not scanned, determining that the control node is abnormal.
Preferably, the acquiring the task to be processed in the control node for processing includes:
acquiring a fourth key value pair created by the abnormal control node;
acquiring the unprocessed task identifier to be processed of the abnormal control node from the fourth key value pair;
and acquiring and processing the corresponding to-be-processed task according to the unprocessed to-be-processed task identifier.
Optionally, before any node in the distributed system sends a contention request message to the Redis database, the method further includes:
determining that a master node in the distributed system is down.
In the above optional embodiment, each node in the distributed system sends a contention request message to the Redis database to contend between the master nodes, it is further required to determine that the master node in the distributed system has failed, and if the master node has not failed, it is not required to contend for the master node again, so as to ensure the validity of the master node and ensure the normal execution of the management mechanism.
Preferably, determining that a master node in the distributed system is failed specifically includes:
scanning a first key-value pair created by the master node in the Redis database according to a fourth preset time period;
and when the first key value pair created by the main node is determined not to be scanned, judging that the main node in the distributed system is invalid.
In the above preferred embodiment, each node scans, according to a fourth preset time period, the first key-value pair created by the host node in the Redis database, and if the first key-value pair created by the host node is not scanned, it indicates that the first key-value pair has been automatically cleared by the Redis database, and it is determined that the host node in the distributed system has failed.
In a second aspect, an embodiment of the present invention provides a distributed task processing apparatus, which is applied to a distributed system, where the distributed system includes at least two nodes, and each node caches a task to be processed by using a same remote dictionary service Redis database, and the apparatus includes:
a sending unit, configured to send a contention request message to the Redis database by any node in the distributed system;
the first determining unit is used for determining that the first determining unit is the main node when the competition success message returned by the Redis database is determined to be received; when determining that the competition failure message returned by the Redis database is received, determining that the Redis database is a control node;
and the processing unit is used for acquiring a task to be processed in the control node for processing when the control node in the distributed system is determined to be abnormal when the processing unit is a main node.
Preferably, the sending unit is specifically configured to set the specified key-value pair by using an SETnx command of the Redis database;
the first determining unit is specifically configured to determine that a competition success message returned by the Redis database is received when it is determined that the received return value is the first parameter value; and when the received return value is determined to be the second parameter value, determining that a competition failure message returned by the Redis database is received.
Optionally, the apparatus further comprises:
a first creating unit, configured to create a first key value pair and a second key value pair when it is determined that any node is a master node, set a value in the first key value pair as the master node identifier, set a value in the second key value pair as each control node identifier, and set expiration time of the first key value pair through an exit command of a Redis database;
and the first updating unit is used for updating the expiration time of the first key value pair according to a first preset time period, wherein the duration corresponding to the first preset time period is less than the duration corresponding to the expiration time of the first key value pair.
Optionally, the apparatus further comprises:
a second creating unit, configured to create a third key value pair and a fourth key value pair when it is determined that any node is a control node, set a value in the third key value pair as a host name of the control node, set expiration time of the third key value pair, and set a value in the fourth key value pair as a to-be-processed task identifier of the control node;
and the second updating unit is used for updating the expiration time of the third key value pair according to a second preset time period, wherein the duration corresponding to the second preset time period is less than the duration corresponding to the expiration time of the third key value pair.
Preferably, the processing unit is specifically configured to scan, for each control node, a third key value pair created by the control node in the Redis database according to a third preset time period; and when the third key value pair created by the control node is not scanned, determining that the control node is abnormal.
Preferably, the processing unit is specifically configured to obtain a fourth key value pair created by the control node with the exception; acquiring the unprocessed task identifier to be processed of the abnormal control node from the fourth key value pair; and acquiring and processing the corresponding to-be-processed task according to the unprocessed to-be-processed task identifier.
Optionally, the apparatus further comprises:
a second determining unit, configured to determine that a master node in the distributed system is invalid before any node in the distributed system sends a contention request message to the Redis database.
Preferably, the second determining unit is specifically configured to scan the first key-value pair created by the master node in the Redis database according to a fourth preset time period; and when the first key value pair created by the main node is determined not to be scanned, judging that the main node in the distributed system is invalid.
For technical effects of the distributed task processing apparatus provided by the present invention, reference may be made to the technical effects of the first aspect or each implementation manner of the first aspect, and details are not described here.
In a third aspect, an embodiment of the present invention provides an electronic device, which includes a memory, a processor, and a computer program stored in the memory and executable on the processor, where the processor implements the distributed task processing method according to the present invention when executing the computer program.
In a fourth aspect, an embodiment of the present invention provides a computer-readable storage medium, on which a computer program is stored, and the computer program, when executed by a processor, implements the steps in the distributed task processing method according to the present invention.
Additional features and advantages of the invention will be set forth in the description which follows, and in part will be obvious from the description, or may be learned by practice of the invention. The objectives and other advantages of the invention will be realized and attained by the structure particularly pointed out in the written description and claims hereof as well as the appended drawings.
Drawings
The accompanying drawings, which are included to provide a further understanding of the invention and are incorporated in and constitute a part of this specification, illustrate embodiments of the invention and together with the description serve to explain the invention and not to limit the invention. In the drawings:
fig. 1 is a schematic flowchart of an implementation of a distributed task processing method according to an embodiment of the present invention;
fig. 2 is a schematic diagram of an implementation flow of determining, by a master node, that a control node in a distributed system is abnormal according to an embodiment of the present invention;
fig. 3 is a schematic diagram of an implementation flow of processing a task to be processed in a control node where an exception is acquired by a master node according to an embodiment of the present invention;
fig. 4 is a schematic diagram of an implementation flow for determining a failure of a master node according to an embodiment of the present invention;
FIG. 5 is a schematic structural diagram of a distributed task processing apparatus according to an embodiment of the present invention;
fig. 6 is a schematic structural diagram of an electronic device according to an embodiment of the present invention.
Detailed Description
In order to solve the problem that when an existing distributed system performs distributed task processing, if a server node in the distributed system is abnormal, the distributed task cannot be completed normally, embodiments of the present invention provide a distributed task processing method and apparatus, an electronic device, and a storage medium.
The preferred embodiments of the present invention will be described below with reference to the accompanying drawings of the specification, it being understood that the preferred embodiments described herein are merely for illustrating and explaining the present invention, and are not intended to limit the present invention, and that the embodiments and features of the embodiments in the present invention may be combined with each other without conflict.
As shown in fig. 1, which is a schematic diagram of an implementation flow of a distributed task processing method according to an embodiment of the present invention, the distributed task processing method may be applied to a distributed system, where the distributed system includes at least two nodes, the nodes are connected through a network, and each node in the distributed system caches a task to be processed by using a same Redis database, where a node in the distributed system may be a server, and specifically may include the following steps:
s11, any node in the distributed system sends a competition request message to the Redis database.
In specific implementation, when a distributed system executes a batch task, the batch task is distributed to each node (namely, a node server) to be executed together, and when each node receives each task to be processed, each task to be processed is cached in a shared Redis database. And each node sends a competition request message to the Redis database before starting to execute respective tasks to be processed so as to carry out main node competition.
Specifically, each node in the distributed system respectively sets a Key-Value pair to compete for the master node by using an SETnx command of the Redis database, where the Key-Value pair may be any Key (Key) and Value (Value) agreed in advance by each node, which is not limited in the embodiment of the present invention.
S12, when the competition success message returned by the Redis database is determined to be received, determining that the Redis database is a main node; and when determining that the competition failure message returned by the Redis database is received, determining that the Redis database is a control node.
In specific implementation, after each node sets a designated key value pair by using an SETnx command, a return value of the Redis database is received, when the received return value is determined to be a first parameter value, a competition success message returned by the Redis database is determined to be received, a node receiving the competition success message returned by the Redis database is a master node, when the received return value is determined to be a second parameter value, a competition failure message returned by the Redis database is determined to be received, a node receiving the competition failure message returned by the Redis database is a control node, where the first parameter value is 1, the second parameter value is 0, that is, the received node with the return value of 1 is the master node, and the received node with the return value of 0 is the control node (where the control node is a slave node).
Further, after the master node and the control node are determined, each node initializes cache data in a respective Redis database, and the specific process is as follows:
when any node is determined to be the master node, the master node creates a first key-value pair and a second key-value pair, sets a value in the first key-value pair as the master node identifier (id), is used for storing the master node identifier information, and sets an expiration time of the first key-value pair through an Expire command of the Redis database, that is, the Expire value, which may be set to 60s, for example, which is not limited in the embodiment of the present invention. The master node sets the value in the second key value pair as each control node identifier for storing the identifier information of each control node, and in the specific implementation process, all the node identifiers in the distributed system can be stored in advance in the Redis database, and when the master node successfully competes, other control node identifiers can be directly obtained in the Redis database, and each control node identifier is set as the value in the second key value pair.
Further, after the master node creates the first key-value pair and the second key-value pair, the master node updates expiration time of the first key-value pair according to a first preset time period, wherein duration corresponding to the first preset time period is less than duration corresponding to the expiration time of the first key-value pair, so as to ensure that the first key-value pair is not automatically deleted by the Redis database. For example, assuming that the expiration time of the first key-value pair is 60 seconds, the first preset time period may be set to 30 seconds, and the expiration time of the first key-value pair is updated every 30 seconds, which is not limited in the embodiment of the present invention.
In specific implementation, the TTL (Time To Live) command of the Redis database may be used To query the remaining expiration Time of the Key.
When any node is determined to be a control node, any control node acquires the first key value pair, creates a third key value pair and a fourth key value pair, sets the value in the third key value pair as the host name (Hostname, namely the name of a node server corresponding to the control node), sets the expiration time of the third key value pair by the control node by using an Expire command of a Redis database, and sets the value in the fourth key value pair as the to-be-processed task identifier of the control node.
In specific implementation, each control node may send a first key-value pair acquisition request to each node except for itself, and after receiving the request, the host node returns the created first key-value pair to the corresponding control node, where each control node may send the first key-value pair acquisition request by using a GET method in HTTP (HyperText Transfer Protocol), or may actively send the first key-value pair to each control node after the host node creates the first key-value pair, which is not limited in the embodiment of the present invention.
In a specific implementation, names of keys (keys) in the first, second, third, and fourth Key value pairs may be named according to an application name (appname) corresponding to the batch of tasks, which is not limited in the embodiment of the present invention. For example, the master node may set the name of the Key in the first Key value pair to: the master node identifier comprises { appname }: ctrl: master, and the master node identifier is set to be the Value (Value) of { appname }: ctrl: master; the master node may set the name of the Key of the second Key-value pair to: manager, setting the value of { appname }, ctrl: manager as each control node identifier; each control node may set the name of the Key in the respective third Key value pair to: { appname }: ctrl: { ctrl }, setting { appname }: the value of { ctrl } is the respective host name; each control node may set the names of keys in the respective fourth Key value pairs as: { appname }: ctrl: { ctrl }: manager, respectively setting their appnames: ctrl: { ctrl }: the value of manager is the identifier of each task to be processed, which is not limited in the embodiment of the present invention. In a specific implementation process, each control node may identify a processed task from a respective { appname }: ctrl: { ctrl }: remove from manager's value to update { appname }: ctrl: { ctrl }: record in manager.
Further, after any control node establishes a third key value pair and a fourth key value pair, the expiration time of the third key value pair is updated according to a second preset time period, and the duration corresponding to the second preset time period is less than the duration corresponding to the expiration time of the third key value pair.
In the embodiment of the invention, after each control node creates the corresponding third key value pair and fourth key value pair, each control node updates the expiration time of the corresponding third key value pair according to a second preset time period, wherein the duration corresponding to the second preset time period is less than the duration corresponding to the expiration time of the third key value pair, so as to ensure that the set third key value pair is not automatically deleted by a Redis database, if the control node is abnormal, the expiration time of the third key value pair cannot be updated, and when the expiration time is up, the third key value pair is cleared by the Redis database, so that the master node can judge whether each control node is abnormal according to whether the third key value pair of each control node exists.
S13, when the self is a main node, when the control node in the distributed system is determined to be abnormal, the task to be processed in the control node is obtained for processing.
In specific implementation, when any node is a master node, the master node may determine that an abnormality occurs in a control node in the distributed system according to the process shown in fig. 2, including the following steps:
s21, aiming at each control node, scanning a third key value pair created by the control node in a Redis database according to a third preset time period.
In specific implementation, for each control node, the master node acquires a third key value pair created by the control node according to a third preset time period, that is: and acquiring the third key value pair created by the control node once every time the time length corresponding to the third preset time period. The third preset time period may be set according to a requirement, which is not limited in the embodiment of the present invention.
Specifically, for each control node, the master node may actively scan, in the Redis database, the name of the Key in the third Key value pair created by the control node at a time length corresponding to a third preset time period (for example, scan the name of the Key in the third Key value pair of the control node: { appname }: ctrl: { ctrl }). The master node may further send a third key value pair obtaining request (e.g., GET { appname }: ctrl: { ctrl }) to the Redis database through a GET method in the HTTP at a time duration corresponding to each third preset time period, so as to obtain the third key value pair set by the control node, which is not limited in the embodiment of the present invention.
S22, when the third key value pair created by the control node is not scanned, determining that the control node is abnormal.
In specific implementation, if the name of a Key in the third Key value pair created by the control node is not scanned in a Redis database by the master node, the third Key value pair corresponding to the control node is not scanned, or the master node receives that a return value of the Redis database is null, and then it is determined that the control node is abnormal.
In an embodiment, the master node may scan, in a Redis database, names of keys in third Key value pairs created by all control nodes at a time corresponding to a third preset time period every time, record a scanned third Key value pair after each scanning, compare the currently scanned third Key value pair with the historically recorded third Key value pair, and determine that a control node corresponding to a value (i.e., a host name of the control node) in the third Key value pair existing in the historically recorded third Key value pair scanned each time and not existing in the currently scanned third Key value pair is abnormal.
Further, the step of acquiring, by the master node, the to-be-processed task in the abnormal control node for processing may include, as shown in fig. 3, an implementation flow diagram that the master node acquires the to-be-processed task in the abnormal control node for processing, and the implementation flow diagram may include the following steps:
and S31, the master node acquires a fourth key value pair created by the abnormal control node.
In specific implementation, for each abnormal control node, the host node may send a fourth key-value pair obtaining request to the Redis database by using a GET method in HTTP, so as to obtain a fourth key-value pair set by the abnormal control node.
And S32, the main node acquires the unprocessed task identifier to be processed of the abnormal control node from the fourth key value pair.
In specific implementation, the master node acquires the unprocessed to-be-processed task identifier from the fourth key value pair corresponding to the abnormal control node, wherein the unprocessed to-be-processed task comprises a task being processed and a task which is not yet processed.
And S33, the main node acquires and processes the corresponding to-be-processed task according to the unprocessed to-be-processed task identifier.
In specific implementation, the main node obtains the to-be-processed task corresponding to the unprocessed to-be-processed task identifier of the abnormal control node from the Redis database, and the main node processes the to-be-processed task corresponding to the unprocessed to-be-processed task identifier.
In one embodiment, before any node in the distributed system sends a contention request message to the Redis database, the method further includes:
determining that a master node in the distributed system is down.
In specific implementation, determining that the master node in the distributed system fails according to the process shown in fig. 4 may include the following steps:
and S41, scanning the first key-value pair created by the main node in the Redis database according to a fourth preset time period.
In specific implementation, when any node is a control node, the control node acquires a first key-value pair created by the master node according to a fourth preset time period, that is: and acquiring the first key value pair corresponding to the main node once every time corresponding to the fourth preset time period. The fourth preset time period may be set according to a requirement, which is not limited in the embodiment of the present invention.
Specifically, each control node may actively scan the first Key-value pair in the master node in the Redis database at a time length corresponding to a fourth preset time period every interval (for example, scan the name of Key in the first Key-value pair: { appname }: ctrl: master).
Each control node may also send a first key-value pair obtaining request (e.g., GET { appname }: ctrl: master) to the Redis database by a GET method in HTTP every time duration corresponding to a fourth preset time period, so as to obtain the first key-value pair set by the master node.
And S42, when the first key value pair created by the main node is determined not to be scanned, judging that the main node in the distributed system is invalid.
During specific implementation, if the control nodes do not scan the first key value pair created by the master node in the Redis database, or if the control nodes receive that the return value of the Redis database is null, the master node is determined to be invalid. When any node is the master node, the master node determines whether the first key-value pair established by the master node per se fails according to a fourth preset time period.
When the master node fails, each node re-competes for the master node according to steps S11-S12.
In the embodiment of the present invention, when a new node is added to the distributed system, the newly added node may determine whether a master node exists in the distributed system by obtaining a first key value pair (e.g., GET { appname }: ctrl: master) from the Redis database, and if a master node exists, the newly added node sends a node identifier to the master node, and the master node writes the node identifier into a value in a second key value pair (e.g., into a value corresponding to { appname }: ctrl: manager), and the newly added node creates a third key value pair and a fourth key value pair, in a manner of S12, which is not described herein again. If no master node exists in the distributed system, the newly added node calls the SETnx command to compete for the master node, see step S11.
To sum up, in the embodiment of the present invention, for the problem of node exception in the distributed system, each node in the distributed system shares the same Redis database to cache the task to be processed and the relevant information of the master node competition (for example, information such as the first key value pair and the second key value pair created by the master node, information such as the third key value pair and the fourth key value pair created by the control node, and the like), and the master node competition and the takeover of the master node to the abnormal node are realized by using the SETnx function and the TTL mechanism of the Redis database.
It should be noted that, the distributed task processing method provided in the embodiment of the present invention may also be applied to a cluster, which is not limited in this embodiment of the present invention.
The distributed task processing method provided by the embodiment of the invention is applied to a distributed system comprising at least two nodes, each node caches a task to be processed by adopting the same Redis database, any node in the distributed system sends a competition request message to the Redis database, if any node receives a competition success message returned by the Redis database, the node is determined to be a master node, if a competition failure message returned by the Redis database is received, the node is determined to be a control node, and when any node is the master node, the control node in the distributed system is determined to be abnormal, the task to be processed in the abnormal control node is obtained to be processed.
Based on the same inventive concept, embodiments of the present invention further provide a distributed task processing apparatus, and since the principle of the distributed task processing apparatus for solving the problem is similar to that of the distributed task processing method, the implementation of the apparatus may refer to the implementation of the method, and repeated details are not described herein.
As shown in fig. 5, which is a schematic structural diagram of a distributed task processing apparatus according to an embodiment of the present invention, the distributed task processing apparatus is applied to a distributed system, the distributed system includes at least two nodes, each node caches a task to be processed by using the same Redis database, and the apparatus includes:
a sending unit 51, configured to send a contention request message to the Redis database by any node in the distributed system;
a first determining unit 52, configured to determine that the first determining unit is a master node when determining that a contention success message returned by the Redis database is received; when determining that the competition failure message returned by the Redis database is received, determining that the Redis database is a control node;
and the processing unit 53 is configured to, when determining that a control node in the distributed system is abnormal, acquire a task to be processed in the control node and process the task.
Preferably, the sending unit 51 is specifically configured to set a specified key-value pair by using an SETnx command of a Redis database;
the first determining unit 52 is specifically configured to determine that a contention success message returned by the Redis database is received when it is determined that the received return value is the first parameter value; and when the received return value is determined to be the second parameter value, determining that a competition failure message returned by the Redis database is received.
Optionally, the apparatus further comprises:
a first creating unit, configured to create a first key value pair and a second key value pair when it is determined that any node is a master node, set a value in the first key value pair as the master node identifier, set a value in the second key value pair as each control node identifier, and set expiration time of the first key value pair through an exit command of a Redis database;
and the first updating unit is used for updating the expiration time of the first key value pair according to a first preset time period, wherein the duration corresponding to the first preset time period is less than the duration corresponding to the expiration time of the first key value pair.
Optionally, the apparatus further comprises:
a second creating unit, configured to create a third key value pair and a fourth key value pair when it is determined that any node is a control node, set a value in the third key value pair as a host name of the control node, set expiration time of the third key value pair, and set a value in the fourth key value pair as a to-be-processed task identifier of the control node;
and the second updating unit is used for updating the expiration time of the third key value pair according to a second preset time period, wherein the duration corresponding to the second preset time period is less than the duration corresponding to the expiration time of the third key value pair.
Preferably, the processing unit 53 is specifically configured to, for each control node, scan a third key value pair created by the control node in the Redis database according to a third preset time period; and when the third key value pair created by the control node is not scanned, determining that the control node is abnormal.
Preferably, the processing unit 53 is specifically configured to obtain a fourth key-value pair created by the control node with the exception; acquiring the unprocessed task identifier to be processed of the abnormal control node from the fourth key value pair; and acquiring and processing the corresponding to-be-processed task according to the unprocessed to-be-processed task identifier.
Optionally, the apparatus further comprises:
a second determining unit, configured to determine that a master node in the distributed system fails before any node in the distributed system sends a contention request message to the Redis database.
Preferably, the second determining unit is specifically configured to scan the first key-value pair created by the master node in the Redis database according to a fourth preset time period; and when the first key value pair created by the main node is determined not to be scanned, judging that the main node in the distributed system is invalid.
Based on the same technical concept, an embodiment of the present invention further provides an electronic device 600, and referring to fig. 6, the electronic device 600 is configured to implement the distributed task processing method described in the foregoing method embodiment, where the electronic device 600 of this embodiment may include: a memory 601, a processor 602, and a computer program, such as a distributed task processing program, stored in the memory and executable on the processor. The processor, when executing the computer program, implements the steps in each of the above embodiments of the distributed task processing method, such as step S11 shown in fig. 1. Alternatively, the processor, when executing the computer program, implements the functions of each module/unit in the above-described device embodiments, for example 51.
The embodiment of the present invention does not limit the specific connection medium between the memory 601 and the processor 602. In the embodiment of the present application, the memory 601 and the processor 602 are connected by a bus 603 in fig. 6, the bus 603 is represented by a thick line in fig. 6, and the connection manner between other components is merely illustrative and not limited thereto. The bus 603 may be divided into an address bus, a data bus, a control bus, etc. For ease of illustration, only one thick line is shown in FIG. 6, but this is not intended to represent only one bus or type of bus.
The memory 601 may be a volatile memory (volatile memory), such as a random-access memory (RAM); the memory 601 may also be a non-volatile memory (non-volatile memory) such as, but not limited to, a read-only memory (rom), a flash memory (flash memory), a Hard Disk Drive (HDD) or a solid-state drive (SSD), or the memory 601 may be any other medium that can be used to carry or store desired program code in the form of instructions or data structures and that can be accessed by a computer. The memory 601 may be a combination of the above memories.
The processor 602 is configured to implement a distributed task processing method shown in fig. 1, and includes:
the processor 602, configured to invoke the computer program stored in the memory 601 to execute step S11 shown in fig. 1, where any node in the distributed system sends a contention request message to the Redis database, and step S12, when determining that a contention success message returned by the Redis database is received, determines that the node is a master node; and S13, when determining that the competition failure message returned by the Redis database is received, determining that the control node is a control node, and when determining that the control node in the distributed system is abnormal, acquiring the task to be processed in the control node for processing when the control node is a main node.
The embodiment of the present application further provides a computer-readable storage medium, which stores computer-executable instructions required to be executed by the processor, and includes a program required to be executed by the processor.
In some possible embodiments, various aspects of the distributed task processing method provided by the present invention may also be implemented in the form of a program product, which includes program code for causing an electronic device to execute steps in the distributed task processing method according to various exemplary embodiments of the present invention described above in this specification when the program product runs on the electronic device, for example, the electronic device may execute step S11 shown in fig. 1, where any node in the distributed system sends a competition request message to a Redis database, and step S12, when it is determined that a competition success message returned by the Redis database is received, determines itself to be a master node; and S13, when determining that the competition failure message returned by the Redis database is received, determining that the control node is a control node, and when determining that the control node in the distributed system is abnormal, acquiring the task to be processed in the control node for processing when the control node is a main node.
As will be appreciated by one skilled in the art, embodiments of the present invention may be provided as a method, apparatus, or computer program product. Accordingly, the present invention may take the form of an entirely hardware embodiment, an entirely software embodiment or an embodiment combining software and hardware aspects. Furthermore, the present invention may take the form of a computer program product embodied on one or more computer-usable storage media (including, but not limited to, disk storage, CD-ROM, optical storage, and the like) having computer-usable program code embodied therein.
The present invention has been described with reference to flowchart illustrations and/or block diagrams of methods, apparatus (devices), and computer program products according to embodiments of the invention. It will be understood that each flow and/or block of the flow diagrams and/or block diagrams, and combinations of flows and/or blocks in the flow diagrams and/or block diagrams, can be implemented by computer program instructions. These computer program instructions may be provided to a processor of a general purpose computer, special purpose computer, embedded processor, or other programmable data processing apparatus to produce a machine, such that the instructions, which execute via the processor of the computer or other programmable data processing apparatus, create means for implementing the functions specified in the flowchart flow or flows and/or block diagram block or blocks.
These computer program instructions may also be stored in a computer-readable memory that can direct a computer or other programmable data processing apparatus to function in a particular manner, such that the instructions stored in the computer-readable memory produce an article of manufacture including instruction means which implement the function specified in the flowchart flow or flows and/or block diagram block or blocks.
These computer program instructions may also be loaded onto a computer or other programmable data processing apparatus to cause a series of operational steps to be performed on the computer or other programmable apparatus to produce a computer implemented process such that the instructions which execute on the computer or other programmable apparatus provide steps for implementing the functions specified in the flowchart flow or flows and/or block diagram block or blocks.
While preferred embodiments of the present invention have been described, additional variations and modifications in those embodiments may occur to those skilled in the art once they learn of the basic inventive concepts. Therefore, it is intended that the appended claims be interpreted as including preferred embodiments and all such alterations and modifications as fall within the scope of the invention.
It will be apparent to those skilled in the art that various changes and modifications may be made in the present invention without departing from the spirit and scope of the invention. Thus, if such modifications and variations of the present invention fall within the scope of the claims of the present invention and their equivalents, the present invention is also intended to include such modifications and variations.

Claims (14)

1. A distributed task processing method is applied to a distributed system, the distributed system comprises at least two nodes, each node caches a task to be processed by adopting the same remote dictionary service Redis database, and the method comprises the following steps:
any node in the distributed system sends a competition request message to the Redis database;
when the competition success message returned by the Redis database is determined to be received, determining that the Redis database is a main node; when the competition failure message returned by the Redis database is determined to be received, determining that the Redis database is a control node;
when the control node is a main node, acquiring a task to be processed in the control node for processing when the control node in the distributed system is determined to be abnormal;
before any node in the distributed system sends a contention request message to the Redis database, the method further includes:
determining that a master node in the distributed system is failed;
determining that the master node in the distributed system fails, specifically comprising:
scanning a first key-value pair created by the master node in the Redis database according to a fourth preset time period; and when determining that a first key value pair created by the main node is not scanned, determining that the main node in the distributed system is invalid, wherein a value in the first key value pair is the main node identifier, the expiration time of the first key value pair is updated by the main node according to a first preset time period, and the duration corresponding to the first preset time period is less than the duration corresponding to the expiration time of the first key value pair.
2. The method of claim 1, wherein any node sends a contention request message to the Redis database, specifically comprising:
setting a designated key value pair by any node by utilizing an SETnx command of a Redis database;
determining that the competition success message returned by the Redis database is received, specifically including:
when the received return value is determined to be the first parameter value, determining that a competition success message returned by the Redis database is received;
determining that the competition failure message returned by the Redis database is received, specifically including:
and when the received return value is determined to be the second parameter value, determining that a competition failure message returned by the Redis database is received.
3. The method of claim 2, wherein when it is determined that any of the nodes is a master node, further comprising:
creating a first key value pair and a second key value pair, setting a value in the first key value pair as the main node identification, and setting a value in the second key value pair as each control node identification; setting the expiration time of the first key-value pair through an Expire command of a Redis database;
and updating the expiration time of the first key-value pair according to the first preset time period.
4. The method of claim 3, wherein when it is determined that the any node is a control node, further comprising:
creating a third key value pair and a fourth key value pair, setting a value in the third key value pair as a host name of the control node, setting expiration time of the third key value pair, and setting a value in the fourth key value pair as a to-be-processed task identifier of the control node;
and updating the expiration time of the third key value pair according to a second preset time period, wherein the time length corresponding to the second preset time period is less than the time length corresponding to the expiration time of the third key value pair.
5. The method of claim 4, wherein determining that the control node in the distributed system is abnormal comprises:
for each control node, scanning a third key value pair created by the control node in the Redis database according to a third preset time period;
and when the third key value pair created by the control node is not scanned, determining that the control node is abnormal.
6. The method of claim 5, wherein obtaining the task to be processed in the control node for processing specifically comprises:
acquiring a fourth key value pair created by the abnormal control node;
acquiring the unprocessed task identifier to be processed of the abnormal control node from the fourth key value pair;
and acquiring and processing the corresponding to-be-processed task according to the unprocessed to-be-processed task identifier.
7. A distributed task processing device is applied to a distributed system, the distributed system comprises at least two nodes, each node adopts the same remote dictionary service Redis database to cache tasks to be processed, and the device comprises:
a sending unit, configured to send a contention request message to the Redis database by any node in the distributed system;
the first determining unit is used for determining that the first determining unit is the main node when the competition success message returned by the Redis database is determined to be received; when determining that the competition failure message returned by the Redis database is received, determining that the Redis database is a control node;
the processing unit is used for acquiring a task to be processed in the control node for processing when the control node in the distributed system is determined to be abnormal when the processing unit is a main node;
the device, still include:
a second determining unit, configured to determine that a master node in the distributed system fails before any node in the distributed system sends a contention request message to the Redis database;
the second determining unit is specifically configured to scan, in the Redis database, a first key-value pair created by the master node according to a fourth preset time period; and when the first key value pair created by the main node is determined not to be scanned, judging that the main node in the distributed system is invalid, updating the expiration time of the first key value pair by the main node according to a first preset time period, wherein the duration corresponding to the first preset time period is less than the duration corresponding to the expiration time of the first key value pair.
8. The apparatus of claim 7,
the sending unit is specifically configured to set a specified key-value pair by using an SETnx command of a Redis database;
the first determining unit is specifically configured to determine that a competition success message returned by the Redis database is received when it is determined that the received return value is the first parameter value; and when the received return value is determined to be the second parameter value, determining that a competition failure message returned by the Redis database is received.
9. The apparatus of claim 8, further comprising:
a first creating unit, configured to create a first key value pair and a second key value pair when it is determined that any node is a master node, set a value in the first key value pair as the master node identifier, set a value in the second key value pair as each control node identifier, and set expiration time of the first key value pair through an exit command of a Redis database;
and the first updating unit is used for updating the expiration time of the first key-value pair according to the first preset time period.
10. The apparatus of claim 9, further comprising:
a second creating unit, configured to create a third key value pair and a fourth key value pair when it is determined that any node is a control node, set a value in the third key value pair as a host name of the control node, set expiration time of the third key value pair, and set a value in the fourth key value pair as a to-be-processed task identifier of the control node;
and the second updating unit is used for updating the expiration time of the third key value pair according to a second preset time period, wherein the duration corresponding to the second preset time period is less than the duration corresponding to the expiration time of the third key value pair.
11. The apparatus of claim 10,
the processing unit is specifically configured to scan, for each control node, a third key value pair created by the control node in the Redis database according to a third preset time period; and when the third key value pair created by the control node is not scanned, determining that the control node is abnormal.
12. The apparatus of claim 11,
the processing unit is specifically configured to obtain a fourth key value pair created by the abnormal control node; acquiring the unprocessed task identifier to be processed of the abnormal control node from the fourth key value pair; and acquiring and processing the corresponding to-be-processed task according to the unprocessed to-be-processed task identifier.
13. An electronic device comprising a memory, a processor and a computer program stored on the memory and executable on the processor, wherein the processor implements the distributed task processing method according to any one of claims 1 to 6 when executing the program.
14. A computer-readable storage medium, on which a computer program is stored, which program, when being executed by a processor, carries out the steps of a distributed task processing method according to any one of claims 1 to 6.
CN202110105312.9A 2021-01-26 2021-01-26 Distributed task processing method and device, electronic equipment and storage medium Active CN112954008B (en)

Priority Applications (1)

Application Number Priority Date Filing Date Title
CN202110105312.9A CN112954008B (en) 2021-01-26 2021-01-26 Distributed task processing method and device, electronic equipment and storage medium

Applications Claiming Priority (1)

Application Number Priority Date Filing Date Title
CN202110105312.9A CN112954008B (en) 2021-01-26 2021-01-26 Distributed task processing method and device, electronic equipment and storage medium

Publications (2)

Publication Number Publication Date
CN112954008A CN112954008A (en) 2021-06-11
CN112954008B true CN112954008B (en) 2022-11-04

Family

ID=76237178

Family Applications (1)

Application Number Title Priority Date Filing Date
CN202110105312.9A Active CN112954008B (en) 2021-01-26 2021-01-26 Distributed task processing method and device, electronic equipment and storage medium

Country Status (1)

Country Link
CN (1) CN112954008B (en)

Families Citing this family (1)

* Cited by examiner, † Cited by third party
Publication number Priority date Publication date Assignee Title
CN116030398B (en) * 2023-03-31 2023-06-23 山东爱特云翔计算机有限公司 Task analysis method, system and storage medium based on multipath video signals

Citations (13)

* Cited by examiner, † Cited by third party
Publication number Priority date Publication date Assignee Title
CN102025550A (en) * 2010-12-20 2011-04-20 中兴通讯股份有限公司 System and method for managing data in distributed cluster
CN103199972A (en) * 2013-03-25 2013-07-10 成都瑞科电气有限公司 Double machine warm backup switching method and warm backup system achieved based on SOA and RS485 bus
CN103226483A (en) * 2013-03-25 2013-07-31 成都瑞科电气有限公司 DHBS (dual hot-backup system) and method thereof based on SOA (service-oriented architecture) and cloud storage
CN104598428A (en) * 2015-01-14 2015-05-06 深圳光启空间技术有限公司 Data processing system
CN104753994A (en) * 2013-12-27 2015-07-01 杭州海康威视系统技术有限公司 Method and device for data synchronization based on cluster server system
CN105162878A (en) * 2015-09-24 2015-12-16 网宿科技股份有限公司 Distributed storage based file distribution system and method
CN107315745A (en) * 2016-04-26 2017-11-03 北京京东尚科信息技术有限公司 A kind of personal letter storage method and system
CN108304255A (en) * 2017-12-29 2018-07-20 北京城市网邻信息技术有限公司 Distributed task dispatching method and device, electronic equipment and readable storage medium storing program for executing
CN109857527A (en) * 2019-01-04 2019-06-07 四川虹美智能科技有限公司 A kind of distributed task dispatching method, system, distributed devices and Redis database
CN109857563A (en) * 2019-02-18 2019-06-07 重庆西部汽车试验场管理有限公司 Task executing method, device and task execution system
WO2019127915A1 (en) * 2017-12-29 2019-07-04 北京奇虎科技有限公司 Distributed consensus protocol-based data reading method and apparatus
CN110457129A (en) * 2019-07-19 2019-11-15 深圳联友科技有限公司 A kind of priority lock method for pre-emptively and system based on zookeeper
CN111400112A (en) * 2020-03-18 2020-07-10 深圳市腾讯计算机系统有限公司 Writing method and device of storage system of distributed cluster and readable storage medium

Patent Citations (13)

* Cited by examiner, † Cited by third party
Publication number Priority date Publication date Assignee Title
CN102025550A (en) * 2010-12-20 2011-04-20 中兴通讯股份有限公司 System and method for managing data in distributed cluster
CN103199972A (en) * 2013-03-25 2013-07-10 成都瑞科电气有限公司 Double machine warm backup switching method and warm backup system achieved based on SOA and RS485 bus
CN103226483A (en) * 2013-03-25 2013-07-31 成都瑞科电气有限公司 DHBS (dual hot-backup system) and method thereof based on SOA (service-oriented architecture) and cloud storage
CN104753994A (en) * 2013-12-27 2015-07-01 杭州海康威视系统技术有限公司 Method and device for data synchronization based on cluster server system
CN104598428A (en) * 2015-01-14 2015-05-06 深圳光启空间技术有限公司 Data processing system
CN105162878A (en) * 2015-09-24 2015-12-16 网宿科技股份有限公司 Distributed storage based file distribution system and method
CN107315745A (en) * 2016-04-26 2017-11-03 北京京东尚科信息技术有限公司 A kind of personal letter storage method and system
CN108304255A (en) * 2017-12-29 2018-07-20 北京城市网邻信息技术有限公司 Distributed task dispatching method and device, electronic equipment and readable storage medium storing program for executing
WO2019127915A1 (en) * 2017-12-29 2019-07-04 北京奇虎科技有限公司 Distributed consensus protocol-based data reading method and apparatus
CN109857527A (en) * 2019-01-04 2019-06-07 四川虹美智能科技有限公司 A kind of distributed task dispatching method, system, distributed devices and Redis database
CN109857563A (en) * 2019-02-18 2019-06-07 重庆西部汽车试验场管理有限公司 Task executing method, device and task execution system
CN110457129A (en) * 2019-07-19 2019-11-15 深圳联友科技有限公司 A kind of priority lock method for pre-emptively and system based on zookeeper
CN111400112A (en) * 2020-03-18 2020-07-10 深圳市腾讯计算机系统有限公司 Writing method and device of storage system of distributed cluster and readable storage medium

Also Published As

Publication number Publication date
CN112954008A (en) 2021-06-11

Similar Documents

Publication Publication Date Title
JP6556851B2 (en) Database system, server device, program, and information processing method
CN108563502B (en) Task scheduling method and device
CN107612950B (en) Method, device and system for providing service and electronic equipment
CN110334126B (en) Timing task processing method and device based on Spring MVC and computer equipment
US6487678B1 (en) Recovery procedure for a dynamically reconfigured quorum group of processors in a distributed computing system
WO2019242455A1 (en) Method and apparatus for user request forwarding, reverse proxy and computer readable storage medium
CN110995481A (en) Configuration method, server and computer-readable storage medium
CN111352943A (en) Method and device for realizing data consistency, server and terminal
CN107688489B (en) Method and system for scheduling tasks
CN111177601B (en) Page rendering processing method, device, equipment and readable storage medium
CN112954008B (en) Distributed task processing method and device, electronic equipment and storage medium
CN112650812A (en) Data fragment storage method and device, computer equipment and storage medium
CN105871568B (en) Software upgrading method and system
CN111159233A (en) Distributed caching method, system, computer device and storage medium
CN113535262A (en) Method, device, equipment and storage medium for controlling starting of proxy node
CN113064732A (en) Distributed system and management method thereof
CN112416542A (en) Distributed task system, management method and device and computer equipment
CN107707672B (en) Method, device and equipment for reconstructing code with balanced load
CN106790269B (en) Recovery method and device for overtime login of application program
CN111367921A (en) Data object refreshing method and device
CN112181785B (en) Method, terminal and storage medium for automatically adding monitoring equipment
CN115361279A (en) Configuration data updating method and device, electronic equipment and computer readable medium
CN110784518A (en) Static resource acquisition method and device
CN109062931B (en) Cache synchronization control method, device, system, server and readable storage medium
CN116361332A (en) Information processing method, equipment and computer readable storage 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