CN108363613A - A kind of locking method of exclusive lock, electronic equipment and storage medium - Google Patents

A kind of locking method of exclusive lock, electronic equipment and storage medium Download PDF

Info

Publication number
CN108363613A
CN108363613A CN201711464383.8A CN201711464383A CN108363613A CN 108363613 A CN108363613 A CN 108363613A CN 201711464383 A CN201711464383 A CN 201711464383A CN 108363613 A CN108363613 A CN 108363613A
Authority
CN
China
Prior art keywords
resource
lock
locking
database
exclusive lock
Prior art date
Legal status (The legal status is an assumption and is not a legal conclusion. Google has not performed a legal analysis and makes no representation as to the accuracy of the status listed.)
Pending
Application number
CN201711464383.8A
Other languages
Chinese (zh)
Inventor
曾昱深
Current Assignee (The listed assignees may be inaccurate. Google has not performed a legal analysis and makes no representation or warranty as to the accuracy of the list.)
Hangzhou Horse Monkey Burning Chives Technology Co Ltd
Original Assignee
Hangzhou Horse Monkey Burning Chives Technology Co Ltd
Priority date (The priority date is an assumption and is not a legal conclusion. Google has not performed a legal analysis and makes no representation as to the accuracy of the date listed.)
Filing date
Publication date
Application filed by Hangzhou Horse Monkey Burning Chives Technology Co Ltd filed Critical Hangzhou Horse Monkey Burning Chives Technology Co Ltd
Priority to CN201711464383.8A priority Critical patent/CN108363613A/en
Publication of CN108363613A publication Critical patent/CN108363613A/en
Pending legal-status Critical Current

Links

Classifications

    • GPHYSICS
    • G06COMPUTING; CALCULATING OR COUNTING
    • G06FELECTRIC DIGITAL DATA PROCESSING
    • G06F9/00Arrangements for program control, e.g. control units
    • G06F9/06Arrangements 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/46Multiprogramming arrangements
    • G06F9/466Transaction processing
    • GPHYSICS
    • G06COMPUTING; CALCULATING OR COUNTING
    • G06FELECTRIC DIGITAL DATA PROCESSING
    • G06F16/00Information retrieval; Database structures therefor; File system structures therefor
    • G06F16/20Information retrieval; Database structures therefor; File system structures therefor of structured data, e.g. relational data
    • G06F16/23Updating
    • GPHYSICS
    • G06COMPUTING; CALCULATING OR COUNTING
    • G06FELECTRIC DIGITAL DATA PROCESSING
    • G06F9/00Arrangements for program control, e.g. control units
    • G06F9/06Arrangements 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/46Multiprogramming arrangements
    • G06F9/52Program synchronisation; Mutual exclusion, e.g. by means of semaphores
    • G06F9/524Deadlock detection or avoidance

Landscapes

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

Abstract

The invention discloses a kind of locking methods of exclusive lock, include the following steps:Obtaining step:It obtains and locks instruction;Prelock step:Multiple resources in Database Systems are inquired successively by resource lock algorithm, and judge whether corresponding resource can be used, and the exclusive lock that locking operation obtains resource is carried out to available resource;Database manipulation step:Corresponding data manipulation is carried out to the resource of locking.The present invention also provides a kind of electronic equipment, the locking devices of computer readable storage medium and Database Systems exclusive lock.The present invention is pre-locked by before carrying out database manipulation to resource, being carried out to resource by Redlock algorithms, and then solves the problems, such as the locking operation of exclusive lock in Database Systems in the prior art is complicated, the time is long, expends resource etc..

Description

