|
| 1 | +/* |
| 2 | + * Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one |
| 3 | + * or more contributor license agreements. Licensed under the Elastic License |
| 4 | + * 2.0 and the Server Side Public License, v 1; you may not use this file except |
| 5 | + * in compliance with, at your election, the Elastic License 2.0 or the Server |
| 6 | + * Side Public License, v 1. |
| 7 | + */ |
| 8 | + |
| 9 | +package org.elasticsearch.cluster.metadata; |
| 10 | + |
| 11 | +import org.elasticsearch.cluster.Diff; |
| 12 | +import org.elasticsearch.cluster.SimpleDiffable; |
| 13 | +import org.elasticsearch.common.io.stream.StreamInput; |
| 14 | +import org.elasticsearch.common.io.stream.StreamOutput; |
| 15 | +import org.elasticsearch.xcontent.ConstructingObjectParser; |
| 16 | +import org.elasticsearch.xcontent.ParseField; |
| 17 | +import org.elasticsearch.xcontent.ToXContentFragment; |
| 18 | +import org.elasticsearch.xcontent.XContentBuilder; |
| 19 | +import org.elasticsearch.xcontent.XContentParser; |
| 20 | + |
| 21 | +import java.io.IOException; |
| 22 | +import java.util.List; |
| 23 | + |
| 24 | +import static org.elasticsearch.xcontent.ConstructingObjectParser.constructorArg; |
| 25 | + |
| 26 | +/** |
| 27 | + * A metadata class to hold error information about errors encountered |
| 28 | + * while applying a cluster state update for a given namespace. |
| 29 | + * <p> |
| 30 | + * This information is held by the {@link ImmutableStateMetadata} class. |
| 31 | + */ |
| 32 | +public record ImmutableStateErrorMetadata(Long version, ErrorKind errorKind, List<String> errors) |
| 33 | + implements |
| 34 | + SimpleDiffable<ImmutableStateErrorMetadata>, |
| 35 | + ToXContentFragment { |
| 36 | + |
| 37 | + static final ParseField ERRORS = new ParseField("errors"); |
| 38 | + static final ParseField VERSION = new ParseField("version"); |
| 39 | + static final ParseField ERROR_KIND = new ParseField("error_kind"); |
| 40 | + |
| 41 | + /** |
| 42 | + * Constructs an immutable state error metadata |
| 43 | + * |
| 44 | + * @param version the metadata version of the content which failed to apply |
| 45 | + * @param errorKind the kind of error we encountered while processing |
| 46 | + * @param errors the list of errors encountered during parsing and validation of the immutable state content |
| 47 | + */ |
| 48 | + public ImmutableStateErrorMetadata {} |
| 49 | + |
| 50 | + @Override |
| 51 | + public void writeTo(StreamOutput out) throws IOException { |
| 52 | + out.writeLong(version); |
| 53 | + out.writeString(errorKind.getKindValue()); |
| 54 | + out.writeCollection(errors, StreamOutput::writeString); |
| 55 | + } |
| 56 | + |
| 57 | + /** |
| 58 | + * Reads an {@link ImmutableStateErrorMetadata} from a {@link StreamInput} |
| 59 | + * |
| 60 | + * @param in the {@link StreamInput} to read from |
| 61 | + * @return {@link ImmutableStateErrorMetadata} |
| 62 | + * @throws IOException |
| 63 | + */ |
| 64 | + public static ImmutableStateErrorMetadata readFrom(StreamInput in) throws IOException { |
| 65 | + return new ImmutableStateErrorMetadata(in.readLong(), ErrorKind.of(in.readString()), in.readList(StreamInput::readString)); |
| 66 | + } |
| 67 | + |
| 68 | + @Override |
| 69 | + public XContentBuilder toXContent(XContentBuilder builder, Params params) throws IOException { |
| 70 | + builder.startObject(); |
| 71 | + builder.field(VERSION.getPreferredName(), version); |
| 72 | + builder.field(ERROR_KIND.getPreferredName(), errorKind.getKindValue()); |
| 73 | + builder.stringListField(ERRORS.getPreferredName(), errors); |
| 74 | + builder.endObject(); |
| 75 | + return builder; |
| 76 | + } |
| 77 | + |
| 78 | + @SuppressWarnings("unchecked") |
| 79 | + private static final ConstructingObjectParser<ImmutableStateErrorMetadata, Void> PARSER = new ConstructingObjectParser<>( |
| 80 | + "immutable_state_error_metadata", |
| 81 | + (a) -> new ImmutableStateErrorMetadata((Long) a[0], ErrorKind.of((String) a[1]), (List<String>) a[2]) |
| 82 | + ); |
| 83 | + |
| 84 | + static { |
| 85 | + PARSER.declareLong(constructorArg(), VERSION); |
| 86 | + PARSER.declareString(constructorArg(), ERROR_KIND); |
| 87 | + PARSER.declareStringArray(constructorArg(), ERRORS); |
| 88 | + } |
| 89 | + |
| 90 | + /** |
| 91 | + * Reads an {@link ImmutableStateErrorMetadata} from xContent |
| 92 | + * |
| 93 | + * @param parser {@link XContentParser} |
| 94 | + * @return {@link ImmutableStateErrorMetadata} |
| 95 | + */ |
| 96 | + public static ImmutableStateErrorMetadata fromXContent(final XContentParser parser) { |
| 97 | + return PARSER.apply(parser, null); |
| 98 | + } |
| 99 | + |
| 100 | + /** |
| 101 | + * Reads an {@link ImmutableStateErrorMetadata} {@link Diff} from {@link StreamInput} |
| 102 | + * |
| 103 | + * @param in the {@link StreamInput} to read the diff from |
| 104 | + * @return a {@link Diff} of {@link ImmutableStateErrorMetadata} |
| 105 | + * @throws IOException |
| 106 | + */ |
| 107 | + public static Diff<ImmutableStateErrorMetadata> readDiffFrom(StreamInput in) throws IOException { |
| 108 | + return SimpleDiffable.readDiffFrom(ImmutableStateErrorMetadata::readFrom, in); |
| 109 | + } |
| 110 | + |
| 111 | + /** |
| 112 | + * Enum for kinds of errors we might encounter while processing immutable cluster state updates. |
| 113 | + */ |
| 114 | + public enum ErrorKind { |
| 115 | + PARSING("parsing"), |
| 116 | + VALIDATION("validation"), |
| 117 | + TRANSIENT("transient"); |
| 118 | + |
| 119 | + private final String kind; |
| 120 | + |
| 121 | + ErrorKind(String kind) { |
| 122 | + this.kind = kind; |
| 123 | + } |
| 124 | + |
| 125 | + /** |
| 126 | + * Returns the String value for this enum value |
| 127 | + * |
| 128 | + * @return the String value for the enum |
| 129 | + */ |
| 130 | + public String getKindValue() { |
| 131 | + return kind; |
| 132 | + } |
| 133 | + |
| 134 | + /** |
| 135 | + * Helper method to construct {@link ErrorKind} from a String. |
| 136 | + * |
| 137 | + * The JDK default implementation throws incomprehensible error. |
| 138 | + * @param kind String value |
| 139 | + * @return {@link ErrorKind} |
| 140 | + */ |
| 141 | + public static ErrorKind of(String kind) { |
| 142 | + for (var report : values()) { |
| 143 | + if (report.kind.equals(kind)) { |
| 144 | + return report; |
| 145 | + } |
| 146 | + } |
| 147 | + throw new IllegalArgumentException("kind not supported [" + kind + "]"); |
| 148 | + } |
| 149 | + } |
| 150 | +} |
0 commit comments