Key-Value type WORM lock pond software module and operation method thereof
Technical field
The present invention relates to parallel computation field Zhong Suo pond software module and method, be specifically related to a kind of Key-Value type WORM lock pond software module and operation method thereof.
Background technology
Lock is a kind of valuable source that multiple task operating system provides, common are the various ways such as critical section, semaphore, mutex, its Core Feature is in parallel Programming field, when two or more threads face while accessing some resources (internal memory, Socket, IO etc.) possible simultaneously, this resource of provisional declaration is being taken (Lock) by certain thread sometime, prevent the conflict access of other threads, to ensure that each access action is all intactly completed in the authority of oneself needs, avoids program bug.
At large server development field, owing to will tackling the high speed of a large amount of clients, concurrent greatly, mass data access, the access of the resource such as internal data module configured, communication module and variation very dense, need the lock resource of efficient and magnanimity to support, the integrality of guarantee data access and security, and the efficiency of guarantee resource access.
Traditional latching operation program design mode is generally divided into two kinds:
1, definition one is " lock greatly "; protect a complete data structure; such as all data in a lock protection one tree type data structure, a queue, a storehouse; all the access of this data structure is all needed to realize by this lock; this takies seldom operating-system resources; but can cause large Concurrency Access time, lock collision is too intensive, and access efficiency is very low.
2, definition is much " little lock "; subdata that segment, very among a small circle in protected data structure; such as a lock is only protected a slice " leaf " in tree form data structure; in program, lock is with data resource according to the configuration of 1:1 quantity, and this can reduce lock collision and promote access efficiency.But because lock itself is also the resource of operating system, in the tissue of mass data, system resources consumption can be very large, can cause system to be collapsed because of inadequate resource, or program is forced to arrange the upper limit, cannot protect too many resource.
Above-mentioned two schemes is all imperfect, or performance is low, or serious waste of resources, and the two is difficult to compromise.A kind of high efficiency, low system resource of software industry shortage takies at present, for the lock salvo of mass data.This has seriously restricted performance and the scale of high concurrent mass data application server.
In addition, the lock resource that legacy operating system provides, its locking method comparison " rough ", the two kinds of modes of operation of (Lock) and release (Unlock) that only lock, do not segment the data access behavior that applications is concrete.This causes locking is at every turn all unconditional " exclusive ", cannot be concurrent, and performance is not high.
But in real work; data access has " reading ", " writing " two kinds of demands conventionally; research shows, under " writing " mode, really needs the lock protection of " exclusive "; prevent bug; and under " reading " mode, because protected data resource is not modified, can allow multiple " reading " concurrent; allow multiple threads to read certain resource, to promote the concurrent efficiency of program simultaneously.
This needs WORM lock logic, i.e. the application layer latching operation behavior of " segmentation ", clearly difference " writing action " and " reading to move ", in addition different processing, the lock protective capability of realization " unique writing " and " concurrent reading ".
At present, most of multiple task operating systems do not provide " WORM lock " resource, and also ununified standard of software industry needs programmer to realize voluntarily in development.But because WORM lock logic development difficulty is larger, industry programmer's level is uneven again, in doing in fact, often goes wrong, and has affected program development work efficiency.
Software industry relatively lacks at present comparatively general, have higher abstractness, friendly application development interface API is provided, can be cross-platform, across the WORM lock resource solution of language call.
Summary of the invention
The shortcoming existing in order to overcome above-mentioned prior art, the object of the present invention is to provide a kind of Key-Value type WORM lock pond software module and operation method thereof, can use little operating-system resources (physical locks resource, internal memory etc.), provide magnanimity " WORM logic lock " resource provisioning layer to use, to realize " little lock " application model efficiently, meet the high-performance development requirement under multitask environment of software industry.
In order to achieve the above object, the technical solution adopted in the present invention is:
A kind of Key-Value type WORM lock pond software module, comprises and intercoupling, the following submodule of linkage work:
Application call interface: define the method for operating of this module, call for other computer programming languages, practical function output;
WORM operation logic layer: provide WORM algorithm logic, for each logic lock of realizing provides WORM operating characteristic;
Hash administration module: accept the Key key assignments of arbitrary data types, quick indexing is searched corresponding WORM logic lock, realizes the accurate access of multiple WORM logic locks;
Dynamic memory pool module: realize dynamic application and the release of inner WORM logic lock data;
System physical locks resource: to the lock resource of operating system application, it adds unlock method this modular program run time call, provides the multithreading safeguard protection of this module self function.
Described lock pond software module provides magnanimity WORM logic lock for other application developments, described WORM logic lock comprises two characteristics, write zone bit and a read counter for one, can store, describe the operation state values of " unique writing " and " multiple reading ", its core data belongs to internal storage data, can indexed management in Hash administration module, also can be by the dynamically application and discharging of dynamic memory pool module, its logical definition is as follows:
1) writing flag set is 1, represents that this logic lock, in " writing " state, forbids that other " writing " or " reading " action enters at present, and cannot again arrange and write zone bit is 1, or stack read counter;
2) read counter stack, is not 0, represents to have at present " reading " action, does not allow other " writing " action to enter, and cannot arrange and write zone bit is 1, but other " reading " action can be concurrent, and this value that is embodied in read counter constantly increases.
An operation method for Key-Value type WORM lock pond software module, comprises the steps:
Step 1: application program is using arbitrary data as Key, call this software module application call interface, lock request of access is proposed, described request comprises adding to be write lock, solution and writes lock, read lock, solution read lock and inquiry lock read-write state, application call interface is received request of access, forwards WORM operation logic layer to and realizes service;
Step 2: WORM operation logic layer method of service is as follows:
Move for " add and write lock " or " read lock ", whether first WORM operation logic layer is retrieved in Hash administration module according to Key has corresponding WORM logic lock to exist, if there is no, to WORM logic lock of dynamic memory pool module application, then push Hash administration module and carry out index management, now this WORM logic lock is considered as existing; If existed, directly add latching operation, return to operating result;
Move for " solution is write lock " or " solution read lock ", whether first WORM operation logic layer is retrieved in Hash administration module according to Key has corresponding WORM logic lock to exist, do not exist directly and return, do not do anything, exist and carry out unlocking operation, after release, check whether it also has other locking states, if nothing, from Hash administration module, delete the index of this WORM logic lock, and discharge this WORM logic lock resource to dynamic memory pool module;
For reading lock status action, whether first WORM operation logic layer is retrieved in Hash administration module according to Key has corresponding WORM logic lock to exist, do not exist and directly return to lock unused state, existence is read real data mode in WORM logic lock and is returned;
Above-mentioned everything all completes under system physical locks protection same, is not interrupted when ensureing everything operation, guarantees the multithreading security of this software module self.
Described operation method comprises following application call interface and relevant WORM logical layer program circuit:
Write lock status query interface: the WORM logic lock that inquiry Key specifies write lock status, specific procedure flow process is as follows:
A) add this modular system physical locks
B) carry out Hash according to Key and search, find corresponding WORM logic lock
If c) found, read and wherein write mark, assignment is to rreturn value
If d) do not found, rreturn value is directly set to 0, represent not lock
E) separate this modular system physical locks, return to rreturn value;
Read lock status poll interface: the read counter state of value of the WORM logic lock that inquiry Key specifies, specific procedure flow process is as follows:
A) add this modular system physical locks
B) carry out Hash according to Key and search, find corresponding WORM logic lock
If c) found, read wherein read counter, assignment is to rreturn value
If d) do not found, rreturn value is directly set to 0, represent not lock
E) separate this modular system physical locks, return to rreturn value;
Trial adds writes lock interface: write lock for the WORM logic lock of Key appointment adds, if this WORM logic lock is added and reads or write lock, unsuccessfully return to vacation, or after successfully locking, return very, specific procedure flow process is as follows:
A) add this modular system physical locks
B) carry out Hash according to Key and search, find corresponding WORM logic lock
If c) do not found, attempt to create WORM logic lock, be set to vacation if create failed rreturn value, jump to g)
If d) created successfully, this WORM logic lock index and Key are built to Key-Value pairing, be pushed to Hash administration module and manage, if Hash pushes unsuccessfully, destroy the WORM logic lock of just having set up, rreturn value is set to vacation, jumps to g)
If e) find or create successfully, check whether this lock has added read lock or write lock, if add and lock, rreturn value is set to vacation
If f) can lock, write mark and be set to 1 realization and lock, rreturn value is set to very simultaneously
G) separate this modular system physical locks
H) check rreturn value, false if, represent to lock unsuccessful, function directly returns to vacation, finishes
If i) rreturn value is true, represent to write lock and add successfully, need to arrange unlimited endless loop, constantly call read lock status poll interface, detect whether all read locks are all removed, be 0 until obtain read counter, side jumps out circulation, returns true;
Determine to add and write lock interface: for adding, the WORM logic lock of Key appointment writes lock, if this WORM logic lock is added and is read or write lock, circular wait, until successfully lock, specific procedure flow process is as follows:
A) unlimited endless loop is set, constantly calls and attempt adding writing lock interface
B) circulation continuation of failure
C) circulation is jumped out in success, returns true;
Lock interface is write in releasing: write lock for the WORM logic lock releasing that Key specifies, specific procedure flow process is as follows:
A) add this modular system physical locks
B) carry out Hash according to Key and search, find corresponding WORM logic lock
If c) do not found, do not do anything
If d) found, setting is write and is masked as 0, destroys WORM logic lock, deletes this lock index, releasing resource from Hash administration module simultaneously
E) separate this modular system physical locks, return;
Attempt read lock interface: for the WORM logic lock read lock that Key specifies, if this WORM logic lock is added and writes lock, unsuccessfully return to vacation, or after successfully locking, return very, specific procedure flow process is as follows:
A) add this modular system physical locks
B) carry out Hash according to Key and search, find corresponding WORM logic lock
If c) do not found, attempt to create, if create failed rreturn value, vacation is set, jump to g)
If d) created successfully, this WORM logic lock index and Key are built to Key-Value pairing, be pushed to Hash administration module and manage, if Hash pushes unsuccessfully, destroy the WORM logic lock just having created, rreturn value is set to vacation, jumps to g)
If e) find or create successfully, check whether this lock has added to write lock, if locked, rreturn value is set to vacation
If f) can lock, read counter+1 is realized and being locked, rreturn value is set to very simultaneously
G) separate this modular system physical locks, return to rreturn value;
Determine read lock interface: for the WORM logic lock read lock of Key appointment, if this WORM logic lock is added and is write lock, circular wait, until successfully add read lock, specific procedure flow process is as follows:
A) unlimited endless loop is set, constantly calls trial read lock
B) circulation continuation of failure
C) circulation is jumped out in success, returns true;
Remove read lock interface: remove read lock for the WORM logic lock that Key specifies, specific procedure flow process is as follows:
A) add this modular system physical locks
B) carry out Hash according to Key and search, find corresponding WORM logic lock
If c) do not found, do not do anything
If d) found, read counter-1, if <=0 of read counter now destroys WORM logic lock, deletes this lock index, releasing resource from Hash administration module simultaneously
E) separate this modular system physical locks, return.
The application call interface of described lock pond software module, for all computer programming language routine calls with call operation system API ability, comprises C, C++, compilation, Java, C#, VB.
Described lock pond software module provides the version of different operating system, realizes spanning operation system platform function, can under Windows, Linux and some other class Unix platform, work, and module product can be organized as following form, practical function output:
.h header file definition (application development interface declaration definition) (Windows, Linux, Unix like)
.dll dynamic link library (Windows)
.so dynamic link library (Linux, Unix like)
.obj Object linking file (Windows, Linux, Unix like)
.lib static link library (Windows, Linux, Unix like)
Compared to the prior art, tool has the following advantages in the present invention:
The common operating system of its function ratio of WORM logic lock of the present invention's definition is locked powerful, be characterized in segmenting the outside action type to data resource, operate for " writing ", realize " unique writing " principle of complete exclusiveness, now be equivalent to the Lock that locks of operating system lock, and for " reading " operation, allow " concurrent reading " conditionally, can improve the concurrent work efficiency of multi-thread software system.
Coordinate again the dynamic memory management principle of C/C++ language, the present invention defines one to be only had WORM logic lock to be just really instantiated as in use effective internal storage data, while use, discharge back Installed System Memory in time, can, with little memory cost, realize the WORM logic lock of magnanimity for application program.
Brief description of the drawings
Fig. 1 is for writing lock status querying flow figure.
Fig. 2 is read lock status poll process flow diagram.
Fig. 3 writes lock process flow diagram for attempting adding.
Fig. 4 writes lock process flow diagram for determining to add.
Fig. 5 writes lock process flow diagram for removing.
Fig. 6 is for attempting read lock process flow diagram.
Fig. 7 is for determining read lock process flow diagram.
Fig. 8 is for removing read lock process flow diagram.
Embodiment
Below in conjunction with embodiment and accompanying drawing, the present invention is described in further detail.
A kind of Key-Value type of the present invention WORM lock pond software module, comprises and intercoupling, the following submodule of linkage work:
Application call interface: define the method for operating of this module, call for other computer programming languages, practical function output;
WORM operation logic layer: provide WORM algorithm logic, for each logic lock of realizing provides WORM operating characteristic;
Hash administration module: accept the Key key assignments of arbitrary data types, quick indexing is searched corresponding WORM logic lock, realizes the accurate access of multiple WORM logic locks;
Dynamic memory pool module: realize dynamic application and the release of inner WORM logic lock data;
System physical locks module: to the lock resource of operating system application, it adds unlock method Calling, provides the multithreading safeguard protection of this module self function.
Described lock pond software module provides magnanimity WORM logic lock for other application developments, described WORM logic lock comprises two characteristics, write zone bit and a read counter for one, can store, describe the operation state values of " unique writing " and " multiple reading ", its core data belongs to internal storage data, can indexed management in Hash administration module, also can be by the dynamically application and discharging of dynamic memory pool module, its logical definition is as follows:
1) writing flag set is 1, represents that this logic lock, in " writing " state, forbids that other " writing " or " reading " action enters at present, and cannot again arrange and write zone bit is 1, or stack read counter;
2) read counter stack, is not 0, represents to have at present " reading " action, does not allow other " writing " action to enter, and cannot arrange and write zone bit is 1, but other " reading " action can be concurrent, and this value that is embodied in read counter constantly increases;
The operation method of Key-Value type WORM lock of the present invention pond software module, comprises the steps:
Step 1: application program is using arbitrary data as Key, call this software module application call interface, lock request of access is proposed, described request comprises adding to be write lock, solution and writes lock, read lock, solution read lock and inquiry lock read-write state, application call interface is received request of access, forwards WORM operation logic layer to and realizes service;
Step 2: WORM operation logic layer method of service is as follows:
Move for " add and write lock " or " read lock ", whether first WORM operation logic layer is retrieved in Hash administration module according to Key has corresponding WORM logic lock to exist, if there is no, to WORM logic lock of dynamic memory pool module application, then push Hash administration module and carry out index management, now this WORM logic lock is considered as existing; If existed, directly add latching operation, return to operating result;
Move for " solution is write lock " or " solution read lock ", whether first WORM operation logic layer is retrieved in Hash administration module according to Key has corresponding WORM logic lock to exist, do not exist directly and return, do not do anything, exist and carry out unlocking operation, after release, check whether it also has other locking states, if nothing, from Hash administration module, delete the index of this WORM logic lock, and discharge this WORM logic lock resource to dynamic memory pool module;
For reading lock status action, whether first WORM operation logic layer is retrieved in Hash administration module according to Key has corresponding WORM logic lock to exist, do not exist and directly return to lock unused state, existence is read real data mode in WORM logic lock and is returned;
Above-mentioned everything all completes under system physical locks protection same, is not interrupted when ensureing everything operation, guarantees the multithreading security of this software module self.
Described operation method comprises following application call interface and relevant WORM logical layer program circuit:
Write lock status query interface: the WORM logic lock that inquiry Key specifies write lock status, specific procedure flow process as shown in Figure 1:
A) add this modular system physical locks
B) carry out Hash according to Key and search, find corresponding WORM logic lock
If c) found, read and wherein write mark, assignment is to rreturn value
If d) do not found, rreturn value is directly set to 0, represent not lock
E) separate this modular system physical locks, return to rreturn value;
Read lock status poll interface: the read counter state of value of WORM logic lock that inquiry Key specifies, specific procedure flow process as shown in Figure 2:
A) add this modular system physical locks
B) carry out Hash according to Key and search, find corresponding WORM logic lock
If c) found, read wherein read counter, assignment is to rreturn value
If d) do not found, rreturn value is directly set to 0, represent not lock
E) separate this modular system physical locks, return to rreturn value;
Trial adds writes lock interface: the WORM logic lock of specifying for Key adds and writes lock, if this WORM logic lock is added and reads or write lock, unsuccessfully returns to vacation, or after successfully locking, return true, specific procedure flow process as shown in Figure 3:
A) add this modular system physical locks
B) carry out Hash according to Key and search, find corresponding WORM logic lock
If c) do not found, attempt to create WORM logic lock, be set to vacation if create failed rreturn value, jump to g)
If d) created successfully, this WORM logic lock index and Key are built to Key-Value pairing, be pushed to Hash administration module and manage, if Hash pushes unsuccessfully, destroy the WORM logic lock of just having set up, rreturn value is set to vacation, jumps to g)
If e) find or create successfully, check whether this lock has added read lock or write lock, if add and lock, rreturn value is set to vacation
If f) can lock, write mark and be set to 1 realization and lock, rreturn value is set to very simultaneously
G) separate this modular system physical locks
H) check rreturn value, false if, represent to lock unsuccessful, function returns to vacation, directly finishes
If i) rreturn value is true, represent to write lock and add successfully, need to arrange unlimited endless loop, constantly call read lock status poll interface, detect whether all read locks are all removed, be 0 until obtain read counter, side jumps out circulation, returns true;
Determine and add and write lock interface: the WORM logic lock of specifying for Key adds writes lock, if this WORM logic lock is added and is read or write lock, circular wait, until successfully lock, specific procedure flow process as shown in Figure 4:
A) unlimited endless loop is set, constantly calls and attempt adding writing lock interface Try2EnableWrite
B) circulation continuation of failure
C) circulation is jumped out in success, returns true;
Lock interface is write in releasing: the WORM logic lock of specifying for Key is removed and write lock, specific procedure flow process as shown in Figure 5:
A) add this modular system physical locks
B) carry out Hash according to Key and search, find corresponding WORM logic lock
If c) do not found, do not do anything
If d) found, arranging and writing zone bit is 0, destroys WORM logic lock, deletes this lock index, releasing resource from Hash administration module simultaneously
E) separate this modular system physical locks, return;
Attempt read lock interface: for the WORM logic lock read lock that Key specifies, if this WORM logic lock is added and writes lock, unsuccessfully return to vacation, or after successfully locking, return true, specific procedure flow process as shown in Figure 6:
A) add this modular system physical locks
B) carry out Hash according to Key and search, find corresponding WORM logic lock
If c) do not found, attempt to create, if create failed rreturn value, vacation is set, jump to g)
If d) created successfully, this WORM logic lock index and Key are built to Key-Value pairing, be pushed to Hash administration module and manage, if Hash pushes unsuccessfully, destroy the WORM logic lock just having created, rreturn value is set to vacation, jumps to g)
If e) find or create successfully, check whether this lock has added to write lock, if locked, rreturn value is set to vacation
If f) can lock, read counter+1 is realized and being locked, rreturn value is set to very simultaneously
G) separate this modular system physical locks, return to rreturn value;
Determine read lock interface: the WORM logic lock read lock of specifying for Key, if this WORM logic lock is added and is write lock, circular wait, until successfully add read lock, specific procedure flow process as shown in Figure 7:
A) unlimited endless loop is set, constantly calls and attempt read lock Try2AddRead
B) circulation continuation of failure
C) circulation is jumped out in success, returns true;
Remove read lock interface: be that the WORM logic lock that Key specifies is removed read lock, specific procedure flow process as shown in Figure 8:
A) add this modular system physical locks
B) carry out Hash according to Key and search, find corresponding WORM logic lock
If c) do not found, do not do anything
If d) found, read counter-1, if <=0 of read counter now destroys WORM logic lock, deletes this lock index, releasing resource from Hash administration module simultaneously
E) separate this modular system physical locks, return.
The application call interface of described lock pond software module, for all computer programming language routine calls with call operation system API ability, comprises C, C++, compilation, Java, C#, VB.
Described lock pond software module provides the version of different operating system, realizes spanning operation system platform function, can under Windows, Linux and some other class Unix platform, work, and module product can be organized as following form, practical function output:
.h header file definition (application development interface declaration definition) (Windows, Linux, Unix like)
.dll dynamic link library (Windows)
.so dynamic link library (Linux, Unix like)
.obj Object linking file (Windows, Linux, Unix like)
.lib static link library (Windows, Linux, Unix like)
Development procedure of the present invention:
The present invention utilizes the structure of C language, defines a WORM logic lock, preserves WORM logic relevant information.
Utilize the compiling macro definition of C language, the access function of the different lock of abstract different operating system platform resource, provides standard calls method, realizes cross-platform transfer ability.
Exploitation C language function, provides the standard access function that WORM logic is relevant: add solutions and " write " to lock and add solution and " read " to lock, status poll function etc.All functions all can be for above-mentioned WORM logic lock operation.
Exploitation dynamic memory management module, it is memory pool, this step can be omitted, directly use malloc, the free function of C language, or the new of C Plus Plus, delete function are realized, and the present invention adopts the memory pool of inventor's designed, designed, be only used to reclaim and reuse internal memory, avoid memory fragmentation, meet server 7*24 hour long-play demand.This is not right emphasis of the present invention, repeats no more.
Exploitation Hash administration module, this step also can be omitted, and directly uses the Map class of C Plus Plus to realize, and the present invention adopts the Hash pond module of inventor's designed, designed, is in order to keep larger flexibility ratio and high efficiency.This is not right emphasis of the present invention, repeats no more.
Aforementioned WORM logic lock indicator index is defined as to the Value of Hash administration module, making every a slice " leaf " of Hash administration module is all the index of a WORM logic lock.Key does not limit, and application layer can be used arbitrary data as Key, looks for corresponding WORM logic lock in Hash pond, realizes operation.
Arbitrary data can be one section of binary data, a C language structure, a character string, an integer, or other data, by application layer, programmer plans, as long as ensure uniqueness, Key can only corresponding one logic lock, two different Key must the different logic lock of correspondence.
Design one, system physical locks, with above-mentioned logic lock interlock, guarantees that the retrieval in Hash pond, the operation of WORM lock body all complete by this system physical locks, and making all lock pond operations itself is multithreading safety.
Design standards application call interface, the access behavior of specification software module of the present invention, provides the unified interface of calling, for different application layer.
In programming procedure, pay close attention to professional platform independence, do not allow to call the distinctive function of each operating system, the power function that uses standard C/C Plus Plus to provide is only provided, pass through again different compilers, be compiled as the various binary code modules such as obj, lib, dll, so, allow application layer cross-platform, call practical function output across language compiling link.
Application example:
The present invention Rython in real time/historical data base plateform system in, applied Multi-instance.
In an application-specific example, need to do lock protection for data paragraphs different in thousands of data files, with the data resource secure access of guaranteeing height and give therein.Through calculating, the quantity size of lock demand is 400,000,000 locks.Certainly, traditional " little lock " model, under so large scale, will cause serious system resource bottleneck, can not realize, and the performance requirement of system does not allow to use inefficient " lock greatly " model.
Through to application of the present invention, system has obtained and has exceeded the concurrent reading and writing of 150,000 times/second up to the logic lock of 400,000,000 handles for, support simultaneously and access, and the actual system resource taking is little, has realized the demand.