All Downloads are FREE. Search and download functionalities are using the official Maven repository.

org.graylog2.configuration.IndexerDiscoveryProvider Maven / Gradle / Ivy

There is a newer version: 6.1.4
Show newest version
/*
 * Copyright (C) 2020 Graylog, Inc.
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the Server Side Public License, version 1,
 * as published by MongoDB, Inc.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
 * Server Side Public License for more details.
 *
 * You should have received a copy of the Server Side Public License
 * along with this program. If not, see
 * .
 */
package org.graylog2.configuration;

import com.google.common.base.Suppliers;
import org.graylog2.bootstrap.preflight.PreflightConfig;
import org.graylog2.bootstrap.preflight.PreflightConfigResult;
import org.graylog2.bootstrap.preflight.PreflightConfigService;
import org.graylog2.cluster.Node;
import org.graylog2.cluster.NodeService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import javax.inject.Inject;
import javax.inject.Named;
import javax.inject.Provider;
import java.net.URI;
import java.util.Collections;
import java.util.List;
import java.util.function.Supplier;
import java.util.stream.Collectors;


public class IndexerDiscoveryProvider implements Provider> {

    private static final Logger LOG = LoggerFactory.getLogger(IndexerDiscoveryProvider.class);

    public static final URI DEFAULT_INDEXER_HOST = URI.create("http://127.0.0.1:9200");

    private final List hosts;
    private final PreflightConfigService preflightConfigService;
    private final NodeService nodeService;

    private final Supplier> resultsCachingSupplier;

    @Inject
    public IndexerDiscoveryProvider(
            @Named("elasticsearch_hosts") List hosts,
            PreflightConfigService preflightConfigService,
            NodeService nodeService) {
        this.hosts = hosts;
        this.preflightConfigService = preflightConfigService;
        this.nodeService = nodeService;
        this.resultsCachingSupplier = Suppliers.memoize(this::doGet);
    }

    @Override
    public List get() {
        return resultsCachingSupplier.get();
    }

    private List doGet() {

        // configured hosts, just use these and don't try any detection
        if (hosts != null && !hosts.isEmpty()) {
            return hosts;
        }

        final PreflightConfigResult preflightResult = preflightConfigService.getPreflightConfigResult();

        // if preflight is finished, we assume that there will be some datanode registered via node-service.
        if (preflightResult == PreflightConfigResult.FINISHED) {
            final List discovered = discover();
            if (!discovered.isEmpty()) {
                return discovered;
            } else {
                // TODO: we could wait here(or in the preflight check) for a datanode to register.
                throw new IllegalStateException("No Datanode available, terminating.");
            }
        }

        // if there are no configured hosts and the preflight never has run or was skipped, we should fallback
        // to our old default localhost:9200 to preserve backwards compatibility.
        LOG.info("No indexer hosts configured, using fallback {}", DEFAULT_INDEXER_HOST);
        return Collections.singletonList(DEFAULT_INDEXER_HOST);

    }

    private List discover() {
        return nodeService.allActive(Node.Type.DATANODE).values().stream()
                .map(Node::getTransportAddress)
                .map(URI::create)
                .collect(Collectors.toList());
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy