annotate src/alice/datasegment/LocalDataSegmentManager.java @ 73:4bfd81352cfa

change to concurrent data segment
author kazz <kazz@cr.ie.u-ryukyu.ac.jp>
date Tue, 21 Feb 2012 22:55:17 +0900
parents a3a2605e16a2
children 82a1c25ca0c8
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
2
f71eabb1df2a create outline of DataSegment model
one
parents:
diff changeset
1 package alice.datasegment;
f71eabb1df2a create outline of DataSegment model
one
parents:
diff changeset
2
56
17f88fd202ae refactor data segment
kazz <kazz@cr.ie.u-ryukyu.ac.jp>
parents: 41
diff changeset
3 import java.util.concurrent.ConcurrentHashMap;
69
1d4f2b72fb31 delete KeyThread
kazz <kazz@cr.ie.u-ryukyu.ac.jp>
parents: 68
diff changeset
4 import java.util.concurrent.LinkedBlockingQueue;
73
4bfd81352cfa change to concurrent data segment
kazz <kazz@cr.ie.u-ryukyu.ac.jp>
parents: 71
diff changeset
5 import java.util.concurrent.ThreadPoolExecutor;
4bfd81352cfa change to concurrent data segment
kazz <kazz@cr.ie.u-ryukyu.ac.jp>
parents: 71
diff changeset
6 import java.util.concurrent.TimeUnit;
56
17f88fd202ae refactor data segment
kazz <kazz@cr.ie.u-ryukyu.ac.jp>
parents: 41
diff changeset
7
68
d4c7f7b1096b remove copy at OutboundTcpConnection
kazz <kazz@cr.ie.u-ryukyu.ac.jp>
parents: 67
diff changeset
8 import org.apache.log4j.Logger;
2
f71eabb1df2a create outline of DataSegment model
one
parents:
diff changeset
9 import org.msgpack.type.Value;
f71eabb1df2a create outline of DataSegment model
one
parents:
diff changeset
10
3
91057e15065f add DataSegment API and CodeSegment
one
parents: 2
diff changeset
11 import alice.codesegment.CodeSegment;
91057e15065f add DataSegment API and CodeSegment
one
parents: 2
diff changeset
12
2
f71eabb1df2a create outline of DataSegment model
one
parents:
diff changeset
13 public class LocalDataSegmentManager extends DataSegmentManager {
f71eabb1df2a create outline of DataSegment model
one
parents:
diff changeset
14
56
17f88fd202ae refactor data segment
kazz <kazz@cr.ie.u-ryukyu.ac.jp>
parents: 41
diff changeset
15 private String reverseKey = "local";
17f88fd202ae refactor data segment
kazz <kazz@cr.ie.u-ryukyu.ac.jp>
parents: 41
diff changeset
16 private ConcurrentHashMap<String, DataSegmentKey> dataSegments = new ConcurrentHashMap<String, DataSegmentKey>();
69
1d4f2b72fb31 delete KeyThread
kazz <kazz@cr.ie.u-ryukyu.ac.jp>
parents: 68
diff changeset
17 private LinkedBlockingQueue<KeyCommand> cmdQueue = new LinkedBlockingQueue<KeyCommand>();
68
d4c7f7b1096b remove copy at OutboundTcpConnection
kazz <kazz@cr.ie.u-ryukyu.ac.jp>
parents: 67
diff changeset
18 private Logger logger = Logger.getLogger("local");
69
1d4f2b72fb31 delete KeyThread
kazz <kazz@cr.ie.u-ryukyu.ac.jp>
parents: 68
diff changeset
19
73
4bfd81352cfa change to concurrent data segment
kazz <kazz@cr.ie.u-ryukyu.ac.jp>
parents: 71
diff changeset
20 private ThreadPoolExecutor dataSegmentExecutor = new ThreadPoolExecutor(Runtime.getRuntime().availableProcessors(), // initial number of threads
4bfd81352cfa change to concurrent data segment
kazz <kazz@cr.ie.u-ryukyu.ac.jp>
parents: 71
diff changeset
21 Runtime.getRuntime().availableProcessors(),
4bfd81352cfa change to concurrent data segment
kazz <kazz@cr.ie.u-ryukyu.ac.jp>
parents: 71
diff changeset
22 Integer.MAX_VALUE, // keepAliveTime
4bfd81352cfa change to concurrent data segment
kazz <kazz@cr.ie.u-ryukyu.ac.jp>
parents: 71
diff changeset
23 TimeUnit.SECONDS,
4bfd81352cfa change to concurrent data segment
kazz <kazz@cr.ie.u-ryukyu.ac.jp>
parents: 71
diff changeset
24 new LinkedBlockingQueue<Runnable>());
4bfd81352cfa change to concurrent data segment
kazz <kazz@cr.ie.u-ryukyu.ac.jp>
parents: 71
diff changeset
25
69
1d4f2b72fb31 delete KeyThread
kazz <kazz@cr.ie.u-ryukyu.ac.jp>
parents: 68
diff changeset
26 private Runnable keyCommandThread = new Runnable() {
1d4f2b72fb31 delete KeyThread
kazz <kazz@cr.ie.u-ryukyu.ac.jp>
parents: 68
diff changeset
27
1d4f2b72fb31 delete KeyThread
kazz <kazz@cr.ie.u-ryukyu.ac.jp>
parents: 68
diff changeset
28 @Override
1d4f2b72fb31 delete KeyThread
kazz <kazz@cr.ie.u-ryukyu.ac.jp>
parents: 68
diff changeset
29 public void run() {
1d4f2b72fb31 delete KeyThread
kazz <kazz@cr.ie.u-ryukyu.ac.jp>
parents: 68
diff changeset
30 while (true) {
1d4f2b72fb31 delete KeyThread
kazz <kazz@cr.ie.u-ryukyu.ac.jp>
parents: 68
diff changeset
31 KeyCommand keyCmd = null;
1d4f2b72fb31 delete KeyThread
kazz <kazz@cr.ie.u-ryukyu.ac.jp>
parents: 68
diff changeset
32 try {
1d4f2b72fb31 delete KeyThread
kazz <kazz@cr.ie.u-ryukyu.ac.jp>
parents: 68
diff changeset
33 keyCmd = cmdQueue.take();
1d4f2b72fb31 delete KeyThread
kazz <kazz@cr.ie.u-ryukyu.ac.jp>
parents: 68
diff changeset
34 } catch (InterruptedException e) {
1d4f2b72fb31 delete KeyThread
kazz <kazz@cr.ie.u-ryukyu.ac.jp>
parents: 68
diff changeset
35 e.printStackTrace();
1d4f2b72fb31 delete KeyThread
kazz <kazz@cr.ie.u-ryukyu.ac.jp>
parents: 68
diff changeset
36 }
1d4f2b72fb31 delete KeyThread
kazz <kazz@cr.ie.u-ryukyu.ac.jp>
parents: 68
diff changeset
37 keyCmd.runCommand();
1d4f2b72fb31 delete KeyThread
kazz <kazz@cr.ie.u-ryukyu.ac.jp>
parents: 68
diff changeset
38 }
1d4f2b72fb31 delete KeyThread
kazz <kazz@cr.ie.u-ryukyu.ac.jp>
parents: 68
diff changeset
39 }
1d4f2b72fb31 delete KeyThread
kazz <kazz@cr.ie.u-ryukyu.ac.jp>
parents: 68
diff changeset
40
1d4f2b72fb31 delete KeyThread
kazz <kazz@cr.ie.u-ryukyu.ac.jp>
parents: 68
diff changeset
41 };
1d4f2b72fb31 delete KeyThread
kazz <kazz@cr.ie.u-ryukyu.ac.jp>
parents: 68
diff changeset
42
6
c78a1cc2cd8f implements Reply
one
parents: 5
diff changeset
43 public LocalDataSegmentManager() {
69
1d4f2b72fb31 delete KeyThread
kazz <kazz@cr.ie.u-ryukyu.ac.jp>
parents: 68
diff changeset
44 new Thread(replyThread, "LocalDataSegmentManager-replyCommand").start();
1d4f2b72fb31 delete KeyThread
kazz <kazz@cr.ie.u-ryukyu.ac.jp>
parents: 68
diff changeset
45 new Thread(keyCommandThread, "LocalDataSegmentManager-runKeyCommand").start();
1d4f2b72fb31 delete KeyThread
kazz <kazz@cr.ie.u-ryukyu.ac.jp>
parents: 68
diff changeset
46 }
1d4f2b72fb31 delete KeyThread
kazz <kazz@cr.ie.u-ryukyu.ac.jp>
parents: 68
diff changeset
47
73
4bfd81352cfa change to concurrent data segment
kazz <kazz@cr.ie.u-ryukyu.ac.jp>
parents: 71
diff changeset
48 public void submitCommand(final DataSegmentKey key, final Command cmd) {
4bfd81352cfa change to concurrent data segment
kazz <kazz@cr.ie.u-ryukyu.ac.jp>
parents: 71
diff changeset
49 Runnable runCommand = new Runnable() {
4bfd81352cfa change to concurrent data segment
kazz <kazz@cr.ie.u-ryukyu.ac.jp>
parents: 71
diff changeset
50
4bfd81352cfa change to concurrent data segment
kazz <kazz@cr.ie.u-ryukyu.ac.jp>
parents: 71
diff changeset
51 @Override
4bfd81352cfa change to concurrent data segment
kazz <kazz@cr.ie.u-ryukyu.ac.jp>
parents: 71
diff changeset
52 public void run() {
4bfd81352cfa change to concurrent data segment
kazz <kazz@cr.ie.u-ryukyu.ac.jp>
parents: 71
diff changeset
53 key.runCommand(cmd);
4bfd81352cfa change to concurrent data segment
kazz <kazz@cr.ie.u-ryukyu.ac.jp>
parents: 71
diff changeset
54 }
4bfd81352cfa change to concurrent data segment
kazz <kazz@cr.ie.u-ryukyu.ac.jp>
parents: 71
diff changeset
55
4bfd81352cfa change to concurrent data segment
kazz <kazz@cr.ie.u-ryukyu.ac.jp>
parents: 71
diff changeset
56 };
4bfd81352cfa change to concurrent data segment
kazz <kazz@cr.ie.u-ryukyu.ac.jp>
parents: 71
diff changeset
57 dataSegmentExecutor.execute(runCommand);
6
c78a1cc2cd8f implements Reply
one
parents: 5
diff changeset
58 }
3
91057e15065f add DataSegment API and CodeSegment
one
parents: 2
diff changeset
59
13
30f97d776a3e implements Alice daemon
one
parents: 12
diff changeset
60 public DataSegmentKey getDataSegmentKey(String key) {
64
7aaadd08288c add getLocal method to DataSegment
kazz
parents: 63
diff changeset
61 DataSegmentKey dsKey = dataSegments.get(key);
63
498d1d2524d3 change getDataSegmentKey
kazz <kazz@cr.ie.u-ryukyu.ac.jp>
parents: 58
diff changeset
62 if (dsKey != null)
498d1d2524d3 change getDataSegmentKey
kazz <kazz@cr.ie.u-ryukyu.ac.jp>
parents: 58
diff changeset
63 return dsKey;
498d1d2524d3 change getDataSegmentKey
kazz <kazz@cr.ie.u-ryukyu.ac.jp>
parents: 58
diff changeset
64 if (key == null)
16
433e601a8e28 network bug fix
kazz <kazz@cr.ie.u-ryukyu.ac.jp>
parents: 14
diff changeset
65 return null;
69
1d4f2b72fb31 delete KeyThread
kazz <kazz@cr.ie.u-ryukyu.ac.jp>
parents: 68
diff changeset
66 DataSegmentKey newDataSegmentKey = new DataSegmentKey();
3
91057e15065f add DataSegment API and CodeSegment
one
parents: 2
diff changeset
67 DataSegmentKey dataSegmentKey = dataSegments.putIfAbsent(key, newDataSegmentKey);
8
78b415d019de Local DS and CS work! maybe...
one
parents: 7
diff changeset
68 if (dataSegmentKey == null) {
78b415d019de Local DS and CS work! maybe...
one
parents: 7
diff changeset
69 dataSegmentKey = newDataSegmentKey;
3
91057e15065f add DataSegment API and CodeSegment
one
parents: 2
diff changeset
70 }
91057e15065f add DataSegment API and CodeSegment
one
parents: 2
diff changeset
71 return dataSegmentKey;
91057e15065f add DataSegment API and CodeSegment
one
parents: 2
diff changeset
72 }
91057e15065f add DataSegment API and CodeSegment
one
parents: 2
diff changeset
73
2
f71eabb1df2a create outline of DataSegment model
one
parents:
diff changeset
74 @Override
40
20616fe4d28a add log viewer
kazz <kazz@cr.ie.u-ryukyu.ac.jp>
parents: 39
diff changeset
75 public void put(String key, Value val, CodeSegment cs) {
3
91057e15065f add DataSegment API and CodeSegment
one
parents: 2
diff changeset
76 DataSegmentKey dataSegmentKey = getDataSegmentKey(key);
40
20616fe4d28a add log viewer
kazz <kazz@cr.ie.u-ryukyu.ac.jp>
parents: 39
diff changeset
77 Command cmd = new Command(CommandType.PUT, null, key, val, 0, 0, replyQueue, cs, reverseKey);
73
4bfd81352cfa change to concurrent data segment
kazz <kazz@cr.ie.u-ryukyu.ac.jp>
parents: 71
diff changeset
78 submitCommand(dataSegmentKey, cmd);
71
a3a2605e16a2 change checking debug mode of logger
kazz <kazz@cr.ie.u-ryukyu.ac.jp>
parents: 69
diff changeset
79 if (logger.isDebugEnabled())
68
d4c7f7b1096b remove copy at OutboundTcpConnection
kazz <kazz@cr.ie.u-ryukyu.ac.jp>
parents: 67
diff changeset
80 logger.debug(cmd.getCommandString());
2
f71eabb1df2a create outline of DataSegment model
one
parents:
diff changeset
81 }
57
7fa9ddb31f64 add comment
one
parents: 56
diff changeset
82
7fa9ddb31f64 add comment
one
parents: 56
diff changeset
83 /**
7fa9ddb31f64 add comment
one
parents: 56
diff changeset
84 * Enqueue update command to the queue of each DataSegment key
7fa9ddb31f64 add comment
one
parents: 56
diff changeset
85 */
2
f71eabb1df2a create outline of DataSegment model
one
parents:
diff changeset
86 @Override
40
20616fe4d28a add log viewer
kazz <kazz@cr.ie.u-ryukyu.ac.jp>
parents: 39
diff changeset
87 public void update(String key, Value val, CodeSegment cs) {
5
80375ae09a1f add update api
one
parents: 3
diff changeset
88 DataSegmentKey dataSegmentKey = getDataSegmentKey(key);
40
20616fe4d28a add log viewer
kazz <kazz@cr.ie.u-ryukyu.ac.jp>
parents: 39
diff changeset
89 Command cmd = new Command(CommandType.UPDATE, null, key, val, 0, 0, replyQueue, cs, reverseKey);
73
4bfd81352cfa change to concurrent data segment
kazz <kazz@cr.ie.u-ryukyu.ac.jp>
parents: 71
diff changeset
90 submitCommand(dataSegmentKey, cmd);
71
a3a2605e16a2 change checking debug mode of logger
kazz <kazz@cr.ie.u-ryukyu.ac.jp>
parents: 69
diff changeset
91 if (logger.isDebugEnabled())
68
d4c7f7b1096b remove copy at OutboundTcpConnection
kazz <kazz@cr.ie.u-ryukyu.ac.jp>
parents: 67
diff changeset
92 logger.debug(cmd.getCommandString());
5
80375ae09a1f add update api
one
parents: 3
diff changeset
93 }
80375ae09a1f add update api
one
parents: 3
diff changeset
94
80375ae09a1f add update api
one
parents: 3
diff changeset
95 @Override
33
20c67f673224 change name of DataSegmentReceiver
kazz <kazz@cr.ie.u-ryukyu.ac.jp>
parents: 30
diff changeset
96 public void take(Receiver receiver, String key, int index, CodeSegment cs) {
3
91057e15065f add DataSegment API and CodeSegment
one
parents: 2
diff changeset
97 DataSegmentKey dataSegmentKey = getDataSegmentKey(key);
91057e15065f add DataSegment API and CodeSegment
one
parents: 2
diff changeset
98 int seq = this.seq.getAndIncrement();
40
20616fe4d28a add log viewer
kazz <kazz@cr.ie.u-ryukyu.ac.jp>
parents: 39
diff changeset
99 Command cmd = new Command(CommandType.TAKE, receiver, key, null, index, seq, replyQueue, cs, null);
6
c78a1cc2cd8f implements Reply
one
parents: 5
diff changeset
100 seqHash.put(seq, cmd);
73
4bfd81352cfa change to concurrent data segment
kazz <kazz@cr.ie.u-ryukyu.ac.jp>
parents: 71
diff changeset
101 submitCommand(dataSegmentKey, cmd);
71
a3a2605e16a2 change checking debug mode of logger
kazz <kazz@cr.ie.u-ryukyu.ac.jp>
parents: 69
diff changeset
102 if (logger.isDebugEnabled())
68
d4c7f7b1096b remove copy at OutboundTcpConnection
kazz <kazz@cr.ie.u-ryukyu.ac.jp>
parents: 67
diff changeset
103 logger.debug(cmd.getCommandString());
2
f71eabb1df2a create outline of DataSegment model
one
parents:
diff changeset
104 }
f71eabb1df2a create outline of DataSegment model
one
parents:
diff changeset
105
f71eabb1df2a create outline of DataSegment model
one
parents:
diff changeset
106 @Override
33
20c67f673224 change name of DataSegmentReceiver
kazz <kazz@cr.ie.u-ryukyu.ac.jp>
parents: 30
diff changeset
107 public void peek(Receiver receiver, String key, int index, CodeSegment cs) {
3
91057e15065f add DataSegment API and CodeSegment
one
parents: 2
diff changeset
108 DataSegmentKey dataSegmentKey = getDataSegmentKey(key);
91057e15065f add DataSegment API and CodeSegment
one
parents: 2
diff changeset
109 int seq = this.seq.getAndIncrement();
40
20616fe4d28a add log viewer
kazz <kazz@cr.ie.u-ryukyu.ac.jp>
parents: 39
diff changeset
110 Command cmd = new Command(CommandType.PEEK, receiver, key, null, index, seq, replyQueue, cs, null);
58
ebdcab7b9b04 add comment
one
parents: 57
diff changeset
111 seqHash.put(seq, cmd); // waiting for PUT or UPDATE at unique sequence number
73
4bfd81352cfa change to concurrent data segment
kazz <kazz@cr.ie.u-ryukyu.ac.jp>
parents: 71
diff changeset
112 submitCommand(dataSegmentKey, cmd);
71
a3a2605e16a2 change checking debug mode of logger
kazz <kazz@cr.ie.u-ryukyu.ac.jp>
parents: 69
diff changeset
113 if (logger.isDebugEnabled())
68
d4c7f7b1096b remove copy at OutboundTcpConnection
kazz <kazz@cr.ie.u-ryukyu.ac.jp>
parents: 67
diff changeset
114 logger.debug(cmd.getCommandString());
2
f71eabb1df2a create outline of DataSegment model
one
parents:
diff changeset
115 }
f71eabb1df2a create outline of DataSegment model
one
parents:
diff changeset
116
f71eabb1df2a create outline of DataSegment model
one
parents:
diff changeset
117 @Override
3
91057e15065f add DataSegment API and CodeSegment
one
parents: 2
diff changeset
118 public void remove(String key) {
91057e15065f add DataSegment API and CodeSegment
one
parents: 2
diff changeset
119 DataSegmentKey dataSegmentKey = getDataSegmentKey(key);
40
20616fe4d28a add log viewer
kazz <kazz@cr.ie.u-ryukyu.ac.jp>
parents: 39
diff changeset
120 Command cmd = new Command(CommandType.REMOVE, null, key, null, 0, 0, replyQueue, null, null);
73
4bfd81352cfa change to concurrent data segment
kazz <kazz@cr.ie.u-ryukyu.ac.jp>
parents: 71
diff changeset
121 submitCommand(dataSegmentKey, cmd);
71
a3a2605e16a2 change checking debug mode of logger
kazz <kazz@cr.ie.u-ryukyu.ac.jp>
parents: 69
diff changeset
122 if (logger.isDebugEnabled())
68
d4c7f7b1096b remove copy at OutboundTcpConnection
kazz <kazz@cr.ie.u-ryukyu.ac.jp>
parents: 67
diff changeset
123 logger.debug(cmd.getCommandString());
2
f71eabb1df2a create outline of DataSegment model
one
parents:
diff changeset
124 }
30
b5a21baf0b07 implements RingTopology
kazz <kazz@cr.ie.u-ryukyu.ac.jp>
parents: 28
diff changeset
125
b5a21baf0b07 implements RingTopology
kazz <kazz@cr.ie.u-ryukyu.ac.jp>
parents: 28
diff changeset
126 @Override public void finish() {
b5a21baf0b07 implements RingTopology
kazz <kazz@cr.ie.u-ryukyu.ac.jp>
parents: 28
diff changeset
127 System.exit(0);
b5a21baf0b07 implements RingTopology
kazz <kazz@cr.ie.u-ryukyu.ac.jp>
parents: 28
diff changeset
128 }
41
f9334781344a add close api
kazz <kazz@cr.ie.u-ryukyu.ac.jp>
parents: 40
diff changeset
129
f9334781344a add close api
kazz <kazz@cr.ie.u-ryukyu.ac.jp>
parents: 40
diff changeset
130 @Override
f9334781344a add close api
kazz <kazz@cr.ie.u-ryukyu.ac.jp>
parents: 40
diff changeset
131 public void close() {
f9334781344a add close api
kazz <kazz@cr.ie.u-ryukyu.ac.jp>
parents: 40
diff changeset
132
f9334781344a add close api
kazz <kazz@cr.ie.u-ryukyu.ac.jp>
parents: 40
diff changeset
133 }
30
b5a21baf0b07 implements RingTopology
kazz <kazz@cr.ie.u-ryukyu.ac.jp>
parents: 28
diff changeset
134
2
f71eabb1df2a create outline of DataSegment model
one
parents:
diff changeset
135 }