CN113190554A - Method for generating primary key and distributed system - Google Patents

Method for generating primary key and distributed system Download PDF

Info

Publication number
CN113190554A
CN113190554A CN202110481413.6A CN202110481413A CN113190554A CN 113190554 A CN113190554 A CN 113190554A CN 202110481413 A CN202110481413 A CN 202110481413A CN 113190554 A CN113190554 A CN 113190554A
Authority
CN
China
Prior art keywords
target
primary key
data table
node
key generation
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.)
Pending
Application number
CN202110481413.6A
Other languages
Chinese (zh)
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.)
Ping An Pension Insurance Corp
Original Assignee
Ping An Pension Insurance Corp
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 Ping An Pension Insurance Corp filed Critical Ping An Pension Insurance Corp
Priority to CN202110481413.6A priority Critical patent/CN113190554A/en
Publication of CN113190554A publication Critical patent/CN113190554A/en
Pending legal-status Critical Current

Links

Images

Classifications

    • GPHYSICS
    • G06COMPUTING; CALCULATING OR 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/22Indexing; Data structures therefor; Storage structures
    • G06F16/2282Tablespace storage structures; Management thereof
    • GPHYSICS
    • G06COMPUTING; CALCULATING OR 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/23Updating
    • GPHYSICS
    • G06COMPUTING; CALCULATING OR 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
    • GPHYSICS
    • G06COMPUTING; CALCULATING OR 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/28Databases characterised by their database models, e.g. relational or object models
    • G06F16/284Relational databases

Landscapes

  • Engineering & Computer Science (AREA)
  • Theoretical Computer Science (AREA)
  • Databases & Information Systems (AREA)
  • Data Mining & Analysis (AREA)
  • Physics & Mathematics (AREA)
  • General Engineering & Computer Science (AREA)
  • General Physics & Mathematics (AREA)
  • Computing Systems (AREA)
  • Software Systems (AREA)
  • Information Retrieval, Db Structures And Fs Structures Therefor (AREA)

Abstract

The invention discloses a method for generating a main key, which comprises the following steps: the management node receives a primary key generation request, and determines primary key generation parameters according to the primary key generation request; the management node determines a target configuration node from the plurality of configuration nodes and sends the primary key generation parameter to the target configuration node; the target configuration node judges whether an unused serial number associated with the name of the target data table is stored locally; when the target configuration node does not locally store the unused serial number associated with the target data table name, the target configuration node sends the primary key generation parameter to Redis, receives the target number of unused serial numbers which are returned by the Redis and are associated with the target data table name, and stores the received unused serial numbers to the local; when the target configuration node locally stores the unused serial number associated with the target data table name, the target configuration node acquires an unused serial number from the local, and generates a primary key according to the acquired unused serial number.

Description

