Skip to content

Commit ef66e49

Browse files
bharatviswa504arp7
authored andcommitted
HDDS-1666. Issue in openKey when allocating block. (#943)
1 parent 585f4d5 commit ef66e49

File tree

2 files changed

+25
-4
lines changed

2 files changed

+25
-4
lines changed

hadoop-ozone/integration-test/src/test/java/org/apache/hadoop/ozone/om/TestKeyManagerImpl.java

Lines changed: 23 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@
2424
import java.nio.file.Paths;
2525
import java.util.ArrayList;
2626
import java.util.BitSet;
27+
import java.util.Collections;
2728
import java.util.HashMap;
2829
import java.util.HashSet;
2930
import java.util.List;
@@ -61,6 +62,7 @@
6162
import org.apache.hadoop.ozone.om.helpers.OmKeyArgs;
6263
import org.apache.hadoop.ozone.om.helpers.OmKeyInfo;
6364
import org.apache.hadoop.ozone.om.helpers.OmKeyLocationInfo;
65+
import org.apache.hadoop.ozone.om.helpers.OmKeyLocationInfoGroup;
6466
import org.apache.hadoop.ozone.om.helpers.OmPrefixInfo;
6567
import org.apache.hadoop.ozone.om.helpers.OmVolumeArgs;
6668
import org.apache.hadoop.ozone.om.helpers.OpenKeySession;
@@ -76,6 +78,7 @@
7678
import org.apache.hadoop.test.GenericTestUtils;
7779
import org.apache.hadoop.test.LambdaTestUtils;
7880

81+
import org.apache.hadoop.util.Time;
7982
import org.junit.After;
8083
import org.junit.AfterClass;
8184
import org.junit.Assert;
@@ -213,11 +216,29 @@ public void allocateBlockFailureInSafeMode() throws Exception {
213216
OmKeyArgs keyArgs = createBuilder()
214217
.setKeyName(KEY_NAME)
215218
.build();
216-
OpenKeySession keySession = keyManager1.openKey(keyArgs);
219+
220+
// As now openKey will allocate at least one block, even if the size
221+
// passed is 0. So adding an entry to openKeyTable manually to test
222+
// allocateBlock failure.
223+
OmKeyInfo omKeyInfo = new OmKeyInfo.Builder()
224+
.setVolumeName(keyArgs.getVolumeName())
225+
.setBucketName(keyArgs.getBucketName())
226+
.setKeyName(keyArgs.getKeyName())
227+
.setOmKeyLocationInfos(Collections.singletonList(
228+
new OmKeyLocationInfoGroup(0, new ArrayList<>())))
229+
.setCreationTime(Time.now())
230+
.setModificationTime(Time.now())
231+
.setDataSize(0)
232+
.setReplicationType(keyArgs.getType())
233+
.setReplicationFactor(keyArgs.getFactor())
234+
.setFileEncryptionInfo(null).build();
235+
metadataManager.getOpenKeyTable().put(
236+
metadataManager.getOpenKey(VOLUME_NAME, BUCKET_NAME, KEY_NAME, 1L),
237+
omKeyInfo);
217238
LambdaTestUtils.intercept(OMException.class,
218239
"SafeModePrecheck failed for allocateBlock", () -> {
219240
keyManager1
220-
.allocateBlock(keyArgs, keySession.getId(), new ExcludeList());
241+
.allocateBlock(keyArgs, 1L, new ExcludeList());
221242
});
222243
}
223244

hadoop-ozone/ozone-manager/src/main/java/org/apache/hadoop/ozone/om/KeyManagerImpl.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -436,7 +436,7 @@ public OpenKeySession openKey(OmKeyArgs args) throws IOException {
436436
// client should expect, in terms of current size of key. If client sets
437437
// a value, then this value is used, otherwise, we allocate a single
438438
// block which is the current size, if read by the client.
439-
final long size = args.getDataSize() >= 0 ?
439+
final long size = args.getDataSize() > 0 ?
440440
args.getDataSize() : scmBlockSize;
441441
final List<OmKeyLocationInfo> locations = new ArrayList<>();
442442

@@ -477,7 +477,7 @@ public OpenKeySession openKey(OmKeyArgs args) throws IOException {
477477
openVersion = keyInfo.getLatestVersionLocations().getVersion();
478478
LOG.debug("Key {} allocated in volume {} bucket {}",
479479
keyName, volumeName, bucketName);
480-
allocateBlockInKey(keyInfo, args.getDataSize(), currentTime);
480+
allocateBlockInKey(keyInfo, size, currentTime);
481481
return new OpenKeySession(currentTime, keyInfo, openVersion);
482482
}
483483

0 commit comments

Comments
 (0)