/
PoolInfoMap.java
1166 lines (1040 loc) · 43.5 KB
/
PoolInfoMap.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
/*
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.resilience.data;
import com.google.common.annotations.VisibleForTesting;
import com.google.common.collect.HashMultimap;
import com.google.common.collect.ImmutableList;
import com.google.common.collect.ImmutableMap;
import com.google.common.collect.ImmutableSet;
import com.google.common.collect.Multimap;
import com.google.common.collect.Sets;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import java.util.Collection;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;
import java.util.NoSuchElementException;
import java.util.Objects;
import java.util.Set;
import java.util.concurrent.locks.Lock;
import java.util.concurrent.locks.ReadWriteLock;
import java.util.concurrent.locks.ReentrantReadWriteLock;
import java.util.regex.Pattern;
import java.util.stream.Collectors;
import diskCacheV111.poolManager.CostModule;
import diskCacheV111.poolManager.PoolSelectionUnit;
import diskCacheV111.poolManager.PoolSelectionUnit.SelectionPool;
import diskCacheV111.poolManager.PoolSelectionUnit.SelectionPoolGroup;
import diskCacheV111.poolManager.PoolSelectionUnit.SelectionUnit;
import diskCacheV111.poolManager.StorageUnit;
import diskCacheV111.pools.PoolCostInfo;
import diskCacheV111.pools.PoolV2Mode;
import diskCacheV111.vehicles.PoolManagerPoolInformation;
import org.dcache.poolmanager.PoolInfo;
import org.dcache.poolmanager.PoolMonitor;
import org.dcache.resilience.handlers.PoolInfoChangeHandler;
import org.dcache.resilience.util.CopyLocationExtractor;
import org.dcache.resilience.util.MapInitializer;
import org.dcache.resilience.util.RandomSelectionStrategy;
import org.dcache.resilience.util.StorageUnitInfoExtractor;
import org.dcache.util.NonReindexableList;
import org.dcache.vehicles.FileAttributes;
/**
* <p>Serves as the central locus of pool-related information.</p>
*
* <p>The internal data structures hold a list of pool names and pool groups
* which will always assign a new index number to a new member even if
* some of the current members happen to be deleted via a PoolSelectionUnit
* operation.</p>
*
* <p>The reason for doing this is to be able to store most of the pool info
* associated with a given file or pool operation in progress as index
* references, allowing the {@link FileOperation} to use 4-byte rather
* than 8-byte 'references'.</p>
*
* <p>The relational tables represented by multimaps of indices
* capture pool and storage unit membership in pool groups. There are
* also three maps which define the resilience constraints for a given
* storage unit, the tags for a pool, and the live mode and cost
* information for a pool.</p>
*
* <p>This class also provides methods for determining the resilient group
* of a given pool, the storage groups connected to a given pool group,
* whether a pool or pool group is resilient, and whether a pool group can
* satisfy the constraints defined by the storage units bound to it.</p>
*
* <p>{@link #apply(PoolInfoDiff)}
* empties and rebuilds pool-related information based on a diff obtained
* from {@link #compare(PoolMonitor)}. An empty diff is equivalent to
* a load operation (at initialization). These methods are called by
* the {@link MapInitializer} at startup, and
* thereafter by the {@link PoolInfoChangeHandler}</p>
*
* <p>This class benefits from read-write synchronization, since there will
* be many more reads of what is for the most part stable information
* (note that the periodically refreshed information is synchronized
* within the PoolInformation object itself; hence changes
* to those values do not require a write lock; e.g., #updatePoolStatus.)</p>
*
* <p>Class is not marked final for stubbing/mocking purposes.</p>
*/
public class PoolInfoMap {
private static final Logger LOGGER = LoggerFactory.getLogger(
PoolInfoMap.class);
private static StorageUnitConstraints getConstraints(Integer storageUnit,
Map<Integer, ResilienceMarker> constraints) {
ResilienceMarker marker = constraints.get(storageUnit);
if (marker != null && !(marker instanceof StorageUnitConstraints)) {
String message = "Index " + storageUnit + " does not correspond "
+ "to a storage unit";
throw new NoSuchElementException(message);
}
StorageUnitConstraints sconstraints = (StorageUnitConstraints) marker;
int required;
Set<String> oneCopyPer;
if (sconstraints != null) {
required = sconstraints.getRequired();
oneCopyPer = sconstraints.getOneCopyPer();
} else {
/*
* This can occur if a link pointing to the pool group of this pool
* has been changed or removed, or a storage unit removed from
* the unit group associated with it. The policy here will
* be to consider these files non-existent for the purpose of
* replication. By leaving required = 1 we guarantee that
* the entry remains "invisible" to replication handling
* and no action will be taken.
*/
required = 1;
oneCopyPer = null;
}
return new StorageUnitConstraints(required, oneCopyPer);
}
private final List<String> pools = new NonReindexableList<>();
private final List<String> groups = new NonReindexableList<>();
private final List<String> sunits = new NonReindexableList<>();
private final Map<Integer, ResilienceMarker> markers = new HashMap<>();
private final Map<Integer, ResilienceMarker> constraints = new HashMap<>();
private final Map<Integer, PoolInformation> poolInfo = new HashMap<>();
private final Multimap<Integer, Integer> poolGroupToPool = HashMultimap.create();
private final Multimap<Integer, Integer> poolToPoolGroup = HashMultimap.create();
private final Multimap<Integer, Integer> storageToPoolGroup = HashMultimap.create();
private final Multimap<Integer, Integer> poolGroupToStorage = HashMultimap.create();
private final ReadWriteLock lock = new ReentrantReadWriteLock(true);
private final Lock write = lock.writeLock();
private final Lock read = lock.readLock();
private boolean useRegex = false;
/**
* <p>Called on a dedicated thread.</p>
*
* <p>Applies a diff under write lock.</p>
*
* <p>Will not clear the NonReindexable lists (pools, groups).
* This is to maintain the same indices for the duration of the
* life of the JVM, since the other maps may contain live references
* to pools or groups.</p>
*/
public void apply(PoolInfoDiff diff) {
write.lock();
try {
/*
* -- Remove stale pools, pool groups and storage units.
*
* Pool removal assumes that the pool has been properly drained
* of files first; this is not taken care of here. Similarly
* for the removal of pool groups.
*/
LOGGER.trace("removing stale pools, pool groups and storage units");
diff.getOldPools().stream().forEach(this::removePool);
diff.getOldGroups().stream().forEach(this::removeGroup);
diff.getOldUnits().stream().forEach(this::removeUnit);
/*
* -- Remove pools from current groups.
*/
LOGGER.trace("removing pools from pool groups");
diff.getPoolsRemovedFromPoolGroup().entries()
.forEach(this::removeFromPoolGroup);
/*
* -- Remove units from current groups.
*/
LOGGER.trace("removing units from pool groups");
diff.getUnitsRemovedFromPoolGroup().entries()
.forEach(this::removeStorageUnit);
/*
* -- Add new storage units, pool groups and pools.
*/
LOGGER.trace("adding new storage units, pool groups and pools");
diff.getNewUnits().stream().forEach(this::addStorageUnit);
diff.getNewGroups().stream().forEach(this::addPoolGroup);
diff.getNewPools().stream().forEach(pools::add);
/*
* -- Add units to pool groups.
*/
LOGGER.trace("adding storage units to pool groups");
diff.getUnitsAddedToPoolGroup().entries().stream()
.forEach(this::addUnitToPoolGroup);
/*
* -- Add pools to pool groups.
*/
LOGGER.trace("adding pools to pool groups");
diff.getPoolsAddedToPoolGroup().entries().stream()
.forEach(this::addPoolToPoolGroups);
/*
* -- Modify constraints.
*/
LOGGER.trace("modifying storage unit constraints");
diff.getConstraints().entrySet().stream()
.forEach(this::updateConstraints);
/*
* -- Add pool information for the new pools.
*
* The new pools are the only ones to have
* entries in the cost info map.
*/
LOGGER.trace("adding live pool information");
diff.getPoolCost().keySet().stream()
.forEach((p) ->
setPoolInfo(p, diff.getModeChanged().get(p),
diff.getTagsChanged().get(p),
diff.poolCost.get(p)));
} finally {
write.unlock();
}
}
/**
* <p>Called on dedicated thread.</p>
*
* <p>Does a diff under read lock.</p>
*
* <p>Gathers new pools, removed pools, new pool groups, removed pool
* groups, new storage units, removed storage units, modified groups
* and storage units, and pool information changes.</p>
*
* @param poolMonitor received from pool manager message.
*
* @return the diff (parsed out into relevant maps and collections).
*/
public PoolInfoDiff compare(PoolMonitor poolMonitor) {
read.lock();
PoolSelectionUnit psu = poolMonitor.getPoolSelectionUnit();
useRegex = psu.isEnabledRegex();
PoolInfoDiff diff = new PoolInfoDiff();
try {
LOGGER.trace("Searching for currently uninitialized pools.");
getUninitializedPools(diff);
LOGGER.trace("comparing pools");
Set<String> commonPools = comparePools(diff, psu);
LOGGER.trace("comparing pool groups");
Set<String> common = comparePoolGroups(diff, psu);
LOGGER.trace("adding pools and units to new pool groups");
addPoolsAndUnitsToNewPoolGroups(diff, psu);
LOGGER.trace("comparing pools in pool groups");
comparePoolsInPoolGroups(diff, common, psu);
LOGGER.trace("comparing storage units");
common = compareStorageUnits(diff, psu);
LOGGER.trace("adding pool groups for new storage units");
addPoolGroupsForNewUnits(diff, psu);
LOGGER.trace("comparing storage unit links and constraints");
compareStorageUnitLinksAndConstraints(diff, common, psu);
LOGGER.trace("comparing pool info");
comparePoolInfo(diff, commonPools, poolMonitor);
} finally {
read.unlock();
}
LOGGER.trace("Diff:\n{}", diff.toString());
return diff;
}
public int getCountableLocations(Collection<String> locations) {
read.lock();
int countable = 0;
try {
for (String location : locations) {
PoolInformation info = poolInfo.get(getPoolIndex(location));
if (info != null && info.isInitialized() && info.isCountable()) {
++countable;
}
}
} finally {
read.unlock();
}
return countable;
}
public Set<String> getExcludedLocationNames(Collection<String> members) {
read.lock();
try {
return members.stream()
.map(l -> poolInfo.get(getPoolIndex(l)))
.filter(Objects::nonNull)
.filter(PoolInformation::isInitialized)
.filter(PoolInformation::isExcluded)
.map(PoolInformation::getName)
.collect(Collectors.toSet());
} finally {
read.unlock();
}
}
public String getGroup(Integer group) {
read.lock();
try {
return groups.get(group);
} finally {
read.unlock();
}
}
public Integer getGroupIndex(String name) {
read.lock();
try {
return groups.indexOf(name);
} finally {
read.unlock();
}
}
public Set<String> getMemberLocations(Integer gindex,
Collection<String> locations) {
read.lock();
try {
Set<String> pools = getPools(getPoolsOfGroup(gindex));
return locations.stream().filter(pools::contains)
.collect(Collectors.toSet());
} finally {
read.unlock();
}
}
/**
* @param writable location is writable if true, readable if false.
* @return all pool group pools which qualify.
*/
public Set<String> getMemberPools(Integer gindex, boolean writable) {
read.lock();
try {
Set<Integer> members = ImmutableSet.copyOf(poolGroupToPool.get(gindex));
members = getValidLocations(members, writable);
return getPools(members);
} finally {
read.unlock();
}
}
public String getPool(Integer pool) {
read.lock();
try {
return pools.get(pool);
} finally {
read.unlock();
}
}
public Collection<Integer> getPoolGroupsFor(String storageUnit) {
Integer uindex = getUnitIndex(storageUnit);
if (uindex == null) {
return ImmutableList.of();
}
read.lock();
try {
return storageToPoolGroup.get(uindex);
} finally {
read.unlock();
}
}
public Integer getPoolIndex(String name) {
read.lock();
try {
return pools.indexOf(name);
} finally {
read.unlock();
}
}
public Set<Integer> getPoolIndices(Collection<String> locations) {
read.lock();
try {
return locations.stream()
.map(pools::indexOf)
.collect(Collectors.toSet());
} finally {
read.unlock();
}
}
public PoolInformation getPoolInformation(Integer index) {
read.lock();
try {
return poolInfo.get(index);
} finally {
read.unlock();
}
}
public PoolManagerPoolInformation getPoolManagerInfo(Integer pool) {
read.lock();
try {
return new PoolManagerPoolInformation(pools.get(pool),
poolInfo.get(pool).getCostInfo());
} finally {
read.unlock();
}
}
public PoolStateUpdate getPoolState(String pool) {
return getPoolState(pool, null, null, null);
}
public PoolStateUpdate getPoolState(String pool, String storageUnit) {
return getPoolState(pool, null, null, storageUnit);
}
public PoolStateUpdate getPoolState(String pool, Integer addedTo,
Integer removedFrom) {
return getPoolState(pool, addedTo, removedFrom, null);
}
public PoolStateUpdate getPoolState(String pool, Integer addedTo,
Integer removedFrom,
String storageUnit) {
PoolInformation info = getPoolInformation(getPoolIndex(pool));
if (info != null) {
return new PoolStateUpdate(pool, info.getMode(), addedTo,
removedFrom, storageUnit);
}
/*
* No information. Treat it as UNINITIALIZED.
*/
return new PoolStateUpdate(pool, null, addedTo, removedFrom, storageUnit);
}
public Set<String> getPools(Collection<Integer> indices) {
read.lock();
try {
return indices.stream().map(pools::get).collect(Collectors.toSet());
} finally {
read.unlock();
}
}
public Collection<Integer> getPoolsOfGroup(Integer group) {
read.lock();
try {
return poolGroupToPool.get(group);
} finally {
read.unlock();
}
}
public Set<String> getReadableLocations(Collection<String> locations) {
read.lock();
try {
return locations.stream()
.filter((l) -> isPoolViable(getPoolIndex(l), false))
.collect(Collectors.toSet());
} finally {
read.unlock();
}
}
public Integer getResilientPoolGroup(Integer pool) {
read.lock();
try {
Set<Integer> rgroups
= poolToPoolGroup.get(pool)
.stream()
.filter(this::isResilientGroup)
.collect(Collectors.toSet());
if (rgroups.size() == 0) {
return null;
}
if (rgroups.size() > 1) {
throw new IllegalStateException(String.format(
"Pool map is inconsistent; pool %s belongs to "
+ "more than one resilient "
+ "group: %s.",
pools.get(pool),
rgroups.stream()
.map(groups::get)
.collect(Collectors.toSet())));
}
return rgroups.iterator().next();
} finally {
read.unlock();
}
}
public Set<String> getResilientPools() {
read.lock();
try {
return pools.stream()
.filter(this::isResilientPool)
.collect(Collectors.toSet());
} finally {
read.unlock();
}
}
public StorageUnitConstraints getStorageUnitConstraints(Integer unit) {
return getConstraints(unit, constraints);
}
public Integer getStorageUnitIndex(FileAttributes attributes) {
String classKey = attributes.getStorageClass();
String unitKey = classKey;
String hsm = attributes.getHsm();
if (hsm != null) {
unitKey += ("@" + hsm);
}
try {
return getUnitIndex(unitKey);
} catch (NoSuchElementException e) {
return resolveStorageUnitIndex(classKey, unitKey);
}
}
public Collection<Integer> getStorageUnitsFor(String poolGroup) {
Integer gindex = getGroupIndex(poolGroup);
if (gindex == null) {
return ImmutableList.of();
}
read.lock();
try {
return poolGroupToStorage.get(gindex);
} finally {
read.unlock();
}
}
public Map<String, String> getTags(Integer pool) {
PoolInformation info = getPoolInformation(pool);
if (info == null) {
return ImmutableMap.of();
}
Map<String, String> tags = info.getTags();
if (tags == null) {
return ImmutableMap.of();
}
return tags;
}
public String getUnit(Integer index) {
read.lock();
try {
return sunits.get(index);
} finally {
read.unlock();
}
}
public Integer getUnitIndex(String name) {
read.lock();
try {
return sunits.indexOf(name);
} finally {
read.unlock();
}
}
public Set<Integer> getValidLocations(Collection<Integer> locations,
boolean writable) {
read.lock();
try {
return locations.stream()
.filter((i) -> isPoolViable(i, writable))
.collect(Collectors.toSet());
} finally {
read.unlock();
}
}
public boolean hasPool(String pool) {
read.lock();
try {
return pools.contains(pool);
} finally {
read.unlock();
}
}
public boolean isValidPoolIndex(Integer index) {
String pool = null;
try {
pool = getPool(index);
} catch (NoSuchElementException e) {
}
return pool != null;
}
public boolean isPoolViable(Integer pool, boolean writable) {
read.lock();
try {
PoolInformation info = poolInfo.get(pool);
return info != null && info.isInitialized()
&& (writable ? info.canRead() && info.canWrite()
: info.canRead());
} finally {
read.unlock();
}
}
public boolean isResilientGroup(Integer gindex) {
read.lock();
try {
return markers.get(gindex).isResilient();
} finally {
read.unlock();
}
}
public boolean isResilientPool(String pool) {
try {
return getResilientPoolGroup(getPoolIndex(pool)) != null;
} catch (NoSuchElementException e) {
return false;
}
}
public boolean isInitialized(String pool) {
try {
PoolInformation info = getPoolInformation(getPoolIndex(pool));
return info != null && info.isInitialized();
} catch (NoSuchElementException e) {
return false;
}
}
public String listPoolInfo(PoolInfoFilter poolInfoFilter) {
final StringBuilder builder = new StringBuilder();
read.lock();
try {
pools.stream()
.map(this::getPoolIndex)
.map(poolInfo::get)
.filter(poolInfoFilter::matches)
.forEach((i) -> builder.append(i).append("\n"));
} finally {
read.unlock();
}
return builder.toString();
}
/**
* All unit tests are synchronous, so there is no need to lock
* the map here.
*/
@VisibleForTesting
public void setUnitConstraints(String group, Integer required,
Collection<String> oneCopyPer) {
constraints.put(sunits.indexOf(group),
new StorageUnitConstraints(required, oneCopyPer));
}
public void updatePoolStatus(PoolStateUpdate update) {
read.lock();
try {
poolInfo.get(pools.indexOf(update.pool)).updateState(update);
} finally {
read.unlock();
}
}
/**
* <p>A coarse-grained verification that the required and tag constraints
* of the pool group and its associated storage groups can be met.
* For the default and each storage unit, it attempts to fulfill the
* max independent location requirement via the
* {@link CopyLocationExtractor}.</p>
*
* @throws IllegalStateException upon encountering the first set of
* constraints which cannot be met.
*/
public void verifyConstraints(Integer pgindex)
throws IllegalStateException {
Collection<Integer> storageGroups;
CopyLocationExtractor extractor;
read.lock();
try {
storageGroups = poolGroupToStorage.get(pgindex);
for (Integer index : storageGroups) {
StorageUnitConstraints unitConstraints
= (StorageUnitConstraints) constraints.get(index);
int required = unitConstraints.getRequired();
extractor = new CopyLocationExtractor(unitConstraints.getOneCopyPer(),
this);
verify(pgindex, extractor, required);
}
} finally {
read.unlock();
}
}
/** Called under write lock **/
private void addPoolGroup(SelectionPoolGroup group) {
String name = group.getName();
groups.add(name);
markers.put(groups.indexOf(name),
new ResilienceMarker(group.isResilient()));
}
/** Called under write lock **/
private void addPoolGroupsForNewUnits(PoolInfoDiff diff,
PoolSelectionUnit psu) {
Collection<StorageUnit> newUnits = diff.getNewUnits();
for (StorageUnit unit : newUnits) {
String name = unit.getName();
StorageUnitInfoExtractor.getResilientGroupsFor(name, psu)
.stream()
.forEach((g) -> diff.unitsAdded.put(g, name));
}
}
/** Called under write lock **/
private void addPoolToPoolGroups(Entry<String, String> entry) {
Integer pindex = pools.indexOf(entry.getKey());
Integer gindex = groups.indexOf(entry.getValue());
poolGroupToPool.put(gindex, pindex);
poolToPoolGroup.put(pindex, gindex);
}
/** Called under write lock **/
private void addPoolsAndUnitsToNewPoolGroups(PoolInfoDiff diff,
PoolSelectionUnit psu) {
Collection<SelectionPoolGroup> newGroups = diff.getNewGroups();
for (SelectionPoolGroup group : newGroups) {
String name = group.getName();
psu.getPoolsByPoolGroup(name)
.stream()
.map(SelectionPool::getName)
.forEach((p) -> diff.poolsAdded.put(p, name));
StorageUnitInfoExtractor.getStorageUnitsInGroup(name, psu)
.stream()
.forEach((u) -> diff.unitsAdded.put(name, u.getName()));
}
}
/** Called under write lock **/
private void addStorageUnit(StorageUnit unit) {
String name = unit.getName();
sunits.add(name);
constraints.put(sunits.indexOf(name),
new StorageUnitConstraints(unit.getRequiredCopies(),
unit.getOnlyOneCopyPer()));
}
/** Called under write lock **/
private void addUnitToPoolGroup(Entry<String, String> entry) {
Integer gindex = groups.indexOf(entry.getKey());
Integer sindex = sunits.indexOf(entry.getValue());
storageToPoolGroup.put(sindex, gindex);
poolGroupToStorage.put(gindex, sindex);
}
/** Called under read lock **/
private Set<String> comparePoolGroups(PoolInfoDiff diff,
PoolSelectionUnit psu) {
Set<String> next = psu.getPoolGroups().values()
.stream()
.map(SelectionPoolGroup::getName)
.collect(Collectors.toSet());
Set<String> curr = poolGroupToPool.keySet()
.stream()
.map(this::getGroup)
.collect(Collectors.toSet());
Sets.difference(next, curr) .stream()
.map((name) -> psu.getPoolGroups().get(name))
.forEach(diff.newGroups::add);
Sets.difference(curr, next) .stream()
.forEach(diff.oldGroups::add);
return Sets.intersection(next, curr);
}
/** Called under read lock **/
private void comparePoolInfo(PoolInfoDiff diff, Set<String> commonPools,
PoolMonitor poolMonitor) {
PoolSelectionUnit psu = poolMonitor.getPoolSelectionUnit();
CostModule costModule = poolMonitor.getCostModule();
/*
* First add the info for all new pools to the diff.
*/
diff.getNewPools().stream()
.forEach((p) -> {
diff.getModeChanged().put(p, getPoolMode(psu.getPool(p)));
diff.getTagsChanged().put(p, getPoolTags(p, costModule));
diff.getPoolCost().put(p, getPoolCostInfo(p, costModule));
});
/*
* Now check for differences with current pools that are still valid.
*/
commonPools.stream()
.forEach((p) -> {
PoolInformation info = poolInfo.get(getPoolIndex(p));
PoolV2Mode newMode = getPoolMode(psu.getPool(p));
PoolV2Mode oldMode = info.getMode();
if (oldMode == null || (newMode != null
&& !oldMode.equals(newMode))) {
diff.getModeChanged().put(p, newMode);
}
ImmutableMap<String, String> newTags
= getPoolTags(p, costModule);
ImmutableMap<String, String> oldTags = info.getTags();
if (oldTags == null || (newTags != null
&& !oldTags.equals(newTags))) {
diff.getTagsChanged().put(p, newTags);
}
/*
* Since we are not altering the actual collections inside
* the PoolInfoMap, but are simply modifying the PoolInformation
* object, and since its own update method is synchronized,
* we can take care of the update here while holding a read lock.
*/
info.update(newMode, newTags, getPoolCostInfo(p, costModule));
});
}
/** Called under read lock **/
private Set<String> comparePools(PoolInfoDiff diff,
PoolSelectionUnit psu) {
Set<String> next = psu.getPools().values()
.stream()
.map(SelectionPool::getName)
.collect(Collectors.toSet());
Set<String> curr = ImmutableSet.copyOf(pools);
Sets.difference(next, curr).stream().forEach(diff.newPools::add);
Sets.difference(curr, next).stream().forEach(diff.oldPools::add);
return Sets.intersection(curr, next);
}
/** Called under read lock **/
private void comparePoolsInPoolGroups(PoolInfoDiff diff,
Set<String> common,
PoolSelectionUnit psu) {
for (String group : common) {
Set<String> next = psu.getPoolsByPoolGroup(group)
.stream()
.map(SelectionPool::getName)
.collect(Collectors.toSet());
Set<String> curr = poolGroupToPool.get(groups.indexOf(group))
.stream()
.map(this::getPool)
.collect(Collectors.toSet());
Sets.difference(next, curr)
.stream()
.forEach((p) -> diff.poolsAdded.put(p, group));
Sets.difference(curr, next)
.stream()
.filter((p) -> !diff.oldPools.contains(p))
.forEach((p) -> diff.poolsRmved.put(p, group));
}
}
/** Called under read lock **/
private void compareStorageUnitLinksAndConstraints(PoolInfoDiff diff,
Set<String> common,
PoolSelectionUnit psu) {
for (String unit : common) {
StorageUnit storageUnit = psu.getStorageUnit(unit);
int index = sunits.indexOf(unit);
Set<String> next = ImmutableSet
.copyOf(StorageUnitInfoExtractor
.getPoolGroupsFor(unit,
psu,
false));
Set<String> curr = storageToPoolGroup.get(index)
.stream()
.map(this::getGroup)
.collect(Collectors.toSet());
Sets.difference(next, curr)
.stream()
.forEach((group) -> diff.unitsAdded.put(group, unit));
Sets.difference(curr, next)
.stream()
.filter((group) -> !diff.oldGroups.contains(group))
.forEach((group) -> diff.unitsRmved.put(group, unit));
int required = storageUnit.getRequiredCopies();
Set<String> oneCopyPer
= ImmutableSet.copyOf(storageUnit.getOnlyOneCopyPer());
StorageUnitConstraints constraints
= (StorageUnitConstraints) this.constraints.get(index);
if (required != constraints.getRequired() ||
!oneCopyPer.equals(constraints.getOneCopyPer())) {
diff.constraints.put(unit,
new StorageUnitConstraints(required,
oneCopyPer));
}
}
}
/** Called under read lock **/
private Set<String> compareStorageUnits(PoolInfoDiff diff,
PoolSelectionUnit psu) {
Set<String> next = psu.getSelectionUnits().values()
.stream()
.filter(StorageUnit.class::isInstance)
.map(SelectionUnit::getName)
.collect(Collectors.toSet());
Set<String> curr = storageToPoolGroup.keySet()
.stream()
.map(this::getUnit)
.collect(Collectors.toSet());
Sets.difference(next, curr)
.stream()
.map(psu::getStorageUnit)
.forEach(diff.newUnits::add);
Sets.difference(curr, next)
.stream().forEach( diff.oldUnits::add);
return Sets.intersection(next, curr);
}
private PoolV2Mode getPoolMode(SelectionPool pool) {
/*
* Allow a NULL value
*/
return pool.getPoolMode();