Spaces:
Running
Running
File size: 7,459 Bytes
b110593 |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 |
// _ _
// __ _____ __ ___ ___ __ _| |_ ___
// \ \ /\ / / _ \/ _` \ \ / / |/ _` | __/ _ \
// \ V V / __/ (_| |\ V /| | (_| | || __/
// \_/\_/ \___|\__,_| \_/ |_|\__,_|\__\___|
//
// Copyright © 2016 - 2024 Weaviate B.V. All rights reserved.
//
// CONTACT: [email protected]
//
package schema
import (
"context"
"fmt"
"reflect"
"github.com/pkg/errors"
"github.com/weaviate/weaviate/entities/models"
"github.com/weaviate/weaviate/entities/schema"
"github.com/weaviate/weaviate/usecases/replica"
"github.com/weaviate/weaviate/usecases/sharding"
)
func (m *Manager) UpdateClass(ctx context.Context, principal *models.Principal,
className string, updated *models.Class,
) error {
m.Lock()
defer m.Unlock()
err := m.Authorizer.Authorize(principal, "update", "schema/objects")
if err != nil {
return err
}
initial := m.getClassByName(className)
if initial == nil {
return ErrNotFound
}
mtEnabled, err := validateUpdatingMT(initial, updated)
if err != nil {
return err
}
// make sure unset optionals on 'updated' don't lead to an error, as all
// optionals would have been set with defaults on the initial already
m.setClassDefaults(updated)
if err := m.validateImmutableFields(initial, updated); err != nil {
return err
}
if err := m.parseVectorIndexConfig(ctx, updated); err != nil {
return err
}
if err := m.parseShardingConfig(ctx, updated); err != nil {
return err
}
if err := m.migrator.ValidateVectorIndexConfigUpdate(ctx,
initial.VectorIndexConfig.(schema.VectorIndexConfig),
updated.VectorIndexConfig.(schema.VectorIndexConfig)); err != nil {
return errors.Wrap(err, "vector index config")
}
if err := m.migrator.ValidateInvertedIndexConfigUpdate(ctx,
initial.InvertedIndexConfig, updated.InvertedIndexConfig); err != nil {
return errors.Wrap(err, "inverted index config")
}
if err := validateShardingConfig(initial, updated, mtEnabled, m.clusterState); err != nil {
return fmt.Errorf("validate sharding config: %w", err)
}
if err := replica.ValidateConfigUpdate(initial, updated, m.clusterState); err != nil {
return fmt.Errorf("replication config: %w", err)
}
updatedSharding := updated.ShardingConfig.(sharding.Config)
initialRF := initial.ReplicationConfig.Factor
updatedRF := updated.ReplicationConfig.Factor
var updatedState *sharding.State
if initialRF != updatedRF {
uss, err := m.scaleOut.Scale(ctx, className, updatedSharding, initialRF, updatedRF)
if err != nil {
return errors.Wrapf(err, "scale out from %d to %d replicas",
initialRF, updatedRF)
}
updatedState = uss
}
tx, err := m.cluster.BeginTransaction(ctx, UpdateClass,
UpdateClassPayload{className, updated, updatedState}, DefaultTxTTL)
if err != nil {
// possible causes for errors could be nodes down (we expect every node to
// the up for a schema transaction) or concurrent transactions from other
// nodes
return errors.Wrap(err, "open cluster-wide transaction")
}
if err := m.cluster.CommitWriteTransaction(ctx, tx); err != nil {
return errors.Wrap(err, "commit cluster-wide transaction")
}
return m.updateClassApplyChanges(ctx, className, updated, updatedState)
}
// validateUpdatingMT validates toggling MT and returns whether mt is enabled
func validateUpdatingMT(current, update *models.Class) (enabled bool, err error) {
enabled = schema.MultiTenancyEnabled(current)
if schema.MultiTenancyEnabled(update) != enabled {
if enabled {
err = fmt.Errorf("disabling multi-tenancy for an existing class is not supported")
} else {
err = fmt.Errorf("enabling multi-tenancy for an existing class is not supported")
}
}
return
}
func validateShardingConfig(current, update *models.Class, mtEnabled bool, cl clusterState) error {
if mtEnabled {
return nil
}
first, ok := current.ShardingConfig.(sharding.Config)
if !ok {
return fmt.Errorf("current config is not well-formed")
}
second, ok := update.ShardingConfig.(sharding.Config)
if !ok {
return fmt.Errorf("updated config is not well-formed")
}
if err := sharding.ValidateConfigUpdate(first, second, cl); err != nil {
return err
}
return nil
}
func (m *Manager) updateClassApplyChanges(ctx context.Context, className string,
updated *models.Class, updatedShardingState *sharding.State,
) error {
if updatedShardingState != nil {
// the sharding state caches the node name, we must therefore set this
// explicitly now.
updatedShardingState.SetLocalName(m.clusterState.LocalName())
}
if err := m.migrator.UpdateVectorIndexConfig(ctx,
className, updated.VectorIndexConfig.(schema.VectorIndexConfig)); err != nil {
return errors.Wrap(err, "vector index config")
}
if err := m.migrator.UpdateInvertedIndexConfig(ctx, className,
updated.InvertedIndexConfig); err != nil {
return errors.Wrap(err, "inverted index config")
}
if !m.schemaCache.classExist(className) {
return ErrNotFound
}
payload, err := CreateClassPayload(updated, updatedShardingState)
if err != nil {
return err
}
payload.ReplaceShards = updatedShardingState != nil
// can be improved by updating the diff
m.schemaCache.updateClass(updated, updatedShardingState)
m.logger.
WithField("action", "schema.update_class").
Debug("saving updated schema to configuration store")
// payload.Shards
if err := m.repo.UpdateClass(ctx, payload); err != nil {
return err
}
m.triggerSchemaUpdateCallbacks()
return nil
}
func (m *Manager) validateImmutableFields(initial, updated *models.Class) error {
immutableFields := []immutableText{
{
name: "class name",
accessor: func(c *models.Class) string { return c.Class },
},
{
name: "vectorizer",
accessor: func(c *models.Class) string { return c.Vectorizer },
},
{
name: "vector index type",
accessor: func(c *models.Class) string { return c.VectorIndexType },
},
}
for _, u := range immutableFields {
if err := m.validateImmutableTextField(u, initial, updated); err != nil {
return err
}
}
if !reflect.DeepEqual(initial.Properties, updated.Properties) {
return errors.Errorf(
"properties cannot be updated through updating the class. Use the add " +
"property feature (e.g. \"POST /v1/schema/{className}/properties\") " +
"to add additional properties")
}
if !reflect.DeepEqual(initial.ModuleConfig, updated.ModuleConfig) {
return errors.Errorf("module config is immutable")
}
return nil
}
type immutableText struct {
accessor func(c *models.Class) string
name string
}
func (m *Manager) validateImmutableTextField(u immutableText,
previous, next *models.Class,
) error {
oldField := u.accessor(previous)
newField := u.accessor(next)
if oldField != newField {
return errors.Errorf("%s is immutable: attempted change from %q to %q",
u.name, oldField, newField)
}
return nil
}
func (m *Manager) UpdateShardStatus(ctx context.Context, principal *models.Principal,
className, shardName, targetStatus string,
) error {
err := m.Authorizer.Authorize(principal, "update",
fmt.Sprintf("schema/%s/shards/%s", className, shardName))
if err != nil {
return err
}
return m.migrator.UpdateShardStatus(ctx, className, shardName, targetStatus)
}
|