Search in sources :

Example 71 with TransportAddress

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

the class ClusterSearchShardsResponseTests method testSerialization.

public void testSerialization() throws Exception {
    Map<String, AliasFilter> indicesAndFilters = new HashMap<>();
    Set<DiscoveryNode> nodes = new HashSet<>();
    int numShards = randomIntBetween(1, 10);
    ClusterSearchShardsGroup[] clusterSearchShardsGroups = new ClusterSearchShardsGroup[numShards];
    for (int i = 0; i < numShards; i++) {
        String index = randomAsciiOfLengthBetween(3, 10);
        ShardId shardId = new ShardId(index, randomAsciiOfLength(12), i);
        String nodeId = randomAsciiOfLength(10);
        ShardRouting shardRouting = TestShardRouting.newShardRouting(shardId, nodeId, randomBoolean(), ShardRoutingState.STARTED);
        clusterSearchShardsGroups[i] = new ClusterSearchShardsGroup(shardId, new ShardRouting[] { shardRouting });
        DiscoveryNode node = new DiscoveryNode(shardRouting.currentNodeId(), new TransportAddress(TransportAddress.META_ADDRESS, randomInt(0xFFFF)), VersionUtils.randomVersion(random()));
        nodes.add(node);
        AliasFilter aliasFilter;
        if (randomBoolean()) {
            aliasFilter = new AliasFilter(RandomQueryBuilder.createQuery(random()), "alias-" + index);
        } else {
            aliasFilter = new AliasFilter(null, Strings.EMPTY_ARRAY);
        }
        indicesAndFilters.put(index, aliasFilter);
    }
    ClusterSearchShardsResponse clusterSearchShardsResponse = new ClusterSearchShardsResponse(clusterSearchShardsGroups, nodes.toArray(new DiscoveryNode[nodes.size()]), indicesAndFilters);
    SearchModule searchModule = new SearchModule(Settings.EMPTY, false, Collections.emptyList());
    List<NamedWriteableRegistry.Entry> entries = new ArrayList<>();
    entries.addAll(searchModule.getNamedWriteables());
    NamedWriteableRegistry namedWriteableRegistry = new NamedWriteableRegistry(entries);
    Version version = VersionUtils.randomVersionBetween(random(), Version.V_5_0_0, Version.CURRENT);
    try (BytesStreamOutput out = new BytesStreamOutput()) {
        out.setVersion(version);
        clusterSearchShardsResponse.writeTo(out);
        try (StreamInput in = new NamedWriteableAwareStreamInput(out.bytes().streamInput(), namedWriteableRegistry)) {
            in.setVersion(version);
            ClusterSearchShardsResponse deserialized = new ClusterSearchShardsResponse();
            deserialized.readFrom(in);
            assertArrayEquals(clusterSearchShardsResponse.getNodes(), deserialized.getNodes());
            assertEquals(clusterSearchShardsResponse.getGroups().length, deserialized.getGroups().length);
            for (int i = 0; i < clusterSearchShardsResponse.getGroups().length; i++) {
                ClusterSearchShardsGroup clusterSearchShardsGroup = clusterSearchShardsResponse.getGroups()[i];
                ClusterSearchShardsGroup deserializedGroup = deserialized.getGroups()[i];
                assertEquals(clusterSearchShardsGroup.getShardId(), deserializedGroup.getShardId());
                assertArrayEquals(clusterSearchShardsGroup.getShards(), deserializedGroup.getShards());
            }
            if (version.onOrAfter(Version.V_5_1_1_UNRELEASED)) {
                assertEquals(clusterSearchShardsResponse.getIndicesAndFilters(), deserialized.getIndicesAndFilters());
            } else {
                assertNull(deserialized.getIndicesAndFilters());
            }
        }
    }
}
Also used : NamedWriteableRegistry(org.elasticsearch.common.io.stream.NamedWriteableRegistry) AliasFilter(org.elasticsearch.search.internal.AliasFilter) DiscoveryNode(org.elasticsearch.cluster.node.DiscoveryNode) HashMap(java.util.HashMap) TransportAddress(org.elasticsearch.common.transport.TransportAddress) ArrayList(java.util.ArrayList) BytesStreamOutput(org.elasticsearch.common.io.stream.BytesStreamOutput) ShardId(org.elasticsearch.index.shard.ShardId) Version(org.elasticsearch.Version) NamedWriteableAwareStreamInput(org.elasticsearch.common.io.stream.NamedWriteableAwareStreamInput) StreamInput(org.elasticsearch.common.io.stream.StreamInput) SearchModule(org.elasticsearch.search.SearchModule) NamedWriteableAwareStreamInput(org.elasticsearch.common.io.stream.NamedWriteableAwareStreamInput) ShardRouting(org.elasticsearch.cluster.routing.ShardRouting) TestShardRouting(org.elasticsearch.cluster.routing.TestShardRouting) HashSet(java.util.HashSet)

