CN112783421A - Asynchronous consumption method and device based on ring buffer - Google Patents

Asynchronous consumption method and device based on ring buffer Download PDF

Info

Publication number
CN112783421A
CN112783421A CN201911082030.0A CN201911082030A CN112783421A CN 112783421 A CN112783421 A CN 112783421A CN 201911082030 A CN201911082030 A CN 201911082030A CN 112783421 A CN112783421 A CN 112783421A
Authority
CN
China
Prior art keywords
address corresponding
ring buffer
read
pointer
consumption
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
CN201911082030.0A
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.)
Beijing Jingdong Century Trading Co Ltd
Beijing Jingdong Shangke Information Technology Co Ltd
Original Assignee
Beijing Jingdong Century Trading Co Ltd
Beijing Jingdong Shangke Information 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 Beijing Jingdong Century Trading Co Ltd, Beijing Jingdong Shangke Information Technology Co Ltd filed Critical Beijing Jingdong Century Trading Co Ltd
Priority to CN201911082030.0A priority Critical patent/CN112783421A/en
Publication of CN112783421A publication Critical patent/CN112783421A/en
Pending legal-status Critical Current

Links

Images

Classifications

    • GPHYSICS
    • G06COMPUTING; CALCULATING OR COUNTING
    • G06FELECTRIC DIGITAL DATA PROCESSING
    • G06F3/00Input arrangements for transferring data to be processed into a form capable of being handled by the computer; Output arrangements for transferring data from processing unit to output unit, e.g. interface arrangements
    • G06F3/06Digital input from, or digital output to, record carriers, e.g. RAID, emulated record carriers or networked record carriers
    • G06F3/0601Interfaces specially adapted for storage systems
    • G06F3/0602Interfaces specially adapted for storage systems specifically adapted to achieve a particular effect
    • G06F3/061Improving I/O performance
    • G06F3/0611Improving I/O performance in relation to response time
    • GPHYSICS
    • G06COMPUTING; CALCULATING OR COUNTING
    • G06FELECTRIC DIGITAL DATA PROCESSING
    • G06F3/00Input arrangements for transferring data to be processed into a form capable of being handled by the computer; Output arrangements for transferring data from processing unit to output unit, e.g. interface arrangements
    • G06F3/06Digital input from, or digital output to, record carriers, e.g. RAID, emulated record carriers or networked record carriers
    • G06F3/0601Interfaces specially adapted for storage systems
    • G06F3/0628Interfaces specially adapted for storage systems making use of a particular technique
    • G06F3/0629Configuration or reconfiguration of storage systems
    • GPHYSICS
    • G06COMPUTING; CALCULATING OR COUNTING
    • G06FELECTRIC DIGITAL DATA PROCESSING
    • G06F3/00Input arrangements for transferring data to be processed into a form capable of being handled by the computer; Output arrangements for transferring data from processing unit to output unit, e.g. interface arrangements
    • G06F3/06Digital input from, or digital output to, record carriers, e.g. RAID, emulated record carriers or networked record carriers
    • G06F3/0601Interfaces specially adapted for storage systems
    • G06F3/0628Interfaces specially adapted for storage systems making use of a particular technique
    • G06F3/0638Organizing or formatting or addressing of data
    • GPHYSICS
    • G06COMPUTING; CALCULATING OR COUNTING
    • G06FELECTRIC DIGITAL DATA PROCESSING
    • G06F3/00Input arrangements for transferring data to be processed into a form capable of being handled by the computer; Output arrangements for transferring data from processing unit to output unit, e.g. interface arrangements
    • G06F3/06Digital input from, or digital output to, record carriers, e.g. RAID, emulated record carriers or networked record carriers
    • G06F3/0601Interfaces specially adapted for storage systems
    • G06F3/0628Interfaces specially adapted for storage systems making use of a particular technique
    • G06F3/0655Vertical data movement, i.e. input-output transfer; data movement between one or more hosts and one or more storage devices
    • G06F3/0656Data buffering arrangements
    • 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/30Arrangements for executing machine instructions, e.g. instruction decode
    • G06F9/30003Arrangements for executing specific machine instructions
    • G06F9/3004Arrangements for executing specific machine instructions to perform operations on memory
    • G06F9/30043LOAD or STORE instructions; Clear instruction
    • 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/50Allocation of resources, e.g. of the central processing unit [CPU]
    • G06F9/5005Allocation of resources, e.g. of the central processing unit [CPU] to service a request
    • G06F9/5027Allocation of resources, e.g. of the central processing unit [CPU] to service a request the resource being a machine, e.g. CPUs, Servers, Terminals
    • 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/526Mutual exclusion algorithms
    • GPHYSICS
    • G06COMPUTING; CALCULATING OR COUNTING
    • G06FELECTRIC DIGITAL DATA PROCESSING
    • G06F2209/00Indexing scheme relating to G06F9/00
    • G06F2209/50Indexing scheme relating to G06F9/50
    • G06F2209/5018Thread allocation

Landscapes

  • Engineering & Computer Science (AREA)
  • Theoretical Computer Science (AREA)
  • Physics & Mathematics (AREA)
  • General Engineering & Computer Science (AREA)
  • General Physics & Mathematics (AREA)
  • Software Systems (AREA)
  • Human Computer Interaction (AREA)
  • Information Retrieval, Db Structures And Fs Structures Therefor (AREA)

Abstract

