From 94bf28010cfad6a29c38aa70e6dad046d867afa2 Mon Sep 17 00:00:00 2001 From: leonardo Date: Sun, 10 Mar 2024 21:58:17 -0300 Subject: [PATCH 1/3] add leonardo_meireles --- .../leonardo-meireles/config/nginx.conf | 24 ++++++ .../leonardo-meireles/docker-compose.yml | 68 +++++++++++++++++ .../leonardo-meireles/sql/script.sql | 73 +++++++++++++++++++ 3 files changed, 165 insertions(+) create mode 100644 participantes/leonardo-meireles/config/nginx.conf create mode 100644 participantes/leonardo-meireles/docker-compose.yml create mode 100644 participantes/leonardo-meireles/sql/script.sql diff --git a/participantes/leonardo-meireles/config/nginx.conf b/participantes/leonardo-meireles/config/nginx.conf new file mode 100644 index 000000000..b12957358 --- /dev/null +++ b/participantes/leonardo-meireles/config/nginx.conf @@ -0,0 +1,24 @@ +worker_processes auto; + +events { + worker_connections 1000; +} + +http { + access_log off; + error_log off; + sendfile on; + + upstream api { + server api01:8080; + server api02:8080; + } + + server { + listen 9999; + + location / { + proxy_pass http://api; + } + } +} \ No newline at end of file diff --git a/participantes/leonardo-meireles/docker-compose.yml b/participantes/leonardo-meireles/docker-compose.yml new file mode 100644 index 000000000..a344e3791 --- /dev/null +++ b/participantes/leonardo-meireles/docker-compose.yml @@ -0,0 +1,68 @@ +version: "3.8" + +services: + api01: &api + image: leonardomeireles55/rinha_backend:lastest + build: + context: ./ + dockerfile: Dockerfile + working_dir: /usr/src/app + hostname: api01 + environment: + - SPRING_R2DBC_URL=r2dbc:postgresql://db:5432/rinha + - SPRING_R2DBC_USERNAME=admin + - SPRING_R2DBC_PASSWORD=123 + ports: + - "8081:8080" + depends_on: + - db + deploy: + resources: + limits: + cpus: "0.58" + memory: "278MB" + restart: always + api02: + <<: *api + hostname: api02 + ports: + - "8082:8080" + deploy: + resources: + limits: + cpus: "0.57" + memory: "140MB" + nginx: + image: nginx:latest + volumes: + - ./config/nginx.conf:/etc/nginx/nginx.conf:ro + depends_on: + - api01 + - api02 + ports: + - "9999:9999" + deploy: + resources: + limits: + cpus: "0.10" + memory: "32MB" + db: + image: postgres:latest + logging: + driver: none + hostname: db + environment: + - POSTGRES_DB=rinha + - POSTGRES_USER=admin + - POSTGRES_PASSWORD=123 +# command: "postgres -c synchronous_commit=0 -c full_page_writes=0" + command: postgres -c checkpoint_timeout=600 -c max_wal_size=2096 -c synchronous_commit=0 -c full_page_writes=0 -c fsync=off -c check_function_bodies=false + ports: + - "5432:5432" + volumes: + - ./sql/script.sql:/docker-entrypoint-initdb.d/script.sql + deploy: + resources: + limits: + cpus: "0.25" + memory: "100MB" \ No newline at end of file diff --git a/participantes/leonardo-meireles/sql/script.sql b/participantes/leonardo-meireles/sql/script.sql new file mode 100644 index 000000000..7f17839a5 --- /dev/null +++ b/participantes/leonardo-meireles/sql/script.sql @@ -0,0 +1,73 @@ +CREATE UNLOGGED TABLE cliente +( + id INT PRIMARY KEY, + limites INT NOT NULL, + saldos INT NOT NULL +); + +INSERT INTO cliente (id, limites, saldos) +VALUES (1, 100000, 0), + (2, 80000, 0), + (3, 1000000, 0), + (4, 10000000, 0), + (5, 500000, 0); + +CREATE UNLOGGED TABLE TRANSACAO +( + ID SERIAL PRIMARY KEY, + ID_CLIENTE INT NOT NULL, + VALOR INT NOT NULL, + TIPO VARCHAR(1) NOT NULL, + DESCRICAO VARCHAR(10) NOT NULL, + REALIZADA_EM TIMESTAMP NOT NULL +); + +CREATE INDEX idx_transacao_id_cliente ON transacao (id_cliente); +CREATE INDEX idx_transacao_id_cliente_realizada_em ON transacao (id_cliente, realizada_em DESC); + + +CREATE OR REPLACE FUNCTION efetuar_transacao( +clienteIdParam int, +tipoParam varchar(1), +valorParam int, +descricaoParam varchar(10) +) +RETURNS TABLE (saldo int, limite int) AS $$ +DECLARE +cliente cliente%rowtype; +novoSaldo +int; +numeroLinhasAfetadas +int; +BEGIN +PERFORM +* FROM cliente where id = clienteIdParam FOR +UPDATE; +IF +tipoParam = 'd' THEN +novoSaldo := valorParam * -1; +ELSE +novoSaldo := valorParam; +END IF; + +UPDATE cliente +SET saldos = saldos + novoSaldo +WHERE id = clienteIdParam + AND (novoSaldo > 0 OR limites * -1 <= saldos + novoSaldo) RETURNING * +INTO cliente; + +GET DIAGNOSTICS numeroLinhasAfetadas = ROW_COUNT; + +IF +numeroLinhasAfetadas = 0 THEN +RAISE EXCEPTION ''; +END IF; + +INSERT INTO transacao (id_cliente, valor, tipo, descricao, realizada_em) +VALUES (clienteIdParam, valorParam, tipoParam, descricaoParam, current_timestamp); + + +RETURN QUERY SELECT cliente.saldos AS saldo, cliente.limites AS limite; +END; +$$ +LANGUAGE plpgsql; \ No newline at end of file From a4715bcd1e1482f9882c674c870cf6a279318a70 Mon Sep 17 00:00:00 2001 From: leonardo Date: Sun, 10 Mar 2024 22:06:53 -0300 Subject: [PATCH 2/3] add readme --- participantes/leonardo-meireles/README.md | 13 +++++++++++++ 1 file changed, 13 insertions(+) create mode 100644 participantes/leonardo-meireles/README.md diff --git a/participantes/leonardo-meireles/README.md b/participantes/leonardo-meireles/README.md new file mode 100644 index 000000000..97f43e47c --- /dev/null +++ b/participantes/leonardo-meireles/README.md @@ -0,0 +1,13 @@ +# Minha tentativa de participar da Rinha de Backend 2024.1 + +### Leonardo Meireles - Quero-1-Emprego Edition + +Github: [@LeonardoMeireles55](https://github.com/LeonardoMeireles55) +Repositório: [https://github.com/LeonardoMeireles55/rinha-backend-leonardo](https://github.com/LeonardoMeireles55/rinha-backend-leonardo) + +### Stack: +- Java +- Spring +- WebFlux +- Postgres +- Nginx From 0c74c6f8bc888721ca961aa2bde21225dc80ef56 Mon Sep 17 00:00:00 2001 From: LeonardoMeireles Date: Mon, 11 Mar 2024 12:17:58 -0300 Subject: [PATCH 3/3] add dockerfile :( --- participantes/leonardo-meireles/Dockerfile | 11 +++++++++++ 1 file changed, 11 insertions(+) create mode 100644 participantes/leonardo-meireles/Dockerfile diff --git a/participantes/leonardo-meireles/Dockerfile b/participantes/leonardo-meireles/Dockerfile new file mode 100644 index 000000000..d6eb5febc --- /dev/null +++ b/participantes/leonardo-meireles/Dockerfile @@ -0,0 +1,11 @@ +#BUILD +FROM maven:3.8.3-openjdk-17-slim AS build +WORKDIR /usr/src/app +COPY . . +RUN mvn clean package -DskipTests + +#RUN +FROM maven:3.8.3-openjdk-17-slim +WORKDIR /usr/src/app +COPY --from=build /usr/src/app/target/rinha-backend-0.0.1-SNAPSHOT.jar app.jar +CMD ["java", "-jar", "app.jar"]