Skip to content

Commit 51b2f9a

Browse files
add test & synchorized
Signed-off-by: OneSizeFitsQuorum <[email protected]>
1 parent cd69d06 commit 51b2f9a

File tree

2 files changed

+83
-22
lines changed

2 files changed

+83
-22
lines changed

iotdb-core/datanode/src/main/java/org/apache/iotdb/db/storageengine/dataregion/wal/allocation/AbstractNodeAllocationStrategy.java

Lines changed: 9 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -34,40 +34,36 @@
3434
import java.io.File;
3535
import java.io.IOException;
3636
import java.util.Arrays;
37-
import java.util.concurrent.atomic.AtomicReference;
3837

3938
public abstract class AbstractNodeAllocationStrategy implements NodeAllocationStrategy {
4039
private static final Logger logger =
4140
LoggerFactory.getLogger(AbstractNodeAllocationStrategy.class);
4241
private static final CommonConfig commonConfig = CommonDescriptor.getInstance().getConfig();
4342

4443
// manage wal folders
45-
protected AtomicReference<FolderManager> folderManager = new AtomicReference<>();
44+
protected FolderManager folderManager;
4645

4746
protected AbstractNodeAllocationStrategy() {
4847
try {
49-
folderManager.set(
48+
folderManager =
5049
new FolderManager(
51-
Arrays.asList(commonConfig.getWalDirs()), DirectoryStrategyType.SEQUENCE_STRATEGY));
50+
Arrays.asList(commonConfig.getWalDirs()), DirectoryStrategyType.SEQUENCE_STRATEGY);
5251
} catch (DiskSpaceInsufficientException e) {
5352
logger.error(
5453
"Fail to create wal node allocation strategy because all disks of wal folders are full.",
5554
e);
5655
}
5756
}
5857

59-
protected IWALNode createWALNode(String identifier) {
58+
protected synchronized IWALNode createWALNode(String identifier) {
6059
try {
61-
// already in lock, so no need to synchronized
62-
if (folderManager.get() == null) {
63-
folderManager.set(
60+
if (folderManager == null) {
61+
folderManager =
6462
new FolderManager(
65-
Arrays.asList(commonConfig.getWalDirs()), DirectoryStrategyType.SEQUENCE_STRATEGY));
63+
Arrays.asList(commonConfig.getWalDirs()), DirectoryStrategyType.SEQUENCE_STRATEGY);
6664
}
67-
return folderManager
68-
.get()
69-
.getNextWithRetry(
70-
folder -> new WALNode(identifier, folder + File.separator + identifier));
65+
return folderManager.getNextWithRetry(
66+
folder -> new WALNode(identifier, folder + File.separator + identifier));
7167
} catch (DiskSpaceInsufficientException e) {
7268
logger.error("Fail to create wal node because all disks of wal folders are full.", e);
7369
return WALFakeNode.getFailureInstance(e);
@@ -79,15 +75,6 @@ protected IWALNode createWALNode(String identifier) {
7975
}
8076
}
8177

82-
protected IWALNode createWALNode(String identifier, String folder) {
83-
try {
84-
return new WALNode(identifier, folder);
85-
} catch (IOException e) {
86-
logger.error("Meet exception when creating wal node", e);
87-
return WALFakeNode.getFailureInstance(e);
88-
}
89-
}
90-
9178
protected IWALNode createWALNode(
9279
String identifier, String folder, long startFileVersion, long startSearchIndex) {
9380
try {

iotdb-core/datanode/src/test/java/org/apache/iotdb/db/storageengine/dataregion/wal/allocation/FirstCreateStrategyTest.java

Lines changed: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@
3838
import org.junit.Test;
3939

4040
import java.io.File;
41+
import java.io.IOException;
4142

4243
import static org.junit.Assert.assertEquals;
4344
import static org.junit.Assert.assertNotEquals;
@@ -141,6 +142,79 @@ public void testRegisterWALNode() throws IllegalPathException {
141142
}
142143
}
143144

145+
@Test
146+
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();
150+
151+
String[] walDirs =
152+
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()
156+
};
157+
158+
String[] originalWalDirs = commonConfig.getWalDirs();
159+
commonConfig.setWalDirs(walDirs);
160+
161+
try {
162+
// Create strategy with valid directories first
163+
FirstCreateStrategy strategy = new FirstCreateStrategy();
164+
165+
// Simulate folderManager becoming null (e.g., due to disk space issues)
166+
// We'll use reflection to set folderManager to null to test re-initialization
167+
try {
168+
java.lang.reflect.Field folderManagerField =
169+
AbstractNodeAllocationStrategy.class.getDeclaredField("folderManager");
170+
folderManagerField.setAccessible(true);
171+
folderManagerField.set(strategy, null);
172+
} catch (NoSuchFieldException | IllegalAccessException e) {
173+
throw new RuntimeException("Failed to set folderManager to null for testing", e);
174+
}
175+
176+
// Now apply for WAL node, should successfully re-initialize folderManager
177+
IWALNode walNode = strategy.applyForWALNode("test_reinit_identifier");
178+
assertNotNull("WAL node should be created after re-initialization", walNode);
179+
180+
// Verify that WAL node was created successfully by logging data
181+
walNode.log(1, getInsertRowNode());
182+
183+
// Verify that WAL files were created in at least one directory
184+
boolean walFileCreated = false;
185+
for (String walDir : walDirs) {
186+
File walDirFile = new File(walDir);
187+
if (walDirFile.exists()) {
188+
File[] nodeDirs = walDirFile.listFiles(File::isDirectory);
189+
if (nodeDirs != null && nodeDirs.length > 0) {
190+
for (File nodeDir : nodeDirs) {
191+
if (nodeDir.exists() && WALFileUtils.listAllWALFiles(nodeDir).length > 0) {
192+
walFileCreated = true;
193+
break;
194+
}
195+
}
196+
}
197+
}
198+
if (walFileCreated) {
199+
break;
200+
}
201+
}
202+
assertTrue("WAL files should be created after re-initialization", walFileCreated);
203+
204+
// Clean up
205+
walNode.close();
206+
} finally {
207+
// Clean up the test directories
208+
for (String walDir : walDirs) {
209+
EnvironmentUtils.cleanDir(walDir);
210+
}
211+
// Clean up temp directory
212+
EnvironmentUtils.cleanDir(tempDir.getAbsolutePath());
213+
// Restore original WAL directories
214+
commonConfig.setWalDirs(originalWalDirs);
215+
}
216+
}
217+
144218
private InsertRowNode getInsertRowNode() throws IllegalPathException {
145219
long time = 110L;
146220
TSDataType[] dataTypes =

0 commit comments

Comments
 (0)