2016-07-25 26 views
0

1)有誰能幫我介紹一下「Remoteexception」的概念嗎?一般意味着什麼?「RemoteException」在HDFS中的含義是什麼?

2)又是什麼意思unwrapRemoteException?不知道它的意思是「如果這個遠程異常包裝了lookupTypes之一」

/** 
    * If this remote exception wraps up one of the lookupTypes 
    * then return this exception. 
    * <p> 
    * Unwraps any IOException. 
    * 
    * @param lookupTypes the desired exception class. 
    * @return IOException, which is either the lookupClass exception or this. 
    */ 
    public IOException unwrapRemoteException(Class<?>... lookupTypes) { 
    if(lookupTypes == null) 
     return this; 
    for(Class<?> lookupClass : lookupTypes) { 
     if(!lookupClass.getName().equals(getClassName())) 
     continue; 
     try { 
     return instantiateException(lookupClass.asSubclass(IOException.class)); 
     } catch(Exception e) { 
     // cannot instantiate lookupClass, just return this 
     return this; 
     } 
    } 
    // wrapped up exception is not in lookupTypes, just return this 
    return this; 
    } 

(Hadoop_HDFS_Open_Source:https://github.com/apache/hadoop

提前感謝!任何幫助都感激不盡!

回答

0

1)有誰能幫我一下「Remoteexception」的概念嗎?一般意味着什麼?

遠程異常在服務器端被引發(創建)。服務器拋出這樣的異常,因爲客戶端發送無效請求,或服務器有內部錯誤或其他。服務器端的RPC服務器序列化該異常,並將其發送到客戶端。客戶端反序列化異常,並獲取異常。

2)又是什麼意思unwrapRemoteException?

問題是「爲什麼我們需要包裝異常」。首先,RemoteException是一個包裝,不是真正的例外。服務器拋出的真正異常可能是AccessControlException(用戶沒有預備),FileNotFoundException(無效請求)。我們把它們包裝在RemoteException中。但爲什麼?因爲代碼更乾淨,更具可讀性。

不知道它的意思是 「如果這個遠程異常包裝了lookupTypes之一」

例如,在DFSClient.java

public HdfsFileStatus getFileInfo(String src) throws IOException { 
    checkOpen(); 
    try { 
    return namenode.getFileInfo(src); 
    } catch(RemoteException re) { 
    throw re.unwrapRemoteException(AccessControlException.class, 
            FileNotFoundException.class, 
            UnresolvedPathException.class); 
    } 
} 

如果re包裝了FileNotFoundException ,那麼getFileInfo只返回FileNotFoundException。然後用戶可以看到更簡潔的異常消息。用戶只需要知道文件沒有找到,不關心它是否是遠程的。

但是,如果re包裝SafeModeException或一些未知的例外。這可能是一些服務器的內部錯誤或配置錯誤。我們完全拋出re(RemoteException)。因此,用戶可以知道錯誤來自遠程(服務器端),即使用戶不知道包裝的SafeModeException(或某個未知的例外)。用戶可以向技術支持尋求幫助。