The invention discloses an asynchronous consumption method and device based on a ring buffer area, and relates to the technical field of computers. One embodiment of the method comprises: calculating the real-time distance between the address corresponding to the write pointer and the address corresponding to the read pointer in the annular buffer area, and judging whether the real-time distance is greater than the safety area distance; if yes, sequentially writing the user records in the write thread into the annular buffer area; and reading the user record from the ring buffer through the read thread, and writing the user record into a remote queue to realize asynchronous consumption. The implementation mode can avoid the problem of concurrent reading and writing, reduce time consumption and obviously improve the efficiency of asynchronous consumption.

Description

Asynchronous consumption method and device based on ring buffer
Technical Field
The invention relates to the technical field of computers, in particular to an asynchronous consumption method and device based on a ring buffer area.
Background
When a user accesses a webpage through an external channel, the network marketing system can conduct persistent processing on the link access records of the sampling parameters.
Commonly used persistence methods include:
firstly, synchronously writing in a database/remote queue, and writing user access records into the database/remote queue and then returning;
and secondly, asynchronously writing the user access records into the database/far-end queue, caching the user access records into a local queue, and then writing the user access records into the database in batches through a background thread, or pulling the user access records singly through the background thread, serializing the user access records and then sending the user access records to the far-end queue.
In the process of implementing the invention, the inventor finds that at least the following problems exist in the prior art:
1. when synchronous writing is performed, the writing efficiency is reduced due to the fact that a large number of requests face, and more time is consumed;
2. when the data is asynchronously written into the database, the memory is easy to overflow or the user request thread is blocked, so that the user request time delay is increased;
3. when asynchronously writing to the remote queue, each single pull of the background thread causes CAS (compare and swap) contention, which results in CPU (Central Processing Unit) waste.
Disclosure of Invention
In view of this, embodiments of the present invention provide an asynchronous consumption method and apparatus based on a ring buffer, which can avoid the problem of concurrent reading and writing, reduce time consumption, and significantly improve the efficiency of asynchronous consumption.
To achieve the above object, according to a first aspect of embodiments of the present invention, there is provided an asynchronous consumption method based on a ring buffer, including:
calculating the real-time distance between the address corresponding to the write pointer and the address corresponding to the read pointer in the annular buffer area, and judging whether the real-time distance is greater than the safety area distance;
if yes, sequentially writing the user records in the write thread into the annular buffer area;
and reading the user record from the ring buffer through the read thread, and writing the user record into a remote queue to realize asynchronous consumption.
Further, before the step of reading the user record from the ring buffer by the read thread, the asynchronous consumption method based on the ring buffer further includes: and determining that the address corresponding to the write pointer is consistent with the address corresponding to the wake-up pointer.
Further, the step of calculating the real-time distance between the address corresponding to the write pointer and the address corresponding to the read pointer in the ring buffer includes: and converting the address corresponding to the write pointer and the address corresponding to the read pointer into array subscripts, and calculating the real-time distance between the array subscripts.
Further, the step of reading the user record from the ring buffer by the read thread comprises:
circularly executing the following steps: judging whether a user record exists in the address corresponding to the reading pointer, if so, adding the user record into a consumption table, and shifting the address corresponding to the reading pointer by one bit;
until no user record exists in the address corresponding to the read pointer or the number of user records in the consumption table is equal to the number threshold.
Further, if no user record exists in the address corresponding to the read pointer, setting the address corresponding to the read pointer as the address corresponding to the wake-up pointer and waiting, and performing the step of executing in a circulating manner under the condition that the waiting time length exceeds the threshold time length.
Further, prior to the step of writing to the remote queue, the ring buffer based asynchronous consumption method further comprises: the user records read from the ring buffer are serialized.
According to a second aspect of embodiments of the present invention, there is provided a ring buffer based asynchronous consumption device, comprising:
the real-time distance calculation module is used for calculating the real-time distance between the address corresponding to the write pointer and the address corresponding to the read pointer in the annular buffer area;
the safety zone distance module is used for judging whether the real-time distance is greater than the safety zone distance;
the writing module is used for sequentially writing the user records in the writing thread into the annular buffer area under the condition that the real-time distance is greater than the safety area distance;
and the reading module is used for reading the user record from the ring buffer through the reading thread and writing the user record into a remote queue so as to realize asynchronous consumption.
Further, the asynchronous consumer device based on the ring buffer also comprises an address determination module, before the reading module reads the user record, the address determination module is used for determining that the address corresponding to the write pointer is consistent with the address corresponding to the wake-up pointer.
According to a third aspect of the embodiments of the present invention, there is provided a terminal, including:
one or more processors;
a storage device for storing one or more programs,
when executed by one or more processors, cause the one or more processors to implement any of the ring buffer based asynchronous consumption methods described above.
According to a fourth aspect of embodiments of the present invention, there is provided a computer readable medium having stored thereon a computer program which, when executed by a processor, implements any of the above-described ring buffer based asynchronous consumption methods.
One embodiment of the above invention has the following advantages or benefits: because the real-time distance between the address corresponding to the write pointer and the address corresponding to the read pointer in the annular buffer area is calculated, whether the real-time distance is greater than the safety area distance is judged; if yes, sequentially writing the user records in the write thread into the annular buffer area; the technical means of asynchronous consumption is realized by reading the user record from the ring buffer through the read thread and writing the user record into the remote queue, so that the technical problems of low efficiency, long time delay and easiness in occurrence of read-write concurrency in the prior art are solved, the read-write concurrency problem is further avoided, the time consumption is reduced, and the asynchronous consumption efficiency is remarkably improved.
Further effects of the above-mentioned non-conventional alternatives will be described below in connection with the embodiments.
Drawings
The drawings are included to provide a better understanding of the invention and are not to be construed as unduly limiting the invention. Wherein:
FIG. 1 is a schematic diagram of a main flow of a ring buffer based asynchronous consumption method according to a first embodiment of the present invention;
FIG. 2a is a schematic diagram of a main flow of a ring buffer based asynchronous consumption method according to a second embodiment of the present invention;
FIG. 2b is a schematic diagram of the ringbuffer structure in the method of FIG. 2 a;
FIG. 3 is a schematic diagram of the main modules of a ring buffer based asynchronous consumer device provided in accordance with an embodiment of the present invention;
FIG. 4 is an exemplary system architecture diagram in which embodiments of the present invention may be employed;
fig. 5 is a schematic block diagram of a computer system suitable for use in implementing a terminal device or server of an embodiment of the invention.
Detailed Description
Exemplary embodiments of the present invention are described below with reference to the accompanying drawings, in which various details of embodiments of the invention are included to assist understanding, and which are to be considered as merely exemplary. Accordingly, those of ordinary skill in the art will recognize that various changes and modifications of the embodiments described herein can be made without departing from the scope and spirit of the invention. Also, descriptions of well-known functions and constructions are omitted in the following description for clarity and conciseness.
Fig. 1 is a schematic diagram of a main flow of an asynchronous consumption method based on a ring buffer according to a first embodiment of the present invention, and as shown in fig. 1, the asynchronous consumption method based on a ring buffer according to the embodiment of the present invention includes:
step S101, calculating the real-time distance between the address corresponding to the write pointer and the address corresponding to the read pointer in the ring buffer, and judging whether the real-time distance is greater than the safety zone distance. If not, namely the real-time distance is smaller than the safety zone distance, ending; if yes, i.e. the real-time distance is greater than the safety zone distance, step S102 is executed.
The safety zone (safezone) is set for preventing the concurrency problem of the write thread and the read thread, the distance of the safety zone is not less than the thread number of the write thread, and the concurrency problem of the read thread and the write thread can be avoided by adopting less space through the setting.
According to the embodiment of the present invention, before the step of reading the user record from the ring buffer by the read thread, the asynchronous consumption method based on the ring buffer further includes: and determining that the address corresponding to the write pointer is consistent with the address corresponding to the wake-up pointer. By judging whether the address corresponding to the write pointer is equal to the address corresponding to the wake-up pointer or not, if so, indicating that a read thread waits at the address, and waking up all the read threads waiting at the address to perform read operation, the read efficiency can be effectively improved.
Specifically, according to an embodiment of the present invention, the step of calculating a real-time distance between an address corresponding to a write pointer (pushIndex) and an address corresponding to a read pointer (polindex) in the ring buffer includes: and converting the address corresponding to the write pointer and the address corresponding to the read pointer into array subscripts, and calculating the real-time distance between the array subscripts.
Before the above steps are performed, a ring buffer (ringbuffer) array is initialized to allocate memory space in the ringbuffer array, and elements in the array are all default to NULL, i.e., NULL pointers. In the asynchronous consumption process, a calculation formula is converted through array subscripts, the Push (writing) time group subscript is Push Index & (capacity-1), the Poll (reading) time group subscript is Poll Index & (capacity-1), wherein the capacity refers to the length of a ringbuffer array, and then the real-time distance between a read pointer and a write pointer is calculated.
And step S102, writing the user records in the write thread into the ring buffer area in sequence.
Specifically, according to a CAS (compare and swap) instruction, a write pointer address to be written is acquired, and user records are sequentially written. No lock operation exists in the write process of the push thread (user request), only one CAS operation is needed, and the time consumption is extremely small.
Step S103, reading the user record from the ring buffer through the read thread, and writing the user record into a remote queue to realize asynchronous consumption.
Specifically, when a plurality of read threads perform read operations, poll locks (read locks) are contended first, so that the number of times of CAS operations performed by the plurality of read threads is reduced, and the time consumption is further reduced.
Specifically, according to an embodiment of the present invention, the step of reading the user record from the ring buffer by the read thread includes:
circularly executing the following steps: judging whether a user record exists in the address corresponding to the reading pointer, if so, adding the user record into a consumption table, and shifting the address corresponding to the reading pointer by one bit;
until no user record exists in the address corresponding to the read pointer or the number of user records in the consumption table is equal to the number threshold.
That is, whether consumption is needed (reading operation is performed) is judged by circularly determining whether subscript elements (in corresponding addresses) corresponding to the polIndex (read pointer) have user records, so that one-time batch data reading of background processing threads (read threads) is realized, CAS competition is further reduced, and asynchronous consumption efficiency is improved.
Further, if no user record exists in the address corresponding to the read pointer, setting the address corresponding to the read pointer as the address corresponding to the wake-up pointer and waiting, and performing the step of executing in a circulating manner under the condition that the waiting time length exceeds the threshold time length. Specifically, wait (thread) is to release the CPU and block other poll threads (at this time, poll index corresponds to NULL, and there is no element in ringbuffer). The waiting (sleeping) time is not suitable to be too long, generally within 3 seconds, and too long may cause untimely poll thread consumption. The starting speed of the poll thread for re-consumption when no element (user record) is consumed is accelerated by waking up the pointer, and the time consumption of asynchronous consumption is further shortened.
Specifically, according to an embodiment of the present invention, before the step of writing to the remote queue, the asynchronous consumption method based on a ring buffer further includes: the user records read from the ring buffer are serialized.
According to the technical scheme of the embodiment of the invention, the real-time distance between the address corresponding to the write pointer and the address corresponding to the read pointer in the annular buffer area is calculated, and whether the real-time distance is greater than the safety area distance is judged; if yes, sequentially writing the user records in the write thread into the annular buffer area; the technical means of asynchronous consumption is realized by reading the user record from the ring buffer through the read thread and writing the user record into the remote queue, so that the technical problems of low efficiency, long time delay and easiness in occurrence of read-write concurrency in the prior art are solved, the read-write concurrency problem is further avoided, the time consumption is reduced, and the asynchronous consumption efficiency is remarkably improved.
FIG. 2a is a schematic diagram of a main flow of a ring buffer based asynchronous consumption method according to a second embodiment of the present invention; as shown in fig. 2a, an embodiment of the present invention provides an asynchronous consumption method based on a ring buffer, including:
step S201, initializing the ringbuffer array.
Specifically, a schematic structural diagram of a ring buffer (ringbuffer) is shown in fig. 2b, where ringbuffer belongs to an array, and memory addresses of elements in the array are stored continuously, and the ringbuffer array is initialized to allocate an array memory space, so that the elements in the array default to NULL (NULL pointer). In order to facilitate the conversion of the subsequent calculation of the real-time distance, the length of the ringbuffer is the power N of 2.
Step S202, calculating the real-time distance between the address corresponding to the write pointer and the address corresponding to the read pointer in the ring buffer.
Specifically, according to the embodiment of the present invention, in the asynchronous consumption process, the calculation formula is converted by array subscripts, where the Push (write) time group subscript is pushIndex & (capacity-1), and the Poll (read) time group subscript is polIndex & (capacity-1), where the capacity refers to the ringbuffer array length, and then the real-time distance between the read pointer and the write pointer is calculated.
Step S203, determine whether the real-time distance is greater than the safety zone distance. If yes, go to step S204, otherwise, end the process.
The safety zone (safezone) is set for preventing the concurrency problem of the write thread and the read thread, the distance of the safety zone is not less than the thread number of the write thread, and the concurrency problem of the read thread and the write thread can be avoided by adopting less space through the setting.
In step S204, the user requests in the write thread are written into the ring buffer in sequence by CAS commands.
The write-in process of the write-in thread does not carry out locking operation, and only once CAS operation is needed, so that the time consumption is greatly shortened.
In step S205, it is determined that the address corresponding to the write pointer is consistent with the address corresponding to the wake-up pointer. If yes, go to step S206, otherwise go to step S202.
By judging whether the address corresponding to the write pointer is equal to the address corresponding to the wake-up pointer or not, if so, indicating that a read thread waits at the address, and waking up all the read threads waiting at the address to perform read operation, the read efficiency can be effectively improved.
In step S206, a plurality of read threads compete for the read lock and read the user request from the ring buffer.
Under the condition that a plurality of reading threads perform reading operation, reading operation is performed after the poll lock (reading lock) is contended, so that the phenomenon that a plurality of reading threads perform CAS operation for many times is reduced, and time consumption is further reduced.
Step S207, writing the pulled user request into the remote queue after serialization processing, so as to implement asynchronous consumption.
Specifically, according to an embodiment of the present invention, the step of reading the user record from the ring buffer by the read thread includes:
circularly executing the following steps: judging whether a consumption-substituting element (user record) exists in the address corresponding to the read pointer, if so, removing the user record from the ringbuffer, adding the user record into the consumption table, and updating the address of the read pointer to polIndex + + (moving the address corresponding to the read pointer by one bit along the reading direction);
until no user record exists in the address corresponding to the read pointer or the number of user records in the consumption table is equal to the number threshold.
That is, whether consumption is needed (reading operation is performed) is judged by circularly determining whether subscript elements (in corresponding addresses) corresponding to the polIndex (read pointer) have user records, so that one-time batch data reading of background processing threads (read threads) is realized, CAS competition is further reduced, and asynchronous consumption efficiency is improved.
Further, if no user record exists in the address corresponding to the read pointer, setting the address corresponding to the read pointer as the address corresponding to the wake-up pointer and waiting, and performing the step of executing in a circulating manner under the condition that the waiting time length exceeds the threshold time length. Specifically, wait (thread) is to release the CPU and block other poll threads (at this time, poll index corresponds to NULL, and there is no element in ringbuffer). The waiting (sleeping) time is not suitable to be too long, generally within 3 seconds, and too long may cause untimely poll thread consumption. The starting speed of the poll thread for re-consumption when no element (user record) is consumed is accelerated by waking up the pointer, and the time consumption of asynchronous consumption is further shortened.
According to the technical scheme of the embodiment of the invention, the real-time distance between the address corresponding to the write pointer and the address corresponding to the read pointer in the annular buffer area is calculated, and whether the real-time distance is greater than the safety area distance is judged; if yes, sequentially writing the user records in the write thread into the annular buffer area; the technical means of asynchronous consumption is realized by reading the user record from the ring buffer through the read thread and writing the user record into the remote queue, so that the technical problems of low efficiency, long time delay and easiness in occurrence of read-write concurrency in the prior art are solved, the read-write concurrency problem is further avoided, the time consumption is reduced, and the asynchronous consumption efficiency is remarkably improved.
FIG. 3 is a schematic diagram of the main modules of a ring buffer based asynchronous consumer device provided in accordance with an embodiment of the present invention; as shown in fig. 3, an embodiment of the present invention provides a ring buffer based asynchronous consumer device 300, which includes:
the real-time distance calculating module 301 is configured to calculate a real-time distance between an address corresponding to the write pointer and an address corresponding to the read pointer in the ring buffer.
The safety zone (safezone) is set for preventing the concurrency problem of the write thread and the read thread, the distance of the safety zone is not less than the thread number of the write thread, and the concurrency problem of the read thread and the write thread can be avoided by adopting less space through the setting.
According to an embodiment of the present invention, the asynchronous consumer device 300 further includes an address determination module, before the module reads the user record from the ring buffer via the read thread, the address determination module is configured to determine that the address corresponding to the write pointer is consistent with the address corresponding to the wake-up pointer.
By judging whether the address corresponding to the write pointer is equal to the address corresponding to the wake-up pointer or not, if so, indicating that a read thread waits at the address, and waking up all the read threads waiting at the address to perform read operation, the read efficiency can be effectively improved.
Specifically, according to an embodiment of the present invention, the real-time distance calculating module 301 is further configured to: and converting the address corresponding to the write pointer and the address corresponding to the read pointer into array subscripts, and calculating the real-time distance between the array subscripts.
The ring buffer (ringbuffer) array is initialized to allocate memory space in the ringbuffer array, and elements in the array are all defaulted to NULL, i.e., NULL pointers. In the asynchronous consumption process, a calculation formula is converted through array subscripts, the Push (writing) time group subscript is Push Index & (capacity-1), the Poll (reading) time group subscript is Poll Index & (capacity-1), wherein the capacity refers to the length of a ringbuffer array, and then the real-time distance between a read pointer and a write pointer is calculated.
A safe zone distance module 302, configured to determine whether the real-time distance is greater than the safe zone distance. If not, namely the real-time distance is smaller than the safety zone distance, ending; if yes, namely the real-time distance is larger than the safe zone distance, the writing operation is executed.
And the writing module 303 is configured to write the user records in the write thread into the ring buffer sequentially when the real-time distance is greater than the safe area distance.
Specifically, according to a CAS (compare and swap) instruction, a write pointer address to be written is acquired, and user records are sequentially written. No lock operation exists in the write process of the push thread (user request), only one CAS operation is needed, and the time consumption is extremely small.
And the reading module 304 is configured to read the user record from the ring buffer through a read thread, and write the user record into a remote queue to implement asynchronous consumption.
Specifically, when a plurality of read threads perform read operations, poll locks (read locks) are contended first, so that the number of times of CAS operations performed by the plurality of read threads is reduced, and the time consumption is further reduced.
Specifically, according to an embodiment of the present invention, the reading module 304 is further configured to:
circularly executing the following steps: judging whether a user record exists in the address corresponding to the reading pointer, if so, adding the user record into a consumption table, and shifting the address corresponding to the reading pointer by one bit;
until no user record exists in the address corresponding to the read pointer or the number of user records in the consumption table is equal to the number threshold.
That is, whether consumption is needed (reading operation is performed) is judged by circularly determining whether subscript elements (in corresponding addresses) corresponding to the polIndex (read pointer) have user records, so that one-time batch data reading of background processing threads (read threads) is realized, CAS competition is further reduced, and asynchronous consumption efficiency is improved.
Further, if there is no user record in the address corresponding to the read pointer, the reading module 304 is configured to set the address corresponding to the read pointer as the address corresponding to the wakeup pointer and wait, and perform the loop execution step when the waiting duration exceeds the threshold duration. Specifically, wait (thread) is to release the CPU and block other poll threads (at this time, poll lnde 4 corresponds to NULL, no element in ringbuffer). The waiting (sleeping) time is not suitable to be too long, generally within 3 seconds, and too long may cause untimely poll thread consumption. The starting speed of the poll thread for re-consumption when no element (user record) is consumed is accelerated by waking up the pointer, and the time consumption of asynchronous consumption is further shortened.
Specifically, according to an embodiment of the present invention, before the step of writing to the remote queue, the reading module 304 is further configured to: the asynchronous consumption method based on the ring buffer further comprises the following steps: the user records read from the ring buffer are serialized.
According to the technical scheme of the embodiment of the invention, the real-time distance between the address corresponding to the write pointer and the address corresponding to the read pointer in the annular buffer area is calculated, and whether the real-time distance is greater than the safety area distance is judged; if yes, sequentially writing the user records in the write thread into the annular buffer area; the technical means of asynchronous consumption is realized by reading the user record from the ring buffer through the read thread and writing the user record into the remote queue, so that the technical problems of low efficiency, long time delay and easiness in occurrence of read-write concurrency in the prior art are solved, the read-write concurrency problem is further avoided, the time consumption is reduced, and the asynchronous consumption efficiency is remarkably improved.
It can be understood that, since the method embodiment and the apparatus embodiment are different presentation forms of the same technical concept, the content of the method embodiment portion in the present application should be synchronously adapted to the apparatus embodiment portion, and is not described herein again.
Fig. 4 illustrates an exemplary system architecture 400 of a ring buffer based asynchronous consumption method or a ring buffer based asynchronous consumption device to which embodiments of the present invention may be applied.
As shown in fig. 4, the system architecture 400 may include terminal devices 401, 402, 403, a network 404, and a server 405 (this architecture is merely an example, and the components included in a particular architecture may be adapted according to application specific circumstances). The network 404 serves as a medium for providing communication links between the terminal devices 401, 402, 403 and the server 405. Network 404 may include various types of connections, such as wire, wireless communication links, or fiber optic cables, to name a few.
A user may use terminal devices 401, 402, 403 to interact with a server 405 over a network 404 to receive or send messages or the like. The terminal devices 401, 402, 403 may have installed thereon various communication client applications, such as shopping-like applications, web browser applications, search-like applications, instant messaging tools, mailbox clients, social platform software, etc. (by way of example only).
The terminal devices 401, 402, 403 may be various electronic devices having a display screen and supporting web browsing, including but not limited to smart phones, tablet computers, laptop portable computers, desktop computers, and the like.
The server 405 may be a server providing various services, such as a background management server (for example only) providing support for shopping websites browsed by users using the terminal devices 401, 402, 403. The backend management server may analyze and perform other processing on the received data such as the product information query request, and feed back a processing result (for example, target push information, product information — just an example) to the terminal device.
It should be noted that the asynchronous consumption method based on ring buffer provided by the embodiment of the present invention is generally executed by the server 405, and accordingly, the asynchronous consumption device based on ring buffer is generally disposed in the server 405.
It should be understood that the number of terminal devices, networks, and servers in fig. 4 is merely illustrative. There may be any number of terminal devices, networks, and servers, as desired for implementation.
Referring now to FIG. 5, shown is a block diagram of a computer system 500 suitable for use with a terminal device implementing an embodiment of the present invention. The terminal device shown in fig. 5 is only an example, and should not bring any limitation to the functions and the scope of use of the embodiments of the present invention.
As shown in fig. 5, the computer system 500 includes a Central Processing Unit (CPU)501 that can perform various appropriate actions and processes according to a program stored in a Read Only Memory (ROM)502 or a program loaded from a storage section 508 into a Random Access Memory (RAM) 503. In the RAM 503, various programs and data necessary for the operation of the system 500 are also stored. The CPU 501, ROM 502, and RAM 503 are connected to each other via a bus 504. An input/output (I/O) interface 505 is also connected to bus 504.
The following components are connected to the I/O interface 505: an input portion 506 including a keyboard, a mouse, and the like; an output portion 507 including a display such as a Cathode Ray Tube (CRT), a Liquid Crystal Display (LCD), and the like, and a speaker; a storage portion 508 including a hard disk and the like; and a communication section 509 including a network interface card such as a LAN card, a modem, or the like. The communication section 509 performs communication processing via a network such as the internet. The driver 510 is also connected to the I/O interface 505 as necessary. A removable medium 511 such as a magnetic disk, an optical disk, a magneto-optical disk, a semiconductor memory, or the like is mounted on the drive 510 as necessary, so that a computer program read out therefrom is mounted into the storage section 508 as necessary.
In particular, according to the embodiments of the present disclosure, the processes described above with reference to the flowcharts may be implemented as computer software programs. For example, embodiments of the present disclosure include a computer program product comprising a computer program embodied on a computer readable medium, the computer program comprising program code for performing the method illustrated in the flow chart. In such an embodiment, the computer program may be downloaded and installed from a network through the communication section 509, and/or installed from the removable medium 511. The computer program performs the above-described functions defined in the system of the present invention when executed by the Central Processing Unit (CPU) 501.
It should be noted that the computer readable medium shown in the present invention can be a computer readable signal medium or a computer readable storage medium or any combination of the two. A computer readable storage medium may be, for example, but not limited to, an electronic, magnetic, optical, electromagnetic, infrared, or semiconductor system, apparatus, or device, or any combination of the foregoing. More specific examples of the computer readable storage medium may include, but are not limited to: an electrical connection having one or more wires, a portable computer diskette, a hard disk, a Random Access Memory (RAM), a read-only memory (ROM), an erasable programmable read-only memory (EPROM or flash memory), an optical fiber, a portable compact disc read-only memory (CD-ROM), an optical storage device, a magnetic storage device, or any suitable combination of the foregoing. In the present invention, a computer readable storage medium may be any tangible medium that can contain, or store a program for use by or in connection with an instruction execution system, apparatus, or device. In the present invention, however, a computer readable signal medium may include a propagated data signal with computer readable program code embodied therein, for example, in baseband or as part of a carrier wave. Such a propagated data signal may take many forms, including, but not limited to, electro-magnetic, optical, or any suitable combination thereof. A computer readable signal medium may also be any computer readable medium that is not a computer readable storage medium and that can communicate, propagate, or transport a program for use by or in connection with an instruction execution system, apparatus, or device. Program code embodied on a computer readable medium may be transmitted using any appropriate medium, including but not limited to: wireless, wire, fiber optic cable, RF, etc., or any suitable combination of the foregoing.
The flowchart and block diagrams in the figures illustrate the architecture, functionality, and operation of possible implementations of systems, methods and computer program products according to various embodiments of the present invention. In this regard, each block in the flowchart or block diagrams may represent a module, segment, or portion of code, which comprises one or more executable instructions for implementing the specified logical function(s). It should also be noted that, in some alternative implementations, the functions noted in the block may occur out of the order noted in the figures. For example, two blocks shown in succession may, in fact, be executed substantially concurrently, or the blocks may sometimes be executed in the reverse order, depending upon the functionality involved. It will also be noted that each block of the block diagrams or flowchart illustration, and combinations of blocks in the block diagrams or flowchart illustration, can be implemented by special purpose hardware-based systems which perform the specified functions or acts, or combinations of special purpose hardware and computer instructions.
The modules described in the embodiments of the present invention may be implemented by software or hardware. The described modules may also be provided in a processor, which may be described as: a processor includes a real-time distance calculation module, a safe zone distance module, a write module, and a read module. The names of these modules do not constitute a limitation to the module itself in some cases, for example, the real-time distance calculation module may also be described as "a module for calculating a real-time distance between an address corresponding to a write pointer and an address corresponding to a read pointer in a ring buffer".
As another aspect, the present invention also provides a computer-readable medium that may be contained in the apparatus described in the above embodiments; or may be separate and not incorporated into the device. The computer readable medium carries one or more programs which, when executed by a device, cause the device to comprise: calculating the real-time distance between the address corresponding to the write pointer and the address corresponding to the read pointer in the annular buffer area, and judging whether the real-time distance is greater than the safety area distance; if yes, sequentially writing the user records in the write thread into the annular buffer area; and reading the user record from the ring buffer through a read thread, and writing the user record into a remote queue to realize asynchronous consumption.
According to the technical scheme of the embodiment of the invention, the real-time distance between the address corresponding to the write pointer and the address corresponding to the read pointer in the annular buffer area is calculated, and whether the real-time distance is greater than the safety area distance is judged; if yes, sequentially writing the user records in the write thread into the annular buffer area; the technical means of asynchronous consumption is realized by reading the user record from the ring buffer through the read thread and writing the user record into the remote queue, so that the technical problems of low efficiency, long time delay and easiness in occurrence of read-write concurrency in the prior art are solved, the read-write concurrency problem is further avoided, the time consumption is reduced, and the asynchronous consumption efficiency is remarkably improved.
The above-described embodiments should not be construed as limiting the scope of the invention. Those skilled in the art will appreciate that various modifications, combinations, sub-combinations, and substitutions can occur, depending on design requirements and other factors. Any modification, equivalent replacement, and improvement made within the spirit and principle of the present invention should be included in the protection scope of the present invention.

