35
35
import org .elasticsearch .snapshots .SnapshotInfo ;
36
36
import org .elasticsearch .snapshots .SnapshotMissingException ;
37
37
import org .elasticsearch .snapshots .SnapshotsService ;
38
+ import org .elasticsearch .tasks .CancellableTask ;
39
+ import org .elasticsearch .tasks .Task ;
40
+ import org .elasticsearch .tasks .TaskCancelledException ;
38
41
import org .elasticsearch .threadpool .ThreadPool ;
39
42
import org .elasticsearch .transport .TransportService ;
40
43
@@ -73,11 +76,19 @@ protected ClusterBlockException checkBlock(GetSnapshotsRequest request, ClusterS
73
76
}
74
77
75
78
@ Override
76
- protected void masterOperation (final GetSnapshotsRequest request , final ClusterState state ,
79
+ protected void masterOperation (GetSnapshotsRequest request , ClusterState state ,
80
+ ActionListener <GetSnapshotsResponse > listener ) throws Exception {
81
+ throw new UnsupportedOperationException ("The task parameter is required" );
82
+ }
83
+
84
+ @ Override
85
+ protected void masterOperation (final Task task , final GetSnapshotsRequest request , final ClusterState state ,
77
86
final ActionListener <GetSnapshotsResponse > listener ) {
78
87
final String repo = request .repository ();
79
88
final String [] snapshots = request .snapshots ();
80
89
final SnapshotsInProgress snapshotsInProgress = state .custom (SnapshotsInProgress .TYPE , SnapshotsInProgress .EMPTY );
90
+ assert task instanceof CancellableTask : task + " not cancellable" ;
91
+
81
92
final Map <String , SnapshotId > allSnapshotIds = new HashMap <>();
82
93
final List <SnapshotInfo > currentSnapshots = new ArrayList <>();
83
94
for (SnapshotInfo snapshotInfo : sortedCurrentSnapshots (snapshotsInProgress , repo )) {
@@ -94,7 +105,7 @@ protected void masterOperation(final GetSnapshotsRequest request, final ClusterS
94
105
}
95
106
96
107
repositoryDataListener .whenComplete (repositoryData -> loadSnapshotInfos (snapshotsInProgress , repo , snapshots ,
97
- request .ignoreUnavailable (), request .verbose (), allSnapshotIds , currentSnapshots , repositoryData ,
108
+ request .ignoreUnavailable (), request .verbose (), allSnapshotIds , currentSnapshots , repositoryData , ( CancellableTask ) task ,
98
109
listener .map (GetSnapshotsResponse ::new )), listener ::onFailure );
99
110
}
100
111
@@ -120,7 +131,12 @@ private static List<SnapshotInfo> sortedCurrentSnapshots(SnapshotsInProgress sna
120
131
private void loadSnapshotInfos (SnapshotsInProgress snapshotsInProgress , String repo , String [] snapshots ,
121
132
boolean ignoreUnavailable , boolean verbose , Map <String , SnapshotId > allSnapshotIds ,
122
133
List <SnapshotInfo > currentSnapshots , @ Nullable RepositoryData repositoryData ,
123
- ActionListener <List <SnapshotInfo >> listener ) {
134
+ CancellableTask task , ActionListener <List <SnapshotInfo >> listener ) {
135
+ if (task .isCancelled ()) {
136
+ listener .onFailure (new TaskCancelledException ("task cancelled" ));
137
+ return ;
138
+ }
139
+
124
140
if (repositoryData != null ) {
125
141
for (SnapshotId snapshotId : repositoryData .getSnapshotIds ()) {
126
142
allSnapshotIds .put (snapshotId .getName (), snapshotId );
@@ -156,7 +172,7 @@ private void loadSnapshotInfos(SnapshotsInProgress snapshotsInProgress, String r
156
172
157
173
if (verbose ) {
158
174
threadPool .generic ().execute (ActionRunnable .supply (
159
- listener , () -> snapshots (snapshotsInProgress , repo , new ArrayList <>(toResolve ), ignoreUnavailable )));
175
+ listener , () -> snapshots (snapshotsInProgress , repo , new ArrayList <>(toResolve ), ignoreUnavailable , task )));
160
176
} else {
161
177
final List <SnapshotInfo > snapshotInfos ;
162
178
if (repositoryData != null ) {
@@ -182,7 +198,10 @@ private void loadSnapshotInfos(SnapshotsInProgress snapshotsInProgress, String r
182
198
* @return list of snapshots
183
199
*/
184
200
private List <SnapshotInfo > snapshots (SnapshotsInProgress snapshotsInProgress , String repositoryName ,
185
- List <SnapshotId > snapshotIds , boolean ignoreUnavailable ) {
201
+ List <SnapshotId > snapshotIds , boolean ignoreUnavailable , CancellableTask task ) {
202
+ if (task .isCancelled ()) {
203
+ throw new TaskCancelledException ("task cancelled" );
204
+ }
186
205
final Set <SnapshotInfo > snapshotSet = new HashSet <>();
187
206
final Set <SnapshotId > snapshotIdsToIterate = new HashSet <>(snapshotIds );
188
207
// first, look at the snapshots in progress
@@ -196,6 +215,9 @@ private List<SnapshotInfo> snapshots(SnapshotsInProgress snapshotsInProgress, St
196
215
// then, look in the repository
197
216
final Repository repository = repositoriesService .repository (repositoryName );
198
217
for (SnapshotId snapshotId : snapshotIdsToIterate ) {
218
+ if (task .isCancelled ()) {
219
+ throw new TaskCancelledException ("task cancelled" );
220
+ }
199
221
try {
200
222
snapshotSet .add (repository .getSnapshotInfo (snapshotId ));
201
223
} catch (Exception ex ) {
0 commit comments