Skip to content

Commit

Permalink
Fix read issue of snapshot response
Browse files Browse the repository at this point in the history
  • Loading branch information
ltaragi committed Aug 22, 2024
1 parent edcd80b commit 3b92273
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 13 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -86,35 +86,41 @@ public class SnapshotStatus implements ToXContentObject, Writeable {

private SnapshotStats stats;

private final long initialTotalSizeInBytes;

@Nullable
private final Boolean includeGlobalState;

SnapshotStatus(StreamInput in) throws IOException {
snapshot = new Snapshot(in);
state = State.fromValue(in.readByte());
shards = Collections.unmodifiableList(in.readList(SnapshotIndexShardStatus::new));
initialTotalSizeInBytes = in.readLong();
System.out.println("@@@ initialTotalSizeInBytes = " + initialTotalSizeInBytes);
includeGlobalState = in.readOptionalBoolean();
final long startTime = in.readLong();
final long time = in.readLong();
updateShardStats(startTime, time, 0L);
updateShardStats(startTime, time, initialTotalSizeInBytes);
}

SnapshotStatus(
Snapshot snapshot,
State state,
List<SnapshotIndexShardStatus> shards,
long initialTotalSizeInBytes,
Boolean includeGlobalState,
long startTime,
long time,
long initialTotalSize
long time
) {
this.snapshot = Objects.requireNonNull(snapshot);
this.state = Objects.requireNonNull(state);
this.shards = Objects.requireNonNull(shards);
this.initialTotalSizeInBytes = initialTotalSizeInBytes;
this.includeGlobalState = includeGlobalState;
shardsStats = new SnapshotShardsStats(shards);
assert time >= 0 : "time must be >= 0 but received [" + time + "]";
updateShardStats(startTime, time, initialTotalSize);
assert initialTotalSizeInBytes >= 0 : "initialTotalSizeInBytes must be >= 0 but received [" + initialTotalSizeInBytes + "]";
updateShardStats(startTime, time, initialTotalSizeInBytes);
}

private SnapshotStatus(
Expand All @@ -124,6 +130,7 @@ private SnapshotStatus(
Map<String, SnapshotIndexStatus> indicesStatus,
SnapshotShardsStats shardsStats,
SnapshotStats stats,
long initialTotalSizeInBytes,
Boolean includeGlobalState
) {
this.snapshot = snapshot;
Expand All @@ -132,6 +139,7 @@ private SnapshotStatus(
this.indicesStatus = indicesStatus;
this.shardsStats = shardsStats;
this.stats = stats;
this.initialTotalSizeInBytes = initialTotalSizeInBytes;
this.includeGlobalState = includeGlobalState;
}

Expand Down Expand Up @@ -202,6 +210,7 @@ public void writeTo(StreamOutput out) throws IOException {
snapshot.writeTo(out);
out.writeByte(state.value());
out.writeList(shards);
out.writeLong(initialTotalSizeInBytes);
out.writeOptionalBoolean(includeGlobalState);
out.writeLong(stats.getStartTime());
out.writeLong(stats.getTime());
Expand All @@ -224,6 +233,7 @@ public SnapshotStats getStats() {
private static final String UUID = "uuid";
private static final String STATE = "state";
private static final String INDICES = "indices";

private static final String INCLUDE_GLOBAL_STATE = "include_global_state";

@Override
Expand Down Expand Up @@ -277,7 +287,7 @@ public XContentBuilder toXContent(XContentBuilder builder, Params params) throws
shards.addAll(index.getShards().values());
}
}
return new SnapshotStatus(snapshot, state, shards, indicesStatus, shardsStats, stats, includeGlobalState);
return new SnapshotStatus(snapshot, state, shards, indicesStatus, shardsStats, stats, 0L, includeGlobalState);
}
);
static {
Expand All @@ -300,8 +310,8 @@ public static SnapshotStatus fromXContent(XContentParser parser) throws IOExcept
return PARSER.parse(parser, null);
}

private void updateShardStats(long startTime, long time, long initialTotalSize) {
stats = new SnapshotStats(startTime, time, 0, 0, 0, 0, initialTotalSize, 0);
private void updateShardStats(long startTime, long time, long initialTotalSizeInBytes) {
stats = new SnapshotStats(startTime, time, 0, 0, 0, 0, initialTotalSizeInBytes, 0);
shardsStats = new SnapshotShardsStats(shards);
for (SnapshotIndexShardStatus shard : shards) {
// BWC: only update timestamps when we did not get a start time from an old node
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -296,10 +296,10 @@ private void buildResponse(
entry.snapshot(),
entry.state(),
Collections.unmodifiableList(shardStatusBuilder),
0L,
entry.includeGlobalState(),
entry.startTime(),
Math.max(threadPool.absoluteTimeInMillis() - entry.startTime(), 0L),
0L
Math.max(threadPool.absoluteTimeInMillis() - entry.startTime(), 0L)
)
);
}
Expand Down Expand Up @@ -380,11 +380,11 @@ private void loadRepositoryData(
new Snapshot(repositoryName, snapshotId),
state,
Collections.unmodifiableList(shardStatusBuilder),
initialSnapshotTotalSize,
snapshotInfo.includeGlobalState(),
startTime,
// Use current time to calculate overall runtime for in-progress snapshots that have endTime == 0
(endTime == 0 ? threadPool.absoluteTimeInMillis() : endTime) - startTime,
initialSnapshotTotalSize
(endTime == 0 ? threadPool.absoluteTimeInMillis() : endTime) - startTime
)
);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ public void testToString() throws Exception {
List<SnapshotIndexShardStatus> snapshotIndexShardStatuses = new ArrayList<>();
snapshotIndexShardStatuses.add(snapshotIndexShardStatus);
boolean includeGlobalState = randomBoolean();
SnapshotStatus status = new SnapshotStatus(snapshot, state, snapshotIndexShardStatuses, includeGlobalState, 0L, 0L, 0L);
SnapshotStatus status = new SnapshotStatus(snapshot, state, snapshotIndexShardStatuses, 0L, includeGlobalState, 0L, 0L);

int initializingShards = 0;
int startedShards = 0;
Expand Down Expand Up @@ -213,7 +213,7 @@ protected SnapshotStatus createTestInstance() {
snapshotIndexShardStatuses.add(snapshotIndexShardStatus);
}
boolean includeGlobalState = randomBoolean();
return new SnapshotStatus(snapshot, state, snapshotIndexShardStatuses, includeGlobalState, 0L, 0L, 0L);
return new SnapshotStatus(snapshot, state, snapshotIndexShardStatuses, 0L, includeGlobalState, 0L, 0L);
}

@Override
Expand Down

0 comments on commit 3b92273

Please sign in to comment.