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

Осокин Дмитрий Hw6 Политех #216

Merged
merged 73 commits into from
May 25, 2024
Merged
Show file tree
Hide file tree
Changes from 71 commits
Commits
Show all changes
73 commits
Select commit Hold shift + click to select a range
0ab3565
hw1 first try
osokindm Feb 18, 2024
cdd1344
hw1 first try
osokindm Feb 20, 2024
0bc4a3c
hw1 first try
osokindm Feb 21, 2024
2850402
Merge remote-tracking branch 'upstream/main' into hw2
osokindm Feb 24, 2024
5953064
hw2 first try
osokindm Feb 28, 2024
44fa83b
hw2 first try
osokindm Feb 28, 2024
dd8325d
hw2 string templates removed
osokindm Feb 29, 2024
85b3b03
hw2 codeclimate
osokindm Feb 29, 2024
7e63897
hw2 codeclimate
osokindm Feb 29, 2024
f04d085
hw2 report
osokindm Mar 6, 2024
361e242
Merge remote-tracking branch 'upstream/main' into hw2
osokindm Mar 6, 2024
1cb5596
Merge remote-tracking branch 'upstream/main' into hw3
osokindm Mar 10, 2024
20e5d6f
Merge remote-tracking branch 'upstream/main' into hw3
osokindm Mar 11, 2024
0e4af77
hw3 codeDrop
osokindm Mar 11, 2024
88dc906
hw3 codeDrop
osokindm Mar 12, 2024
3b9167a
hw3 some fixes
osokindm Mar 12, 2024
161db28
hw3 test fixes and changes using ref
osokindm Mar 13, 2024
ac1c7bf
Merge remote-tracking branch 'upstream/main' into hw3
osokindm Mar 13, 2024
6ae531e
hw3 codeclimate fixes
osokindm Mar 13, 2024
61fe433
hw3 codeclimate fixes
osokindm Mar 13, 2024
f4de8b4
Merge remote-tracking branch 'upstream/main' into hw3
osokindm Mar 18, 2024
d2dc175
hw3 report
osokindm Mar 20, 2024
c67d6a5
hw3 report
osokindm Mar 20, 2024
7bf82ce
Merge remote-tracking branch 'upstream/main' into hw3
osokindm Mar 23, 2024
fa14300
hw4 codeDrop
osokindm Mar 27, 2024
bf8d5cf
Merge remote-tracking branch 'upstream/main' into hw4
osokindm Mar 27, 2024
6e337c9
hw4 codeDrop
osokindm Mar 27, 2024
969e9aa
hw4 works
osokindm Mar 27, 2024
9adbad8
hw4 codeclimate
osokindm Mar 27, 2024
14b117e
hw4 report
osokindm Apr 3, 2024
e514f08
hw4 report
osokindm Apr 3, 2024
2c295d7
Merge remote-tracking branch 'upstream/main' into hw4
osokindm Apr 6, 2024
eefc94d
codeDrop
osokindm Apr 9, 2024
847631a
codeDrop
osokindm Apr 9, 2024
d0d6188
codeDrop
osokindm Apr 10, 2024
aca5ac7
codeDrop
osokindm Apr 10, 2024
19bc6c5
codeDrop
osokindm Apr 10, 2024
101be9e
two tests not working
osokindm Apr 10, 2024
3d24854
green tests
osokindm Apr 10, 2024
3f534b2
Merge branch 'main' into hw5
osokindm Apr 10, 2024
ec9764e
started refactoring
osokindm Apr 10, 2024
f5b7bfe
Merge remote-tracking branch 'origin/hw5' into hw5
osokindm Apr 10, 2024
618b738
refactoring
osokindm Apr 11, 2024
232e37d
code climate
osokindm Apr 11, 2024
e30ea9a
code climate
osokindm Apr 11, 2024
4fea109
code climate
osokindm Apr 11, 2024
fc29758
code climate
osokindm Apr 11, 2024
5cdd876
code climate
osokindm Apr 11, 2024
29e0ec9
code climate
osokindm Apr 11, 2024
84be982
code climate
osokindm Apr 11, 2024
f35bf92
code climate
osokindm Apr 11, 2024
9cab995
code climate
osokindm Apr 11, 2024
24e1725
Merge branch 'main' into hw5
incubos Apr 13, 2024
411ed9b
hw5 report
osokindm Apr 17, 2024
960f2b8
Merge remote-tracking branch 'origin/hw5' into hw5
osokindm Apr 17, 2024
5263319
Merge remote-tracking branch 'upstream/main' into hw5
osokindm Apr 22, 2024
54d892a
hw5 fixes, not all yet
osokindm Apr 23, 2024
1051c92
hw5 fixes, not all yet
osokindm Apr 23, 2024
03d1f7b
hw5 fixes, not all yet
osokindm Apr 23, 2024
6f35013
hw5 fixes
osokindm Apr 23, 2024
afc1dbc
hw5 climate
osokindm Apr 24, 2024
df974df
hw5 climate
osokindm Apr 24, 2024
21ea0d2
hw5 climate
osokindm Apr 24, 2024
be0a6b4
hw6 wip
osokindm Apr 24, 2024
305e85a
hw6 wip
osokindm Apr 24, 2024
68e09ba
hw6
osokindm Apr 25, 2024
70bead5
hw6
osokindm Apr 25, 2024
f1f98f2
Merge branch 'main' into hw6
incubos Apr 28, 2024
dfa8715
hw6 report'ik
osokindm May 1, 2024
ad78207
Merge remote-tracking branch 'origin/hw6' into hw6
osokindm May 1, 2024
b9e28ad
hw6 report'ik
osokindm May 1, 2024
7f841ba
Merge branch 'main' into hw6
lamtev May 9, 2024
b184ffb
Merge branch 'main' into hw6
incubos May 25, 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
49 changes: 0 additions & 49 deletions .gitignore

