Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Stage 1, Андрей Чешев, Политех #16

Merged
merged 18 commits into from
Feb 29, 2024
118 changes: 118 additions & 0 deletions src/main/java/ru/vk/itmo/test/andreycheshev/ServerImpl.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,118 @@
package ru.vk.itmo.test.andreycheshev;

import one.nio.http.*;
import one.nio.server.AcceptorConfig;
import ru.vk.itmo.ServiceConfig;
import ru.vk.itmo.dao.BaseEntry;
import ru.vk.itmo.dao.Config;
import ru.vk.itmo.dao.Entry;
import ru.vk.itmo.test.andreycheshev.dao.ReferenceDao;

import java.io.IOException;
import java.io.UncheckedIOException;
import java.lang.foreign.MemorySegment;
import java.lang.foreign.ValueLayout;
import java.nio.charset.StandardCharsets;

import static one.nio.http.Request.*;
import static one.nio.http.Response.*;

public class ServerImpl extends HttpServer {
private static final String REQUEST_PATH = "/v0/entity";
private static final String ID = "id=";
private final ReferenceDao dao;

public ServerImpl(ServiceConfig config) throws IOException {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Думаю, не очень хорошо, что конфиг сервиса просочился на уровень сервера. Было бы лучше, если бы сервис разобрал данный конфиг, на его основе сформировал бы конфиги зависимостей и инстанцировал их с этими конфигами

super(createServerConfig(config));

Config daoConfig = new Config(config.workingDir(), 100000);

this.dao = new ReferenceDao(daoConfig);
}

public static HttpServerConfig createServerConfig(ServiceConfig config) {
AcceptorConfig acceptorConfig = new AcceptorConfig();
acceptorConfig.port = config.selfPort();
acceptorConfig.reusePort = true;

HttpServerConfig serverConfig = new HttpServerConfig();
serverConfig.acceptors = new AcceptorConfig[]{acceptorConfig};
serverConfig.closeSessions = true;

return serverConfig;
}

public Response get(final Request request) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Методы get,put,delete и createServerConfig являются публичными, хотя этого не требуется. Нужно ужесточить доступность/видимость

String id = request.getParameter(ID);
if (id == null || id.isEmpty()) {
return new Response(BAD_REQUEST, Response.EMPTY);
}

Entry<MemorySegment> entry = dao.get(fromString(id));

return (entry == null)
? new Response(NOT_FOUND, Response.EMPTY)
: new Response(OK, entry.value().toArray(ValueLayout.JAVA_BYTE));
}

public Response put(final Request request) {
String id = request.getParameter(ID);
if (id == null || id.isEmpty()) {
return new Response(BAD_REQUEST, Response.EMPTY);
}

Entry<MemorySegment> entry = new BaseEntry<>(
fromString(id),
MemorySegment.ofArray(request.getBody())
);
dao.upsert(entry);

return new Response(CREATED, Response.EMPTY);
}

public Response delete(final Request request) {
String id = request.getParameter(ID);
if (id == null || id.isEmpty()) {
return new Response(BAD_REQUEST, Response.EMPTY);
}

Entry<MemorySegment> entry = new BaseEntry<>(fromString(id), null);
dao.upsert(entry);

return new Response(ACCEPTED, Response.EMPTY);
}

@Override
public void handleRequest(Request request, HttpSession session) throws IOException {
String path = request.getPath();
if (!path.equals(REQUEST_PATH)) {
session.sendResponse(new Response(BAD_REQUEST, Response.EMPTY));
return;
}

int method = request.getMethod();

Response response = switch (method) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Для всех методов отсутствует обработка исключений от DAO

Что, кстати, будет при выбросе исключения?) Автоматом 500 ответ или, может, отсутствие ответа?)

case METHOD_GET -> get(request);
case METHOD_PUT -> put(request);
case METHOD_DELETE -> delete(request);
default -> new Response(METHOD_NOT_ALLOWED, Response.EMPTY);
};

session.sendResponse(response);
}

private MemorySegment fromString(String data) {
return MemorySegment.ofArray(data.getBytes(StandardCharsets.UTF_8));
}

