Skip to content

Commit

Permalink
fixed discovery port retrieval & bugs
Browse files Browse the repository at this point in the history
  • Loading branch information
pecollet committed Mar 15, 2024
1 parent a817763 commit e2ef03b
Show file tree
Hide file tree
Showing 5 changed files with 149 additions and 42 deletions.
10 changes: 8 additions & 2 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@

<groupId>com.neo4j</groupId>
<artifactId>aws-ec2-asg-discovery</artifactId>
<version>0.4.1</version>
<version>0.4.2</version>

<properties>
<maven.compiler.source>17</maven.compiler.source>
Expand All @@ -30,7 +30,7 @@
<dependencies>
<dependency>
<groupId>com.neo4j</groupId>
<artifactId>neo4j-enterprise</artifactId>
<artifactId>neo4j-discovery</artifactId>
<version>${neo4j.version}</version>
<scope>provided</scope>
</dependency>
Expand Down Expand Up @@ -61,6 +61,12 @@
<version>5.10.1</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.mockito</groupId>
<artifactId>mockito-core</artifactId>
<version>5.11.0</version>
<scope>test</scope>
</dependency>
</dependencies>
<build>
<plugins>
Expand Down
21 changes: 13 additions & 8 deletions src/main/java/cs/neo4j/Ec2DiscoveryResolver.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import static com.neo4j.configuration.ClusterBaseSettings.DEFAULT_DISCOVERY_PORT;
import static com.neo4j.configuration.ClusterBaseSettings.DEFAULT_TRANSACTION_PORT;
import com.neo4j.causalclustering.discovery.resolve.RemotesResolver;
import com.neo4j.configuration.ClusterAddressSettings;
import com.neo4j.configuration.DiscoverySettings;
import org.neo4j.annotations.service.ServiceProvider;
import org.neo4j.configuration.helpers.SocketAddress;
Expand Down Expand Up @@ -32,6 +33,8 @@ public class Ec2DiscoveryResolver implements RemotesResolver {
private int discoveryPort;
private AwsClient awsClient;

private static String tagSeparator = ":";

@Override
public String getName() {
return NAME;
Expand All @@ -50,16 +53,21 @@ public void init(Type type, Configuration configuration, LogProvider logProvider
this.awsSecret = configuration.get(Ec2DiscoverySettings.aws_secret);
this.addressType = configuration.get(Ec2DiscoverySettings.aws_address_type);
this.vmTag = configuration.get(Ec2DiscoverySettings.vm_tag);
this.vmTagKey = this.vmTag.split(",")[0];
this.vmTagValue = this.vmTag.split(",")[1];
if (this.vmTag != null) {
this.vmTagKey = this.vmTag.split(tagSeparator)[0];
this.vmTagValue = this.vmTag.split(tagSeparator)[1];
}

discoveryPort = getDiscoveryPort();
awsClient = instantiateAwsClient();
}

private int getDiscoveryPort() {
public int getDiscoveryPort() {
int port=0;
port = configuration.get(DiscoverySettings.discovery_listen_address).getPort();
port = switch (this.type) {
case DISCOVERY -> configuration.get(ClusterAddressSettings.discovery_advertised_address).getPort();
case CLUSTER -> configuration.get(ClusterAddressSettings.cluster_advertised_address).getPort();
};
if (port == 0) {
port = switch (this.type) {
case DISCOVERY -> DEFAULT_DISCOVERY_PORT;
Expand All @@ -82,20 +90,17 @@ private AwsClient instantiateAwsClient() {
public Stream<SocketAddress> addresses() {
//TODO: may need to re-instantiate the aws client?
try {
//ASG name tag priority
//ASG name priority vs VM tag
if (this.asgName != null) {
return awsClient.getVmAddressesByAsgName(this.asgName)
.map(s -> {
SocketAddress addr = new SocketAddress(s, discoveryPort);

return addr;
});
} else if (this.vmTag != null) {

return awsClient.getVmAddressesByTag(this.vmTagKey, this.vmTagValue)
.map(s -> {
SocketAddress addr = new SocketAddress(s, discoveryPort);

return addr;
});
}
Expand Down
44 changes: 44 additions & 0 deletions src/test/java/cs/neo4j/AwsClientTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
package cs.neo4j;

import org.junit.jupiter.api.Test;
import software.amazon.awssdk.services.ec2.Ec2Client;
import software.amazon.awssdk.services.ec2.model.DescribeInstancesRequest;
import software.amazon.awssdk.services.ec2.model.DescribeInstancesResponse;
import org.mockito.Mockito;
import org.mockito.Mockito;

import static org.junit.jupiter.api.Assertions.*;
import static org.mockito.Mockito.mock;

public class AwsClientTest {


Ec2Client ec2ClientMock = mock(Ec2Client.class);

@Test
void shouldTestSomething() {
boolean exception=false;


// DescribeInstancesResponse describeInstancesResponse = DescribeInstancesResponse.builder()
// .reservations(reservation -> reservation.instances(instance -> instance.stateName("running")))
// .build();
//
// when(ec2ClientMock.describeInstances(any(DescribeInstancesRequest.class)))
// .thenReturn(describeInstancesResponse);
// AwsClient awsClient = new AwsClient("????",
// "?????", "eu-west-1");
//
// try {
// Stream<String> resultStream = awsClient.getVmAddressesByAsgName("myasg");
// List<String> results = resultStream.collect(Collectors.toList());
// assertEquals(1, results.size());
// assertEquals("ip-10-0-0-17.eu-west-1.compute.internal", results.get(0));
//
// } catch (Exception e) {
// exception=true;
// }

assertFalse(exception);
}
}
32 changes: 0 additions & 32 deletions src/test/java/cs/neo4j/AwsHttpClientTest.java

This file was deleted.

84 changes: 84 additions & 0 deletions src/test/java/cs/neo4j/Ec2DiscoveryResolverTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,84 @@
package cs.neo4j;

import com.neo4j.causalclustering.discovery.resolve.RemotesResolver;
import com.neo4j.configuration.ClusterAddressSettings;
import com.neo4j.configuration.ClusterNetworkSettings;
import com.neo4j.configuration.DiscoverySettings;
import org.junit.jupiter.api.Test;
import org.neo4j.configuration.Config;
import org.neo4j.configuration.GraphDatabaseSettings;
import org.neo4j.configuration.SettingsDeclaration;
import org.neo4j.configuration.helpers.SocketAddress;
import org.neo4j.graphdb.config.Configuration;
import org.neo4j.graphdb.config.Setting;
import org.neo4j.logging.LogProvider;
import org.neo4j.logging.NullLogProvider;

import java.util.stream.Collectors;

import static org.mockito.Mockito.mock;
import static org.neo4j.configuration.SettingImpl.newBuilder;
import static org.neo4j.configuration.SettingValueParsers.STRING;

public class Ec2DiscoveryResolverTest {

Ec2DiscoveryResolver resolver = new Ec2DiscoveryResolver();
Config config = Config.newBuilder().build();

LogProvider logProvider = NullLogProvider.getInstance();;


@Test
void shouldReturnName() {
resolver.init(RemotesResolver.Type.DISCOVERY, config, logProvider);
assert(resolver.getName() == "EC2");
}

@Test
void shouldGetDefaultDiscoveryPort() {
resolver.init(RemotesResolver.Type.DISCOVERY, config, logProvider);
assert(resolver.getDiscoveryPort() == 5000);
}

@Test
void shouldGetDefaultClusterPort() {
resolver.init(RemotesResolver.Type.CLUSTER, config, logProvider);
assert(resolver.getDiscoveryPort() == 6000);
}

@Test
void shouldGetAdvertisedDiscoveryPort() {
config.setIfNotSet(ClusterAddressSettings.discovery_advertised_address,
new SocketAddress("10.0.0.2", 1234));
resolver.init(RemotesResolver.Type.DISCOVERY, config, logProvider);
assert(resolver.getDiscoveryPort() == 1234);
}

@Test
void shouldGetAdvertisedClusterPort() {
config.setIfNotSet(ClusterAddressSettings.cluster_advertised_address,
new SocketAddress("10.0.0.2", 4567));
resolver.init(RemotesResolver.Type.CLUSTER, config, logProvider);
assert(resolver.getDiscoveryPort() == 4567);
}

@Test
void shouldUseAsg() {
config.setIfNotSet(Ec2DiscoverySettings.asg_name, "myasg");
resolver.init(RemotesResolver.Type.DISCOVERY, config, logProvider);

assert(true);
// assert(resolver.addresses().collect(Collectors.toList()) == );
}

@Test
void shouldUseTag() {
config.setIfNotSet(Ec2DiscoverySettings.vm_tag, "mytag:production");
resolver.init(RemotesResolver.Type.DISCOVERY, config, logProvider);

assert(true);
}



}

0 comments on commit e2ef03b

Please sign in to comment.