Method for generating primary key and distributed system
Technical Field
The invention relates to the technical field of distributed storage, in particular to a method for generating a primary key and a distributed system for generating the primary key.
Background
In a relational database, each table has its own primary key, which refers to a column or combination of columns, whose value uniquely identifies each row in the table, by which the physical integrity of the table is enforced.
In the existing primary key generation scheme, in order to ensure that the primary key has uniqueness and monotone incrementability, a database lock is used for realizing the primary key generation task of a data table, but under the high concurrency condition, the method has the problems of low efficiency and incapability of fully utilizing the performance of a computer.
Aiming at the technical problems that the efficiency is low and the computer performance cannot be fully utilized when a database lock is used for realizing a primary key generation task under the high concurrency condition in the prior art, an effective solution does not exist at present.
Disclosure of Invention
The invention aims to provide a method for generating a primary key and a distributed system for generating the primary key, which can solve the technical problems that the efficiency is low and the performance of a computer cannot be fully utilized when a database lock is used for realizing a primary key generation task under the high concurrency condition in the prior art.
One aspect of the present invention provides a method for generating a primary key, which is applied to a distributed system, where the distributed system includes a management node and a plurality of configuration nodes, and the method includes: the management node receives a primary key generation request; the management node determines a primary key generation parameter according to the primary key generation request, wherein the primary key generation parameter comprises a target data table name and a target number with a numerical value larger than 1; the management node determines a target configuration node from the plurality of configuration nodes and sends the primary key generation parameter to the target configuration node; the target configuration node judges whether an unused serial number associated with the target data table name is stored locally, wherein the unused serial number is used for generating a primary key of the data table associated with the target data table name; when the target configuration node does not locally store the unused serial number associated with the target data table name, the target configuration node sends the primary key generation parameter to Redis, receives the unused serial numbers of the target number, which are returned by the Redis and are associated with the target data table name, and stores the received unused serial numbers to the local; when the target configuration node locally stores the unused serial number associated with the target data table name, the target configuration node locally acquires an unused serial number and generates the primary key according to the acquired unused serial number.
Optionally, the step of the management node determining a target configuration node from the plurality of configuration nodes includes: the management node acquires a locally stored directory table, wherein the directory table comprises a plurality of pieces of directory information, and each piece of directory information comprises a data table name, the number of unused serial numbers associated with the data table name and a node identifier of a configuration node storing the unused serial numbers associated with the data table name; the management node compares the target data table name with the data table names in the directory information to obtain a comparison result; and the management node determines the target configuration node according to the comparison result.
Optionally, the step of determining, by the management node, the target configuration node according to the comparison result includes: when the comparison result represents that only one data table name in the directory information is consistent with the target data table name, the management node determines the configuration node pointed by the node identifier in the directory information as the target configuration node.
Optionally, the step of determining, by the management node, the target configuration node according to the comparison result includes: when the comparison result represents that the data table name in the plurality of pieces of directory information is consistent with the target data table name, the management node determines the directory information containing the largest number of unused serial numbers from the plurality of pieces of directory information, and determines the configuration node pointed by the node identifier in the determined directory information as the target configuration node.
Optionally, the step of determining, by the management node, the target configuration node according to the comparison result includes: when the comparison result represents that the name of the data table in each piece of directory information is inconsistent with the name of the target data table, the management node judges whether the configuration nodes pointed by all the node identifiers in the directory table cover all the configuration nodes in the distributed system; when all the configuration nodes in the distributed system are covered, the management node determines the configuration node pointed by the node identifier with the least occurrence times in the directory table as the target configuration node; when all the configuration nodes in the distributed system are not covered, the management node determines the uncovered configuration nodes from the distributed system and selects one configuration node from the uncovered configuration nodes as the target configuration node.
Optionally, the step of determining, by the management node, a primary key generation parameter according to the primary key generation request includes: the management node analyzes the primary key generation request to obtain an analysis result, wherein the analysis result comprises the name of the target data table; when the management node judges that the analysis result comprises the target number, the management node acquires the name of the target data table and the target number from the analysis result to form the primary key generation parameter; when the management node judges that the target number is not included in the analysis result, the management node obtains a preset numerical value which is preset and is associated with the target data table name, and the preset numerical value is used as the target number and the target data table name in the analysis result to form the primary key generation parameter.
Optionally, the step of determining, by the target configuration node, whether an unused sequence number associated with the target data table name is locally stored includes: the target configuration node judges whether the primary key generation parameters are received for the first time; when the target configuration node receives the primary key generation parameter for the first time, the target configuration node judges that an unused serial number associated with the target data table name is not stored locally; when the target configuration node receives the primary key generation parameter for the non-first time, the target configuration node continuously judges whether an unused serial number associated with the target data table name is stored in a local memory; when the unused sequence number associated with the target data table name is not stored in the local memory of the target configuration node, the target configuration node judges that the unused sequence number associated with the target data table name is not stored locally.
Optionally, the step of generating the primary key according to the obtained unused serial number includes: acquiring a primary key generation rule associated with the name of the target data table; and processing the unused serial number according to the primary key generation rule to generate the primary key.
Optionally, after the step of generating the primary key according to the obtained unused serial number, the method further includes: the target configuration node counts the number of unused serial numbers associated with the names of the locally remaining target data tables at the current moment, and synchronizes the counted number to the management node; and the management node updates the directory table according to the synchronous number.
Another aspect of the present invention provides a distributed system for generating a primary key, the distributed system comprising a management node and a plurality of configuration nodes, wherein:
the management node is used for receiving a primary key generation request; determining a primary key generation parameter according to the primary key generation request; determining a target configuration node from the plurality of configuration nodes, and sending the primary key generation parameter to the target configuration node; the primary key generation parameters comprise the name of a target data table and the target number of which the numerical value is more than 1;
the target configuration node is used for judging whether an unused serial number associated with the target data table name is stored locally, wherein the unused serial number is used for generating a primary key of the data table associated with the target data table name; when the target configuration node does not locally store the unused serial number associated with the target data table name, the target configuration node sends the primary key generation parameter to Redis, receives the unused serial numbers of the target number, which are returned by the Redis and are associated with the target data table name, and stores the received unused serial numbers to the local; when the target configuration node locally stores the unused serial number associated with the target data table name, the target configuration node locally acquires an unused serial number and generates the primary key according to the acquired unused serial number.
Yet another aspect of the present invention provides a computer apparatus, comprising: a memory, a processor and a computer program stored on the memory and executable on the processor, the processor implementing the method of generating a primary key according to any of the above embodiments when executing the computer program.
Yet another aspect of the present invention provides a computer-readable storage medium, on which a computer program is stored, which, when executed by a processor, implements the method for generating a primary key according to any one of the above embodiments. Further, the computer-readable storage medium may mainly include a storage program area and a storage data area, wherein the storage program area may store an operating system, an application program required for at least one function, and the like; the storage data area may store data created according to the use of the blockchain node, and the like.
The method for generating the primary key provided by the invention determines a target configuration node for generating the primary key after a management node receives a primary key generation request, the target configuration node requests a certain number of unused serial numbers from Redis and stores the unused serial numbers in the local, then the primary key corresponding to a data table is generated according to one of the unused serial numbers, when the target configuration node generates the primary key for the same data table again, the local residual unused serial numbers are directly called to generate the primary key without sending the request to Redis again, the mode can not continuously request Redis even under the high concurrency condition, a large amount of IO (input/output) waiting can not be caused, the data congestion condition under the high concurrency condition can be effectively responded, the performance of a computer is fully utilized, the waiting time of the computer and the like is reduced, the primary key generation efficiency is improved, and the whole process can be completed through an internal mechanism, the method and the device do not need to additionally depend on other middleware, and solve the technical problems that the efficiency is low and the performance of the computer cannot be fully utilized in the prior art for realizing the primary key generation task by using the database lock under the high concurrency condition.
Drawings
Various other advantages and benefits will become apparent to those of ordinary skill in the art upon reading the following detailed description of the preferred embodiments. The drawings are only for purposes of illustrating the preferred embodiments and are not to be construed as limiting the invention. Also, like reference numerals are used to refer to like parts throughout the drawings. In the drawings:
FIG. 1 is a flow chart schematically illustrating a method of generating a primary key according to a first embodiment of the present invention;
FIG. 2 is a diagram schematically illustrating a scheme of generating a primary key according to a first embodiment of the present invention;
FIG. 3 schematically illustrates a block diagram of a distributed system for generating primary keys, in accordance with a second embodiment of the present invention;
fig. 4 schematically shows a block diagram of a computer device adapted to implement the method of generating a primary key according to a third embodiment of the present invention.
Detailed Description
In order to make the objects, technical solutions and advantages of the present invention more apparent, the present invention is described in further detail below with reference to the accompanying drawings and embodiments. It should be understood that the specific embodiments described herein are merely illustrative of the invention and are not intended to limit the invention. All other embodiments, which can be derived by a person skilled in the art from the embodiments given herein without making any creative effort, shall fall within the protection scope of the present invention.
It should be noted that, in this document, the terms "comprises," "comprising," or any other variation thereof, are intended to cover a non-exclusive inclusion, such that a process, method, article, or apparatus that comprises a list of elements does not include only those elements but may include other elements not expressly listed or inherent to such process, method, article, or apparatus. Without further limitation, an element defined by the phrase "comprising an … …" does not exclude the presence of other like elements in a process, method, article, or apparatus that comprises the element.
Example one
Fig. 1 schematically shows a flowchart of a method for generating a primary key according to a first embodiment of the present invention, where the method for generating a primary key may be applied to a distributed system, and the distributed system may include a management node and a plurality of configuration nodes, and as shown in fig. 1, the method for generating a primary key may include steps S1 to S6, specifically:
in step S1, the management node receives a primary key generation request.
In data tables, the primary key is usually used to uniquely identify data, the primary keys in different data tables are different, and the primary key used to identify different rows of data in each data table is also different, i.e., the primary key has uniqueness. In this embodiment, when a user inserts data into a data table through a client, each piece of data needs to be identified by a unique primary key, and in order to obtain a primary key, the client may generate a primary key generation request based on the data table, so as to apply for a primary key. Since the primary keys in different data tables are not the same, the primary key generation request necessarily includes the data table name (i.e., the following target data table name) to specify which data table is assigned the primary key, thereby avoiding confusion with the primary keys of other data tables.
Step S2, the management node determines a primary key generation parameter according to the primary key generation request, where the primary key generation parameter includes a target data table name and a target number whose value is greater than 1.
The primary key generation request is used for requesting a primary key for the data table, the primary key generation request carries the data table name of the data table, namely the target data table name, and the target data table name can be obtained by analyzing the primary key generation request. Optionally, the user may customize how many unused serial numbers are requested at a time for generating the primary key, that is, the primary key generation request may also carry a target number. When the target number is not carried in the primary key generation request, a preset fixed numerical value, that is, a preset numerical value, may be obtained, and the preset number is used as the target number. Specifically, step S2 may include steps S21 to S23, in which:
step S21, the management node analyzes the primary key generation request to obtain an analysis result, wherein the analysis result comprises the name of the target data table;
step S22, when the management node determines that the parsing result includes the target number, the management node obtains the name of the target data table and the target number from the parsing result to form the primary key generation parameter;
step S23, when the management node determines that the target number is not included in the analysis result, the management node obtains a preset value associated with the target data table name, and forms the primary key generation parameter by using the preset value as the target number and the target data table name in the analysis result. Wherein the preset value is an integer greater than 1.
Optionally, the primary key generation parameter may further include a preset start value, which is a start value of an unused serial number used to define the requested target number, and the preset start value may be obtained by parsing the primary key generation request, and then the preset start value, the target number, and the target data table name are formed into the primary key generation parameter together.
Step S3, the management node determines a target configuration node from the plurality of configuration nodes, and sends the primary key generation parameter to the target configuration node.
The management node can arbitrarily select a configuration node as a target configuration node, and can also determine a target configuration node according to a pre-stored directory table. For example, in step S3, the step of the management node determining a target configuration node from the plurality of configuration nodes includes steps S31 to S33, wherein:
step S31, the management node acquires a locally stored directory table, wherein the directory table includes a plurality of pieces of directory information, each piece of directory information includes a data table name, the number of unused serial numbers associated with the data table name, and a node identifier of a configuration node storing the unused serial numbers associated with the data table name;
step S32, the management node compares the target data table name with the data table names in the directory information to obtain a comparison result;
and step S33, the management node determines the target configuration node according to the comparison result.
The node identifier is used for uniquely marking the configuration node, and may be a physical address of the configuration node or an ID number of the configuration node. The comparison result comprises a plurality of conditions, and different determination strategies are preset for each condition, so that a target configuration node is completely determined without omission, and smooth execution of the process is ensured. The specific strategy is as follows:
policy one, step S33 may include step S331, wherein:
step S331, when the comparison result indicates that only one data table name in the directory information is consistent with the target data table name, the management node determines the configuration node pointed by the node identifier in the directory information as the target configuration node.
Strategy two, step S33 may further include step S331', wherein:
step S331', when the comparison result indicates that the data table name in the plurality of pieces of directory information is consistent with the target data table name, the management node determines, from the plurality of pieces of directory information, the directory information including the largest number of unused serial numbers, and determines, as the target configuration node, the configuration node to which the node identifier in the determined directory information points.
In this embodiment, in order to avoid that the unused serial number occupies the system storage space for a long time, the unused serial number in the configuration node needs to be consumed as soon as possible, so when the data table names in the pieces of directory information are all consistent with the target data table name, the directory information with the largest number of unused serial numbers can be determined from the pieces of directory information, and then the configuration node pointed to by the node identifier of the directory information is determined as the target configuration node.
Strategy three, step S33 may further include steps S331 "to S333", wherein:
step S331', when the comparison result indicates that the name of the data table in each piece of directory information is inconsistent with the name of the target data table, the management node judges whether the configuration nodes pointed by all the node identifiers in the directory table cover all the configuration nodes in the distributed system;
step S332 ", when all the configuration nodes in the distributed system are covered, the management node determines the configuration node pointed by the node identifier with the least occurrence number in the directory table as the target configuration node;
step S333 ″, when all the configuration nodes in the distributed system are not covered, the management node determines the configuration nodes that are not covered from the distributed system, and selects a configuration node from the configuration nodes that are not covered as the target configuration node.
In order to accelerate the generation speed of the primary key as much as possible, when each data table name in the directory table is not consistent with the target data table name and the information defined by the directory table relates to all configuration nodes of the distributed system, based on the principle that the less the node identifier appears, the more idle the configuration node pointed to is, the configuration node pointed to by the node identifier with the least occurrence in the directory table can be determined as the target configuration node. However, when the information defined by the directory table is all configuration nodes related to the distributed system, it indicates that some configuration nodes are still processing idle state, and at this time, one configuration node is selected from the uncovered configuration nodes as the target configuration node. Randomly selecting one configuration node from the uncovered configuration nodes as a target configuration node; or selecting the configuration node with the maximum data processing capacity from the uncovered configuration nodes as a target configuration node to further accelerate the processing speed; the configuration node with the largest memory can be selected from the uncovered configuration nodes as the target configuration node, so that more unused serial numbers can be stored.
Among them, the data processing capability may be confirmed in advance, such as:
in the first scheme, the data processing capability refers to the data volume M1 processed by the configuration node within a predetermined time period T1 according to a predetermined processing logic, wherein the more the data volume processed by the configuration node is, the stronger the data processing capability is; the smaller the amount of data processed by the configuration node, the poorer the data processing capability. This amount of data can then be determined as a parameter characterizing the data processing capacity of the configuration node.
Or, in the second scheme, the data processing capability refers to a time length T2 required by the configuration node to process the data of the predetermined data volume M2 according to the predetermined processing logic, wherein the shorter the time length required by the configuration node is, the stronger the data processing capability is; the longer the time required to configure the node, the poorer the data processing capability. The duration may be determined as a parameter characterizing the data processing capacity of the configuration node at this time.
Or, in the third scheme, the data processing capability refers to the data amount M3 that is processed by the configuration node in a unit time length, that is, the data processing speed of the configuration node, wherein the faster the data processing speed of the configuration node is, the stronger the data processing capability is; the slower the data processing speed of the configuration node, the worse the data processing capability. The data processing speed may be determined at this time as a parameter configuring the data processing capability of the node.
Step S4, the target configuration node determines whether an unused sequence number associated with the target data table name is locally stored, where the unused sequence number is used to generate a primary key of the data table associated with the target data table name.
In this embodiment, a sequence number list associated with a target data table name may be locally searched, a status bit of each sequence number stored in the sequence number list is determined, where the status bit is used to identify whether the sequence number is in a used state or an unused state, and when any one or more status bits indicate an unused state, it is determined that an unused sequence number associated with the target data table name is locally stored.
Alternatively, the step S4 may include steps S41 to S44, wherein:
step S41, the target configuration node judges whether the primary key generation parameter is received for the first time;
step S42, when the target configuration node receives the primary key generation parameter for the first time, the target configuration node determines that an unused sequence number associated with the target data table name is not locally stored;
step S43, when the target configuration node receives the primary key generation parameter for the non-first time, the target configuration node continues to determine whether an unused serial number associated with the target data table name is stored in a local memory;
step S44, when the unused sequence number associated with the target data table name is not stored in the local memory of the target configuration node, the target configuration node determines that the unused sequence number associated with the target data table name is not stored locally.
Since the primary key generation parameter includes the target data table name, if the target configuration node receives the primary key generation parameter for the first time (for example, the processor has not processed the primary key generation parameter), the target configuration node must not request a sequence number for the data table pointed by the target data table name, and then it may be determined that the unused sequence number associated with the target data table name is not stored locally.
If not, whether the local memory stores the unused serial number associated with the name of the target data table or not can be judged, if yes, the unused serial number is considered to be stored locally, and if not, the unused serial number is considered to be not stored locally. The serial number is stored in the local memory, and then the main key is generated according to the unused serial number of the local memory, so that the working efficiency is improved to a certain extent.
Step S5, when the target configuration node does not locally store the unused sequence number associated with the target data table name, the target configuration node sends the primary key generation parameter to Redis, receives the unused sequence number of the target number associated with the target data table name and returned by the Redis, and stores the received unused sequence number to local.
Redis is a storage system capable of executing a computing function, and can generate a serial number (also called an unused serial number) which is gradually increased based on an incrementBy method, and can ensure that serial numbers obtained between devices applying serial numbers to Redis do not conflict and have uniqueness, thereby ensuring that a finally generated primary key is unique. After receiving the primary key generation parameter, the Redis calls an incrementBy method to generate related target quantity of unused serial numbers for the target data table name, the serial numbers meet the monotone increasing characteristic, and if the primary key generation parameter comprises a preset initial value, the initial value of the unused serial numbers generated by the Redis is the preset initial value.
After receiving the unused sequence numbers returned by the Redis, the target configuration node may store the unused sequence numbers locally, specifically, may store the unused sequence numbers in a local memory.
Step S6, when the target configuration node locally stores an unused serial number associated with the target data table name, the target configuration node locally acquires an unused serial number, and generates the primary key according to the acquired unused serial number.
If the step S4 determines that the unused serial number is locally stored, indicating that the unused serial number has been previously assigned to the name of the target data table by Redis, directly performing step S6; if step S4 determines that the unused serial number is not stored locally, it may be that a serial number has not been assigned to the target data table name before Redis, or that the assigned serial number is used up, then step S6 may be executed after applying for the unused serial number to Redis in step S5.
In this embodiment, each primary key generation request is used to request a primary key, each serial number is used to generate a primary key, based on the current primary key generation request, an unused serial number may be randomly selected, or an unused serial number with the smallest value is selected, then a primary key is generated based on the selected unused serial number, and then the status bit of the serial number is set to be used.
For the local residual unused serial numbers associated with the names of the target data tables, when the target configuration node receives the primary key generation parameter containing the name of the target data table next time, the target configuration node continues to obtain one unused serial number generation primary key from the local, and does not need to request the Redis again, so that the time is saved, and the primary key generation speed is increased.
Optionally, in step S6, the step of generating the primary key according to the obtained unused serial number includes step S61 and step S62, wherein:
step S61, acquiring a primary key generation rule associated with the name of the target data table;
and step S62, processing the unused serial number according to the primary key generation rule to generate the primary key.
Each data table name may be associated with a different primary key generation rule, the primary key generation rule may define a primary key prefix, and may generate a primary key by combining the selected unused serial number as a primary key suffix with the defined primary key prefix and the unused serial number as a primary key suffix. The prefix primary key is, for example, a primary key prefix of a current time (year, month, day, hour, minute and second), the current primary key prefix may be 2021/3/11/11:34:03, and the next primary key prefix may be 2021/3/11/11:34: 15. In the embodiment, the primary key generation algorithm is simple, the running performance of the computer can be ensured, and the primary key conforms to the monotone increasing characteristic, is simple and easy to read, and has obvious advantages in the aspects of service operation and troubleshooting.
Optionally, after step S6, the method further comprises step a1 and step a2, wherein:
step A1, the target configuration node counts the number of unused serial numbers associated with the names of the locally remained target data table at the current time, and synchronizes the counted number to the management node;
step A2, the management node updates the directory table according to the number of synchronizations.
The configuration node is only used for internal calculation, the management node is used as a bridge for communication between the configuration node and an external client, after a target configuration node generates a primary key, the primary key is sent to the management node, the management node returns the primary key to the client, meanwhile, in order to guarantee the accuracy of the directory table, the target configuration node also counts the number of the current remaining unused serial numbers, and then synchronizes the number to the management node, so that the management node updates the target table, for example, the number of the unused serial numbers related to the number data table name and the target configuration node in the directory table is reduced by 1.
The present invention is explained in detail below with a specific example.
As shown in fig. 2, JVM-01, JVM-02, and JVM-03 represent three configuration nodes, the management node is not shown in the figure, TableA and TableB are both data table names, the application number segment is a serial number, and in order to avoid confusion, serial numbers associated with different data table names in each configuration node are stored separately. According to the determination strategy of the target configuration node, different configuration nodes may process primary keys associated with the same data table name, and therefore, sequence numbers associated with the same data table name may be stored in different configuration nodes. If the target configuration node receives the primary key generation parameter for the first time, the target configuration node sends the primary key generation parameter to Redis, the Redis returns unused sequence numbers of target quantity (namely step values in the graph) associated with target data table names (namely tableName values in the graph) based on an elementary by method, and the target configuration node selects one unused sequence number to generate the primary key after storing the unused sequence numbers locally; if the target configuration node does not receive the primary key generation parameter for the first time, whether an unused serial number associated with the target data table name is stored locally or not is judged, if the unused serial number is directly acquired to generate the primary key, if the unused serial number is not acquired, a primary key generation request is sent to Redis, and the subsequent steps are consistent with the primary key generation parameter for the first time.
Example two
The second embodiment of the present invention provides a distributed system for generating a primary key, which corresponds to the first embodiment, and corresponding technical features and technical effects are not described in detail in this embodiment, and reference may be made to the first embodiment for relevant points. Specifically, fig. 3 schematically shows a block diagram of a distributed system for generating a primary key according to the second embodiment of the present invention, and as shown in fig. 3, the distributed system 300 may include a management node 301 and a plurality of configuration nodes 302, where:
the management node 301 is configured to receive a primary key generation request; determining a primary key generation parameter according to the primary key generation request; determining a target configuration node 302 from the plurality of configuration nodes 302 and sending the primary key generation parameter to the target configuration node 302; the primary key generation parameters comprise the name of a target data table and the target number of which the numerical value is more than 1;
the target configuration node 302 is configured to determine whether an unused sequence number associated with the target data table name is locally stored, where the unused sequence number is used to generate a primary key of the data table associated with the target data table name; when the target configuration node 302 does not locally store the unused sequence number associated with the target data table name, the target configuration node 302 sends the primary key generation parameter to the Redis, receives the unused sequence number of the target number associated with the target data table name returned by the Redis, and stores the received unused sequence number to the local; when the target configuration node 302 locally stores the unused serial number associated with the target data table name, the target configuration node 302 locally obtains an unused serial number, and generates the primary key according to the obtained unused serial number.
Optionally, when the step of determining a target configuration node from the plurality of configuration nodes is executed by the management node, the management node is further configured to: acquiring a locally stored directory table, wherein the directory table comprises a plurality of pieces of directory information, and each piece of directory information comprises a data table name, the number of unused serial numbers associated with the data table name, and a node identifier of a configuration node storing the unused serial numbers associated with the data table name; comparing the target data table name with the data table names in the directory information to obtain a comparison result; and determining the target configuration node according to the comparison result.
Optionally, when the step of determining the target configuration node according to the comparison result is executed by the management node, the management node is further configured to: when the comparison result represents that only one data table name in the directory information is consistent with the target data table name, the management node determines the configuration node pointed by the node identifier in the directory information as the target configuration node.
Optionally, when the step of determining the target configuration node according to the comparison result is executed by the management node, the management node is further configured to: when the comparison result represents that the data table name in the plurality of pieces of directory information is consistent with the target data table name, the management node determines the directory information containing the largest number of unused serial numbers from the plurality of pieces of directory information, and determines the configuration node pointed by the node identifier in the determined directory information as the target configuration node.
Optionally, when the step of determining the target configuration node according to the comparison result is executed by the management node, the management node is further configured to: when the comparison result represents that the name of the data table in each piece of directory information is inconsistent with the name of the target data table, the management node judges whether the configuration nodes pointed by all the node identifiers in the directory table cover all the configuration nodes in the distributed system; when all the configuration nodes in the distributed system are covered, the management node determines the configuration node pointed by the node identifier with the least occurrence times in the directory table as the target configuration node; when all the configuration nodes in the distributed system are not covered, the management node determines the uncovered configuration nodes from the distributed system and selects one configuration node from the uncovered configuration nodes as the target configuration node.
Optionally, when the step of determining the primary key generation parameter according to the primary key generation request is executed by the management node, the management node is further configured to: the management node analyzes the primary key generation request to obtain an analysis result, wherein the analysis result comprises the name of the target data table; when the management node judges that the analysis result comprises the target number, the management node acquires the name of the target data table and the target number from the analysis result to form the primary key generation parameter; when the management node judges that the target number is not included in the analysis result, the management node obtains a preset numerical value which is preset and is associated with the target data table name, and the preset numerical value is used as the target number and the target data table name in the analysis result to form the primary key generation parameter.
Optionally, when the step of determining whether the unused sequence number associated with the target data table name is stored locally is executed by the target configuration node, the target configuration node is further configured to: the target configuration node judges whether the primary key generation parameters are received for the first time; when the target configuration node receives the primary key generation parameter for the first time, the target configuration node judges that an unused serial number associated with the target data table name is not stored locally; when the target configuration node receives the primary key generation parameter for the non-first time, the target configuration node continuously judges whether an unused serial number associated with the target data table name is stored in a local memory; when the unused sequence number associated with the target data table name is not stored in the local memory of the target configuration node, the target configuration node judges that the unused sequence number associated with the target data table name is not stored locally.
Optionally, when the target configuration node executes the step of generating the primary key according to the obtained unused sequence number, the target configuration node is further configured to: acquiring a primary key generation rule associated with the name of the target data table; and processing the unused serial number according to the primary key generation rule to generate the primary key.
Optionally, the target configuration node is further configured to: after the step of generating the primary key according to the obtained unused serial number is executed, counting the number of the unused serial numbers associated with the names of the locally-remaining target data tables at the current moment, and synchronizing the counted number to the management node; the management node is further configured to update the directory table according to the number of synchronizations.
EXAMPLE III
Fig. 4 schematically shows a block diagram of a computer device adapted to implement the method of generating a primary key according to a third embodiment of the present invention. In this embodiment, the computer device 400 may be a smart phone, a tablet computer, a notebook computer, a desktop computer, a rack server, a blade server, a tower server, or a rack server (including an independent server or a server cluster composed of a plurality of servers), and the like that execute programs. As shown in fig. 4, the computer device 400 of the present embodiment includes at least, but is not limited to: a memory 401, a processor 402, a network interface 403 communicatively coupled to each other via a system bus. It is noted that FIG. 4 only shows the computer device 400 having components 401 and 403, but it is understood that not all of the shown components are required and that more or fewer components may be implemented instead.
In this embodiment, the memory 403 includes at least one type of computer-readable storage medium, which includes flash memory, a hard disk, a multimedia card, a card-type memory (e.g., SD or DX memory, etc.), a Random Access Memory (RAM), a Static Random Access Memory (SRAM), a read-only memory (ROM), an electrically erasable programmable read-only memory (EEPROM), a programmable read-only memory (PROM), a magnetic memory, a magnetic disk, an optical disk, and the like. In some embodiments, the storage 401 may be an internal storage unit of the computer device 400, such as a hard disk or a memory of the computer device 400. In other embodiments, the memory 401 may also be an external storage device of the computer device 400, such as a plug-in hard disk, a Smart Media Card (SMC), a Secure Digital (SD) Card, a Flash memory Card (Flash Card), etc. provided on the computer device 400. Of course, the memory 401 may also include both internal and external storage devices for the computer device 400. In the present embodiment, the memory 401 is generally used for storing an operating system installed in the computer apparatus 400 and various kinds of application software, such as a program code of a method of generating a primary key, and the like. Further, the memory 401 may also be used to temporarily store various types of data that have been output or are to be output.
Processor 402 may be a Central Processing Unit (CPU), controller, microcontroller, microprocessor, or other data Processing chip in some embodiments. The processor 402 is generally used to control the overall operation of the computer device 400. Such as program codes for executing a method of generating a primary key related to control and processing related to data interaction or communication with the computer apparatus 400.
In this embodiment, the method for generating the primary key stored in the memory 401 may be further divided into one or more program modules and executed by one or more processors (in this embodiment, the processor 402) to complete the present invention.
The network interface 403 may comprise a wireless network interface or a wired network interface, the network interface 403 typically being used to establish communication links between the computer device 400 and other computer devices. For example, the network interface 403 is used to connect the computer apparatus 400 with an external terminal through a network, establish a data transmission channel and a communication link between the computer apparatus 400 and the external terminal, and the like. The network may be a wireless or wired network such as an Intranet (Intranet), the Internet (Internet), a Global System of mobile communication (GSM), Wideband Code Division Multiple Access (WCDMA), a 4G network, a 5G network, Bluetooth (Bluetooth), or Wi-Fi.
Example four
The fourth embodiment further provides a computer-readable storage medium, including a flash memory, a hard disk, a multimedia card, a card-type memory (e.g., SD or DX memory, etc.), a Random Access Memory (RAM), a Static Random Access Memory (SRAM), a read-only memory (ROM), an electrically erasable programmable read-only memory (EEPROM), a programmable read-only memory (PROM), a magnetic memory, a magnetic disk, an optical disk, a server, an App application mall, etc., on which a computer program is stored, which when executed by a processor implements a method of generating a primary key. Further, the computer-readable storage medium may mainly include a storage program area and a storage data area, wherein the storage program area may store an operating system, an application program required for at least one function, and the like; the storage data area may store data created according to the use of the blockchain node, and the like.
The block chain is a novel application mode of computer technologies such as distributed data storage, point-to-point transmission, a consensus mechanism, an encryption algorithm and the like. A block chain (Blockchain), which is essentially a decentralized database, is a series of data blocks associated by using a cryptographic method, and each data block contains information of a batch of network transactions, so as to verify the validity (anti-counterfeiting) of the information and generate a next block. The blockchain may include a blockchain underlying platform, a platform product service layer, an application service layer, and the like.
It will be apparent to those skilled in the art that the modules or steps of the embodiments of the invention described above may be implemented by a general purpose computing device, they may be centralized on a single computing device or distributed across a network of multiple computing devices, and alternatively, they may be implemented by program code executable by a computing device, such that they may be stored in a storage device and executed by a computing device, and in some cases, the steps shown or described may be performed in an order different than that described herein, or they may be separately fabricated into individual integrated circuit modules, or multiple ones of them may be fabricated into a single integrated circuit module. Thus, embodiments of the invention are not limited to any specific combination of hardware and software.
The above-mentioned serial numbers of the embodiments of the present invention are merely for description and do not represent the merits of the embodiments.
Through the above description of the embodiments, those skilled in the art will clearly understand that the method of the above embodiments can be implemented by software plus a necessary general hardware platform, and certainly can also be implemented by hardware, but in many cases, the former is a better implementation manner.
The above description is only a preferred embodiment of the present invention, and not intended to limit the scope of the present invention, and all modifications of equivalent structures and equivalent processes, which are made by using the contents of the present specification and the accompanying drawings, or directly or indirectly applied to other related technical fields, are included in the scope of the present invention.