Example 72 with TransportAddress

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

the class Netty4TransportMultiPortIntegrationIT method testThatInfosAreExposed.

@Network
public void testThatInfosAreExposed() throws Exception {
    NodesInfoResponse response = client().admin().cluster().prepareNodesInfo().clear().setTransport(true).get();
    for (NodeInfo nodeInfo : response.getNodes()) {
        assertThat(nodeInfo.getTransport().getProfileAddresses().keySet(), hasSize(1));
        assertThat(nodeInfo.getTransport().getProfileAddresses(), hasKey("client1"));
        BoundTransportAddress boundTransportAddress = nodeInfo.getTransport().getProfileAddresses().get("client1");
        for (TransportAddress transportAddress : boundTransportAddress.boundAddresses()) {
            assertThat(transportAddress, instanceOf(TransportAddress.class));
        }
        // bound addresses
        for (TransportAddress transportAddress : boundTransportAddress.boundAddresses()) {
            assertThat(transportAddress, instanceOf(TransportAddress.class));
            assertThat(transportAddress.address().getPort(), is(allOf(greaterThanOrEqualTo(randomPort), lessThanOrEqualTo(randomPort + 10))));
        }
        // publish address
        assertThat(boundTransportAddress.publishAddress(), instanceOf(TransportAddress.class));
        TransportAddress publishAddress = boundTransportAddress.publishAddress();
        assertThat(NetworkAddress.format(publishAddress.address().getAddress()), is("127.0.0.7"));
        assertThat(publishAddress.address().getPort(), is(4321));
    }
}
Also used : NodesInfoResponse(org.elasticsearch.action.admin.cluster.node.info.NodesInfoResponse) NodeInfo(org.elasticsearch.action.admin.cluster.node.info.NodeInfo) BoundTransportAddress(org.elasticsearch.common.transport.BoundTransportAddress) TransportAddress(org.elasticsearch.common.transport.TransportAddress) BoundTransportAddress(org.elasticsearch.common.transport.BoundTransportAddress) Network(org.elasticsearch.test.junit.annotations.Network)

Example 73 with TransportAddress

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

the class Netty4TransportPublishAddressIT method testDifferentPorts.

