-
Notifications
You must be signed in to change notification settings - Fork 47
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 #196
Merged
Merged
Changes from 18 commits
Commits
Show all changes
37 commits
Select commit
Hold shift + click to select a range
caa3b3b
1
Handiesto 3af5bd4
cc1
Handiesto daef6f8
cc2
Handiesto c0481f0
cc3
Handiesto 36d6d2a
cc4
Handiesto 1cda826
reports
Handiesto 42a949a
reports_fix
Handiesto 9653834
Merge branch 'main' into main
incubos 25ff852
Merge branch 'main' into main
incubos 9b5fe6b
hw2
Handiesto 810edff
hw2_2
Handiesto beeb6c9
сс
Handiesto e4db067
Merge remote-tracking branch 'upstream/main' into hw2
Handiesto abbe08d
report2
Handiesto bcdf9fb
Merge branch 'main' into hw2
atimofeyev 5f7f9b0
Merge remote-tracking branch 'upstream/main' into hw2
Handiesto 60e8135
hw5
Handiesto c4f5480
Merge remote-tracking branch 'origin/hw2' into hw2
Handiesto 517208e
hw5_сс
Handiesto 451452c
hw5_сс1
Handiesto df94762
hw5_сс1
Handiesto ef914b3
hw5_сс34
Handiesto 5568cf8
hw5_сс35
Handiesto 292deb2
Merge branch 'main' into hw2
incubos 644ca83
report
Handiesto 45db18d
Merge remote-tracking branch 'origin/hw2' into hw2
Handiesto 20a777e
Merge remote-tracking branch 'upstream/main' into hw2
Handiesto e4f4c07
hw6
Handiesto 741bab9
hw6.1
Handiesto 231b1df
hw6.2
Handiesto 60e7694
hw6.3
Handiesto ab49f24
hw6.4
Handiesto 990fc13
hw6.5
Handiesto 8e0f8ed
hw6.5
Handiesto f2a3422
hw6.6
Handiesto dd7b3ed
hw6.7
Handiesto 3532b14
Merge branch 'main' into hw2
atimofeyev File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
14 changes: 14 additions & 0 deletions
14
src/main/java/ru/vk/itmo/test/klimplyasov/HandleResult.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
package ru.vk.itmo.test.klimplyasov; | ||
|
||
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); | ||
} | ||
} |
74 changes: 74 additions & 0 deletions
74
src/main/java/ru/vk/itmo/test/klimplyasov/MergeHandleResult.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,74 @@ | ||
package ru.vk.itmo.test.klimplyasov; | ||
|
||
import one.nio.http.HttpSession; | ||
import one.nio.http.Response; | ||
import org.slf4j.Logger; | ||
import org.slf4j.LoggerFactory; | ||
|
||
import java.io.IOException; | ||
import java.net.HttpURLConnection; | ||
import java.util.concurrent.atomic.AtomicInteger; | ||
|
||
public class MergeHandleResult { | ||
private static final Logger log = LoggerFactory.getLogger(MergeHandleResult.class); | ||
private final HandleResult[] handleResults; | ||
private final AtomicInteger totalCount; | ||
private final AtomicInteger validCount; | ||
private final int ackThreshold; | ||
private final int totalExpected; | ||
private final HttpSession currentSession; | ||
|
||
public MergeHandleResult(HttpSession session, int size, int ackThreshold) { | ||
this.currentSession = session; | ||
this.handleResults = new HandleResult[size]; | ||
this.totalCount = new AtomicInteger(); | ||
this.validCount = new AtomicInteger(); | ||
this.ackThreshold = ackThreshold; | ||
this.totalExpected = size; | ||
} | ||
|
||
public boolean add(int index, HandleResult handleResult) { | ||
handleResults[index] = handleResult; | ||
int valid = handleResult.status() == HttpURLConnection.HTTP_OK | ||
|| handleResult.status() == HttpURLConnection.HTTP_CREATED | ||
|| handleResult.status() == HttpURLConnection.HTTP_ACCEPTED | ||
|| handleResult.status() == HttpURLConnection.HTTP_NOT_FOUND | ||
? validCount.incrementAndGet() : validCount.get(); | ||
if (valid >= ackThreshold || totalCount.incrementAndGet() == totalExpected) { | ||
sendResult(); | ||
return true; | ||
} | ||
return false; | ||
} | ||
|
||
private void sendResult() { | ||
HandleResult mergedResult = new HandleResult(HttpURLConnection.HTTP_GATEWAY_TIMEOUT, null); | ||
int validCount = 0; | ||
Handiesto marked this conversation as resolved.
Show resolved
Hide resolved
|
||
for (HandleResult handleResult : handleResults) { | ||
if (handleResult.status() == HttpURLConnection.HTTP_OK | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. а тут NPE потенциальный, массив может заполняться не по порядку There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Понял, исправлю |
||
|| handleResult.status() == HttpURLConnection.HTTP_CREATED | ||
|| handleResult.status() == HttpURLConnection.HTTP_ACCEPTED | ||
|| handleResult.status() == HttpURLConnection.HTTP_NOT_FOUND) { | ||
validCount++; | ||
if (mergedResult.timestamp() <= handleResult.timestamp()) { | ||
mergedResult = handleResult; | ||
} | ||
} | ||
} | ||
try { | ||
if (validCount < ackThreshold) { | ||
currentSession.sendResponse(new Response(Response.GATEWAY_TIMEOUT, Response.EMPTY)); | ||
} else { | ||
currentSession.sendResponse(new Response(String.valueOf(mergedResult.status()), mergedResult.data())); | ||
} | ||
} catch (Exception e) { | ||
log.error("Exception during handleRequest", e); | ||
try { | ||
currentSession.sendResponse(new Response(Response.INTERNAL_ERROR, Response.EMPTY)); | ||
} catch (IOException ex) { | ||
log.error("Exception while sending close connection", e); | ||
currentSession.scheduleClose(); | ||
} | ||
} | ||
} | ||
} |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
здесь могут быть проблемы в многопоточной среде, чтение/запись в массив идет из разных потоков. В таком случае просто final ссылка не поможет. Нужны какие-то примитивы синхронизации.
Подойдет как какая-нибудь потокобезопасная коллекция, так и AtomicReferenceArray