From ef66e4999f3cd5f0ea2fa018359facb776bf892f Mon Sep 17 00:00:00 2001 From: Bharat Viswanadham Date: Mon, 15 Jul 2019 17:54:41 -0700 Subject: [PATCH] HDDS-1666. Issue in openKey when allocating block. (#943) --- .../hadoop/ozone/om/TestKeyManagerImpl.java | 25 +++++++++++++++++-- .../hadoop/ozone/om/KeyManagerImpl.java | 4 +-- 2 files changed, 25 insertions(+), 4 deletions(-) diff --git a/hadoop-ozone/integration-test/src/test/java/org/apache/hadoop/ozone/om/TestKeyManagerImpl.java b/hadoop-ozone/integration-test/src/test/java/org/apache/hadoop/ozone/om/TestKeyManagerImpl.java index 0aa301ae8fd..0c5ce2b487d 100644 --- a/hadoop-ozone/integration-test/src/test/java/org/apache/hadoop/ozone/om/TestKeyManagerImpl.java +++ b/hadoop-ozone/integration-test/src/test/java/org/apache/hadoop/ozone/om/TestKeyManagerImpl.java @@ -24,6 +24,7 @@ import java.nio.file.Path; import java.nio.file.Paths; import java.util.ArrayList; import java.util.BitSet; +import java.util.Collections; import java.util.HashMap; import java.util.HashSet; import java.util.List; @@ -61,6 +62,7 @@ import org.apache.hadoop.ozone.om.helpers.OmBucketInfo; import org.apache.hadoop.ozone.om.helpers.OmKeyArgs; import org.apache.hadoop.ozone.om.helpers.OmKeyInfo; import org.apache.hadoop.ozone.om.helpers.OmKeyLocationInfo; +import org.apache.hadoop.ozone.om.helpers.OmKeyLocationInfoGroup; import org.apache.hadoop.ozone.om.helpers.OmPrefixInfo; import org.apache.hadoop.ozone.om.helpers.OmVolumeArgs; import org.apache.hadoop.ozone.om.helpers.OpenKeySession; @@ -76,6 +78,7 @@ import org.apache.hadoop.security.UserGroupInformation; import org.apache.hadoop.test.GenericTestUtils; import org.apache.hadoop.test.LambdaTestUtils; +import org.apache.hadoop.util.Time; import org.junit.After; import org.junit.AfterClass; import org.junit.Assert; @@ -213,11 +216,29 @@ public class TestKeyManagerImpl { OmKeyArgs keyArgs = createBuilder() .setKeyName(KEY_NAME) .build(); - OpenKeySession keySession = keyManager1.openKey(keyArgs); + + // As now openKey will allocate at least one block, even if the size + // passed is 0. So adding an entry to openKeyTable manually to test + // allocateBlock failure. + OmKeyInfo omKeyInfo = new OmKeyInfo.Builder() + .setVolumeName(keyArgs.getVolumeName()) + .setBucketName(keyArgs.getBucketName()) + .setKeyName(keyArgs.getKeyName()) + .setOmKeyLocationInfos(Collections.singletonList( + new OmKeyLocationInfoGroup(0, new ArrayList<>()))) + .setCreationTime(Time.now()) + .setModificationTime(Time.now()) + .setDataSize(0) + .setReplicationType(keyArgs.getType()) + .setReplicationFactor(keyArgs.getFactor()) + .setFileEncryptionInfo(null).build(); + metadataManager.getOpenKeyTable().put( + metadataManager.getOpenKey(VOLUME_NAME, BUCKET_NAME, KEY_NAME, 1L), + omKeyInfo); LambdaTestUtils.intercept(OMException.class, "SafeModePrecheck failed for allocateBlock", () -> { keyManager1 - .allocateBlock(keyArgs, keySession.getId(), new ExcludeList()); + .allocateBlock(keyArgs, 1L, new ExcludeList()); }); } diff --git a/hadoop-ozone/ozone-manager/src/main/java/org/apache/hadoop/ozone/om/KeyManagerImpl.java b/hadoop-ozone/ozone-manager/src/main/java/org/apache/hadoop/ozone/om/KeyManagerImpl.java index 9e29825d8b1..90f7e4a901a 100644 --- a/hadoop-ozone/ozone-manager/src/main/java/org/apache/hadoop/ozone/om/KeyManagerImpl.java +++ b/hadoop-ozone/ozone-manager/src/main/java/org/apache/hadoop/ozone/om/KeyManagerImpl.java @@ -436,7 +436,7 @@ public class KeyManagerImpl implements KeyManager { // client should expect, in terms of current size of key. If client sets // a value, then this value is used, otherwise, we allocate a single // block which is the current size, if read by the client. - final long size = args.getDataSize() >= 0 ? + final long size = args.getDataSize() > 0 ? args.getDataSize() : scmBlockSize; final List locations = new ArrayList<>(); @@ -477,7 +477,7 @@ public class KeyManagerImpl implements KeyManager { openVersion = keyInfo.getLatestVersionLocations().getVersion(); LOG.debug("Key {} allocated in volume {} bucket {}", keyName, volumeName, bucketName); - allocateBlockInKey(keyInfo, args.getDataSize(), currentTime); + allocateBlockInKey(keyInfo, size, currentTime); return new OpenKeySession(currentTime, keyInfo, openVersion); }