Search in sources :

Example 66 with TransportAddress

use of org.elasticsearch.common.transport.TransportAddress in project bboss-elastic by bbossgroups.

the class ElasticSearchTransportClient method openClient.

// @Override
// public void addEvent(Event event, IndexNameBuilder indexNameBuilder, String indexType, long ttlMs)
// throws Exception {
// if (bulkRequestBuilder == null) {
// bulkRequestBuilder = client.prepareBulk();
// }
// 
// IndexRequestBuilder indexRequestBuilder = null;
// if (indexRequestBuilderFactory == null) {
// XContentBuilder bytesStream = null;
// try {
// bytesStream = serializer.getContentBuilder(event);
// indexRequestBuilder = client.prepareIndex(indexNameBuilder.getIndexName(event), indexType)
// .setSource(bytesStream);
// } finally {
// if (bytesStream != null) {
// // bytesStream.cl
// }
// }
// 
// } else {
// indexRequestBuilder = indexRequestBuilderFactory.createIndexRequest(client,
// indexNameBuilder.getIndexPrefix(event), indexType, event);
// }
// 
// if (ttlMs > 0) {
// indexRequestBuilder.setTTL(ttlMs);
// }
// bulkRequestBuilder.add(indexRequestBuilder);
// }
/**
 * Open client to elaticsearch cluster
 *
 * @param clusterName
 */
private void openClient(String clusterName) {
    logger.info("Using ElasticSearch hostnames: {} ", Arrays.toString(serverAddresses));
    Settings settings = null;
    Settings.Builder builder = Settings.builder();
    if (this.elasticUser != null && !this.elasticUser.equals("")) {
        builder.put("cluster.name", clusterName).put("xpack.security.user", this.elasticUser + ":" + this.elasticPassword);
    // .put("shield.user",
    // this.elasticUser+":"+this.elasticPassword)
    } else {
        builder.put("cluster.name", clusterName);
    }
    if (this.extendElasticsearchPropes != null && extendElasticsearchPropes.size() > 0) {
        Iterator<Entry<Object, Object>> iterator = extendElasticsearchPropes.entrySet().iterator();
        while (iterator.hasNext()) {
            Entry<Object, Object> entry = iterator.next();
            builder.put((String) entry.getKey(), String.valueOf(entry.getValue()));
        }
    }
    settings = builder.build();
    try {
        TransportClient transportClient = this.elasticUser != null && !this.elasticUser.equals("") ? new PreBuiltXPackTransportClient(settings) : new PreBuiltTransportClient(settings);
        // TransportClient transportClient = new TransportClient(settings);
        for (TransportAddress host : serverAddresses) {
            transportClient.addTransportAddress(host);
        }
        if (client != null) {
            client.close();
        }
        client = transportClient;
    } catch (RuntimeException e) {
        e.printStackTrace();
    } catch (Throwable e) {
        e.printStackTrace();
    }
}
Also used : Entry(java.util.Map.Entry) PreBuiltXPackTransportClient(org.elasticsearch.xpack.client.PreBuiltXPackTransportClient) PreBuiltXPackTransportClient(org.elasticsearch.xpack.client.PreBuiltXPackTransportClient) TransportClient(org.elasticsearch.client.transport.TransportClient) PreBuiltTransportClient(org.elasticsearch.transport.client.PreBuiltTransportClient) PreBuiltTransportClient(org.elasticsearch.transport.client.PreBuiltTransportClient) TransportAddress(org.elasticsearch.common.transport.TransportAddress) Settings(org.elasticsearch.common.settings.Settings)

Example 67 with TransportAddress

use of org.elasticsearch.common.transport.TransportAddress in project bboss-elastic by bbossgroups.

the class ElasticSearchTransportClient method configureHostnames.

private void configureHostnames(String[] hostNames) throws UnknownHostException {
    logger.warn(Arrays.toString(hostNames));
    serverAddresses = new TransportAddress[hostNames.length];
    for (int i = 0; i < hostNames.length; i++) {
        String[] hostPort = hostNames[i].trim().split(":");
        String host = hostPort[0].trim();
        int port = hostPort.length == 2 ? Integer.parseInt(hostPort[1].trim()) : DEFAULT_PORT;
        // serverAddresses[i] = new InetSocketTransportAddress(host, port);
        serverAddresses[i] = new TransportAddress(InetAddress.getByName(host), port);
    }
}
Also used : TransportAddress(org.elasticsearch.common.transport.TransportAddress)

