Online debugging method and device based on thread local variable, equipment and medium

The online debugging method using thread local variables addresses performance and security issues by enabling debugging only when needed, facilitating quick problem resolution in complex functions.

CN119149383APending Publication Date: 2024-12-17PING AN BANK CO LTD
View PDF 0 Cites 0 Cited by

Patent Information

Application Number
CN202410752354.5
Authority / Receiving Office
CN · China
Patent Type
Applications(China)
Current Assignee / Owner
Filing Date
2024-06-12
Publication Date
2024-12-17

AI Technical Summary

Technical Problem

Existing online debugging methods, such as Arthas, impact performance and security in production environments due to their implementation methods, making it difficult to quickly locate complex function issues using logs or tracking information.

Method used

An online debugging method utilizing thread local variables to set a debugging flag and save object information of key nodes, returning it with the interface response message, ensuring debugging is enabled only when needed, thus maintaining security and performance.

Benefits of technology

Enables efficient and secure online debugging by tracking data changes in the request process, allowing quick problem location without affecting normal business operations.

✦ Generated by Eureka AI based on patent content.

Smart Images

  • Figure CN119149383A_ABST
    Figure CN119149383A_ABST
Patent Text Reader

Abstract

The invention discloses an online debugging method and device based on thread local variables, equipment and a medium. The method comprises the following steps: receiving a to-be-debugged request; analyzing the request, and if the request contains a debugging identifier, starting debugging; setting a debugging identifier and a debugging information set on the thread local variable; calling a debugging interface, and storing the data of the object information of the key node into a debugging information set; and adding a preset field in the interface response message, and returning the debugging information set and the interface response message. According to the application, aiming at the request for starting debugging, the debugging identifier is set through the thread local variable, the data of the object information of the key node in the request process is stored, and the data and the interface response message are returned together, so that the data change in the whole request process can be tracked, online debugging is realized, and the problem is quickly positioned. And the debugging is started only when the problem needs to be positioned, so that the normal service is not influenced, and the security is very high.
Need to check novelty before this filing date? Find Prior Art

Description

Technical Field

[0001] The present application relates to the field of debugging technology, and in particular to an online debugging method, apparatus, computer equipment and readable storage medium based on thread local variables. Background Art

[0002] For some complex functions, when problems occur online and need to be located, they are usually located through logs or tracking information. For performance reasons, business systems generally record key logs and tracking information, and sometimes this information is not enough to quickly locate the problem. Summary of the invention

[0003] Based on this, it is necessary to propose an online debugging method, device, computer equipment and readable storage medium based on thread local variables to address the above problems, so as to quickly locate the problems.

[0004] In a first aspect, the present application provides an online debugging method based on thread local variables, the method comprising:

[0005] Receive requests for debugging;

[0006] Parsing the request, and if the request contains a debugging flag, enabling debugging;

[0007] Setting the debugging flag and debugging information set in thread local variables;

[0008] Calling the debugging interface to save the data of the object information of the key node into the debugging information set;

[0009] A preset field is added to the interface response message, wherein the preset field is used to indicate that a debugging information set needs to be returned, and the debugging information set and the interface response message are returned.

[0010] In some embodiments, before starting debugging, the method further includes:

[0011] Get the pre-saved random string, the incoming timestamp of the incoming request, and the incoming random string;

[0012] Calculate the hash value of the sum of the pre-saved random string, the incoming timestamp, and the incoming random string;

[0013] Compare the hash value to the signature value of the incoming request;

[0014] If the hash value is equal to the signature value of the incoming request, debugging is enabled.

[0015] In some embodiments, before starting debugging, the method further includes:

[0016] If the hash value is equal to the signature value of the incoming request, the difference between the incoming timestamp and the server timestamp is calculated, where the server timestamp indicates the time when the server received the request;

[0017] If the difference is within a preset time range, debugging is started.

[0018] In some embodiments, before starting debugging, the method further includes:

[0019] The debugging network segment is configured through the allow instruction of nginx to limit the network segment for executing the debugging method.

[0020] In some embodiments, the method further comprises:

