@@ -186,24 +186,31 @@ public void setupHA() throws IOException {
186186 }
187187
188188 private void startMinHdfs () throws Exception {
189- Path baseDir = temporaryFolder .newFolder ("baseDir" ).toPath ();
190189 int maxAttempts = 3 ;
191190 for (int attempt = 1 ; attempt <= maxAttempts ; attempt ++) {
191+ // Create a fresh baseDir for each attempt to avoid residual state
192+ Path baseDir = temporaryFolder .newFolder ("baseDir-" + attempt ).toPath ();
192193 try {
193194 Path hdfsHome = createHdfsDataFolder (baseDir );
194195 tryStartingHdfs (hdfsHome );
195196 break ;
196197 } catch (IOException e ) {
197198 // Log the exception
198- System .out .println ("Attempt " + attempt + " failed with error: " + e .getMessage ());
199+ System .out .println ("Attempt " + attempt + " to start HDFS failed: " + e .getMessage ());
200+ // Clean up the failed attempt
201+ try {
202+ FileUtils .deleteDirectory (baseDir .toFile ());
203+ } catch (IOException cleanupException ) {
204+ // Log but don't fail on cleanup errors
205+ System .out .println ("Failed to cleanup baseDir after attempt " + attempt + ": " + cleanupException .getMessage ());
206+ }
199207 // If the maximum number of attempts is reached, rethrow the exception
200- FileUtils .deleteDirectory (baseDir .toFile ());
201208 if (attempt == maxAttempts ) {
202209 throw e ;
203210 }
204211 // Add a small delay before retrying to allow filesystem to stabilize
205212 try {
206- Thread .sleep (1000 * attempt ); // Progressive backoff: 1s, 2s
213+ Thread .sleep (1000L * attempt * attempt ); // Progressive backoff: 1s, 4s
207214 } catch (InterruptedException ie ) {
208215 Thread .currentThread ().interrupt ();
209216 throw new IOException ("Interrupted while waiting to retry HDFS startup" , ie );
@@ -256,6 +263,9 @@ private void tryStartingHdfs(Path hdfsHome) throws ClassNotFoundException, NoSuc
256263
257264 MiniDFSCluster .Builder builder = new MiniDFSCluster .Builder (cfg );
258265 builder .nameNodePort (explicitPort );
266+ // Explicitly enable formatting and directory management for clean test environment
267+ builder .format (true );
268+ builder .manageNameDfsDirs (true );
259269 if (isHA ()) {
260270 MiniDFSNNTopology .NNConf nn1 = new MiniDFSNNTopology .NNConf ("nn1" ).setIpcPort (0 );
261271 MiniDFSNNTopology .NNConf nn2 = new MiniDFSNNTopology .NNConf ("nn2" ).setIpcPort (0 );
0 commit comments