Claims (10)

1. A method for generating a primary key, the method being applied to a distributed system, the distributed system comprising a management node and a plurality of configuration nodes, the method comprising:
the management node receives a primary key generation request;
the management node determines a primary key generation parameter according to the primary key generation request, wherein the primary key generation parameter comprises a target data table name and a target number with a numerical value larger than 1;
the management node determines a target configuration node from the plurality of configuration nodes and sends the primary key generation parameter to the target configuration node;
the target configuration node judges whether an unused serial number associated with the target data table name is stored locally, wherein the unused serial number is used for generating a primary key of the data table associated with the target data table name;
when the target configuration node does not locally store the unused serial number associated with the target data table name, the target configuration node sends the primary key generation parameter to Redis, receives the unused serial numbers of the target number, which are returned by the Redis and are associated with the target data table name, and stores the received unused serial numbers to the local;
when the target configuration node locally stores the unused serial number associated with the target data table name, the target configuration node locally acquires an unused serial number and generates the primary key according to the acquired unused serial number.
2. The method of claim 1, wherein the step of the management node determining a target configuration node from the plurality of configuration nodes comprises:
the management node acquires a locally stored directory table, wherein the directory table comprises a plurality of pieces of directory information, and each piece of directory information comprises a data table name, the number of unused serial numbers associated with the data table name and a node identifier of a configuration node storing the unused serial numbers associated with the data table name;
the management node compares the target data table name with the data table names in the directory information to obtain a comparison result;
and the management node determines the target configuration node according to the comparison result.
3. The method of claim 2, wherein the step of the management node determining the target configuration node according to the comparison result comprises:
when the comparison result represents that only one data table name in the directory information is consistent with the target data table name, the management node determines the configuration node pointed by the node identifier in the directory information as the target configuration node.
4. The method of claim 2, wherein the step of the management node determining the target configuration node according to the comparison result comprises:
when the comparison result represents that the data table name in the plurality of pieces of directory information is consistent with the target data table name, the management node determines the directory information containing the largest number of unused serial numbers from the plurality of pieces of directory information, and determines the configuration node pointed by the node identifier in the determined directory information as the target configuration node.
5. The method of claim 2, wherein the step of the management node determining the target configuration node according to the comparison result comprises:
when the comparison result represents that the name of the data table in each piece of directory information is inconsistent with the name of the target data table, the management node judges whether the configuration nodes pointed by all the node identifiers in the directory table cover all the configuration nodes in the distributed system;
when all the configuration nodes in the distributed system are covered, the management node determines the configuration node pointed by the node identifier with the least occurrence times in the directory table as the target configuration node;
when all the configuration nodes in the distributed system are not covered, the management node determines the uncovered configuration nodes from the distributed system and selects one configuration node from the uncovered configuration nodes as the target configuration node.
6. The method of claim 1, wherein the step of the management node determining primary key generation parameters from the primary key generation request comprises:
the management node analyzes the primary key generation request to obtain an analysis result, wherein the analysis result comprises the name of the target data table;
when the management node judges that the analysis result comprises the target number, the management node acquires the name of the target data table and the target number from the analysis result to form the primary key generation parameter;
when the management node judges that the target number is not included in the analysis result, the management node obtains a preset numerical value which is preset and is associated with the target data table name, and the preset numerical value is used as the target number and the target data table name in the analysis result to form the primary key generation parameter.
7. The method of claim 1, wherein the step of the target configuration node determining whether an unused sequence number associated with the target data table name is stored locally comprises:
the target configuration node judges whether the primary key generation parameters are received for the first time;
when the target configuration node receives the primary key generation parameter for the first time, the target configuration node judges that an unused serial number associated with the target data table name is not stored locally;
when the target configuration node receives the primary key generation parameter for the non-first time, the target configuration node continuously judges whether an unused serial number associated with the target data table name is stored in a local memory;
when the unused sequence number associated with the target data table name is not stored in the local memory of the target configuration node, the target configuration node judges that the unused sequence number associated with the target data table name is not stored locally.
8. The method of claim 1, wherein the step of generating the primary key according to the obtained unused sequence number comprises:
acquiring a primary key generation rule associated with the name of the target data table;
and processing the unused serial number according to the primary key generation rule to generate the primary key.
9. The method of claim 2, wherein after the step of generating the primary key according to the obtained unused sequence number, the method further comprises:
the target configuration node counts the number of unused serial numbers associated with the names of the locally remaining target data tables at the current moment, and synchronizes the counted number to the management node;
and the management node updates the directory table according to the synchronous number.
10. A distributed system for generating a primary key, the distributed system comprising a management node and a plurality of configuration nodes, wherein:
the management node is used for receiving a primary key generation request; determining a primary key generation parameter according to the primary key generation request; determining a target configuration node from the plurality of configuration nodes, and sending the primary key generation parameter to the target configuration node; the primary key generation parameters comprise the name of a target data table and the target number of which the numerical value is more than 1;
the target configuration node is used for judging whether an unused serial number associated with the target data table name is stored locally, wherein the unused serial number is used for generating a primary key of the data table associated with the target data table name; when the target configuration node does not locally store the unused serial number associated with the target data table name, the target configuration node sends the primary key generation parameter to Redis, receives the unused serial numbers of the target number, which are returned by the Redis and are associated with the target data table name, and stores the received unused serial numbers to the local; when the target configuration node locally stores the unused serial number associated with the target data table name, the target configuration node locally acquires an unused serial number and generates the primary key according to the acquired unused serial number.
CN202110481413.6A 2021-04-30 2021-04-30 Method for generating primary key and distributed system Pending CN113190554A (en)

