Ao Bing, I wrote a low-level bug that no novice can write, and I was scolded badly.

https://mp.weixin.qq.com/s/yB9s771gDz6oMKZsUnJuyg

Ao Bing, I wrote a low-level bug that no novice can write, and I was scolded badly.

Included in topics

# Programmer  1568  # program Life  669

Foreword

image.png

Why is there a preface today?

Because your Bingbing had a card yesterday, it was directly recommended by WeChat official, knowing the recommendation, it is just okay (in my heart).

There were also several account owners who sent greetings, directly envied by their real names, haha, Bingbing will also be a man recommended by WeChat in the future, and you are also readers with brand names.

image.png

Preface

Do you think that Bing Bing is so busy that he will definitely not write technical articles for you to see?

Bingbing Nuan came, but to be honest, I guess there was not much time to write technical articles before. You saw two articles last weekend. It was either the annual meeting or the summit. It was really busy.

This week is also the same. We rushed to the project at the end of the year. Today, I will list the next Q plan. My dear, an average demand is three days. What the hell.

And there will be an annual meeting of the technical department on Thursday. I am still the director of the video (haha haha). I definitely need to confirm a lot of details. Then I should be able to show you an article on the weekend (spoilers will be available in the next issue) My goddess is in the mirror).

I will go home by car on Tuesday next week, and the wage earners will go home for the Chinese New Year. I also asked my mother to release the news that I am going home. It is estimated that the matchmaker is already on the way, and I will be able to get a blind date. Now, there are so many materials for the New Year, so happy.

This is a major before I talk about a stupid bug, originally just going to let him stay in my notes inside, but still could not help but want to share it, so that we avoid such a stupid mistake ( in fact, I think a water Some articles with a little technical content, so as not to write N articles all about daily life, you probably want to Diss me again ).

text

Let me first describe the bug phenomenon:

image.png

The values ​​of these two input boxes are obtained from KV (storage middleware such as Redis), which can also be modified in real time. I am smart, thinking that if there is no value in KV, I will take a static variable by default. There is a bottom line in this way. Static variables are loaded when the class is loaded, and it is more efficient when I take it.

This also laid the groundwork for the bug behind me. The problem is that the bug is annoying because the pre-release environment is good, but the line is bad.

First look at how the code is written:

image.png

You can see that in the code, I went to KV to get the value in the static code block. If there is a value, I use the KV as the initial value. If I didn’t get it, I also have the default value. I was still thinking that my idea was really clever. DB is highly efficient, using constants to make the bottom line, so as not to have no value without configuration, report a null pointer or something.

At that time, I kept cheering on myself, it was wonderful , I don't know how stupid I wrote.

This seems to be no problem, but my value can be modified, which is a problem, and I still use variables in several places, not the KV that I always use.

And I use constants as variables, which violates the original intention of this thing.

After I went online, Sanwai said: Ao Bing has a big problem, why I refreshed the value once and it was wrong, and then refreshed the value again.

I was terrified, knowing that I wrote a BUG, ​​I wiped the sweat off my temples without a trace, and took a deep breath.

He replied: I haven't written the code yet. Why are you messing up? I told you not to mess up. Now your points are broken.

Sanwai seemed to really think he was wrong, and left silently without saying anything.

image.png

At this time, I turned on the computer immediately, and ignored the crooked feeling, my thoughts turned quickly, and I started to build the entire value transfer link in the noisy sea.

I changed the value of the page, and then I also changed the value of this static variable and KV, but I found online that I refreshed the page for a while after the modification, and then for the previous value.

This is strange, mainly because it cannot be reproduced in advance, which is very cheating, uncomfortable and crooked.

Tip: Pre-release means that the code is the same as online, the database is the same, and the environment configuration is different.

I looked at the code again and again, but still no problem.

After the warm and lustful afternoon meal that day, I suddenly found the core of the problem with a flash of inspiration. The anticipation is good because it is a machine.

The machines on our line are load-balanced. There are two machines. I only changed the value of one machine at a time, and the other one was not changed.

Then the problem is simple, we look at the following figure:

image.png

The pre-release situation is a stand-alone machine. No matter how I read or modify it, the data is accurate every time (here I haven't realized that it is wrong to get the variable every time).

But online is different. When the server is started, each machine is assigned a value. In this case, it is okay to just read it (no wonder the static constants are all private, and they basically have this problem if they can be modified).

But if you modify it, your modified request only hits one machine. If the next load balancing request comes to this machine, then you are lucky and the result is correct, but often the load balancing algorithm is so fair, and the rain and dew are all in the situation. 1/2 of the request will get the wrong result.

And there is another problem with this place. It should be after the KV is modified, the variables are modified. In some places, I use variables, because I want to change it every time, and it will be fine.

In fact, the correct way is to read KV every time I go, and only read static variables when KV is empty. As a bottom line solution, you should not change the value of the constant.

And everyone should know, what if I modify the KV successfully in case something goes wrong when I modify the constant? You are wrong to take the value of a constant.

So the query and modification should be done like this:

image.png

I mainly want to remind everyone to be cautious about operating static constants. If they can change, they don’t change. Don’t write about operations like this. I have a lot of services with 40 or 50 online machines. This kind of problem is really hard to find. .

I never expected that I would actually write this kind of code, and write it out next time, so that I can only make Weiya not messy, lest it breaks again haha.

image.png


Third Prince Ao Bing

The more you know, the more you don’t know

Appreciate the QR codeLike the author


Guess you like

Origin blog.51cto.com/14924531/2532816