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-6440. Handle custom metadata (x-amz-meta) during put-object through S3 API. #3200

Closed
wants to merge 11 commits into from
Expand Up @@ -23,6 +23,8 @@
import org.apache.hadoop.hdds.client.ReplicationType;

import java.time.Instant;
import java.util.HashMap;
import java.util.Map;

/**
* A class that encapsulates OzoneKey.
Expand Down Expand Up @@ -56,6 +58,8 @@ public class OzoneKey {

private ReplicationConfig replicationConfig;

private Map<String, String> metadata = new HashMap<>();

/**
* Constructs OzoneKey from OmKeyInfo.
*
Expand All @@ -76,6 +80,22 @@ public OzoneKey(String volumeName, String bucketName,
ReplicationFactor.valueOf(replicationFactor));
}

@SuppressWarnings("parameternumber")
public OzoneKey(String volumeName, String bucketName,
String keyName, long size, long creationTime,
long modificationTime, ReplicationType type,
int replicationFactor, Map<String, String> metadata) {
Copy link
Contributor

Choose a reason for hiding this comment

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

I think we can omit this constructor (with type and factor), it should be deprecated in favor of ReplicationConfig right away.

this.volumeName = volumeName;
this.bucketName = bucketName;
this.name = keyName;
this.dataSize = size;
this.creationTime = Instant.ofEpochMilli(creationTime);
Copy link
Contributor

Choose a reason for hiding this comment

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

These should be set by OM?

Copy link
Contributor

Choose a reason for hiding this comment

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

Also, nesting the constructors would help reduce code.

Copy link
Contributor

Choose a reason for hiding this comment

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

looking into it

this.modificationTime = Instant.ofEpochMilli(modificationTime);
this.replicationConfig = ReplicationConfig.fromTypeAndFactor(type,
ReplicationFactor.valueOf(replicationFactor));
this.metadata.putAll(metadata);
}

/**
* Constructs OzoneKey from OmKeyInfo.
*
Expand All @@ -93,6 +113,20 @@ public OzoneKey(String volumeName, String bucketName,
this.replicationConfig = replicationConfig;
}

@SuppressWarnings("parameternumber")
public OzoneKey(String volumeName, String bucketName,
String keyName, long size, long creationTime,
long modificationTime, ReplicationConfig replicationConfig,
Map<String, String> metadata) {
this.volumeName = volumeName;
this.bucketName = bucketName;
this.name = keyName;
this.dataSize = size;
this.creationTime = Instant.ofEpochMilli(creationTime);
this.modificationTime = Instant.ofEpochMilli(modificationTime);
this.replicationConfig = replicationConfig;
Copy link
Contributor

Choose a reason for hiding this comment

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

Suggested change
this.volumeName = volumeName;
this.bucketName = bucketName;
this.name = keyName;
this.dataSize = size;
this.creationTime = Instant.ofEpochMilli(creationTime);
this.modificationTime = Instant.ofEpochMilli(modificationTime);
this.replicationConfig = replicationConfig;
this(volumeName, bucketName, keyName, size, creationTime,
modificationTime, replicationConfig);

this.metadata.putAll(metadata);
}
/**
* Returns Volume Name associated with the Key.
*
Expand Down Expand Up @@ -153,6 +187,10 @@ public Instant getModificationTime() {
* @return replicationType
*/

public Map<String, String> getMetadata() {
return metadata;
}

