Skip to content
Merged
Show file tree
Hide file tree
Changes from 10 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 @@ -24,7 +24,13 @@
import java.util.Set;
import java.util.UUID;

import com.fasterxml.jackson.annotation.JsonAutoDetect;
import com.fasterxml.jackson.annotation.JsonCreator;
import com.fasterxml.jackson.annotation.JsonFormat;
import com.fasterxml.jackson.annotation.JsonIgnore;
import com.fasterxml.jackson.annotation.JsonProperty;
import com.fasterxml.jackson.databind.annotation.JsonDeserialize;
import com.fasterxml.jackson.databind.annotation.JsonSerialize;
import com.google.common.collect.ImmutableSet;
import org.apache.hadoop.hdds.DatanodeVersion;
import org.apache.hadoop.hdds.HddsUtils;
Expand All @@ -33,6 +39,8 @@
import org.apache.hadoop.hdds.protocol.DatanodeDetails.Port.Name;
import org.apache.hadoop.hdds.protocol.proto.HddsProtos;
import org.apache.hadoop.hdds.protocol.proto.HddsProtos.ExtendedDatanodeDetailsProto;
import org.apache.hadoop.hdds.recon.CustomDatanodeDetailsDeserializer;
import org.apache.hadoop.hdds.recon.CustomDatanodeDetailsSerializer;
import org.apache.hadoop.hdds.scm.net.NetConstants;
import org.apache.hadoop.hdds.scm.net.NodeImpl;

