Financial market subscription method, device and medium based on shared memory message queue
By adopting shared memory message queue and lock-free queue technology in the financial market system, the loose coupling architecture between business processes and market processes is realized, solving the problems of high coupling of existing systems and low market push performance, and improving market subscription and Push efficiency and system flexibility.
Patent Information
- Application Number
- CN202311398128.3
- Authority / Receiving Office
- CN · China
- Patent Type
- Patents(China)
- Current Assignee / Owner
- Filing Date
- 2023-10-26
- Publication Date
- 2024-12-10
- Estimated Expiration
- 2043-10-26
AI Technical Summary
The existing financial market system has high coupling, low architectural flexibility, and low market push performance, making it difficult to meet the needs of high concurrency and low latency.
Using a multi-process architecture based on shared memory message queues, the loose coupling of business processes and market processes is achieved through shared memory, and the ring queue and lock-free queue are used to reduce communication delays between processes, improve market push speed and system flexibility.
It has achieved independent upgrades between business processes and market processes, improved market subscription and push efficiency, reduced system development costs, and improved architecture flexibility and reusability.
Smart Images

Figure CN117608871B_ABST
Abstract
Description
Technical Field
[0001] The present invention relates to the field of server technology, and in particular to a financial market subscription method, device and medium based on a shared memory message queue. Background Art
[0002] With the continuous updating of technology and the rapid development of business, traditional modular development methods are becoming increasingly difficult to adapt to the complex business changes in today's securities market. The single-process implementation method cannot meet the needs of rapid upgrades and iterations of multi-market conditions. The microservice architecture is also difficult to adapt to the market's high concurrency and low latency requirements. The frequent upgrades and modifications of the market system also bring unpredictable systemic risks.
[0003] Existing financial quotes generally use a single process to implement subscription services for all markets. Business implementation and quote implementation are mutually coupled, making it difficult to transform the business or upgrade the quotes. At present, upgrades to different quote markets are relatively frequent, and the upgrade plan time is not unified. The upgrade and operation and maintenance of a single market affect the entire business system. With the continuous development of the business, the business system is becoming more and more complex, and the number of financial quotes that need to be subscribed is constantly increasing. The system architecture of financial quotes needs to be constantly adjusted as the business changes. The delay of quote push is also a key point that needs to be considered when building the quote system. The new system architecture must not only solve the coupling of different markets, but also solve the performance loss caused by system splitting.
[0004] In a multi-process architecture, the business process and the market process are split, and there is a lack of reliable vertical expansion solutions. The latency of inter-process network communication is difficult to meet the low-latency market communication scenario. How to decouple the business process and the market process and improve the efficiency of market subscription and push is an urgent problem to be solved. Summary of the invention
[0005] The main purpose of the present invention is to provide a financial market subscription method, device and medium based on a shared memory message queue, aiming to solve the technical problems of high coupling of existing business systems, low architectural flexibility and low market push performance.
[0006] A first aspect of the present invention provides a financial market subscription method based on a shared memory message queue, comprising:
[0007] The business process writes a subscription request for financial market data to the request message queue of the market process in the shared memory;
[0008] The business process monitors the response message queue in the shared memory to see whether it receives the market update notification written by the market process based on the subscription request;
[0009] When receiving the market update notification, the business process searches the market code mapping table of the corresponding market cache area according to the market code in the market update notification to obtain the address offset of the market information corresponding to the market code in the market cache area;
[0010] The business process reads the subscribed market data in the market information from the corresponding market buffer area in the shared memory based on the address offset.
[0011] Optionally, in a first implementation of the first aspect of the present invention, the financial market subscription method further includes:
[0012] The market process monitors the request message queue in the shared memory to see whether it receives the subscription request written by the business process;
[0013] When the market process receives the subscription request, the subscription information and the response message queue information in the subscription request are written into the subscription list.
[0014] Optionally, in a second implementation of the first aspect of the present invention, the financial market subscription further includes:
[0015] The market process receives the market data provided by the corresponding market data source, parses it, and writes it into the corresponding market buffer area in the shared memory;
[0016] The market process traverses the subscription list, and if there is subscription information matching the newly written market data in the subscription list, a corresponding market update notification is generated and written into the response message queue of the corresponding business process.
[0017] Optionally, in a third implementation of the first aspect of the present invention, the financial market subscription method further includes:
[0018] Before initiating a subscription request, each business process creates its own reply message queue in the shared memory. Different business processes subscribe to the same or different financial quotes, and the same reply message queue is only accessible to the same quote process.
[0019] Optionally, in a fourth implementation of the first aspect of the present invention, the financial market subscription method further includes:
[0020] Before receiving a subscription request, each market process creates its own request message queue and market buffer in the shared memory. Different market processes are prohibited from cross-accessing the request message queue and market buffer of other market processes. The same request message queue and market buffer are allowed to be accessed by different business processes.
[0021] Optionally, in a fifth implementation of the first aspect of the present invention, the shared memory message queue includes a header and a data area, the header is used to store a data block memory size, a data block number, a read index, a write index and a timed mutex lock, the data area is a data block array of a fixed memory size, the data block array includes multiple data blocks, each data block includes a data block status, a data length and data, the data block status is used to identify whether the data in the corresponding data block is empty, and each of the shared memory message queues adopts a ring queue structure;
[0022] When the write index is equal to the read index, the circular queue of the shared memory message queue is in an empty data state; when the write index plus 1 is equal to the read index, the circular queue of the shared memory message queue is in a full data state; when calculating a new read index or write index, if the read index or write index is greater than or equal to the number of data blocks, the read index or write index is reset to 0; when writing data to any data block in the shared memory message queue, the data block to be written is locked by the timing mutex lock.
[0023] Optionally, in a sixth implementation of the first aspect of the present invention, the market information is stored in a market cache area in rows, the storage area allocated to each row is the same length, the market information stored in each row storage area includes a market code, an index and market data, there are multiple caches for storing market data and a circular queue structure is adopted, the length and structure of each cache are the same, and the index is the read index and write index of the circular queue of the cache.
[0024] Optionally, in a seventh implementation manner of the first aspect of the present invention, the cache ring queue includes at least three caches, the read index of the ring queue is used to identify the readable cache in the current ring queue, and the write index of the ring queue is used to identify the writable cache in the current ring queue;
[0025] The read index and write index of the circular queue are written into the market data buffer by the market data process single thread, and the execution order is: write market data, change write index, and change read index.
[0026] A second aspect of the present invention provides a computer device, comprising: a memory and at least one processor, wherein the memory stores instructions; the at least one processor calls the instructions in the memory so that the computer device executes each step of the above-mentioned financial market subscription method based on a shared memory message queue.
[0027] A third aspect of the present invention provides a computer-readable storage medium, wherein the computer-readable storage medium stores instructions, which, when executed on a computer, enable the computer to execute the various steps of the above-mentioned financial market subscription method based on a shared memory message queue.
[0028] Compared with the prior art, the beneficial effects of the present invention are as follows: the present invention is based on a multi-process architecture, realizes a financial quotation subscription method based on multi-process shared memory communication, realizes a loosely coupled architecture that separates the business process from the quotation process through a multi-process shared memory management method and an inter-process shared memory communication method, and furthermore, the present invention reduces the delay of inter-process communication through shared memory and a lock-free queue based on time difference, improves the push speed of real-time quotation, improves the reusability of the quotation process, and also reduces the development cost of the system. In addition, the business process and the quotation process can be upgraded independently, which improves the development and deployment efficiency and the flexibility of the technical architecture. BRIEF DESCRIPTION OF THE DRAWINGS
[0029] Figure 1 A schematic diagram of an embodiment of the multi-process shared memory architecture design of the present invention;
[0030] Figure 2 A schematic flow chart of an embodiment of a financial market subscription method based on a shared memory message queue of the present invention;
[0031] Figure 3 It is a flowchart of another embodiment of a financial market subscription method based on a shared memory message queue of the present invention;
[0032] Figure 4 A schematic diagram of an embodiment of the present invention in which a business process and a market process communicate based on a shared memory;
[0033] Figure 5 It is a schematic diagram of an embodiment of multiple processes writing and listening to a shared memory queue in an embodiment of the present invention;
[0034] Figure 6 A schematic diagram of the structure of an embodiment of a market data buffer area in an embodiment of the present invention;
[0035] Figure 7 The figure is a schematic diagram of the structure of a computer device according to an embodiment of the present invention. DETAILED DESCRIPTION
[0036] In order to make the purpose, technical solution and advantages of the embodiments of the present invention clearer, the technical solution in the embodiments of the present invention will be clearly and completely described below in conjunction with the drawings in the embodiments of the present invention. Obviously, the described embodiments are part of the embodiments of the present invention, not all of the embodiments. Based on the embodiments of the present invention, all other embodiments obtained by ordinary technicians in this field without creative work are within the scope of protection of the present invention.
[0037] In this application, the term "include", "comprise" or any other variant thereof is intended to cover non-exclusive inclusion, so that the process, method, article or system including a series of elements includes not only those elements, but also other elements not explicitly listed, or also includes elements inherent to such process, method, article or system. In the absence of more restrictions, the elements defined by the sentence "include one..." do not exclude the existence of other identical elements in the process, method, article or system including the element. In this application, "at least one (item)" refers to one or more, "multiple" refers to two or more, "at least two (items)" refers to two or three and more than three, and "and / or" is used to describe the association relationship of the associated objects, indicating that three relationships can exist, for example, "A and / or B" can represent: only A exists, only B exists, and A and B exist at the same time, wherein A, B can be singular or plural. The character " / " generally indicates that the associated objects before and after are in a "or" relationship. "At least one of the following (individuals)" or its similar expression refers to any combination of these items. For example, at least one of a, b, or c can represent: a, b, c, "a and b", "a and c", "b and c", or "a and b and c".
[0038] In order to solve the technical problems of high coupling of existing business systems, low architectural flexibility and low market push performance, the present invention adopts a multi-process shared memory communication method to realize financial market subscription. By transforming a single process into a multi-process architecture, the business process is separated from the market process, so that the market processes of different markets can be upgraded independently, and the communication between the business process and the market process is realized through shared memory. The architecture design of the multi-process shared memory of the present invention is as follows: Figure 1 shown.
[0039] Shared memory consists of two parts: message queue and buffer. Shared memory message queue is divided into two types: request message queue and reply message queue. The request message queue is used to receive market subscription requests, and the reply message queue is used to push market update notifications. The buffer is used to store market data obtained from the market source, such as stock information data from the exchange.
[0040] (1) Request message queue
[0041] The request message queue is created by the market process in the shared memory according to the pre-specified queue name and is unique. The business process accesses the request message queue through the pre-specified queue name. Each market process maintains an independent request message queue and cross-access is prohibited. There are several market processes and multiple business processes can access the same request message queue.
[0042] (2) Reply message queue
[0043] The reply message queue is created by the business process. The queue name can be pre-specified or randomly generated and is unique. Each business process creates and maintains an independent reply message queue. There are several reply message queues and they correspond one to one with financial market information. A reply message queue is only used to receive push notifications for one type of financial market information data. The reply message queue is only accessible to one market information process. The same market information process can access one or more reply message queues.
[0044] (3) Market Cache Area
[0045] The market price buffer is created by the market price process in the shared memory. Multiple business processes can access the same market price buffer. Each market price process maintains an independent market price buffer. Cross-access is prohibited. There are several market price buffers.
[0046] The request message queue is multiple write and one read, that is, the request message queue allows one or more business processes to write subscription requests, but only allows one market process to read subscription requests; the reply message queue is one write and one read, that is, the reply message queue only allows one market process to write market update notifications and only allows one business process to read market update notifications.
[0047] For example, Figure 1 As shown, business process 1 creates a reply message queue 1 in the shared memory, business process 2 creates a reply message queue 2 in the shared memory, and market process 1 creates a request message queue 1 and market buffer 1 in the shared memory. Market process 1 is used to obtain market data of trading market 1. Request message queue 1 allows business processes 1 and 2 to write their respective subscription requests, but only allows market process 1 to read subscription requests; reply message queue 1 only allows market process 1 to write market update notifications and only allows business process 1 to read market update notifications; market buffer 1 allows business processes 1 and 2 to read their subscribed market data.
[0048] The multi-process shared memory management method of the present invention provides a standardized method for inter-process shared memory data exchange, which is used to realize secure message queue communication in shared memory. According to the business scenario of the market, the present invention divides the communication data in the shared memory into two parts: a message queue and a market buffer area for management. The message queue is used for message subscription and publication, and the market buffer area is used to store real-time data of the market. Through the separation of data storage, in the case of a failure in the message queue, the data update in the market buffer area is not affected. The message queue is divided into a request message queue and a reply message queue, which are managed by the subscriber. The request message queue is managed by the market process, and the number of market processes under a single trading market is 1; the reply message queue is managed by the business process, and the number of reply queues under a single business process is 1, which avoids group failures of multiple business processes caused by a single process failure. The request message queue is multi-write and one-read, and the reply message queue is one-write and one-read. The market buffer area stores market data of fixed length and structure. By constructing multiple shared memory queues or buffer areas with different purposes, the routing problem of market subscription and push in the case of multiple subscription processes and multiple market processes is solved, and the market data is ensured to be accurately delivered to the target.
[0049] See also Figure 2 The present invention provides a financial market subscription method based on the multi-process shared memory communication in the above embodiment. An embodiment of the financial market subscription method based on the shared memory message queue of the present invention includes:
[0050] 101. The business process writes a subscription request for financial market data to the request message queue of the market process in the shared memory;
[0051] 102. The business process monitors the response message queue in the shared memory to see whether it receives the market update notification written by the market process based on the subscription request;
[0052] 103. When receiving the market update notification, the business process searches the market code mapping table of the corresponding market cache area according to the market code in the market update notification to obtain the address offset of the market information corresponding to the market code in the market cache area;
[0053] 104. The business process reads the subscribed market data in the market information from the corresponding market buffer area in the shared memory based on the address offset.
[0054] In this embodiment, the business process is used to actively initiate a subscription request for market data; the market process is connected to a market data source (such as major exchanges) to obtain the latest market data from the market data source and write it into the shared memory buffer area, and is also used to push market update notifications to the business process.
[0055] The business end initiates a subscription request for financial market data through the business process. Specifically, the business process accesses the request message queue of the market process in the shared memory with a pre-specified queue name, and writes a subscription request in the request message queue. The subscription request includes subscription information, such as a stock code or stock name, and also includes reply message queue information, such as the queue name of the reply message queue created by the business process. After the subscription request is published, the business process monitors the reply message queue in real time to see if it receives a market update notification, which is written by the market process to the reply message queue of the business process.
[0056] When the business process receives a market update notification, the business process searches and reads the subscribed market data, such as the trading data of each stock, in the corresponding market buffer area of the market process in the shared memory according to the market code (such as the stock name or code) in the market update notification. The market process pre-writes the market data in the corresponding market buffer area according to the market code mapping relationship table, and then the business process obtains the address offset of the market information corresponding to the market code in the market buffer area according to the market code mapping relationship table, and then reads the subscribed market data in the market information from the corresponding market buffer area.
[0057] Based on the multi-process shared memory architecture in the above embodiment, this embodiment provides a standardized method for market subscription, publishing, and reading. Through different shared memory data structures, subscription, publishing, and reading are separated. This method can meet the needs of different business scenarios and avoid duplication.
[0058] See also Figure 3 The present invention provides a financial market subscription method based on multi-process shared memory communication in the above embodiment. Another embodiment of the financial market subscription method based on shared memory message queue of the present invention includes:
[0059] 201. The market process monitors whether the request message queue in the shared memory receives the subscription request written by the business process;
[0060] 202. When the market process receives the subscription request, the subscription information and the response message queue information in the subscription request are written into the subscription list;
[0061] 203. The market process receives the market data provided by the corresponding market data source, parses the data, and writes the data into the corresponding market buffer area in the shared memory;
[0062] 204. The market process traverses the subscription list. If there is subscription information matching the newly written market data in the subscription list, a corresponding market update notification is generated and written into a response message queue of the corresponding business process.
[0063] The execution order between steps 201 and 202 and steps 203 and 204 is not limited and can be set according to actual needs.
[0064] In this embodiment, the market process monitors in real time whether the request message queue created by itself receives a subscription request for market data, which may come from one business process or multiple business processes. When the market process receives a subscription request, it writes the subscription information (such as the stock code of one or more stocks) and the response message queue information (such as the queue name or queue ID) in each subscription request into the subscription list.
[0065] The market data source is the market data server of the stock exchange. The market process can obtain the market data of the stock exchange from the market data server. For example, market process 1 obtains the stock market data of the Shanghai Stock Exchange Main Board from the Shanghai Stock Exchange, and market process 2 obtains the stock market data of the ChiNext from the Shenzhen Stock Exchange. The obtained market data is then parsed and written into the market buffer area created by itself.
[0066] See also Figure 4 The present invention provides a financial market subscription method based on multi-process shared memory communication in the above embodiment. Another embodiment of the financial market subscription method based on shared memory message queue of the present invention includes:
[0067] 301. The market process creates a request message queue in the shared memory according to the pre-specified queue name;
[0068] 302. Market process listening request message queue;
[0069] 303. The business process creates a reply message queue. The queue name can be pre-specified or randomly generated and is unique.
[0070] 304. The business process initiates a market subscription and writes the subscription data and the reply message queue name into the request message queue;
[0071] 305. The market process receives the subscription request through the request message queue, and writes the subscription data and the reply message queue name into the subscription list;
[0072] 306. The market process receives market data through the exchange market source and writes it into the market buffer area;
[0073] 307. The market process traverses the subscription list and writes the market update notification into the specified reply message queue;
[0074] 308. The business process receives the update notification by listening to the reply message queue;
[0075] 309. The business process accesses the market data buffer area to read the market data according to the update notification. The business process may directly access the market data buffer area to read the market data without relying on the above steps.
[0076] The present invention performs data communication through inter-process shared memory and provides a standardized method for market subscription, publishing and reading. The shared memory includes two parts: a message queue and a cache area. Through different shared memory data structures, the separation of subscription, publishing and reading is achieved, which can not only meet the needs of different business scenarios, but also avoid repeated construction of the system.
[0077] The present invention combines a financial market subscription method based on multi-process shared memory communication, transforms a single process into a multi-process architecture, separates the business process from the market process, and enables the market processes of different markets to be upgraded independently. The communication between the business process and the market process is achieved through shared memory. At the same time, the market process can be reused in other market calculation scenarios without the need to develop from scratch. Only the business process can be developed to realize new business and support the market subscription service of a single market.
[0078] The message queue part of the shared memory data communication in the present invention provides a method for writing and reading a shared memory message queue, and supports data subscription requests and update notifications through request message queues and reply message queues. The request message queue is multi-write-one-read, and the reply message queue is one-write-one-read, realizing a one-to-many subscription mode. The shared memory message queue adopts multi-write-one-read to simultaneously meet the read and write requirements of the two queues.
[0079] like Figure 5 As shown, the shared memory message queue in the present invention adopts a circular queue and realizes shared memory communication by a multiple write and one read method. The shared memory message queue includes two parts: a header and a data area. The header defines the data block memory size, the number of data blocks, a read index, a write index, and a timed mutex lock; the data area is a data block array of a fixed memory size, and the data area memory size = data block memory size * number of data blocks. Each data block includes three parts: data block status, data length, and data, wherein the data block status is used to identify whether the data in the data block is empty. When initializing the shared memory message queue, the data block memory size and data block number parameters of the data area are specified by the process that creates the queue, and are written into the data block memory size and data block number of the header for storage. When multiple processes access or the process is restarted, the data block memory size and data block number parameters can be restored from the shared memory.
[0080] When multiple processes (such as business processes or market processes) write messages (such as subscription requests for financial market data or market update notifications) to a shared memory message queue, it is necessary to avoid conflicts when multiple processes access the same block of memory, so it is necessary to lock the currently accessed data block; in addition, since a single process may experience normal operation or unexpected crashes, it is necessary to avoid the inability to release the mutex lock due to the restart of a single process, which in turn causes multiple processes to deadlock. To avoid the above situation, the present invention introduces a timed mutex lock, which forces the release of the mutex lock when the mutex lock cannot be released and exceeds the validity period, thereby ensuring the continuity of the locking operation of multiple processes.
[0081] The timed mutex lock allocates an integer type memory area in the shared memory to store the timestamp. The initial timestamp value in the memory is 0. When locking, the current timestamp is obtained and the timestamp is calculated by CAS (Compare And Swap (compare and swap) atomic operation writes the current timestamp to the shared memory, and the expected value specified in the write parameter is 0 (the expected value is the current value stored in the memory expected by the CAS caller). If the timestamp is written successfully, it means that the current process has obtained the ownership of the mutex lock; after locking, according to the principle of CAS atomic operation, the expected value and the current value will be compared when writing data. If the values are different, the write fails. Since the current value is not 0, the process that has not obtained the lock will continue to try to lock and write a new timestamp, and the process enters the waiting state; when unlocking, the process that has obtained the lock resets the timestamp to 0 through the CAS atomic operation, indicating that the ownership of the mutex lock has been released. The process in the waiting state can compete for the ownership of the mutex lock by performing the locking operation; if the process that has obtained the ownership of the mutex lock is closed or restarted before releasing it, the ownership cannot be retained, and the mutex lock cannot be released. The process in the waiting state will check the validity period of the mutex lock according to the timestamp. If it expires, the timestamp will be reset to 0, and the mutex lock state will be automatically restored.
[0082] When multiple processes write messages, they seize the timed mutex. After the process obtains ownership, it first determines whether the data is full. If the data is full, it enters the waiting state. If the data is not full, it calculates the memory address of the writable data block through the write index and writes the data to the data block. The data block status is set to 1 (data is not empty). Finally, the write index is updated to the new index address and the timed mutex is released.
[0083] When a single process reads a message, it continuously monitors the memory changes of the write index. When the write index changes, it first determines whether the data is empty. If the data is empty, it enters the listening wait state. If the data is not empty, it calculates the memory address of the readable data block through the read index. After reading the data, the data block status is set to 0 (data is empty), and finally the read index is updated to the new index address.
[0084] According to the principle of circular queue, the condition for judging whether the queue is empty is: write index = read index; the condition for judging whether the queue is full is: (write index + 1) = read index. When calculating a new index, since the number of data blocks specifies the maximum length of the queue, to avoid crossing the boundary, when the index >= number of data blocks, reset the index to 0. When the read index = write index, it means that the data in the data block is empty and new data can be written; when the read index < write index, it means that the data in the data block is not empty and data can be read or written; when the read index - 1 > write index, it means that the data in the data block is not empty and data can be read or written.
[0085] The cache area part of the shared memory data communication in the present invention, in order to reduce the delay of multi-process communication in the above embodiment and improve the push speed of real-time market information, the present invention also provides a lock-free queue management method based on time difference to realize the fast reading and writing of market data in shared memory.
[0086] like Figure 6 As shown, the business process sends a market subscription request through the request message queue and receives a market update notification through the reply message queue; the market process receives a market subscription request through the request message queue and sends a market update notification through the reply message queue; the market process writes market data in the corresponding market buffer area according to the market code mapping relationship table, and the business process reads market data from the corresponding market buffer area according to the market code mapping relationship table.
[0087] The market code mapping relationship table includes the mapping relationship between the market code and the market buffer address offset. The market information is stored in the market buffer in rows. The storage area allocated to each row has the same length. By traversing the market buffer, the market code mapping relationship table corresponding to the market buffer can be rebuilt. The market code mapping relationship table is generally stored in the form of HashMap, and the time complexity is O(1). When the business process receives the market update notification from the reply message queue, it can locate and read the subscribed market data in the market buffer in the shortest time by combining HashMap and address offset for search.
[0088] In the market buffer area, each row of the storage area stores market information including market code, index and market data. The market buffer area uses queues as cache to store market data. Each row of cache has multiple and uses a circular queue structure. The circular queue structure contains at least three caches (such as Figure 6 Cache 1, cache 2 and cache 3) shown in , the length and structure of each cache are the same, and the index is the read index and write index of the ring queue.
[0089] In the process of reading and writing queues, mutex locks are usually used to ensure data synchronization. In scenarios requiring high concurrency and low latency, lock-free queues are recommended. Lock-free queues use CAS atomic operations, which are essentially lock-based operations. In high concurrency and low latency scenarios, locks are unavoidable. For access to a single piece of market data, multiple reads and one write are generally required. In order to ensure the consistency of reading and writing, both reading and writing need to be locked. As the concurrency capability increases, the impact of the lock duration on performance is particularly important.
[0090] Based on this problem, the present invention proposes a lock-free queue management method based on time difference, which introduces the data structure of the ring queue into the data storage structure and fully considers the characteristics of financial market conditions. There is a certain time difference between the push times of two market snapshots. Within a specific time range, multiple processes or threads read and write multiple memory areas to different memory areas. It is safe to read and write different memory areas separately. The space-for-time method is adopted, and there is no need to lock, which reduces the CAS atomic operations in the entire market reading process.
[0091] The present invention is based on the time difference lock-free queue management method, using a circular queue to divide the memory area into multiple caches, each cache length and structure are the same, the index includes a read index and a write index, which are used to identify the current readable cache and writable cache, and the read index and the write index are written by the market process single thread, and the execution order is: write market data, change write index, change read index, write index modification takes precedence over read index, and ensures that the read and write index values are different from each other, which can effectively avoid read and write conflicts. At the same time, considering the scheduling cycle of the CPU time slice, the present invention adopts the method of exchanging space for time, adding at least one cache as a scheduling cache area, avoiding the reading and writing of multiple processes or threads to the same memory area due to inconsistent index values caused by the CPU time slice switching in the read and write phase, and also avoiding the conflict of read and write index values. The length of the circular queue determines the size of the shared memory to be allocated, and it is necessary to select a reasonable circular queue length to avoid resource waste. The present invention considers that the time difference of the market snapshot is greater than the scheduling cycle of the CPU time slice, so it is preferred to use a circular queue containing three caches, and the number of caches can also be appropriately increased according to the changes in the business scenario.
[0092] After locating the subscribed market data location through the market code mapping relationship table, the real-time cache of the market is read through the lock-free queue management method based on time difference. The present invention reduces the delay of inter-process communication, improves the push speed and real-time performance of real-time market information, and improves the reusability of market processes by making full use of the characteristics of shared memory and lock-free queues.
[0093] Figure 71 is a schematic diagram of the structure of a computer device provided in an embodiment of the present invention. The computer device 500 may have relatively large differences due to different configurations or performances, and may include one or more processors (central processing units, CPU) 510 (for example, one or more processors) and a memory 520, and one or more storage media 530 (for example, one or more mass storage devices) storing application programs 533 or data 532. Among them, the memory 520 and the storage medium 530 can be short-term storage or permanent storage. The program stored in the storage medium 530 may include one or more modules (not shown in the figure), and each module may include a series of instruction operations in the computer device 500. Furthermore, the processor 510 may be configured to communicate with the storage medium 530 to execute a series of instruction operations in the storage medium 530 on the computer device 500.
[0094] The computer device 500 may also include one or more power supplies 540, one or more wired or wireless network interfaces 550, one or more input and output interfaces 560, and / or one or more operating systems 531, such as Windows Server, Mac OS X, Unix, Linux, FreeBSD, etc. It will be appreciated by those skilled in the art that Figure 7 The illustrated computer device structure does not constitute a limitation on the computer device, and may include more or fewer components than illustrated, or combine certain components, or arrange the components differently.
[0095] The present invention also provides a computer device, which includes a memory and a processor. The memory stores computer-readable instructions. When the computer-readable instructions are executed by the processor, the processor executes the steps of the financial market subscription method based on a shared memory message queue in the above-mentioned embodiments.
[0096] The present invention also provides a computer-readable storage medium, which may be a non-volatile computer-readable storage medium or a volatile computer-readable storage medium. Instructions are stored in the computer-readable storage medium. When the instructions are executed on a computer, the computer executes the steps of the financial market subscription method based on a shared memory message queue.
[0097] Those skilled in the art can clearly understand that, for the convenience and brevity of description, the specific working processes of the systems, devices and units described above can refer to the corresponding processes in the aforementioned method embodiments and will not be repeated here.
[0098] If the integrated unit is implemented in the form of a software functional unit and sold or used as an independent product, it can be stored in a computer-readable storage medium. Based on this understanding, the technical solution of the present invention is essentially or the part that contributes to the prior art or all or part of the technical solution can be embodied in the form of a software product. The computer software product is stored in a storage medium, including a number of instructions to enable a computer device (which can be a personal computer, a server, or a network device, etc.) to perform all or part of the steps of the method described in each embodiment of the present invention. The aforementioned storage medium includes: U disk, mobile hard disk, read-only memory (ROM), random access memory (RAM), disk or optical disk and other media that can store program codes.
[0099] As described above, the above embodiments are only used to illustrate the technical solutions of the present invention, rather than to limit the same. Although the present invention has been described in detail with reference to the aforementioned embodiments, those skilled in the art should understand that the technical solutions described in the aforementioned embodiments may still be modified, or some of the technical features thereof may be replaced by equivalents. However, these modifications or replacements do not deviate the essence of the corresponding technical solutions from the spirit and scope of the technical solutions of the embodiments of the present invention.
Claims
1. A financial quote subscription method based on a shared memory message queue, characterized in that: The shared memory message queue includes two types: request message queue and response message queue. The financial market subscription method includes: The business process writes a subscription request for financial market data to the request message queue of the market process in the shared memory; The business process monitors the response message queue in the shared memory to see whether it receives the market update notification written by the market process based on the subscription request; When receiving the market update notification, the business process searches the market code mapping table of the corresponding market cache area according to the market code in the market update notification to obtain the address offset of the market information corresponding to the market code in the market cache area; Based on the address offset, the business process reads the subscribed market data in the market information corresponding to the market cache area in the shared memory. The market cache area stores the market information in rows, and the storage area allocated to each row has the same length. The market information stored in each row storage area includes a market code, an index and market data. There are multiple caches for storing market data and a circular queue structure is adopted. The length and structure of each cache are the same, and the index is the read index and write index of the circular queue of the cache.
2. The financial quote subscription method according to claim 1, characterized in that: The financial market subscription method further includes: The market process monitors the request message queue in the shared memory to see whether it receives the subscription request written by the business process; When the market process receives the subscription request, the subscription information and the response message queue information in the subscription request are written into the subscription list.
3. The financial quote subscription method according to claim 2, characterized in that: The financial market subscription method further includes: The market process receives the market data provided by the corresponding market data source, parses it, and writes it into the corresponding market buffer area in the shared memory; The market process traverses the subscription list, and if there is subscription information matching the newly written market data in the subscription list, a corresponding market update notification is generated and written into the response message queue of the corresponding business process.
4. The financial quotes subscription method according to claim 1, characterized in that: The financial market subscription method further includes: Before initiating a subscription request, each business process creates its own reply message queue in the shared memory. Different business processes subscribe to the same or different financial quotes, and the reply message queue is only accessible to one quote process.
5. The financial quotes subscription method according to claim 1, characterized in that: The financial market subscription method further includes: Before receiving a subscription request, each market process creates its own request message queue and market buffer in the shared memory. Different market processes are prohibited from cross-accessing the request message queue and market buffer of other market processes. The same request message queue and market buffer are allowed to be accessed by different business processes.
6. The financial quote subscription method according to claim 1, characterized in that: The shared memory message queue includes a header and a data area, wherein the header is used to store the data block memory size, the number of data blocks, the read index, the write index and the timed mutex lock, and the data area is a data block array of fixed memory size, wherein the data block array includes a plurality of data blocks, each data block includes a data block state, a data length and data, and the data block state is used to identify whether the data in the corresponding data block is empty, and each of the shared memory message queues adopts a circular queue structure; When the write index is equal to the read index, the circular queue of the shared memory message queue is in an empty data state; when the write index plus 1 is equal to the read index, the circular queue of the shared memory message queue is in a full data state; when calculating a new read index or write index, if the read index or write index is greater than or equal to the number of data blocks, the read index or write index is reset to 0; when writing data to any data block in the shared memory message queue, the data block to be written is locked by the timing mutex lock.
7. The financial quote subscription method according to claim 1, characterized in that: The cache ring queue includes at least three caches, the read index of the ring queue is used to identify the readable cache in the current ring queue, and the write index of the ring queue is used to identify the writable cache in the current ring queue; The read index and write index of the circular queue are written into the market data buffer by the market data process single thread, and the execution order is: write market data, change write index, and change read index.
8. A computer device, characterized in that: The computer device comprises: a memory and at least one processor, wherein instructions are stored in the memory; The at least one processor calls the instruction in the memory so that the computer device executes the financial market subscription method based on the shared memory message queue as described in any one of claims 1 to 7.
9. A computer-readable storage medium having instructions stored thereon, characterized in that: When the instructions are executed by the processor, a financial market subscription method based on a shared memory message queue as described in any one of claims 1 to 7 is implemented.
