-
Notifications
You must be signed in to change notification settings - Fork 5
/
DistributedVerticle.java
179 lines (161 loc) · 6.84 KB
/
DistributedVerticle.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
package org.genericsystem.watch.gamma;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.ArrayBlockingQueue;
import java.util.concurrent.BlockingQueue;
import org.genericsystem.watch.beta.RoundRobin;
import io.vertx.core.AbstractVerticle;
import io.vertx.core.Future;
import io.vertx.core.Vertx;
import io.vertx.core.VertxOptions;
import io.vertx.core.eventbus.DeliveryOptions;
import io.vertx.core.eventbus.EventBusOptions;
import io.vertx.core.http.HttpClient;
import io.vertx.core.json.JsonObject;
import io.vertx.core.spi.cluster.ClusterManager;
import io.vertx.spi.cluster.hazelcast.HazelcastClusterManager;
public class DistributedVerticle extends AbstractVerticle {
private static final long AVAILABILITY_PERIODICITY = 10000;
private static final long ROUNDROBIN_PERIODICITY = 1000;
private static final String PUBLIC_ADDRESS = "publicAddress";
public static final String BASE_PATH = System.getenv("HOME") + "/git/genericsystem2015/gs-cv/";
private static final String FILENAME = "filename";
private static final String OK = "OK";
private static final String KO = "KO";
private static final String ID = "ID";
private static final String IP = "IP";
private final String PRIVATE_ADDRESS = "privateAddress " + hashCode();
private static final DeliveryOptions TIMEOUT = new DeliveryOptions().setSendTimeout(500);
private final RoundRobin roundrobin = new RoundRobin();
private List<JsonObject> messages = new ArrayList<>();
private List<JsonObject> tasks = new ArrayList<>();
private final String ip;
private DistributedVerticle(String ip) {
this.ip = ip;
System.out.println("Ip : " + ip);
long id = System.currentTimeMillis();
messages.add(new JsonObject().put(ID, id).put("task", new JsonObject().put(ID, id).put(FILENAME, "pdf/image.pdf").put(IP, ip)));
id = System.currentTimeMillis();
messages.add(new JsonObject().put(ID, id).put("task", new JsonObject().put(ID, id).put(FILENAME, "pdf/image2.pdf").put(IP, ip)));
id = System.currentTimeMillis();
messages.add(new JsonObject().put(ID, id).put("task", new JsonObject().put(ID, id).put(FILENAME, "pdf/image3.pdf").put(IP, ip)));
}
@Override
public void start() throws Exception {
vertx.eventBus().consumer(PRIVATE_ADDRESS, handler -> {
System.out.println("Receive from : " + (String) handler.body() + " on : " + PRIVATE_ADDRESS + " " + Thread.currentThread());
JsonObject task = new JsonObject((String) handler.body()).getJsonObject("task");
tasks.add(task);
handler.reply(OK);
String fileName = task.getString(FILENAME);
File file = new File(System.getenv("HOME") + "/copy/" + fileName);
if (!file.exists()) {
vertx.executeBlocking(future -> download(future, fileName, task.getString(IP)), result -> {
if (result.failed()) {
System.out.println("");
throw new IllegalStateException(result.cause());
}
System.out.println("Blocking task callback on thread : " + Thread.currentThread());
System.out.println("Task +" + task.encodePrettily() + "is done, removing " + Thread.currentThread());
tasks.remove(task);
});
} else
System.out.println("File : " + fileName + " is already dowloaded");
});
vertx.eventBus().consumer(PUBLIC_ADDRESS, message -> {
// System.out.println("Receive on plublic adress private address : " + (String) message.body() + " on : " + PRIVATE_ADDRESS + " " + Thread.currentThread());
roundrobin.register((String) message.body());
});
vertx.setPeriodic(AVAILABILITY_PERIODICITY, h -> {
// System.out.println("Periodic publish : "+PRIVATE_ADDRESS+" " + Thread.currentThread());
vertx.eventBus().publish(PUBLIC_ADDRESS, PRIVATE_ADDRESS);
});
vertx.setPeriodic(ROUNDROBIN_PERIODICITY, h -> {
for (JsonObject json : new ArrayList<>(messages)) {
String robin = roundrobin.getNextAddress();
if (robin != null) {
System.out.println("Periodic round robin from " + PRIVATE_ADDRESS + " to " + robin + " " + Thread.currentThread());
vertx.eventBus().send(robin, json.encodePrettily(), TIMEOUT, replyHandler -> {
if (replyHandler.failed())
throw new IllegalStateException(replyHandler.cause());
System.out.println("Receive response : " + replyHandler.result().body() + " " + Thread.currentThread());
if (OK.equals(replyHandler.result().body()))
messages.remove(json);
});
}
}
});
}
private <T> void download(Future<T> future, String fileName, String ip) {
// System.out.println("Execute blocking task : " + Thread.currentThread());
BlockingQueue<byte[]> blockingQueue = new ArrayBlockingQueue<>(1);
HttpClient httpClient = vertx.createHttpClient().getNow(8084, ip, fileName, resp -> resp.bodyHandler(body -> {
// System.out.println("get Bytes : " + Thread.currentThread());
try {
blockingQueue.put(body.getBytes());
} catch (InterruptedException e) {
future.fail(e);
return;
}
}));
httpClient.close();
byte[] bytes;
try {
bytes = blockingQueue.take();
} catch (InterruptedException e) {
e.printStackTrace();
future.fail(e);
return;
}
FileOutputStream fos;
try {
File file = new File(System.getenv("HOME") + "/copy/" + fileName);
new File(System.getenv("HOME") + "/copy/" + fileName.substring(0, fileName.lastIndexOf("/"))).mkdirs();
fos = new FileOutputStream(file);
fos.write(bytes);
fos.close();
} catch (IOException e) {
e.printStackTrace();
future.fail(e);
return;
}
// System.out.println("Blocking task complete on thread : " + Thread.currentThread());
future.complete();
}
public static void main(String[] args) {
ClusterManager mgr = new HazelcastClusterManager();
VertxOptions vertxOptions = new VertxOptions().setClustered(true).setClusterManager(mgr);
vertxOptions.setEventBusOptions(new EventBusOptions()).setClustered(true);
String ip = LocalNet.getIpAddress();
vertxOptions.setClusterHost(ip);
vertxOptions.setMaxWorkerExecuteTime(Long.MAX_VALUE);
Vertx.clusteredVertx(vertxOptions, res -> {
if (res.failed())
throw new IllegalStateException(res.cause());
Vertx vertx = res.result();
vertx.deployVerticle(new HttpServerVerticle(), complete -> {
if (complete.failed())
throw new IllegalStateException(complete.cause());
vertx.deployVerticle(new DistributedVerticle(ip), result -> {
if (complete.failed())
throw new IllegalStateException(complete.cause());
});
vertx.deployVerticle(new DistributedVerticle(ip), result -> {
if (complete.failed())
throw new IllegalStateException(complete.cause());
});
vertx.deployVerticle(new DistributedVerticle(ip), result -> {
if (complete.failed())
throw new IllegalStateException(complete.cause());
});
vertx.deployVerticle(new DistributedVerticle(ip), result -> {
if (complete.failed())
throw new IllegalStateException(complete.cause());
});
});
});
}
}