Claims (10)

1. An asynchronous consumption method based on a ring buffer, comprising:
calculating the real-time distance between the address corresponding to the write pointer and the address corresponding to the read pointer in the annular buffer area, and judging whether the real-time distance is greater than the safety area distance;
if yes, sequentially writing the user records in the write thread into the annular buffer area;
and reading the user record from the ring buffer through a read thread, and writing the user record into a remote queue to realize asynchronous consumption.
2. The asynchronous consumption ring buffer-based method according to claim 1, wherein prior to said step of reading user records from said ring buffer by a read thread, said asynchronous consumption ring buffer-based method further comprises: and determining that the address corresponding to the write pointer is consistent with the address corresponding to the wake-up pointer.
3. The asynchronous consumption method based on ring buffer according to claim 1, wherein said step of calculating the real-time distance between the address corresponding to the write pointer and the address corresponding to the read pointer in the ring buffer comprises: and converting the address corresponding to the write pointer and the address corresponding to the read pointer into array subscripts, and calculating the real-time distance between the array subscripts.
4. The asynchronous consumption ring buffer-based method according to claim 1, wherein said step of reading the user record from the ring buffer by a read thread comprises:
circularly executing the following steps: judging whether a user record exists in an address corresponding to a read pointer, if so, adding the user record to a consumption table, and shifting the address corresponding to the read pointer by one bit;
until no user record exists in the address corresponding to the read pointer or the number of user records in the consumption table is equal to a number threshold.
5. The asynchronous consumption method based on ring buffer according to claim 4, wherein if there is no user record in the address corresponding to the read pointer, the address corresponding to the read pointer is set as the address corresponding to the wake-up pointer and waits, and the loop execution step is performed when the waiting duration exceeds the threshold duration.
6. The asynchronous ring buffer-based consumption method according to claim 1, wherein prior to said step of writing to a remote queue, said asynchronous ring buffer-based consumption method further comprises: and carrying out serialization processing on the user records read from the ring buffer.
7. An asynchronous consumer device based on a ring buffer, comprising:
the real-time distance calculation module is used for calculating the real-time distance between the address corresponding to the write pointer and the address corresponding to the read pointer in the annular buffer area;
the safety zone distance module is used for judging whether the real-time distance is greater than the safety zone distance;
the writing module is used for sequentially writing the user records in the writing thread into the annular buffer area under the condition that the real-time distance is greater than the safety area distance;
and the reading module is used for reading the user record from the ring buffer through a reading thread and writing the user record into a remote queue so as to realize asynchronous consumption.
8. The asynchronous ring buffer-based consumer device according to claim 7, further comprising an address determination module configured to determine that the address corresponding to the write pointer matches the address corresponding to the wake-up pointer before the read module reads the user record.
9. A terminal, comprising:
one or more processors;
a storage device for storing one or more programs,
when executed by the one or more processors, cause the one or more processors to implement the method of any one of claims 1-6.
10. A computer-readable medium, on which a computer program is stored, which, when being executed by a processor, carries out the method according to any one of claims 1-6.
CN201911082030.0A 2019-11-07 2019-11-07 Asynchronous consumption method and device based on ring buffer Pending CN112783421A (en)

