|
21 | 21 | import java.io.InputStream; |
22 | 22 | import java.io.OutputStream; |
23 | 23 |
|
| 24 | +import org.apache.commons.logging.Log; |
| 25 | +import org.apache.commons.logging.LogFactory; |
24 | 26 | import org.apache.hadoop.conf.Configuration; |
25 | | -import org.apache.hadoop.fs.FileSystem; |
26 | | -import org.apache.hadoop.fs.Path; |
27 | | -import org.apache.hadoop.io.IOUtils; |
| 27 | +import org.apache.hadoop.fs.*; |
| 28 | +import org.apache.hadoop.fs.permission.FsPermission; |
| 29 | +import org.apache.hadoop.hdfs.protocol.*; |
| 30 | +import org.apache.hadoop.hdfs.protocol.FSConstants.UpgradeAction; |
| 31 | +import org.apache.hadoop.hdfs.server.common.*; |
| 32 | +import org.apache.hadoop.hdfs.server.namenode.NotReplicatedYetException; |
| 33 | +import org.apache.hadoop.io.*; |
| 34 | +import org.apache.hadoop.ipc.RemoteException; |
| 35 | +import org.apache.hadoop.security.AccessControlException; |
28 | 36 |
|
29 | 37 | import junit.framework.TestCase; |
30 | 38 |
|
|
34 | 42 | * properly in case of errors. |
35 | 43 | */ |
36 | 44 | public class TestDFSClientRetries extends TestCase { |
| 45 | + public static final Log LOG = |
| 46 | + LogFactory.getLog(TestDFSClientRetries.class.getName()); |
37 | 47 |
|
38 | 48 | // writes 'len' bytes of data to out. |
39 | 49 | private static void writeData(OutputStream out, int len) throws IOException { |
@@ -97,4 +107,132 @@ public void testWriteTimeoutAtDataNode() throws IOException, |
97 | 107 | } |
98 | 108 |
|
99 | 109 | // more tests related to different failure cases can be added here. |
| 110 | + |
| 111 | + class TestNameNode implements ClientProtocol |
| 112 | + { |
| 113 | + int num_calls = 0; |
| 114 | + |
| 115 | + // The total number of calls that can be made to addBlock |
| 116 | + // before an exception is thrown |
| 117 | + int num_calls_allowed; |
| 118 | + public final String ADD_BLOCK_EXCEPTION = "Testing exception thrown from" |
| 119 | + + "TestDFSClientRetries::" |
| 120 | + + "TestNameNode::addBlock"; |
| 121 | + public final String RETRY_CONFIG |
| 122 | + = "dfs.client.block.write.locateFollowingBlock.retries"; |
| 123 | + |
| 124 | + public TestNameNode(Configuration conf) throws IOException |
| 125 | + { |
| 126 | + // +1 because the configuration value is the number of retries and |
| 127 | + // the first call is not a retry (e.g., 2 retries == 3 total |
| 128 | + // calls allowed) |
| 129 | + this.num_calls_allowed = conf.getInt(RETRY_CONFIG, 5) + 1; |
| 130 | + } |
| 131 | + |
| 132 | + public long getProtocolVersion(String protocol, |
| 133 | + long clientVersion) |
| 134 | + throws IOException |
| 135 | + { |
| 136 | + return versionID; |
| 137 | + } |
| 138 | + |
| 139 | + public LocatedBlock addBlock(String src, String clientName) |
| 140 | + throws IOException |
| 141 | + { |
| 142 | + num_calls++; |
| 143 | + if (num_calls > num_calls_allowed) { |
| 144 | + throw new IOException("addBlock called more times than " |
| 145 | + + RETRY_CONFIG |
| 146 | + + " allows."); |
| 147 | + } else { |
| 148 | + throw new RemoteException(NotReplicatedYetException.class.getName(), |
| 149 | + ADD_BLOCK_EXCEPTION); |
| 150 | + } |
| 151 | + } |
| 152 | + |
| 153 | + |
| 154 | + // The following methods are stub methods that are not needed by this mock class |
| 155 | + |
| 156 | + public LocatedBlocks getBlockLocations(String src, long offset, long length) throws IOException { return null; } |
| 157 | + |
| 158 | + public void create(String src, FsPermission masked, String clientName, EnumSetWritable<CreateFlag> flag, short replication, long blockSize) throws IOException {} |
| 159 | + |
| 160 | + public LocatedBlock append(String src, String clientName) throws IOException { return null; } |
| 161 | + |
| 162 | + public boolean setReplication(String src, short replication) throws IOException { return false; } |
| 163 | + |
| 164 | + public void setPermission(String src, FsPermission permission) throws IOException {} |
| 165 | + |
| 166 | + public void setOwner(String src, String username, String groupname) throws IOException {} |
| 167 | + |
| 168 | + public void abandonBlock(Block b, String src, String holder) throws IOException {} |
| 169 | + |
| 170 | + public boolean complete(String src, String clientName) throws IOException { return false; } |
| 171 | + |
| 172 | + public void reportBadBlocks(LocatedBlock[] blocks) throws IOException {} |
| 173 | + |
| 174 | + public boolean rename(String src, String dst) throws IOException { return false; } |
| 175 | + |
| 176 | + public boolean delete(String src) throws IOException { return false; } |
| 177 | + |
| 178 | + public boolean delete(String src, boolean recursive) throws IOException { return false; } |
| 179 | + |
| 180 | + public boolean mkdirs(String src, FsPermission masked) throws IOException { return false; } |
| 181 | + |
| 182 | + public FileStatus[] getListing(String src) throws IOException { return null; } |
| 183 | + |
| 184 | + public void renewLease(String clientName) throws IOException {} |
| 185 | + |
| 186 | + public long[] getStats() throws IOException { return null; } |
| 187 | + |
| 188 | + public DatanodeInfo[] getDatanodeReport(FSConstants.DatanodeReportType type) throws IOException { return null; } |
| 189 | + |
| 190 | + public long getPreferredBlockSize(String filename) throws IOException { return 0; } |
| 191 | + |
| 192 | + public boolean setSafeMode(FSConstants.SafeModeAction action) throws IOException { return false; } |
| 193 | + |
| 194 | + public void saveNamespace() throws IOException {} |
| 195 | + |
| 196 | + public boolean restoreFailedStorage(String arg) throws AccessControlException { return false; } |
| 197 | + |
| 198 | + public void refreshNodes() throws IOException {} |
| 199 | + |
| 200 | + public void finalizeUpgrade() throws IOException {} |
| 201 | + |
| 202 | + public UpgradeStatusReport distributedUpgradeProgress(UpgradeAction action) throws IOException { return null; } |
| 203 | + |
| 204 | + public void metaSave(String filename) throws IOException {} |
| 205 | + |
| 206 | + public FileStatus getFileInfo(String src) throws IOException { return null; } |
| 207 | + |
| 208 | + public ContentSummary getContentSummary(String path) throws IOException { return null; } |
| 209 | + |
| 210 | + public void setQuota(String path, long namespaceQuota, long diskspaceQuota) throws IOException {} |
| 211 | + |
| 212 | + public void fsync(String src, String client) throws IOException {} |
| 213 | + |
| 214 | + public void setTimes(String src, long mtime, long atime) throws IOException {} |
| 215 | + |
| 216 | + } |
| 217 | + |
| 218 | + public void testNotYetReplicatedErrors() throws IOException |
| 219 | + { |
| 220 | + Configuration conf = new Configuration(); |
| 221 | + |
| 222 | + // allow 1 retry (2 total calls) |
| 223 | + conf.setInt("dfs.client.block.write.locateFollowingBlock.retries", 1); |
| 224 | + |
| 225 | + TestNameNode tnn = new TestNameNode(conf); |
| 226 | + DFSClient client = new DFSClient(tnn, tnn, conf, null); |
| 227 | + OutputStream os = client.create("testfile", true); |
| 228 | + os.write(20); // write one random byte |
| 229 | + |
| 230 | + try { |
| 231 | + os.close(); |
| 232 | + } catch (Exception e) { |
| 233 | + assertTrue("Retries are not being stopped correctly", |
| 234 | + e.getMessage().equals(tnn.ADD_BLOCK_EXCEPTION)); |
| 235 | + } |
| 236 | + } |
| 237 | + |
100 | 238 | } |
0 commit comments