/
HttpPoolMgrEngineV3.java
1627 lines (1452 loc) · 62.7 KB
/
HttpPoolMgrEngineV3.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
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
package diskCacheV111.poolManager;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import java.io.OutputStream;
import java.io.PrintWriter;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.Comparator;
import java.util.Date;
import java.util.EnumSet;
import java.util.List;
import java.util.Map;
import java.util.TreeMap;
import java.util.concurrent.TimeUnit;
import java.util.stream.Collectors;
import diskCacheV111.util.CacheException;
import diskCacheV111.util.HTMLWriter;
import diskCacheV111.util.PnfsId;
import diskCacheV111.util.TimeoutCacheException;
import diskCacheV111.vehicles.PnfsMapPathMessage;
import diskCacheV111.vehicles.RestoreHandlerInfo;
import diskCacheV111.vehicles.StorageInfo;
import diskCacheV111.vehicles.hsmControl.HsmControlGetBfDetailsMsg;
import dmg.cells.nucleus.CellCommandListener;
import dmg.cells.nucleus.CellEndpoint;
import dmg.cells.nucleus.CellInfo;
import dmg.cells.nucleus.CellInfoProvider;
import dmg.cells.nucleus.CellLifeCycleAware;
import dmg.cells.nucleus.CellMessageReceiver;
import dmg.cells.nucleus.CellMessageSender;
import dmg.cells.nucleus.CellPath;
import dmg.cells.nucleus.DomainContextAware;
import dmg.cells.nucleus.NoRouteToCellException;
import dmg.util.AgingHash;
import dmg.util.HttpException;
import dmg.util.HttpRequest;
import dmg.util.HttpResponseEngine;
import org.dcache.cells.CellStub;
import org.dcache.namespace.FileAttribute;
import org.dcache.poolmanager.Partition;
import org.dcache.poolmanager.PoolManagerGetRestoreHandlerInfo;
import org.dcache.poolmanager.PoolManagerHandlerSubscriber;
import org.dcache.util.Args;
import org.dcache.vehicles.FileAttributes;
import org.dcache.vehicles.PnfsGetFileAttributes;
import static java.util.concurrent.TimeUnit.SECONDS;
public class HttpPoolMgrEngineV3 implements
HttpResponseEngine, Runnable, CellInfoProvider, CellMessageSender,
CellMessageReceiver, DomainContextAware,
CellCommandListener, CellLifeCycleAware
{
private static final String PARAMETER_DCACHE = "dcache";
private static final String PARAMETER_GREP = "grep";
private static final String PARAMETER_LINKGROUP = "linkGroup";
private static final String PARAMETER_NET = "net";
private static final String PARAMETER_PROTOCOL = "protocol";
private static final String PARAMETER_SORT = "sort";
private static final String PARAMETER_STORE = "store";
private static final String PARAMETER_TYPE = "type";
private static final Logger _log =
LoggerFactory.getLogger(HttpPoolMgrEngineV3.class);
private static final long TIMEOUT = 20000;
private final Thread _restoreCollector;
private final String _poolManagerAddress;
private final String _pnfsManagerAddress;
private CellStub _poolManager;
private CellStub _pnfsManager;
private CellStub _hsmController;
private PoolManagerHandlerSubscriber _pm;
private RestoreRequestsReceiver _receiver;
private final AgingHash _pnfsPathMap = new AgingHash(500);
private final AgingHash _fileAttributesMap = new AgingHash(500);
private static final ThreadLocal<SimpleDateFormat> _formatter =
new ThreadLocal<SimpleDateFormat>()
{
@Override
protected SimpleDateFormat initialValue()
{
return new SimpleDateFormat("MM.dd HH:mm:ss");
}
};
private volatile List<Object[]> _lazyRestoreList =
new ArrayList<>();
private long _collectorUpdate = 60000L;
private long _errorCounter;
private long _requestCounter;
private final Object _updateLock = new Object();
private boolean _addStorageInfo;
private boolean _addHsmInfo;
private String[] _siDetails;
private String _cssFile = "/poolInfo/css/default.css";
private Map<String, Object> _context;
private CellEndpoint _endpoint;
public HttpPoolMgrEngineV3(String[] argsString)
{
_receiver = new RestoreRequestsReceiver();
Args arguments = new Args(argsString);
_poolManagerAddress = arguments.getOption("poolmanager");
_pnfsManagerAddress = arguments.getOption("pnfsmanager");
for (int i = 0; i < argsString.length; i++) {
_log.info("HttpPoolMgrEngineV3 : argument : {} : {}", i, argsString[i]);
if (argsString[i].equals("addStorageInfo")) {
_addStorageInfo = true;
_log.info("Option accepted : addStorageInfo");
} else if (argsString[i].equals("addHsmInfo")) {
_addHsmInfo = true;
_log.info("Option accepted : addHsmInfo");
} else if (argsString[i].startsWith("details=")) {
_log.info("Details for lazy restore : {}", argsString[i]);
decodeDetails(argsString[i]);
} else if (argsString[i].equals("cacheLifetime")) {
_receiver.setLifetime(Integer.parseInt(argsString[i]));
} else if (argsString[i].equals("cacheLifetimeUnit")) {
_receiver.setLifetimeUnit(TimeUnit.valueOf(argsString[i].toUpperCase()));
} else if (argsString[i].startsWith("css=")) {
decodeCss(argsString[i].substring(4));
}
}
_receiver.initialize();
_restoreCollector = new Thread(this, "restore-collector");
_log.info("Using CSS file : {}", _cssFile);
}
@Override
public void setCellEndpoint(CellEndpoint endpoint)
{
_endpoint = endpoint;
_poolManager = new CellStub(endpoint, new CellPath(_poolManagerAddress), TIMEOUT, SECONDS);
_pnfsManager = new CellStub(endpoint, new CellPath(_pnfsManagerAddress), TIMEOUT, SECONDS);
_hsmController = new CellStub(endpoint, new CellPath("HsmManager"), TIMEOUT, SECONDS);
_pm = new PoolManagerHandlerSubscriber();
_pm.setPoolManager(_poolManager);
}
@Override
public void setDomainContext(Map<String, Object> context)
{
_context = context;
}
@Override
public void afterStart()
{
_pm.start();
_pm.afterStart();
_restoreCollector.start();
}
@Override
public void beforeStop()
{
_restoreCollector.interrupt();
try {
_restoreCollector.join();
} catch (InterruptedException e) {
_log.warn("Interrupted while waiting for restore-collector to terminate");
}
_pm.beforeStop();
}
public void messageArrived(PoolManagerGetRestoreHandlerInfo message) {
_receiver.messageArrived(message);
}
private void decodeCss(String cssDetails)
{
cssDetails = cssDetails.trim();
if ((!cssDetails.isEmpty()) && !cssDetails.equals("default")) {
_cssFile = cssDetails;
}
}
private void decodeDetails(String details)
{
if (details.startsWith("details=") && (details.length() >= 9)) {
_siDetails = details.substring(8).split(",");
}
}
@Override
public void run()
{
_log.info("Restore Collector Thread started");
try {
while (!Thread.interrupted()) {
try {
synchronized (_updateLock) {
_updateLock.wait(_collectorUpdate);
}
runRestoreCollector();
} catch(NoRouteToCellException e) {
_log.warn("Restore Collector got : " + e, e);
}
}
} catch (InterruptedException e) {
_log.debug("Restore Collector interrupted");
}
}
@Override
public void getInfo(PrintWriter pw)
{
pw.println(" PoolManagerEngine : [$Id: HttpPoolMgrEngineV3.java,v 1.26 2007-08-16 20:20:56 behrmann Exp $]");
pw.println(" Request Counter : "+_requestCounter);
pw.println(" Error Counter : "+_errorCounter);
pw.println(" Collector Update : "+(_collectorUpdate/1000L)+" seconds");
pw.println(" addStorageInfo : "+_addStorageInfo);
pw.println(" addHsmInfo : "+_addHsmInfo);
}
public static final String hh_set_update = "[<updateTime/sec>]";
public String ac_set_update_$_0_1(Args args)
{
if (args.argc() == 0) {
synchronized (_updateLock) {
_updateLock.notifyAll();
}
} else {
long x = Long.parseLong(args.argv(0));
if (x < 30) {
throw new
IllegalArgumentException("<updateTime> must be > 30");
}
synchronized (_updateLock) {
_collectorUpdate = x * 1000;
_updateLock.notifyAll();
}
}
return "";
}
private void runRestoreCollector()
throws NoRouteToCellException, InterruptedException
{
List<RestoreHandlerInfo> infos = _receiver.getAllRequests();
List<Object[]> agedList = new ArrayList<>();
for (RestoreHandlerInfo info : infos) {
try {
Object[] a = new Object[3];
a[0] = info;
String name = info.getName();
String pnfsId = name.substring(0, name.indexOf('@'));
//
// collect the paths
//
String path = (String) _pnfsPathMap.get(pnfsId);
if (path == null) {
path = getPathByPnfsId(pnfsId);
}
if (path == null) {
a[1] = pnfsId;
} else {
_pnfsPathMap.put(pnfsId, a[1] = path);
}
//
// collect the storage infos
//
if (_addStorageInfo) {
FileAttributes fileAttributes = (FileAttributes) _fileAttributesMap.get(
pnfsId);
if (fileAttributes == null) {
fileAttributes = getFileAttributesByPnfsId(pnfsId);
}
if (fileAttributes != null) {
StorageInfo storageInfo = fileAttributes.getStorageInfo();
if (_addHsmInfo) {
StorageInfo si = getHsmInfoByStorageInfo(pnfsId,
storageInfo);
if (si != null) {
storageInfo = si;
fileAttributes.setStorageInfo(si);
}
}
if (_siDetails != null) { // allows to select items
if (fileAttributes.isDefined(FileAttribute.SIZE)) {
storageInfo.setKey("size",
String.valueOf(fileAttributes.getSize()));
}
storageInfo.setKey("new",
String.valueOf(storageInfo.isCreatedOnly()));
storageInfo.setKey("stored",
String.valueOf(storageInfo.isStored()));
storageInfo.setKey("sClass",
fileAttributes.getStorageClass());
storageInfo.setKey("cClass",
fileAttributes.getCacheClass());
storageInfo.setKey("hsm", fileAttributes.getHsm());
}
_fileAttributesMap.put(pnfsId, a[2] = storageInfo);
}
}
agedList.add(a);
} catch (Exception e) {
_log.warn(e.toString(), e);
}
}
_lazyRestoreList = agedList;
}
private StorageInfo getHsmInfoByStorageInfo(String pnfsId, StorageInfo storageInfo)
{
try {
HsmControlGetBfDetailsMsg msg =
new HsmControlGetBfDetailsMsg(new PnfsId(pnfsId),storageInfo,"default");
return _hsmController.sendAndWait(msg).getStorageInfo();
} catch (InterruptedException | CacheException | NoRouteToCellException e) {
_log.warn(e.toString(), e);
return null;
}
}
private String getPathByPnfsId(String pnfsId)
{
try {
PnfsMapPathMessage msg = new PnfsMapPathMessage(new PnfsId(pnfsId));
return _pnfsManager.sendAndWait(msg).getGlobalPath();
} catch (InterruptedException | CacheException | NoRouteToCellException e) {
_log.warn(e.toString());
return null;
}
}
private FileAttributes getFileAttributesByPnfsId(String pnfsId)
{
try {
PnfsGetFileAttributes msg =
new PnfsGetFileAttributes(new PnfsId(pnfsId), EnumSet.of(FileAttribute.SIZE, FileAttribute.STORAGEINFO));
return _pnfsManager.sendAndWait(msg).getFileAttributes();
} catch (InterruptedException | CacheException | NoRouteToCellException e) {
_log.warn(e.toString());
return null;
}
}
private void printMenu(PrintWriter pw, String sort, String grep)
{
String action = "detail";
String tableDataColor = "#eeeeee";
String tableColor = "#dddddd";
String[] sel =
{ "PnfsId" , "i.name",
"Time" , "i.start",
"Pool" , "i.pool",
"Status" , "i.status",
"Tape" , "hsm.osm.volumeName",
"StorageClass", "sclass",
"Path" , "path"
};
pw.println("<form name=\"input\" action=\""+action+"\" method=\"get\">");
pw.println("<center><table border=0 cellspacing=2 width=\"95%\" bgcolor=\""+tableColor+"\">");
pw.println(" <tr>");
pw.println(" <td align=center bgcolor=\""+tableDataColor+"\">");
pw.println(" <input type=\"submit\" value=\"Select\" name=\"otto\">");
pw.println(" </td>");
pw.println(" <td align=center bgcolor=\""+tableDataColor+"\">");
pw.println(" Sort by");
pw.println(" </td>");
String radio = "type=\"radio\" name=\"sort\"";
String checked = "checked=\"checked\" ";
for (int i = 0, n = sel.length ; i < n; i+=2) {
pw.println(" <td align=center bgcolor=\""+tableDataColor+"\">");
pw.println(" <input "+radio+" id=\""+sel[i]+"\" value=\""+
sel[i+1]+"\" "+
((((sort==null)&&(i==0))||
((sort!=null)&&sel[i+1].equals(sort))) ? checked : "")+"/>");
pw.println(" <label for=\""+sel[i]+"\">"+sel[i]+"</label>");
pw.println(" </td>");
}
pw.println(" <td align=center bgcolor=\""+tableDataColor+"\">");
pw.println(" Search <input type=\"text\" value=\""+
(grep == null ? "" : grep)
+"\" name=\"grep\">");
pw.println(" </td>");
pw.println(" </tr>");
pw.println("</table></center>");
}
private void printCssFile(PrintWriter pw, String filename)
{
if (filename.equals("test.html")) {
//
// table test for css (internal and external)
//
pw.println("<html>");
pw.println("<head>");
pw.println("<title>Titel der Datei</title>");
pw.println("<link rel=\"stylesheet\" type=\"text/css\" href=\""+_cssFile+"\">");
pw.println("</head>");
pw.println("<body>");
pw.println("<h1>This is a header - 1 </h1>");
pw.println("<h2>This is a header - 2 </h2>");
pw.println("<center>");
pw.println("<table class=\"s-table\">");
pw.println("<tr class=\"s-table\"><th class=\"s-table\">Header 1</th><th class=\"s-table\">Header 2</th></tr>");
pw.println("<tr class=\"s-table-a\"><td class=\"s-table\">entry-1</td><td class=\"s-table\">entry-2</td></tr>");
pw.println("<tr class=\"s-table-b\"><td class=\"s-table\">entry-3</td><td class=\"s-table\">entry-4</td></tr>");
pw.println("</table>");
pw.println("</body>");
pw.println("</html>");
} else if (filename.equals("default.css")) {
printInternalCssFile(pw);
}
}
private void printInternalCssFile(PrintWriter pw)
{
pw.println("body { background-color:orange; }");
pw.println("table.s-table { width:90%; border:1px; border-style:solid; border-spacing:0px; border-collapse:collapse; }");
pw.println("tr.s-table { background-color:#115259; color:white; font-size:18; }");
pw.println("tr.s-table-a { background-color:#bebebe; text-align:center; font-size:16; }");
pw.println("tr.s-table-b { background-color:#efefef; text-align:center; font-size:16; }");
pw.println("tr.s-table-e { background-color:red; text-align:center; font-size:16; }");
pw.println("td.s-table { border:1px ; border-style:solid; border-spacing:1px; padding:3; }");
pw.println("th.s-table { border:1px ; border-style:solid; border-spacing:1px;}");
pw.println("td.s-table-disabled { border:1px ; border-style:solid; border-spacing:0px; padding:3;}");
pw.println("td.s-table-e { background-color:red;}");
pw.println("td.s-table-regular { border:1px ; border-style:solid; border-spacing:0px; padding:3;}");
pw.println("span.s-table-disabled { color:gray ; }");
pw.println("span.s-table-regular { color:black ; }");
pw.println("a.s-table:visited { text-decoration:none; color:blue; }");
pw.println("a.s-table:link { text-decoration:none; color:blue; }");
pw.println("table.m-table { width:90%; border:0px; border-style:none; border-spacing:0px; border-collapse:collapse; }");
pw.println("td.m-table { background-color:white; text-align:center; border:1px ; border-style:solid; border-spacing:1px;}");
pw.println("a.m-table:visited { text-decoration:none; }");
pw.println("a.m-table-active:visited { text-decoration:none; color:red; }");
pw.println("a.m-table:link { text-decoration:none; }");
pw.println("a.m-table-active:link { text-decoration:none; color:red; }");
pw.println("table.l-table { width:90%; color:black; table-layout:auto;");
pw.println(" border:1px; border-style:none; border-spacing:0px; border-collapse:collapse; }");
pw.println("tr.l-table { background-color:green; }");
pw.println("td.l-table { background-color:white; color:black;");
pw.println("width:10.5%;");
pw.println("padding:4; text-align:center;");
pw.println("border:1px; border-style:solid; border-spacing:0px; border-collapse:collapse; }");
pw.println("span.l-table { font-size:16;}");
pw.println("a.l-table:visited { text-decoration:none; color:blue; }");
pw.println("a.l-table:link { text-decoration:none; color:blue; }");
pw.println("table.f-table-a { width:90%; color:black; table-layout:auto; background-color:white;");
pw.println("border:1px; border-style:solid; border-spacing:0px; border-collapse:collapse;}");
pw.println("td.f-table-a { text-align:center; padding:10; }");
pw.println("span.f-table-a { text-align:center; font-size:20px }");
pw.println("table.f-table-b { width:100%; color:black; table-layout:auto; ");
pw.println(" border:1px; border-style:none; border-spacing:0px; border-collapse:collapse; }");
pw.println("th.f-table-b { width:20%;}");
pw.println("td.f-table-b { width:20%; background-color:#eeeeee; color:black;");
pw.println("padding:4; text-align:center; border:1px; border-style:solid; border-spacing:0px; }");
pw.println("span.m-title { font-size:18; color=red; }");
pw.println("a.big-link:visited { text-decoration:none; color:blue; }");
pw.println("a.big-link:link { text-decoration:none; color:blue; }");
pw.println("span.big-link { font-size:24; text-align:center }");
}
@Override
public void queryUrl(HttpRequest request) throws HttpException
{
OutputStream out = request.getOutputStream();
PrintWriter pw = request.getPrintWriter();
String[] urlItems = request.getRequestTokens();
request.printHttpHeader(0);
_requestCounter ++;
try {
if (urlItems.length < 1) {
return;
}
if ((urlItems.length > 1) && (urlItems[1].equals("css"))) {
//
// the internal css stuff (if nothing else is specifed)
//
if (urlItems.length > 2) {
printCssFile(pw, urlItems[2]);
}
//
} else if ((urlItems.length > 1) && (urlItems[1].equals("parameterHandler"))) {
//
// the parameter handler (dCache partitioning)
//
if (urlItems.length > 2) {
//
// the paramter set
//
if (urlItems[2].equals("set")) {
printParameter(pw, urlItems.length > 3 ? urlItems[3] : "*");
}
}
} else if ((urlItems.length > 1) && (urlItems[1].equals("restoreHandler"))) {
if (urlItems.length > 2) {
if (urlItems[2].equals("lazy")) {
//
// LAZY retore queue
//
if ((urlItems.length > 3) && urlItems[3].startsWith("detail")) {
String sort = request.getParameter(PARAMETER_SORT);
String grep = request.getParameter(PARAMETER_GREP);
printMenu(pw, sort, grep);
printLazyRestoreInfo(out, sort, grep);
} else {
//
// regular restore queue
//
printLazyRestoreInfo(out, null,null);
}
} else if (urlItems[2].startsWith("detail")) {
String sort = request.getParameter(PARAMETER_SORT);
String grep = request.getParameter(PARAMETER_GREP);
printMenu(pw, sort, grep);
printRestoreInfo(out, sort, grep);
} else {
printRestoreInfo(out, null, null);
}
} else {
printRestoreInfo(out, null, null);
}
} else {
printConfigurationPages(pw, urlItems, request);
}
} catch (HttpException httpe) {
_errorCounter ++;
throw httpe;
} catch (Exception e) {
_errorCounter ++;
showProblem(pw, e.getMessage());
pw.println("<ul>");
for (int i = 0; i < urlItems.length; i++) {
pw.println("<li> ["+i+"] ");
pw.println(urlItems[i]);
}
pw.println("</ul>");
} finally {
pw.println("</body>");
pw.println("</html>");
}
}
private void printConfigurationHeader(PrintWriter pw)
{
pw.println("<html>");
pw.println("<head>");
pw.println("<title>PoolManager (Pool SelectionUnit) Configuration</title>");
pw.println("<link rel=\"stylesheet\" type=\"text/css\" href=\""+_cssFile+"\">");
pw.println("</head>");
pw.println("<body class=\"m-body\">");
}
private void printConfigurationPages(PrintWriter pw, String[] urlItems, HttpRequest request)
throws HttpException, NoRouteToCellException, InterruptedException
{
printConfigurationHeader(pw);
printPoolManagerHeader(pw, null);
if (urlItems.length < 2) {
showDirectory(pw);
} else if (urlItems[1].equals("pools")) {
showDirectory(pw, 1);
queryPool(pw, urlItems[2]);
} else if (urlItems[1].equals("units")) {
showDirectory(pw, 3);
StringBuilder sb = new StringBuilder();
int i;
for (i = 2; i < (urlItems.length-1); i++) {
sb.append(urlItems[i]).append("/");
}
sb.append(urlItems[i]);
queryUnit(pw, sb.toString());
} else if (urlItems[1].equals("ugroups")) {
showDirectory(pw, 4);
queryUnitGroup(pw, urlItems[2]);
} else if (urlItems[1].equals("pgroups")) {
showDirectory(pw, 2);
queryPoolGroup(pw, urlItems[2]);
} else if (urlItems[1].equals("links")) {
showDirectory(pw, 5);
queryLink(pw, urlItems[2]);
} else if (urlItems[1].equals("linklist")) {
showDirectory(pw, 7);
queryLinkList(pw);
} else if (urlItems[1].equals("match")) {
showDirectory(pw, 6);
showMatch(pw, request);
} else {
throw new HttpException(404, "Unknown key : "+urlItems[1]);
}
}
private void printParameter(PrintWriter pw, String key)
{
try {
printConfigurationHeader(pw);
printPoolManagerHeader(pw, "Partition Manager");
showDirectory(pw, 0);
Map<String,Partition> parameterMap = _poolManager.sendAndWait("pmx get map", Map.class);
if (key.equals("section")) {
printParameterInSections(pw, parameterMap);
} else if (key.equals("matrix")) {
printParameterInMatrix(pw, parameterMap);
}
} catch (TimeoutCacheException e) {
showTimeout(pw);
} catch (CacheException | InterruptedException | NoRouteToCellException e) {
showProblem(pw, e.getMessage());
}
pw.println("<hr><address>Created "+(new Date())+" $Id: HttpPoolMgrEngineV3.java,v 1.26 2007-08-16 20:20:56 behrmann Exp $");
pw.println("</body></html>");
}
private void printParameterInMatrix(PrintWriter pw, Map<String,Partition> parameterMap)
{
Partition defaultParas = parameterMap.get("default");
if (defaultParas == null) {
return;
}
Map<String,Object[]>[] restMap = new Map[parameterMap.size()-1];
String[] header = new String[parameterMap.size()-1];
int row = 0;
for (Map.Entry<String,Partition> entry : parameterMap.entrySet()) {
String mapName = entry.getKey();
if (mapName.equals("default")) {
continue;
}
header[row] = mapName;
restMap[row] = entry.getValue().toMap();
row ++;
}
pw.println("<center><table class=\"s-table\"");
pw.print("<tr class=\"s-table\">");
pw.print("<th class=\"s-table\">Key</th><th class=\"s-table\">Default</th>");
for (int m = 0; m < restMap.length; m++) {
pw.println("<th class=\"s-table\">" + header[m] + "</th>");
}
pw.println("</tr>");
Map<String, Object[]> defaultMap = new TreeMap<>(defaultParas.toMap());
row = 0;
String[] setColor = { "s-table-disabled", "s-table-regular" };
String[] rowClass = { "s-table-a", "s-table-b" };
for (Map.Entry<String, Object[]> entry : defaultMap.entrySet()) {
String key = entry.getKey();
Object[] e = entry.getValue();
boolean isSet;
String value = e[1].toString();
//
// the keys
//
pw.print("<tr class=\""+rowClass[row%rowClass.length]+"\">");
pw.print("<th class=\"s-table\">"); pw.print(key); pw.print("</th>");
//
// default values
//
pw.print("<td class=\"s-table-regular\">");
pw.print("<span class=\"s-table-regular\">");
pw.print(value); pw.println("</span></td>");
//
// the other partitions
//
for (Map<String, Object[]> aRestMap : restMap) {
e = aRestMap.get(key);
isSet = (Boolean) e[0];
value = e[1].toString();
pw.print("<td class=\"" + setColor[isSet ? 1 : 0] + "\">");
pw.print("<span class=\"" + setColor[isSet ? 1 : 0] + "\">");
pw.println(value);
pw.println("</span></td>");
}
pw.println("</tr>");
row++;
}
pw.println("</table></center>");
}
private void printParameterInSections(PrintWriter pw, Map<String,Partition> parameterMap)
{
for (Map.Entry<String,Partition> entry : parameterMap.entrySet()) {
String name = entry.getKey();
Partition p = entry.getValue();
pw.println("<h2>"+name+"</h2>");
printParameterEntry(pw, p);
}
}
private void printParameterEntry(PrintWriter pw, Partition p)
{
Map<String, Object[]> map = p.toMap();
int column = 0;
int maxColumn = 2;
pw.println("<center><table width=\"90%\" cellspacing=4 cellpadding=4 bgcolor=yellow>");
pw.print("<tr>");
for (int l = 0; l < (maxColumn+1); l ++) {
pw.print("<th align=center>Key</th><th align=center>Value</th>");
}
pw.println("</tr>");
for (Map.Entry<String, Object[]> entry : map.entrySet()) {
String name = entry.getKey();
Object[] array = entry.getValue();
boolean isSet = (Boolean)array[0];
Object value = array[1].toString();
if (column == 0) {
pw.print("<tr>");
}
String col = isSet ? "black" : "gray";
pw.print("<th bgcolor=white>"); pw.print(name); pw.print("</th>");
pw.print("<td bgcolor=white align=center ><font color="); pw.print(col) ; pw.print(">");
pw.print(value); pw.println("</font></td>");
if (column == maxColumn) {
pw.print("</tr>");
}
column = (column + 1) % (maxColumn+1);
}
pw.println("</table></center>");
}
private void printLazyRestoreInfo(OutputStream out, String sorting, String grep)
{
HTMLWriter html = new HTMLWriter(out, _context);
html.addHeader("/styles/restoreHandler.css",
"dCache Dataset Restore Monitor (Lazy)");
html.beginTable("sortable",
"pnfs", "PnfsId",
"subnet", "Subnet",
"candidate", "PoolCandidate",
"started", "Started",
"clients", "Clients",
"retries", "Retries",
"status", "Status");
List<Object[]> copy = new ArrayList<>(_lazyRestoreList);
Collections.sort(copy, new OurComparator(sorting));
for (Object[] a: copy) {
if ((grep == null) || grepOk(grep, a)) {
showRestoreInfo(html,
(RestoreHandlerInfo) a[0],
(String) a[1],
(StorageInfo) a[2]);
}
}
html.endTable();
html.addFooter(getClass().getName());
}
private void printRestoreInfo(OutputStream out, String sorting, String grep)
{
HTMLWriter html = new HTMLWriter(out, _context);
html.addHeader("/styles/restoreHandler.css",
"dCache Dataset Restore Monitor");
RestoreHandlerInfo[] list = _lazyRestoreList.stream()
.map(o -> o[0])
.collect(Collectors.toList())
.toArray(new RestoreHandlerInfo[0]);
Arrays.sort(list, new OurComparator(sorting));
html.beginTable("sortable",
"pnfs", "PnfsId",
"subnet", "Subnet",
"candidate", "PoolCandidate",
"started", "Started",
"clients", "Clients",
"retries", "Retries",
"status", "Status");
for (RestoreHandlerInfo info : list) {
if ((grep == null) || grepOk(grep, info)) {
showRestoreInfo(html, info);
}
}
html.endTable();
html.addFooter(getClass().getName());
}
private boolean grepOk(String grep, Object o)
{
RestoreHandlerInfo info;
if (o instanceof RestoreHandlerInfo) {
info = (RestoreHandlerInfo)o;
} else if (o instanceof Object[]) {
info = (RestoreHandlerInfo)((Object[])o)[0];
} else {
return true;
}
StringBuilder sb = new StringBuilder();
sb.append(info.getName()).append(info.getPool()).append(info.getStartTime()).append(info.getStatus());
Object er = info.getErrorMessage();
if (er != null) {
sb.append(er.toString());
}
if (sb.indexOf(grep) > -1) {
return true;
}
if (!(o instanceof Object[])) {
return false;
}
Object[] a = (Object[])o;
if ((a[1] != null) && (a[1].toString().contains(grep))) {
return true;
}
StorageInfo si = (StorageInfo)a[2];
if (si == null) {
return false;
}
return si.toString().contains(grep);
}
@Override
public CellInfo getCellInfo(CellInfo info)
{
return info;
}
private static class OurComparator implements Comparator<Object>
{
private final String _type;
private OurComparator(String type)
{
_type = type;
}
@Override
public int compare(Object o1, Object o2)
{
if (o1 instanceof RestoreHandlerInfo) {
return compareInfo((RestoreHandlerInfo) o1, (RestoreHandlerInfo) o2);
}
if (o1 instanceof Object[]) {
return compareArray((Object[]) o1, (Object[]) o2);
}
return 0;
}
private int compareInfo(RestoreHandlerInfo i1, RestoreHandlerInfo i2)
{
if (_type == null) {
return i1.getName().compareTo(i2.getName());
} else if (_type.equals("i.name")) {
return i1.getName().compareTo(i2.getName());
} else if (_type.equals("i.error")) {
return (i1.getErrorCode() + i1.getName()).
compareTo(i2.getErrorCode() + i2.getName());
} else if (_type.equals("i.status")) {
return i1.getStatus().compareTo(i2.getStatus());
} else if (_type.equals("i.pool")) {
String a = i1.getPool();
String b = i2.getPool();
if ((a == null) || (b == null)) {
return a == null ? -1 : b == null ? 1 : 0;
}
return a.compareTo(b);
} else if (_type.equals("i.start")) {
return Long.compare(i1.getStartTime(),i2.getStartTime());
} else {
return i1.getName().compareTo(i2.getName());
}
}
private int compareArray(Object[] o1, Object[] o2)
{
if (_type == null || _type.startsWith("i.")) {
return compareInfo((RestoreHandlerInfo) o1[0], (RestoreHandlerInfo) o2[0]);
}
if (_type.equals("path")) {
return o1[1].toString().compareTo(o2[1].toString());
}
return compareStorageInfo((StorageInfo) o1[2], (StorageInfo) o2[2]);
}
private int compareStorageInfo(StorageInfo s1, StorageInfo s2)
{
if (_type.equals("sclass")) {
return s1.getStorageClass().compareTo(s2.getStorageClass());
}
String k1 = s1.getKey(_type);
String k2 = s2.getKey(_type);
if ((k1 == null) || (k2 == null)) {
return k1 == null ? -1 : k2 == null ? 1 : 0;
}
return k1.compareTo(k2);
}
}
private void showRestoreInfo(HTMLWriter html, RestoreHandlerInfo info)
{
showRestoreInfo(html, info, null, null);
}
private void showRestoreInfo(HTMLWriter html,
RestoreHandlerInfo info,
String path,
StorageInfo storageInfo)
{
String name = info.getName();
int pos = name.indexOf('@');
String pnfsId = name.substring(0,pos);
String subnet = name.substring(pos+1);
int rc = info.getErrorCode();
String msg = info.getErrorMessage();
String started= _formatter.get().format(new Date(info.getStartTime()));
boolean error = (rc != 0) || ((msg != null) && (!msg.isEmpty()));
String pool = info.getPool();
pool = (pool == null) || (pool.isEmpty() || pool.equals("<unknown>")) ? "N.N." : pool;
String status = info.getStatus();
status = (status == null) || (status.isEmpty()) ? " " : status;
if (error) {
html.beginRow("error", "error odd");
} else {
html.beginRow(null, "odd");
}
html.td("pnfs", pnfsId);
html.td("subnet", subnet);
html.td("pool", pool);
html.td("started", started);
html.td("clients", info.getClientCount());
html.td("retries", info.getRetryCount());
html.td("status", status);
if (path != null) {
html.endRow(false);
if (error) {
html.beginRow("error", "error odd");
} else {
html.beginRow(null, "odd");
}
html.td(7, "path", path);
}
if (storageInfo != null) {
html.endRow(false);
if (error) {