@Override
public synchronized void stop() {
try {
dao.close();
} catch (IOException e) {
throw new UncheckedIOException(e);
}
super.stop();
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Сначала закрывается dao, затем сервер перестаёт слушать порт
Получается, что будет попытка обработать часть запросов уже с закрытым dao
Поэтому, нужно сначала прекратить принимать http запросы, а уже потом закрывать dao

}
}
21 changes: 21 additions & 0 deletions src/main/java/ru/vk/itmo/test/andreycheshev/ServerStarter.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
package ru.vk.itmo.test.andreycheshev;

import ru.vk.itmo.ServiceConfig;

import java.io.IOException;
import java.nio.file.Path;
import java.util.List;

public class ServerStarter {
public static void main(String[] args) throws IOException {
ServerImpl server = new ServerImpl(
new ServiceConfig(
8080,
"http://localhost",
List.of("http://localhost"),
Path.of("/home/andrey/andrey/tmp")
));

server.start();
}
}
54 changes: 54 additions & 0 deletions src/main/java/ru/vk/itmo/test/andreycheshev/ServiceImpl.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
package ru.vk.itmo.test.andreycheshev;

import ru.vk.itmo.Service;
import ru.vk.itmo.ServiceConfig;
import ru.vk.itmo.test.ServiceFactory;

import java.io.IOException;
import java.io.UncheckedIOException;
import java.util.concurrent.CompletableFuture;

public class ServiceImpl implements Service {
private final ServiceConfig serviceConfig;
private ServerImpl server;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Вероятно, здесь будет достаточно HttpServer/Server

private boolean isActive = false;

public ServiceImpl(ServiceConfig config) {
this.serviceConfig = config;
initServer();
}

private void initServer() {
try {
server = new ServerImpl(serviceConfig);
} catch (IOException e) {
throw new UncheckedIOException(e);
}
isActive = true;
}

@Override
public CompletableFuture<Void> start() throws IOException {
if (!isActive) {
initServer();
}
server.start();
return CompletableFuture.completedFuture(null);
}

@Override
public CompletableFuture<Void> stop() throws IOException {
server.stop();
isActive = false;
return CompletableFuture.completedFuture(null);
}

@ServiceFactory(stage = 1)
public static class Factory implements ServiceFactory.Factory {

@Override
public Service create(ServiceConfig config) {
return new ServiceImpl(config);
}
}
}
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package ru.vk.itmo.test.reference.dao;
package ru.vk.itmo.test.andreycheshev.dao;

import java.io.IOException;
import java.lang.foreign.MemorySegment;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package ru.vk.itmo.test.reference.dao;
package ru.vk.itmo.test.andreycheshev.dao;

import ru.vk.itmo.dao.Entry;

Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package ru.vk.itmo.test.reference.dao;
package ru.vk.itmo.test.andreycheshev.dao;

import ru.vk.itmo.dao.Entry;

Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package ru.vk.itmo.test.reference.dao;
package ru.vk.itmo.test.andreycheshev.dao;

import java.lang.foreign.MemorySegment;
import java.lang.foreign.ValueLayout;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package ru.vk.itmo.test.reference.dao;
package ru.vk.itmo.test.andreycheshev.dao;

import ru.vk.itmo.dao.Entry;

Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package ru.vk.itmo.test.reference.dao;
package ru.vk.itmo.test.andreycheshev.dao;

import ru.vk.itmo.dao.Config;
import ru.vk.itmo.dao.Dao;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package ru.vk.itmo.test.reference.dao;
package ru.vk.itmo.test.andreycheshev.dao;

import ru.vk.itmo.dao.BaseEntry;
import ru.vk.itmo.dao.Entry;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package ru.vk.itmo.test.reference.dao;
package ru.vk.itmo.test.andreycheshev.dao;

import ru.vk.itmo.dao.Entry;

Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package ru.vk.itmo.test.reference.dao;
package ru.vk.itmo.test.andreycheshev.dao;

import java.io.IOException;
import java.io.UncheckedIOException;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package ru.vk.itmo.test.reference.dao;
package ru.vk.itmo.test.andreycheshev.dao;

import ru.vk.itmo.dao.Entry;

Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package ru.vk.itmo.test.reference.dao;
package ru.vk.itmo.test.andreycheshev.dao;