A kind of locking method of exclusive lock, electronic equipment and storage medium
Technical field
The present invention relates to the operation of the lock of database more particularly to a kind of locking method of exclusive lock, electronic equipment, storage Jie Matter and device.
Background technology
Db transaction refers to a logic unit in Database Systems implementation procedure, by a limited database The sequence of operation is constituted.One db transaction typically includes the read/write operation of a database.Db transaction exists for Ensure that database can there are one the methods for being restored to normal condition from failure, and for database can there are one can Ensure the method for consistency;And it, can be among these applications when there is multiple application programs concurrently to access database It is mutually isolated, avoids interfering.And resource lock is to prevent two application programs right simultaneously in Database Systems business process One common data is written and read, to avoid the mechanism of the inconsistent situation of unnecessary data is caused.And exclusive lock is resource lock One of fundamental type, if adding exclusive lock (X locks) to data object A than affairs T, then only affairs T is allowed to data Object A is written and read operation, any other affairs all cannot add any lock to data object A, until the lock is by affairs T institutes Release.For example, in MySQL database before to data resource operation, it is necessary first to lock, but lock to resource Operation needs to lock available resource under the process that the affairs in Database Systems are opened, to ensure that data can be different Restore in the case of often normal.This locking operation, which exists, opens a thread, starts the sequence of operations such as affairs, and in database Start these operations such as thread, unlatching affairs to need to waste a large amount of memory headroom, cpu resource etc., its locking operation is caused to have The standby time is long, expends the problems such as resource, can drag the response efficiency of slow server in this way.
Invention content
For overcome the deficiencies in the prior art, one of the objects of the present invention is to provide a kind of locking method of exclusive lock, It can solve the locking in Database Systems in the prior art for resource and the operations such as startup thread, unlatching affairs is needed to cause Lock times are long, expend the problems such as resource.
The second object of the present invention is to provide a kind of electronic equipment, can solve in Database Systems in the prior art The locking of resource is needed to start thread, opens the problems such as operations such as affairs cause long lock times, consuming resource.
The third object of the present invention is to provide a kind of computer readable storage medium, can solve to count in the prior art The operations such as startup thread, unlatching affairs are needed to cause lock times are long, expend resource etc. the locking of resource according in the system of library Problem.
An object of the present invention adopts the following technical scheme that realization:
A kind of locking method of exclusive lock, includes the following steps:
Obtaining step:It obtains and locks instruction;
Prelock step:Multiple resources in Database Systems are inquired successively by resource lock algorithm, and judgement pair It answers whether resource can be used, and the exclusive lock that locking operation obtains resource is carried out to available resource;
Database manipulation step:Corresponding data manipulation is carried out to the resource of locking.
Further, the resource lock algorithm is Redlock algorithms.
Further, the Database Systems are MySQL database system.
The second object of the present invention adopts the following technical scheme that realization:
A kind of electronic equipment, including memory, processor and storage can be run on a memory and on a processor Computer program, the processor realize following steps when executing described program:
Obtaining step:It obtains and locks instruction;
Prelock step:Multiple resources in Database Systems are inquired successively by resource lock algorithm, and judgement pair It answers whether resource can be used, and the exclusive lock that locking operation obtains resource is carried out to available resource;
Database manipulation step:Corresponding data manipulation is carried out to the resource of locking.
Further, the resource lock algorithm is Redlock algorithms.
Further, the Database Systems are MySQL database system.
The third object of the present invention adopts the following technical scheme that realization:
A kind of computer readable storage medium, is stored thereon with computer program, and the computer program is held by processor The step of locking method of the exclusive lock provided such as one of the object of the invention is provided when row.
Compared with prior art, the beneficial effects of the present invention are:
The present invention carries out resource by before carrying out database manipulation to database resource, first passing through resource lock algorithm Prelock operates to obtain the exclusive lock of the resource, then carries out corresponding data manipulation to the resource again, solves the prior art Some unnecessary operations of row such as startup thread, unlatching affairs are needed in middle Database Systems in locking operation, reduce data The load in library.
Description of the drawings
Fig. 1 is the functional block diagram of the locking method of exclusive lock provided by the invention;
Fig. 2 is the flow chart of the locking method of exclusive lock provided by the invention;
Fig. 3 is the module map of the locking device of exclusive lock provided by the invention.
Specific implementation mode
In the following, in conjunction with attached drawing and specific implementation mode, the present invention is described further, it should be noted that not Under the premise of conflicting, new implementation can be formed between various embodiments described below or between each technical characteristic in any combination Example.
For in original Database Systems, for example a program will access a resource A, and it is exclusive to access. In traditional database, program needs to open an affairs, is then waited for during affairs carry out, waits for this money The availability in source, after resource is available, so that it may to carry out data manipulation to resource.But in the process for opening affairs and waiting In, resource of database, such as memory space, cpu resource etc. are equal to be to waste, while also increasing the negative of database Lotus.
And the present invention by using redis data high-speed low-consumption the characteristics of, come to resource by using Redlock algorithms A is locked to obtain the exclusive lock of resource A, then program when accessing resource A, only need to directly access resource A into Row data manipulation, and then a small amount of memory headroom can be consumed, without carrying out waiting money with an affairs are created in the database The availability in source.
In other words, the present invention is in order to solve the defect of locking operation in database, it is proposed that a kind of locking of exclusive lock Method, this method apply to server end, are carried out to the available resources in database by calling third party's resource lock algorithm Then locking in advance directly carries out corresponding data manipulation to the resource, avoids conventional database systems to resource When being locked, the operation opened affairs and locked to available resource during affairs carry out is needed, causes to lock Appearance the problems such as time is long, waste of resource, thereby reduce the load of database.
In other words, as shown in Figure 1, the present invention is that before carrying out data manipulation carries out prelock to obtain to resource first The exclusive lock for taking available resources, to solve needed when locking operation in Database Systems in the prior art open thread, open Some operations such as dynamic affairs lead to problems such as locking operation consuming resource, time long.
In addition, involved in the present invention to Redis be a database journey using the C memory key-value pairs of increasing income programmed Sequence.Since the main read-write terminal of Redis database programs is memory, so having, execution efficiency is higher, is easy to synchronization Feature.Redis database programs can be synchronized to any number of from server from master server.Due to completely real Publish/subscribe mechanism is showed so that when anywhere being synchronized from database, can receive the completion message hair of master server Cloth records so that it is convenient to which it is used as distributed data base.
And Redlock algorithms are a resource lock algorithms for being directed to distributed system of redis program developers announcement.This Invention is pre-locked by the progress of available resource using Redlock algorithms by the way that Redlock algorithms to be combined with Database Systems Obtain the exclusive lock of resource, then again by carrying out data manipulation to resource, it is right inside traditional Database Systems to can avoid When resource is locked, first has to open affairs and carry out waiting for judgement available to resource in the process and to money in affairs What source was locked, and cause resource, memory space waste reduce database load.
As shown in Fig. 2, a kind of locking method of exclusive lock, is applied to server end comprising following steps:
S1, locking instruction is obtained;
S2, it calls resource lock algorithm to inquire multiple resources of database successively, and judges that corresponding resource whether may be used With, and available resource is carried out pre-locked to obtain the exclusive lock of resource.
For a Database Systems, belong to a distributed system, the resource in database has multiple.Pass through System generates a random number and sends request lock to resource successively, and judges whether corresponding resource can be used, if so, to the resource into Row is pre-locked;If it is not, then continuing to send the request locked to next resource.
It is Redlock algorithms to carry out pre-locked used resource lock algorithm to resource in the present invention, belongs to third party The algorithm provided, therefore call the Redlock on Redis servers to calculate first to after locking instruction in received server-side Method pre-locked to resource obtains the exclusive lock of corresponding resource to realize.
Wherein, Redlock algorithms are a kind of resource lock algorithms increased income, are implemented as follows:
A, current time is obtained and as the time started;
B, it generates a random number at random by system, then sends the request of lock in each resource node in sequence. During being locked, for each lock operation setting one quick Time To Failure (if it is intended to one 10 seconds lock of acquisition, that The Time To Failure of each lock operation is set as 5-50ms).It in this way can be to avoid communication when resource progress for failure The long time is occupied, sends the request of lock with other resource nodes as soon as possible by way of quickly failing.
C, current time is obtained, and obtains obtaining according to current time and time started and locks the consumed time.
D, the effective time of the lock is subtracted the time for obtaining resource lock and consuming by the time-to-live of lock.When here effective Between refer to time when carrying out data manipulation to resource, and the time-to-live refers to the time being latched in.
In addition, can refer to for the specific implementation of Redlock algorithms:https://redis.io/topics/ Distlock, translator of Chinese are shown in:https://www.jianshu.com/p/fba7dd6dcef5.Only it is done herein Simple introduction, the algorithm are not the inventive points of the present invention.
In addition, if sending the request of lock to all resources, when the exclusive lock for the acquisition available resources that fail provides, It can then go to attempt to unlock all resource nodes in Database Systems.
S3, corresponding data manipulation is carried out to the resource of locking.
After acquiring corresponding resource lock, so that it may to carry out corresponding database manipulation to the resource, for example inquire The volume of data operations such as data, modification data.
The present invention also provides a kind of electronic equipment comprising memory, processor and storage are on a memory and can The computer program run in processing, the processor realize following steps when executing described program:
Obtaining step:It obtains and locks instruction;
Prelock step:Multiple resources in Database Systems are inquired successively by resource lock algorithm, and judgement pair It answers whether resource can be used, and the exclusive lock that locking operation obtains resource is carried out to available resource;
Database manipulation step:Corresponding data manipulation is carried out to the resource of locking.
The present invention also provides a kind of computer readable storage mediums, are stored thereon with computer program, computer program Following steps are realized when being executed by processor:
Obtaining step:It obtains and locks instruction;
Prelock step:Multiple resources in Database Systems are inquired successively by resource lock algorithm, and judgement pair It answers whether resource can be used, and the exclusive lock that locking operation obtains resource is carried out to available resource;
Database manipulation step:Corresponding data manipulation is carried out to the resource of locking.
As shown in figure 3, a kind of locking device of exclusive lock comprising:
Acquisition module locks instruction for obtaining;
Pre- lock module for being inquired successively multiple resources in Database Systems by resource lock algorithm, and is sentenced Whether disconnected corresponding resource can be used, and carry out the exclusive lock that locking operation obtains resource to available resource;
Database operating modules carry out corresponding data manipulation for the resource to locking.
The above embodiment is only the preferred embodiment of the present invention, and the scope of protection of the present invention is not limited thereto, The variation and replacement for any unsubstantiality that those skilled in the art is done on the basis of the present invention belong to institute of the present invention Claimed range.

