Spaces:
Running
Running
File size: 8,060 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 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 |
// _ _
// __ _____ __ ___ ___ __ _| |_ ___
// \ \ /\ / / _ \/ _` \ \ / / |/ _` | __/ _ \
// \ V V / __/ (_| |\ V /| | (_| | || __/
// \_/\_/ \___|\__,_| \_/ |_|\__,_|\__\___|
//
// Copyright © 2016 - 2024 Weaviate B.V. All rights reserved.
//
// CONTACT: [email protected]
//
package schema
import (
"context"
"fmt"
"time"
"github.com/weaviate/weaviate/entities/models"
"github.com/weaviate/weaviate/entities/schema"
"github.com/weaviate/weaviate/usecases/cluster"
"github.com/weaviate/weaviate/usecases/sharding"
)
func (m *Manager) repairSchema(ctx context.Context, remote *State) error {
m.logger.WithField("action", "repair_schema").
Info("Attempting to repair schema")
before := time.Now()
local := &m.schemaCache.State
if local.ObjectSchema == nil || remote.ObjectSchema == nil {
return fmt.Errorf("nil schema found, local: %v, remote: %v",
local.ObjectSchema, remote.ObjectSchema)
}
if err := (&clusterSyncRepairs{}).start(ctx, m, local, remote); err != nil {
return err
}
m.logger.WithField("action", "repair_schema").
Infof("Schema repair complete, took %s", time.Since(before))
return nil
}
func (m *Manager) repairLocalClasses(ctx context.Context, classes []*models.Class) error {
for _, class := range classes {
shardState, err := sharding.InitState(class.Class,
class.ShardingConfig.(sharding.Config),
m.clusterState, class.ReplicationConfig.Factor,
schema.MultiTenancyEnabled(class))
if err != nil {
return fmt.Errorf("init sharding state: %w", err)
}
if err = m.addClassApplyChanges(ctx, class, shardState); err != nil {
return fmt.Errorf("add class %q locally: %w", class.Class, err)
}
}
return nil
}
func (m *Manager) repairLocalProperties(properties map[string][]*models.Property) error {
for class, props := range properties {
for _, prop := range props {
_, err := m.schemaCache.addProperty(class, prop)
if err != nil {
return fmt.Errorf(`add prop "%s.%s" locally: %w`, class, prop.Name, err)
}
}
}
return nil
}
func (m *Manager) repairLocalTenants(tenantsByClass map[string][]sharding.Physical) {
m.schemaCache.LockGuard(func() {
for class, tenants := range tenantsByClass {
ss := m.schemaCache.State.ShardingState[class]
if ss.Physical == nil {
ss.Physical = make(map[string]sharding.Physical, len(tenants))
}
for _, tenant := range tenants {
ss.Physical[tenant.Name] = tenant
}
}
})
}
func (m *Manager) repairRemoteClasses(ctx context.Context, classes []*models.Class) error {
for _, class := range classes {
err := m.schemaCache.RLockGuard(func() error {
err := m.remoteRepairTx(ctx, RepairClass,
AddClassPayload{class, m.schemaCache.ShardingState[class.Class]})
if err != nil {
return fmt.Errorf("repair remote classes: %w", err)
}
return nil
})
if err != nil {
return err
}
}
return nil
}
func (m *Manager) repairRemoteProperties(ctx context.Context, properties map[string][]*models.Property) error {
for class, props := range properties {
for _, prop := range props {
err := m.remoteRepairTx(ctx, RepairProperty, AddPropertyPayload{class, prop})
if err != nil {
return fmt.Errorf("repair remote props: %w", err)
}
}
}
return nil
}
func (m *Manager) repairRemoteTenants(ctx context.Context, tenants map[string][]sharding.Physical) error {
for class, phys := range tenants {
request := AddTenantsPayload{
Class: class,
Tenants: make([]TenantCreate, 0, len(phys)),
}
for _, p := range phys {
request.Tenants = append(request.Tenants, TenantCreate{
Name: p.Name,
Nodes: p.BelongsToNodes,
Status: schema.ActivityStatus(models.TenantActivityStatusHOT),
})
}
err := m.remoteRepairTx(ctx, RepairTenant, request)
if err != nil {
return fmt.Errorf("repair remote tenants: %w", err)
}
}
return nil
}
func (m *Manager) remoteRepairTx(ctx context.Context,
txType cluster.TransactionType, payload interface{},
) error {
tx, err := m.cluster.BeginTransaction(ctx, txType, payload, DefaultTxTTL)
if err != nil {
return fmt.Errorf("open cluster-wide transaction: %w", err)
}
if err = m.cluster.CommitWriteTransaction(ctx, tx); err != nil {
return fmt.Errorf("not every node was able to commit: %w", err)
}
return nil
}
type repairSet struct {
classes []*models.Class
props map[string][]*models.Property
tenants map[string][]sharding.Physical
}
func newRepairSet() repairSet {
return repairSet{
props: make(map[string][]*models.Property),
tenants: make(map[string][]sharding.Physical),
}
}
type clusterSyncRepairs struct {
local, remote repairSet
}
func (repairs *clusterSyncRepairs) init(local, remote *State) {
local2Remote := comparisonUnit{
anomaly: newComparisonState(local),
comparator: newComparisonState(remote),
}
repairs.local = determineRepairsNeeded(local2Remote)
remote2Local := comparisonUnit{
anomaly: newComparisonState(remote),
comparator: newComparisonState(local),
}
repairs.remote = determineRepairsNeeded(remote2Local)
}
func (repairs *clusterSyncRepairs) start(ctx context.Context, m *Manager, local, remote *State) error {
repairs.init(local, remote)
if err := repairs.startLocal(ctx, m); err != nil {
return err
}
return repairs.startRemote(ctx, m)
}
func (repairs *clusterSyncRepairs) startLocal(ctx context.Context, m *Manager) error {
if err := m.repairLocalClasses(ctx, repairs.local.classes); err != nil {
return err
}
if err := m.repairLocalProperties(repairs.local.props); err != nil {
return err
}
m.repairLocalTenants(repairs.local.tenants)
return nil
}
func (repairs *clusterSyncRepairs) startRemote(ctx context.Context, m *Manager) error {
m.cluster.StartAcceptIncoming()
if err := m.repairRemoteClasses(ctx, repairs.remote.classes); err != nil {
return err
}
if err := m.repairRemoteProperties(ctx, repairs.remote.props); err != nil {
return err
}
return m.repairRemoteTenants(ctx, repairs.remote.tenants)
}
type comparisonState struct {
*State
classSet map[string]*models.Class
}
func newComparisonState(state *State) comparisonState {
return comparisonState{
State: state,
classSet: classSliceToMap(state.ObjectSchema.Classes),
}
}
type comparisonUnit struct {
anomaly, comparator comparisonState
}
func determineRepairsNeeded(states comparisonUnit) repairSet {
repairs := newRepairSet()
anomaly, comparator := states.anomaly, states.comparator
for className, comparatorClass := range comparator.classSet {
anomalyClass, found := anomaly.classSet[className]
if !found {
repairs.classes = append(repairs.classes, comparatorClass)
continue
}
if anomalyClass.MultiTenancyConfig != nil && anomalyClass.MultiTenancyConfig.Enabled {
anomalySS := anomaly.ShardingState[className]
comparatorSS := comparator.ShardingState[className]
for name, phys := range comparatorSS.Physical {
if anomalySS.Physical == nil {
repairs.tenants[className] = append(repairs.tenants[className], phys)
continue
}
_, found = anomalySS.Physical[name]
if !found {
repairs.tenants[className] = append(repairs.tenants[className], phys)
}
}
}
var propsToRepair []*models.Property
for _, comparatorProp := range comparatorClass.Properties {
var propFound bool
for _, anomalyProp := range anomalyClass.Properties {
if anomalyProp.Name == comparatorProp.Name {
propFound = true
break
}
}
if !propFound {
propsToRepair = append(propsToRepair, comparatorProp)
}
}
repairs.props[className] = propsToRepair
}
return repairs
}
func classSliceToMap(cs []*models.Class) map[string]*models.Class {
m := make(map[string]*models.Class, len(cs))
for _, c := range cs {
m[c.Class] = c
}
return m
}
|