This is distributed arithmetic expression calculator.
In the photo, we can see two agents working (one gorutine from each) and one agent died.
The user wants to calculate arithmetic expressions. He enters the code 2 + 2 * 2 and wants to get the answer 6. But our addition and multiplication (also subtraction) operations take a “very, very” long time. Therefore, the option in which the user makes an http request and receives the result as a response is impossible. Moreover: the calculation of each such operation in our “alternative reality” takes “giant” computing power. Accordingly, we must be able to perform each action separately and we can scale this system by adding computing power to our system in the form of new “machines”. Therefore, when a user sends an expression, he receives an expression identifier in response and can, at some periodicity, check with the server whether the expression has been counted? If the expression is finally evaluated, he will get the result. Remember that some parts of an arithmetic expression can be evaluated in parallel.
Expressions - You can write some expressions to calculate (registered users only).
Operations - You can change the execution time of each operation (registered users only).
Agents - You can see how many servers can currently process expressions.
Login - You can register or log in to your account.
Orchestrator:
- HTTP-server
- Parses expression from users and sends to Agents through RabbitMQ.
- Consumes results from Agents, inserts them to the expressions and sends new tokens to Agents to calculate.
- Consumes pings from Agents and kills those who didn't send anything.
- Writing the data to the database.
Agent:
- Consumes expressions from the Orchestartor and gives it to its goroutines for calculations.
- Consumes results from each goroutine and sends it to Orchestartor through RabbitMQ.
- Sends pings to Orchestartor.
- Every agent have 5 goroutines.
- There are 3 agents.
Auth:
- Log in to user's account.
- Register new users.
Some example:
I uses reverse Polish notation
2 + 2 --parse--> 2 2 +
And we can give 2 2 + to some goroutine to calculate.
But what about this example?
2 + 2 + 2 + 2 --parse--> 2 2 + 2 + 2 +
I think it's slow, because we need to solve 2 2 +, then 4 2 +, then 6 2 +
SO, I parses it to RPN differently.
I just add some brackets to expression.
2 + 2 + 2 + 2 --add-brackets--> (2 + 2) + (2 + 2) --parse--> 2 2 + 2 2 + +
And now we can run parallel 2 2 + and 2 2 + and then just add up their results.
We have N expressions, every expression is processed by some agent. But that's not all, inside each expression we process subexpressions with different agents.
If the HTTP-server crashed and we have expressions that did not have time to be calculated, by rebooting the server we will return to their calculations.
Run this command
git clone https://github.com/Prrromanssss/DAEC-fullstack
Run this command
docker-compose up -d
http://127.0.0.1:5173/
I have unit-tests to test the work of my parser. You can see that all tests have passed in github actions.
- Valid cases
- 4 + -2 + 5 * 6
- 2 + 2 + 2 + 2
- 2 + 2 * 4 + 3 - 4 + 5
- (23 + 125) - 567 * 23
- -3 +6
- Invalid cases
- 4 / 0
- 45 + x - 5
- 45 + 4*
- ---4 + 5
- 52 * 3 /