[0021] Add prompt information to the debugging information.

[0022] In some embodiments, receiving a request for debugging includes:

[0023] Use Filter to intercept requests.

[0024] In some embodiments, setting the debug flag and debug information set in the thread local variable includes:

[0025] Set the boolean type variable debugFlag and the DebugInfoDTO type variable debugInfoList in the thread local variables, debugFlag as the debugging flag, and debugInfoList as the debugging information collection.

[0026] In some embodiments, the method further comprises:

[0027] The data of the object information is converted into a json string and saved in the debugging information collection.

[0028] In some embodiments, the method further comprises:

[0029] If a service thread pool or other asynchronous methods are used, the cross-thread transmission of the debugging identifier and the debugging information set is achieved through inheritable thread local variables or open source transmittable thread local variables.

[0030] In a second aspect, the present application further provides an online debugging device based on thread local variables, the device comprising:

[0031] A receiving module, used for receiving a request requiring debugging;

[0032] A parsing module, used for parsing the request, and if the request contains a debugging flag, enabling debugging;

[0033] A setting module, used for setting the debugging flag and debugging information set in the thread local variable;

[0034] A debugging module, used to call a debugging interface and save the data of the object information of the key node into the debugging information set;

[0035] The return module is used to add a preset field in the interface response message, wherein the preset field is used to indicate that a debugging information set needs to be returned, and return the debugging information set and the interface response message.

[0036] In a third aspect, an embodiment of the present application further provides a computer device, comprising a memory and a processor, wherein the memory stores a computer program, and when the computer program is executed by the processor, the processor executes the steps of the online debugging method based on thread local variables as described in any one of the first aspects.

[0037] In a fourth aspect, an embodiment of the present application further provides a computer-readable storage medium storing a computer program. When the computer program is executed by a processor, the processor executes the steps of the online debugging method based on thread local variables as described in any one of the first aspects.

[0038] The embodiments of the present application have at least the following beneficial effects:

[0039] In the embodiment of the present application, for requests that require debugging, a debugging flag is set in the thread local variable, the object information data of the key nodes in the request process is saved, and returned together with the interface response message, so that the data changes in the entire request process can be tracked, online debugging can be achieved, and the problem can be quickly located. In addition, debugging is enabled only when the problem needs to be located, which does not affect normal business and has high security. BRIEF DESCRIPTION OF THE DRAWINGS

[0040] In order to more clearly illustrate the embodiments of the present application or the technical solutions in the prior art, the drawings required for use in the embodiments or the description of the prior art will be briefly introduced below. Obviously, the drawings described below are only some embodiments of the present application. For ordinary technicians in this field, other drawings can be obtained based on these drawings without paying creative work.

[0041] in:

[0042] Figure 1 A flowchart of an online debugging method based on thread local variables in one embodiment of the present application;

[0043] Figure 2 This is a diagram of the internal structure of a computer device in one embodiment of the present application. DETAILED DESCRIPTION

[0044] The following will be combined with the drawings in the embodiments of the present application to clearly and completely describe the technical solutions in the embodiments of the present application. Obviously, the described embodiments are only part of the embodiments of the present application, not all of the embodiments. Based on the embodiments in the present application, all other embodiments obtained by ordinary technicians in this field without creative work are within the scope of protection of this application.

[0045] For some complex functions, when problems occur online and need to be located, they are usually located through logs or tracking information. For performance reasons, business systems generally record key logs and tracking information, and sometimes this information is not enough to quickly locate the problem. If a function similar to online debugging can be implemented, the efficiency of location can be greatly improved.

[0046] Currently, the open source Arthas can achieve online debugging to a certain extent by deploying the Arthas package on the production machine and using the watch and trace commands, and can view the return value or exception of a method, trace the method call stack, etc. However, Arthas is implemented by using proxy classes to enhance the original bytecode and generate new classes to replace the normal classes of the JVM, which has an impact on all requests and may cause performance problems. At the same time, since it can directly replace online code, it may cause production accidents if used improperly. There are relatively few real online practice cases. For some systems with high security requirements, it is generally not allowed to deploy the Arthas package in the production environment.

