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 6 #217

Closed
wants to merge 83 commits into from
Closed
Show file tree
Hide file tree
Changes from 76 commits
Commits
Show all changes
83 commits
Select commit Hold shift + click to select a range
fe2ee53
Test completed
alexBlack01 Feb 17, 2024
773e724
Add result
alexBlack01 Feb 18, 2024
e899083
Fix code style
alexBlack01 Feb 18, 2024
b3f5809
Fix code style
alexBlack01 Feb 18, 2024
a13a743
Fix code style
alexBlack01 Feb 18, 2024
da42951
Fix code style
alexBlack01 Feb 18, 2024
0029cbd
Add report
alexBlack01 Feb 21, 2024
25a784e
Add report
alexBlack01 Feb 21, 2024
87f77fb
Add report
alexBlack01 Feb 21, 2024
8355b8c
Report
alexBlack01 Feb 21, 2024
5913fc7
Report
alexBlack01 Feb 21, 2024
3141895
Merge branch 'main' into practice1
incubos Feb 25, 2024
30be026
Merge branch 'main' into practice1
incubos Feb 27, 2024
0783e70
Merge branch 'main' into practice1
incubos Feb 27, 2024
c8f98e2
Merge branch 'main' into practice1
incubos Feb 27, 2024
8c91869
Fix with report
alexBlack01 Feb 28, 2024
b49a242
Merge remote-tracking branch 'origin/practice1' into practice1
alexBlack01 Feb 28, 2024
149bc54
Fix with report
alexBlack01 Feb 28, 2024
4a55642
Add code for stage 2
alexBlack01 Feb 28, 2024
c4bc01f
Add part of report
alexBlack01 Mar 6, 2024
74e6f56
Add last part of report
alexBlack01 Mar 6, 2024
21ef485
For build
alexBlack01 Mar 6, 2024
6dfb117
For build
alexBlack01 Mar 6, 2024
83ca308
For build
alexBlack01 Mar 6, 2024
1bf1d24
For build
alexBlack01 Mar 6, 2024
e835f26
Merge remote-tracking branch 'refs/remotes/upstream/main' into practice2
alexBlack01 Mar 6, 2024
42cfe60
Correcting code based on review
alexBlack01 Mar 13, 2024
939964f
Correcting code based on review
alexBlack01 Mar 13, 2024
c00bbb5
Merge remote-tracking branch 'refs/remotes/upstream/main' into practice3
alexBlack01 Mar 13, 2024
ab1f9d7
Sketches
alexBlack01 Mar 13, 2024
76c28ee
Sketches
alexBlack01 Mar 13, 2024
16e52a1
Stage 3
alexBlack01 Mar 14, 2024
7432db0
Fix bugs
alexBlack01 Mar 14, 2024
7cc056a
Add report
alexBlack01 Mar 20, 2024
c3268c5
Add report
alexBlack01 Mar 20, 2024
24018f5
Merge remote-tracking branch 'refs/remotes/upstream/main' into practice3
alexBlack01 Mar 20, 2024
dfbb2f4
Add report
alexBlack01 Mar 20, 2024
018b200
Fix hash
alexBlack01 Mar 27, 2024
5245bc1
Merge remote-tracking branch 'refs/remotes/upstream/main' into practice4
alexBlack01 Mar 27, 2024
48d2322
Change dao
alexBlack01 Mar 27, 2024
153bb7f
Add request handler
alexBlack01 Mar 27, 2024
62277c8
Add replication
alexBlack01 Mar 28, 2024
3241ea6
Fix
alexBlack01 Mar 28, 2024
5669c04
Fix
alexBlack01 Mar 28, 2024
37af132
Fix
alexBlack01 Mar 28, 2024
d7d30af
Fix
alexBlack01 Mar 28, 2024
2b41fcd
Fix
alexBlack01 Mar 28, 2024
820a894
Add part of report
alexBlack01 Apr 3, 2024
6cd09ce
Add part of report
alexBlack01 Apr 3, 2024
0b17813
Add part of report
alexBlack01 Apr 3, 2024
8b6f88c
Add part of report
alexBlack01 Apr 3, 2024
d0cf8a3
Add report
alexBlack01 Apr 3, 2024
48a0bce
Merge branch 'main' into practice4
pashchenko8 Apr 6, 2024
540b67c
Add solution
alexBlack01 Apr 9, 2024
f69d073
Add solution
alexBlack01 Apr 9, 2024
4ff81db
Merge remote-tracking branch 'refs/remotes/upstream/main' into practice5
alexBlack01 Apr 9, 2024
56e2a5d
Fix solution
alexBlack01 Apr 9, 2024
f583d82
Fix solution
alexBlack01 Apr 9, 2024
5aaa874
Fix solution
alexBlack01 Apr 9, 2024
bca9871
Fix solution
alexBlack01 Apr 9, 2024
dbd1e8d
Fix solution
alexBlack01 Apr 10, 2024
7f33f75
Fix solution
alexBlack01 Apr 10, 2024
fd54b14
Fix solution
alexBlack01 Apr 10, 2024
9a32617
Fix solution
alexBlack01 Apr 10, 2024
37a2fbd
Fix solution
alexBlack01 Apr 10, 2024
ba4137a
Fix solution
alexBlack01 Apr 11, 2024
2695558
Fix solution
alexBlack01 Apr 11, 2024
7437f7e
Fix solution
alexBlack01 Apr 11, 2024
1a8e41d
Fix solution
alexBlack01 Apr 11, 2024
d74c194
Merge branch 'main' into practice5
incubos Apr 13, 2024
4207e95
Add report
alexBlack01 Apr 17, 2024
09d5e2a
Merge remote-tracking branch 'origin/practice5' into practice5
alexBlack01 Apr 17, 2024
7eed192
Add report
alexBlack01 Apr 17, 2024
2e2cfe1
Merge remote-tracking branch 'refs/remotes/upstream/main' into practice6
alexBlack01 Apr 22, 2024
1f536c0
Add solution
alexBlack01 Apr 24, 2024
0fc0074
Fix code style
alexBlack01 Apr 24, 2024
902715f
Fix code style
alexBlack01 Apr 24, 2024
d042ada
Fix code style
alexBlack01 Apr 25, 2024
74692bb
Merge branch 'main' into practice6
incubos Apr 28, 2024
5cb8448
Report 6 stage
alexBlack01 May 1, 2024
6382e8f
Merge remote-tracking branch 'origin/practice6' into practice6
alexBlack01 May 1, 2024
81bc896
Report 6 stage
alexBlack01 May 1, 2024
6293d52
Merge remote-tracking branch 'refs/remotes/upstream/main' into practice6
alexBlack01 May 26, 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
Expand Up @@ -3,19 +3,19 @@
import one.nio.util.Hash;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.NavigableMap;
import java.util.SortedMap;
import java.util.TreeMap;
import java.util.stream.Collectors;

