fix(emr): preserve millisecond timestamp precision (#6149)

This commit is contained in:
Jimmy Liu Jia authored and GitHub committed 2026-10-21 13:29:21 -04:00
1 parent f4a9b15ced
commit ac7371fb33
3 files changed
+77 -7

No files matched your search

@@ -22,6 +22,7 @@ import jakarta.inject.Inject;
import jakarta.ws.rs.core.Response;
import org.jboss.logging.Logger;
import java.math.BigDecimal;
import java.time.Instant;
import java.time.temporal.ChronoUnit;
import java.util.ArrayList;
@@ -879,13 +880,13 @@ public class EmrHandler {
}
}
private long epoch(Instant instant) {
return instant == null ? 0L : instant.getEpochSecond();
private BigDecimal epoch(Instant instant) {
return instant == null ? BigDecimal.ZERO : BigDecimal.valueOf(instant.toEpochMilli(), 3);
}
private void putEpoch(ObjectNode node, String field, Instant instant) {
if (instant != null) {
node.put(field, instant.getEpochSecond());
node.put(field, epoch(instant));
}
}
}
@@ -22,6 +22,7 @@ import jakarta.inject.Inject;
import java.security.SecureRandom;
import java.time.Instant;
import java.time.temporal.ChronoUnit;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.LinkedHashMap;
@@ -95,7 +96,7 @@ public class EmrService {
? "INSTANCE_GROUP" : "INSTANCE_FLEET");
cluster.setAutoTerminate(!cluster.isKeepJobFlowAliveWhenNoSteps());
cluster.setMasterPublicDnsName(masterDnsName(region));
cluster.setCreationDateTime(Instant.now());
cluster.setCreationDateTime(Instant.now().truncatedTo(ChronoUnit.MILLIS));
for (EmrInstanceGroup g : cluster.getInstanceGroups()) {
g.setId("ig-" + randomId(13));
g.setRunningInstanceCount(g.getRequestedInstanceCount());
@@ -137,6 +138,9 @@ public class EmrService {
continue;
}
Instant createdAt = c.getCreationDateTime();
if (createdAt != null) {
createdAt = createdAt.truncatedTo(ChronoUnit.MILLIS);
}
if (createdAfter != null && (createdAt == null || !createdAt.isAfter(createdAfter))) {
continue;
}
@@ -1,5 +1,7 @@
package io.github.hectorvent.floci.services.emr;
import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.ObjectMapper;
import io.github.hectorvent.floci.services.emr.model.EmrCluster;
import io.github.hectorvent.floci.testing.RestAssuredJsonUtils;
import io.quarkus.test.junit.QuarkusTest;
@@ -23,6 +25,9 @@ import static org.hamcrest.Matchers.not;
import static org.hamcrest.Matchers.notNullValue;
import static org.hamcrest.Matchers.nullValue;
import static org.hamcrest.Matchers.startsWith;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertNotNull;
import static org.junit.jupiter.api.Assertions.assertTrue;
/**
* EMR cluster lifecycle over the AWS JSON 1.1 wire protocol: run job flow → describe →
@@ -39,6 +44,9 @@ class EmrIntegrationTest {
@Inject
EmrService emrService;
@Inject
ObjectMapper objectMapper;
@BeforeAll
static void configure() {
RestAssuredJsonUtils.configureAwsContentTypes();
@@ -84,7 +92,7 @@ class EmrIntegrationTest {
@Test
@Order(3)
void listClustersFilteredByStateAndCreationTime() {
void listClustersFilteredByStateAndCreationTime() throws Exception {
emr("ListClusters", "{\"ClusterStates\":[\"WAITING\"]}")
.then().statusCode(200)
.body("Clusters.find { it.Id == '" + clusterId + "' }.Name", equalTo("floci-emr"));
@@ -101,6 +109,7 @@ class EmrIntegrationTest {
.body("Clusters.find { it.Id == '" + clusterId + "' }.Name", equalTo("floci-emr"));
Instant createdAt = emrService.describeCluster(clusterId).getCreationDateTime();
assertEquals(0, createdAt.getNano() % 1_000_000);
BigDecimal epochSeconds = BigDecimal.valueOf(createdAt.getEpochSecond())
.add(BigDecimal.valueOf(createdAt.getNano(), 9));
String shortlyBefore = epochSeconds.subtract(new BigDecimal("0.1")).toPlainString();
@@ -138,6 +147,38 @@ class EmrIntegrationTest {
cluster.setCreationDateTime(originalCreationTime);
}
cluster.setCreationDateTime(Instant.ofEpochSecond(1_761_000_000L, 123_456_789));
try {
JsonNode clusters = objectMapper.readTree(emr("ListClusters", "{}")
.then().statusCode(200).extract().asString()).path("Clusters");
JsonNode listedTime = null;
for (JsonNode listed : clusters) {
if (clusterId.equals(listed.path("Id").asText())) {
listedTime = listed.path("Status").path("Timeline").path("CreationDateTime");
break;
}
}
assertNotNull(listedTime);
assertTrue(listedTime.isNumber());
assertEquals(0, new BigDecimal("1761000000.123").compareTo(listedTime.decimalValue()));
JsonNode describedTime = objectMapper.readTree(emr("DescribeCluster",
"{\"ClusterId\":\"" + clusterId + "\"}")
.then().statusCode(200).extract().asString())
.path("Cluster").path("Status").path("Timeline").path("CreationDateTime");
assertTrue(describedTime.isNumber());
assertEquals(0, new BigDecimal("1761000000.123").compareTo(describedTime.decimalValue()));
emr("ListClusters", "{\"CreatedAfter\":" + listedTime.asText() + "}")
.then().statusCode(200)
.body("Clusters.find { it.Id == '" + clusterId + "' }", nullValue());
emr("ListClusters", "{\"CreatedBefore\":" + listedTime.asText() + "}")
.then().statusCode(200)
.body("Clusters.find { it.Id == '" + clusterId + "' }", nullValue());
} finally {
cluster.setCreationDateTime(originalCreationTime);
}
emr("ListClusters", "{\"CreatedAfter\":\"invalid\"}")
.then().statusCode(400)
.body("__type", equalTo("InvalidRequestException"));
@@ -234,7 +275,7 @@ class EmrIntegrationTest {
@Test
@Order(11)
void securityConfigurationRoundTrip() {
void securityConfigurationRoundTrip() throws Exception {
emr("CreateSecurityConfiguration",
"{\"Name\":\"floci-emr-sec\",\"SecurityConfiguration\":\"{\\\"EncryptionConfiguration\\\":{}}\"}")
.then().statusCode(200)
@@ -245,7 +286,31 @@ class EmrIntegrationTest {
.then().statusCode(200)
.body("SecurityConfiguration", equalTo("{\"EncryptionConfiguration\":{}}"));
emr("DeleteSecurityConfiguration", "{\"Name\":\"floci-emr-sec\"}").then().statusCode(200);
emrService.describeSecurityConfiguration("floci-emr-sec")
.setCreationDateTime(Instant.ofEpochMilli(1_761_000_000_123L));
try {
JsonNode describedTime = objectMapper.readTree(emr("DescribeSecurityConfiguration",
"{\"Name\":\"floci-emr-sec\"}")
.then().statusCode(200).extract().asString()).path("CreationDateTime");
assertTrue(describedTime.isNumber());
assertEquals(0, new BigDecimal("1761000000.123").compareTo(describedTime.decimalValue()));
JsonNode configurations = objectMapper.readTree(emr("ListSecurityConfigurations", "{}")
.then().statusCode(200).extract().asString()).path("SecurityConfigurations");
JsonNode listedTime = null;
for (JsonNode configuration : configurations) {
if ("floci-emr-sec".equals(configuration.path("Name").asText())) {
listedTime = configuration.path("CreationDateTime");
break;
}
}
assertNotNull(listedTime);
assertTrue(listedTime.isNumber());
assertEquals(0, new BigDecimal("1761000000.123").compareTo(listedTime.decimalValue()));
} finally {
emr("DeleteSecurityConfiguration", "{\"Name\":\"floci-emr-sec\"}")
.then().statusCode(200);
}
}
@Test