-
Notifications
You must be signed in to change notification settings - Fork 131
/
PoolOperation.java
248 lines (216 loc) · 9.5 KB
/
PoolOperation.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
/*
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 org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import diskCacheV111.util.CacheException;
import org.dcache.resilience.util.ExceptionMessage;
import org.dcache.resilience.util.PoolScanTask;
/**
* <p>Object stored in the {@link PoolOperationMap}.</p>
*/
public final class PoolOperation {
private static final Logger LOGGER = LoggerFactory.getLogger(PoolOperation.class);
private static final String TO_STRING = "(completed: %s / %s : %s%%) – "
+ "(updated: %s)(scanned: %s)(prev %s)(curr %s)(%s) %s";
enum State {
IDLE, // NEXT OPERATION READY TO RUN
WAITING, // OPERATION SUBMITTED TO THE EXECUTOR
RUNNING, // OPERATION IS ACTIVE
CANCELED, // OPERATION WAS TERMINATED BY USER
FAILED, // OPERATION FAILED WITH EXCEPTION
EXCLUDED // POOL EXCLUDED FROM OPERATIONS
}
enum NextAction {
NOP, // REMAIN IN CURRENT QUEUE
UP_TO_DOWN, // PROMOTE TO WAITING
DOWN_TO_UP // CANCEL ANY CURRENT DOWN AND PROMOTE TO WAITING
}
boolean forceScan; // Overrides non-handling of restarts
long lastUpdate;
long lastScan;
Integer group; // Only set when the psuAction != NONE
Integer unit; // Set when unit has changed, or scan
// is periodic or initiated by command
State state;
boolean newPool;
PoolStatusForResilience lastStatus;
PoolStatusForResilience currStatus;
PoolScanTask task;
CacheException exception;
private int children;
private int completed;
private int failed;
PoolOperation() {
forceScan = false;
group = null;
unit = null;
state = State.IDLE;
lastUpdate = System.currentTimeMillis();
lastScan = lastUpdate;
lastStatus = PoolStatusForResilience.UNINITIALIZED;
currStatus = PoolStatusForResilience.UNINITIALIZED;
children = 0;
completed = 0;
failed = 0;
}
public String toString() {
return String.format(TO_STRING,
completed,
children == 0 && completed > 0 ? "?" : children,
getFormattedPercentDone(),
FileOperation.getFormattedDateFromMillis(lastUpdate),
FileOperation.getFormattedDateFromMillis(lastScan),
lastStatus, currStatus, state,
exception == null ? getFailedMessage() :
new ExceptionMessage(exception));
}
public synchronized boolean isExcluded() {
return state == State.EXCLUDED;
}
/**
* <p>Provides a transition table for determining what to do when
* a successive status change notification is received.</p>
*/
synchronized NextAction getNextAction(PoolStatusForResilience incoming) {
if (state == State.EXCLUDED) {
currStatus = incoming;
return NextAction.NOP;
}
lastStatus = currStatus;
currStatus = incoming;
switch(lastStatus) {
case DOWN:
switch (currStatus) {
case READ_ONLY:
case ENABLED:
return NextAction.DOWN_TO_UP;
case DOWN:
return NextAction.NOP;
}
case READ_ONLY:
case ENABLED:
case UNINITIALIZED:
/*
* The transition from UNINITIALIZED to another
* state occurs when the resilience service comes on line,
* whether simultaneously with pool initialization or
* not. The probability of pools going down at initialization
* is low, but it is possible that resilience could go down
* and then restart to find a number of pools down. We
* therefore have to consider the uninitialized-to-down
* transition as actionable. On the other hand, it is
* less crucial to handle restarts from this initial state,
* and preferable not to, since the majority of pools will
* most of the time initialize to a viable readable status,
* and handling this transition will unnecessarily provoke
* an immediate system-wide scan.
*/
switch (currStatus) {
case DOWN:
return NextAction.UP_TO_DOWN;
case READ_ONLY:
case ENABLED:
return NextAction.NOP;
}
default:
return NextAction.NOP;
}
}
synchronized void incrementCompleted(boolean failed) {
LOGGER.trace("entering incrementCompleted, state {}, failed {}, "
+ "children {}, completed = {}.",
state, failed, children, completed );
if (state == State.RUNNING) {
++completed;
if (failed) {
++this.failed;
}
}
LOGGER.trace("leaving incrementCompleted, state {}, failed {}, "
+ "children {}, completed = {}.",
state, children, completed );
}
synchronized boolean isComplete() {
boolean isComplete = children > 0 && children == completed;
LOGGER.trace("isComplete {}, children {}, completed = {}.",
isComplete, children, completed );
return isComplete;
}
synchronized int failedChildren() {
return failed;
}
synchronized void resetChildren() {
children = 0;
completed = 0;
}
synchronized void resetFailed() {
failed = 0;
}
synchronized void setChildren(int children) {
if (state == State.RUNNING) {
this.children = children;
}
}
private String getFailedMessage() {
return failed == 0 ? "" : failed + " file operations failed";
}
private String getFormattedPercentDone() {
String percent = children == 0 ?
"?" :
(children == completed ? "100" :
String.format("%.1f", 100 * (double) completed
/ (double) children));
if ("100.0".equals(percent)) {
return "99.9";
}
return percent;
}
}