1 | package test.rewolucja.reservation; |
---|
2 | |
---|
3 | import eduni.simjava.Sim_event; |
---|
4 | import eduni.simjava.Sim_predicate; |
---|
5 | import eduni.simjava.Sim_type_p; |
---|
6 | import gridsim.GridSim; |
---|
7 | import gridsim.GridSimTags; |
---|
8 | import gridsim.IO_data; |
---|
9 | import gridsim.filter.FilterResult; |
---|
10 | import gridsim.gssim.GssimTags; |
---|
11 | import gridsim.gssim.Transaction_IO_data; |
---|
12 | import gridsim.gssim.filter.TransactionResult; |
---|
13 | |
---|
14 | import java.util.ArrayList; |
---|
15 | import java.util.List; |
---|
16 | import java.util.Properties; |
---|
17 | |
---|
18 | import org.apache.commons.logging.Log; |
---|
19 | import org.apache.commons.logging.LogFactory; |
---|
20 | import org.joda.time.DateTime; |
---|
21 | |
---|
22 | import schedframe.exceptions.NotAuthorizedException; |
---|
23 | import schedframe.exceptions.PermanentException; |
---|
24 | import schedframe.exceptions.ReservationException; |
---|
25 | import schedframe.resources.ResourceProvider; |
---|
26 | import schedframe.scheduling.AbstractResourceRequirements; |
---|
27 | import schedframe.scheduling.AbstractTimeRequirements; |
---|
28 | import schedframe.scheduling.Offer; |
---|
29 | import schedframe.scheduling.ResourceUsage; |
---|
30 | import schedframe.scheduling.SecurityContextInterface; |
---|
31 | import schedframe.scheduling.TimeResourceAllocation; |
---|
32 | import schedframe.scheduling.plugin.grid.ModuleType; |
---|
33 | import test.rewolucja.scheduling.implementation.GridBroker; |
---|
34 | |
---|
35 | public class GridReservationManagerNew implements ReservationManagerNew { |
---|
36 | |
---|
37 | private Log log = LogFactory.getLog(GridReservationManagerNew.class); |
---|
38 | |
---|
39 | protected GridBroker broker; |
---|
40 | int transactionId = 0; |
---|
41 | |
---|
42 | public GridReservationManagerNew(GridBroker gridBroker) { |
---|
43 | this.broker = gridBroker; |
---|
44 | } |
---|
45 | |
---|
46 | public void cancelReservation(ReservationNew reservation, |
---|
47 | SecurityContextInterface securityContext) |
---|
48 | throws ReservationException, NotAuthorizedException, PermanentException { |
---|
49 | throw new ReservationException("Not implemented."); |
---|
50 | } |
---|
51 | |
---|
52 | public int checkStatus(ReservationNew reservation, |
---|
53 | SecurityContextInterface securityContext) throws ReservationException, NotAuthorizedException, PermanentException { |
---|
54 | throw new ReservationException("Not implemented."); |
---|
55 | } |
---|
56 | |
---|
57 | public ReservationNew commitReservation(ReservationNew reservation, |
---|
58 | SecurityContextInterface securityContext) |
---|
59 | throws ReservationException, NotAuthorizedException, PermanentException { |
---|
60 | |
---|
61 | String resourceName = reservation.getResourceName(); |
---|
62 | if (resourceName == null) |
---|
63 | throw new ReservationException("Resource provider is not defined."); |
---|
64 | |
---|
65 | if (resourceName == null || resourceName.length() == 0) |
---|
66 | throw new ReservationException("Provider id is not defined."); |
---|
67 | |
---|
68 | int destination = GridSim.getEntityId(resourceName); |
---|
69 | if (destination == -1) |
---|
70 | throw new ReservationException("Provider " + resourceName |
---|
71 | + " does not exist."); |
---|
72 | |
---|
73 | int transactionId = this.incrementID(); |
---|
74 | |
---|
75 | broker.getLogicalResource().send(destination, GridSimTags.SCHEDULE_NOW, |
---|
76 | GridSimTags.SEND_AR_COMMIT_ONLY, |
---|
77 | new Transaction_IO_data(transactionId, reservation, 10, destination)); |
---|
78 | |
---|
79 | // waiting for a response from the GridResource with an unique tag |
---|
80 | // of transaction id |
---|
81 | FilterResult tag = new TransactionResult(transactionId, |
---|
82 | GridSimTags.RETURN_AR_COMMIT); |
---|
83 | |
---|
84 | // only look for this type of ack for same Gridlet ID |
---|
85 | Sim_event ev = new Sim_event(); |
---|
86 | broker.getLogicalResource().sim_get_next(tag, ev); |
---|
87 | |
---|
88 | // get the result back |
---|
89 | IO_data ioData = (IO_data) ev.get_data(); |
---|
90 | Object obj = ioData.getData(); |
---|
91 | |
---|
92 | ReservationNew result = null; |
---|
93 | if (obj instanceof ReservationNew) { |
---|
94 | result = (ReservationNew) obj; |
---|
95 | log.warn("GridReservationManager.commitReservation() - Add reservation status check" + result.jobId + ";"+new DateTime(result.getStartMillis())+";"+new DateTime(result.getEndMillis())); |
---|
96 | |
---|
97 | } else if (obj instanceof ReservationException) { |
---|
98 | throw new ReservationException((ReservationException) obj); |
---|
99 | |
---|
100 | } else { |
---|
101 | throw new ReservationException(obj.getClass().getName() |
---|
102 | + " is not valid Reservation type."); |
---|
103 | |
---|
104 | } |
---|
105 | |
---|
106 | return result; |
---|
107 | } |
---|
108 | |
---|
109 | public ReservationNew commitReservation(ReservationNew reservation, |
---|
110 | TimeResourceAllocation resourceUsage, |
---|
111 | SecurityContextInterface securityContext) throws ReservationException, NotAuthorizedException, PermanentException { |
---|
112 | |
---|
113 | String resourceName = reservation.getResourceName(); |
---|
114 | if (resourceName == null) |
---|
115 | throw new ReservationException("Resource provider is not defined."); |
---|
116 | |
---|
117 | if (resourceName == null || resourceName.length() == 0) |
---|
118 | throw new ReservationException("Provider id is not defined."); |
---|
119 | |
---|
120 | int destination = GridSim.getEntityId(resourceName); |
---|
121 | if (destination == -1) |
---|
122 | throw new ReservationException("Provider " + resourceName |
---|
123 | + " does not exist."); |
---|
124 | |
---|
125 | int transactionId = this.incrementID(); |
---|
126 | |
---|
127 | Object data[] = {reservation, resourceUsage }; |
---|
128 | broker.getLogicalResource().send(destination, GridSimTags.SCHEDULE_NOW, |
---|
129 | GridSimTags.SEND_AR_COMMIT_ONLY, |
---|
130 | new Transaction_IO_data(transactionId, data, 10, destination)); |
---|
131 | |
---|
132 | // waiting for a response from the GridResource with an unique tag |
---|
133 | // of transaction id |
---|
134 | FilterResult tag = new TransactionResult(transactionId, |
---|
135 | GridSimTags.RETURN_AR_COMMIT); |
---|
136 | |
---|
137 | // only look for this type of ack for same Gridlet ID |
---|
138 | Sim_event ev = new Sim_event(); |
---|
139 | broker.getLogicalResource().sim_get_next(tag, ev); |
---|
140 | |
---|
141 | ReservationNew result = null; |
---|
142 | // get the result back |
---|
143 | Object obj = ev.get_data(); |
---|
144 | |
---|
145 | if (obj instanceof ReservationNew) { |
---|
146 | result = (ReservationNew) obj; |
---|
147 | log.warn("GridReservationManager.commitReservation() - Add reservation status check"); |
---|
148 | |
---|
149 | } else if (obj instanceof ReservationException) { |
---|
150 | throw new ReservationException((ReservationException) obj); |
---|
151 | |
---|
152 | } else { |
---|
153 | throw new ReservationException(obj.getClass().getName() |
---|
154 | + " is not valid Reservation type."); |
---|
155 | |
---|
156 | } |
---|
157 | |
---|
158 | return result; |
---|
159 | |
---|
160 | } |
---|
161 | |
---|
162 | public List<ReservationNew> createReservation( |
---|
163 | AbstractTimeRequirements<?> timeRequirements, |
---|
164 | AbstractResourceRequirements<?> resourceRequirements, |
---|
165 | SecurityContextInterface securityContext) |
---|
166 | throws ReservationException, NotAuthorizedException, PermanentException { |
---|
167 | throw new ReservationException("Not implemented."); |
---|
168 | } |
---|
169 | |
---|
170 | public ReservationNew createReservation(ResourceProvider provider, |
---|
171 | AbstractTimeRequirements<?> timeRequirements, |
---|
172 | AbstractResourceRequirements<?> resourceRequirements, |
---|
173 | List<String> hostCandidates, |
---|
174 | SecurityContextInterface securityContext) |
---|
175 | throws ReservationException, NotAuthorizedException, PermanentException { |
---|
176 | throw new ReservationException("Not implemented."); |
---|
177 | } |
---|
178 | |
---|
179 | public List<ReservationNew> createReservation(ResourceUsage resourceUsage, |
---|
180 | SecurityContextInterface securityContext) |
---|
181 | throws ReservationException, NotAuthorizedException, PermanentException { |
---|
182 | |
---|
183 | ResourceProvider provider = resourceUsage.getProvider(); |
---|
184 | if (provider == null) |
---|
185 | throw new ReservationException("Resource provider is not defined."); |
---|
186 | |
---|
187 | String providerId = provider.getProviderId(); |
---|
188 | if (providerId == null || providerId.length() == 0) |
---|
189 | throw new ReservationException("Provider id is not defined."); |
---|
190 | |
---|
191 | int destination = GridSim.getEntityId(providerId); |
---|
192 | if (destination == -1) |
---|
193 | throw new ReservationException("Provider " + providerId |
---|
194 | + " does not exist."); |
---|
195 | |
---|
196 | int transactionId = this.incrementID(); |
---|
197 | broker.getLogicalResource().send(destination, GridSimTags.SCHEDULE_NOW, |
---|
198 | GridSimTags.SEND_AR_CREATE, |
---|
199 | new Transaction_IO_data(transactionId, resourceUsage, 10, destination)); |
---|
200 | |
---|
201 | // waiting for a response from the GridResource with an unique tag |
---|
202 | // of transaction id |
---|
203 | FilterResult tag = new TransactionResult(transactionId, |
---|
204 | GridSimTags.RETURN_AR_CREATE); |
---|
205 | |
---|
206 | // only look for this type of ack for same Gridlet ID |
---|
207 | Sim_event ev = new Sim_event(); |
---|
208 | broker.getLogicalResource().sim_get_next(tag, ev); |
---|
209 | |
---|
210 | List<ReservationNew> result = null; |
---|
211 | // get the result back |
---|
212 | IO_data ioData = (IO_data) ev.get_data(); |
---|
213 | Object obj = ioData.getData(); |
---|
214 | |
---|
215 | if (obj instanceof List) { |
---|
216 | result = (List<ReservationNew>) obj; |
---|
217 | log.warn("GridReservationManager.createReservation() - Add reservation status check"); |
---|
218 | |
---|
219 | } else if (obj instanceof ReservationException) { |
---|
220 | throw new ReservationException((ReservationException) obj); |
---|
221 | |
---|
222 | } else { |
---|
223 | throw new ReservationException(obj.getClass().getName() |
---|
224 | + " is not valid Reservation type."); |
---|
225 | |
---|
226 | } |
---|
227 | |
---|
228 | return result; |
---|
229 | } |
---|
230 | |
---|
231 | public List<Offer> getOffer(AbstractTimeRequirements<?> timeRequirements, |
---|
232 | AbstractResourceRequirements<?> resourceRequirements, |
---|
233 | SecurityContextInterface securityContext) |
---|
234 | throws ReservationException, NotAuthorizedException, PermanentException { |
---|
235 | |
---|
236 | List<?> gridsimProviders = this.broker.getMyGridResources(); |
---|
237 | |
---|
238 | int gridsimProvidersCount = gridsimProviders.size(); |
---|
239 | for (int i = 0; i < gridsimProvidersCount; i++) { |
---|
240 | int providerID = (Integer)gridsimProviders.get(i); |
---|
241 | Object[] data = { timeRequirements, resourceRequirements }; |
---|
242 | Transaction_IO_data ioData = new Transaction_IO_data(this.incrementID(), data, 8, providerID); |
---|
243 | broker.getLogicalResource().send(providerID, GridSimTags.SCHEDULE_NOW, |
---|
244 | GssimTags.GET_OFFERS, ioData); |
---|
245 | } |
---|
246 | |
---|
247 | List<Offer> result = new ArrayList<Offer>(gridsimProvidersCount); |
---|
248 | Sim_event e = new Sim_event(); |
---|
249 | Sim_predicate onlyGRMSGetOffers = new Sim_type_p( |
---|
250 | GssimTags.RETURN_GET_OFFERS); |
---|
251 | |
---|
252 | // collect answers for above send |
---|
253 | for (int i = 0; i < gridsimProvidersCount; i++) { |
---|
254 | broker.getLogicalResource().sim_get_next(onlyGRMSGetOffers, e); |
---|
255 | IO_data ioData = (IO_data) e.get_data(); |
---|
256 | List<Offer> response = (List<Offer>) ioData.getData(); |
---|
257 | result.addAll(response); |
---|
258 | } |
---|
259 | |
---|
260 | return result; |
---|
261 | } |
---|
262 | |
---|
263 | public Offer getOffer(String resourceName, |
---|
264 | AbstractTimeRequirements<?> timeRequirements, |
---|
265 | AbstractResourceRequirements<?> resourceRequirements, |
---|
266 | SecurityContextInterface securityContext) |
---|
267 | throws ReservationException, NotAuthorizedException, PermanentException { |
---|
268 | |
---|
269 | Object[] data = { timeRequirements, resourceRequirements }; |
---|
270 | broker.getLogicalResource().send(resourceName, GridSimTags.SCHEDULE_NOW, |
---|
271 | GssimTags.GET_OFFERS, data); |
---|
272 | |
---|
273 | Sim_event e = new Sim_event(); |
---|
274 | Sim_predicate onlyGRMSGetOffers = new Sim_type_p( |
---|
275 | GssimTags.RETURN_GET_OFFERS); |
---|
276 | broker.getLogicalResource().sim_get_next(onlyGRMSGetOffers, e); |
---|
277 | Offer response = (Offer) e.get_data(); |
---|
278 | |
---|
279 | return response; |
---|
280 | } |
---|
281 | |
---|
282 | public void modifyReservation(ReservationNew reservation, |
---|
283 | TimeResourceAllocation resourceUsage, |
---|
284 | SecurityContextInterface securityContext) throws ReservationException, NotAuthorizedException, PermanentException { |
---|
285 | throw new ReservationException("Not implemented."); |
---|
286 | } |
---|
287 | |
---|
288 | public boolean supportProvider(ResourceProvider provider) { |
---|
289 | return true; |
---|
290 | } |
---|
291 | |
---|
292 | public ModuleType getType() { |
---|
293 | return ModuleType.RESERVATION_MANAGER; |
---|
294 | } |
---|
295 | |
---|
296 | public void init(Properties properties) { |
---|
297 | } |
---|
298 | |
---|
299 | public void dispose() { |
---|
300 | } |
---|
301 | |
---|
302 | private int incrementID(){ |
---|
303 | this.transactionId++; |
---|
304 | return transactionId; |
---|
305 | } |
---|
306 | |
---|
307 | |
---|
308 | |
---|
309 | } |
---|
310 | |
---|