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

Тимофеев Кирилл, ИТМО DWS, Stage 5 #171

Merged
merged 60 commits into from
May 10, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
60 commits
Select commit Hold shift + click to select a range
ff5d54a
add scratch
NoGe4Ek Feb 20, 2024
556d022
task 1
NoGe4Ek Feb 21, 2024
56fb7e6
task 1
NoGe4Ek Feb 21, 2024
84f4a21
task 1
NoGe4Ek Feb 21, 2024
aed8a67
task 1
NoGe4Ek Feb 21, 2024
2ca29d4
task 1
NoGe4Ek Feb 21, 2024
5e7d22d
task 1
NoGe4Ek Feb 21, 2024
78d545e
Merge branch 'main' into feature/task1
incubos Feb 25, 2024
0411733
Merge branch 'polis-vk:main' into feature/task1
NoGe4Ek Feb 26, 2024
e3b9f99
Merge branch 'main' into feature/task2
NoGe4Ek Feb 28, 2024
0225f8a
task 2
NoGe4Ek Feb 28, 2024
bd639b4
task 2
NoGe4Ek Feb 28, 2024
44ac9e8
task 2
NoGe4Ek Feb 28, 2024
d22b117
task 2
NoGe4Ek Feb 28, 2024
5d47f5b
task 2
NoGe4Ek Mar 3, 2024
eb15ec7
task 2
NoGe4Ek Mar 3, 2024
bcb8433
task 2
NoGe4Ek Mar 5, 2024
2cec7fe
task 2
NoGe4Ek Mar 10, 2024
19f0548
task 2
NoGe4Ek Mar 10, 2024
d85275d
Merge remote-tracking branch 'origin/main' into feature/task3
NoGe4Ek Mar 10, 2024
8548fca
task 3
NoGe4Ek Mar 12, 2024
29d33fe
task 3
NoGe4Ek Mar 12, 2024
d1eaaff
task 3
NoGe4Ek Mar 12, 2024
0279872
task 3
NoGe4Ek Mar 12, 2024
2552ed4
task 3
NoGe4Ek Mar 18, 2024
4fe0431
task 3
NoGe4Ek Mar 18, 2024
5dad83c
task 3
NoGe4Ek Mar 18, 2024
e2bb50e
task 4
NoGe4Ek Mar 26, 2024
e334599
Merge remote-tracking branch 'origin/main' into feature/task4
NoGe4Ek Mar 26, 2024
e87321d
task 4
NoGe4Ek Mar 26, 2024
dd10123
task 4
NoGe4Ek Mar 26, 2024
6ed5014
Merge branch 'main' into feature/task4
incubos Mar 28, 2024
abf5ebd
task 4
NoGe4Ek Mar 31, 2024
26ede68
task 4
NoGe4Ek Mar 31, 2024
d95fe92
Merge remote-tracking branch 'origin/feature/task4' into feature/task4
NoGe4Ek Mar 31, 2024
74bfa51
task 4
NoGe4Ek Apr 2, 2024
53545c2
task 4
NoGe4Ek Apr 2, 2024
a6996fd
task 4
NoGe4Ek Apr 2, 2024
bd61886
Merge branch 'main' into feature/task4
pashchenko8 Apr 6, 2024
a029579
Merge branch 'main' into feature/task4
pashchenko8 Apr 6, 2024
84afde1
Merge remote-tracking branch 'origin/main' into feature/task4
NoGe4Ek Apr 10, 2024
6a3d816
task 4
NoGe4Ek Apr 10, 2024
2196790
task 5
NoGe4Ek Apr 10, 2024
e0da691
task 5
NoGe4Ek Apr 10, 2024
e636b9c
task 5
NoGe4Ek Apr 10, 2024
ff6ee13
task 5
NoGe4Ek Apr 10, 2024
9fe55dd
task 5
NoGe4Ek Apr 10, 2024
81a2811
Merge branch 'main' into feature/task5
incubos Apr 13, 2024
341af6e
task 5
NoGe4Ek Apr 15, 2024
b838378
Merge remote-tracking branch 'origin/main' into feature/task5
NoGe4Ek Apr 15, 2024
9b68f07
Merge remote-tracking branch 'origin/feature/task5' into feature/task5
NoGe4Ek Apr 15, 2024
03460f5
task 5
NoGe4Ek Apr 15, 2024
334ea70
task 5
NoGe4Ek Apr 15, 2024
ed348af
Merge remote-tracking branch 'origin/main' into feature/task5
NoGe4Ek Apr 23, 2024
68ceb42
Merge branch 'main' into feature/task5
incubos Apr 28, 2024
cfee744
Merge branch 'main' into feature/task5
incubos Apr 30, 2024
181daaa
Merge remote-tracking branch 'origin/feature/task5' into feature/task5
NoGe4Ek May 2, 2024
7972e05
task 6
NoGe4Ek May 3, 2024
aae3cd6
task 6
NoGe4Ek May 3, 2024
e894c8b
Merge branch 'main' into feature/task5
incubos May 10, 2024
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
package ru.vk.itmo.test.timofeevkirill.reference;

