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

HDDS-117. Add convenience methods for port management in DatanodeDetails #7408

Merged
merged 2 commits into from
Nov 15, 2024
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
Original file line number Diff line number Diff line change
Expand Up @@ -167,7 +167,7 @@ private synchronized void connectToDatanode(DatanodeDetails dn)
}
// read port from the data node, on failure use default configured
// port.
int port = dn.getPort(DatanodeDetails.Port.Name.STANDALONE).getValue();
int port = dn.getStandalonePort().getValue();
if (port == 0) {
port = config.getInt(OzoneConfigKeys.HDDS_CONTAINER_IPC_PORT,
OzoneConfigKeys.HDDS_CONTAINER_IPC_PORT_DEFAULT);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -185,8 +185,7 @@ private String getPipelineCacheKey(Pipeline pipeline,
// Standalone port is chosen since all datanodes should have a
// standalone port regardless of version and this port should not
// have any collisions.
key += closestNode.getHostName() + closestNode.getPort(
DatanodeDetails.Port.Name.STANDALONE);
key += closestNode.getHostName() + closestNode.getStandalonePort();
} catch (IOException e) {
LOG.error("Failed to get closest node to create pipeline cache key:" +
e.getMessage());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -234,6 +234,18 @@ public synchronized void setPort(Name name, int port) {
setPort(new Port(name, port));
}

public void setRatisPort(int port) {
setPort(Name.RATIS, port);
}

public void setRestPort(int port) {
setPort(Name.REST, port);
}

public void setStandalonePort(int port) {
setPort(Name.STANDALONE, port);
}

/**
* Returns all the Ports used by DataNode.
*
Expand Down Expand Up @@ -324,20 +336,52 @@ public void setPersistedOpStateExpiryEpochSec(long expiry) {
* @return Port
*/
public synchronized Port getPort(Port.Name name) {
Port ratisPort = null;
for (Port port : ports) {
if (port.getName().equals(name)) {
return port;
}
if (port.getName().equals(Name.RATIS)) {
ratisPort = port;
}
}
// if no separate admin/server/datastream port, return single Ratis one for
// compat
// if no separate admin/server/datastream port,
// return single Ratis one for compatibility
if (name == Name.RATIS_ADMIN || name == Name.RATIS_SERVER ||
name == Name.RATIS_DATASTREAM) {
return getPort(Name.RATIS);
return ratisPort;
}
return null;
}

/**
* Helper method to get the Ratis port.
*
* @return Port
*/
public Port getRatisPort() {
return getPort(Name.RATIS);
}

/**
* Helper method to get the REST port.
*
* @return Port
*/
public Port getRestPort() {
return getPort(Name.REST);
}

/**
* Helper method to get the Standalone port.
*
* @return Port
*/
public Port getStandalonePort() {
return getPort(Name.STANDALONE);
}


/**
* Starts building a new DatanodeDetails from the protobuf input.
*
Expand Down Expand Up @@ -863,6 +907,36 @@ public static Port newPort(Port.Name name, Integer value) {
return new Port(name, value);
}

/**
* Constructs a new Ratis Port with the given port number.
*
* @param portNumber Port number
* @return the {@link Port} instance
*/
public static Port newRatisPort(Integer portNumber) {
return newPort(Name.RATIS, portNumber);
}

/**
* Constructs a new REST Port with the given port number.
*
* @param portNumber Port number
* @return the {@link Port} instance
*/
public static Port newRestPort(Integer portNumber) {
return newPort(Name.REST, portNumber);
}

/**
* Constructs a new Standalone Port with the given port number.
*
* @param portNumber Port number
* @return the {@link Port} instance
*/
public static Port newStandalonePort(Integer portNumber) {
return newPort(Name.STANDALONE, portNumber);
}

/**
* Container to hold DataNode Port details.
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,19 +37,19 @@ public class TestExcludeList {
public void excludeNodesShouldBeCleanedBasedOnGivenTime() {
ExcludeList list = new ExcludeList(10, clock);
list.addDatanode(DatanodeDetails.newBuilder().setUuid(UUID.randomUUID())
.setIpAddress("127.0.0.1").setHostName("localhost").addPort(
DatanodeDetails.newPort(DatanodeDetails.Port.Name.STANDALONE, 2001))
.setIpAddress("127.0.0.1").setHostName("localhost")
.addPort(DatanodeDetails.newStandalonePort(2001))
.build());
assertEquals(1, list.getDatanodes().size());
clock.fastForward(11);
assertEquals(0, list.getDatanodes().size());
list.addDatanode(DatanodeDetails.newBuilder().setUuid(UUID.randomUUID())
.setIpAddress("127.0.0.2").setHostName("localhost").addPort(
DatanodeDetails.newPort(DatanodeDetails.Port.Name.STANDALONE, 2001))
.setIpAddress("127.0.0.2").setHostName("localhost")
.addPort(DatanodeDetails.newStandalonePort(2001))
.build());
list.addDatanode(DatanodeDetails.newBuilder().setUuid(UUID.randomUUID())
.setIpAddress("127.0.0.3").setHostName("localhost").addPort(
DatanodeDetails.newPort(DatanodeDetails.Port.Name.STANDALONE, 2001))
.setIpAddress("127.0.0.3").setHostName("localhost")
.addPort(DatanodeDetails.newStandalonePort(2001))
.build());
assertEquals(2, list.getDatanodes().size());
}
Expand All @@ -58,8 +58,8 @@ public void excludeNodesShouldBeCleanedBasedOnGivenTime() {
public void excludeNodeShouldNotBeCleanedIfExpiryTimeIsZero() {
ExcludeList list = new ExcludeList(0, clock);
list.addDatanode(DatanodeDetails.newBuilder().setUuid(UUID.randomUUID())
.setIpAddress("127.0.0.1").setHostName("localhost").addPort(
DatanodeDetails.newPort(DatanodeDetails.Port.Name.STANDALONE, 2001))
.setIpAddress("127.0.0.1").setHostName("localhost")
.addPort(DatanodeDetails.newStandalonePort(2001))
.build());
assertEquals(1, list.getDatanodes().size());
clock.fastForward(1);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,6 @@

import org.apache.hadoop.hdds.conf.ConfigurationSource;
import org.apache.hadoop.hdds.protocol.DatanodeDetails;
import org.apache.hadoop.hdds.protocol.DatanodeDetails.Port.Name;
import org.apache.hadoop.hdds.protocol.datanode.proto.ContainerProtos;
import org.apache.hadoop.hdds.protocol.datanode.proto.ContainerProtos.ContainerCommandRequestProto;
import org.apache.hadoop.hdds.protocol.proto.HddsProtos;
Expand Down Expand Up @@ -198,9 +197,7 @@ public void start() throws IOException {
}

//register the real port to the datanode details.
datanodeDetails.setPort(DatanodeDetails
.newPort(Name.STANDALONE,
realPort));
datanodeDetails.setPort(DatanodeDetails.newStandalonePort(realPort));

isStarted = true;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -152,11 +152,11 @@ public static DatanodeDetails createDatanodeDetails() {
.nextInt(256) + "." + random.nextInt(256);

DatanodeDetails.Port containerPort =
DatanodeDetails.newPort(DatanodeDetails.Port.Name.STANDALONE, 0);
DatanodeDetails.newStandalonePort(0);
DatanodeDetails.Port ratisPort =
DatanodeDetails.newPort(DatanodeDetails.Port.Name.RATIS, 0);
DatanodeDetails.newRatisPort(0);
DatanodeDetails.Port restPort =
DatanodeDetails.newPort(DatanodeDetails.Port.Name.REST, 0);
DatanodeDetails.newRestPort(0);
DatanodeDetails.Builder builder = DatanodeDetails.newBuilder();
builder.setUuid(UUID.randomUUID())
.setHostName("localhost")
Expand Down Expand Up @@ -345,7 +345,7 @@ public static ContainerController getEmptyContainerController() {
public static XceiverServerRatis newXceiverServerRatis(
DatanodeDetails dn, OzoneConfiguration conf) throws IOException {
conf.setInt(OzoneConfigKeys.HDDS_CONTAINER_RATIS_IPC_PORT,
dn.getPort(DatanodeDetails.Port.Name.RATIS).getValue());
dn.getRatisPort().getValue());

return XceiverServerRatis.newXceiverServerRatis(null, dn, conf,
getNoopContainerDispatcher(), getEmptyContainerController(),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -198,8 +198,7 @@ public void testDatanodeStateContext() throws IOException,
OzoneConsts.OZONE_SCM_DATANODE_ID_FILE_DEFAULT);
idPath.delete();
DatanodeDetails datanodeDetails = getNewDatanodeDetails();
DatanodeDetails.Port port = DatanodeDetails.newPort(
DatanodeDetails.Port.Name.STANDALONE,
DatanodeDetails.Port port = DatanodeDetails.newStandalonePort(
OzoneConfigKeys.HDDS_CONTAINER_IPC_PORT_DEFAULT);
datanodeDetails.setPort(port);
ContainerUtils.writeDatanodeDetailsTo(datanodeDetails, idPath, conf);
Expand Down Expand Up @@ -325,8 +324,7 @@ public void testDatanodeStateMachineWithIdWriteFail() throws Exception {
OzoneConsts.OZONE_SCM_DATANODE_ID_FILE_DEFAULT);
idPath.delete();
DatanodeDetails datanodeDetails = getNewDatanodeDetails();
DatanodeDetails.Port port = DatanodeDetails.newPort(
DatanodeDetails.Port.Name.STANDALONE,
DatanodeDetails.Port port = DatanodeDetails.newStandalonePort(
OzoneConfigKeys.HDDS_CONTAINER_IPC_PORT_DEFAULT);
datanodeDetails.setPort(port);

Expand Down Expand Up @@ -408,12 +406,9 @@ public void testDatanodeStateMachineWithInvalidConfiguration()
}

private DatanodeDetails getNewDatanodeDetails() {
DatanodeDetails.Port containerPort = DatanodeDetails.newPort(
DatanodeDetails.Port.Name.STANDALONE, 0);
DatanodeDetails.Port ratisPort = DatanodeDetails.newPort(
DatanodeDetails.Port.Name.RATIS, 0);
DatanodeDetails.Port restPort = DatanodeDetails.newPort(
DatanodeDetails.Port.Name.REST, 0);
DatanodeDetails.Port containerPort = DatanodeDetails.newStandalonePort(0);
DatanodeDetails.Port ratisPort = DatanodeDetails.newRatisPort(0);
DatanodeDetails.Port restPort = DatanodeDetails.newRestPort(0);
DatanodeDetails.Port streamPort = DatanodeDetails.newPort(
DatanodeDetails.Port.Name.RATIS_DATASTREAM, 0);
return DatanodeDetails.newBuilder()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,6 @@
import java.nio.charset.StandardCharsets;

import static java.nio.charset.StandardCharsets.UTF_8;
import static org.apache.hadoop.hdds.protocol.DatanodeDetails.Port;
import static org.apache.hadoop.hdds.protocol.MockDatanodeDetails.randomDatanodeDetails;
import static org.apache.hadoop.hdds.protocol.datanode.proto.ContainerProtos.Type.ReadChunk;
import static org.apache.hadoop.hdds.scm.protocolPB.ContainerCommandResponseBuilders.getReadChunkResponse;
Expand Down Expand Up @@ -92,7 +91,7 @@ public void testTarName() throws IOException {
public void testDatanodeIDPersistent(@TempDir File tempDir) throws Exception {
// Generate IDs for testing
DatanodeDetails id1 = randomDatanodeDetails();
id1.setPort(DatanodeDetails.newPort(Port.Name.STANDALONE, 1));
id1.setPort(DatanodeDetails.newStandalonePort(1));
assertWriteRead(tempDir, id1);

// Add certificate serial id.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ void testWriteReadBeforeRatisDatastreamPortLayoutVersion(@TempDir File dir)
// if no separate admin/server/datastream port, return single Ratis one for
// compat
assertEquals(read.getPort(DatanodeDetails.Port.Name.RATIS_DATASTREAM),
read.getPort(DatanodeDetails.Port.Name.RATIS));
read.getRatisPort());
}

@Test
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -546,12 +546,9 @@ static HddsDispatcher createDispatcher(DatanodeDetails dd, UUID scmId,
// This method has to be removed once we move scm/TestUtils.java
// from server-scm project to container-service or to common project.
private static DatanodeDetails randomDatanodeDetails() {
DatanodeDetails.Port containerPort = DatanodeDetails.newPort(
DatanodeDetails.Port.Name.STANDALONE, 0);
DatanodeDetails.Port ratisPort = DatanodeDetails.newPort(
DatanodeDetails.Port.Name.RATIS, 0);
DatanodeDetails.Port restPort = DatanodeDetails.newPort(
DatanodeDetails.Port.Name.REST, 0);
DatanodeDetails.Port containerPort = DatanodeDetails.newStandalonePort(0);
DatanodeDetails.Port ratisPort = DatanodeDetails.newRatisPort(0);
DatanodeDetails.Port restPort = DatanodeDetails.newRestPort(0);
DatanodeDetails.Builder builder = DatanodeDetails.newBuilder();
builder.setUuid(UUID.randomUUID())
.setHostName("localhost")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -273,12 +273,9 @@ private CloseContainerCommand forceCloseWithoutPipeline() {
*/
private static DatanodeDetails randomDatanodeDetails() {
String ipAddress = "127.0.0.1";
DatanodeDetails.Port containerPort = DatanodeDetails.newPort(
DatanodeDetails.Port.Name.STANDALONE, 0);
DatanodeDetails.Port ratisPort = DatanodeDetails.newPort(
DatanodeDetails.Port.Name.RATIS, 0);
DatanodeDetails.Port restPort = DatanodeDetails.newPort(
DatanodeDetails.Port.Name.REST, 0);
DatanodeDetails.Port containerPort = DatanodeDetails.newStandalonePort(0);
DatanodeDetails.Port ratisPort = DatanodeDetails.newRatisPort(0);
DatanodeDetails.Port restPort = DatanodeDetails.newRestPort(0);
DatanodeDetails.Builder builder = DatanodeDetails.newBuilder();
builder.setUuid(UUID.randomUUID())
.setHostName("localhost")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -300,12 +300,9 @@ private DatanodeDetails createDatanodeDetails() {
random.nextInt(256) + "." + random.nextInt(256) + "." + random
.nextInt(256) + "." + random.nextInt(256);

DatanodeDetails.Port containerPort = DatanodeDetails.newPort(
DatanodeDetails.Port.Name.STANDALONE, 0);
DatanodeDetails.Port ratisPort = DatanodeDetails.newPort(
DatanodeDetails.Port.Name.RATIS, 0);
DatanodeDetails.Port restPort = DatanodeDetails.newPort(
DatanodeDetails.Port.Name.REST, 0);
DatanodeDetails.Port containerPort = DatanodeDetails.newStandalonePort(0);
DatanodeDetails.Port ratisPort = DatanodeDetails.newRatisPort(0);
DatanodeDetails.Port restPort = DatanodeDetails.newRestPort(0);
DatanodeDetails.Builder builder = DatanodeDetails.newBuilder();
builder.setUuid(UUID.randomUUID())
.setHostName("localhost")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -102,11 +102,9 @@ public void init() throws Exception {
.setPersistedOpState(HddsProtos.NodeOperationalState.IN_SERVICE)
.setPersistedOpStateExpiry(0);
DatanodeDetails.Port containerPort =
DatanodeDetails.newPort(DatanodeDetails.Port.Name.STANDALONE,
OzoneConfigKeys.HDDS_CONTAINER_IPC_PORT_DEFAULT);
DatanodeDetails.newStandalonePort(OzoneConfigKeys.HDDS_CONTAINER_IPC_PORT_DEFAULT);
DatanodeDetails.Port ratisPort =
DatanodeDetails.newPort(DatanodeDetails.Port.Name.RATIS,
OzoneConfigKeys.HDDS_CONTAINER_RATIS_IPC_PORT_DEFAULT);
DatanodeDetails.newRatisPort(OzoneConfigKeys.HDDS_CONTAINER_RATIS_IPC_PORT_DEFAULT);
DatanodeDetails.Port replicationPort =
DatanodeDetails.newPort(DatanodeDetails.Port.Name.REPLICATION,
replicationConfig.getPort());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -72,12 +72,9 @@ public void testStartupSlvLessThanMlv() throws Exception {
}

private DatanodeDetails getNewDatanodeDetails() {
DatanodeDetails.Port containerPort = DatanodeDetails.newPort(
DatanodeDetails.Port.Name.STANDALONE, 0);
DatanodeDetails.Port ratisPort = DatanodeDetails.newPort(
DatanodeDetails.Port.Name.RATIS, 0);
DatanodeDetails.Port restPort = DatanodeDetails.newPort(
DatanodeDetails.Port.Name.REST, 0);
DatanodeDetails.Port containerPort = DatanodeDetails.newStandalonePort(0);
DatanodeDetails.Port ratisPort = DatanodeDetails.newRatisPort(0);
DatanodeDetails.Port restPort = DatanodeDetails.newRestPort(0);
return DatanodeDetails.newBuilder()
.setUuid(UUID.randomUUID())
.setHostName("localhost")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -215,9 +215,9 @@ private boolean allPortsMatch(List<DatanodeDetails> dns) {
if (dns.size() < 2) {
return true;
}
int port = dns.get(0).getPort(DatanodeDetails.Port.Name.RATIS).getValue();
int port = dns.get(0).getRatisPort().getValue();
for (int i = 1; i < dns.size(); i++) {
if (dns.get(i).getPort(DatanodeDetails.Port.Name.RATIS).getValue()
if (dns.get(i).getRatisPort().getValue()
!= port) {
return false;
}
Expand Down
Loading