Example 68 with TransportAddress

use of org.elasticsearch.common.transport.TransportAddress in project elasticsearch by elastic.

the class TransportClient method buildTemplate.

private static ClientTemplate buildTemplate(Settings providedSettings, Settings defaultSettings, Collection<Class<? extends Plugin>> plugins, HostFailureListener failureListner) {
    if (Node.NODE_NAME_SETTING.exists(providedSettings) == false) {
        providedSettings = Settings.builder().put(providedSettings).put(Node.NODE_NAME_SETTING.getKey(), "_client_").build();
    }
    final PluginsService pluginsService = newPluginService(providedSettings, plugins);
    final Settings settings = Settings.builder().put(defaultSettings).put(pluginsService.updatedSettings()).build();
    final List<Closeable> resourcesToClose = new ArrayList<>();
    final ThreadPool threadPool = new ThreadPool(settings);
    resourcesToClose.add(() -> ThreadPool.terminate(threadPool, 10, TimeUnit.SECONDS));
    final NetworkService networkService = new NetworkService(settings, Collections.emptyList());
    try {
        final List<Setting<?>> additionalSettings = new ArrayList<>();
        final List<String> additionalSettingsFilter = new ArrayList<>();
        additionalSettings.addAll(pluginsService.getPluginSettings());
        additionalSettingsFilter.addAll(pluginsService.getPluginSettingsFilter());
        for (final ExecutorBuilder<?> builder : threadPool.builders()) {
            additionalSettings.addAll(builder.getRegisteredSettings());
        }
        SettingsModule settingsModule = new SettingsModule(settings, additionalSettings, additionalSettingsFilter);
        SearchModule searchModule = new SearchModule(settings, true, pluginsService.filterPlugins(SearchPlugin.class));
        List<NamedWriteableRegistry.Entry> entries = new ArrayList<>();
        entries.addAll(NetworkModule.getNamedWriteables());
        entries.addAll(searchModule.getNamedWriteables());
        entries.addAll(ClusterModule.getNamedWriteables());
        entries.addAll(pluginsService.filterPlugins(Plugin.class).stream().flatMap(p -> p.getNamedWriteables().stream()).collect(Collectors.toList()));
        NamedWriteableRegistry namedWriteableRegistry = new NamedWriteableRegistry(entries);
        NamedXContentRegistry xContentRegistry = new NamedXContentRegistry(Stream.of(searchModule.getNamedXContents().stream(), pluginsService.filterPlugins(Plugin.class).stream().flatMap(p -> p.getNamedXContent().stream())).flatMap(Function.identity()).collect(toList()));
        ModulesBuilder modules = new ModulesBuilder();
        // plugin modules must be added here, before others or we can get crazy injection errors...
        for (Module pluginModule : pluginsService.createGuiceModules()) {
            modules.add(pluginModule);
        }
        modules.add(b -> b.bind(ThreadPool.class).toInstance(threadPool));
        ActionModule actionModule = new ActionModule(true, settings, null, settingsModule.getIndexScopedSettings(), settingsModule.getClusterSettings(), settingsModule.getSettingsFilter(), threadPool, pluginsService.filterPlugins(ActionPlugin.class), null, null);
        modules.add(actionModule);
        CircuitBreakerService circuitBreakerService = Node.createCircuitBreakerService(settingsModule.getSettings(), settingsModule.getClusterSettings());
        resourcesToClose.add(circuitBreakerService);
        BigArrays bigArrays = new BigArrays(settings, circuitBreakerService);
        resourcesToClose.add(bigArrays);
        modules.add(settingsModule);
        NetworkModule networkModule = new NetworkModule(settings, true, pluginsService.filterPlugins(NetworkPlugin.class), threadPool, bigArrays, circuitBreakerService, namedWriteableRegistry, xContentRegistry, networkService, null);
        final Transport transport = networkModule.getTransportSupplier().get();
        final TransportService transportService = new TransportService(settings, transport, threadPool, networkModule.getTransportInterceptor(), boundTransportAddress -> DiscoveryNode.createLocal(settings, new TransportAddress(TransportAddress.META_ADDRESS, 0), UUIDs.randomBase64UUID()), null);
        modules.add((b -> {
            b.bind(BigArrays.class).toInstance(bigArrays);
            b.bind(PluginsService.class).toInstance(pluginsService);
            b.bind(CircuitBreakerService.class).toInstance(circuitBreakerService);
            b.bind(NamedWriteableRegistry.class).toInstance(namedWriteableRegistry);
            b.bind(Transport.class).toInstance(transport);
            b.bind(TransportService.class).toInstance(transportService);
            b.bind(NetworkService.class).toInstance(networkService);
        }));
        Injector injector = modules.createInjector();
        final TransportClientNodesService nodesService = new TransportClientNodesService(settings, transportService, threadPool, failureListner == null ? (t, e) -> {
        } : failureListner);
        final TransportProxyClient proxy = new TransportProxyClient(settings, transportService, nodesService, actionModule.getActions().values().stream().map(x -> x.getAction()).collect(Collectors.toList()));
        List<LifecycleComponent> pluginLifecycleComponents = new ArrayList<>();
        pluginLifecycleComponents.addAll(pluginsService.getGuiceServiceClasses().stream().map(injector::getInstance).collect(Collectors.toList()));
        resourcesToClose.addAll(pluginLifecycleComponents);
        transportService.start();
        transportService.acceptIncomingRequests();
        ClientTemplate transportClient = new ClientTemplate(injector, pluginLifecycleComponents, nodesService, proxy, namedWriteableRegistry);
        resourcesToClose.clear();
        return transportClient;
    } finally {
        IOUtils.closeWhileHandlingException(resourcesToClose);
    }
}
Also used : NamedWriteableRegistry(org.elasticsearch.common.io.stream.NamedWriteableRegistry) Arrays(java.util.Arrays) Injector(org.elasticsearch.common.inject.Injector) NetworkModule(org.elasticsearch.common.network.NetworkModule) BigArrays(org.elasticsearch.common.util.BigArrays) Settings(org.elasticsearch.common.settings.Settings) TimeValue.timeValueSeconds(org.elasticsearch.common.unit.TimeValue.timeValueSeconds) ThreadPool(org.elasticsearch.threadpool.ThreadPool) NamedXContentRegistry(org.elasticsearch.common.xcontent.NamedXContentRegistry) ActionRequest(org.elasticsearch.action.ActionRequest) PluginsService(org.elasticsearch.plugins.PluginsService) Transport(org.elasticsearch.transport.Transport) Setting(org.elasticsearch.common.settings.Setting) Collection(java.util.Collection) UUIDs(org.elasticsearch.common.UUIDs) ClusterModule(org.elasticsearch.cluster.ClusterModule) Collectors(java.util.stream.Collectors) AbstractClient(org.elasticsearch.client.support.AbstractClient) List(java.util.List) Stream(java.util.stream.Stream) TransportAddress(org.elasticsearch.common.transport.TransportAddress) SearchPlugin(org.elasticsearch.plugins.SearchPlugin) Module(org.elasticsearch.common.inject.Module) InternalSettingsPreparer(org.elasticsearch.node.InternalSettingsPreparer) Function(java.util.function.Function) CircuitBreakerService(org.elasticsearch.indices.breaker.CircuitBreakerService) ArrayList(java.util.ArrayList) ActionModule(org.elasticsearch.action.ActionModule) DiscoveryNode(org.elasticsearch.cluster.node.DiscoveryNode) NetworkService(org.elasticsearch.common.network.NetworkService) SettingsModule(org.elasticsearch.common.settings.SettingsModule) NetworkPlugin(org.elasticsearch.plugins.NetworkPlugin) NamedWriteableRegistry(org.elasticsearch.common.io.stream.NamedWriteableRegistry) TcpTransport(org.elasticsearch.transport.TcpTransport) TimeValue(org.elasticsearch.common.unit.TimeValue) Node(org.elasticsearch.node.Node) ExecutorBuilder(org.elasticsearch.threadpool.ExecutorBuilder) TransportService(org.elasticsearch.transport.TransportService) ActionRequestBuilder(org.elasticsearch.action.ActionRequestBuilder) SearchModule(org.elasticsearch.search.SearchModule) ActionPlugin(org.elasticsearch.plugins.ActionPlugin) ActionResponse(org.elasticsearch.action.ActionResponse) IOUtils(org.apache.lucene.util.IOUtils) Plugin(org.elasticsearch.plugins.Plugin) Action(org.elasticsearch.action.Action) TimeUnit(java.util.concurrent.TimeUnit) Collectors.toList(java.util.stream.Collectors.toList) LifecycleComponent(org.elasticsearch.common.component.LifecycleComponent) Closeable(java.io.Closeable) ModulesBuilder(org.elasticsearch.common.inject.ModulesBuilder) Collections(java.util.Collections) ActionListener(org.elasticsearch.action.ActionListener) NetworkPlugin(org.elasticsearch.plugins.NetworkPlugin) TransportAddress(org.elasticsearch.common.transport.TransportAddress) Closeable(java.io.Closeable) ActionModule(org.elasticsearch.action.ActionModule) ArrayList(java.util.ArrayList) ThreadPool(org.elasticsearch.threadpool.ThreadPool) ActionPlugin(org.elasticsearch.plugins.ActionPlugin) LifecycleComponent(org.elasticsearch.common.component.LifecycleComponent) Injector(org.elasticsearch.common.inject.Injector) NamedXContentRegistry(org.elasticsearch.common.xcontent.NamedXContentRegistry) CircuitBreakerService(org.elasticsearch.indices.breaker.CircuitBreakerService) Settings(org.elasticsearch.common.settings.Settings) PluginsService(org.elasticsearch.plugins.PluginsService) Setting(org.elasticsearch.common.settings.Setting) SearchPlugin(org.elasticsearch.plugins.SearchPlugin) BigArrays(org.elasticsearch.common.util.BigArrays) TransportService(org.elasticsearch.transport.TransportService) SettingsModule(org.elasticsearch.common.settings.SettingsModule) NetworkService(org.elasticsearch.common.network.NetworkService) SearchModule(org.elasticsearch.search.SearchModule) ModulesBuilder(org.elasticsearch.common.inject.ModulesBuilder) NetworkModule(org.elasticsearch.common.network.NetworkModule) ClusterModule(org.elasticsearch.cluster.ClusterModule) Module(org.elasticsearch.common.inject.Module) ActionModule(org.elasticsearch.action.ActionModule) SettingsModule(org.elasticsearch.common.settings.SettingsModule) SearchModule(org.elasticsearch.search.SearchModule) Transport(org.elasticsearch.transport.Transport) TcpTransport(org.elasticsearch.transport.TcpTransport) NetworkModule(org.elasticsearch.common.network.NetworkModule)