public void testDifferentPorts() throws Exception {
    if (!NetworkUtils.SUPPORTS_V6) {
        return;
    }
    logger.info("--> starting a node on ipv4 only");
    Settings ipv4Settings = Settings.builder().put("network.host", "127.0.0.1").build();
    // should bind 127.0.0.1:XYZ
    String ipv4OnlyNode = internalCluster().startNode(ipv4Settings);
    logger.info("--> starting a node on ipv4 and ipv6");
    Settings bothSettings = Settings.builder().put("network.host", "_local_").build();
    // should bind [::1]:XYZ and 127.0.0.1:XYZ+1
    internalCluster().startNode(bothSettings);
    logger.info("--> waiting for the cluster to declare itself stable");
    // fails if port of publish address does not match corresponding bound address
    ensureStableCluster(2);
    logger.info("--> checking if boundAddress matching publishAddress has same port");
    NodesInfoResponse nodesInfoResponse = client().admin().cluster().prepareNodesInfo().get();
    for (NodeInfo nodeInfo : nodesInfoResponse.getNodes()) {
        BoundTransportAddress boundTransportAddress = nodeInfo.getTransport().getAddress();
        if (nodeInfo.getNode().getName().equals(ipv4OnlyNode)) {
            assertThat(boundTransportAddress.boundAddresses().length, equalTo(1));
            assertThat(boundTransportAddress.boundAddresses()[0].getPort(), equalTo(boundTransportAddress.publishAddress().getPort()));
        } else {
            assertThat(boundTransportAddress.boundAddresses().length, greaterThan(1));
            for (TransportAddress boundAddress : boundTransportAddress.boundAddresses()) {
                assertThat(boundAddress, instanceOf(TransportAddress.class));
                TransportAddress inetBoundAddress = (TransportAddress) boundAddress;
                if (inetBoundAddress.address().getAddress() instanceof Inet4Address) {
                    // IPv4 address is preferred publish address for _local_
                    assertThat(inetBoundAddress.getPort(), equalTo(boundTransportAddress.publishAddress().getPort()));
                }
            }
        }
    }
}
Also used : NodesInfoResponse(org.elasticsearch.action.admin.cluster.node.info.NodesInfoResponse) Inet4Address(java.net.Inet4Address) NodeInfo(org.elasticsearch.action.admin.cluster.node.info.NodeInfo) BoundTransportAddress(org.elasticsearch.common.transport.BoundTransportAddress) TransportAddress(org.elasticsearch.common.transport.TransportAddress) BoundTransportAddress(org.elasticsearch.common.transport.BoundTransportAddress) Settings(org.elasticsearch.common.settings.Settings)

Example 74 with TransportAddress

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

the class Ec2DiscoveryTests method testPublicIp.

public void testPublicIp() throws InterruptedException {
    int nodes = randomInt(10);
    for (int i = 0; i < nodes; i++) {
        poorMansDNS.put(AmazonEC2Mock.PREFIX_PUBLIC_IP + (i + 1), buildNewFakeTransportAddress());
    }
    Settings nodeSettings = Settings.builder().put(DISCOVERY_EC2.HOST_TYPE_SETTING.getKey(), "public_ip").build();
    List<DiscoveryNode> discoveryNodes = buildDynamicNodes(nodeSettings, nodes);
    assertThat(discoveryNodes, hasSize(nodes));
    // We check that we are using here expected address
    int node = 1;
    for (DiscoveryNode discoveryNode : discoveryNodes) {
        TransportAddress address = discoveryNode.getAddress();
        TransportAddress expected = poorMansDNS.get(AmazonEC2Mock.PREFIX_PUBLIC_IP + node++);
        assertEquals(address, expected);
    }
}
Also used : DiscoveryNode(org.elasticsearch.cluster.node.DiscoveryNode) TransportAddress(org.elasticsearch.common.transport.TransportAddress) Settings(org.elasticsearch.common.settings.Settings)

Example 75 with TransportAddress

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

the class RetryTests method testReindexFromRemote.

public void testReindexFromRemote() throws Exception {
    NodeInfo nodeInfo = client().admin().cluster().prepareNodesInfo().get().getNodes().get(0);
    TransportAddress address = nodeInfo.getHttp().getAddress().publishAddress();
    RemoteInfo remote = new RemoteInfo("http", address.getAddress(), address.getPort(), new BytesArray("{\"match_all\":{}}"), null, null, emptyMap(), RemoteInfo.DEFAULT_SOCKET_TIMEOUT, RemoteInfo.DEFAULT_CONNECT_TIMEOUT);
    ReindexRequestBuilder request = ReindexAction.INSTANCE.newRequestBuilder(client()).source("source").destination("dest").setRemoteInfo(remote);
    testCase(ReindexAction.NAME, request, matcher().created(DOC_COUNT));
}
Also used : BytesArray(org.elasticsearch.common.bytes.BytesArray) NodeInfo(org.elasticsearch.action.admin.cluster.node.info.NodeInfo) TransportAddress(org.elasticsearch.common.transport.TransportAddress) RemoteInfo(org.elasticsearch.index.reindex.remote.RemoteInfo)

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