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

Stage6 Emelyanov Pavel #225

Closed
wants to merge 39 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
39 commits
Select commit Hold shift + click to select a range
d69c12e
add code for stage1
llav3ji2019 Feb 19, 2024
8e728f2
add scripts for each request
llav3ji2019 Feb 19, 2024
5b43d1e
add statistic for put and get requests
llav3ji2019 Feb 19, 2024
1a7bf92
add report for stage 1
llav3ji2019 Feb 19, 2024
94c7311
fix my realisation
llav3ji2019 Feb 28, 2024
2aefb00
add code for stage 2
llav3ji2019 Feb 29, 2024
fd857d2
Merge branch 'main' into task2
incubos Feb 29, 2024
7a80dda
add report for stage2
llav3ji2019 Mar 5, 2024
195fdb8
Merge remote-tracking branch 'origin/task2' into task2
llav3ji2019 Mar 5, 2024
0ae5935
fix conflict
llav3ji2019 Mar 5, 2024
676e37e
add photos for report
llav3ji2019 Mar 5, 2024
d1e582d
add stage3
llav3ji2019 Mar 14, 2024
48f0569
fix coed style
llav3ji2019 Mar 14, 2024
adfd491
fix coed style
llav3ji2019 Mar 14, 2024
3dfc632
add stage 4
llav3ji2019 Mar 28, 2024
7b681b9
fix code style
llav3ji2019 Mar 28, 2024
48d90ce
fix code style
llav3ji2019 Mar 28, 2024
56d660a
fix coed style
llav3ji2019 Apr 3, 2024
24e7dcf
fix coed style
llav3ji2019 Apr 11, 2024
e894605
add stage 5
llav3ji2019 Apr 11, 2024
b706c6f
updating branch
llav3ji2019 Apr 11, 2024
a14225b
fix stage 5
llav3ji2019 Apr 11, 2024
882b0b9
fix stage 5
llav3ji2019 Apr 11, 2024
9f35539
fix stage 5
llav3ji2019 Apr 11, 2024
b65186d
Merge branch 'main' into task5
incubos Apr 13, 2024
1dc748e
fix code style
llav3ji2019 Apr 17, 2024
8585361
fix code style
llav3ji2019 Apr 17, 2024
685f707
add reports
llav3ji2019 Apr 17, 2024
ed1f795
add reports
llav3ji2019 Apr 17, 2024
2c78643
add reports
llav3ji2019 Apr 17, 2024
85e2558
add reports
llav3ji2019 Apr 17, 2024
950bfbe
Merge remote-tracking branch 'origin/task5' into task5
llav3ji2019 Apr 17, 2024
5794e54
add report
llav3ji2019 Apr 17, 2024
d09bfbf
fix row use
llav3ji2019 Apr 17, 2024
cd4b90e
add stage6 code
llav3ji2019 Apr 25, 2024
1d489ac
updating branch
llav3ji2019 Apr 25, 2024
bdb8de3
fix code style
llav3ji2019 Apr 25, 2024
1c6a57e
fix code style
llav3ji2019 Apr 25, 2024
925d427
Merge branch 'main' into task6
incubos Apr 28, 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,86 @@
package ru.vk.itmo.test.pavelemelyanov;

import one.nio.util.Hash;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.NavigableMap;
import java.util.SortedMap;
import java.util.TreeMap;

public class ConsistentHashing {
private final NavigableMap<Integer, String> virtualNodeMapping = new TreeMap<>();
private final List<String> clusterUrls;

public ConsistentHashing(List<String> clusterUrls, int numbOfVirtualNodes) {
this.clusterUrls = clusterUrls;
for (String clusterUrl : clusterUrls) {
for (int i = 0; i < numbOfVirtualNodes; i++) {
addNode(i, clusterUrl);
}
}
}

public String getNode(String key) {
if (virtualNodeMapping.isEmpty()) {
return null;
}

final int hash = getHash(key);
SortedMap<Integer, String> tailMap = virtualNodeMapping.tailMap(hash);
return (tailMap.isEmpty() ? virtualNodeMapping.firstEntry() : tailMap.firstEntry()).getValue();
}

public List<String> getNodes(String key, int from) {
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);
}
}

return res;
}

private int getHash(String key) {
return Hash.murmur3(key);
}

private void addNode(int numOfNode, String node) {
int hash = getHash(node + numOfNode);
virtualNodeMapping.put(hash, node);
}

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

public ClockwiseIterator(int slot) {
head = virtualNodeMapping.headMap(slot)
.values()
.iterator();
tail = virtualNodeMapping.tailMap(slot)
.values()
.iterator();
}

