[Original] & fast failure mechanisms fail safe mechanism

This is the first 29 original articles why technology

Before writing "This road Java basic question is really pit! I beg you, think seriously before answering. " When this article, I mentioned the rapid failure and defeat security mechanisms in section 8.1.

But I found that when I search for "fast failure" or "fail safe" when retrieved results of 90 percent or more are talking about Java collection is how fast failure or failure of security.

In my opinion, when it comes to quick failure, failure safety, our first thought is that this should be a mechanism, an idea, a model, it belongs to the system design category , and only secondarily should think of its various application scenarios and implementation. And not immediately think of the collection, so a bit cart before the horse feeling.

You can look at the description on the wiki for fast failure and failure of security:

Quick failed: http: //en.wikipedia.org/wiki/Fail-fast

Fail Safe: http: //en.wikipedia.org/wiki/Fail-safe

In short: the system is running, if an error occurs, the system immediately ends this design is to fail fast. System operation, if an error occurs, the system will not stop running, it ignores the error (but there will be places to record), continues to run, this design is fail safe.

This article will compare the set of Java quickly fail safe and Dubbo framework quickly fail safe.

After reading, you know Java implementation and collection of Dubbo achieve quite different.

No one better than anyone else, only the combination of the scene, the more appropriate it than anyone else.

Java collections - Quick failure

** phenomenon: ** When using iterates over a collection of objects, if the traversal of the contents of a collection of objects were to add, delete, modify operation will throw ConcurrentModificationException.

** Principle: ** iterator directly access the contents of the collection is traversed, and the use of a variable modCount during traversal. During the collection is traversed if the content changes, it will change the value of modCount. Whenever the next iteration uses hashNext () / next () before a traversing element will detect whether expectedmodCount modCount variable value, which is then returned to traverse; otherwise, an exception is thrown ConcurrentModificationException terminate traversal.

** Note: ** this exception when the condition is detected modCount! = ExpectedmodCount this condition. ModCount modify the value set for just another expectedmodCount value, it will not throw an exception if the collection changes occur. Thus, whether or not this exception is thrown depends on the concurrent programming operation, the abnormality detection is only recommended for concurrent modifications of the bug.

** Scene: ** collections in java.util package are fail-fast, not concurrent modification (the modified iteration) occurs in a multi-threaded.

The above knowledge I have , "the Road Java basic question is really pit! I beg you, think seriously before answering. " This article has been careful look like in the third section of the details, are interested can read about:

Java collections - Fail Safe

** phenomenon: ** The failure of the security mechanisms of the collection container, not directly accessible when traversing on the contents of the collection, but the first copy of the original contents of the collection, to traverse on a copy of the collection.

Principle **: ** Because iteration is set to traverse copy of the original, so that during traversal of modifications made to the original set are not to be detected by the iterator will not be triggered ConcurrentModificationException.

** disadvantages: * copy content based advantage of avoiding a ConcurrentModificationException, but again, do not have access to the iterator modified content, namely: iterates over the set of copies is traversing moment get, traversing modify the original collection iterator that occurred during is not known. This is his shortcomings, at the same time, because it is to be copied, it is more eating memory.

** Scene: container under ** java.util.concurrent package is safe fails, you can use a multithreaded concurrent, concurrent modifications.

For example, the article comes before the CopyOnWriteArrayList:

Portion of the reference set of links involved: https: //www.cnblogs.com/ygj0930/p/6543350.html

The fault-tolerant cluster Dubbo

Before describing the rapid failure and failure of security is reflected in Dubbo, we must first talk about a cluster fault tolerance in Dubbo, because the rapid-fail and fail safe is a fault-tolerant mechanisms.

Or look at the pictures before appearing:

Among them, a cluster Cluster is to use multiple service providers into one Cluster Invoker, and exposing the Invoker service to consumers. As a result, consumers only need to service remote calls to pass this Invoker, as to which service provider specific calls, as well as how to deal with issues such as post-call failed, are now handed over to a cluster module to deal with.

Cluster module is a service provider and service consumer middle layer, shielding the case of a service provider for the service consumer, so the consumer can focus on processing service remote call related matters.

For fault-tolerant way official line is this to say:

Note Well, the official website said is the main provider of such a few, and not exhaustive list, you can see by looking at the source code:

org.apache.dubbo.rpc.cluster.support.AbstractClusterInvoker

There are nine implementation class, explained the official nine fault-tolerant way for you to choose:

The article focuses on are:

org.apache.dubbo.rpc.cluster.support.FailfastClusterInvoker org.apache.dubbo.rpc.cluster.support.FailsafeClusterInvoker

Demo project to build

In order to facilitate the rapid demonstrate failure and failure of security is reflected in Dubbo, we need to build a simple Demo project, I do not build process demonstrates, this section only for Demo key areas will be described:

Server interfaces as follows:

Server interface as follows:

Sleep three seconds, a timeout analog service.

Dubbo xml server configuration is as follows:

Consumer end Dubbo xml configuration is as follows:

Test class in the end consumer consumption follows:

Dubbo in rapid failure

Fast failure corresponding implementation classes are:

org.apache.dubbo.rpc.cluster.support.FailfastClusterInvoker

Enable the implementation class, only you need to specify the cluster property in Dubbo xml as failfast:

Look at the comments on the implementation class is how to write:

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 call only once, immediately after the failure of an exception is thrown. Suitable for idempotent operations, such as new records.

Implementation class source code is as follows:

Execution results are as follows:

Failure of security in Dubbo

Fail Safe corresponding implementation classes are:

org.apache.dubbo.rpc.cluster.support.FailsafeClusterInvoker

Enable the implementation class, only you need to specify the cluster property in Dubbo xml for failsafe:

Look at the comments on the implementation class is how to write:

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 a failure security of Cluster Invoker. The so-called failure of security means that when an exception occurs during the call, FailsafeClusterInvoker will only print exceptions, without throwing an exception. Apply to write audit logs and other operations.

Implementation class source code is as follows:

Implementation of the results follows, first of all you can see a timeout exception is caught:

Therefore, although overtime, but in the Test class, or to print out returnStr:

The story behind the article

This week output this article is not easy.

Since the company annual meeting Saturday with the games, I was fortunate elected a team captain, it is time Monday to Friday this week outside of work are busy with preparations for the Games of the material, statistics and other related work. There's only a little while to get up early can readily neutral time looking through the book, but the writing is not enough time.

Saturday activities start from 6:30 in the morning to get up to 23 o'clock at night to finish the final finishing work, busy the whole day. Fortunately, the team where I stand out in the 10 team, won the first.

In the fierce tug of war that's part of war, all the cheerleaders in uniform, hiss shouting slogans sound, the team members reflect the gritty, hardcore bite spirit, deeply impressed me. There are two opportunities bye, we are with the team pass, but we once again defeated the strength of strong opponents, still holding the rope tug of war finals. Because opponents round bye, so energetic, we regret lost. But before the referee did not blow the whistle, we never give up.

After completion of all projects than I where the final team total score ranked first. Before the final results are not announced, we never give up.

Sunday, and I grew up with cousin marriage, 7:00 in the morning to go to the scene to help. Noon meal I did not dare to drink too much wine, because I think this week's article did not write, I have to stay awake. But to see the sister and her husband stood at the moment of the big screen, I was moved to tears. Afternoon with the family to play one afternoon. At home at night I have been very tired, but I still output this article.

Girlfriend asked me if I could not help but write this week, I thought for a moment: You can not. Since that moment I thought, Mr. Lu Yao in the "morning from noon" in the sentence: only love and religious zeal of the will and talent possible to cause some kind of achievement.

This is also an annual meeting of the PPT is very consistent with: put your all, dedicated to everything you love.

I am an ordinary programmer , but I love this industry.

The final say

If the separate collection and achieve the realization of Java Dubbo framework of view, I feel these are two different points of knowledge, but then further on to pull away, they can be found are fail-fast implementation mechanism and fail safe mechanism. Or are inextricably linked.

Or said before, rapid failure mechanism and failure safety mechanism, there is no better than anyone else, only the combination of the scene, the more appropriate it than anyone else.

Associated with this article as well as the following two articles are welcome to read:

"This road Java basic question is really pit! I beg you, think seriously before answering. "

"This road Java basic question is really pit! I did not think there is a sequel. "

Caishuxueqian, it is inevitable there will be flaws, if you find the wrong place, please leave a message and pointed out to me, and I modify them.

If you feel pretty good article, your forwarding, sharing, appreciation, thumbs up, the message is for me the greatest encouragement.

Thank you for reading, I adhere to the original, welcome and thank you for your attention.

the above.

Published 164 original articles · won praise 32 · views 10000 +

Guess you like

Origin blog.csdn.net/weixin_44626952/article/details/103960260