annotate src/main/java/alice/datasegment/LocalDataSegmentManager.java @ 419:aefbe41fcf12 dispose

change tab to space
author sugi
date Tue, 15 Jul 2014 16:00:22 +0900
parents abc54fa0c81b
children 3fff2edc8d33
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
aefbe41fcf12 change tab to space
sugi
parents: 366
diff changeset
67 @Override
aefbe41fcf12 change tab to space
sugi
parents: 366
diff changeset
68 public void put(String key, Object val) {
aefbe41fcf12 change tab to space
sugi
parents: 366
diff changeset
69 DataSegmentKey dataSegmentKey = getDataSegmentKey(key);
aefbe41fcf12 change tab to space
sugi
parents: 366
diff changeset
70 Command cmd = new Command(CommandType.PUT, null, key, val, 0, 0, null, null, reverseKey);
aefbe41fcf12 change tab to space
sugi
parents: 366
diff changeset
71 dataSegmentKey.runCommand(cmd);
aefbe41fcf12 change tab to space
sugi
parents: 366
diff changeset
72 if (logger.isDebugEnabled())
aefbe41fcf12 change tab to space
sugi
parents: 366
diff changeset
73 logger.debug(cmd.getCommandString());
aefbe41fcf12 change tab to space
sugi
parents: 366
diff changeset
74 }
aefbe41fcf12 change tab to space
sugi
parents: 366
diff changeset
75
aefbe41fcf12 change tab to space
sugi
parents: 366
diff changeset
76 @Override
aefbe41fcf12 change tab to space
sugi
parents: 366
diff changeset
77 public void quickPut(String key, Object val) {
aefbe41fcf12 change tab to space
sugi
parents: 366
diff changeset
78 put(key, val);
aefbe41fcf12 change tab to space
sugi
parents: 366
diff changeset
79 }
aefbe41fcf12 change tab to space
sugi
parents: 366
diff changeset
80
aefbe41fcf12 change tab to space
sugi
parents: 366
diff changeset
81 /**
aefbe41fcf12 change tab to space
sugi
parents: 366
diff changeset
82 * Enqueue update command to the queue of each DataSegment key
aefbe41fcf12 change tab to space
sugi
parents: 366
diff changeset
83 */
345
8f71c3e6f11d Change directory structure Maven standard
sugi
parents:
diff changeset
84
419
aefbe41fcf12 change tab to space
sugi
parents: 366
diff changeset
85 @Override
aefbe41fcf12 change tab to space
sugi
parents: 366
diff changeset
86 public void update(String key, Object val) {
aefbe41fcf12 change tab to space
sugi
parents: 366
diff changeset
87 DataSegmentKey dataSegmentKey = getDataSegmentKey(key);
aefbe41fcf12 change tab to space
sugi
parents: 366
diff changeset
88 Command cmd = new Command(CommandType.UPDATE, null, key, val, 0, 0, null, null, reverseKey);
aefbe41fcf12 change tab to space
sugi
parents: 366
diff changeset
89 dataSegmentKey.runCommand(cmd);
aefbe41fcf12 change tab to space
sugi
parents: 366
diff changeset
90 if (logger.isDebugEnabled())
aefbe41fcf12 change tab to space
sugi
parents: 366
diff changeset
91 logger.debug(cmd.getCommandString());
aefbe41fcf12 change tab to space
sugi
parents: 366
diff changeset
92 }
aefbe41fcf12 change tab to space
sugi
parents: 366
diff changeset
93
aefbe41fcf12 change tab to space
sugi
parents: 366
diff changeset
94 @Override
aefbe41fcf12 change tab to space
sugi
parents: 366
diff changeset
95 public void quickUpdate(String key, Object val) {
aefbe41fcf12 change tab to space
sugi
parents: 366
diff changeset
96 update(key, val);
aefbe41fcf12 change tab to space
sugi
parents: 366
diff changeset
97 }
aefbe41fcf12 change tab to space
sugi
parents: 366
diff changeset
98
aefbe41fcf12 change tab to space
sugi
parents: 366
diff changeset
99 @Override
aefbe41fcf12 change tab to space
sugi
parents: 366
diff changeset
100 public void take(Receiver receiver, CodeSegment cs) {
aefbe41fcf12 change tab to space
sugi
parents: 366
diff changeset
101 DataSegmentKey dataSegmentKey = getDataSegmentKey(receiver.key);
aefbe41fcf12 change tab to space
sugi
parents: 366
diff changeset
102 int seq = this.seq.getAndIncrement();
aefbe41fcf12 change tab to space
sugi
parents: 366
diff changeset
103 Command cmd = new Command(CommandType.TAKE, receiver, receiver.key, null, receiver.index, seq, replyQueue, cs, null);
aefbe41fcf12 change tab to space
sugi
parents: 366
diff changeset
104 dataSegmentKey.runCommand(cmd);
aefbe41fcf12 change tab to space
sugi
parents: 366
diff changeset
105 if (logger.isDebugEnabled())
aefbe41fcf12 change tab to space
sugi
parents: 366
diff changeset
106 logger.debug(cmd.getCommandString());
aefbe41fcf12 change tab to space
sugi
parents: 366
diff changeset
107 }
aefbe41fcf12 change tab to space
sugi
parents: 366
diff changeset
108
aefbe41fcf12 change tab to space
sugi
parents: 366
diff changeset
109 @Override
aefbe41fcf12 change tab to space
sugi
parents: 366
diff changeset
110 public void quickTake(Receiver receiver, CodeSegment cs) {
aefbe41fcf12 change tab to space
sugi
parents: 366
diff changeset
111 take(receiver, cs);
aefbe41fcf12 change tab to space
sugi
parents: 366
diff changeset
112 }
aefbe41fcf12 change tab to space
sugi
parents: 366
diff changeset
113
aefbe41fcf12 change tab to space
sugi
parents: 366
diff changeset
114 @Override
aefbe41fcf12 change tab to space
sugi
parents: 366
diff changeset
115 public void peek(Receiver receiver, CodeSegment cs) {
aefbe41fcf12 change tab to space
sugi
parents: 366
diff changeset
116 DataSegmentKey dataSegmentKey = getDataSegmentKey(receiver.key);
aefbe41fcf12 change tab to space
sugi
parents: 366
diff changeset
117 int seq = this.seq.getAndIncrement();
aefbe41fcf12 change tab to space
sugi
parents: 366
diff changeset
118 Command cmd = new Command(CommandType.PEEK, receiver, receiver.key, null, receiver.index, seq, replyQueue, cs, null);
aefbe41fcf12 change tab to space
sugi
parents: 366
diff changeset
119 dataSegmentKey.runCommand(cmd);
aefbe41fcf12 change tab to space
sugi
parents: 366
diff changeset
120 if (logger.isDebugEnabled())
aefbe41fcf12 change tab to space
sugi
parents: 366
diff changeset
121 logger.debug(cmd.getCommandString());
aefbe41fcf12 change tab to space
sugi
parents: 366
diff changeset
122 }
345
8f71c3e6f11d Change directory structure Maven standard
sugi
parents:
diff changeset
123
419
aefbe41fcf12 change tab to space
sugi
parents: 366
diff changeset
124 @Override
aefbe41fcf12 change tab to space
sugi
parents: 366
diff changeset
125 public void quickPeek(Receiver receiver, CodeSegment cs) {
aefbe41fcf12 change tab to space
sugi
parents: 366
diff changeset
126 peek(receiver, cs);
aefbe41fcf12 change tab to space
sugi
parents: 366
diff changeset
127 }
aefbe41fcf12 change tab to space
sugi
parents: 366
diff changeset
128
aefbe41fcf12 change tab to space
sugi
parents: 366
diff changeset
129 @Override
aefbe41fcf12 change tab to space
sugi
parents: 366
diff changeset
130 public void remove(String key) {
aefbe41fcf12 change tab to space
sugi
parents: 366
diff changeset
131 DataSegmentKey dataSegmentKey = getDataSegmentKey(key);
aefbe41fcf12 change tab to space
sugi
parents: 366
diff changeset
132 Command cmd = new Command(CommandType.REMOVE, null, key, null, 0, 0, replyQueue, null, null);
aefbe41fcf12 change tab to space
sugi
parents: 366
diff changeset
133 dataSegmentKey.runCommand(cmd);
aefbe41fcf12 change tab to space
sugi
parents: 366
diff changeset
134 if (logger.isDebugEnabled())
aefbe41fcf12 change tab to space
sugi
parents: 366
diff changeset
135 logger.debug(cmd.getCommandString());
aefbe41fcf12 change tab to space
sugi
parents: 366
diff changeset
136 }
aefbe41fcf12 change tab to space
sugi
parents: 366
diff changeset
137
aefbe41fcf12 change tab to space
sugi
parents: 366
diff changeset
138 @Override public void finish() {
aefbe41fcf12 change tab to space
sugi
parents: 366
diff changeset
139 System.exit(0);
aefbe41fcf12 change tab to space
sugi
parents: 366
diff changeset
140 }
aefbe41fcf12 change tab to space
sugi
parents: 366
diff changeset
141
aefbe41fcf12 change tab to space
sugi
parents: 366
diff changeset
142 @Override
aefbe41fcf12 change tab to space
sugi
parents: 366
diff changeset
143 public void close() {
aefbe41fcf12 change tab to space
sugi
parents: 366
diff changeset
144
aefbe41fcf12 change tab to space
sugi
parents: 366
diff changeset
145 }
345
8f71c3e6f11d Change directory structure Maven standard
sugi
parents:
diff changeset
146
419
aefbe41fcf12 change tab to space
sugi
parents: 366
diff changeset
147 public void recommand(Receiver receiver, CodeSegment cs) {
aefbe41fcf12 change tab to space
sugi
parents: 366
diff changeset
148 DataSegmentKey dataSegmentKey = getDataSegmentKey(receiver.key);
aefbe41fcf12 change tab to space
sugi
parents: 366
diff changeset
149 int seq = this.seq.getAndIncrement();
aefbe41fcf12 change tab to space
sugi
parents: 366
diff changeset
150 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
151 dataSegmentKey.runCommand(cmd);
aefbe41fcf12 change tab to space
sugi
parents: 366
diff changeset
152 if (logger.isDebugEnabled())
aefbe41fcf12 change tab to space
sugi
parents: 366
diff changeset
153 logger.debug(cmd.getCommandString());
345
8f71c3e6f11d Change directory structure Maven standard
sugi
parents:
diff changeset
154
419
aefbe41fcf12 change tab to space
sugi
parents: 366
diff changeset
155 }
345
8f71c3e6f11d Change directory structure Maven standard
sugi
parents:
diff changeset
156
419
aefbe41fcf12 change tab to space
sugi
parents: 366
diff changeset
157 @Override
aefbe41fcf12 change tab to space
sugi
parents: 366
diff changeset
158 public void ping(String returnKey) {
aefbe41fcf12 change tab to space
sugi
parents: 366
diff changeset
159
aefbe41fcf12 change tab to space
sugi
parents: 366
diff changeset
160 }
345
8f71c3e6f11d Change directory structure Maven standard
sugi
parents:
diff changeset
161
419
aefbe41fcf12 change tab to space
sugi
parents: 366
diff changeset
162 @Override
aefbe41fcf12 change tab to space
sugi
parents: 366
diff changeset
163 public void response(String returnKey) {
aefbe41fcf12 change tab to space
sugi
parents: 366
diff changeset
164
aefbe41fcf12 change tab to space
sugi
parents: 366
diff changeset
165 }
345
8f71c3e6f11d Change directory structure Maven standard
sugi
parents:
diff changeset
166
419
aefbe41fcf12 change tab to space
sugi
parents: 366
diff changeset
167 @Override
aefbe41fcf12 change tab to space
sugi
parents: 366
diff changeset
168 public void shutdown() {
345
8f71c3e6f11d Change directory structure Maven standard
sugi
parents:
diff changeset
169
419
aefbe41fcf12 change tab to space
sugi
parents: 366
diff changeset
170 }
345
8f71c3e6f11d Change directory structure Maven standard
sugi
parents:
diff changeset
171
8f71c3e6f11d Change directory structure Maven standard
sugi
parents:
diff changeset
172 }