github.com/weaviate/weaviate@v1.24.6/usecases/sharding/remote_index_incoming.go (about)

     1  //                           _       _
     2  // __      _____  __ ___   ___  __ _| |_ ___
     3  // \ \ /\ / / _ \/ _` \ \ / / |/ _` | __/ _ \
     4  //  \ V  V /  __/ (_| |\ V /| | (_| | ||  __/
     5  //   \_/\_/ \___|\__,_| \_/ |_|\__,_|\__\___|
     6  //
     7  //  Copyright © 2016 - 2024 Weaviate B.V. All rights reserved.
     8  //
     9  //  CONTACT: hello@weaviate.io
    10  //
    11  
    12  package sharding
    13  
    14  import (
    15  	"context"
    16  	"fmt"
    17  	"io"
    18  
    19  	"github.com/go-openapi/strfmt"
    20  	"github.com/pkg/errors"
    21  	"github.com/weaviate/weaviate/entities/additional"
    22  	"github.com/weaviate/weaviate/entities/aggregation"
    23  	"github.com/weaviate/weaviate/entities/filters"
    24  	"github.com/weaviate/weaviate/entities/schema"
    25  	"github.com/weaviate/weaviate/entities/search"
    26  	"github.com/weaviate/weaviate/entities/searchparams"
    27  	"github.com/weaviate/weaviate/entities/storobj"
    28  	"github.com/weaviate/weaviate/usecases/objects"
    29  	"github.com/weaviate/weaviate/usecases/replica"
    30  )
    31  
    32  type RemoteIncomingRepo interface {
    33  	GetIndexForIncoming(className schema.ClassName) RemoteIndexIncomingRepo
    34  }
    35  
    36  type RemoteIndexIncomingRepo interface {
    37  	IncomingPutObject(ctx context.Context, shardName string,
    38  		obj *storobj.Object) error
    39  	IncomingBatchPutObjects(ctx context.Context, shardName string,
    40  		objs []*storobj.Object) []error
    41  	IncomingBatchAddReferences(ctx context.Context, shardName string,
    42  		refs objects.BatchReferences) []error
    43  	IncomingGetObject(ctx context.Context, shardName string, id strfmt.UUID,
    44  		selectProperties search.SelectProperties,
    45  		additional additional.Properties) (*storobj.Object, error)
    46  	IncomingExists(ctx context.Context, shardName string,
    47  		id strfmt.UUID) (bool, error)
    48  	IncomingDeleteObject(ctx context.Context, shardName string,
    49  		id strfmt.UUID) error
    50  	IncomingMergeObject(ctx context.Context, shardName string,
    51  		mergeDoc objects.MergeDocument) error
    52  	IncomingMultiGetObjects(ctx context.Context, shardName string,
    53  		ids []strfmt.UUID) ([]*storobj.Object, error)
    54  	IncomingSearch(ctx context.Context, shardName string,
    55  		vector []float32, targetVector string, distance float32, limit int,
    56  		filters *filters.LocalFilter, keywordRanking *searchparams.KeywordRanking,
    57  		sort []filters.Sort, cursor *filters.Cursor, groupBy *searchparams.GroupBy,
    58  		additional additional.Properties,
    59  	) ([]*storobj.Object, []float32, error)
    60  	IncomingAggregate(ctx context.Context, shardName string,
    61  		params aggregation.Params) (*aggregation.Result, error)
    62  
    63  	IncomingFindUUIDs(ctx context.Context, shardName string,
    64  		filters *filters.LocalFilter) ([]strfmt.UUID, error)
    65  	IncomingDeleteObjectBatch(ctx context.Context, shardName string,
    66  		uuids []strfmt.UUID, dryRun bool) objects.BatchSimpleObjects
    67  	IncomingGetShardQueueSize(ctx context.Context, shardName string) (int64, error)
    68  	IncomingGetShardStatus(ctx context.Context, shardName string) (string, error)
    69  	IncomingUpdateShardStatus(ctx context.Context, shardName, targetStatus string) error
    70  	IncomingOverwriteObjects(ctx context.Context, shard string,
    71  		vobjects []*objects.VObject) ([]replica.RepairResponse, error)
    72  	IncomingDigestObjects(ctx context.Context, shardName string,
    73  		ids []strfmt.UUID) (result []replica.RepairResponse, err error)
    74  
    75  	// Scale-Out Replication POC
    76  	IncomingFilePutter(ctx context.Context, shardName,
    77  		filePath string) (io.WriteCloser, error)
    78  	IncomingCreateShard(ctx context.Context, className string, shardName string) error
    79  	IncomingReinitShard(ctx context.Context, shardName string) error
    80  }
    81  
    82  type RemoteIndexIncoming struct {
    83  	repo RemoteIncomingRepo
    84  }
    85  
    86  func NewRemoteIndexIncoming(repo RemoteIncomingRepo) *RemoteIndexIncoming {
    87  	return &RemoteIndexIncoming{
    88  		repo: repo,
    89  	}
    90  }
    91  
    92  func (rii *RemoteIndexIncoming) PutObject(ctx context.Context, indexName,
    93  	shardName string, obj *storobj.Object,
    94  ) error {
    95  	index := rii.repo.GetIndexForIncoming(schema.ClassName(indexName))
    96  	if index == nil {
    97  		return errors.Errorf("local index %q not found", indexName)
    98  	}
    99  
   100  	return index.IncomingPutObject(ctx, shardName, obj)
   101  }
   102  
   103  func (rii *RemoteIndexIncoming) BatchPutObjects(ctx context.Context, indexName,
   104  	shardName string, objs []*storobj.Object,
   105  ) []error {
   106  	index := rii.repo.GetIndexForIncoming(schema.ClassName(indexName))
   107  	if index == nil {
   108  		return duplicateErr(errors.Errorf("local index %q not found", indexName),
   109  			len(objs))
   110  	}
   111  
   112  	return index.IncomingBatchPutObjects(ctx, shardName, objs)
   113  }
   114  
   115  func (rii *RemoteIndexIncoming) BatchAddReferences(ctx context.Context, indexName,
   116  	shardName string, refs objects.BatchReferences,
   117  ) []error {
   118  	index := rii.repo.GetIndexForIncoming(schema.ClassName(indexName))
   119  	if index == nil {
   120  		return duplicateErr(errors.Errorf("local index %q not found", indexName),
   121  			len(refs))
   122  	}
   123  
   124  	return index.IncomingBatchAddReferences(ctx, shardName, refs)
   125  }
   126  
   127  func (rii *RemoteIndexIncoming) GetObject(ctx context.Context, indexName,
   128  	shardName string, id strfmt.UUID, selectProperties search.SelectProperties,
   129  	additional additional.Properties,
   130  ) (*storobj.Object, error) {
   131  	index := rii.repo.GetIndexForIncoming(schema.ClassName(indexName))
   132  	if index == nil {
   133  		return nil, errors.Errorf("local index %q not found", indexName)
   134  	}
   135  
   136  	return index.IncomingGetObject(ctx, shardName, id, selectProperties, additional)
   137  }
   138  
   139  func (rii *RemoteIndexIncoming) Exists(ctx context.Context, indexName,
   140  	shardName string, id strfmt.UUID,
   141  ) (bool, error) {
   142  	index := rii.repo.GetIndexForIncoming(schema.ClassName(indexName))
   143  	if index == nil {
   144  		return false, errors.Errorf("local index %q not found", indexName)
   145  	}
   146  
   147  	return index.IncomingExists(ctx, shardName, id)
   148  }
   149  
   150  func (rii *RemoteIndexIncoming) DeleteObject(ctx context.Context, indexName,
   151  	shardName string, id strfmt.UUID,
   152  ) error {
   153  	index := rii.repo.GetIndexForIncoming(schema.ClassName(indexName))
   154  	if index == nil {
   155  		return errors.Errorf("local index %q not found", indexName)
   156  	}
   157  
   158  	return index.IncomingDeleteObject(ctx, shardName, id)
   159  }
   160  
   161  func (rii *RemoteIndexIncoming) MergeObject(ctx context.Context, indexName,
   162  	shardName string, mergeDoc objects.MergeDocument,
   163  ) error {
   164  	index := rii.repo.GetIndexForIncoming(schema.ClassName(indexName))
   165  	if index == nil {
   166  		return errors.Errorf("local index %q not found", indexName)
   167  	}
   168  
   169  	return index.IncomingMergeObject(ctx, shardName, mergeDoc)
   170  }
   171  
   172  func (rii *RemoteIndexIncoming) MultiGetObjects(ctx context.Context, indexName,
   173  	shardName string, ids []strfmt.UUID,
   174  ) ([]*storobj.Object, error) {
   175  	index := rii.repo.GetIndexForIncoming(schema.ClassName(indexName))
   176  	if index == nil {
   177  		return nil, errors.Errorf("local index %q not found", indexName)
   178  	}
   179  
   180  	return index.IncomingMultiGetObjects(ctx, shardName, ids)
   181  }
   182  
   183  func (rii *RemoteIndexIncoming) Search(ctx context.Context, indexName, shardName string,
   184  	vector []float32, targetVector string, distance float32, limit int, filters *filters.LocalFilter,
   185  	keywordRanking *searchparams.KeywordRanking, sort []filters.Sort, cursor *filters.Cursor,
   186  	groupBy *searchparams.GroupBy, additional additional.Properties,
   187  ) ([]*storobj.Object, []float32, error) {
   188  	index := rii.repo.GetIndexForIncoming(schema.ClassName(indexName))
   189  	if index == nil {
   190  		return nil, nil, errors.Errorf("local index %q not found", indexName)
   191  	}
   192  
   193  	return index.IncomingSearch(
   194  		ctx, shardName, vector, targetVector, distance, limit, filters, keywordRanking, sort, cursor, groupBy, additional)
   195  }
   196  
   197  func (rii *RemoteIndexIncoming) Aggregate(ctx context.Context, indexName, shardName string,
   198  	params aggregation.Params,
   199  ) (*aggregation.Result, error) {
   200  	index := rii.repo.GetIndexForIncoming(schema.ClassName(indexName))
   201  	if index == nil {
   202  		return nil, errors.Errorf("local index %q not found", indexName)
   203  	}
   204  
   205  	return index.IncomingAggregate(ctx, shardName, params)
   206  }
   207  
   208  func (rii *RemoteIndexIncoming) FindUUIDs(ctx context.Context, indexName, shardName string,
   209  	filters *filters.LocalFilter,
   210  ) ([]strfmt.UUID, error) {
   211  	index := rii.repo.GetIndexForIncoming(schema.ClassName(indexName))
   212  	if index == nil {
   213  		return nil, errors.Errorf("local index %q not found", indexName)
   214  	}
   215  
   216  	return index.IncomingFindUUIDs(ctx, shardName, filters)
   217  }
   218  
   219  func (rii *RemoteIndexIncoming) DeleteObjectBatch(ctx context.Context, indexName, shardName string,
   220  	uuids []strfmt.UUID, dryRun bool,
   221  ) objects.BatchSimpleObjects {
   222  	index := rii.repo.GetIndexForIncoming(schema.ClassName(indexName))
   223  	if index == nil {
   224  		err := errors.Errorf("local index %q not found", indexName)
   225  		return objects.BatchSimpleObjects{objects.BatchSimpleObject{Err: err}}
   226  	}
   227  
   228  	return index.IncomingDeleteObjectBatch(ctx, shardName, uuids, dryRun)
   229  }
   230  
   231  func (rii *RemoteIndexIncoming) GetShardQueueSize(ctx context.Context,
   232  	indexName, shardName string,
   233  ) (int64, error) {
   234  	index := rii.repo.GetIndexForIncoming(schema.ClassName(indexName))
   235  	if index == nil {
   236  		return 0, errors.Errorf("local index %q not found", indexName)
   237  	}
   238  
   239  	return index.IncomingGetShardQueueSize(ctx, shardName)
   240  }
   241  
   242  func (rii *RemoteIndexIncoming) GetShardStatus(ctx context.Context,
   243  	indexName, shardName string,
   244  ) (string, error) {
   245  	index := rii.repo.GetIndexForIncoming(schema.ClassName(indexName))
   246  	if index == nil {
   247  		return "", errors.Errorf("local index %q not found", indexName)
   248  	}
   249  
   250  	return index.IncomingGetShardStatus(ctx, shardName)
   251  }
   252  
   253  func (rii *RemoteIndexIncoming) UpdateShardStatus(ctx context.Context,
   254  	indexName, shardName, targetStatus string,
   255  ) error {
   256  	index := rii.repo.GetIndexForIncoming(schema.ClassName(indexName))
   257  	if index == nil {
   258  		return errors.Errorf("local index %q not found", indexName)
   259  	}
   260  
   261  	return index.IncomingUpdateShardStatus(ctx, shardName, targetStatus)
   262  }
   263  
   264  func (rii *RemoteIndexIncoming) FilePutter(ctx context.Context,
   265  	indexName, shardName, filePath string,
   266  ) (io.WriteCloser, error) {
   267  	index := rii.repo.GetIndexForIncoming(schema.ClassName(indexName))
   268  	if index == nil {
   269  		return nil, errors.Errorf("local index %q not found", indexName)
   270  	}
   271  
   272  	return index.IncomingFilePutter(ctx, shardName, filePath)
   273  }
   274  
   275  func (rii *RemoteIndexIncoming) CreateShard(ctx context.Context,
   276  	indexName, shardName string,
   277  ) error {
   278  	index := rii.repo.GetIndexForIncoming(schema.ClassName(indexName))
   279  	if index == nil {
   280  		return errors.Errorf("local index %q not found", indexName)
   281  	}
   282  
   283  	return index.IncomingCreateShard(ctx, indexName, shardName)
   284  }
   285  
   286  func (rii *RemoteIndexIncoming) ReInitShard(ctx context.Context,
   287  	indexName, shardName string,
   288  ) error {
   289  	index := rii.repo.GetIndexForIncoming(schema.ClassName(indexName))
   290  	if index == nil {
   291  		return errors.Errorf("local index %q not found", indexName)
   292  	}
   293  
   294  	return index.IncomingReinitShard(ctx, shardName)
   295  }
   296  
   297  func (rii *RemoteIndexIncoming) OverwriteObjects(ctx context.Context,
   298  	indexName, shardName string, vobjects []*objects.VObject,
   299  ) ([]replica.RepairResponse, error) {
   300  	index := rii.repo.GetIndexForIncoming(schema.ClassName(indexName))
   301  	if index == nil {
   302  		return nil, fmt.Errorf("local index %q not found", indexName)
   303  	}
   304  
   305  	return index.IncomingOverwriteObjects(ctx, shardName, vobjects)
   306  }
   307  
   308  func (rii *RemoteIndexIncoming) DigestObjects(ctx context.Context,
   309  	indexName, shardName string, ids []strfmt.UUID,
   310  ) ([]replica.RepairResponse, error) {
   311  	index := rii.repo.GetIndexForIncoming(schema.ClassName(indexName))
   312  	if index == nil {
   313  		return nil, fmt.Errorf("local index %q not found", indexName)
   314  	}
   315  
   316  	return index.IncomingDigestObjects(ctx, shardName, ids)
   317  }