Skip to content

Commit

Permalink
Merge pull request #186 from dave-tucker/no-index
Browse files Browse the repository at this point in the history
cache: Disable Index Checking on Populate
  • Loading branch information
dave-tucker authored Jul 8, 2021
2 parents fecdcab + f899bd7 commit d17ab7f
Show file tree
Hide file tree
Showing 3 changed files with 58 additions and 26 deletions.
21 changes: 12 additions & 9 deletions cache/cache.go
Original file line number Diff line number Diff line change
Expand Up @@ -114,7 +114,7 @@ func (r *RowCache) RowByModel(m model.Model) model.Model {
}

// Create writes the provided content to the cache
func (r *RowCache) Create(uuid string, m model.Model) error {
func (r *RowCache) Create(uuid string, m model.Model, checkIndexes bool) error {
r.mutex.Lock()
defer r.mutex.Unlock()
if _, ok := r.cache[uuid]; ok {
Expand All @@ -129,15 +129,15 @@ func (r *RowCache) Create(uuid string, m model.Model) error {
}
newIndexes := newColumnToValue(r.schema.Indexes)
for index := range r.indexes {

val, err := valueFromIndex(info, index)

if err != nil {
return err
}
if existing, ok := r.indexes[index][val]; ok {

if existing, ok := r.indexes[index][val]; ok && checkIndexes {
return NewIndexExistsError(r.name, val, index, uuid, existing)
}

newIndexes[index][val] = uuid
}

Expand All @@ -152,7 +152,7 @@ func (r *RowCache) Create(uuid string, m model.Model) error {
}

// Update updates the content in the cache
func (r *RowCache) Update(uuid string, m model.Model) error {
func (r *RowCache) Update(uuid string, m model.Model, checkIndexes bool) error {
r.mutex.Lock()
defer r.mutex.Unlock()
if _, ok := r.cache[uuid]; !ok {
Expand Down Expand Up @@ -188,7 +188,8 @@ func (r *RowCache) Update(uuid string, m model.Model) error {
// old and new values are NOT the same

// check that there are no conflicts
if conflict, ok := r.indexes[index][newVal]; ok && conflict != uuid {

if conflict, ok := r.indexes[index][newVal]; ok && checkIndexes && conflict != uuid {
errs = append(errs, NewIndexExistsError(
r.name,
newVal,
Expand All @@ -197,6 +198,7 @@ func (r *RowCache) Update(uuid string, m model.Model) error {
conflict,
))
}

newIndexes[index][newVal] = uuid
oldIndexes[index][oldVal] = ""
}
Expand Down Expand Up @@ -339,7 +341,7 @@ func NewTableCache(schema *ovsdb.DatabaseSchema, dbModel *model.DBModel, data Da
return nil, fmt.Errorf("table %s is not in schema", table)
}
for uuid, row := range rowData {
if err := cache[table].Create(uuid, row); err != nil {
if err := cache[table].Create(uuid, row, true); err != nil {
return nil, err
}
}
Expand Down Expand Up @@ -414,6 +416,7 @@ func (t *TableCache) Disconnected() {
func (t *TableCache) Populate(tableUpdates ovsdb.TableUpdates) {
t.mutex.Lock()
defer t.mutex.Unlock()

for table := range t.dbModel.Types() {
updates, ok := tableUpdates[table]
if !ok {
Expand All @@ -428,15 +431,15 @@ func (t *TableCache) Populate(tableUpdates ovsdb.TableUpdates) {
}
if existing := tCache.Row(uuid); existing != nil {
if !reflect.DeepEqual(newModel, existing) {
if err := tCache.Update(uuid, newModel); err != nil {
if err := tCache.Update(uuid, newModel, false); err != nil {
panic(err)
}
t.eventProcessor.AddEvent(updateEvent, table, existing, newModel)
}
// no diff
continue
}
if err := tCache.Create(uuid, newModel); err != nil {
if err := tCache.Create(uuid, newModel, false); err != nil {
panic(err)
}
t.eventProcessor.AddEvent(addEvent, table, nil, newModel)
Expand Down
57 changes: 43 additions & 14 deletions cache/cache_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -111,35 +111,53 @@ func TestRowCacheCreate(t *testing.T) {
require.Nil(t, err)

tests := []struct {
name string
uuid string
model *testModel
wantErr bool
name string
uuid string
model *testModel
checkIndex bool
wantErr bool
}{
{
"inserts a new row",
"foo",
&testModel{Foo: "foo"},
true,
false,
},
{
"error duplicate uuid",
"bar",
&testModel{Foo: "foo"},
true,
true,
},
{
"error duplicate index",
"baz",
&testModel{Foo: "bar"},
true,
true,
},
{
"error duplicate uuid, no index check",
"bar",
&testModel{Foo: "bar"},
false,
true,
},
{
"no error duplicate index",
"baz",
&testModel{Foo: "bar"},
false,
false,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
rc := tc.Table("Open_vSwitch")
require.NotNil(t, rc)
err := rc.Create(tt.uuid, tt.model)
err := rc.Create(tt.uuid, tt.model, tt.checkIndex)
if tt.wantErr {
assert.Error(t, err)
} else {
Expand Down Expand Up @@ -225,7 +243,7 @@ func TestRowCacheCreateMultiIndex(t *testing.T) {
t.Run(tt.name, func(t *testing.T) {
rc := tc.Table("Open_vSwitch")
require.NotNil(t, rc)
err := rc.Create(tt.uuid, tt.model)
err := rc.Create(tt.uuid, tt.model, true)
if tt.wantErr {
assert.Error(t, err)
if tt.wantIndexExistsErr {
Expand Down Expand Up @@ -275,35 +293,46 @@ func TestRowCacheUpdate(t *testing.T) {
require.Nil(t, err)

tests := []struct {
name string
uuid string
model *testModel
wantErr bool
name string
uuid string
model *testModel
checkIndex bool
wantErr bool
}{
{
"error if row does not exist",
"foo",
&testModel{Foo: "foo"},
true,
true,
},
{
"update",
"bar",
&testModel{Foo: "baz"},
true,
false,
},
{
"error new index would cause duplicate",
"baz",
"bar",
&testModel{Foo: "foobar"},
true,
true,
},
{
"no error new index would cause duplicate",
"bar",
&testModel{Foo: "foobar"},
false,
false,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
rc := tc.Table("Open_vSwitch")
require.NotNil(t, rc)
err := rc.Update(tt.uuid, tt.model)
err := rc.Update(tt.uuid, tt.model, tt.checkIndex)
if tt.wantErr {
assert.Error(t, err)
} else {
Expand Down Expand Up @@ -381,7 +410,7 @@ func TestRowCacheUpdateMultiIndex(t *testing.T) {
t.Run(tt.name, func(t *testing.T) {
rc := tc.Table("Open_vSwitch")
require.NotNil(t, rc)
err := rc.Update(tt.uuid, tt.model)
err := rc.Update(tt.uuid, tt.model, true)
if tt.wantErr {
assert.Error(t, err)
} else {
Expand Down Expand Up @@ -792,7 +821,7 @@ func TestIndex(t *testing.T) {
}
table := tc.Table("Open_vSwitch")

err = table.Create(obj.UUID, obj)
err = table.Create(obj.UUID, obj, true)
assert.Nil(t, err)
t.Run("Index by single column", func(t *testing.T) {
idx, err := table.Index("foo")
Expand Down
6 changes: 3 additions & 3 deletions server/database.go
Original file line number Diff line number Diff line change
Expand Up @@ -142,7 +142,7 @@ func (db *inMemoryDatabase) Insert(database string, table string, rowUUID string
}

// insert in to db
if err := targetDb.Table(table).Create(rowUUID, model); err != nil {
if err := targetDb.Table(table).Create(rowUUID, model, true); err != nil {
if indexErr, ok := err.(*cache.IndexExistsError); ok {
e := ovsdb.ConstraintViolation{}
return ovsdb.OperationResult{
Expand Down Expand Up @@ -227,7 +227,7 @@ func (db *inMemoryDatabase) Update(database, table string, where []ovsdb.Conditi
if err != nil {
panic(err)
}
if err = targetDb.Table(table).Update(uuid.(string), row); err != nil {
if err = targetDb.Table(table).Update(uuid.(string), row, true); err != nil {
if indexErr, ok := err.(*cache.IndexExistsError); ok {
e := ovsdb.ConstraintViolation{}
return ovsdb.OperationResult{
Expand Down Expand Up @@ -300,7 +300,7 @@ func (db *inMemoryDatabase) Mutate(database, table string, where []ovsdb.Conditi
panic(err)
}
// the field in old has been set, write back to db
err = targetDb.Table(table).Update(uuid.(string), old)
err = targetDb.Table(table).Update(uuid.(string), old, true)
if err != nil {
if indexErr, ok := err.(*cache.IndexExistsError); ok {
e := ovsdb.ConstraintViolation{}
Expand Down

0 comments on commit d17ab7f

Please sign in to comment.