Spaces:
Running
Running
File size: 10,550 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 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 |
// _ _
// __ _____ __ ___ ___ __ _| |_ ___
// \ \ /\ / / _ \/ _` \ \ / / |/ _` | __/ _ \
// \ V V / __/ (_| |\ V /| | (_| | || __/
// \_/\_/ \___|\__,_| \_/ |_|\__,_|\__\___|
//
// Copyright © 2016 - 2024 Weaviate B.V. All rights reserved.
//
// CONTACT: [email protected]
//
package schema
import (
"context"
"encoding/json"
"fmt"
"testing"
"github.com/sirupsen/logrus"
"github.com/sirupsen/logrus/hooks/test"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
"github.com/weaviate/weaviate/entities/models"
"github.com/weaviate/weaviate/entities/schema"
ucs "github.com/weaviate/weaviate/usecases/schema"
"github.com/weaviate/weaviate/usecases/sharding"
)
func TestRepositoryMigrate(t *testing.T) {
var (
ctx = context.Background()
logger, _ = test.NewNullLogger()
dirName = t.TempDir()
canceledCtx, cancel = context.WithCancel(ctx)
)
cancel()
schema := ucs.NewState(3)
addClass(&schema, "C1", 0, 1, 0)
addClass(&schema, "C2", 0, 3, 3)
t.Run("SaveOldSchema", func(t *testing.T) {
repo, _ := newRepo(dirName, 0, logger)
defer repo.Close()
if err := repo.saveSchemaV1(schema); err != nil {
t.Fatalf("save all schema: %v", err)
}
})
t.Run("LoadOldchema", func(t *testing.T) {
repo, err := newRepo(dirName, -1, logger)
if err != nil {
t.Fatalf("create new repo %v", err)
}
defer repo.Close()
_, err = repo.Load(canceledCtx)
assert.ErrorIs(t, err, context.Canceled)
state, err := repo.Load(ctx)
assert.Nil(t, err)
assert.Equal(t, schema, state)
})
t.Run("LoadSchema", func(t *testing.T) {
repo, err := newRepo(dirName, -1, logger)
if err != nil {
t.Fatalf("create new repo %v", err)
}
defer repo.Close()
state, err := repo.Load(ctx)
assert.Nil(t, err)
assert.Equal(t, schema, state)
})
t.Run("LoadSchemaWithHigherVersion", func(t *testing.T) {
_, err := newRepo(dirName, 1, logger)
assert.NotNil(t, err)
})
}
func TestRepositorySaveLoad(t *testing.T) {
var (
ctx = context.Background()
canceledCtx, cancel = context.WithCancel(ctx)
logger, _ = test.NewNullLogger()
dirName = t.TempDir()
)
cancel()
repo, err := newRepo(dirName, -1, logger)
if err != nil {
t.Fatalf("create new repo: %v", err)
}
// load empty schema
res, err := repo.Load(ctx)
if err != nil {
t.Fatalf("loading schema from empty file: %v", err)
}
if len(res.ShardingState) != 0 || len(res.ObjectSchema.Classes) != 0 {
t.Fatalf("expected empty schema got %v", res)
}
// save and load non empty schema
schema := ucs.NewState(3)
addClass(&schema, "C1", 0, 1, 0)
addClass(&schema, "C2", 0, 3, 3)
err = repo.Save(canceledCtx, schema)
assert.ErrorIs(t, err, context.Canceled)
if err = repo.Save(ctx, schema); err != nil {
t.Fatalf("save schema: %v", err)
}
if err = repo.Save(ctx, schema); err != nil {
t.Fatalf("save schema: %v", err)
}
res, err = repo.Load(context.Background())
if err != nil {
t.Fatalf("load schema: %v", err)
}
assert.Equal(t, schema, res)
// delete class
deleteClass(&schema, "C2")
repo.DeleteClass(ctx, "C2") // second call to test impotency
if err := repo.DeleteClass(ctx, "C2"); err != nil {
t.Errorf("delete bucket: %v", err)
}
repo.asserEqualSchema(t, schema, "delete class")
}
func TestRepositoryUpdateClass(t *testing.T) {
var (
ctx = context.Background()
logger, _ = test.NewNullLogger()
dirName = t.TempDir()
)
repo, err := newRepo(dirName, -1, logger)
if err != nil {
t.Fatalf("create new repo: %v", err)
}
// save and load non empty schema
schema := ucs.NewState(3)
cls, ss := addClass(&schema, "C1", 0, 1, 0)
payload, err := ucs.CreateClassPayload(cls, ss)
assert.Nil(t, err)
if err := repo.NewClass(ctx, payload); err != nil {
t.Fatalf("create new class: %v", err)
}
if err := repo.NewClass(ctx, payload); err == nil {
t.Fatal("create new class: must fail since class already exits")
}
repo.asserEqualSchema(t, schema, "create class")
// update class
deleteClass(&schema, "C1")
cls, ss = addClass(&schema, "C1", 0, 2, 1)
payload, err = ucs.CreateClassPayload(cls, ss)
assert.Nil(t, err)
payload.Name = "C3"
if err := repo.UpdateClass(ctx, payload); err == nil {
t.Fatal("updating class by adding shards to non existing class must fail")
}
payload.Name = "C1"
if err := repo.UpdateClass(ctx, payload); err != nil {
t.Errorf("update class: %v", err)
}
repo.asserEqualSchema(t, schema, "update class")
// overwrite class
deleteClass(&schema, "C1")
cls, ss = addClass(&schema, "C1", 2, 2, 3)
payload, err = ucs.CreateClassPayload(cls, ss)
assert.Nil(t, err)
payload.ReplaceShards = true
if err := repo.UpdateClass(ctx, payload); err != nil {
t.Errorf("update class: %v", err)
}
repo.asserEqualSchema(t, schema, "overwrite class")
// delete class
deleteClass(&schema, "C1")
repo.DeleteClass(ctx, "C1") // second call to test impotency
if err := repo.DeleteClass(ctx, "C1"); err != nil {
t.Errorf("delete bucket: %v", err)
}
repo.asserEqualSchema(t, schema, "delete class")
}
func TestRepositoryUpdateShards(t *testing.T) {
var (
ctx = context.Background()
logger, _ = test.NewNullLogger()
dirName = t.TempDir()
)
repo, err := newRepo(dirName, -1, logger)
if err != nil {
t.Fatalf("create new repo: %v", err)
}
schema := ucs.NewState(2)
cls, ss := addClass(&schema, "C1", 0, 2, 1)
payload, err := ucs.CreateClassPayload(cls, ss)
assert.Nil(t, err)
if err := repo.NewClass(ctx, payload); err != nil {
t.Errorf("update class: %v", err)
}
repo.asserEqualSchema(t, schema, "update class")
// add two shards
deleteClass(&schema, "C1")
_, ss = addClass(&schema, "C1", 0, 2, 5)
shards := serializeShards(ss.Physical)
if err := repo.NewShards(ctx, "C1", shards); err != nil {
t.Fatalf("add new shards: %v", err)
}
if err := repo.NewShards(ctx, "C3", shards); err == nil {
t.Fatal("add new shards to a non existing class must fail")
}
repo.asserEqualSchema(t, schema, "adding new shards")
t.Run("fails updating non existent shards", func(t *testing.T) {
nonExistentShards := createShards(4, 2, models.TenantActivityStatusCOLD)
nonExistentShardPairs := serializeShards(nonExistentShards)
err := repo.UpdateShards(ctx, "C1", nonExistentShardPairs)
require.NotNil(t, err)
assert.ErrorContains(t, err, "shard not found")
})
existentShards := createShards(3, 2, models.TenantActivityStatusCOLD)
existentShardPairs := serializeShards(existentShards)
t.Run("fails updating shards of non existent class", func(t *testing.T) {
err := repo.UpdateShards(ctx, "ClassNonExistent", existentShardPairs)
require.NotNil(t, err)
assert.ErrorContains(t, err, "class not found")
})
t.Run("succeeds updating shards", func(t *testing.T) {
err := repo.UpdateShards(ctx, "C1", existentShardPairs)
require.Nil(t, err)
replaceShards(ss, existentShards)
repo.asserEqualSchema(t, schema, "update shards")
})
xset := removeShards(ss, []int{0, 3, 4})
if err := repo.DeleteShards(ctx, "C1", xset); err != nil {
t.Fatalf("delete shards: %v", err)
}
repo.asserEqualSchema(t, schema, "remove shards")
if err := repo.DeleteShards(ctx, "C3", xset); err != nil {
t.Fatalf("delete shards from unknown class: %v", err)
}
}
func createClass(name string, start, nProps, nShards int) (models.Class, sharding.State) {
cls := models.Class{Class: name}
for i := start; i < start+nProps; i++ {
prop := models.Property{
Name: fmt.Sprintf("property-%d", i),
DataType: schema.DataTypeText.PropString(),
Tokenization: models.PropertyTokenizationWhitespace,
}
cls.Properties = append(cls.Properties, &prop)
}
ss := sharding.State{IndexID: name}
ss.Physical = createShards(start, nShards, models.TenantActivityStatusHOT)
return cls, ss
}
func createShards(start, nShards int, activityStatus string) map[string]sharding.Physical {
if nShards < 1 {
return nil
}
shards := make(map[string]sharding.Physical, nShards)
for i := start; i < start+nShards; i++ {
name := fmt.Sprintf("shard-%d", i)
node := fmt.Sprintf("node-%d", i)
shards[name] = sharding.Physical{
Name: name,
BelongsToNodes: []string{node},
Status: activityStatus,
}
}
return shards
}
func replaceShards(ss *sharding.State, shards map[string]sharding.Physical) {
for name, shard := range shards {
ss.Physical[name] = shard
}
}
func removeShards(ss *sharding.State, shards []int) []string {
res := make([]string, len(shards))
for i, j := range shards {
name := fmt.Sprintf("shard-%d", j)
delete(ss.Physical, name)
res[i] = name
}
return res
}
func addClass(schema *ucs.State, name string, start, nProps, nShards int) (*models.Class, *sharding.State) {
cls, ss := createClass(name, start, nProps, nShards)
if schema.ObjectSchema == nil {
schema.ObjectSchema = &models.Schema{}
}
if schema.ShardingState == nil {
schema.ShardingState = make(map[string]*sharding.State)
}
schema.ObjectSchema.Classes = append(schema.ObjectSchema.Classes, &cls)
schema.ShardingState[name] = &ss
return &cls, &ss
}
func deleteClass(schema *ucs.State, name string) {
idx := -1
for i, cls := range schema.ObjectSchema.Classes {
if cls.Class == name {
idx = i
break
}
}
if idx == -1 {
return
}
schema.ObjectSchema.Classes = append(schema.ObjectSchema.Classes[:idx], schema.ObjectSchema.Classes[idx+1:]...)
delete(schema.ShardingState, name)
}
func (r *store) asserEqualSchema(t *testing.T, expected ucs.State, msg string) {
t.Helper()
actual, err := r.Load(context.Background())
if err != nil {
t.Fatalf("load schema: %s: %v", msg, err)
}
assert.Equal(t, expected, actual)
}
func serializeShards(shards map[string]sharding.Physical) []ucs.KeyValuePair {
xs := make([]ucs.KeyValuePair, 0, len(shards))
for k, v := range shards {
val, _ := json.Marshal(&v)
xs = append(xs, ucs.KeyValuePair{Key: k, Value: val})
}
return xs
}
func newRepo(homeDir string, version int, logger logrus.FieldLogger) (*store, error) {
r := NewStore(homeDir, logger)
if version > -1 {
r.version = version
}
return r, r.Open()
}
|