enh: simulate random i/o error
This commit is contained in:
parent
4586e09b7b
commit
7e1bbd7459
|
@ -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
|
||||||
|
|
||||||
|
@ -201,7 +211,10 @@ TdFilePtr taosCreateFile(const char *path, int32_t tdFileOptions) {
|
||||||
return fp;
|
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);
|
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;
|
||||||
|
@ -888,7 +916,7 @@ int32_t taosFStatFile(TdFilePtr pFile, int64_t *size, int32_t *mtime) {
|
||||||
int32_t code = _fstat64(pFile->fd, &fileStat);
|
int32_t code = _fstat64(pFile->fd, &fileStat);
|
||||||
#else
|
#else
|
||||||
struct stat fileStat;
|
struct stat fileStat;
|
||||||
int32_t code = fstat(pFile->fd, &fileStat);
|
int32_t code = fstat(pFile->fd, &fileStat);
|
||||||
#endif
|
#endif
|
||||||
if (-1 == code) {
|
if (-1 == code) {
|
||||||
terrno = TAOS_SYSTEM_ERROR(errno);
|
terrno = TAOS_SYSTEM_ERROR(errno);
|
||||||
|
@ -944,7 +972,7 @@ int32_t taosUnLockFile(TdFilePtr pFile) {
|
||||||
terrno = TSDB_CODE_INVALID_PARA;
|
terrno = TSDB_CODE_INVALID_PARA;
|
||||||
return terrno;
|
return terrno;
|
||||||
}
|
}
|
||||||
|
|
||||||
#ifdef WINDOWS
|
#ifdef WINDOWS
|
||||||
BOOL fSuccess = FALSE;
|
BOOL fSuccess = FALSE;
|
||||||
OVERLAPPED overlapped = {0};
|
OVERLAPPED overlapped = {0};
|
||||||
|
@ -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,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) {
|
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;
|
||||||
}
|
}
|
||||||
if (pFileIn->fd < 0 || pFileOut->fd < 0) {
|
if (pFileIn->fd < 0 || pFileOut->fd < 0) {
|
||||||
terrno = TSDB_CODE_INVALID_PARA;
|
terrno = TSDB_CODE_INVALID_PARA;
|
||||||
return terrno;
|
return terrno;
|
||||||
}
|
}
|
||||||
|
|
||||||
#ifdef WINDOWS
|
#ifdef WINDOWS
|
||||||
|
@ -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;
|
||||||
}
|
}
|
||||||
|
@ -1308,10 +1353,10 @@ int32_t taosFsyncFile(TdFilePtr pFile) {
|
||||||
terrno = TAOS_SYSTEM_ERROR(errno);
|
terrno = TAOS_SYSTEM_ERROR(errno);
|
||||||
return terrno;
|
return terrno;
|
||||||
}
|
}
|
||||||
|
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
#ifdef WINDOWS
|
#ifdef WINDOWS
|
||||||
if (pFile->hFile != NULL) {
|
if (pFile->hFile != NULL) {
|
||||||
if (pFile->tdFileOptions & TD_FILE_WRITE_THROUGH) {
|
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) {
|
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,16 +1482,19 @@ 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;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (pFile->fp == NULL) {
|
if (pFile->fp == NULL) {
|
||||||
terrno = TSDB_CODE_INVALID_PARA;
|
terrno = TSDB_CODE_INVALID_PARA;
|
||||||
return terrno;
|
return terrno;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (fgets(buf, maxSize, pFile->fp) == NULL) {
|
if (fgets(buf, maxSize, pFile->fp) == NULL) {
|
||||||
if (feof(pFile->fp)) {
|
if (feof(pFile->fp)) {
|
||||||
return 0;
|
return 0;
|
||||||
|
@ -1451,7 +1503,7 @@ int64_t taosGetsFile(TdFilePtr pFile, int32_t maxSize, char *__restrict buf) {
|
||||||
return terrno;
|
return terrno;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
return strlen(buf);
|
return strlen(buf);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -1582,7 +1634,10 @@ int32_t taosLinkFile(char *src, char *dst) {
|
||||||
return 0;
|
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);
|
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
|
||||||
|
|
Loading…
Reference in New Issue