This file was deleted.

76 changes: 0 additions & 76 deletions build.gradle

This file was deleted.

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

import one.nio.http.Response;
import ru.vk.itmo.test.osokindm.dao.Entry;

import java.lang.foreign.MemorySegment;
import java.util.Iterator;

public class ChunkedResponse extends Response {

private static final String CHUNKED_HEADER = "Transfer-Encoding: chunked";
private static final String CONTENT_TYPE_HEADER = "Content-Type: result-range";
private final Iterator<Entry<MemorySegment>> rangeResult;

public ChunkedResponse(String resultCode, Iterator<Entry<MemorySegment>> rangeResult) {
super(resultCode);
super.addHeader(CHUNKED_HEADER);
super.addHeader(CONTENT_TYPE_HEADER);
this.rangeResult = rangeResult;
}

public Iterator<Entry<MemorySegment>> getResultIterator() {
return rangeResult;
}

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

import one.nio.http.HttpServer;
import one.nio.http.HttpSession;
import one.nio.http.Response;
import one.nio.net.Socket;
import ru.vk.itmo.test.osokindm.dao.Entry;

import java.io.IOException;
import java.lang.foreign.MemorySegment;
import java.lang.foreign.ValueLayout;
import java.nio.ByteBuffer;
import java.nio.charset.StandardCharsets;
import java.util.Iterator;

public class CustomHttpSession extends HttpSession {

private static final byte[] CHUNK_SEPARATOR = "\r\n".getBytes(StandardCharsets.UTF_8);
private static final byte[] DELIMITER = "\n".getBytes(StandardCharsets.UTF_8);
private static final byte[] ZERO = "0".getBytes(StandardCharsets.UTF_8);

public CustomHttpSession(Socket socket, HttpServer server) {
super(socket, server);
}

@Override
protected void writeResponse(Response response, boolean includeBody) throws IOException {
if (response instanceof ChunkedResponse) {
Copy link
Member

Choose a reason for hiding this comment

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

Здесь имело смысл воспользоваться Pattern Matching for instanceof

super.writeResponse(response, false);
Iterator<Entry<MemorySegment>> iterator = ((ChunkedResponse) response).getResultIterator();
while (iterator.hasNext()) {
writeChunk(iterator.next());
}

super.write(ZERO, 0, ZERO.length);
super.write(CHUNK_SEPARATOR, 0, CHUNK_SEPARATOR.length);
super.write(CHUNK_SEPARATOR, 0, CHUNK_SEPARATOR.length);
super.close();
} else {
super.writeResponse(response, includeBody);
}
}

private void writeChunk(Entry<MemorySegment> entry) throws IOException {
byte[] keyBytes = entry.key().toArray(ValueLayout.JAVA_BYTE);
byte[] valueBytes = entry.value().toArray(ValueLayout.JAVA_BYTE);
int entryLength = keyBytes.length + valueBytes.length + DELIMITER.length;
ByteBuffer buffer = ByteBuffer.allocate(Integer.BYTES + entryLength + CHUNK_SEPARATOR.length * 2);
buffer.put(Integer.toHexString(entryLength).getBytes(StandardCharsets.UTF_8));
buffer.put(CHUNK_SEPARATOR);
buffer.put(entry.key().toArray(ValueLayout.JAVA_BYTE));
buffer.put(DELIMITER);
buffer.put(entry.value().toArray(ValueLayout.JAVA_BYTE));
buffer.put(CHUNK_SEPARATOR);

super.write(buffer.array(), 0, buffer.position());
}

}
19 changes: 13 additions & 6 deletions src/main/java/ru/vk/itmo/test/osokindm/DaoWrapper.java
Original file line number Diff line number Diff line change
@@ -1,14 +1,15 @@
package ru.vk.itmo.test.osokindm;

import one.nio.http.Request;
import ru.vk.itmo.dao.BaseEntry;
import ru.vk.itmo.dao.Config;
import ru.vk.itmo.dao.Entry;
import ru.vk.itmo.test.osokindm.dao.BaseEntry;
import ru.vk.itmo.test.osokindm.dao.Entry;
import ru.vk.itmo.test.osokindm.dao.ReferenceDao;

import java.io.IOException;
import java.lang.foreign.MemorySegment;
import java.nio.charset.StandardCharsets;
import java.util.Iterator;

public class DaoWrapper {

Expand All @@ -23,15 +24,21 @@ public Entry<MemorySegment> get(String id) {
return storage.get(key);
}

public void delete(String id) {
public Iterator<Entry<MemorySegment>> get(String start, String end) {
MemorySegment startKey = getMemorySegment(start);
MemorySegment endKey = getMemorySegment(end);
return storage.get(startKey, endKey);
}

public void delete(String id, long timestamp) {
MemorySegment key = getMemorySegment(id);
storage.upsert(new BaseEntry<>(key, null));
storage.upsert(new BaseEntry<>(key, null, timestamp));
}

public void upsert(String id, Request request) {
public void upsert(String id, Request request, long timestamp) {
MemorySegment key = getMemorySegment(id);
MemorySegment value = getMemorySegment(request.getBody());
storage.upsert(new BaseEntry<>(key, value));
storage.upsert(new BaseEntry<>(key, value, timestamp));
}

public void stop() throws IOException {
Expand Down
11 changes: 10 additions & 1 deletion src/main/java/ru/vk/itmo/test/osokindm/HttpServerImpl.java
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@
import one.nio.http.HttpSession;
import one.nio.http.Request;
import one.nio.http.Response;
import one.nio.net.Socket;
import one.nio.server.RejectedSessionException;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

Expand All @@ -18,6 +20,7 @@
public class HttpServerImpl extends HttpServer {

private static final String ID_REQUEST = "id=";
private static final String START_REQUEST = "start=";
private static final long KEEP_ALIVE_TIME = 60L;
private static final Logger LOGGER = LoggerFactory.getLogger(HttpServerImpl.class);
private final ThreadPoolExecutor requestWorkers;
Expand All @@ -34,6 +37,11 @@ public HttpServerImpl(HttpServerConfig config) throws IOException {
requestWorkers.prestartAllCoreThreads();
}

@Override
public HttpSession createSession(Socket socket) throws RejectedSessionException {
return new CustomHttpSession(socket, this);
}

@Override
public synchronized void stop() {
super.stop();
Expand All @@ -52,7 +60,8 @@ public synchronized void stop() {
@Override
public void handleRequest(Request request, HttpSession session) {
String id = request.getParameter(ID_REQUEST);
if (id == null || id.isEmpty()) {
String start = request.getParameter(START_REQUEST);
if ((id == null || id.isEmpty()) && (start == null || start.isEmpty())) {
try {
handleDefault(request, session);
} catch (IOException e) {
Expand Down
2 changes: 1 addition & 1 deletion src/main/java/ru/vk/itmo/test/osokindm/Node.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,9 @@
import java.util.concurrent.atomic.AtomicInteger;

public class Node {
private static final int MAX_ERRORS = 10;
public final String address;
public final int name;
private static final int MAX_ERRORS = 10;
private final AtomicInteger errors;
private volatile boolean isAlive;

Expand Down
23 changes: 23 additions & 0 deletions src/main/java/ru/vk/itmo/test/osokindm/RendezvousRouter.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,9 @@
import one.nio.util.Hash;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.TreeMap;

public class RendezvousRouter {

Expand Down Expand Up @@ -32,4 +34,25 @@ public Node getNode(String key) {
return maxHashNode;
}

@SuppressWarnings("MixedMutabilityReturnType")
public List<Node> getNodes(String key, int nodeAmount) {
if (key == null) {
return Collections.emptyList();
}
TreeMap<Integer, Node> sortedNodes = new TreeMap<>();
for (Node node : nodes) {
int hash = Hash.murmur3(node.name + key);
sortedNodes.put(hash, node);
}
List<Node> selectedNodes = new ArrayList<>();
int nodesNeeded = nodeAmount;
for (Node node : sortedNodes.values()) {
if (nodesNeeded == 0) break;
selectedNodes.add(node);
nodesNeeded--;
}

return selectedNodes;
}

}
Loading
Loading