The world is not perfect. Something can go wrong at any moment. Fortunately, most of us don't launch rockets into space or build airplanes.

A modern person depends on the application in his phone and our task is to make it so that at any moment in time, under any coincidence of circumstances, he can open the application and see pictures with cats.

People are not perfect. We make mistakes all the time. We make typos, we can forget something or succumb to laziness. A person can swell corny or get hit by a car.

Iron isn't perfect. Hard drives are dying. Datacenters are losing channels. Processors overheat and power grids fail.

The software is not perfect. The memory is flowing. Connections are torn. Replicas break down and data goes into oblivion.

Shit happens - as our overseas friends say. What can we do about all this? And the answer is banal to the point of simplicity - nothing. We can test forever, raise a ton of environments, copy production and keep a hundred thousand backup servers, but it still won't save: the world is not perfect.

The only right decision here is to accept. You need to accept the world as it is and minimize losses. Each time you set up a new service, you need to remember that it will break at the most inopportune moment.

It will break for sure. You will definitely make a mistake. Iron will surely fail. The cluster will surely crumble. And according to the laws of this imperfect world, it will happen exactly when you least expect it.

What do most of us do to deceive everyone (including ourselves)? - We set up alerts. We write cunning metrics, collect logs and create alerts, thousands, hundreds of thousands of alerts. Our mailboxes are full. Our phones are bursting with SMS and calls. We plant entire floors of people to look at charts. And when once again we lose access to the service, the parsing begins: what did we forget to monitor.

All this is just an appearance of reliability. No alerts, metrics and monitoring will help.

Today they called you and you fixed the service - no one noticed that something was broken. And tomorrow you left for the mountains. And the day after tomorrow it swelled. People are not perfect. Fortunately, we are engineers, we live in an imperfect world and we are learning to conquer it.

So why should you wake up at night or in the morning to read your mail instead of coffee? Why a business should depend on one person and his performance. Why. I do not understand.

I just understand that you can't live like that, and I don't want to live like that. And the answer is simple: Automate it (yes, with a capital letter). We need more than just alerts and calls at night. We need automatic responses to these messages. We need to be sure that the system can fix itself. The system must be flexible and flexible.

Unfortunately, we don't have a smart enough AI yet. Fortunately, all of our problems are formalizable.

I don't have a silver bullet, but I have a Proof of Concept for AWS.

AWS Lambda

Serverless - first of all, something that is not running cannot break.
Event based - received an event, processed it, shut down.
Knows the JVM, which means that you can use all the experience from the Java world (which means that I can use Clojure).
3d-party - No need to track and maintain AWS Lambda.

Pipeline looks like this:
Event -> SNS Topic -> AWS Lambda -> Reaction

By the way, SNS topic can have several endpoints. This means that you can simply add mail and receive notifications as well. And we can extend the lambda function and make notifications much more useful: for example, send alerts right away along with charts or add SMS sending.

A complete example of one Lambda function can be found at: github.com/lowl4tency/aws-lambda-example
The lambda function nails all the nodes in the ELB not in the inService state.

Parsing the code

In this example, we will kill all nodes that are not in the InService state. By the way, the entire Lambda function takes ~ 50 lines of code in one file, which means that it is easy to maintain and easy to enter.

Any Clojure project starts with project.clj

I used the official Java SDK and the excellent Amazonica library, which is a wrapper for this SDK. Well, in order not to drag a lot of unnecessary things, we exclude those parts of the SDK that we do not need

] ] ] ]]
For more flexibility in each Lambda function, I use a config file with the most basic edn. In order to be able to handle events, we need to slightly change the function declaration.

(ns aws-lambda-example.core (: gen-class: implements)
Point of entry. We read the event at the input, process this event using handle-event and write it to the JSON stream as a result.

(defn -handleRequest "Parser of input and genarator of JSON output" (let (-> (io / reader is) json / read (-> (io / reader is) json / read walk / keywordize-keys handle-event (json / write w)) (.flush w))))
Workhorse:

(defn handle-event (let (when (seq unhealthy) (pprint "The next instances are unhealthy:") (pprint unhealthy) (ec2 / terminate-instances: instance-ids unhealthy)) (: message (get-in event [ : Records 0: Sns: Message]): elb-instance-ids (mapv: instance-id instances))))

We get a list of nodes in ELB and filter them by status. All nodes that are in the InService state are removed from the list. The rest are terminated.

Everything that we print via pprint will go to the CloudWatch logs. This can be useful for debugging. Since we do not have a constantly running lambda and there is no way to connect to the REPL, this can be quite useful.

(: message (get-in event [: Records 0: Sns: Message]): instance-ids (mapv: instance-id instances))))
In this place, the entire structure that we will generate and return from this function will be written in JSON and will be seen as a result of execution in the Lambda Web interface.

In function unhealthy-elb-instances filter our list and get instance-id only for those nodes that ELB considered non-working. We get a list of instances and filter them by tags.

