[ELASTIC-20] Exception when accessing ES client with Magnolia running in a container Created: 18/Jan/22  Updated: 18/Jan/22

Status: Open
Project: Elasticsearch
Component/s: None
Affects Version/s: 1.0.0
Fix Version/s: None

Type: Bug Priority: Neutral
Reporter: wolf bubenik Assignee: Unassigned
Resolution: Unresolved Votes: 0
Labels: None
Remaining Estimate: Not Specified
Time Spent: Not Specified
Original Estimate: Not Specified
Environment:

Java 11, Magnolia 6.2.11, docker image in Azure cloud


Template:
Patch included:
Yes
Acceptance criteria:
Empty
Task DoD:
[ ]* Doc/release notes changes? Comment present?
[ ]* Downstream builds green?
[ ]* Solution information and context easily available?
[ ]* Tests
[ ]* FixVersion filled and not yet released
[ ]  Architecture Decision Record (ADR)
Bug DoR:
[ ]* Steps to reproduce, expected, and actual results filled
[ ]* Affected version filled

 Description   

Magnolia creates a RestClient instance for each ES cluster configuration on start-up of the magnolia-query-manager module. This fails if magnolia is running in a cloud, where the CMS and the search service are in different containers.

To fix this, the info.magnolia.elasticsearch.ElasticsearchClient may be patched to lazily instantiate the rest clients when needed and not in advance.

@Override
    public void start(ModuleLifecycleContext moduleLifecycleContext) {
        try {
            defaultElasticsearchVersion = Version.parseVersion(PropertyUtil.getString(
                    CommonFunctionality.getSession(SearchProviderConstants.Workspaces.CONFIG)
                            .getNode(SearchProviderConstants.NodePaths.ELASTIC_CONFIG_MODULE),
                    SearchProviderConstants.NodeProperties.DEFAULT_ELASTICSEARCH_VERSION_PROPERTY));
        } catch (RepositoryException e) {
            log.error("Unable to determine default API specification for Elasticsearch.", e);
        }
        if (!ClusterItemConfiguration.apiSpecExists(defaultElasticsearchVersion)) {
            try {
                ClusterItemConfiguration.createApiSpec(defaultElasticsearchVersion);
            } catch (ElasticConfigurationException e) {
                log.error("Unable to create default API specification for Elasticsearch.", e);
            }
        }
        try {
            //for mch we create the client when needed.
//            ClusterItemConfiguration.getAllClusters().forEach((name, cluster) -> {
//                try {
//                    addClient(name, cluster);
//                } catch (ClusterConfigurationException e) {
//                    log.error("Unable to retrieve configuration for Elasticsearch.");
//                }
//            });
            indexToServer = IndexItemConfiguration.getAllIndexToClusterMappings();
        } catch (ElasticConfigurationException e) {
            log.error("Unable to retrieve configuration for Elasticsearch.");
        }
    } 
public RestClient getClient(String clusterName) { if(!clients.containsKey(clusterName)) { 
//somehow the client was not created 
try { var config = ClusterItemConfiguration.getAllClusters().get(clusterName); this.addClient(config); 
} catch (Exception e) { 
log.error("Could not create client for {}", clusterName, e); t
hrow new RuntimeException("Could not create client for " + clusterName); 
} 
} return clients.get(clusterName); }

Generated at Mon Feb 12 01:46:31 CET 2024 using Jira 9.4.2#940002-sha1:46d1a51de284217efdcb32434eab47a99af2938b.