Skip to content

Commit ea2d3e2

Browse files
committed
cpp worker: rollback
1 parent 60d8ead commit ea2d3e2

22 files changed

+351
-202
lines changed

Dockerfile

-13
This file was deleted.

Makefile

-13
This file was deleted.

data/test.txt

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
fsdfsdfsdfвыа

scripts/build.sh

-4
This file was deleted.

scripts/kill-workers.sh

+7
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
#!/bin/bash
2+
set -e
3+
4+
for i in {0..4}; do
5+
docker stop "worker${i}"
6+
docker rm "worker${i}"
7+
done

scripts/run-server.sh

+4
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
#!/bin/bash
2+
set -e
3+
4+
docker run --name server --rm -d -p 9090:9090 -p 9091:9091 qemu-riscv-cluster/metrics-server

scripts/run-workers.sh

+8
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
#!/bin/bash
2+
set -e
3+
4+
for i in {0..4}; do
5+
docker compose run -d \
6+
--name "worker${i}" \
7+
worker metrics 127.0.0.1 9091 "worker${i}"
8+
done

src/.env

+2
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
LOG_PORT=3100
2+
LOKI_IP=172.29.0.2

src/Dockerfile.hash

+22
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
FROM alpine:3.16.3 AS builder
2+
3+
RUN apk update && apk add --no-cache g++ cmake make openssl-dev curl-dev
4+
5+
WORKDIR /app
6+
7+
COPY get-prometheus-cpp.sh .
8+
RUN sh get-prometheus-cpp.sh
9+
10+
COPY Makefile *.cpp *.hpp ./
11+
RUN make
12+
13+
FROM alpine:3.16.3
14+
15+
RUN apk update && apk add --no-cache libstdc++ libgcc libssl3 libcurl
16+
17+
COPY --from=builder /app/worker /usr/local/bin/worker
18+
COPY --from=builder /usr/local/lib/libprometheus* /usr/local/lib
19+
20+
WORKDIR /data
21+
22+
ENTRYPOINT ["worker"]

src/Makefile

+9
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
.PHONY: all clean
2+
3+
all: worker
4+
5+
worker: main.cpp metrics-collector.cpp metrics-collector.hpp \
6+
md_calculator.cpp md_calculator.hpp file.cpp file.hpp
7+
g++ -Wall -Werror -o worker \
8+
main.cpp md_calculator.cpp file.cpp metrics-collector.cpp -lssl -lcrypto \
9+
$(shell pkg-config --libs prometheus-cpp-core prometheus-cpp-push)

src/docker-compose.yml

+20
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
version: '3.8'
2+
3+
services:
4+
hash:
5+
command: metrics 127.0.0.1 9091 worker0
6+
build:
7+
context: .
8+
dockerfile: Dockerfile.hash
9+
container_name: hash
10+
volumes:
11+
- ./data:/data
12+
working_dir: /data
13+
logging:
14+
driver: loki
15+
options:
16+
loki-timeout: 10s
17+
no-file: "true"
18+
loki-external-labels: "container_name={{.Name}}"
19+
labels: "container_name, host"
20+
loki-url: "http://${LOKI_IP}:${LOG_PORT}/loki/api/v1/push"

src/file.cpp

+28
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
#include <fstream>
2+
#include <vector>
3+
#include <stdexcept>
4+
5+
#include "file.hpp"
6+
#include "md_calculator.hpp"
7+
8+
std::string File::calculate_hash(const std::string& file_path, const std::string& algorithm) {
9+
std::ifstream stream(file_path, std::ios::binary);
10+
if (!stream) {
11+
throw std::runtime_error("Failed to open file: " + file_path);
12+
}
13+
14+
MDCalculator calculator(algorithm);
15+
std::vector<char> buffer(4096);
16+
17+
while (stream) {
18+
stream.read(buffer.data(), buffer.size());
19+
calculator.update(
20+
reinterpret_cast<const unsigned char*>(buffer.data()),
21+
stream.gcount()
22+
);
23+
}
24+
25+
stream.close();
26+
27+
return calculator.finalize();
28+
}

src/file.hpp

+8
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
#pragma once
2+
3+
#include <string>
4+
5+
class File {
6+
public:
7+
static std::string calculate_hash(const std::string& file_path, const std::string& algorithm);
8+
};
File renamed without changes.

src/hash.cpp

-111
This file was deleted.

src/main.cpp

+90
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,90 @@
1+
#include "metrics-collector.hpp"
2+
#include "file.hpp"
3+
4+
#include <iostream>
5+
#include <cstring>
6+
#include <thread>
7+
#include <signal.h>
8+
9+
void print_usage(const std::string& program_name) {
10+
std::cerr << "Usage: " << program_name << " hash"
11+
<< " PATH ALGORITHM" << std::endl
12+
<< "Supported algorithms are: "
13+
<< "md2, md5, sha, "
14+
<< "sha1, sha224, sha256, sha384, sha512, "
15+
<< "mdc2 and ripemd160" << std::endl;
16+
}
17+
18+
int hash_main(int argc, char* argv[]) {
19+
if (argc < 4) {
20+
print_usage(argv[0]);
21+
return 1;
22+
}
23+
24+
try {
25+
std::string hash = File::calculate_hash(argv[2], argv[3]);
26+
std::cout << "Digest is: " << hash << std::endl;
27+
} catch (const std::exception& e) {
28+
std::cerr << "Error: " << e.what() << std::endl;
29+
return 1;
30+
}
31+
32+
return 0;
33+
}
34+
35+
36+
bool is_running = true;
37+
38+
void signal_handler(int signo) {
39+
std::cerr << "[INFO] Received " << strsignal(signo) << " signal. Stopping" << std::endl;
40+
is_running = false;
41+
}
42+
43+
int metrics_main(int argc, char *argv[]) {
44+
signal(SIGTERM, signal_handler);
45+
signal(SIGINT, signal_handler);
46+
47+
if (argc != 5) {
48+
std::cerr << "usage: worker" << argv[0] << " metrics"
49+
<< " ADDRESS PORT WORKER_NAME" << std::endl;
50+
return 1;
51+
}
52+
53+
MetricsCollector metrics_collector(argv[2], argv[3], argv[4]);
54+
55+
srand(time(NULL));
56+
while (is_running) {
57+
std::this_thread::sleep_for(std::chrono::seconds(rand() % 5 + 1));
58+
std::cerr << "[INFO] Doing task" << std::endl;
59+
metrics_collector.StartTask();
60+
61+
// some "work" that requires memory
62+
void *data = malloc(rand() % (1024 * 1024 * 50)); // 0 - 50 megabytes
63+
for (volatile int i = 0; i < 1000000000 + rand() % 10000000000; i++) {}
64+
free(data);
65+
66+
metrics_collector.StopTask();
67+
std::cerr << "[INFO] Task done" << std::endl;
68+
}
69+
70+
return 0;
71+
}
72+
73+
int main(int argc, char *argv[])
74+
{
75+
static const char usage[] = "Specify hash/metrics";
76+
77+
if (argc < 2) {
78+
std::cerr << usage << std::endl;
79+
return 1;
80+
}
81+
82+
if (strcmp(argv[1], "hash") == 0) {
83+
return hash_main(argc, argv);
84+
} else if (strcmp(argv[1], "metrics") == 0) {
85+
return metrics_main(argc, argv);
86+
}
87+
88+
std::cerr << usage << std::endl;
89+
return 1;
90+
}

0 commit comments

Comments
 (0)