From 69960f88b5fe23715a99a39cb80ae67873f31b8f Mon Sep 17 00:00:00 2001 From: Yusuke Kato Date: Mon, 11 Sep 2023 14:04:18 +0900 Subject: [PATCH] Refactor agent ngt core. (#2172) Signed-off-by: kpango --- .github/actions/deploy-chaos-mesh/action.yaml | 7 +--- .github/actions/setup-e2e/action.yaml | 17 +------- go.mod | 2 +- go.sum | 31 ++++++++++++++ hack/benchmark/internal/operation/search.go | 21 +++++++--- hack/go.mod.default | 2 +- internal/core/algorithm/ngt/ngt.go | 40 ++++++++++++++----- internal/core/algorithm/ngt/ngt_test.go | 12 +++--- internal/errors/ngt.go | 2 + .../core/ngt/handler/grpc/linear_search.go | 14 +++++-- pkg/agent/core/ngt/handler/grpc/search.go | 6 +++ pkg/agent/core/ngt/service/ngt.go | 22 ++++++---- 12 files changed, 120 insertions(+), 56 deletions(-) diff --git a/.github/actions/deploy-chaos-mesh/action.yaml b/.github/actions/deploy-chaos-mesh/action.yaml index 8518719c68..40cb61c580 100644 --- a/.github/actions/deploy-chaos-mesh/action.yaml +++ b/.github/actions/deploy-chaos-mesh/action.yaml @@ -15,7 +15,6 @@ # name: "Deploy Chaos Mesh" description: "A action to deploy Chaos Mesh" - inputs: chaos_mesh_version: description: "The Chaos Mesh version to use. The default version is `versions/CHAOS_MESH_VERSION`" @@ -25,7 +24,6 @@ inputs: description: "Extra options that passed to Helm command" required: false default: "" - runs: using: "composite" steps: @@ -38,18 +36,15 @@ runs: echo "version=${CHAOS_MESH_VERSION}" >> $GITHUB_OUTPUT env: CHAOS_MESH_VERSION: ${{ inputs.chaos_mesh_version }} - - name: Check k3d shell: bash id: check_k3d run: | - kubectl cluster-info - + kubectl cluster-info dump - name: Check Helm shell: bash run: | helm version - - name: Deploy Chaos Mesh shell: bash run: | diff --git a/.github/actions/setup-e2e/action.yaml b/.github/actions/setup-e2e/action.yaml index a1d67591c3..897f333926 100644 --- a/.github/actions/setup-e2e/action.yaml +++ b/.github/actions/setup-e2e/action.yaml @@ -15,7 +15,6 @@ # name: "Setup E2E environment" description: "A action to set up the environment for executing E2E test" - inputs: require_libhdf5: description: "If libhdf5 is not required, set this to false" @@ -40,11 +39,7 @@ inputs: target_images: description: "Image names" required: false - default: "vdaas/vald-agent-ngt \ - vdaas/vald-discoverer-k8s \ - vdaas/vald-lb-gateway \ - vdaas/vald-manager-index" - + default: "vdaas/vald-agent-ngt vdaas/vald-discoverer-k8s vdaas/vald-lb-gateway vdaas/vald-manager-index" outputs: HELM_EXTRA_OPTIONS: description: "Helm extra options that specifies E2E target image tags" @@ -52,7 +47,6 @@ outputs: IMAGE_TAGS: description: "Specifies E2E target image tags" value: ${{ steps.specify_container_versions.outputs.IMAGE_TAGS }} - runs: using: "composite" steps: @@ -62,40 +56,33 @@ runs: run: | sudo apt-get update sudo apt-get install -y libhdf5-dev - - name: Setup Go environment if: ${{ inputs.require_go == 'true' }} uses: ./.github/actions/setup-go - - name: Setup Helm environment if: ${{ inputs.require_helm == 'true' }} uses: ./.github/actions/setup-helm - - name: Wait for target Docker images if: startsWith( github.ref, 'refs/tags/') uses: ./.github/actions/wait-for-docker-image with: images: ${{ inputs.target_images }} - - name: Determine Docker image tag id: determine_tag_name uses: ./.github/actions/determine-docker-image-tag - - name: Specify container versions id: specify_container_versions uses: ./.github/actions/detect-docker-image-tags with: tag_name: ${{ steps.determine_tag_name.outputs.PRIMARY_TAG }} images: ${{ inputs.target_images }} - - uses: ./.github/actions/setup-k3d if: ${{ inputs.require_k3d == 'true' }} with: agents: 3 ingress_port: ${{ inputs.ingress_port }} options: "--image docker.io/rancher/k3s:latest" - - name: Check Kubernetes cluster shell: bash run: | - kubectl cluster-info + kubectl cluster-info dump diff --git a/go.mod b/go.mod index 3eb2a1ce15..d759a7989d 100755 --- a/go.mod +++ b/go.mod @@ -1,6 +1,6 @@ module github.com/vdaas/vald -go 1.20 +go 1.21 replace ( cloud.google.com/go => cloud.google.com/go v0.110.7 diff --git a/go.sum b/go.sum index a94d0a372f..04e935e515 100644 --- a/go.sum +++ b/go.sum @@ -154,6 +154,7 @@ git.sr.ht/~sbinet/gg v0.5.0/go.mod h1:G2C0eRESqlKhS7ErsNey6HHrqU1PwsnCQlekFi9Q2O git.wow.st/gmp/jni v0.0.0-20210610011705-34026c7e22d0/go.mod h1:+axXBRUTIDlCeE73IKeD/os7LoEnTKdkp8/gQOFjqyo= github.com/BurntSushi/toml v1.3.2/go.mod h1:CxXYINrC8qIiEnFrOxCa7Jy5BFHlXnUU2pbicEuybxQ= github.com/DATA-DOG/go-sqlmock v1.5.0 h1:Shsta01QNfFxHCfpW6YH2STWB0MudeXXEWMr20OEh60= +github.com/DATA-DOG/go-sqlmock v1.5.0/go.mod h1:f/Ixk793poVmq4qj/V1dPUg2JEAKC73Q5eFN3EC/SaM= github.com/JohnCGriffin/overflow v0.0.0-20211019200055-46fa312c352c/go.mod h1:X0CRv0ky0k6m906ixxpzmDRLvX58TFUKS2eePweuyxk= github.com/Kodeworks/golang-image-ico v0.0.0-20141118225523-73f0f4cfade9/go.mod h1:7uhhqiBaR4CpN0k9rMjOtjpcfGd6DG2m04zQxKnWQ0I= github.com/Masterminds/goutils v1.1.1 h1:5nUrii3FMTL5diU80unEVvNevw1nH4+ZV4DSLVJLSYI= @@ -174,28 +175,49 @@ github.com/antihax/optional v1.0.0/go.mod h1:uupD/76wgC+ih3iEmQUL+0Ugr19nfwCT1kd github.com/apache/arrow/go/v12 v12.0.0/go.mod h1:d+tV/eHZZ7Dz7RPrFKtPK02tpr+c9/PEd/zm8mDS9Vg= github.com/apache/thrift v0.16.0/go.mod h1:PHK3hniurgQaNMZYaCLEqXKsYK8upmhPbmdP2FXSqgU= github.com/armon/go-socks5 v0.0.0-20160902184237-e75332964ef5 h1:0CwZNZbxp69SHPdPJAN/hZIm0C4OItdklCFmMRWYpio= +github.com/armon/go-socks5 v0.0.0-20160902184237-e75332964ef5/go.mod h1:wHh0iHkYZB8zMSxRWpUBQtwG5a7fFgvEO+odwuTv2gs= github.com/aws/aws-sdk-go v1.45.6 h1:Y2isQQBZsnO15dzUQo9YQRThtHgrV200XCH05BRHVJI= github.com/aws/aws-sdk-go v1.45.6/go.mod h1:aVsgQcEevwlmQ7qHE9I3h+dtQgpqhFB+i8Phjh7fkwI= github.com/aws/aws-sdk-go-v2 v1.21.0 h1:gMT0IW+03wtYJhRqTVYn0wLzwdnK9sRMcxmtfGzRdJc= +github.com/aws/aws-sdk-go-v2 v1.21.0/go.mod h1:/RfNgGmRxI+iFOB1OeJUyxiU+9s88k3pfHvDagGEp0M= github.com/aws/aws-sdk-go-v2/aws/protocol/eventstream v1.4.13 h1:OPLEkmhXf6xFPiz0bLeDArZIDx1NNS4oJyG4nv3Gct0= +github.com/aws/aws-sdk-go-v2/aws/protocol/eventstream v1.4.13/go.mod h1:gpAbvyDGQFozTEmlTFO8XcQKHzubdq0LzRyJpG6MiXM= github.com/aws/aws-sdk-go-v2/config v1.18.39 h1:oPVyh6fuu/u4OiW4qcuQyEtk7U7uuNBmHmJSLg1AJsQ= +github.com/aws/aws-sdk-go-v2/config v1.18.39/go.mod h1:+NH/ZigdPckFpgB1TRcRuWCB/Kbbvkxc/iNAKTq5RhE= github.com/aws/aws-sdk-go-v2/credentials v1.13.37 h1:BvEdm09+ZEh2XtN+PVHPcYwKY3wIeB6pw7vPRM4M9/U= +github.com/aws/aws-sdk-go-v2/credentials v1.13.37/go.mod h1:ACLrdkd4CLZyXOghZ8IYumQbcooAcp2jo/s2xsFH8IM= github.com/aws/aws-sdk-go-v2/feature/ec2/imds v1.13.11 h1:uDZJF1hu0EVT/4bogChk8DyjSF6fof6uL/0Y26Ma7Fg= +github.com/aws/aws-sdk-go-v2/feature/ec2/imds v1.13.11/go.mod h1:TEPP4tENqBGO99KwVpV9MlOX4NSrSLP8u3KRy2CDwA8= github.com/aws/aws-sdk-go-v2/feature/s3/manager v1.11.83 h1:wcluDLIQ0uYaxv0fCWQRimbXkPdTgWHUD21j1CzXEwc= +github.com/aws/aws-sdk-go-v2/feature/s3/manager v1.11.83/go.mod h1:nGCBuon134gW67yAtxHKV73x+tAcY/xG4ZPNPDB1h/I= github.com/aws/aws-sdk-go-v2/internal/configsources v1.1.41 h1:22dGT7PneFMx4+b3pz7lMTRyN8ZKH7M2cW4GP9yUS2g= +github.com/aws/aws-sdk-go-v2/internal/configsources v1.1.41/go.mod h1:CrObHAuPneJBlfEJ5T3szXOUkLEThaGfvnhTf33buas= github.com/aws/aws-sdk-go-v2/internal/endpoints/v2 v2.4.35 h1:SijA0mgjV8E+8G45ltVHs0fvKpTj8xmZJ3VwhGKtUSI= +github.com/aws/aws-sdk-go-v2/internal/endpoints/v2 v2.4.35/go.mod h1:SJC1nEVVva1g3pHAIdCp7QsRIkMmLAgoDquQ9Rr8kYw= github.com/aws/aws-sdk-go-v2/internal/ini v1.3.42 h1:GPUcE/Yq7Ur8YSUk6lVkoIMWnJNO0HT18GUzCWCgCI0= +github.com/aws/aws-sdk-go-v2/internal/ini v1.3.42/go.mod h1:rzfdUlfA+jdgLDmPKjd3Chq9V7LVLYo1Nz++Wb91aRo= github.com/aws/aws-sdk-go-v2/internal/v4a v1.1.0 h1:U5yySdwt2HPo/pnQec04DImLzWORbeWML1fJiLkKruI= +github.com/aws/aws-sdk-go-v2/internal/v4a v1.1.0/go.mod h1:EhC/83j8/hL/UB1WmExo3gkElaja/KlmZM/gl1rTfjM= github.com/aws/aws-sdk-go-v2/service/internal/accept-encoding v1.9.14 h1:m0QTSI6pZYJTk5WSKx3fm5cNW/DCicVzULBgU/6IyD0= +github.com/aws/aws-sdk-go-v2/service/internal/accept-encoding v1.9.14/go.mod h1:dDilntgHy9WnHXsh7dDtUPgHKEfTJIBUTHM8OWm0f/0= github.com/aws/aws-sdk-go-v2/service/internal/checksum v1.1.36 h1:eev2yZX7esGRjqRbnVk1UxMLw4CyVZDpZXRCcy75oQk= +github.com/aws/aws-sdk-go-v2/service/internal/checksum v1.1.36/go.mod h1:lGnOkH9NJATw0XEPcAknFBj3zzNTEGRHtSw+CwC1YTg= github.com/aws/aws-sdk-go-v2/service/internal/presigned-url v1.9.35 h1:CdzPW9kKitgIiLV1+MHobfR5Xg25iYnyzWZhyQuSlDI= +github.com/aws/aws-sdk-go-v2/service/internal/presigned-url v1.9.35/go.mod h1:QGF2Rs33W5MaN9gYdEQOBBFPLwTZkEhRwI33f7KIG0o= github.com/aws/aws-sdk-go-v2/service/internal/s3shared v1.15.4 h1:v0jkRigbSD6uOdwcaUQmgEwG1BkPfAPDqaeNt/29ghg= +github.com/aws/aws-sdk-go-v2/service/internal/s3shared v1.15.4/go.mod h1:LhTyt8J04LL+9cIt7pYJ5lbS/U98ZmXovLOR/4LUsk8= github.com/aws/aws-sdk-go-v2/service/s3 v1.38.5 h1:A42xdtStObqy7NGvzZKpnyNXvoOmm+FENobZ0/ssHWk= +github.com/aws/aws-sdk-go-v2/service/s3 v1.38.5/go.mod h1:rDGMZA7f4pbmTtPOk5v5UM2lmX6UAbRnMDJeDvnH7AM= github.com/aws/aws-sdk-go-v2/service/sso v1.13.6 h1:2PylFCfKCEDv6PeSN09pC/VUiRd10wi1VfHG5FrW0/g= +github.com/aws/aws-sdk-go-v2/service/sso v1.13.6/go.mod h1:fIAwKQKBFu90pBxx07BFOMJLpRUGu8VOzLJakeY+0K4= github.com/aws/aws-sdk-go-v2/service/ssooidc v1.15.1 h1:hd0SKLMdOL/Sl6Z0np1PX9LeH2gqNtBe0MhTedA8MGI= +github.com/aws/aws-sdk-go-v2/service/ssooidc v1.15.1/go.mod h1:XO/VcyoQ8nKyKfFW/3DMsRQXsfh/052tHTWmg3xBXRg= github.com/aws/aws-sdk-go-v2/service/sts v1.21.5 h1:CQBFElb0LS8RojMJlxRSo/HXipvTZW2S44Lt9Mk2aYQ= +github.com/aws/aws-sdk-go-v2/service/sts v1.21.5/go.mod h1:VC7JDqsqiwXukYEDjoHh9U0fOJtNWh04FPQz4ct4GGU= github.com/aws/smithy-go v1.14.2 h1:MJU9hqBGbvWZdApzpvoF2WAIJDbtjK2NDJSiJP7HblQ= +github.com/aws/smithy-go v1.14.2/go.mod h1:Tg+OJXh4MB2R/uN61Ko2f6hTZwB/ZYGOtib8J3gBHzA= github.com/benbjohnson/clock v1.3.5 h1:VvXlSJBzZpA/zum6Sj74hxwYI2DIxRWuNIoXAzHZz5o= +github.com/benbjohnson/clock v1.3.5/go.mod h1:J11/hYXuz8f4ySSvYwY0FKfm+ezbsZBKZxNJlLklBHA= github.com/beorn7/perks v1.0.1 h1:VlbKKnNfV8bJzeqoa4cOKqO6bYr3WgKZxO8Z16+hsOM= github.com/beorn7/perks v1.0.1/go.mod h1:G2ZrVWU2WbWT9wwq4/hrbKbnv/1ERSJQ0ibhJ6rlkpw= github.com/bitly/go-hostpool v0.0.0-20171023180738-a3a6125de932/go.mod h1:NOuUCSz6Q9T7+igc/hlvDOUdtWKryOrtFyIVABv/p7k= @@ -223,6 +245,7 @@ github.com/creack/pty v1.1.18/go.mod h1:MOBLtS5ELjhRRrroQr9kyvTxUAFNvYEK993ew/Vr github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c= github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= github.com/denisenkom/go-mssqldb v0.12.3 h1:pBSGx9Tq67pBOTLmxNuirNTeB8Vjmf886Kx+8Y+8shw= +github.com/denisenkom/go-mssqldb v0.12.3/go.mod h1:k0mtMFOnU+AihqFxPMiF05rtiDrorD1Vrm1KEz5hxDo= github.com/dgryski/go-rendezvous v0.0.0-20200823014737-9f7001d12a5f h1:lO4WD4F/rVNCu3HqELle0jiPLLBs70cWOduZpkS1E78= github.com/dgryski/go-rendezvous v0.0.0-20200823014737-9f7001d12a5f/go.mod h1:cuUVRXasLTGF7a8hSLbxyZXjz+1KgoB3wDUb6vlszIc= github.com/disintegration/gift v1.2.1/go.mod h1:Jh2i7f7Q2BM7Ezno3PhfezbR1xpUg9dUg3/RlKGr4HI= @@ -267,6 +290,7 @@ github.com/go-logr/logr v1.2.4/go.mod h1:jdQByPbusPIv2/zmleS9BjJVeZ6kBagPoEUsqbV github.com/go-logr/stdr v1.2.2 h1:hSWxHoqTgW2S2qGc0LTAI563KZ5YKYRhT3MFKZMbjag= github.com/go-logr/stdr v1.2.2/go.mod h1:mMo/vtBO5dYbehREoey6XUKy/eSumjCCveDpRre4VKE= github.com/go-logr/zapr v1.2.4 h1:QHVo+6stLbfJmYGkQ7uGHUCu5hnAFAj6mDe6Ea0SeOo= +github.com/go-logr/zapr v1.2.4/go.mod h1:FyHWQIzQORZ0QVE1BtVHv3cKtNLuXsbNLtpuhNapBOA= github.com/go-ole/go-ole v1.2.6/go.mod h1:pprOEPIfldk/42T2oK7lQ4v4JSDwmV0As9GaiUsvbm0= github.com/go-openapi/jsonpointer v0.20.0 h1:ESKJdU9ASRfaPNOPRx12IUyA1vn3R9GiE3KYD14BXdQ= github.com/go-openapi/jsonpointer v0.20.0/go.mod h1:6PGzBjjIIumbLYysB73Klnms1mwnU4G3YHOECG3CedA= @@ -308,6 +332,7 @@ github.com/gogo/protobuf v1.3.2/go.mod h1:P1XiOD3dCwIKUDQYPy72D8LYyHL2YPYrpS2s69 github.com/goki/freetype v0.0.0-20181231101311-fa8a33aabaff/go.mod h1:wfqRWLHRBsRgkp5dmbG56SA0DmVtwrF5N3oPdI8t+Aw= github.com/goki/freetype v0.0.0-20220119013949-7a161fd3728c/go.mod h1:wfqRWLHRBsRgkp5dmbG56SA0DmVtwrF5N3oPdI8t+Aw= github.com/golang-sql/civil v0.0.0-20220223132316-b832511892a9 h1:au07oEsX2xN0ktxqI+Sida1w446QrXBRJ0nee3SNZlA= +github.com/golang-sql/civil v0.0.0-20220223132316-b832511892a9/go.mod h1:8vg3r2VgvsThLBIFL93Qb5yWzgyZWhEmBwUJWevAkK0= github.com/golang-sql/sqlexp v0.1.0 h1:ZCD6MBpcuOVfGVqsEmY5/4FtYiKz6tSyUv9LPEDei6A= github.com/golang-sql/sqlexp v0.1.0/go.mod h1:J4ad9Vo8ZCWQ2GMrC4UCQy1JpCbwU9m3EOqtpKwwwHI= github.com/golang/freetype v0.0.0-20170609003504-e2365dfdc4a0 h1:DACJavvAHhabrF08vX0COfcOBJRhZ8lUbR+ZWIs0Y5g= @@ -330,7 +355,9 @@ github.com/google/go-cmp v0.5.9 h1:O2Tfq5qg4qc4AmwVlvv0oLiVAGB7enBSJ2x2DqQFi38= github.com/google/go-cmp v0.5.9/go.mod h1:17dUlkBOakJ0+DkrSSNjCkIjxS6bF9zb3elmeNGIjoY= github.com/google/go-pkcs11 v0.2.0/go.mod h1:6eQoGcuNJpa7jnd5pMGdkSaQpNDYvPlXWMcjXXThLlY= github.com/google/go-replayers/grpcreplay v1.1.0 h1:S5+I3zYyZ+GQz68OfbURDdt/+cSMqCK1wrvNx7WBzTE= +github.com/google/go-replayers/grpcreplay v1.1.0/go.mod h1:qzAvJ8/wi57zq7gWqaE6AwLM6miiXUQwP1S+I9icmhk= github.com/google/go-replayers/httpreplay v1.2.0 h1:VM1wEyyjaoU53BwrOnaf9VhAyQQEEioJvFYxYcLRKzk= +github.com/google/go-replayers/httpreplay v1.2.0/go.mod h1:WahEFFZZ7a1P4VM1qEeHy+tME4bwyqPcwWbNlUI1Mcg= github.com/google/gofuzz v1.2.0 h1:xRy4A+RhZaiKjJ1bPfwQ8sedCA+YS2YcCHW6ec7JMi0= github.com/google/gofuzz v1.2.0/go.mod h1:dBl0BpW6vV/+mYPU4Po3pmUjxk6FQPldtuIdl/M65Eg= github.com/google/martian/v3 v3.3.2 h1:IqNFLAmvJOgVlpdEBiQbDc2EwKW77amAycfTuWKdfvw= @@ -387,6 +414,7 @@ github.com/jmespath/go-jmespath v0.4.0/go.mod h1:T8mJZnbsbmF+m6zOOFylbeCJqk5+pHW github.com/jmespath/go-jmespath/internal/testify v1.5.1 h1:shLQSRRSCCPj3f2gpwzGwWFoC7ycTf1rcQZHOlsJ6N8= github.com/jmespath/go-jmespath/internal/testify v1.5.1/go.mod h1:L3OGu8Wl2/fWfCI6z80xFu9LTZmf1ZRjMHUOPmWr69U= github.com/jmoiron/sqlx v1.3.5 h1:vFFPA71p1o5gAeqtEAwLU4dnX2napprKtHr7PYIcN3g= +github.com/jmoiron/sqlx v1.3.5/go.mod h1:nRVWtLre0KfCLJvgxzCsLVMogSvQ1zNJtpYr2Ccp0mQ= github.com/josephspurrier/goversioninfo v0.0.0-20200309025242-14b0ab84c6ca/go.mod h1:eJTEwMjXb7kZ633hO3Ln9mBUCOjX2+FlTljvpl9SYdE= github.com/josephspurrier/goversioninfo v1.4.0/go.mod h1:JWzv5rKQr+MmW+LvM412ToT/IkYDZjaclF2pKDss8IY= github.com/josharian/intern v1.0.0 h1:vlS4z54oSdjm0bgjRigI+G1HpF+tI+9rE5LLzOg8HmY= @@ -422,6 +450,7 @@ github.com/leanovate/gopter v0.2.9 h1:fQjYxZaynp97ozCzfOyOuAGOU4aU/z37zf/tOujFk7 github.com/leanovate/gopter v0.2.9/go.mod h1:U2L/78B+KVFIx2VmW6onHJQzXtFb+p5y3y2Sh+Jxxv8= github.com/ledongthuc/pdf v0.0.0-20220302134840-0c2507a12d80/go.mod h1:imJHygn/1yfhB7XSJJKlFZKl/J+dCPAknuiaGOshXAs= github.com/lib/pq v1.10.9 h1:YXG7RB+JIjhP29X+OtkiDnYaXQwpS4JEWq7dtCCRUEw= +github.com/lib/pq v1.10.9/go.mod h1:AlVN5x4E4T544tWzH6hKfbfQvm3HdbOxrmggDNAPY9o= github.com/liggitt/tabwriter v0.0.0-20181228230101-89fcab3d43de h1:9TO3cAIGXtEhnIaL+V+BEER86oLrvS+kWobKpbJuye0= github.com/liggitt/tabwriter v0.0.0-20181228230101-89fcab3d43de/go.mod h1:zAbeS9B/r2mtpb6U+EI2rYA5OAXxsYw6wTamcNW+zcE= github.com/lucasb-eyer/go-colorful v1.2.0 h1:1nnpGOrhyZZuNyfu1QjKiUICQ74+3FNCN69Aj6K7nkY= @@ -480,6 +509,7 @@ github.com/planetscale/vtprotobuf v0.5.0/go.mod h1:wm1N3qk9G/4+VM1WhpkLbvY/d8+0P github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM= github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4= github.com/prashantv/gostub v1.1.0 h1:BTyx3RfQjRHnUWaGF9oQos79AlQ5k8WNktv7VGvVH4g= +github.com/prashantv/gostub v1.1.0/go.mod h1:A5zLQHz7ieHGG7is6LLXLz7I8+3LZzsrV0P1IAHhP5U= github.com/prometheus/client_golang v1.16.0 h1:yk/hx9hDbrGHovbci4BY+pRMfSuuat626eFsHb7tmT8= github.com/prometheus/client_golang v1.16.0/go.mod h1:Zsulrv/L9oM40tJ7T815tM89lFEugiJ9HzIqaAx4LKc= github.com/prometheus/client_model v0.4.0 h1:5lQXD3cAg1OXBf4Wq03gTrXHeaV0TQvGfUooCfx1yqY= @@ -509,6 +539,7 @@ github.com/scylladb/go-reflectx v1.0.1/go.mod h1:rWnOfDIRWBGN0miMLIcoPt/Dhi2doCM github.com/scylladb/gocqlx v1.5.0 h1:p7NEqRaCMAtW2nvq62iyUNXmIYP29373YOC7D2Xd7Qg= github.com/scylladb/gocqlx v1.5.0/go.mod h1:QarZcw5kpYh31MXfxiN2JWWvF1cgZbYqfTfXwmwhpEQ= github.com/sergi/go-diff v1.3.1 h1:xkr+Oxo4BOQKmkn/B9eMK0g5Kg/983T9DqqPHwYqD+8= +github.com/sergi/go-diff v1.3.1/go.mod h1:aMJSSKb2lpPvRNec0+w3fl7LP9IOFzdc9Pa4NFbPK1I= github.com/shurcooL/go v0.0.0-20200502201357-93f07166e636/go.mod h1:TDJrrUr11Vxrven61rcy3hJMUqaf/CLWYhHNPmT14Lk= github.com/shurcooL/httpfs v0.0.0-20230704072500-f1e31cf0ba5c/go.mod h1:owqhoLW1qZoYLZzLnBw+QkPP9WZnjlSWihhxAJC1+/M= github.com/shurcooL/vfsgen v0.0.0-20230704071429-0000e147ea92/go.mod h1:7/OT02F6S6I7v6WXb+IjhMuZEYfH/RJ5RwEWnEo5BMg= diff --git a/hack/benchmark/internal/operation/search.go b/hack/benchmark/internal/operation/search.go index 997e1f8230..fc7ad62313 100644 --- a/hack/benchmark/internal/operation/search.go +++ b/hack/benchmark/internal/operation/search.go @@ -16,6 +16,7 @@ package operation import ( "context" "strconv" + "sync/atomic" "testing" "github.com/vdaas/vald/apis/grpc/v1/payload" @@ -138,6 +139,10 @@ func (o *operation) StreamSearchByID(ctx context.Context, b *testing.B, maxIdNum if err != nil { b.Fatal(err) } + if sc == nil { + b.Fatal("stream channel is nil for StreamSearchByID") + } + wg := sync.WaitGroup{} wg.Add(1) @@ -148,12 +153,16 @@ func (o *operation) StreamSearchByID(ctx context.Context, b *testing.B, maxIdNum } b.ResetTimer() + var finished atomic.Bool + finished.Store(false) + go func() { defer wg.Done() - for { + for sc != nil { res, err := sc.Recv() if err == io.EOF { + finished.Store(true) return } if err != nil { @@ -166,18 +175,20 @@ func (o *operation) StreamSearchByID(ctx context.Context, b *testing.B, maxIdNum } }() - for i := 0; i < b.N; i++ { + for i := 0; i < b.N && !finished.Load() && sc != nil; i++ { err = sc.Send(&payload.Search_IDRequest{ Id: strconv.Itoa(i % maxIdNum), Config: cfg, }) if err != nil { - b.Fatal(err) + b.Error(err) } } - if err := sc.CloseSend(); err != nil { - b.Fatal(err) + if sc != nil { + if err := sc.CloseSend(); err != nil { + b.Fatal(err) + } } wg.Wait() }) diff --git a/hack/go.mod.default b/hack/go.mod.default index 2bd345c3ba..ee8282afa5 100755 --- a/hack/go.mod.default +++ b/hack/go.mod.default @@ -1,6 +1,6 @@ module github.com/vdaas/vald -go 1.20 +go 1.21 replace ( cloud.google.com/go => cloud.google.com/go latest diff --git a/internal/core/algorithm/ngt/ngt.go b/internal/core/algorithm/ngt/ngt.go index ca76373c25..f700adba93 100644 --- a/internal/core/algorithm/ngt/ngt.go +++ b/internal/core/algorithm/ngt/ngt.go @@ -27,6 +27,7 @@ import "C" import ( "context" "reflect" + "sync/atomic" "unsafe" "github.com/vdaas/vald/internal/core/algorithm" @@ -44,7 +45,7 @@ type ( Search(ctx context.Context, vec []float32, size int, epsilon, radius float32) ([]SearchResult, error) // Linear Search returns linear search result as []SearchResult - LinearSearch(vec []float32, size int) ([]SearchResult, error) + LinearSearch(ctx context.Context, vec []float32, size int) ([]SearchResult, error) // Insert returns NGT object id. // This only stores not indexing, you must call CreateIndex and SaveIndex. @@ -96,6 +97,7 @@ type ( radius float32 epsilon float32 poolSize uint32 + cnt uint64 prop C.NGTProperty epool sync.Pool index C.NGTIndex @@ -399,7 +401,7 @@ func (n *ngt) Search(ctx context.Context, vec []float32, size int, epsilon, radi *(*C.float)(unsafe.Pointer(&radius)), results, ebuf) - + vec = nil if ret == ErrorCode { ne := ebuf n.rUnlock(true) @@ -409,11 +411,15 @@ func (n *ngt) Search(ctx context.Context, vec []float32, size int, epsilon, radi rsize := int(C.ngt_get_result_size(results, ebuf)) if rsize <= 0 { + if atomic.LoadUint64(&n.cnt) == 0 { + n.PutErrorBuffer(ebuf) + return nil, errors.ErrSearchResultEmptyButNoDataStored + } err = n.newGoError(ebuf) - if err == nil { - err = errors.ErrEmptySearchResult + if err != nil { + return nil, err } - return nil, err + return nil, errors.ErrEmptySearchResult } result = make([]SearchResult, rsize) @@ -438,7 +444,7 @@ func (n *ngt) Search(ctx context.Context, vec []float32, size int, epsilon, radi } // Linear Search returns linear search result as []SearchResult. -func (n *ngt) LinearSearch(vec []float32, size int) (result []SearchResult, err error) { +func (n *ngt) LinearSearch(ctx context.Context, vec []float32, size int) (result []SearchResult, err error) { if len(vec) != int(n.dimension) { return nil, errors.ErrIncompatibleDimensionSize(len(vec), int(n.dimension)) } @@ -459,6 +465,7 @@ func (n *ngt) LinearSearch(vec []float32, size int) (result []SearchResult, err *(*C.size_t)(unsafe.Pointer(&size)), results, ebuf) + vec = nil if ret == ErrorCode { ne := ebuf @@ -469,15 +476,24 @@ func (n *ngt) LinearSearch(vec []float32, size int) (result []SearchResult, err rsize := int(C.ngt_get_result_size(results, ebuf)) if rsize <= 0 { + if atomic.LoadUint64(&n.cnt) == 0 { + n.PutErrorBuffer(ebuf) + return nil, errors.ErrSearchResultEmptyButNoDataStored + } err = n.newGoError(ebuf) - if err == nil { - err = errors.ErrEmptySearchResult + if err != nil { + return nil, err } - return nil, err + return nil, errors.ErrEmptySearchResult } result = make([]SearchResult, rsize) - for i := range result { + select { + case <-ctx.Done(): + n.PutErrorBuffer(ebuf) + return result[:i], nil + default: + } d := C.ngt_get_result(results, C.uint32_t(i), ebuf) if d.id == 0 && d.distance == 0 { result[i] = SearchResult{0, 0, n.newGoError(ebuf)} @@ -503,10 +519,12 @@ func (n *ngt) Insert(vec []float32) (id uint, err error) { n.lock(true) id = uint(C.ngt_insert_index_as_float(n.index, (*C.float)(&vec[0]), C.uint32_t(n.dimension), ebuf)) n.unlock(true) + vec = nil if id == 0 { return 0, n.newGoError(ebuf) } n.PutErrorBuffer(ebuf) + atomic.AddUint64(&n.cnt, 1) return id, nil } @@ -658,6 +676,8 @@ func (n *ngt) Remove(id uint) error { } n.PutErrorBuffer(ebuf) + atomic.AddUint64(&n.cnt, ^uint64(0)) + return nil } diff --git a/internal/core/algorithm/ngt/ngt_test.go b/internal/core/algorithm/ngt/ngt_test.go index e78194eeba..04c25ac8cc 100644 --- a/internal/core/algorithm/ngt/ngt_test.go +++ b/internal/core/algorithm/ngt/ngt_test.go @@ -335,7 +335,7 @@ func TestLoad(t *testing.T) { // check no vector can be searched vs, err := n.Search(ctx, []float32{0, 1, 2, 3, 4, 5, 6, 7, 8}, 10, 0, 0) - if err != nil && !errors.Is(err, errors.ErrEmptySearchResult) { + if err != nil && !errors.Is(err, errors.ErrSearchResultEmptyButNoDataStored) { return err } if len(vs) != 0 { @@ -467,7 +467,7 @@ func TestLoad(t *testing.T) { // check no vector can be searched vs, err := n.Search(ctx, []float32{0, 0.1, 0.2, 0.3, 0.4, 0.5, 0.6, 0.7, 0.8}, 10, 0, 0) - if err != nil && !errors.Is(err, errors.ErrEmptySearchResult) { + if err != nil && !errors.Is(err, errors.ErrSearchResultEmptyButNoDataStored) { return err } if len(vs) != 0 { @@ -1836,7 +1836,7 @@ func Test_ngt_Search(t *testing.T) { }, }, { - name: "return ErrEmptySearchResult error if there is no inserted vector", + name: "return ErrSearchResultEmptyButNoDataStored error if there is no inserted vector", args: args{ ctx: context.Background(), vec: []float32{0.1, 0.2, 0.3, 0.4, 0.5, 0.6, 0.7, 0.8, 0.9}, @@ -1853,11 +1853,11 @@ func Test_ngt_Search(t *testing.T) { }, createFunc: defaultCreateFunc, want: want{ - err: errors.ErrEmptySearchResult, + err: errors.ErrSearchResultEmptyButNoDataStored, }, }, { - name: "return ErrEmptySearchResult error if the context is canceled", + name: "return ErrSearchResultEmptyButNoDataStored error if the context is canceled", args: args{ ctx: func() context.Context { ctx, cancel := context.WithCancel(context.Background()) @@ -1878,7 +1878,7 @@ func Test_ngt_Search(t *testing.T) { }, createFunc: defaultCreateFunc, want: want{ - err: errors.ErrEmptySearchResult, + err: errors.ErrSearchResultEmptyButNoDataStored, }, }, } diff --git a/internal/errors/ngt.go b/internal/errors/ngt.go index d994b9b40a..872307a1ba 100644 --- a/internal/errors/ngt.go +++ b/internal/errors/ngt.go @@ -40,6 +40,8 @@ var ( ErrRemoveRequestedBeforeIndexing = func(oid uint) error { return Errorf("object id %d is not indexed we cannot remove it", oid) } + + ErrSearchResultEmptyButNoDataStored = New("empty search result from cgo but no index data stored in ngt, this error can be ignored.") ) type NGTError struct { diff --git a/pkg/agent/core/ngt/handler/grpc/linear_search.go b/pkg/agent/core/ngt/handler/grpc/linear_search.go index 78e77df0fe..9ed55f04bb 100644 --- a/pkg/agent/core/ngt/handler/grpc/linear_search.go +++ b/pkg/agent/core/ngt/handler/grpc/linear_search.go @@ -34,7 +34,7 @@ import ( ) func (s *server) LinearSearch(ctx context.Context, req *payload.Search_Request) (res *payload.Search_Response, err error) { - _, span := trace.StartSpan(ctx, apiName+"/"+vald.LinearSearchRPCName) + ctx, span := trace.StartSpan(ctx, apiName+"/"+vald.LinearSearchRPCName) defer func() { if span != nil { span.End() @@ -67,9 +67,12 @@ func (s *server) LinearSearch(ctx context.Context, req *payload.Search_Request) } return nil, err } - res, err = s.ngt.LinearSearch( + res, err = s.ngt.LinearSearch(ctx, req.GetVector(), req.GetConfig().GetNum()) + if err == nil && res == nil { + return nil, nil + } if err != nil || res == nil { var attrs []attribute.KeyValue switch { @@ -157,7 +160,7 @@ func (s *server) LinearSearch(ctx context.Context, req *payload.Search_Request) } func (s *server) LinearSearchByID(ctx context.Context, req *payload.Search_IDRequest) (res *payload.Search_Response, err error) { - _, span := trace.StartSpan(ctx, apiName+"/"+vald.LinearSearchByIDRPCName) + ctx, span := trace.StartSpan(ctx, apiName+"/"+vald.LinearSearchByIDRPCName) defer func() { if span != nil { span.End() @@ -191,9 +194,12 @@ func (s *server) LinearSearchByID(ctx context.Context, req *payload.Search_IDReq } return nil, err } - vec, res, err := s.ngt.LinearSearchByID( + vec, res, err := s.ngt.LinearSearchByID(ctx, uuid, req.GetConfig().GetNum()) + if err == nil && res == nil { + return nil, nil + } if err != nil || res == nil { var attrs []attribute.KeyValue switch { diff --git a/pkg/agent/core/ngt/handler/grpc/search.go b/pkg/agent/core/ngt/handler/grpc/search.go index 289619efa1..b2149ab4fd 100644 --- a/pkg/agent/core/ngt/handler/grpc/search.go +++ b/pkg/agent/core/ngt/handler/grpc/search.go @@ -72,6 +72,9 @@ func (s *server) Search(ctx context.Context, req *payload.Search_Request) (res * req.GetConfig().GetNum(), req.GetConfig().GetEpsilon(), req.GetConfig().GetRadius()) + if err == nil && res == nil { + return nil, nil + } if err != nil || res == nil { var attrs []attribute.KeyValue switch { @@ -198,6 +201,9 @@ func (s *server) SearchByID(ctx context.Context, req *payload.Search_IDRequest) req.GetConfig().GetNum(), req.GetConfig().GetEpsilon(), req.GetConfig().GetRadius()) + if err == nil && res == nil { + return nil, nil + } if err != nil || res == nil { var attrs []attribute.KeyValue switch { diff --git a/pkg/agent/core/ngt/service/ngt.go b/pkg/agent/core/ngt/service/ngt.go index f7c638a3af..3d815db0bd 100644 --- a/pkg/agent/core/ngt/service/ngt.go +++ b/pkg/agent/core/ngt/service/ngt.go @@ -52,8 +52,8 @@ type NGT interface { Start(ctx context.Context) <-chan error Search(ctx context.Context, vec []float32, size uint32, epsilon, radius float32) (*payload.Search_Response, error) SearchByID(ctx context.Context, uuid string, size uint32, epsilon, radius float32) ([]float32, *payload.Search_Response, error) - LinearSearch(vec []float32, size uint32) (*payload.Search_Response, error) - LinearSearchByID(uuid string, size uint32) ([]float32, *payload.Search_Response, error) + LinearSearch(ctx context.Context, vec []float32, size uint32) (*payload.Search_Response, error) + LinearSearchByID(ctx context.Context, uuid string, size uint32) ([]float32, *payload.Search_Response, error) Insert(uuid string, vec []float32) (err error) InsertWithTime(uuid string, vec []float32, t int64) (err error) InsertMultiple(vecs map[string][]float32) (err error) @@ -883,7 +883,10 @@ func (n *ngt) Search(ctx context.Context, vec []float32, size uint32, epsilon, r if n.IsIndexing() { return nil, errors.ErrCreateIndexingIsInProgress } - log.Errorf("cgo error detected: ngt api returned error %v", err) + if errors.Is(err, errors.ErrSearchResultEmptyButNoDataStored) && n.Len() == 0 { + return nil, nil + } + log.Errorf("cgo error detected during search: ngt api returned error %v", err) return nil, err } @@ -905,23 +908,26 @@ func (n *ngt) SearchByID(ctx context.Context, uuid string, size uint32, epsilon, return vec, dst, nil } -func (n *ngt) LinearSearch(vec []float32, size uint32) (res *payload.Search_Response, err error) { +func (n *ngt) LinearSearch(ctx context.Context, vec []float32, size uint32) (res *payload.Search_Response, err error) { if n.IsIndexing() { return nil, errors.ErrCreateIndexingIsInProgress } - sr, err := n.core.LinearSearch(vec, int(size)) + sr, err := n.core.LinearSearch(ctx, vec, int(size)) if err != nil { if n.IsIndexing() { return nil, errors.ErrCreateIndexingIsInProgress } - log.Errorf("cgo error detected: ngt api returned error %v", err) + if errors.Is(err, errors.ErrSearchResultEmptyButNoDataStored) && n.Len() == 0 { + return nil, nil + } + log.Errorf("cgo error detected during linear search: ngt api returned error %v", err) return nil, err } return n.toSearchResponse(sr) } -func (n *ngt) LinearSearchByID(uuid string, size uint32) (vec []float32, dst *payload.Search_Response, err error) { +func (n *ngt) LinearSearchByID(ctx context.Context, uuid string, size uint32) (vec []float32, dst *payload.Search_Response, err error) { if n.IsIndexing() { return nil, nil, errors.ErrCreateIndexingIsInProgress } @@ -929,7 +935,7 @@ func (n *ngt) LinearSearchByID(uuid string, size uint32) (vec []float32, dst *pa if err != nil { return nil, nil, err } - dst, err = n.LinearSearch(vec, size) + dst, err = n.LinearSearch(ctx, vec, size) if err != nil { return vec, nil, err }