Skip to content

Commit

Permalink
Store lastUpdateTime as timestamp
Browse files Browse the repository at this point in the history
  • Loading branch information
crystall-bitquill committed May 10, 2023
1 parent 5c2cd17 commit ae5d987
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 32 deletions.
18 changes: 8 additions & 10 deletions wrapper/src/main/java/software/amazon/jdbc/HostSpec.java
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@

import java.sql.Timestamp;
import java.time.Instant;
import java.time.format.DateTimeFormatter;
import java.util.Arrays;
import java.util.Collections;
import java.util.Objects;
Expand All @@ -42,8 +41,7 @@ public class HostSpec {
protected Set<String> allAliases = ConcurrentHashMap.newKeySet();
protected long weight; // Greater or equal 0. Lesser the weight, the healthier node.
protected String hostId;
protected String lastUpdateTime;
private final DateTimeFormatter formatter = DateTimeFormatter.ISO_LOCAL_DATE_TIME;
protected Timestamp lastUpdateTime;

public HostSpec(final String host) {
this.host = host;
Expand All @@ -52,7 +50,7 @@ public HostSpec(final String host) {
this.role = HostRole.WRITER;
this.allAliases.add(this.asAlias());
this.weight = DEFAULT_WEIGHT;
this.lastUpdateTime = formatter.format(Timestamp.from(Instant.now()).toLocalDateTime());
this.lastUpdateTime = Timestamp.from(Instant.now());
}

public HostSpec(final String host, final int port) {
Expand All @@ -62,7 +60,7 @@ public HostSpec(final String host, final int port) {
this.role = HostRole.WRITER;
this.allAliases.add(this.asAlias());
this.weight = DEFAULT_WEIGHT;
this.lastUpdateTime = formatter.format(Timestamp.from(Instant.now()).toLocalDateTime());
this.lastUpdateTime = Timestamp.from(Instant.now());
}

public HostSpec(final String host, final int port, final HostRole role) {
Expand All @@ -72,7 +70,7 @@ public HostSpec(final String host, final int port, final HostRole role) {
this.role = role;
this.allAliases.add(this.asAlias());
this.weight = DEFAULT_WEIGHT;
this.lastUpdateTime = formatter.format(Timestamp.from(Instant.now()).toLocalDateTime());
this.lastUpdateTime = Timestamp.from(Instant.now());
}

public HostSpec(final String host, final int port, final HostRole role, final HostAvailability availability) {
Expand All @@ -82,7 +80,7 @@ public HostSpec(final String host, final int port, final HostRole role, final Ho
this.role = role;
this.allAliases.add(this.asAlias());
this.weight = DEFAULT_WEIGHT;
this.lastUpdateTime = formatter.format(Timestamp.from(Instant.now()).toLocalDateTime());
this.lastUpdateTime = Timestamp.from(Instant.now());
}

public HostSpec(final String host, final int port, final HostRole role, final HostAvailability availability,
Expand All @@ -93,11 +91,11 @@ public HostSpec(final String host, final int port, final HostRole role, final Ho
this.role = role;
this.allAliases.add(this.asAlias());
this.weight = weight;
this.lastUpdateTime = formatter.format(Timestamp.from(Instant.now()).toLocalDateTime());
this.lastUpdateTime = Timestamp.from(Instant.now());
}

public HostSpec(final String host, final int port, final HostRole role, final HostAvailability availability,
final long weight, final String lastUpdateTime) {
final long weight, final Timestamp lastUpdateTime) {
this.host = host;
this.port = port;
this.availability = availability;
Expand Down Expand Up @@ -141,7 +139,7 @@ public void setAvailability(final HostAvailability availability) {
this.availability = availability;
}

public String getLastUpdateTime() {
public Timestamp getLastUpdateTime() {
return this.lastUpdateTime;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,6 @@
import java.sql.Statement;
import java.sql.Timestamp;
import java.time.Instant;
import java.time.format.DateTimeFormatter;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
Expand Down Expand Up @@ -412,11 +411,11 @@ private HostSpec createHost(final ResultSet resultSet) throws SQLException {
final boolean isWriter = resultSet.getBoolean(2);
final float cpuUtilization = resultSet.getFloat(3);
final float nodeLag = resultSet.getFloat(4);
String lastUpdateTime;
Timestamp lastUpdateTime;
try {
lastUpdateTime = convertTimestampToString(resultSet.getTimestamp(5));
lastUpdateTime = resultSet.getTimestamp(5);
} catch (WrongArgumentException e) {
lastUpdateTime = convertTimestampToString(Timestamp.from(Instant.now()));
lastUpdateTime = Timestamp.from(Instant.now());
}

// Calculate weight based on node lag in time and CPU utilization.
Expand All @@ -425,7 +424,7 @@ private HostSpec createHost(final ResultSet resultSet) throws SQLException {
return createHost(hostName, isWriter, weight, lastUpdateTime);
}

private HostSpec createHost(String host, final boolean isWriter, final long weight, final String lastUpdateTime) {
private HostSpec createHost(String host, final boolean isWriter, final long weight, final Timestamp lastUpdateTime) {
host = host == null ? "?" : host;
final String endpoint = getHostEndpoint(host);
final int port = this.clusterInstanceTemplate.isPortSpecified()
Expand All @@ -444,11 +443,6 @@ private HostSpec createHost(String host, final boolean isWriter, final long weig
return hostSpec;
}

private String convertTimestampToString(Timestamp timestamp) {
DateTimeFormatter formatter = DateTimeFormatter.ISO_LOCAL_DATE_TIME;
return timestamp == null ? null : formatter.format(timestamp.toLocalDateTime());
}

/**
* Build a host dns endpoint based on host/node name.
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,6 @@
import java.sql.Statement;
import java.sql.Timestamp;
import java.time.Instant;
import java.time.format.DateTimeFormatter;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
Expand Down Expand Up @@ -482,22 +481,21 @@ void testGetTopology_StaleRecord() throws SQLException {
getAuroraHostListProvider("jdbc:someprotocol://", mockHostListProviderService, "jdbc:someprotocol://url"));
auroraHostListProvider.isInitialized = true;

final String hostName = "hostName";
final String hostName1 = "hostName1";
final String hostName2 = "hostName2";
final Float cpuUtilization = 11.1F;
final Float nodeLag = 0.123F;
final Timestamp firstTimestamp = Timestamp.from(Instant.now());
final Timestamp secondTimestamp = new Timestamp(firstTimestamp.getTime() + 1);
final Timestamp secondTimestamp = new Timestamp(firstTimestamp.getTime() + 100);
when(mockResultSet.next()).thenReturn(true, true, false);
when(mockResultSet.getString(1)).thenReturn(hostName).thenReturn(hostName);
when(mockResultSet.getString(1)).thenReturn(hostName1).thenReturn(hostName2);
when(mockResultSet.getBoolean(2)).thenReturn(true).thenReturn(true);
when(mockResultSet.getFloat(3)).thenReturn(cpuUtilization).thenReturn(cpuUtilization);
when(mockResultSet.getFloat(4)).thenReturn(nodeLag).thenReturn(nodeLag);
when(mockResultSet.getTimestamp(5)).thenReturn(firstTimestamp).thenReturn(secondTimestamp);
long weight = Math.round(nodeLag) * 100L + Math.round(cpuUtilization);
final DateTimeFormatter formatter = DateTimeFormatter.ISO_LOCAL_DATE_TIME;
final String secondTimestampString = formatter.format(secondTimestamp.toLocalDateTime());
final HostSpec expectedWriter =
new HostSpec(hostName, -1, HostRole.WRITER, HostAvailability.AVAILABLE, weight, secondTimestampString);
new HostSpec(hostName2, -1, HostRole.WRITER, HostAvailability.AVAILABLE, weight, secondTimestamp);

final FetchTopologyResult result = auroraHostListProvider.getTopology(mockConnection, true);
verify(auroraHostListProvider, atMostOnce()).queryForTopology(mockConnection);
Expand All @@ -514,7 +512,6 @@ void testGetTopology_InvalidLastUpdatedTimestamp() throws SQLException {
final String hostName = "hostName";
final Float cpuUtilization = 11.1F;
final Float nodeLag = 0.123F;
final Timestamp firstTimestamp = Timestamp.from(Instant.now());
when(mockResultSet.next()).thenReturn(true, false);
when(mockResultSet.getString(1)).thenReturn(hostName);
when(mockResultSet.getBoolean(2)).thenReturn(true);
Expand All @@ -525,12 +522,10 @@ void testGetTopology_InvalidLastUpdatedTimestamp() throws SQLException {
final FetchTopologyResult result = auroraHostListProvider.getTopology(mockConnection, true);
verify(auroraHostListProvider, atMostOnce()).queryForTopology(mockConnection);

final DateTimeFormatter formatter = DateTimeFormatter.ISO_LOCAL_DATE_TIME;
final String expectedLastUpdatedTimeStampRounded =
formatter.format(Timestamp.from(Instant.now()).toLocalDateTime()).substring(0, 16);
final String expectedLastUpdatedTimeStampRounded = Timestamp.from(Instant.now()).toString().substring(0, 16);
assertEquals(1, result.hosts.size());
assertEquals(
expectedLastUpdatedTimeStampRounded,
result.hosts.get(0).getLastUpdateTime().substring(0, 16));
result.hosts.get(0).getLastUpdateTime().toString().substring(0, 16));
}
}

0 comments on commit ae5d987

Please sign in to comment.