-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathChainClient.java
More file actions
266 lines (231 loc) · 10 KB
/
ChainClient.java
File metadata and controls
266 lines (231 loc) · 10 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
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
package cs249;
import edu.sjsu.cs249.chain.*;
import io.grpc.ManagedChannel;
import io.grpc.ManagedChannelBuilder;
import io.grpc.Server;
import io.grpc.ServerBuilder;
import org.apache.zookeeper.KeeperException;
import org.apache.zookeeper.WatchedEvent;
import org.apache.zookeeper.Watcher;
import org.apache.zookeeper.ZooKeeper;
import static io.grpc.stub.ServerCalls.asyncUnimplementedUnaryCall;
import java.io.IOException;
import java.net.InetSocketAddress;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Scanner;
import java.util.concurrent.ScheduledThreadPoolExecutor;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.atomic.AtomicBoolean;
public class ChainClient extends TailClientGrpc.TailClientImplBase implements Watcher {
private Server server;
private String host;
private String zk_directory;
private int port;
private ZooKeeper zk;
private int cxid;
private HashMap<Integer,com.google.protobuf.GeneratedMessageV3> sentRequest;
private long currentChainTail;
private long currentChainHead;
private TailChainReplicaGrpc.TailChainReplicaBlockingStub chainTail;
private TailChainReplicaGrpc.TailChainReplicaBlockingStub chainHead;
private AtomicBoolean retryChainCheck = new AtomicBoolean();
private void setChainHead(long session) throws KeeperException, InterruptedException {
if (currentChainHead == session) {
return;
}
chainHead = null;
chainHead = getStub(session);
currentChainHead = session;
System.out.println("Head is " + String.valueOf(session));
}
private TailChainReplicaGrpc.TailChainReplicaBlockingStub getStub(long session) throws KeeperException, InterruptedException {
byte data[] = zk.getData( this.zk_directory + "/" + Long.toHexString(session), false, null);
InetSocketAddress addr = str2addr(new String(data).split("\n")[0]);
ManagedChannel channel = ManagedChannelBuilder.forAddress(addr.getHostName(), addr.getPort()).usePlaintext().build();
return TailChainReplicaGrpc.newBlockingStub(channel);
}
private void setChainTail(long session) throws KeeperException, InterruptedException {
if (currentChainTail == session) {
return;
}
chainTail = null;
chainTail = getStub(session);
currentChainTail = session;
System.out.println("Tail is " + String.valueOf(session));
}
private static InetSocketAddress str2addr(String addr) {
int colon = addr.lastIndexOf(':');
return new InetSocketAddress(addr.substring(0, colon), Integer.parseInt(addr.substring(colon+1)));
}
private ChainClient(String zkServer, String _directory, String _myHost, int _myPort) throws KeeperException, InterruptedException, IOException {
this.host = _myHost;
this.port = _myPort;
this.zk_directory = _directory;
this.server = ServerBuilder.forPort(_myPort).addService(this).build();
this.cxid = 0;
this.sentRequest = new HashMap<Integer,com.google.protobuf.GeneratedMessageV3>();
zk = new ZooKeeper(zkServer, 2181, this);
while ( Long.toHexString(this.zk.getSessionId()).matches("0")) {
Thread.sleep(50);
}
System.out.println("ZooKeeper connected with session ID: " + Long.toHexString(this.zk.getSessionId()));
this.chainHead = null;
this.chainHead = null;
checkHeadTail();
new ScheduledThreadPoolExecutor(1).scheduleAtFixedRate(
() -> { if (retryChainCheck.getAndSet(false)) checkHeadTail();},
5,
5,
TimeUnit.SECONDS
);
}
private int getNewCxid() {
return this.cxid ++;
}
private void get(String key) {
if ( this.chainTail != null ) {
GetResponse rsp = chainTail.get(GetRequest.newBuilder().setKey(key).build());
if (rsp.getRc() != 0) {
System.out.printf("Error %d occurred\n", rsp.getRc());
} else {
System.out.printf("%d\n", rsp.getValue());
}
} // if
else {
System.out.println("Tail is unavailable.");
}
}
private void del(String key) {
if ( this.chainHead != null ) {
int value = this.getNewCxid();
TailDeleteRequest rsq = TailDeleteRequest.newBuilder().setKey(key).setCxid(value).setHost(this.host).setPort(this.port).build();
this.sentRequest.put(value, rsq);
HeadResponse rsp = chainHead.delete(rsq);
if (rsp.getRc() != 0) {
System.out.printf("Error %d occurred\n", rsp.getRc());
} else {
System.out.println("%s request accept with cxid:" + String.valueOf(value ));
}
} // if
else {
System.out.println("Head is unavailable.");
}
}
private void inc(String key, int val) {
if ( this.chainHead != null ) {
int value = this.getNewCxid();
TailIncrementRequest req = TailIncrementRequest.newBuilder().setKey(key).setIncrValue(val).setCxid(value).setHost(this.host).setPort(this.port).build();
this.sentRequest.put(value, req);
HeadResponse rsp = chainHead.increment(req);
if (rsp.getRc() != 0) {
System.out.printf("Error %d occurred\n", rsp.getRc());
} else {
System.out.println("request accept with cxid:" + String.valueOf(value ));
}
} // if
else {
System.out.println("Head is unavailable.");
}
}
@Override
public void cxidProcessed(edu.sjsu.cs249.chain.CxidProcessedRequest request,
io.grpc.stub.StreamObserver<edu.sjsu.cs249.chain.ChainResponse> responseObserver) {
int cxid = request.getCxid();
ChainResponse rsp;
if ( this.sentRequest.containsKey(cxid)) {
rsp = ChainResponse.newBuilder().setRc(0).build();
com.google.protobuf.GeneratedMessageV3 req = this.sentRequest.get(cxid);
if ( req.getClass() == TailIncrementRequest.class) {
System.out.printf("Increment Request with cxid:%d, Key:%s, Incr:%d\n", cxid, ((TailIncrementRequest) req).getKey(), ((TailIncrementRequest)req).getIncrValue());
this.sentRequest.remove(cxid);
} // if
else {
System.out.printf("Delete Request with cxid:%d, Key:%s\n", cxid, ((TailDeleteRequest) req).getKey());
this.sentRequest.remove(cxid);
} // else
} // if
else {
rsp = ChainResponse.newBuilder().setRc(1).build();
} // else
responseObserver.onNext(rsp);
responseObserver.onCompleted();
}
public static void main(String args[]) throws Exception {
// args[0] for zookeeper IP, port is 2180 as default
// args[1] for zookeeper directory
// args[2] for local host for this client
// args[3] for listening port for this client
System.out.println("An application made by Ching-Chan Lee for CS249 HW2 Chain Replication-Tail");
ChainClient client = new ChainClient(args[0], args[1], args[2], Integer.valueOf(args[3]));
// ChainClient client = new ChainClient("10.10.10.1", "/tailchain_hello", "10.10.10.1", Integer.valueOf("60000"));
client.server.start();
Scanner scanner = new Scanner(System.in);
String line;
System.out.println("Pleese Enter Command!");
while ((line = scanner.nextLine()) != null) {
String parts[] = line.split(" ");
if (parts[0].equals("get")) {
client.get(parts[1]);
} else if (parts[0].equals("inc")) {
if ( parts.length == 3 )
client.inc(parts[1], Integer.parseInt(parts[2]));
else
System.out.println("Incorrect format! >>INC KEY VALUE<<");
} else if (parts[0].equals("del")) {
client.del(parts[1]);
} else {
System.out.println("don't know " + parts[0]);
System.out.println("i know:");
System.out.println("get key");
System.out.println("inc key value");
System.out.println("del key");
}
}
client.server.awaitTermination();
}
private void checkHeadTail() {
try {
List<String> children = zk.getChildren(this.zk_directory, true);
ArrayList<Long> sessions = new ArrayList<Long>();
for (String child: children) {
try {
if ( child.startsWith("0x") )
child = child.substring(2);
System.out.println(Long.parseLong(child, 16));
sessions.add(Long.parseLong(child, 16));
} catch (NumberFormatException e) {
e.printStackTrace();
}
}
if ( sessions.size() == 0) {
return ;
} // if
else if ( sessions.size() == 1 ) {
long head = sessions.get(0);
long tail = sessions.get(children.size() - 1);
setChainHead(head);
setChainTail(tail);
} // else if
else {
System.out.println("Enter else");
sessions.sort(Long::compare);
long head = sessions.get(0);
long tail = sessions.get(children.size() - 1);
setChainHead(head);
setChainTail(tail);
} // else
} catch (KeeperException | InterruptedException e) {
retryChainCheck.set(true);
}
}
@Override
public void process(WatchedEvent watchedEvent) {
if (watchedEvent.getState().equals(Event.KeeperState.Expired) || watchedEvent.getState().equals(Event.KeeperState.Closed)) {
System.err.println("disconnected from zookeeper");
System.exit(2);
}
checkHeadTail();
}
}