Skip to content

Commit

Permalink
Clean walnode after drop database when using simple consensus (apache…
Browse files Browse the repository at this point in the history
…#13877)

* Clean walnode after drop database when using simple consensus

* add UT

* add UT
  • Loading branch information
HTHou authored Oct 23, 2024
1 parent 9e48f9c commit 38f7219
Show file tree
Hide file tree
Showing 4 changed files with 42 additions and 16 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -781,17 +781,10 @@ public void deleteDataRegion(DataRegionId regionId) {
}
break;
case ConsensusFactory.SIMPLE_CONSENSUS:
// remove UniqueId Information
// delete region information in wal and may delete wal
WALManager.getInstance()
.removeUniqueIdInfo(
WALManager.getApplicantUniqueId(
region.getDatabaseName() + FILE_NAME_SEPARATOR + region.getDataRegionId(),
true));
WALManager.getInstance()
.removeUniqueIdInfo(
WALManager.getApplicantUniqueId(
region.getDatabaseName() + FILE_NAME_SEPARATOR + region.getDataRegionId(),
false));
.deleteRegionAndMayDeleteWALNode(
region.getDatabaseName(), region.getDataRegionId());
break;
case ConsensusFactory.RATIS_CONSENSUS:
default:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,8 @@
import java.util.concurrent.TimeUnit;
import java.util.concurrent.atomic.AtomicLong;

import static org.apache.iotdb.commons.conf.IoTDBConstant.FILE_NAME_SEPARATOR;

/** This class is used to manage and allocate wal nodes. */
public class WALManager implements IService {
private static final Logger logger = LoggerFactory.getLogger(WALManager.class);
Expand Down Expand Up @@ -129,13 +131,17 @@ public void deleteWALNode(String applicantUniqueId) {
WritingMetrics.getInstance().removeWALNodeInfoMetrics(applicantUniqueId);
}

/** UniqueId will be removed only when using ElasticStrategy. */
public void removeUniqueIdInfo(String applicantUniqueId) {
/** Region information and WAL node will be removed when using ElasticStrategy. */
public void deleteRegionAndMayDeleteWALNode(String databaseName, String dataRegionId) {
if (config.getWalMode() == WALMode.DISABLE || !(walNodesManager instanceof ElasticStrategy)) {
return;
}

((ElasticStrategy) walNodesManager).removeUniqueIdInfo(applicantUniqueId);
String dataRegionName = databaseName + FILE_NAME_SEPARATOR + dataRegionId;
((ElasticStrategy) walNodesManager)
.deleteUniqueIdAndMayDeleteWALNode(getApplicantUniqueId(dataRegionName, true));
((ElasticStrategy) walNodesManager)
.deleteUniqueIdAndMayDeleteWALNode(getApplicantUniqueId(dataRegionName, false));
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@

package org.apache.iotdb.db.storageengine.dataregion.wal.allocation;

import org.apache.iotdb.commons.utils.FileUtils;
import org.apache.iotdb.db.storageengine.dataregion.wal.WALManager;
import org.apache.iotdb.db.storageengine.dataregion.wal.node.IWALNode;
import org.apache.iotdb.db.storageengine.dataregion.wal.node.WALNode;

Expand Down Expand Up @@ -69,7 +71,7 @@ public IWALNode applyForWALNode(String applicantUniqueId) {
}
walNodes.add((WALNode) node);
}
uniqueId2Nodes.put(applicantUniqueId, walNodes.get(nodeIdCounter));
uniqueId2Nodes.put(applicantUniqueId, walNodes.get(walNodes.size() - 1));
}

return uniqueId2Nodes.get(applicantUniqueId);
Expand All @@ -78,10 +80,24 @@ public IWALNode applyForWALNode(String applicantUniqueId) {
}
}

public void removeUniqueIdInfo(String applicantUniqueId) {
public void deleteUniqueIdAndMayDeleteWALNode(String applicantUniqueId) {
nodesLock.lock();
try {
uniqueId2Nodes.remove(applicantUniqueId);
WALNode walNode = uniqueId2Nodes.remove(applicantUniqueId);
if (!uniqueId2Nodes.containsValue(walNode)) {
if (walNode != null) {
walNode.close();
if (walNode.getLogDirectory().exists()) {
FileUtils.deleteFileOrDirectory(walNode.getLogDirectory());
}
WALManager.getInstance().subtractTotalDiskUsage(walNode.getDiskUsage());
WALManager.getInstance().subtractTotalFileNum(walNode.getFileNum());
}
walNodes.remove(walNode);
if (walNodes.isEmpty()) {
nodeIdCounter = -1;
}
}
} finally {
nodesLock.unlock();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,17 @@ public void testAllocateWALNode() throws IllegalPathException {
assertNotEquals(0, WALFileUtils.listAllWALFiles(nodeDir).length);
}
}
int walNodeNum = 3;
for (int i = 0; i < 12; i++) {
elasticStrategy.deleteUniqueIdAndMayDeleteWALNode(String.valueOf(i));
if ((i + 1) % ElasticStrategy.APPLICATION_NODE_RATIO == 0) {
assertEquals(--walNodeNum, elasticStrategy.getNodesNum());
walNodes[i / ElasticStrategy.APPLICATION_NODE_RATIO] = null;
} else {
assertEquals(walNodeNum, elasticStrategy.getNodesNum());
}
}
assertEquals(0, elasticStrategy.getNodesNum());
} finally {
for (IWALNode walNode : walNodes) {
if (walNode != null) {
Expand Down

0 comments on commit 38f7219

Please sign in to comment.