File size: 4,653 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
//                           _       _
// __      _____  __ ___   ___  __ _| |_ ___
// \ \ /\ / / _ \/ _` \ \ / / |/ _` | __/ _ \
//  \ 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/stretchr/testify/assert"
	"github.com/stretchr/testify/require"
	"github.com/weaviate/weaviate/entities/models"
	"github.com/weaviate/weaviate/entities/schema"
	"github.com/weaviate/weaviate/usecases/sharding"
)

// This test makes sure that even when a commit fails, the coordinator still
// honors the commit. This was introduced as part of
// https://github.com/weaviate/weaviate/issues/2616, where a schema
// inconsistency was guaranteed as soon as any node died in it's commit phase
// because the coordinator would behave differently than other (alive) which it
// told to commit.
func TestFailedCommits(t *testing.T) {
	type test struct {
		name string
		// prepare runs before any commit errors occur to build an initial state
		prepare func(*testing.T, *Manager)
		// action runs with commit errors
		action    func(*testing.T, *Manager)
		expSchema []*models.Class
	}

	ctx := context.Background()
	vTrue := true
	vFalse := false

	tests := []test{
		{
			name: "Add a class",
			action: func(t *testing.T, sm *Manager) {
				sm.AddClass(ctx, nil, &models.Class{
					Class:           "MyClass",
					VectorIndexType: "hnsw",
				})
			},
			expSchema: []*models.Class{
				classWithDefaultsWithProps(t, "MyClass", nil),
			},
		},
		{
			name: "Delete a class",
			prepare: func(t *testing.T, sm *Manager) {
				sm.AddClass(ctx, nil, &models.Class{
					Class:           "MyClass",
					VectorIndexType: "hnsw",
				})
				sm.AddClass(ctx, nil, &models.Class{
					Class:           "OtherClass",
					VectorIndexType: "hnsw",
				})
			},
			action: func(t *testing.T, sm *Manager) {
				assert.Nil(t, sm.DeleteClass(ctx, nil, "MyClass"))
			},
			expSchema: []*models.Class{
				classWithDefaultsWithProps(t, "OtherClass", nil),
			},
		},
		{
			name: "Extend a class with a property",
			prepare: func(t *testing.T, sm *Manager) {
				sm.AddClass(ctx, nil, &models.Class{
					Class:           "MyClass",
					VectorIndexType: "hnsw",
				})
			},
			action: func(t *testing.T, sm *Manager) {
				err := sm.AddClassProperty(ctx, nil, "MyClass", &models.Property{
					Name:     "prop_1",
					DataType: schema.DataTypeInt.PropString(),
				})
				assert.Nil(t, err)
			},
			expSchema: []*models.Class{
				classWithDefaultsWithProps(t, "MyClass", []*models.Property{
					{
						Name:            "prop_1",
						DataType:        schema.DataTypeInt.PropString(),
						IndexFilterable: &vTrue,
						IndexSearchable: &vFalse,
					},
				}),
			},
		},
	}

	for _, test := range tests {
		t.Run(test.name, func(t *testing.T) {
			clusterState := &fakeClusterState{
				hosts: []string{"node1", "node2"},
			}

			// required for the startup sync
			txJSON, _ := json.Marshal(ReadSchemaPayload{
				Schema: &State{
					ObjectSchema: &models.Schema{
						Classes: []*models.Class{},
					},
				},
			})

			txClient := &fakeTxClient{
				openInjectPayload: json.RawMessage(txJSON), // required for the startup sync
			}

			initialSchema := &State{
				ObjectSchema:  &models.Schema{},
				ShardingState: map[string]*sharding.State{},
			}

			sm, err := newManagerWithClusterAndTx(t, clusterState, txClient, initialSchema)
			require.Nil(t, err)

			sm.StartServing(context.Background())

			if test.prepare != nil {
				test.prepare(t, sm)
			}

			txClient.commitErr = fmt.Errorf("Oh I, I just died in your arms tonight")
			test.action(t, sm)

			assert.ElementsMatch(t, test.expSchema, sm.GetSchemaSkipAuth().Objects.Classes)
		})
	}
}

func classWithDefaultsWithProps(t *testing.T, name string,

	props []*models.Property,

) *models.Class {
	class := &models.Class{Class: name, VectorIndexType: "hnsw"}
	class.Vectorizer = "none"

	sc, err := sharding.ParseConfig(map[string]interface{}{}, 1)
	require.Nil(t, err)

	class.ShardingConfig = sc

	class.VectorIndexConfig = fakeVectorConfig{}
	class.ReplicationConfig = &models.ReplicationConfig{Factor: 1}
	class.MultiTenancyConfig = &models.MultiTenancyConfig{Enabled: false}

	(&fakeModuleConfig{}).SetClassDefaults(class)
	setInvertedConfigDefaults(class)

	class.Properties = props

	return class
}