|
| 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; you may not use this file except in compliance with the Elastic License |
| 5 | + * 2.0. |
| 6 | + */ |
| 7 | + |
| 8 | +package org.elasticsearch.xpack.core.rollup.action; |
| 9 | + |
| 10 | +import org.elasticsearch.common.Strings; |
| 11 | +import org.elasticsearch.common.io.stream.StreamInput; |
| 12 | +import org.elasticsearch.common.io.stream.StreamOutput; |
| 13 | +import org.elasticsearch.index.shard.ShardId; |
| 14 | +import org.elasticsearch.tasks.Task; |
| 15 | +import org.elasticsearch.xcontent.ConstructingObjectParser; |
| 16 | +import org.elasticsearch.xcontent.ParseField; |
| 17 | +import org.elasticsearch.xcontent.XContentBuilder; |
| 18 | +import org.elasticsearch.xcontent.XContentParser; |
| 19 | + |
| 20 | +import java.io.IOException; |
| 21 | +import java.time.Instant; |
| 22 | +import java.util.Objects; |
| 23 | +import java.util.concurrent.atomic.AtomicLong; |
| 24 | + |
| 25 | +import static org.elasticsearch.xcontent.ConstructingObjectParser.constructorArg; |
| 26 | + |
| 27 | +public class RollupShardStatus implements Task.Status { |
| 28 | + public static final String NAME = "rollup-index-shard"; |
| 29 | + private static final ParseField SHARD_FIELD = new ParseField("shard"); |
| 30 | + private static final ParseField STATUS_FIELD = new ParseField("status"); |
| 31 | + private static final ParseField START_TIME_FIELD = new ParseField("start_time"); |
| 32 | + private static final ParseField IN_NUM_DOCS_RECEIVED_FIELD = new ParseField("in_num_docs_received"); |
| 33 | + private static final ParseField OUT_NUM_DOCS_SENT_FIELD = new ParseField("out_num_docs_sent"); |
| 34 | + private static final ParseField OUT_NUM_DOCS_INDEXED_FIELD = new ParseField("out_num_docs_indexed"); |
| 35 | + private static final ParseField OUT_NUM_DOCS_FAILED_FIELD = new ParseField("out_num_docs_failed"); |
| 36 | + |
| 37 | + private final ShardId shardId; |
| 38 | + private final long rollupStart; |
| 39 | + private Status status; |
| 40 | + private AtomicLong numReceived = new AtomicLong(0); |
| 41 | + private AtomicLong numSent = new AtomicLong(0); |
| 42 | + private AtomicLong numIndexed = new AtomicLong(0); |
| 43 | + private AtomicLong numFailed = new AtomicLong(0); |
| 44 | + |
| 45 | + private static final ConstructingObjectParser<RollupShardStatus, Void> PARSER; |
| 46 | + static { |
| 47 | + PARSER = new ConstructingObjectParser<>( |
| 48 | + NAME, |
| 49 | + args -> new RollupShardStatus( |
| 50 | + ShardId.fromString((String) args[0]), |
| 51 | + Status.valueOf((String) args[1]), |
| 52 | + Instant.parse((String) args[2]).toEpochMilli(), |
| 53 | + new AtomicLong((Long) args[3]), |
| 54 | + new AtomicLong((Long) args[4]), |
| 55 | + new AtomicLong((Long) args[5]), |
| 56 | + new AtomicLong((Long) args[6]) |
| 57 | + ) |
| 58 | + ); |
| 59 | + |
| 60 | + PARSER.declareString(constructorArg(), SHARD_FIELD); |
| 61 | + PARSER.declareString(constructorArg(), STATUS_FIELD); |
| 62 | + PARSER.declareString(constructorArg(), START_TIME_FIELD); |
| 63 | + PARSER.declareLong(constructorArg(), IN_NUM_DOCS_RECEIVED_FIELD); |
| 64 | + PARSER.declareLong(constructorArg(), OUT_NUM_DOCS_SENT_FIELD); |
| 65 | + PARSER.declareLong(constructorArg(), OUT_NUM_DOCS_INDEXED_FIELD); |
| 66 | + PARSER.declareLong(constructorArg(), OUT_NUM_DOCS_FAILED_FIELD); |
| 67 | + } |
| 68 | + |
| 69 | + public RollupShardStatus(StreamInput in) throws IOException { |
| 70 | + shardId = new ShardId(in); |
| 71 | + status = in.readEnum(Status.class); |
| 72 | + rollupStart = in.readLong(); |
| 73 | + numReceived = new AtomicLong(in.readLong()); |
| 74 | + numSent = new AtomicLong(in.readLong()); |
| 75 | + numIndexed = new AtomicLong(in.readLong()); |
| 76 | + numFailed = new AtomicLong(in.readLong()); |
| 77 | + } |
| 78 | + |
| 79 | + public RollupShardStatus( |
| 80 | + ShardId shardId, |
| 81 | + Status status, |
| 82 | + long rollupStart, |
| 83 | + AtomicLong numReceived, |
| 84 | + AtomicLong numSent, |
| 85 | + AtomicLong numIndexed, |
| 86 | + AtomicLong numFailed |
| 87 | + ) { |
| 88 | + this.shardId = shardId; |
| 89 | + this.status = status; |
| 90 | + this.rollupStart = rollupStart; |
| 91 | + this.numReceived = numReceived; |
| 92 | + this.numSent = numSent; |
| 93 | + this.numIndexed = numIndexed; |
| 94 | + this.numFailed = numFailed; |
| 95 | + } |
| 96 | + |
| 97 | + public RollupShardStatus(ShardId shardId) { |
| 98 | + status = Status.STARTED; |
| 99 | + this.shardId = shardId; |
| 100 | + this.rollupStart = System.currentTimeMillis(); |
| 101 | + } |
| 102 | + |
| 103 | + public void init(AtomicLong numReceived, AtomicLong numSent, AtomicLong numIndexed, AtomicLong numFailed) { |
| 104 | + this.numReceived = numReceived; |
| 105 | + this.numSent = numSent; |
| 106 | + this.numIndexed = numIndexed; |
| 107 | + this.numFailed = numFailed; |
| 108 | + } |
| 109 | + |
| 110 | + public Status getStatus() { |
| 111 | + return status; |
| 112 | + } |
| 113 | + |
| 114 | + public void setStatus(Status status) { |
| 115 | + this.status = status; |
| 116 | + } |
| 117 | + |
| 118 | + public static RollupShardStatus fromXContent(XContentParser parser) throws IOException { |
| 119 | + return PARSER.parse(parser, null); |
| 120 | + } |
| 121 | + |
| 122 | + @Override |
| 123 | + public XContentBuilder toXContent(XContentBuilder builder, Params params) throws IOException { |
| 124 | + builder.startObject(); |
| 125 | + builder.field(SHARD_FIELD.getPreferredName(), shardId); |
| 126 | + builder.field(STATUS_FIELD.getPreferredName(), status); |
| 127 | + builder.field(START_TIME_FIELD.getPreferredName(), Instant.ofEpochMilli(rollupStart).toString()); |
| 128 | + builder.field(IN_NUM_DOCS_RECEIVED_FIELD.getPreferredName(), numReceived.get()); |
| 129 | + builder.field(OUT_NUM_DOCS_SENT_FIELD.getPreferredName(), numSent.get()); |
| 130 | + builder.field(OUT_NUM_DOCS_INDEXED_FIELD.getPreferredName(), numIndexed.get()); |
| 131 | + builder.field(OUT_NUM_DOCS_FAILED_FIELD.getPreferredName(), numFailed.get()); |
| 132 | + builder.endObject(); |
| 133 | + return builder; |
| 134 | + } |
| 135 | + |
| 136 | + @Override |
| 137 | + public String getWriteableName() { |
| 138 | + return NAME; |
| 139 | + } |
| 140 | + |
| 141 | + @Override |
| 142 | + public void writeTo(StreamOutput out) throws IOException { |
| 143 | + shardId.writeTo(out); |
| 144 | + out.writeEnum(status); |
| 145 | + out.writeLong(rollupStart); |
| 146 | + out.writeLong(numReceived.get()); |
| 147 | + out.writeLong(numSent.get()); |
| 148 | + out.writeLong(numIndexed.get()); |
| 149 | + out.writeLong(numFailed.get()); |
| 150 | + } |
| 151 | + |
| 152 | + @Override |
| 153 | + public boolean equals(Object o) { |
| 154 | + if (this == o) { |
| 155 | + return true; |
| 156 | + } |
| 157 | + if (o == null || getClass() != o.getClass()) { |
| 158 | + return false; |
| 159 | + } |
| 160 | + RollupShardStatus that = (RollupShardStatus) o; |
| 161 | + return rollupStart == that.rollupStart |
| 162 | + && Objects.equals(shardId.getIndexName(), that.shardId.getIndexName()) |
| 163 | + && Objects.equals(shardId.id(), that.shardId.id()) |
| 164 | + && status == that.status |
| 165 | + && Objects.equals(numReceived.get(), that.numReceived.get()) |
| 166 | + && Objects.equals(numSent.get(), that.numSent.get()) |
| 167 | + && Objects.equals(numIndexed.get(), that.numIndexed.get()) |
| 168 | + && Objects.equals(numFailed.get(), that.numFailed.get()); |
| 169 | + } |
| 170 | + |
| 171 | + @Override |
| 172 | + public int hashCode() { |
| 173 | + return Objects.hash( |
| 174 | + shardId.getIndexName(), |
| 175 | + shardId.id(), |
| 176 | + rollupStart, |
| 177 | + status, |
| 178 | + numReceived.get(), |
| 179 | + numSent.get(), |
| 180 | + numIndexed.get(), |
| 181 | + numFailed.get() |
| 182 | + ); |
| 183 | + } |
| 184 | + |
| 185 | + @Override |
| 186 | + public String toString() { |
| 187 | + return Strings.toString(this); |
| 188 | + } |
| 189 | + |
| 190 | + public enum Status { |
| 191 | + STARTED, |
| 192 | + FINISHED, |
| 193 | + ABORT |
| 194 | + } |
| 195 | + |
| 196 | + public void setNumSent(AtomicLong numSent) { |
| 197 | + this.numSent = numSent; |
| 198 | + } |
| 199 | + |
| 200 | + public void setNumIndexed(AtomicLong numIndexed) { |
| 201 | + this.numIndexed = numIndexed; |
| 202 | + } |
| 203 | + |
| 204 | + public void setNumFailed(AtomicLong numFailed) { |
| 205 | + this.numFailed = numFailed; |
| 206 | + } |
| 207 | +} |
0 commit comments