|
| 1 | +/** |
| 2 | + * Licensed to the Apache Software Foundation (ASF) under one or more |
| 3 | + * contributor license agreements. See the NOTICE file distributed with this |
| 4 | + * work for additional information regarding copyright ownership. The ASF |
| 5 | + * licenses this file to you under the Apache License, Version 2.0 (the |
| 6 | + * "License"); you may not use this file except in compliance with the License. |
| 7 | + * You may obtain a copy of the License at |
| 8 | + * <p> |
| 9 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 10 | + * <p> |
| 11 | + * Unless required by applicable law or agreed to in writing, software |
| 12 | + * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT |
| 13 | + * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the |
| 14 | + * License for the specific language governing permissions and limitations under |
| 15 | + * the License. |
| 16 | + */ |
| 17 | + |
| 18 | +package org.apache.hadoop.ozone.client.rpc; |
| 19 | + |
| 20 | +import org.apache.hadoop.hdds.client.ReplicationType; |
| 21 | +import org.apache.hadoop.hdds.conf.OzoneConfiguration; |
| 22 | +import org.apache.hadoop.hdds.protocol.DatanodeDetails; |
| 23 | +import org.apache.hadoop.hdds.protocol.proto.HddsProtos; |
| 24 | +import org.apache.hadoop.hdds.scm.container.ContainerID; |
| 25 | +import org.apache.hadoop.hdds.scm.container.ContainerInfo; |
| 26 | +import org.apache.hadoop.hdds.scm.pipeline.Pipeline; |
| 27 | +import org.apache.hadoop.ozone.MiniOzoneCluster; |
| 28 | +import org.apache.hadoop.ozone.OzoneConfigKeys; |
| 29 | +import org.apache.hadoop.ozone.OzoneConsts; |
| 30 | +import org.apache.hadoop.ozone.client.ObjectStore; |
| 31 | +import org.apache.hadoop.ozone.client.OzoneClient; |
| 32 | +import org.apache.hadoop.ozone.client.OzoneClientFactory; |
| 33 | +import org.apache.hadoop.ozone.client.io.KeyOutputStream; |
| 34 | +import org.apache.hadoop.ozone.client.io.OzoneOutputStream; |
| 35 | +import org.apache.hadoop.ozone.container.ContainerTestHelper; |
| 36 | +import org.apache.hadoop.ozone.om.helpers.OmKeyArgs; |
| 37 | +import org.apache.hadoop.ozone.om.helpers.OmKeyInfo; |
| 38 | +import org.apache.hadoop.ozone.om.helpers.OmKeyLocationInfo; |
| 39 | +import org.junit.Assert; |
| 40 | +import org.junit.Test; |
| 41 | + |
| 42 | +import java.io.IOException; |
| 43 | +import java.util.List; |
| 44 | +import java.util.UUID; |
| 45 | +import java.util.concurrent.TimeUnit; |
| 46 | + |
| 47 | +import static org.apache.hadoop.hdds.scm.ScmConfigKeys.HDDS_SCM_WATCHER_TIMEOUT; |
| 48 | +import static org.apache.hadoop.hdds.scm.ScmConfigKeys.OZONE_SCM_STALENODE_INTERVAL; |
| 49 | + |
| 50 | +/** |
| 51 | + * Tests MultiBlock Writes with Dn failures by Ozone Client. |
| 52 | + */ |
| 53 | +public class TestMultiBlockWritesWithDnFailures { |
| 54 | + |
| 55 | + private MiniOzoneCluster cluster; |
| 56 | + private OzoneConfiguration conf; |
| 57 | + private OzoneClient client; |
| 58 | + private ObjectStore objectStore; |
| 59 | + private int chunkSize; |
| 60 | + private int blockSize; |
| 61 | + private String volumeName; |
| 62 | + private String bucketName; |
| 63 | + private String keyString; |
| 64 | + private int maxRetries; |
| 65 | + |
| 66 | + /** |
| 67 | + * Create a MiniDFSCluster for testing. |
| 68 | + * <p> |
| 69 | + * Ozone is made active by setting OZONE_ENABLED = true |
| 70 | + * |
| 71 | + * @throws IOException |
| 72 | + */ |
| 73 | + private void init() throws Exception { |
| 74 | + conf = new OzoneConfiguration(); |
| 75 | + maxRetries = 100; |
| 76 | + chunkSize = (int) OzoneConsts.MB; |
| 77 | + blockSize = 4 * chunkSize; |
| 78 | + conf.setTimeDuration(OzoneConfigKeys.OZONE_CLIENT_WATCH_REQUEST_TIMEOUT, 5, |
| 79 | + TimeUnit.SECONDS); |
| 80 | + conf.setTimeDuration(HDDS_SCM_WATCHER_TIMEOUT, 1000, TimeUnit.MILLISECONDS); |
| 81 | + conf.setTimeDuration(OZONE_SCM_STALENODE_INTERVAL, 100, TimeUnit.SECONDS); |
| 82 | + conf.setInt(OzoneConfigKeys.DFS_RATIS_CLIENT_REQUEST_MAX_RETRIES_KEY, 5); |
| 83 | + conf.setTimeDuration( |
| 84 | + OzoneConfigKeys.DFS_RATIS_CLIENT_REQUEST_RETRY_INTERVAL_KEY, |
| 85 | + 1, TimeUnit.SECONDS); |
| 86 | + |
| 87 | + conf.setQuietMode(false); |
| 88 | + cluster = MiniOzoneCluster.newBuilder(conf) |
| 89 | + .setNumDatanodes(6).build(); |
| 90 | + cluster.waitForClusterToBeReady(); |
| 91 | + //the easiest way to create an open container is creating a key |
| 92 | + client = OzoneClientFactory.getClient(conf); |
| 93 | + objectStore = client.getObjectStore(); |
| 94 | + keyString = UUID.randomUUID().toString(); |
| 95 | + volumeName = "datanodefailurehandlingtest"; |
| 96 | + bucketName = volumeName; |
| 97 | + objectStore.createVolume(volumeName); |
| 98 | + objectStore.getVolume(volumeName).createBucket(bucketName); |
| 99 | + } |
| 100 | + |
| 101 | + private void startCluster() throws Exception { |
| 102 | + init(); |
| 103 | + } |
| 104 | + |
| 105 | + /** |
| 106 | + * Shutdown MiniDFSCluster. |
| 107 | + */ |
| 108 | + private void shutdown() { |
| 109 | + if (cluster != null) { |
| 110 | + cluster.shutdown(); |
| 111 | + } |
| 112 | + } |
| 113 | + |
| 114 | + @Test |
| 115 | + public void testMultiBlockWritesWithDnFailures() throws Exception { |
| 116 | + startCluster(); |
| 117 | + String keyName = "ratis3"; |
| 118 | + OzoneOutputStream key = createKey(keyName, ReplicationType.RATIS, 0); |
| 119 | + String data = |
| 120 | + ContainerTestHelper |
| 121 | + .getFixedLengthString(keyString, blockSize + chunkSize); |
| 122 | + key.write(data.getBytes()); |
| 123 | + |
| 124 | + // get the name of a valid container |
| 125 | + Assert.assertTrue(key.getOutputStream() instanceof KeyOutputStream); |
| 126 | + KeyOutputStream groupOutputStream = |
| 127 | + (KeyOutputStream) key.getOutputStream(); |
| 128 | + List<OmKeyLocationInfo> locationInfoList = |
| 129 | + groupOutputStream.getLocationInfoList(); |
| 130 | + Assert.assertTrue(locationInfoList.size() == 2); |
| 131 | + long containerId = locationInfoList.get(1).getContainerID(); |
| 132 | + ContainerInfo container = cluster.getStorageContainerManager() |
| 133 | + .getContainerManager() |
| 134 | + .getContainer(ContainerID.valueof(containerId)); |
| 135 | + Pipeline pipeline = |
| 136 | + cluster.getStorageContainerManager().getPipelineManager() |
| 137 | + .getPipeline(container.getPipelineID()); |
| 138 | + List<DatanodeDetails> datanodes = pipeline.getNodes(); |
| 139 | + cluster.shutdownHddsDatanode(datanodes.get(0)); |
| 140 | + cluster.shutdownHddsDatanode(datanodes.get(1)); |
| 141 | + |
| 142 | + // The write will fail but exception will be handled and length will be |
| 143 | + // updated correctly in OzoneManager once the steam is closed |
| 144 | + key.write(data.getBytes()); |
| 145 | + key.close(); |
| 146 | + OmKeyArgs keyArgs = new OmKeyArgs.Builder().setVolumeName(volumeName) |
| 147 | + .setBucketName(bucketName).setType(HddsProtos.ReplicationType.RATIS) |
| 148 | + .setFactor(HddsProtos.ReplicationFactor.THREE).setKeyName(keyName) |
| 149 | + .setRefreshPipeline(true) |
| 150 | + .build(); |
| 151 | + OmKeyInfo keyInfo = cluster.getOzoneManager().lookupKey(keyArgs); |
| 152 | + Assert.assertEquals(2 * data.getBytes().length, keyInfo.getDataSize()); |
| 153 | + validateData(keyName, data.concat(data).getBytes()); |
| 154 | + shutdown(); |
| 155 | + } |
| 156 | + |
| 157 | + private OzoneOutputStream createKey(String keyName, ReplicationType type, |
| 158 | + long size) throws Exception { |
| 159 | + return ContainerTestHelper |
| 160 | + .createKey(keyName, type, size, objectStore, volumeName, bucketName); |
| 161 | + } |
| 162 | + |
| 163 | + private void validateData(String keyName, byte[] data) throws Exception { |
| 164 | + ContainerTestHelper |
| 165 | + .validateData(keyName, data, objectStore, volumeName, bucketName); |
| 166 | + } |
| 167 | + |
| 168 | +} |
0 commit comments