Skip to content

Commit 97909c1

Browse files
authored
Merge pull request #62 from tharropoulos/fix/null-node-access-ts
Fix Node health checks referencing a null pointer
2 parents 27bacf7 + 45c7db3 commit 97909c1

File tree

3 files changed

+38
-3
lines changed

3 files changed

+38
-3
lines changed

src/main/java/org/typesense/api/ApiCall.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,7 @@ boolean isDueForHealthCheck(Node node) {
6161
// Loops in a round-robin fashion to check for a healthy node and returns it
6262
Node getNode() {
6363
if (configuration.nearestNode != null) {
64-
if (isDueForHealthCheck((configuration.nearestNode)) || configuration.nearestNode.isHealthy) {
64+
if (configuration.nearestNode.isHealthy || isDueForHealthCheck((configuration.nearestNode)) ) {
6565
return configuration.nearestNode;
6666
}
6767
}

src/main/java/org/typesense/resources/Node.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ public Node(String protocol, String host, String port) {
2525
this.host = host;
2626
this.port = port;
2727
this.isHealthy = true;
28+
this.lastAccessTimestamp = LocalDateTime.now();
2829

2930
if (protocol == null) {
3031
throw new RuntimeException("Protocol cannot be null");

src/test/java/org/typesense/api/APICallTest.java

Lines changed: 36 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,23 +14,33 @@
1414
class APICallTest {
1515

1616
private ApiCall apiCall;
17+
private Node nearestNode;
1718

18-
@BeforeEach
19-
void setUp() throws Exception {
19+
void setUpNoNearestNode() throws Exception {
2020
List<Node> nodes = new ArrayList<>();
2121
nodes.add(new Node("http","localhost","8108"));
2222
nodes.add(new Node("http","localhost","7108"));
2323
nodes.add(new Node("http","localhost","6108"));
2424
apiCall = new ApiCall(new Configuration(nodes, Duration.ofSeconds(3),"xyz"));
2525
}
2626

27+
void setUpNearestNode() throws Exception {
28+
List<Node> nodes = new ArrayList<>();
29+
nodes.add(new Node("http","localhost","8108"));
30+
nodes.add(new Node("http","localhost","7108"));
31+
nodes.add(new Node("http","localhost","6108"));
32+
nearestNode = new Node("http","localhost","0000");
33+
apiCall = new ApiCall(new Configuration(nearestNode, nodes, Duration.ofSeconds(3),"xyz"));
34+
}
35+
2736
@AfterEach
2837
void tearDown() throws Exception {
2938

3039
}
3140

3241
@Test
3342
void testRoundRobin() throws Exception {
43+
setUpNoNearestNode();
3444
assertEquals("7108", apiCall.getNode().port);
3545
assertEquals("6108", apiCall.getNode().port);
3646
assertEquals("8108", apiCall.getNode().port);
@@ -39,4 +49,28 @@ void testRoundRobin() throws Exception {
3949
assertEquals("6108", apiCall.getNode().port);
4050
assertEquals("8108", apiCall.getNode().port);
4151
}
52+
53+
54+
@Test
55+
void testUnhealthyNearestNode() throws Exception {
56+
setUpNearestNode();
57+
nearestNode.isHealthy = false;
58+
assertEquals("7108", apiCall.getNode().port);
59+
}
60+
61+
@Test
62+
void testHealthyNearestNode() throws Exception {
63+
setUpNearestNode();
64+
assertEquals("0000", apiCall.getNode().port);
65+
}
66+
67+
@Test
68+
void testUnhealthyNearestNodeDueForHealthCheck() throws Exception {
69+
setUpNearestNode();
70+
nearestNode.isHealthy = false;
71+
nearestNode.lastAccessTimestamp = nearestNode.lastAccessTimestamp.minusSeconds(63);
72+
assertEquals("0000", apiCall.getNode().port);
73+
}
74+
75+
4276
}

0 commit comments

Comments
 (0)