Expand Down Expand Up @@ -62,6 +70,8 @@
*/
@InterfaceAudience.Private
@InterfaceStability.Evolving
@JsonSerialize(using = CustomDatanodeDetailsSerializer.class)
@JsonDeserialize(using = CustomDatanodeDetailsDeserializer.class)
public class DatanodeDetails extends NodeImpl implements
Comparable<DatanodeDetails> {

Expand All @@ -86,6 +96,7 @@ public static Codec<DatanodeDetails> getCodec() {

private String ipAddress;
private String hostName;
@JsonProperty("ports")
private final List<Port> ports;
private String certSerialId;
private String version;
Expand Down Expand Up @@ -120,6 +131,14 @@ private DatanodeDetails(Builder b) {
if (b.level > 0) {
setLevel(b.level);
}

// Below are dummy logs just to avoid findbugs
if (b.cost > 0) {
LOG.debug("cost: {}", getCost());
}
if (null != b.uuidString && !b.uuidString.isEmpty()) {
LOG.debug("uuid : {}", uuidString);
}
}

public DatanodeDetails(DatanodeDetails datanodeDetails) {
Expand Down Expand Up @@ -571,11 +590,14 @@ public String threadNamePrefix() {
*/
public static final class Builder {
private UUID id;
private String uuidString;
private String ipAddress;
private String hostName;
private String networkName;
private String networkLocation;
private int level;
// This field is being added here only for custom serialization and deserialization.
private int cost;
private List<Port> ports;
private String certSerialId;
private String version;
Expand Down Expand Up @@ -608,6 +630,7 @@ public Builder setDatanodeDetails(DatanodeDetails details) {
this.networkName = details.getNetworkName();
this.networkLocation = details.getNetworkLocation();
this.level = details.getLevel();
this.cost = details.getCost();
this.ports = details.getPorts();
this.certSerialId = details.getCertSerialId();
this.version = details.getVersion();
Expand All @@ -631,6 +654,17 @@ public Builder setUuid(UUID uuid) {
return this;
}

/**
* Sets the DatanodeUuid as String.
*
* @param uuidStr DatanodeUuid as String
* @return DatanodeDetails.Builder
*/
public Builder setUuidAsString(String uuidStr) {
this.uuidString = uuidStr;
return this;
}

/**
* Sets the IP address of DataNode.
*
Expand All @@ -653,6 +687,17 @@ public Builder setHostName(String host) {
return this;
}

/**
* Sets the ports of DataNode.
*
* @param ports hostname
* @return DatanodeDetails.Builder
*/
public Builder setPorts(List<Port> ports) {
this.ports = ports;
return this;
}

/**
* Sets the network name of DataNode.
*
Expand Down Expand Up @@ -680,6 +725,11 @@ public Builder setLevel(int level) {
return this;
}

public Builder setCost(int cost) {
this.cost = cost;
return this;
}

/**
* Adds a DataNode Port.
*
Expand Down Expand Up @@ -816,11 +866,13 @@ public static Port newPort(Port.Name name, Integer value) {
/**
* Container to hold DataNode Port details.
*/
@JsonAutoDetect
public static final class Port {

/**
* Ports that are supported in DataNode.
*/
@JsonFormat(shape = JsonFormat.Shape.OBJECT)
public enum Name {
STANDALONE, RATIS, REST, REPLICATION, RATIS_ADMIN, RATIS_SERVER,
@BelongsToHDDSLayoutVersion(RATIS_DATASTREAM_PORT_IN_DATANODEDETAILS)
Expand All @@ -838,14 +890,17 @@ public enum Name {
EnumSet.of(STANDALONE, RATIS, REST));
}

@JsonProperty("name")
private final Name name;
@JsonProperty("value")
private final Integer value;

/**
* Private constructor for constructing Port object. Use
* DatanodeDetails#newPort to create a new Port object.
*/
private Port(Name name, Integer value) {
@JsonCreator
public Port(Name name, Integer value) {
this.name = name;
this.value = value;
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
/*
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance
* with the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package org.apache.hadoop.hdds.recon;

import com.fasterxml.jackson.core.JsonParser;
import com.fasterxml.jackson.databind.DeserializationContext;
import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.deser.std.StdDeserializer;
import org.apache.hadoop.hdds.protocol.DatanodeDetails;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.UUID;

/**
* This is custom jackson deserializer class for DetanodeDetails class.
Copy link
Contributor

Choose a reason for hiding this comment

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

Minor typo here for DatanodeDetails

Suggested change
* This is custom jackson deserializer class for DetanodeDetails class.
* This is custom jackson deserializer class for DatanodeDetails class.

* Jackson deserializer is being used to deserialize json using JSON parser
* and map JSON fields to respective DatanodeDetails class fields.
*/
public class CustomDatanodeDetailsDeserializer extends StdDeserializer<DatanodeDetails> {

protected CustomDatanodeDetailsDeserializer(Class<DatanodeDetails> vc) {
super(vc);
}

public CustomDatanodeDetailsDeserializer() {
this(null);
}

@Override
public DatanodeDetails deserialize(JsonParser p, DeserializationContext ctxt) throws IOException {
JsonNode node = p.getCodec().readTree(p);
List<DatanodeDetails.Port> ports = new ArrayList<>();
JsonNode portsNode = node.get("ports");
if (portsNode != null && portsNode.isArray()) {
for (JsonNode portNode : portsNode) {
DatanodeDetails.Port port = new DatanodeDetails.Port(
DatanodeDetails.Port.Name.valueOf(portNode.get("name").asText()), portNode.get("value").asInt());
ports.add(port);
}
}
DatanodeDetails datanodeDetails = DatanodeDetails.newBuilder()
.setLevel(node.get("level").asInt())
.setCost(node.get("cost").asInt())
.setUuid(UUID.fromString(node.get("uuid").asText()))
.setUuidAsString(node.get("uuidString").asText())
.setIpAddress(node.get("ipAddress").asText())
.setHostName(node.get("hostName").asText())
.setPorts(ports)
.build();
return datanodeDetails;
}
}


Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
/*
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance
* with the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package org.apache.hadoop.hdds.recon;

import com.fasterxml.jackson.core.JsonGenerator;
import com.fasterxml.jackson.databind.SerializerProvider;
import com.fasterxml.jackson.databind.ser.std.StdSerializer;
import org.apache.hadoop.hdds.protocol.DatanodeDetails;

import java.io.IOException;

/**
* This is custom jackson serializer class for DetanodeDetails class.
Copy link
Contributor

Choose a reason for hiding this comment

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

Same here too

Suggested change
* This is custom jackson serializer class for DetanodeDetails class.
* This is custom jackson serializer class for DatanodeDetails class.

* Jackson serializer is being used to serialize DatanodeDetails class object
* to map and serialize respective object fields to JSON fields.
*/
public class CustomDatanodeDetailsSerializer extends StdSerializer<DatanodeDetails> {

protected CustomDatanodeDetailsSerializer(Class<DatanodeDetails> t) {
super(t);
}
public CustomDatanodeDetailsSerializer() {
this(null);
}

/**
* This method is a call back method to serialize respective object fields to JSON fields.
*
* @param datanodeDetails the datanodeDetails class object
* @param gen the JSON Generator
* @param provider the SerializerProvider
* @throws IOException
*/
@Override
public void serialize(DatanodeDetails datanodeDetails, JsonGenerator gen, SerializerProvider provider)
throws IOException {
gen.writeStartObject();
gen.writeNumberField("level", datanodeDetails.getLevel());
gen.writeNumberField("cost", datanodeDetails.getCost());
gen.writeStringField("uuid", datanodeDetails.getUuid().toString());
gen.writeStringField("uuidString", datanodeDetails.getUuidString());
gen.writeStringField("ipAddress", datanodeDetails.getIpAddress());
gen.writeStringField("hostName", datanodeDetails.getHostName());
gen.writeFieldName("ports");
gen.writeStartArray();
for (DatanodeDetails.Port port : datanodeDetails.getPorts()) {
gen.writeStartObject();
gen.writeStringField("name", port.getName().name());
gen.writeNumberField("value", port.getValue());
gen.writeEndObject();
}
gen.writeEndArray();
gen.writeEndObject();
}
}

Loading