Mercurial > hg > Database > Alice
annotate src/alice/datasegment/RemoteDataSegmentManager.java @ 58:ebdcab7b9b04
add comment
author | one |
---|---|
date | Wed, 08 Feb 2012 17:06:39 +0900 |
parents | 27a64e771c4c |
children | 498d1d2524d3 |
rev | line source |
---|---|
12 | 1 package alice.datasegment; |
2 | |
23
54bf607118ae
change method to create RemoteDSM
kazz <kazz@cr.ie.u-ryukyu.ac.jp>
parents:
20
diff
changeset
|
3 import java.io.IOException; |
54bf607118ae
change method to create RemoteDSM
kazz <kazz@cr.ie.u-ryukyu.ac.jp>
parents:
20
diff
changeset
|
4 import java.net.InetSocketAddress; |
54bf607118ae
change method to create RemoteDSM
kazz <kazz@cr.ie.u-ryukyu.ac.jp>
parents:
20
diff
changeset
|
5 import java.nio.channels.SocketChannel; |
54bf607118ae
change method to create RemoteDSM
kazz <kazz@cr.ie.u-ryukyu.ac.jp>
parents:
20
diff
changeset
|
6 |
25 | 7 import org.apache.log4j.Logger; |
12 | 8 import org.msgpack.type.Value; |
9 | |
10 import alice.codesegment.CodeSegment; | |
14
e3f1b21718b0
implements RemoteDataSegment
kazz <kazz@cr.ie.u-ryukyu.ac.jp>
parents:
13
diff
changeset
|
11 import alice.daemon.Connection; |
e3f1b21718b0
implements RemoteDataSegment
kazz <kazz@cr.ie.u-ryukyu.ac.jp>
parents:
13
diff
changeset
|
12 import alice.daemon.IncomingTcpConnection; |
e3f1b21718b0
implements RemoteDataSegment
kazz <kazz@cr.ie.u-ryukyu.ac.jp>
parents:
13
diff
changeset
|
13 import alice.daemon.OutboundTcpConnection; |
12 | 14 |
14
e3f1b21718b0
implements RemoteDataSegment
kazz <kazz@cr.ie.u-ryukyu.ac.jp>
parents:
13
diff
changeset
|
15 public class RemoteDataSegmentManager extends DataSegmentManager { |
e3f1b21718b0
implements RemoteDataSegment
kazz <kazz@cr.ie.u-ryukyu.ac.jp>
parents:
13
diff
changeset
|
16 |
e3f1b21718b0
implements RemoteDataSegment
kazz <kazz@cr.ie.u-ryukyu.ac.jp>
parents:
13
diff
changeset
|
17 Connection connection; |
39 | 18 Logger logger; |
14
e3f1b21718b0
implements RemoteDataSegment
kazz <kazz@cr.ie.u-ryukyu.ac.jp>
parents:
13
diff
changeset
|
19 |
28
98ab26e09a98
Configuration Manager work and implements reverseKey
kazz <kazz@cr.ie.u-ryukyu.ac.jp>
parents:
27
diff
changeset
|
20 public RemoteDataSegmentManager(String connectionKey, final String reverseKey, final String hostName, final int port) { |
39 | 21 logger = Logger.getLogger(connectionKey); |
23
54bf607118ae
change method to create RemoteDSM
kazz <kazz@cr.ie.u-ryukyu.ac.jp>
parents:
20
diff
changeset
|
22 connection = new Connection(); |
54bf607118ae
change method to create RemoteDSM
kazz <kazz@cr.ie.u-ryukyu.ac.jp>
parents:
20
diff
changeset
|
23 final RemoteDataSegmentManager manager = this; |
28
98ab26e09a98
Configuration Manager work and implements reverseKey
kazz <kazz@cr.ie.u-ryukyu.ac.jp>
parents:
27
diff
changeset
|
24 new Thread(replyThread, "RemoteDataSegmentManager-" + connectionKey).start(); |
98ab26e09a98
Configuration Manager work and implements reverseKey
kazz <kazz@cr.ie.u-ryukyu.ac.jp>
parents:
27
diff
changeset
|
25 new Thread("Connect-" + connectionKey) { |
23
54bf607118ae
change method to create RemoteDSM
kazz <kazz@cr.ie.u-ryukyu.ac.jp>
parents:
20
diff
changeset
|
26 public void run() { |
54bf607118ae
change method to create RemoteDSM
kazz <kazz@cr.ie.u-ryukyu.ac.jp>
parents:
20
diff
changeset
|
27 boolean connect = true; |
54bf607118ae
change method to create RemoteDSM
kazz <kazz@cr.ie.u-ryukyu.ac.jp>
parents:
20
diff
changeset
|
28 do { |
54bf607118ae
change method to create RemoteDSM
kazz <kazz@cr.ie.u-ryukyu.ac.jp>
parents:
20
diff
changeset
|
29 try { |
54bf607118ae
change method to create RemoteDSM
kazz <kazz@cr.ie.u-ryukyu.ac.jp>
parents:
20
diff
changeset
|
30 SocketChannel sc = SocketChannel.open(new InetSocketAddress(hostName, port)); |
54bf607118ae
change method to create RemoteDSM
kazz <kazz@cr.ie.u-ryukyu.ac.jp>
parents:
20
diff
changeset
|
31 connection.socket = sc.socket(); |
54bf607118ae
change method to create RemoteDSM
kazz <kazz@cr.ie.u-ryukyu.ac.jp>
parents:
20
diff
changeset
|
32 connect = false; |
39 | 33 logger.info("Connect to " + connection.getInfoString()); |
23
54bf607118ae
change method to create RemoteDSM
kazz <kazz@cr.ie.u-ryukyu.ac.jp>
parents:
20
diff
changeset
|
34 } catch (IOException e) { |
54bf607118ae
change method to create RemoteDSM
kazz <kazz@cr.ie.u-ryukyu.ac.jp>
parents:
20
diff
changeset
|
35 try { |
54
27a64e771c4c
change connection wait time
kazz <kazz@cr.ie.u-ryukyu.ac.jp>
parents:
41
diff
changeset
|
36 Thread.sleep(50); |
23
54bf607118ae
change method to create RemoteDSM
kazz <kazz@cr.ie.u-ryukyu.ac.jp>
parents:
20
diff
changeset
|
37 } catch (InterruptedException e1) { |
54bf607118ae
change method to create RemoteDSM
kazz <kazz@cr.ie.u-ryukyu.ac.jp>
parents:
20
diff
changeset
|
38 e1.printStackTrace(); |
54bf607118ae
change method to create RemoteDSM
kazz <kazz@cr.ie.u-ryukyu.ac.jp>
parents:
20
diff
changeset
|
39 } |
54bf607118ae
change method to create RemoteDSM
kazz <kazz@cr.ie.u-ryukyu.ac.jp>
parents:
20
diff
changeset
|
40 } |
54bf607118ae
change method to create RemoteDSM
kazz <kazz@cr.ie.u-ryukyu.ac.jp>
parents:
20
diff
changeset
|
41 } while (connect); |
28
98ab26e09a98
Configuration Manager work and implements reverseKey
kazz <kazz@cr.ie.u-ryukyu.ac.jp>
parents:
27
diff
changeset
|
42 new IncomingTcpConnection(connection, manager, reverseKey).start(); |
23
54bf607118ae
change method to create RemoteDSM
kazz <kazz@cr.ie.u-ryukyu.ac.jp>
parents:
20
diff
changeset
|
43 new OutboundTcpConnection(connection).start(); |
54bf607118ae
change method to create RemoteDSM
kazz <kazz@cr.ie.u-ryukyu.ac.jp>
parents:
20
diff
changeset
|
44 } |
54bf607118ae
change method to create RemoteDSM
kazz <kazz@cr.ie.u-ryukyu.ac.jp>
parents:
20
diff
changeset
|
45 }.start(); |
54bf607118ae
change method to create RemoteDSM
kazz <kazz@cr.ie.u-ryukyu.ac.jp>
parents:
20
diff
changeset
|
46 } |
54bf607118ae
change method to create RemoteDSM
kazz <kazz@cr.ie.u-ryukyu.ac.jp>
parents:
20
diff
changeset
|
47 |
58 | 48 /** |
49 * send put command to target DataSegment | |
50 */ | |
12 | 51 @Override |
40 | 52 public void put(String key, Value val, CodeSegment cs) { |
53 Command cmd = new Command(CommandType.PUT, null, key, val, 0, 0, null, cs, null); | |
58 | 54 connection.sendCommand(cmd); // put command on the transmission thread |
40 | 55 logger.debug(cmd.getCommandString()); |
12 | 56 } |
57 | |
58 @Override | |
40 | 59 public void update(String key, Value val, CodeSegment cs) { |
60 Command cmd = new Command(CommandType.UPDATE, null, key, val, 0, 0, null, cs, null); | |
39 | 61 connection.sendCommand(cmd); |
40 | 62 logger.debug(cmd.getCommandString()); |
12 | 63 } |
64 | |
65 @Override | |
33
20c67f673224
change name of DataSegmentReceiver
kazz <kazz@cr.ie.u-ryukyu.ac.jp>
parents:
30
diff
changeset
|
66 public void take(Receiver receiver, String key, int index, CodeSegment cs) { |
14
e3f1b21718b0
implements RemoteDataSegment
kazz <kazz@cr.ie.u-ryukyu.ac.jp>
parents:
13
diff
changeset
|
67 int seq = this.seq.getAndIncrement(); |
28
98ab26e09a98
Configuration Manager work and implements reverseKey
kazz <kazz@cr.ie.u-ryukyu.ac.jp>
parents:
27
diff
changeset
|
68 Command cmd = new Command(CommandType.TAKE, receiver, key, null, index, seq, replyQueue, cs, null); |
14
e3f1b21718b0
implements RemoteDataSegment
kazz <kazz@cr.ie.u-ryukyu.ac.jp>
parents:
13
diff
changeset
|
69 seqHash.put(seq, cmd); |
39 | 70 connection.sendCommand(cmd); |
40 | 71 logger.debug(cmd.getCommandString()); |
12 | 72 } |
73 | |
74 @Override | |
33
20c67f673224
change name of DataSegmentReceiver
kazz <kazz@cr.ie.u-ryukyu.ac.jp>
parents:
30
diff
changeset
|
75 public void peek(Receiver receiver, String key, int index, CodeSegment cs) { |
14
e3f1b21718b0
implements RemoteDataSegment
kazz <kazz@cr.ie.u-ryukyu.ac.jp>
parents:
13
diff
changeset
|
76 int seq = this.seq.getAndIncrement(); |
28
98ab26e09a98
Configuration Manager work and implements reverseKey
kazz <kazz@cr.ie.u-ryukyu.ac.jp>
parents:
27
diff
changeset
|
77 Command cmd = new Command(CommandType.PEEK, receiver, key, null, index, seq, replyQueue, cs, null); |
14
e3f1b21718b0
implements RemoteDataSegment
kazz <kazz@cr.ie.u-ryukyu.ac.jp>
parents:
13
diff
changeset
|
78 seqHash.put(seq, cmd); |
e3f1b21718b0
implements RemoteDataSegment
kazz <kazz@cr.ie.u-ryukyu.ac.jp>
parents:
13
diff
changeset
|
79 connection.sendCommand(cmd); |
40 | 80 logger.debug(cmd.getCommandString()); |
12 | 81 } |
82 | |
83 @Override | |
84 public void remove(String key) { | |
39 | 85 Command cmd = new Command(CommandType.REMOVE, null, key, null, 0, 0, null, null, null); |
86 connection.sendCommand(cmd); | |
40 | 87 logger.debug(cmd.getCommandString()); |
12 | 88 } |
41 | 89 |
90 @Override | |
30 | 91 public void finish() { |
39 | 92 Command cmd = new Command(CommandType.FINISH, null, null, null, 0, 0, null, null, null); |
93 connection.sendCommand(cmd); | |
30 | 94 } |
13 | 95 |
41 | 96 @Override |
97 public void close() { | |
98 Command cmd = new Command(CommandType.CLOSE, null, null, null, 0, 0, null, null, null); | |
99 connection.sendCommand(cmd); | |
100 } | |
101 | |
12 | 102 } |