import one.nio.http.HttpSession;
import one.nio.http.Response;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.io.IOException;

public final class ExceptionUtils {
private static final Logger log = LoggerFactory.getLogger(ExceptionUtils.class);

public static void handleErrorFromHandleRequest(Exception e, HttpSession session) {
log.error("Exception during handleRequest", e);
try {
session.sendResponse(new Response(Response.INTERNAL_ERROR, Response.EMPTY));
} catch (IOException ex) {
log.error("Exception while sending close connection", ex);
session.scheduleClose();
}
}

private ExceptionUtils() {

}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
package ru.vk.itmo.test.timofeevkirill.reference;

public record HandleResult(int status, byte[] data, long timestamp) {

public HandleResult(int status, byte[] data, long timestamp) {
this.status = status;
this.data = data;
this.timestamp = timestamp;
}

public HandleResult(int status, byte[] data) {
this(status, data, 0);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,84 @@
package ru.vk.itmo.test.timofeevkirill.reference;

import one.nio.http.HttpSession;
import one.nio.http.Response;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.net.HttpURLConnection;
import java.util.Queue;
import java.util.concurrent.CompletableFuture;
import java.util.concurrent.ConcurrentLinkedQueue;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.atomic.AtomicInteger;

public class MergeHandleResult {
private static final Logger log = LoggerFactory.getLogger(MergeHandleResult.class);
private final Queue<CompletableFuture<HandleResult>> resultFutures;
private final Queue<HandleResult> results;
private final AtomicInteger count;
private final int ack;
private final int from;
private final HttpSession session;

public MergeHandleResult(HttpSession session, int size, int ack) {
this.session = session;
this.resultFutures = new ConcurrentLinkedQueue<>();
this.results = new ConcurrentLinkedQueue<>();
this.count = new AtomicInteger();
this.ack = ack;
this.from = size;
}

@SuppressWarnings("FutureReturnValueIgnored")
public void add(CompletableFuture<HandleResult> resultFuture, ExecutorService executor) {
resultFutures.add(resultFuture);
resultFuture.whenCompleteAsync((result, e) -> {
if (e == null) {
results.add(result);
int get = count.incrementAndGet();
if (get == from) {
sendResult();
cancelRemainingFutures();
}
} else {
log.error("Error remote handle result: " + e);
}
}, executor);
}

private void cancelRemainingFutures() {
CompletableFuture<HandleResult> future;
while ((future = resultFutures.poll()) != null) {
future.cancel(true);
}
}

private void sendResult() {
HandleResult mergedResult = new HandleResult(HttpURLConnection.HTTP_GATEWAY_TIMEOUT, null);

int positiveCount = 0;
for (HandleResult handleResult : results) {
if (handleResult.status() == HttpURLConnection.HTTP_OK
|| handleResult.status() == HttpURLConnection.HTTP_CREATED
|| handleResult.status() == HttpURLConnection.HTTP_ACCEPTED
|| handleResult.status() == HttpURLConnection.HTTP_NOT_FOUND) {
positiveCount++;
if (mergedResult.timestamp() <= handleResult.timestamp()) {
mergedResult = handleResult;
}
}
}

try {
if (positiveCount < ack) {
session.sendResponse(new Response(Response.GATEWAY_TIMEOUT, Response.EMPTY));
} else {
session.sendResponse(new Response(String.valueOf(mergedResult.status()), mergedResult.data()));
}
} catch (Exception e) {
ExceptionUtils.handleErrorFromHandleRequest(e, session);
}

}
}
Loading
Loading