/
PoolInfoResources.java
548 lines (479 loc) · 24.2 KB
/
PoolInfoResources.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
/*
COPYRIGHT STATUS:
Dec 1st 2001, Fermi National Accelerator Laboratory (FNAL) documents and
software are sponsored by the U.S. Department of Energy under Contract No.
DE-AC02-76CH03000. Therefore, the U.S. Government retains a world-wide
non-exclusive, royalty-free license to publish or reproduce these documents
and software for U.S. Government purposes. All documents and software
available from this server are protected under the U.S. and Foreign
Copyright Laws, and FNAL reserves all rights.
Distribution of the software available from this server is free of
charge subject to the user following the terms of the Fermitools
Software Legal Information.
Redistribution and/or modification of the software shall be accompanied
by the Fermitools Software Legal Information (including the copyright
notice).
The user is asked to feed back problems, benefits, and/or suggestions
about the software to the Fermilab Software Providers.
Neither the name of Fermilab, the URA, nor the names of the contributors
may be used to endorse or promote products derived from this software
without specific prior written permission.
DISCLAIMER OF LIABILITY (BSD):
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
"AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL FERMILAB,
OR THE URA, OR THE U.S. DEPARTMENT of ENERGY, OR CONTRIBUTORS BE LIABLE
FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT
OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR
BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
Liabilities of the Government:
This software is provided by URA, independent from its Prime Contract
with the U.S. Department of Energy. URA is acting independently from
the Government and in its own private capacity and is not acting on
behalf of the U.S. Government, nor as its contractor nor its agent.
Correspondingly, it is understood and agreed that the U.S. Government
has no connection to this software and in no manner whatsoever shall
be liable for nor assume any responsibility or obligation for any claim,
cost, or damages arising out of or resulting from the use of the software
available from this server.
Export Control:
All documents and software available from this server are subject to U.S.
export control laws. Anyone downloading information from this server is
obligated to secure any necessary Government licenses before exporting
documents or software obtained from this server.
*/
package org.dcache.restful.resources.pool;
import com.fasterxml.jackson.databind.ObjectMapper;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import io.swagger.annotations.ApiResponse;
import io.swagger.annotations.ApiResponses;
import io.swagger.annotations.Authorization;
import io.swagger.annotations.ResponseHeader;
import org.json.JSONException;
import org.springframework.stereotype.Component;
import javax.inject.Inject;
import javax.inject.Named;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.ws.rs.BadRequestException;
import javax.ws.rs.Consumes;
import javax.ws.rs.DELETE;
import javax.ws.rs.DefaultValue;
import javax.ws.rs.ForbiddenException;
import javax.ws.rs.GET;
import javax.ws.rs.InternalServerErrorException;
import javax.ws.rs.PATCH;
import javax.ws.rs.Path;
import javax.ws.rs.PathParam;
import javax.ws.rs.Produces;
import javax.ws.rs.QueryParam;
import javax.ws.rs.core.Context;
import javax.ws.rs.core.MediaType;
import javax.ws.rs.core.Response;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Comparator;
import java.util.List;
import java.util.stream.Collectors;
import diskCacheV111.poolManager.PoolSelectionUnit;
import diskCacheV111.poolManager.PoolSelectionUnit.SelectionPool;
import diskCacheV111.pools.PoolV2Mode;
import diskCacheV111.util.CacheException;
import diskCacheV111.util.PnfsId;
import diskCacheV111.vehicles.Message;
import diskCacheV111.vehicles.PoolModifyModeMessage;
import diskCacheV111.vehicles.PoolMoverKillMessage;
import dmg.cells.nucleus.CellPath;
import dmg.cells.nucleus.NoRouteToCellException;
import org.dcache.cells.CellStub;
import org.dcache.pool.movers.json.MoverData;
import org.dcache.pool.nearline.json.NearlineData;
import org.dcache.poolmanager.PoolMonitor;
import org.dcache.restful.providers.PagedList;
import org.dcache.restful.providers.pool.PoolInfo;
import org.dcache.restful.providers.pool.PoolModeUpdate;
import org.dcache.restful.providers.selection.Pool;
import org.dcache.restful.services.pool.PoolInfoService;
import org.dcache.restful.services.transfers.TransferInfoService;
import org.dcache.restful.util.HttpServletRequests;
import static org.dcache.restful.providers.ErrorResponseProvider.NOT_IMPLEMENTED;
import static org.dcache.restful.providers.PagedList.TOTAL_COUNT_HEADER;
import static org.dcache.restful.providers.SuccessfulResponse.successfulResponse;
/**
* <p>RESTful API to the {@link PoolInfoService} service.</p>
*
* @version v1.0
*/
@Component
@Api(value = "pools", authorizations = {@Authorization("basicAuth")})
@Path("/pools")
public final class PoolInfoResources {
private static final String TYPE_ERROR =
"type specification %s not supported; please indicate all "
+ "door-initiated movers by an undefined "
+ "type parameter, or p2p movers using "
+ "'p2p-client,p2p-server'";
@Context
private HttpServletRequest request;
@Context
private HttpServletResponse response;
@Inject
private PoolInfoService service;
@Inject
private TransferInfoService transferInfoService;
@Inject
private PoolMonitor poolMonitor;
@Inject
@Named("pool-stub")
private CellStub poolStub;
@GET
@ApiOperation("Get information about all pools (name, group membership, links). "
+ "Requires admin role. Results sorted lexicographically by pool name.")
@ApiResponses({
@ApiResponse(code = 403, message = "Pool info only accessible to admin users."),
})
@Produces(MediaType.APPLICATION_JSON)
public List<Pool> getPools() throws CacheException {
if (!HttpServletRequests.isAdmin(request)) {
throw new ForbiddenException(
"Pool info only accessible to admin users.");
}
PoolSelectionUnit psu = poolMonitor.getPoolSelectionUnit();
return psu.getPools().values()
.stream()
.sorted(Comparator.comparing(SelectionPool::getName))
.map((p) -> new Pool(p.getName(), psu))
.collect(Collectors.toList());
}
@GET
@ApiOperation("Get information about a specific pool (name, group membership, links). "
+ "Requires admin role.")
@ApiResponses({
@ApiResponse(code = 403, message = "Pool info only accessible to admin users."),
})
@Path("/{pool}")
@Produces(MediaType.APPLICATION_JSON)
public Pool getPool(@ApiParam(value = "The pool to be described.",
required = true)
@PathParam("pool") String pool) {
if (!HttpServletRequests.isAdmin(request)) {
throw new ForbiddenException(
"Pool info only accessible to admin users.");
}
return new Pool(pool, poolMonitor.getPoolSelectionUnit());
}
@GET
@ApiOperation("Get information about a specific pool (configuration, state, usage). "
+ "Requires admin role.")
@ApiResponses({
@ApiResponse(code = 403, message = "Pool info only accessible to admin users."),
})
@Path("/{pool}/usage")
@Produces(MediaType.APPLICATION_JSON)
public PoolInfo getPoolUsage(@ApiParam(value = "The pool to be described.",
required = true)
@PathParam("pool") String pool) {
if (!HttpServletRequests.isAdmin(request)) {
throw new ForbiddenException(
"Pool info only accessible to admin users.");
}
PoolInfo info = new PoolInfo();
service.getDiagnosticInfo(pool, info);
return info;
}
@GET
@ApiOperation("Get information about a specific PNFS-ID usage within a "
+ "specific pool. Requires admin role.")
@ApiResponses({
@ApiResponse(code = 403, message = "Pool info only accessible to admin users."),
})
@Path("/{pool}/{pnfsid}")
@Produces(MediaType.APPLICATION_JSON)
public PoolInfo getRepositoryInfoForFile(@ApiParam(value = "The pool to be described.",
required = true)
@PathParam("pool") String pool,
@ApiParam(value = "The PNFS-ID of the file to be described.",
required = true)
@PathParam("pnfsid") PnfsId pnfsid) {
if (!HttpServletRequests.isAdmin(request)) {
throw new ForbiddenException(
"Repository info for file only accessible to admin users.");
}
PoolInfo info = new PoolInfo();
service.getCacheInfo(pool, pnfsid, info);
return info;
}
@GET
@ApiOperation("Get histogram data concerning activity on a specific pool (48-hour window).")
@Path("/{pool}/histograms/queues")
@Produces(MediaType.APPLICATION_JSON)
public PoolInfo getQueueHistograms(@ApiParam(value = "The pool to be described.",
required = true)
@PathParam("pool") String group) {
PoolInfo info = new PoolInfo();
service.getQueueStat(group, info);
return info;
}
@GET
@ApiOperation("Get histogram data concerning file lifetime on a specific pool (60-day window).")
@Path("/{pool}/histograms/files")
@Produces(MediaType.APPLICATION_JSON)
public PoolInfo getFilesHistograms(@ApiParam(value = "The pool to be described.",
required = true)
@PathParam("pool") String group) {
PoolInfo info = new PoolInfo();
service.getFileStat(group, info);
return info;
}
@GET
@ApiOperation(value = "Get mover information for a specific pool. Requires admin role.",
responseHeaders = {
@ResponseHeader(name = "X-Total-Count", description = "Total "
+ "number of potential responses. This may be greater "
+ "than the number of response if offset or limit are "
+ "specified")
})
@ApiResponses({
@ApiResponse(code = 403, message = "Mover info only accessible to admin users."),
})
@Path("/{pool}/movers")
@Produces(MediaType.APPLICATION_JSON)
public List<MoverData> getMovers(@ApiParam("The pool to be described.")
@PathParam("pool") String pool,
@ApiParam("A comma-seperated list of mover types. "
+ "Currently, either 'p2p-client,p2p-server' "
+ "or none (meaning all) is supported.")
@QueryParam("type") String typeList,
@ApiParam("The number of items to skip.")
@DefaultValue("0")
@QueryParam("offset") int offset,
@ApiParam("The maximum number of items to return.")
@QueryParam("limit") Integer limit,
@ApiParam("Select movers operating on a specific PNFS-ID.")
@QueryParam("pnfsid") String pnfsid,
@ApiParam("Select movers with a specific queue.")
@QueryParam("queue") String queue,
@ApiParam("Select movers in a particular state.")
@QueryParam("state") String state,
@ApiParam("Select movers with a specific mode.")
@QueryParam("mode") String mode,
@ApiParam("Select movers initiated by a specific door.")
@QueryParam("door") String door,
@ApiParam("Select movers with a specific storage class.")
@QueryParam("storageClass") String storageClass,
@ApiParam("How returned items should be sorted.")
@DefaultValue("door,startTime")
@QueryParam("sort") String sort) {
if (!HttpServletRequests.isAdmin(request)) {
throw new ForbiddenException(
"Mover info only accessible to admin users.");
}
limit = limit == null ? Integer.MAX_VALUE : limit;
String[] type = typeList == null ? new String[0]:
typeList.split(",");
PagedList<MoverData> pagedList;
try {
if (type.length == 0) {
pagedList = service.getMovers(pool,
offset,
limit,
pnfsid,
queue,
state,
mode,
door,
storageClass,
sort);
response.addIntHeader(TOTAL_COUNT_HEADER, pagedList.total);
return pagedList.contents;
} else if (type.length == 2) {
if ((type[0].equals("p2p-client")
&& type[1].equals("p2p-server"))
|| (type[1].equals("p2p-client")
&& type[0].equals("p2p-server"))) {
pagedList = service.getP2p(pool,
offset,
limit,
pnfsid,
queue,
state,
storageClass,
sort);
response.addIntHeader(TOTAL_COUNT_HEADER, pagedList.total);
return pagedList.contents;
}
}
} catch (InterruptedException | NoRouteToCellException | CacheException e) {
throw new InternalServerErrorException(e);
}
String error = String.format(TYPE_ERROR, typeList);
throw new InternalServerErrorException(error, NOT_IMPLEMENTED);
}
@GET
@ApiOperation("Get nearline activity information for a specific pool. "
+ "Requires admin role.")
@ApiResponses({
@ApiResponse(code = 400, message = "unrecognized queue type"),
@ApiResponse(code = 403, message = "Flush info only accessible to admin users."),
@ApiResponse(code = 500, message = "Internal Server Error"),
})
@Path("/{pool}/nearline/queues")
@Produces(MediaType.APPLICATION_JSON)
public List<NearlineData> getNearlineQueues(@ApiParam("The pool to be described.")
@PathParam("pool") String pool,
@ApiParam("Select transfers of a specific type "
+ "(flush, stage, remove).")
@QueryParam("type") String typeList,
@ApiParam("The number of items to skip.")
@DefaultValue("0")
@QueryParam("offset") int offset,
@ApiParam("The maximum number of items to return.")
@QueryParam("limit") Integer limit,
@ApiParam("Select only operations affecting this PNFS-ID.")
@QueryParam("pnfsid") String pnfsid,
@ApiParam("Select only operations in this state.")
@QueryParam("state") String state,
@ApiParam("Select only operations of this storage class.")
@QueryParam("storageClass") String storageClass,
@ApiParam("How the returned values should be sorted.")
@DefaultValue("class,created")
@QueryParam("sort") String sort) {
if (!HttpServletRequests.isAdmin(request)) {
throw new ForbiddenException(
"Flush info only accessible to admin users.");
}
limit = limit == null ? Integer.MAX_VALUE : limit;
List<NearlineData> list = new ArrayList<>();
PagedList<NearlineData> pagedList;
int count = 0;
try {
String[] types = typeList == null ? new String[0]:
typeList.split(",");
for (String type: types) {
switch (type) {
case "flush":
pagedList = service.getFlush(pool,
offset,
limit,
pnfsid,
state,
storageClass,
sort);
list.addAll(pagedList.contents);
count += pagedList.total;
break;
case "stage":
pagedList = service.getStage(pool,
offset,
limit,
pnfsid,
state,
storageClass,
sort);
list.addAll(pagedList.contents);
count += pagedList.total;
break;
case "remove":
pagedList = service.getRemove(pool,
offset,
limit,
pnfsid,
state,
storageClass,
sort);
list.addAll(pagedList.contents);
count += pagedList.total;
break;
default:
throw new BadRequestException("unrecognized queue type: "
+ type);
}
}
response.addIntHeader(TOTAL_COUNT_HEADER, count);
return list;
} catch (InterruptedException | NoRouteToCellException | CacheException e) {
throw new InternalServerErrorException(e);
}
}
@DELETE
@ApiOperation("Kill a mover. Requires admin role.")
@ApiResponses({
@ApiResponse(code = 400, message = "Bad Request"),
@ApiResponse(code = 403, message = "Pool command only accessible to admin users."),
@ApiResponse(code = 500, message = "Internal Server Error"),
})
@Path("/{pool}/movers/{id : [0-9]+}")
@Produces(MediaType.APPLICATION_JSON)
public Response killMovers(@ApiParam(value = "The pool with the mover to be killed.",
required = true)
@PathParam("pool") String pool,
@ApiParam(value = "The id of the mover to be killed.",
required = true)
@PathParam("id") int id ) {
if (!HttpServletRequests.isAdmin(request)) {
throw new ForbiddenException(
"Pool command only accessible to admin users.");
}
try {
poolStub.sendAndWait(new CellPath(pool),
new PoolMoverKillMessage(pool, id,
"Killed by user."));
transferInfoService.setCancelled(pool, id);
} catch (IllegalArgumentException e) {
throw new BadRequestException(e);
} catch (CacheException e) {
if (e.getRc() == CacheException.MOVER_NOT_FOUND) {
transferInfoService.setCancelled(pool, id);
} else {
throw new InternalServerErrorException(e);
}
} catch (InterruptedException | NoRouteToCellException e) {
throw new InternalServerErrorException(e);
}
return successfulResponse(Response.Status.OK);
}
@PATCH
@ApiOperation("Modify a pool's mode. Requires admin role.")
@ApiResponses({
@ApiResponse(code = 400, message = "Bad Request"),
@ApiResponse(code = 403, message = "Pool command only accessible to admin users."),
@ApiResponse(code = 500, message = "Internal Server Error"),
})
@Path("/{pool}/usage/mode")
@Consumes(MediaType.APPLICATION_JSON)
@Produces(MediaType.APPLICATION_JSON)
public Response updateMode(@ApiParam(value = "The pool affected by the mode change.",
required = true)
@PathParam("pool") String pool,
@ApiParam(value = "JSON object describing how the "
+ "pool should be modified. "
+ "(Corresponds to PoolModeUpdate.)",
required = true)
String requestPayload) {
if (!HttpServletRequests.isAdmin(request)) {
throw new ForbiddenException(
"Pool command only accessible to admin users.");
}
try {
PoolModeUpdate update
= new ObjectMapper().readValue(requestPayload,
PoolModeUpdate.class);
PoolV2Mode mode = new PoolV2Mode(update.mode());
mode.setResilienceEnabled(update.isResilience());
Message message = new PoolModifyModeMessage(pool, mode);
poolStub.sendAndWait(new CellPath(pool), message);
} catch (JSONException | IllegalArgumentException | IOException e) {
throw new BadRequestException(e);
} catch (InterruptedException | NoRouteToCellException | CacheException e) {
throw new InternalServerErrorException(e);
}
return successfulResponse(Response.Status.OK);
}
}