This is the 29th original article by Why Technologies
I beg you, think carefully before you answer. In this article, I mentioned fast failures and fail-safe mechanisms in section 8.1.
But I found that when I searched for “fail fast” or “fail safe,” more than 90 percent of the results were about how fast fail or failsafe was implemented in Java collections.
In my opinion, when it comes to fast failure and failure safety, we should first think of a mechanism, an idea, a mode, which belongs to the category of system design, and then we should think of its various application scenarios and concrete implementation. Rather than immediately thinking of a collection, which is kind of putting the cart before the horse.
Take a look at the wiki description of fast failure and failsafe:
Fail fast: http://en.wikipedia.org/wiki/Fail-fast
Failure safety: http://en.wikipedia.org/wiki/Fail-safe
In short: if something goes wrong while the system is running, the system ends immediately. This design is failure fast. Failure safety is the design of a system that does not stop running if an error occurs, ignores the error (but has a place to record it) and continues running.
This article compares fast failure, failsafe and failsafe in Java collections with those in the Dubbo framework.
After reading this, you know that the implementation in Java collections is quite different from the implementation in Dubbo.
There is no one better than who, only the combination of the scene, who is more appropriate than who.
Java Collections – Fail fast
Phenomenon: when using iterators iterate over a collection object, if the traversal process to increase the content of the collection objects, delete, modify, will throw ConcurrentModificationException.
How it works: Iterators directly access the contents of a collection during traversal and use a modCount variable during traversal. If the contents of the collection change during traversal, the value of modCount is changed. Whenever the iterator iterates over the next element using hashNext()/next(), it checks whether the modCount variable is the expectedmodCount value and returns the traversal if it is; Otherwise throw ConcurrentModificationException, terminate the traversal.
Note: The exception is thrown if modCount is detected! =expectedmodCount This condition. If the set changes when the modCount value is just set to the expectedmodCount value, the exception will not be thrown. Therefore, concurrent operations cannot be programmed depending on whether or not this exception is thrown; this exception is only recommended for detecting concurrent modification bugs.
Scenario: Collection classes in the java.util package fail fast and cannot be modified concurrently (iteratively) in multiple threads.
I beg you, think carefully before you answer. The third section of the article is detailed in detail, if you are interested in reading it:
Java Collections – Failsafe
Symptom: The collection container with fail-safe mechanism does not access the collection content directly during traversal, but copies the original collection content first and traverses the copied collection.
Principle: as the iteration is to traverse the copies of the original collection, so in the process of traversing the changes to the original collection and cannot be detected by the iterator, so will not trigger a ConcurrentModificationException.
Faults: based on the copy content has the advantage of avoiding the ConcurrentModificationException, but by the same token, the iterator can not access to the contents of the modified, namely: iterators iterate over a collection of began to traverse the moment he got copies of the original collection during traversal of modifying the iterator is don’t know. This is also his disadvantage, at the same time, because is the need to copy, so eat memory.
Scenario: Containers under the java.util.concurrent package are security failures and can be used and modified concurrently in multiple threads.
For example, CopyOnWriteArrayList mentioned in the previous article:
Collection part involves the reference link: https://www.cnblogs.com/ygj0930/p/6543350.html
Cluster fault tolerance in Dubbo
Before describing fast failure and fail-safe in Dubbo, we must first talk about cluster fault tolerance in Dubbo, because fast failure and fail-safe are one of its fault tolerance mechanisms.
Again, look at the previous image:
The purpose of a Cluster Cluster is to combine multiple service providers into a Cluster Invoker and expose this Invoker to the service consumer. As a result, the service consumer only needs to make remote calls through the Invoker, leaving it to the cluster module to decide which service provider to call and what to do if the call fails.
The cluster module is the middle layer between the service provider and the service consumer, shielding the service provider from the service consumer so that the service consumer can focus on remote invocations.
The official website of fault tolerance mode is as follows:
Note: the official website says that it mainly provides such kinds of products, and does not list them completely. You can see them by viewing the source code:
org.apache.dubbo.rpc.cluster.support.AbstractClusterInvoker
There are nine implementation classes, which means there are nine fault-tolerant ways to choose from:
This paper mainly discusses:
org.apache.dubbo.rpc.cluster.support.FailfastClusterInvoker org.apache.dubbo.rpc.cluster.support.FailsafeClusterInvoker
Build Demo project
In order to demonstrate fast failure and failsafe in Dubbo, we need to build a simple Demo project. I will not demonstrate the construction process, but this section will only explain the key points of the Demo:
The server interface is as follows:
The server interface is implemented as follows:
Hibernate for 3 seconds, and the simulation service times out.
The configuration of Dubbo XML on the server is as follows:
The Dubbo XML configuration on the consumer side is as follows:
The consumer consumes the following in the Test class:
Quick failure in Dubbo
The implementation classes for fast failure are:
org.apache.dubbo.rpc.cluster.support.FailfastClusterInvoker
To enable the implementation class, just specify the cluster attribute as failfast in the Dubbo XML:
Let’s take a look at the comments on the implementation class:
Execute exactly once, which means this policy will throw an exception immediately in case of an invocation error. Usually used for Non – idempotent write operations.
FailfastClusterInvoker is invoked only once and an exception is thrown immediately after a failure. Ideal for idempotent operations, such as new records.
The source code for the implementation class is as follows:
The result is as follows:
Failsafe in Dubbo
The implementation classes for failsafe are:
org.apache.dubbo.rpc.cluster.support.FailsafeClusterInvoker
To enable the implementation class, just specify the cluster attribute as failsafe in the Dubbo XML:
Let’s take a look at the comments on the implementation class:
When invoke fails, log the error message and ignore this error by returning an empty Result. Usually used to write audit logs and other operations
FailsafeClusterInvoker is a fail-safe ClusterInvoker. Fail-safe means that when an exception occurs during invocation, FailsafeClusterInvoker only prints the exception, not throws it. This operation applies to writing audit logs.
The source code for the implementation class is as follows:
First, you can see that the timeout exception is caught:
So returnStr is printed in the Test class even though it’s timed out:
The story behind the article
Exporting this article this week has not been easy.
Due to the annual meeting and sports meeting of the company on Saturday, I was honored to be elected as the captain of a team, so I was busy with material preparation and data statistics of the sports meeting from Monday to Friday this week. It’s not enough to get up early and have a few minutes to flip through a book, but to write an article.
It was a busy day on Saturday, starting at 6:30 a.m. and finishing the finishing touches at 23 p.m. Fortunately, my team stood out among the 10 teams and won the first place.
In the extremely fierce tug-of-war, I was deeply moved by the indomitable and persevering spirit of all the cheerleaders in uniform and Shouting loudly. There were two missed opportunities for our team, but we beat strong opponents again and again and still hold the rope of the finals tug-of-war. As the opponent lost one round, so we are full of energy, we regret to lose. But we don’t give up until the referee blows his whistle.
After all the competitions, my team ranked first in total score. We will not give up until the final result is announced.
On Sunday, I got married to my cousin, who I grew up with, and went to the scene to help out at 7 a.m. I didn’t dare to drink too much at lunch because I thought I had to stay awake for the week’s essays. But when I saw my sister and her husband standing under the big screen, I was still moved to tears. I spent the whole afternoon with my family. I was tired when I got home in the evening, but I output this article anyway.
My girlfriend asked me if I could not write this week. I thought for a moment and said: No. Because at that moment I thought of a sentence by Mr. Lu Yao in Morning Begins at noon: only with the passion of first love and the will of religion can one achieve a certain kind of career.
This is also very consistent with a PPT in the annual meeting: give your all to everything you love.
I am an ordinary programmer, but I love this industry.
One last word
If you look at the implementation of Java collections and the implementation of the Dubbo framework separately, it feels like two different points of knowledge, but if you pull it apart further, you can see that they are both the implementation of fail-fast and fail-safe mechanisms. There’s still a lot of connections.
Again, there is no better fail-fast mechanism than fail-safe mechanism, only the combination of scenarios, who is better than who.
Welcome to read the following two articles related to this article:
I beg you, think carefully before you answer.
I didn’t think there would be a sequel.
If you find something wrong, please leave a message and point it out to me so that I can modify it.
If you think the article is good, your forwarding, sharing, appreciation, like, message is the biggest encouragement to me.
Thank you for reading, I insist on original, very welcome and thank you for your attention.
The above.
Welcome to pay attention to the public account [WHY Technology], adhere to the output of original. May you and I make progress together.