XRAY: How To Solve Xray Scan Builds Timing Out With Error: [{"status":500,"message":"failed to index the build"}], attempting to reconnect

XRAY: How To Solve Xray Scan Builds Timing Out With Error: [{"status":500,"message":"failed to index the build"}], attempting to reconnect

AuthorFullName__c
Dor Tambour
articleNumber
000005372
ft:sourceType
Salesforce
FirstPublishedDate
2022-08-15T10:24:56Z
lastModifiedDate
2023-01-22T11:06:38Z
VersionNumber
2

When scanning a build for the first time with Xray you might encounter the following server 500 error:
 

Xray scan connection lost: Artifactory response: [{"status":500,"message":"failed to index the build"}], attempting to reconnect. 

The reason for this error is timeout when Xray performs the following AQL query against Artifactory and Artifactory does not respond on time:
items.find({"@build.name" : "<build-name>", "@build.number" : "<build-number>", "artifact.module.build.repo": "artifactory-build-info", "artifact.module.build.name" : "<project-name>", "artifact.module.build.number" : "<build-number>"}).include("repo","path","name")

This AQL is trying to fetch build artifacts from Artifactory.

When the AQL fails, the following error will be displayed in the indexer microservice logs:
Caused by: failed to get items for build by aql: items.find({"@build.name" : "<build-name>", "@build.number" : "<build-number>", "artifact.module.build.repo": "artifactory-build-info", "artifact.module.build.name" : "<project-name>", "artifact.module.build.number" : "<build-number>"}).include("repo","path","name")
 --- at /go/src/jfrog.com/xray/pkg/utils/bin_mgr_utils/bin_mgr_utils.go:17 (PerformAql) ---
Caused by: Post "http://$ARTIFACTORY_URL/artifactory/api/search/aql": net/http: timeout awaiting response headers

In order to fix the timeout issue we might use the following approaches:

1. Increasing the time Xray is waiting for a response from an external service.
Since Xray is timing out we can increase the time it is waiting for external requests until
timing out. To achieve that, we can add the following parameter in the Xray system.yaml:
server:
  "httpClientResponseHeaderTimeout" = "<seconds>"

The default value of this parameter is 300 and it is measured in seconds.
Sometimes the specified AQL will take longer depending on the scale of your database.

2. Changing our Artifactory database

The AQL is database related and therefore migrating from an internal database such as Derby might improve the performance of the AQL. Usually, these AQLs are optimized for external DB such as Postgres so migrating the DB might reduce the time it takes for the query to be executed.