-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMasterServer.java
More file actions
243 lines (215 loc) · 7.14 KB
/
Copy pathMasterServer.java
File metadata and controls
243 lines (215 loc) · 7.14 KB
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
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.io.PrintWriter;
import java.rmi.NotBoundException;
import java.rmi.RemoteException;
import java.rmi.registry.LocateRegistry;
import java.rmi.registry.Registry;
import java.rmi.server.UnicastRemoteObject;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map.Entry;
import java.util.Random;
import java.util.StringTokenizer;
import java.util.Timer;
import java.util.TimerTask;
public class MasterServer implements MasterServerClientInterface {
HashMap<Integer, ReplicaLoc> replicaPaths;
HashMap<String, Integer> filePrimReplica;
ArrayList<String> files;
int numReplicas = 4;
Random rand;
int txID = 0;
long executionTime;
Logger masterLogger;
public MasterServer() throws IOException {
this.rand = new Random();
this.executionTime = 0;
masterLogger = Logger.getInstance();
// initialize the file list
this.files = new ArrayList<String>();
readFilesDirectory();
// initialize the file primary replica map
filePrimReplica = new HashMap<String, Integer>();
for (int i = 0; i < files.size(); i++)
filePrimReplica.put(files.get(i), rand.nextInt(numReplicas));
// initialize the replica path map
masterLogger
.logMessage(" Initializing replicas paths in Master Server ");
replicaPaths = new HashMap<Integer, ReplicaLoc>();
BufferedReader br = new BufferedReader(new FileReader(
Global.REPLICA_INPUT_PATH));
int replicaIndex = 0;
String line;
StringTokenizer st;
while ((line = br.readLine()) != null) {
st = new StringTokenizer(line, Global.REPLICA_DELIM);
replicaPaths.put(replicaIndex, new ReplicaLoc(st.nextToken(),
Integer.parseInt(st.nextToken()), replicaIndex));
replicaIndex++;
}
br.close();
doHearbeats();
}
private void doHearbeats() {
new Timer().schedule(new TimerTask() {
@Override
public void run() {
// this code will be executed after 5 seconds
masterLogger.logMessage("===>> Heartbeats @ sec = "
+ executionTime);
for (Iterator<Entry<Integer, ReplicaLoc>> iterator = replicaPaths
.entrySet().iterator(); iterator.hasNext();) {
Entry<Integer, ReplicaLoc> entry = iterator.next();
ReplicaLoc repl = entry.getValue();
String replLoc = repl.location;
int replPort = repl.replicaPort;
// Reading from replica
Registry registryReplica1 = null;
try {
registryReplica1 = LocateRegistry.getRegistry(replLoc,
replPort);
} catch (RemoteException e) {
e.printStackTrace();
}
ReplicaServerClientInterface replHandler = null;
try {
replHandler = (ReplicaServerClientInterface) registryReplica1
.lookup(Global.REPLICA_LOOKUP);
} catch (RemoteException | NotBoundException e) {
e.printStackTrace();
}
try {
if (!replHandler.checkIsAlive()) {
masterLogger.logMessage("Replica #"
+ replHandler.getReplicaID()
+ " is NOT alive !");
} else {
masterLogger.logMessage("Replica #"
+ replHandler.getReplicaID()
+ " is alive !");
}
} catch (RemoteException e) {
e.printStackTrace();
}
}
masterLogger.logMessage("===>> Heartbeats END");
executionTime += 60;
doHearbeats();
}
}, 60 * 1000);
}
private void readFilesDirectory() throws IOException {
BufferedReader buff = new BufferedReader(new FileReader(
Global.FILES_DIRECTORY));
String fName = "";
while ((fName = buff.readLine()) != null)
this.files.add(fName);
buff.close();
}
@Override
public HashMap<Integer, ReplicaLoc> getReplicaPaths()
throws RemoteException {
return replicaPaths;
}
@Override
public ReplicaLoc[] read(String fileName) throws FileNotFoundException,
IOException, RemoteException {
masterLogger.logMessage("===>> read '" + fileName + "' request");
for (int i = 0; i < files.size(); i++)
masterLogger.logMessage(files.get(i));
if (files.contains(fileName)) {
ReplicaLoc primLoc = replicaPaths
.get(filePrimReplica.get(fileName));
return new ReplicaLoc[] { primLoc };
}
masterLogger.logMessage("===>> " + fileName + " not exsist");
return null;
}
@Override
public WriteMsg write(FileContent data) throws RemoteException,
IOException, NotBoundException {
masterLogger.logMessage("===>> Write '" + data.fileName + "' request");
if (!files.contains(data.fileName)) {
// create new file and set meta data
files.add(data.fileName);
filePrimReplica.put(data.fileName, rand.nextInt(numReplicas));
for (Iterator<Entry<Integer, ReplicaLoc>> iterator = replicaPaths
.entrySet().iterator(); iterator.hasNext();) {
Entry<Integer, ReplicaLoc> entry = iterator.next();
ReplicaLoc repl = entry.getValue();
String replLoc = repl.location;
int replPort = repl.replicaPort;
// Reading from replica
Registry registryReplica1 = LocateRegistry.getRegistry(replLoc,
replPort);
ReplicaServerClientInterface replHandler = (ReplicaServerClientInterface) registryReplica1
.lookup(Global.REPLICA_LOOKUP);
replHandler.createFile(data.fileName);
}
// edit in filesDirectory.in
try {
PrintWriter out = new PrintWriter(new BufferedWriter(
new FileWriter(Global.FILES_DIRECTORY, true)));
out.print(data.fileName + "\n");
out.close();
} catch (Exception e) {
System.err.println("Error in broadcast method: "
+ e.getMessage());
}
// done
masterLogger
.logMessage("===>> Creating empty file in all replicas ....");
}
ReplicaLoc primLoc = replicaPaths.get(filePrimReplica
.get(data.fileName));
WriteMsg wMsg = new WriteMsg(txID++, 1, primLoc);
return wMsg;
}
public void notify(int ack, String fileName) {
switch (ack) {
case Global.READACK:
masterLogger.logMessage("===> done reading " + fileName);
break;
case Global.WRITEACK:
masterLogger.logMessage("===> done writing " + fileName
+ "in primary replica");
break;
case Global.BROADCASTSTARTACK:
masterLogger.logMessage("===> Start flushing " + fileName
+ " to all replicas");
break;
case Global.BROADCASTENDACK:
masterLogger.logMessage("===> done flushing " + fileName
+ " to all replicas");
break;
default:
masterLogger.logMessage("===> Aport ");
break;
}
}
public static void main(String[] args) {
try {
Configurator config = Configurator.getInstance();
String masterHostname = config.getMasterHostname();
int masterPort = config.getMasterPort();
System.setProperty("java.rmi.server.hostname", masterHostname);
LocateRegistry.createRegistry(masterPort);
MasterServer obj = new MasterServer();
MasterServerClientInterface stub = (MasterServerClientInterface) UnicastRemoteObject
.exportObject(obj, 0);
// Bind the remote object's stub in the registry
Registry registry = LocateRegistry.getRegistry(masterPort);
registry.rebind("MasterServerClientInterface", stub);
obj.masterLogger.logMessage("\t\t Master Server ready ! ");
} catch (Exception e) {
System.err.println(" Master Server exception: " + e.toString());
e.printStackTrace();
}
}
}