In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-02-24 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/03 Report--
This article mainly explains "how to read the source code efficiently". Friends who are interested might as well take a look. The method introduced in this paper is simple, fast and practical. Let's let the editor take you to learn how to read the source code efficiently.
1. Why read the source code? 1.1 improve technical capability in general-purpose basic technology
In the field of JAVA, including JAVA collection, Java concurrency (JUC) and so on, they are high-frequency technologies used in projects. Selecting appropriate data structures, thread concurrency models and reasonable control of lock granularity in a variety of complex scenarios can significantly improve the availability and robustness of applications, and it is very easy to highlight their own technical strength, more likely to be recognized by leaders and help the workplace.
Of course, reading the source code is not the only way to know the principle, but as a famous programmer, facing the code, it may be more direct to experience the charm of the code.
1.2 create your own highlights in key areas
My company uses Dubbo and RocketMQ, and I have the honor to participate in the application and operation of these technology stacks, and have accumulated rich experience in using them. In order to highlight the advantages in these two fields, I have read their source code in detail. I have published a large number of technical articles on knowledge sharing platforms such as CSDN and official accounts, systematically analyzing their implementation principles, architecture design concepts, and combining theory with actual combat. Let me become an unyielding technical expert in the field of Dubbo and RocketMQ, and the core backbone of the team.
At the same time, because the article is systematic, chosen by the publishing house and invited to publish a book, the book "inside RocketMQ Technology" arises at the historic moment, which becomes a very bright business card in my professional skills list, forms a recognized technical influence, and has a certain ability of "brand premium".
Of course, you can also wait until something goes wrong to look at the source code. "input-output ratio" is higher, but this is a passive process. If the concurrency of the production environment is not high, you may not encounter real problems for one or two years, and the accumulation of experience is very slow. If you have worked for 4 or 5 years, you may not be able to compete with people who have worked for 2 or 3 years.
So if you want to quickly create a bright spot, you still need to take the initiative to read the source code and systematically master its design concept and implementation principle.
1.3 learn design and coding from excellent source code
The process of learning programming is actually a process of imitation, excellent source code are master works, very nutritious, you can see how the masters abstract interfaces, how to apply SOLID principles, as well as a lot of very useful programming skills.
For example, JUnit builds a system from patterns, in which you can see a large number of applications of design patterns. These are living cases, which are better than looking at those design pattern theories or simple examples.
2. How to read the source code
Based on years of reading experience, I have sorted out this set of methods:
Understand the usage scenarios of this software and the responsibilities that will be assumed in the architectural design.
Look for official documents to grasp the design concept of this software as a whole.
Build your own development and debugging environment and run the official Demo examples to lay the foundation for further research.
First trunk process and then branch process, pay attention to cutting, break one by one.
Next, I would like to share some of my experience in reading the RocketMQ source code to make the above theory as graphic as possible.
2.1 understand the application scenarios of RocketMQ
The usage scene of MQ is relatively clear, and its two basic duties are decoupling and peak cutting and valley filling.
Take the simplest scenario: when new users sign up to send points and coupons, the original architecture is usually designed as follows:
It can be seen that user registration and issuing coupons, sending points are tightly coupled. With the continuous development of the business, the activity department proposes that during the Spring Festival, users register not to send points, but to send coupons, but to give away a New year gift package. If based on the above architecture, it is necessary to change the main process of user registration, which violates the design concept of closing to modification and opening to expansion in the design pattern.
The emergence of MQ can solve the above problems very well:
By reading through the official documents, we can not only get the overall context of MQ (such as NameServer routing discovery, message sending, message storage, message consumption, message filtering), but also be interested and curious about the advanced features of "high-end atmosphere", such as sequential consumption, zero copy, synchronous flushing, asynchronous flushing, etc., driving us to read its source code and explore its implementation details. It makes it possible for us to do some self-thinking in reading the source code.
2.3 build a development and debugging environment
Different systems are built in different ways. I have a hand-in-hand article here to teach you how to install RocketMQ and IDEA Debug environment.
2.4 trunk first, then branch
After setting up the local development environment, do not directly use Debug to track the overall process of message delivery, because this process is too long. From a coarse granularity, the process is shown in the following figure:
After such a decomposition, we can focus on understanding the design principle of a piece, and the continuous time required can be greatly reduced, "eat" in a mouthful, and finally complete the understanding of the whole system.
This also brings an additional benefit: the output of multiple articles in batches is increased, and the sense of achievement is improved.
3. It's easy to give up reading the source code. What should I do?
Reading the source code is boring, a person alone is easy to give up, especially when there is a problem, how can we stick to it and finish it?
My answer is to insist, but allow a short rest, stay, and then repeatedly sprint, fight, until the capture of this "mountain".
Because once you give up, all your previous efforts will be wasted, and once you break through, your own ability can get a qualitative leap.
I encountered difficulties when reading the Netty source code. At that time, I just finished writing the memory leak detection of Netty, and I was ready to begin to study the memory allocation mechanism. This piece is very abstract, involving a complex data structure, and I need to master how to map between binary trees and arrays, involving a large number of bit operations, etc., which makes it difficult for me to explore its principle. Give up?
When you can't make a breakthrough for a week or two, it's easy to think: is it a waste of time to invest time continuously with no return?
In fact, I thought about giving up, but then I thought: what will I do after giving up? Play a game? Watch TV?
Since it's playing games and watching TV, isn't it a waste of time? After thinking clearly about this layer, continue to tackle key problems, continue to break through has become the only choice.
Of course, when you encounter difficulties, it is also very necessary to relax occasionally for a day or two and readjust your state. A short break can make us less anxious and help us reorganize our thinking and start a new sprint.
When we encountered a difficult problem at that time, what measures did we take to help us overcome it?
1. Call for help from "du Niang"
When reading the source code, you can directly COPY a small section of the code to Baidu to search, there may be Daniel has done an interpretation of these codes, can play a guiding role.
After my search at that time, I found that the memory allocation algorithm of Netty is not the first, but the implementation of the jemalloc algorithm. By consulting the relevant technical documents, we can understand the memory allocation algorithm of Netty as a whole.
2 、 Debug
Netty pursues the extreme performance and uses a large number of bit operations. because bit operations are rarely used in normal work, it seems more laborious. In order to make up for this deficiency, it is easier to understand bit operations by using DEBUG mode combined with runtime data.
After 10 days of the above efforts, I slowly unraveled Netty's memory allocation mechanism, and after breaking it down, I wrote a series of articles describing Netty memory allocation:
Among them, 192.168.1.3 this machine was overloaded for a period of time, the response time was too long, and 30% of the requests sent to it failed.
And 30% is precisely the circuit breaker error rate set by Sentinel, so Sentinel thinks that the entire "user information search" service is unavailable and is cut off.
This is obviously unreasonable because the 192.168.1.4 and 192.168.1.5 machines are still alive!
This is essentially because the circuit breaker error rate is defined to the service level: service-> circuit breaker error rate
When thinking about this problem, I think that when configuring circuit breaker rules, we need to refine, and the circuit breaker error rate should be defined to the resource group level: [service, machine]-> circuit breaker error rate.
In this way, when the service provider on one machine is fused, it does not affect other machines, ensuring true high availability.
Through contact with the authorities, my idea was affirmed by its author, which in turn promoted the development of the community.
At this point, I believe you have a deeper understanding of "how to read the source code efficiently". You might as well do it in practice. Here is the website, more related content can enter the relevant channels to inquire, follow us, continue to learn!
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.