|
25 | 25 | import org.apache.iotdb.db.queryengine.plan.planner.plan.node.PlanNodeId; |
26 | 26 | import org.apache.iotdb.db.queryengine.plan.planner.plan.node.write.InsertRowNode; |
27 | 27 | import org.apache.iotdb.db.storageengine.dataregion.wal.node.IWALNode; |
| 28 | +import org.apache.iotdb.db.storageengine.dataregion.wal.node.WALNode; |
28 | 29 | import org.apache.iotdb.db.storageengine.dataregion.wal.utils.WALFileUtils; |
29 | 30 | import org.apache.iotdb.db.utils.EnvironmentUtils; |
30 | 31 | import org.apache.iotdb.db.utils.constant.TestConstant; |
|
38 | 39 | import org.junit.Test; |
39 | 40 |
|
40 | 41 | import java.io.File; |
| 42 | +import java.io.IOException; |
| 43 | +import java.nio.file.Files; |
| 44 | +import java.nio.file.Path; |
41 | 45 |
|
42 | 46 | import static org.junit.Assert.assertEquals; |
43 | 47 | import static org.junit.Assert.assertNotEquals; |
@@ -141,6 +145,83 @@ public void testRegisterWALNode() throws IllegalPathException { |
141 | 145 | } |
142 | 146 | } |
143 | 147 |
|
| 148 | + @Test |
| 149 | + public void testReInitializeAfterDiskSpaceCleaned() throws IllegalPathException, IOException { |
| 150 | + // Create unique temporary directory for testing |
| 151 | + Path tempDir = Files.createTempDirectory("iotdb_wal_reinit_test_"); |
| 152 | + |
| 153 | + String[] testWalDirs = |
| 154 | + new String[] { |
| 155 | + tempDir.resolve("wal_reinit_test1").toString(), |
| 156 | + tempDir.resolve("wal_reinit_test2").toString(), |
| 157 | + tempDir.resolve("wal_reinit_test3").toString() |
| 158 | + }; |
| 159 | + |
| 160 | + String[] originalWalDirs = commonConfig.getWalDirs(); |
| 161 | + |
| 162 | + try { |
| 163 | + commonConfig.setWalDirs(testWalDirs); |
| 164 | + // Create strategy with valid directories first |
| 165 | + FirstCreateStrategy strategy = new FirstCreateStrategy(); |
| 166 | + |
| 167 | + // Simulate folderManager becoming null (e.g., due to disk space issues) |
| 168 | + // We'll use reflection to set folderManager to null to test re-initialization |
| 169 | + try { |
| 170 | + java.lang.reflect.Field folderManagerField = |
| 171 | + AbstractNodeAllocationStrategy.class.getDeclaredField("folderManager"); |
| 172 | + folderManagerField.setAccessible(true); |
| 173 | + folderManagerField.set(strategy, null); |
| 174 | + } catch (NoSuchFieldException | IllegalAccessException e) { |
| 175 | + throw new RuntimeException("Failed to set folderManager to null for testing", e); |
| 176 | + } |
| 177 | + |
| 178 | + // Now apply for WAL node, should successfully re-initialize folderManager |
| 179 | + IWALNode walNode = strategy.applyForWALNode("test_reinit_identifier"); |
| 180 | + assertNotNull("WAL node should be created after re-initialization", walNode); |
| 181 | + |
| 182 | + // Verify that re-initialization actually occurred - should return WALNode, not WALFakeNode |
| 183 | + assertTrue( |
| 184 | + "Returned node should be WALNode instance after successful re-initialization", |
| 185 | + walNode instanceof WALNode); |
| 186 | + |
| 187 | + // Verify that WAL node was created successfully by logging data |
| 188 | + walNode.log(1, getInsertRowNode()); |
| 189 | + |
| 190 | + // Verify that WAL files were created in at least one directory |
| 191 | + boolean walFileCreated = false; |
| 192 | + for (String walDir : testWalDirs) { |
| 193 | + File walDirFile = new File(walDir); |
| 194 | + if (walDirFile.exists()) { |
| 195 | + File[] nodeDirs = walDirFile.listFiles(File::isDirectory); |
| 196 | + if (nodeDirs != null && nodeDirs.length > 0) { |
| 197 | + for (File nodeDir : nodeDirs) { |
| 198 | + if (nodeDir.exists() && WALFileUtils.listAllWALFiles(nodeDir).length > 0) { |
| 199 | + walFileCreated = true; |
| 200 | + break; |
| 201 | + } |
| 202 | + } |
| 203 | + } |
| 204 | + } |
| 205 | + if (walFileCreated) { |
| 206 | + break; |
| 207 | + } |
| 208 | + } |
| 209 | + assertTrue("WAL files should be created after re-initialization", walFileCreated); |
| 210 | + |
| 211 | + // Clean up |
| 212 | + walNode.close(); |
| 213 | + } finally { |
| 214 | + // Clean up the test directories |
| 215 | + for (String walDir : testWalDirs) { |
| 216 | + EnvironmentUtils.cleanDir(walDir); |
| 217 | + } |
| 218 | + // Clean up temp directory |
| 219 | + EnvironmentUtils.cleanDir(tempDir.toString()); |
| 220 | + // Restore original WAL directories |
| 221 | + commonConfig.setWalDirs(originalWalDirs); |
| 222 | + } |
| 223 | + } |
| 224 | + |
144 | 225 | private InsertRowNode getInsertRowNode() throws IllegalPathException { |
145 | 226 | long time = 110L; |
146 | 227 | TSDataType[] dataTypes = |
|
0 commit comments