[0047] Based on the above problems, the embodiments of the present application provide an online debugging method, device, computer equipment and readable storage medium based on thread local variables. For requests that need to enable debugging, a debugging flag debugFlag is set in the thread local (threadLocal) variable, and the object information data of the key nodes in the request process is saved and returned together with the interface response message, so that the data changes in the entire request process can be tracked, online debugging can be achieved, and the problem can be quickly located. And debugging is enabled only when the problem needs to be located, which does not affect normal business and has high security.

[0048] The technical solutions shown in the present application are described in detail below through specific embodiments. It should be noted that the following embodiments can exist independently or in combination with each other, and the same or similar contents will not be described repeatedly in different embodiments.

[0049] In a first aspect, an embodiment of the present application provides an online debugging method based on thread local variables. Figure 1This is a flow chart of an online debugging method based on thread local variables in one embodiment of the present application. Figure 1 In some embodiments, the online debugging method includes:

[0050] S110: receiving a request for debugging.

[0051] In some embodiments, the online debugging method based on thread local variables is applied to a backend server. Thread local variables, i.e. threadLocal variables, refer to variables that are confined to the thread, belong to the thread itself, are not shared between multiple threads, and can realize data sharing within the thread without affecting other threads. Java provides the ThreadLocal class to support thread local variables, which is a way to achieve thread safety.

[0052] In some embodiments, a user may enter a request in the front-end page of the device, such as an http request: http: / / abc.com / detai? channel=xxx, where channel is a request parameter. At the same time, a debug flag debugFlag is added to the request parameter to indicate that the request is a request that needs debugging. The request is then sent to the back-end server, so that the back-end server receives the request that needs debugging.

[0053] In some embodiments, the backend server may intercept requests through a Filter or other filters to successfully intercept requests that need to be debugged from multiple requests.

[0054] S120: Parse the request, and if the request contains a debugging flag, enable debugging.

[0055] After receiving the above http request through the front-end page, the back-end server parses the http request, mainly the request parameters in the http request. If the request parameter contains the debug flag debugFlag, debugging is enabled. If the request parameter does not contain the debug flag debugFlag, debugging is not enabled.

[0056] In other words, the debug flag debugFlag is equivalent to the debug switch.

[0057] S130: Setting a debugging flag and a debugging information set in a thread local variable.

[0058] After debugging is enabled, the backend server needs to set the debug flag debugFlag and debug information collection in the thread local variable (hereinafter referred to as threadLocal variable).

[0059] Specifically, set a ThreadLocal in the threadLocal variable<boolean>The debugFlag type variable indicates the debug flag. Set a ThreadLocal <List <debuginfodto>>The debugInfoList type variable represents a debug information collection. The debug information collection is used to store debug information.

[0060] Among them, the DebugInfoDTO type variable can be set to store content as needed, for example: logInfo represents the current step description, and content represents the object content to be returned.

[0061] For example: List <debuginfodto>The final result is as follows, which represents the object information of some key nodes in the current request process: obtaining content from the cache, performing verification, verification results, etc.

[0062] [{"logInfo":"1. Get the cache object","content":{"cacheObject":{"id":"sdg124","name":"534sdgsdg"}}},

[0063] {"logInfo":"2. Verify Category A qualification","content":{"verifyResult":false,"reason":"Time limit exceeded"}},

[0064] {"logInfo":"...","content":{...}}]

[0065] With this debugging information, it is easy to analyze the data changes and results during the request process and locate the problem.

[0066] S140: calling a debugging interface to save the data of the object information of the key node into a debugging information set.

[0067] The server executes the above http request and calls the debugging interface at the same time, and saves the object information data of the key nodes in the request process into the debugging information collection. The object information of the key nodes can be the object information of some key nodes that are very helpful for locating the problem.

[0068] Taking the example in step S130 as an example, the data saved in the debugging information set (ie, debugging information) includes the id of the cache object, the name of the cache object, the verification result: failure, the reason: the number of times exceeded, and the like.

