/
PoolSelectionUnitV2.java
2743 lines (2352 loc) · 89.9 KB
/
PoolSelectionUnitV2.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 com.google.common.base.Predicates;
import com.google.common.collect.Maps;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import java.io.PrintWriter;
import java.io.Serializable;
import java.io.StringWriter;
import java.net.URI;
import java.net.UnknownHostException;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Date;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.NoSuchElementException;
import java.util.Set;
import java.util.TreeSet;
import java.util.concurrent.locks.Lock;
import java.util.concurrent.locks.ReadWriteLock;
import java.util.concurrent.locks.ReentrantReadWriteLock;
import java.util.regex.Pattern;
import diskCacheV111.vehicles.GenericStorageInfo;
import diskCacheV111.vehicles.StorageInfo;
import dmg.util.Args;
import dmg.util.CommandSyntaxException;
import dmg.cells.nucleus.CellCommandListener;
import dmg.cells.nucleus.CellSetupProvider;
import org.dcache.util.Glob;
public class PoolSelectionUnitV2
implements Serializable,
PoolSelectionUnit,
CellCommandListener,
CellSetupProvider
{
private static final String __version = "$Id: PoolSelectionUnitV2.java,v 1.42 2007-10-25 14:03:54 tigran Exp $";
private static final Logger _log = LoggerFactory.getLogger(PoolSelectionUnitV2.class);
private static final String NO_NET = "<no net>";
@Override
public String getVersion() {
return __version;
}
public static final int STORE = 1;
public static final int DCACHE = 2;
public static final int NET = 3;
public static final int PROTOCOL = 4;
private static final long serialVersionUID = 4852540784324544199L;
private final Map<String, PGroup> _pGroups = new HashMap<>();
private final Map<String, Pool> _pools = new HashMap<>();
private final Map<String, Link> _links = new HashMap<>();
private final Map<String, LinkGroup> _linkGroups = new HashMap<>();
private final Map<String, UGroup> _uGroups = new HashMap<>();
private final Map<String, Unit> _units = new HashMap<>();
private boolean _useRegex;
private boolean _allPoolsActive;
/**
* Ok, this is the critical part of PoolManager, but (!!!) the whole select
* path is READ-ONLY, unless we change setup. So ReadWriteLock is what we
* are looking for, while is a point of serialization.
*/
private final ReadWriteLock _psuReadWriteLock = new ReentrantReadWriteLock();
private final Lock _psuReadLock = _psuReadWriteLock.readLock();
private final Lock _psuWriteLock = _psuReadWriteLock.writeLock();
private final NetHandler _netHandler = new NetHandler();
@Override
public Map<String, SelectionLink> getLinks() {
_psuReadLock.lock();
try {
return Maps.<String, SelectionLink>newHashMap(_links);
} finally {
_psuReadLock.unlock();
}
}
@Override
public Map<String, SelectionUnit> getSelectionUnits() {
_psuReadLock.lock();
try {
return Maps.<String, SelectionUnit>newHashMap(_units);
} finally {
_psuReadLock.unlock();
}
}
@Override
public Map<String, SelectionUnitGroup> getUnitGroups() {
_psuReadLock.lock();
try {
return Maps.<String, SelectionUnitGroup>newHashMap(_uGroups);
} finally {
_psuReadLock.unlock();
}
}
@Override
public Collection<SelectionLink> getLinksPointingToPoolGroup(String poolGroup) throws NoSuchElementException {
_psuReadLock.lock();
try {
PGroup group = _pGroups.get(poolGroup);
if (group == null) {
throw new NoSuchElementException("No such pool group: " + poolGroup);
}
return new ArrayList<SelectionLink>(group._linkList.values());
} finally {
_psuReadLock.unlock();
}
}
@Override
public SelectionLink getLinkByName(String name) throws NoSuchElementException {
Link link = null;
_psuReadLock.lock();
try {
link = _links.get(name);
if (link == null) {
throw new NoSuchElementException("Link not found : " + name);
}
} finally {
_psuReadLock.unlock();
}
return link;
}
@Override
public String[] getDefinedPools(boolean enabledOnly) {
List<String> list = new ArrayList<>();
_psuReadLock.lock();
try {
for (Pool pool : _pools.values()) {
if ((!enabledOnly) || pool.isEnabled()) {
list.add(pool.getName());
}
}
} finally {
_psuReadLock.unlock();
}
return list.toArray(new String[list.size()]);
}
@Override
public String[] getActivePools() {
List<String> list = new ArrayList<>();
_psuReadLock.lock();
try {
for (Pool pool : _pools.values()) {
if (pool.isEnabled() && pool.isActive()) {
list.add(pool.getName());
}
}
} finally {
_psuReadLock.unlock();
}
return list.toArray(new String[list.size()]);
}
@Override
public void beforeSetup()
{
clear();
}
@Override
public void afterSetup()
{
}
@Override
public void printSetup(PrintWriter pw)
{
_psuReadLock.lock();
try {
pw.append("#\n# Printed by ").append(getClass().getName())
.append(" at ").append(new Date().toString()).append(
"\n#\n#\n");
pw.append("psu set regex ").append(_useRegex?"on":"off").append("\n");
pw.append("psu set allpoolsactive ").append(_allPoolsActive?"on":"off").append("\n");
pw.append("#\n# The units ...\n#\n");
for (Unit unit : _units.values()) {
int type = unit.getType();
pw.append("psu create unit ").append(
type == STORE ? "-store " : type == DCACHE ? "-dcache"
: type == PROTOCOL ? "-protocol" : "-net ")
.append(" ").append(unit.getName()).append("\n");
}
pw.append("#\n# The unit Groups ...\n#\n");
for (UGroup group : _uGroups.values()) {
pw.append("psu create ugroup ").append(group.getName()).append(
"\n");
for (Unit unit : group._unitList.values()) {
pw.append("psu addto ugroup ").append(group.getName())
.append(" ").append(unit.getName()).append("\n");
}
}
pw.append("#\n# The pools ...\n#\n");
for (Pool pool : _pools.values()) {
pw.append("psu create pool ").append(pool.getName());
if (!pool.isPing()) {
pw.append(" -noping");
}
if (!pool.isEnabled()) {
pw.append(" -disabled");
}
pw.append("\n");
}
pw.append("#\n# The pool groups ...\n#\n");
for (PGroup group : _pGroups.values()) {
pw.append("psu create pgroup ").append(group.getName()).append(
"\n");
for (Pool pool : group._poolList.values()) {
pw.append("psu addto pgroup ").append(group.getName())
.append(" ").append(pool.getName()).append("\n");
}
}
pw.append("#\n# The links ...\n#\n");
for (Link link : _links.values()) {
pw.append("psu create link ").append(link.getName());
for (UGroup group : link._uGroupList.values()) {
pw.append(" ").append(group.getName());
}
pw.append("\n");
pw.append("psu set link ").append(link.getName()).append(" ")
.println(link.getAttraction());
for (PoolCore poolCore : link._poolList.values()) {
pw.append("psu add link ").append(link.getName()).append(
" ").println(poolCore.getName());
}
}
pw.append("#\n# The link Groups ...\n#\n");
for (LinkGroup linkGroup : _linkGroups.values()) {
pw.append("psu create linkGroup ").append(linkGroup.getName());
pw.append("\n");
pw.append("psu set linkGroup custodialAllowed ").append(
linkGroup.getName()).append(" ").println(
linkGroup.isCustodialAllowed());
pw.append("psu set linkGroup replicaAllowed ").append(
linkGroup.getName()).append(" ").println(
linkGroup.isReplicaAllowed());
pw.append("psu set linkGroup nearlineAllowed ").append(
linkGroup.getName()).append(" ").println(
linkGroup.isNearlineAllowed());
pw.append("psu set linkGroup outputAllowed ").append(
linkGroup.getName()).append(" ").println(
linkGroup.isOutputAllowed());
pw.append("psu set linkGroup onlineAllowed ").append(
linkGroup.getName()).append(" ").println(
linkGroup.isOnlineAllowed());
for (Map.Entry<String, Set<String>> aAttribute : linkGroup
.attributes().entrySet()) {
String attributeName = aAttribute.getKey();
for (String aAttributeValue : aAttribute.getValue()) {
pw.append("psu set linkGroup attribute ").append(
linkGroup.getName()).append(" ").append(
attributeName).append("=").println(
aAttributeValue);
}
}
for (SelectionLink link : linkGroup.getLinks()) {
pw.append("psu addto linkGroup ").append(
linkGroup.getName()).append(" ").println(
link.getName());
}
}
} finally {
_psuReadLock.unlock();
}
}
public void clear() {
_psuWriteLock.lock();
try {
_netHandler.clear();
_pGroups.clear();
_pools.clear();
_links.clear();
_uGroups.clear();
_units.clear();
_linkGroups.clear();
} finally {
_psuWriteLock.unlock();
}
}
public void setActive(String poolName, boolean active) {
_psuWriteLock.lock();
try {
Pool pool = _pools.get(poolName);
if (pool != null) {
pool.setActive(active);
}
} finally {
_psuWriteLock.unlock();
}
}
public long getActive(String poolName) {
long active = 100000000L;
_psuReadLock.lock();
try {
Pool pool = _pools.get(poolName);
if (pool != null) {
active = pool.getActive();
}
} finally {
_psuReadLock.unlock();
}
return active;
}
private int setEnabled(Glob glob, boolean enabled)
{
_psuWriteLock.lock();
try {
int count = 0;
for (Pool pool: getPools(glob.toPattern())) {
count++;
pool.setEnabled(enabled);
}
return count;
} finally {
_psuWriteLock.unlock();
}
}
public boolean isEnabled(String poolName) {
boolean isEnabled = false;
_psuReadLock.lock();
try {
Pool pool = _pools.get(poolName);
if (pool != null) {
isEnabled = pool.isEnabled();
}
} finally {
_psuReadLock.unlock();
}
return isEnabled;
}
@Override
public SelectionPool getPool(String poolName) {
SelectionPool pool = null;
_psuReadLock.lock();
try {
pool = _pools.get(poolName);
} finally {
_psuReadLock.unlock();
}
return pool;
}
@Override
public SelectionPool getPool(String poolName, boolean create) {
Pool pool = _pools.get(poolName);
if ((pool != null) || !create) {
return pool;
}
pool = new Pool(poolName);
_psuReadLock.lock();
try {
_pools.put(pool.getName(), pool);
PGroup group = _pGroups.get("default");
if (group == null) {
throw new IllegalArgumentException("Not found : " + "default");
}
//
// shall we disallow more than one parent group ?
//
// if( pool._pGroupList.size() > 0 )
// throw new
// IllegalArgumentException( poolName +" already member" ) ;
pool._pGroupList.put(group.getName(), group);
group._poolList.put(pool.getName(), pool);
} finally {
_psuReadLock.unlock();
}
return pool;
}
public Map<String, Link> match(Map<String, Link> map, Unit unit,
DirectionType ioType) {
Map<String, Link> newmap = match(unit, null, ioType);
if (map == null) {
return newmap;
}
Map<String, Link> resultMap = new HashMap<>();
for (Link link : map.values()) {
if (newmap.get(link.getName()) != null) {
resultMap.put(link.getName(), link);
}
}
return resultMap;
}
private LinkMap match(LinkMap linkMap, Unit unit, LinkGroup linkGroup,
DirectionType ioType) {
Map<String, Link> map = match(unit, linkGroup, ioType);
for (Link link : map.values()) {
linkMap.addLink(link);
}
return linkMap;
}
/**
* @Guarded by _psuReadLock
*/
@Override
public PoolPreferenceLevel[] match(DirectionType type, String netUnitName, String protocolUnitName,
StorageInfo storageInfo, String linkGroupName) {
String storeUnitName = storageInfo.getStorageClass()+"@"+storageInfo.getHsm();
String dCacheUnitName = storageInfo.getCacheClass();
_log.debug("running match: type={} store={} dCacheUnit={} net={} protocol={} SI={} linkGoup={}", type,
storeUnitName, dCacheUnitName, netUnitName, protocolUnitName, storageInfo, linkGroupName);
Map<String, String> variableMap = storageInfo.getMap();
PoolPreferenceLevel[] result = null;
_psuReadLock.lock();
try {
//
// resolve the unit from the unitname (or net unit mask)
//
// regexp code added by rw2 12/5/02
// original code is in the else
//
List<Unit> list = new ArrayList<>();
if (_useRegex) {
Unit universalCoverage = null;
Unit classCoverage = null;
for (Unit unit : _units.values()) {
if (unit.getType() != STORE) {
continue;
}
if (unit.getName().equals("*@*")) {
universalCoverage = unit;
} else if (unit.getName().equals("*@" + storeUnitName)) {
classCoverage = unit;
} else {
if (Pattern.matches(unit.getName(), storeUnitName)) {
list.add(unit);
break;
}
}
}
//
// If a pattern matches then use it, fail over to a class,
// then universal. If nothing, throw exception
//
if (list.isEmpty()) {
if (classCoverage != null) {
list.add(classCoverage);
} else if (universalCoverage != null) {
list.add(universalCoverage);
} else {
throw new IllegalArgumentException(
"Unit not found : " + storeUnitName);
}
}
} else {
Unit unit = _units.get(storeUnitName);
if (unit == null) {
int ind = storeUnitName.lastIndexOf("@");
if ((ind > 0) && (ind < (storeUnitName.length() - 1))) {
String template = "*@"
+ storeUnitName.substring(ind + 1);
if ((unit = _units.get(template)) == null) {
if ((unit = _units.get("*@*")) == null) {
_log.debug("no matching storage unit found for: {}", storeUnitName);
throw new IllegalArgumentException(
"Unit not found : " + storeUnitName);
}
}
} else {
throw new IllegalArgumentException(
"IllegalUnitFormat : " + storeUnitName);
}
}
_log.debug("matching storage unit found for: {}", storeUnitName);
list.add(unit);
}
if (protocolUnitName != null) {
Unit unit = findProtocolUnit(protocolUnitName);
//
if (unit == null){
_log.debug("no matching protocol unit found for: {}", protocolUnitName);
throw new IllegalArgumentException("Unit not found : "
+ protocolUnitName);
}
_log.debug("matching protocol unit found: {}", unit);
list.add(unit);
}
if (dCacheUnitName != null) {
Unit unit = _units.get(dCacheUnitName);
if (unit == null) {
_log.debug("no matching dCache unit found for: {}", dCacheUnitName);
throw new IllegalArgumentException("Unit not found : "
+ dCacheUnitName);
}
_log.debug("matching dCache unit found: {}", unit);
list.add(unit);
}
if (netUnitName != null) {
try {
Unit unit = _netHandler.match(netUnitName);
if (unit == null) {
_log.debug("no matching net unit found for: {}", netUnitName);
throw new IllegalArgumentException(
"Unit not matched : " + netUnitName);
}
_log.debug("matching net unit found: {}" + unit);
list.add(unit);
} catch (UnknownHostException uhe) {
throw new IllegalArgumentException(
"NetUnit not resolved : " + netUnitName);
}
}
//
// match the requests ( logical AND )
//
//
// Map map = null ;
// while( units.hasNext() )map = match( map , (Unit)units.next() ) ;
// Iterator links = map.values().iterator() ;
//
//
// i) sort according to the type (read,write,cache)
// ii) the and is only OK if we have at least as many
// units (from the arguments) as required by the
// number of uGroupList(s).
// iii) check for the hashtable if required.
//
int fitCount = list.size();
Set<Link> sortedSet = new TreeSet<>(new LinkComparator(type));
//
// use subset on links if it's defined
//
LinkGroup linkGroup = null;
if (linkGroupName != null) {
linkGroup = _linkGroups.get(linkGroupName);
if (linkGroup == null) {
_log.debug("LinkGroup not found : {}", linkGroupName );
throw new IllegalArgumentException("LinkGroup not found : "
+ linkGroupName);
}
}
//
// find all links that matches the specified list of units
//
LinkMap matchingLinks = new LinkMap();
for (Unit unit : list) {
matchingLinks = match(matchingLinks, unit, linkGroup, type);
}
Iterator<Link> linkIterator = matchingLinks.iterator();
while (linkIterator.hasNext()) {
Link link = linkIterator.next();
if ((link._uGroupList.size() <= fitCount)
&& ((variableMap == null) || link.exec(variableMap))) {
sortedSet.add(link);
}
}
int pref = -1;
List<List<Link>> listList = new ArrayList<>();
List<Link> current = null;
switch (type) {
case READ:
for (Link link : sortedSet) {
if (link.getReadPref() < 1) {
continue;
}
if (link.getReadPref() != pref) {
listList.add(current = new ArrayList<>());
pref = link.getReadPref();
}
current.add(link);
}
break;
case CACHE:
for (Link link : sortedSet) {
if (link.getCachePref() < 1) {
continue;
}
if (link.getCachePref() != pref) {
listList.add(current = new ArrayList<>());
pref = link.getCachePref();
}
current.add(link);
}
break;
case P2P:
for (Link link : sortedSet) {
int tmpPref = link.getP2pPref() < 0 ? link.getReadPref()
: link.getP2pPref();
if (tmpPref < 1) {
continue;
}
if (tmpPref != pref) {
listList.add(current = new ArrayList<>());
pref = tmpPref;
}
current.add(link);
}
break;
case WRITE:
for (Link link : sortedSet) {
if (link.getWritePref() < 1) {
continue;
}
if (link.getWritePref() != pref) {
listList.add(current = new ArrayList<>());
pref = link.getWritePref();
}
current.add(link);
}
}
List<Link>[] x = listList.toArray(new List[listList.size()]);
result = new PoolPreferenceLevel[x.length];
//
// resolve the links to the pools
//
for (int i = 0; i < x.length; i++) {
List<Link> linkList = x[i];
List<String> resultList = new ArrayList<>();
String tag = null;
for (Link link : linkList) {
//
// get the link if available
//
if ((tag == null) && (link.getTag() != null)) {
tag = link.getTag();
}
for (PoolCore poolCore : link._poolList.values()) {
if (poolCore instanceof Pool) {
Pool pool = (Pool) poolCore;
_log.debug("Pool: {} can read from tape? : {}", pool, pool.canReadFromTape());
if (((type == DirectionType.READ && pool.canRead())
|| (type == DirectionType.CACHE && pool.canReadFromTape()
&& poolCanStageFile(pool, storageInfo))
|| (type == DirectionType.WRITE && pool.canWrite())
|| (type == DirectionType.P2P && pool.canWriteForP2P()))
&& (_allPoolsActive || pool.isActive())) {
resultList.add(pool.getName());
}
} else {
for (Pool pool : ((PGroup)poolCore)._poolList.values()) {
_log.debug("Pool: {} can read from tape? : {}", pool, pool.canReadFromTape());
if (((type == DirectionType.READ && pool.canRead())
|| (type == DirectionType.CACHE && pool.canReadFromTape()
&& poolCanStageFile(pool, storageInfo))
|| (type == DirectionType.WRITE && pool.canWrite())
|| (type == DirectionType.P2P && pool.canWriteForP2P()))
&& (_allPoolsActive || pool.isActive())) {
resultList.add(pool.getName());
}
}
}
}
}
result[i] = new PoolPreferenceLevel(resultList, tag);
}
} finally {
_psuReadLock.unlock();
}
if( _log.isDebugEnabled() ) {
StringBuilder sb = new StringBuilder("match done: ");
for( int i = 0; i < result.length; i++) {
sb.append("[").append(i).append("] :");
for(String poolName: result[i].getPoolList()) {
sb.append(" ").append(poolName);
}
}
_log.debug(sb.toString());
}
return result;
}
@Override
public String getProtocolUnit(String protocolUnitName) {
Unit unit = findProtocolUnit(protocolUnitName);
return unit == null ? null : unit.getName();
}
//
// Legal formats : <protocol>/<version>
//
private boolean _protocolsChecked;
public Unit findProtocolUnit(String protocolUnitName) {
//
if ((protocolUnitName == null) || (protocolUnitName.length() == 0)) {
return null;
}
//
int position = protocolUnitName.indexOf('/');
//
//
if ((position < 0) || (position == 0)
|| (position == (protocolUnitName.length() - 1))) {
throw new IllegalArgumentException(
"Not a valid protocol specification : " + protocolUnitName);
}
//
// we try :
// <protocol>/<majorVersion>
// <protocol>/*
// */*
//
Unit unit = null;
_psuReadLock.lock();
try {
unit = _units.get(protocolUnitName);
if (unit != null) {
return unit;
}
//
//
unit = _units.get(protocolUnitName.substring(0, position) + "/*");
//
if (unit == null) {
unit = _units.get("*/*");
}
} finally {
_psuReadLock.unlock();
}
//
return unit;
}
@Override
public String getNetIdentifier(String address) throws UnknownHostException {
_psuReadLock.lock();
try {
NetUnit unit = _netHandler.match(address);
if (unit == null) {
return NO_NET;
}
return unit.getCanonicalName();
} finally {
_psuReadLock.unlock();
}
}
/**
* Picks links associated with a unit (elementary rule).
*
* @param unit
* The unit as the matching criteria
* @param linkGroup
* Use only subset of links if defined, or all associated links
* if not defined (null)
* @return the matching links
*/
public Map<String, Link> match(Unit unit, LinkGroup linkGroup, DirectionType iotype) {
Map<String, Link> map = new HashMap<>();
_psuReadLock.lock();
try {
for (UGroup uGroup : unit._uGroupList.values()) {
for (Link link : uGroup._linkList.values()) {
if (linkGroup == null) {
if (iotype == DirectionType.READ
|| link.getLinkGroup() == null) {
//
// no link group specified
// only consider link if it isn't in any link group
// ( "default link group" )
//
_log.debug("link {} matching to unit {}", link.getName(), unit);
map.put(link.getName(), link);
}
} else if (linkGroup.contains(link)) {
//
// only take link if it is in the specified link group
//
_log.debug("link {} matching to unit {}", link.getName(), unit);
map.put(link.getName(), link);
}
}
}
} finally {
_psuReadLock.unlock();
}
return map;
}
public final static String hh_psu_set_allpoolsactive = "on|off";
public String ac_psu_set_allpoolsactive_$_1(Args args) throws CommandSyntaxException {
String mode = args.argv(0);
_psuWriteLock.lock();
try {
switch (mode) {
case "on":
case "true":
_allPoolsActive = true;
break;
case "off":
case "false":
_allPoolsActive = false;
break;
default:
throw new CommandSyntaxException("Syntax error");
}
} finally {
_psuWriteLock.unlock();
}
return "";
}
public final static String hh_psu_netmatch = "<host address>";
public String ac_psu_netmatch_$_1(Args args) throws UnknownHostException {
NetUnit unit = null;
_psuReadLock.lock();
try {
unit = _netHandler.match(args.argv(0));
} finally {
_psuReadLock.unlock();
}
if (unit == null) {
throw new IllegalArgumentException("Host not a unit : "
+ args.argv(0));
}
return unit.toString();
}
public final static String hh_psu_match = "[-linkGroup=<link group>] read|cache|write|p2p <store unit>|* <dCache unit>|* <net unit>|* <protocol unit>|* ";
public String ac_psu_match_$_5(Args args) throws Exception {
try {
long start = System.currentTimeMillis();
StorageInfo si = GenericStorageInfo.valueOf(args.argv(1), args.argv(2));
PoolPreferenceLevel[] list = match(args.argv(0).equals("*") ? DirectionType.ANY
: DirectionType.valueOf(args.argv(0).toUpperCase()),
args.argv(3).equals("*") ? null : args.argv(3), args
.argv(4).equals("*") ? null : args.argv(4), si,
args.getOpt("linkGroup"));
start = System.currentTimeMillis() - start;
StringBuilder sb = new StringBuilder();
for (int i = 0; i < list.length; i++) {
String tag = list[i].getTag();
sb.append("Preference : ").append(i).append("\n");
sb.append(" Tag : ").append(tag == null ? "NONE" : tag)
.append("\n");
for (String s : list[i].getPoolList()) {
sb.append(" ").append(s)
.append("\n");
}
}
sb.append("(time used : ").append(start).append(" millis)\n");
return sb.toString();
} catch (Exception ee) {
ee.printStackTrace();
throw ee;
}
}
public final static String hh_psu_match2 = "<unit> [...] [-net=<net unit>}";
public String ac_psu_match2_$_1_99(Args args)
{
StringBuilder sb = new StringBuilder();
Map<String, Link> map = null;
int required = args.argc();
_psuReadLock.lock();
try {
for (int i = 0; i < args.argc(); i++) {
String unitName = args.argv(i);
Unit unit = _units.get(unitName);
if (unit == null) {
throw new IllegalArgumentException("Unit not found : "
+ unitName);
}
// TODO:
map = match(map, unit, DirectionType.READ);
}
String netUnitName = args.getOpt("net");
if (netUnitName != null) {
Unit unit = _netHandler.find(new NetUnit(netUnitName));
if (unit == null) {
throw new IllegalArgumentException(
"Unit not found in netList : " + netUnitName);
}
// TODO:
map = match(map, unit, DirectionType.READ);
}
for (Link link : map.values()) {
if (link._uGroupList.size() != required) {
continue;
}
sb.append("Link : ").append(link.toString()).append("\n");
for(SelectionPool pool: link.getPools()) {
sb.append(" ").append(pool.getName()).append(
"\n");
}
}
} finally {
_psuReadLock.unlock();
}
return sb.toString();
}
// /////////////////////////////////////////////////////////////////////////////
//
// the CLI
//
// ..............................................................
//
// the create's
//
public final static String hh_psu_create_pgroup = "<pool group>";
public String ac_psu_create_pgroup_$_1(Args args) {
String name = args.argv(0);
_psuWriteLock.lock();
try {