Priority Applications (1)

Application Number Priority Date Filing Date Title
CN201911082030.0A CN112783421A (en) 2019-11-07 2019-11-07 Asynchronous consumption method and device based on ring buffer

Applications Claiming Priority (1)

Application Number Priority Date Filing Date Title
CN201911082030.0A CN112783421A (en) 2019-11-07 2019-11-07 Asynchronous consumption method and device based on ring buffer

Publications (1)

Publication Number Publication Date
CN112783421A true CN112783421A (en) 2021-05-11

Family

ID=75748899

Family Applications (1)

Application Number Title Priority Date Filing Date
CN201911082030.0A Pending CN112783421A (en) 2019-11-07 2019-11-07 Asynchronous consumption method and device based on ring buffer

Country Status (1)

Country Link
CN (1) CN112783421A (en)

Cited By (1)

* Cited by examiner, † Cited by third party
Publication number Priority date Publication date Assignee Title
CN117407148A (en) * 2022-07-08 2024-01-16 华为技术有限公司 Data writing method, data reading device, electronic equipment and storage medium

Citations (7)

* Cited by examiner, † Cited by third party
Publication number Priority date Publication date Assignee Title
CN101296217A (en) * 2007-04-24 2008-10-29 中芯国际集成电路制造(上海)有限公司 Elastic buffering mechanism and method for compensating clock diversity
CN102591815A (en) * 2011-12-27 2012-07-18 Tcl集团股份有限公司 Method and device for using annular data buffer to read and write batch data
CN102708207A (en) * 2012-05-23 2012-10-03 深圳市龙视传媒有限公司 Method and device for receiving processing data and video server
US20130081060A1 (en) * 2011-09-22 2013-03-28 Oleksandr Otenko System and Method for Efficient Concurrent Queue Implementation
CN105183665A (en) * 2015-09-08 2015-12-23 福州瑞芯微电子股份有限公司 Data-caching access method and data-caching controller
CN107729559A (en) * 2017-11-08 2018-02-23 携程旅游网络技术(上海)有限公司 Method, system, equipment and the storage medium of data base read-write asynchronous access
CN108733344A (en) * 2018-05-28 2018-11-02 深圳市道通智能航空技术有限公司 Data read-write method, device and circle queue

