Message writing method and device
By using the preset log mode in Kafka to distinguish fixed-length and variable-length modes, and classifying writing and reading of messages of different lengths, the problem of low writing efficiency of Kafka messages is solved and efficient message processing is achieved.
Patent Information
- Application Number
- CN202410395752.6
- Authority / Receiving Office
- CN · China
- Patent Type
- Applications(China)
- Current Assignee / Owner
- Filing Date
- 2024-04-02
- Publication Date
- 2024-10-18
AI Technical Summary
The Kafka message writing mechanism ignores the impact of different message lengths on writing efficiency, resulting in a common writing mechanism for fixed-length and non-fixed-length messages, resulting in a decrease in overall writing efficiency.
Use the preset log mode to distinguish the fixed-length mode and the variable-length mode in Kafka's log segments, and perform targeted classification and writing according to the message length. If the message length exceeds the preset length, it will be split into fixed-length and additional messages, and passed Specific storage location calculations enable fast positioning and reading.
By distinguishing storage length modes, the efficiency of message writing and reading is improved, the time-consuming creation of index files is avoided, and the overall message processing efficiency is improved.
Smart Images

Figure CN118796765A_ABST
Abstract
Description
Technical Field
[0001] The present application relates to the field of computer technology, and in particular to a message writing method and device. Background Art
[0002] Kafka is a distributed stream processing system and a widely used message middleware with high reliability and suitable for cloud processing environments.
[0003] In Kafka, messages are continuously subdivided and written according to topics, log partitions, and log segments. The storage space under each topic is divided into multiple log partitions, and the storage space under each log partition is divided into multiple log segments. Each log segment includes a log file. When messages are written, they are written to the corresponding log files in chronological order.
[0004] However, the current Kafka message writing mechanism ignores the impact of different message lengths on message writing efficiency, and adopts the same general writing mechanism for fixed-length and non-fixed-length messages, resulting in a decrease in the overall message writing efficiency. Summary of the invention
[0005] The embodiments of the present application provide a message writing method and device to solve the technical problem that the current Kafka message writing mechanism ignores the impact of different message lengths on message writing efficiency, adopts a universal writing mechanism for fixed-length and non-fixed-length messages, and leads to a decrease in the overall message writing efficiency.
[0006] In a first aspect, an embodiment of the present application provides a message writing method, comprising:
[0007] According to a preset log mode, a target message is written in a log segment based on Kafka; the preset log mode includes a fixed-length mode with a fixed storage length and a variable-length mode with a non-fixed storage length in a log file of the log segment.
[0008] In one embodiment, writing the target message in the log segment based on Kafka according to the preset log mode includes:
[0009] If the preset log mode is a fixed-length mode, determining whether the message content length of the target message exceeds a first storage length; the first storage length is a preset storage length of the message content in the log file of the log segment in the fixed-length mode;
[0010] If not, writing the message header and message content of the target message in the log file of the log segment in sequence;
[0011] If it exceeds, the target message is written into the abnormal message queue of the log segment and an alarm log is output.
[0012] In one embodiment, writing the target message in the log segment based on Kafka according to the preset log mode includes:
[0013] If the preset log mode is a variable length mode, determining whether the message content length of the target message exceeds a second storage length; the second storage length is a preset storage length of the message content in the log file of the log segment in the variable length mode;
[0014] If not, setting the value of the data position field in the message header of the target message to a first preset value to obtain a first message header;
[0015] Writing the first message header and the message content of the target message in the log file of the log segment in sequence;
[0016] If it exceeds, splitting the target message into a fixed-length message and an additional message, and writing the fixed-length message and the additional message into the log segment respectively;
[0017] The fixed-length message is a message whose content length is equal to the second storage length, and the additional message is a message in the target message other than the fixed-length message.
[0018] In one embodiment, writing the fixed-length message and the additional message in the log segment separately includes:
[0019] Creating a new additional file in the log segment, setting the value of the data position field in the message header of the fixed-length message to a second preset value, to obtain a second message header; the second preset value is used to indicate a preset starting position of the additional message in the additional file;
[0020] Writing the second message header and the message content of the fixed-length message in sequence into the log file of the log segment;
[0021] The preset start position is used as the start writing position, and the message content of the additional message is written into the additional file.
[0022] In one embodiment, after writing the target message in the log segment based on Kafka according to the preset log mode, the following steps are performed:
[0023] If the preset log mode is a fixed-length mode, the log segment is located according to the absolute offset of the target message; the absolute offset of the target message is the offset of the target message relative to the reference message, and the reference message is the first message in the first log file in the log partition where the target message is located;
[0024] Obtaining the storage position of the target message in the log file of the log segment according to the base offset of the log file of the log segment, the file header length of the log file of the log segment, the message header length of the target message, the absolute offset of the target message and the first storage length; the base offset is the offset of the first message in the log file of the log segment relative to the base message;
[0025] The target message is read based on the storage location.
[0026] In one embodiment, after writing the target message in the log segment based on Kafka according to the preset log mode, the following steps are performed:
[0027] If the preset log mode is a variable length mode, obtaining the value of the data position field in the message header of the log file of the log segment;
[0028] The target message is read according to the value of the data location field.
[0029] In one embodiment, reading the target message according to the value of the data location field includes:
[0030] If the value of the data location field is the first preset value, the log segment is located according to the absolute offset of the target message; the absolute offset of the target message is the offset of the target message relative to the reference message, and the reference message is the first message in the first log file in the log partition where the target message is located;
[0031] According to the base offset of the log file of the log segment, the file header length of the log file of the log segment, the message header length of the target message, the absolute offset of the target message and the second storage length, the storage position of the target message in the log file of the log segment is obtained; the base offset is the offset of the first message in the log file of the log segment relative to the base message;
[0032] The target message is read based on the storage location.
[0033] In one embodiment, reading the target message according to the value of the data location field includes:
[0034] If the value of the data position field is the second preset value, the log segment is located according to the absolute offset of the fixed-length message; the absolute offset of the fixed-length message is the offset of the fixed-length message relative to the reference message, and the reference message is the first message in the first log file in the log partition where the fixed-length message is located;
[0035] According to the base offset of the log file of the log segment, the file header length of the log file of the log segment, the message header length of the fixed-length message, the absolute offset of the fixed-length message and the second storage length, the storage position of the fixed-length message in the log file of the log segment is obtained; the base offset is the offset of the first message in the log file of the log segment relative to the base message;
[0036] Reading the fixed-length message based on the storage location;
[0037] Acquire a preset start position of the additional message in the additional file according to the value of the data position field, and read the additional message from the additional file with the preset start position as the start reading position;
[0038] The fixed-length message and the additional message are combined to generate the target message.
[0039] In one embodiment, after sequentially writing the message header and the message content of the target message into the log file of the log segment, the method includes:
[0040] If the message content length of the target message is less than the first storage length, the message content of the target message is padded to the first storage length.
[0041] In a second aspect, an embodiment of the present application provides a message writing device, including:
[0042] A writing module is used to write a target message in a log segment based on Kafka according to a preset log mode; the preset log mode includes a fixed-length mode with a fixed storage length and a variable-length mode with a non-fixed storage length in the log file of the log segment.
[0043] In a third aspect, an embodiment of the present application provides an electronic device, including a processor and a memory storing a computer program, wherein the processor implements the steps of the message writing method described in the first aspect when executing the program.
[0044] In a fourth aspect, an embodiment of the present application provides a computer program product, including a computer program, which, when executed by a processor, implements the steps of the message writing method described in the first aspect.
[0045] In a fifth aspect, an embodiment of the present application provides a non-transitory computer-readable storage medium, including a computer program, which, when executed by a processor, implements the steps of the message writing method described in the first aspect.
[0046] The message writing method and device provided by the present application write the target message in the log segment based on kafka according to the preset log mode. Since the preset log mode includes a fixed-length mode with a fixed storage length and a variable-length mode with a non-fixed storage length in the log file of the log segment, that is, by distinguishing the storage length and dividing different writing modes based on the storage length, targeted classification writing of target messages of fixed length and non-fixed length is achieved, which greatly improves the overall message writing efficiency. BRIEF DESCRIPTION OF THE DRAWINGS
[0047] In order to more clearly illustrate the technical solutions in the present application or the prior art, a brief introduction will be given below to the drawings required for use in the embodiments or the description of the prior art. Obviously, the drawings described below are some embodiments of the present application. For ordinary technicians in this field, other drawings can be obtained based on these drawings without paying any creative work.
[0048] Figure 1 This is one of the flowcharts of the message writing method provided in the embodiment of the present application;
[0049] Figure 2 This is the second flow chart of the message writing method provided in the embodiment of the present application;
[0050] Figure 3 This is the third flow chart of the message writing method provided in the embodiment of the present application;
[0051] Figure 4 This is the message storage structure diagram in traditional Kafka;
[0052] Figure 5 This is the fourth flowchart of the message writing method provided in the embodiment of the present application;
[0053] Figure 6 This is the fifth flow chart of the message writing method provided in the embodiment of the present application;
[0054] Figure 7 It is a schematic diagram of the structure of an electronic device provided in an embodiment of the present application. DETAILED DESCRIPTION
[0055] In order to make the purpose, technical solutions and advantages of this application clearer, the technical solutions in this application will be clearly and completely described below in conjunction with the drawings in the embodiments of this application. Obviously, the described embodiments are part of the embodiments of this application, not all of them. Based on the embodiments in this application, all other embodiments obtained by ordinary technicians in this field without creative work are within the scope of protection of this application.
[0056] The present application embodiment provides a message writing method, which may include:
[0057] Write the target message in the Kafka-based log segment according to the preset log mode;
[0058] The preset log mode includes a fixed-length mode in which the storage length of the log file of the log segment is fixed and a variable-length mode in which the storage length is not fixed.
[0059] Operator billing details, also known as call bills, lists, service usage records, etc., are billing records of communications generated by users using the services provided by operators. The current process of forming billing details (including voice bills, traffic bills, SMS bills, etc.) includes collecting user communication information from network elements to business support systems, sorting (converting user communication information from files to a batch of records in a specified format), batch pricing (billing each record based on tariff information), combining accounts (combining cost information into bills), warehousing, etc. Each link in these processes corresponds to different applications.
[0060] The data processing of the above process is carried out by using the message transmission mechanism, which is to sort and process the collected files, output them into message records, and transmit the messages through the message middleware. The transmission process includes frequently writing messages in the message middleware and frequently reading messages from the message middleware.
[0061] The message writing method of this embodiment can use Kafka as the message middleware to write messages during the operator billing details generation process.
[0062] It should be noted that after writing the target message, it is possible to determine whether to create a timestamp index file according to demand. At the same time, there is no need to create an offset index file.
[0063] The message writing method provided in this embodiment writes the target message in the log segment based on Kafka according to the preset log mode. Since the preset log mode includes a fixed-length mode with a fixed storage length and a variable-length mode with a non-fixed storage length in the log file of the log segment, that is, by distinguishing the storage length and dividing different writing modes based on the storage length, targeted classification writing of target messages of fixed length and non-fixed length is achieved, greatly improving the overall message writing efficiency.
[0064] Furthermore, in the traditional message writing process, a timestamp index file and an offset index file are also required to facilitate the subsequent retrieval and reading of the written messages. In this embodiment, it is possible to decide whether to create a timestamp index file based on demand, and no longer create an offset index file, thereby saving the time of creating an index file during the message writing process and further improving the message writing efficiency.
[0065] Figure 1 This is one of the flow charts of the message writing method provided in the embodiment of the present application; Figure 1 In one embodiment, writing a target message in a Kafka-based log segment according to a preset log mode may include:
[0066] 101. If the preset log mode is the fixed-length mode, determine whether the message content length of the target message exceeds the first storage length;
[0067] The first storage length is the preset storage length of the message content in the log file of the log segment in the fixed-length mode;
[0068] 102. If it does not exceed, the message header and message content of the target message are written in sequence in the log file of the log segment;
[0069] 103. If exceeded, the target message is written into the abnormal message queue of the log segment and an alarm log is output.
[0070] In step 101, when the preset log mode is the fixed-length mode, the fixed-length mode keyword will be written in the file header of the log file of the log segment when the log segment is initially created, and a new field will be added after the keyword to save the value of the keyword and the first storage length, indicating that the log file has enabled the fixed-length mode and recorded the relevant parameter information. Since the corresponding preset log mode is saved in each log file, it can be ensured that the existing messages in the original log file are not affected when the preset log mode is switched in the new log file.
[0071] In step 102, if the message content length of the target message is less than the first storage length, the message content of the target message is padded to the first storage length to form a clear boundary with other messages to facilitate data identification and processing. In addition, 0 or 1 or a combination of the two can be used to padded the message content, which is not limited here.
[0072] In practical applications, there is no strict timing relationship between step 102 and step 103; that is, they can be executed simultaneously, or any one of the steps can be executed first, depending on actual needs and is not limited here.
[0073] In this embodiment, a certain topic can be used as a unit and the preset log mode under the topic can be set to a fixed-length mode. When the message content lengths of messages related to the topic are not much different, the first storage length can be set to the longest message content length among these messages, and then these messages can be written into the log file under the topic, so as to make full use of the preset storage space and avoid the reduction of writing efficiency due to excessive space occupation, thereby improving writing efficiency.
[0074] Figure 2This is the second flow chart of the message writing method provided in the embodiment of the present application; refer to Figure 2 In one embodiment, writing a target message in a Kafka-based log segment according to a preset log mode may include:
[0075] 201. If the preset log mode is the variable length mode, determine whether the message content length of the target message exceeds the second storage length;
[0076] The second storage length is a preset storage length of the message content in the log file of the log segment in the variable length mode;
[0077] 202. If not, setting the value of the data position field in the message header of the target message to a first preset value to obtain a first message header;
[0078] 203. Write the first message header and message content of the target message in the log file of the log segment in sequence;
[0079] 204. If it exceeds, the target message is split into a fixed-length message and an additional message, and the fixed-length message and the additional message are written into the log segment respectively.
[0080] The fixed-length message is a message whose content length is equal to the second storage length, and the additional message is a message in the target message other than the fixed-length message.
[0081] In step 201, when the preset log mode is the variable length mode, the variable length mode keyword will be written in the file header of the log file of the log segmentation when the log segmentation is initially created, and a new field will be added after the keyword to save the value of the keyword and the second storage length, indicating that the log file has enabled the variable length mode and recorded the relevant parameter information. Since the corresponding preset log mode is saved in each log file, it can be ensured that the existing messages in the original log file are not affected when the preset log mode is switched in the new log file.
[0082] In step 202 to step 203, the first preset value may be zero. After the first message header and message content of the target message are written in sequence in the log file of the log segment, if the message content length of the target message is less than the second storage length, the message content of the target message is padded to be equal to the second storage length. That is, in the variable length mode, if the message content length of the target message does not exceed the second storage length, the target message is written in a similar fixed-length mode. The difference is that the value of the data position field in the message header of the target message must be set to the first preset value, indicating that there is no additional message.
[0083] In actual applications, there is no strict timing relationship between step 202 and step 204; that is, they can be executed simultaneously, or any one step can be executed first, depending on actual needs and is not limited here, but step 203 must be executed after step 202.
[0084] In this embodiment, a certain topic can be used as a unit and the preset log mode under the topic can be set to a variable length mode. When the message content lengths of messages related to the topic vary greatly, the second storage length can be set to the mode of the message content lengths of these messages, ensuring that as many messages as possible can be written into the log file under the topic without being split, and then a relatively small number of messages that exceed the second storage length can be split and written, making full use of the preset storage space and avoiding a decrease in writing efficiency due to excessive space occupation, thereby improving writing efficiency.
[0085] Figure 3 This is the third flow chart of the message writing method provided in the embodiment of the present application; Figure 3 In one embodiment, writing fixed-length messages and additional messages separately in log segments may include:
[0086] 301. Create a new additional file in the log segment, set the value of the data position field in the message header of the fixed-length message to a second preset value, and obtain a second message header;
[0087] The second preset value is used to indicate a preset starting position of the additional message in the additional file;
[0088] 302. Write the second message header and the message content of the fixed-length message in sequence in the log file of the log segment;
[0089] 303. Write the message content of the additional message into the additional file with the preset start position as the start writing position.
[0090] If the message content length of the target message exceeds the second storage length, the target message is split into a fixed-length message and an additional message, and an additional file is created. The fixed-length message and the additional message are written respectively in the log file and the additional file of the log segment. In addition, the value of the data position field in the message header of the fixed-length message must be set to the preset starting position of the additional message in the additional file. The preset starting position is usually represented by a number greater than zero, that is, the second preset value is usually greater than zero.
[0091] It should be noted that the additional file contains an additional message record body and a CRC checksum, wherein each record in the additional message record body includes an additional message record header and additional message content, the additional message record header may include the message content length of the additional message and the offset of the additional message relative to the first message in the first log file in the log partition where the log segment is located, etc. The CRC checksum is used to ensure the integrity of the additional file.
[0092] This embodiment creates an additional file to write additional messages exceeding the second storage length in the target message, and writes the fixed-length message in the target message in the original log file. It can use the additional file to expand the variable-length message writing method while retaining the original fixed-length message writing mode.
[0093] Figure 4 This is the message storage structure diagram in traditional Kafka;
[0094] Figure 5 This is a flowchart of the message writing method provided in the embodiment of the present application; refer to Figure 5 In one embodiment, according to the preset log mode, after writing the target message in the Kafka-based log segment, it may include:
[0095] 501. If the preset log mode is the fixed-length mode, locate the log segment according to the absolute offset of the target message;
[0096] The absolute offset of the target message is the offset of the target message relative to the base message. The base message is the first message in the first log file in the log partition where the target message is located.
[0097] 502. Obtain a storage position of the target message in the log file of the log segmentation according to a base offset of the log file of the log segmentation, a file header length of the log file of the log segmentation, a message header length of the target message, an absolute offset of the target message, and a first storage length;
[0098] The base offset is the offset of the first message in the log file of the log segment relative to the base message;
[0099] 503. Read the target message based on the storage location.
[0100] Reference Figure 4 The storage space under topic 1 is divided into N+1 log partitions, and the storage space under log partition 0 is divided into M+1 log segments. Each log segment includes a log file with the suffix log, an offset index file with the suffix index, and a timestamp index file with the suffix timeindex. Each log file can write multiple messages in chronological order.
[0101] In step 501, it is assumed that the log partition where the target message is located is log partition 0, the benchmark message is the first message in the first log file 00000000000000000000.log in log partition 0, and the absolute offset of the target message is the offset of the target message relative to the first message in 000000000000000000000.log.
[0102] The binary search method can be used to locate the log segment where the target message is located according to the absolute offset of the target message.
[0103] In step 502, assuming that the log segment where the target message is located is determined to be log segment 1, the reference offset is the offset of the first message in the log file 000000000000123456789.log of log segment 1 relative to the first message in 000000000000000000000.log. Since the log file of each log segment is named after its corresponding reference offset, the reference offset in this embodiment is 000000000000123456789. The storage position of the target message in 00000000000123456789.log can be calculated using the following formula:
[0104] S1=L1+(L2+L3)*(offset-baseoffset);
[0105] Among them, S1 is the storage location of the target message in 00000000000123456789.log, L1 is the file header length of the log file 00000000000123456789.log, L2 is the message header length of the target message, L3 is the first storage length, offset is the absolute offset of the target message, and baseoffset is the base offset.
[0106] Reference Figure 4 Taking log segment 0 as an example, in the traditional message writing process, in addition to creating the log file 000000000000000000000.log in log segment 0, it is also necessary to synchronously create the offset index file 00000000000000000000.index and the timestamp index file 00000000000000000000.timeindex, where the offset index file is used to save the mapping relationship between the absolute offset of the message (that is, the offset of the message relative to the first message in the first log file of the log partition where it is located) and the physical storage address of the message, and the timestamp index file is used to save the mapping relationship between the timestamp of the message and the absolute offset of the message.
[0107] Since both the offset index file and the timestamp index file are sparse index files, that is, not every message has a corresponding index item in the index file, therefore, when searching for the target message through the timestamp index file, it is possible that only the absolute offset corresponding to the timestamp close to the timestamp of the target message can be found, and then the physical storage address corresponding to the absolute offset close to it is found in the offset index file based on the absolute offset, and then the characteristic of the absolute offset increasing sequentially in the log file is used to search sequentially in the log file until the target message is found; when searching for the target message through the offset index file, it is possible that only the physical storage address corresponding to the absolute offset close to the absolute offset of the target message can be found, and then the characteristic of the absolute offset increasing sequentially in the log file is used to search sequentially in the log file until the target message is found.
[0108] This embodiment does not need to create an offset index file and a timestamp index file, and searches for the target message through the index file. It can directly calculate the storage location of the target message and read the target message in the fixed-length mode using a specific formula, and fully utilize the writing format characteristics of the target message in Kafka to quickly locate the target message and improve the message reading efficiency.
[0109] Figure 6 This is a flowchart of the message writing method provided in the embodiment of the present application; refer to Figure 6 In one embodiment, according to the preset log mode, after writing the target message in the Kafka-based log segment, it may include:
[0110] 601. If the preset log mode is the variable length mode, obtain the value of the data position field in the message header of the log file of the log segment;
[0111] 602. Read the target message according to the value of the data location field.
[0112] An embodiment of step 602 is as follows:
[0113] 602a. If the value of the data location field is a first preset value, locate the log segment according to the absolute offset of the target message;
[0114] The absolute offset of the target message is the offset of the target message relative to the base message. The base message is the first message in the first log file in the log partition where the target message is located.
[0115] 602b. Obtain the storage position of the target message in the log file of the log segmentation according to the base offset of the log file of the log segmentation, the file header length of the log file of the log segmentation, the message header length of the target message, the absolute offset of the target message, and the second storage length;
[0116] The base offset is the offset of the first message in the log file of the log segment relative to the base message;
[0117] 602c. Read the target message based on the storage location.
[0118] In step 602a to step 602c, that is, when the value of the data position field is the first preset value and there is no additional message, the target message is read in fixed-length mode.
[0119] Another embodiment of step 602 is as follows:
[0120] 602A. If the value of the data position field is a second preset value, locate the log segment according to the absolute offset of the fixed-length message;
[0121] The absolute offset of a fixed-length message is the offset of the fixed-length message relative to the base message. The base message is the first message in the first log file in the log partition where the fixed-length message is located.
[0122] 602B. Obtain a storage position of the fixed-length message in the log file of the log segment according to the base offset of the log file of the log segment, the file header length of the log file of the log segment, the message header length of the fixed-length message, the absolute offset of the fixed-length message, and the second storage length;
[0123] The base offset is the offset of the first message in the log file of the log segment relative to the base message;
[0124] 602C, reading a fixed-length message based on a storage location;
[0125] 602D. Obtain a preset start position of the additional message in the additional file according to the value of the data position field, use the preset start position as the start reading position, and read the additional message from the additional file;
[0126] 602E. Combine the fixed-length message and the additional message to generate a target message.
[0127] That is, when the value of the data position field is the second preset value and there is an additional message, the fixed-length message is first read according to the fixed-length mode, and then the additional message is read from the additional file, and the fixed-length message and the additional message are merged to generate the target message, thereby realizing the reading of the target message.
[0128] This embodiment does not need to create an offset index file and a timestamp index file, and searches for the target message through the index file. It can perform targeted message reading according to the message content length of the target message in the variable-length mode. When the message content length does not exceed the second storage length, the reading method in the fixed-length mode is adopted, and the storage position of the target message is directly calculated using a specific formula and the target message is read. The writing format characteristics of the target message in Kafka are fully utilized to quickly locate the target message and improve the message reading efficiency; when the message content length exceeds the second storage length, the fixed-length message and the additional message are read separately and then merged to read the target message. Since there are relatively few overlong messages, only a few overlong messages need to be read twice, but even if they need to be read twice, there is no need to search sequentially through the index file, thereby improving the message reading efficiency.
[0129] The message writing device provided in an embodiment of the present application is described below. The message writing device described below and the message writing method described above can be referenced to each other.
[0130] The present application embodiment provides a message writing device, which may include:
[0131] A writing module is used to write a target message in a log segment based on Kafka according to a preset log mode; the preset log mode includes a fixed-length mode with a fixed storage length and a variable-length mode with a non-fixed storage length in the log file of the log segment.
[0132] The message writing device provided in this embodiment writes the target message in the log segment based on Kafka according to the preset log mode. Since the preset log mode includes a fixed-length mode with a fixed storage length and a variable-length mode with a non-fixed storage length in the log file of the log segment, that is, by distinguishing the storage length and dividing different writing modes based on the storage length, targeted classification writing of target messages of fixed length and non-fixed length is achieved, greatly improving the overall message writing efficiency.
[0133] Furthermore, in the traditional message writing process, a timestamp index file and an offset index file are also required to facilitate the subsequent retrieval and reading of the written messages. In this embodiment, it is possible to decide whether to create a timestamp index file based on demand, and no longer create an offset index file, thereby saving the time of creating an index file during the message writing process and further improving the message writing efficiency.
[0134] In one embodiment, the writing module is specifically used to:
[0135] If the preset log mode is a fixed-length mode, determining whether the message content length of the target message exceeds a first storage length; the first storage length is a preset storage length of the message content in the log file of the log segment in the fixed-length mode;
[0136] If not, writing the message header and message content of the target message in the log file of the log segment in sequence;
[0137] If it exceeds, the target message is written into the abnormal message queue of the log segment and an alarm log is output.
[0138] In one embodiment, the writing module is specifically used to:
[0139] If the preset log mode is a variable length mode, determining whether the message content length of the target message exceeds a second storage length; the second storage length is a preset storage length of the message content in the log file of the log segment in the variable length mode;
[0140] If not, setting the value of the data position field in the message header of the target message to a first preset value to obtain a first message header;
[0141] Writing the first message header and the message content of the target message in the log file of the log segment in sequence;
[0142] If it exceeds, splitting the target message into a fixed-length message and an additional message, and writing the fixed-length message and the additional message into the log segment respectively;
[0143] The fixed-length message is a message whose content length is equal to the second storage length, and the additional message is a message in the target message other than the fixed-length message.
[0144] In one embodiment, the writing module is specifically used to:
[0145] Creating a new additional file in the log segment, setting the value of the data position field in the message header of the fixed-length message to a second preset value, to obtain a second message header; the second preset value is used to indicate a preset starting position of the additional message in the additional file;
[0146] Writing the second message header and the message content of the fixed-length message in sequence into the log file of the log segment;
[0147] The preset start position is used as the start writing position, and the message content of the additional message is written into the additional file.
[0148] In one embodiment, a reading module is further included, which is used to:
[0149] If the preset log mode is a fixed-length mode, the log segment is located according to the absolute offset of the target message; the absolute offset of the target message is the offset of the target message relative to the reference message, and the reference message is the first message in the first log file in the log partition where the target message is located;
[0150] Obtaining the storage position of the target message in the log file of the log segment according to the base offset of the log file of the log segment, the file header length of the log file of the log segment, the message header length of the target message, the absolute offset of the target message and the first storage length; the base offset is the offset of the first message in the log file of the log segment relative to the base message;
[0151] The target message is read based on the storage location.
[0152] In one embodiment, the reading module is specifically used to:
[0153] If the preset log mode is a variable length mode, obtaining the value of the data position field in the message header of the log file of the log segment;
[0154] The target message is read according to the value of the data location field.
[0155] In one embodiment, the reading module is specifically used to:
[0156] If the value of the data location field is the first preset value, the log segment is located according to the absolute offset of the target message; the absolute offset of the target message is the offset of the target message relative to the reference message, and the reference message is the first message in the first log file in the log partition where the target message is located;
[0157] According to the base offset of the log file of the log segment, the file header length of the log file of the log segment, the message header length of the target message, the absolute offset of the target message and the second storage length, the storage position of the target message in the log file of the log segment is obtained; the base offset is the offset of the first message in the log file of the log segment relative to the base message;
[0158] The target message is read based on the storage location.
[0159] In one embodiment, the reading module is specifically used to:
[0160] If the value of the data position field is the second preset value, the log segment is located according to the absolute offset of the fixed-length message; the absolute offset of the fixed-length message is the offset of the fixed-length message relative to the reference message, and the reference message is the first message in the first log file in the log partition where the fixed-length message is located;
[0161] According to the base offset of the log file of the log segment, the file header length of the log file of the log segment, the message header length of the fixed-length message, the absolute offset of the fixed-length message and the second storage length, the storage position of the fixed-length message in the log file of the log segment is obtained; the base offset is the offset of the first message in the log file of the log segment relative to the base message;
[0162] Reading the fixed-length message based on the storage location;
[0163] Acquire a preset start position of the additional message in the additional file according to the value of the data position field, and read the additional message from the additional file with the preset start position as the start reading position;
[0164] The fixed-length message and the additional message are combined to generate the target message.
[0165] In one embodiment, the writing module is specifically used to:
[0166] If the message content length of the target message is less than the first storage length, the message content of the target message is padded to the first storage length.
[0167] Figure 7 An example of a structural diagram of an electronic device is shown in FIG. Figure 7 As shown, the electronic device may include: a processor 710, a communication interface 720, a memory 730 and a communication bus 740, wherein the processor 710, the communication interface 720, and the memory 730 communicate with each other through the communication bus 740. The processor 710 may call a computer program in the memory 730 to execute the steps of the message writing method, for example, including:
[0168] According to a preset log mode, a target message is written in a log segment based on Kafka; the preset log mode includes a fixed-length mode with a fixed storage length and a variable-length mode with a non-fixed storage length in a log file of the log segment.
[0169] In addition, the logic instructions in the above-mentioned memory 730 can be implemented in the form of a software functional unit and can be stored in a computer-readable storage medium when it is sold or used as an independent product. Based on this understanding, the technical solution of the present application can be essentially or partly embodied in the form of a software product that contributes to the prior art, and the computer software product is stored in a storage medium, including several 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 application. The aforementioned storage medium includes: various media that can store program codes, such as a USB flash drive, a mobile hard disk, a read-only memory (ROM), a random access memory (RAM), a disk or an optical disk.
[0170] On the other hand, an embodiment of the present application further provides a computer program product, the computer program product including a computer program, the computer program can be stored on a non-transitory computer-readable storage medium, and when the computer program is executed by a processor, the computer can perform the steps of the message writing method provided in the above embodiments, for example, including:
[0171] According to a preset log mode, a target message is written in a log segment based on Kafka; the preset log mode includes a fixed-length mode with a fixed storage length and a variable-length mode with a non-fixed storage length in a log file of the log segment.
[0172] On the other hand, an embodiment of the present application further provides a processor-readable storage medium, wherein the processor-readable storage medium stores a computer program, wherein the computer program is used to enable the processor to execute the steps of the message writing method provided in the above embodiments, for example, including:
[0173] According to a preset log mode, a target message is written in a log segment based on Kafka; the preset log mode includes a fixed-length mode with a fixed storage length and a variable-length mode with a non-fixed storage length in a log file of the log segment.
[0174] The processor-readable storage medium can be any available medium or data storage device that can be accessed by the processor, including but not limited to magnetic storage (such as floppy disks, hard disks, magnetic tapes, magneto-optical disks (MO), etc.), optical storage (such as CD, DVD, BD, HVD, etc.), and semiconductor storage (such as ROM, EPROM, EEPROM, non-volatile memory (NANDFLASH), solid-state drive (SSD)), etc.
[0175] The device embodiments described above are merely illustrative, wherein the units described as separate components may or may not be physically separated, and the components displayed as units may or may not be physical units, that is, they may be located in one place, or they may be distributed on multiple network units. Some or all of the modules may be selected according to actual needs to achieve the purpose of the scheme of this embodiment. Ordinary technicians in this field can understand and implement it without paying creative labor.
[0176] Through the description of the above implementation methods, those skilled in the art can clearly understand that each implementation method can be implemented by means of software plus a necessary general hardware platform, and of course, it can also be implemented by hardware. Based on this understanding, the above technical solution is essentially or the part that contributes to the prior art can be embodied in the form of a software product, and the computer software product can be stored in a computer-readable storage medium, such as ROM / RAM, a disk, an optical disk, etc., including a number of instructions for a computer device (which can be a personal computer, a server, or a network device, etc.) to execute the methods described in each embodiment or some parts of the embodiments.
[0177] Finally, it should be noted that the above embodiments are only used to illustrate the technical solutions of the present application, rather than to limit it. Although the present application has been described in detail with reference to the aforementioned embodiments, those skilled in the art should understand that they can still modify the technical solutions described in the aforementioned embodiments, or make equivalent replacements for some of the technical features therein. 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 application.
Claims
1. A message writing method, characterized in that: include: Write the target message in the Kafka-based log segment according to the preset log mode; The preset log mode includes a fixed-length mode in which the storage length of the log file of the log segment is fixed and a variable-length mode in which the storage length is not fixed.
2. The message writing method according to claim 1, characterized in that: The step of writing the target message in the log segment based on Kafka according to the preset log mode includes: If the preset log mode is a fixed-length mode, determining whether the message content length of the target message exceeds a first storage length; the first storage length is a preset storage length of the message content in the log file of the log segment in the fixed-length mode; If not, writing the message header and message content of the target message in the log file of the log segment in sequence; If it exceeds, the target message is written into the abnormal message queue of the log segment and an alarm log is output.
3. The message writing method according to claim 1, characterized in that: The step of writing the target message in the log segment based on Kafka according to the preset log mode includes: If the preset log mode is a variable length mode, determining whether the message content length of the target message exceeds a second storage length; the second storage length is a preset storage length of the message content in the log file of the log segment in the variable length mode; If not, setting the value of the data position field in the message header of the target message to a first preset value to obtain a first message header; Writing the first message header and the message content of the target message in the log file of the log segment in sequence; If it exceeds, splitting the target message into a fixed-length message and an additional message, and writing the fixed-length message and the additional message into the log segment respectively; The fixed-length message is a message whose content length is equal to the second storage length, and the additional message is a message in the target message other than the fixed-length message.
4. The message writing method according to claim 3, characterized in that: The step of writing the fixed-length message and the additional message into the log segment respectively includes: Creating a new additional file in the log segment, setting the value of the data position field in the message header of the fixed-length message to a second preset value, to obtain a second message header; the second preset value is used to indicate a preset starting position of the additional message in the additional file; Writing the second message header and the message content of the fixed-length message in sequence into the log file of the log segment; The preset start position is used as the start writing position, and the message content of the additional message is written into the additional file.
5. The message writing method according to claim 2, characterized in that: After writing the target message in the log segment based on Kafka according to the preset log mode, the following steps are included: If the preset log mode is a fixed-length mode, the log segment is located according to the absolute offset of the target message; the absolute offset of the target message is the offset of the target message relative to the reference message, and the reference message is the first message in the first log file in the log partition where the target message is located; Obtaining the storage position of the target message in the log file of the log segment according to the base offset of the log file of the log segment, the file header length of the log file of the log segment, the message header length of the target message, the absolute offset of the target message and the first storage length; the base offset is the offset of the first message in the log file of the log segment relative to the base message; The target message is read based on the storage location.
6. The message writing method according to claim 4, characterized in that: After writing the target message in the log segment based on Kafka according to the preset log mode, the following steps are included: If the preset log mode is a variable length mode, obtaining the value of the data position field in the message header of the log file of the log segment; The target message is read according to the value of the data location field.
7. The message writing method according to claim 6, characterized in that: The step of reading the target message according to the value of the data location field comprises: If the value of the data location field is the first preset value, the log segment is located according to the absolute offset of the target message; the absolute offset of the target message is the offset of the target message relative to the reference message, and the reference message is the first message in the first log file in the log partition where the target message is located; According to the base offset of the log file of the log segment, the file header length of the log file of the log segment, the message header length of the target message, the absolute offset of the target message and the second storage length, the storage position of the target message in the log file of the log segment is obtained; the base offset is the offset of the first message in the log file of the log segment relative to the base message; The target message is read based on the storage location.
8. The message writing method according to claim 6, characterized in that: The step of reading the target message according to the value of the data location field comprises: If the value of the data position field is the second preset value, the log segment is located according to the absolute offset of the fixed-length message; the absolute offset of the fixed-length message is the offset of the fixed-length message relative to the reference message, and the reference message is the first message in the first log file in the log partition where the fixed-length message is located; According to the base offset of the log file of the log segment, the file header length of the log file of the log segment, the message header length of the fixed-length message, the absolute offset of the fixed-length message and the second storage length, the storage position of the fixed-length message in the log file of the log segment is obtained; the base offset is the offset of the first message in the log file of the log segment relative to the base message; Reading the fixed-length message based on the storage location; Acquire a preset start position of the additional message in the additional file according to the value of the data position field, and read the additional message from the additional file with the preset start position as the start reading position; The fixed-length message and the additional message are combined to generate the target message.
9. The message writing method according to claim 2, characterized in that: After the message header and the message content of the target message are sequentially written into the log file of the log segment, the method includes: If the message content length of the target message is less than the first storage length, the message content of the target message is padded to the first storage length.
10. A message writing device, characterized in that: include: The writing module is used to write the target message in the Kafka-based log segment according to the preset log mode; The preset log mode includes a fixed-length mode in which the storage length of the log file of the log segment is fixed and a variable-length mode in which the storage length is not fixed.
11. An electronic device comprising a processor and a memory storing a computer program, characterized in that: When the processor executes the computer program, the steps of the message writing method according to any one of claims 1 to 9 are implemented.
12. A computer program product, comprising a computer program, characterized in that When the computer program is executed by a processor, the steps of the message writing method according to any one of claims 1 to 9 are implemented.
13. A non-transitory computer-readable storage medium having a computer program stored thereon, characterized in that: When the computer program is executed by a processor, the steps of the message writing method according to any one of claims 1 to 9 are implemented.