Priority Applications (1)

Application Number Priority Date Filing Date Title
CN202110481413.6A CN113190554A (en) 2021-04-30 2021-04-30 Method for generating primary key and distributed system

Applications Claiming Priority (1)

Application Number Priority Date Filing Date Title
CN202110481413.6A CN113190554A (en) 2021-04-30 2021-04-30 Method for generating primary key and distributed system

Publications (1)

Publication Number Publication Date
CN113190554A true CN113190554A (en) 2021-07-30

Family

ID=76983101

Family Applications (1)

Application Number Title Priority Date Filing Date
CN202110481413.6A Pending CN113190554A (en) 2021-04-30 2021-04-30 Method for generating primary key and distributed system

Country Status (1)

Country Link
CN (1) CN113190554A (en)

Citations (4)

* Cited by examiner, † Cited by third party
Publication number Priority date Publication date Assignee Title
US20180212857A1 (en) * 2017-01-26 2018-07-26 International Business Machines Corporation Proactive channel agent
CN109165216A (en) * 2018-08-02 2019-01-08 杭州启博科技有限公司 A kind of generation method and system, storage medium of Redis distributed data base major key id
CN111523002A (en) * 2020-04-23 2020-08-11 中国农业银行股份有限公司 Main key distribution method, device, server and storage medium
CN112632071A (en) * 2020-12-23 2021-04-09 平安普惠企业管理有限公司 Database primary key id generation method, device, equipment and storage medium