public class ConsistentHashing {
private final NavigableMap<Integer, String> circle;
private final List<String> clusterUrls;

public ConsistentHashing(List<String> clusterUrls, int numbOfVirtualNodes) {
circle = new TreeMap<>();

this.clusterUrls = clusterUrls;
for (String clusterUrl : clusterUrls) {
for (int i = 0; i < numbOfVirtualNodes; i++) {
addNode(i, clusterUrl);
Expand All @@ -39,28 +39,45 @@ public String getNode(String key) {
}

public List<String> getNodes(String key, int from) {
if (circle.isEmpty()) {
return new ArrayList<>();
List<String> res = new ArrayList<>();

if (key != null && from > 0) {
if (from < clusterUrls.size()) {
int slot = getHash(key);
Iterator<String> it = new ClockwiseIterator(slot);
while (it.hasNext() && res.size() < from) {
String part = it.next();
res.add(part);
}
} else {
res.addAll(clusterUrls);
}
}

final int hash = getHash(key);
SortedMap<Integer, String> tailMap = circle.tailMap(hash);
return (tailMap.isEmpty() ? circle : tailMap).values()
.stream().limit(from).collect(Collectors.toList());
return res;
}

List<String> getNodes(String key, List<String> clusterUrls, int from) {
Map<Integer, String> nodesHashes = new TreeMap<>();
private int getHash(String key) {
return Hash.murmur3(key);
}

private class ClockwiseIterator implements Iterator<String> {
private final Iterator<String> head;
private final Iterator<String> tail;

for (String nodeUrl : clusterUrls) {
nodesHashes.put(Hash.murmur3(nodeUrl + key), nodeUrl);
public ClockwiseIterator(int slot) {
this.head = circle.headMap(slot).values().iterator();
this.tail = circle.tailMap(slot).values().iterator();
}

return nodesHashes.values().stream().limit(from)
.collect(Collectors.toList());
}
@Override
public boolean hasNext() {
return head.hasNext() || tail.hasNext();
}

private int getHash(String key) {
return Hash.murmur3(key);
@Override
public String next() {
return tail.hasNext() ? tail.next() : head.next();
}
}
}
18 changes: 16 additions & 2 deletions src/main/java/ru/vk/itmo/test/tuzikovalexandr/Constants.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import one.nio.http.Response;

import java.net.HttpURLConnection;
import java.nio.charset.StandardCharsets;
import java.util.Map;
import java.util.Set;

Expand All @@ -13,7 +14,8 @@ public final class Constants {
);
public static final String TOO_MANY_REQUESTS = "429 Too Many Requests";
public static final String NOT_ENOUGH_REPLICAS = "504 Not Enough Replicas";
public static final int REQUEST_TIMEOUT = 300;
public static final int SERVER_ERROR = 500;
public static final int REQUEST_TIMEOUT = 500;
public static final Map<Integer, String> HTTP_CODE = Map.of(
HttpURLConnection.HTTP_OK, Response.OK,
HttpURLConnection.HTTP_ACCEPTED, Response.ACCEPTED,
Expand All @@ -25,10 +27,22 @@ public final class Constants {
public static final String BASE_URL = "http://localhost";
public static final int BASE_PORT = 8080;
public static final long FLUSHING_THRESHOLD_BYTES = 1024 * 1024;
public static final int NUMBER_OF_VIRTUAL_NODES = 5;
public static final int NUMBER_OF_VIRTUAL_NODES = 100;
public static final String HTTP_TIMESTAMP_HEADER = "X-Timestamp";
public static final String NIO_TIMESTAMP_HEADER = "x-timestamp:";
public static final String HTTP_TERMINATION_HEADER = "X-Termination";
public static final String RANGE_REQUEST = "/v0/entities?start=";
public static final String ID_REQUEST = "/v0/entity?id=";
public static final String CRLF = "\r\n";
public static final byte[] LAST_STRING = "0\r\n\r\n".getBytes(StandardCharsets.UTF_8);
public static final String NEW_LINE = "\n";
public static final byte[] HEADER =
"""
HTTP/1.1 200 OK\r
Content-Type: text/plain\r
Transfer-Encoding: chunked\r
\r
""".getBytes(StandardCharsets.UTF_8);

private Constants() {
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
import java.lang.foreign.MemorySegment;
import java.lang.foreign.ValueLayout;
import java.nio.charset.StandardCharsets;
import java.util.Iterator;

public class RequestHandler {
private final Dao dao;
Expand Down Expand Up @@ -84,4 +85,11 @@ private Response sendResponseWithTimestamp(String resultCode, byte[] body, long
private MemorySegment fromString(String data) {
return data == null ? null : MemorySegment.ofArray(data.getBytes(StandardCharsets.UTF_8));
}

public Iterator<EntryWithTimestamp<MemorySegment>> getEntries(String start, String end) {
MemorySegment startVal = fromString(start);
MemorySegment endVal = end != null ? fromString(end) : null;

return dao.get(startVal, endVal);
}
}
Loading
Loading