annotate src/main/java/alice/datasegment/LocalDataSegmentManager.java @ 650:4289b232b3fd

nulValue
author suruga
date Fri, 02 Feb 2018 18:26:49 +0900
parents 8c17a9e66cc7
children e91a574b69de 80a6c4a1c601 ffa43f252492
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
345
8f71c3e6f11d Change directory structure Maven standard
sugi
parents:
diff changeset
1 package alice.datasegment;
8f71c3e6f11d Change directory structure Maven standard
sugi
parents:
diff changeset
2
8f71c3e6f11d Change directory structure Maven standard
sugi
parents:
diff changeset
3 import java.util.concurrent.ConcurrentHashMap;
8f71c3e6f11d Change directory structure Maven standard
sugi
parents:
diff changeset
4 import java.util.concurrent.LinkedBlockingQueue;
8f71c3e6f11d Change directory structure Maven standard
sugi
parents:
diff changeset
5 import java.util.concurrent.ThreadPoolExecutor;
8f71c3e6f11d Change directory structure Maven standard
sugi
parents:
diff changeset
6 import java.util.concurrent.TimeUnit;
8f71c3e6f11d Change directory structure Maven standard
sugi
parents:
diff changeset
7
8f71c3e6f11d Change directory structure Maven standard
sugi
parents:
diff changeset
8 import org.apache.log4j.Logger;
8f71c3e6f11d Change directory structure Maven standard
sugi
parents:
diff changeset
9
8f71c3e6f11d Change directory structure Maven standard
sugi
parents:
diff changeset
10 import alice.codesegment.CodeSegment;
8f71c3e6f11d Change directory structure Maven standard
sugi
parents:
diff changeset
11
8f71c3e6f11d Change directory structure Maven standard
sugi
parents:
diff changeset
12 public class LocalDataSegmentManager extends DataSegmentManager {
419
aefbe41fcf12 change tab to space
sugi
parents: 366
diff changeset
13
aefbe41fcf12 change tab to space
sugi
parents: 366
diff changeset
14 private String reverseKey = "local";
aefbe41fcf12 change tab to space
sugi
parents: 366
diff changeset
15 private ConcurrentHashMap<String, DataSegmentKey> dataSegments = new ConcurrentHashMap<String, DataSegmentKey>();
aefbe41fcf12 change tab to space
sugi
parents: 366
diff changeset
16 private Logger logger = Logger.getLogger("local");
aefbe41fcf12 change tab to space
sugi
parents: 366
diff changeset
17
aefbe41fcf12 change tab to space
sugi
parents: 366
diff changeset
18 private ThreadPoolExecutor dataSegmentExecutor = new ThreadPoolExecutor(Runtime.getRuntime().availableProcessors(), // initial number of threads
aefbe41fcf12 change tab to space
sugi
parents: 366
diff changeset
19 Runtime.getRuntime().availableProcessors(),
aefbe41fcf12 change tab to space
sugi
parents: 366
diff changeset
20 Integer.MAX_VALUE, // keepAliveTime
aefbe41fcf12 change tab to space
sugi
parents: 366
diff changeset
21 TimeUnit.SECONDS,
aefbe41fcf12 change tab to space
sugi
parents: 366
diff changeset
22 new LinkedBlockingQueue<Runnable>());
aefbe41fcf12 change tab to space
sugi
parents: 366
diff changeset
23
aefbe41fcf12 change tab to space
sugi
parents: 366
diff changeset
24 public LocalDataSegmentManager() {
aefbe41fcf12 change tab to space
sugi
parents: 366
diff changeset
25 new Thread(replyThread, "LocalDataSegmentManager-replyCommand").start();
aefbe41fcf12 change tab to space
sugi
parents: 366
diff changeset
26 }
aefbe41fcf12 change tab to space
sugi
parents: 366
diff changeset
27
aefbe41fcf12 change tab to space
sugi
parents: 366
diff changeset
28 public void setReverseKey(String s){
aefbe41fcf12 change tab to space
sugi
parents: 366
diff changeset
29 reverseKey = s;
aefbe41fcf12 change tab to space
sugi
parents: 366
diff changeset
30 }
aefbe41fcf12 change tab to space
sugi
parents: 366
diff changeset
31
aefbe41fcf12 change tab to space
sugi
parents: 366
diff changeset
32 private class RunCommand implements Runnable {
aefbe41fcf12 change tab to space
sugi
parents: 366
diff changeset
33
aefbe41fcf12 change tab to space
sugi
parents: 366
diff changeset
34 DataSegmentKey key;
aefbe41fcf12 change tab to space
sugi
parents: 366
diff changeset
35 Command cmd;
aefbe41fcf12 change tab to space
sugi
parents: 366
diff changeset
36
aefbe41fcf12 change tab to space
sugi
parents: 366
diff changeset
37 public RunCommand(DataSegmentKey key, Command cmd) {
aefbe41fcf12 change tab to space
sugi
parents: 366
diff changeset
38 this.key = key;
aefbe41fcf12 change tab to space
sugi
parents: 366
diff changeset
39 this.cmd = cmd;
aefbe41fcf12 change tab to space
sugi
parents: 366
diff changeset
40 }
aefbe41fcf12 change tab to space
sugi
parents: 366
diff changeset
41
aefbe41fcf12 change tab to space
sugi
parents: 366
diff changeset
42 @Override
aefbe41fcf12 change tab to space
sugi
parents: 366
diff changeset
43 public void run() {
aefbe41fcf12 change tab to space
sugi
parents: 366
diff changeset
44 key.runCommand(cmd);
aefbe41fcf12 change tab to space
sugi
parents: 366
diff changeset
45 }
aefbe41fcf12 change tab to space
sugi
parents: 366
diff changeset
46
aefbe41fcf12 change tab to space
sugi
parents: 366
diff changeset
47 }
345
8f71c3e6f11d Change directory structure Maven standard
sugi
parents:
diff changeset
48
419
aefbe41fcf12 change tab to space
sugi
parents: 366
diff changeset
49 public void submitCommand(DataSegmentKey key, Command cmd) {
aefbe41fcf12 change tab to space
sugi
parents: 366
diff changeset
50 dataSegmentExecutor.execute(new RunCommand(key, cmd));
aefbe41fcf12 change tab to space
sugi
parents: 366
diff changeset
51 }
aefbe41fcf12 change tab to space
sugi
parents: 366
diff changeset
52
aefbe41fcf12 change tab to space
sugi
parents: 366
diff changeset
53 public DataSegmentKey getDataSegmentKey(String key) {
aefbe41fcf12 change tab to space
sugi
parents: 366
diff changeset
54 DataSegmentKey dsKey = dataSegments.get(key);
aefbe41fcf12 change tab to space
sugi
parents: 366
diff changeset
55 if (dsKey != null)
aefbe41fcf12 change tab to space
sugi
parents: 366
diff changeset
56 return dsKey;
aefbe41fcf12 change tab to space
sugi
parents: 366
diff changeset
57 if (key == null)
aefbe41fcf12 change tab to space
sugi
parents: 366
diff changeset
58 return null;
aefbe41fcf12 change tab to space
sugi
parents: 366
diff changeset
59 DataSegmentKey newDataSegmentKey = new DataSegmentKey();
aefbe41fcf12 change tab to space
sugi
parents: 366
diff changeset
60 DataSegmentKey dataSegmentKey = dataSegments.putIfAbsent(key, newDataSegmentKey);
aefbe41fcf12 change tab to space
sugi
parents: 366
diff changeset
61 if (dataSegmentKey == null) {
aefbe41fcf12 change tab to space
sugi
parents: 366
diff changeset
62 dataSegmentKey = newDataSegmentKey;
aefbe41fcf12 change tab to space
sugi
parents: 366
diff changeset
63 }
aefbe41fcf12 change tab to space
sugi
parents: 366
diff changeset
64 return dataSegmentKey;
aefbe41fcf12 change tab to space
sugi
parents: 366
diff changeset
65 }
aefbe41fcf12 change tab to space
sugi
parents: 366
diff changeset
66
491
7d67bc538549 bug fix
sugi
parents: 483
diff changeset
67 public void removeDataSegmentKey(String key) {
7d67bc538549 bug fix
sugi
parents: 483
diff changeset
68 if (key!=null)
7d67bc538549 bug fix
sugi
parents: 483
diff changeset
69 dataSegments.remove(key);
7d67bc538549 bug fix
sugi
parents: 483
diff changeset
70 }
7d67bc538549 bug fix
sugi
parents: 483
diff changeset
71
419
aefbe41fcf12 change tab to space
sugi
parents: 366
diff changeset
72 @Override
533
b3c9554ccb1b change compressed API to set data specified DSM name
Nozomi Teruya <e125769@ie.u-ryukyu.ac.jp>
parents: 529
diff changeset
73 public void put(String key, ReceiveData rData, boolean quickFlag) {
538
8c17a9e66cc7 Compressed LDSM refactoring & flip refactoring
Nozomi Teruya <e125769@ie.u-ryukyu.ac.jp>
parents: 533
diff changeset
74 Command cmd = new Command(CommandType.PUT, null, key, rData, 0, 0, null, null, reverseKey);
8c17a9e66cc7 Compressed LDSM refactoring & flip refactoring
Nozomi Teruya <e125769@ie.u-ryukyu.ac.jp>
parents: 533
diff changeset
75 put1(key, cmd);
8c17a9e66cc7 Compressed LDSM refactoring & flip refactoring
Nozomi Teruya <e125769@ie.u-ryukyu.ac.jp>
parents: 533
diff changeset
76 }
8c17a9e66cc7 Compressed LDSM refactoring & flip refactoring
Nozomi Teruya <e125769@ie.u-ryukyu.ac.jp>
parents: 533
diff changeset
77
8c17a9e66cc7 Compressed LDSM refactoring & flip refactoring
Nozomi Teruya <e125769@ie.u-ryukyu.ac.jp>
parents: 533
diff changeset
78 public void put1(String key, Command cmd) {
419
aefbe41fcf12 change tab to space
sugi
parents: 366
diff changeset
79 DataSegmentKey dataSegmentKey = getDataSegmentKey(key);
aefbe41fcf12 change tab to space
sugi
parents: 366
diff changeset
80 dataSegmentKey.runCommand(cmd);
aefbe41fcf12 change tab to space
sugi
parents: 366
diff changeset
81 if (logger.isDebugEnabled())
aefbe41fcf12 change tab to space
sugi
parents: 366
diff changeset
82 logger.debug(cmd.getCommandString());
aefbe41fcf12 change tab to space
sugi
parents: 366
diff changeset
83 }
aefbe41fcf12 change tab to space
sugi
parents: 366
diff changeset
84
aefbe41fcf12 change tab to space
sugi
parents: 366
diff changeset
85 /**
aefbe41fcf12 change tab to space
sugi
parents: 366
diff changeset
86 * Enqueue update command to the queue of each DataSegment key
aefbe41fcf12 change tab to space
sugi
parents: 366
diff changeset
87 */
345
8f71c3e6f11d Change directory structure Maven standard
sugi
parents:
diff changeset
88
419
aefbe41fcf12 change tab to space
sugi
parents: 366
diff changeset
89 @Override
533
b3c9554ccb1b change compressed API to set data specified DSM name
Nozomi Teruya <e125769@ie.u-ryukyu.ac.jp>
parents: 529
diff changeset
90 public void update(String key, ReceiveData rData, boolean quickFlag) {
458
bcf6f4a6fcd0 need set Meta DataSegment PUT API
sugi
parents: 455
diff changeset
91 Command cmd = new Command(CommandType.UPDATE, null, key, rData, 0, 0, null, null, reverseKey);
538
8c17a9e66cc7 Compressed LDSM refactoring & flip refactoring
Nozomi Teruya <e125769@ie.u-ryukyu.ac.jp>
parents: 533
diff changeset
92 put1(key, cmd);
419
aefbe41fcf12 change tab to space
sugi
parents: 366
diff changeset
93 }
aefbe41fcf12 change tab to space
sugi
parents: 366
diff changeset
94
aefbe41fcf12 change tab to space
sugi
parents: 366
diff changeset
95 @Override
533
b3c9554ccb1b change compressed API to set data specified DSM name
Nozomi Teruya <e125769@ie.u-ryukyu.ac.jp>
parents: 529
diff changeset
96 public void take(Receiver receiver, CodeSegment cs, boolean quickFlag) {
538
8c17a9e66cc7 Compressed LDSM refactoring & flip refactoring
Nozomi Teruya <e125769@ie.u-ryukyu.ac.jp>
parents: 533
diff changeset
97
8c17a9e66cc7 Compressed LDSM refactoring & flip refactoring
Nozomi Teruya <e125769@ie.u-ryukyu.ac.jp>
parents: 533
diff changeset
98 Command cmd = new Command(CommandType.TAKE, receiver, receiver.key, null, receiver.index, 0, replyQueue, cs, null);
8c17a9e66cc7 Compressed LDSM refactoring & flip refactoring
Nozomi Teruya <e125769@ie.u-ryukyu.ac.jp>
parents: 533
diff changeset
99 take1(receiver, cmd);
8c17a9e66cc7 Compressed LDSM refactoring & flip refactoring
Nozomi Teruya <e125769@ie.u-ryukyu.ac.jp>
parents: 533
diff changeset
100 }
8c17a9e66cc7 Compressed LDSM refactoring & flip refactoring
Nozomi Teruya <e125769@ie.u-ryukyu.ac.jp>
parents: 533
diff changeset
101
8c17a9e66cc7 Compressed LDSM refactoring & flip refactoring
Nozomi Teruya <e125769@ie.u-ryukyu.ac.jp>
parents: 533
diff changeset
102 public void take1(Receiver receiver, Command cmd) {
419
aefbe41fcf12 change tab to space
sugi
parents: 366
diff changeset
103 int seq = this.seq.getAndIncrement();
538
8c17a9e66cc7 Compressed LDSM refactoring & flip refactoring
Nozomi Teruya <e125769@ie.u-ryukyu.ac.jp>
parents: 533
diff changeset
104 cmd.setSeq(seq);
8c17a9e66cc7 Compressed LDSM refactoring & flip refactoring
Nozomi Teruya <e125769@ie.u-ryukyu.ac.jp>
parents: 533
diff changeset
105 //seqHash.put(seq, cmd);
8c17a9e66cc7 Compressed LDSM refactoring & flip refactoring
Nozomi Teruya <e125769@ie.u-ryukyu.ac.jp>
parents: 533
diff changeset
106 DataSegmentKey dataSegmentKey = getDataSegmentKey(receiver.key);
419
aefbe41fcf12 change tab to space
sugi
parents: 366
diff changeset
107 dataSegmentKey.runCommand(cmd);
aefbe41fcf12 change tab to space
sugi
parents: 366
diff changeset
108 if (logger.isDebugEnabled())
aefbe41fcf12 change tab to space
sugi
parents: 366
diff changeset
109 logger.debug(cmd.getCommandString());
aefbe41fcf12 change tab to space
sugi
parents: 366
diff changeset
110 }
aefbe41fcf12 change tab to space
sugi
parents: 366
diff changeset
111
aefbe41fcf12 change tab to space
sugi
parents: 366
diff changeset
112 @Override
533
b3c9554ccb1b change compressed API to set data specified DSM name
Nozomi Teruya <e125769@ie.u-ryukyu.ac.jp>
parents: 529
diff changeset
113 public void peek(Receiver receiver, CodeSegment cs, boolean quickFlag) {
538
8c17a9e66cc7 Compressed LDSM refactoring & flip refactoring
Nozomi Teruya <e125769@ie.u-ryukyu.ac.jp>
parents: 533
diff changeset
114 Command cmd = new Command(CommandType.PEEK, receiver, receiver.key, null, receiver.index, 0, replyQueue, cs, null);
8c17a9e66cc7 Compressed LDSM refactoring & flip refactoring
Nozomi Teruya <e125769@ie.u-ryukyu.ac.jp>
parents: 533
diff changeset
115 take1(receiver, cmd);
419
aefbe41fcf12 change tab to space
sugi
parents: 366
diff changeset
116 }
345
8f71c3e6f11d Change directory structure Maven standard
sugi
parents:
diff changeset
117
419
aefbe41fcf12 change tab to space
sugi
parents: 366
diff changeset
118 @Override
aefbe41fcf12 change tab to space
sugi
parents: 366
diff changeset
119 public void remove(String key) {
aefbe41fcf12 change tab to space
sugi
parents: 366
diff changeset
120 DataSegmentKey dataSegmentKey = getDataSegmentKey(key);
aefbe41fcf12 change tab to space
sugi
parents: 366
diff changeset
121 Command cmd = new Command(CommandType.REMOVE, null, key, null, 0, 0, replyQueue, null, null);
aefbe41fcf12 change tab to space
sugi
parents: 366
diff changeset
122 dataSegmentKey.runCommand(cmd);
aefbe41fcf12 change tab to space
sugi
parents: 366
diff changeset
123 if (logger.isDebugEnabled())
aefbe41fcf12 change tab to space
sugi
parents: 366
diff changeset
124 logger.debug(cmd.getCommandString());
aefbe41fcf12 change tab to space
sugi
parents: 366
diff changeset
125 }
aefbe41fcf12 change tab to space
sugi
parents: 366
diff changeset
126
aefbe41fcf12 change tab to space
sugi
parents: 366
diff changeset
127 @Override public void finish() {
aefbe41fcf12 change tab to space
sugi
parents: 366
diff changeset
128 System.exit(0);
aefbe41fcf12 change tab to space
sugi
parents: 366
diff changeset
129 }
aefbe41fcf12 change tab to space
sugi
parents: 366
diff changeset
130
aefbe41fcf12 change tab to space
sugi
parents: 366
diff changeset
131 @Override
aefbe41fcf12 change tab to space
sugi
parents: 366
diff changeset
132 public void close() {
aefbe41fcf12 change tab to space
sugi
parents: 366
diff changeset
133
aefbe41fcf12 change tab to space
sugi
parents: 366
diff changeset
134 }
345
8f71c3e6f11d Change directory structure Maven standard
sugi
parents:
diff changeset
135
419
aefbe41fcf12 change tab to space
sugi
parents: 366
diff changeset
136 public void recommand(Receiver receiver, CodeSegment cs) {
aefbe41fcf12 change tab to space
sugi
parents: 366
diff changeset
137 DataSegmentKey dataSegmentKey = getDataSegmentKey(receiver.key);
aefbe41fcf12 change tab to space
sugi
parents: 366
diff changeset
138 int seq = this.seq.getAndIncrement();
aefbe41fcf12 change tab to space
sugi
parents: 366
diff changeset
139 Command cmd = new Command(receiver.type, receiver, receiver.key, null, receiver.index, seq, replyQueue, cs, null);
aefbe41fcf12 change tab to space
sugi
parents: 366
diff changeset
140 dataSegmentKey.runCommand(cmd);
aefbe41fcf12 change tab to space
sugi
parents: 366
diff changeset
141 if (logger.isDebugEnabled())
aefbe41fcf12 change tab to space
sugi
parents: 366
diff changeset
142 logger.debug(cmd.getCommandString());
aefbe41fcf12 change tab to space
sugi
parents: 366
diff changeset
143 }
345
8f71c3e6f11d Change directory structure Maven standard
sugi
parents:
diff changeset
144
419
aefbe41fcf12 change tab to space
sugi
parents: 366
diff changeset
145 @Override
aefbe41fcf12 change tab to space
sugi
parents: 366
diff changeset
146 public void ping(String returnKey) {
aefbe41fcf12 change tab to space
sugi
parents: 366
diff changeset
147
aefbe41fcf12 change tab to space
sugi
parents: 366
diff changeset
148 }
345
8f71c3e6f11d Change directory structure Maven standard
sugi
parents:
diff changeset
149
419
aefbe41fcf12 change tab to space
sugi
parents: 366
diff changeset
150 @Override
aefbe41fcf12 change tab to space
sugi
parents: 366
diff changeset
151 public void response(String returnKey) {
aefbe41fcf12 change tab to space
sugi
parents: 366
diff changeset
152
aefbe41fcf12 change tab to space
sugi
parents: 366
diff changeset
153 }
345
8f71c3e6f11d Change directory structure Maven standard
sugi
parents:
diff changeset
154
419
aefbe41fcf12 change tab to space
sugi
parents: 366
diff changeset
155 @Override
aefbe41fcf12 change tab to space
sugi
parents: 366
diff changeset
156 public void shutdown() {
345
8f71c3e6f11d Change directory structure Maven standard
sugi
parents:
diff changeset
157
419
aefbe41fcf12 change tab to space
sugi
parents: 366
diff changeset
158 }
345
8f71c3e6f11d Change directory structure Maven standard
sugi
parents:
diff changeset
159
483
86c45738dd9e success fix topology ..... may be
sugi
parents: 458
diff changeset
160 @Override
86c45738dd9e success fix topology ..... may be
sugi
parents: 458
diff changeset
161 public void setSendError(boolean b) {
86c45738dd9e success fix topology ..... may be
sugi
parents: 458
diff changeset
162
86c45738dd9e success fix topology ..... may be
sugi
parents: 458
diff changeset
163 }
345
8f71c3e6f11d Change directory structure Maven standard
sugi
parents:
diff changeset
164 }