Spaces:
Running
Running
File size: 12,619 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 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 |
// _ _
// __ _____ __ ___ ___ __ _| |_ ___
// \ \ /\ / / _ \/ _` \ \ / / |/ _` | __/ _ \
// \ V V / __/ (_| |\ V /| | (_| | || __/
// \_/\_/ \___|\__,_| \_/ |_|\__,_|\__\___|
//
// Copyright © 2016 - 2024 Weaviate B.V. All rights reserved.
//
// CONTACT: [email protected]
//
//go:build integrationTest
// +build integrationTest
package db
import (
"context"
"testing"
"github.com/sirupsen/logrus"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
"github.com/weaviate/weaviate/adapters/repos/db/inverted"
"github.com/weaviate/weaviate/entities/additional"
"github.com/weaviate/weaviate/entities/dto"
"github.com/weaviate/weaviate/entities/filters"
"github.com/weaviate/weaviate/entities/models"
"github.com/weaviate/weaviate/entities/schema"
libschema "github.com/weaviate/weaviate/entities/schema"
"github.com/weaviate/weaviate/entities/search"
enthnsw "github.com/weaviate/weaviate/entities/vectorindex/hnsw"
)
// Updates are non trivial, because vector indices are built under the
// assumption that items are immutable (this is true for HNSW, the assumption
// is that this is generally true in the majority of cases). Therefore an
// update is essentially a delete and a new import with a new doc ID. This
// needs to be tested extensively because there's a lot of room for error
// regarding the clean up of Doc ID pointers in the inverted indices, etc.
func TestUpdateJourney(t *testing.T) {
dirName := t.TempDir()
logger := logrus.New()
schemaGetter := &fakeSchemaGetter{
schema: schema.Schema{Objects: &models.Schema{Classes: nil}},
shardState: singleShardState(),
}
repo, err := New(logger, Config{
MemtablesFlushIdleAfter: 60,
RootPath: dirName,
QueryMaximumResults: 10000,
MaxImportGoroutinesFactor: 1,
}, &fakeRemoteClient{}, &fakeNodeResolver{}, &fakeRemoteNodeClient{}, &fakeReplicationClient{}, nil)
require.Nil(t, err)
repo.SetSchemaGetter(schemaGetter)
require.Nil(t, repo.WaitForStartup(testCtx()))
defer repo.Shutdown(context.Background())
migrator := NewMigrator(repo, logger)
schema := libschema.Schema{
Objects: &models.Schema{
Classes: []*models.Class{updateTestClass()},
},
}
t.Run("add schema", func(t *testing.T) {
err := migrator.AddClass(context.Background(), updateTestClass(), schemaGetter.shardState)
require.Nil(t, err)
})
schemaGetter.schema = schema
t.Run("import some objects", func(t *testing.T) {
for _, res := range updateTestData() {
err := repo.PutObject(context.Background(), res.Object(), res.Vector, nil)
require.Nil(t, err)
}
tracker := getTracker(repo, "UpdateTestClass")
require.Nil(t, err)
sum, count, mean, err := tracker.PropertyTally("name")
require.Nil(t, err)
assert.Equal(t, 4, sum)
assert.Equal(t, 4, count)
assert.InEpsilon(t, 1, mean, 0.1)
})
searchVector := []float32{0.1, 0.1, 0.1}
t.Run("verify vector search results are initially as expected",
func(t *testing.T) {
res, err := repo.VectorSearch(context.Background(), dto.GetParams{
ClassName: "UpdateTestClass",
SearchVector: searchVector,
Pagination: &filters.Pagination{
Limit: 100,
},
})
expectedInAnyOrder := []interface{}{
"element-0", "element-1", "element-2", "element-3",
}
require.Nil(t, err)
require.Len(t, res, 4)
assert.ElementsMatch(t, expectedInAnyOrder, extractPropValues(res, "name"))
})
searchInv := func(t *testing.T, op filters.Operator, value int) []interface{} {
res, err := repo.ObjectSearch(context.Background(), 0, 100,
&filters.LocalFilter{
Root: &filters.Clause{
Operator: op,
On: &filters.Path{
Class: "UpdateTestClass",
Property: libschema.PropertyName("intProp"),
},
Value: &filters.Value{
Type: libschema.DataTypeInt,
Value: value,
},
},
}, nil, additional.Properties{}, "")
require.Nil(t, err)
return extractPropValues(res, "name")
}
t.Run("verify invert index results are initially as expected",
func(t *testing.T) {
expectedInAnyOrder := []interface{}{
"element-0", "element-1", "element-2", "element-3",
}
assert.ElementsMatch(t, expectedInAnyOrder, searchInv(t, filters.OperatorGreaterThanEqual, 0))
expectedInAnyOrder = []interface{}{"element-0"}
assert.ElementsMatch(t, expectedInAnyOrder, searchInv(t, filters.OperatorEqual, 0))
expectedInAnyOrder = []interface{}{"element-1"}
assert.ElementsMatch(t, expectedInAnyOrder, searchInv(t, filters.OperatorEqual, 10))
expectedInAnyOrder = []interface{}{"element-2"}
assert.ElementsMatch(t, expectedInAnyOrder, searchInv(t, filters.OperatorEqual, 20))
expectedInAnyOrder = []interface{}{"element-3"}
assert.ElementsMatch(t, expectedInAnyOrder, searchInv(t, filters.OperatorEqual, 30))
})
t.Run("update vector position of one item to move it into a different direction",
func(t *testing.T) {
// updating element-0 to be very far away from our search vector
updatedVec := []float32{-0.1, -0.12, -0.105}
id := updateTestData()[0].ID
old, err := repo.ObjectByID(context.Background(), id, search.SelectProperties{}, additional.Properties{}, "")
require.Nil(t, err)
err = repo.PutObject(context.Background(), old.Object(), updatedVec, nil)
require.Nil(t, err)
tracker := getTracker(repo, "UpdateTestClass")
require.Nil(t, err)
sum, count, mean, err := tracker.PropertyTally("name")
require.Nil(t, err)
assert.Equal(t, 4, sum)
assert.Equal(t, 4, count)
assert.InEpsilon(t, 1, mean, 0.1)
})
t.Run("verify new vector search results are as expected", func(t *testing.T) {
res, err := repo.VectorSearch(context.Background(), dto.GetParams{
ClassName: "UpdateTestClass",
SearchVector: searchVector,
Pagination: &filters.Pagination{
Limit: 100,
},
})
expectedInAnyOrder := []interface{}{
"element-0", "element-1", "element-2", "element-3",
}
require.Nil(t, err)
require.Len(t, res, 4)
assert.ElementsMatch(t, expectedInAnyOrder, extractPropValues(res, "name"))
})
t.Run("verify invert results still work properly", func(t *testing.T) {
expectedInAnyOrder := []interface{}{
"element-0", "element-1", "element-2", "element-3",
}
assert.ElementsMatch(t, expectedInAnyOrder, searchInv(t, filters.OperatorGreaterThanEqual, 0))
expectedInAnyOrder = []interface{}{"element-0"}
assert.ElementsMatch(t, expectedInAnyOrder, searchInv(t, filters.OperatorEqual, 0))
expectedInAnyOrder = []interface{}{"element-1"}
assert.ElementsMatch(t, expectedInAnyOrder, searchInv(t, filters.OperatorEqual, 10))
expectedInAnyOrder = []interface{}{"element-2"}
assert.ElementsMatch(t, expectedInAnyOrder, searchInv(t, filters.OperatorEqual, 20))
expectedInAnyOrder = []interface{}{"element-3"}
assert.ElementsMatch(t, expectedInAnyOrder, searchInv(t, filters.OperatorEqual, 30))
})
t.Run("update a second object and modify vector and invert props at the same time",
func(t *testing.T) {
// this time we are updating element-2 and move it away from the search
// vector, as well as updating an invert prop
updatedVec := []float32{-0.1, -0.12, -0.105123}
id := updateTestData()[2].ID
old, err := repo.ObjectByID(context.Background(), id, search.SelectProperties{}, additional.Properties{}, "")
require.Nil(t, err)
old.Schema.(map[string]interface{})["intProp"] = int64(21)
err = repo.PutObject(context.Background(), old.Object(), updatedVec, nil)
require.Nil(t, err)
tracker := getTracker(repo, "UpdateTestClass")
require.Nil(t, err)
sum, count, mean, err := tracker.PropertyTally("name")
require.Nil(t, err)
assert.Equal(t, 4, sum)
assert.Equal(t, 4, count)
assert.InEpsilon(t, 1, mean, 0.1)
})
t.Run("verify new vector search results are as expected", func(t *testing.T) {
res, err := repo.VectorSearch(context.Background(), dto.GetParams{
ClassName: "UpdateTestClass",
SearchVector: searchVector,
Pagination: &filters.Pagination{
Limit: 100,
},
})
expectedInAnyOrder := []interface{}{
"element-0", "element-1", "element-2", "element-3",
}
require.Nil(t, err)
require.Len(t, res, 4)
assert.ElementsMatch(t, expectedInAnyOrder, extractPropValues(res, "name"))
})
t.Run("verify invert results have been updated correctly", func(t *testing.T) {
expectedInAnyOrder := []interface{}{
"element-0", "element-1", "element-2", "element-3",
}
assert.ElementsMatch(t, expectedInAnyOrder, searchInv(t, filters.OperatorGreaterThanEqual, 0))
expectedInAnyOrder = []interface{}{"element-0"}
assert.ElementsMatch(t, expectedInAnyOrder, searchInv(t, filters.OperatorEqual, 0))
expectedInAnyOrder = []interface{}{"element-1"}
assert.ElementsMatch(t, expectedInAnyOrder, searchInv(t, filters.OperatorEqual, 10))
expectedInAnyOrder = []interface{}{} // value is no longer 20, but 21
assert.ElementsMatch(t, expectedInAnyOrder, searchInv(t, filters.OperatorEqual, 20))
expectedInAnyOrder = []interface{}{"element-2"}
assert.ElementsMatch(t, expectedInAnyOrder, searchInv(t, filters.OperatorEqual, 21))
expectedInAnyOrder = []interface{}{"element-3"}
assert.ElementsMatch(t, expectedInAnyOrder, searchInv(t, filters.OperatorEqual, 30))
})
t.Run("test recount", func(t *testing.T) {
tracker := getTracker(repo, "UpdateTestClass")
require.Nil(t, err)
sum, count, mean, err := tracker.PropertyTally("name")
require.Nil(t, err)
assert.Equal(t, 4, sum)
assert.Equal(t, 4, count)
assert.InEpsilon(t, 1, mean, 0.1)
tracker.Clear()
sum, count, mean, err = tracker.PropertyTally("name")
require.Nil(t, err)
assert.Equal(t, 0, sum)
assert.Equal(t, 0, count)
assert.Equal(t, float64(0), mean)
logger := logrus.New()
migrator := NewMigrator(repo, logger)
migrator.RecountProperties(context.Background())
sum, count, mean, err = tracker.PropertyTally("name")
require.Nil(t, err)
assert.Equal(t, 4, sum)
assert.Equal(t, 4, count)
assert.Equal(t, float64(1), mean)
})
}
func updateTestClass() *models.Class {
return &models.Class{
Class: "UpdateTestClass",
VectorIndexConfig: enthnsw.NewDefaultUserConfig(),
InvertedIndexConfig: &models.InvertedIndexConfig{
CleanupIntervalSeconds: 3,
},
Properties: []*models.Property{
{
DataType: []string{string(schema.DataTypeInt)},
Name: "intProp",
},
{
DataType: schema.DataTypeText.PropString(),
Tokenization: models.PropertyTokenizationWhitespace,
Name: "name",
},
},
}
}
func updateTestData() search.Results {
return search.Results{
search.Result{
ClassName: "UpdateTestClass",
ID: "426b0b29-9ded-40b6-b786-da3d1fec412f",
Schema: map[string]interface{}{
"intProp": int64(0),
"name": "element-0",
},
Vector: []float32{0.89379513, 0.67022973, 0.57360715},
},
search.Result{
ClassName: "UpdateTestClass",
ID: "a1560f12-f0f0-4439-b5b8-b7bcecf5fed7",
Schema: map[string]interface{}{
"intProp": int64(10),
"name": "element-1",
},
Vector: []float32{0.9660323, 0.35887036, 0.6072966},
},
search.Result{
ClassName: "UpdateTestClass",
ID: "0c73f145-5dc4-49a9-bd58-82725f8b13fa",
Schema: map[string]interface{}{
"intProp": int64(20),
"name": "element-2",
},
Vector: []float32{0.8194746, 0.56142205, 0.5130103},
},
search.Result{
ClassName: "UpdateTestClass",
ID: "aec8462e-276a-4989-a612-8314c35d163a",
Schema: map[string]interface{}{
"intProp": int64(30),
"name": "element-3",
},
Vector: []float32{0.42401955, 0.8278863, 0.5952888},
},
}
}
func extractPropValues(in search.Results, propName string) []interface{} {
out := make([]interface{}, len(in))
for i, res := range in {
out[i] = res.Schema.(map[string]interface{})[propName]
}
return out
}
func getTracker(repo *DB, className string) *inverted.JsonShardMetaData {
index := repo.GetIndex("UpdateTestClass")
var shard ShardLike
index.ForEachShard(func(name string, shardv ShardLike) error {
shard = shardv
return nil
})
tracker := shard.GetPropertyLengthTracker()
return tracker
}
|