Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Implement auto-config #48

Merged
merged 5 commits into from
Aug 12, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@

<jdk.version>1.8</jdk.version>

<hazelcast.version>4.0.2</hazelcast.version>
<hazelcast.version>4.1-SNAPSHOT</hazelcast.version>

<junit.version>4.13</junit.version>
<mockito.version>1.10.19</mockito.version>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,11 +16,18 @@
package com.hazelcast.azure;

import com.hazelcast.config.properties.PropertyDefinition;
import com.hazelcast.internal.nio.IOUtil;
import com.hazelcast.logging.ILogger;
import com.hazelcast.logging.Logger;
import com.hazelcast.spi.discovery.DiscoveryNode;
import com.hazelcast.spi.discovery.DiscoveryStrategy;
import com.hazelcast.spi.discovery.DiscoveryStrategyFactory;

import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.nio.charset.StandardCharsets;
import java.util.ArrayList;
import java.util.Collection;
import java.util.List;
Expand All @@ -31,6 +38,8 @@
*/
public class AzureDiscoveryStrategyFactory implements DiscoveryStrategyFactory {

private static final ILogger LOGGER = Logger.getLogger(AzureDiscoveryStrategyFactory.class);
mtyazici marked this conversation as resolved.
Show resolved Hide resolved

@Override
public Class<? extends DiscoveryStrategy> getDiscoveryStrategyType() {
return AzureDiscoveryStrategy.class;
Expand All @@ -50,4 +59,56 @@ public Collection<PropertyDefinition> getConfigurationProperties() {
}
return result;
}

/**
* Checks if Hazelcast is running on Azure.
* <p>
* To check if Hazelcast is running on Azure, we first check whether "internal.cloudapp.net" is present in the file
* "/etc/resolv.conf". Such an approach is not officially documented but seems like a good enough heuristic to detect an
* Azure Compute VM Instance. Since it's not the official method, we still need to make an API call to a local, non-routable
* address http://169.254.169.254/metadata/instance.
*
* @return true if running on Azure Instance
* @see https://docs.microsoft.com/en-us/azure/virtual-machines/linux/instance-metadata-service#metadata-apis
*/
@Override
public boolean isAutoDetectionApplicable() {
return azureDnsServerConfigured() && azureInstanceMetadataAvailable();
}

private static boolean azureDnsServerConfigured() {
return readFileContents("/etc/resolv.conf").contains("internal.cloudapp.net");
}

static String readFileContents(String fileName) {
InputStream is = null;
try {
File file = new File(fileName);
byte[] data = new byte[(int) file.length()];
is = new FileInputStream(file);
is.read(data);
return new String(data, StandardCharsets.UTF_8);
} catch (IOException e) {
throw new RuntimeException("Could not get " + fileName, e);
} finally {
IOUtil.closeResource(is);
}
}

private static boolean azureInstanceMetadataAvailable() {
return isEndpointAvailable("http://169.254.169.254/metadata/instance?api-version=2020-06-01");
}


static boolean isEndpointAvailable(String url) {
return !RestClient.create(url)
.withHeader("Metadata", "True")
.get()
.isEmpty();
}

@Override
public DiscoveryStrategyLevel discoveryStrategyLevel() {
return DiscoveryStrategyLevel.CLOUD_VM;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -15,20 +15,37 @@

package com.hazelcast.azure;

import com.github.tomakehurst.wiremock.junit.WireMockRule;
import com.hazelcast.config.Config;
import com.hazelcast.config.DiscoveryConfig;
import com.hazelcast.config.XmlConfigBuilder;
import com.hazelcast.internal.nio.IOUtil;
import com.hazelcast.spi.discovery.DiscoveryStrategy;
import com.hazelcast.spi.discovery.impl.DefaultDiscoveryService;
import com.hazelcast.spi.discovery.integration.DiscoveryServiceSettings;
import org.junit.Rule;
import org.junit.Test;

import java.io.BufferedWriter;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStreamWriter;
import java.nio.charset.StandardCharsets;
import java.util.Iterator;

import static com.github.tomakehurst.wiremock.client.WireMock.aResponse;
import static com.github.tomakehurst.wiremock.client.WireMock.get;
import static com.github.tomakehurst.wiremock.client.WireMock.stubFor;
import static com.github.tomakehurst.wiremock.client.WireMock.urlEqualTo;
import static com.github.tomakehurst.wiremock.core.WireMockConfiguration.wireMockConfig;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertTrue;

public class AzureDiscoveryStrategyFactoryTest {
@Rule
public WireMockRule wireMockRule = new WireMockRule(wireMockConfig().dynamicPort());

@Test
public void validConfiguration() {
Expand All @@ -51,4 +68,46 @@ private static DiscoveryStrategy createStrategy(String xmlFileName) {
Iterator<DiscoveryStrategy> strategies = service.getDiscoveryStrategies().iterator();
return strategies.next();
}

@Test
public void isEndpointAvailable() {
// given
String endpoint = "/some-endpoint";
String url = String.format("http://localhost:%d%s", wireMockRule.port(), endpoint);
stubFor(get(urlEqualTo(endpoint)).willReturn(aResponse().withStatus(200).withBody("some-body")));

// when
boolean isAvailable = AzureDiscoveryStrategyFactory.isEndpointAvailable(url);

// then
assertTrue(isAvailable);
}

@Test
public void readFileContents()
throws IOException {
// given
String expectedContents = "Hello, world!\nThis is a test with Unicode ✓.";
String testFile = createTestFile(expectedContents);

// when
String actualContents = AzureDiscoveryStrategyFactory.readFileContents(testFile);

// then
assertEquals(expectedContents, actualContents);
}

private static String createTestFile(String expectedContents)
throws IOException {
File temp = File.createTempFile("test", ".tmp");
temp.deleteOnExit();
BufferedWriter bufferedWriter = null;
try {
bufferedWriter = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(temp), StandardCharsets.UTF_8));
bufferedWriter.write(expectedContents);
} finally {
IOUtil.closeResource(bufferedWriter);
}
return temp.getAbsolutePath();
}
}