Example 69 with TransportAddress

use of org.elasticsearch.common.transport.TransportAddress in project elasticsearch by elastic.

the class Node method writePortsFile.

/** Writes a file to the logs dir containing the ports for the given transport type */
private void writePortsFile(String type, BoundTransportAddress boundAddress) {
    Path tmpPortsFile = environment.logsFile().resolve(type + ".ports.tmp");
    try (BufferedWriter writer = Files.newBufferedWriter(tmpPortsFile, Charset.forName("UTF-8"))) {
        for (TransportAddress address : boundAddress.boundAddresses()) {
            InetAddress inetAddress = InetAddress.getByName(address.getAddress());
            if (inetAddress instanceof Inet6Address && inetAddress.isLinkLocalAddress()) {
                // no link local, just causes problems
                continue;
            }
            writer.write(NetworkAddress.format(new InetSocketAddress(inetAddress, address.getPort())) + "\n");
        }
    } catch (IOException e) {
        throw new RuntimeException("Failed to write ports file", e);
    }
    Path portsFile = environment.logsFile().resolve(type + ".ports");
    try {
        Files.move(tmpPortsFile, portsFile, StandardCopyOption.ATOMIC_MOVE);
    } catch (IOException e) {
        throw new RuntimeException("Failed to rename ports file", e);
    }
}
Also used : Path(java.nio.file.Path) BoundTransportAddress(org.elasticsearch.common.transport.BoundTransportAddress) TransportAddress(org.elasticsearch.common.transport.TransportAddress) InetSocketAddress(java.net.InetSocketAddress) Inet6Address(java.net.Inet6Address) IOException(java.io.IOException) InetAddress(java.net.InetAddress) BufferedWriter(java.io.BufferedWriter)

