Original: Little sister taste (wechat public ID: XJjdog), welcome to share, reprint, please keep the source.

There is a new architect in the department, with BAT background, who lives in the third ring road, drives a BMW to work and has a parking space.

The boy doesn’t say much, but when he does, he speaks with certainty and unshakable confidence. The reason is that, with hundreds of millions of concurrent experience, every second of requests is more than any other enterprise running in a year. It was very enviable, since he made more money on it than I did.

As the saying goes, if you want to avoid accidents at work, don’t write code. Work more prone to accidents, a relaxed no one to care, this is the reality.

But sometimes it depends on the results. The new R&D leader does not understand technology, but he knows technical indicators, so he counts the number of git submitted by everyone. If git activity is green and a-share, it will pass the test.

The architect did some thinking and decided on the highest concurrency requirement: count the average response time of the interface and the number of requests since startup.

Why is it high concurrency? This is because it counts all interfaces, and is naturally larger than the number of requests per interface. Package of AOP code, every interface has to walk from him.

It’s time for our architect. The code is shown here.

The architect said that my code didn’t need comments. Comments are for junk code. I thought it was. He was obviously influenced by Netflix.

The program allows for high concurrency scenarios, using thread-safe ConcurrentHashMap, and then fetching the corresponding data each time through the monitoring key, and then incrementing the value. This simple code really doesn’t need any comments.

As the most concurrent code on the project, out of trust with the senior architect, we did not need to do any code reviews, nor did we need to do any testing. Everyone is very busy, code you, to take a walk online.

I suggest you look for problems with the code first, and if you do, you’re better than the architect. If you don’t find out and it doesn’t prove that you’re weaker than an architect, there’s nothing to be sad about.

Here’s a picture to block your mind.

Installation B was struck by lightning, and after running online for some time, the memory overflowed.

There was a lot of noise. After all, xJjDog said that memory overflow problems take a long time to troubleshoot, taking about 40 days on average. At the beginning of the demonstration, the architect secretly started the Eclipse MAT. The MAT is great for analyzing memory problems, but only if you have to tamper with the stack.

The architect will use JMAP, most importantly because of its high authority, so he makes a copy of it for offline analysis.

I could understand how he felt, since the problem was in his own code. He noticed that the memory heap was full of MonitorKeys and MonitorValues.

Monitor$MonitorKey@15aeb7ab
Copy the code

I have a good relationship with the architect, and he asked me: Do we have more specific interfaces?

I say: not, you don’t look at the page view is big, so how many interface can a bullshit business have? Hundreds of them made it through the day.

He said: I found tens of millions of them in the pile…

He said nothing, for he found that there were many of the same interface. It must be the argument, so he added this to the code. The back one is cut off.

key = key.split("\ \"?") [0];
Copy the code

The results were posted online, and not long after that the memory ran out again. The code had forgotten to override the equals and hashCode methods for MonitorKey.

I couldn’t help blushing. As a good friend, I shouldn’t have let him make such a fool of myself. But I was secretly happy because his salary was higher than mine.

So that’s a big problem. Many of you know the HashMap right off the bat, even memorizing red-black trees. But in another way to ask, but a face muddled.

One way to ask is: Can a normal object be used as a key for a HashMap?

The answer is obviously yes, but be careful to override the hashCode and equals methods. There is a high probability of a memory leak if you forget to rewrite.

Unfortunately, there are many cases of forgetting in reality. Big architects can fall for this, too.

After the code overrides the hashCode and equals methods, there are no more overruns on the line.


Wait, that’s not all. After all, it is an architect, and such a bug alone does not prove the level. Bugs written by architects are certainly unusual.

As this happens more often, r&d leaders become less interested in technical authority. We decided to do a code review, starting with the code with the highest concurrency.

Unfortunately, the architect has a problem with the visit code. It’s not a big problem, but it’s a problem.

The visit method, first takes out the key, then calls empty, and then plugs the value. This is obviously not an atomic operation.

Thread 1: obtains the value of key a. Thread 2: obtains the value of key A. Thread 1: obtains the value of key A. Thread 1: obtains the value of key A. Thread 1: obtains the value of key A. Thread 2: obtains the value of key ACopy the code

In this case, B is lost.

The business could bear it, but the technical experts could not bear it and proposed the revision.

The architect said, “Synchronized” instead of “visit”.

public synchronized void visit(String url, String desc, long timeCost) 
Copy the code

I said no. There are more elegant ways to write it, more efficient. Using the putIfAbsent method, the code changes as follows:

MonitorKey key = new MonitorKey(url, desc);
MonitorValue value = monitors.putIfAbsent(key, new MonitorValue());
value = monitors.get(key);
value.count.getAndIncrement();
value.totalTime.getAndAdd(timeCost);
value.avgTime = value.totalTime.get() / value.count.get();
Copy the code

There was an argument about these two ways.

The technical director holds the cheek to think for a long time, looked at the arguable red-faced students, said: this is the reason why I don’t trust you. The online environment should be as stable as possible with minimal changes. Why not use synchronized when you can easily solve a problem by adding synchronized? The following code changes are too large and risky.

The director then turned his head to me. “This BUG is unusual,” he said. Share what you’ve learned and what you’ve learned to get people to follow this minimalist architecture. In our ordinary work, we should also try to be result-oriented. It doesn’t matter what means we use, as long as we can do things well.

That’s where this article comes in. I’m educated and aware that my salary is not going up.

Maybe a like or triple friendship from you could help me.

Author introduction: Little sister taste (XJjdog), a programmer is not allowed to detour the public number. Focus on infrastructure and Linux. Ten years of architecture, 10 billion traffic per day, and you discuss the high concurrency world, give you a different taste. My personal wechat xJJdog0, welcome to add friends, further communication.