diff --git a/hadoop-hdds/container-service/pom.xml b/hadoop-hdds/container-service/pom.xml
index 542462e8c7..43f400c727 100644
--- a/hadoop-hdds/container-service/pom.xml
+++ b/hadoop-hdds/container-service/pom.xml
@@ -52,6 +52,12 @@ http://maven.apache.org/xsd/maven-4.0.0.xsd">
test
+
+ org.yaml
+ snakeyaml
+ 1.8
+
+
io.dropwizard.metrics
metrics-core
diff --git a/hadoop-hdds/container-service/src/main/java/org/apache/hadoop/ozone/container/common/impl/ChunkLayOutVersion.java b/hadoop-hdds/container-service/src/main/java/org/apache/hadoop/ozone/container/common/impl/ChunkLayOutVersion.java
index fff68de65f..d1b1bd6649 100644
--- a/hadoop-hdds/container-service/src/main/java/org/apache/hadoop/ozone/container/common/impl/ChunkLayOutVersion.java
+++ b/hadoop-hdds/container-service/src/main/java/org/apache/hadoop/ozone/container/common/impl/ChunkLayOutVersion.java
@@ -18,6 +18,8 @@
package org.apache.hadoop.ozone.container.common.impl;
+import com.google.common.base.Preconditions;
+
/**
* Defines layout versions for the Chunks.
*/
@@ -42,6 +44,22 @@ private ChunkLayOutVersion(int version, String description) {
this.description = description;
}
+ /**
+ * Return ChunkLayOutVersion object for the chunkVersion.
+ * @param chunkVersion
+ * @return ChunkLayOutVersion
+ */
+ public static ChunkLayOutVersion getChunkLayOutVersion(int chunkVersion) {
+ Preconditions.checkArgument((chunkVersion <= ChunkLayOutVersion
+ .getLatestVersion().getVersion()));
+ for(ChunkLayOutVersion chunkLayOutVersion : CHUNK_LAYOUT_VERSION_INFOS) {
+ if(chunkLayOutVersion.getVersion() == chunkVersion) {
+ return chunkLayOutVersion;
+ }
+ }
+ return null;
+ }
+
/**
* Returns all versions.
*
diff --git a/hadoop-hdds/container-service/src/main/java/org/apache/hadoop/ozone/container/common/impl/ContainerData.java b/hadoop-hdds/container-service/src/main/java/org/apache/hadoop/ozone/container/common/impl/ContainerData.java
index 06aae6665a..0bd7795a26 100644
--- a/hadoop-hdds/container-service/src/main/java/org/apache/hadoop/ozone/container/common/impl/ContainerData.java
+++ b/hadoop-hdds/container-service/src/main/java/org/apache/hadoop/ozone/container/common/impl/ContainerData.java
@@ -42,7 +42,7 @@ public class ContainerData {
private final long containerId;
// Layout version of the container data
- private final ChunkLayOutVersion layOutVersion;
+ private final int layOutVersion;
// Metadata of the container will be a key value pair.
// This can hold information like volume name, owner etc.,
@@ -67,7 +67,27 @@ public class ContainerData {
public ContainerData(ContainerType type, long containerId) {
this.containerType = type;
this.containerId = containerId;
- this.layOutVersion = ChunkLayOutVersion.getLatestVersion();
+ this.layOutVersion = ChunkLayOutVersion.getLatestVersion().getVersion();
+ this.metadata = new TreeMap<>();
+ this.state = ContainerLifeCycleState.OPEN;
+ this.readCount = new AtomicLong(0L);
+ this.readBytes = new AtomicLong(0L);
+ this.writeCount = new AtomicLong(0L);
+ this.writeBytes = new AtomicLong(0L);
+ this.bytesUsed = new AtomicLong(0L);
+ }
+
+ /**
+ * Creates a ContainerData Object, which holds metadata of the container.
+ * @param type - ContainerType
+ * @param containerId - ContainerId
+ * @param layOutVersion - Container layOutVersion
+ */
+ public ContainerData(ContainerType type, long containerId, int
+ layOutVersion) {
+ this.containerType = type;
+ this.containerId = containerId;
+ this.layOutVersion = layOutVersion;
this.metadata = new TreeMap<>();
this.state = ContainerLifeCycleState.OPEN;
this.readCount = new AtomicLong(0L);
@@ -113,8 +133,8 @@ public synchronized void setState(ContainerLifeCycleState state) {
* Returns the layOutVersion of the actual container data format.
* @return layOutVersion
*/
- public ChunkLayOutVersion getLayOutVersion() {
- return layOutVersion;
+ public int getLayOutVersion() {
+ return ChunkLayOutVersion.getChunkLayOutVersion(layOutVersion).getVersion();
}
/**
diff --git a/hadoop-hdds/container-service/src/main/java/org/apache/hadoop/ozone/container/common/impl/KeyValueContainerData.java b/hadoop-hdds/container-service/src/main/java/org/apache/hadoop/ozone/container/common/impl/KeyValueContainerData.java
index 37eaa49fc1..57b5264331 100644
--- a/hadoop-hdds/container-service/src/main/java/org/apache/hadoop/ozone/container/common/impl/KeyValueContainerData.java
+++ b/hadoop-hdds/container-service/src/main/java/org/apache/hadoop/ozone/container/common/impl/KeyValueContainerData.java
@@ -52,12 +52,23 @@ public KeyValueContainerData(ContainerProtos.ContainerType type, long id) {
this.numPendingDeletionBlocks = 0;
}
+ /**
+ * Constructs KeyValueContainerData object.
+ * @param type - containerType
+ * @param id - ContainerId
+ * @param layOutVersion
+ */
+ public KeyValueContainerData(ContainerProtos.ContainerType type, long id,
+ int layOutVersion) {
+ super(type, id, layOutVersion);
+ this.numPendingDeletionBlocks = 0;
+ }
/**
* Returns path.
*
* @return - path
*/
- public String getDBPath() {
+ public String getDbPath() {
return dbPath;
}
@@ -66,7 +77,7 @@ public String getDBPath() {
*
* @param path - String.
*/
- public void setDBPath(String path) {
+ public void setDbPath(String path) {
this.dbPath = path;
}
@@ -74,7 +85,7 @@ public void setDBPath(String path) {
* Get container file path.
* @return - Physical path where container file and checksum is stored.
*/
- public String getContainerPath() {
+ public String getContainerFilePath() {
return containerFilePath;
}
@@ -82,7 +93,7 @@ public String getContainerPath() {
* Set container Path.
* @param containerPath - File path.
*/
- public void setContainerPath(String containerPath) {
+ public void setContainerFilePath(String containerPath) {
this.containerFilePath = containerPath;
}
diff --git a/hadoop-hdds/container-service/src/main/java/org/apache/hadoop/ozone/container/common/impl/KeyValueYaml.java b/hadoop-hdds/container-service/src/main/java/org/apache/hadoop/ozone/container/common/impl/KeyValueYaml.java
new file mode 100644
index 0000000000..b7ce0d96cb
--- /dev/null
+++ b/hadoop-hdds/container-service/src/main/java/org/apache/hadoop/ozone/container/common/impl/KeyValueYaml.java
@@ -0,0 +1,274 @@
+/*
+ * 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.ozone.container.common.impl;
+
+import com.google.common.base.Preconditions;
+import org.apache.hadoop.hdds.protocol.datanode.proto.ContainerProtos;
+import org.yaml.snakeyaml.Yaml;
+
+
+import java.beans.IntrospectionException;
+import java.io.IOException;
+import java.io.InputStream;
+import java.io.Writer;
+import java.io.FileInputStream;
+import java.io.FileOutputStream;
+import java.io.OutputStreamWriter;
+
+import java.io.File;
+
+
+import java.util.Set;
+import java.util.TreeSet;
+import java.util.Map;
+
+import org.yaml.snakeyaml.constructor.AbstractConstruct;
+import org.yaml.snakeyaml.constructor.Constructor;
+import org.yaml.snakeyaml.introspector.BeanAccess;
+import org.yaml.snakeyaml.introspector.Property;
+import org.yaml.snakeyaml.introspector.PropertyUtils;
+import org.yaml.snakeyaml.nodes.MappingNode;
+import org.yaml.snakeyaml.nodes.Node;
+import org.yaml.snakeyaml.nodes.ScalarNode;
+import org.yaml.snakeyaml.nodes.Tag;
+import org.yaml.snakeyaml.representer.Representer;
+
+/**
+ * Class for creating and reading .container files.
+ */
+
+public final class KeyValueYaml {
+
+ private KeyValueYaml() {
+
+ }
+ /**
+ * Creates a .container file in yaml format.
+ *
+ * @param containerFile
+ * @param containerData
+ * @throws IOException
+ */
+ public static void createContainerFile(File containerFile, ContainerData
+ containerData) throws IOException {
+
+ Preconditions.checkNotNull(containerFile, "yamlFile cannot be null");
+ Preconditions.checkNotNull(containerData, "containerData cannot be null");
+
+ PropertyUtils propertyUtils = new PropertyUtils();
+ propertyUtils.setBeanAccess(BeanAccess.FIELD);
+ propertyUtils.setAllowReadOnlyProperties(true);
+
+ Representer representer = new KeyValueContainerDataRepresenter();
+ representer.setPropertyUtils(propertyUtils);
+ representer.addClassTag(org.apache.hadoop.ozone.container.common.impl
+ .KeyValueContainerData.class, new Tag("KeyValueContainerData"));
+
+ Constructor keyValueDataConstructor = new KeyValueDataConstructor();
+
+ Yaml yaml = new Yaml(keyValueDataConstructor, representer);
+
+ Writer writer = new OutputStreamWriter(new FileOutputStream(containerFile),
+ "UTF-8");
+ yaml.dump(containerData, writer);
+ writer.close();
+ }
+
+ /**
+ * Read the yaml file, and return containerData.
+ *
+ * @param containerFile
+ * @throws IOException
+ */
+ public static KeyValueContainerData readContainerFile(File containerFile)
+ throws IOException {
+ Preconditions.checkNotNull(containerFile, "containerFile cannot be null");
+
+ InputStream input = null;
+ KeyValueContainerData keyValueContainerData;
+ try {
+ PropertyUtils propertyUtils = new PropertyUtils();
+ propertyUtils.setBeanAccess(BeanAccess.FIELD);
+ propertyUtils.setAllowReadOnlyProperties(true);
+
+ Representer representer = new KeyValueContainerDataRepresenter();
+ representer.setPropertyUtils(propertyUtils);
+ representer.addClassTag(org.apache.hadoop.ozone.container.common.impl
+ .KeyValueContainerData.class, new Tag("KeyValueContainerData"));
+
+ Constructor keyValueDataConstructor = new KeyValueDataConstructor();
+
+ Yaml yaml = new Yaml(keyValueDataConstructor, representer);
+ yaml.setBeanAccess(BeanAccess.FIELD);
+
+ input = new FileInputStream(containerFile);
+ keyValueContainerData = (KeyValueContainerData)
+ yaml.load(input);
+ } finally {
+ if (input!= null) {
+ input.close();
+ }
+ }
+ return keyValueContainerData;
+ }
+
+ /**
+ * Representer class to define which fields need to be stored in yaml file.
+ */
+ private static class KeyValueContainerDataRepresenter extends Representer {
+ @Override
+ protected Set getProperties(Class extends Object> type)
+ throws IntrospectionException {
+ Set set = super.getProperties(type);
+ Set filtered = new TreeSet();
+ if (type.equals(KeyValueContainerData.class)) {
+ // filter properties
+ for (Property prop : set) {
+ String name = prop.getName();
+ // When a new field needs to be added, it needs to be added here.
+ if (name.equals("containerType") || name.equals("containerId") ||
+ name.equals("layOutVersion") || name.equals("state") ||
+ name.equals("metadata") || name.equals("dbPath") ||
+ name.equals("containerFilePath") || name.equals(
+ "containerDBType")) {
+ filtered.add(prop);
+ }
+ }
+ }
+ return filtered;
+ }
+ }
+
+ /**
+ * Constructor class for KeyValueData, which will be used by Yaml.
+ */
+ private static class KeyValueDataConstructor extends Constructor {
+ KeyValueDataConstructor() {
+ //Adding our own specific constructors for tags.
+ this.yamlConstructors.put(new Tag("KeyValueContainerData"),
+ new ConstructKeyValueContainerData());
+ this.yamlConstructors.put(Tag.INT, new ConstructLong());
+ }
+
+ private class ConstructKeyValueContainerData extends AbstractConstruct {
+ public Object construct(Node node) {
+ MappingNode mnode = (MappingNode) node;
+ Map