Patent Citations (4)

* Cited by examiner, † Cited by third party
Publication number Priority date Publication date Assignee Title
US20180212857A1 (en) * 2017-01-26 2018-07-26 International Business Machines Corporation Proactive channel agent
CN109165216A (en) * 2018-08-02 2019-01-08 杭州启博科技有限公司 A kind of generation method and system, storage medium of Redis distributed data base major key id
CN111523002A (en) * 2020-04-23 2020-08-11 中国农业银行股份有限公司 Main key distribution method, device, server and storage medium
CN112632071A (en) * 2020-12-23 2021-04-09 平安普惠企业管理有限公司 Database primary key id generation method, device, equipment and storage medium

Similar Documents

Publication Publication Date Title
CN109831487B (en) Fragmented file verification method and terminal equipment
WO2017092470A1 (en) Data storage method and device
CN109766349B (en) Task duplicate prevention method, device, computer equipment and storage medium
CN110543448A (en) data synchronization method, device, equipment and computer readable storage medium
AU2016382909A1 (en) Method and device for processing short link, and short link server
CN111294390B (en) Block data rapid synchronization method, device, equipment and storage medium
CN111723073B (en) Data storage processing method, device, processing system and storage medium
CN111722918A (en) Service identification code generation method and device, storage medium and electronic equipment
CN111988419A (en) File uploading method, file downloading method, file uploading device, file downloading device, computer equipment and storage medium
CN109714249B (en) Method and related device for pushing applet messages
CN111767144A (en) Transaction routing determination method, device, equipment and system for transaction data
CN113132267B (en) Distributed system, data aggregation method and computer readable storage medium
CN108399175B (en) Data storage and query method and device
CN111177121A (en) Order data feedback method and device, computer equipment and storage medium
CN108595685B (en) Data processing method and device
CN108389124B (en) Data processing method, data processing device, computer equipment and storage medium
CN111259012B (en) Data homogenizing method, device, computer equipment and storage medium
CN116303343A (en) Data slicing method, device, electronic equipment and storage medium
CN113190554A (en) Method for generating primary key and distributed system
CN110955460A (en) Service process starting method and device, electronic equipment and storage medium
CN106156169B (en) Discrete data processing method and device
CN114064706A (en) Data storage method and device and server
CN115033551A (en) Database migration method and device, electronic equipment and storage medium
CN109951529B (en) Resource management method and device
CN112486556B (en) Method, system, terminal and storage medium for server to be compatible with BMC

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