Skip to content

Commit f7c65c5

Browse files
SicMundus-satimofeyev
authored andcommitted
Волков Никита, ИТМО DWS, Stage 1 (polis-vk#17)
* init commit * data research result * result * refactoring --------- Co-authored-by: atimofeyev <andrey.timofeyev@gmail.com>
1 parent 79dbf0a commit f7c65c5

14 files changed

+3079
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,105 @@
1+
package ru.vk.itmo.test.volkovnikita;
2+
3+
import one.nio.http.HttpServer;
4+
import one.nio.http.HttpServerConfig;
5+
import one.nio.http.HttpSession;
6+
import one.nio.http.Param;
7+
import one.nio.http.Path;
8+
import one.nio.http.Request;
9+
import one.nio.http.RequestMethod;
10+
import one.nio.http.Response;
11+
import one.nio.server.AcceptorConfig;
12+
import ru.vk.itmo.ServiceConfig;
13+
import ru.vk.itmo.dao.BaseEntry;
14+
import ru.vk.itmo.dao.Dao;
15+
import ru.vk.itmo.dao.Entry;
16+
import ru.vk.itmo.test.reference.dao.ReferenceDao;
17+
18+
import java.io.IOException;
19+
import java.lang.foreign.MemorySegment;
20+
import java.lang.foreign.ValueLayout;
21+
import java.util.Set;
22+
23+
public class HttpServerImpl extends HttpServer {
24+
25+
private final Dao<MemorySegment, Entry<MemorySegment>> dao;
26+
27+
static final Set<String> METHODS = Set.of(
28+
"GET",
29+
"PUT",
30+
"DELETE"
31+
);
32+
33+
public HttpServerImpl(ServiceConfig config, ReferenceDao dao) throws IOException {
34+
super(createServerConfig(config));
35+
this.dao = dao;
36+
}
37+
38+
private static HttpServerConfig createServerConfig(ServiceConfig config) {
39+
HttpServerConfig serverConfig = new HttpServerConfig();
40+
AcceptorConfig acceptorConfig = new AcceptorConfig();
41+
acceptorConfig.port = config.selfPort();
42+
acceptorConfig.reusePort = true;
43+
44+
serverConfig.acceptors = new AcceptorConfig[]{acceptorConfig};
45+
serverConfig.closeSessions = true;
46+
return serverConfig;
47+
}
48+
49+
@Path(value = "/v0/status")
50+
public Response status() {
51+
return Response.ok("It's okay (c) Tinkoff");
52+
}
53+
54+
@Path(value = "/v0/entity")
55+
@RequestMethod(Request.METHOD_GET)
56+
public Response getEntry(@Param(value = "id", required = true) String id) {
57+
if (isIdUncorrect(id)) {
58+
return new Response(Response.BAD_REQUEST, Response.EMPTY);
59+
}
60+
MemorySegment key = MemorySegment.ofArray(id.toCharArray());
61+
Entry<MemorySegment> entry = dao.get(key);
62+
return entry == null ? new Response(Response.NOT_FOUND, Response.EMPTY) :
63+
new Response(Response.OK, entry.value().toArray(ValueLayout.JAVA_BYTE));
64+
}
65+
66+
@Path(value = "/v0/entity")
67+
@RequestMethod(Request.METHOD_PUT)
68+
public Response up(@Param(value = "id", required = true) String id, Request request) {
69+
if (isIdUncorrect(id)) {
70+
return new Response(Response.BAD_REQUEST, Response.EMPTY);
71+
}
72+
MemorySegment key = MemorySegment.ofArray(id.toCharArray());
73+
MemorySegment value = MemorySegment.ofArray(request.getBody());
74+
dao.upsert(new BaseEntry<>(key, value));
75+
return new Response(Response.CREATED, Response.EMPTY);
76+
}
77+
78+
@Path(value = "/v0/entity")
79+
@RequestMethod(Request.METHOD_DELETE)
80+
public Response deleteEntry(@Param(value = "id", required = true) String id) {
81+
if (isIdUncorrect(id)) {
82+
return new Response(Response.BAD_REQUEST, Response.EMPTY);
83+
}
84+
MemorySegment key = MemorySegment.ofArray(id.toCharArray());
85+
dao.upsert(new BaseEntry<>(key, null));
86+
return new Response(Response.ACCEPTED, Response.EMPTY);
87+
}
88+
89+
@Override
90+
public void handleDefault(Request request, HttpSession session) throws IOException {
91+
Response response;
92+
if (METHODS.contains(request.getMethodName())) {
93+
response = new Response(Response.BAD_REQUEST, Response.EMPTY);
94+
session.sendResponse(response);
95+
} else {
96+
response = new Response(Response.METHOD_NOT_ALLOWED, Response.EMPTY);
97+
session.sendResponse(response);
98+
}
99+
session.sendResponse(response);
100+
}
101+
102+
private boolean isIdUncorrect(String id) {
103+
return id == null || id.isEmpty() || id.isBlank();
104+
}
105+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
package ru.vk.itmo.test.volkovnikita;
2+
3+
import ru.vk.itmo.ServiceConfig;
4+
import ru.vk.itmo.dao.Config;
5+
import ru.vk.itmo.test.reference.dao.ReferenceDao;
6+
7+
import java.io.IOException;
8+
import java.nio.file.Files;
9+
import java.util.List;
10+
11+
public final class Server {
12+
13+
public static final long FLUSH_THRESHOLD_BYTES = 4 * 1024L;
14+
15+
private Server() {
16+
17+
}
18+
19+
public static void main(String[] args) throws IOException {
20+
ServiceConfig config = new ServiceConfig(
21+
8080,
22+
"http://localhost",
23+
List.of("http://localhost"),
24+
Files.createTempDirectory(".")
25+
);
26+
ReferenceDao dao = new ReferenceDao(new Config(config.workingDir(), FLUSH_THRESHOLD_BYTES));
27+
HttpServerImpl server = new HttpServerImpl(config, dao);
28+
server.start();
29+
}
30+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
package ru.vk.itmo.test.volkovnikita;
2+
3+
import ru.vk.itmo.Service;
4+
import ru.vk.itmo.ServiceConfig;
5+
import ru.vk.itmo.dao.Config;
6+
import ru.vk.itmo.test.ServiceFactory;
7+
import ru.vk.itmo.test.reference.dao.ReferenceDao;
8+
9+
import java.io.IOException;
10+
import java.util.concurrent.CompletableFuture;
11+
12+
public class ServiceImpl implements Service {
13+
public static final long FLUSH_THRESHOLD_BYTES = 4 * 1024L;
14+
15+
private HttpServerImpl server;
16+
private final ServiceConfig config;
17+
private ReferenceDao dao;
18+
19+
public ServiceImpl(ServiceConfig config) {
20+
this.config = config;
21+
}
22+
23+
@Override
24+
public synchronized CompletableFuture<Void> start() throws IOException {
25+
dao = new ReferenceDao(new Config(config.workingDir(), FLUSH_THRESHOLD_BYTES));
26+
server = new HttpServerImpl(config, dao);
27+
server.start();
28+
return CompletableFuture.completedFuture(null);
29+
}
30+
31+
@Override
32+
public synchronized CompletableFuture<Void> stop() throws IOException {
33+
server.stop();
34+
dao.close();
35+
return CompletableFuture.completedFuture(null);
36+
}
37+
38+
@ServiceFactory(stage = 1)
39+
public static class Factory implements ServiceFactory.Factory {
40+
41+
@Override
42+
public Service create(ServiceConfig config) {
43+
return new ServiceImpl(config);
44+
}
45+
}
46+
}
Loading
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,81 @@
1+
Value Percentile TotalCount 1/(1-Percentile)
2+
3+
0.289 0.000000 1 1.00
4+
0.608 0.100000 4007 1.11
5+
0.827 0.200000 8017 1.25
6+
1.020 0.300000 12022 1.43
7+
1.216 0.400000 16038 1.67
8+
1.401 0.500000 20050 2.00
9+
1.515 0.550000 22028 2.22
10+
1.709 0.600000 24031 2.50
11+
2.026 0.650000 26035 2.86
12+
2.627 0.700000 28035 3.33
13+
3.503 0.750000 30038 4.00
14+
4.019 0.775000 31043 4.44
15+
4.555 0.800000 32049 5.00
16+
5.139 0.825000 33042 5.71
17+
5.931 0.850000 34046 6.67
18+
6.775 0.875000 35046 8.00
19+
7.187 0.887500 35548 8.89
20+
7.655 0.900000 36045 10.00
21+
8.147 0.912500 36552 11.43
22+
8.767 0.925000 37050 13.33
23+
9.647 0.937500 37550 16.00
24+
10.087 0.943750 37800 17.78
25+
10.695 0.950000 38052 20.00
26+
11.367 0.956250 38299 22.86
27+
12.367 0.962500 38550 26.67
28+
13.207 0.968750 38799 32.00
29+
13.767 0.971875 38925 35.56
30+
14.263 0.975000 39051 40.00
31+
15.031 0.978125 39174 45.71
32+
15.999 0.981250 39300 53.33
33+
17.263 0.984375 39428 64.00
34+
17.647 0.985938 39490 71.11
35+
18.303 0.987500 39551 80.00
36+
19.311 0.989062 39612 91.43
37+
20.591 0.990625 39675 106.67
38+
22.159 0.992188 39739 128.00
39+
22.895 0.992969 39769 142.22
40+
23.839 0.993750 39800 160.00
41+
25.023 0.994531 39831 182.86
42+
26.191 0.995313 39864 213.33
43+
27.615 0.996094 39894 256.00
44+
28.399 0.996484 39910 284.44
45+
28.767 0.996875 39927 320.00
46+
28.927 0.997266 39942 365.71
47+
29.167 0.997656 39957 426.67
48+
29.599 0.998047 39972 512.00
49+
30.191 0.998242 39980 568.89
50+
30.415 0.998437 39988 640.00
51+
30.735 0.998633 39996 731.43
52+
31.711 0.998828 40004 853.33
53+
32.495 0.999023 40011 1024.00
54+
32.991 0.999121 40015 1137.78
55+
33.471 0.999219 40019 1280.00
56+
33.631 0.999316 40023 1462.86
57+
33.695 0.999414 40027 1706.67
58+
33.727 0.999512 40033 2048.00
59+
33.727 0.999561 40033 2275.56
60+
33.759 0.999609 40035 2560.00
61+
33.791 0.999658 40037 2925.71
62+
33.823 0.999707 40040 3413.33
63+
33.855 0.999756 40043 4096.00
64+
33.855 0.999780 40043 4551.11
65+
33.855 0.999805 40043 5120.00
66+
33.887 0.999829 40045 5851.43
67+
33.887 0.999854 40045 6826.67
68+
33.919 0.999878 40046 8192.00
69+
33.919 0.999890 40046 9102.22
70+
33.951 0.999902 40048 10240.00
71+
33.951 0.999915 40048 11702.86
72+
33.951 0.999927 40048 13653.33
73+
33.951 0.999939 40048 16384.00
74+
33.951 0.999945 40048 18204.44
75+
34.015 0.999951 40049 20480.00
76+
34.015 0.999957 40049 23405.71
77+
34.015 0.999963 40049 27306.67
78+
34.015 0.999969 40049 32768.00
79+
34.015 0.999973 40049 36408.89
80+
34.079 0.999976 40050 40960.00
81+
34.079 1.000000 40050 inf

0 commit comments

Comments
 (0)