@Deprecated
public ReplicationType getReplicationType() {
return ReplicationType
Expand Down
Expand Up @@ -35,8 +35,6 @@ public class OzoneKeyDetails extends OzoneKey {
*/
private List<OzoneKeyLocation> ozoneKeyLocations;

private Map<String, String> metadata;

private FileEncryptionInfo feInfo;

/**
Expand All @@ -50,9 +48,8 @@ public OzoneKeyDetails(String volumeName, String bucketName, String keyName,
ReplicationType type, Map<String, String> metadata,
FileEncryptionInfo feInfo, int replicationFactor) {
super(volumeName, bucketName, keyName, size, creationTime,
modificationTime, type, replicationFactor);
modificationTime, type, replicationFactor, metadata);
this.ozoneKeyLocations = ozoneKeyLocations;
this.metadata = metadata;
this.feInfo = feInfo;
}

Expand All @@ -68,9 +65,8 @@ public OzoneKeyDetails(String volumeName, String bucketName, String keyName,
Map<String, String> metadata,
FileEncryptionInfo feInfo) {
super(volumeName, bucketName, keyName, size, creationTime,
modificationTime, replicationConfig);
modificationTime, replicationConfig, metadata);
this.ozoneKeyLocations = ozoneKeyLocations;
this.metadata = metadata;
this.feInfo = feInfo;
}

Expand All @@ -81,10 +77,6 @@ public List<OzoneKeyLocation> getOzoneKeyLocations() {
return ozoneKeyLocations;
}

public Map<String, String> getMetadata() {
return metadata;
}

public FileEncryptionInfo getFileEncryptionInfo() {
return feInfo;
}
Expand Down
Expand Up @@ -1627,7 +1627,8 @@ public OzoneKey headObject(String volumeName, String bucketName,

return new OzoneKey(keyInfo.getVolumeName(), keyInfo.getBucketName(),
keyInfo.getKeyName(), keyInfo.getDataSize(), keyInfo.getCreationTime(),
keyInfo.getModificationTime(), keyInfo.getReplicationConfig());
keyInfo.getModificationTime(), keyInfo.getReplicationConfig(),
keyInfo.getMetadata());

}

Expand Down
12 changes: 12 additions & 0 deletions hadoop-ozone/dist/src/main/smoketest/s3/objectputget.robot
Expand Up @@ -153,3 +153,15 @@ Zero byte file

${result} = Execute AWSS3APICli and checkrc get-object --bucket ${BUCKET} --key ${PREFIX}/putobject/key=value/zerobyte --range bytes=0-10000 /tmp/testfile2.result 255
Should contain ${result} InvalidRange

Create file with user defined metadata
Execute echo "Randomtext" > /tmp/testfile2
Execute AWSS3ApiCli put-object --bucket ${BUCKET} --key ${PREFIX}/putobject/custom-metadata/key1 --body /tmp/testfile2 --metadata="custom-key1=custom-value1,custom-key2=custom-value2"

${result} = Execute AWSS3APICli head-object --bucket ${BUCKET} --key ${PREFIX}/putobject/custom-metadata/key1
Should contain ${result} \"custom-key1\": \"custom-value1\"
Should contain ${result} \"custom-key2\": \"custom-value2\"

${result} = Execute ozone sh key info /s3v/${BUCKET}/${PREFIX}/putobject/custom-metadata/key1
Should contain ${result} \"custom-key1\" : \"custom-value1\"
Should contain ${result} \"custom-key2\" : \"custom-value2\"
Expand Up @@ -19,14 +19,24 @@

import javax.annotation.PostConstruct;
import javax.inject.Inject;
import javax.ws.rs.core.MultivaluedMap;
import javax.ws.rs.core.Response;

import java.io.IOException;
import java.util.Collections;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.function.Function;
import java.util.stream.Collectors;

import org.apache.commons.lang3.StringUtils;
import org.apache.hadoop.hdds.scm.client.HddsClientUtils;
import org.apache.hadoop.ozone.client.OzoneBucket;
import org.apache.hadoop.ozone.client.OzoneClient;
import org.apache.hadoop.ozone.client.OzoneKey;
import org.apache.hadoop.ozone.client.OzoneVolume;
import org.apache.hadoop.ozone.om.exceptions.OMException;
import org.apache.hadoop.ozone.om.exceptions.OMException.ResultCodes;
Expand All @@ -35,10 +45,12 @@
import org.apache.hadoop.ozone.s3.exception.S3ErrorTable;

import com.google.common.annotations.VisibleForTesting;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import static org.apache.hadoop.ozone.s3.exception.S3ErrorTable.newError;
import static org.apache.hadoop.ozone.s3.util.S3Consts.CUSTOM_METADATA_HEADER_PREFIX;

/**
* Basic helpers for all the REST endpoints.
Expand Down Expand Up @@ -190,6 +202,38 @@ private Iterator<? extends OzoneBucket> iterateBuckets(
}
}

protected Map<String, String> getCustomMetadataFromHeaders(
kerneltime marked this conversation as resolved.
Show resolved Hide resolved
MultivaluedMap<String, String> requestHeaders) {
Map<String, String> customMetadata = new HashMap<>();
if (requestHeaders == null || requestHeaders.isEmpty()) {
return customMetadata;
}

Set<String> customMetadataKeys = requestHeaders.keySet().stream()
.filter(k -> k.startsWith(CUSTOM_METADATA_HEADER_PREFIX))
.collect(Collectors.toSet());
if (!customMetadataKeys.isEmpty()) {
for (String key : customMetadataKeys) {
String mapKey =
key.substring(CUSTOM_METADATA_HEADER_PREFIX.length());
List<String> values = requestHeaders.get(key);
customMetadata.put(mapKey, StringUtils.join(values, ","));
}
}
return customMetadata;
}

protected void addCustomMetadataHeaders(
Response.ResponseBuilder responseBuilder, OzoneKey key) {

Map<String, String> metadata = key.getMetadata();
for (Map.Entry<String, String> entry : metadata.entrySet()) {
responseBuilder
.header(CUSTOM_METADATA_HEADER_PREFIX + entry.getKey(),
entry.getValue());
}
}

@VisibleForTesting
public void setClient(OzoneClient ozoneClient) {
this.client = ozoneClient;
Expand Down
Expand Up @@ -47,7 +47,6 @@
import java.time.ZoneId;
import java.time.ZonedDateTime;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
Expand Down Expand Up @@ -196,9 +195,10 @@ public Response put(

// Normal put object
OzoneBucket bucket = getBucket(bucketName);

Map<String, String> customMetadata =
getCustomMetadataFromHeaders(headers.getRequestHeaders());
output = bucket.createKey(keyPath, length, replicationType,
replicationFactor, new HashMap<>());
replicationFactor, customMetadata);

if ("STREAMING-AWS4-HMAC-SHA256-PAYLOAD"
.equals(headers.getHeaderString("x-amz-content-sha256"))) {
Expand Down Expand Up @@ -377,6 +377,7 @@ public Response head(
.header("Content-Length", key.getDataSize())
.header("Content-Type", "binary/octet-stream");
addLastModifiedDate(response, key);
addCustomMetadataHeaders(response, key);
return response.build();
}

Expand Down Expand Up @@ -771,7 +772,7 @@ private CopyObjectResponse copyObject(String copyHeader,
sourceInputStream = sourceOzoneBucket.readKey(sourceKey);

destOutputStream = destOzoneBucket.createKey(destkey, sourceKeyLen,
replicationType, replicationFactor, new HashMap<>());
replicationType, replicationFactor, sourceKeyDetails.getMetadata());

IOUtils.copy(sourceInputStream, destOutputStream);

Expand Down
Expand Up @@ -62,4 +62,6 @@ private S3Consts() {
public static final String S3_XML_NAMESPACE = "http://s3.amazonaws" +
".com/doc/2006-03-01/";

public static final String CUSTOM_METADATA_HEADER_PREFIX = "x-amz-meta-";

}