Skip to content
Merged
Show file tree
Hide file tree
Changes from 5 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
Original file line number Diff line number Diff line change
Expand Up @@ -1053,6 +1053,25 @@ public static int getFreeSocketPort() {
return port;
}

/**
* Return free ports. There is no guarantee they will remain free, so
* ports should be used immediately. The number of free ports returned by
* this method should match argument {@code numOfPorts}.
*
* @param numOfPorts Number of free ports to acquire.
* @return Free ports for binding a local socket.
*/
public static Set<Integer> getFreeSocketPorts(int numOfPorts) {
final Set<Integer> freePorts = new HashSet<>(numOfPorts);
for (int i = 0; i < numOfPorts * 5; i++) {
freePorts.add(getFreeSocketPort());
Copy link
Member

Choose a reason for hiding this comment

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

getFreeSocketPort() as per code can return 0 as well, we should avoid that? We can't classify 0 as a unique free port.
if it returns 0, then your test would also fail. you would be expecting the port to be 0, but it would be different

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Yes, let me take care of this.

if (freePorts.size() == numOfPorts) {
return freePorts;
}
}
throw new IllegalStateException(numOfPorts + " free ports could not be acquired.");
}

/**
* Return an @{@link InetAddress} to bind to. If bindWildCardAddress is true
* than returns null.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
import static org.apache.hadoop.hdfs.qjournal.QJMTestUtil.FAKE_NSINFO;
import static org.junit.Assert.fail;

import java.io.Closeable;
import java.io.File;
import java.io.IOException;
import java.net.InetSocketAddress;
Expand All @@ -45,13 +46,16 @@
import org.apache.hadoop.thirdparty.com.google.common.base.Joiner;
import org.apache.hadoop.test.GenericTestUtils;

public class MiniJournalCluster {
public final class MiniJournalCluster implements Closeable {

public static final String CLUSTER_WAITACTIVE_URI = "waitactive";
public static class Builder {
private String baseDir;
private int numJournalNodes = 3;
private boolean format = true;
private final Configuration conf;
private int[] httpPorts = null;
private int[] rpcPorts = null;

static {
DefaultMetricsSystem.setMiniClusterMode(true);
Expand All @@ -76,6 +80,16 @@ public Builder format(boolean f) {
return this;
}

public Builder setHttpPorts(int... ports) {
this.httpPorts = ports;
return this;
}

public Builder setRpcPorts(int... ports) {
this.rpcPorts = ports;
return this;
}

public MiniJournalCluster build() throws IOException {
return new MiniJournalCluster(this);
}
Expand All @@ -99,6 +113,19 @@ private JNInfo(JournalNode node) {
private final JNInfo[] nodes;

private MiniJournalCluster(Builder b) throws IOException {

if (b.httpPorts != null && b.httpPorts.length != b.numJournalNodes) {
throw new IllegalArgumentException(
"Num of http ports (" + b.httpPorts.length + ") should match num of JournalNodes ("
+ b.numJournalNodes + ")");
}

if (b.rpcPorts != null && b.rpcPorts.length != b.numJournalNodes) {
throw new IllegalArgumentException(
"Num of rpc ports (" + b.rpcPorts.length + ") should match num of JournalNodes ("
+ b.numJournalNodes + ")");
}

LOG.info("Starting MiniJournalCluster with " +
b.numJournalNodes + " journal nodes");

Expand Down Expand Up @@ -173,8 +200,10 @@ private Configuration createConfForNode(Builder b, int idx) {
Configuration conf = new Configuration(b.conf);
File logDir = getStorageDir(idx);
conf.set(DFSConfigKeys.DFS_JOURNALNODE_EDITS_DIR_KEY, logDir.toString());
conf.set(DFSConfigKeys.DFS_JOURNALNODE_RPC_ADDRESS_KEY, "localhost:0");
conf.set(DFSConfigKeys.DFS_JOURNALNODE_HTTP_ADDRESS_KEY, "localhost:0");
int httpPort = b.httpPorts != null ? b.httpPorts[idx] : 0;
int rpcPort = b.rpcPorts != null ? b.rpcPorts[idx] : 0;
conf.set(DFSConfigKeys.DFS_JOURNALNODE_RPC_ADDRESS_KEY, "localhost:" + rpcPort);
conf.set(DFSConfigKeys.DFS_JOURNALNODE_HTTP_ADDRESS_KEY, "localhost:" + httpPort);
return conf;
}

Expand Down Expand Up @@ -274,4 +303,10 @@ public void setNamenodeSharedEditsConf(String jid) {
.DFS_NAMENODE_SHARED_EDITS_DIR_KEY, quorumJournalURI.toString());
}
}

@Override
public void close() throws IOException {
this.shutdown();
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -22,15 +22,23 @@
import java.io.File;
import java.io.IOException;
import java.net.URI;
import java.util.Set;

import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.hdfs.DFSConfigKeys;
import org.apache.hadoop.hdfs.MiniDFSCluster;
import org.apache.hadoop.hdfs.qjournal.server.JournalNode;
import org.junit.Test;
import org.apache.hadoop.net.NetUtils;
import org.apache.hadoop.test.LambdaTestUtils;

import org.junit.Test;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

public class TestMiniJournalCluster {

private static final Logger LOG = LoggerFactory.getLogger(TestMiniJournalCluster.class);

@Test
public void testStartStop() throws IOException {
Configuration conf = new Configuration();
Expand All @@ -52,4 +60,92 @@ public void testStartStop() throws IOException {
c.shutdown();
}
}

@Test
public void testStartStopWithPorts() throws Exception {
Configuration conf = new Configuration();

LambdaTestUtils.intercept(
IllegalArgumentException.class,
"Num of http ports (1) should match num of JournalNodes (3)",
"MiniJournalCluster port validation failed",
() -> {
new MiniJournalCluster.Builder(conf).setHttpPorts(8481).build();
});

LambdaTestUtils.intercept(
IllegalArgumentException.class,
"Num of rpc ports (2) should match num of JournalNodes (3)",
"MiniJournalCluster port validation failed",
() -> {
new MiniJournalCluster.Builder(conf).setRpcPorts(8481, 8482).build();
});

LambdaTestUtils.intercept(
IllegalArgumentException.class,
"Num of rpc ports (1) should match num of JournalNodes (3)",
"MiniJournalCluster port validation failed",
() -> {
new MiniJournalCluster.Builder(conf).setHttpPorts(800, 9000, 10000).setRpcPorts(8481)
.build();
});

LambdaTestUtils.intercept(
IllegalArgumentException.class,
"Num of http ports (4) should match num of JournalNodes (3)",
"MiniJournalCluster port validation failed",
() -> {
new MiniJournalCluster.Builder(conf).setHttpPorts(800, 9000, 1000, 2000)
.setRpcPorts(8481, 8482, 8483).build();
});

final Set<Integer> httpAndRpcPorts = NetUtils.getFreeSocketPorts(6);
LOG.info("Free socket ports: {}", httpAndRpcPorts);

final int[] httpPorts = new int[3];
final int[] rpcPorts = new int[3];
int httpPortIdx = 0;
int rpcPortIdx = 0;
for (Integer httpAndRpcPort : httpAndRpcPorts) {
if (httpPortIdx < 3) {
httpPorts[httpPortIdx++] = httpAndRpcPort;
} else {
rpcPorts[rpcPortIdx++] = httpAndRpcPort;
}
}

LOG.info("Http ports selected: {}", httpPorts);
LOG.info("Rpc ports selected: {}", rpcPorts);

for (int i = 0; i < 3; i++) {
assertNotEquals(0, rpcPorts[i]);
assertNotEquals(0, httpPorts[i]);
}
Copy link
Member

Choose a reason for hiding this comment

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

you could have asserted directly on httpAndRpcPorts. :-)

    assertFalse(httpAndRpcPorts.contains(0));

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Done, Thanks.


try (MiniJournalCluster miniJournalCluster = new MiniJournalCluster.Builder(conf)
.setHttpPorts(httpPorts)
.setRpcPorts(rpcPorts).build()) {
miniJournalCluster.waitActive();
URI uri = miniJournalCluster.getQuorumJournalURI("myjournal");
String[] addrs = uri.getAuthority().split(";");
assertEquals(3, addrs.length);

assertEquals(httpPorts[0], miniJournalCluster.getJournalNode(0).getHttpAddress().getPort());
assertEquals(httpPorts[1], miniJournalCluster.getJournalNode(1).getHttpAddress().getPort());
assertEquals(httpPorts[2], miniJournalCluster.getJournalNode(2).getHttpAddress().getPort());

assertEquals(rpcPorts[0],
miniJournalCluster.getJournalNode(0).getRpcServer().getAddress().getPort());
assertEquals(rpcPorts[1],
miniJournalCluster.getJournalNode(1).getRpcServer().getAddress().getPort());
assertEquals(rpcPorts[2],
miniJournalCluster.getJournalNode(2).getRpcServer().getAddress().getPort());

JournalNode node = miniJournalCluster.getJournalNode(0);
String dir = node.getConf().get(DFSConfigKeys.DFS_JOURNALNODE_EDITS_DIR_KEY);
assertEquals(new File(MiniDFSCluster.getBaseDirectory() + "journalnode-0").getAbsolutePath(),
dir);
}
}

}