use of org.opensearch.common.transport.TransportAddress in project OpenSearch by opensearch-project.
the class GceSeedHostsProvider method getSeedAddresses.
/**
* We build the list of Nodes from GCE Management API
* Information can be cached using `cloud.gce.refresh_interval` property if needed.
*/
@Override
public List<TransportAddress> getSeedAddresses(HostsResolver hostsResolver) {
// We check that needed properties have been set
if (this.project == null || this.project.isEmpty() || this.zones == null || this.zones.isEmpty()) {
throw new IllegalArgumentException("one or more gce discovery settings are missing. " + "Check opensearch.yml file. Should have [" + GceInstancesService.PROJECT_SETTING.getKey() + "] and [" + GceInstancesService.ZONE_SETTING.getKey() + "].");
}
if (refreshInterval.millis() != 0) {
if (cachedDynamicHosts != null && (refreshInterval.millis() < 0 || (System.currentTimeMillis() - lastRefresh) < refreshInterval.millis())) {
if (logger.isTraceEnabled())
logger.trace("using cache to retrieve node list");
return cachedDynamicHosts;
}
lastRefresh = System.currentTimeMillis();
}
logger.debug("start building nodes list using GCE API");
cachedDynamicHosts = new ArrayList<>();
String ipAddress = null;
try {
InetAddress inetAddress = networkService.resolvePublishHostAddresses(NetworkService.GLOBAL_NETWORK_PUBLISH_HOST_SETTING.get(settings).toArray(Strings.EMPTY_ARRAY));
if (inetAddress != null) {
ipAddress = NetworkAddress.format(inetAddress);
}
} catch (IOException e) {
// We can't find the publish host address... Hmmm. Too bad :-(
// We won't simply filter it
}
try {
Collection<Instance> instances = gceInstancesService.instances();
if (instances == null) {
logger.trace("no instance found for project [{}], zones [{}].", this.project, this.zones);
return cachedDynamicHosts;
}
for (Instance instance : instances) {
String name = instance.getName();
String type = instance.getMachineType();
String status = instance.getStatus();
logger.trace("gce instance {} with status {} found.", name, status);
// See https://github.com/elastic/elasticsearch-cloud-gce/issues/3
if (Status.TERMINATED.equals(status)) {
logger.debug("node {} is TERMINATED. Ignoring", name);
continue;
}
// see if we need to filter by tag
boolean filterByTag = false;
if (tags.isEmpty() == false) {
logger.trace("start filtering instance {} with tags {}.", name, tags);
if (instance.getTags() == null || instance.getTags().isEmpty() || instance.getTags().getItems() == null || instance.getTags().getItems().isEmpty()) {
// If this instance have no tag, we filter it
logger.trace("no tags for this instance but we asked for tags. {} won't be part of the cluster.", name);
filterByTag = true;
} else {
// check that all tags listed are there on the instance
logger.trace("comparing instance tags {} with tags filter {}.", instance.getTags().getItems(), tags);
for (String tag : tags) {
boolean found = false;
for (String instancetag : instance.getTags().getItems()) {
if (instancetag.equals(tag)) {
found = true;
break;
}
}
if (!found) {
filterByTag = true;
break;
}
}
}
}
if (filterByTag) {
logger.trace("filtering out instance {} based tags {}, not part of {}", name, tags, instance.getTags() == null || instance.getTags().getItems() == null ? "" : instance.getTags());
continue;
} else {
logger.trace("instance {} with tags {} is added to discovery", name, tags);
}
String ip_public = null;
String ip_private = null;
List<NetworkInterface> interfaces = instance.getNetworkInterfaces();
for (NetworkInterface networkInterface : interfaces) {
if (ip_public == null) {
// Trying to get Public IP Address (For future use)
if (networkInterface.getAccessConfigs() != null) {
for (AccessConfig accessConfig : networkInterface.getAccessConfigs()) {
if (Strings.hasText(accessConfig.getNatIP())) {
ip_public = accessConfig.getNatIP();
break;
}
}
}
}
if (ip_private == null) {
ip_private = networkInterface.getNetworkIP();
}
// If we have both public and private, we can stop here
if (ip_private != null && ip_public != null)
break;
}
try {
if (ip_private.equals(ipAddress)) {
// We found the current node.
// We can ignore it in the list of DiscoveryNode
logger.trace("current node found. Ignoring {} - {}", name, ip_private);
} else {
String address = ip_private;
// Test if we have opensearch_port metadata defined here
if (instance.getMetadata() != null && instance.getMetadata().containsKey("opensearch_port")) {
Object opensearch_port = instance.getMetadata().get("opensearch_port");
logger.trace("opensearch_port is defined with {}", opensearch_port);
if (opensearch_port instanceof String) {
address = address.concat(":").concat((String) opensearch_port);
} else {
// Ignoring other values
logger.trace("opensearch_port is instance of {}. Ignoring...", opensearch_port.getClass().getName());
}
}
// ip_private is a single IP Address. We need to build a TransportAddress from it
// If user has set `opensearch_port` metadata, we don't need to ping all ports
TransportAddress[] addresses = transportService.addressesFromString(address);
for (TransportAddress transportAddress : addresses) {
logger.trace("adding {}, type {}, address {}, transport_address {}, status {}", name, type, ip_private, transportAddress, status);
cachedDynamicHosts.add(transportAddress);
}
}
} catch (Exception e) {
final String finalIpPrivate = ip_private;
logger.warn((Supplier<?>) () -> new ParameterizedMessage("failed to add {}, address {}", name, finalIpPrivate), e);
}
}
} catch (Exception e) {
logger.warn("exception caught during discovery", e);
}
logger.debug("{} addresses added", cachedDynamicHosts.size());
logger.debug("using transport addresses {}", cachedDynamicHosts);
return cachedDynamicHosts;
}
use of org.opensearch.common.transport.TransportAddress in project OpenSearch by opensearch-project.
the class GceDiscoveryTests method testNodesWithDifferentTagsAndOneTagSet.
public void testNodesWithDifferentTagsAndOneTagSet() {
Settings nodeSettings = Settings.builder().put(GceInstancesServiceImpl.PROJECT_SETTING.getKey(), projectName).put(GceInstancesServiceImpl.ZONE_SETTING.getKey(), "europe-west1-b").putList(GceSeedHostsProvider.TAGS_SETTING.getKey(), "opensearch").build();
mock = new GceInstancesServiceMock(nodeSettings);
List<TransportAddress> dynamicHosts = buildDynamicNodes(mock, nodeSettings);
assertThat(dynamicHosts, hasSize(1));
}
use of org.opensearch.common.transport.TransportAddress in project OpenSearch by opensearch-project.
the class GceDiscoveryTests method testMultipleZonesAndTwoNodesInSameZone.
public void testMultipleZonesAndTwoNodesInSameZone() {
Settings nodeSettings = Settings.builder().put(GceInstancesServiceImpl.PROJECT_SETTING.getKey(), projectName).putList(GceInstancesServiceImpl.ZONE_SETTING.getKey(), "us-central1-a", "europe-west1-b").build();
mock = new GceInstancesServiceMock(nodeSettings);
List<TransportAddress> dynamicHosts = buildDynamicNodes(mock, nodeSettings);
assertThat(dynamicHosts, hasSize(2));
}
use of org.opensearch.common.transport.TransportAddress in project OpenSearch by opensearch-project.
the class GceDiscoveryTests method testNodesWithSameTagsAndNoTagSet.
public void testNodesWithSameTagsAndNoTagSet() {
Settings nodeSettings = Settings.builder().put(GceInstancesServiceImpl.PROJECT_SETTING.getKey(), projectName).put(GceInstancesServiceImpl.ZONE_SETTING.getKey(), "europe-west1-b").build();
mock = new GceInstancesServiceMock(nodeSettings);
List<TransportAddress> dynamicHosts = buildDynamicNodes(mock, nodeSettings);
assertThat(dynamicHosts, hasSize(2));
}
use of org.opensearch.common.transport.TransportAddress in project OpenSearch by opensearch-project.
the class GceDiscoveryTests method testNoRegionReturnsEmptyList.
/**
* For issue https://github.com/elastic/elasticsearch/issues/16967:
* When using multiple regions and one of them has no instance at all, this
* was producing a NPE as a result.
*/
public void testNoRegionReturnsEmptyList() {
Settings nodeSettings = Settings.builder().put(GceInstancesServiceImpl.PROJECT_SETTING.getKey(), projectName).putList(GceInstancesServiceImpl.ZONE_SETTING.getKey(), "europe-west1-b", "us-central1-a").build();
mock = new GceInstancesServiceMock(nodeSettings);
List<TransportAddress> dynamicHosts = buildDynamicNodes(mock, nodeSettings);
assertThat(dynamicHosts, hasSize(1));
}
Aggregations