Skip to content

Commit

Permalink
fix: 支持检查Milvus Collection是否存在 TencentBlueKing#2573
Browse files Browse the repository at this point in the history
  • Loading branch information
cnlkl committed Sep 13, 2024
1 parent a8941a2 commit 7b8f313
Show file tree
Hide file tree
Showing 3 changed files with 106 additions and 7 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,19 @@ package com.tencent.bkrepo.job.batch.task.cache.preload.ai.milvus

import com.tencent.bkrepo.common.api.constant.HttpHeaders
import com.tencent.bkrepo.common.api.constant.MediaTypes
import com.tencent.bkrepo.common.api.util.readJsonString
import com.tencent.bkrepo.common.api.util.toJsonString
import com.tencent.bkrepo.common.storage.innercos.http.toRequestBody
import com.tencent.bkrepo.job.batch.task.cache.preload.ai.milvus.request.CreateCollectionReq
import com.tencent.bkrepo.job.batch.task.cache.preload.ai.milvus.request.HasCollectionReq
import com.tencent.bkrepo.job.batch.task.cache.preload.ai.milvus.response.HasCollectionRes
import com.tencent.bkrepo.job.batch.task.cache.preload.ai.milvus.response.MilvusResponse
import okhttp3.MediaType.Companion.toMediaType
import okhttp3.OkHttpClient
import okhttp3.Request
import okhttp3.Response
import okhttp3.internal.headersContentLength
import org.slf4j.LoggerFactory

class MilvusClient(
private val clientProperties: MilvusClientProperties
Expand Down Expand Up @@ -36,15 +43,22 @@ class MilvusClient(
.post(reqBody)
.url("${clientProperties.uri}/v2/vectordb/collections/create")
.build()
client.newCall(request).execute().use { response ->
if (!response.isSuccessful) {
throw RuntimeException("create collection[${req.collectionName}] failed")
}
client.newCall(request).execute().throwIfFailed {
logger.info("create collection[${req.collectionName}] success")
}
}

fun collectionExists(collectionName: String): Boolean {
TODO()
fun collectionExists(dbName: String, collectionName: String): Boolean {
val mediaType = MediaTypes.APPLICATION_JSON.toMediaType()
val body = HasCollectionReq(dbName, collectionName).toJsonString().toRequestBody(mediaType)
val request = Request.Builder()
.url("${clientProperties.uri}/v2/vectordb/collections/has")
.post(body)
.build()
return client.newCall(request).execute().throwIfFailed { response ->
val data = response.body!!.byteStream().readJsonString<MilvusResponse<HasCollectionRes>>().data
data.has
}
}

fun dropCollection(collectionName: String) {
Expand All @@ -56,4 +70,24 @@ class MilvusClient(
fun describeIndex(collectionName: String) {}

fun createIndex(collectionName: String) {}
}

private fun <T> Response.throwIfFailed(handler: (response: Response) -> T): T {
use {
if (isSuccessful) {
return handler(this)
} else {
val message = if (headersContentLength() < DEFAULT_MESSAGE_LIMIT) {
body?.string()
} else {
""
}
throw RuntimeException("request milvus failed, code: $code, message: $message")
}
}
}

companion object {
private val logger = LoggerFactory.getLogger(MilvusClient::class.java)
private const val DEFAULT_MESSAGE_LIMIT = 4096
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
/*
* Tencent is pleased to support the open source community by making BK-CI 蓝鲸持续集成平台 available.
*
* Copyright (C) 2024 THL A29 Limited, a Tencent company. All rights reserved.
*
* BK-CI 蓝鲸持续集成平台 is licensed under the MIT license.
*
* A copy of the MIT License is included in this file.
*
*
* Terms of the MIT License:
* ---------------------------------------------------
* Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated
* documentation files (the "Software"), to deal in the Software without restriction, including without limitation the
* rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to
* permit persons to whom the Software is furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in all copies or substantial portions of
* the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT
* LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN
* NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
* WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*/

package com.tencent.bkrepo.job.batch.task.cache.preload.ai.milvus.request

data class HasCollectionReq(
val dbName: String,
val collectionName: String
)
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
/*
* Tencent is pleased to support the open source community by making BK-CI 蓝鲸持续集成平台 available.
*
* Copyright (C) 2024 THL A29 Limited, a Tencent company. All rights reserved.
*
* BK-CI 蓝鲸持续集成平台 is licensed under the MIT license.
*
* A copy of the MIT License is included in this file.
*
*
* Terms of the MIT License:
* ---------------------------------------------------
* Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated
* documentation files (the "Software"), to deal in the Software without restriction, including without limitation the
* rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to
* permit persons to whom the Software is furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in all copies or substantial portions of
* the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT
* LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN
* NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
* WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*/

package com.tencent.bkrepo.job.batch.task.cache.preload.ai.milvus.response

data class HasCollectionRes(
val has: Boolean
)

0 comments on commit 7b8f313

Please sign in to comment.