|
20 | 20 | import org.elasticsearch.cluster.metadata.Metadata; |
21 | 21 | import org.elasticsearch.cluster.service.ClusterService; |
22 | 22 | import org.elasticsearch.common.Strings; |
23 | | -import org.elasticsearch.common.bytes.BytesReference; |
24 | 23 | import org.elasticsearch.core.TimeValue; |
25 | 24 | import org.elasticsearch.snapshots.SnapshotException; |
26 | 25 | import org.elasticsearch.snapshots.SnapshotInfo; |
27 | 26 | import org.elasticsearch.xcontent.ToXContent; |
28 | | -import org.elasticsearch.xcontent.XContentBuilder; |
29 | | -import org.elasticsearch.xcontent.json.JsonXContent; |
30 | 27 | import org.elasticsearch.xpack.core.ClientHelper; |
31 | 28 | import org.elasticsearch.xpack.core.scheduler.SchedulerEngine; |
32 | 29 | import org.elasticsearch.xpack.core.slm.SnapshotInvocationRecord; |
|
39 | 36 |
|
40 | 37 | import java.io.IOException; |
41 | 38 | import java.time.Instant; |
42 | | -import java.util.Collections; |
43 | 39 | import java.util.HashMap; |
44 | 40 | import java.util.Map; |
45 | 41 | import java.util.Optional; |
46 | 42 |
|
47 | | -import static org.elasticsearch.ElasticsearchException.REST_EXCEPTION_SKIP_STACK_TRACE; |
48 | | - |
49 | 43 | public class SnapshotLifecycleTask implements SchedulerEngine.Listener { |
50 | 44 |
|
51 | 45 | private static final Logger logger = LogManager.getLogger(SnapshotLifecycleTask.class); |
@@ -135,11 +129,6 @@ public void onResponse(CreateSnapshotResponse createSnapshotResponse) { |
135 | 129 | request.snapshot(), |
136 | 130 | "failed to create snapshot successfully, " + failures + " out of " + total + " total shards failed" |
137 | 131 | ); |
138 | | - // Add each failed shard's exception as suppressed, the exception contains |
139 | | - // information about which shard failed |
140 | | - // TODO: this seems wrong, investigate whether we actually need all the shard level exception here given that we |
141 | | - // could be dealing with tens of thousands of them at a time |
142 | | - snapInfo.shardFailures().forEach(e::addSuppressed); |
143 | 132 | // Call the failure handler to register this as a failure and persist it |
144 | 133 | onFailure(e); |
145 | 134 | } |
@@ -194,13 +183,17 @@ static Optional<SnapshotLifecyclePolicyMetadata> getSnapPolicyMetadata(final Str |
194 | 183 | ); |
195 | 184 | } |
196 | 185 |
|
| 186 | + public static String exceptionToString(Exception ex) { |
| 187 | + return Strings.toString((builder, params) -> { |
| 188 | + ElasticsearchException.generateThrowableXContent(builder, params, ex); |
| 189 | + return builder; |
| 190 | + }, ToXContent.EMPTY_PARAMS); |
| 191 | + } |
| 192 | + |
197 | 193 | /** |
198 | 194 | * A cluster state update task to write the result of a snapshot job to the cluster metadata for the associated policy. |
199 | 195 | */ |
200 | 196 | private static class WriteJobStatus extends ClusterStateUpdateTask { |
201 | | - private static final ToXContent.Params STACKTRACE_PARAMS = new ToXContent.MapParams( |
202 | | - Collections.singletonMap(REST_EXCEPTION_SKIP_STACK_TRACE, "false") |
203 | | - ); |
204 | 197 |
|
205 | 198 | private final String policyName; |
206 | 199 | private final String snapshotName; |
@@ -230,18 +223,6 @@ static WriteJobStatus failure(String policyId, String snapshotName, long timesta |
230 | 223 | return new WriteJobStatus(policyId, snapshotName, timestamp, timestamp, Optional.of(exception)); |
231 | 224 | } |
232 | 225 |
|
233 | | - private String exceptionToString() throws IOException { |
234 | | - if (exception.isPresent()) { |
235 | | - try (XContentBuilder causeXContentBuilder = JsonXContent.contentBuilder()) { |
236 | | - causeXContentBuilder.startObject(); |
237 | | - ElasticsearchException.generateThrowableXContent(causeXContentBuilder, STACKTRACE_PARAMS, exception.get()); |
238 | | - causeXContentBuilder.endObject(); |
239 | | - return BytesReference.bytes(causeXContentBuilder).utf8ToString(); |
240 | | - } |
241 | | - } |
242 | | - return null; |
243 | | - } |
244 | | - |
245 | 226 | @Override |
246 | 227 | public ClusterState execute(ClusterState currentState) throws Exception { |
247 | 228 | SnapshotLifecycleMetadata snapMeta = currentState.metadata().custom(SnapshotLifecycleMetadata.TYPE); |
@@ -274,7 +255,14 @@ public ClusterState execute(ClusterState currentState) throws Exception { |
274 | 255 |
|
275 | 256 | if (exception.isPresent()) { |
276 | 257 | stats.snapshotFailed(policyName); |
277 | | - newPolicyMetadata.setLastFailure(new SnapshotInvocationRecord(snapshotName, null, snapshotFinishTime, exceptionToString())); |
| 258 | + newPolicyMetadata.setLastFailure( |
| 259 | + new SnapshotInvocationRecord( |
| 260 | + snapshotName, |
| 261 | + null, |
| 262 | + snapshotFinishTime, |
| 263 | + exception.map(SnapshotLifecycleTask::exceptionToString).orElse(null) |
| 264 | + ) |
| 265 | + ); |
278 | 266 | } else { |
279 | 267 | stats.snapshotTaken(policyName); |
280 | 268 | newPolicyMetadata.setLastSuccess(new SnapshotInvocationRecord(snapshotName, snapshotStartTime, snapshotFinishTime, null)); |
|
0 commit comments