Patent Citations (7)

* Cited by examiner, † Cited by third party
Publication number Priority date Publication date Assignee Title
CN101296217A (en) * 2007-04-24 2008-10-29 中芯国际集成电路制造(上海)有限公司 Elastic buffering mechanism and method for compensating clock diversity
US20130081060A1 (en) * 2011-09-22 2013-03-28 Oleksandr Otenko System and Method for Efficient Concurrent Queue Implementation
CN102591815A (en) * 2011-12-27 2012-07-18 Tcl集团股份有限公司 Method and device for using annular data buffer to read and write batch data
CN102708207A (en) * 2012-05-23 2012-10-03 深圳市龙视传媒有限公司 Method and device for receiving processing data and video server
CN105183665A (en) * 2015-09-08 2015-12-23 福州瑞芯微电子股份有限公司 Data-caching access method and data-caching controller
CN107729559A (en) * 2017-11-08 2018-02-23 携程旅游网络技术(上海)有限公司 Method, system, equipment and the storage medium of data base read-write asynchronous access
CN108733344A (en) * 2018-05-28 2018-11-02 深圳市道通智能航空技术有限公司 Data read-write method, device and circle queue

Cited By (1)

* Cited by examiner, † Cited by third party
Publication number Priority date Publication date Assignee Title
CN117407148A (en) * 2022-07-08 2024-01-16 华为技术有限公司 Data writing method, data reading device, electronic equipment and storage medium

