ID distribution method and device of distributed system
By using preloaded number segments and consistent hashing algorithms in the distributed system, combined with ETCD's node registration and routing table maintenance, the problem that the database's self-increasing ID cannot meet the demand is solved, and the global uniqueness and efficient distribution of IDs are achieved. , suitable for big data and high concurrency scenarios.
Patent Information
- Application Number
- CN202410814662.6
- Authority / Receiving Office
- CN · China
- Patent Type
- Applications(China)
- Current Assignee / Owner
- Filing Date
- 2024-06-21
- Publication Date
- 2024-10-01
AI Technical Summary
In complex distributed systems, the database's self-increasing ID cannot meet performance and scalability requirements, resulting in the inability to effectively manage the unique identification of large amounts of data and messages.
A distributed system ID allocation method is adopted. Through preloaded number segments and consistent hashing algorithms, the target node is determined based on the business identifier for ID allocation, ensuring the global uniqueness and monotonic increase of the ID. ETCD is used for node registration. and routing table maintenance to achieve efficient ID allocation.
It achieves global uniqueness and efficient allocation of IDs in distributed systems, avoids ID rollback problems, improves performance and availability, and is suitable for big data scenarios and high concurrency environments.
Smart Images

Figure CN118740790A_ABST
Abstract
Description
Technical Field
[0001] The present application relates to the field of server development technology, and more specifically, to a method and device for allocating IDs in a distributed system. Background Art
[0002] In complex distributed systems, it is often necessary to uniquely identify a large amount of data and messages. For example, in systems of financial, e-commerce, payment and other products, data is growing day by day, and after the data is divided into databases and tables, a unique ID is needed to identify a piece of data or message. The database's auto-increment ID cannot meet the needs well in terms of performance and scalability. At this time, a system that can generate a globally unique ID is very necessary. Summary of the invention
[0003] In view of this, this application provides the following technical solutions:
[0004] A method for allocating IDs in a distributed system, comprising:
[0005] The first node obtains an ID allocation request, wherein the ID allocation request includes a service identifier and data to be identified, the first node is a target node for processing the ID allocation request that can be uniquely determined based on the service identifier, and the data to be identified is data to which an ID needs to be allocated;
[0006] An ID is allocated to the data to be identified based on a preloaded number segment, wherein the preloaded number segment is a number segment having a binding relationship with the service identifier.
[0007] Optionally, it also includes:
[0008] Each node registers its own IP and port information to ETCD in advance;
[0009] The master node obtains the registration information of all nodes from ETCD to generate and maintain a routing table, which includes the communication paths of all nodes. The master node is one of all nodes.
[0010] The master node sends the routing table to each of the other nodes.
[0011] Optionally, the first node obtains the ID allocation request, including:
[0012] The first node obtains the ID allocation request sent by the terminal, and determines itself as a target node for processing the ID allocation request based on the service identifier and the routing table;
[0013] or,
[0014] The first node obtains the ID allocation request sent by the second node, and the first node is the target node for processing the ID allocation request determined by the second node based on the service identifier and the routing table after receiving the ID allocation request.
[0015] Optionally, the determination of the target node includes:
[0016] Based on the service identifier and the routing table, a target node for processing the ID allocation request is calculated by a consistent hashing algorithm.
[0017] Optionally, the allocating an ID to the data to be identified based on the preloaded number segment includes:
[0018] Determining whether there is a target number segment corresponding to the service identifier;
[0019] If so, determine whether there is an unassigned ID in the target number segment;
[0020] If so, the first ID among the unassigned IDs in the target number segment is assigned to the data to be identified.
[0021] Optionally, the allocating an ID to the data to be identified based on the preloaded number segment includes:
[0022] If there is no target number segment corresponding to the service identifier, preload a new number segment and bind the new number segment to the service identifier;
[0023] The first ID among the unassigned IDs in the number segment bound to the service identifier is assigned to the data to be identified.
[0024] Optionally, it also includes: periodically obtaining the lease information issued by the master node, each lease information issued includes a validity period, each lease information is fixed within its corresponding validity period, and the lease information includes the routing table and the allocated number segment.
[0025] Optionally, the validity period is shorter than the period for obtaining the lease information, and the allocating an ID to the data to be identified based on the preloaded number segment includes:
[0026] When the validity period ends and the time period for obtaining new lease information has not yet arrived, if an ID allocation request is obtained, a temporary lease is requested from the master node, and an ID is allocated to the data to be identified based on the temporary lease. The temporary lease includes a routing table and an allocated number segment.
[0027] Optionally, it also includes:
[0028] If the third node in the lease information is unavailable during the validity period of the lease information, causing the ID allocation request response to fail, the ID allocation request is attempted repeatedly.
[0029] The present application also discloses an ID allocation device for a distributed system, comprising:
[0030] A request obtaining module, used to obtain an ID allocation request, wherein the ID allocation request includes a service identifier and data to be identified, and the request obtaining module is a processing module for a target node for processing the ID allocation request that can be uniquely determined based on the service identifier, and the data to be identified is data to which an ID needs to be allocated;
[0031] The ID allocation module is used to allocate an ID to the data to be identified based on a preloaded number segment, where the preloaded number segment is a number segment that has a binding relationship with the service identifier.
[0032] It can be known from the above technical scheme that the embodiment of the present application discloses a method and device for allocating IDs in a distributed system, the method comprising: a first node obtains an ID allocation request, the ID allocation request includes a service identifier and data to be identified, the first node is a target node for processing the ID allocation request that can be uniquely determined based on the service identifier, the data to be identified is data that needs to be assigned an ID; an ID is allocated to the data to be identified based on a preloaded number segment, the preloaded number segment is a number segment that has a binding relationship with the service identifier. The above scheme can uniquely determine a target node for ID allocation processing based on the service identifier of the ID allocation request obtained, and the target node can incrementally allocate IDs based on the preloaded number segment, thereby achieving monotonically increasing ID allocation and ensuring the global uniqueness of the allocated ID. BRIEF DESCRIPTION OF THE DRAWINGS
[0033] In order to more clearly illustrate the embodiments of the present application or the technical solutions in the prior art, the drawings required for use in the embodiments or the description of the prior art will be briefly introduced below. Obviously, the drawings described below are only embodiments of the present application. For ordinary technicians in this field, other drawings can be obtained based on the provided drawings without paying any creative work.
[0034] Figure 1 A flowchart of a method for allocating IDs in a distributed system disclosed in an embodiment of the present application;
[0035] Figure 2 A flowchart of node information sharing disclosed in an embodiment of the present application;
[0036] Figure 3 A schematic diagram of the implementation architecture of the ID allocation solution of the distributed system disclosed in the embodiment of the present application;
[0037] Figure 4 This is an example diagram of the ID allocation process in the traditional number segment mode;
[0038] Figure 5 A flowchart of a service node registration disclosed in an embodiment of the present application;
[0039] Figure 6 A service discovery flow chart of a node disclosed in an embodiment of the present application;
[0040] Figure 7 A schematic diagram of the structure of an ID allocation device for a distributed system disclosed in an embodiment of the present application;
[0041] Figure 8 A schematic diagram of the structure of a server disclosed in an embodiment of the present application. DETAILED DESCRIPTION
[0042] For the purpose of reference and clarity, the descriptions, abbreviations or acronyms of the technical terms used below are summarized as follows:
[0043] ETCD: ETCD is a highly available distributed key-value storage system, mainly used for sharing configuration information and service discovery.
[0044] Routing lease: The routing lease saves the IP and port information of all service nodes in the distributed system and is maintained by the master node in the cluster. The master node registers it with ETCD, forms and broadcasts the lease to ensure that the lease information will not be changed during the lease period.
[0045] Segment mode: Segment mode is a method of pre-generating and storing serial numbers. In a distributed system, each service or node can be assigned an independent segment to ensure that the serial numbers generated by each node are not repeated. The main advantage of segment mode is that it can balance the load between different nodes and reduce the pressure on the database.
[0046] The following will be combined with the drawings in the embodiments of the present application to clearly and completely describe the technical solutions in the embodiments of the present application. Obviously, the described embodiments are only part of the embodiments of the present application, not all of the embodiments. Based on the embodiments in the present application, all other embodiments obtained by ordinary technicians in this field without creative work are within the scope of protection of this application.
[0047] Figure 1 This is a flow chart of a distributed system ID allocation method disclosed in an embodiment of the present application. Figure 1 As shown, the ID allocation method of the distributed system may include:
[0048] Step 101: The first node obtains an ID allocation request, which includes a service identifier and data to be identified. The first node is a target node for processing the ID allocation request that can be uniquely determined based on the service identifier, and the data to be identified is data to which an ID needs to be allocated.
[0049] The first node may be any node in the distributed system that can provide ID allocation service. The first node obtains the ID allocation request by directly receiving the ID allocation request from the client or by receiving the ID allocation request forwarded by other processing nodes.
[0050] The service identifier in the ID allocation request can correspond to a unique service. In the present application scheme, a unique target node can be determined based on the unique service identifier, that is, the ID of the data to be identified (message or data) belonging to the same service can only be allocated by one node, and ID allocation requests for the same service at different times will all be assigned to the same node for ID allocation processing.
[0051] Step 102: Allocate an ID for the data to be identified based on a preloaded number segment, where the preloaded number segment is a number segment that has a binding relationship with the service identifier.
[0052] In the present application, the node providing ID allocation service allocates ID based on the number segment mode. The ID allocated by the number segment mode is monotonically increasing, which can ensure that the next ID is greater than the previous ID in terms of value, and can meet special requirements such as transaction version number, IM (instant messaging) incremental message, and sorting.
[0053] Specifically, the node providing ID allocation service can pre-load a number segment, such as the number segment of 001-100. Assuming that the number segment corresponds to service A, when service A sends an ID allocation request, the ID allocation request will be assigned to the node, and starting from ID 001, IDs will be assigned to the data to be identified of service A in ascending order.
[0054] Based on the above, since the ID allocation request of a fixed service can only be assigned to a fixed node for ID allocation, there will be no situation where different nodes are responsible for the ID allocation task of the same service at the same time; and each node can implement monotonically increasing ID allocation based on the number segment mode, so the global uniqueness of the allocated ID can be guaranteed, that is, there will be no duplicate ID numbers in the entire distributed system. In addition, in the solution of ID allocation in the number segment mode, since the number segment is pre-loaded by the node, the node can directly perform ID allocation after obtaining the ID allocation request without frequent interaction with the database, thereby providing an ultra-high ID allocation rate to meet the performance requirements of most application scenarios.
[0055] The ID allocation method of the distributed system described in this embodiment can uniquely determine a target node for ID allocation processing based on the service identifier of the ID allocation request obtained. The target node can perform ID allocation incrementally based on the preloaded number segment, thereby achieving monotonically increasing ID allocation and ensuring the global uniqueness of the allocated ID.
[0056] In other implementations, the ID allocation method of the distributed system may also include node information sharing, which may be completed in advance to ensure smooth subsequent data interaction between different nodes. Here, data interaction between different nodes mainly refers to the situation of forwarding ID allocation requests between nodes. Figure 2 This is a flowchart of node information sharing disclosed in the embodiment of this application. Figure 2 As shown, the content of node information sharing may include:
[0057] Step 201: Each node registers its own IP and port information to ETCD in advance.
[0058] In this application, each node that can provide ID allocation services needs to register its own IP and port information in ETCD in a unified manner, so as to realize the statistical aggregation of all node-related information. For example, the service node registers its own IP and service port in a fixed path of the ETCD registration center. For example, if the local IP is 192.168.1.1 and the port is 8080, it is concatenated into a string of "192.168.1.1:8080" and registered in the " / mgtv / id-serv / nodes / " path of ETCD.
[0059] Step 202: The master node obtains registration information of all nodes from ETCD to generate and maintain a routing table, wherein the routing table includes communication paths of all nodes, and the master node is one of all nodes.
[0060] In the specific implementation, after the service node registers its own information under the fixed path of ETCD, it can also monitor the change information such as addition and deletion of other service nodes under the path. When the node information changes, the CreateRevision attribute of the ETCD event is used to set the minimum value, that is, the first registered service node, as the primary node leader. The CreateRevision attribute is a unique attribute of ETCD. It is incremental for a certain key. When a node is registered, it will cause CreateRevision to change. The service node with the smallest CreateRevision value is the first registered service node, which is selected as the primary node in this solution. Generally speaking, the selected primary node will not be easily replaced. Only when the primary node can no longer work will a new primary node be re-elected. The aforementioned content of selecting the primary node is only an example and does not constitute an implementation restriction on the selection of the primary node.
[0061] In summary, the master node is a general management node selected from all nodes, and the master node can communicate with other nodes. The selection of the master node can be based on specific rules, such as but not limited to the earliest registration time, the best performance, etc.
[0062] In this embodiment, the master node can communicate directly with ETCD and obtain registration information of all nodes from ETCD, including IP and port information of each node. The master node generates and maintains global routing table information based on this information.
[0063] Step 203: The master node sends the routing table to each of the other nodes.
[0064] After the master node obtains the routing table, it can send the routing table to each other node so that each node can know the node cluster information, so that when the client sends an ID allocation request later, the node can obtain the node cluster information according to the routing table, and then determine the target node that needs to process the ID allocation request.
[0065] The above content is an introduction to node information sharing from the perspective of the global distributed system. From the perspective of a single service node, if the service node is not the master node, its actual process is: register its own IP and port information with ETCD, and receive the routing table from the master node. If the service node is the master node, its actual process is: register its own IP and port information with ETCD, obtain the registration information of all nodes from ETCD to generate and maintain the routing table, and send the routing table to other nodes.
[0066] Figure 3This is a schematic diagram of the implementation architecture of the ID allocation scheme of the distributed system disclosed in the embodiment of the present application, where Seq is the abbreviation of the sequence number, which is equivalent to the ID. The cluster information is updated through the notification mechanism of ETCD. If there is any change in the node information registered with ETCD, the ETCD service will synchronously push the change to the node subscribing to the event, which can be combined with Figure 3 Understand the foregoing.
[0067] In one implementation, the determination of the target node may include: based on the service identifier and the routing table, calculating by a consistent hashing algorithm the target node for processing the ID allocation request, that is, the node actually requested by the ID allocation request.
[0068] Figure 4 This is an example diagram of the ID allocation process in the traditional number segment mode. Figure 4 The number segment mode will cause ID rollback in a distributed system. For example, when a client requests ID allocation from Alloc A node, the allocated ID is 101. The second time it requests ID allocation from Alloc B node, the number segment of Alloc B node starts from 200 (if it starts from 100 like Alloc A node, it will cause ID duplication), so 201 is allocated. The third time it requests ID allocation from Alloc A node, 102 is allocated, and ID rollback occurs in the third request. That is, when the ID allocation request jumps between nodes corresponding to different number segments, the ID rollback occurs.
[0069] Based on this, the present application scheme adopts the ETCD service registration and node routing method. Each service node registers its own IP, port and other information to ETCD. The master node generates and maintains the global routing table information. When the client requests, the cluster information is obtained according to the routing table, and the actual requested node is calculated through the consistent hashing algorithm. Since ID allocation requests carrying the same business identifier will always be assigned to the same target node, the ID rollback problem caused by node jumping can be avoided.
[0070] Based on the above content, the first node in the aforementioned embodiment obtains the ID allocation request, which may include: the first node obtains the ID allocation request sent by the terminal, and determines itself as the target node for processing the ID allocation request based on the service identifier and the routing table; or, the first node obtains the ID allocation request sent by the second node, and the first node is the target node for processing the ID allocation request determined by the second node based on the service identifier and the routing table after receiving the ID allocation request. In the implementation in which the first node obtains the ID allocation request from the second node, the second node directly receives the ID allocation request from the client, and based on the service identifier, uses the consistent hashing algorithm to calculate and determine that the current ID allocation request should be processed by the first node, and then routes the current ID allocation request to the first node for processing based on the information recorded in the routing table. After the first node allocates the ID in response to the ID allocation request, it transmits the allocated ID to the second node, and the second node returns the allocated ID to the client.
[0071] The above content introduces how to determine the target node and the specific implementation of the target node (first node) obtaining the ID allocation request, which is convenient for technical personnel in the field to better understand and implement the solution of the present application.
[0072] In the aforementioned embodiment, the allocation of an ID to the data to be identified based on the preloaded number segment may include: determining whether there is a target number segment corresponding to the service identifier; if so, determining whether there is an unassigned ID in the target number segment; if so, allocating the first ID among the unassigned IDs in the target number segment to the data to be identified.
[0073] It is understandable that the first node may have previously allocated an ID for an ID allocation request with the same service identifier and established a binding relationship between the service identifier and the number segment. Therefore, when an ID allocation request is received, it is necessary to first determine whether there is a target number segment corresponding to the service identifier. If it exists and there are still unassigned IDs in the number segment, the first (smallest) ID among the unassigned IDs can be directly assigned to the data to be identified in the ID allocation request.
[0074] If there is no target number segment corresponding to the service identifier, a new number segment can be preloaded and bound to the service identifier; the first ID among the unassigned IDs in the number segment bound to the service identifier is assigned to the data to be identified.
[0075] If there is no target number segment corresponding to the service identifier currently, a new number segment needs to be bound to the service identifier so as to perform ID allocation for the ID allocation request carrying the service identifier.
[0076] In one example, when a client requests any ID allocation service node in this distributed system through grpc (grpc is a framework for remote service calls), the current service node will perform a consistent hash operation on the business biz_key of the requested ID to calculate the service node corresponding to the current biz_key. If the calculated service node is equal to itself, the local memory will be searched for the number segment corresponding to the current biz_key. If there is and the number segment has not been allocated, the allocated ID identification bits in the number segment will be accumulated through atomic operations, and the accumulated ID value will be returned; if the current number segment has been allocated, the next number segment will be preloaded, and then the allocation operation will be performed. Biz_key is just a noun, which is used to represent the unique identifier of the business party's current ID allocation request (corresponding to the business identifier). For example, if an order number is to be allocated to a user with ID 100, the biz_key can be user_order_100. This ensures that the user will be allocated to the same service node after each hash calculation to ensure data consistency.
[0077] If it is a batch ID allocation request, the current processing node can traverse all biz_keys and perform allocation operations separately. If the batch biz_key hash calculation does not belong to the processing of this node, it can request the corresponding node to process it through rpc (remote service call), and then merge the allocation results of other nodes and return them uniformly.
[0078] This embodiment provides a globally unique monotonically increasing ID allocation service to solve the problem that messages, orders, etc. in big data scenarios require globally unique IDs. It ensures ultra-high availability of the system through distributed services, and solves the allocation efficiency problem in high-concurrency scenarios through the number segment mode + batch allocation capability.
[0079] Based on the disclosed content of the aforementioned embodiment, the distributed ID allocation method may further include: periodically obtaining the lease information issued by the master node, each lease information issued includes a validity period, each lease information is fixed within its corresponding validity period, and the lease information includes the routing table and the allocated number segment. Since the lease information carries the routing table, the lease information is also referred to as a routing lease in the present application.
[0080] It is understandable that factors such as network delays, packet loss, and momentary outages cannot be completely eliminated, and the client may not be able to perceive cluster routing changes in real time; due to uncontrollable network factors, the client cannot perceive cluster status changes in real time. When the routing information changes, the client may use old routing information and route the ID allocation request to the wrong old node, resulting in ID rollback. Based on this, the present application scheme abandons the availability of seconds by introducing a lease mechanism to ensure data consistency and partition tolerance. Specifically, the lease mechanism set in this scheme has a validity period of seconds. After the lease is issued, the service that issues the lease guarantees that the lease will not change during the validity period. If the service node changes, the lease must be maintained until it expires. During this period, some changed nodes (such as deleted nodes) will be unavailable.
[0081] The master node can be used to issue routing leases. During the validity period of the lease, the routing information is guaranteed not to change. The client can make requests without worrying about dirty data problems caused by node changes. When some ID allocation nodes are unavailable, if the lease is still valid, some faulty nodes in the cluster will be unavailable, but they will still be visible to the client. At this time, some requests will fail, but the data will be completely consistent. When the routing lease expires, the leader node will send the updated routing lease to each ID allocation node and update its segment information. The client's subsequent request data will be completely consistent.
[0082] Combined with the above-mentioned service node registration and routing lease acquisition related content, Figure 5 This is a flowchart of a service node registration disclosed in an embodiment of the present application, which can be combined with Figure 5 Understand the relevant contents of the aforementioned embodiments.
[0083] In the implementation, the request forwarded by rpc to other nodes will carry the version information of the routing lease of the current node. If the routing lease version information of the target node of rpc is inconsistent with that of the current node, it means that the routing lease has changed and the routing node data has changed during the rpc process. At this time, in order to ensure data consistency, the ID allocation request should be rejected and a failure should be returned. The client needs to retry.
[0084] If the third node in the lease information is unavailable during the validity period of the lease information, causing the ID allocation request response to fail, the ID allocation request is attempted again. If the target node for the allocated route is unavailable and the route lease is valid, the client can retry the request continuously through an exponential backoff algorithm, or retry the request at a fixed interval until the request succeeds; or wait for the route lease to expire, obtain new route lease node information, and recalculate the route hash, at which time a new available node can be obtained and the subsequent ID allocation process can be executed.
[0085] After the routing lease changes, all ID allocation service nodes will clear the current number segment and reapply for a new number segment. This method will cause some IDs to jump forward. For example, after the routing lease changes, in order to ensure data consistency, the current number segment can be cleared. If the current number segment is 100-200 and is allocated to 105, and the subsequent IDs have not yet been allocated, after the clearing, the new number segment is 200-300, and the next ID allocation will be 201. At this time, a forward jump occurs, but it is guaranteed that the ID will not roll back.
[0086] Figure 6 The service discovery flowchart of the node disclosed in the embodiment of the present application also explains the process of ID allocation node registration for service. When a node is registered, it is necessary to determine whether it is the first registered node through the rversion field when it is registered to ETCD. If it is, it is selected as the master node, and the IP port and other information of the master node are registered to ETCD through the lease mechanism. Other service nodes registered subsequently are slave nodes. The slave node needs to listen to the master node lease in ETCD to determine who is the master node, and execute the logic of re-selecting the master node after sensing the subsequent changes in the master node. Combined with Figure 6 As shown in the figure, after the node registers the service with ETCD, it first determines whether it is the master node. If so, the leader information of the master node is written into the ETCD lease, and the renewal of the leader lease of the master node is maintained; if it is not the master node, the node status is set to enable it to work normally, and the leader lease information is monitored at the same time. If a lease change event is monitored, first determine whether it is a node addition event (put event). If so, write the leader lease into the local cache; if not, continue to determine whether it is a node deletion event (del event). If so, first determine whether the current lease has expired. If so, directly obtain the service list and update the node cluster information. If both are, wait for the current lease to pass and then obtain the service list and update the node cluster information; start the next round of processes from determining whether it is the master node.
[0087] In one implementation, the validity period is shorter than the period for obtaining the lease information, and the allocation of an ID for the data to be identified based on the preloaded number segment may include: at the end of the validity period and before the time period for obtaining new lease information is reached, if an ID allocation request is obtained, requesting a temporary lease from the master node, and allocating an ID for the data to be identified based on the temporary lease, wherein the temporary lease includes a routing table and an allocated number segment.
[0088] The lease mechanism is highly dependent on time. This solution uses relative time (in milliseconds, representing expiration in xx milliseconds) to eliminate the impact of time errors. When the master node issues a lease to the ID allocation node, it will reduce the actual validity period of the lease by a portion and return it to the ID allocation service node. The purpose is to make the service node's lease expire before the master node. During this idle period, the ID allocation service node needs to request a temporary lease from the master node for each allocation. A temporary lease is a lease that is only valid for the current time and has no validity period. During the idle period, in order to ensure data consistency, a new lease cannot be allocated immediately, so a temporary lease is used to allow the system to run normally without interrupting services to ensure global data consistency. This implementation avoids the problem of dirty data allocation caused by service node time lag by setting the lease information of other nodes other than the master node to expire in advance.
[0089] The distributed ID allocation method disclosed in the present application utilizes the characteristics of distributed systems. It will not lose consistency, data, or duplicate numbers during restart or failover. It can have more flexible deployment capabilities and higher performance through dynamic scaling of nodes. Compared with distributed ID allocation algorithms represented by Snowflake, it has a monotonically increasing property and avoids the problem of duplicate numbering caused by clock rollback in the Snowflake algorithm through a lease mechanism. Therefore, the present application has higher versatility and is suitable for more scenarios.
[0090] For the aforementioned method embodiments, for the sake of simplicity, they are all described as a series of action combinations, but those skilled in the art should be aware that the present application is not limited by the order of the actions described, because according to the present application, some steps can be performed in other orders or simultaneously. Secondly, those skilled in the art should also be aware that the embodiments described in the specification are all preferred embodiments, and the actions and modules involved are not necessarily required by the present application.
[0091] The method is described in detail in the embodiments disclosed in the above-mentioned application. The method of the application can be implemented by various forms of devices. Therefore, the application also discloses a device, and a specific embodiment is given below for detailed description.
[0092] Figure 7 This is a schematic diagram of the structure of an ID allocation device for a distributed system disclosed in an embodiment of the present application. Figure 7 As shown, the ID allocation device 70 of the distributed system may include:
[0093] A request obtaining module 701 is used to obtain an ID allocation request, wherein the ID allocation request includes a service identifier and data to be identified. The request obtaining module is a processing module for a target node for processing the ID allocation request that can be uniquely determined based on the service identifier, and the data to be identified is data to which an ID needs to be allocated;
[0094] The ID allocation module 702 is used to allocate an ID to the data to be identified based on a preloaded number segment, where the preloaded number segment is a number segment that has a binding relationship with the service identifier.
[0095] The ID allocation device of the distributed system described in this embodiment can uniquely determine a target node for ID allocation processing based on the service identifier of the ID allocation request obtained. The target node can perform ID allocation incrementally based on the preloaded number segment, thereby achieving monotonically increasing ID allocation and ensuring the global uniqueness of the allocated ID.
[0096] The specific implementation of the ID allocation device of the above-mentioned distributed system and the various modules it contains can be found in the content introduction of the corresponding parts of the method embodiment, which will not be repeated here.
[0097] Any one of the distributed system ID allocation devices described in the above embodiments includes a processor and a memory. The request acquisition module and the ID allocation module in the above embodiments are stored in the memory as program modules, and the processor executes the program modules stored in the memory to implement corresponding functions.
[0098] The processor includes a kernel, which retrieves the corresponding program module from the memory. One or more kernels can be set, and the processing of the access data can be realized by adjusting the kernel parameters.
[0099] The memory may include non-permanent memory in a computer-readable medium, random access memory (RAM) and / or non-volatile memory, such as read-only memory (ROM) or flash RAM, and the memory includes at least one memory chip.
[0100] In an exemplary embodiment, a computer-readable storage medium is also provided, which can be directly loaded into the internal memory of a computer and contains software code. After being loaded and executed by a computer, the computer program can implement the steps shown in any embodiment of the above-mentioned distributed ID allocation method.
[0101] In an exemplary embodiment, a computer program product is also provided, which can be directly loaded into the internal memory of a computer, and contains software code. After being loaded and executed by a computer, the computer program can implement the steps shown in any embodiment of the above-mentioned distributed ID allocation method.
[0102] Furthermore, an embodiment of the present application provides a server. Figure 8 This is a schematic diagram of the structure of a server disclosed in an embodiment of the present application. Figure 8As shown, the server includes at least one processor 801, and at least one memory 802 and a bus 803 connected to the processor; wherein the processor and the memory communicate with each other through the bus; the processor is used to call the program instructions in the memory to execute the above-mentioned distributed ID allocation method.
[0103] In this specification, each embodiment is described in a progressive manner, and each embodiment focuses on the differences from other embodiments. The same or similar parts between the embodiments can be referred to each other. For the device disclosed in the embodiment, since it corresponds to the method disclosed in the embodiment, the description is relatively simple, and the relevant parts can be referred to the method part.
[0104] It should also be noted that, in this article, relational terms such as first and second, etc. are only used to distinguish one entity or operation from another entity or operation, and do not necessarily require or imply any such actual relationship or order between these entities or operations. Moreover, the terms "include", "comprise" or any other variants thereof are intended to cover non-exclusive inclusion, so that a process, method, article or device including a series of elements includes not only those elements, but also other elements not explicitly listed, or also includes elements inherent to such process, method, article or device. In the absence of further restrictions, the elements defined by the sentence "comprise a ..." do not exclude the presence of other identical elements in the process, method, article or device including the elements.
[0105] The steps of the method or algorithm described in conjunction with the embodiments disclosed herein may be implemented directly using hardware, a software module executed by a processor, or a combination of the two. The software module may be placed in a random access memory (RAM), a memory, a read-only memory (ROM), an electrically programmable ROM, an electrically erasable programmable ROM, a register, a hard disk, a removable disk, a CD-ROM, or any other form of storage medium known in the art.
[0106] The above description of the disclosed embodiments enables those skilled in the art to implement or use the present application. Various modifications to these embodiments will be apparent to those skilled in the art, and the general principles defined herein may be implemented in other embodiments without departing from the spirit or scope of the present application. Therefore, the present application will not be limited to the embodiments shown herein, but will conform to the widest scope consistent with the principles and novel features disclosed herein.
Claims
1. A method for allocating IDs in a distributed system, characterized in that: include: The first node obtains an ID allocation request, wherein the ID allocation request includes a service identifier and data to be identified, the first node is a target node for processing the ID allocation request that can be uniquely determined based on the service identifier, and the data to be identified is data to which an ID needs to be allocated; An ID is allocated to the data to be identified based on a preloaded number segment, wherein the preloaded number segment is a number segment having a binding relationship with the service identifier.
2. The ID allocation method of a distributed system according to claim 1, characterized in that: Also includes: Each node registers its own IP and port information to ETCD in advance; The master node obtains the registration information of all nodes from ETCD to generate and maintain a routing table, which includes the communication paths of all nodes. The master node is one of all nodes. The master node sends the routing table to each of the other nodes.
3. The ID allocation method of a distributed system according to claim 2, characterized in that: The first node obtains the ID allocation request, including: The first node obtains the ID allocation request sent by the terminal, and determines itself as a target node for processing the ID allocation request based on the service identifier and the routing table; or, The first node obtains the ID allocation request sent by the second node, and the first node is the target node for processing the ID allocation request determined by the second node based on the service identifier and the routing table after receiving the ID allocation request.
4. The ID allocation method of a distributed system according to claim 2, characterized in that: The determination of the target node includes: Based on the service identifier and the routing table, a target node for processing the ID allocation request is calculated by a consistent hashing algorithm.
5. The ID allocation method of a distributed system according to claim 1, characterized in that: The allocating an ID to the data to be identified based on the preloaded number segment includes: Determining whether there is a target number segment corresponding to the service identifier; If so, determine whether there is an unassigned ID in the target number segment; If so, the first ID among the unassigned IDs in the target number segment is assigned to the data to be identified.
6. The ID allocation method of a distributed system according to claim 5, characterized in that: The allocating an ID to the data to be identified based on the preloaded number segment includes: If there is no target number segment corresponding to the service identifier, preload a new number segment and bind the new number segment to the service identifier; The first ID among the unassigned IDs in the number segment bound to the service identifier is assigned to the data to be identified.
7. The ID allocation method of a distributed system according to claim 2, characterized in that: Also includes: The lease information issued by the master node is obtained periodically, each lease information issued includes a validity period, each lease information is fixed within its corresponding validity period, and the lease information includes the routing table and the allocated number segment.
8. The ID allocation method of a distributed system according to claim 7, characterized in that: The duration of the validity period is less than the duration of the cycle for obtaining the lease information, and the assigning of an ID to the data to be identified based on the preloaded number segment includes: When the validity period ends and the time period for obtaining new lease information has not yet arrived, if an ID allocation request is obtained, a temporary lease is requested from the master node, and an ID is allocated to the data to be identified based on the temporary lease. The temporary lease includes a routing table and an allocated number segment.
9. The ID allocation method of a distributed system according to claim 7, characterized in that: Also includes: If the third node in the lease information is unavailable during the validity period of the lease information, causing the ID allocation request response to fail, the ID allocation request is attempted repeatedly.
10. An ID allocation device for a distributed system, characterized in that: include: A request obtaining module, used to obtain an ID allocation request, wherein the ID allocation request includes a service identifier and data to be identified, and the request obtaining module is a processing module for a target node for processing the ID allocation request that can be uniquely determined based on the service identifier, and the data to be identified is data to which an ID needs to be allocated; The ID allocation module is used to allocate an ID to the data to be identified based on a preloaded number segment, where the preloaded number segment is a number segment that has a binding relationship with the service identifier.
Citation Information
Cited By
Business identifier distribution method and device, electronic equipment and readable medium
CN119814544A
Petroleum asset identifier generation and self-healing management method and system
CN122534045A
