From d275718077f29060764458677c8a18a37fed51ad Mon Sep 17 00:00:00 2001 From: Florian Schade Date: Thu, 26 Nov 2026 15:24:55 +0100 Subject: [PATCH] feat(search): add a command to optimize the search index --- .../v0/mocks/search_provider_service.go | 83 ++++++ .../opencloud/services/search/v0/search.pb.go | 282 +++++++++++++----- .../services/search/v0/search.pb.micro.go | 33 ++ .../services/search/v0/search.swagger.json | 45 +++ .../services/search/v0/search_grpc.pb.go | 46 ++- .../opencloud/services/search/v0/search.proto | 15 + services/search/README.md | 12 + services/search/pkg/bleve/backend.go | 29 ++ services/search/pkg/bleve/export_test.go | 6 + services/search/pkg/bleve/optimize_test.go | 85 ++++++ services/search/pkg/command/index.go | 79 +++-- services/search/pkg/command/index_test.go | 11 + services/search/pkg/command/optimize.go | 58 ++++ services/search/pkg/command/optimize_test.go | 50 ++++ services/search/pkg/command/root.go | 1 + services/search/pkg/opensearch/backend.go | 6 + services/search/pkg/search/mocks/engine.go | 51 ++++ services/search/pkg/search/mocks/searcher.go | 51 ++++ services/search/pkg/search/search.go | 10 + services/search/pkg/search/service.go | 6 + .../search/pkg/service/grpc/v0/service.go | 23 ++ .../pkg/service/grpc/v0/service_suite_test.go | 13 + .../pkg/service/grpc/v0/service_test.go | 96 ++++++ 23 files changed, 982 insertions(+), 109 deletions(-) create mode 100644 services/search/pkg/bleve/optimize_test.go create mode 100644 services/search/pkg/command/optimize.go create mode 100644 services/search/pkg/command/optimize_test.go create mode 100644 services/search/pkg/service/grpc/v0/service_suite_test.go create mode 100644 services/search/pkg/service/grpc/v0/service_test.go diff --git a/protogen/gen/opencloud/services/search/v0/mocks/search_provider_service.go b/protogen/gen/opencloud/services/search/v0/mocks/search_provider_service.go index 62f31ea371..f441f0efbe 100644 --- a/protogen/gen/opencloud/services/search/v0/mocks/search_provider_service.go +++ b/protogen/gen/opencloud/services/search/v0/mocks/search_provider_service.go @@ -122,6 +122,89 @@ func (_c *SearchProviderService_IndexSpace_Call) RunAndReturn(run func(ctx conte return _c } +// OptimizeIndex provides a mock function for the type SearchProviderService +func (_mock *SearchProviderService) OptimizeIndex(ctx context.Context, in *v0.OptimizeIndexRequest, opts ...client.CallOption) (*v0.OptimizeIndexResponse, error) { + var tmpRet mock.Arguments + if len(opts) > 0 { + tmpRet = _mock.Called(ctx, in, opts) + } else { + tmpRet = _mock.Called(ctx, in) + } + ret := tmpRet + + if len(ret) == 0 { + panic("no return value specified for OptimizeIndex") + } + + var r0 *v0.OptimizeIndexResponse + var r1 error + if returnFunc, ok := ret.Get(0).(func(context.Context, *v0.OptimizeIndexRequest, ...client.CallOption) (*v0.OptimizeIndexResponse, error)); ok { + return returnFunc(ctx, in, opts...) + } + if returnFunc, ok := ret.Get(0).(func(context.Context, *v0.OptimizeIndexRequest, ...client.CallOption) *v0.OptimizeIndexResponse); ok { + r0 = returnFunc(ctx, in, opts...) + } else { + if ret.Get(0) != nil { + r0 = ret.Get(0).(*v0.OptimizeIndexResponse) + } + } + if returnFunc, ok := ret.Get(1).(func(context.Context, *v0.OptimizeIndexRequest, ...client.CallOption) error); ok { + r1 = returnFunc(ctx, in, opts...) + } else { + r1 = ret.Error(1) + } + return r0, r1 +} + +// SearchProviderService_OptimizeIndex_Call is a *mock.Call that shadows Run/Return methods with type explicit version for method 'OptimizeIndex' +type SearchProviderService_OptimizeIndex_Call struct { + *mock.Call +} + +// OptimizeIndex is a helper method to define mock.On call +// - ctx context.Context +// - in *v0.OptimizeIndexRequest +// - opts ...client.CallOption +func (_e *SearchProviderService_Expecter) OptimizeIndex(ctx any, in any, opts ...any) *SearchProviderService_OptimizeIndex_Call { + return &SearchProviderService_OptimizeIndex_Call{Call: _e.mock.On("OptimizeIndex", + append([]any{ctx, in}, opts...)...)} +} + +func (_c *SearchProviderService_OptimizeIndex_Call) Run(run func(ctx context.Context, in *v0.OptimizeIndexRequest, opts ...client.CallOption)) *SearchProviderService_OptimizeIndex_Call { + _c.Call.Run(func(args mock.Arguments) { + var arg0 context.Context + if args[0] != nil { + arg0 = args[0].(context.Context) + } + var arg1 *v0.OptimizeIndexRequest + if args[1] != nil { + arg1 = args[1].(*v0.OptimizeIndexRequest) + } + var arg2 []client.CallOption + var variadicArgs []client.CallOption + if len(args) > 2 { + variadicArgs = args[2].([]client.CallOption) + } + arg2 = variadicArgs + run( + arg0, + arg1, + arg2..., + ) + }) + return _c +} + +func (_c *SearchProviderService_OptimizeIndex_Call) Return(optimizeIndexResponse *v0.OptimizeIndexResponse, err error) *SearchProviderService_OptimizeIndex_Call { + _c.Call.Return(optimizeIndexResponse, err) + return _c +} + +func (_c *SearchProviderService_OptimizeIndex_Call) RunAndReturn(run func(ctx context.Context, in *v0.OptimizeIndexRequest, opts ...client.CallOption) (*v0.OptimizeIndexResponse, error)) *SearchProviderService_OptimizeIndex_Call { + _c.Call.Return(run) + return _c +} + // Search provides a mock function for the type SearchProviderService func (_mock *SearchProviderService) Search(ctx context.Context, in *v0.SearchRequest, opts ...client.CallOption) (*v0.SearchResponse, error) { var tmpRet mock.Arguments diff --git a/protogen/gen/opencloud/services/search/v0/search.pb.go b/protogen/gen/opencloud/services/search/v0/search.pb.go index 82489194fa..e183be5e60 100644 --- a/protogen/gen/opencloud/services/search/v0/search.pb.go +++ b/protogen/gen/opencloud/services/search/v0/search.pb.go @@ -523,6 +523,92 @@ func (x *IndexSpaceResponse) GetStatus() IndexSpaceResponse_Status { return IndexSpaceResponse_STATUS_UNKNOWN } +type OptimizeIndexRequest struct { + state protoimpl.MessageState + sizeCache protoimpl.SizeCache + unknownFields protoimpl.UnknownFields +} + +func (x *OptimizeIndexRequest) Reset() { + *x = OptimizeIndexRequest{} + if protoimpl.UnsafeEnabled { + mi := &file_opencloud_services_search_v0_search_proto_msgTypes[6] + ms := protoimpl.X.MessageStateOf(protoimpl.Pointer(x)) + ms.StoreMessageInfo(mi) + } +} + +func (x *OptimizeIndexRequest) String() string { + return protoimpl.X.MessageStringOf(x) +} + +func (*OptimizeIndexRequest) ProtoMessage() {} + +func (x *OptimizeIndexRequest) ProtoReflect() protoreflect.Message { + mi := &file_opencloud_services_search_v0_search_proto_msgTypes[6] + if protoimpl.UnsafeEnabled && x != nil { + ms := protoimpl.X.MessageStateOf(protoimpl.Pointer(x)) + if ms.LoadMessageInfo() == nil { + ms.StoreMessageInfo(mi) + } + return ms + } + return mi.MessageOf(x) +} + +// Deprecated: Use OptimizeIndexRequest.ProtoReflect.Descriptor instead. +func (*OptimizeIndexRequest) Descriptor() ([]byte, []int) { + return file_opencloud_services_search_v0_search_proto_rawDescGZIP(), []int{6} +} + +type OptimizeIndexResponse struct { + state protoimpl.MessageState + sizeCache protoimpl.SizeCache + unknownFields protoimpl.UnknownFields + + // The duration it took to optimize the index. + Duration *durationpb.Duration `protobuf:"bytes,1,opt,name=duration,proto3" json:"duration,omitempty"` +} + +func (x *OptimizeIndexResponse) Reset() { + *x = OptimizeIndexResponse{} + if protoimpl.UnsafeEnabled { + mi := &file_opencloud_services_search_v0_search_proto_msgTypes[7] + ms := protoimpl.X.MessageStateOf(protoimpl.Pointer(x)) + ms.StoreMessageInfo(mi) + } +} + +func (x *OptimizeIndexResponse) String() string { + return protoimpl.X.MessageStringOf(x) +} + +func (*OptimizeIndexResponse) ProtoMessage() {} + +func (x *OptimizeIndexResponse) ProtoReflect() protoreflect.Message { + mi := &file_opencloud_services_search_v0_search_proto_msgTypes[7] + if protoimpl.UnsafeEnabled && x != nil { + ms := protoimpl.X.MessageStateOf(protoimpl.Pointer(x)) + if ms.LoadMessageInfo() == nil { + ms.StoreMessageInfo(mi) + } + return ms + } + return mi.MessageOf(x) +} + +// Deprecated: Use OptimizeIndexResponse.ProtoReflect.Descriptor instead. +func (*OptimizeIndexResponse) Descriptor() ([]byte, []int) { + return file_opencloud_services_search_v0_search_proto_rawDescGZIP(), []int{7} +} + +func (x *OptimizeIndexResponse) GetDuration() *durationpb.Duration { + if x != nil { + return x.Duration + } + return nil +} + var File_opencloud_services_search_v0_search_proto protoreflect.FileDescriptor var file_opencloud_services_search_v0_search_proto_rawDesc = []byte{ @@ -620,60 +706,77 @@ var file_opencloud_services_search_v0_search_proto_rawDesc = []byte{ 0x54, 0x55, 0x53, 0x5f, 0x53, 0x55, 0x43, 0x43, 0x45, 0x53, 0x53, 0x10, 0x01, 0x12, 0x12, 0x0a, 0x0e, 0x53, 0x54, 0x41, 0x54, 0x55, 0x53, 0x5f, 0x53, 0x4b, 0x49, 0x50, 0x50, 0x45, 0x44, 0x10, 0x02, 0x12, 0x10, 0x0a, 0x0c, 0x53, 0x54, 0x41, 0x54, 0x55, 0x53, 0x5f, 0x45, 0x52, 0x52, 0x4f, - 0x52, 0x10, 0x03, 0x32, 0xb3, 0x02, 0x0a, 0x0e, 0x53, 0x65, 0x61, 0x72, 0x63, 0x68, 0x50, 0x72, - 0x6f, 0x76, 0x69, 0x64, 0x65, 0x72, 0x12, 0x85, 0x01, 0x0a, 0x06, 0x53, 0x65, 0x61, 0x72, 0x63, - 0x68, 0x12, 0x2b, 0x2e, 0x6f, 0x70, 0x65, 0x6e, 0x63, 0x6c, 0x6f, 0x75, 0x64, 0x2e, 0x73, 0x65, - 0x72, 0x76, 0x69, 0x63, 0x65, 0x73, 0x2e, 0x73, 0x65, 0x61, 0x72, 0x63, 0x68, 0x2e, 0x76, 0x30, - 0x2e, 0x53, 0x65, 0x61, 0x72, 0x63, 0x68, 0x52, 0x65, 0x71, 0x75, 0x65, 0x73, 0x74, 0x1a, 0x2c, - 0x2e, 0x6f, 0x70, 0x65, 0x6e, 0x63, 0x6c, 0x6f, 0x75, 0x64, 0x2e, 0x73, 0x65, 0x72, 0x76, 0x69, - 0x63, 0x65, 0x73, 0x2e, 0x73, 0x65, 0x61, 0x72, 0x63, 0x68, 0x2e, 0x76, 0x30, 0x2e, 0x53, 0x65, - 0x61, 0x72, 0x63, 0x68, 0x52, 0x65, 0x73, 0x70, 0x6f, 0x6e, 0x73, 0x65, 0x22, 0x20, 0x82, 0xd3, - 0xe4, 0x93, 0x02, 0x1a, 0x3a, 0x01, 0x2a, 0x22, 0x15, 0x2f, 0x61, 0x70, 0x69, 0x2f, 0x76, 0x30, - 0x2f, 0x73, 0x65, 0x61, 0x72, 0x63, 0x68, 0x2f, 0x73, 0x65, 0x61, 0x72, 0x63, 0x68, 0x12, 0x98, - 0x01, 0x0a, 0x0a, 0x49, 0x6e, 0x64, 0x65, 0x78, 0x53, 0x70, 0x61, 0x63, 0x65, 0x12, 0x2f, 0x2e, - 0x6f, 0x70, 0x65, 0x6e, 0x63, 0x6c, 0x6f, 0x75, 0x64, 0x2e, 0x73, 0x65, 0x72, 0x76, 0x69, 0x63, - 0x65, 0x73, 0x2e, 0x73, 0x65, 0x61, 0x72, 0x63, 0x68, 0x2e, 0x76, 0x30, 0x2e, 0x49, 0x6e, 0x64, - 0x65, 0x78, 0x53, 0x70, 0x61, 0x63, 0x65, 0x52, 0x65, 0x71, 0x75, 0x65, 0x73, 0x74, 0x1a, 0x30, - 0x2e, 0x6f, 0x70, 0x65, 0x6e, 0x63, 0x6c, 0x6f, 0x75, 0x64, 0x2e, 0x73, 0x65, 0x72, 0x76, 0x69, - 0x63, 0x65, 0x73, 0x2e, 0x73, 0x65, 0x61, 0x72, 0x63, 0x68, 0x2e, 0x76, 0x30, 0x2e, 0x49, 0x6e, - 0x64, 0x65, 0x78, 0x53, 0x70, 0x61, 0x63, 0x65, 0x52, 0x65, 0x73, 0x70, 0x6f, 0x6e, 0x73, 0x65, - 0x22, 0x25, 0x82, 0xd3, 0xe4, 0x93, 0x02, 0x1f, 0x3a, 0x01, 0x2a, 0x22, 0x1a, 0x2f, 0x61, 0x70, - 0x69, 0x2f, 0x76, 0x30, 0x2f, 0x73, 0x65, 0x61, 0x72, 0x63, 0x68, 0x2f, 0x69, 0x6e, 0x64, 0x65, - 0x78, 0x2d, 0x73, 0x70, 0x61, 0x63, 0x65, 0x30, 0x01, 0x32, 0xa7, 0x01, 0x0a, 0x0d, 0x49, 0x6e, - 0x64, 0x65, 0x78, 0x50, 0x72, 0x6f, 0x76, 0x69, 0x64, 0x65, 0x72, 0x12, 0x95, 0x01, 0x0a, 0x06, - 0x53, 0x65, 0x61, 0x72, 0x63, 0x68, 0x12, 0x30, 0x2e, 0x6f, 0x70, 0x65, 0x6e, 0x63, 0x6c, 0x6f, + 0x52, 0x10, 0x03, 0x22, 0x16, 0x0a, 0x14, 0x4f, 0x70, 0x74, 0x69, 0x6d, 0x69, 0x7a, 0x65, 0x49, + 0x6e, 0x64, 0x65, 0x78, 0x52, 0x65, 0x71, 0x75, 0x65, 0x73, 0x74, 0x22, 0x4e, 0x0a, 0x15, 0x4f, + 0x70, 0x74, 0x69, 0x6d, 0x69, 0x7a, 0x65, 0x49, 0x6e, 0x64, 0x65, 0x78, 0x52, 0x65, 0x73, 0x70, + 0x6f, 0x6e, 0x73, 0x65, 0x12, 0x35, 0x0a, 0x08, 0x64, 0x75, 0x72, 0x61, 0x74, 0x69, 0x6f, 0x6e, + 0x18, 0x01, 0x20, 0x01, 0x28, 0x0b, 0x32, 0x19, 0x2e, 0x67, 0x6f, 0x6f, 0x67, 0x6c, 0x65, 0x2e, + 0x70, 0x72, 0x6f, 0x74, 0x6f, 0x62, 0x75, 0x66, 0x2e, 0x44, 0x75, 0x72, 0x61, 0x74, 0x69, 0x6f, + 0x6e, 0x52, 0x08, 0x64, 0x75, 0x72, 0x61, 0x74, 0x69, 0x6f, 0x6e, 0x32, 0xd8, 0x03, 0x0a, 0x0e, + 0x53, 0x65, 0x61, 0x72, 0x63, 0x68, 0x50, 0x72, 0x6f, 0x76, 0x69, 0x64, 0x65, 0x72, 0x12, 0x85, + 0x01, 0x0a, 0x06, 0x53, 0x65, 0x61, 0x72, 0x63, 0x68, 0x12, 0x2b, 0x2e, 0x6f, 0x70, 0x65, 0x6e, + 0x63, 0x6c, 0x6f, 0x75, 0x64, 0x2e, 0x73, 0x65, 0x72, 0x76, 0x69, 0x63, 0x65, 0x73, 0x2e, 0x73, + 0x65, 0x61, 0x72, 0x63, 0x68, 0x2e, 0x76, 0x30, 0x2e, 0x53, 0x65, 0x61, 0x72, 0x63, 0x68, 0x52, + 0x65, 0x71, 0x75, 0x65, 0x73, 0x74, 0x1a, 0x2c, 0x2e, 0x6f, 0x70, 0x65, 0x6e, 0x63, 0x6c, 0x6f, 0x75, 0x64, 0x2e, 0x73, 0x65, 0x72, 0x76, 0x69, 0x63, 0x65, 0x73, 0x2e, 0x73, 0x65, 0x61, 0x72, - 0x63, 0x68, 0x2e, 0x76, 0x30, 0x2e, 0x53, 0x65, 0x61, 0x72, 0x63, 0x68, 0x49, 0x6e, 0x64, 0x65, - 0x78, 0x52, 0x65, 0x71, 0x75, 0x65, 0x73, 0x74, 0x1a, 0x31, 0x2e, 0x6f, 0x70, 0x65, 0x6e, 0x63, - 0x6c, 0x6f, 0x75, 0x64, 0x2e, 0x73, 0x65, 0x72, 0x76, 0x69, 0x63, 0x65, 0x73, 0x2e, 0x73, 0x65, - 0x61, 0x72, 0x63, 0x68, 0x2e, 0x76, 0x30, 0x2e, 0x53, 0x65, 0x61, 0x72, 0x63, 0x68, 0x49, 0x6e, - 0x64, 0x65, 0x78, 0x52, 0x65, 0x73, 0x70, 0x6f, 0x6e, 0x73, 0x65, 0x22, 0x26, 0x82, 0xd3, 0xe4, - 0x93, 0x02, 0x20, 0x3a, 0x01, 0x2a, 0x22, 0x1b, 0x2f, 0x61, 0x70, 0x69, 0x2f, 0x76, 0x30, 0x2f, - 0x73, 0x65, 0x61, 0x72, 0x63, 0x68, 0x2f, 0x69, 0x6e, 0x64, 0x65, 0x78, 0x2f, 0x73, 0x65, 0x61, - 0x72, 0x63, 0x68, 0x42, 0xf2, 0x02, 0x5a, 0x4a, 0x67, 0x69, 0x74, 0x68, 0x75, 0x62, 0x2e, 0x63, - 0x6f, 0x6d, 0x2f, 0x6f, 0x70, 0x65, 0x6e, 0x63, 0x6c, 0x6f, 0x75, 0x64, 0x2d, 0x65, 0x75, 0x2f, - 0x6f, 0x70, 0x65, 0x6e, 0x63, 0x6c, 0x6f, 0x75, 0x64, 0x2f, 0x70, 0x72, 0x6f, 0x74, 0x6f, 0x67, - 0x65, 0x6e, 0x2f, 0x67, 0x65, 0x6e, 0x2f, 0x6f, 0x70, 0x65, 0x6e, 0x63, 0x6c, 0x6f, 0x75, 0x64, - 0x2f, 0x73, 0x65, 0x72, 0x76, 0x69, 0x63, 0x65, 0x2f, 0x73, 0x65, 0x61, 0x72, 0x63, 0x68, 0x2f, - 0x76, 0x30, 0x92, 0x41, 0xa2, 0x02, 0x12, 0xb7, 0x01, 0x0a, 0x10, 0x4f, 0x70, 0x65, 0x6e, 0x43, - 0x6c, 0x6f, 0x75, 0x64, 0x20, 0x73, 0x65, 0x61, 0x72, 0x63, 0x68, 0x22, 0x51, 0x0a, 0x0e, 0x4f, - 0x70, 0x65, 0x6e, 0x43, 0x6c, 0x6f, 0x75, 0x64, 0x20, 0x47, 0x6d, 0x62, 0x48, 0x12, 0x29, 0x68, - 0x74, 0x74, 0x70, 0x73, 0x3a, 0x2f, 0x2f, 0x67, 0x69, 0x74, 0x68, 0x75, 0x62, 0x2e, 0x63, 0x6f, - 0x6d, 0x2f, 0x6f, 0x70, 0x65, 0x6e, 0x63, 0x6c, 0x6f, 0x75, 0x64, 0x2d, 0x65, 0x75, 0x2f, 0x6f, - 0x70, 0x65, 0x6e, 0x63, 0x6c, 0x6f, 0x75, 0x64, 0x1a, 0x14, 0x73, 0x75, 0x70, 0x70, 0x6f, 0x72, - 0x74, 0x40, 0x6f, 0x70, 0x65, 0x6e, 0x63, 0x6c, 0x6f, 0x75, 0x64, 0x2e, 0x65, 0x75, 0x2a, 0x49, - 0x0a, 0x0a, 0x41, 0x70, 0x61, 0x63, 0x68, 0x65, 0x2d, 0x32, 0x2e, 0x30, 0x12, 0x3b, 0x68, 0x74, - 0x74, 0x70, 0x73, 0x3a, 0x2f, 0x2f, 0x67, 0x69, 0x74, 0x68, 0x75, 0x62, 0x2e, 0x63, 0x6f, 0x6d, - 0x2f, 0x6f, 0x70, 0x65, 0x6e, 0x63, 0x6c, 0x6f, 0x75, 0x64, 0x2d, 0x65, 0x75, 0x2f, 0x6f, 0x70, - 0x65, 0x6e, 0x63, 0x6c, 0x6f, 0x75, 0x64, 0x2f, 0x62, 0x6c, 0x6f, 0x62, 0x2f, 0x6d, 0x61, 0x69, - 0x6e, 0x2f, 0x4c, 0x49, 0x43, 0x45, 0x4e, 0x53, 0x45, 0x32, 0x05, 0x31, 0x2e, 0x30, 0x2e, 0x30, - 0x2a, 0x02, 0x01, 0x02, 0x32, 0x10, 0x61, 0x70, 0x70, 0x6c, 0x69, 0x63, 0x61, 0x74, 0x69, 0x6f, - 0x6e, 0x2f, 0x6a, 0x73, 0x6f, 0x6e, 0x3a, 0x10, 0x61, 0x70, 0x70, 0x6c, 0x69, 0x63, 0x61, 0x74, - 0x69, 0x6f, 0x6e, 0x2f, 0x6a, 0x73, 0x6f, 0x6e, 0x72, 0x3e, 0x0a, 0x10, 0x44, 0x65, 0x76, 0x65, - 0x6c, 0x6f, 0x70, 0x65, 0x72, 0x20, 0x4d, 0x61, 0x6e, 0x75, 0x61, 0x6c, 0x12, 0x2a, 0x68, 0x74, - 0x74, 0x70, 0x73, 0x3a, 0x2f, 0x2f, 0x64, 0x6f, 0x63, 0x73, 0x2e, 0x6f, 0x70, 0x65, 0x6e, 0x63, - 0x6c, 0x6f, 0x75, 0x64, 0x2e, 0x65, 0x75, 0x2f, 0x73, 0x65, 0x72, 0x76, 0x69, 0x63, 0x65, 0x73, - 0x2f, 0x73, 0x65, 0x61, 0x72, 0x63, 0x68, 0x2f, 0x62, 0x06, 0x70, 0x72, 0x6f, 0x74, 0x6f, 0x33, + 0x63, 0x68, 0x2e, 0x76, 0x30, 0x2e, 0x53, 0x65, 0x61, 0x72, 0x63, 0x68, 0x52, 0x65, 0x73, 0x70, + 0x6f, 0x6e, 0x73, 0x65, 0x22, 0x20, 0x82, 0xd3, 0xe4, 0x93, 0x02, 0x1a, 0x3a, 0x01, 0x2a, 0x22, + 0x15, 0x2f, 0x61, 0x70, 0x69, 0x2f, 0x76, 0x30, 0x2f, 0x73, 0x65, 0x61, 0x72, 0x63, 0x68, 0x2f, + 0x73, 0x65, 0x61, 0x72, 0x63, 0x68, 0x12, 0x98, 0x01, 0x0a, 0x0a, 0x49, 0x6e, 0x64, 0x65, 0x78, + 0x53, 0x70, 0x61, 0x63, 0x65, 0x12, 0x2f, 0x2e, 0x6f, 0x70, 0x65, 0x6e, 0x63, 0x6c, 0x6f, 0x75, + 0x64, 0x2e, 0x73, 0x65, 0x72, 0x76, 0x69, 0x63, 0x65, 0x73, 0x2e, 0x73, 0x65, 0x61, 0x72, 0x63, + 0x68, 0x2e, 0x76, 0x30, 0x2e, 0x49, 0x6e, 0x64, 0x65, 0x78, 0x53, 0x70, 0x61, 0x63, 0x65, 0x52, + 0x65, 0x71, 0x75, 0x65, 0x73, 0x74, 0x1a, 0x30, 0x2e, 0x6f, 0x70, 0x65, 0x6e, 0x63, 0x6c, 0x6f, + 0x75, 0x64, 0x2e, 0x73, 0x65, 0x72, 0x76, 0x69, 0x63, 0x65, 0x73, 0x2e, 0x73, 0x65, 0x61, 0x72, + 0x63, 0x68, 0x2e, 0x76, 0x30, 0x2e, 0x49, 0x6e, 0x64, 0x65, 0x78, 0x53, 0x70, 0x61, 0x63, 0x65, + 0x52, 0x65, 0x73, 0x70, 0x6f, 0x6e, 0x73, 0x65, 0x22, 0x25, 0x82, 0xd3, 0xe4, 0x93, 0x02, 0x1f, + 0x3a, 0x01, 0x2a, 0x22, 0x1a, 0x2f, 0x61, 0x70, 0x69, 0x2f, 0x76, 0x30, 0x2f, 0x73, 0x65, 0x61, + 0x72, 0x63, 0x68, 0x2f, 0x69, 0x6e, 0x64, 0x65, 0x78, 0x2d, 0x73, 0x70, 0x61, 0x63, 0x65, 0x30, + 0x01, 0x12, 0xa2, 0x01, 0x0a, 0x0d, 0x4f, 0x70, 0x74, 0x69, 0x6d, 0x69, 0x7a, 0x65, 0x49, 0x6e, + 0x64, 0x65, 0x78, 0x12, 0x32, 0x2e, 0x6f, 0x70, 0x65, 0x6e, 0x63, 0x6c, 0x6f, 0x75, 0x64, 0x2e, + 0x73, 0x65, 0x72, 0x76, 0x69, 0x63, 0x65, 0x73, 0x2e, 0x73, 0x65, 0x61, 0x72, 0x63, 0x68, 0x2e, + 0x76, 0x30, 0x2e, 0x4f, 0x70, 0x74, 0x69, 0x6d, 0x69, 0x7a, 0x65, 0x49, 0x6e, 0x64, 0x65, 0x78, + 0x52, 0x65, 0x71, 0x75, 0x65, 0x73, 0x74, 0x1a, 0x33, 0x2e, 0x6f, 0x70, 0x65, 0x6e, 0x63, 0x6c, + 0x6f, 0x75, 0x64, 0x2e, 0x73, 0x65, 0x72, 0x76, 0x69, 0x63, 0x65, 0x73, 0x2e, 0x73, 0x65, 0x61, + 0x72, 0x63, 0x68, 0x2e, 0x76, 0x30, 0x2e, 0x4f, 0x70, 0x74, 0x69, 0x6d, 0x69, 0x7a, 0x65, 0x49, + 0x6e, 0x64, 0x65, 0x78, 0x52, 0x65, 0x73, 0x70, 0x6f, 0x6e, 0x73, 0x65, 0x22, 0x28, 0x82, 0xd3, + 0xe4, 0x93, 0x02, 0x22, 0x3a, 0x01, 0x2a, 0x22, 0x1d, 0x2f, 0x61, 0x70, 0x69, 0x2f, 0x76, 0x30, + 0x2f, 0x73, 0x65, 0x61, 0x72, 0x63, 0x68, 0x2f, 0x6f, 0x70, 0x74, 0x69, 0x6d, 0x69, 0x7a, 0x65, + 0x2d, 0x69, 0x6e, 0x64, 0x65, 0x78, 0x32, 0xa7, 0x01, 0x0a, 0x0d, 0x49, 0x6e, 0x64, 0x65, 0x78, + 0x50, 0x72, 0x6f, 0x76, 0x69, 0x64, 0x65, 0x72, 0x12, 0x95, 0x01, 0x0a, 0x06, 0x53, 0x65, 0x61, + 0x72, 0x63, 0x68, 0x12, 0x30, 0x2e, 0x6f, 0x70, 0x65, 0x6e, 0x63, 0x6c, 0x6f, 0x75, 0x64, 0x2e, + 0x73, 0x65, 0x72, 0x76, 0x69, 0x63, 0x65, 0x73, 0x2e, 0x73, 0x65, 0x61, 0x72, 0x63, 0x68, 0x2e, + 0x76, 0x30, 0x2e, 0x53, 0x65, 0x61, 0x72, 0x63, 0x68, 0x49, 0x6e, 0x64, 0x65, 0x78, 0x52, 0x65, + 0x71, 0x75, 0x65, 0x73, 0x74, 0x1a, 0x31, 0x2e, 0x6f, 0x70, 0x65, 0x6e, 0x63, 0x6c, 0x6f, 0x75, + 0x64, 0x2e, 0x73, 0x65, 0x72, 0x76, 0x69, 0x63, 0x65, 0x73, 0x2e, 0x73, 0x65, 0x61, 0x72, 0x63, + 0x68, 0x2e, 0x76, 0x30, 0x2e, 0x53, 0x65, 0x61, 0x72, 0x63, 0x68, 0x49, 0x6e, 0x64, 0x65, 0x78, + 0x52, 0x65, 0x73, 0x70, 0x6f, 0x6e, 0x73, 0x65, 0x22, 0x26, 0x82, 0xd3, 0xe4, 0x93, 0x02, 0x20, + 0x3a, 0x01, 0x2a, 0x22, 0x1b, 0x2f, 0x61, 0x70, 0x69, 0x2f, 0x76, 0x30, 0x2f, 0x73, 0x65, 0x61, + 0x72, 0x63, 0x68, 0x2f, 0x69, 0x6e, 0x64, 0x65, 0x78, 0x2f, 0x73, 0x65, 0x61, 0x72, 0x63, 0x68, + 0x42, 0xf2, 0x02, 0x5a, 0x4a, 0x67, 0x69, 0x74, 0x68, 0x75, 0x62, 0x2e, 0x63, 0x6f, 0x6d, 0x2f, + 0x6f, 0x70, 0x65, 0x6e, 0x63, 0x6c, 0x6f, 0x75, 0x64, 0x2d, 0x65, 0x75, 0x2f, 0x6f, 0x70, 0x65, + 0x6e, 0x63, 0x6c, 0x6f, 0x75, 0x64, 0x2f, 0x70, 0x72, 0x6f, 0x74, 0x6f, 0x67, 0x65, 0x6e, 0x2f, + 0x67, 0x65, 0x6e, 0x2f, 0x6f, 0x70, 0x65, 0x6e, 0x63, 0x6c, 0x6f, 0x75, 0x64, 0x2f, 0x73, 0x65, + 0x72, 0x76, 0x69, 0x63, 0x65, 0x2f, 0x73, 0x65, 0x61, 0x72, 0x63, 0x68, 0x2f, 0x76, 0x30, 0x92, + 0x41, 0xa2, 0x02, 0x12, 0xb7, 0x01, 0x0a, 0x10, 0x4f, 0x70, 0x65, 0x6e, 0x43, 0x6c, 0x6f, 0x75, + 0x64, 0x20, 0x73, 0x65, 0x61, 0x72, 0x63, 0x68, 0x22, 0x51, 0x0a, 0x0e, 0x4f, 0x70, 0x65, 0x6e, + 0x43, 0x6c, 0x6f, 0x75, 0x64, 0x20, 0x47, 0x6d, 0x62, 0x48, 0x12, 0x29, 0x68, 0x74, 0x74, 0x70, + 0x73, 0x3a, 0x2f, 0x2f, 0x67, 0x69, 0x74, 0x68, 0x75, 0x62, 0x2e, 0x63, 0x6f, 0x6d, 0x2f, 0x6f, + 0x70, 0x65, 0x6e, 0x63, 0x6c, 0x6f, 0x75, 0x64, 0x2d, 0x65, 0x75, 0x2f, 0x6f, 0x70, 0x65, 0x6e, + 0x63, 0x6c, 0x6f, 0x75, 0x64, 0x1a, 0x14, 0x73, 0x75, 0x70, 0x70, 0x6f, 0x72, 0x74, 0x40, 0x6f, + 0x70, 0x65, 0x6e, 0x63, 0x6c, 0x6f, 0x75, 0x64, 0x2e, 0x65, 0x75, 0x2a, 0x49, 0x0a, 0x0a, 0x41, + 0x70, 0x61, 0x63, 0x68, 0x65, 0x2d, 0x32, 0x2e, 0x30, 0x12, 0x3b, 0x68, 0x74, 0x74, 0x70, 0x73, + 0x3a, 0x2f, 0x2f, 0x67, 0x69, 0x74, 0x68, 0x75, 0x62, 0x2e, 0x63, 0x6f, 0x6d, 0x2f, 0x6f, 0x70, + 0x65, 0x6e, 0x63, 0x6c, 0x6f, 0x75, 0x64, 0x2d, 0x65, 0x75, 0x2f, 0x6f, 0x70, 0x65, 0x6e, 0x63, + 0x6c, 0x6f, 0x75, 0x64, 0x2f, 0x62, 0x6c, 0x6f, 0x62, 0x2f, 0x6d, 0x61, 0x69, 0x6e, 0x2f, 0x4c, + 0x49, 0x43, 0x45, 0x4e, 0x53, 0x45, 0x32, 0x05, 0x31, 0x2e, 0x30, 0x2e, 0x30, 0x2a, 0x02, 0x01, + 0x02, 0x32, 0x10, 0x61, 0x70, 0x70, 0x6c, 0x69, 0x63, 0x61, 0x74, 0x69, 0x6f, 0x6e, 0x2f, 0x6a, + 0x73, 0x6f, 0x6e, 0x3a, 0x10, 0x61, 0x70, 0x70, 0x6c, 0x69, 0x63, 0x61, 0x74, 0x69, 0x6f, 0x6e, + 0x2f, 0x6a, 0x73, 0x6f, 0x6e, 0x72, 0x3e, 0x0a, 0x10, 0x44, 0x65, 0x76, 0x65, 0x6c, 0x6f, 0x70, + 0x65, 0x72, 0x20, 0x4d, 0x61, 0x6e, 0x75, 0x61, 0x6c, 0x12, 0x2a, 0x68, 0x74, 0x74, 0x70, 0x73, + 0x3a, 0x2f, 0x2f, 0x64, 0x6f, 0x63, 0x73, 0x2e, 0x6f, 0x70, 0x65, 0x6e, 0x63, 0x6c, 0x6f, 0x75, + 0x64, 0x2e, 0x65, 0x75, 0x2f, 0x73, 0x65, 0x72, 0x76, 0x69, 0x63, 0x65, 0x73, 0x2f, 0x73, 0x65, + 0x61, 0x72, 0x63, 0x68, 0x2f, 0x62, 0x06, 0x70, 0x72, 0x6f, 0x74, 0x6f, 0x33, } var ( @@ -689,7 +792,7 @@ func file_opencloud_services_search_v0_search_proto_rawDescGZIP() []byte { } var file_opencloud_services_search_v0_search_proto_enumTypes = make([]protoimpl.EnumInfo, 1) -var file_opencloud_services_search_v0_search_proto_msgTypes = make([]protoimpl.MessageInfo, 6) +var file_opencloud_services_search_v0_search_proto_msgTypes = make([]protoimpl.MessageInfo, 8) var file_opencloud_services_search_v0_search_proto_goTypes = []interface{}{ (IndexSpaceResponse_Status)(0), // 0: opencloud.services.search.v0.IndexSpaceResponse.Status (*SearchRequest)(nil), // 1: opencloud.services.search.v0.SearchRequest @@ -698,28 +801,33 @@ var file_opencloud_services_search_v0_search_proto_goTypes = []interface{}{ (*SearchIndexResponse)(nil), // 4: opencloud.services.search.v0.SearchIndexResponse (*IndexSpaceRequest)(nil), // 5: opencloud.services.search.v0.IndexSpaceRequest (*IndexSpaceResponse)(nil), // 6: opencloud.services.search.v0.IndexSpaceResponse - (*v0.Reference)(nil), // 7: opencloud.messages.search.v0.Reference - (*v0.Match)(nil), // 8: opencloud.messages.search.v0.Match - (*durationpb.Duration)(nil), // 9: google.protobuf.Duration + (*OptimizeIndexRequest)(nil), // 7: opencloud.services.search.v0.OptimizeIndexRequest + (*OptimizeIndexResponse)(nil), // 8: opencloud.services.search.v0.OptimizeIndexResponse + (*v0.Reference)(nil), // 9: opencloud.messages.search.v0.Reference + (*v0.Match)(nil), // 10: opencloud.messages.search.v0.Match + (*durationpb.Duration)(nil), // 11: google.protobuf.Duration } var file_opencloud_services_search_v0_search_proto_depIdxs = []int32{ - 7, // 0: opencloud.services.search.v0.SearchRequest.ref:type_name -> opencloud.messages.search.v0.Reference - 8, // 1: opencloud.services.search.v0.SearchResponse.matches:type_name -> opencloud.messages.search.v0.Match - 7, // 2: opencloud.services.search.v0.SearchIndexRequest.ref:type_name -> opencloud.messages.search.v0.Reference - 8, // 3: opencloud.services.search.v0.SearchIndexResponse.matches:type_name -> opencloud.messages.search.v0.Match - 9, // 4: opencloud.services.search.v0.IndexSpaceResponse.space_duration:type_name -> google.protobuf.Duration - 0, // 5: opencloud.services.search.v0.IndexSpaceResponse.status:type_name -> opencloud.services.search.v0.IndexSpaceResponse.Status - 1, // 6: opencloud.services.search.v0.SearchProvider.Search:input_type -> opencloud.services.search.v0.SearchRequest - 5, // 7: opencloud.services.search.v0.SearchProvider.IndexSpace:input_type -> opencloud.services.search.v0.IndexSpaceRequest - 3, // 8: opencloud.services.search.v0.IndexProvider.Search:input_type -> opencloud.services.search.v0.SearchIndexRequest - 2, // 9: opencloud.services.search.v0.SearchProvider.Search:output_type -> opencloud.services.search.v0.SearchResponse - 6, // 10: opencloud.services.search.v0.SearchProvider.IndexSpace:output_type -> opencloud.services.search.v0.IndexSpaceResponse - 4, // 11: opencloud.services.search.v0.IndexProvider.Search:output_type -> opencloud.services.search.v0.SearchIndexResponse - 9, // [9:12] is the sub-list for method output_type - 6, // [6:9] is the sub-list for method input_type - 6, // [6:6] is the sub-list for extension type_name - 6, // [6:6] is the sub-list for extension extendee - 0, // [0:6] is the sub-list for field type_name + 9, // 0: opencloud.services.search.v0.SearchRequest.ref:type_name -> opencloud.messages.search.v0.Reference + 10, // 1: opencloud.services.search.v0.SearchResponse.matches:type_name -> opencloud.messages.search.v0.Match + 9, // 2: opencloud.services.search.v0.SearchIndexRequest.ref:type_name -> opencloud.messages.search.v0.Reference + 10, // 3: opencloud.services.search.v0.SearchIndexResponse.matches:type_name -> opencloud.messages.search.v0.Match + 11, // 4: opencloud.services.search.v0.IndexSpaceResponse.space_duration:type_name -> google.protobuf.Duration + 0, // 5: opencloud.services.search.v0.IndexSpaceResponse.status:type_name -> opencloud.services.search.v0.IndexSpaceResponse.Status + 11, // 6: opencloud.services.search.v0.OptimizeIndexResponse.duration:type_name -> google.protobuf.Duration + 1, // 7: opencloud.services.search.v0.SearchProvider.Search:input_type -> opencloud.services.search.v0.SearchRequest + 5, // 8: opencloud.services.search.v0.SearchProvider.IndexSpace:input_type -> opencloud.services.search.v0.IndexSpaceRequest + 7, // 9: opencloud.services.search.v0.SearchProvider.OptimizeIndex:input_type -> opencloud.services.search.v0.OptimizeIndexRequest + 3, // 10: opencloud.services.search.v0.IndexProvider.Search:input_type -> opencloud.services.search.v0.SearchIndexRequest + 2, // 11: opencloud.services.search.v0.SearchProvider.Search:output_type -> opencloud.services.search.v0.SearchResponse + 6, // 12: opencloud.services.search.v0.SearchProvider.IndexSpace:output_type -> opencloud.services.search.v0.IndexSpaceResponse + 8, // 13: opencloud.services.search.v0.SearchProvider.OptimizeIndex:output_type -> opencloud.services.search.v0.OptimizeIndexResponse + 4, // 14: opencloud.services.search.v0.IndexProvider.Search:output_type -> opencloud.services.search.v0.SearchIndexResponse + 11, // [11:15] is the sub-list for method output_type + 7, // [7:11] is the sub-list for method input_type + 7, // [7:7] is the sub-list for extension type_name + 7, // [7:7] is the sub-list for extension extendee + 0, // [0:7] is the sub-list for field type_name } func init() { file_opencloud_services_search_v0_search_proto_init() } @@ -800,6 +908,30 @@ func file_opencloud_services_search_v0_search_proto_init() { return nil } } + file_opencloud_services_search_v0_search_proto_msgTypes[6].Exporter = func(v interface{}, i int) interface{} { + switch v := v.(*OptimizeIndexRequest); i { + case 0: + return &v.state + case 1: + return &v.sizeCache + case 2: + return &v.unknownFields + default: + return nil + } + } + file_opencloud_services_search_v0_search_proto_msgTypes[7].Exporter = func(v interface{}, i int) interface{} { + switch v := v.(*OptimizeIndexResponse); i { + case 0: + return &v.state + case 1: + return &v.sizeCache + case 2: + return &v.unknownFields + default: + return nil + } + } } type x struct{} out := protoimpl.TypeBuilder{ @@ -807,7 +939,7 @@ func file_opencloud_services_search_v0_search_proto_init() { GoPackagePath: reflect.TypeOf(x{}).PkgPath(), RawDescriptor: file_opencloud_services_search_v0_search_proto_rawDesc, NumEnums: 1, - NumMessages: 6, + NumMessages: 8, NumExtensions: 0, NumServices: 2, }, diff --git a/protogen/gen/opencloud/services/search/v0/search.pb.micro.go b/protogen/gen/opencloud/services/search/v0/search.pb.micro.go index 15738fa76b..d487290c05 100644 --- a/protogen/gen/opencloud/services/search/v0/search.pb.micro.go +++ b/protogen/gen/opencloud/services/search/v0/search.pb.micro.go @@ -49,6 +49,12 @@ func NewSearchProviderEndpoints() []*api.Endpoint { Stream: true, Handler: "rpc", }, + { + Name: "SearchProvider.OptimizeIndex", + Path: []string{"/api/v0/search/optimize-index"}, + Method: []string{"POST"}, + Handler: "rpc", + }, } } @@ -59,6 +65,9 @@ type SearchProviderService interface { // IndexSpace (re)indexes one or all spaces. The response is streamed, sending // progress information after each space has been indexed. IndexSpace(ctx context.Context, in *IndexSpaceRequest, opts ...client.CallOption) (SearchProvider_IndexSpaceService, error) + // OptimizeIndex merges the segments of the index into one, without indexing + // anything again. It answers once the merge is done. + OptimizeIndex(ctx context.Context, in *OptimizeIndexRequest, opts ...client.CallOption) (*OptimizeIndexResponse, error) } type searchProviderService struct { @@ -137,6 +146,16 @@ func (x *searchProviderServiceIndexSpace) Recv() (*IndexSpaceResponse, error) { return m, nil } +func (c *searchProviderService) OptimizeIndex(ctx context.Context, in *OptimizeIndexRequest, opts ...client.CallOption) (*OptimizeIndexResponse, error) { + req := c.c.NewRequest(c.name, "SearchProvider.OptimizeIndex", in) + out := new(OptimizeIndexResponse) + err := c.c.Call(ctx, req, out, opts...) + if err != nil { + return nil, err + } + return out, nil +} + // Server API for SearchProvider service type SearchProviderHandler interface { @@ -144,12 +163,16 @@ type SearchProviderHandler interface { // IndexSpace (re)indexes one or all spaces. The response is streamed, sending // progress information after each space has been indexed. IndexSpace(context.Context, *IndexSpaceRequest, SearchProvider_IndexSpaceStream) error + // OptimizeIndex merges the segments of the index into one, without indexing + // anything again. It answers once the merge is done. + OptimizeIndex(context.Context, *OptimizeIndexRequest, *OptimizeIndexResponse) error } func RegisterSearchProviderHandler(s server.Server, hdlr SearchProviderHandler, opts ...server.HandlerOption) error { type searchProvider interface { Search(ctx context.Context, in *SearchRequest, out *SearchResponse) error IndexSpace(ctx context.Context, stream server.Stream) error + OptimizeIndex(ctx context.Context, in *OptimizeIndexRequest, out *OptimizeIndexResponse) error } type SearchProvider struct { searchProvider @@ -168,6 +191,12 @@ func RegisterSearchProviderHandler(s server.Server, hdlr SearchProviderHandler, Stream: true, Handler: "rpc", })) + opts = append(opts, api.WithEndpoint(&api.Endpoint{ + Name: "SearchProvider.OptimizeIndex", + Path: []string{"/api/v0/search/optimize-index"}, + Method: []string{"POST"}, + Handler: "rpc", + })) return s.Handle(s.NewHandler(&SearchProvider{h}, opts...)) } @@ -219,6 +248,10 @@ func (x *searchProviderIndexSpaceStream) Send(m *IndexSpaceResponse) error { return x.stream.Send(m) } +func (h *searchProviderHandler) OptimizeIndex(ctx context.Context, in *OptimizeIndexRequest, out *OptimizeIndexResponse) error { + return h.SearchProviderHandler.OptimizeIndex(ctx, in, out) +} + // Api Endpoints for IndexProvider service func NewIndexProviderEndpoints() []*api.Endpoint { diff --git a/protogen/gen/opencloud/services/search/v0/search.swagger.json b/protogen/gen/opencloud/services/search/v0/search.swagger.json index db24bd32b5..3a4ff75278 100644 --- a/protogen/gen/opencloud/services/search/v0/search.swagger.json +++ b/protogen/gen/opencloud/services/search/v0/search.swagger.json @@ -106,6 +106,39 @@ ] } }, + "/api/v0/search/optimize-index": { + "post": { + "summary": "OptimizeIndex merges the segments of the index into one, without indexing\nanything again. It answers once the merge is done.", + "operationId": "SearchProvider_OptimizeIndex", + "responses": { + "200": { + "description": "A successful response.", + "schema": { + "$ref": "#/definitions/v0OptimizeIndexResponse" + } + }, + "default": { + "description": "An unexpected error response.", + "schema": { + "$ref": "#/definitions/googlerpcStatus" + } + } + }, + "parameters": [ + { + "name": "body", + "in": "body", + "required": true, + "schema": { + "$ref": "#/definitions/v0OptimizeIndexRequest" + } + } + ], + "tags": [ + "SearchProvider" + ] + } + }, "/api/v0/search/search": { "post": { "operationId": "SearchProvider_Search", @@ -454,6 +487,18 @@ } } }, + "v0OptimizeIndexRequest": { + "type": "object" + }, + "v0OptimizeIndexResponse": { + "type": "object", + "properties": { + "duration": { + "type": "string", + "description": "The duration it took to optimize the index." + } + } + }, "v0Photo": { "type": "object", "properties": { diff --git a/protogen/gen/opencloud/services/search/v0/search_grpc.pb.go b/protogen/gen/opencloud/services/search/v0/search_grpc.pb.go index 417a235f5b..7148858dd7 100644 --- a/protogen/gen/opencloud/services/search/v0/search_grpc.pb.go +++ b/protogen/gen/opencloud/services/search/v0/search_grpc.pb.go @@ -19,8 +19,9 @@ import ( const _ = grpc.SupportPackageIsVersion9 const ( - SearchProvider_Search_FullMethodName = "/opencloud.services.search.v0.SearchProvider/Search" - SearchProvider_IndexSpace_FullMethodName = "/opencloud.services.search.v0.SearchProvider/IndexSpace" + SearchProvider_Search_FullMethodName = "/opencloud.services.search.v0.SearchProvider/Search" + SearchProvider_IndexSpace_FullMethodName = "/opencloud.services.search.v0.SearchProvider/IndexSpace" + SearchProvider_OptimizeIndex_FullMethodName = "/opencloud.services.search.v0.SearchProvider/OptimizeIndex" ) // SearchProviderClient is the client API for SearchProvider service. @@ -31,6 +32,9 @@ type SearchProviderClient interface { // IndexSpace (re)indexes one or all spaces. The response is streamed, sending // progress information after each space has been indexed. IndexSpace(ctx context.Context, in *IndexSpaceRequest, opts ...grpc.CallOption) (grpc.ServerStreamingClient[IndexSpaceResponse], error) + // OptimizeIndex merges the segments of the index into one, without indexing + // anything again. It answers once the merge is done. + OptimizeIndex(ctx context.Context, in *OptimizeIndexRequest, opts ...grpc.CallOption) (*OptimizeIndexResponse, error) } type searchProviderClient struct { @@ -70,6 +74,16 @@ func (c *searchProviderClient) IndexSpace(ctx context.Context, in *IndexSpaceReq // This type alias is provided for backwards compatibility with existing code that references the prior non-generic stream type by name. type SearchProvider_IndexSpaceClient = grpc.ServerStreamingClient[IndexSpaceResponse] +func (c *searchProviderClient) OptimizeIndex(ctx context.Context, in *OptimizeIndexRequest, opts ...grpc.CallOption) (*OptimizeIndexResponse, error) { + cOpts := append([]grpc.CallOption{grpc.StaticMethod()}, opts...) + out := new(OptimizeIndexResponse) + err := c.cc.Invoke(ctx, SearchProvider_OptimizeIndex_FullMethodName, in, out, cOpts...) + if err != nil { + return nil, err + } + return out, nil +} + // SearchProviderServer is the server API for SearchProvider service. // All implementations must embed UnimplementedSearchProviderServer // for forward compatibility. @@ -78,6 +92,9 @@ type SearchProviderServer interface { // IndexSpace (re)indexes one or all spaces. The response is streamed, sending // progress information after each space has been indexed. IndexSpace(*IndexSpaceRequest, grpc.ServerStreamingServer[IndexSpaceResponse]) error + // OptimizeIndex merges the segments of the index into one, without indexing + // anything again. It answers once the merge is done. + OptimizeIndex(context.Context, *OptimizeIndexRequest) (*OptimizeIndexResponse, error) mustEmbedUnimplementedSearchProviderServer() } @@ -94,6 +111,9 @@ func (UnimplementedSearchProviderServer) Search(context.Context, *SearchRequest) func (UnimplementedSearchProviderServer) IndexSpace(*IndexSpaceRequest, grpc.ServerStreamingServer[IndexSpaceResponse]) error { return status.Error(codes.Unimplemented, "method IndexSpace not implemented") } +func (UnimplementedSearchProviderServer) OptimizeIndex(context.Context, *OptimizeIndexRequest) (*OptimizeIndexResponse, error) { + return nil, status.Error(codes.Unimplemented, "method OptimizeIndex not implemented") +} func (UnimplementedSearchProviderServer) mustEmbedUnimplementedSearchProviderServer() {} func (UnimplementedSearchProviderServer) testEmbeddedByValue() {} @@ -144,6 +164,24 @@ func _SearchProvider_IndexSpace_Handler(srv interface{}, stream grpc.ServerStrea // This type alias is provided for backwards compatibility with existing code that references the prior non-generic stream type by name. type SearchProvider_IndexSpaceServer = grpc.ServerStreamingServer[IndexSpaceResponse] +func _SearchProvider_OptimizeIndex_Handler(srv interface{}, ctx context.Context, dec func(interface{}) error, interceptor grpc.UnaryServerInterceptor) (interface{}, error) { + in := new(OptimizeIndexRequest) + if err := dec(in); err != nil { + return nil, err + } + if interceptor == nil { + return srv.(SearchProviderServer).OptimizeIndex(ctx, in) + } + info := &grpc.UnaryServerInfo{ + Server: srv, + FullMethod: SearchProvider_OptimizeIndex_FullMethodName, + } + handler := func(ctx context.Context, req interface{}) (interface{}, error) { + return srv.(SearchProviderServer).OptimizeIndex(ctx, req.(*OptimizeIndexRequest)) + } + return interceptor(ctx, in, info, handler) +} + // SearchProvider_ServiceDesc is the grpc.ServiceDesc for SearchProvider service. // It's only intended for direct use with grpc.RegisterService, // and not to be introspected or modified (even as a copy) @@ -155,6 +193,10 @@ var SearchProvider_ServiceDesc = grpc.ServiceDesc{ MethodName: "Search", Handler: _SearchProvider_Search_Handler, }, + { + MethodName: "OptimizeIndex", + Handler: _SearchProvider_OptimizeIndex_Handler, + }, }, Streams: []grpc.StreamDesc{ { diff --git a/protogen/proto/opencloud/services/search/v0/search.proto b/protogen/proto/opencloud/services/search/v0/search.proto index 806245d95e..030e6fbaa3 100644 --- a/protogen/proto/opencloud/services/search/v0/search.proto +++ b/protogen/proto/opencloud/services/search/v0/search.proto @@ -50,6 +50,14 @@ service SearchProvider { body: "*" }; } + // OptimizeIndex merges the segments of the index into one, without indexing + // anything again. It answers once the merge is done. + rpc OptimizeIndex(OptimizeIndexRequest) returns (OptimizeIndexResponse) { + option (google.api.http) = { + post: "/api/v0/search/optimize-index", + body: "*" + }; + } } service IndexProvider { @@ -136,3 +144,10 @@ message IndexSpaceResponse { // The outcome of indexing this particular space. Status status = 6; } + +message OptimizeIndexRequest {} + +message OptimizeIndexResponse { + // The duration it took to optimize the index. + google.protobuf.Duration duration = 1; +} diff --git a/services/search/README.md b/services/search/README.md index a9e023f24d..ed2d290f90 100644 --- a/services/search/README.md +++ b/services/search/README.md @@ -198,6 +198,18 @@ opencloud search index --all-spaces --force-rescan The command connects to the service's gRPC address (`SEARCH_GRPC_ADDR`) unless `--endpoint` is given, with the TLS mode set in `OC_GRPC_CLIENT_TLS_MODE`. With `on`, the server certificate is verified against that address, so if `SEARCH_GRPC_ADDR` is a bind address such as `0.0.0.0:9220`, pass `--endpoint` with a host name the certificate is valid for. +## Optimizing the Index + +Every write adds segments to the Bleve index, which the service merges in the background. After a large reindex, many segments can remain, and each search has to read all of them. The `optimize` command merges them into one without indexing anything again: + +```shell +opencloud search optimize +``` + +The index stays searchable and writable while it is optimized. The command returns once the merge is done. Pressing Ctrl+C stops the merge; if a background merge is running at that moment, the command stops once that one is done. A merge that fails, for example because the disk is full, is retried by Bleve until it succeeds, so the command does not return; Ctrl+C ends it. It takes the same `--endpoint` and `--insecure` flags as `index`. + +The command applies to Bleve only. OpenSearch merges its segments according to the merge policy of the cluster, so the command reports it as not supported there. + ## Metrics The search service exposes the following prometheus metrics at `/metrics` (as configured using the `SEARCH_DEBUG_ADDR` env var): diff --git a/services/search/pkg/bleve/backend.go b/services/search/pkg/bleve/backend.go index 5a42cc4e1d..73aa18fa59 100644 --- a/services/search/pkg/bleve/backend.go +++ b/services/search/pkg/bleve/backend.go @@ -2,11 +2,13 @@ package bleve import ( "context" + "fmt" "math" "sync" "time" "github.com/blevesearch/bleve/v2" + "github.com/blevesearch/bleve/v2/index/scorch" "github.com/blevesearch/bleve/v2/search/query" storageProvider "github.com/cs3org/go-cs3apis/cs3/storage/provider/v1beta1" "github.com/opencloud-eu/reva/v3/pkg/errtypes" @@ -32,6 +34,7 @@ type Backend struct { index *serialIndex queryCreator searchQuery.Creator[query.Query] log log.Logger + optimizeMu sync.Mutex } func NewBackend(index bleve.Index, queryCreator searchQuery.Creator[query.Query], log log.Logger) *Backend { @@ -209,6 +212,32 @@ func (b *Backend) DocCount() (uint64, error) { return b.index.DocCount() } +// Optimize merges all segments into one while the index stays in use +func (b *Backend) Optimize(ctx context.Context) error { + // scorch refuses a second merge with an error only its message tells apart + if !b.optimizeMu.TryLock() { + return search.ErrOptimizeInProgress + } + defer b.optimizeMu.Unlock() + + advanced, err := b.index.Advanced() + if err != nil { + return err + } + s, ok := advanced.(*scorch.Scorch) + if !ok { + return fmt.Errorf("%w: %T", search.ErrOptimizeUnsupported, advanced) + } + // a nil plan merges into a single segment. a failing merge is retried by + // scorch without end and never returns, only cancelling ctx ends the wait + err = s.ForceMerge(ctx, nil) + if err != nil { + return err + } + // scorch reports a cancelled merge as done, and so a closed index + return ctx.Err() +} + func (b *Backend) Upsert(id string, r search.Resource) error { batch, err := b.NewBatch(defaultBatchSize) if err != nil { diff --git a/services/search/pkg/bleve/export_test.go b/services/search/pkg/bleve/export_test.go index f3a694d9f8..bf0511848d 100644 --- a/services/search/pkg/bleve/export_test.go +++ b/services/search/pkg/bleve/export_test.go @@ -7,3 +7,9 @@ package bleve func SetMaxBytes(b *Batch, n uint64) { b.maxBytes = n } + +// LockOptimize holds the lock of a running optimization until the returned func is called +func LockOptimize(b *Backend) func() { + b.optimizeMu.Lock() + return b.optimizeMu.Unlock +} diff --git a/services/search/pkg/bleve/optimize_test.go b/services/search/pkg/bleve/optimize_test.go new file mode 100644 index 0000000000..1a80ffda55 --- /dev/null +++ b/services/search/pkg/bleve/optimize_test.go @@ -0,0 +1,85 @@ +package bleve_test + +import ( + "context" + "fmt" + "time" + + bleveSearch "github.com/blevesearch/bleve/v2" + . "github.com/onsi/ginkgo/v2" + . "github.com/onsi/gomega" + + "github.com/opencloud-eu/opencloud/pkg/log" + "github.com/opencloud-eu/opencloud/services/search/pkg/bleve" + "github.com/opencloud-eu/opencloud/services/search/pkg/content" + bleveQuery "github.com/opencloud-eu/opencloud/services/search/pkg/query/bleve" + "github.com/opencloud-eu/opencloud/services/search/pkg/search" +) + +var _ = Describe("Optimize", func() { + // segments reads the counters scorch keeps of the segments in its root + segments := func(idx bleveSearch.Index) (memory, file uint64) { + stats, ok := idx.StatsMap()["index"].(map[string]any) + Expect(ok).To(BeTrue()) + Expect(stats).To(HaveKeyWithValue("TotMemorySegmentsAtRoot", BeAssignableToTypeOf(uint64(0)))) + Expect(stats).To(HaveKeyWithValue("TotFileSegmentsAtRoot", BeAssignableToTypeOf(uint64(0)))) + return stats["TotMemorySegmentsAtRoot"].(uint64), stats["TotFileSegmentsAtRoot"].(uint64) + } + + // the persister and merger of scorch run in the background, a busy machine + // needs longer than the default second + const settle = 10 * time.Second + + newDiskBackend := func() (*bleve.Backend, bleveSearch.Index) { + idx, _, err := bleve.NewIndex(GinkgoT().TempDir(), log.NopLogger()) + Expect(err).ToNot(HaveOccurred()) + DeferCleanup(idx.Close) + return bleve.NewBackend(idx, bleveQuery.DefaultCreator, log.NopLogger()), idx + } + + It("merges the segments of the index into one and keeps the documents", func() { + eng, idx := newDiskBackend() + + const writes = 5 + for i := range writes { + id := fmt.Sprintf("1$2!%d", i) + Expect(eng.Upsert(id, search.Resource{ID: id, RootID: "1$2!2", Path: ".", Document: content.Document{Name: id}})).To(Succeed()) + // persisted one by one, scorch would otherwise merge the writes in memory + Eventually(func() uint64 { m, _ := segments(idx); return m }).WithTimeout(settle).Should(BeZero()) + } + _, before := segments(idx) + Expect(before).To(BeNumerically(">", 1)) + + Expect(eng.Optimize(context.Background())).To(Succeed()) + + Eventually(func() uint64 { _, f := segments(idx); return f }).WithTimeout(settle).Should(Equal(uint64(1))) + Expect(eng.DocCount()).To(Equal(uint64(writes))) + }) + + It("reports a context cancelled before the merge", func() { + eng, _ := newDiskBackend() + ctx, cancel := context.WithCancel(context.Background()) + cancel() + + Expect(eng.Optimize(ctx)).To(MatchError(context.Canceled)) + }) + + It("refuses a second optimization while one runs", func() { + eng, _ := newDiskBackend() + unlock := bleve.LockOptimize(eng) + defer unlock() + + Expect(eng.Optimize(context.Background())).To(MatchError(search.ErrOptimizeInProgress)) + }) + + It("reports an index that is not a scorch index as unsupported", func() { + mapping, err := bleve.NewMapping() + Expect(err).ToNot(HaveOccurred()) + idx, err := bleveSearch.NewMemOnly(mapping) + Expect(err).ToNot(HaveOccurred()) + DeferCleanup(idx.Close) + + eng := bleve.NewBackend(idx, bleveQuery.DefaultCreator, log.NopLogger()) + Expect(eng.Optimize(context.Background())).To(MatchError(search.ErrOptimizeUnsupported)) + }) +}) diff --git a/services/search/pkg/command/index.go b/services/search/pkg/command/index.go index 80af78692e..0ecec048a1 100644 --- a/services/search/pkg/command/index.go +++ b/services/search/pkg/command/index.go @@ -17,6 +17,7 @@ import ( "github.com/opencloud-eu/reva/v3/pkg/rgrpc/todo/pool" "github.com/spf13/cobra" + "google.golang.org/grpc" ) // Index is the entrypoint for the server command. @@ -32,8 +33,6 @@ func Index(cfg *config.Config) *cobra.Command { allSpacesFlag, _ := cmd.Flags().GetBool("all-spaces") spaceFlag, _ := cmd.Flags().GetString("space") forceRescanFlag, _ := cmd.Flags().GetBool("force-rescan") - endpointFlag, _ := cmd.Flags().GetString("endpoint") - insecureFlag, _ := cmd.Flags().GetBool("insecure") concurrencyFlag, _ := cmd.Flags().GetInt("concurrency") if spaceFlag == "" && !allSpacesFlag { @@ -43,30 +42,12 @@ func Index(cfg *config.Config) *cobra.Command { return fmt.Errorf("concurrency %d exceeds max allowed %d", concurrencyFlag, cfg.ReindexMaxConcurrency) } - if !cmd.Flags().Changed("endpoint") { - endpointFlag = cfg.GRPC.Addr - } - - tlsMode := pool.TLSOff - if !insecureFlag { - mode, err := pool.StringToTLSMode(cfg.GRPCClientTLS.Mode) - if err != nil { - return err - } - tlsMode = mode - } - - conn, err := pool.NewConn(endpointFlag, - pool.WithTLSMode(tlsMode), - pool.WithTLSCACert(cfg.GRPCClientTLS.CACert), - ) + c, conn, err := dialSearchProvider(cmd, cfg) if err != nil { - return fmt.Errorf("failed to dial %s: %w", endpointFlag, err) + return err } defer conn.Close() - c := searchsvc.NewSearchProviderClient(conn) - // Cancel the operation when the user presses Ctrl+C (SIGINT) or the // process receives SIGTERM. The cancellation propagates over the // gRPC stream so the server stops indexing. @@ -118,16 +99,7 @@ func Index(cfg *config.Config) *cobra.Command { false, "force a rescan of all files, even if they are already indexed. This will make the indexing process much slower, but ensures that the index is up-to-date using the current search service configuration.", ) - indexCmd.Flags().String( - "endpoint", - "", - "the address of the search service gRPC endpoint. Defaults to the service's configured gRPC address (SEARCH_GRPC_ADDR). With OC_GRPC_CLIENT_TLS_MODE=on the server certificate must be valid for this address.", - ) - indexCmd.Flags().Bool( - "insecure", - false, - "disable TLS for the gRPC connection.", - ) + addConnFlags(indexCmd) indexCmd.Flags().Int( "concurrency", 3, @@ -163,3 +135,46 @@ func printProgress(progress *searchsvc.IndexSpaceResponse) { width := len(strconv.FormatInt(progress.GetTotalSpaces(), 10)) fmt.Printf("[%*d/%d %-7s] %s\n", width, progress.GetIndexedSpaces(), progress.GetTotalSpaces(), status, msg) } + +// addConnFlags adds the flags dialSearchProvider reads +func addConnFlags(cmd *cobra.Command) { + cmd.Flags().String( + "endpoint", + "", + "the address of the search service gRPC endpoint. Defaults to the service's configured gRPC address (SEARCH_GRPC_ADDR). With OC_GRPC_CLIENT_TLS_MODE=on the server certificate must be valid for this address.", + ) + cmd.Flags().Bool( + "insecure", + false, + "disable TLS for the gRPC connection.", + ) +} + +// dialSearchProvider connects to the running search service, the caller closes +// the returned connection +func dialSearchProvider(cmd *cobra.Command, cfg *config.Config) (searchsvc.SearchProviderClient, *grpc.ClientConn, error) { + endpointFlag, _ := cmd.Flags().GetString("endpoint") + insecureFlag, _ := cmd.Flags().GetBool("insecure") + + if !cmd.Flags().Changed("endpoint") { + endpointFlag = cfg.GRPC.Addr + } + + tlsMode := pool.TLSOff + if !insecureFlag { + mode, err := pool.StringToTLSMode(cfg.GRPCClientTLS.Mode) + if err != nil { + return nil, nil, err + } + tlsMode = mode + } + + conn, err := pool.NewConn(endpointFlag, + pool.WithTLSMode(tlsMode), + pool.WithTLSCACert(cfg.GRPCClientTLS.CACert), + ) + if err != nil { + return nil, nil, fmt.Errorf("failed to dial %s: %w", endpointFlag, err) + } + return searchsvc.NewSearchProviderClient(conn), conn, nil +} diff --git a/services/search/pkg/command/index_test.go b/services/search/pkg/command/index_test.go index f34d3558ba..a9f9838921 100644 --- a/services/search/pkg/command/index_test.go +++ b/services/search/pkg/command/index_test.go @@ -1,12 +1,15 @@ package command_test import ( + "context" "net" + "time" . "github.com/onsi/ginkgo/v2" . "github.com/onsi/gomega" "google.golang.org/grpc" "google.golang.org/grpc/credentials/insecure" + "google.golang.org/protobuf/types/known/durationpb" "github.com/opencloud-eu/opencloud/pkg/shared" searchsvc "github.com/opencloud-eu/opencloud/protogen/gen/opencloud/services/search/v0" @@ -16,6 +19,14 @@ import ( type fakeSearchProvider struct { searchsvc.UnimplementedSearchProviderServer + optimizeErr error +} + +func (f fakeSearchProvider) OptimizeIndex(context.Context, *searchsvc.OptimizeIndexRequest) (*searchsvc.OptimizeIndexResponse, error) { + if f.optimizeErr != nil { + return nil, f.optimizeErr + } + return &searchsvc.OptimizeIndexResponse{Duration: durationpb.New(time.Second)}, nil } func (fakeSearchProvider) IndexSpace(_ *searchsvc.IndexSpaceRequest, stream grpc.ServerStreamingServer[searchsvc.IndexSpaceResponse]) error { diff --git a/services/search/pkg/command/optimize.go b/services/search/pkg/command/optimize.go new file mode 100644 index 0000000000..2e982794e1 --- /dev/null +++ b/services/search/pkg/command/optimize.go @@ -0,0 +1,58 @@ +package command + +import ( + "context" + "errors" + "fmt" + "os" + "os/signal" + "syscall" + + "github.com/spf13/cobra" + merrors "go-micro.dev/v4/errors" + "google.golang.org/grpc/status" + + "github.com/opencloud-eu/opencloud/pkg/config/configlog" + searchsvc "github.com/opencloud-eu/opencloud/protogen/gen/opencloud/services/search/v0" + "github.com/opencloud-eu/opencloud/services/search/pkg/config" + "github.com/opencloud-eu/opencloud/services/search/pkg/config/parser" +) + +// Optimize merges the segments of the search index of the running service. +func Optimize(cfg *config.Config) *cobra.Command { + optimizeCmd := &cobra.Command{ + Use: "optimize", + Short: "merge the segments of the search index into one, without indexing anything again", + PreRunE: func(*cobra.Command, []string) error { + return configlog.ReturnFatal(parser.ParseConfig(cfg)) + }, + RunE: func(cmd *cobra.Command, _ []string) error { + c, conn, err := dialSearchProvider(cmd, cfg) + if err != nil { + return err + } + defer conn.Close() + + // the cancellation reaches the server, which stops the merge + ctx, stop := signal.NotifyContext(context.Background(), os.Interrupt, syscall.SIGTERM) + defer stop() + + fmt.Println("optimizing the search index...") + res, err := c.OptimizeIndex(ctx, &searchsvc.OptimizeIndexRequest{}) + switch { + // a merge that finished before the signal counts as done + case err != nil && errors.Is(ctx.Err(), context.Canceled): + fmt.Println("aborted, the optimization has been stopped") + return nil + case err != nil: + // the service answers with a go-micro error, its json sits in the status message + return errors.New(merrors.Parse(status.Convert(err).Message()).GetDetail()) + } + fmt.Printf("optimized the search index in %s\n", res.GetDuration().AsDuration()) + return nil + }, + } + addConnFlags(optimizeCmd) + + return optimizeCmd +} diff --git a/services/search/pkg/command/optimize_test.go b/services/search/pkg/command/optimize_test.go new file mode 100644 index 0000000000..9c912b2895 --- /dev/null +++ b/services/search/pkg/command/optimize_test.go @@ -0,0 +1,50 @@ +package command_test + +import ( + "net" + "net/http" + + . "github.com/onsi/ginkgo/v2" + . "github.com/onsi/gomega" + merrors "go-micro.dev/v4/errors" + "google.golang.org/grpc" + "google.golang.org/grpc/codes" + "google.golang.org/grpc/credentials/insecure" + "google.golang.org/grpc/status" + + "github.com/opencloud-eu/opencloud/pkg/shared" + searchsvc "github.com/opencloud-eu/opencloud/protogen/gen/opencloud/services/search/v0" + "github.com/opencloud-eu/opencloud/services/search/pkg/command" + "github.com/opencloud-eu/opencloud/services/search/pkg/config" +) + +var _ = Describe("Optimize", func() { + // runOptimize runs the optimize command against a search service without + // TLS that answers with provider + runOptimize := func(provider fakeSearchProvider) error { + lis, err := net.Listen("tcp", "127.0.0.1:0") + Expect(err).ToNot(HaveOccurred()) + srv := grpc.NewServer(grpc.Creds(insecure.NewCredentials())) + searchsvc.RegisterSearchProviderServer(srv, provider) + go func() { _ = srv.Serve(lis) }() + DeferCleanup(srv.Stop) + + cfg := &config.Config{ + GRPC: config.GRPCConfig{Addr: lis.Addr().String()}, + GRPCClientTLS: &shared.GRPCClientTLS{}, + } + cmd := command.Optimize(cfg) + Expect(cmd.ParseFlags(nil)).To(Succeed()) + return cmd.RunE(cmd, nil) + } + + It("asks the service to optimize its index", func() { + Expect(runOptimize(fakeSearchProvider{})).To(Succeed()) + }) + + It("returns the detail of the go-micro error the service answers with", func() { + merr := merrors.New("eu.opencloud.api.search", "the search engine merges its index on its own", http.StatusNotImplemented) + err := runOptimize(fakeSearchProvider{optimizeErr: status.Error(codes.Unimplemented, merr.Error())}) + Expect(err).To(MatchError("the search engine merges its index on its own")) + }) +}) diff --git a/services/search/pkg/command/root.go b/services/search/pkg/command/root.go index ff158883f2..24f06d6b1b 100644 --- a/services/search/pkg/command/root.go +++ b/services/search/pkg/command/root.go @@ -17,6 +17,7 @@ func GetCommands(cfg *config.Config) []*cobra.Command { // interaction with this service Index(cfg), + Optimize(cfg), // infos about this service Health(cfg), diff --git a/services/search/pkg/opensearch/backend.go b/services/search/pkg/opensearch/backend.go index d5610a4483..1364eba59a 100644 --- a/services/search/pkg/opensearch/backend.go +++ b/services/search/pkg/opensearch/backend.go @@ -255,6 +255,12 @@ func (b *Backend) DocCount() (uint64, error) { return uint64(resp.Count), nil } +// Optimize leaves the segments to the merge policy of the cluster, operators +// that want a forced merge run it against OpenSearch directly +func (b *Backend) Optimize(context.Context) error { + return search.ErrOptimizeUnsupported +} + func (b *Backend) Upsert(id string, r search.Resource) error { batch, err := b.NewBatch(defaultBatchSize) if err != nil { diff --git a/services/search/pkg/search/mocks/engine.go b/services/search/pkg/search/mocks/engine.go index 6797500fc3..110c8d07e6 100644 --- a/services/search/pkg/search/mocks/engine.go +++ b/services/search/pkg/search/mocks/engine.go @@ -336,6 +336,57 @@ func (_c *Engine_NewBatch_Call) RunAndReturn(run func(batchSize int) (search.Bat return _c } +// Optimize provides a mock function for the type Engine +func (_mock *Engine) Optimize(ctx context.Context) error { + ret := _mock.Called(ctx) + + if len(ret) == 0 { + panic("no return value specified for Optimize") + } + + var r0 error + if returnFunc, ok := ret.Get(0).(func(context.Context) error); ok { + r0 = returnFunc(ctx) + } else { + r0 = ret.Error(0) + } + return r0 +} + +// Engine_Optimize_Call is a *mock.Call that shadows Run/Return methods with type explicit version for method 'Optimize' +type Engine_Optimize_Call struct { + *mock.Call +} + +// Optimize is a helper method to define mock.On call +// - ctx context.Context +func (_e *Engine_Expecter) Optimize(ctx any) *Engine_Optimize_Call { + return &Engine_Optimize_Call{Call: _e.mock.On("Optimize", ctx)} +} + +func (_c *Engine_Optimize_Call) Run(run func(ctx context.Context)) *Engine_Optimize_Call { + _c.Call.Run(func(args mock.Arguments) { + var arg0 context.Context + if args[0] != nil { + arg0 = args[0].(context.Context) + } + run( + arg0, + ) + }) + return _c +} + +func (_c *Engine_Optimize_Call) Return(err error) *Engine_Optimize_Call { + _c.Call.Return(err) + return _c +} + +func (_c *Engine_Optimize_Call) RunAndReturn(run func(ctx context.Context) error) *Engine_Optimize_Call { + _c.Call.Return(run) + return _c +} + // Purge provides a mock function for the type Engine func (_mock *Engine) Purge(id string, onlyDeleted bool) error { ret := _mock.Called(id, onlyDeleted) diff --git a/services/search/pkg/search/mocks/searcher.go b/services/search/pkg/search/mocks/searcher.go index 0cf6704769..5d21bf6269 100644 --- a/services/search/pkg/search/mocks/searcher.go +++ b/services/search/pkg/search/mocks/searcher.go @@ -136,6 +136,57 @@ func (_c *Searcher_MoveItem_Call) RunAndReturn(run func(ref *providerv1beta1.Ref return _c } +// OptimizeIndex provides a mock function for the type Searcher +func (_mock *Searcher) OptimizeIndex(ctx context.Context) error { + ret := _mock.Called(ctx) + + if len(ret) == 0 { + panic("no return value specified for OptimizeIndex") + } + + var r0 error + if returnFunc, ok := ret.Get(0).(func(context.Context) error); ok { + r0 = returnFunc(ctx) + } else { + r0 = ret.Error(0) + } + return r0 +} + +// Searcher_OptimizeIndex_Call is a *mock.Call that shadows Run/Return methods with type explicit version for method 'OptimizeIndex' +type Searcher_OptimizeIndex_Call struct { + *mock.Call +} + +// OptimizeIndex is a helper method to define mock.On call +// - ctx context.Context +func (_e *Searcher_Expecter) OptimizeIndex(ctx any) *Searcher_OptimizeIndex_Call { + return &Searcher_OptimizeIndex_Call{Call: _e.mock.On("OptimizeIndex", ctx)} +} + +func (_c *Searcher_OptimizeIndex_Call) Run(run func(ctx context.Context)) *Searcher_OptimizeIndex_Call { + _c.Call.Run(func(args mock.Arguments) { + var arg0 context.Context + if args[0] != nil { + arg0 = args[0].(context.Context) + } + run( + arg0, + ) + }) + return _c +} + +func (_c *Searcher_OptimizeIndex_Call) Return(err error) *Searcher_OptimizeIndex_Call { + _c.Call.Return(err) + return _c +} + +func (_c *Searcher_OptimizeIndex_Call) RunAndReturn(run func(ctx context.Context) error) *Searcher_OptimizeIndex_Call { + _c.Call.Return(run) + return _c +} + // PurgeDeleted provides a mock function for the type Searcher func (_mock *Searcher) PurgeDeleted(spaceID *providerv1beta1.StorageSpaceId) error { ret := _mock.Called(spaceID) diff --git a/services/search/pkg/search/search.go b/services/search/pkg/search/search.go index 90a9ac6d0e..a897aabf25 100644 --- a/services/search/pkg/search/search.go +++ b/services/search/pkg/search/search.go @@ -39,10 +39,20 @@ var scopeRegex = regexp.MustCompile(`(?:(?:AND|OR)\s+)?scope:\s*([^" "\n\r]*)(?: // ErrResourceNotFound is returned when the index holds no document with the requested id var ErrResourceNotFound = errors.New("resource not found") +// ErrOptimizeUnsupported is returned by an engine that leaves merging its +// segments to the engine itself +var ErrOptimizeUnsupported = errors.New("the search engine merges its index on its own") + +// ErrOptimizeInProgress is returned while another optimization of the index runs +var ErrOptimizeInProgress = errors.New("the index is being optimized already") + // Engine is the interface to the search engine type Engine interface { Search(ctx context.Context, req *searchService.SearchIndexRequest) (*searchService.SearchIndexResponse, error) DocCount() (uint64, error) + // Optimize merges the segments of the index, ErrOptimizeUnsupported if the + // engine merges on its own + Optimize(ctx context.Context) error Upsert(id string, r Resource) error Move(id string, parentID string, targetPath string) error diff --git a/services/search/pkg/search/service.go b/services/search/pkg/search/service.go index a8d409ef31..1b5de5ea46 100644 --- a/services/search/pkg/search/service.go +++ b/services/search/pkg/search/service.go @@ -60,6 +60,7 @@ type Searcher interface { IndexSpace(spaceID *provider.StorageSpaceId, forceRescan bool) error PurgeSpace(spaceID *provider.StorageSpaceId) error PurgeDeleted(spaceID *provider.StorageSpaceId) error + OptimizeIndex(ctx context.Context) error TrashItem(resourceID *provider.ResourceId) PurgeItem(ref *provider.Reference) @@ -696,6 +697,11 @@ func (s *Service) PurgeDeleted(spaceID *provider.StorageSpaceId) error { return nil } +// OptimizeIndex merges the segments of the index, the documents stay as they are +func (s *Service) OptimizeIndex(ctx context.Context) error { + return s.engine.Optimize(ctx) +} + // UpdateMetadata rewrites the tags and favorites of an indexed resource without // extracting its content again, a resource not indexed yet is upserted in full. func (s *Service) UpdateMetadata(ref *provider.Reference) { diff --git a/services/search/pkg/service/grpc/v0/service.go b/services/search/pkg/service/grpc/v0/service.go index 70b1ee694e..bdfc859bbe 100644 --- a/services/search/pkg/service/grpc/v0/service.go +++ b/services/search/pkg/service/grpc/v0/service.go @@ -4,6 +4,7 @@ import ( "context" "errors" "fmt" + "net/http" "sync" "time" @@ -248,6 +249,28 @@ func (s Service) IndexSpace(_ context.Context, in *searchsvc.IndexSpaceRequest, return g.Wait() } +// OptimizeIndex merges the segments of the index and answers once that is done +func (s Service) OptimizeIndex(ctx context.Context, _ *searchsvc.OptimizeIndexRequest, out *searchsvc.OptimizeIndexResponse) error { + t := time.Now() + err := s.searcher.OptimizeIndex(ctx) + switch { + case errors.Is(err, context.Canceled): + // the client hung up, nobody waits for the answer + return err + case errors.Is(err, search.ErrOptimizeUnsupported): + // the wrapped error names the go type of the index, which stays in the log + s.log.Info().Err(err).Msg("refused to optimize the index") + return merrors.New(s.id, search.ErrOptimizeUnsupported.Error(), http.StatusNotImplemented) + case errors.Is(err, search.ErrOptimizeInProgress): + return merrors.New(s.id, err.Error(), http.StatusPreconditionFailed) + case err != nil: + s.log.Error().Err(err).Msg("failed to optimize the index") + return merrors.InternalServerError(s.id, "failed to optimize the index") + } + out.Duration = durationpb.New(time.Since(t)) + return nil +} + // FromCache pulls a search result from cache func (s Service) FromCache(key string) (*searchsvc.SearchResponse, bool) { v, err := s.cache.Get(key) diff --git a/services/search/pkg/service/grpc/v0/service_suite_test.go b/services/search/pkg/service/grpc/v0/service_suite_test.go new file mode 100644 index 0000000000..b3d922b571 --- /dev/null +++ b/services/search/pkg/service/grpc/v0/service_suite_test.go @@ -0,0 +1,13 @@ +package service_test + +import ( + "testing" + + . "github.com/onsi/ginkgo/v2" + . "github.com/onsi/gomega" +) + +func TestService(t *testing.T) { + RegisterFailHandler(Fail) + RunSpecs(t, "Service Suite") +} diff --git a/services/search/pkg/service/grpc/v0/service_test.go b/services/search/pkg/service/grpc/v0/service_test.go new file mode 100644 index 0000000000..e17d491d07 --- /dev/null +++ b/services/search/pkg/service/grpc/v0/service_test.go @@ -0,0 +1,96 @@ +package service_test + +import ( + "bytes" + "context" + "errors" + "fmt" + "net/http" + + gateway "github.com/cs3org/go-cs3apis/cs3/gateway/v1beta1" + . "github.com/onsi/ginkgo/v2" + . "github.com/onsi/gomega" + "github.com/opencloud-eu/reva/v3/pkg/rgrpc/todo/pool" + "github.com/rs/zerolog" + "github.com/stretchr/testify/mock" + merrors "go-micro.dev/v4/errors" + "google.golang.org/grpc" + + "github.com/opencloud-eu/opencloud/pkg/log" + searchsvc "github.com/opencloud-eu/opencloud/protogen/gen/opencloud/services/search/v0" + "github.com/opencloud-eu/opencloud/services/search/pkg/config" + "github.com/opencloud-eu/opencloud/services/search/pkg/search" + "github.com/opencloud-eu/opencloud/services/search/pkg/search/mocks" + service "github.com/opencloud-eu/opencloud/services/search/pkg/service/grpc/v0" +) + +var _ = Describe("OptimizeIndex", func() { + var ( + searcher *mocks.Searcher + handler searchsvc.SearchProviderHandler + logs *bytes.Buffer + ) + + BeforeEach(func() { + searcher = mocks.NewSearcher(GinkgoT()) + logs = &bytes.Buffer{} + gws := pool.GetSelector[gateway.GatewayAPIClient]( + "GatewaySelector", + "eu.opencloud.api.gateway", + func(grpc.ClientConnInterface) gateway.GatewayAPIClient { return nil }, + ) + + var err error + handler, err = service.NewHandler( + service.Config(&config.Config{}), + service.Logger(log.Logger{Logger: zerolog.New(logs)}), + // the handler builds a token manager, which refuses an empty secret + service.JWTSecret("not-a-secret"), + service.GatewaySelector(gws), + service.Searcher(searcher), + ) + Expect(err).ToNot(HaveOccurred()) + }) + + It("reports how long the optimization took", func() { + searcher.EXPECT().OptimizeIndex(mock.Anything).Return(nil) + + out := &searchsvc.OptimizeIndexResponse{} + Expect(handler.OptimizeIndex(context.Background(), &searchsvc.OptimizeIndexRequest{}, out)).To(Succeed()) + Expect(out.GetDuration()).ToNot(BeNil()) + }) + + DescribeTable("maps the failure to a status", + func(cause error, code int32) { + searcher.EXPECT().OptimizeIndex(mock.Anything).Return(cause) + + err := handler.OptimizeIndex(context.Background(), &searchsvc.OptimizeIndexRequest{}, &searchsvc.OptimizeIndexResponse{}) + Expect(merrors.FromError(err).GetCode()).To(Equal(code)) + }, + Entry("an engine that merges on its own", search.ErrOptimizeUnsupported, int32(http.StatusNotImplemented)), + Entry("a merge already running", search.ErrOptimizeInProgress, int32(http.StatusPreconditionFailed)), + Entry("any other error", errors.New("disk full"), int32(http.StatusInternalServerError)), + ) + + It("keeps the cause of an unexpected error out of the response", func() { + searcher.EXPECT().OptimizeIndex(mock.Anything).Return(errors.New("/var/lib/index: disk full")) + + err := handler.OptimizeIndex(context.Background(), &searchsvc.OptimizeIndexRequest{}, &searchsvc.OptimizeIndexResponse{}) + Expect(merrors.FromError(err).GetDetail()).ToNot(ContainSubstring("disk full")) + }) + + It("keeps the index type out of the unsupported answer", func() { + searcher.EXPECT().OptimizeIndex(mock.Anything).Return(fmt.Errorf("%w: %s", search.ErrOptimizeUnsupported, "*upsidedown.UpsideDownCouch")) + + err := handler.OptimizeIndex(context.Background(), &searchsvc.OptimizeIndexRequest{}, &searchsvc.OptimizeIndexResponse{}) + Expect(merrors.FromError(err).GetDetail()).To(Equal(search.ErrOptimizeUnsupported.Error())) + }) + + It("logs no error when the client cancels", func() { + searcher.EXPECT().OptimizeIndex(mock.Anything).Return(context.Canceled) + + err := handler.OptimizeIndex(context.Background(), &searchsvc.OptimizeIndexRequest{}, &searchsvc.OptimizeIndexResponse{}) + Expect(err).To(MatchError(context.Canceled)) + Expect(logs.String()).ToNot(ContainSubstring(`"level":"error"`)) + }) +})