In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-16 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Servers >
Share
Shulou(Shulou.com)06/01 Report--
This article introduces what are the practical skills of reading the source code in DevOps, the content is very detailed, interested friends can refer to, I hope it can be helpful to you.
I. preparatory stage
1. Make a plan
Reading source code is the same as reading, you must have a time plan, deadline is * productivity.
Making a reasonable plan may require you to have a global grasp of the structure of the code, as well as the importance and difficulty of each function.
two。 Choose a good book
As a study material, the book must be *.
The information on the Internet is so fragmented that it may not be systematic to learn.
More systematic books will generally explain the configuration and integration of open source code in detail; then introduce some general modules, and then track and analyze the code of each component or process.
When choosing books, we should also pay attention to the choice.
Through the introduction of each chapter of the book, we can see whether the book is told in accordance with this logic.
For example, when I was learning openstack, I chose such a book according to the directory: his four pages are basic-installation-code-secondary development. This is a very good step by step book.
3. Choose a good IDE
The author uses Vim except Java. Of course, it all depends on everyone's habits.
I generally prefer lightweight IDE, so I recommend some lightweight ones:
Vim
Sublime
SourceInsight
The full name of IDE is integrated development environment. If you just want to get the code running, you only need a compiler or interpreter. The code can be edited in plain text.
IDE provides more development aids, allowing developers to focus on the logic of the code.
The most common functions such as automatic error correction, code completion, function query and so on. Many IDE of C also generate makefile automatically, which saves a lot of tedious content.
However, the functionality and minimalism of IDE is always a paradox.
When choosing IDE, readers should choose an IDE that meets their needs and should not pursue powerful functions too much.
In general, what are the auxiliary needs we might have when using IDE:
Integration of testing tools
Automatic packing
Code positioning
Customized, rich plug-ins
Error check
Debug
Project template
4. Download the full version library
A complete code base refers to each historical version that reflects the iterative process of the code. This has many benefits:
You can get a record of changes to the code.
You can also get the complete test code, and when you want to submit patch, you can use version management tools to generate patch for different versions.
Second, first acquaintance code
1. Read the project documentation
Most open source projects will have a certain description of their architecture, read through will give you a more in-depth understanding of the project.
Focus on documents such as Getting started and Example to learn the knowledge needed to download, install, and use the project.
For example, openstack, the network topology explanation on the official website is the most comprehensive and accurate:
two。 Classification file
Distinguish the role of each file in the code base.
At the right time, having an overall grasp of all the files will help you choose the priority when you read the code later. Know what is the core and which can be customized.
The following is part of the author's collection of nova source files:
/ nova/api/auth.py: middleware for general authentication, accessing keystone
/ nova/api/manager.py:Metadata Management initialization
/ nova/api/ec2/__init__.py:Amazon EC2 API binding, the starting point for routing EC2 requests
/ nova/api/ec2/apirequest.py:APIRequest class
/ nova/api/metadata/__init__.py:Nova metadata service
. . .
. . .
When I first started to write notes, I was not sure about some things myself. Such comments do not ultimately give you a very clear understanding of the relationship between all the files in the code.
But it doesn't matter, it's good to try to do such a thing in the early days, which may be a step for you to master the overall structure of the source code.
3. Master the development framework
The purpose of the framework is to simplify development. But it also makes the code less intuitive.
For example, for many open source software developed in spring, if you don't even know spring, you will find that you can't even find the code entry. Because the code under the development framework is "hijacked"!
Let's take, for example, when spring+springmvc+mybatis develops web applications. If you understand these three basic frameworks, you can clearly understand the role of the following files:
All controller corresponding to url are in com.dc.controller.
All the data interfaces are in com.dc.dao.
All entity objects are in com.dc.entity
All data interfaces and sql statements correspond to com.dc.dao.mapper.
All service definitions are in com.dc.service and com.dc.service.Impl
More specifically, when I see a function like this:
You will immediately know that it is a controller function in spring when dealing with url paths.
So, if you are sure that open source code uses a mature development framework, be sure to be familiar with it first.
Third, be familiar with code behavior
1. Component execution process
The more complex systems are sub-components, familiar with each component and sort out the relationship between them.
For example, the execution flowchart of openstack:
The virtual machine startup process is as follows:
a. The interface or command line obtains authentication information from keystone through RESTful API.
B. keystone requests authentication information through the user, and generates auth-token to return to the corresponding authentication request.
c. The interface or command line sends a request for boot instance (with auth-token) to nova-api via RESTful API.
D. Nova-api sends an authentication request to keystone after accepting the request to see if token is a valid user and token.
two。 Using sample code and unit tests
The sample code can help you learn to use the API of the relevant open source projects.
In the process of development, most open source projects write a lot of unit test code in order to verify the function of their implementation. This code is actually very good sample code.
There are too many benefits of reading unit tests. Here is a list of the benefits summarized by Zhihu netizens:
Since a unit test is usually just a few hours of development work, you can easily read the relevant code.
Each unit test can be run independently, saving you time to track and debug.
Unit testing defines the function of the software to a large extent and can help you quickly grasp the relevant API of the project.
If you modify the code of an open source project, you can verify that your changes are correct by modifying the unit test.
Note 1: original text link https://www.zhihu.com/question/19637879/answer/13545260
If the project provides off-the-shelf example projects:
First, try to run example as described in the starting document. If it goes well, congratulations on getting off to a good start; if you encounter a problem, first try to find the answer in the project's FAQ and other documents.
Third, the problem (such as abnormal information) can be searched as keywords to find relevant solutions, you encounter, others will generally encounter, enthusiastic friends will record the process of solving.
You can submit the question to the mailing list of the project. Please take a look at it for you. Do not attempt to modify the example until you have successfully run it.
After running * example, try to modify example according to your understanding and needs, test advanced features, etc.
3. Tracking and analysis
Almost none of the complex open source software is a process to the end, at this time we need to choose a major process.
For example, in openstack, I did a lot of work at the beginning to sort out the process of creating a virtual machine.
* * step, make a comment line by line along the process of creating a virtual machine from the code entrance:
When the basic notes of the process are completed and I have mastered it, I will peel off the cocoon and sum up a more intuitive and brief way of expression:
When you gradually understand one or two major processes, you will generally find that the other branch processes are very similar. This lays a good foundation for mastering the whole process.
4. Sort the functions that need to be known in detail
When arranging your own in-depth reading, you should make reasonable arrangements according to the estimated workload.
Generally speaking, initialization, reading parameters, and so on are secondary and relatively simple. The core module is much more complex.
Or take haproxy as an example, the core code in the main function is run_poll_loop ().
The author has tried to start with the init () function, but found that a lot of initialized data simply do not know what to do, after reading it all can not remember anything.
However, starting directly from the main function, it is easier to understand when it is constantly found that when some parameters are processed, it is easier to reverse trace its initialization process.
4. Master the status of data
1. Master data flow
From the point of view of data flow, all code logic is processing data.
For example, openstack, starting from the virtual machine name, configuration and other data entered by the initial user, the code logic of openstack processes, processes, filters and selects the data, and finally passes it to libvirt to create the virtual machine.
Therefore, mastering the organization of data is very helpful to understand the logic of the code, and it is also a prerequisite for secondary development.
So, when learning the code, keep asking yourself, do I have a grasp of how the data is organized? Do I have a grasp of the process in which the data is constantly processed throughout the process?
two。 Use debug to observe the status of data
As mentioned earlier, when you study a certain process in detail, you must pay attention to the way and content of data transmission. Data flow is the basis of understanding the process, and expanding data flow is also a common skill in secondary development.
For example, in eclipse, get the data content of a point in the process through the debug break point:
3. Observe the status of the data using standard output
The author sometimes likes to directly use console to output and print some information about the object. This is also useful when verifying whether a piece of code has been executed or when looking at some data.
For example, view the output of javascript through the standard output of chrome:
5. Three stages of inference
1. Study the underlying calls
Studying the underlying invocation is often a common means of operation and maintenance. For example, in the early days of openstack, we are not familiar with the source code of openstack. What should we do?
Directly study the underlying calls to openstack. At the bottom of Openstack, we have studied all the basic operations such as calling libvirt and creating virtual machines.
Therefore, for most openstack problems, we can go straight to the problem site, recover and troubleshoot.
So what are the benefits of studying the underlying calls to understand the source code?
After the author is basically familiar with all the underlying calls of openstack, with such a question to see the source code "how on earth does the code run gradually from the entrance to the underlying call that I know?", the author quickly combs the execution process of the code.
two。 Learn to ask questions in the community or stackoverflow
There are quite a few warm-hearted people in the community.
Of course, the author believes that asking questions also requires certain skills. Here is a quote from netizens of Zhihu:
Many people in stackoverflow have one thing in common when asking questions, that is, they first express their views on the questions raised, describe their ideas, and where they have reached, which is a respect for the respondents.
You are explaining how far you can go, and you have shown what efforts you have made, which is your sincerity to the problem.
In this way, the respondents will feel the value of the answer, maybe it is to think of the years when they have worked hard for rookies in the past, or to think that you are worth helping, or so on. The so-called self-help is just help.
Original link: http://www.zhihu.com/question/24228283/answer/27102646
3. Learn to draw a flow chart
The flowchart can more easily show the logic of code execution. Ignore the unimportant code and emphasize the main functions.
Block diagrams commonly used in summary design:
Mind mapping:
This is the end of the practical skills of reading the source code in DevOps. I hope the above content can be of some help and learn more knowledge. If you think the article is good, you can share it for more people to see.
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.