Claims (7)

1. a kind of locking method of exclusive lock is applied to server end, it is characterised in that include the following steps:
Obtaining step:It obtains and locks instruction;
Prelock step:Multiple resources in Database Systems are inquired successively by resource lock algorithm, and judge corresponding money Whether source can be used, and carry out the exclusive lock that locking operation obtains resource to available resource;
Database manipulation step:Corresponding data manipulation is carried out to the resource of locking.
2. the method as described in claim 1, it is characterised in that:The resource lock algorithm is Redlock algorithms.
3. the method as described in claim 1, it is characterised in that:The Database Systems are MySQL database system.
4. a kind of electronic equipment, including memory, processor and storage are on a memory and the meter that can run on a processor Calculation machine program, it is characterised in that:The processor realizes following steps when executing described program:
Obtaining step:It obtains and locks instruction;
Prelock step:Multiple resources in Database Systems are inquired successively by resource lock algorithm, and judge corresponding money Whether source can be used, and carry out the exclusive lock that locking operation obtains resource to available resource;
Database manipulation step:Corresponding data manipulation is carried out to the resource of locking.
5. electronic equipment as claimed in claim 4, it is characterised in that:The resource lock algorithm is Redlock algorithms.
6. electronic equipment as claimed in claim 4, it is characterised in that:The Database Systems are MySQL database system.
7. a kind of computer readable storage medium, is stored thereon with computer program, it is characterised in that:The computer program quilt The step of locking method of the exclusive lock as described in any one of claim 1-3 is realized when processor executes.
CN201711464383.8A 2017-12-28 2017-12-28 A kind of locking method of exclusive lock, electronic equipment and storage medium Pending CN108363613A (en)

