File size: 7,464 Bytes
95d599c | 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 | // _ _
// __ _____ __ ___ ___ __ _| |_ ___
// \ \ /\ / / _ \/ _` \ \ / / |/ _` | __/ _ \
// \ V V / __/ (_| |\ V /| | (_| | || __/
// \_/\_/ \___|\__,_| \_/ |_|\__,_|\__\___|
//
// Copyright © 2016 - 2025 Weaviate B.V. All rights reserved.
//
// CONTACT: hello@weaviate.io
//
package schema
import (
"context"
"github.com/cenkalti/backoff/v4"
"github.com/prometheus/client_golang/prometheus"
"github.com/weaviate/weaviate/cluster/types"
"github.com/weaviate/weaviate/cluster/utils"
"github.com/weaviate/weaviate/entities/models"
"github.com/weaviate/weaviate/entities/versioned"
"github.com/weaviate/weaviate/usecases/monitoring"
"github.com/weaviate/weaviate/usecases/sharding"
)
// SchemaReader is used for retrying schema queries. It is a thin wrapper around
// the original schema, separating retry logic from the actual operation.
// Retry may be needed due to eventual consistency issues where
// updates might take some time to arrive at the follower.
type SchemaReader struct {
schema *schema
versionedSchemaReader VersionedSchemaReader
}
func NewSchemaReader(sc *schema, vsr VersionedSchemaReader) SchemaReader {
return SchemaReader{
schema: sc,
versionedSchemaReader: vsr,
}
}
func NewSchemaReaderWithoutVersion(sc *schema) SchemaReader {
return SchemaReader{
schema: sc,
versionedSchemaReader: VersionedSchemaReader{
schema: sc,
WaitForUpdate: func(context.Context, uint64) error { return nil },
},
}
}
func (rs SchemaReader) States() map[string]types.ClassState {
t := prometheus.NewTimer(monitoring.GetMetrics().SchemaReadsLocal.WithLabelValues("States"))
defer t.ObserveDuration()
return rs.schema.States()
}
func (rs SchemaReader) ClassInfo(class string) (ci ClassInfo) {
t := prometheus.NewTimer(monitoring.GetMetrics().SchemaReadsLocal.WithLabelValues("ClassInfo"))
defer t.ObserveDuration()
res, _ := rs.ClassInfoWithVersion(context.TODO(), class, 0)
return res
}
// ClassEqual returns the name of an existing class with a similar name, and "" otherwise
// strings.EqualFold is used to compare classes
func (rs SchemaReader) ClassEqual(name string) string {
x, _ := rs.schema.ClassEqual(name)
return x
}
func (rs SchemaReader) MultiTenancy(class string) models.MultiTenancyConfig {
t := prometheus.NewTimer(monitoring.GetMetrics().SchemaReadsLocal.WithLabelValues("MultiTenancy"))
defer t.ObserveDuration()
res, _ := rs.MultiTenancyWithVersion(context.TODO(), class, 0)
return res
}
// Read performs a read operation `reader` on the specified class and sharding state
func (rs SchemaReader) Read(class string, reader func(*models.Class, *sharding.State) error) error {
t := prometheus.NewTimer(monitoring.GetMetrics().SchemaReadsLocal.WithLabelValues("Read"))
defer t.ObserveDuration()
return rs.retry(func(s *schema) error {
return s.Read(class, reader)
})
}
func (rs SchemaReader) Shards(class string) ([]string, error) {
var shards []string
err := rs.Read(class, func(class *models.Class, state *sharding.State) error {
shards = state.AllPhysicalShards()
return nil
})
return shards, err
}
func (rs SchemaReader) LocalShards(class string) ([]string, error) {
var shards []string
err := rs.Read(class, func(class *models.Class, state *sharding.State) error {
shards = state.AllLocalPhysicalShards()
return nil
})
return shards, err
}
// ReadOnlyClass returns a shallow copy of a class.
// The copy is read-only and should not be modified.
func (rs SchemaReader) ReadOnlyClass(class string) (cls *models.Class) {
t := prometheus.NewTimer(monitoring.GetMetrics().SchemaReadsLocal.WithLabelValues("ReadOnlyClass"))
defer t.ObserveDuration()
res, _ := rs.ReadOnlyClassWithVersion(context.TODO(), class, 0)
return res
}
func (rs SchemaReader) GetAliasesForClass(class string) []*models.Alias {
return rs.schema.GetAliasesForClass(class)
}
// ReadOnlyVersionedClass returns a shallow copy of a class along with its version.
// The copy is read-only and should not be modified.
func (rs SchemaReader) ReadOnlyVersionedClass(className string) versioned.Class {
class, version := rs.schema.ReadOnlyClass(className)
return versioned.Class{
Class: class,
Version: version,
}
}
func (rs SchemaReader) metaClass(class string) (meta *metaClass) {
rs.retry(func(s *schema) error {
if meta = s.metaClass(class); meta == nil {
return ErrClassNotFound
}
return nil
})
return
}
// ReadOnlySchema returns a read only schema
// Changing the schema outside this package might lead to undefined behavior.
//
// it creates a shallow copy of existing classes
//
// This function assumes that class attributes are being overwritten.
// The properties attribute is the only one that might vary in size;
// therefore, we perform a shallow copy of the existing properties.
// This implementation assumes that individual properties are overwritten rather than partially updated
func (rs SchemaReader) ReadOnlySchema() models.Schema {
t := prometheus.NewTimer(monitoring.GetMetrics().SchemaReadsLocal.WithLabelValues("ReadOnlySchema"))
defer t.ObserveDuration()
return rs.schema.ReadOnlySchema()
}
func (rs SchemaReader) ResolveAlias(alias string) string {
t := prometheus.NewTimer(monitoring.GetMetrics().SchemaReadsLocal.WithLabelValues("ResolveAlias"))
defer t.ObserveDuration()
return rs.schema.ResolveAlias(alias)
}
func (rs SchemaReader) Aliases() map[string]string {
return rs.schema.getAliases("", "")
}
// ShardOwner returns the node owner of the specified shard
func (rs SchemaReader) ShardOwner(class, shard string) (owner string, err error) {
t := prometheus.NewTimer(monitoring.GetMetrics().SchemaReadsLocal.WithLabelValues("ShardOwner"))
defer t.ObserveDuration()
res, err := rs.ShardOwnerWithVersion(context.TODO(), class, shard, 0)
return res, err
}
// ShardFromUUID returns shard name of the provided uuid
func (rs SchemaReader) ShardFromUUID(class string, uuid []byte) (shard string) {
t := prometheus.NewTimer(monitoring.GetMetrics().SchemaReadsLocal.WithLabelValues("ShardFromUUID"))
defer t.ObserveDuration()
res, _ := rs.ShardFromUUIDWithVersion(context.TODO(), class, uuid, 0)
return res
}
// ShardReplicas returns the replica nodes of a shard
func (rs SchemaReader) ShardReplicas(class, shard string) (nodes []string, err error) {
t := prometheus.NewTimer(monitoring.GetMetrics().SchemaReadsLocal.WithLabelValues("ShardReplicas"))
defer t.ObserveDuration()
res, err := rs.ShardReplicasWithVersion(context.TODO(), class, shard, 0)
return res, err
}
// TenantsShards returns shard name for the provided tenant and its activity status
func (rs SchemaReader) TenantsShards(class string, tenants ...string) (map[string]string, error) {
t := prometheus.NewTimer(monitoring.GetMetrics().SchemaReadsLocal.WithLabelValues("TenantsShards"))
defer t.ObserveDuration()
return rs.TenantsShardsWithVersion(context.TODO(), 0, class, tenants...)
}
func (rs SchemaReader) GetShardsStatus(class, tenant string) (models.ShardStatusList, error) {
t := prometheus.NewTimer(monitoring.GetMetrics().SchemaReadsLocal.WithLabelValues("GetShardsStatus"))
defer t.ObserveDuration()
return rs.schema.GetShardsStatus(class, tenant)
}
func (rs SchemaReader) Len() int { return rs.schema.len() }
func (rs SchemaReader) retry(f func(*schema) error) error {
return backoff.Retry(func() error {
return f(rs.schema)
}, utils.NewBackoff())
}
|