(defn unhealthy-elb-instances (- >>
In function get-elb-instances-status call the API method and get a list of all nodes with statuses for one specific ELB

(defn get-elb-instances-status (- >> (elb / describe-instance-health: load-balancer-name elb-name): instance-states (map get-health-status)))
For convenience, we remove unnecessary things and generate a list only with information that is of interest to us. This is the instance-id and status of each instance.

(defn get-health-status (: instance-id (: instance-id instance): state (: state instance)))
And we filter our list by removing those nodes that are in the InService state.

(defn unhealthy-elb-instances (- >> instances-status (remove # (= (: state%) "InService")) (map: instance-id)))
And that's all: 50 lines that will allow you not to wake up at night and calmly go to the mountains.

Deployment

For ease of deployment, I use a simple bash-script

#! / bin / bash # Loader AWS Lambda aws lambda create-function --debug \ --function-name example \ --handler aws-lambda-example.core \ --runtime java8 \ --memory 256 \ --timeout 59 \ --role arn: aws: iam :: 611066707117: role / lambda_exec_role \ --zip-file fileb: //./target/aws-lambda-example-0.1.0-SNAPSHOT-standalone.jar
We set up an alert and attach it to the SNS topic. SNS topic is attached to the lambda as an endpoint. We are calmly driving into the mountains or are run over by a car.

By the way, due to flexibility, it is possible to program any behavior of the system, and not only by system, but also by business metrics.

It is believed that in this world you need to be better than everyone else in absolutely everything. Do everything perfect, make no mistakes and look perfect. But this is the path to defeat and unhappiness. Imperfect people are more likely to be successful and also happier.

We strive for perfection in everything. Study well, make no mistakes, work late, build perfect relationships, raise a child prodigy, look like an athlete or a model. We set ourselves the highest bar, and then we suffer from our perfectionism. The desire to achieve the best result in any business makes a person weaker, more unhappy and sadder.

Best the enemy of the good. We hear this proverb since childhood, but we do not often think how wise it is. William Shakespeare, in the tragedy "King Lear", also wrote: "Striving for the best, we often spoil the good." With excessive zeal, we often spoil what was already good before.

Striving for perfection ruins our life. Either perfect or bad? The constant race for the perfect result takes all the time, takes all the strength, distracts from the main thing, prevents you from achieving other goals and makes you unhappy.

1. Imperfection and communication with others

Many people try to behave perfectly with others, but they have few friends, they are not popular and are often lonely. It's like an excellent student at school. Ideality is boring and cold. The very best friends and buddies have a lot of flaws. They are flawed, they make mistakes, and they don't look like the perfect person. But in this they are like others. We are attracted to people who strive to be ourselves, not an artificial template. It's nice to have fun with them, do stupid things and be friends.

2. Imperfection is better for relationships and love

There are girls who look after their appearance and try to become ideal models from a glossy magazine. They are confused about looks and behavior. Girls who tried to become perfect are squeezed, have a bunch of complexes and turned on the wrong one. A relationship with such a girl will not make a man happy. She will annoy the man with her problems and demand that he conform to the ideal of a man. It's exhausting and stressful. Sex with such girls will be boring and rotten. The ideal beauty will think about how she looks at every moment and whether her hair is tousled. An imperfect girl can net in jeans on the steps, tie her hair up with headphones, go camping with a minimum of clothes and have sex where you want it, not just in the royal suite. Imperfect girls know how to enjoy life, get pleasure and have fun. It is with such a girlfriend that it is easy to become happy.

3. Imperfection and success

Perfectionism has ruined many promising people and businesses. The race for perfection does not allow us to look at everything more broadly. We bother with little things, bringing to perfection, forgetting about other matters. We set ourselves endless and unattainable goals. It takes up all the time and energy we need elsewhere.

Striving for perfection only ruins us. We become unhappy, unable to achieve our goals and dreams due to the fact that we are mired in unnecessary things.

It's better to be imperfect. You will be happier and more successful.

To the question Why are people not perfect? given by the author Ivan Makarov the best answer is ideal is boring

Answer from Spikelets[guru]
Because everyone has their own idea of ​​the ideal.


Answer from Jenix[guru]
There are no ideal people and cannot be.


Answer from Undergrowth[guru]
Please, let’s scatter the question on the shelves))
_
Let's assume that there is a certain person who is perfect. Now imagine that it will not be assessed by the ideal, and precisely by those whose measurements and understandings are distorted. Ideally, these observers will ideally find exactly who they are, and the distortions they see in it are THEIR OWN DISTORTIONS.
_
Secondly, although it should have been done from the very beginning. We need to find out what is PERFECT. What is ideal for one - for another - the last bullshit. Therefore, in order to measure someone or something by the standards of ideality, it is necessary, as it was initially found out, - to be the most ideal, secondly, it is necessary to choose not relative, but UNIVERSAL measures of ideality, those measures that are laws, not only the laws of physics, but laws thin.
_
Thirdly. We cannot say with certainty that imperfection is imperfection. There is such a thing as the perfection of the imperfect. For example, nature with sharp rocks, and cliffs and ravines where you can not only break your legs but also kill yourself to hell is called perfect, and a person with the same potholes and ravines in character is no longer ideal and is perceived as FIIIIII (so it does not canal! !! =)). Everything that exists no longer contradicts any laws, otherwise it would not exist. And perfection is what development strives for, and whether it ever achieves we do not know ... Probably just aspiring. When a plant grows and reaches for the light, this does not mean that it has set a goal to grow up to the sun, but it seeks to become closer to it, the sun still sets a goal.


Answer from Neurosis[guru]
it's all about emotions people in physics have emotions for something


Answer from Uncle Nikita[guru]
no, they shouldn't - otherwise they had to be balls or circles - because balls and circles are ideal shapes


Answer from Dragon[expert]
Why do you think people are not perfect? People have the most ideal ability - the ability to self-development. A person can surpass the greatest ideal and then develop even further. And what is more ideal than that which can transcend all ideals?


Answer from Ol9sik[guru]
Man is perfect by his imperfection ... As one character said in one film: "I don't need an ideal woman. It's hard to inflate an ideal woman ..."


Answer from Vitalik Oblique eye[newbie]
There are perfect for sure 1 person is definitely there


Answer from Veronica[guru]
If everyone was perfect, what should you strive for? It will never be that way.


Answer from Lenkin[guru]
I thought the same.


Answer from Galina Filyanova[guru]
not ideal what kind of people - old, young, women, men. ... ? everyone has their own ideals


Answer from Mikhail Levin[guru]
what did you find ideal in the laws of physics? Read Quantum Mechanics!
And why should people be ideal, and not dinosaurs or flu viruses?


Answer from White rabbit[guru]
"Are there generally adequate people here who are at least a little capable of reasoning?"
The fact of the matter is that you are answered by people capable of reasoning, and not to empty and illiterate chatter, which you, apparently, by your naivety, consider to be smart ...
What is "perfect"?
What does it mean that people (not an individual person - but people!) Are not ideal?
What makes you think that the laws (?!) Of physics are ideal?


There are no ideal people, there are people who hide their flaws. (Mikhail Shchetinin)

The ideal is a guiding star. Without it, there is no firm direction, and there is no direction - there is no life. (Lev Nikolaevich Tolstoy)

Every person, great or small, is a poet if he sees an ideal because of his actions. (Henrik Ibsen)

One should not think that only one who is impeccably pure and highly moral contributes to cultivation. All great things form a person. (Johann Wolfgang Goethe)

Probably the most tragic of imperfections is human imperfection. (Evgeny Mikhailovich Bogat)

People rarely improve unless they have a role model other than themselves. (Oliver Goldsmith)

A person is never as close to perfection as when filling out a job application. ()

Pity is he who lives without an ideal! (Ivan Sergeevich Turgenev)

Good friends, good books, and a sleeping conscience are the ideal life. (Mark Twain (Samuel Lenghorn Clemens))

Don't exalt yourself by humiliating others. It's useless. Your level will still manifest itself at the most inopportune moment for you. Better improve. ()

An essential part of our excellence is noticing our imperfections. (François Lamotte-Levays)

Don't hold on too hard to your shortcomings on the pretext that perfection is not of this world. (Jules Renard)

Humiliation is, in fact, nothing more than a comparison of one's personal dignity with moral perfection. (Immanuel Kant)

The ideal is a way of grumbling. (Paul Valerie)

Perfection is just a little boring. This is truly the irony of life: what we all strive for is better when it is not fully achieved. (William Somerset Maugham)

Power and profit, splendor and glory: whoever does not touch them is truly pure. But the one who touches, and does not have dirt on himself, is doubly pure. Much knowledge and cunning, sharpness and discernment: whoever is deprived of them is truly exalted. But the one who is endowed with them, and does not use, is doubly exalted. (Hong Zicheng)

There are as few ideal women as ideal men, but you meet them more often. (Hildegard Knef)

With someone who denies the ideal, it can easily happen that he takes the vulgar for the beautiful. (Johann Wolfgang Goethe)

Our ideal is not a castrated, deprived of corporeality, an abstract being, our ideal, it is an integral, real, all-round, perfect, educated person. (Ludwig Andreas Feuerbach)

The ideal is in yourself. The obstacles to achieving it are in you. Your position is the material from which you must realize this ideal. (Thomas Carlyle)

Nothing is so perfect as to be free from all reproaches. (Aesop)

Even if you want to be only your ideal, you will have to force the whole world to do so. (Friedrich Nietzsche)

Only the consciousness of common tasks and ultimate ideals makes a person persistent and consistent. (Semyon Ludvigovich Frank)

A perfect man seeks everything in himself, an insignificant one - in others. (Confucius (Kun-Tzu))

My eye sees the ideals of other people, and this sight often delights me; but you, myopic ones, think that these are my ideals. (Friedrich Nietzsche)

Effort is a necessary condition for moral improvement. (Lev Nikolaevich Tolstoy)