enh: simulate random i/o error

This commit is contained in:
kailixu 2024-08-12 13:37:11 +08:00
parent c14363625d
commit 89b4c7a3ed
1 changed files with 2 additions and 37 deletions

View File

@ -65,7 +65,6 @@ typedef struct TdFile {
#define FILE_WITH_LOCK 1 #define FILE_WITH_LOCK 1
#ifdef BUILD_WITH_RAND_ERR #ifdef BUILD_WITH_RAND_ERR
#define BUILD_WITH_RAND_IO_ERR
#define STUB_RAND_IO_ERR(ret) \ #define STUB_RAND_IO_ERR(ret) \
if (tsEnableRandErr) { \ if (tsEnableRandErr) { \
uint32_t r = taosRand() % 10001; \ uint32_t r = taosRand() % 10001; \
@ -75,6 +74,8 @@ typedef struct TdFile {
return (ret); \ return (ret); \
} \ } \
} }
#else
#define STUB_RAND_IO_ERR(ret)
#endif #endif
void taosGetTmpfilePath(const char *inputTmpDir, const char *fileNamePrefix, char *dstPath) { void taosGetTmpfilePath(const char *inputTmpDir, const char *fileNamePrefix, char *dstPath) {
@ -215,9 +216,7 @@ TdFilePtr taosCreateFile(const char *path, int32_t tdFileOptions) {
} }
int32_t taosRemoveFile(const char *path) { int32_t taosRemoveFile(const char *path) {
#ifdef BUILD_WITH_RAND_IO_ERR
STUB_RAND_IO_ERR(terrno) 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);
@ -227,9 +226,7 @@ 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_IO_ERR
STUB_RAND_IO_ERR(terrno) STUB_RAND_IO_ERR(terrno)
#endif
#ifdef WINDOWS #ifdef WINDOWS
bool finished = false; bool finished = false;
@ -713,9 +710,7 @@ 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_IO_ERR
STUB_RAND_IO_ERR(terrno) STUB_RAND_IO_ERR(terrno)
#endif
#if FILE_WITH_LOCK #if FILE_WITH_LOCK
(void)taosThreadRwlockRdlock(&(pFile->rwlock)); (void)taosThreadRwlockRdlock(&(pFile->rwlock));
#endif #endif
@ -769,9 +764,7 @@ 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_IO_ERR
STUB_RAND_IO_ERR(terrno) 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;
@ -817,9 +810,7 @@ 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_IO_ERR
STUB_RAND_IO_ERR(terrno) 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;
@ -901,9 +892,7 @@ 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_IO_ERR
STUB_RAND_IO_ERR(terrno) 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;
@ -997,9 +986,7 @@ 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_IO_ERR
STUB_RAND_IO_ERR(terrno) 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;
@ -1060,9 +1047,7 @@ 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_IO_ERR
STUB_RAND_IO_ERR(terrno) 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;
@ -1168,9 +1153,7 @@ 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_IO_ERR
STUB_RAND_IO_ERR(NULL) STUB_RAND_IO_ERR(NULL)
#endif
FILE *fp = NULL; FILE *fp = NULL;
#ifdef WINDOWS #ifdef WINDOWS
HANDLE hFile = NULL; HANDLE hFile = NULL;
@ -1231,9 +1214,7 @@ TdFilePtr taosOpenFile(const char *path, int32_t tdFileOptions) {
} }
int32_t taosCloseFile(TdFilePtr *ppFile) { int32_t taosCloseFile(TdFilePtr *ppFile) {
#ifdef BUILD_WITH_RAND_IO_ERR
STUB_RAND_IO_ERR(terrno) 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;
@ -1276,9 +1257,7 @@ 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_IO_ERR
STUB_RAND_IO_ERR(terrno) 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;
@ -1341,9 +1320,7 @@ 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_IO_ERR
STUB_RAND_IO_ERR(terrno) STUB_RAND_IO_ERR(terrno)
#endif
if (pFile == NULL) { if (pFile == NULL) {
return 0; return 0;
} }
@ -1408,9 +1385,7 @@ 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_IO_ERR
STUB_RAND_IO_ERR(terrno) STUB_RAND_IO_ERR(terrno)
#endif
int64_t ret = -1; int64_t ret = -1;
int32_t code = 0; int32_t code = 0;
@ -1485,9 +1460,7 @@ 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_IO_ERR
STUB_RAND_IO_ERR(terrno) 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;
@ -1638,9 +1611,7 @@ 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_IO_ERR
STUB_RAND_IO_ERR(NULL) 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);
@ -1661,23 +1632,17 @@ 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_IO_ERR
STUB_RAND_IO_ERR(terrno) 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_IO_ERR
STUB_RAND_IO_ERR(terrno) STUB_RAND_IO_ERR(terrno)
#endif
return fwrite(ptr, size, nitems, stream); return fwrite(ptr, size, nitems, stream);
} }
int taosCloseCFile(FILE *f) { int taosCloseCFile(FILE *f) {
#ifdef BUILD_WITH_RAND_IO_ERR
STUB_RAND_IO_ERR(terrno) STUB_RAND_IO_ERR(terrno)
#endif
return fclose(f); return fclose(f);
} }