Skip to content

Commit d57d330

Browse files
committed
Merge branch 'feature/zmqtests' into develop
2 parents c428c43 + e4e5d04 commit d57d330

8 files changed

Lines changed: 106 additions & 77 deletions

File tree

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -83,7 +83,7 @@ public void run() {
8383
writeWorker.start();
8484

8585
while (true) {
86-
long ns = service.updateAllNodes();
86+
long ns = service.updateAllLargeNodes();
8787
if (ns < 1E9) {
8888
try {
8989
Thread.sleep(1000);

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

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -265,12 +265,12 @@ public long[] getAllNownNodes() {
265265
}
266266

267267
/**
268-
* Reads the persistent like histories and adds new likes in to the commons
268+
* Reads the persistent like histories and adds new likes into the commons
269269
* list of each node
270270
*
271271
* @return The number of nanoseconds spend within this method
272272
*/
273-
public long updateAllNodes() {
273+
public long updateAllLargeNodes() {
274274
long start = System.nanoTime();
275275
long[] allNodes = NodeFactory.getAllNodeMuids();
276276
if (allNodes == null) {
@@ -279,7 +279,7 @@ public long updateAllNodes() {
279279

280280
for (long uuid : allNodes) {
281281
Node n = NodeFactory.getNode(uuid);
282-
n.updateCommons();
282+
n.updateLargeNodeCommons();
283283
}
284284
return System.nanoTime() - start;
285285
}

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -108,7 +108,7 @@ public Response handleRequest(final LikeServerRequest request) {
108108
* All other request should be write requests -> forward them to the
109109
* write worker asynchronously
110110
*/
111-
writeWorkDispatcher.execute(request, null/* ignore the response */);
111+
writeWorkDispatcher.execute(request, /* ignore the response */null);
112112

113113
return new RequestQueuedResponse();
114114
}

src/main/java/de/metalcon/like/server/core/Commons.java

Lines changed: 44 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ public Commons(
3131
final Vote likeType) {
3232
this.node = node;
3333
persistentCommonsMap =
34-
new PersistentUidMap(node.getMuid() + "" + likeType);
34+
new PersistentUidMap(node.getUid() + "" + likeType);
3535
this.likeType = likeType;
3636
}
3737

@@ -71,27 +71,35 @@ public long[] getCommonNodes(final long muid) {
7171
}
7272

7373
/**
74-
* Updates the commons of node and writes the data to disk
74+
* Updates the commons of node and writes the data to disk if nod has a
75+
* large in degree
7576
*/
76-
public void update() {
77+
public void updateLargeNode() {
7778
final int now = (int) (System.currentTimeMillis() / 1000l);
7879

7980
/*
80-
* Update all outgoing nodes
81+
* Update all incoming nodes if this node is large.
8182
*/
82-
final long[] outNodes = node.getLikesOut(Vote.UP).toArray();
83-
if (outNodes != null) {
84-
for (long friendUUID : outNodes) {
85-
if (friendUUID == 0) {
86-
break;
83+
final long[] inNodes = node.getLikesIn(Vote.UP).toArray();
84+
if (inNodes != null && inNodes.length >= Node.LARGE_NODE_DEGREE) {
85+
if (inNodes != null) {
86+
for (long friendUUID : inNodes) {
87+
if (friendUUID == 0) {
88+
break;
89+
}
90+
(NodeFactory.getNode(friendUUID)).updateCommons(node
91+
.getUid());
8792
}
88-
updateFriend(NodeFactory.getNode(friendUUID), false);
8993
}
9094
}
9195

9296
persistentCommonsMap.setUpdateTimeStamp(now);
9397
}
9498

99+
public void updateNode(final long likedNode) {
100+
updateFriend(NodeFactory.getNode(likedNode), false);
101+
}
102+
95103
/**
96104
* Adds the friend to all entities in the commonsMap liked by the friend
97105
* node.
@@ -100,22 +108,26 @@ public void update() {
100108
* The friend to be added to the commonsMap
101109
*/
102110
public void friendAdded(final Node friend) {
111+
/*
112+
* Update the commons with friend
113+
*/
103114
updateFriend(friend, true);
104115

105116
/*
106-
* Instead of frequently running LikeService.updateAllNodes() we could
107-
* run following code. The problem here is that big nodes would trigger
108-
* these expensive calls much too often.
117+
* Update all incoming nodes if this node is not too large.
109118
*/
110-
// final long[] inNodes = node.getLikesIn(Vote.UP).toArray();
111-
// if (inNodes != null) {
112-
// for (long friendUUID : inNodes) {
113-
// if (friendUUID == 0) {
114-
// break;
115-
// }
116-
// (NodeFactory.getNode(friendUUID)).updateCommons();
117-
// }
118-
// }
119+
final long[] inNodes = node.getLikesIn(Vote.UP).toArray();
120+
if (inNodes == null || inNodes.length < Node.LARGE_NODE_DEGREE) {
121+
if (inNodes != null) {
122+
for (long friendUUID : inNodes) {
123+
if (friendUUID == 0) {
124+
break;
125+
}
126+
(NodeFactory.getNode(friendUUID)).updateCommons(node
127+
.getUid());
128+
}
129+
}
130+
}
119131
}
120132

121133
/**
@@ -130,14 +142,14 @@ public void friendRemoved(final Node friend) {
130142
/*
131143
* Remove the friend from the commons list of the liked entity
132144
*/
133-
persistentCommonsMap.remove(outID, friend.getMuid());
145+
persistentCommonsMap.remove(outID, friend.getUid());
134146
}
135147

136148
for (long outID : friend.getLikesOut(Vote.DOWN)) {
137149
/*
138150
* Remove the friend from the commons list of the liked entity
139151
*/
140-
persistentCommonsMap.remove(outID, friend.getMuid());
152+
persistentCommonsMap.remove(outID, friend.getUid());
141153
}
142154
}
143155

@@ -160,15 +172,15 @@ public void updateFriend(final Node friend, final boolean ignoreTimestamp) {
160172
int searchTS =
161173
ignoreTimestamp ? 0 : persistentCommonsMap
162174
.getLastUpdateTimeStamp();
163-
for (Like like : friend.getLikesFromTimeOn(searchTS)) {
164-
if (like.getMUID() == node.getMuid()) {
175+
for (Like like : friend.getLikeHistoryFromTimeOn(searchTS)) {
176+
if (like.getMUID() == node.getUid()) {
165177
continue;
166178
}
167179
if (like.getVote() == likeType) {
168180
/*
169181
* Q1 node -> friend -> likedNode
170182
*/
171-
persistentCommonsMap.append(like.getMUID(), friend.getMuid());
183+
persistentCommonsMap.append(like.getMUID(), friend.getUid());
172184

173185
/*
174186
* Q2 node -> likedNode && node -> friend -> likedNode FIXME:
@@ -178,19 +190,19 @@ public void updateFriend(final Node friend, final boolean ignoreTimestamp) {
178190
* friend.getLikedNodes()
179191
*/
180192
if (node.getLikesOut(likeType).contains(like.getMUID())) {
181-
persistentCommonsMap.append(friend.getMuid(),
182-
like.getMUID());
193+
persistentCommonsMap
194+
.append(friend.getUid(), like.getMUID());
183195
}
184196
} else {
185-
persistentCommonsMap.remove(like.getMUID(), friend.getMuid());
186-
persistentCommonsMap.remove(friend.getMuid(), like.getMUID());
197+
persistentCommonsMap.remove(like.getMUID(), friend.getUid());
198+
persistentCommonsMap.remove(friend.getUid(), like.getMUID());
187199
}
188200
}
189201
}
190202

191203
@Override
192204
public String toString() {
193-
return node.getMuid() + "(" + likeType + "):\n"
205+
return node.getUid() + "(" + likeType + "):\n"
194206
+ persistentCommonsMap.toString();
195207
}
196208
}

src/main/java/de/metalcon/like/server/core/Node.java

Lines changed: 40 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -14,12 +14,19 @@ public class Node {
1414
private static final int LastLikeCacheSize = 10;
1515

1616
// Class Variables
17-
private final long Muid;
17+
private final long Uid;
1818

1919
private final Commons likeCommons;
2020

2121
private final Commons dislikeCommons;
2222

23+
/*
24+
* This defines what large node means. Nodes with this in/out degree will
25+
* not be updated during the creation of a new edge. They must be updated
26+
* frequently by a special thread.
27+
*/
28+
public static final int LARGE_NODE_DEGREE = 100;
29+
2330
/*
2431
* lastLikes[lastLikesFirstEntryPointer] is the newest like The list is
2532
* ordered by descending timestamps (newest first)
@@ -60,36 +67,18 @@ public class Node {
6067
Node(
6168
final long muid,
6269
final boolean isNewNode) {
63-
Muid = muid;
64-
// commons = new Commons(this, storageDir,
65-
// PersistentMuidArrayMap.class);
66-
67-
// commons = new Commons(this, storageDir,
68-
// PersistentMuidArrayMapRedis.class);
69-
70-
// commons = new Commons(this, storageDir, LazyPersistentMuidMap.class);
70+
Uid = muid;
7171

7272
likeCommons = new Commons(this, Vote.UP);
7373

7474
dislikeCommons = new Commons(this, Vote.DOWN);
7575

76-
// try {
77-
// friends = new PersistentMuidSet(storageDir + "/" + Muid
78-
// + "_friends");
79-
// inNodes = new PersistentMuidSet(storageDir + "/" + Muid
80-
// + "_inNodes");
81-
//
82-
// } catch (IOException e) {
83-
// e.printStackTrace();
84-
// System.exit(1);
85-
// }
86-
87-
likedOut = new PersistentUidSet(Muid + "likedOut");
88-
likedIn = new PersistentUidSet(Muid + "likedIn");
89-
dislikedOut = new PersistentUidSet(Muid + "dislikedOut");
90-
dislikedIn = new PersistentUidSet(Muid + "dislikedIn");
91-
92-
likeHistory = new PersistentLikeHistory(Muid);
76+
likedOut = new PersistentUidSet(Uid + "likedOut");
77+
likedIn = new PersistentUidSet(Uid + "likedIn");
78+
dislikedOut = new PersistentUidSet(Uid + "dislikedOut");
79+
dislikedIn = new PersistentUidSet(Uid + "dislikedIn");
80+
81+
likeHistory = new PersistentLikeHistory(Uid);
9382
}
9483

9584
/**
@@ -110,7 +99,7 @@ public void delete() throws IOException {
11099
dislikedOut.delete();
111100
dislikedIn.delete();
112101

113-
NodeFactory.removeNodeFromPersistentList(Muid);
102+
NodeFactory.removeNodeFromPersistentList(Uid);
114103

115104
likeHistory.delete();
116105
}
@@ -122,7 +111,7 @@ public void delete() throws IOException {
122111
* The time all likes have to be younger than
123112
* @return An array of the newest likes
124113
*/
125-
public Like[] getLikesFromTimeOn(final int timestamp) {
114+
public Like[] getLikeHistoryFromTimeOn(final int timestamp) {
126115
int arrayLength = 10;
127116
Like[] likesFoundInCache = new Like[arrayLength];
128117
int likesFoundInCachePointer = 0;
@@ -263,8 +252,8 @@ public void addLike(final Like like) throws IOException {
263252
likedNode = NodeFactory.createNewNode(like.getMUID());
264253
}
265254

266-
addOutNode(likedNode.Muid, like.getVote());
267-
likedNode.addInNode(Muid, like.getVote());
255+
addOutNode(likedNode.Uid, like.getVote());
256+
likedNode.addInNode(Uid, like.getVote());
268257

269258
synchronized (lastLikesCache) {
270259
if (lastLikesFirstEntryPointer == 0) {
@@ -321,7 +310,7 @@ public void addLike(final long muid, final int timestamp, final Vote vote)
321310
* @throws IOException
322311
*/
323312
public void removeFriendship(final Node friend) throws IOException {
324-
addLike(new Like(friend.getMuid(),
313+
addLike(new Like(friend.getUid(),
325314
(int) System.currentTimeMillis() / 1000, Vote.NEUTRAL));
326315
}
327316

@@ -432,15 +421,29 @@ public long[] getCommonDislikedNodes(final long muid) {
432421
return dislikeCommons.getCommonNodes(muid);
433422
}
434423

435-
public final long getMuid() {
436-
return Muid;
424+
/**
425+
*
426+
* @return The unique identifyer of this node
427+
*/
428+
public final long getUid() {
429+
return Uid;
437430
}
438431

439-
protected Commons getCommons() {
440-
return likeCommons;
432+
/**
433+
* Updates commons of incoming nodes with this node if this node has a large
434+
* in degree
435+
*/
436+
public void updateLargeNodeCommons() {
437+
likeCommons.updateLargeNode();
441438
}
442439

443-
public void updateCommons() {
444-
likeCommons.update();
440+
/**
441+
* Update all commons this node has with likedNode
442+
*
443+
* @param likedNode
444+
* the freshly liked node
445+
*/
446+
public void updateCommons(final long likedNode) {
447+
likeCommons.updateNode(likedNode);
445448
}
446449
}

src/main/java/de/metalcon/like/server/core/PersistentUidSet.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -70,7 +70,7 @@ public boolean remove(long uuid) {
7070
}
7171

7272
public boolean remove(Node n) {
73-
return remove(n.getMuid());
73+
return remove(n.getUid());
7474
}
7575

7676
/**

src/test/java/de/metalcon/like/requests/ZmqTest.java

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,18 @@ public class ZmqTest {
3232

3333
private ZMQ.Context ctx;
3434

35+
<<<<<<< HEAD
36+
// LikeButtonServer server;
37+
// try {
38+
// server = new LikeButtonServer();
39+
// server.start();
40+
// } catch (MetalconException e) {
41+
// e.printStackTrace();
42+
// }
43+
//
44+
// Thread.sleep(100);
45+
ZMQ.Context ctx = ZMQ.context(1);
46+
=======
3547
@Before
3648
public void setUpBeforeClass() throws Exception {
3749
LikeButtonServer server;
@@ -45,6 +57,9 @@ public void setUpBeforeClass() throws Exception {
4557
ctx = ZMQ.context(1);
4658

4759
dispatcher = new Dispatcher();
60+
>>>>>>> develop
61+
62+
dispatcher = new Dispatcher();
4863

4964
String serviceID = "serviceID";
5065
dispatcher.registerServiceAdapter(serviceID, new ZmqAdapter(ctx,

src/test/java/de/metalcon/like/server/api/DeleteEdgeTest.java

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ public void testStateafterRemove() throws IOException {
2929

3030
likeService.putEdge(4, 2, Vote.UP);
3131

32-
likeService.updateAllNodes();
32+
likeService.updateAllLargeNodes();
3333

3434
long[] commons = likeService.getCommonNodes(1, 3, Vote.UP);
3535
HashSet<Long> set = convertArrayToHashSet(commons);
@@ -58,5 +58,4 @@ public void testStateafterRemove() throws IOException {
5858
assertFalse(set.contains(2L));
5959
assertTrue(set.contains(4L));
6060
}
61-
6261
}

0 commit comments

Comments
 (0)