Priority Applications (1)

Application Number Priority Date Filing Date Title
CN201711464383.8A CN108363613A (en) 2017-12-28 2017-12-28 A kind of locking method of exclusive lock, electronic equipment and storage medium

Applications Claiming Priority (1)

Application Number Priority Date Filing Date Title
CN201711464383.8A CN108363613A (en) 2017-12-28 2017-12-28 A kind of locking method of exclusive lock, electronic equipment and storage medium

Publications (1)

Publication Number Publication Date
CN108363613A true CN108363613A (en) 2018-08-03

Family

ID=63010612

Family Applications (1)

Application Number Title Priority Date Filing Date
CN201711464383.8A Pending CN108363613A (en) 2017-12-28 2017-12-28 A kind of locking method of exclusive lock, electronic equipment and storage medium

Country Status (1)

Country Link
CN (1) CN108363613A (en)

Cited By (3)

* Cited by examiner, † Cited by third party
Publication number Priority date Publication date Assignee Title
CN109558218A (en) * 2018-12-04 2019-04-02 山东浪潮通软信息科技有限公司 A kind of distributed service data lock implementation method based on Redis
CN112231131A (en) * 2020-09-28 2021-01-15 北京金山云网络技术有限公司 Method, device and equipment for realizing database lock and readable storage medium
CN112231131B (en) * 2020-09-28 2024-05-28 北京金山云网络技术有限公司 Method, device and equipment for realizing database lock and readable storage medium

Citations (6)