Similar Documents

Publication Publication Date Title
CN112540806B (en) Method and device for rendering small program page, electronic equipment and storage medium
CN107256206B (en) Method and device for converting character stream format
US11314451B2 (en) Method and apparatus for storing data
US20200050481A1 (en) Computing Method Applied to Artificial Intelligence Chip, and Artificial Intelligence Chip
CN110555068A (en) Data export method and device
KR20210083222A (en) Method, apparatus, device and storage medium for processing voice data
CN113778644B (en) Task processing method, device, equipment and storage medium
CN111258800A (en) Page processing method and device and electronic equipment
CN112084042A (en) Message processing method and device
CN112783421A (en) Asynchronous consumption method and device based on ring buffer
CN113051055A (en) Task processing method and device
CN110515749B (en) Method, device, server and storage medium for queue scheduling of information transmission
CN112948138A (en) Method and device for processing message
CN109213815B (en) Method, device, server terminal and readable medium for controlling execution times
CN113722037B (en) User interface refreshing method and device, electronic equipment and storage medium
CN113377295B (en) Data storage and reading method, device and equipment for multi-producer single-consumer
CN112784139A (en) Query method, query device, electronic equipment and computer readable medium
CN113760274A (en) Front-end component logic injection method and device
CN113656645B (en) Log consumption method and device
CN113132480B (en) Data transmission method, device and system
CN111324512B (en) Method, apparatus, electronic device, and computer-readable medium for generating text
CN114968936B (en) Time line acquisition method and device based on data lake and execution node
CN115297169B (en) Data processing method, device, electronic equipment and medium
CN118673068A (en) Data transfer method, device, electronic equipment and storage medium
CN114925305A (en) Page loading method and device

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