Example 70 with TransportAddress

use of org.elasticsearch.common.transport.TransportAddress in project elasticsearch by elastic.

the class TcpTransport method parse.

/** parse a hostname+port range spec into its equivalent addresses */
static TransportAddress[] parse(String hostPortString, String defaultPortRange, int perAddressLimit) throws UnknownHostException {
    Objects.requireNonNull(hostPortString);
    String host;
    String portString = null;
    if (hostPortString.startsWith("[")) {
        // Parse a bracketed host, typically an IPv6 literal.
        Matcher matcher = BRACKET_PATTERN.matcher(hostPortString);
        if (!matcher.matches()) {
            throw new IllegalArgumentException("Invalid bracketed host/port range: " + hostPortString);
        }
        host = matcher.group(1);
        // could be null
        portString = matcher.group(2);
    } else {
        int colonPos = hostPortString.indexOf(':');
        if (colonPos >= 0 && hostPortString.indexOf(':', colonPos + 1) == -1) {
            // Exactly 1 colon.  Split into host:port.
            host = hostPortString.substring(0, colonPos);
            portString = hostPortString.substring(colonPos + 1);
        } else {
            // 0 or 2+ colons.  Bare hostname or IPv6 literal.
            host = hostPortString;
            // 2+ colons and not bracketed: exception
            if (colonPos >= 0) {
                throw new IllegalArgumentException("IPv6 addresses must be bracketed: " + hostPortString);
            }
        }
    }
    // if port isn't specified, fill with the default
    if (portString == null || portString.isEmpty()) {
        portString = defaultPortRange;
    }
    // generate address for each port in the range
    Set<InetAddress> addresses = new HashSet<>(Arrays.asList(InetAddress.getAllByName(host)));
    List<TransportAddress> transportAddresses = new ArrayList<>();
    int[] ports = new PortsRange(portString).ports();
    int limit = Math.min(ports.length, perAddressLimit);
    for (int i = 0; i < limit; i++) {
        for (InetAddress address : addresses) {
            transportAddresses.add(new TransportAddress(address, ports[i]));
        }
    }
    return transportAddresses.toArray(new TransportAddress[transportAddresses.size()]);
}
Also used : Matcher(java.util.regex.Matcher) TransportAddress(org.elasticsearch.common.transport.TransportAddress) BoundTransportAddress(org.elasticsearch.common.transport.BoundTransportAddress) ArrayList(java.util.ArrayList) InetAddress(java.net.InetAddress) PortsRange(org.elasticsearch.common.transport.PortsRange) HashSet(java.util.HashSet) IntHashSet(com.carrotsearch.hppc.IntHashSet)

