-
Notifications
You must be signed in to change notification settings - Fork 131
/
FileUpdate.java
419 lines (363 loc) · 16.3 KB
/
FileUpdate.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
/*
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 org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import java.sql.Connection;
import java.util.ArrayList;
import java.util.Collection;
import java.util.NoSuchElementException;
import diskCacheV111.util.AccessLatency;
import diskCacheV111.util.CacheException;
import diskCacheV111.util.FileNotFoundCacheException;
import diskCacheV111.util.PnfsId;
import org.dcache.resilience.data.PoolOperation.SelectionAction;
import org.dcache.resilience.db.LocalNamespaceAccess;
import org.dcache.resilience.db.NamespaceAccess;
import org.dcache.resilience.db.ScanSummary;
import org.dcache.resilience.handlers.FileOperationHandler;
import org.dcache.resilience.handlers.ResilienceMessageHandler;
import org.dcache.resilience.util.ExceptionMessage;
import org.dcache.resilience.util.LocationSelector;
import org.dcache.vehicles.FileAttributes;
import static org.dcache.resilience.data.MessageType.ADD_CACHE_LOCATION;
import static org.dcache.resilience.data.MessageType.CLEAR_CACHE_LOCATION;
import static org.dcache.resilience.data.MessageType.CORRUPT_FILE;
import static org.dcache.resilience.data.MessageType.POOL_STATUS_DOWN;
import static org.dcache.resilience.data.MessageType.POOL_STATUS_UP;
/**
* <p>A transient encapsulation of pertinent configuration data regarding
* a file location.</p>
*
* <p>Implements verification/validation methods to determine if the update
* should be registered in the operation map or not.</p>
*
* @see FileOperationMap#register(FileUpdate)
* @see FileOperationHandler#handleLocationUpdate(FileUpdate)
* @see ResilienceMessageHandler#updatePnfsLocation(FileUpdate)
* @see LocalNamespaceAccess#handleQuery(Connection, ScanSummary)
*/
public final class FileUpdate {
private static final Logger LOGGER = LoggerFactory.getLogger(
FileUpdate.class);
/**
* @return <code>null</code> if AccessLatency is not ONLINE, or if the file
* is not found or there are no locations for it and the
* message being processed is for a clear cache location; otherwise
* the file attribute set required to process resilience.
*/
public static FileAttributes getAttributes(PnfsId pnfsId, String pool,
MessageType messageType,
NamespaceAccess namespace)
throws CacheException {
try {
FileAttributes attributes = namespace.getRequiredAttributes(pnfsId);
if (attributes == null) {
throw new FileNotFoundCacheException(String.format("No attributes "
+ "returned for %s", pnfsId));
}
LOGGER.trace("Got required attributes for {}.", pnfsId);
if (!attributes.getAccessLatency().equals(AccessLatency.ONLINE)) {
LOGGER.trace("AccessLatency of {} is not ONLINE; ignoring ...",
pnfsId);
return null;
}
if (attributes.getLocations().isEmpty()) {
if (messageType == CLEAR_CACHE_LOCATION) {
LOGGER.trace("ClearCacheLocationMessage for {}; "
+ "no current locations; "
+ "file probably deleted "
+ "from namespace.",
pnfsId);
return null;
}
if (messageType != ADD_CACHE_LOCATION) {
/*
* Since the scan began or the broken file reported,
* the file has been removed.
*/
throw new FileNotFoundCacheException
(String.format("File no longer found: %s"
, pnfsId));
}
/*
* May be due to a race between PnfsManager and resilience
* to process the message into/from the namespace.
*
* We can assume here that this is a new file, so
* we just add the originating location to the attribute
* location list.
*/
LOGGER.trace("{} has no locations yet.", pnfsId);
Collection<String> singleLoc = new ArrayList<>();
singleLoc.add(pool);
attributes.setLocations(singleLoc);
}
LOGGER.debug("After call to namespace, {} has locations {}.",
pnfsId,
attributes.getLocations());
return attributes;
} catch (FileNotFoundCacheException e) {
LOGGER.debug("{}; {} has likely been deleted from the namespace.",
e.getMessage(),
pnfsId);
return null;
}
}
public final PnfsId pnfsId;
public final String pool;
public final MessageType type;
public final SelectionAction action;
private final boolean isFullScan;
private Integer group;
private FileAttributes attributes;
private int poolIndex;
private Integer unitIndex;
private Integer count;
private boolean fromReload;
@VisibleForTesting
public FileUpdate(PnfsId pnfsId,
String pool,
MessageType type,
Integer poolIndex,
Integer groupIndex,
Integer unitIndex,
FileAttributes attributes) {
this(pnfsId, pool, type, SelectionAction.NONE, groupIndex, true);
this.poolIndex = poolIndex;
this.unitIndex = unitIndex;
this.attributes = attributes;
}
public FileUpdate(PnfsId pnfsId, String pool, MessageType type, boolean full) {
this(pnfsId, pool, type, SelectionAction.NONE, null, full);
}
/**
* @param pnfsId of the file.
* @param pool either the source of the message, or the pool being scanned.
* @param type CORRUPT_FILE, CLEAR_CACHE_LOCATION, ADD_CACHE_LOCATION,
* POOL_STATUS_DOWN, or POOL_STATUS_UP.
* @param action from PoolSelectionUnit (ADD, REMOVE, MODIFY, NONE).
* @param group of the pool, if action is not NONE or MODIFY
* (can be <code>null</code>).
* @param full if true, set the op count to the computed difference
* between required and readable; otherwise,
* set the op count to 1.
*/
public FileUpdate(PnfsId pnfsId, String pool, MessageType type,
SelectionAction action, Integer group, boolean full) {
this.pnfsId = pnfsId;
this.pool = pool;
this.type = type;
this.action = action;
this.group = group;
fromReload = false;
isFullScan = full;
}
public FileAttributes getAttributes() {
return attributes;
}
public Integer getCount() {
return count;
}
public Integer getGroup() {
return group;
}
public int getPoolIndex() {
return poolIndex;
}
public int getSelectionAction() {
return action.ordinal();
}
public long getSize() { return attributes.getSize(); }
public Integer getUnitIndex() {
return unitIndex;
}
public Integer getSourceIndex() {
return type == CORRUPT_FILE ||
type == CLEAR_CACHE_LOCATION ? null : poolIndex;
}
public boolean isFromReload() {
return fromReload;
}
public boolean isParent() {
return type == POOL_STATUS_DOWN || type == POOL_STATUS_UP;
}
public void setCount(Integer count) {
this.count = count;
}
public void setFromReload(boolean fromReload) {
this.fromReload = fromReload;
}
public boolean shouldVerifySticky() {
return !isFromReload() && type != CLEAR_CACHE_LOCATION &&
(!isParent() || action == SelectionAction.ADD);
}
public String toString() {
return String.format(
"(%s)(%s)(%s)(parent %s)(source %s)"
+ "(psu action %s)(group %s)(count %s)",
pnfsId, pool, type, isParent(), getSourceIndex(),
action, group, count);
}
public boolean validateAttributes(NamespaceAccess access)
throws CacheException {
LOGGER.trace("validateAttributes for {}", this);
attributes = getAttributes(pnfsId, pool, type, access);
LOGGER.trace("validateAttributes, {}", attributes);
return attributes != null;
}
public boolean validateForAction(Integer storageUnit,
PoolInfoMap poolInfoMap,
LocationSelector locationSelector) {
/*
* Storage unit is not recorded in checkpoint, so it should
* be set here.
*/
try {
unitIndex = poolInfoMap.getStorageUnitIndex(attributes);
} catch (NoSuchElementException e) {
LOGGER.error("validateForAction, cannot handle {}: {}.",
pnfsId, new ExceptionMessage(e));
return false;
}
LOGGER.trace("validateForAction {} got unit from attributes {}.",
pnfsId, unitIndex);
/*
* Check to see if this is from a reload of the checkpoint record.
* If so, the operation count should be non-null.
*/
if (fromReload) {
LOGGER.debug("validateForAction, data was reloaded, restoredCount {}",
count);
return count > 0;
}
StorageUnitConstraints constraints
= poolInfoMap.getStorageUnitConstraints(unitIndex);
Collection<String> locations
= poolInfoMap.getMemberLocations(group,
attributes.getLocations());
/*
* This indicates that all the locations for the file do not belong
* to the given pool group. This could happen if all locations
* that were once part of the group are removed. In this case,
* the operation is a NOP.
*/
if (locations.isEmpty()) {
return false;
}
int required = constraints.getRequired();
/*
* Force the file operation into the table if the storage unit matches
* the modified one, or if this is a periodic or admin initiated scan.
*
* In the former case, the scan was triggered by a change in storage
* unit requirements. This could be from an altered number of replicas,
* or from a change in tag partitioning; even if the required number
* of copies exist, they may need to be removed and recopied if the
* tags have changed.
*
* The count must thus be the minimum necessary for the worst case
* scenario -- that is, remove all but one replica and recopy to
* the required number. If it turns out this number is more than
* what is actually needed, the file operation will void itself at
* that point and quit.
*/
if (storageUnit == ScanSummary.ALL_UNITS || unitIndex.equals(storageUnit)) {
/*
* The maximum number of steps required to redistribute all files
* would be (N - 1) removes + (required - 1) copies, where N
* is the max of required and current locations.
*/
count = Math.max(required, locations.size()) + required - 2;
return true;
}
/*
* Check the constraints.
* Countable means readable OR intentionally excluded locations.
* If there are copies missing only from excluded locations,
* do nothing.
*
* NOTE. An initial check for consistency with the pools is
* avoided here so as not to block this thread on a
* send and wait. The locations will be reverified as
* part of the operation logic. While this means
* operations could unnecessarily be created, it
* ensures more efficient use of the thread resources.
*/
int countable = poolInfoMap.getCountableLocations(locations);
count = required - countable;
LOGGER.debug("validateForAction ({} needs {} replicas, locations {}, "
+ "{} countable; difference = {}.",
pnfsId, required,
locations, countable, count);
if (count == 0) {
LOGGER.debug("{}, requirements are already met.", pnfsId);
return false;
}
/*
* Multiple copies per update are set only when the file is a new
* entry in the namespace. A pool status change or clear cache
* location message will trigger only a single migration or single
* remove.
*/
count = isFullScan ? Math.abs(count) : 1;
LOGGER.trace("validateForAction, computed count as {}", count);
return true;
}
public boolean verifyPoolGroup(PoolInfoMap poolInfoMap) {
poolIndex = poolInfoMap.getPoolIndex(pool);
if (group != null) {
return poolInfoMap.isResilientGroup(group);
}
group = poolInfoMap.getResilientPoolGroup(poolIndex);
return group != null;
}
}