@@ -141,9 +141,9 @@ private FanOutOneBlockAsyncDFSOutputHelper() {
141141
142142 private interface LeaseManager {
143143
144- void begin (DFSClient client , HdfsFileStatus stat );
144+ void begin (FanOutOneBlockAsyncDFSOutput output );
145145
146- void end (DFSClient client , HdfsFileStatus stat );
146+ void end (FanOutOneBlockAsyncDFSOutput output );
147147 }
148148
149149 private static final LeaseManager LEASE_MANAGER ;
@@ -178,44 +178,28 @@ private static LeaseManager createLeaseManager3_4() throws NoSuchMethodException
178178 beginFileLeaseMethod .setAccessible (true );
179179 Method endFileLeaseMethod = DFSClient .class .getDeclaredMethod ("endFileLease" , String .class );
180180 endFileLeaseMethod .setAccessible (true );
181- Method getConfigurationMethod = DFSClient .class .getDeclaredMethod ("getConfiguration" );
182- getConfigurationMethod .setAccessible (true );
183- Method getNamespaceMehtod = HdfsFileStatus .class .getDeclaredMethod ("getNamespace" );
184-
181+ Method getUniqKeyMethod = DFSOutputStream .class .getMethod ("getUniqKey" );
185182 return new LeaseManager () {
186183
187- private static final String DFS_OUTPUT_STREAM_UNIQ_DEFAULT_KEY =
188- "dfs.client.output.stream.uniq.default.key" ;
189- private static final String DFS_OUTPUT_STREAM_UNIQ_DEFAULT_KEY_DEFAULT = "DEFAULT" ;
190-
191- private String getUniqId (DFSClient client , HdfsFileStatus stat )
192- throws IllegalAccessException , IllegalArgumentException , InvocationTargetException {
193- // Copied from DFSClient in Hadoop 3.4.0
194- long fileId = stat .getFileId ();
195- String namespace = (String ) getNamespaceMehtod .invoke (stat );
196- if (namespace == null ) {
197- Configuration conf = (Configuration ) getConfigurationMethod .invoke (client );
198- String defaultKey = conf .get (DFS_OUTPUT_STREAM_UNIQ_DEFAULT_KEY ,
199- DFS_OUTPUT_STREAM_UNIQ_DEFAULT_KEY_DEFAULT );
200- return defaultKey + "_" + fileId ;
201- } else {
202- return namespace + "_" + fileId ;
203- }
184+ private String getUniqKey (FanOutOneBlockAsyncDFSOutput output )
185+ throws IllegalAccessException , InvocationTargetException {
186+ return (String ) getUniqKeyMethod .invoke (output .getDummyStream ());
204187 }
205188
206189 @ Override
207- public void begin (DFSClient client , HdfsFileStatus stat ) {
190+ public void begin (FanOutOneBlockAsyncDFSOutput output ) {
208191 try {
209- beginFileLeaseMethod .invoke (client , getUniqId (client , stat ), null );
192+ beginFileLeaseMethod .invoke (output .getClient (), getUniqKey (output ),
193+ output .getDummyStream ());
210194 } catch (IllegalAccessException | InvocationTargetException e ) {
211195 throw new RuntimeException (e );
212196 }
213197 }
214198
215199 @ Override
216- public void end (DFSClient client , HdfsFileStatus stat ) {
200+ public void end (FanOutOneBlockAsyncDFSOutput output ) {
217201 try {
218- endFileLeaseMethod .invoke (client , getUniqId ( client , stat ));
202+ endFileLeaseMethod .invoke (output . getClient (), getUniqKey ( output ));
219203 } catch (IllegalAccessException | InvocationTargetException e ) {
220204 throw new RuntimeException (e );
221205 }
@@ -232,18 +216,19 @@ private static LeaseManager createLeaseManager3() throws NoSuchMethodException {
232216 return new LeaseManager () {
233217
234218 @ Override
235- public void begin (DFSClient client , HdfsFileStatus stat ) {
219+ public void begin (FanOutOneBlockAsyncDFSOutput output ) {
236220 try {
237- beginFileLeaseMethod .invoke (client , stat .getFileId (), null );
221+ beginFileLeaseMethod .invoke (output .getClient (), output .getDummyStream ().getFileId (),
222+ output .getDummyStream ());
238223 } catch (IllegalAccessException | InvocationTargetException e ) {
239224 throw new RuntimeException (e );
240225 }
241226 }
242227
243228 @ Override
244- public void end (DFSClient client , HdfsFileStatus stat ) {
229+ public void end (FanOutOneBlockAsyncDFSOutput output ) {
245230 try {
246- endFileLeaseMethod .invoke (client , stat .getFileId ());
231+ endFileLeaseMethod .invoke (output . getClient (), output . getDummyStream () .getFileId ());
247232 } catch (IllegalAccessException | InvocationTargetException e ) {
248233 throw new RuntimeException (e );
249234 }
@@ -323,12 +308,12 @@ public boolean progress() {
323308 }
324309 }
325310
326- static void beginFileLease (DFSClient client , HdfsFileStatus stat ) {
327- LEASE_MANAGER .begin (client , stat );
311+ private static void beginFileLease (FanOutOneBlockAsyncDFSOutput output ) {
312+ LEASE_MANAGER .begin (output );
328313 }
329314
330- static void endFileLease (DFSClient client , HdfsFileStatus stat ) {
331- LEASE_MANAGER .end (client , stat );
315+ static void endFileLease (FanOutOneBlockAsyncDFSOutput output ) {
316+ LEASE_MANAGER .end (output );
332317 }
333318
334319 static DataChecksum createChecksum (DFSClient client ) {
@@ -540,20 +525,19 @@ private static FanOutOneBlockAsyncDFSOutput createOutput(DistributedFileSystem d
540525 LOG .debug ("When create output stream for {}, exclude list is {}, retry={}" , src ,
541526 getDataNodeInfo (toExcludeNodes ), retry );
542527 }
528+ EnumSetWritable <CreateFlag > createFlags = getCreateFlags (overwrite , noLocalWrite );
543529 HdfsFileStatus stat ;
544530 try {
545531 stat = FILE_CREATOR .create (namenode , src ,
546532 FsPermission .getFileDefault ().applyUMask (FsPermission .getUMask (conf )), clientName ,
547- getCreateFlags (overwrite , noLocalWrite ), createParent , replication , blockSize ,
548- CryptoProtocolVersion .supported ());
533+ createFlags , createParent , replication , blockSize , CryptoProtocolVersion .supported ());
549534 } catch (Exception e ) {
550535 if (e instanceof RemoteException ) {
551536 throw (RemoteException ) e ;
552537 } else {
553538 throw new NameNodeException (e );
554539 }
555540 }
556- beginFileLease (client , stat );
557541 boolean succ = false ;
558542 LocatedBlock locatedBlock = null ;
559543 List <Future <Channel >> futureList = null ;
@@ -578,7 +562,8 @@ private static FanOutOneBlockAsyncDFSOutput createOutput(DistributedFileSystem d
578562 Encryptor encryptor = createEncryptor (conf , stat , client );
579563 FanOutOneBlockAsyncDFSOutput output =
580564 new FanOutOneBlockAsyncDFSOutput (conf , dfs , client , namenode , clientName , src , stat ,
581- locatedBlock , encryptor , datanodes , summer , ALLOC , monitor );
565+ createFlags .get (), locatedBlock , encryptor , datanodes , summer , ALLOC , monitor );
566+ beginFileLease (output );
582567 succ = true ;
583568 return output ;
584569 } catch (RemoteException e ) {
@@ -617,7 +602,6 @@ public void operationComplete(Future<Channel> future) throws Exception {
617602 });
618603 }
619604 }
620- endFileLease (client , stat );
621605 }
622606 }
623607 }
@@ -654,12 +638,13 @@ public static boolean shouldRetryCreate(RemoteException e) {
654638 return e .getClassName ().endsWith ("RetryStartFileException" );
655639 }
656640
657- static void completeFile (DFSClient client , ClientProtocol namenode , String src , String clientName ,
658- ExtendedBlock block , HdfsFileStatus stat ) {
641+ static void completeFile (FanOutOneBlockAsyncDFSOutput output , DFSClient client ,
642+ ClientProtocol namenode , String src , String clientName , ExtendedBlock block ,
643+ HdfsFileStatus stat ) {
659644 for (int retry = 0 ;; retry ++) {
660645 try {
661646 if (namenode .complete (src , clientName , block , stat .getFileId ())) {
662- endFileLease (client , stat );
647+ endFileLease (output );
663648 return ;
664649 } else {
665650 LOG .warn ("complete file " + src + " not finished, retry = " + retry );
0 commit comments