-
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 1 #7
Merged
Merged
Changes from all commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
16439b0
First version
19f618f
Refactor
b52f870
Add report + improve code style
4a17a81
Add a suggestion to the report about compression need
3ceda47
Fix pr
781334a
Fix codestyle
c16c007
Merge branch 'main' into stage-1
urbanchef 202fdc6
Merge branch 'main' into stage-1
urbanchef 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
44 changes: 44 additions & 0 deletions
44
src/main/java/ru/vk/itmo/test/proninvalentin/MemorySegmentFactory.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,44 @@ | ||
package ru.vk.itmo.test.proninvalentin; | ||
|
||
import ru.vk.itmo.dao.BaseEntry; | ||
import ru.vk.itmo.dao.Entry; | ||
|
||
import java.lang.foreign.MemorySegment; | ||
import java.lang.foreign.ValueLayout; | ||
import java.nio.charset.StandardCharsets; | ||
|
||
public class MemorySegmentFactory { | ||
public byte[] toByteArray(MemorySegment data) { | ||
if (data == null) { | ||
throw new IllegalArgumentException(); | ||
} | ||
|
||
return data.toArray(ValueLayout.JAVA_BYTE); | ||
} | ||
|
||
public MemorySegment fromString(String data) { | ||
return data == null | ||
? null | ||
: MemorySegment.ofArray(data.getBytes(StandardCharsets.UTF_8)); | ||
} | ||
|
||
public Entry<MemorySegment> toMemorySegment(String key, String value) { | ||
if (key == null || value == null) { | ||
throw new IllegalArgumentException(); | ||
} | ||
|
||
MemorySegment msKey = fromString(key); | ||
MemorySegment msValue = fromString(value); | ||
return new BaseEntry<>(msKey, msValue); | ||
} | ||
|
||
public Entry<MemorySegment> toDeletedMemorySegment(String key) { | ||
if (key == null) { | ||
throw new IllegalArgumentException(); | ||
} | ||
|
||
MemorySegment msKey = fromString(key); | ||
return new BaseEntry<>(msKey, null); | ||
} | ||
} | ||
|
143 changes: 143 additions & 0 deletions
143
src/main/java/ru/vk/itmo/test/proninvalentin/Server.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,143 @@ | ||
package ru.vk.itmo.test.proninvalentin; | ||
|
||
import one.nio.http.HttpException; | ||
import one.nio.http.HttpServer; | ||
import one.nio.http.HttpServerConfig; | ||
import one.nio.http.HttpSession; | ||
import one.nio.http.Param; | ||
import one.nio.http.Path; | ||
import one.nio.http.Request; | ||
import one.nio.http.RequestMethod; | ||
import one.nio.http.Response; | ||
import one.nio.server.AcceptorConfig; | ||
import org.slf4j.Logger; | ||
import org.slf4j.LoggerFactory; | ||
import ru.vk.itmo.ServiceConfig; | ||
import ru.vk.itmo.dao.BaseEntry; | ||
import ru.vk.itmo.dao.Dao; | ||
import ru.vk.itmo.dao.Entry; | ||
import ru.vk.itmo.test.reference.dao.ReferenceDao; | ||
|
||
import java.io.IOException; | ||
import java.lang.foreign.MemorySegment; | ||
import java.util.Set; | ||
|
||
public class Server extends HttpServer { | ||
private final Dao<MemorySegment, Entry<MemorySegment>> dao; | ||
private final MemorySegmentFactory msFactory; | ||
private static final Logger logger = LoggerFactory.getLogger(Server.class); | ||
private static final Set<Integer> SUPPORTED_HTTP_METHODS = Set.of( | ||
Request.METHOD_GET, | ||
Request.METHOD_PUT, | ||
Request.METHOD_DELETE | ||
); | ||
|
||
public Server(ServiceConfig config, ReferenceDao dao) throws IOException { | ||
super(createServerConfig(config)); | ||
|
||
this.dao = dao; | ||
this.msFactory = new MemorySegmentFactory(); | ||
} | ||
|
||
private static HttpServerConfig createServerConfig(ServiceConfig serviceConfig) { | ||
HttpServerConfig serverConfig = new HttpServerConfig(); | ||
AcceptorConfig acceptorConfig = new AcceptorConfig(); | ||
acceptorConfig.port = serviceConfig.selfPort(); | ||
acceptorConfig.reusePort = true; | ||
|
||
serverConfig.acceptors = new AcceptorConfig[]{acceptorConfig}; | ||
serverConfig.closeSessions = true; | ||
return serverConfig; | ||
} | ||
|
||
@Override | ||
public void handleDefault(Request request, HttpSession session) throws IOException { | ||
int httpMethod = request.getMethod(); | ||
Response response = isSupportedMethod(httpMethod) | ||
? new Response(Response.BAD_REQUEST, Response.EMPTY) | ||
: new Response(Response.METHOD_NOT_ALLOWED, Response.EMPTY); | ||
session.sendResponse(response); | ||
} | ||
|
||
private boolean isSupportedMethod(int httpMethod) { | ||
return SUPPORTED_HTTP_METHODS.contains(httpMethod); | ||
} | ||
|
||
@Override | ||
public void handleRequest(Request request, HttpSession session) { | ||
try { | ||
super.handleRequest(request, session); | ||
} catch (Exception e) { | ||
logger.error("Error while processing request", e); | ||
|
||
String responseCode = e.getClass() == HttpException.class | ||
? Response.BAD_REQUEST | ||
: Response.INTERNAL_ERROR; | ||
sendResponse(session, new Response(responseCode, Response.EMPTY)); | ||
} | ||
} | ||
|
||
private static void sendResponse(HttpSession session, Response response) { | ||
try { | ||
session.sendResponse(response); | ||
} catch (IOException e) { | ||
logger.error("Error while sending response", e); | ||
} | ||
} | ||
|
||
@Override | ||
public synchronized void stop() { | ||
super.stop(); | ||
} | ||
|
||
// region Handlers | ||
|
||
@Path("/v0/entity") | ||
@RequestMethod(Request.METHOD_PUT) | ||
public Response upsert(@Param(value = "id", required = true) String id, Request request) { | ||
if (isNullOrEmpty(id) || request.getBody() == null) { | ||
return new Response(Response.BAD_REQUEST, Response.EMPTY); | ||
} | ||
|
||
MemorySegment key = msFactory.fromString(id); | ||
MemorySegment value = MemorySegment.ofArray(request.getBody()); | ||
dao.upsert(new BaseEntry<>(key, value)); | ||
return new Response(Response.CREATED, Response.EMPTY); | ||
} | ||
|
||
@Path("/v0/entity") | ||
@RequestMethod(Request.METHOD_GET) | ||
public Response get(@Param(required = true, value = "id") String id) { | ||
if (isNullOrEmpty(id)) { | ||
return new Response(Response.BAD_REQUEST, Response.EMPTY); | ||
} | ||
|
||
MemorySegment key = msFactory.fromString(id); | ||
|
||
Entry<MemorySegment> entry = dao.get(key); | ||
if (entry == null) { | ||
return new Response(Response.NOT_FOUND, Response.EMPTY); | ||
} | ||
|
||
byte[] value = msFactory.toByteArray(entry.value()); | ||
return Response.ok(value); | ||
} | ||
|
||
@Path("/v0/entity") | ||
@RequestMethod(Request.METHOD_DELETE) | ||
public Response delete(@Param(required = true, value = "id") String id) { | ||
if (isNullOrEmpty(id)) { | ||
return new Response(Response.BAD_REQUEST, Response.EMPTY); | ||
} | ||
|
||
Entry<MemorySegment> deletedMemorySegment = msFactory.toDeletedMemorySegment(id); | ||
dao.upsert(deletedMemorySegment); | ||
return new Response(Response.ACCEPTED, Response.EMPTY); | ||
} | ||
|
||
private boolean isNullOrEmpty(String str) { | ||
return str == null || str.isEmpty(); | ||
} | ||
|
||
// endregion | ||
} |
51 changes: 51 additions & 0 deletions
51
src/main/java/ru/vk/itmo/test/proninvalentin/ServiceImpl.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,51 @@ | ||
package ru.vk.itmo.test.proninvalentin; | ||
|
||
import ru.vk.itmo.Service; | ||
import ru.vk.itmo.ServiceConfig; | ||
import ru.vk.itmo.dao.Config; | ||
import ru.vk.itmo.test.ServiceFactory; | ||
import ru.vk.itmo.test.reference.dao.ReferenceDao; | ||
|
||
import java.io.IOException; | ||
import java.io.UncheckedIOException; | ||
import java.util.concurrent.CompletableFuture; | ||
|
||
public class ServiceImpl implements Service { | ||
private final Config daoConfig; | ||
private final ServiceConfig config; | ||
private ReferenceDao dao; | ||
private Server server; | ||
|
||
public ServiceImpl(ServiceConfig config) throws IOException { | ||
int flushThresholdBytes = 1 << 27; // 128 MB | ||
this.config = config; | ||
this.daoConfig = new Config(config.workingDir(), flushThresholdBytes); | ||
} | ||
|
||
@Override | ||
public CompletableFuture<Void> start() throws IOException { | ||
dao = new ReferenceDao(daoConfig); | ||
server = new Server(config, dao); | ||
server.start(); | ||
return CompletableFuture.completedFuture(null); | ||
} | ||
|
||
@Override | ||
public CompletableFuture<Void> stop() throws IOException { | ||
server.stop(); | ||
dao.close(); | ||
return CompletableFuture.completedFuture(null); | ||
} | ||
|
||
@ServiceFactory(stage = 1) | ||
public static class Factory implements ServiceFactory.Factory { | ||
@Override | ||
public Service create(ServiceConfig config) { | ||
try { | ||
return new ServiceImpl(config); | ||
} catch (IOException e) { | ||
throw new UncheckedIOException(e); | ||
} | ||
} | ||
} | ||
} |
34 changes: 34 additions & 0 deletions
34
src/main/java/ru/vk/itmo/test/proninvalentin/StartServer.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,34 @@ | ||
package ru.vk.itmo.test.proninvalentin; | ||
|
||
import ru.vk.itmo.ServiceConfig; | ||
import ru.vk.itmo.dao.Config; | ||
import ru.vk.itmo.test.reference.dao.ReferenceDao; | ||
|
||
import java.io.IOException; | ||
import java.nio.file.Files; | ||
import java.nio.file.Path; | ||
import java.util.List; | ||
|
||
public final class StartServer { | ||
private StartServer() { | ||
// Suppress warning | ||
} | ||
|
||
public static void main(String[] args) throws IOException { | ||
String url = "http://localhost"; | ||
int port = 8080; | ||
int flushThresholdBytes = 1 << 27; // 128 MB | ||
Path profilingDataPath = Path.of( | ||
"/Users/valentinpronin/IdeaProjects/2024-highload-dht/" | ||
+ "src/main/java/ru/vk/itmo/test/" | ||
+ "proninvalentin/server_profiling_data"); | ||
Files.createDirectories(profilingDataPath); | ||
|
||
Config daoConfig = new Config(profilingDataPath, flushThresholdBytes); | ||
ServiceConfig serviceConfig = new ServiceConfig(port, url, List.of(url), profilingDataPath); | ||
ReferenceDao referenceDao = new ReferenceDao(daoConfig); | ||
|
||
Server server = new Server(serviceConfig, referenceDao); | ||
server.start(); | ||
} | ||
} |
6 changes: 6 additions & 0 deletions
6
src/main/java/ru/vk/itmo/test/proninvalentin/lua_scripts/get.lua
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,6 @@ | ||
function request() | ||
id = math.random(1, 300000) | ||
path = "/v0/entity?id=" .. id | ||
method = "GET" | ||
return wrk.format(method, path, body) | ||
end |
9 changes: 9 additions & 0 deletions
9
src/main/java/ru/vk/itmo/test/proninvalentin/lua_scripts/put.lua
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,9 @@ | ||
id = 0 | ||
|
||
function request() | ||
body = string.rep("random_value", 100) | ||
method = "PUT" | ||
id = id + 1 | ||
path = "/v0/entity?id=" .. id | ||
return wrk.format(method, path, headers, body) | ||
end |
Binary file added
BIN
+149 KB
.../vk/itmo/test/proninvalentin/reports/stage1/aprof/get/alloc/alloc-get-20000.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added
BIN
+148 KB
.../vk/itmo/test/proninvalentin/reports/stage1/aprof/get/alloc/alloc-get-25000.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
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.
Не хватает обработки исключений от дао здесь и ниже и проверки request body на null
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.
Проверка на request
request.getBody() == null
выше же или вы про что-то другое?Решил обработку исключений сделать глобальной и переопределил handleRequest, добавив
try
блок.