Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Make Columns and Tables Optional #257

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions cache/cache.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"crypto/sha256"
"encoding/gob"
"encoding/hex"
"errors"
"fmt"
"log"
"os"
Expand Down Expand Up @@ -877,6 +878,10 @@ func (t *TableCache) ApplyModifications(tableName string, base model.Model, upda

current, err := info.FieldByColumn(k)
if err != nil {
if errors.Is(err, mapper.ErrOmitted) {
// skip fields that aren't supported by the runtime schema
continue
}
return err
}

Expand Down
30 changes: 15 additions & 15 deletions cache/cache_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,7 @@ func TestRowCache_Rows(t *testing.T) {

func TestRowCacheCreate(t *testing.T) {
var schema ovsdb.DatabaseSchema
db, err := model.NewClientDBModel("Open_vSwitch", map[string]model.Model{"Open_vSwitch": &testModel{}})
db, err := model.NewClientDBModel("Open_vSwitch", map[string]model.Model{"Open_vSwitch": &testModel{}}, nil)
require.Nil(t, err)
err = json.Unmarshal([]byte(`
{"name": "Open_vSwitch",
Expand Down Expand Up @@ -170,7 +170,7 @@ func TestRowCacheCreate(t *testing.T) {

func TestRowCacheCreateMultiIndex(t *testing.T) {
var schema ovsdb.DatabaseSchema
db, err := model.NewClientDBModel("Open_vSwitch", map[string]model.Model{"Open_vSwitch": &testModel{}})
db, err := model.NewClientDBModel("Open_vSwitch", map[string]model.Model{"Open_vSwitch": &testModel{}}, nil)
require.Nil(t, err)
err = json.Unmarshal([]byte(`
{"name": "Open_vSwitch",
Expand Down Expand Up @@ -264,7 +264,7 @@ func TestRowCacheCreateMultiIndex(t *testing.T) {

func TestRowCacheUpdate(t *testing.T) {
var schema ovsdb.DatabaseSchema
db, err := model.NewClientDBModel("Open_vSwitch", map[string]model.Model{"Open_vSwitch": &testModel{}})
db, err := model.NewClientDBModel("Open_vSwitch", map[string]model.Model{"Open_vSwitch": &testModel{}}, nil)
require.Nil(t, err)
err = json.Unmarshal([]byte(`
{"name": "Open_vSwitch",
Expand Down Expand Up @@ -348,7 +348,7 @@ func TestRowCacheUpdate(t *testing.T) {

func TestRowCacheUpdateMultiIndex(t *testing.T) {
var schema ovsdb.DatabaseSchema
db, err := model.NewClientDBModel("Open_vSwitch", map[string]model.Model{"Open_vSwitch": &testModel{}})
db, err := model.NewClientDBModel("Open_vSwitch", map[string]model.Model{"Open_vSwitch": &testModel{}}, nil)
require.Nil(t, err)
err = json.Unmarshal([]byte(`
{"name": "Open_vSwitch",
Expand Down Expand Up @@ -431,7 +431,7 @@ func TestRowCacheUpdateMultiIndex(t *testing.T) {

func TestRowCacheDelete(t *testing.T) {
var schema ovsdb.DatabaseSchema
db, err := model.NewClientDBModel("Open_vSwitch", map[string]model.Model{"Open_vSwitch": &testModel{}})
db, err := model.NewClientDBModel("Open_vSwitch", map[string]model.Model{"Open_vSwitch": &testModel{}}, nil)
require.Nil(t, err)
err = json.Unmarshal([]byte(`
{"name": "Open_vSwitch",
Expand Down Expand Up @@ -629,7 +629,7 @@ func TestEventHandlerFuncs_OnDelete(t *testing.T) {
}

func TestTableCacheTable(t *testing.T) {
db, err := model.NewClientDBModel("Open_vSwitch", map[string]model.Model{"Open_vSwitch": &testModel{}})
db, err := model.NewClientDBModel("Open_vSwitch", map[string]model.Model{"Open_vSwitch": &testModel{}}, nil)
assert.Nil(t, err)
var schema ovsdb.DatabaseSchema
err = json.Unmarshal([]byte(`
Expand Down Expand Up @@ -687,7 +687,7 @@ func TestTableCacheTables(t *testing.T) {
map[string]model.Model{
"test1": &testModel{},
"test2": &testModel{},
"test3": &testModel{}})
"test3": &testModel{}}, nil)
assert.Nil(t, err)
var schema ovsdb.DatabaseSchema
err = json.Unmarshal([]byte(`
Expand Down Expand Up @@ -762,7 +762,7 @@ func TestTableCacheTables(t *testing.T) {

func TestTableCache_populate(t *testing.T) {
t.Log("Create")
db, err := model.NewClientDBModel("Open_vSwitch", map[string]model.Model{"Open_vSwitch": &testModel{}})
db, err := model.NewClientDBModel("Open_vSwitch", map[string]model.Model{"Open_vSwitch": &testModel{}}, nil)
assert.Nil(t, err)
var schema ovsdb.DatabaseSchema
err = json.Unmarshal([]byte(`
Expand Down Expand Up @@ -832,7 +832,7 @@ func TestTableCache_populate(t *testing.T) {

func TestTableCachePopulate(t *testing.T) {
t.Log("Create")
db, err := model.NewClientDBModel("Open_vSwitch", map[string]model.Model{"Open_vSwitch": &testModel{}})
db, err := model.NewClientDBModel("Open_vSwitch", map[string]model.Model{"Open_vSwitch": &testModel{}}, nil)
assert.Nil(t, err)
var schema ovsdb.DatabaseSchema
err = json.Unmarshal([]byte(`
Expand Down Expand Up @@ -901,7 +901,7 @@ func TestTableCachePopulate(t *testing.T) {
}

func TestTableCachePopulate2(t *testing.T) {
db, err := model.NewClientDBModel("Open_vSwitch", map[string]model.Model{"Open_vSwitch": &testModel{}})
db, err := model.NewClientDBModel("Open_vSwitch", map[string]model.Model{"Open_vSwitch": &testModel{}}, nil)
assert.Nil(t, err)
var schema ovsdb.DatabaseSchema
err = json.Unmarshal([]byte(`
Expand Down Expand Up @@ -1026,7 +1026,7 @@ func TestIndex(t *testing.T) {
Bar string `ovsdb:"bar"`
Baz int `ovsdb:"baz"`
}
db, err := model.NewClientDBModel("Open_vSwitch", map[string]model.Model{"Open_vSwitch": &indexTestModel{}})
db, err := model.NewClientDBModel("Open_vSwitch", map[string]model.Model{"Open_vSwitch": &indexTestModel{}}, nil)
assert.Nil(t, err)
var schema ovsdb.DatabaseSchema
err = json.Unmarshal([]byte(`
Expand Down Expand Up @@ -1123,7 +1123,7 @@ func TestIndex(t *testing.T) {

func TestTableCacheRowByModelSingleIndex(t *testing.T) {
var schema ovsdb.DatabaseSchema
db, err := model.NewClientDBModel("Open_vSwitch", map[string]model.Model{"Open_vSwitch": &testModel{}})
db, err := model.NewClientDBModel("Open_vSwitch", map[string]model.Model{"Open_vSwitch": &testModel{}}, nil)
require.Nil(t, err)
err = json.Unmarshal([]byte(`
{"name": "Open_vSwitch",
Expand Down Expand Up @@ -1174,7 +1174,7 @@ func TestTableCacheRowByModelSingleIndex(t *testing.T) {

func TestTableCacheRowByModelTwoIndexes(t *testing.T) {
var schema ovsdb.DatabaseSchema
db, err := model.NewClientDBModel("Open_vSwitch", map[string]model.Model{"Open_vSwitch": &testModel{}})
db, err := model.NewClientDBModel("Open_vSwitch", map[string]model.Model{"Open_vSwitch": &testModel{}}, nil)
require.Nil(t, err)
err = json.Unmarshal([]byte(`
{"name": "Open_vSwitch",
Expand Down Expand Up @@ -1227,7 +1227,7 @@ func TestTableCacheRowByModelTwoIndexes(t *testing.T) {

func TestTableCacheRowByModelMultiIndex(t *testing.T) {
var schema ovsdb.DatabaseSchema
db, err := model.NewClientDBModel("Open_vSwitch", map[string]model.Model{"Open_vSwitch": &testModel{}})
db, err := model.NewClientDBModel("Open_vSwitch", map[string]model.Model{"Open_vSwitch": &testModel{}}, nil)
require.Nil(t, err)
err = json.Unmarshal([]byte(`
{"name": "Open_vSwitch",
Expand Down Expand Up @@ -1379,7 +1379,7 @@ func TestTableCacheApplyModifications(t *testing.T) {
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
db, err := model.NewClientDBModel("Open_vSwitch", map[string]model.Model{"Open_vSwitch": &testDBModel{}})
db, err := model.NewClientDBModel("Open_vSwitch", map[string]model.Model{"Open_vSwitch": &testDBModel{}}, nil)
assert.Nil(t, err)
var schema ovsdb.DatabaseSchema
err = json.Unmarshal([]byte(`
Expand Down
2 changes: 1 addition & 1 deletion client/api_test_model.go
Original file line number Diff line number Diff line change
Expand Up @@ -157,7 +157,7 @@ func apiTestCache(t *testing.T, data map[string]map[string]model.Model) *cache.T
var schema ovsdb.DatabaseSchema
err := json.Unmarshal(apiTestSchema, &schema)
assert.Nil(t, err)
db, err := model.NewClientDBModel("OVN_Northbound", map[string]model.Model{"Logical_Switch": &testLogicalSwitch{}, "Logical_Switch_Port": &testLogicalSwitchPort{}})
db, err := model.NewClientDBModel("OVN_Northbound", map[string]model.Model{"Logical_Switch": &testLogicalSwitch{}, "Logical_Switch_Port": &testLogicalSwitchPort{}}, nil)
assert.Nil(t, err)
dbModel, errs := model.NewDatabaseModel(schema, db)
assert.Empty(t, errs)
Expand Down
33 changes: 33 additions & 0 deletions client/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ import (
"github.com/go-logr/logr"
"github.com/go-logr/stdr"
"github.com/ovn-org/libovsdb/cache"
"github.com/ovn-org/libovsdb/mapper"
"github.com/ovn-org/libovsdb/model"
"github.com/ovn-org/libovsdb/ovsdb"
"github.com/ovn-org/libovsdb/ovsdb/serverdb"
Expand Down Expand Up @@ -1121,3 +1122,35 @@ func (o *ovsdbClient) WhereAll(m model.Model, conditions ...model.Condition) Con
func (o *ovsdbClient) WhereCache(predicate interface{}) ConditionalAPI {
return o.primaryDB().api.WhereCache(predicate)
}

// IsTableSupported checks whether a provided Model is supported by the
// runtime schema
func (o *ovsdbClient) IsTableSupported(m model.Model) bool {
o.primaryDB().modelMutex.RLock()
defer o.primaryDB().modelMutex.RUnlock()
tableName := o.primaryDB().model.FindTable(reflect.TypeOf(m))
if _, ok := o.primaryDB().model.Schema.Tables[tableName]; ok {
return true
}
return false
}

// IsColumnSupported checks whether a provided column (derived via field Pointer in a Model)
// is supported by the runtime schema
func (o *ovsdbClient) IsColumnSupported(m model.Model, fieldPtr interface{}) bool {
o.primaryDB().modelMutex.RLock()
defer o.primaryDB().modelMutex.RUnlock()
tableName := o.primaryDB().model.FindTable(reflect.TypeOf(m))
tSchema, ok := o.primaryDB().model.Schema.Tables[tableName]
if !ok {
return false
}
info, err := mapper.NewInfo(tableName, &tSchema, m)
if err != nil {
return false
}
if _, err := info.ColumnByPtr(fieldPtr); err != nil {
return false
}
return true
}
13 changes: 7 additions & 6 deletions client/client_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,7 @@ var defDB, _ = model.NewClientDBModel("Open_vSwitch",
"Open_vSwitch": &OpenvSwitch{},
"Bridge": &Bridge{},
},
nil,
)

var schema = `{
Expand Down Expand Up @@ -562,7 +563,7 @@ func BenchmarkUpdate1(b *testing.B) {
clientDBModel, err := model.NewClientDBModel("Open_vSwitch", map[string]model.Model{
"Bridge": &Bridge{},
"Open_vSwitch": &OpenvSwitch{},
})
}, nil)
require.NoError(b, err)
dbModel, errs := model.NewDatabaseModel(s, clientDBModel)
require.Empty(b, errs)
Expand All @@ -588,7 +589,7 @@ func BenchmarkUpdate2(b *testing.B) {
clientDBModel, err := model.NewClientDBModel("Open_vSwitch", map[string]model.Model{
"Bridge": &Bridge{},
"Open_vSwitch": &OpenvSwitch{},
})
}, nil)
require.NoError(b, err)
dbModel, errs := model.NewDatabaseModel(s, clientDBModel)
require.Empty(b, errs)
Expand All @@ -615,7 +616,7 @@ func BenchmarkUpdate3(b *testing.B) {
clientDBModel, err := model.NewClientDBModel("Open_vSwitch", map[string]model.Model{
"Bridge": &Bridge{},
"Open_vSwitch": &OpenvSwitch{},
})
}, nil)
require.NoError(b, err)
dbModel, errs := model.NewDatabaseModel(s, clientDBModel)
require.Empty(b, errs)
Expand Down Expand Up @@ -643,7 +644,7 @@ func BenchmarkUpdate5(b *testing.B) {
clientDBModel, err := model.NewClientDBModel("Open_vSwitch", map[string]model.Model{
"Bridge": &Bridge{},
"Open_vSwitch": &OpenvSwitch{},
})
}, nil)
require.NoError(b, err)
dbModel, errs := model.NewDatabaseModel(s, clientDBModel)
require.Empty(b, errs)
Expand Down Expand Up @@ -673,7 +674,7 @@ func BenchmarkUpdate8(b *testing.B) {
clientDBModel, err := model.NewClientDBModel("Open_vSwitch", map[string]model.Model{
"Bridge": &Bridge{},
"Open_vSwitch": &OpenvSwitch{},
})
}, nil)
require.NoError(b, err)
dbModel, errs := model.NewDatabaseModel(s, clientDBModel)
require.Empty(b, errs)
Expand Down Expand Up @@ -720,7 +721,7 @@ func TestUpdate(t *testing.T) {
clientDBModel, err := model.NewClientDBModel("Open_vSwitch", map[string]model.Model{
"Bridge": &Bridge{},
"Open_vSwitch": &OpenvSwitch{},
})
}, nil)
require.NoError(t, err)
dbModel, errs := model.NewDatabaseModel(s, clientDBModel)
require.Empty(t, errs)
Expand Down
2 changes: 1 addition & 1 deletion cmd/stress/stress.go
Original file line number Diff line number Diff line change
Expand Up @@ -249,7 +249,7 @@ func main() {
}

var err error
clientDBModel, err = model.NewClientDBModel("Open_vSwitch", map[string]model.Model{"Open_vSwitch": &ovsType{}, "Bridge": &bridgeType{}})
clientDBModel, err = model.NewClientDBModel("Open_vSwitch", map[string]model.Model{"Open_vSwitch": &ovsType{}, "Bridge": &bridgeType{}}, nil)
if err != nil {
log.Fatal(err)
}
Expand Down
2 changes: 1 addition & 1 deletion example/play_with_ovs/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,7 @@ func main() {
update = make(chan model.Model)

clientDBModel, err := model.NewClientDBModel("Open_vSwitch",
map[string]model.Model{bridgeTable: &vswitchd.Bridge{}, ovsTable: &vswitchd.OpenvSwitch{}})
map[string]model.Model{bridgeTable: &vswitchd.Bridge{}, ovsTable: &vswitchd.OpenvSwitch{}}, nil)
if err != nil {
log.Fatal("Unable to create DB model ", err)
}
Expand Down
Loading