2525import org .apache .iotdb .db .queryengine .plan .planner .plan .node .PlanNodeId ;
2626import org .apache .iotdb .db .queryengine .plan .planner .plan .node .write .InsertRowNode ;
2727import org .apache .iotdb .db .storageengine .dataregion .wal .node .IWALNode ;
28+ import org .apache .iotdb .db .storageengine .dataregion .wal .node .WALFakeNode ;
29+ import org .apache .iotdb .db .storageengine .dataregion .wal .node .WALNode ;
2830import org .apache .iotdb .db .storageengine .dataregion .wal .utils .WALFileUtils ;
2931import org .apache .iotdb .db .utils .EnvironmentUtils ;
3032import org .apache .iotdb .db .utils .constant .TestConstant ;
3941
4042import java .io .File ;
4143import java .io .IOException ;
44+ import java .nio .file .Files ;
45+ import java .nio .file .Path ;
4246
4347import static org .junit .Assert .assertEquals ;
4448import static org .junit .Assert .assertNotEquals ;
4549import static org .junit .Assert .assertNotNull ;
50+ import static org .junit .Assert .assertNull ;
4651import static org .junit .Assert .assertTrue ;
4752
4853public class FirstCreateStrategyTest {
@@ -144,15 +149,14 @@ public void testRegisterWALNode() throws IllegalPathException {
144149
145150 @ Test
146151 public void testReInitializeAfterDiskSpaceCleaned () throws IllegalPathException , IOException {
147- // Create temporary directories for testing
148- File tempDir = new File (System .getProperty ("java.io.tmpdir" ), "iotdb_wal_reinit_test" );
149- tempDir .mkdirs ();
152+ // Create unique temporary directory for testing
153+ Path tempDir = Files .createTempDirectory ("iotdb_wal_reinit_test_" );
150154
151155 String [] testWalDirs =
152156 new String [] {
153- new File ( tempDir , "wal_reinit_test1" ).getAbsolutePath (),
154- new File ( tempDir , "wal_reinit_test2" ).getAbsolutePath (),
155- new File ( tempDir , "wal_reinit_test3" ).getAbsolutePath ()
157+ tempDir . resolve ( "wal_reinit_test1" ).toString (),
158+ tempDir . resolve ( "wal_reinit_test2" ).toString (),
159+ tempDir . resolve ( "wal_reinit_test3" ).toString ()
156160 };
157161
158162 String [] originalWalDirs = commonConfig .getWalDirs ();
@@ -177,6 +181,11 @@ public void testReInitializeAfterDiskSpaceCleaned() throws IllegalPathException,
177181 IWALNode walNode = strategy .applyForWALNode ("test_reinit_identifier" );
178182 assertNotNull ("WAL node should be created after re-initialization" , walNode );
179183
184+ // Verify that re-initialization actually occurred - should return WALNode, not WALFakeNode
185+ assertTrue (
186+ "Returned node should be WALNode instance after successful re-initialization" ,
187+ walNode instanceof WALNode );
188+
180189 // Verify that WAL node was created successfully by logging data
181190 walNode .log (1 , getInsertRowNode ());
182191
@@ -209,7 +218,61 @@ public void testReInitializeAfterDiskSpaceCleaned() throws IllegalPathException,
209218 EnvironmentUtils .cleanDir (walDir );
210219 }
211220 // Clean up temp directory
212- EnvironmentUtils .cleanDir (tempDir .getAbsolutePath ());
221+ EnvironmentUtils .cleanDir (tempDir .toString ());
222+ // Restore original WAL directories
223+ commonConfig .setWalDirs (originalWalDirs );
224+ }
225+ }
226+
227+ @ Test
228+ public void testReInitializeFailsWhenDiskStillFull () throws IllegalPathException , IOException {
229+ // Create unique temporary directory for testing
230+ Path tempDir = Files .createTempDirectory ("iotdb_wal_reinit_fail_test_" );
231+
232+ // Use non-existent directories that will cause re-initialization to fail
233+ String [] nonExistentWalDirs =
234+ new String [] {
235+ tempDir .resolve ("non_existent_dir1" ).toString (),
236+ tempDir .resolve ("non_existent_dir2" ).toString (),
237+ tempDir .resolve ("non_existent_dir3" ).toString ()
238+ };
239+
240+ String [] originalWalDirs = commonConfig .getWalDirs ();
241+ commonConfig .setWalDirs (nonExistentWalDirs );
242+
243+ try {
244+ // Create strategy - this will fail to initialize folderManager
245+ FirstCreateStrategy strategy = new FirstCreateStrategy ();
246+
247+ // Verify that folderManager is null due to initialization failure
248+ try {
249+ java .lang .reflect .Field folderManagerField =
250+ AbstractNodeAllocationStrategy .class .getDeclaredField ("folderManager" );
251+ folderManagerField .setAccessible (true );
252+ Object folderManager = folderManagerField .get (strategy );
253+ assertNull ("folderManager should be null due to initialization failure" , folderManager );
254+ } catch (NoSuchFieldException | IllegalAccessException e ) {
255+ throw new RuntimeException ("Failed to access folderManager field" , e );
256+ }
257+
258+ // Now apply for WAL node - re-initialization should also fail and return WALFakeNode
259+ IWALNode walNode = strategy .applyForWALNode ("test_reinit_fail_identifier" );
260+
261+ // Verify that WALFakeNode is returned when re-initialization fails
262+ assertNotNull ("WAL node should be returned even when re-initialization fails" , walNode );
263+ assertTrue (
264+ "Should return WALFakeNode when re-initialization fails" , walNode instanceof WALFakeNode );
265+
266+ // WALFakeNode should be the failure instance
267+ WALFakeNode fakeNode = (WALFakeNode ) walNode ;
268+ assertTrue (
269+ "WALFakeNode should contain failure information" ,
270+ fakeNode .toString ().contains ("DiskSpaceInsufficientException" )
271+ || fakeNode .toString ().contains ("Failed to create WAL node" ));
272+
273+ } finally {
274+ // Clean up temp directory
275+ EnvironmentUtils .cleanDir (tempDir .toString ());
213276 // Restore original WAL directories
214277 commonConfig .setWalDirs (originalWalDirs );
215278 }
0 commit comments