Fix Bug in Snapshot Status Response Timestamps (#46919) (#46970)

Fixing a corner case where snapshot total time calculation was off when
getting the `SnapshotStatus` of an in-progress snapshot.

Closes #46913
This commit is contained in:
Armin Braun 2019-09-23 15:01:47 +02:00 committed by GitHub
parent f06aa0c6c0
commit 2da040601b
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 14 additions and 3 deletions

View File

@ -20,6 +20,7 @@
package org.elasticsearch.action.admin.cluster.snapshots.status;
import org.elasticsearch.Version;
import org.elasticsearch.common.Strings;
import org.elasticsearch.common.io.stream.StreamInput;
import org.elasticsearch.common.io.stream.StreamOutput;
import org.elasticsearch.common.io.stream.Writeable;
@ -70,6 +71,7 @@ public class SnapshotStats implements Writeable, ToXContentObject {
long incrementalSize, long totalSize, long processedSize) {
this.startTime = startTime;
this.time = time;
assert time >= 0 : "Tried to initialize snapshot stats with negative total time [" + time + "]";
this.incrementalFileCount = incrementalFileCount;
this.totalFileCount = totalFileCount;
this.processedFileCount = processedFileCount;
@ -323,6 +325,8 @@ public class SnapshotStats implements Writeable, ToXContentObject {
// Update duration
time = endTime - startTime;
}
assert time >= 0
: "Update with [" + Strings.toString(stats) + "][" + updateTimestamps + "] resulted in negative total time [" + time + "]";
}
@Override

View File

@ -103,6 +103,7 @@ public class SnapshotStatus implements ToXContentObject, Writeable {
this.shards = Objects.requireNonNull(shards);
this.includeGlobalState = includeGlobalState;
shardsStats = new SnapshotShardsStats(shards);
assert time >= 0 : "time must be >= 0 but received [" + time + "]";
updateShardStats(startTime, time);
}

View File

@ -238,9 +238,14 @@ public class TransportSnapshotsStatusAction extends TransportMasterNodeAction<Sn
throw new IllegalArgumentException("Unknown snapshot state " + snapshotInfo.state());
}
final long startTime = snapshotInfo.startTime();
final long endTime = snapshotInfo.endTime();
assert endTime >= startTime || (endTime == 0L && snapshotInfo.state().completed() == false)
: "Inconsistent timestamps found in SnapshotInfo [" + snapshotInfo + "]";
builder.add(new SnapshotStatus(new Snapshot(repositoryName, snapshotId), state,
Collections.unmodifiableList(shardStatusBuilder), snapshotInfo.includeGlobalState(),
startTime, snapshotInfo.endTime() - startTime));
startTime,
// Use current time to calculate overall runtime for in-progress snapshots that have endTime == 0
(endTime == 0 ? threadPool.absoluteTimeInMillis() : endTime) - startTime));
}
}
}

View File

@ -28,8 +28,9 @@ public class SnapshotStatsTests extends AbstractXContentTestCase<SnapshotStats>
@Override
protected SnapshotStats createTestInstance() {
long startTime = randomNonNegativeLong();
long time = randomNonNegativeLong();
// Using less than half of Long.MAX_VALUE for random time values to avoid long overflow in tests that add the two time values
long startTime = randomLongBetween(0, Long.MAX_VALUE / 2 - 1);
long time = randomLongBetween(0, Long.MAX_VALUE / 2 - 1);
int incrementalFileCount = randomIntBetween(0, Integer.MAX_VALUE);
int totalFileCount = randomIntBetween(0, Integer.MAX_VALUE);
int processedFileCount = randomIntBetween(0, Integer.MAX_VALUE);