* Cited by examiner, † Cited by third party
Publication number Priority date Publication date Assignee Title
CN102984268A (en) * 2012-12-11 2013-03-20 中国人民解放军国防科学技术大学 Access method and device for SCSI sharing storage resource for high-availability cluster
CN103488526A (en) * 2013-09-02 2014-01-01 用友软件股份有限公司 System and method for locking business resource in distributed system
CN105426469A (en) * 2015-11-16 2016-03-23 天津南大通用数据技术股份有限公司 Database cluster metadata management method and system
CN105786628A (en) * 2016-02-24 2016-07-20 浪潮通用软件有限公司 Processing method for achieving thread synchronization of distributed system
CN106610865A (en) * 2015-10-21 2017-05-03 阿里巴巴集团控股有限公司 Data locking and unlocking method and apparatus
CN107402821A (en) * 2017-07-03 2017-11-28 阿里巴巴集团控股有限公司 Access control method, device and the equipment of shared resource

Patent Citations (6)

* Cited by examiner, † Cited by third party
Publication number Priority date Publication date Assignee Title
CN102984268A (en) * 2012-12-11 2013-03-20 中国人民解放军国防科学技术大学 Access method and device for SCSI sharing storage resource for high-availability cluster
CN103488526A (en) * 2013-09-02 2014-01-01 用友软件股份有限公司 System and method for locking business resource in distributed system
CN106610865A (en) * 2015-10-21 2017-05-03 阿里巴巴集团控股有限公司 Data locking and unlocking method and apparatus
CN105426469A (en) * 2015-11-16 2016-03-23 天津南大通用数据技术股份有限公司 Database cluster metadata management method and system
CN105786628A (en) * 2016-02-24 2016-07-20 浪潮通用软件有限公司 Processing method for achieving thread synchronization of distributed system
CN107402821A (en) * 2017-07-03 2017-11-28 阿里巴巴集团控股有限公司 Access control method, device and the equipment of shared resource

Non-Patent Citations (1)

* Cited by examiner, † Cited by third party
Title
张铁蕾: "基于Redis的分布式锁到底安全吗(上)", 《分布式》 *

Cited By (3)

* Cited by examiner, † Cited by third party
Publication number Priority date Publication date Assignee Title
CN109558218A (en) * 2018-12-04 2019-04-02 山东浪潮通软信息科技有限公司 A kind of distributed service data lock implementation method based on Redis
CN112231131A (en) * 2020-09-28 2021-01-15 北京金山云网络技术有限公司 Method, device and equipment for realizing database lock and readable storage medium
CN112231131B (en) * 2020-09-28 2024-05-28 北京金山云网络技术有限公司 Method, device and equipment for realizing database lock and readable storage medium

Similar Documents

Publication Publication Date Title
US5499364A (en) System and method for optimizing message flows between agents in distributed computations
US11681683B2 (en) Transaction compensation for single phase resources
CN110069346B (en) Method and device for sharing resources among multiple processes and electronic equipment
CN110968603B (en) Data access method and device
WO2021169342A1 (en) Resource management method for node in kubernetes, device, and medium
US20110239219A1 (en) Protecting shared resources using shared memory and sockets
CN111064626B (en) Configuration updating method, device, server and readable storage medium
CN111314212B (en) API gateway based on Netty and plug-in mechanism and control method
CN110457157A (en) Distributed transaction abnormality eliminating method, device, computer equipment and storage medium
CN112099961A (en) Realization method of distributed lock manager based on lock state cache
US9176796B2 (en) Shared memory reusable IPC library
CN113315750B (en) Kafka message issuing method, device and storage medium
CN108363613A (en) A kind of locking method of exclusive lock, electronic equipment and storage medium
CN113377875A (en) Cross-link data processing method and device, electronic equipment and readable storage medium
CN109445966B (en) Event processing method, device, medium and computing equipment
CN112015563A (en) Message queue switching method and device, electronic equipment and storage medium
CN114500289B (en) Control plane recovery method, device, control node and storage medium
CN113259475B (en) Distributed session processing system and method based on micro-service architecture
CN113835904A (en) Remote procedure call control method, device, equipment and storage medium
CN114780165A (en) Application service configuration hot loading method based on message queue and related equipment
CN112486696A (en) Method and equipment for acquiring distributed lock
CN114896258B (en) Transaction data synchronization method and device, computer equipment and storage medium
CN117742979B (en) Distributed lock method oriented to space-time data processing and electronic equipment
CN115174440B (en) Connectivity detection method, device, storage medium and equipment
CN113467960B (en) Multi-path storage method, system, terminal and storage medium based on shared lock

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
RJ01 Rejection of invention patent application after publication

Application publication date: 20180803

RJ01 Rejection of invention patent application after publication