In this article, we first throw out the source code reading methods, and then discuss how to use these techniques in detail based on the Sentinel creation process. Finally, we answer some misunderstandings of source code reading.
Sentinel series consists of 15 articles, which mainly analyze the architectural design concept and core implementation key points of Sentinel by means of source code analysis, and analyze and think about them in practice.
Many friends are asking me how to read the source code. This can be summarized as the following points, and then combined with Sentienl source analysis column for each point to disassemble, the source code reading method for a “combat”.
- Read the official documents to know what kind of problems the framework to be studied can solve, what the overall architecture design and ideas are, and what main points are included.
- Start from the official Demo program, learn the basic use method, further deepen their understanding, and look for opportunities to find a breakthrough.
- Look for a breakthrough, break through one by one, first main branch and then side branch, moderate decomposition, each broken.
Next I will show how I used this methodology to learn Sentinel.
Before preparing for further study of Sentinel, WE first carefully read the official documents of Sentinel, thus forming a basic understanding of Sentinel. We can learn the core content of Sentinel from the official documents, as shown in the following figure:
These contents can be simply as a learning outline, from which you can choose their interest to focus on learning.
After reading the official documentation, I set out to create the Sentinel series.
This article will not introduce the details of each knowledge point in detail, the specific explanation of each knowledge point, we can click on the links interested in in-depth study, this article is mainly to the point, focusing on the author is how to learn the source code.
1. Preliminary Study on Current limiting and Fusing of Alibaba Sentinel This article is mainly carried out from the following points:
- What is Sentinel? What are the main problems that can be solved?
- Usage scenarios of current limiting and fusing
- Sentinel source code structure
- Run Sentine Demo in IntelliJ IDEA
The third point is not necessarily necessary, but it is not difficult to draw these conclusions because it is easy to draw conclusions based on the global knowledge of Sentinel and the naming of each module. The fourth point here is very important. Usually a good open source framework will provide a complete Demo. You can see the Sentinel Demo is very rich, and I chose Dubbo as an example in this article. This will be more close to the actual combat, more conducive to finding a breakthrough.
There are three main purposes for running Demo:
- By running Demo, understand the basic use of the framework.
- Build a debuggable environment for subsequent debugging when you cannot understand the code. According to the runtime data, you can speed up the understanding of the code, but do not start debugging.
- Find the source code to read the entry.
2, source analysis of The principle of Sentinel Dubbo adaptation follows the first article, since the use of Dubbo as its example code, it is natural to think about how Sentinel is to achieve Dubbo adaptation and non-invasive business.
Through the study of this article, we learned that the adaptation of Dubbo can be realized through the extension mechanism of Dubbo. In Dubbo Filter, we can see the core API SphU. Entry related to Sentinel. In this way, we can find the core entrance for further study of Sentinel. In other words, we will open the door to the world of Sentinel kernel through the study of this method in the subsequent articles.
Note: When reading this article, I felt that the adaptation of Dubbo was very simple. However, with my in-depth study of this series, I found that this method was not so simple. Many points were not fully understood at that time, which will be elaborated in the follow-up, which is also the benefit brought by continuous learning and continuous thinking.
3,Looking for a key to Alibaba Sentinel
In this paper, the implementation process of SphU. Entry method was followed in detail to reveal the key points of its implementation. Sure enough, the core operation mechanism of Sentinel was found through tracking the process of this method: Slot processing chain.
The names of these slots will tell you a lot about how they work, and the focus of future research will be on how they work.
4. Implementation principle of Sentinel call context Environment According to the chain of the above Slot, the NodeSelectorSlot was studied. Through learning, we learned that the NodeSelectorSlot is mainly used to construct the call chain of Sentinel. That is, call context management, which is precisely the entry node to build the call chain. In Sentinel, the corresponding node will store the call information of each resource in real time.
5. Principles of Sentinel Real-time Data Acquisition
- Detailed explanation of real-time acquisition data
- Sentinel sliding window implementation principle
If the Slot chain is executed in the same order, the next ClusterBuilderSlot is named ClusterBuilderSlot, which is related to cluster traffic limiting, but follows the learning strategy of “simplify first, then difficult”. In this case, it will skip the learning of the cluster traffic limiting first, and then go back to learn about cluster traffic limiting later.
In order to realize the functions of current limiting and fusing, the first problem to be solved is how to collect the service (resource) call information in real time. For example, if the current limiting width of an interface is set to 1W/ TPS, how to determine the current TPS first? Alibaba Sentinel adopts sliding window to realize real-time data statistics, and its implementation class is StatisticSlot.
After understanding the real-time data acquisition principle of Sentinel, the implementation of flow limiting is very simple. It is to call the information based on the collection and compare it with the flow limiting rules to determine whether the flow limiting is needed. Sentinel also provides a variety of processing strategies after triggering the flow limiting. Such as fast failure, uniform queuing, warm up, etc.
But I’m trying to study current limit of time, I will current-limiting core logic and the trigger current limiting the processing strategy of decomposition, while learning to current limiting, chose the most simple processing strategy (uniform line), will be difficult to preheating system decomposition, a single up the article to learn again, so the dismantling of guaranteeing learning single article unavailable, and enhance their own “production”, Increase your sense of accomplishment.
This section mainly includes the following four articles:
- Implementation principle of Sentinel FlowSlot current limiting
- Implementation principle of RateLimiter SmoothBursty
- Implementation principle of RateLimiter SmoothWarmingUp
- Sentinel uniform queuing and preheating principle and practical suggestions
The core algorithms in the field of lower limit flow, such as leaky bucket algorithm and funnel algorithm, are also emphasized here. In addition, the preheating mechanism of Sentinel mainly refers to the implementation of Guava, so the realization principle of Guava’s RateLimite is carefully studied here.
It is simple to compare the current call information with the fusible rules. If it meets the fusible rules, an exception is thrown. If only the nature of the implementation of circuit breaker itself is indeed simple, but in combination with the reality, there are more problems to be considered, which will be reflected repeatedly in the later practice chapters, and it is found that Sentinel is relatively rough in the implementation of circuit breaker.
After the previous articles, Sentinel has learned the basic single-machine current limiting and fusing. Now it is time to start thinking about how to use Sentinel. However, the fusing and current limiting rules of the Sentinel official background operation and maintenance management system can only be stored in memory and cannot be directly used in production environment. Therefore, a solution needs to be proposed. This paper introduces in detail how the author conducts research on the method of dynamic data source configuration based on official data. Completion is considered according to the standards of the architectural design solution at work and is highly recommended.
9,Current Limiting and Fusing of Sentinel Dubbo Adapter
With support for dynamic data sources, I went on to think about how circuit breakers are being introduced in microservices. After some reflection, I found that the granularity of the official Dubbo adapter is at the service level, with no control over the machine level, as shown in the following figure:
For example, the current Sentinel Dubbo adapter does not currently support a service where one of the service providers is slow and requests sent to that machine are fusing. The current implementation version will fuse all requests that call this service, so the official Dubbo adapter needs to be improved. With their own thinking will understand more knowledge, so through learning source code, must not “letter book”, to have their own thinking and doubt ability, so as to make a certain contribution to the open source community, common progress and growth.
The above is my method of learning source code, I hope to really help you and feeling.
Finally, I’ll talk about answering a few questions about reading source code.
1, look at the source will forget? Why is it that when we look at the source code, we feel like we understand it, but we forget it so easily?
Always understand that looking at the source code is just a means to thinking about the design principles of the framework, understanding implementation details and guiding practice through the source code. Focus on thinking. Of course, the implementation of details after seeing easy to forget, but as long as the understanding of the idea, in need of use can see their own written articles, suddenly can pick up.
2, look at the source code can be directly used to Debug learning
I strongly advise against doing this because you get lost in the details. The correct posture is to look for the entrance and comb through it with our own thinking. When we cannot read the source code or understand its ideas, we can use Debug to help us understand it faster through the visualized data at runtime.
Well, that’s all for this article. Your likes and retweets are the biggest encouragement for me to continue to output high-quality articles.
Welcome to add the author micro signal (DINGwPMZ), add group discussion, the author quality column catalog: 1, source analysis RocketMQ column (40 +) 2, source analysis Sentinel column (12 +) 3, source analysis Dubbo column (28 +) 4, source analysis Mybatis column 5, source analysis Netty column (18 +) 6, source analysis JUC column Source code analysis (MyCat) for Elasticjob