-
Notifications
You must be signed in to change notification settings - Fork 131
/
AsynchronousRedirectedTransfer.java
219 lines (191 loc) · 6.5 KB
/
AsynchronousRedirectedTransfer.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
package org.dcache.util;
import com.google.common.base.Throwables;
import com.google.common.util.concurrent.FutureCallback;
import com.google.common.util.concurrent.Futures;
import com.google.common.util.concurrent.ListenableFuture;
import javax.security.auth.Subject;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.Executor;
import java.util.concurrent.FutureTask;
import diskCacheV111.util.CacheException;
import diskCacheV111.util.FsPath;
import diskCacheV111.util.PnfsHandler;
import org.dcache.auth.attributes.Restriction;
import static com.google.common.base.Preconditions.checkState;
/**
* A transfer where the mover can send a redirect message to the door
* asynchronously.
*
* The transfer startup phase is identical to a regular Transfer, however notification of
* redirect and transfer completion is done asynchronously through callbacks. Subclasses
* are to implement onQueued, onRedirect, onFinish and onFailure. The class deals with out
* of order notifications and guarantees that:
*
* - onQueued is always called before onRedirect
* - onRedirect is always called before onFinish
* - that onRedirect and onFinish are not called once onFailure was called
*
* The class implements automatic killing of the mover in case the transfer is
* aborted.
*/
public abstract class AsynchronousRedirectedTransfer<T> extends Transfer
{
private final Executor executor;
private final Monitor monitor = new Monitor();
public AsynchronousRedirectedTransfer(Executor executor, PnfsHandler pnfs, Subject namespaceSubject, Restriction restriction, Subject subject, FsPath path) {
super(pnfs, namespaceSubject, restriction, subject, path);
this.executor = executor;
}
public AsynchronousRedirectedTransfer(Executor executor, PnfsHandler pnfs, Subject subject, Restriction restriction, FsPath path)
{
super(pnfs, subject, restriction, path);
this.executor = executor;
}
@Override
public ListenableFuture<Void> selectPoolAndStartMoverAsync(String queue, TransferRetryPolicy policy)
{
return monitor.setQueueFuture(super.selectPoolAndStartMoverAsync(queue, policy));
}
/**
* Signals that the transfer is redirected.
*/
public void redirect(T object)
{
executor.execute(() -> monitor.redirect(object));
}
/**
* Aborts the transfer unless already completed.
*
* This cancels pool selection and kills the mover. The onFailure callback is called. This method
* blocks until the callback completes.
*/
public void abort(Throwable t)
{
try {
FutureTask<Object> task = new FutureTask<>(() -> monitor.doAbort(t), null);
executor.execute(task);
task.get();
} catch (ExecutionException e) {
Throwables.propagate(e.getCause());
} catch (InterruptedException e) {
Thread.currentThread().interrupt();
}
}
@Override
public void finished(CacheException error)
{
super.finished(error);
executor.execute(() -> monitor.finished(error));
}
protected abstract void onQueued() throws Exception;
protected abstract void onRedirect(T object) throws Exception;
protected abstract void onFinish() throws Exception;
protected abstract void onFailure(Throwable t);
/**
* To avoid locking the monitor of the Transfer object during callbacks,
* we have an explicit monitor guarding our state machine and ensuring
* that callbacks are executed sequentially.
*/
private class Monitor
{
private T redirectObject;
private boolean isQueued;
private boolean isRedirected;
private boolean isFinished;
private boolean isDone;
private ListenableFuture<Void> queueFuture;
private synchronized ListenableFuture<Void> setQueueFuture(ListenableFuture<Void> future)
{
checkState(queueFuture == null);
queueFuture = future;
Futures.addCallback(future, new FutureCallback<Void>()
{
@Override
public void onSuccess(Void result)
{
executor.execute(monitor::doQueued);
}
@Override
public void onFailure(Throwable t)
{
executor.execute(() -> monitor.doAbort(t));
}
});
return future;
}
private synchronized void doQueued()
{
try {
isQueued = true;
if (isDone) {
doKill("transfer aborted");
} else {
onQueued();
doRedirect();
}
} catch (Exception e) {
doAbort(e);
}
}
private synchronized void doRedirect()
{
try {
if (!isDone && isQueued && isRedirected) {
onRedirect(redirectObject);
doFinish();
}
} catch (Exception e) {
doAbort(e);
}
}
private synchronized void doFinish()
{
try {
if (!isDone && isQueued && isRedirected && isFinished) {
onFinish();
isDone = true;
}
} catch (Exception e) {
doAbort(e);
}
}
private synchronized void doAbort(Throwable t)
{
if (!isDone) {
doKill(explain(t));
onFailure(t);
isDone = true;
}
}
protected String explain(Throwable t)
{
return String.valueOf(t);
}
private synchronized void doKill(String explanation)
{
if (queueFuture != null) {
queueFuture.cancel(true);
}
killMover(0, "killed by door: " + explanation);
}
/**
* Signals that the transfer is redirected.
*/
public synchronized void redirect(T object)
{
checkState(!isRedirected);
redirectObject = object;
isRedirected = true;
doRedirect();
}
public synchronized void finished(CacheException error)
{
if (error != null) {
doAbort(error);
} else {
isFinished = true;
doFinish();
}
}
}
}