import ru.vk.itmo.dao.Entry;

Expand Down
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Original file line number Diff line number Diff line change
@@ -0,0 +1,95 @@
Value Percentile TotalCount 1/(1-Percentile)

0.102 0.000000 1 1.00
0.547 0.100000 28095 1.11
0.815 0.200000 56098 1.25
1.079 0.300000 84182 1.43
1.335 0.400000 112210 1.67
1.596 0.500000 140265 2.00
1.735 0.550000 154275 2.22
1.885 0.600000 168297 2.50
2.055 0.650000 182420 2.86
2.263 0.700000 196357 3.33
2.561 0.750000 210425 4.00
2.791 0.775000 217382 4.44
3.153 0.800000 224402 5.00
3.775 0.825000 231400 5.71
4.739 0.850000 238424 6.67
6.083 0.875000 245428 8.00
6.927 0.887500 248930 8.89
7.975 0.900000 252439 10.00
9.183 0.912500 255944 11.43
10.695 0.925000 259465 13.33
12.575 0.937500 262961 16.00
13.727 0.943750 264718 17.78
14.887 0.950000 266470 20.00
16.375 0.956250 268222 22.86
18.335 0.962500 269966 26.67
20.863 0.968750 271724 32.00
22.415 0.971875 272602 35.56
24.127 0.975000 273474 40.00
25.791 0.978125 274353 45.71
27.519 0.981250 275233 53.33
29.503 0.984375 276108 64.00
30.527 0.985938 276540 71.11
31.711 0.987500 276979 80.00
33.087 0.989062 277417 91.43
34.847 0.990625 277857 106.67
36.703 0.992188 278299 128.00
37.759 0.992969 278514 142.22
38.847 0.993750 278734 160.00
39.967 0.994531 278951 182.86
41.055 0.995313 279170 213.33
42.463 0.996094 279393 256.00
43.391 0.996484 279499 284.44
44.639 0.996875 279609 320.00
45.727 0.997266 279718 365.71
46.655 0.997656 279827 426.67
47.679 0.998047 279939 512.00
48.287 0.998242 279993 568.89
49.087 0.998437 280047 640.00
49.951 0.998633 280103 731.43
50.655 0.998828 280157 853.33
52.383 0.999023 280211 1024.00
54.143 0.999121 280239 1137.78
55.615 0.999219 280265 1280.00
56.863 0.999316 280293 1462.86
57.951 0.999414 280321 1706.67
59.103 0.999512 280348 2048.00
60.735 0.999561 280361 2275.56
62.239 0.999609 280375 2560.00
63.327 0.999658 280389 2925.71
65.055 0.999707 280402 3413.33
66.047 0.999756 280416 4096.00
66.559 0.999780 280425 4551.11
66.943 0.999805 280430 5120.00
67.583 0.999829 280437 5851.43
68.351 0.999854 280443 6826.67
69.311 0.999878 280450 8192.00
69.823 0.999890 280454 9102.22
70.271 0.999902 280457 10240.00
70.719 0.999915 280462 11702.86
71.039 0.999927 280464 13653.33
71.551 0.999939 280467 16384.00
71.871 0.999945 280469 18204.44
72.127 0.999951 280472 20480.00
72.255 0.999957 280473 23405.71
72.383 0.999963 280474 27306.67
72.575 0.999969 280476 32768.00
72.639 0.999973 280477 36408.89
72.703 0.999976 280478 40960.00
72.767 0.999979 280479 46811.43
72.767 0.999982 280479 54613.33
72.831 0.999985 280480 65536.00
72.895 0.999986 280482 72817.78
72.895 0.999988 280482 81920.00
72.895 0.999989 280482 93622.86
72.895 0.999991 280482 109226.67
72.895 0.999992 280482 131072.00
72.959 0.999993 280483 145635.56
72.959 0.999994 280483 163840.00
72.959 0.999995 280483 187245.71
72.959 0.999995 280483 218453.33
72.959 0.999996 280483 262144.00
73.087 0.999997 280484 291271.11
73.087 1.000000 280484 inf
Loading
Loading