CN110888858B - Database operation method and device, storage medium and electronic device - Google Patents
Database operation method and device, storage medium and electronic device Download PDFInfo
- Publication number
- CN110888858B CN110888858B CN201911040315.8A CN201911040315A CN110888858B CN 110888858 B CN110888858 B CN 110888858B CN 201911040315 A CN201911040315 A CN 201911040315A CN 110888858 B CN110888858 B CN 110888858B
- Authority
- CN
- China
- Prior art keywords
- account
- lock
- lock resource
- target
- resource
- Prior art date
- Legal status (The legal status is an assumption and is not a legal conclusion. Google has not performed a legal analysis and makes no representation as to the accuracy of the status listed.)
- Active
Links
Images
Classifications
-
- G—PHYSICS
- G06—COMPUTING; CALCULATING OR COUNTING
- G06F—ELECTRIC DIGITAL DATA PROCESSING
- G06F16/00—Information retrieval; Database structures therefor; File system structures therefor
- G06F16/20—Information retrieval; Database structures therefor; File system structures therefor of structured data, e.g. relational data
- G06F16/21—Design, administration or maintenance of databases
-
- G—PHYSICS
- G06—COMPUTING; CALCULATING OR COUNTING
- G06F—ELECTRIC DIGITAL DATA PROCESSING
- G06F9/00—Arrangements for program control, e.g. control units
- G06F9/06—Arrangements for program control, e.g. control units using stored programs, i.e. using an internal store of processing equipment to receive or retain programs
- G06F9/46—Multiprogramming arrangements
- G06F9/50—Allocation of resources, e.g. of the central processing unit [CPU]
- G06F9/5005—Allocation of resources, e.g. of the central processing unit [CPU] to service a request
- G06F9/5027—Allocation of resources, e.g. of the central processing unit [CPU] to service a request the resource being a machine, e.g. CPUs, Servers, Terminals
- G06F9/5038—Allocation of resources, e.g. of the central processing unit [CPU] to service a request the resource being a machine, e.g. CPUs, Servers, Terminals considering the execution order of a plurality of tasks, e.g. taking priority or time dependency constraints into consideration
-
- Y—GENERAL TAGGING OF NEW TECHNOLOGICAL DEVELOPMENTS; GENERAL TAGGING OF CROSS-SECTIONAL TECHNOLOGIES SPANNING OVER SEVERAL SECTIONS OF THE IPC; TECHNICAL SUBJECTS COVERED BY FORMER USPC CROSS-REFERENCE ART COLLECTIONS [XRACs] AND DIGESTS
- Y02—TECHNOLOGIES OR APPLICATIONS FOR MITIGATION OR ADAPTATION AGAINST CLIMATE CHANGE
- Y02D—CLIMATE CHANGE MITIGATION TECHNOLOGIES IN INFORMATION AND COMMUNICATION TECHNOLOGIES [ICT], I.E. INFORMATION AND COMMUNICATION TECHNOLOGIES AIMING AT THE REDUCTION OF THEIR OWN ENERGY USE
- Y02D10/00—Energy efficient computing, e.g. low power processors, power management or thermal management
Landscapes
- Engineering & Computer Science (AREA)
- Theoretical Computer Science (AREA)
- Databases & Information Systems (AREA)
- Physics & Mathematics (AREA)
- General Engineering & Computer Science (AREA)
- General Physics & Mathematics (AREA)
- Software Systems (AREA)
- Data Mining & Analysis (AREA)
- Information Retrieval, Db Structures And Fs Structures Therefor (AREA)
Abstract
The application discloses an operation method and device of a database, a storage medium and an electronic device. Wherein the method comprises the following steps: acquiring a first request of a first account, wherein the first request is used for requesting a server to execute target operation on target data in a database, the first account is used on a client, and the database is used for providing service for the client; under the condition that the first account is allocated with the first lock resource, the second lock resource is preempted for the first account, wherein the first lock resource is used for limiting the concurrency of data operations executed by the server in a concurrency mode to be smaller than or equal to a preset threshold, and the second lock resource and the first lock resource are lock resources of different types; and executing target operation on the target data under the condition that the first account preempts the second lock resource. The method and the device solve the technical problem that the data of the databases in the related technology are inconsistent.
Description
Technical Field
The present application relates to the field of internet, and in particular, to a method and apparatus for operating a database, a storage medium, and an electronic apparatus.
Background
With the rapid development of informatization construction, an information system has become a key for enterprises to maintain business operation, the enterprises are urgent to improve the usability of the information system, ensure the continuity of business, and reduce the loss caused by disasters or faults to the greatest extent. On the other hand, the diversified business types lead to the increasing complexity of data access requirements, the rapid increase of data volume also leads to the overwhelming of database servers, and enterprises are also in urgent need of improving the operation efficiency of information systems.
In large activities (such as annual celebration of companies and shopping festival), the data reading and writing amount is extremely large, for example: when users interact, the user can initiate card exchange invitation, and invite friends to help to replace cards so as to achieve the aim that the sum of cards is a fixed number (such as 9), the friends of the user can replace cards according to the situation that the friends hold the cards, and help to complete an activity task, so that the problem that data updating is inconsistent due to the fact that a plurality of users update the same card at the same time can occur, and the cards used for replacement have the possibility of being replaced when the cards of other people are replaced, and at the moment, the consistency of a plurality of data updating needs to be ensured.
In view of the above problems, no effective solution has been proposed at present.
Disclosure of Invention
The embodiment of the application provides a database operation method and device, a storage medium and an electronic device, which are used for at least solving the technical problem of inconsistent data of a database in the related technology.
According to an aspect of an embodiment of the present application, there is provided a method for operating a database, including: acquiring a first request of a first account, wherein the first request is used for requesting a server to execute target operation on target data in a database, the first account is used on a client, and the database is used for providing service for the client; under the condition that the first account is allocated with the first lock resource, the second lock resource is preempted for the first account, wherein the first lock resource is used for limiting the concurrency of data operations executed by the server in a concurrency mode to be smaller than or equal to a preset threshold, and the second lock resource and the first lock resource are lock resources of different types; and executing target operation on the target data under the condition that the first account preempts the second lock resource.
According to another aspect of the embodiments of the present application, there is also provided an operation device for a database, including: the system comprises an acquisition unit, a server and a database, wherein the acquisition unit is used for acquiring a first request of a first account, the first request is used for requesting the server to execute target operation on target data in the database, the first account is used for being used on a client, and the database is used for providing service for the client; the preemption unit is used for preempting second lock resources for the first account under the condition that the first lock resources are allocated for the first account, wherein the first lock resources are used for limiting the concurrency of data operations executed by the server in a concurrent mode to be smaller than or equal to a preset threshold, and the second lock resources and the first lock resources are lock resources of different types; and the operation unit is used for executing target operation on the target data under the condition that the first account is preempted to the second lock resource.
According to another aspect of the embodiments of the present application, there is also provided a storage medium including a stored program that when executed performs the above-described method.
According to another aspect of the embodiments of the present application, there is also provided an electronic device including a memory, a processor, and a computer program stored on the memory and executable on the processor, the processor executing the method described above by the computer program.
In the embodiment of the application, a first request for acquiring a first account is adopted, the first request is used for requesting a server to execute target operation on target data in a database, the first account is used on a client, and the database is used for providing service for the client; under the condition that the first account is allocated with the first lock resource, the second lock resource is preempted for the first account, the first lock resource is used for limiting the concurrency quantity of data operations executed by the server in concurrency to be smaller than or equal to a preset threshold, and the second lock resource and the first lock resource are lock resources of different types; under the condition that the first account is preempted to the second lock resource, the mode of executing target operation on target data is used for limiting the concurrency of data operation on each server through the number of the first lock resources, and the operation authority of the target data is obtained through the operation number of the second lock resource on the same target data in the form of preempting the lock resources, so that the technical problem of inconsistent data update of databases in the related art is solved, wherein the problem is solved when a large number of requests simultaneously request to operate the same target data.
Drawings
The accompanying drawings, which are included to provide a further understanding of the application and are incorporated in and constitute a part of this application, illustrate embodiments of the application and together with the description serve to explain the application and do not constitute an undue limitation to the application. In the drawings:
FIG. 1 is a schematic diagram of a hardware environment of a method of operation of a database according to an embodiment of the present application;
FIG. 2 is a flow chart of an alternative method of operation of a database according to an embodiment of the present application;
FIG. 3 is a schematic diagram of an alternative server and database system according to an embodiment of the present application;
FIG. 4 is a flow chart of an alternative method of operation of a database according to an embodiment of the present application;
FIG. 5 is a schematic diagram of an alternative database operating device according to an embodiment of the present application;
and
Fig. 6 is a block diagram of a terminal according to an embodiment of the present application.
Detailed Description
In order to make the present application solution better understood by those skilled in the art, the following description will be made in detail and with reference to the accompanying drawings in the embodiments of the present application, it is apparent that the described embodiments are only some embodiments of the present application, not all embodiments. All other embodiments, which can be made by one of ordinary skill in the art based on the embodiments herein without making any inventive effort, shall fall within the scope of the present application.
It should be noted that the terms "first," "second," and the like in the description and claims of the present application and the above figures are used for distinguishing between similar objects and not necessarily for describing a particular sequential or chronological order. It is to be understood that the data so used may be interchanged where appropriate such that embodiments of the present application described herein may be implemented in sequences other than those illustrated or otherwise described herein. Furthermore, the terms "comprises," "comprising," and "having," and any variations thereof, are intended to cover a non-exclusive inclusion, such that a process, method, system, article, or apparatus that comprises a list of steps or elements is not necessarily limited to those steps or elements expressly listed but may include other steps or elements not expressly listed or inherent to such process, method, article, or apparatus.
First, partial terms or terminology appearing in describing embodiments of the present application are applicable to the following explanation:
redis (full name: remote Dictionary Server remote dictionary service) is an open source log-type, key-Value database written in ANSI C language, supporting network, capable of being based on memory and persistent, and provides multiple language APIs.
Cookies, sometimes referred to as Cookies in their plural form, refer to data (typically encrypted) stored on a user's local terminal by some web sites for purposes of identifying and tracking the user's identity.
According to an aspect of the embodiments of the present application, a method embodiment of an operation method of a database is provided.
Alternatively, in the present embodiment, the above-described operation method of the database may be applied to a hardware environment constituted by the terminal 101 and the server 103 as shown in fig. 1. As shown in fig. 1, the server 103 is connected to the terminal 101 through a network, which may be used to provide services (e.g., game services, multimedia services, etc.) to the terminal or clients installed on the terminal, and a database 105 may be provided on the server or independent of the server, for providing data storage services to the server 103, where the network includes, but is not limited to: the terminal 101 is not limited to a PC, a mobile phone, a tablet computer, or the like. The method of operating the database according to the embodiment of the present application may be performed by the server 103, or may be performed jointly by the server 103 and the terminal 101.
FIG. 2 is a flowchart of an alternative database operation method according to an embodiment of the present application, as shown in FIG. 2, which may include the steps of:
Step S202, a first request of a first account is acquired, wherein the first request is used for requesting a server to execute target operation on target data in a database, the first account is used on a client, and the database is used for providing service for the client.
The client is a client installed on the terminal, and may be a multimedia content client, a game client, a live broadcast client, a communication client, etc., and a video client is taken as an example for the following explanation; the first account number, that is, the account number used in the client may be a mobile phone number, an instant messaging account number, a temporary account number, etc.; the database is used for providing data required by service for the client, and comprises the target data; the target operation includes operations such as updating, deleting, replacing, etc. the target data.
In step S204, under the condition that it is determined that the first account is allocated with the first lock resource, the second lock resource is preempted for the first account, where the first lock resource is used to limit the concurrency of data operations executed concurrently by the server to be less than or equal to a preset threshold, and the second lock resource and the first lock resource are lock resources of different types.
The number of the first lock resources on each server defines the concurrency of data operations executed concurrently by the server, for example, there are N first lock resources, where the concurrency is N (where N is an integer greater than or equal to 1, such as 5); for the first account, only if the first lock resource is obtained, the next operation can be performed, namely the second lock resource is preempted, and the target operation is performed after the second lock resource is preempted; the data operation is an operation on the data (including the above-described target operation), such as modification, deletion, addition, replenishment, and the like.
The lock resource is equivalent to a virtual lock simulating an "entity lock", the locked object is "data", the virtual lock can be managed by software, for example, a specific example is a program code running in a new form in JAVA and having logic functions of "generating lock, distributing lock, recovering lock", and the like.
Step S206, executing target operation on the target data under the condition that the first account is preempted to the second lock resource.
Through the steps, the concurrency of data operation on each server is limited through the number of the first lock resources, the operation authority of the target data is obtained through the operation number of the second lock resources to the same target data in a preemptive lock resource mode, the problem that data updating is inconsistent when a large number of requests simultaneously request to operate the same target data is avoided, and the technical problem of data inconsistency of a database in the related art can be solved. The technical solution of the present application is further described in detail below in connection with the steps shown in fig. 2.
In the technical solution provided in step S202, a first request of a first account is obtained, where the first request is used to request a server to execute a target operation on target data in a database.
The database is a distributed database including, but not limited to, key-value NoSQL type databases (e.g., redis Riak et al), column family NoSQL type databases (also known as wide column store, e.g., hbase Cassandra et al), document NoSQL (e.g., mong., mobb et al). The database is described below as a Redis example.
In the technical solution provided in step S204, if it is determined that the first account is allocated with the first lock resource, the second lock resource is preempted for the first account.
For a single server, before determining a target instance in a plurality of instances, generating sequentially self-increasing lock identification IDs (such as 1 to N), generating the plurality of instances according to a preset threshold (namely N), and limiting the concurrency of data operations executed by the server concurrently through the preset threshold; the multiple instances and the lock identifications corresponding to each of the multiple instances are stored in a hash container (e.g., concurrentHashMap).
The ConcurrentHashMap container is internally provided with a plurality of locks (namely lock resources), each lock is used for locking a part of data (namely data corresponding to the lock resources) in the container, so that when a multithread accesses the data of different data segments in the container, lock competition does not exist among threads, thereby effectively improving concurrent access efficiency, namely the lock segmentation technology, firstly, the data is stored in a segment by segment, then each segment of data is provided with a lock, and when one thread occupies the lock to access one segment of data, the data of other segments can be accessed by other threads.
In the above embodiment, the first account may acquire the first lock resource according to the following steps S2042-S2044:
in step S2042, a target instance among a plurality of instances, which are instances allocated for the first account, is determined, the plurality of instances being instances generated on the server for allocating the first lock resource.
Optionally, when determining the target instance of the plurality of instances, in a case where there is an instance that has been allocated to the first account (in other words, the first account has previously initiated a request, the request is a request that has been repeatedly initiated, and the instance has been allocated to the request before the first account) among the plurality of instances, the instance that has been allocated to the first account is taken as the target instance; and under the condition that no instance which is allocated to the first account exists in the plurality of instances, allocating a target instance to the first account from the plurality of instances, wherein a lock identifier corresponding to the target instance is matched with the account identifier of the first account, for example, taking a model of an account identifier hashcode of the first account to obtain a lock identifier ID, and taking the instance of the lock identifier of the lock ID as the target instance.
In step S2044, the first lock resource to which the target instance belongs is allocated to the first account.
Optionally, when the first lock resource to which the target instance belongs is allocated to the first account, a first preemption request is sent to the target instance, where the first preemption request is used to request the target instance to allocate the first lock resource for the first account; and under the condition that the received first response message of the target instance indicates that the preemption is successful, distributing the first lock resource indicated by the first response message to the first account, and under the condition that the first response message indicates that the preemption is failed, returning a prompt of operation failure to the first account.
Similar to the aforementioned preempting the first lock resource, preempting the second lock resource for the first account includes: a second preemption request is sent to the database, and the second preemption request is used for requesting the database to allocate a second lock resource corresponding to the target data for the first account; and under the condition that the second response message of the received database indicates that the preemption is successful, distributing the second lock resource indicated by the second response message to the first account, and under the condition that the second response message indicates that the preemption is failed, returning a prompt of operation failure to the first account.
Optionally, the second lock resource is adapted to the type of the database, e.g. when the database is a dis, the second lock resource may be a setnx implemented lock resource; when the database is Hbase, the second lock resource may be a lock resource implemented by a ReadWriteLock class in jdk, and the other types of databases are similar to the first lock resource, and the description is continued by taking the example that the database is Redis and the second lock resource is a lock resource implemented by setnx. The second lock resource is of a different type than the first lock resource, the second lock resource being used for a distributed database, such as setnx, and the first lock resource being used for a server, such as ReentrantLock.
In the technical solution provided in step S206, under the condition that the first account preemptively occupies the second lock resource, the target operation is performed on the target data, for example, the operations of updating, exchanging, deleting, etc. the card are performed.
Optionally, after the target operation is performed on the target data, the association between the first lock resource and the second lock resource and the first account is released, and the first lock resource and the second lock resource which are released from the association with the first account are allowed to be preempted by any other account, so that the first account can also reinitiate new preemption.
The technical scheme of the method and the device can be used for data updating in a high concurrency scene, and the problem of how to guarantee data updating consistency by combining a first type lock (ReentintLock) at a server end with a Redis distributed lock (such as setnx) is solved.
As an alternative embodiment, the following description will take an example of applying the technical solution of the present application to ensuring consistency of card data as follows:
if only a distributed lock, such as a setnx mode of Redis, is adopted, the smooth proceeding of the behavior of the multi-request preemption lock can be ensured under the condition of low concurrency, but the situation that the same card is used twice or more times occasionally occurs in the pressure test process of the server, which means that under the high concurrency scene, the situation that the data preempting one Redis distributed lock is inconsistent due to multiple preemptions possibly occurs in the multi-machine synchronization process in the Redis cluster is required to be solved.
In order to ensure consistency of multiple data updates in a high concurrency scenario, in a system architecture as shown in fig. 3, a server cluster (including 2 servers is taken as an example), and a Redis database cluster (including a master and a slave are taken as examples), under the condition of adding a Redis distributed lock used in a service, the concurrency condition inside a single server is limited by adding the ReentintLock in the single server, and before attempting to preempt the Redis distributed lock, the corresponding ReentintLock (i.e. a lock in a container or a lock resource) needs to be preempted, so that control is performed on the concurrency condition on the single server, and accuracy of data update storage and stability of the service are ensured. The specific implementation steps are as shown in fig. 4:
on a single server, a certain number of retortlock examples (i.e. a plurality of examples) are generated in advance after the service is started, for example, lock ids which are sequentially increased from 0 are generated, each lock id correspondingly generates one retortlock example, and the lock ids and the retortlock examples are stored in ConcurrentHashMap so as to utilize the characteristic that ConcurrentHashMap is suitable for a high concurrency scene.
In step S402, the user accesses an interface, for example, a data update interface provided by the server when the user updates the card.
Step S404, searching a pre-generated lock in the ConcurrentHashMap container.
When a user performs card digital updating operation, a corresponding retartlock instance is needed to be obtained first, a login user id (i.e. an account number of the user) is obtained through a user cookie, a lock id is obtained by taking a module according to the hash code hash of the login user id and the number of retartlocks, a retartlock instance corresponding to the lock id is obtained through ConcurrentHashMap, for example, the user id is 123, the number of retartlocks is 10, and the result after taking the module is 3, then an attempt is made to assign a lock with the lock id of 3 to the user.
Step S406, after obtaining the ReetrantLock instance, attempting to preempt the first lock resource, and solving the problem of excessive concurrency of a single server.
By means of the instruction ReetrantLock.tryLock () to preempt lock resources, subsequent operations can only be performed if the preemption of lock resources is successful.
It should be noted that, if only the distributed lock is used, under the condition of higher concurrency, under the condition that a single server instantaneously has a plurality of requests, the distributed lock of Redis is obtained at the same time, so that dirty data is generated, and therefore, the ReentintRetlock is added into the single server, the concurrency problem on the single service is controlled, and in order to ensure the accuracy of data and prevent the condition of updating the same data at the same time, concurrency is controlled by preempting the lock; redis distributed locks are continuously used among a plurality of servers, so that the problem of concurrent data updating of services is solved.
In step S408, in case of failure of the preemption lock, the corresponding operation failure code is returned directly for service performance.
In step S410, under the condition that the preemption lock is successful, the first lock resource rettrantlock is acquired.
Step S412, under the condition that the acquisition of the first lock resource ReetrantLock fails, the corresponding operation failure code is directly returned, and the ReetrantLock lock resource is released.
In step S414, after the preemption of the retranstlock lock is successful, the preemption operation of the rets distributed lock is continued, i.e. the second preemption is tried, so as to solve the problem of excessive concurrency among multiple servers in service.
By means of setnx of Redis, the data updating and storing operation can be performed only when the Redis distributed lock corresponding to the card data id to be modified is preempted successfully.
Step S416, under the condition that the preemption of the Redis distributed lock fails, the corresponding operation failure code is directly returned, and the distributed lock resource and the ReetrantLock of the Redis are released.
Step S418, under the condition that the preemption of the Redis distributed lock is successful, the Redis distributed lock corresponding to the card data id to be modified is obtained.
Step S420, when obtaining the Redis distributed lock corresponding to the card data id to be modified, if the obtaining fails due to the abnormality, returning a corresponding operation failure code, and releasing the distributed lock resource of the Redis and the ReetrantLock.
Step S422, after obtaining the Redis distributed lock corresponding to the card data id to be modified, updating the data.
If the card exchange number operation of two users is involved, the operation needs to be carried out on the two updated users, so that the consistency of the updating of the two card numbers is ensured.
In step S424, after the operation, whether successful or unsuccessful, the locks that have preempted the success need to be sequentially released.
In Redis, the distributed lock data corresponding to the card id is cleared, and an unLock () method is performed in the ReetrantLock instance to release the ReetrantLock.
The above embodiments are described taking a card game as an example, in which a user can obtain cards after participating in the game, and a lottery is conducted by exchanging cards with each other to achieve a condition. The scheme of the application can be used for controlling concurrency control in a card changing process, and the scheme is also applicable to the technical scheme of the application because other scenes requiring concurrency updating of the same data are needed to be described.
The scheme realizes the data consistency update under the high concurrency scene (such as various end products of video, reading, social, education, finance and the like and application thereof) by using the Reentinlock and Redis distributed locks.
It should be noted that, for simplicity of description, the foregoing method embodiments are all expressed as a series of action combinations, but it should be understood by those skilled in the art that the present application is not limited by the order of actions described, as some steps may be performed in other order or simultaneously in accordance with the present application. Further, those skilled in the art will also appreciate that the embodiments described in the specification are all preferred embodiments, and that the acts and modules referred to are not necessarily required in the present application.
From the description of the above embodiments, it will be clear to a person skilled in the art that the method according to the above embodiments may be implemented by means of software plus the necessary general hardware platform, but of course also by means of hardware, but in many cases the former is a preferred embodiment. Based on such understanding, the technical solution of the present application may be embodied essentially or in a part contributing to the prior art in the form of a software product stored in a storage medium (such as ROM/RAM, magnetic disk, optical disk), comprising several instructions for causing a terminal device (which may be a mobile phone, a computer, a server, or a network device, etc.) to perform the method described in the embodiments of the present application.
According to another aspect of the embodiments of the present application, there is also provided an operating device for a database for implementing the operating method of the database. FIG. 5 is a schematic diagram of an alternative database operating device, as shown in FIG. 5, according to an embodiment of the present application, which may include:
an obtaining unit 501, configured to obtain a first request for requesting a server to perform a target operation on target data in a database, where the first request is used on a client, and the database is used to provide a service for the client;
A preempting unit 503, configured to preempt, when it is determined that a first lock resource is allocated to the first account, a second lock resource for the first account, where the first lock resource is used to limit a concurrency amount of data operations that are executed concurrently by the server to be less than or equal to a preset threshold, and the second lock resource and the first lock resource are lock resources of different types;
an operation unit 505, configured to perform a target operation on the target data in a case where the first account preemptively occupies the second lock resource.
It should be noted that, the acquiring unit 501 in this embodiment may be used to perform step S202 in the embodiment of the present application, the preempting unit 503 in this embodiment may be used to perform step S204 in the embodiment of the present application, and the operating unit 505 in this embodiment may be used to perform step S206 in the embodiment of the present application.
It should be noted that the above modules are the same as examples and application scenarios implemented by the corresponding steps, but are not limited to what is disclosed in the above embodiments. It should be noted that the above modules may be implemented in software or hardware as a part of the apparatus in the hardware environment shown in fig. 1.
Through the module, the concurrency of data operation on each server is limited through the number of the first lock resources, the operation authority of the target data is obtained through the operation number of the second lock resources to the same target data in a preemptive lock resource mode, the problem that data updating is inconsistent when a large number of requests simultaneously request to operate the same target data is avoided, and the technical problem of data inconsistency of a database in the related art can be solved.
Alternatively, the preemption unit may include: a determining module, configured to determine a target instance from a plurality of instances, where the target instance is an instance allocated for the first account, and the plurality of instances are instances generated on the server and used for allocating the first lock resource; and the allocation module is used for allocating the first lock resource to which the target instance belongs to the first account.
Optionally, the determining module is further operable to: in the case that an instance allocated to the first account exists among the plurality of instances, taking the instance allocated to the first account as a target instance; and under the condition that no instance which is allocated to the first account exists in the plurality of instances, allocating the target instance to the first account from the plurality of instances, wherein the lock identifier corresponding to the target instance is matched with the account identifier of the first account.
Optionally, the allocation module is further operable to: sending a first preemption request to a target instance, wherein the first preemption request is used for requesting the target instance to allocate a first lock resource for a first account; and in the case that the received first response message of the target instance indicates that the preemption is successful, allocating the first lock resource indicated by the first response message to the first account.
Optionally, the device of the present application may further include a generating unit, configured to generate, before determining the target instance of the multiple instances, the multiple instances according to a preset threshold, where the preset threshold is used to define a concurrency amount of data operations that are executed concurrently by the server; the multiple instances and the lock identifications corresponding to each of the multiple instances are stored.
Optionally, the preemption unit is further operable to: sending a second preemption request to the database, wherein the second preemption request is used for requesting the database to allocate a second lock resource corresponding to the target data for the first account; and in the case that the received second response message of the database indicates that the preemption is successful, allocating the second lock resource indicated by the second response message to the first account.
Optionally, the device of the present application may further include a releasing unit, configured to release the association between the first lock resource, the second lock resource and the first account after the target operation is performed on the target data, where the first lock resource and the second lock resource that are released from the association with the first account are allowed to be preempted by the second account.
It should be noted that the above modules are the same as examples and application scenarios implemented by the corresponding steps, but are not limited to what is disclosed in the above embodiments. It should be noted that the above modules may be implemented in software or in hardware as part of the apparatus shown in fig. 1, where the hardware environment includes a network environment.
According to another aspect of the embodiments of the present application, there is also provided a server or a terminal for implementing the above-mentioned method of operating a database.
Fig. 6 is a block diagram of a terminal according to an embodiment of the present application, and as shown in fig. 6, the terminal may include: one or more (only one is shown in fig. 6) processors 601, memory 603, and transmission means 605, as shown in fig. 6, the terminal may further comprise an input output device 607.
The memory 603 may be configured to store software programs and modules, such as program instructions/modules corresponding to the methods and apparatuses for operating a database in the embodiments of the present application, and the processor 601 executes the software programs and modules stored in the memory 603, thereby performing various functional applications and data processing, that is, implementing the methods for operating a database as described above. Memory 603 may include high-speed random access memory, and may also include non-volatile memory, such as one or more magnetic storage devices, flash memory, or other non-volatile solid state memory. In some examples, the memory 603 may further include memory remotely located with respect to the processor 601, which may be connected to the terminal through a network. Examples of such networks include, but are not limited to, the internet, intranets, local area networks, mobile communication networks, and combinations thereof.
The transmission device 605 is used to receive or transmit data via a network, and may also be used for data transmission between the processor and the memory. Specific examples of the network described above may include wired networks and wireless networks. In one example, the transmission device 605 includes a network adapter (Network Interface Controller, NIC) that may be connected to other network devices and routers via a network cable to communicate with the internet or a local area network. In one example, the transmission device 605 is a Radio Frequency (RF) module that is configured to communicate wirelessly with the internet.
In particular, the memory 603 is used to store applications.
The processor 601 may call an application program stored in the memory 603 through the transmission means 605 to perform the steps of:
acquiring a first request of a first account, wherein the first request is used for requesting a server to execute target operation on target data in a database, the first account is used on a client, and the database is used for providing service for the client;
under the condition that the first account is allocated with the first lock resource, the second lock resource is preempted for the first account, wherein the first lock resource is used for limiting the concurrency of data operations executed by the server in a concurrency mode to be smaller than or equal to a preset threshold, and the second lock resource and the first lock resource are lock resources of different types;
And executing target operation on the target data under the condition that the first account preempts the second lock resource.
The processor 601 is further configured to perform the steps of:
determining a target instance of a plurality of instances, wherein the target instance is an instance allocated for the first account, and the plurality of instances are instances generated on the server for allocating the first lock resource;
and distributing the first lock resource to which the target instance belongs to the first account.
According to the method and the device for obtaining the first account, a first request for obtaining the first account is adopted, the first request is used for requesting a server to execute target operation on target data in a database, the first account is used on a client, and the database is used for providing service for the client; under the condition that the first account is allocated with the first lock resource, the second lock resource is preempted for the first account, the first lock resource is used for limiting the concurrency quantity of data operations executed by the server in concurrency to be smaller than or equal to a preset threshold, and the second lock resource and the first lock resource are lock resources of different types; under the condition that the first account is preempted to the second lock resource, the mode of executing target operation on target data is used for limiting the concurrency of data operation on each server through the number of the first lock resources, and the operation authority of the target data is obtained through the operation number of the second lock resource on the same target data in the form of preempting the lock resources, so that the technical problem of inconsistent data update of databases in the related art is solved, wherein the problem is solved when a large number of requests simultaneously request to operate the same target data.
Alternatively, specific examples in this embodiment may refer to examples described in the foregoing embodiments, and this embodiment is not described herein.
It will be appreciated by those skilled in the art that the structure shown in fig. 6 is only illustrative, and the terminal may be a smart phone (such as an Android phone, an iOS phone, etc.), a tablet computer, a palmtop computer, a mobile internet device (Mobile Internet Devices, MID), a PAD, etc. Fig. 6 is not limited to the structure of the electronic device. For example, the terminal may also include more or fewer components (e.g., network interfaces, display devices, etc.) than shown in fig. 6, or have a different configuration than shown in fig. 6.
Those of ordinary skill in the art will appreciate that all or part of the steps in the various methods of the above embodiments may be implemented by a program for instructing a terminal device to execute in association with hardware, the program may be stored in a computer readable storage medium, and the storage medium may include: flash disk, read-Only Memory (ROM), random-access Memory (Random Access Memory, RAM), magnetic or optical disk, and the like.
Embodiments of the present application also provide a storage medium. Alternatively, in the present embodiment, the above-described storage medium may be used for executing the program code of the operation method of the database.
Alternatively, in this embodiment, the storage medium may be located on at least one network device of the plurality of network devices in the network shown in the above embodiment.
Alternatively, in the present embodiment, the storage medium is configured to store program code for performing the steps of:
acquiring a first request of a first account, wherein the first request is used for requesting a server to execute target operation on target data in a database, the first account is used on a client, and the database is used for providing service for the client;
under the condition that the first account is allocated with the first lock resource, the second lock resource is preempted for the first account, wherein the first lock resource is used for limiting the concurrency of data operations executed by the server in a concurrency mode to be smaller than or equal to a preset threshold, and the second lock resource and the first lock resource are lock resources of different types;
and executing target operation on the target data under the condition that the first account preempts the second lock resource.
Optionally, the storage medium is further arranged to store program code for performing the steps of:
determining a target instance of a plurality of instances, wherein the target instance is an instance allocated for the first account, and the plurality of instances are instances generated on the server for allocating the first lock resource;
And distributing the first lock resource to which the target instance belongs to the first account.
Alternatively, specific examples in this embodiment may refer to examples described in the foregoing embodiments, and this embodiment is not described herein.
Alternatively, in the present embodiment, the storage medium may include, but is not limited to: a U-disk, a Read-Only Memory (ROM), a random access Memory (RAM, random Access Memory), a removable hard disk, a magnetic disk, or an optical disk, or other various media capable of storing program codes.
The foregoing embodiment numbers of the present application are merely for describing, and do not represent advantages or disadvantages of the embodiments.
The integrated units in the above embodiments may be stored in the above-described computer-readable storage medium if implemented in the form of software functional units and sold or used as separate products. Based on such understanding, the technical solution of the present application may be embodied in essence or a part contributing to the prior art or all or part of the technical solution in the form of a software product stored in a storage medium, including several instructions to cause one or more computer devices (which may be personal computers, servers or network devices, etc.) to perform all or part of the steps of the methods described in the various embodiments of the present application.
In the foregoing embodiments of the present application, the descriptions of the embodiments are emphasized, and for a portion of this disclosure that is not described in detail in this embodiment, reference is made to the related descriptions of other embodiments.
In several embodiments provided in the present application, it should be understood that the disclosed client may be implemented in other manners. The above-described embodiments of the apparatus are merely exemplary, and the division of the units, such as the division of the units, is merely a logical function division, and may be implemented in another manner, for example, multiple units or components may be combined or may be integrated into another system, or some features may be omitted, or not performed. Alternatively, the coupling or direct coupling or communication connection shown or discussed with each other may be through some interfaces, units or modules, or may be in electrical or other forms.
The units described as separate units may or may not be physically separate, and units shown as units may or may not be physical units, may be located in one place, or may be distributed on a plurality of network units. Some or all of the units may be selected according to actual needs to achieve the purpose of the solution of this embodiment.
In addition, each functional unit in each embodiment of the present application may be integrated in one processing unit, or each unit may exist alone physically, or two or more units may be integrated in one unit. The integrated units may be implemented in hardware or in software functional units.
The foregoing is merely a preferred embodiment of the present application and it should be noted that modifications and adaptations to those skilled in the art may be made without departing from the principles of the present application and are intended to be comprehended within the scope of the present application.
Claims (10)
1. A method of operating a database, comprising:
acquiring a first request of a first account, wherein the first request is used for requesting a server to execute target operation on target data in a database, the first account is used on a client, and the database is used for providing service for the client;
under the condition that a first lock resource is allocated to the first account, a second lock resource is preempted for the first account, wherein the first lock resource is used for limiting the concurrency quantity of data operation which is executed by the server in a concurrency mode to be smaller than or equal to a preset threshold, the second lock resource and the first lock resource are lock resources of different types, the first lock resource is a plurality of ReetrantLock examples which are generated in advance and used for controlling the concurrency quantity on the server, and the quantity of the ReetrantLock examples is consistent with the quantity of the concurrency quantity to be controlled; generating a plurality of retranstlock instances includes: generating lock ids which are sequentially self-increased from 0 after the service is started, wherein each lock id correspondingly generates a ReetrantLock instance; the allocating the first lock resource for the first account includes: the hash code of the first account is subjected to modulo operation to obtain a lock id, and a ReetrantLock instance corresponding to the lock id is allocated to the first account;
Executing the target operation on the target data under the condition that the first account is preempted to the second lock resource;
after the target operation is executed on the target data, the association between the first lock resource, the second lock resource and the first account is released, and the first lock resource and the second lock resource which are released from the association with the first account are allowed to be preempted by any other account.
2. The method of claim 1, further comprising allocating the first lock resource for the first account number as follows:
determining a target instance of a plurality of instances, wherein the target instance is an instance allocated for the first account, the plurality of instances being instances generated on the server for allocating the first lock resource;
and distributing the first lock resource to which the target instance belongs to the first account.
3. The method of claim 2, wherein determining a target instance of the plurality of instances comprises:
in the case that an instance allocated to the first account exists in the plurality of instances, taking the instance allocated to the first account as the target instance;
And under the condition that the plurality of instances do not exist the instances allocated to the first account, allocating the target instance to the first account from the plurality of instances, wherein the lock identifier corresponding to the target instance is matched with the account identifier of the first account.
4. The method of claim 2, wherein assigning the first lock resource to which the target instance belongs to the first account comprises:
sending a first preemption request to the target instance, wherein the first preemption request is used for requesting the target instance to allocate the first lock resource for the first account;
and under the condition that the received first response message of the target instance indicates that the preemption is successful, allocating the first lock resource indicated by the first response message to the first account.
5. The method of claim 2, wherein prior to determining the target instance of the plurality of instances, the method comprises:
generating the plurality of instances according to the preset threshold, wherein the preset threshold is used for limiting the concurrency of the data operation executed by the server concurrently;
and saving the plurality of instances and the lock identification corresponding to each instance in the plurality of instances.
6. The method of any of claims 1-5, wherein preempting the second lock resource for the first account comprises:
sending a second preemption request to the database, wherein the second preemption request is used for requesting the database to allocate the second lock resource corresponding to the target data for the first account;
and under the condition that the received second response message of the database indicates that the preemption is successful, allocating the second lock resource indicated by the second response message to the first account.
7. The method of any one of claims 1 to 5, wherein after performing the target operation on the target data, the method further comprises:
and releasing the association between the first lock resource and the first account, wherein the first lock resource and the second lock resource which are released from the association between the first account are allowed to be preempted by a second account.
8. An operating device for a database, comprising:
the system comprises an acquisition unit, a server and a database, wherein the acquisition unit is used for acquiring a first request of a first account, the first request is used for requesting the server to execute target operation on target data in the database, the first account is used for being used on a client, and the database is used for providing service for the client;
A preemption unit, configured to preempt a second lock resource for the first account when it is determined that the first account is allocated with a first lock resource, where the first lock resource is used to define that a concurrency amount of data operations performed concurrently by the server is less than or equal to a preset threshold, the second lock resource and the first lock resource are lock resources of different types, and the first lock resource is a plurality of retranstlock instances that are generated in advance on the server and used to control the concurrency amount, and the number of retranstlock instances is consistent with a number of concurrency amounts to be controlled; generating a plurality of retranstlock instances includes: generating lock ids which are sequentially self-increased from 0 after the service is started, wherein each lock id correspondingly generates a ReetrantLock instance; the allocating the first lock resource for the first account includes: the hash code of the first account is subjected to modulo operation to obtain a lock id, and a ReetrantLock instance corresponding to the lock id is allocated to the first account;
the operation unit is used for executing the target operation on the target data under the condition that the first account is preempted to the second lock resource;
and the releasing unit is used for releasing the association between the first lock resource and the first account after the target operation is executed on the target data, and the first lock resource and the second lock resource which are released from the association with the first account are allowed to be preempted by any other accounts.
9. A storage medium comprising a stored program, wherein the program when run performs the method of any one of the preceding claims 1 to 7.
10. An electronic device comprising a memory, a processor and a computer program stored on the memory and executable on the processor, characterized in that the processor performs the method of any of the preceding claims 1 to 7 by means of the computer program.
Priority Applications (1)
Application Number | Priority Date | Filing Date | Title |
---|---|---|---|
CN201911040315.8A CN110888858B (en) | 2019-10-29 | 2019-10-29 | Database operation method and device, storage medium and electronic device |
Applications Claiming Priority (1)
Application Number | Priority Date | Filing Date | Title |
---|---|---|---|
CN201911040315.8A CN110888858B (en) | 2019-10-29 | 2019-10-29 | Database operation method and device, storage medium and electronic device |
Publications (2)
Publication Number | Publication Date |
---|---|
CN110888858A CN110888858A (en) | 2020-03-17 |
CN110888858B true CN110888858B (en) | 2023-06-30 |
Family
ID=69746606
Family Applications (1)
Application Number | Title | Priority Date | Filing Date |
---|---|---|---|
CN201911040315.8A Active CN110888858B (en) | 2019-10-29 | 2019-10-29 | Database operation method and device, storage medium and electronic device |
Country Status (1)
Country | Link |
---|---|
CN (1) | CN110888858B (en) |
Families Citing this family (7)
Publication number | Priority date | Publication date | Assignee | Title |
---|---|---|---|---|
CN112817995B (en) * | 2021-02-22 | 2023-09-15 | 北京奇艺世纪科技有限公司 | Data processing method and device, electronic equipment and storage medium |
CN113836161B (en) * | 2021-09-30 | 2023-11-03 | 紫光云技术有限公司 | Method for realizing timed task atomicity by utilizing database lock |
CN113986850B (en) * | 2021-10-27 | 2022-05-17 | 北京天宇威视科技股份有限公司 | Storage method, device, equipment and computer readable storage medium of electronic volume |
CN113918530B (en) * | 2021-12-14 | 2022-05-13 | 北京达佳互联信息技术有限公司 | Method and device for realizing distributed lock, electronic equipment and medium |
CN114595445A (en) * | 2022-02-10 | 2022-06-07 | 深圳市小满科技有限公司 | Task processing method and device and related product |
CN115269207B (en) * | 2022-09-29 | 2022-12-23 | 浙江省公众信息产业有限公司无线运营分公司 | Method and system for allocating resources to vCPE network element |
CN115809301B (en) * | 2023-02-03 | 2023-04-21 | 天翼云科技有限公司 | Database processing method, device, electronic device and readable storage medium |
Citations (2)
Publication number | Priority date | Publication date | Assignee | Title |
---|---|---|---|---|
CN108874552A (en) * | 2018-06-28 | 2018-11-23 | 杭州云英网络科技有限公司 | Distributed lock executes method, apparatus and system, application server and storage medium |
CN110032424A (en) * | 2019-01-28 | 2019-07-19 | 阿里巴巴集团控股有限公司 | The implementation method and device of distributed lock |
Family Cites Families (10)
Publication number | Priority date | Publication date | Assignee | Title |
---|---|---|---|---|
US4716528A (en) * | 1986-02-03 | 1987-12-29 | International Business Machines Corporation | Method for managing lock escalation in a multiprocessing, multiprogramming environment |
GB2321540A (en) * | 1997-01-25 | 1998-07-29 | Ibm | Automatic transactional locking using object request broker |
CN101364230B (en) * | 2008-09-24 | 2011-04-20 | 金蝶软件(中国)有限公司 | Concurrent access control method and device |
DE102009042128A1 (en) * | 2009-09-18 | 2011-03-24 | Siemens Aktiengesellschaft | Method and system for using temporary exclusive locks for parallel resource access |
CN106354795B (en) * | 2016-08-26 | 2019-09-17 | 南威软件股份有限公司 | A kind of Distributed data exchange system |
CN107977376B (en) * | 2016-10-24 | 2020-07-07 | 腾讯科技(深圳)有限公司 | Distributed database system and transaction processing method |
CN106528876B (en) * | 2016-12-09 | 2019-08-23 | 北京奇虎科技有限公司 | The information processing method and distributed information processing system of distributed system |
CN108319496B (en) * | 2017-01-18 | 2022-03-04 | 阿里巴巴集团控股有限公司 | Resource access method, service server, distributed system and storage medium |
CN109901798B (en) * | 2019-02-27 | 2022-12-09 | 新华三技术有限公司成都分公司 | Data storage method and device |
CN110231979A (en) * | 2019-05-07 | 2019-09-13 | 深圳壹账通智能科技有限公司 | Transaction methods, device, equipment and storage medium based on block chain |
-
2019
- 2019-10-29 CN CN201911040315.8A patent/CN110888858B/en active Active
Patent Citations (2)
Publication number | Priority date | Publication date | Assignee | Title |
---|---|---|---|---|
CN108874552A (en) * | 2018-06-28 | 2018-11-23 | 杭州云英网络科技有限公司 | Distributed lock executes method, apparatus and system, application server and storage medium |
CN110032424A (en) * | 2019-01-28 | 2019-07-19 | 阿里巴巴集团控股有限公司 | The implementation method and device of distributed lock |
Also Published As
Publication number | Publication date |
---|---|
CN110888858A (en) | 2020-03-17 |
Similar Documents
Publication | Publication Date | Title |
---|---|---|
CN110888858B (en) | Database operation method and device, storage medium and electronic device | |
CN112019475B (en) | Resource access method, device, system and storage medium under server-free architecture | |
CN109542611B (en) | Database as a service system, database scheduling method, device and storage medium | |
CN106936899B (en) | Configuration method of distributed statistical analysis system and distributed statistical analysis system | |
US10445186B1 (en) | Associating a guest application within a virtual machine to create dependencies in backup/restore policy | |
CN107402722B (en) | Data migration method and storage device | |
CN110069346B (en) | Method and device for sharing resources among multiple processes and electronic equipment | |
US10083199B1 (en) | Data migration in active data store | |
CN111641515B (en) | VNF life cycle management method and device | |
CN112988377B (en) | Resource allocation method, system and medium for cloud service | |
CN107423942B (en) | Service transfer method and device | |
CN111831232A (en) | Data storage method and device, storage medium and electronic device | |
CN111078119B (en) | Data reconstruction method, system, device and computer readable storage medium | |
CN114185558A (en) | Native application master selection method and device based on K8s and storage medium | |
CN107516048B (en) | Method and equipment for controlling file access in distributed file system | |
WO2021013185A1 (en) | Virtual machine migration processing and strategy generation method, apparatus and device, and storage medium | |
CN113296891A (en) | Multi-scene knowledge graph processing method and device based on platform | |
CN107967265B (en) | File access method, data server and file access system | |
CN109992447B (en) | Data copying method, device and storage medium | |
CN110798358A (en) | Distributed service identification method and device, computer readable medium and electronic equipment | |
CN103686224A (en) | Method and system for transcoding task obtaining on basis of distributed locks | |
CN111404979B (en) | Method and device for processing service request and computer readable storage medium | |
US10972343B2 (en) | System and method for device configuration update | |
CN114979234A (en) | Session control sharing method and system in distributed cluster system | |
CN110502460B (en) | Data processing method and node |
Legal Events
Date | Code | Title | Description |
---|---|---|---|
PB01 | Publication | ||
PB01 | Publication | ||
SE01 | Entry into force of request for substantive examination | ||
SE01 | Entry into force of request for substantive examination | ||
GR01 | Patent grant | ||
GR01 | Patent grant |