/** * Creates a new {@link ClusterNodes} by parsing the given source. * * @param source must not be {@literal null} or empty. */ private ClusterNodes(String source) { Assert.hasText(source, "Cluster nodes source must not be null or empty!"); String[] nodes = StringUtils.delimitedListToStringArray(source, COMMA); this.clusterNodes = Arrays.stream(nodes).map(node -> { String[] segments = StringUtils.delimitedListToStringArray(node, COLON); Assert.isTrue(segments.length == 2, () -> String.format("Invalid cluster node %s in %s! Must be in the format host:port!", node, source)); String host = segments[0].trim(); String port = segments[1].trim(); Assert.hasText(host, () -> String.format("No host name given cluster node %s!", node)); Assert.hasText(port, () -> String.format("No port given in cluster node %s!", node)); return new TransportAddress(toInetAddress(host), Integer.valueOf(port)); }).collect(Collectors.toList()); }
@Override public Connection createPhysicalConnection(String url, Properties info) throws SQLException { if (client == null) { synchronized (this) { if (client == null) { Settings.Builder builder = Settings.builder(); info.forEach((k, v) -> builder.put(k.toString(), v.toString())); String[] hostAndPortArray = url.split("/")[2].split(","); int length = hostAndPortArray.length; TransportAddress[] addresses = new TransportAddress[length]; try { String[] hostAndPortArr; for (int i = 0; i < length; ++i) { hostAndPortArr = hostAndPortArray[i].split(":"); addresses[i] = new TransportAddress(InetAddress.getByName(hostAndPortArr[0]), Integer.parseInt(hostAndPortArr[1])); } } catch (UnknownHostException e) { throw new SQLException(e); } client = new PreBuiltXPackTransportClient(builder.build()).addTransportAddresses(addresses); } } } Connection conn = new ElasticSearchConnection(client); createCount.incrementAndGet(); return conn; }
@Inject public ElasticsearchClient(ElasticsearchTableDescriptionProvider descriptions, ElasticsearchConnectorConfig config) throws IOException { tableDescriptions = requireNonNull(descriptions, "description is null"); ElasticsearchConnectorConfig configuration = requireNonNull(config, "config is null"); requestTimeout = configuration.getRequestTimeout(); maxAttempts = configuration.getMaxRequestRetries(); maxRetryTime = configuration.getMaxRetryTime(); for (ElasticsearchTableDescription tableDescription : tableDescriptions.getAllTableDescriptions()) { if (!clients.containsKey(tableDescription.getClusterName())) { Settings settings = Settings.builder().put("cluster.name", tableDescription.getClusterName()).build(); TransportAddress address = new TransportAddress(InetAddress.getByName(tableDescription.getHost()), tableDescription.getPort()); TransportClient client = new PreBuiltTransportClient(settings).addTransportAddress(address); clients.put(tableDescription.getClusterName(), client); } } this.columnMetadataCache = CacheBuilder.newBuilder() .expireAfterWrite(30, MINUTES) .refreshAfterWrite(15, MINUTES) .maximumSize(500) .build(asyncReloading(CacheLoader.from(this::loadColumns), executor)); }
private void initClient() throws UnknownHostException { Settings.Builder settingsBuilder = Settings.builder(); settingsBuilder.put( Settings.Builder.EMPTY_SETTINGS ); meta.getSettingsMap().entrySet().stream().forEach( ( s ) -> settingsBuilder.put( s.getKey(), environmentSubstitute( s.getValue() ) ) ); PreBuiltTransportClient tClient = new PreBuiltTransportClient( settingsBuilder.build() ); for ( Server server : meta.getServers() ) { tClient.addTransportAddress( new TransportAddress( InetAddress.getByName( environmentSubstitute( server.getAddress() ) ), server.getPort() ) ); } client = tClient; /** With the upgrade to elasticsearch 6.3.0, removed the NodeBuilder, * which was removed from the elasticsearch 5.0 API, see: * https://www.elastic.co/guide/en/elasticsearch/reference/5.0/breaking_50_java_api_changes * .html#_nodebuilder_removed */ }
return new TransportAddress(isa); } else if(request.getRemoteAddress() instanceof InetSocketAddress){ return new TransportAddress((InetSocketAddress)request.getRemoteAddress()); } else { throw new ElasticsearchSecurityException("Cannot handle this request. Remote address is "+request.getRemoteAddress()+" with request class "+request.getClass());
for (String host : hostArray) { int i = host.indexOf(":"); transportClient.addTransportAddress(new TransportAddress(InetAddress.getByName(host.substring(0, i)), Integer.parseInt(host.substring(i + 1))));
public ElasticsearchQueryBuilder(List<ElasticsearchColumnHandle> columnHandles, ElasticsearchConnectorConfig config, ElasticsearchSplit split) { requireNonNull(columnHandles, "columnHandles is null"); requireNonNull(config, "config is null"); requireNonNull(split, "split is null"); columns = columnHandles; tupleDomain = split.getTupleDomain(); index = split.getIndex(); shard = split.getShard(); type = split.getType(); Settings settings = Settings.builder() .put("client.transport.ignore_cluster_name", true) .build(); try { InetAddress address = InetAddress.getByName(split.getSearchNode()); client = new PreBuiltTransportClient(settings) .addTransportAddress(new TransportAddress(address, split.getPort())); } catch (UnknownHostException e) { throw new PrestoException(ELASTICSEARCH_CONNECTION_ERROR, format("Error connecting to search node (%s:%d)", split.getSearchNode(), split.getPort()), e); } scrollTimeout = config.getScrollTimeout(); scrollSize = config.getScrollSize(); }
InetAddress iAdress = InetAddress.getByName(ipAndPort[0]); int port = Integer.parseInt(ipAndPort[1]); threadPool.getThreadContext().putTransient(ConfigConstants.SG_REMOTE_ADDRESS, new TransportAddress(iAdress, port)); } catch (UnknownHostException | NumberFormatException e) { log.error("Cannot parse remote IP or port: {}, user injection failed.", parts[2], e);
.addTransportAddress(new TransportAddress(new InetSocketAddress(hostname, port)))) {
protected TransportClient getUserTransportClient(ClusterInfo info, String keyStore, Settings initTransportClientSettings) { final String prefix = getResourceFolder()==null?"":getResourceFolder()+"/"; Settings tcSettings = Settings.builder() .put("cluster.name", info.clustername) .put("searchguard.ssl.transport.truststore_filepath", FileHelper.getAbsoluteFilePathFromClassPath(prefix+"truststore.jks")) .put("searchguard.ssl.transport.enforce_hostname_verification", false) .put("searchguard.ssl.transport.keystore_filepath", FileHelper.getAbsoluteFilePathFromClassPath(prefix+keyStore)) .put(initTransportClientSettings) .build(); TransportClient tc = new TransportClientImpl(tcSettings, asCollection(Netty4Plugin.class, SearchGuardPlugin.class)); tc.addTransportAddress(new TransportAddress(new InetSocketAddress(info.nodeHost, info.nodePort))); return tc; }
protected TransportClient getInternalTransportClient(ClusterInfo info, Settings initTransportClientSettings) { final String prefix = getResourceFolder()==null?"":getResourceFolder()+"/"; Settings tcSettings = Settings.builder() .put("cluster.name", info.clustername) .put("searchguard.ssl.transport.truststore_filepath", FileHelper.getAbsoluteFilePathFromClassPath(prefix+"truststore.jks")) .put("searchguard.ssl.transport.enforce_hostname_verification", false) .put("searchguard.ssl.transport.keystore_filepath", FileHelper.getAbsoluteFilePathFromClassPath(prefix+"kirk-keystore.jks")) .put(initTransportClientSettings) .build(); TransportClient tc = new TransportClientImpl(tcSettings, asCollection(Netty4Plugin.class, SearchGuardPlugin.class)); tc.addTransportAddress(new TransportAddress(new InetSocketAddress(info.nodeHost, info.nodePort))); return tc; }
getThreadContext().putTransient(ConfigConstants.SG_REMOTE_ADDRESS, new TransportAddress((InetSocketAddress) Base64Helper.deserializeObject(originalRemoteAddress))); getThreadContext().putTransient(ConfigConstants.SG_REMOTE_ADDRESS, new TransportAddress((InetSocketAddress) Base64Helper.deserializeObject(originalRemoteAddress))); } else { getThreadContext().putTransient(ConfigConstants.SG_REMOTE_ADDRESS, request.remoteAddress());
client.addTransportAddress( new TransportAddress( InetAddress.getByName( transMeta.environmentSubstitute( server.getAddress() ) ), server.getPort() ) );
@Test public void transportClientClusterHealth() { try (ElasticsearchContainer container = new ElasticsearchContainer()) { container.start(); TransportAddress transportAddress = new TransportAddress(container.getTcpHost()); String expectedClusterName = "docker-cluster"; Settings settings = Settings.builder().put("cluster.name", expectedClusterName).build(); try (TransportClient transportClient = new PreBuiltTransportClient(settings) .addTransportAddress(transportAddress)) { ClusterHealthResponse healths = transportClient.admin().cluster().prepareHealth().get(); String clusterName = healths.getClusterName(); assertThat(clusterName, is(expectedClusterName)); } } }
@Override public void readFrom(StreamInput in) throws IOException { int boundAddressLength = in.readInt(); boundAddresses = new TransportAddress[boundAddressLength]; for (int i = 0; i < boundAddressLength; i++) { boundAddresses[i] = new TransportAddress(in); } publishAddress = new TransportAddress(in); }
/** * Temporary method that allows turning a {@link NodeView} into a {@link DiscoveryNode}. This representation will never be used in * practice, because in 6.4 and above a consumer of the response will only be able to retrieve a representation of {@link NodeView} * objects. * * Effectively this will be used to hold the state of the object in 6.x so there is no need to have 2 backing objects that * represent the state of the Response. In practice these will always be read by a consumer as a NodeView, but it eases the * transition to master which will not contain any representation of a {@link DiscoveryNode}. */ DiscoveryNode convertToDiscoveryNode() { return new DiscoveryNode(name, nodeId, "", "", "", new TransportAddress(TransportAddress.META_ADDRESS, 0), Collections.emptyMap(), Collections.emptySet(), Version.CURRENT); }
static DiscoveryNode buildSeedNode(String clusterName, String address, boolean proxyMode) { if (proxyMode) { TransportAddress transportAddress = new TransportAddress(TransportAddress.META_ADDRESS, 0); String hostName = address.substring(0, indexOfPortSeparator(address)); return new DiscoveryNode("", clusterName + "#" + address, UUIDs.randomBase64UUID(), hostName, address, transportAddress, Collections.singletonMap("server_name", hostName), EnumSet.allOf(DiscoveryNode.Role.class), Version.CURRENT.minimumCompatibilityVersion()); } else { TransportAddress transportAddress = new TransportAddress(RemoteClusterAware.parseSeedAddress(address)); return new DiscoveryNode(clusterName + "#" + transportAddress.toString(), transportAddress, Version.CURRENT.minimumCompatibilityVersion()); } }
tc.addTransportAddress(new TransportAddress(new InetSocketAddress(info.nodeHost, info.nodePort))); Assert.assertEquals(info.numNodes, tc.admin().cluster().nodesInfo(new NodesInfoRequest()).actionGet().getNodes().size());
private <T extends TransportResponse> void handleResponse(InetSocketAddress remoteAddress, final StreamInput stream, final TransportResponseHandler<T> handler) { final T response; try { response = handler.read(stream); response.remoteAddress(new TransportAddress(remoteAddress)); } catch (Exception e) { handleException(handler, new TransportSerializationException( "Failed to deserialize response from handler [" + handler.getClass().getName() + "]", e)); return; } threadPool.executor(handler.executor()).execute(new AbstractRunnable() { @Override public void onFailure(Exception e) { handleException(handler, new ResponseHandlerFailureTransportException(e)); } @Override protected void doRun() throws Exception { handler.handleResponse(response); } }); }
private static DiscoveryNode maybeAddProxyAddress(String proxyAddress, DiscoveryNode node) { if (proxyAddress == null || proxyAddress.isEmpty()) { return node; } else { // resovle proxy address lazy here InetSocketAddress proxyInetAddress = RemoteClusterAware.parseSeedAddress(proxyAddress); return new DiscoveryNode(node.getName(), node.getId(), node.getEphemeralId(), node.getHostName(), node .getHostAddress(), new TransportAddress(proxyInetAddress), node.getAttributes(), node.getRoles(), node.getVersion()); } }