[0069] S150: adding a preset field to the interface response message, where the preset field is used to indicate that a debugging information set needs to be returned, and returning the debugging information set and the interface response message.

[0070] During a normal request, the backend server will only return an interface response message, that is, the data returned by executing the http request. In addition to returning the interface response message, the embodiment of the present application also needs to return debugging information for the user to analyze and locate the problem. Therefore, it is necessary to add a preset field in the returned interface response message, and the preset field is used to indicate that a debugging information set needs to be returned. The backend server then packages the interface response message and the debugging information set and returns them together.

[0071] In some embodiments, the preset field may be any field as long as it has a mapping relationship with the returned debugging information set. For example, the preset field may be debgugInfo.

[0072] In some embodiments, since the interface response message is generally returned in the form of a json string, the backend server needs to convert the data of the object information into a json string and save it in the debugging information set, so as to package the interface response message and the debugging information set and return them together.

[0073] In combination with the above, for requests that require debugging, this application sets a debug flag debugFlag in the thread local variable, saves the object information of key nodes in the request process, and returns it together with the interface response message, so that data changes in the entire request process can be tracked, online debugging can be achieved, and problems can be quickly located. And debugging is enabled only when problems need to be located, which does not affect normal business and has high security.

[0074] In some embodiments, for security reasons, before debugging is enabled, a signature or encryption may be added and a valid time may be set, or access may be restricted to the intranet only. Therefore, before debugging is enabled, the online debugging method may further include:

[0075] Get the pre-saved random string, the incoming timestamp of the incoming request, and the incoming random string;

[0076] Calculate the hash value of the sum of the pre-saved random string, the incoming timestamp, and the incoming random string;

[0077] Compare the hash value to the signature value of the incoming request;

[0078] If the hash value is equal to the signature value passed in the request, debugging is enabled.

[0079] In this embodiment, a simple and efficient hashing method is used to obtain the random string R pre-saved by the server, the incoming timestamp T when the request is incoming, and the random string S passed in when the request is incoming. The hash value of the sum of the pre-saved random string R, the incoming timestamp T, and the incoming random string S R+T+S is calculated, and the calculated hash value of R+T+S is compared with the signature value sign when the request is incoming. If the hash value of R+T+S is equal to the signature value sign when the request is incoming, the verification is passed once, and debugging is allowed to start.

[0080] In some embodiments, before starting debugging, the online debugging method further includes:

[0081] If the hash value is equal to the signature value of the incoming request, calculate the difference between the incoming timestamp and the server timestamp. The server timestamp indicates the time when the server received the request.

[0082] If the difference is within the preset time range, debugging is started.

[0083] After the first verification is passed, a second verification can be performed. Specifically, if the hash value of R+T+S is equal to the signature value sign when the request is passed, the difference between the incoming timestamp T when the request is passed and the server timestamp T1 when the server receives the request is verified. Under normal circumstances, a difference of 5s is allowed. If the difference between T and T1 is within 5s, the second verification is also passed, and debugging is allowed to start. In this way, the sign value will automatically become invalid. As long as the judgment rule of R+T+S and the random string R pre-saved by the server are not leaked, it is safe.

[0084] In some embodiments, before starting debugging, the online debugging method further includes:

[0085] Configure the debugging network segment through the nginx allow directive to limit the network segment where debugging methods are executed.

[0086] That is, if you want to restrict content access, you can use the nginx allow directive to configure the debugging network segment to restrict it. For example, if you restrict that the debugging method can only be executed in the intranet, you can use the nginx allow directive to configure the intranet segment to restrict it to further improve security.

[0087] In some embodiments, the online debugging method further includes:

[0088] Add prompt information in debugging information.

[0089] Taking the example in step S130 as an example, 1. obtaining cache objects and 2. verifying the eligibility of category A are prompt information, which is equivalent to annotations. Through the prompt information, the user can know the specific content of the debugging information, which is convenient for analyzing and locating problems. It can be understood that if the prompt information is relatively complete, it is not even necessary to analyze the data of the object information of the key nodes in the debugging information. The problem can be directly located through the prompt information to improve efficiency.

