enh: simulate random i/o error

This commit is contained in:
kailixu 2024-08-12 12:13:26 +08:00
parent 4586e09b7b
commit 7e1bbd7459
1 changed files with 82 additions and 16 deletions

View File

@ -64,6 +64,16 @@ typedef struct TdFile {
#define FILE_WITH_LOCK 1 #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) { void taosGetTmpfilePath(const char *inputTmpDir, const char *fileNamePrefix, char *dstPath) {
#ifdef WINDOWS #ifdef WINDOWS
@ -202,6 +212,9 @@ TdFilePtr taosCreateFile(const char *path, int32_t tdFileOptions) {
} }
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); int32_t code = remove(path);
if (-1 == code) { if (-1 == code) {
terrno = TAOS_SYSTEM_ERROR(errno); terrno = TAOS_SYSTEM_ERROR(errno);
@ -211,6 +224,9 @@ int32_t taosRemoveFile(const char *path) {
} }
int32_t taosRenameFile(const char *oldName, const char *newName) { int32_t taosRenameFile(const char *oldName, const char *newName) {
#ifdef BUILD_WITH_RAND_ERR
STUB_RAND_IO_ERR(terrno)
#endif
#ifdef WINDOWS #ifdef WINDOWS
bool finished = false; 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) { 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 #if FILE_WITH_LOCK
(void)taosThreadRwlockRdlock(&(pFile->rwlock)); (void)taosThreadRwlockRdlock(&(pFile->rwlock));
#endif #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) { 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) { if (pFile == NULL) {
terrno = TSDB_CODE_INVALID_PARA; terrno = TSDB_CODE_INVALID_PARA;
return 0; 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) { 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) { if (pFile == NULL) {
terrno = TSDB_CODE_INVALID_PARA; terrno = TSDB_CODE_INVALID_PARA;
return 0; 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) { 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) { if (pFile == NULL) {
terrno = TSDB_CODE_INVALID_PARA; terrno = TSDB_CODE_INVALID_PARA;
return terrno; return terrno;
@ -966,11 +994,13 @@ int32_t taosUnLockFile(TdFilePtr pFile) {
} }
int32_t taosFtruncateFile(TdFilePtr pFile, int64_t l_size) { 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) { if (NULL == pFile || pFile->fd < 0) {
terrno = TSDB_CODE_INVALID_PARA; terrno = TSDB_CODE_INVALID_PARA;
return terrno; return terrno;
} }
#ifdef WINDOWS #ifdef WINDOWS
HANDLE h = (HANDLE)_get_osfhandle(pFile->fd); HANDLE h = (HANDLE)_get_osfhandle(pFile->fd);
@ -1026,6 +1056,9 @@ int32_t taosFtruncateFile(TdFilePtr pFile, int64_t l_size) {
} }
int64_t taosFSendFile(TdFilePtr pFileOut, TdFilePtr pFileIn, int64_t *offset, int64_t 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) { if (pFileOut == NULL || pFileIn == NULL) {
terrno = TSDB_CODE_INVALID_PARA; terrno = TSDB_CODE_INVALID_PARA;
return terrno; return terrno;
@ -1131,6 +1164,9 @@ bool lastErrorIsFileNotExist() { return terrno == TAOS_SYSTEM_ERROR(ENOENT); }
#endif // WINDOWS #endif // WINDOWS
TdFilePtr taosOpenFile(const char *path, int32_t tdFileOptions) { TdFilePtr taosOpenFile(const char *path, int32_t tdFileOptions) {
#ifdef BUILD_WITH_RAND_ERR
STUB_RAND_IO_ERR(NULL)
#endif
FILE *fp = NULL; FILE *fp = NULL;
#ifdef WINDOWS #ifdef WINDOWS
HANDLE hFile = NULL; HANDLE hFile = NULL;
@ -1191,6 +1227,9 @@ TdFilePtr taosOpenFile(const char *path, int32_t tdFileOptions) {
} }
int32_t taosCloseFile(TdFilePtr *ppFile) { int32_t taosCloseFile(TdFilePtr *ppFile) {
#ifdef BUILD_WITH_RAND_ERR
STUB_RAND_IO_ERR(terrno)
#endif
int32_t code = 0; int32_t code = 0;
if (ppFile == NULL || *ppFile == NULL) { if (ppFile == NULL || *ppFile == NULL) {
return 0; return 0;
@ -1233,6 +1272,9 @@ int32_t taosCloseFile(TdFilePtr *ppFile) {
} }
int64_t taosPReadFile(TdFilePtr pFile, void *buf, int64_t count, int64_t offset) { 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) { if (pFile == NULL) {
terrno = TSDB_CODE_INVALID_PARA; terrno = TSDB_CODE_INVALID_PARA;
return terrno; return terrno;
@ -1295,6 +1337,9 @@ int64_t taosPReadFile(TdFilePtr pFile, void *buf, int64_t count, int64_t offset)
} }
int32_t taosFsyncFile(TdFilePtr pFile) { int32_t taosFsyncFile(TdFilePtr pFile) {
#ifdef BUILD_WITH_RAND_ERR
STUB_RAND_IO_ERR(terrno)
#endif
if (pFile == NULL) { if (pFile == NULL) {
return 0; return 0;
} }
@ -1359,11 +1404,15 @@ int32_t taosUmaskFile(int32_t maskVal) {
} }
int64_t taosGetLineFile(TdFilePtr pFile, char **__restrict ptrBuf) { int64_t taosGetLineFile(TdFilePtr pFile, char **__restrict ptrBuf) {
#ifdef BUILD_WITH_RAND_ERR
STUB_RAND_IO_ERR(terrno)
#endif
int64_t ret = -1; int64_t ret = -1;
int32_t code = 0; int32_t code = 0;
#if FILE_WITH_LOCK #if FILE_WITH_LOCK
(void)taosThreadRwlockRdlock(&(pFile->rwlock)); (void)
taosThreadRwlockRdlock(&(pFile->rwlock));
#endif #endif
if (pFile == NULL || ptrBuf == NULL) { if (pFile == NULL || ptrBuf == NULL) {
terrno = TSDB_CODE_INVALID_PARA; terrno = TSDB_CODE_INVALID_PARA;
@ -1433,6 +1482,9 @@ END:
} }
int64_t taosGetsFile(TdFilePtr pFile, int32_t maxSize, char *__restrict buf) { 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) { if (pFile == NULL || buf == NULL) {
terrno = TSDB_CODE_INVALID_PARA; terrno = TSDB_CODE_INVALID_PARA;
return terrno; return terrno;
@ -1583,6 +1635,9 @@ int32_t taosLinkFile(char *src, char *dst) {
} }
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); FILE* f = fopen(filename, mode);
if (NULL == f) { if (NULL == f) {
terrno = TAOS_SYSTEM_ERROR(errno); 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) { 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); return fread(buffer, size, count, stream);
} }
size_t taosWriteToCFile(const void *ptr, size_t size, size_t nitems, FILE *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); 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) { int taosSetAutoDelFile(char *path) {
#ifdef WINDOWS #ifdef WINDOWS