
通过客户端client向namenode写入元数据时发现了一段很有意思的重试机制特记录以待日后学习查阅:
代码如下:
boolean shouldRetry = true;
int retryCount = CREATE_RETRY_COUNT; --10
//TODO 重试的代码结构
while (shouldRetry) {
shouldRetry = false;
try {
stat = dfsClient.namenode.create(src, masked, dfsClient.clientName,
new EnumSetWritable(flag), createParent, replication, blockSize,SUPPORTED_CRYPTO_VERSIONS);
break;
} catch (RemoteException re) {
IOException e = re.unwrapRemoteException(AccessControlException.class,
DSQuotaExceededException.class, FileAlreadyExistsException.class,
FileNotFoundException.class, ParentNotDirectoryException.class,
NSQuotaExceededException.class, RetryStartFileException.class, SafeModeException.class,
UnresolvedPathException.class, SnapshotAccessControlException.class,
UnknownCryptoProtocolVersionException.class);
if (e instanceof RetryStartFileException) {
//TODO 重试
if (retryCount > 0) {
shouldRetry = true;
retryCount--;
} else {
throw new IOException("Too many retries because of encryption" + " zone operations", e);
}
} else {
throw e;
}
}
}
可以看到代码初次运行时会初始化10次重试机会,一旦代码英文IOException(public class RetryStartFileException extends IOException)发生异常,将会被捕获,进行重试,知道运行成功或10次一会用完,这是会抛出异常。同样如果代码因为其他原因导致运行失败也会被捕获,抛出异常。
异常类图(补充):
欢迎分享,转载请注明来源:内存溢出
微信扫一扫
支付宝扫一扫
评论列表(0条)