CN118819899A - A data processing method, medium and electronic device for financial transaction scenarios - Google Patents
A data processing method, medium and electronic device for financial transaction scenarios Download PDFInfo
- Publication number
- CN118819899A CN118819899A CN202410915504.XA CN202410915504A CN118819899A CN 118819899 A CN118819899 A CN 118819899A CN 202410915504 A CN202410915504 A CN 202410915504A CN 118819899 A CN118819899 A CN 118819899A
- Authority
- CN
- China
- Prior art keywords
- thread
- ring buffer
- threadlocal
- transaction
- data
- 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
Links
Classifications
-
- G—PHYSICS
- G06—COMPUTING OR CALCULATING; COUNTING
- G06F—ELECTRIC DIGITAL DATA PROCESSING
- G06F9/00—Arrangements for program control, e.g. control units
- G06F9/06—Arrangements for program control, e.g. control units using stored programs, i.e. using an internal store of processing equipment to receive or retain programs
- G06F9/46—Multiprogramming arrangements
- G06F9/54—Interprogram communication
- G06F9/544—Buffers; Shared memory; Pipes
-
- G—PHYSICS
- G06—COMPUTING OR CALCULATING; COUNTING
- G06F—ELECTRIC DIGITAL DATA PROCESSING
- G06F9/00—Arrangements for program control, e.g. control units
- G06F9/06—Arrangements for program control, e.g. control units using stored programs, i.e. using an internal store of processing equipment to receive or retain programs
- G06F9/46—Multiprogramming arrangements
- G06F9/50—Allocation of resources, e.g. of the central processing unit [CPU]
- G06F9/5005—Allocation of resources, e.g. of the central processing unit [CPU] to service a request
- G06F9/5011—Allocation of resources, e.g. of the central processing unit [CPU] to service a request the resources being hardware resources other than CPUs, Servers and Terminals
- G06F9/5016—Allocation of resources, e.g. of the central processing unit [CPU] to service a request the resources being hardware resources other than CPUs, Servers and Terminals the resource being the memory
-
- G—PHYSICS
- G06—COMPUTING OR CALCULATING; COUNTING
- G06F—ELECTRIC DIGITAL DATA PROCESSING
- G06F9/00—Arrangements for program control, e.g. control units
- G06F9/06—Arrangements for program control, e.g. control units using stored programs, i.e. using an internal store of processing equipment to receive or retain programs
- G06F9/46—Multiprogramming arrangements
- G06F9/52—Program synchronisation; Mutual exclusion, e.g. by means of semaphores
-
- G—PHYSICS
- G06—COMPUTING OR CALCULATING; COUNTING
- G06F—ELECTRIC DIGITAL DATA PROCESSING
- G06F9/00—Arrangements for program control, e.g. control units
- G06F9/06—Arrangements for program control, e.g. control units using stored programs, i.e. using an internal store of processing equipment to receive or retain programs
- G06F9/46—Multiprogramming arrangements
- G06F9/54—Interprogram communication
- G06F9/542—Event management; Broadcasting; Multicasting; Notifications
-
- G—PHYSICS
- G06—COMPUTING OR CALCULATING; COUNTING
- G06Q—INFORMATION AND COMMUNICATION TECHNOLOGY [ICT] SPECIALLY ADAPTED FOR ADMINISTRATIVE, COMMERCIAL, FINANCIAL, MANAGERIAL OR SUPERVISORY PURPOSES; SYSTEMS OR METHODS SPECIALLY ADAPTED FOR ADMINISTRATIVE, COMMERCIAL, FINANCIAL, MANAGERIAL OR SUPERVISORY PURPOSES, NOT OTHERWISE PROVIDED FOR
- G06Q40/00—Finance; Insurance; Tax strategies; Processing of corporate or income taxes
- G06Q40/04—Trading; Exchange, e.g. stocks, commodities, derivatives or currency exchange
Landscapes
- Engineering & Computer Science (AREA)
- Theoretical Computer Science (AREA)
- Software Systems (AREA)
- Physics & Mathematics (AREA)
- General Physics & Mathematics (AREA)
- General Engineering & Computer Science (AREA)
- Business, Economics & Management (AREA)
- Accounting & Taxation (AREA)
- Finance (AREA)
- Development Economics (AREA)
- General Business, Economics & Management (AREA)
- Technology Law (AREA)
- Strategic Management (AREA)
- Marketing (AREA)
- Economics (AREA)
- Multimedia (AREA)
- Information Retrieval, Db Structures And Fs Structures Therefor (AREA)
Abstract
本发明公开了一种用于金融交易场景的数据处理方法、介质及电子设备,所述方法包括:初始化环形缓冲区;使用ThreadLocal进行线程本地存储;在生产者线程中,通过ThreadLocal获取线程本地事件对象;获取环形缓冲区下一个可用的序列号;将线程本地事件对象中的数据写入环形缓冲区中的事件对象;通过环形缓冲区发布事件,通知消费者有新的事件可处理;消费者线程从环形缓冲区中获取事件;使用线程本地的处理状态对象处理从环形缓冲区中获取交易数据;在处理交易数据后,更新线程本地的处理状态对象。本发明提供了一种高效、低延迟、线程安全的金融交易系统数据处理方法,满足了金融系统高并发、高吞吐量、低延迟的严格要求。
The present invention discloses a data processing method, medium and electronic device for financial transaction scenarios, the method comprising: initializing a ring buffer; using ThreadLocal for thread local storage; in a producer thread, obtaining a thread local event object through ThreadLocal; obtaining the next available sequence number of the ring buffer; writing the data in the thread local event object into the event object in the ring buffer; publishing events through the ring buffer to notify consumers that there are new events to be processed; the consumer thread obtains events from the ring buffer; using a thread local processing status object to process transaction data obtained from the ring buffer; after processing the transaction data, updating the thread local processing status object. The present invention provides an efficient, low-latency, thread-safe financial transaction system data processing method, which meets the strict requirements of high concurrency, high throughput and low latency of the financial system.
Description
技术领域Technical Field
本发明涉及金融交易领域,具体是一种用于金融交易场景的数据处理方法、介质及电子设备。The present invention relates to the field of financial transactions, and in particular to a data processing method, a medium and an electronic device for use in financial transaction scenarios.
背景技术Background Art
在高并发数据处理场景中,如在线交易系统和实时数据分析,系统需要快速、高效地处理大量并发请求。目前在高频数据处理场景中,常见的实现方案有使用传统的阻塞队列(如ArrayBlockingQueue或LinkedBlockingQueue)结合线程池来进行数据生产和消费。In high-concurrency data processing scenarios, such as online trading systems and real-time data analysis, the system needs to process a large number of concurrent requests quickly and efficiently. Currently, in high-frequency data processing scenarios, the common implementation scheme is to use traditional blocking queues (such as ArrayBlockingQueue or LinkedBlockingQueue) combined with thread pools to produce and consume data.
传统的队列和锁机制往往难以满足高性能要求,容易导致线程竞争和性能瓶颈。RingBuffer作为一种高效的缓冲区结构,可以提供高吞吐量和低延迟,而ThreadLocal可以为每个线程提供独立的变量存储,避免线程间的共享和竞争。Traditional queue and lock mechanisms often fail to meet high performance requirements and easily lead to thread contention and performance bottlenecks. RingBuffer, as an efficient buffer structure, can provide high throughput and low latency, while ThreadLocal can provide independent variable storage for each thread to avoid sharing and contention between threads.
发明内容Summary of the invention
鉴于上述问题,提出了本发明以便提供克服上述问题或者至少部分地解决上述问题的一种用于金融交易场景的数据处理方法、介质及电子设备。In view of the above problems, the present invention is proposed to provide a data processing method, medium and electronic device for financial transaction scenarios that overcome the above problems or at least partially solve the above problems.
为实现上述目的,在本申请的第一方面提供了一种用于金融交易场景的数据处理方法,所述方法包括:To achieve the above-mentioned purpose, a first aspect of the present application provides a data processing method for a financial transaction scenario, the method comprising:
初始化环形缓冲区;Initialize the ring buffer;
使用ThreadLocal进行线程本地存储;Use ThreadLocal for thread local storage;
在生产者线程中,通过ThreadLocal获取线程本地事件对象,并将交易数据写入线程本地事件对象;In the producer thread, obtain the thread local event object through ThreadLocal and write the transaction data into the thread local event object;
获取环形缓冲区下一个可用的序列号,通过获取的序列号,从环形缓冲区中获取对应的事件对象;Get the next available sequence number in the ring buffer, and use the obtained sequence number to get the corresponding event object from the ring buffer;
将线程本地事件对象中的数据写入环形缓冲区中的事件对象;Write the data in the thread local event object to the event object in the ring buffer;
通过环形缓冲区发布事件,通知消费者有新的事件可处理;Publish events through the ring buffer to notify consumers that new events are available for processing;
消费者线程从环形缓冲区中获取事件,使用消费者线程的序列号来标识需要处理的事件,并通过ThreadLocal获取消费者线程本地的处理状态对象;The consumer thread obtains events from the ring buffer, uses the serial number of the consumer thread to identify the events to be processed, and obtains the local processing status object of the consumer thread through ThreadLocal;
使用线程本地的处理状态对象处理从环形缓冲区中获取交易数据;Use the thread-local processing state object to process the transaction data from the ring buffer;
在处理交易数据后,更新线程本地的处理状态对象。After processing the transaction data, update the thread-local processing status object.
可选的,所述初始化环形缓冲区包括:Optionally, the initializing the ring buffer includes:
根据交易系统的预期并发量和数据处理需求选择缓冲区;Select the buffer based on the expected concurrency and data processing requirements of the trading system;
选择等待策略以平衡系统延迟和CPU使用率;Choose a wait strategy to balance system latency and CPU usage;
创建事件工厂,事件工厂用于创建环形缓冲区中的事件对象;Create an event factory, which is used to create event objects in the ring buffer;
初始化Disruptor框架,通过Disruptor框架创建和管理环形缓冲区;Initialize the Disruptor framework, create and manage the ring buffer through the Disruptor framework;
配置事件处理器,事件处理器用于处理从环形缓冲区中读取的事件。Configure the event handler, which is used to process events read from the ring buffer.
可选的,,所述使用ThreadLocal进行线程本地存储包括:Optionally, the use of ThreadLocal for thread local storage includes:
确定需要ThreadLocal存储的对象;Determine the objects that need ThreadLocal storage;
创建ThreadLocal变量,为每个需要线程本地存储的对象创建ThreadLocal变量;Create ThreadLocal variables for each object that requires thread local storage;
初始化ThreadLocal变量,在系统初始化过程中,为每个线程初始化ThreadLocal变量;Initialize ThreadLocal variables. During system initialization, initialize ThreadLocal variables for each thread.
在线程中获取ThreadLocal对象,在处理交易数据时,通过ThreadLocal变量获取线程本地存储的对象;Get the ThreadLocal object in the thread. When processing transaction data, get the object stored locally in the thread through the ThreadLocal variable.
在生产者线程中,将交易数据写入ThreadLocal存储的交易事件对象;In the producer thread, write the transaction data into the transaction event object stored in ThreadLocal;
在消费者线程中,通过ThreadLocal变量获取处理状态对象。In the consumer thread, the processing status object is obtained through the ThreadLocal variable.
可选的,所述存储的对象包括交易事件对象和处理状态对象,交易事件对象包括交易ID和交易价格,处理状态对象包括交易处理的中间状态和交易处理的结果。Optionally, the stored objects include transaction event objects and processing status objects, the transaction event objects include transaction ID and transaction price, and the processing status objects include intermediate states of transaction processing and results of transaction processing.
可选的,在处理交易数据后,还会根据处理结果来执行日志记录操作或者通知其他系统。Optionally, after the transaction data is processed, logging operations may be performed or other systems may be notified based on the processing results.
可选的,所述环形缓冲区通过Compare-And-Swap机制实现无锁的序列号获取和事件发布。Optionally, the ring buffer implements lock-free sequence number acquisition and event publishing through a Compare-And-Swap mechanism.
可选的,环形缓冲区支持多生产者和多消费者模式,通过同步机制,多个生产者可以同时发布事件,多个消费者可以同时处理事件。Optionally, the ring buffer supports multi-producer and multi-consumer mode. Through the synchronization mechanism, multiple producers can publish events at the same time, and multiple consumers can process events at the same time.
可选的,通过无锁设计和预分配内存,事件对象在环形缓冲区中预先分配,以确保在高并发环境下数据的一致性和处理的准确性。Optionally, through lock-free design and pre-allocated memory, event objects are pre-allocated in a ring buffer to ensure data consistency and processing accuracy in a high-concurrency environment.
在本申请的第二方面提供了一种计算机可读存储介质,所述计算机可读存储介质中存储有计算机程序,所述计算机程序被处理器加载并执行时,采用了第一方面中任一项所述的方法。In a second aspect of the present application, a computer-readable storage medium is provided, in which a computer program is stored. When the computer program is loaded and executed by a processor, the method described in any one of the first aspects is adopted.
在本申请的第三方面提供了一种电子设备,包括存储器、处理器及存储在存储器中并能够在处理器上运行的计算机程序,所述处理器加载并执行计算机程序时,采用了第一方面中任一项所述的方法。In a third aspect of the present application, an electronic device is provided, comprising a memory, a processor, and a computer program stored in the memory and capable of running on the processor, wherein when the processor loads and executes the computer program, the method described in any one of the first aspects is adopted.
综上所述,由于采用了上述技术方案,本发明的有益效果是:In summary, due to the adoption of the above technical solution, the beneficial effects of the present invention are:
本发明结合使用环形缓冲区和ThreadLocal,提供了一种高效、低延迟、线程安全的金融交易系统数据处理方法,显著提高了处理性能,满足了金融系统高并发、高吞吐量、低延迟的严格要求,具有广泛的应用前景和市场价值。The present invention combines the use of a ring buffer and ThreadLocal to provide an efficient, low-latency, thread-safe financial transaction system data processing method, which significantly improves the processing performance, meets the strict requirements of high concurrency, high throughput, and low latency of the financial system, and has broad application prospects and market value.
附图说明BRIEF DESCRIPTION OF THE DRAWINGS
图1是本申请实施例提供的一种用于金融交易场景的数据处理方法的流程示意图。FIG1 is a flow chart of a data processing method for a financial transaction scenario provided in an embodiment of the present application.
具体实施方式DETAILED DESCRIPTION
为使本发明实施例的目的、技术方案和优点更加清楚,下面将结合本发明中的附图,对本发明实施例中的技术方案进行清楚、完整地描述,显然,所描述的实施例是本发明一部分实施例,而不是全部的实施例,基于本发明中的实施例,本领域普通技术人员在没有作出创造性劳动前提下所获得的所有其他实施例,都属于本发明保护的范围。In order to make the purpose, technical solutions and advantages of the embodiments of the present invention clearer, the technical solutions in the embodiments of the present invention will be clearly and completely described below in conjunction with the drawings in the present invention. Obviously, the described embodiments are part of the embodiments of the present invention, rather than all the embodiments. Based on the embodiments in the present invention, all other embodiments obtained by ordinary technicians in this field without making creative work are within the scope of protection of the present invention.
请参阅图1,本实施例提供一种用于金融交易场景的数据处理方法,包括:Referring to FIG. 1 , this embodiment provides a data processing method for a financial transaction scenario, including:
S1、初始化环形缓冲区。S1. Initialize the ring buffer.
在金融交易场景中,初始化环形缓冲区(下文用RingBuffer替代)是关键步骤之一,确保系统能够高效处理高并发交易请求。详细步骤如下:In financial transaction scenarios, initializing the ring buffer (hereinafter referred to as RingBuffer) is one of the key steps to ensure that the system can efficiently handle high-concurrency transaction requests. The detailed steps are as follows:
S101、确定缓冲区大小。S101. Determine the buffer size.
缓冲区大小是RingBuffer的核心参数,直接影响系统的吞吐量和性能。根据交易系统的预期并发量和数据处理需求,选择合适的缓冲区大小。例如,设置缓冲区大小为1024或2048,以确保在高并发环境下有足够的缓冲空间。The buffer size is the core parameter of RingBuffer, which directly affects the throughput and performance of the system. Choose an appropriate buffer size based on the expected concurrency and data processing requirements of the trading system. For example, set the buffer size to 1024 or 2048 to ensure that there is enough buffer space in a high-concurrency environment.
S102、选择等待策略。S102: Select a waiting strategy.
等待策略决定了消费者在等待生产者发布新数据时的行为。常见的等待策略包括:The waiting strategy determines the behavior of the consumer when waiting for the producer to publish new data. Common waiting strategies include:
BlockingWaitStrategy:使用锁和条件变量,适用于对延迟要求不高的场景。BlockingWaitStrategy: Uses locks and condition variables, suitable for scenarios where latency requirements are not high.
YieldingWaitStrategy:消费者线程会不断让出CPU,适用于低延迟和高吞吐量的场景。YieldingWaitStrategy: The consumer thread will continue to give up the CPU, which is suitable for low latency and high throughput scenarios.
BusySpinWaitStrategy:消费者线程忙等待,适用于极低延迟的场景,但会消耗更多CPU资源。BusySpinWaitStrategy: The consumer thread is busy waiting, which is suitable for extremely low latency scenarios, but consumes more CPU resources.
选择合适的等待策略以平衡系统延迟和CPU使用率。Choose an appropriate wait strategy to balance system latency and CPU usage.
S103、创建事件工厂。S103. Create an event factory.
事件工厂用于创建RingBuffer中的事件对象。金融交易场景中的事件对象通常包含交易ID、价格等信息。The event factory is used to create event objects in RingBuffer. Event objects in financial transaction scenarios usually contain information such as transaction ID and price.
S104、初始化Disruptor框架。S104. Initialize the Disruptor framework.
Disruptor框架是实现RingBuffer的核心工具。通过Disruptor框架,可以方便地创建和管理RingBuffer,以及生产者和消费者线程。The Disruptor framework is the core tool for implementing RingBuffer. Through the Disruptor framework, you can easily create and manage RingBuffer, as well as producer and consumer threads.
S105、配置事件处理器。S105. Configure event handler.
事件处理器用于处理从RingBuffer中读取的事件。根据交易系统的需求,可以配置单个或多个事件处理器来处理交易数据。The event processor is used to process events read from the RingBuffer. Depending on the needs of the trading system, a single or multiple event processors can be configured to process trading data.
S2、使用ThreadLocal进行线程本地存储。S2. Use ThreadLocal for thread local storage.
在金融交易场景中,为了避免线程间的数据共享和竞争,我们使用ThreadLocal为每个线程提供独立的变量存储。详细步骤如下:In financial trading scenarios, in order to avoid data sharing and competition between threads, we use ThreadLocal to provide independent variable storage for each thread. The detailed steps are as follows:
S201、确定需要ThreadLocal存储的对象。S201. Determine the objects that need to be stored in ThreadLocal.
在金融交易系统中,确定哪些数据或对象需要在线程本地存储,以避免线程间的数据共享和竞争。通常,这些对象包括:In financial trading systems, determine which data or objects need to be stored locally in the thread to avoid data sharing and contention between threads. Typically, these objects include:
交易事件对象(TradeEvent):包括交易ID、交易价格等信息。Transaction event object (TradeEvent): includes transaction ID, transaction price and other information.
处理状态对象(ProcessingState):包括交易处理的中间状态和交易处理的结果等。Processing State Object: includes the intermediate state of transaction processing and the result of transaction processing.
S202、创建ThreadLocal变量。S202. Create a ThreadLocal variable.
为每个需要线程本地存储的对象创建ThreadLocal变量。这些变量在每个线程中独立存在,避免了线程间的数据竞争。Create a ThreadLocal variable for each object that requires thread local storage. These variables exist independently in each thread, avoiding data competition between threads.
S203、初始化ThreadLocal变量。S203. Initialize ThreadLocal variables.
在系统初始化过程中,为每个线程初始化ThreadLocal变量。使用ThreadLocal的withInitial方法,为每个线程分配独立的对象实例。During system initialization, initialize the ThreadLocal variable for each thread. Use the withInitial method of ThreadLocal to allocate a separate object instance for each thread.
S204、在线程中获取ThreadLocal对象。S204. Get the ThreadLocal object in the thread.
在处理交易数据时,通过ThreadLocal变量获取线程本地存储的对象。这些对象仅在线程内部使用,避免了线程间的数据共享。When processing transaction data, objects stored locally in the thread are obtained through ThreadLocal variables. These objects are only used within the thread, avoiding data sharing between threads.
S205、将数据写入ThreadLocal对象。S205. Write the data into the ThreadLocal object.
在生产者线程中,将交易数据写入ThreadLocal存储的交易事件对象。这样每个生产者线程都能独立处理自己的数据,不会与其他线程发生冲突。In the producer thread, the transaction data is written to the transaction event object stored in ThreadLocal. In this way, each producer thread can process its own data independently without conflicting with other threads.
S206、处理数据并更新ThreadLocal状态。S206. Process data and update ThreadLocal status.
在消费者线程中,通过ThreadLocal变量获取处理状态对象。使用该对象处理从RingBuffer获取的交易数据,并在处理完成后更新处理状态。In the consumer thread, the processing status object is obtained through the ThreadLocal variable. The object is used to process the transaction data obtained from the RingBuffer and the processing status is updated after the processing is completed.
S207、释放ThreadLocal对象。S207. Release the ThreadLocal object.
在某些情况下,在线程执行完毕后,需要手动清理ThreadLocal对象,以防止内存泄漏。这可以通过调用ThreadLocal.remove()方法来实现。In some cases, after the thread has finished executing, you need to manually clean up the ThreadLocal object to prevent memory leaks. This can be done by calling the ThreadLocal.remove() method.
S3、事件生产S3, Event Production
S301、获取线程本地事件对象。S301. Obtain a thread-local event object.
在生产者线程中,通过ThreadLocal获取线程本地的事件对象。这样可以确保每个线程都有独立的事件对象,避免线程间的数据共享和竞争。In the producer thread, the thread-local event object is obtained through ThreadLocal. This ensures that each thread has an independent event object and avoids data sharing and competition between threads.
S302、写入交易数据。S302: Write transaction data.
将交易数据(如交易ID和价格)写入线程本地的事件对象。这一步操作是线程本地的,不涉及线程间的同步问题。Write the transaction data (such as transaction ID and price) to the thread-local event object. This operation is thread-local and does not involve synchronization issues between threads.
S303、获取RingBuffer的下一个序列号。S303. Get the next sequence number of RingBuffer.
使用RingBuffer的`next()`方法获取下一个可用的序列号。这是一个无锁的操作,通过CAS(Compare-And-Swap)机制保证线程安全。Use the `next()` method of RingBuffer to get the next available sequence number. This is a lock-free operation, which is thread-safe through the CAS (Compare-And-Swap) mechanism.
S304、获取RingBuffer中的事件对象。S304: Get the event object in RingBuffer.
通过获取的序列号,从RingBuffer中获取对应的事件对象。RingBuffer预先分配内存,确保获取的事件对象是已分配的,无需额外的内存分配操作。Get the corresponding event object from the RingBuffer through the obtained sequence number. RingBuffer pre-allocates memory to ensure that the obtained event object is allocated without additional memory allocation operations.
S305、将数据写入RingBuffer中的事件对象。S305: Write the data into the event object in RingBuffer.
将线程本地事件对象中的数据写入RingBuffer中的事件对象。这一步是将数据从ThreadLocal事件对象复制到RingBuffer中。Write the data in the thread local event object to the event object in the RingBuffer. This step is to copy the data from the ThreadLocal event object to the RingBuffer.
S306、发布事件。S306: Publish an event.
使用RingBuffer的publish(sequence)方法发布事件,通知消费者有新的事件可处理。这也是一个无锁操作,通过CAS机制保证线程安全。Use the publish(sequence) method of RingBuffer to publish events and notify consumers that there are new events to process. This is also a lock-free operation, and thread safety is guaranteed by the CAS mechanism.
S4、事件处理。S4. Event processing.
S401、从RingBuffer获取事件。S401. Get events from RingBuffer.
消费者线程从RingBuffer中获取事件,使用消费者线程的序列号来标识需要处理的事件。RingBuffer确保事件按照生产者发布的顺序被消费者读取。The consumer thread gets events from the RingBuffer and uses the sequence number of the consumer thread to identify the events to be processed. RingBuffer ensures that events are read by consumers in the order in which they are published by the producer.
S402、获取线程本地处理状态。S402: Obtain thread local processing status.
通过ThreadLocal获取消费者线程本地的处理状态对象(如Get the local processing state object of the consumer thread through ThreadLocal (such as
`ProcessingState`)。每个消费者线程都有独立的处理状态对象,避免了线程间的数据共享和竞争。`ProcessingState`). Each consumer thread has an independent processing state object, avoiding data sharing and competition between threads.
S403、处理交易数据。S403: Process transaction data.
使用线程本地的处理状态对象处理从RingBuffer中获取的交易数据。处理逻辑可以根据具体需求进行定制,如计算、存储、过滤等操作。Use the thread-local processing state object to process the transaction data obtained from the RingBuffer. The processing logic can be customized according to specific needs, such as calculation, storage, filtering and other operations.
S404、更新处理状态。S404: Update processing status.
在处理交易数据后,更新线程本地的处理状态对象。这确保了处理状态的独立性和线程安全性。After processing the transaction data, update the thread-local processing state object. This ensures the independence and thread safety of the processing state.
S405、执行后续操作。S405: Execute subsequent operations.
在处理交易数据后,还会根据处理结果执行一些后续操作,如日志记录、通知其他系统等。这些操作也在本地完成,确保不影响其他线程的执行。After processing the transaction data, some subsequent operations will be performed based on the processing results, such as logging, notifying other systems, etc. These operations are also completed locally to ensure that they do not affect the execution of other threads.
本实施例在多线程同步中使用CAS机制保证线程安全,RingBuffer通过CAS(Compare-And-Swap)机制实现无锁的序列号获取和事件发布,确保生产者和消费者之间的线程安全。这种机制减少了锁竞争,提高了系统性能。This embodiment uses the CAS mechanism in multi-thread synchronization to ensure thread safety. RingBuffer uses the CAS (Compare-And-Swap) mechanism to achieve lock-free sequence number acquisition and event publishing, ensuring thread safety between producers and consumers. This mechanism reduces lock contention and improves system performance.
RingBuffer的设计保证了事件的有序处理,消费者按照生产者发布的顺序处理事件,确保数据一致性和处理的准确性。The design of RingBuffer ensures the orderly processing of events. Consumers process events in the order published by producers, ensuring data consistency and processing accuracy.
RingBuffer支持多生产者和多消费者模式。通过高效的同步机制,多个生产者可以同时发布事件,多个消费者可以同时处理事件,适应高并发环境下的需求。通过无锁设计和预分配内存,RingBuffer避免了数据一致性问题。事件对象在RingBuffer中预先分配,确保在高并发环境下数据的一致性和处理的准确性。通过减少锁竞争和上下文切换,使用CAS机制和ThreadLocal存储,本发明显著优化了系统性能,提高了金融交易系统的吞吐量和响应速度。RingBuffer supports multi-producer and multi-consumer modes. Through an efficient synchronization mechanism, multiple producers can publish events at the same time, and multiple consumers can process events at the same time, adapting to the needs of high-concurrency environments. Through lock-free design and pre-allocated memory, RingBuffer avoids data consistency issues. Event objects are pre-allocated in RingBuffer to ensure data consistency and processing accuracy in a high-concurrency environment. By reducing lock contention and context switching, using CAS mechanism and ThreadLocal storage, the present invention significantly optimizes system performance and improves the throughput and response speed of financial transaction systems.
本实施例在金融交易场景中,结合使用RingBuffer和ThreadLocal,以提升单机处理性能,具有以下显著优点:In the financial transaction scenario, this embodiment combines RingBuffer and ThreadLocal to improve the single-machine processing performance, which has the following significant advantages:
高吞吐量:无锁设计:RingBuffer采用无锁设计,通过CASHigh throughput: Lock-free design: RingBuffer adopts lock-free design, through CAS
(Compare-And-Swap)机制实现数据的生产和消费,减少了锁竞争和上下文切换,提高了系统的吞吐量。预分配内存:事件对象在RingBuffer中预先分配,避免了运行时的频繁内存分配和垃圾回收,提高了系统的整体性能。The (Compare-And-Swap) mechanism realizes data production and consumption, reduces lock contention and context switching, and improves system throughput. Pre-allocated memory: Event objects are pre-allocated in the RingBuffer, avoiding frequent memory allocation and garbage collection at runtime, and improving the overall performance of the system.
低延迟:线程本地存储:使用ThreadLocal为每个线程提供独立的存储空间,避免了线程间的数据共享和竞争,显著降低了数据处理的延迟。高效等待策略:通过选择合适的等待策略(如BusySpinWaitStrategy或YieldingWaitStrategy),进一步降低了事件处理的延迟,满足了金融交易系统对低延迟的严格要求。Low latency: Thread local storage: Use ThreadLocal to provide independent storage space for each thread, avoiding data sharing and competition between threads, significantly reducing the latency of data processing. Efficient waiting strategy: By selecting an appropriate waiting strategy (such as BusySpinWaitStrategy or YieldingWaitStrategy), the latency of event processing is further reduced, meeting the strict requirements of financial trading systems for low latency.
线程安全:独立的线程本地变量:ThreadLocal变量确保每个线程有独立的事件对象和处理状态,避免了线程间的数据竞争,提高了系统的线程安全性。有序事件处理:RingBuffer确保事件按照生产者发布的顺序被消费者读取和处理,保证了数据的一致性和处理的准确性。Thread safety: Independent thread local variables: ThreadLocal variables ensure that each thread has an independent event object and processing status, avoiding data competition between threads and improving the thread safety of the system. Ordered event processing: RingBuffer ensures that events are read and processed by consumers in the order published by producers, ensuring data consistency and processing accuracy.
易扩展性:支持多生产者多消费者:RingBuffer支持多生产者和多消费者模式,通过高效的同步机制,多个生产者可以同时发布事件,多个消费者可以同时处理事件,适应不同规模的交易系统需求。Easy scalability: Support multiple producers and multiple consumers: RingBuffer supports multiple producers and multiple consumers. Through an efficient synchronization mechanism, multiple producers can publish events at the same time, and multiple consumers can process events at the same time, adapting to the needs of transaction systems of different scales.
灵活的配置:根据系统的具体需求,可以灵活配置RingBuffer的大小和等待策略,满足不同负载和性能要求。Flexible configuration: According to the specific needs of the system, the size and waiting strategy of RingBuffer can be flexibly configured to meet different load and performance requirements.
资源高效利用:减少上下文切换:无锁设计和高效的等待策略减少了线程间的上下文切换,降低了系统开销,提高了CPU资源的利用率。Efficient resource utilization: Reduce context switching: Lock-free design and efficient waiting strategy reduce context switching between threads, reduce system overhead, and improve CPU resource utilization.
优化内存使用:ThreadLocal和预分配内存机制减少了垃圾回收的频率,优化了内存使用,提高了系统的稳定性和性能。Optimize memory usage: ThreadLocal and pre-allocated memory mechanisms reduce the frequency of garbage collection, optimize memory usage, and improve system stability and performance.
适应性强:应对高并发场景:本发明特别适用于金融交易系统中的高并发数据处理场景,能够高效处理大量并发交易请求,保证系统的高可用性和可靠性。Strong adaptability: Coping with high-concurrency scenarios: The present invention is particularly suitable for high-concurrency data processing scenarios in financial transaction systems, and can efficiently process a large number of concurrent transaction requests to ensure high availability and reliability of the system.
广泛应用:除了金融交易场景,本发明的方法和系统也适用于其他需要高性能数据处理的领域,如实时数据分析、在线广告竞价、物联网数据处理等。Wide application: In addition to financial transaction scenarios, the method and system of the present invention are also applicable to other fields that require high-performance data processing, such as real-time data analysis, online advertising bidding, Internet of Things data processing, etc.
通过结合使用RingBuffer和ThreadLocal,本实施例提供了一种高效、低延迟、线程安全的金融交易系统数据处理方法。显著提高了单机处理性能,满足了高并发、高吞吐量、低延迟的严格要求,具有广泛的应用前景和市场价值。By combining RingBuffer and ThreadLocal, this embodiment provides an efficient, low-latency, and thread-safe financial transaction system data processing method. It significantly improves the single-machine processing performance, meets the strict requirements of high concurrency, high throughput, and low latency, and has broad application prospects and market value.
此外,需要说明的是:本申请实施例还提供了还提供一种计算机可读存储介质,所述计算机可读存储介质中存储有计算机程序,所述计算机程序被处理器加载并执行时,执行了上述实施例所述的方法。In addition, it should be noted that: an embodiment of the present application also provides a computer-readable storage medium, in which a computer program is stored. When the computer program is loaded and executed by a processor, the method described in the above embodiment is executed.
此外,需要说明的是:本申请实施例还提供了还提供一种电子设备,包括存储器、处理器及存储在存储器中并能够在处理器上运行的计算机程序,所述处理器加载并执行计算机程序时,执行了上述实施例所述的方法。In addition, it should be noted that an embodiment of the present application also provides an electronic device, including a memory, a processor, and a computer program stored in the memory and capable of running on the processor. When the processor loads and executes the computer program, the method described in the above embodiment is executed.
以上内容是结合具体的优选实施方式对本发明所作的进一步详细说明,不能认定本发明的具体实施只局限于这些说明。对于本发明所属技术领域的普通技术人员来说,在不脱离本发明构思的前提下,还可以做出若干简单推演或替换,都应当视为属于本发明的保护范围。The above contents are further detailed descriptions of the present invention in combination with specific preferred embodiments, and it cannot be determined that the specific implementation of the present invention is limited to these descriptions. For ordinary technicians in the technical field to which the present invention belongs, several simple deductions or substitutions can be made without departing from the concept of the present invention, which should be regarded as falling within the protection scope of the present invention.
Claims (10)
Priority Applications (1)
| Application Number | Priority Date | Filing Date | Title |
|---|---|---|---|
| CN202410915504.XA CN118819899A (en) | 2024-07-09 | 2024-07-09 | A data processing method, medium and electronic device for financial transaction scenarios |
Applications Claiming Priority (1)
| Application Number | Priority Date | Filing Date | Title |
|---|---|---|---|
| CN202410915504.XA CN118819899A (en) | 2024-07-09 | 2024-07-09 | A data processing method, medium and electronic device for financial transaction scenarios |
Publications (1)
| Publication Number | Publication Date |
|---|---|
| CN118819899A true CN118819899A (en) | 2024-10-22 |
Family
ID=93068019
Family Applications (1)
| Application Number | Title | Priority Date | Filing Date |
|---|---|---|---|
| CN202410915504.XA Pending CN118819899A (en) | 2024-07-09 | 2024-07-09 | A data processing method, medium and electronic device for financial transaction scenarios |
Country Status (1)
| Country | Link |
|---|---|
| CN (1) | CN118819899A (en) |
Cited By (1)
| Publication number | Priority date | Publication date | Assignee | Title |
|---|---|---|---|---|
| CN121056424A (en) * | 2025-10-31 | 2025-12-02 | 广州致远电子股份有限公司 | CAN frame data transmission method, apparatus, device and storage medium |
-
2024
- 2024-07-09 CN CN202410915504.XA patent/CN118819899A/en active Pending
Cited By (1)
| Publication number | Priority date | Publication date | Assignee | Title |
|---|---|---|---|---|
| CN121056424A (en) * | 2025-10-31 | 2025-12-02 | 广州致远电子股份有限公司 | CAN frame data transmission method, apparatus, device and storage medium |
Similar Documents
| Publication | Publication Date | Title |
|---|---|---|
| US9250979B2 (en) | Asynchronous grace-period primitives for user-space applications | |
| US7962923B2 (en) | System and method for generating a lock-free dual queue | |
| CN101763289B (en) | Message passing method based on shared memory | |
| US20080201561A1 (en) | Multi-threaded parallel processor methods and apparatus | |
| US8806168B2 (en) | Producer-consumer data transfer using piecewise circular queue | |
| US11468001B1 (en) | Processing-in-memory concurrent processing system and method | |
| JP4568292B2 (en) | Method, apparatus, program, and information processing system for executing cache line polling | |
| JP6260303B2 (en) | Arithmetic processing device and control method of arithmetic processing device | |
| US20240036919A1 (en) | Efficient task allocation | |
| CN101510162B (en) | Software transaction internal memory implementing method based on delaying policy | |
| CN111052094B (en) | Spin lock efficiency enhancement for user space using C-state and turbo acceleration | |
| CN106250348A (en) | A kind of heterogeneous polynuclear framework buffer memory management method based on GPU memory access characteristic | |
| CN117501254A (en) | Providing atomicity for complex operations using near-memory computation | |
| CN112764904A (en) | Method for preventing starvation of low priority tasks in multitask-based system | |
| US20050066149A1 (en) | Method and system for multithreaded processing using errands | |
| CN112732416A (en) | Parallel data processing method and parallel processor for effectively eliminating data access delay | |
| US8869172B2 (en) | Method and system method and system for exception-less system calls for event driven programs | |
| EP2284703B1 (en) | Scheduling of tasks in a parallel computer system according to defined policies | |
| CN118819899A (en) | A data processing method, medium and electronic device for financial transaction scenarios | |
| US11481250B2 (en) | Cooperative workgroup scheduling and context prefetching based on predicted modification of signal values | |
| WO2025200123A1 (en) | Cache management method, cache management apparatus, and processor and electronic apparatus | |
| CN114610494A (en) | Resource allocation method, electronic device, and computer-readable storage medium | |
| US7661115B2 (en) | Method, apparatus and program storage device for preserving locked pages in memory when in user mode | |
| US20120137300A1 (en) | Information Processor and Information Processing Method | |
| Souto et al. | Improving concurrency and memory usage in distributed operating systems for lightweight manycores via cooperative time-sharing lightweight tasks |
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 |