| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
|
|
| package schema |
|
|
| import ( |
| "context" |
| "fmt" |
| "os" |
| "strings" |
|
|
| "github.com/pkg/errors" |
| "github.com/sirupsen/logrus" |
| command "github.com/weaviate/weaviate/cluster/proto/api" |
| clusterSchema "github.com/weaviate/weaviate/cluster/schema" |
| entcfg "github.com/weaviate/weaviate/entities/config" |
| "github.com/weaviate/weaviate/entities/models" |
| "github.com/weaviate/weaviate/entities/modulecapabilities" |
| "github.com/weaviate/weaviate/entities/schema" |
| schemaConfig "github.com/weaviate/weaviate/entities/schema/config" |
| "github.com/weaviate/weaviate/entities/versioned" |
| "github.com/weaviate/weaviate/usecases/auth/authorization" |
| "github.com/weaviate/weaviate/usecases/auth/authorization/filter" |
| "github.com/weaviate/weaviate/usecases/config" |
| "github.com/weaviate/weaviate/usecases/sharding" |
| ) |
|
|
| var ( |
| ErrNotFound = errors.New("not found") |
| ErrUnexpectedMultiple = errors.New("unexpected multiple results") |
| ) |
|
|
| |
| |
| |
| |
| |
| type SchemaManager interface { |
| |
| AddClass(ctx context.Context, cls *models.Class, ss *sharding.State) (uint64, error) |
| RestoreClass(ctx context.Context, cls *models.Class, ss *sharding.State) (uint64, error) |
| UpdateClass(ctx context.Context, cls *models.Class, ss *sharding.State) (uint64, error) |
| DeleteClass(ctx context.Context, name string) (uint64, error) |
| AddProperty(ctx context.Context, class string, p ...*models.Property) (uint64, error) |
| UpdateShardStatus(ctx context.Context, class, shard, status string) (uint64, error) |
| AddTenants(ctx context.Context, class string, req *command.AddTenantsRequest) (uint64, error) |
| UpdateTenants(ctx context.Context, class string, req *command.UpdateTenantsRequest) (uint64, error) |
| DeleteTenants(ctx context.Context, class string, req *command.DeleteTenantsRequest) (uint64, error) |
|
|
| |
| Join(_ context.Context, nodeID, raftAddr string, voter bool) error |
| Remove(_ context.Context, nodeID string) error |
| Stats() map[string]any |
| StorageCandidates() []string |
| StoreSchemaV1() error |
|
|
| |
| |
| QueryReadOnlyClasses(names ...string) (map[string]versioned.Class, error) |
| QuerySchema() (models.Schema, error) |
| QueryTenants(class string, tenants []string) ([]*models.Tenant, uint64, error) |
| QueryCollectionsCount() (int, error) |
| QueryShardOwner(class, shard string) (string, uint64, error) |
| QueryTenantsShards(class string, tenants ...string) (map[string]string, uint64, error) |
| QueryShardingState(class string) (*sharding.State, uint64, error) |
| QueryClassVersions(names ...string) (map[string]uint64, error) |
|
|
| |
| CreateAlias(ctx context.Context, alias string, class *models.Class) (uint64, error) |
| ReplaceAlias(ctx context.Context, alias *models.Alias, newClass *models.Class) (uint64, error) |
| DeleteAlias(ctx context.Context, alias string) (uint64, error) |
| GetAliases(ctx context.Context, alias string, class *models.Class) ([]*models.Alias, error) |
| GetAlias(ctx context.Context, alias string) (*models.Alias, error) |
| } |
|
|
| |
| type SchemaReader interface { |
| |
| WaitForUpdate(ctx context.Context, version uint64) error |
|
|
| |
| |
| |
| ClassEqual(name string) string |
| MultiTenancy(class string) models.MultiTenancyConfig |
| ClassInfo(class string) (ci clusterSchema.ClassInfo) |
| ReadOnlyClass(name string) *models.Class |
| ReadOnlyVersionedClass(name string) versioned.Class |
| ReadOnlySchema() models.Schema |
| Aliases() map[string]string |
| ShardReplicas(class, shard string) ([]string, error) |
| ShardFromUUID(class string, uuid []byte) string |
| ShardOwner(class, shard string) (string, error) |
| Read(class string, reader func(*models.Class, *sharding.State) error) error |
| Shards(class string) ([]string, error) |
| LocalShards(class string) ([]string, error) |
| GetShardsStatus(class, tenant string) (models.ShardStatusList, error) |
| ResolveAlias(alias string) string |
| GetAliasesForClass(class string) []*models.Alias |
|
|
| |
| |
| |
| ClassInfoWithVersion(ctx context.Context, class string, version uint64) (clusterSchema.ClassInfo, error) |
| MultiTenancyWithVersion(ctx context.Context, class string, version uint64) (models.MultiTenancyConfig, error) |
| ReadOnlyClassWithVersion(ctx context.Context, class string, version uint64) (*models.Class, error) |
| ShardOwnerWithVersion(ctx context.Context, lass, shard string, version uint64) (string, error) |
| ShardFromUUIDWithVersion(ctx context.Context, class string, uuid []byte, version uint64) (string, error) |
| ShardReplicasWithVersion(ctx context.Context, class, shard string, version uint64) ([]string, error) |
| TenantsShardsWithVersion(ctx context.Context, version uint64, class string, tenants ...string) (map[string]string, error) |
| } |
|
|
| type validator interface { |
| ValidateVectorIndexConfigUpdate(old, updated schemaConfig.VectorIndexConfig) error |
| ValidateInvertedIndexConfigUpdate(old, updated *models.InvertedIndexConfig) error |
| ValidateVectorIndexConfigsUpdate(old, updated map[string]schemaConfig.VectorIndexConfig) error |
| } |
|
|
| |
| |
| |
| |
| |
| type Handler struct { |
| schemaManager SchemaManager |
| schemaReader SchemaReader |
|
|
| cloud modulecapabilities.OffloadCloud |
|
|
| validator validator |
|
|
| logger logrus.FieldLogger |
| Authorizer authorization.Authorizer |
| schemaConfig *config.SchemaHandlerConfig |
| config config.Config |
| vectorizerValidator VectorizerValidator |
| moduleConfig ModuleConfig |
| clusterState clusterState |
| configParser VectorConfigParser |
| invertedConfigValidator InvertedConfigValidator |
| parser Parser |
| classGetter *ClassGetter |
|
|
| asyncIndexingEnabled bool |
| } |
|
|
| |
| func NewHandler( |
| schemaReader SchemaReader, |
| schemaManager SchemaManager, |
| validator validator, |
| logger logrus.FieldLogger, authorizer authorization.Authorizer, schemaConfig *config.SchemaHandlerConfig, |
| config config.Config, |
| configParser VectorConfigParser, vectorizerValidator VectorizerValidator, |
| invertedConfigValidator InvertedConfigValidator, |
| moduleConfig ModuleConfig, clusterState clusterState, |
| cloud modulecapabilities.OffloadCloud, |
| parser Parser, classGetter *ClassGetter, |
| ) (Handler, error) { |
| handler := Handler{ |
| config: config, |
| schemaConfig: schemaConfig, |
| schemaReader: schemaReader, |
| schemaManager: schemaManager, |
| parser: parser, |
| validator: validator, |
| logger: logger, |
| Authorizer: authorizer, |
| configParser: configParser, |
| vectorizerValidator: vectorizerValidator, |
| invertedConfigValidator: invertedConfigValidator, |
| moduleConfig: moduleConfig, |
| clusterState: clusterState, |
| cloud: cloud, |
| classGetter: classGetter, |
|
|
| asyncIndexingEnabled: entcfg.Enabled(os.Getenv("ASYNC_INDEXING")), |
| } |
| return handler, nil |
| } |
|
|
| |
| func (h *Handler) GetConsistentSchema(ctx context.Context, principal *models.Principal, consistency bool) (schema.Schema, error) { |
| var fullSchema schema.Schema |
| if !consistency { |
| fullSchema = h.getSchema() |
| } else { |
| consistentSchema, err := h.schemaManager.QuerySchema() |
| if err != nil { |
| return schema.Schema{}, fmt.Errorf("could not read schema with strong consistency: %w", err) |
| } |
| fullSchema = schema.Schema{ |
| Objects: &consistentSchema, |
| } |
| } |
|
|
| filteredClasses := filter.New[*models.Class](h.Authorizer, h.config.Authorization.Rbac).Filter( |
| ctx, |
| h.logger, |
| principal, |
| fullSchema.Objects.Classes, |
| authorization.READ, |
| func(class *models.Class) string { |
| return authorization.CollectionsMetadata(class.Class)[0] |
| }, |
| ) |
|
|
| return schema.Schema{ |
| Objects: &models.Schema{ |
| Classes: filteredClasses, |
| }, |
| }, nil |
| } |
|
|
| |
| |
| |
| func (h *Handler) GetSchemaSkipAuth() schema.Schema { return h.getSchema() } |
|
|
| func (h *Handler) getSchema() schema.Schema { |
| s := h.schemaReader.ReadOnlySchema() |
| return schema.Schema{ |
| Objects: &s, |
| } |
| } |
|
|
| func (h *Handler) Nodes() []string { |
| return h.clusterState.AllNames() |
| } |
|
|
| func (h *Handler) NodeName() string { |
| return h.clusterState.LocalName() |
| } |
|
|
| func (h *Handler) UpdateShardStatus(ctx context.Context, |
| principal *models.Principal, class, shard, status string, |
| ) (uint64, error) { |
| err := h.Authorizer.Authorize(ctx, principal, authorization.UPDATE, authorization.ShardsMetadata(class, shard)...) |
| if err != nil { |
| return 0, err |
| } |
|
|
| return h.schemaManager.UpdateShardStatus(ctx, class, shard, status) |
| } |
|
|
| func (h *Handler) ShardsStatus(ctx context.Context, |
| principal *models.Principal, class, shard string, |
| ) (models.ShardStatusList, error) { |
| err := h.Authorizer.Authorize(ctx, principal, authorization.READ, authorization.ShardsMetadata(class, shard)...) |
| if err != nil { |
| return nil, err |
| } |
|
|
| return h.schemaReader.GetShardsStatus(class, shard) |
| } |
|
|
| |
| |
| |
| |
| |
| func (h *Handler) JoinNode(ctx context.Context, node string, nodePort string, voter bool) error { |
| nodeAddr, ok := h.clusterState.NodeHostname(node) |
| if !ok { |
| return fmt.Errorf("could not resolve addr for node id %v", node) |
| } |
| nodeAddr = strings.Split(nodeAddr, ":")[0] |
|
|
| if nodePort == "" { |
| nodePort = fmt.Sprintf("%d", config.DefaultRaftPort) |
| } |
|
|
| if err := h.schemaManager.Join(ctx, node, nodeAddr+":"+nodePort, voter); err != nil { |
| return fmt.Errorf("node failed to join cluster: %w", err) |
| } |
| return nil |
| } |
|
|
| |
| func (h *Handler) RemoveNode(ctx context.Context, node string) error { |
| if err := h.schemaManager.Remove(ctx, node); err != nil { |
| return fmt.Errorf("node failed to leave cluster: %w", err) |
| } |
| return nil |
| } |
|
|
| |
| func (h *Handler) Statistics() map[string]any { |
| return h.schemaManager.Stats() |
| } |
|
|
| func (h *Handler) StoreSchemaV1() error { |
| return h.schemaManager.StoreSchemaV1() |
| } |
|
|