Skip to content

Commit

Permalink
feat: HostSpec to not default lastUpdateTime. Handle null lastUpdateT…
Browse files Browse the repository at this point in the history
…ime in RdsHostListProvider (#877)
  • Loading branch information
aaronchung-bitquill authored Feb 9, 2024
1 parent 1245bf3 commit f7bc8a8
Show file tree
Hide file tree
Showing 3 changed files with 62 additions and 8 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,6 @@ public HostSpecBuilder lastUpdateTime(Timestamp lastUpdateTime) {

public HostSpec build() {
checkHostIsSet();
setDefaultLastUpdateTime();
return new HostSpec(this.host, this.port, this.hostId, this.role, this.availability,
this.weight, this.lastUpdateTime, this.hostAvailabilityStrategy);
}
Expand All @@ -99,10 +98,4 @@ private void checkHostIsSet() {
throw new IllegalArgumentException("host parameter must be set.");
}
}

private void setDefaultLastUpdateTime() {
if (this.lastUpdateTime == null) {
this.lastUpdateTime = Timestamp.from(Instant.now());
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -404,7 +404,7 @@ private List<HostSpec> processQueryResults(final ResultSet resultSet) throws SQL
} else {
// Take the latest updated writer node as the current writer. All others will be ignored.
List<HostSpec> sortedWriters = writers.stream()
.sorted(Comparator.comparing(HostSpec::getLastUpdateTime).reversed())
.sorted(Comparator.comparing(HostSpec::getLastUpdateTime, Comparator.nullsLast(Comparator.reverseOrder())))
.collect(Collectors.toList());
hosts.add(sortedWriters.get(0));
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -578,4 +578,65 @@ void testGetTopology_InvalidLastUpdatedTimestamp() throws SQLException {
expectedLastUpdatedTimeStampRounded,
result.hosts.get(0).getLastUpdateTime().toString().substring(0, 16));
}

@Test
void testGetTpology_returnsLatestWriter() throws SQLException {
rdsHostListProvider = Mockito.spy(
getRdsHostListProvider(mockHostListProviderService, "jdbc:someprotocol://url"));
rdsHostListProvider.isInitialized = true;

HostSpec expectedWriterHost = new HostSpecBuilder(new SimpleHostAvailabilityStrategy())
.host("expectedWriterHost")
.role(HostRole.WRITER)
.lastUpdateTime(Timestamp.valueOf("3000-01-01 00:00:00"))
.build();

HostSpec unexpectedWriterHost0 = new HostSpecBuilder(new SimpleHostAvailabilityStrategy())
.host("unexpectedWriterHost0")
.role(HostRole.WRITER)
.lastUpdateTime(Timestamp.valueOf("1000-01-01 00:00:00"))
.build();

HostSpec unexpectedWriterHost1 = new HostSpecBuilder(new SimpleHostAvailabilityStrategy())
.host("unexpectedWriterHost1")
.role(HostRole.WRITER)
.lastUpdateTime(Timestamp.valueOf("2000-01-01 00:00:00"))
.build();

HostSpec unexpectedWriterHostWithNullLastUpdateTime0 = new HostSpecBuilder(new SimpleHostAvailabilityStrategy())
.host("unexpectedWriterHostWithNullLastUpdateTime0")
.role(HostRole.WRITER)
.lastUpdateTime(null)
.build();

HostSpec unexpectedWriterHostWithNullLastUpdateTime1 = new HostSpecBuilder(new SimpleHostAvailabilityStrategy())
.host("unexpectedWriterHostWithNullLastUpdateTime1")
.role(HostRole.WRITER)
.lastUpdateTime(null)
.build();

when(mockResultSet.next()).thenReturn(true, true, true, true, true, false);

when(mockResultSet.getString(1)).thenReturn(
unexpectedWriterHostWithNullLastUpdateTime0.getHost(),
unexpectedWriterHost0.getHost(),
expectedWriterHost.getHost(),
unexpectedWriterHost1.getHost(),
unexpectedWriterHostWithNullLastUpdateTime1.getHost());
when(mockResultSet.getBoolean(2)).thenReturn(true, true, true, true, true);
when(mockResultSet.getFloat(3)).thenReturn((float) 0, (float) 0, (float) 0, (float) 0, (float) 0);
when(mockResultSet.getFloat(4)).thenReturn((float) 0, (float) 0, (float) 0, (float) 0, (float) 0);
when(mockResultSet.getTimestamp(5)).thenReturn(
unexpectedWriterHostWithNullLastUpdateTime0.getLastUpdateTime(),
unexpectedWriterHost0.getLastUpdateTime(),
expectedWriterHost.getLastUpdateTime(),
unexpectedWriterHost1.getLastUpdateTime(),
unexpectedWriterHostWithNullLastUpdateTime1.getLastUpdateTime()
);

final FetchTopologyResult result = rdsHostListProvider.getTopology(mockConnection, true);
verify(rdsHostListProvider, atMostOnce()).queryForTopology(mockConnection);

assertEquals(expectedWriterHost.getHost(), result.hosts.get(0).getHost());
}
}

0 comments on commit f7bc8a8

Please sign in to comment.