From 7e1bbd7459c5cd43260accb68ceb460a4eee2b20 Mon Sep 17 00:00:00 2001 From: kailixu Date: Mon, 12 Aug 2024 12:13:26 +0800 Subject: [PATCH] enh: simulate random i/o error --- source/os/src/osFile.c | 98 +++++++++++++++++++++++++++++++++++------- 1 file changed, 82 insertions(+), 16 deletions(-) diff --git a/source/os/src/osFile.c b/source/os/src/osFile.c index 821488c90c..ef57182154 100644 --- a/source/os/src/osFile.c +++ b/source/os/src/osFile.c @@ -64,6 +64,16 @@ typedef struct TdFile { #define FILE_WITH_LOCK 1 +#define STUB_RAND_IO_ERR(ret) \ + if (tsEnableRandErr) { \ + uint32_t r = taosRand() % 10001; \ + if ((r + 1) <= tsRandErrChance) { \ + errno = EIO; \ + terrno = TAOS_SYSTEM_ERROR(errno); \ + return (ret); \ + } \ + } + void taosGetTmpfilePath(const char *inputTmpDir, const char *fileNamePrefix, char *dstPath) { #ifdef WINDOWS @@ -201,7 +211,10 @@ TdFilePtr taosCreateFile(const char *path, int32_t tdFileOptions) { return fp; } -int32_t taosRemoveFile(const char *path) { +int32_t taosRemoveFile(const char *path) { +#ifdef BUILD_WITH_RAND_ERR + STUB_RAND_IO_ERR(terrno) +#endif int32_t code = remove(path); if (-1 == code) { terrno = TAOS_SYSTEM_ERROR(errno); @@ -211,6 +224,9 @@ int32_t taosRemoveFile(const char *path) { } int32_t taosRenameFile(const char *oldName, const char *newName) { +#ifdef BUILD_WITH_RAND_ERR + STUB_RAND_IO_ERR(terrno) +#endif #ifdef WINDOWS bool finished = false; @@ -694,6 +710,9 @@ int taosOpenFileNotStream(const char *path, int32_t tdFileOptions) { } int64_t taosReadFile(TdFilePtr pFile, void *buf, int64_t count) { +#ifdef BUILD_WITH_RAND_ERR + STUB_RAND_IO_ERR(terrno) +#endif #if FILE_WITH_LOCK (void)taosThreadRwlockRdlock(&(pFile->rwlock)); #endif @@ -747,6 +766,9 @@ int64_t taosReadFile(TdFilePtr pFile, void *buf, int64_t count) { } int64_t taosWriteFile(TdFilePtr pFile, const void *buf, int64_t count) { +#ifdef BUILD_WITH_RAND_ERR + STUB_RAND_IO_ERR(terrno) +#endif if (pFile == NULL) { terrno = TSDB_CODE_INVALID_PARA; return 0; @@ -792,6 +814,9 @@ int64_t taosWriteFile(TdFilePtr pFile, const void *buf, int64_t count) { } int64_t taosPWriteFile(TdFilePtr pFile, const void *buf, int64_t count, int64_t offset) { +#ifdef BUILD_WITH_RAND_ERR + STUB_RAND_IO_ERR(terrno) +#endif if (pFile == NULL) { terrno = TSDB_CODE_INVALID_PARA; return 0; @@ -873,6 +898,9 @@ int64_t taosLSeekFile(TdFilePtr pFile, int64_t offset, int32_t whence) { } int32_t taosFStatFile(TdFilePtr pFile, int64_t *size, int32_t *mtime) { +#ifdef BUILD_WITH_RAND_ERR + STUB_RAND_IO_ERR(terrno) +#endif if (pFile == NULL) { terrno = TSDB_CODE_INVALID_PARA; return terrno; @@ -888,7 +916,7 @@ int32_t taosFStatFile(TdFilePtr pFile, int64_t *size, int32_t *mtime) { int32_t code = _fstat64(pFile->fd, &fileStat); #else struct stat fileStat; - int32_t code = fstat(pFile->fd, &fileStat); + int32_t code = fstat(pFile->fd, &fileStat); #endif if (-1 == code) { terrno = TAOS_SYSTEM_ERROR(errno); @@ -944,7 +972,7 @@ int32_t taosUnLockFile(TdFilePtr pFile) { terrno = TSDB_CODE_INVALID_PARA; return terrno; } - + #ifdef WINDOWS BOOL fSuccess = FALSE; OVERLAPPED overlapped = {0}; @@ -966,11 +994,13 @@ int32_t taosUnLockFile(TdFilePtr pFile) { } int32_t taosFtruncateFile(TdFilePtr pFile, int64_t l_size) { +#ifdef BUILD_WITH_RAND_ERR + STUB_RAND_IO_ERR(terrno) +#endif if (NULL == pFile || pFile->fd < 0) { terrno = TSDB_CODE_INVALID_PARA; return terrno; } - #ifdef WINDOWS HANDLE h = (HANDLE)_get_osfhandle(pFile->fd); @@ -1026,13 +1056,16 @@ int32_t taosFtruncateFile(TdFilePtr pFile, int64_t l_size) { } int64_t taosFSendFile(TdFilePtr pFileOut, TdFilePtr pFileIn, int64_t *offset, int64_t size) { +#ifdef BUILD_WITH_RAND_ERR + STUB_RAND_IO_ERR(terrno) +#endif if (pFileOut == NULL || pFileIn == NULL) { terrno = TSDB_CODE_INVALID_PARA; return terrno; } - if (pFileIn->fd < 0 || pFileOut->fd < 0) { - terrno = TSDB_CODE_INVALID_PARA; - return terrno; + if (pFileIn->fd < 0 || pFileOut->fd < 0) { + terrno = TSDB_CODE_INVALID_PARA; + return terrno; } #ifdef WINDOWS @@ -1131,6 +1164,9 @@ bool lastErrorIsFileNotExist() { return terrno == TAOS_SYSTEM_ERROR(ENOENT); } #endif // WINDOWS TdFilePtr taosOpenFile(const char *path, int32_t tdFileOptions) { +#ifdef BUILD_WITH_RAND_ERR + STUB_RAND_IO_ERR(NULL) +#endif FILE *fp = NULL; #ifdef WINDOWS HANDLE hFile = NULL; @@ -1191,6 +1227,9 @@ TdFilePtr taosOpenFile(const char *path, int32_t tdFileOptions) { } int32_t taosCloseFile(TdFilePtr *ppFile) { +#ifdef BUILD_WITH_RAND_ERR + STUB_RAND_IO_ERR(terrno) +#endif int32_t code = 0; if (ppFile == NULL || *ppFile == NULL) { return 0; @@ -1233,6 +1272,9 @@ int32_t taosCloseFile(TdFilePtr *ppFile) { } int64_t taosPReadFile(TdFilePtr pFile, void *buf, int64_t count, int64_t offset) { +#ifdef BUILD_WITH_RAND_ERR + STUB_RAND_IO_ERR(terrno) +#endif if (pFile == NULL) { terrno = TSDB_CODE_INVALID_PARA; return terrno; @@ -1295,6 +1337,9 @@ int64_t taosPReadFile(TdFilePtr pFile, void *buf, int64_t count, int64_t offset) } int32_t taosFsyncFile(TdFilePtr pFile) { +#ifdef BUILD_WITH_RAND_ERR + STUB_RAND_IO_ERR(terrno) +#endif if (pFile == NULL) { return 0; } @@ -1308,10 +1353,10 @@ int32_t taosFsyncFile(TdFilePtr pFile) { terrno = TAOS_SYSTEM_ERROR(errno); return terrno; } - + return 0; } - + #ifdef WINDOWS if (pFile->hFile != NULL) { if (pFile->tdFileOptions & TD_FILE_WRITE_THROUGH) { @@ -1359,11 +1404,15 @@ int32_t taosUmaskFile(int32_t maskVal) { } int64_t taosGetLineFile(TdFilePtr pFile, char **__restrict ptrBuf) { +#ifdef BUILD_WITH_RAND_ERR + STUB_RAND_IO_ERR(terrno) +#endif int64_t ret = -1; int32_t code = 0; - + #if FILE_WITH_LOCK - (void)taosThreadRwlockRdlock(&(pFile->rwlock)); + (void) + taosThreadRwlockRdlock(&(pFile->rwlock)); #endif if (pFile == NULL || ptrBuf == NULL) { terrno = TSDB_CODE_INVALID_PARA; @@ -1433,16 +1482,19 @@ END: } int64_t taosGetsFile(TdFilePtr pFile, int32_t maxSize, char *__restrict buf) { +#ifdef BUILD_WITH_RAND_ERR + STUB_RAND_IO_ERR(terrno) +#endif if (pFile == NULL || buf == NULL) { terrno = TSDB_CODE_INVALID_PARA; return terrno; } - + if (pFile->fp == NULL) { terrno = TSDB_CODE_INVALID_PARA; return terrno; } - + if (fgets(buf, maxSize, pFile->fp) == NULL) { if (feof(pFile->fp)) { return 0; @@ -1451,7 +1503,7 @@ int64_t taosGetsFile(TdFilePtr pFile, int32_t maxSize, char *__restrict buf) { return terrno; } } - + return strlen(buf); } @@ -1582,7 +1634,10 @@ int32_t taosLinkFile(char *src, char *dst) { return 0; } -FILE *taosOpenCFile(const char *filename, const char *mode) { +FILE *taosOpenCFile(const char *filename, const char *mode) { +#ifdef BUILD_WITH_RAND_ERR + STUB_RAND_IO_ERR(NULL) +#endif FILE* f = fopen(filename, mode); if (NULL == f) { terrno = TAOS_SYSTEM_ERROR(errno); @@ -1603,14 +1658,25 @@ int taosSeekCFile(FILE *file, int64_t offset, int whence) { } size_t taosReadFromCFile(void *buffer, size_t size, size_t count, FILE *stream) { +#ifdef BUILD_WITH_RAND_ERR + STUB_RAND_IO_ERR(terrno) +#endif return fread(buffer, size, count, stream); } size_t taosWriteToCFile(const void *ptr, size_t size, size_t nitems, FILE *stream) { +#ifdef BUILD_WITH_RAND_ERR + STUB_RAND_IO_ERR(terrno) +#endif return fwrite(ptr, size, nitems, stream); } -int taosCloseCFile(FILE *f) { return fclose(f); } +int taosCloseCFile(FILE *f) { +#ifdef BUILD_WITH_RAND_ERR + STUB_RAND_IO_ERR(terrno) +#endif + return fclose(f); +} int taosSetAutoDelFile(char *path) { #ifdef WINDOWS