1 | |
|
2 | |
|
3 | |
|
4 | |
|
5 | |
|
6 | |
|
7 | |
|
8 | |
|
9 | |
|
10 | |
|
11 | |
|
12 | |
|
13 | |
|
14 | |
|
15 | |
|
16 | |
|
17 | |
|
18 | |
|
19 | |
package org.apache.giraph.ooc; |
20 | |
|
21 | |
import org.apache.giraph.conf.ImmutableClassesGiraphConfiguration; |
22 | |
import org.apache.giraph.conf.IntConfOption; |
23 | |
import org.apache.giraph.ooc.command.IOCommand; |
24 | |
import org.apache.giraph.ooc.command.LoadPartitionIOCommand; |
25 | |
import org.apache.giraph.ooc.command.StoreDataBufferIOCommand; |
26 | |
import org.apache.giraph.ooc.command.StoreIncomingMessageIOCommand; |
27 | |
import org.apache.giraph.ooc.command.StorePartitionIOCommand; |
28 | |
import org.apache.giraph.ooc.command.WaitIOCommand; |
29 | |
import org.apache.giraph.ooc.policy.OutOfCoreOracle; |
30 | |
import org.apache.log4j.Logger; |
31 | |
|
32 | |
import java.util.ArrayList; |
33 | |
import java.util.Arrays; |
34 | |
import java.util.List; |
35 | |
import java.util.Queue; |
36 | |
import java.util.concurrent.ConcurrentLinkedQueue; |
37 | |
|
38 | |
import static com.google.common.base.Preconditions.checkNotNull; |
39 | |
|
40 | |
|
41 | |
|
42 | |
|
43 | |
public class OutOfCoreIOScheduler { |
44 | |
|
45 | |
|
46 | |
|
47 | |
|
48 | |
|
49 | 0 | public static final IntConfOption OOC_WAIT_INTERVAL = |
50 | |
new IntConfOption("giraph.oocWaitInterval", 1000, |
51 | |
"Duration (in milliseconds) which IO threads in out-of-core " + |
52 | |
"mechanism would wait until a command becomes available"); |
53 | |
|
54 | 0 | private static final Logger LOG = |
55 | 0 | Logger.getLogger(OutOfCoreIOScheduler.class); |
56 | |
|
57 | |
private final OutOfCoreEngine oocEngine; |
58 | |
|
59 | |
private final int waitInterval; |
60 | |
|
61 | |
|
62 | |
|
63 | |
|
64 | |
private final List<Queue<IOCommand>> threadLoadCommandQueue; |
65 | |
|
66 | |
private volatile boolean shouldTerminate; |
67 | |
|
68 | |
|
69 | |
|
70 | |
|
71 | |
|
72 | |
|
73 | |
|
74 | |
|
75 | |
OutOfCoreIOScheduler(final ImmutableClassesGiraphConfiguration conf, |
76 | 0 | OutOfCoreEngine oocEngine, int numDisks) { |
77 | 0 | this.oocEngine = oocEngine; |
78 | 0 | this.waitInterval = OOC_WAIT_INTERVAL.get(conf); |
79 | 0 | threadLoadCommandQueue = new ArrayList<>(numDisks); |
80 | 0 | for (int i = 0; i < numDisks; ++i) { |
81 | 0 | threadLoadCommandQueue.add( |
82 | |
new ConcurrentLinkedQueue<IOCommand>()); |
83 | |
} |
84 | 0 | shouldTerminate = false; |
85 | 0 | } |
86 | |
|
87 | |
|
88 | |
|
89 | |
|
90 | |
|
91 | |
|
92 | |
|
93 | |
public IOCommand getNextIOCommand(int threadId) { |
94 | 0 | if (shouldTerminate) { |
95 | 0 | return null; |
96 | |
} |
97 | 0 | IOCommand command = null; |
98 | |
do { |
99 | 0 | if (command != null && LOG.isInfoEnabled()) { |
100 | 0 | LOG.info("getNextIOCommand: command " + command + " was proposed to " + |
101 | |
"the oracle, but got denied. Generating another command!"); |
102 | |
} |
103 | 0 | OutOfCoreOracle.IOAction[] actions = |
104 | 0 | oocEngine.getOracle().getNextIOActions(); |
105 | 0 | if (LOG.isDebugEnabled()) { |
106 | 0 | LOG.debug("getNextIOCommand: actions are " + Arrays.toString(actions)); |
107 | |
} |
108 | |
|
109 | 0 | if (!threadLoadCommandQueue.get(threadId).isEmpty()) { |
110 | |
|
111 | 0 | boolean canLoad = false; |
112 | 0 | for (OutOfCoreOracle.IOAction action : actions) { |
113 | 0 | if (action == OutOfCoreOracle.IOAction.LOAD_PARTITION || |
114 | |
action == OutOfCoreOracle.IOAction.LOAD_UNPROCESSED_PARTITION || |
115 | |
action == OutOfCoreOracle.IOAction.LOAD_TO_SWAP_PARTITION || |
116 | |
action == OutOfCoreOracle.IOAction.URGENT_LOAD_PARTITION) { |
117 | 0 | canLoad = true; |
118 | 0 | break; |
119 | |
} |
120 | |
} |
121 | 0 | if (canLoad) { |
122 | 0 | command = threadLoadCommandQueue.get(threadId).poll(); |
123 | 0 | checkNotNull(command); |
124 | 0 | if (oocEngine.getOracle().approve(command)) { |
125 | 0 | return command; |
126 | |
} else { |
127 | |
|
128 | |
|
129 | 0 | threadLoadCommandQueue.get(threadId).offer(command); |
130 | |
} |
131 | |
} |
132 | |
} |
133 | 0 | command = null; |
134 | 0 | for (OutOfCoreOracle.IOAction action : actions) { |
135 | |
Integer partitionId; |
136 | 0 | switch (action) { |
137 | |
case STORE_MESSAGES_AND_BUFFERS: |
138 | 0 | partitionId = oocEngine.getMetaPartitionManager() |
139 | 0 | .getOffloadPartitionBufferId(threadId); |
140 | 0 | if (partitionId != null) { |
141 | 0 | command = new StoreDataBufferIOCommand(oocEngine, partitionId, |
142 | |
StoreDataBufferIOCommand.DataBufferType.PARTITION); |
143 | |
} else { |
144 | 0 | partitionId = oocEngine.getMetaPartitionManager() |
145 | 0 | .getOffloadMessageBufferId(threadId); |
146 | 0 | if (partitionId != null) { |
147 | 0 | command = new StoreDataBufferIOCommand(oocEngine, partitionId, |
148 | |
StoreDataBufferIOCommand.DataBufferType.MESSAGE); |
149 | |
} else { |
150 | 0 | partitionId = oocEngine.getMetaPartitionManager() |
151 | 0 | .getOffloadMessageId(threadId); |
152 | 0 | if (partitionId != null) { |
153 | 0 | command = new StoreIncomingMessageIOCommand(oocEngine, |
154 | 0 | partitionId); |
155 | |
} |
156 | |
} |
157 | |
} |
158 | |
break; |
159 | |
case STORE_PROCESSED_PARTITION: |
160 | 0 | partitionId = oocEngine.getMetaPartitionManager() |
161 | 0 | .getOffloadPartitionId(threadId); |
162 | 0 | if (partitionId != null && |
163 | 0 | oocEngine.getMetaPartitionManager() |
164 | 0 | .isPartitionProcessed(partitionId)) { |
165 | 0 | command = new StorePartitionIOCommand(oocEngine, partitionId); |
166 | |
} |
167 | |
break; |
168 | |
case STORE_PARTITION: |
169 | 0 | partitionId = oocEngine.getMetaPartitionManager() |
170 | 0 | .getOffloadPartitionId(threadId); |
171 | 0 | if (partitionId != null) { |
172 | 0 | command = new StorePartitionIOCommand(oocEngine, partitionId); |
173 | |
} |
174 | |
break; |
175 | |
case LOAD_UNPROCESSED_PARTITION: |
176 | 0 | partitionId = oocEngine.getMetaPartitionManager() |
177 | 0 | .getLoadPartitionId(threadId); |
178 | 0 | if (partitionId != null && |
179 | 0 | !oocEngine.getMetaPartitionManager() |
180 | 0 | .isPartitionProcessed(partitionId)) { |
181 | 0 | command = new LoadPartitionIOCommand(oocEngine, partitionId, |
182 | 0 | oocEngine.getSuperstep()); |
183 | |
} |
184 | |
break; |
185 | |
case LOAD_TO_SWAP_PARTITION: |
186 | 0 | partitionId = oocEngine.getMetaPartitionManager() |
187 | 0 | .getLoadPartitionId(threadId); |
188 | 0 | if (partitionId != null && |
189 | 0 | !oocEngine.getMetaPartitionManager() |
190 | 0 | .isPartitionProcessed(partitionId) && |
191 | 0 | oocEngine.getMetaPartitionManager().hasProcessedOnMemory()) { |
192 | 0 | command = new LoadPartitionIOCommand(oocEngine, partitionId, |
193 | 0 | oocEngine.getSuperstep()); |
194 | |
} |
195 | |
break; |
196 | |
case LOAD_PARTITION: |
197 | 0 | partitionId = oocEngine.getMetaPartitionManager() |
198 | 0 | .getLoadPartitionId(threadId); |
199 | 0 | if (partitionId != null) { |
200 | 0 | if (oocEngine.getMetaPartitionManager() |
201 | 0 | .isPartitionProcessed(partitionId)) { |
202 | 0 | command = new LoadPartitionIOCommand(oocEngine, partitionId, |
203 | 0 | oocEngine.getSuperstep() + 1); |
204 | |
} else { |
205 | 0 | command = new LoadPartitionIOCommand(oocEngine, partitionId, |
206 | 0 | oocEngine.getSuperstep()); |
207 | |
} |
208 | |
} |
209 | |
break; |
210 | |
case URGENT_LOAD_PARTITION: |
211 | |
|
212 | 0 | break; |
213 | |
default: |
214 | 0 | throw new IllegalStateException("getNextIOCommand: the IO action " + |
215 | |
"is not defined!"); |
216 | |
} |
217 | 0 | if (command != null) { |
218 | 0 | break; |
219 | |
} |
220 | |
} |
221 | 0 | if (command == null) { |
222 | 0 | command = new WaitIOCommand(oocEngine, waitInterval); |
223 | |
} |
224 | 0 | } while (!oocEngine.getOracle().approve(command)); |
225 | 0 | return command; |
226 | |
} |
227 | |
|
228 | |
|
229 | |
|
230 | |
|
231 | |
|
232 | |
|
233 | |
public void ioCommandCompleted(IOCommand command) { |
234 | 0 | oocEngine.ioCommandCompleted(command); |
235 | 0 | } |
236 | |
|
237 | |
|
238 | |
|
239 | |
|
240 | |
|
241 | |
|
242 | |
public void addIOCommand(IOCommand ioCommand) { |
243 | 0 | if (ioCommand instanceof LoadPartitionIOCommand) { |
244 | 0 | int ownerThread = oocEngine.getMetaPartitionManager() |
245 | 0 | .getOwnerThreadId(ioCommand.getPartitionId()); |
246 | 0 | threadLoadCommandQueue.get(ownerThread).offer(ioCommand); |
247 | 0 | } else { |
248 | 0 | throw new IllegalStateException("addIOCommand: IO command type is not " + |
249 | |
"supported for addition"); |
250 | |
} |
251 | 0 | } |
252 | |
|
253 | |
|
254 | |
|
255 | |
|
256 | |
public void shutdown() { |
257 | 0 | shouldTerminate = true; |
258 | 0 | if (LOG.isInfoEnabled()) { |
259 | 0 | LOG.info("shutdown: OutOfCoreIOScheduler shutting down!"); |
260 | |
} |
261 | 0 | } |
262 | |
} |