Skip to content

Commit fa20454

Browse files
committed
client: honor last transaction IDs
Signed-off-by: Dan Williams <[email protected]>
1 parent 22bd5be commit fa20454

File tree

3 files changed

+123
-17
lines changed

3 files changed

+123
-17
lines changed

cache/cache.go

+37
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ import (
1717
"github.com/ovn-org/libovsdb/mapper"
1818
"github.com/ovn-org/libovsdb/model"
1919
"github.com/ovn-org/libovsdb/ovsdb"
20+
"github.com/ovn-org/libovsdb/util"
2021
)
2122

2223
const (
@@ -703,6 +704,42 @@ func (t *TableCache) Purge(dbModel model.DatabaseModel) {
703704
}
704705
}
705706

707+
// PurgeTable drops all data in the given table's cache and reinitializes it using the
708+
// provided database model
709+
func (t *TableCache) PurgeTable(dbModel model.DatabaseModel, name string) error {
710+
return t.PurgeTableRows(dbModel, name, nil)
711+
}
712+
713+
// PurgeTableRows drops all rows in the given table's cache that match the given conditions
714+
func (t *TableCache) PurgeTableRows(dbModel model.DatabaseModel, name string, conditions []ovsdb.Condition) error {
715+
t.mutex.Lock()
716+
defer t.mutex.Unlock()
717+
t.dbModel = dbModel
718+
tableTypes := t.dbModel.Types()
719+
dataType, ok := tableTypes[name]
720+
if !ok {
721+
return fmt.Errorf("table %s not found", name)
722+
}
723+
if len(conditions) == 0 {
724+
t.cache[name] = newRowCache(name, t.dbModel, dataType)
725+
return nil
726+
}
727+
728+
r := t.cache[name]
729+
rows, err := r.RowsByCondition(conditions)
730+
if err != nil {
731+
return err
732+
}
733+
delErrors := []error{}
734+
for uuid := range rows {
735+
if err := r.Delete(uuid); err != nil {
736+
delErrors = append(delErrors, fmt.Errorf("failed to delete %s: %w", uuid, err))
737+
}
738+
}
739+
740+
return util.CombineErrors(delErrors, "failed to delete rows")
741+
}
742+
706743
// AddEventHandler registers the supplied EventHandler to receive cache events
707744
func (t *TableCache) AddEventHandler(handler EventHandler) {
708745
t.eventProcessor.AddEventHandler(handler)

client/client.go

+66-17
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ import (
2424
"github.com/ovn-org/libovsdb/model"
2525
"github.com/ovn-org/libovsdb/ovsdb"
2626
"github.com/ovn-org/libovsdb/ovsdb/serverdb"
27+
"github.com/ovn-org/libovsdb/util"
2728
)
2829

2930
// Constants defined for libovsdb
@@ -199,6 +200,65 @@ func (o *ovsdbClient) Connect(ctx context.Context) error {
199200
return nil
200201
}
201202

203+
func (db *database) purge(name string) error {
204+
// If a table has any !Since monitors or has no conditions, purge it
205+
// If a table only has Since monitors with conditions, purge only rows that match the conditions
206+
type purge struct {
207+
conditions []ovsdb.Condition
208+
purgeAll bool
209+
}
210+
211+
purges := make(map[string]*purge)
212+
for _, monitor := range db.monitors {
213+
for _, tm := range monitor.Tables {
214+
p, ok := purges[tm.Table]
215+
if !ok {
216+
p = &purge{}
217+
purges[tm.Table] = p
218+
}
219+
if monitor.Method == ovsdb.ConditionalMonitorSinceRPC {
220+
model, err := db.model.NewModel(tm.Table)
221+
if err != nil {
222+
p.purgeAll = true
223+
continue
224+
}
225+
info, err := db.model.NewModelInfo(model)
226+
if err != nil {
227+
p.purgeAll = true
228+
continue
229+
}
230+
ovsdbCond, err := db.model.Mapper.NewCondition(info, tm.Condition.Field, tm.Condition.Function, tm.Condition.Value)
231+
if err != nil {
232+
p.purgeAll = true
233+
continue
234+
}
235+
p.conditions = append(p.conditions, *ovsdbCond)
236+
} else {
237+
p.purgeAll = true
238+
}
239+
}
240+
}
241+
if len(purges) == 0 {
242+
db.cache.Purge(db.model)
243+
return nil
244+
}
245+
246+
var purgeErrors []error
247+
for name, p := range purges {
248+
if p.purgeAll {
249+
if err := db.cache.PurgeTable(db.model, name); err != nil {
250+
purgeErrors = append(purgeErrors, err)
251+
}
252+
} else {
253+
if err := db.cache.PurgeTableRows(db.model, name, p.conditions); err != nil {
254+
purgeErrors = append(purgeErrors, err)
255+
}
256+
}
257+
}
258+
259+
return util.CombineErrors(purgeErrors, fmt.Sprintf("failed to purge database %s", name))
260+
}
261+
202262
func (o *ovsdbClient) connect(ctx context.Context, reconnect bool) error {
203263
o.rpcMutex.Lock()
204264
defer o.rpcMutex.Unlock()
@@ -226,15 +286,7 @@ func (o *ovsdbClient) connect(ctx context.Context, reconnect bool) error {
226286
}
227287

228288
if !connected {
229-
if len(connectErrors) == 1 {
230-
return connectErrors[0]
231-
}
232-
var combined []string
233-
for _, e := range connectErrors {
234-
combined = append(combined, e.Error())
235-
}
236-
237-
return fmt.Errorf("unable to connect to any endpoints: %s", strings.Join(combined, ". "))
289+
return util.CombineErrors(connectErrors, "unable to connect to any endpoints")
238290
}
239291

240292
// if we're reconnecting, re-start all the monitors
@@ -243,6 +295,10 @@ func (o *ovsdbClient) connect(ctx context.Context, reconnect bool) error {
243295
for dbName, db := range o.databases {
244296
db.monitorsMutex.Lock()
245297
defer db.monitorsMutex.Unlock()
298+
299+
if err := db.purge(dbName); err != nil {
300+
o.logger.V(3).Error(err, "failed to purge")
301+
}
246302
for id, request := range db.monitors {
247303
err := o.monitor(ctx, MonitorCookie{DatabaseName: dbName, ID: id}, true, request)
248304
if err != nil {
@@ -349,8 +405,6 @@ func (o *ovsdbClient) tryEndpoint(ctx context.Context, u *url.URL) error {
349405
return err
350406
}
351407
db.api = newAPI(db.cache, o.logger)
352-
} else {
353-
db.cache.Purge(db.model)
354408
}
355409
db.cacheMutex.Unlock()
356410
}
@@ -832,12 +886,7 @@ func (o *ovsdbClient) monitor(ctx context.Context, cookie MonitorCookie, reconne
832886

833887
var args []interface{}
834888
if monitor.Method == ovsdb.ConditionalMonitorSinceRPC {
835-
// FIXME: We should pass the monitor.LastTransactionID here
836-
// But that would require delaying clearing the cache until
837-
// after the monitors have been re-established - the logic
838-
// would also need to be different for monitor and monitor_cond
839-
// as we must always clear the cache in that instance
840-
args = ovsdb.NewMonitorCondSinceArgs(dbName, cookie, requests, emptyUUID)
889+
args = ovsdb.NewMonitorCondSinceArgs(dbName, cookie, requests, monitor.LastTransactionID)
841890
} else {
842891
args = ovsdb.NewMonitorArgs(dbName, cookie, requests)
843892
}

util/errors.go

+20
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
package util
2+
3+
import (
4+
"fmt"
5+
"strings"
6+
)
7+
8+
func CombineErrors(errors []error, msg string) error {
9+
if len(errors) == 0 {
10+
return nil
11+
} else if len(errors) == 1 {
12+
return errors[0]
13+
}
14+
15+
var combined []string
16+
for _, e := range errors {
17+
combined = append(combined, e.Error())
18+
}
19+
return fmt.Errorf("%s: %s", msg, strings.Join(combined, ". "))
20+
}

0 commit comments

Comments
 (0)