Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -912,8 +912,12 @@ private TSStatus loadTsFileAsync(
conversionTaskId,
authenticatedUser);

if (!LoadUtil.loadFilesToActiveDir(loadAttributes, absolutePaths, true)) {
throw new PipeException(DataNodePipeMessages.LOAD_ACTIVE_LISTENING_PIPE_DIR_IS_NOT);
try {
if (!LoadUtil.loadFilesToActiveDir(loadAttributes, absolutePaths, true)) {
throw new PipeException(DataNodePipeMessages.LOAD_ACTIVE_LISTENING_PIPE_DIR_IS_NOT);
}
} catch (final DiskSpaceInsufficientException e) {

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

minor iotdb-core/datanode/src/main/java/org/apache/iotdb/db/pipe/receiver/protocol/thrift/IoTDBDataNodeReceiver.java:919 — New DISK_SPACE_INSUFFICIENT mapping in loadTsFileAsync is untested

The added catch that converts DiskSpaceInsufficientException into RpcUtils.getStatus(e.getErrorCode(), e.getMessage()) has no test: IoTDBDataNodeReceiverTest contains no DISK_SPACE_INSUFFICIENT/DiskSpaceInsufficient assertion and no test stubs LoadUtil.loadFilesToActiveDir to throw. A regression here (wrong status code, unmarked retryable task, or lost staging file) would pass CI, since the retry/keep-files behaviour depends on the returned code being in StatusUtils.NEED_RETRY.

Fix: Add a test that stubs LoadUtil.loadFilesToActiveDir to throw DiskSpaceInsufficientException and asserts the seal response code is TSStatusCode.DISK_SPACE_INSUFFICIENT and that the retryable task keeps its staged files.

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Implemented in commit c8d73be5800. Added a receiver regression test that drives a V2 seal request while stubbing LoadUtil.loadFilesToActiveDir to throw DiskSpaceInsufficientException. It asserts the response code, retry classification, paused task state, and retained staged file. Both targeted test classes pass (14 tests total).

return RpcUtils.getStatus(e.getErrorCode(), e.getMessage());
}
PipeTsFileConversionTaskManager.markReceiverOwned(conversionTaskId);
return new TSStatus(TSStatusCode.SUCCESS_STATUS.getStatusCode());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,15 @@ public interface ILoadDiskSelector {
interface DiskDirectorySelector {
File selectDirectory(final File sourceDirectory, final String fileName, final int tierLevel)
throws DiskSpaceInsufficientException, LoadFileException;

default File selectDirectory(
final File sourceDirectory,
final String fileName,
final int tierLevel,
final double minimumFreeRatio)
throws DiskSpaceInsufficientException, LoadFileException {
return selectDirectory(sourceDirectory, fileName, tierLevel);
}
}

File selectTargetDirectory(
Expand All @@ -39,6 +48,16 @@ File selectTargetDirectory(
final int tierLevel)
throws DiskSpaceInsufficientException, LoadFileException;

default File selectTargetDirectory(
final File sourceDirectory,
final String fileName,
final boolean appendFileName,
final int tierLevel,
final double minimumFreeRatio)
throws DiskSpaceInsufficientException, LoadFileException {
return selectTargetDirectory(sourceDirectory, fileName, appendFileName, tierLevel);
}

static ILoadDiskSelector initDiskSelector(
final String selectStrategy, final String[] dirs, final DiskDirectorySelector selector) {
final ILoadDiskSelector diskSelector;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,18 @@ public File selectTargetDirectory(
final boolean appendFileName,
final int tierLevel)
throws DiskSpaceInsufficientException, LoadFileException {
return directorySelector.selectDirectory(sourceDirectory, FileName, tierLevel);
return selectTargetDirectory(sourceDirectory, FileName, appendFileName, tierLevel, 0.0);
}

@Override
public File selectTargetDirectory(
final File sourceDirectory,
final String FileName,
final boolean appendFileName,
final int tierLevel,
final double minimumFreeRatio)
throws DiskSpaceInsufficientException, LoadFileException {
return directorySelector.selectDirectory(
sourceDirectory, FileName, tierLevel, minimumFreeRatio);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
package org.apache.iotdb.db.storageengine.load.disk;

import org.apache.iotdb.commons.exception.DiskSpaceInsufficientException;
import org.apache.iotdb.commons.utils.JVMCommonUtils;
import org.apache.iotdb.db.exception.load.LoadFileException;
import org.apache.iotdb.db.i18n.StorageEngineMessages;
import org.apache.iotdb.metrics.utils.FileStoreUtils;
Expand Down Expand Up @@ -85,6 +86,17 @@ public File selectTargetDirectory(
final boolean appendFileName,
final int tierLevel)
throws DiskSpaceInsufficientException, LoadFileException {
return selectTargetDirectory(sourceDirectory, fileName, appendFileName, tierLevel, 0.0);
}

@Override
public File selectTargetDirectory(
final File sourceDirectory,
final String fileName,
final boolean appendFileName,
final int tierLevel,
final double minimumFreeRatio)
throws DiskSpaceInsufficientException, LoadFileException {
String fileDirRoot = null;
try {
if (sourceDirectory != null) {
Expand All @@ -101,20 +113,17 @@ public File selectTargetDirectory(
e);
}

File targetFile = null;
if (rootDisks2DataDirsMapForLoad.containsKey(fileDirRoot)) {
if (appendFileName) {
// if there is an overlap between firDirRoot and data directories' disk roots, try to get
// targetFile in the same disk
targetFile = fsFactory.getFile(rootDisks2DataDirsMapForLoad.get(fileDirRoot), fileName);
} else {
targetFile = new File(rootDisks2DataDirsMapForLoad.get(fileDirRoot));
final String targetDirectory = rootDisks2DataDirsMapForLoad.get(fileDirRoot);
if (minimumFreeRatio <= 0
|| JVMCommonUtils.getDiskFreeRatio(targetDirectory) > minimumFreeRatio) {
return appendFileName
? fsFactory.getFile(targetDirectory, fileName)
: new File(targetDirectory);
}

return targetFile;
}

// if there isn't an overlap, downgrade to storage balance(sequence) strategy.
return super.selectTargetDirectory(sourceDirectory, fileName, appendFileName, tierLevel);
return super.selectTargetDirectory(
sourceDirectory, fileName, appendFileName, tierLevel, minimumFreeRatio);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -51,18 +51,22 @@
import java.nio.file.StandardCopyOption;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.Comparator;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
import java.util.Objects;
import java.util.UUID;
import java.util.concurrent.atomic.AtomicInteger;
import java.util.stream.Collectors;

public class LoadUtil {

private static final Logger LOGGER = LoggerFactory.getLogger(LoadUtil.class);

private static final double PIPE_ASYNC_LOAD_DISK_SPACE_SAFETY_MARGIN = 0.05;

private static volatile ILoadDiskSelector loadDiskSelector = updateLoadDiskSelector();

public static boolean loadTsFileAsyncToActiveDir(
Expand Down Expand Up @@ -213,16 +217,22 @@ public static boolean loadFilesToActiveDir(
final Map<String, String> loadAttributes,
final List<String> files,
final boolean isDeleteAfterLoad)
throws IOException {
throws IOException, DiskSpaceInsufficientException {
if (files == null || files.isEmpty()) {
return true;
}

final File targetFilePath;
final double minimumFreeRatio =
CommonDescriptor.getInstance().getConfig().getDiskSpaceWarningThreshold()
+ PIPE_ASYNC_LOAD_DISK_SPACE_SAFETY_MARGIN;
try {
final File file = new File(files.get(0));
targetFilePath =
loadDiskSelector.selectTargetDirectory(file.getParentFile(), file.getName(), false, 0);
loadDiskSelector.selectTargetDirectory(
file.getParentFile(), file.getName(), false, 0, minimumFreeRatio);
} catch (DiskSpaceInsufficientException e) {
throw e;
} catch (Exception e) {
LOGGER.warn(StorageEngineMessages.FAIL_TO_LOAD_DISK_SPACE, files.get(0), e);
return false;
Expand Down Expand Up @@ -250,6 +260,10 @@ public static boolean loadFilesToActiveDir(
sourceFile.getAbsolutePath()));
}
}
if (JVMCommonUtils.getDiskFreeRatio(targetFilePath.getAbsolutePath()) <= minimumFreeRatio) {
throw new DiskSpaceInsufficientException(
Collections.singletonList(targetFilePath.getAbsolutePath()));
}
sourceFiles.sort(Comparator.comparing(LoadUtil::isTsFile));
transferFilesToActiveDir(
targetDir,
Expand Down Expand Up @@ -468,16 +482,43 @@ public static ILoadDiskSelector updateLoadDiskSelector() {

final FolderManager finalFolderManager = folderManager;
final DiskSpaceInsufficientException finalException = exception;
final AtomicInteger nextLoadDirectoryIndex = new AtomicInteger();
final ILoadDiskSelector.DiskDirectorySelector selector =
new ILoadDiskSelector.DiskDirectorySelector() {
@Override
public File selectDirectory(
final File sourceDirectory, final String fileName, final int tierLevel)
throws DiskSpaceInsufficientException {
return selectDirectory(sourceDirectory, fileName, tierLevel, 0.0);
}

@Override
public File selectDirectory(
final File sourceDirectory,
final String fileName,
final int tierLevel,
final double minimumFreeRatio)
throws DiskSpaceInsufficientException {
if (finalException != null) {
throw finalException;
}
if (minimumFreeRatio <= 0) {
return new File(finalFolderManager.getNextFolder());
}
final int firstDirectoryIndex =
nextLoadDirectoryIndex.getAndUpdate(index -> (index + 1) % dirs.length);
for (int i = 0; i < dirs.length; i++) {
final String folder = dirs[(firstDirectoryIndex + i) % dirs.length];
if (JVMCommonUtils.getDiskFreeRatio(folder) > minimumFreeRatio) {
return new File(folder);
}
}
throw new DiskSpaceInsufficientException(Arrays.asList(dirs));
}
};
ILoadDiskSelector loadDiskSelector =
ILoadDiskSelector.initDiskSelector(
IoTDBDescriptor.getInstance().getConfig().getLoadDiskSelectStrategy(),
dirs,
(sourceDir, fileName, tierLevel) -> {
if (finalException != null) {
throw finalException;
}
return new File(finalFolderManager.getNextFolder());
});
IoTDBDescriptor.getInstance().getConfig().getLoadDiskSelectStrategy(), dirs, selector);

LoadUtil.loadDiskSelector = loadDiskSelector;
return loadDiskSelector;
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,121 @@
/*
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance
* with the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
*/

package org.apache.iotdb.db.pipe.receiver.protocol.thrift;

import org.apache.iotdb.commons.exception.DiskSpaceInsufficientException;
import org.apache.iotdb.commons.utils.StatusUtils;
import org.apache.iotdb.db.conf.IoTDBConfig;
import org.apache.iotdb.db.conf.IoTDBDescriptor;
import org.apache.iotdb.db.pipe.sink.payload.evolvable.request.PipeTransferTsFilePieceWithModReq;
import org.apache.iotdb.db.pipe.sink.payload.evolvable.request.PipeTransferTsFileSealWithModReq;
import org.apache.iotdb.db.storageengine.load.converter.PipeTsFileConversionTaskManager;
import org.apache.iotdb.db.storageengine.load.util.LoadUtil;
import org.apache.iotdb.rpc.TSStatusCode;
import org.apache.iotdb.service.rpc.thrift.TPipeTransferResp;

import org.junit.Assert;
import org.junit.Rule;
import org.junit.Test;
import org.junit.rules.TemporaryFolder;
import org.junit.runner.RunWith;
import org.powermock.api.mockito.PowerMockito;
import org.powermock.core.classloader.annotations.PowerMockIgnore;
import org.powermock.core.classloader.annotations.PrepareForTest;
import org.powermock.modules.junit4.PowerMockRunner;

import java.io.File;
import java.io.IOException;
import java.util.Collections;
import java.util.UUID;

import static org.mockito.ArgumentMatchers.anyList;
import static org.mockito.ArgumentMatchers.anyMap;
import static org.mockito.ArgumentMatchers.eq;

@RunWith(PowerMockRunner.class)
@PowerMockIgnore({"com.sun.org.apache.xerces.*", "javax.xml.*", "org.xml.*", "javax.management.*"})
@PrepareForTest(LoadUtil.class)
public class IoTDBDataNodeReceiverDiskSpaceTest {

@Rule public TemporaryFolder temporaryFolder = new TemporaryFolder();

@Test
public void testSealKeepsStagedFileWhenAsyncLoadHasInsufficientDiskSpace() throws Exception {
final File receiverDir = temporaryFolder.newFolder("receiver");
final TestReceiver receiver = new TestReceiver(receiverDir);
final String fileName = "disk-space.tsfile";
final byte[] fileContent = new byte[] {1, 2, 3};
final String taskId = UUID.randomUUID().toString();
final IoTDBConfig config = IoTDBDescriptor.getInstance().getConfig();
final boolean originalRequireUserIdentity = config.isPipeReceiverRequireUserIdentity();
final DiskSpaceInsufficientException exception =
new DiskSpaceInsufficientException(Collections.singletonList("load-dir"));

config.setPipeReceiverRequireUserIdentity(false);
try {
Assert.assertEquals(
TSStatusCode.SUCCESS_STATUS.getStatusCode(),
receiver.writeFilePiece(fileName, fileContent).getStatus().getCode());
final File stagedFile = new File(receiverDir, fileName);
final PipeTransferTsFileSealWithModReq request =
PipeTransferTsFileSealWithModReq.toTPipeTransferReq(
fileName, fileContent.length, "root.db")
.setConversionTaskInfo(taskId, false);

PowerMockito.mockStatic(LoadUtil.class);
PowerMockito.when(LoadUtil.loadFilesToActiveDir(anyMap(), anyList(), eq(true)))
.thenThrow(exception);

final TPipeTransferResp response = receiver.seal(request);

Assert.assertEquals(
TSStatusCode.DISK_SPACE_INSUFFICIENT.getStatusCode(), response.getStatus().getCode());
Assert.assertTrue(StatusUtils.needRetry(response.getStatus()));
Assert.assertEquals(
PipeTsFileConversionTaskManager.State.PAUSED,
PipeTsFileConversionTaskManager.get(taskId).getState());
Assert.assertTrue(stagedFile.isFile());
} finally {
config.setPipeReceiverRequireUserIdentity(originalRequireUserIdentity);
PipeTsFileConversionTaskManager.markSuccess(taskId);
receiver.handleExit();
}
}

private static final class TestReceiver extends IoTDBDataNodeReceiver {

private TestReceiver(final File receiverDir) {
receiverFileDirWithIdSuffix.set(receiverDir);
isUsingAsyncLoadTsFileStrategy.set(true);
}

private TPipeTransferResp writeFilePiece(final String fileName, final byte[] fileContent)
throws IOException {
return handleTransferFilePiece(
PipeTransferTsFilePieceWithModReq.toTPipeTransferReq(fileName, 0, fileContent),
false,
true);
}

private TPipeTransferResp seal(final PipeTransferTsFileSealWithModReq request) {
return handleTransferFileSealV2(request);
}
}
}
Loading
Loading