-
Notifications
You must be signed in to change notification settings - Fork 0
/
Dstore.java
310 lines (273 loc) · 12.6 KB
/
Dstore.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
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
import java.io.BufferedReader;
import java.io.File;
import java.io.IOException;
import java.net.Socket;
import java.net.UnknownHostException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Comparator;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Set;
import java.util.concurrent.Callable;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.stream.Collectors;
import java.util.stream.IntStream;
import java.util.stream.Stream;
import java.io.PrintWriter;
import java.net.InetAddress;
import java.net.ServerSocket;
import java.io.InputStreamReader;
public class Dstore {
private static Integer port, cport, timeout;
private static String fileFolder;
//Logger
private static final Logger logger = Logger.getLogger(Dstore.class);
//Cnontroller Handler
private static ControllerHandler controllerHandler;
//Task Thread pool
private static ExecutorService taskPool = Executors.newCachedThreadPool();
public static void main(String[] args) {
try { //Parse inputs
port = Integer.parseInt(args[0]);
cport = Integer.parseInt(args[1]);
timeout = Integer.parseInt(args[2]); //ms
fileFolder = args[3];
} catch (Exception e) {
logger.err("Malformed arguemnts; java Dstore port cport timeout file_folder");
return;
}
try { // Clear Folder Contents
Path folderPath = Paths.get(fileFolder);
logger.info("Cleaning folder contents: " + folderPath.toAbsolutePath().toString());
if (Files.exists(folderPath))
Files.walk(folderPath)
.sorted(Comparator.reverseOrder())
.map(Path::toFile)
.forEach(File::delete);
Files.createDirectory(folderPath);
} catch (IOException e) {
logger.err("Error cleaning folder: '" + fileFolder + "''");
return;
}
try { // Connect To The Controller
connectToController();
} catch (Exception e) {
logger.err("Error connecting to controller");
return;
}
acceptConnections(); // Accept Connections From Clients and other Dstores
}
private static void connectToController() throws UnknownHostException, IOException {
Socket socket = new Socket(InetAddress.getLocalHost(), cport);
controllerHandler = new ControllerHandler(socket);
new Thread(controllerHandler).start();
}
private static void acceptConnections() {
logger.info("Accepting connections");
try (ServerSocket serverSocket = new ServerSocket(port)) {
for (;;) {
try {
Socket socket = serverSocket.accept();
taskPool.execute(new ClientHandler(socket));
} catch (IOException e) { logger.err("Error Creating Client"); }
}
} catch (IOException e) {
logger.err("Error Accepting Connections - Clients can no longer connect");
}
}
private static class ClientHandler extends ConnectionHandler {
//Logger
private static final Logger logger = Logger.getLogger(ClientHandler.class);
public ClientHandler(Socket socket) throws IOException {
super(socket);
socket.setSoTimeout(timeout);
logger.info("New Client Created");
}
public void run() {
try (socket; out; in) {
String message;
if ((message = in.readLine()) != null) handleMessage(message);
} catch (Exception e) { } finally {
logger.info("Client Disconnected");
}
}
private void handleMessage(String message) {
try {
var arguments = message.split(" ");
switch (getOperation(message)) {
case Protocol.STORE -> store(arguments[1], Integer.parseInt(arguments[2]), false);
case Protocol.LOAD_DATA -> load(arguments[1]);
case Protocol.REBALANCE_STORE -> store(arguments[1], Integer.parseInt(arguments[2]), true);
default -> logger.err("Malformed client message: '" + message + "'");
}
} catch (Exception e) {
logger.err("Error handling client/dstore message: '" + message + "'");
}
}
private void store(String filename, Integer filesize, Boolean isRebalance) {
logger.info("Store operation started for file: '" + filename + "'..");
out.println(Protocol.ACK); // Notify client message received
try {
byte[] data = new byte[filesize]; //Get data from client
socket.getInputStream().readNBytes(data, 0, filesize);
Path path = Paths.get(fileFolder, filename); //Store file in folder
Files.write(path, data);
if (!isRebalance) controllerHandler.sendMessage(Protocol.STORE_ACK + " " + filename); // Notify Controller store complete
logger.info("Store of file '" + filename + "' complete");
} catch (Exception e) {
logger.err("Error storing file: '" + filename + "'");
}
}
private void load(String filename) {
logger.info("Load operation started for file: '" + filename + "'..");
try {
Path path = Paths.get(fileFolder, filename);
if (!Files.exists(path)) return; // If file doesn't exist
byte[] data = Files.readAllBytes(path); // Read file
socket.getOutputStream().write(data); // Send data
logger.info("Load of file '" + filename + "' complete");
} catch (Exception e) {
logger.err("Error loading file: " + filename + "'");
}
}
}
private static class ControllerHandler extends ConnectionHandler {
//Logger
private static final Logger logger = Logger.getLogger(ControllerHandler.class);
public ControllerHandler(Socket socket) throws IOException {
super(socket);
out.println("JOIN " + port);
logger.info("Connected to Controller");
}
public void sendMessage(String message) {
out.println(message);
}
public void run() {
try (socket; in; out) {
String message;
while ((message = in.readLine()) != null) handleMessage(message);
} catch (Exception e) { } finally {
logger.info("Server connection offline");
System.exit(0);
}
}
private void handleMessage(String message) {
try {
var arguments = message.split(" ");
switch (getOperation(message)) {
case Protocol.LIST -> list();
case Protocol.REMOVE -> remove(arguments[1]);
case Protocol.REBALANCE -> {
var rebal = Arrays.asList(arguments);
HashMap<String, Set<Integer>> filesToSend = new HashMap<>();
ArrayList<String> filesToDelete = new ArrayList<>();
var iter = rebal.iterator();
iter.next();
// Get values from message
var noOfFiles = Integer.parseInt(iter.next());
IntStream.range(0, noOfFiles).forEach(i -> {
var filename = iter.next();
var noOfPorts = Integer.parseInt(iter.next());
Set<Integer> ports = new HashSet<>();
IntStream.range(0, noOfPorts).forEach(j -> ports.add(Integer.parseInt(iter.next())));
filesToSend.put(filename, ports);
});
var noOfFilesDelete = Integer.parseInt(iter.next());
IntStream.range(0, noOfFilesDelete).forEach(i -> filesToDelete.add(iter.next()));
rebalance(filesToSend, filesToDelete);
}
default -> logger.err("Malformed controller message: '" + message + "'");
}
} catch (Exception e) {
logger.err("Error handling controller message");
}
}
private void list() {
logger.info("List operation started..");
try (Stream<Path> stream = Files.list(Paths.get(fileFolder))) {
var files = String.join(" ", stream.filter(file -> !Files.isDirectory(file))
.map(Path::getFileName).map(Path::toString)
.collect(Collectors.toList()));
out.println((files.length() == 0) ? Protocol.LIST : Protocol.LIST + " " + files);
logger.info("List complete");
} catch (Exception e) {
logger.err("Error in list operation");
}
}
private void remove(String filename) {
logger.info("Remove operation started for: '" + filename + "'..");
Path path = Paths.get(fileFolder, filename);
try {
if (Files.deleteIfExists(path)) out.println(Protocol.REMOVE_ACK + " " + filename);
else out.println(Protocol.ERROR_FILE_DOES_NOT_EXIST + " " + filename);
logger.info("Removal of file: '" + filename + "' complete");
} catch (IOException e) {
logger.err("Error removing file: " + filename);
}
}
private void rebalance(HashMap<String, Set<Integer>> filesToSend, ArrayList<String> filesToDelete) {
logger.info("Rebalance operation started..");
Set<SendFile> allFilesSend = new HashSet<>();
filesToSend.entrySet().stream().forEach(e -> {
Integer filesize;
try { filesize = (int) Files.size(Paths.get(fileFolder, e.getKey()));
} catch (Exception e1) { return; }
e.getValue().forEach(p -> allFilesSend.add(new SendFile(e.getKey(), p, filesize)));
});
try {
var results = taskPool.invokeAll(allFilesSend);
var allSuccessful = results.stream().allMatch(e -> {
try { return e.get();
} catch (Exception ex) { return false; }
});
if (!allSuccessful) return;
} catch (Exception e1) { return; }
filesToDelete.forEach(f -> {
try {
logger.info("Deleting file '" + f + "'");
Files.deleteIfExists(Paths.get(fileFolder, f));
} catch (Exception e) { return; }
});
out.println(Protocol.REBALANCE_COMPLETE);
logger.info("Rebalance operation complete");
}
private static class SendFile implements Callable<Boolean> {
private final String filename;
private final Integer filesize;
private final Integer port;
public SendFile(String filename, Integer port, Integer filesize) {
this.filename = filename;
this.filesize = filesize;
this.port = port;
}
@Override
public Boolean call() {
try (
Socket socket = new Socket(InetAddress.getLocalHost(), port);
PrintWriter out = new PrintWriter(socket.getOutputStream(), true);
BufferedReader in = new BufferedReader(new InputStreamReader(socket.getInputStream()));
) {
logger.info("Sending '" + filename + "' to: " + port);
socket.setSoTimeout(timeout);
out.println(Protocol.REBALANCE_STORE + " " + filename + " " + filesize);
if (in.readLine().equals(Protocol.ACK)) {
byte[] data = Files.readAllBytes(Paths.get(fileFolder, filename));
socket.getOutputStream().write(data);
return true;
}
} catch (Exception e) {
logger.err("Error sending '" + filename + "' to: " + port);
}
return false;
}
}
}
private static String getOperation(String message) {
return message.split(" ", 2)[0];
}
}