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

ISPN-10567 Allow container exposed ports when CONTAINER is the server driver #7273

Merged
merged 1 commit into from
Nov 14, 2019
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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
2 changes: 1 addition & 1 deletion Jenkinsfile
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ pipeline {
stage('Tests') {
steps {
configFileProvider([configFile(fileId: 'maven-settings-with-deploy-snapshot', variable: 'MAVEN_SETTINGS')]) {
sh "$MAVEN_HOME/bin/mvn verify -B -V -e -s $MAVEN_SETTINGS -Dmaven.test.failure.ignore=true -Dansi.strip=true"
sh "$MAVEN_HOME/bin/mvn verify -B -V -e -s $MAVEN_SETTINGS -Dmaven.test.failure.ignore=true -Dansi.strip=true -Dorg.infinispan.test.server.container.preferContainerExposedPorts=true"
}
// TODO Add StabilityTestDataPublisher after https://issues.jenkins-ci.org/browse/JENKINS-42610 is fixed
// Capture target/surefire-reports/*.xml, target/failsafe-reports/*.xml,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -307,7 +307,7 @@ public void start() {

private void actualStart() {
log.debugf("Starting remote cache manager %x", System.identityHashCode(this));
channelFactory = new ChannelFactory();
channelFactory = createChannelFactory();

if (marshaller == null) {
marshaller = configuration.marshaller();
Expand Down Expand Up @@ -361,6 +361,10 @@ private void actualStart() {
started = true;
}

public ChannelFactory createChannelFactory() {
return new ChannelFactory();
}

@Override
public boolean isTransactional(String cacheName) {
ClientStatistics stats = ClientStatistics.dummyClientStatistics(timeService);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -339,7 +339,7 @@ private void updateServers(Collection<SocketAddress> newServers) {
}

@GuardedBy("lock")
private Collection<SocketAddress> updateTopologyInfo(byte[] cacheName, Collection<SocketAddress> newServers, boolean quiet) {
protected Collection<SocketAddress> updateTopologyInfo(byte[] cacheName, Collection<SocketAddress> newServers, boolean quiet) {
Collection<SocketAddress> servers = topologyInfo.getServers(new WrappedByteArray(cacheName));
Set<SocketAddress> addedServers = new HashSet<>(newServers);
addedServers.removeAll(servers);
Expand Down
11 changes: 10 additions & 1 deletion server/runtime/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -436,12 +436,21 @@
<activation>
<activeByDefault>false</activeByDefault>
</activation>
<properties>
<container.argLine>${forkJvmArgs} ${testjvm.jigsawArgs} -Dserver.output.dir=${server.output.dir} -Djdk.attach.allowAttachSelf=true -Dorg.infinispan.test.server.driver=CONTAINER</container.argLine>
</properties>
<build>
<plugins>
<plugin>
<artifactId>maven-failsafe-plugin</artifactId>
<configuration>
<argLine>${forkJvmArgs} ${testjvm.jigsawArgs} -Dserver.output.dir=${server.output.dir} -Djdk.attach.allowAttachSelf=true -Dorg.infinispan.test.server.driver=CONTAINER</argLine>
<argLine>${container.argLine}</argLine>
</configuration>
</plugin>
<plugin>
<artifactId>maven-surefire-plugin</artifactId>
<configuration>
<argLine>${container.argLine}</argLine>
</configuration>
</plugin>
</plugins>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,11 @@
import javax.management.remote.JMXConnectorFactory;
import javax.management.remote.JMXServiceURL;

import com.github.dockerjava.api.DockerClient;
import com.github.dockerjava.api.model.ContainerNetwork;
import com.github.dockerjava.api.model.Network;
import org.infinispan.client.hotrod.RemoteCacheManager;
import org.infinispan.client.hotrod.configuration.ConfigurationBuilder;
import org.infinispan.commons.logging.Log;
import org.infinispan.commons.util.Version;
import org.infinispan.test.Exceptions;
Expand All @@ -29,10 +34,7 @@
import org.testcontainers.containers.wait.strategy.Wait;
import org.testcontainers.images.builder.ImageFromDockerfile;

import com.github.dockerjava.api.DockerClient;
import com.github.dockerjava.api.command.InspectContainerResponse;
import com.github.dockerjava.api.model.ContainerNetwork;
import com.github.dockerjava.api.model.Network;
import static org.infinispan.server.test.ContainerUtil.getIpAddressFromContainer;

/**
* WARNING: Work in progress. Does not work yet.
Expand All @@ -50,6 +52,7 @@ public class ContainerInfinispanServerDriver extends InfinispanServerDriver {
CountdownLatchLoggingConsumer latch;
ImageFromDockerfile image;
private File rootDir;
private final boolean preferContainerExposedPorts = Boolean.valueOf(System.getProperty("org.infinispan.test.server.container.preferContainerExposedPorts", "false"));
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You can replace this with Boolean.getBoolean("org.infinispan.test.server.container.preferContainerExposedPorts")


protected ContainerInfinispanServerDriver(InfinispanServerTestConfiguration configuration) {
super(
Expand Down Expand Up @@ -200,11 +203,9 @@ public InetSocketAddress getServerSocket(int server, int port) {
@Override
public InetAddress getServerAddress(int server) {
GenericContainer container = containers.get(server);
InspectContainerResponse containerInfo = container.getContainerInfo();
ContainerNetwork network = containerInfo.getNetworkSettings().getNetworks().values().iterator().next();
// We talk directly to the container, and not through forwarded addresses on localhost because of
// https://github.com/testcontainers/testcontainers-java/issues/452
return Exceptions.unchecked(() -> InetAddress.getByName(network.getIpAddress()));
return Exceptions.unchecked(() -> InetAddress.getByName(getIpAddressFromContainer(container)));
}

@Override
Expand Down Expand Up @@ -264,4 +265,13 @@ public MBeanServerConnection getJmxConnection(int server) {
return jmxConnector.getMBeanServerConnection();
});
}

@Override
public RemoteCacheManager createRemoteCacheManager(ConfigurationBuilder builder) {
if (preferContainerExposedPorts) {
return new ContainerRemoteCacheManager(containers).wrap(builder);
} else {
return new RemoteCacheManager(builder.build());
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
package org.infinispan.server.test;

import static org.infinispan.server.test.ContainerUtil.getIpAddressFromContainer;

import java.net.InetSocketAddress;
import java.net.SocketAddress;
import java.util.ArrayList;
import java.util.Collection;
import java.util.List;

import org.infinispan.client.hotrod.RemoteCacheManager;
import org.infinispan.client.hotrod.configuration.ConfigurationBuilder;
import org.infinispan.client.hotrod.impl.transport.netty.ChannelFactory;
import org.testcontainers.containers.GenericContainer;

public class ContainerRemoteCacheManager {

private final List<GenericContainer> containers;

public ContainerRemoteCacheManager(List<GenericContainer> containers) {
this.containers = containers;
}

public RemoteCacheManager wrap(ConfigurationBuilder builder) {
return new RemoteCacheManager(builder.build()) {
@Override
public ChannelFactory createChannelFactory() {
return new ChannelFactory() {
protected Collection<SocketAddress> updateTopologyInfo(byte[] cacheName, Collection<SocketAddress> newServers, boolean quiet) {
List<SocketAddress> localHostServers = new ArrayList<>();
for (SocketAddress address : newServers) {
InetSocketAddress inetSocketAddress = (InetSocketAddress) address;
GenericContainer container = getGeneriContainerBy(inetSocketAddress);
localHostServers.add(new InetSocketAddress("localhost", container.getMappedPort(inetSocketAddress.getPort())));
}
return super.updateTopologyInfo(cacheName, localHostServers, quiet);
}
};
}
};
}

private GenericContainer getGeneriContainerBy(InetSocketAddress inetSocketAddress) {
for (GenericContainer container : containers) {
String hostName = getIpAddressFromContainer(container);
if (inetSocketAddress.getHostName().equals(hostName)) {
return container;
}
}
throw new IllegalStateException();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
package org.infinispan.server.test;

import org.testcontainers.containers.GenericContainer;

import com.github.dockerjava.api.command.InspectContainerResponse;
import com.github.dockerjava.api.model.ContainerNetwork;

public class ContainerUtil {

public static String getIpAddressFromContainer(GenericContainer container) {
InspectContainerResponse containerInfo = container.getContainerInfo();
ContainerNetwork network = containerInfo.getNetworkSettings().getNetworks().values().iterator().next();
return network.getIpAddress();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@

import javax.management.MBeanServerConnection;

import org.infinispan.client.hotrod.RemoteCacheManager;
import org.infinispan.client.hotrod.configuration.ConfigurationBuilder;
import org.infinispan.manager.CacheContainer;
import org.infinispan.manager.DefaultCacheManager;
import org.infinispan.server.DefaultExitHandler;
Expand Down Expand Up @@ -123,4 +125,9 @@ public MBeanServerConnection getJmxConnection(int server) {
DefaultCacheManager cacheManager = servers.get(server).getCacheManagers().values().iterator().next();
return cacheManager.getCacheManagerConfiguration().globalJmxStatistics().mbeanServerLookup().getMBeanServer();
}

@Override
public RemoteCacheManager createRemoteCacheManager(ConfigurationBuilder builder) {
return new RemoteCacheManager(builder.build());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@
import javax.management.MBeanServerConnection;
import javax.security.auth.x500.X500Principal;

import org.infinispan.client.hotrod.RemoteCacheManager;
import org.infinispan.client.hotrod.configuration.ConfigurationBuilder;
import org.infinispan.commons.util.Util;
import org.infinispan.lifecycle.ComponentStatus;
Expand Down Expand Up @@ -345,4 +346,6 @@ public void pause(int server) {
* @param server the index of the server
*/
public abstract MBeanServerConnection getJmxConnection(int server);

public abstract RemoteCacheManager createRemoteCacheManager(ConfigurationBuilder builder);
}
Original file line number Diff line number Diff line change
Expand Up @@ -101,7 +101,7 @@ RemoteCacheManager newHotRodClient(ConfigurationBuilder builder) {
InetSocketAddress serverAddress = serverDriver.getServerSocket(i, 11222);
builder.addServer().host(serverAddress.getHostName()).port(serverAddress.getPort());
}
RemoteCacheManager remoteCacheManager = new RemoteCacheManager(builder.build());
RemoteCacheManager remoteCacheManager = serverDriver.createRemoteCacheManager(builder);
return remoteCacheManager;
}

Expand Down