Skip to content

Commit e4e5d04

Browse files
committed
implemented queuing for write requests and some tests
2 parents 80f19c9 + c428c43 commit e4e5d04

7 files changed

Lines changed: 174 additions & 92 deletions

File tree

pom.xml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@
1717
<dependency>
1818
<groupId>de.metalcon</groupId>
1919
<artifactId>like-server-api</artifactId>
20-
<version>0.1.0</version>
20+
<version>0.2.0</version>
2121
</dependency>
2222
<dependency>
2323
<groupId>de.metalcon</groupId>
@@ -39,7 +39,7 @@
3939
<dependency>
4040
<groupId>net.hh</groupId>
4141
<artifactId>RequestDispatcher</artifactId>
42-
<version>1.1.0</version>
42+
<version>1.3.0</version>
4343
</dependency>
4444

4545

src/main/java/de/metalcon/like/server/LikeButtonServer.java

Lines changed: 26 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -21,9 +21,9 @@
2121
*/
2222
public class LikeButtonServer extends Thread {
2323

24-
public final static String FRONTEND_LISTEN_URI = "tcp://*:1003";
24+
public final static String FRONTEND_LISTEN_URI = "tcp://*:1234";
2525

26-
public final static String WRITE_WORKER_LISTEN_URI = "inproc://like";
26+
public final static String WRITE_WORKER_LISTEN_URI = "ipc://like";
2727

2828
private final static String STORAGE_DIR = "/dev/shm/likeDB";
2929

@@ -40,20 +40,41 @@ public class LikeButtonServer extends Thread {
4040
*/
4141
private final ZmqWorker<LikeServerRequest, Response> writeWorker;
4242

43+
private final LikeServerRequestHandler likeRequestHandler;
44+
4345
public LikeButtonServer() throws MetalconException {
4446
service = new LikeService(STORAGE_DIR);
4547

46-
ZMQ.Context ctx = ZMQ.context(1);
48+
final ZMQ.Context ctx = ZMQ.context(1);
49+
50+
likeRequestHandler = new LikeServerRequestHandler(ctx, service);
4751

4852
frontendWorker =
4953
new ZmqWorker<LikeServerRequest, Response>(ctx,
50-
FRONTEND_LISTEN_URI, new LikeServerRequestHandler(ctx,
51-
service));
54+
FRONTEND_LISTEN_URI, likeRequestHandler);
5255

5356
writeWorker =
5457
new ZmqWorker<LikeServerRequest, Response>(ctx,
5558
WRITE_WORKER_LISTEN_URI,
5659
new LikeServerWriteRequestHandler(service));
60+
61+
Runtime.getRuntime().addShutdownHook(new Thread() {
62+
63+
@Override
64+
public void run() {
65+
likeRequestHandler.close();
66+
67+
frontendWorker.close();
68+
writeWorker.close();
69+
ctx.term();
70+
try {
71+
frontendWorker.join();
72+
writeWorker.join();
73+
} catch (InterruptedException e) {
74+
e.printStackTrace();
75+
}
76+
}
77+
});
5778
}
5879

5980
@Override

src/main/java/de/metalcon/like/server/api/backend/LikeGraphApi.java

Lines changed: 59 additions & 56 deletions
Original file line numberDiff line numberDiff line change
@@ -14,65 +14,68 @@
1414
*/
1515
public interface LikeGraphApi {
1616

17-
/**
18-
* Retrieves the list of common neighbors (X) such that
19-
*
20-
* (from) -[:(dis)like]-> (X) -[:(dis)like]-> (to)
21-
*
22-
* @param from
23-
* muid of the (from) node (see top)
24-
* @param to
25-
* muid of the (to) node (see top)
26-
* @param v
27-
* defines the relation: Vote.up or Vote.down between (from), (X)
28-
* and (to)
29-
* @return (X), the muids that (from) has in common with (to)
30-
*/
31-
public long[] getCommonNodes(long from, long to, Vote v) throws IOException;
17+
/**
18+
* Retrieves the list of common neighbors (X) such that
19+
*
20+
* (from) -[:(dis)like]-> (X) -[:(dis)like]-> (to)
21+
*
22+
* @param from
23+
* muid of the (from) node (see top)
24+
* @param to
25+
* muid of the (to) node (see top)
26+
* @param v
27+
* defines the relation: Vote.up or Vote.down between (from), (X)
28+
* and (to)
29+
* @return (X), the muids that (from) has in common with (to) or null if one
30+
* of the nodes are not known or they don't have anything in common
31+
*/
32+
public long[] getCommonNodes(long from, long to, Vote v) throws IOException;
3233

33-
/**
34-
* Returns a list of Muids of nodes (dis)liked by nodes (dis)liked by the
35-
* node associated with the given nodeMUID
36-
*
37-
* @param nodeMUID
38-
* The requested node
39-
* @param vote
40-
* The relation type (Vote.UP or Vote.DOWN)
41-
* @return The list of nodes liked by any node liked by the node with the
42-
* given MUID
43-
*/
44-
public long[] getLikedLikes(final long nodeMUID, final Vote vote)
45-
throws IOException;
34+
/**
35+
* Returns a list of Muids of nodes (dis)liked by nodes (dis)liked by the
36+
* node associated with the given nodeMUID
37+
*
38+
* @param nodeMUID
39+
* The requested node
40+
* @param vote
41+
* The relation type (Vote.UP or Vote.DOWN)
42+
* @return The list of nodes liked by any node liked by the node with the
43+
* given MUID
44+
*/
45+
public long[] getLikedLikes(final long nodeMUID, final Vote vote)
46+
throws IOException;
4647

47-
/**
48-
* puts a new Edge to the data store.
49-
*
50-
* @param from
51-
* @param to
52-
*/
53-
public void putEdge(long from, long to, final Vote vote) throws IOException;
48+
/**
49+
* puts a new Edge to the data store.
50+
*
51+
* @param from
52+
* @param to
53+
*/
54+
public void putEdge(long from, long to, final Vote vote) throws IOException;
5455

55-
/**
56-
* deletes an Edge from the graph
57-
*
58-
* @param from
59-
* @param to
60-
* @return true if the edge was in the graph
61-
*/
62-
public void deleteEdge(long from, long to) throws IOException;
56+
/**
57+
* deletes an Edge from the graph
58+
*
59+
* @param from
60+
* @param to
61+
* @return true if the edge was in the graph
62+
*/
63+
public void deleteEdge(long from, long to) throws IOException;
6364

64-
/**
65-
* Returns a list of MUIDs of nodes (dis)liked by the node with the MUID
66-
* 'nodeMUID' if directionOut is true (or null if the requested node does
67-
* not exist). If directionOut is set to false all nodes disliking the given
68-
* node are returned
69-
*
70-
* @param nodeMUID
71-
* The requested node
72-
* @return The list of nodes (dis)liking the node or being (dis)liked by the
73-
* node
74-
*/
75-
public long[] getLikes(final long nodeMUID, final Direction direction,
76-
final Vote vote) throws IOException;
65+
/**
66+
* Returns a list of MUIDs of nodes (dis)liked by the node with the MUID
67+
* 'nodeMUID' if directionOut is true (or null if the requested node does
68+
* not exist). If directionOut is set to false all nodes disliking the given
69+
* node are returned
70+
*
71+
* @param nodeMUID
72+
* The requested node
73+
* @return The list of nodes (dis)liking the node or being (dis)liked by the
74+
* node
75+
*/
76+
public long[] getLikes(
77+
final long nodeMUID,
78+
final Direction direction,
79+
final Vote vote) throws IOException;
7780

7881
}

src/main/java/de/metalcon/like/server/api/backend/LikeService.java

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,6 @@
77

88
import de.metalcon.dbhelper.LevelDbHandler;
99
import de.metalcon.exceptions.MetalconException;
10-
import de.metalcon.exceptions.MetalconRuntimeException;
1110
import de.metalcon.like.api.Direction;
1211
import de.metalcon.like.api.Vote;
1312
import de.metalcon.like.server.core.Like;
@@ -46,9 +45,7 @@ public LikeService(
4645
public long[] getCommonNodes(final long from, final long to, final Vote v) {
4746
Node f = NodeFactory.getNode(from);
4847
if (f == null) {
49-
throw new MetalconRuntimeException(
50-
"Requested getCommonNodes with an unknown from ID");
51-
// return null;
48+
return null;
5249
}
5350
return f.getCommonNodes(to, v);
5451
}

src/main/java/de/metalcon/like/server/api/frontend/LikeServerRequestHandler.java

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,6 @@
77
import org.zeromq.ZMQ;
88

99
import de.metalcon.api.responses.Response;
10-
import de.metalcon.api.responses.SuccessResponse;
1110
import de.metalcon.api.responses.errors.InternalServerErrorResponse;
1211
import de.metalcon.like.api.Vote;
1312
import de.metalcon.like.api.requests.LikeServerAddRelationRequest;
@@ -19,11 +18,12 @@
1918
import de.metalcon.like.api.requests.LikeServerRequest;
2019
import de.metalcon.like.api.responses.LikeServerMuidListResponse;
2120
import de.metalcon.like.api.responses.LikeServerVoteResponse;
21+
import de.metalcon.like.api.responses.RequestQueuedResponse;
2222
import de.metalcon.like.server.LikeButtonServer;
2323
import de.metalcon.like.server.api.backend.LikeService;
2424

2525
public class LikeServerRequestHandler implements
26-
RequestHandler<LikeServerRequest, Response> {
26+
RequestHandler<LikeServerRequest, Response>, AutoCloseable {
2727

2828
private static final long serialVersionUID = 5330271229952005271L;
2929

@@ -39,8 +39,8 @@ public LikeServerRequestHandler(
3939
final String writeWorkerID = "WriteWorker";
4040

4141
writeWorkDispatcher.registerServiceAdapter(writeWorkerID,
42-
new ZmqAdapter<LikeServerRequest, SuccessResponse>(ctx,
43-
LikeButtonServer.WRITE_WORKER_LISTEN_URI));
42+
new ZmqAdapter(ctx, LikeButtonServer.WRITE_WORKER_LISTEN_URI));
43+
4444
writeWorkDispatcher.setDefaultService(
4545
LikeServerAddRelationRequest.class, writeWorkerID);
4646
writeWorkDispatcher.setDefaultService(
@@ -49,7 +49,6 @@ public LikeServerRequestHandler(
4949

5050
@Override
5151
public Response handleRequest(final LikeServerRequest request) {
52-
5352
/*
5453
* get common nodes
5554
*/
@@ -111,6 +110,11 @@ public Response handleRequest(final LikeServerRequest request) {
111110
*/
112111
writeWorkDispatcher.execute(request, /* ignore the response */null);
113112

114-
return null;
113+
return new RequestQueuedResponse();
114+
}
115+
116+
@Override
117+
public void close() {
118+
115119
}
116120
}

src/main/java/de/metalcon/like/server/api/frontend/LikeServerWriteRequestHandler.java

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,7 @@
44

55
import net.hh.request_dispatcher.server.RequestHandler;
66
import de.metalcon.api.responses.Response;
7-
import de.metalcon.api.responses.SuccessResponse;
8-
import de.metalcon.api.responses.errors.InternalServerErrorResponse;
7+
import de.metalcon.exceptions.MetalconRuntimeException;
98
import de.metalcon.like.api.requests.LikeServerAddRelationRequest;
109
import de.metalcon.like.api.requests.LikeServerRemoveRelationRequest;
1110
import de.metalcon.like.api.requests.LikeServerRequest;
@@ -25,15 +24,14 @@ public LikeServerWriteRequestHandler(
2524

2625
@Override
2726
public Response handleRequest(final LikeServerRequest request) {
28-
2927
/*
3028
* add relation
3129
*/
3230
if (request instanceof LikeServerAddRelationRequest) {
3331
final LikeServerAddRelationRequest r =
3432
(LikeServerAddRelationRequest) request;
3533
service.putEdge(r.getFrom(), r.getTo(), r.getVote());
36-
return new SuccessResponse();
34+
return null;
3735
}
3836

3937
/*
@@ -44,11 +42,13 @@ public Response handleRequest(final LikeServerRequest request) {
4442
(LikeServerRemoveRelationRequest) request;
4543
try {
4644
service.deleteEdge(r.getFrom(), r.getTo());
47-
return new SuccessResponse();
45+
return null;
4846
} catch (IOException e) {
4947
e.printStackTrace();
50-
return new InternalServerErrorResponse(e.getMessage(),
51-
"Check if the like service DB directories are writable");
48+
throw new MetalconRuntimeException(
49+
e.getMessage()
50+
+ "\n"
51+
+ "Check if the like service DB directories are writable");
5252
}
5353
}
5454
return null;

0 commit comments

Comments
 (0)