@Override
public boolean hasNext() {
return head.hasNext() || tail.hasNext();
}

@Override
public String next() {
return tail.hasNext() ? tail.next() : head.next();
}
}
}
Original file line number Diff line number Diff line change
@@ -1,16 +1,21 @@
package ru.vk.itmo.test.pavelemelyanov;

import one.nio.async.CustomThreadFactory;

import java.util.concurrent.ArrayBlockingQueue;
import java.util.concurrent.BlockingQueue;
import java.util.concurrent.RejectedExecutionHandler;
import java.util.concurrent.ThreadPoolExecutor;
import java.util.concurrent.TimeUnit;

public final class ExecutorServiceConfig {
public static final int CORE_AMOUNT = Runtime.getRuntime().availableProcessors();
public static final int CORE_POOL_SIZE = CORE_AMOUNT / 2;
public static final int CORE_POOL_SIZE = CORE_AMOUNT;
public static final int MAX_CORE_POOL_SIZE = CORE_AMOUNT;
public static final int KEEP_ALIVE_TIME = 200;
public static final int QUEUE_CAPACITY = 64;
public static final long KEEP_ALIVE_TIME = 60;
public static final TimeUnit UNIT = TimeUnit.SECONDS;
public static final int QUEUE_CAPACITY = 1000;
public static final CustomThreadFactory threadFactory = new CustomThreadFactory("t", true);
public static final BlockingQueue<Runnable> queue = new ArrayBlockingQueue<>(QUEUE_CAPACITY);
public static final RejectedExecutionHandler HANDLER = new ThreadPoolExecutor.AbortPolicy();

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
package ru.vk.itmo.test.pavelemelyanov;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.concurrent.ExecutorService;
import java.util.concurrent.ThreadPoolExecutor;
import java.util.concurrent.TimeUnit;

public class ExecutorServiceWrapper {
public static final int TERMINATION_TIMEOUT = 60;
private static final Logger LOG = LoggerFactory.getLogger(MyServer.class);

private final ExecutorService workingPool;

public ExecutorServiceWrapper() {
workingPool = new ThreadPoolExecutor(
ExecutorServiceConfig.CORE_POOL_SIZE,
ExecutorServiceConfig.MAX_CORE_POOL_SIZE,
ExecutorServiceConfig.KEEP_ALIVE_TIME,
ExecutorServiceConfig.UNIT,
ExecutorServiceConfig.queue,
ExecutorServiceConfig.threadFactory,
ExecutorServiceConfig.HANDLER
);
}

public ExecutorService getExecutorService() {
return workingPool;
}

public void shutdownAndAwaitTermination() {
workingPool.shutdown();
try {
if (!workingPool.awaitTermination(TERMINATION_TIMEOUT, TimeUnit.SECONDS)) {
workingPool.shutdownNow();
if (!workingPool.awaitTermination(TERMINATION_TIMEOUT, TimeUnit.SECONDS)) {
LOG.error("ExecutorService error with stopping");
}
}
} catch (InterruptedException ex) {
workingPool.shutdownNow();
Thread.currentThread().interrupt();
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
import ru.vk.itmo.ServiceConfig;
import ru.vk.itmo.test.ServiceFactory;

@ServiceFactory(stage = 1)
@ServiceFactory(stage = 6)
public class FactoryImpl implements ServiceFactory.Factory {

@Override
Expand Down
34 changes: 34 additions & 0 deletions src/main/java/ru/vk/itmo/test/pavelemelyanov/HttpUtils.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
package ru.vk.itmo.test.pavelemelyanov;

import one.nio.http.Request;
import one.nio.http.Response;

import java.net.HttpURLConnection;
import java.util.Map;
import java.util.Set;

public final class HttpUtils {
public static final String NOT_ENOUGH_REPLICAS = "504 Not Enough Replicas";
public static final Set<Integer> SUPPORTED_METHODS = Set.of(
Request.METHOD_GET,
Request.METHOD_PUT,
Request.METHOD_DELETE
);
public static final int REQUEST_TIMEOUT_IN_MILLIS = 3000;
public static final Map<Integer, String> HTTP_CODE = Map.of(
HttpURLConnection.HTTP_OK, Response.OK,
HttpURLConnection.HTTP_ACCEPTED, Response.ACCEPTED,
HttpURLConnection.HTTP_CREATED, Response.CREATED,
HttpURLConnection.HTTP_NOT_FOUND, Response.NOT_FOUND,
HttpURLConnection.HTTP_BAD_REQUEST, Response.BAD_REQUEST,
HttpURLConnection.HTTP_INTERNAL_ERROR, Response.INTERNAL_ERROR
);
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";

private HttpUtils() {

}
}
Loading
Loading