Aggregations

TransportAddress (org.elasticsearch.common.transport.TransportAddress)129 Settings (org.elasticsearch.common.settings.Settings)46 DiscoveryNode (org.elasticsearch.cluster.node.DiscoveryNode)45 BoundTransportAddress (org.elasticsearch.common.transport.BoundTransportAddress)33 ArrayList (java.util.ArrayList)31 IOException (java.io.IOException)30 InetSocketAddress (java.net.InetSocketAddress)28 InetAddress (java.net.InetAddress)22 PreBuiltTransportClient (org.elasticsearch.transport.client.PreBuiltTransportClient)20 HashSet (java.util.HashSet)16 UnknownHostException (java.net.UnknownHostException)15 TransportClient (org.elasticsearch.client.transport.TransportClient)15 TransportService (org.elasticsearch.transport.TransportService)14 NamedWriteableRegistry (org.elasticsearch.common.io.stream.NamedWriteableRegistry)13 AtomicReference (java.util.concurrent.atomic.AtomicReference)12 ClusterState (org.elasticsearch.cluster.ClusterState)12 NetworkService (org.elasticsearch.common.network.NetworkService)12 ThreadPool (org.elasticsearch.threadpool.ThreadPool)12 HashMap (java.util.HashMap)11 List (java.util.List)11