[0090] In some embodiments, the added prompt information also serves to facilitate displaying the returned object information data in the order of steps, so as to facilitate the user to analyze and locate the problem. For example, it can be displayed as the following table:

[0091]

[0092] In some embodiments, the online debugging method further includes:

[0093] If a business thread pool or other asynchronous method is used, the cross-thread transmission of debug flags and debug information sets is achieved through the inheritable thread local variable inheritableThreadLocal or the open source transmittable thread local variable TransmittableThreadLocal.

[0094] Both the business thread pool and the asynchronous method use multiple threads to process requests. If the backend server uses the business thread pool or other asynchronous methods to process http requests, the threadLocal variable is not enough to ensure that the debug flag is effective throughout the request process. The debug flag and debug information collection can be transferred across threads through inheritableThreadLocal or the open source TransmittableThreadLocal to ensure that the debug flag and debug information collection can be successfully transferred to the next thread, thereby ensuring that the entire request process can be debugged, and then the object information data of the entire request process can be obtained, which is conducive to the comprehensiveness of problem positioning.

[0095] In a second aspect, an embodiment of the present application further provides an online debugging device based on thread local variables, comprising:

[0096] A receiving module, used for receiving a request requiring debugging;

[0097] The parsing module is used to parse the request and enable debugging if the request contains a debug flag.

[0098] The setting module is used to set the debug flag and debug information collection in the thread local variables;

[0099] The debugging module is used to call the debugging interface and save the data of the object information of the key node into the debugging information set;

[0100] The return module is used to add a preset field in the interface response message, the preset field is used to indicate that a debugging information set needs to be returned, and the debugging information set and the interface response message are returned.

[0101] In a third aspect, an embodiment of the present application further provides a computer device, comprising a memory and a processor, wherein the memory stores a computer program, and when the computer program is executed by the processor, the processor executes the steps of the online debugging method based on thread local variables as described in any one of the first aspects.

[0102] Figure 2 The internal structure diagram of a computer device in one embodiment of the present application. Figure 2 In some embodiments, the computer device includes a processor, a memory, and a network interface connected via a system bus. The memory includes a non-volatile storage medium and an internal memory. The non-volatile storage medium stores an operating system and may also store a computer program. When the computer program is executed by the processor, the processor may implement any of the steps of the online debugging method based on thread local variables. The internal memory may also store a computer program. When the computer program is executed by the processor, the processor may implement any of the steps of the online debugging method based on thread local variables. Those skilled in the art will understand that Figure 2 The structure shown is merely a block diagram of a portion of the structure related to the solution of the present application, and does not constitute a limitation on the computer device to which the solution of the present application is applied. The specific computer device may include more or fewer components than those shown in the figure, or combine certain components, or have a different arrangement of components.

[0103] In a fourth aspect, an embodiment of the present application further provides a computer-readable storage medium storing a computer program. When the computer program is executed by a processor, the processor executes the steps of the online debugging method based on thread local variables as described in any one of the first aspects.

[0104] Those skilled in the art can understand that all or part of the processes in the above-mentioned embodiment methods can be completed by instructing the relevant hardware through a computer program, and the computer program can be stored in a non-volatile computer-readable storage medium. When the program is executed, it can include the processes of the embodiments of the above-mentioned methods. Among them, any reference to memory, storage, database or other media used in the embodiments provided in this application can include non-volatile and / or volatile memory. Non-volatile memory may include read-only memory (ROM), programmable ROM (PROM), electrically programmable ROM (EPROM), electrically erasable programmable ROM (EEPROM) or flash memory. Volatile memory may include random access memory (RAM) or external cache memory. As an illustration and not limitation, RAM is available in many forms, such as static RAM (SRAM), dynamic RAM (DRAM), synchronous DRAM (SDRAM), double data rate SDRAM (DDRSDRAM), enhanced SDRAM (ESDRAM), synchronous link (Synchlink) DRAM (SLDRAM), memory bus (Rambus) direct RAM (RDRAM), direct memory bus dynamic RAM (DRDRAM), and memory bus dynamic RAM (RDRAM).

[0105] The technical features of the above embodiments may be combined arbitrarily. To make the description concise, not all possible combinations of the technical features in the above embodiments are described. However, as long as there is no contradiction in the combination of these technical features, they should be considered to be within the scope of this specification.

[0106] The above embodiments only express several implementation methods of the present application, and the descriptions thereof are relatively specific and detailed, but they cannot be understood as limiting the scope of the present application. It should be pointed out that, for a person of ordinary skill in the art, several modifications and improvements can be made without departing from the concept of the present application, and these all belong to the protection scope of the present application. Therefore, the protection scope of the present application shall be subject to the attached claims.< / debuginfodto> < / debuginfodto> < / boolean>

Claims

1. An online debugging method based on thread local variables, characterized in that: The method comprises: Receive requests for debugging; Parsing the request, and if the request contains a debugging flag, enabling debugging; Setting the debugging flag and debugging information set in thread local variables; Calling the debugging interface to save the data of the object information of the key node into the debugging information set; A preset field is added to the interface response message, wherein the preset field is used to indicate that a debugging information set needs to be returned, and the debugging information set and the interface response message are returned.

2. The online debugging method based on thread local variables according to claim 1, characterized in that: Before starting debugging, the method further includes: Get the pre-saved random string, the incoming timestamp of the incoming request, and the incoming random string; Calculate the hash value of the sum of the pre-saved random string, the incoming timestamp, and the incoming random string; Compare the hash value to the signature value of the incoming request; If the hash value is equal to the signature value of the incoming request, debugging is enabled.

3. The online debugging method based on thread local variables according to claim 2, characterized in that: Before starting debugging, the method further includes: If the hash value is equal to the signature value of the incoming request, the difference between the incoming timestamp and the server timestamp is calculated, where the server timestamp indicates the time when the server received the request; If the difference is within a preset time range, debugging is started.

4. The online debugging method based on thread local variables according to claim 1, characterized in that: Before starting debugging, the method further includes: The debugging network segment is configured through the allow instruction of nginx to limit the network segment for executing the debugging method.

5. The online debugging method based on thread local variables according to claim 1, characterized in that: The method further comprises: Add prompt information to the debugging information.

6. The online debugging method based on thread local variables according to claim 1, characterized in that: The receiving a request requiring debugging includes: Use Filter to intercept requests.

7. The online debugging method based on thread local variables according to claim 1, characterized in that: The step of setting the debugging flag and the debugging information set in the thread local variable includes: Set the boolean type variable debugFlag and the DebugInfoDTO type variable debugInfoList in the thread local variables, debugFlag as the debugging flag, and debugInfoList as the debugging information collection.

8. The online debugging method based on thread local variables according to any one of claims 1 to 7, characterized in that: The method further comprises: The data of the object information is converted into a json string and saved in the debugging information collection.

9. The online debugging method based on thread local variables according to any one of claims 1 to 7, characterized in that: The method further comprises: If a service thread pool or other asynchronous methods are used, the cross-thread transmission of the debugging identifier and the debugging information set is achieved through inheritable thread local variables or open source transmittable thread local variables.

10. An online debugging device based on thread local variables, characterized in that: The device comprises: A receiving module, used for receiving a request requiring debugging; A parsing module, used for parsing the request, and if the request contains a debugging flag, enabling debugging; A setting module, used for setting the debugging flag and debugging information set in the thread local variable; A debugging module, used to call a debugging interface and save the data of the object information of the key node into the debugging information set; The return module is used to add a preset field in the interface response message, wherein the preset field is used to indicate that a debugging information set needs to be returned, and return the debugging information set and the interface response message.

11. A computer device, characterized in that: The invention comprises a memory and a processor, wherein the memory stores a computer program, and when the computer program is executed by the processor, the processor executes the steps of the online debugging method based on thread local variables as claimed in any one of claims 1 to 9.

12. A computer-readable storage medium, characterized in that: A computer program is stored, and when the computer program is executed by a processor, the processor executes the steps of the online debugging method based on thread local variables as claimed in any one of claims 1 to 9.