Skip to content

Commit

Permalink
Fix k8s frontend and backend sync issue (#694)
Browse files Browse the repository at this point in the history
* - Add health check controller

* - Fix neo4j url

* - Fix endpoint url

* - Add Readiness Probe

* - Adjust Readiness Probe Timings

* - Adjust Readiness Probe params

* - Use external url

* - Update port config in deployment

* - Update port config in deployment

* - Update Health Check

* - Update Health Check timings

* - Remove Unnecessary Code

* - Add changes to prod k8 charts
  • Loading branch information
haideriqbal authored Jul 23, 2024
1 parent 6fb0c6a commit 89bdeec
Show file tree
Hide file tree
Showing 7 changed files with 115 additions and 1 deletion.
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
package uk.ac.ebi.spot.ols.controller.api.v2;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Pageable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.http.ResponseEntity;
import org.springframework.http.HttpStatus;
import uk.ac.ebi.spot.ols.controller.api.v2.responses.V2PagedAndFacetedResponse;
import uk.ac.ebi.spot.ols.model.v2.V2Entity;
import uk.ac.ebi.spot.ols.repository.neo4j.OlsNeo4jClient;
import uk.ac.ebi.spot.ols.repository.v2.V2OntologyRepository;

import java.util.Map;

@RestController
@RequestMapping("/api/v2")
public class HealthCheckController {
@Autowired
V2OntologyRepository ontologyRepository;

@Autowired
OlsNeo4jClient neo4jClient;
private static final Logger logger = LoggerFactory.getLogger(HealthCheckController.class);

@RequestMapping("/health")
public ResponseEntity<String> checkHealth() {
if (!checkSolr()) {
return ResponseEntity.status(HttpStatus.SERVICE_UNAVAILABLE).body("Solr is not initialized.");
}
if (!checkNeo4j()) {
return ResponseEntity.status(HttpStatus.SERVICE_UNAVAILABLE).body("Neo4j is not initialized.");
}
return ResponseEntity.ok("All systems are operational.");
}

private boolean checkNeo4j() {
try {
if (neo4jClient.getDatabaseNodeCount() > 0) {
logger.info("Neo4J is initialized.");
return true;
} else {
logger.error("Neo4J is not initialized yet as Neo4J node elements were less than 1.");
return false;
}
} catch (Exception e) {
logger.error("Neo4j endpoint returned an error.", e);
return false;
}
}

private boolean checkSolr() {
Pageable pageable = Pageable.ofSize(20);
try {
V2PagedAndFacetedResponse<V2Entity> result = new V2PagedAndFacetedResponse<>(
ontologyRepository.find(pageable, "en", null, null, null,
false, Map.of()));
if (result.totalElements > 0) {
logger.info("Solr is initialized.");
return true;
} else {
logger.error("Solr is not initialized yet as 'totalElements' in jsonResponse not found or less than 1.");
return false;
}
} catch (Exception e) {
logger.error("Solr health check returned an error.", e);
return false;
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,13 @@ public class OlsNeo4jClient {

@Autowired
Neo4jClient neo4jClient;

private static final Logger logger = LoggerFactory.getLogger(OlsNeo4jClient.class);


public long getDatabaseNodeCount() {
return neo4jClient.returnNodeCount();
}

public Page<JsonElement> getAll(String type, Map<String,String> properties, Pageable pageable) {

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,13 @@ public Session getSession() {

}

public long returnNodeCount() {
Session session = getSession();
Result result = session.run("MATCH (n) RETURN COUNT(n)");
long count = result.single().get(0).asLong();
session.close();
return count;
}

// only used by OLS3 graph repo, remove at some point
public List<Map<String,Object>> rawQuery(String query) {
Expand Down
11 changes: 11 additions & 0 deletions k8chart-dev/ols4/templates/ols4-backend-deployment.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,17 @@ spec:
imagePullPolicy: Always
ports:
- containerPort: 8080
readinessProbe:
httpGet:
path: /ols4/api/v2/health # Ensure this matches the context path
port: 8080
scheme: HTTP
httpHeaders:
- name: Host
value: {{ .Release.Name }}-backend
initialDelaySeconds: 15
periodSeconds: 60
timeoutSeconds: 10
resources:
requests:
memory: 10Gi
Expand Down
4 changes: 4 additions & 0 deletions k8chart-dev/ols4/templates/ols4-frontend-deployment.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,10 @@ spec:
operator: In
values:
- hh-rke-wp-webadmin-56-worker-2.caas.ebi.ac.uk
initContainers:
- name: wait-for-backend
image: busybox
command: [ 'sh', '-c', 'until nc -z {{ .Release.Name }}-backend 8080; do echo waiting for backend; sleep 5; done;' ]
containers:
- name: web
image: ghcr.io/ebispot/ols4-frontend:{{.Values.imageTag}}
Expand Down
11 changes: 11 additions & 0 deletions k8chart/ols4/templates/ols4-backend-deployment.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,17 @@ spec:
imagePullPolicy: Always
ports:
- containerPort: 8080
readinessProbe:
httpGet:
path: /ols4/api/v2/health
port: 8080
scheme: HTTP
httpHeaders:
- name: Host
value: { { .Release.Name } }-backend
initialDelaySeconds: 15
periodSeconds: 60
timeoutSeconds: 10
resources:
requests:
memory: 10Gi
Expand Down
4 changes: 4 additions & 0 deletions k8chart/ols4/templates/ols4-frontend-deployment.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,10 @@ spec:
values:
- hh-rke-wp-webadmin-47-worker-4.caas.ebi.ac.uk
- hx-rke-wp-webadmin-40-worker-4.caas.ebi.ac.uk
initContainers:
- name: wait-for-backend
image: busybox
command: [ 'sh', '-c', 'until nc -z {{ .Release.Name }}-backend 8080; do echo waiting for backend; sleep 5; done;' ]
containers:
- name: web
image: ghcr.io/ebispot/ols4-frontend:{{.Values.imageTag}}
Expand Down

0 comments on commit 89bdeec

Please sign in to comment.