enh: adjust the implementation of random selection

This commit is contained in:
Shungang Li 2024-08-13 15:09:54 +08:00
parent 21f8d6b7c1
commit c3b1e267b9
1 changed files with 14 additions and 72 deletions

View File

@ -947,34 +947,6 @@ _exit:
TAOS_RETURN(code); TAOS_RETURN(code);
} }
static int32_t s3InitEpIndexArray(SArray **pIndexArray) {
SArray *indexArray = taosArrayInit(tsS3EpNum, sizeof(int8_t));
if (!indexArray) {
uError("%s: %s", __func__, "out of memory");
TAOS_RETURN(TSDB_CODE_OUT_OF_MEMORY);
}
for (int8_t i = 0; i < tsS3EpNum; ++i) {
if (taosArrayPush(indexArray, &i) == NULL) {
taosArrayDestroy(indexArray);
uError("%s: %s", __func__, "out of memory");
TAOS_RETURN(TSDB_CODE_OUT_OF_MEMORY);
}
}
if (tsS3EpNum > 1) {
for (int8_t i = 0; i < tsS3EpNum; ++i) {
int8_t j = taosRand() % tsS3EpNum;
int8_t tmp = *(int8_t *)taosArrayGet(indexArray, i);
taosArraySet(indexArray, i, taosArrayGet(indexArray, j));
taosArraySet(indexArray, j, &tmp);
}
}
*pIndexArray = indexArray;
TAOS_RETURN(TSDB_CODE_SUCCESS);
}
int32_t s3PutObjectFromFile2ByEp(const char *file, const char *object_name, int8_t withcp, int8_t epIndex) { int32_t s3PutObjectFromFile2ByEp(const char *file, const char *object_name, int8_t withcp, int8_t epIndex) {
int32_t code = 0; int32_t code = 0;
int32_t lmtime = 0; int32_t lmtime = 0;
@ -1039,19 +1011,15 @@ int32_t s3PutObjectFromFile2ByEp(const char *file, const char *object_name, int8
int32_t s3PutObjectFromFile2(const char *file, const char *object_name, int8_t withcp) { int32_t s3PutObjectFromFile2(const char *file, const char *object_name, int8_t withcp) {
int32_t code = TSDB_CODE_SUCCESS; int32_t code = TSDB_CODE_SUCCESS;
SArray *indexArray = NULL; int8_t startIndex = taosRand() % tsS3EpNum;
TAOS_CHECK_RETURN(s3InitEpIndexArray(&indexArray));
for (int8_t i = 0; i < tsS3EpNum; ++i) { for (int8_t i = 0; i < tsS3EpNum; ++i) {
int8_t epIndex = *(int8_t *)taosArrayGet(indexArray, i); int8_t epIndex = (startIndex + i) % tsS3EpNum;
code = s3PutObjectFromFile2ByEp(file, object_name, withcp, epIndex); code = s3PutObjectFromFile2ByEp(file, object_name, withcp, epIndex);
if (code == TSDB_CODE_SUCCESS) { if (code == TSDB_CODE_SUCCESS) {
break; break;
} }
} }
taosArrayDestroy(indexArray);
return code; return code;
} }
@ -1122,19 +1090,15 @@ static int32_t s3PutObjectFromFileOffsetByEp(const char *file, const char *objec
int32_t s3PutObjectFromFileOffset(const char *file, const char *object_name, int64_t offset, int64_t size) { int32_t s3PutObjectFromFileOffset(const char *file, const char *object_name, int64_t offset, int64_t size) {
int32_t code = TSDB_CODE_SUCCESS; int32_t code = TSDB_CODE_SUCCESS;
SArray *indexArray = NULL; int8_t startIndex = taosRand() % tsS3EpNum;
TAOS_CHECK_RETURN(s3InitEpIndexArray(&indexArray));
for (int8_t i = 0; i < tsS3EpNum; ++i) { for (int8_t i = 0; i < tsS3EpNum; ++i) {
int8_t epIndex = *(int8_t *)taosArrayGet(indexArray, i); int8_t epIndex = (startIndex + i) % tsS3EpNum;
code = s3PutObjectFromFileOffsetByEp(file, object_name, offset, size, epIndex); code = s3PutObjectFromFileOffsetByEp(file, object_name, offset, size, epIndex);
if (code == TSDB_CODE_SUCCESS) { if (code == TSDB_CODE_SUCCESS) {
break; break;
} }
} }
taosArrayDestroy(indexArray);
return code; return code;
} }
@ -1242,21 +1206,15 @@ static SArray *getListByPrefixByEp(const char *prefix, int8_t epIndex) {
static SArray *getListByPrefix(const char *prefix) { static SArray *getListByPrefix(const char *prefix) {
SArray *objectArray = NULL; SArray *objectArray = NULL;
SArray *indexArray = NULL; int8_t startIndex = taosRand() % tsS3EpNum;
if (s3InitEpIndexArray(&indexArray) != TSDB_CODE_SUCCESS) {
return NULL;
}
for (int8_t i = 0; i < tsS3EpNum; ++i) { for (int8_t i = 0; i < tsS3EpNum; ++i) {
int8_t epIndex = *(int8_t *)taosArrayGet(indexArray, i); int8_t epIndex = (startIndex + i) % tsS3EpNum;
objectArray = getListByPrefixByEp(prefix, epIndex); objectArray = getListByPrefixByEp(prefix, epIndex);
if (objectArray) { if (objectArray) {
break; break;
} }
} }
taosArrayDestroy(indexArray);
return objectArray; return objectArray;
} }
@ -1291,19 +1249,15 @@ static int32_t s3DeleteObjectsByEp(const char *object_name[], int nobject, int8_
int32_t s3DeleteObjects(const char *object_name[], int nobject) { int32_t s3DeleteObjects(const char *object_name[], int nobject) {
int32_t code = 0; int32_t code = 0;
SArray *indexArray = NULL; int8_t startIndex = taosRand() % tsS3EpNum;
TAOS_CHECK_RETURN(s3InitEpIndexArray(&indexArray));
for (int8_t i = 0; i < tsS3EpNum; ++i) { for (int8_t i = 0; i < tsS3EpNum; ++i) {
int8_t epIndex = *(int8_t *)taosArrayGet(indexArray, i); int8_t epIndex = (startIndex + i) % tsS3EpNum;
code = s3DeleteObjectsByEp(object_name, nobject, epIndex); code = s3DeleteObjectsByEp(object_name, nobject, epIndex);
if (code == TSDB_CODE_SUCCESS) { if (code == TSDB_CODE_SUCCESS) {
break; break;
} }
} }
taosArrayDestroy(indexArray);
return code; return code;
} }
@ -1394,19 +1348,15 @@ _retry:
int32_t s3GetObjectBlock(const char *object_name, int64_t offset, int64_t size, bool check, uint8_t **ppBlock) { int32_t s3GetObjectBlock(const char *object_name, int64_t offset, int64_t size, bool check, uint8_t **ppBlock) {
int32_t code = 0; int32_t code = 0;
SArray *indexArray = NULL; int8_t startIndex = taosRand() % tsS3EpNum;
TAOS_CHECK_RETURN(s3InitEpIndexArray(&indexArray));
for (int8_t i = 0; i < tsS3EpNum; ++i) { for (int8_t i = 0; i < tsS3EpNum; ++i) {
int8_t epIndex = *(int8_t *)taosArrayGet(indexArray, i); int8_t epIndex = (startIndex + i) % tsS3EpNum;
code = s3GetObjectBlockByEp(object_name, offset, size, check, ppBlock, epIndex); code = s3GetObjectBlockByEp(object_name, offset, size, check, ppBlock, epIndex);
if (code == TSDB_CODE_SUCCESS) { if (code == TSDB_CODE_SUCCESS) {
break; break;
} }
} }
taosArrayDestroy(indexArray);
return code; return code;
} }
@ -1458,19 +1408,15 @@ static int32_t s3GetObjectToFileByEp(const char *object_name, const char *fileNa
int32_t s3GetObjectToFile(const char *object_name, const char *fileName) { int32_t s3GetObjectToFile(const char *object_name, const char *fileName) {
int32_t code = 0; int32_t code = 0;
SArray *indexArray = NULL; int8_t startIndex = taosRand() % tsS3EpNum;
TAOS_CHECK_RETURN(s3InitEpIndexArray(&indexArray));
for (int8_t i = 0; i < tsS3EpNum; ++i) { for (int8_t i = 0; i < tsS3EpNum; ++i) {
int8_t epIndex = *(int8_t *)taosArrayGet(indexArray, i); int8_t epIndex = (startIndex + i) % tsS3EpNum;
code = s3GetObjectToFileByEp(object_name, fileName, epIndex); code = s3GetObjectToFileByEp(object_name, fileName, epIndex);
if (code == TSDB_CODE_SUCCESS) { if (code == TSDB_CODE_SUCCESS) {
break; break;
} }
} }
taosArrayDestroy(indexArray);
return code; return code;
} }
@ -1531,19 +1477,15 @@ static long s3SizeByEp(const char *object_name, int8_t epIndex) {
long s3Size(const char *object_name) { long s3Size(const char *object_name) {
long size = 0; long size = 0;
SArray *indexArray = NULL; int8_t startIndex = taosRand() % tsS3EpNum;
TAOS_CHECK_RETURN(s3InitEpIndexArray(&indexArray));
for (int8_t i = 0; i < tsS3EpNum; ++i) { for (int8_t i = 0; i < tsS3EpNum; ++i) {
int8_t epIndex = *(int8_t *)taosArrayGet(indexArray, i); int8_t epIndex = (startIndex + i) % tsS3EpNum;
size = s3SizeByEp(object_name, epIndex); size = s3SizeByEp(object_name, epIndex);
if (size > 0) { if (size > 0) {
break; break;
} }
} }
taosArrayDestroy(indexArray);
return size; return size;
} }