@@ -21,8 +21,8 @@ import (
2121 "encoding/json"
2222 "fmt"
2323 "strings"
24- "time"
2524
25+ "github.com/skydive-project/skydive/common"
2626 "github.com/skydive-project/skydive/filters"
2727 "github.com/skydive-project/skydive/logging"
2828 "github.com/skydive-project/skydive/storage/orientdb"
@@ -31,7 +31,8 @@ import (
3131// OrientDBBackend describes an OrientDB backend
3232type OrientDBBackend struct {
3333 Backend
34- client orientdb.ClientInterface
34+ client orientdb.ClientInterface
35+ election common.MasterElection
3536}
3637
3738type eventTime struct {
@@ -90,7 +91,7 @@ func (o *OrientDBBackend) updateTimes(e string, id string, events ...eventTime)
9091 attrs = append (attrs , fmt .Sprintf ("%s = %d" , event .name , event .t .Unix ()))
9192 }
9293 query := fmt .Sprintf ("UPDATE %s SET %s WHERE DeletedAt IS NULL AND ArchivedAt IS NULL AND ID = '%s'" , e , strings .Join (attrs , ", " ), id )
93- result , err := o .client .Search (query )
94+ result , err := o .client .SQL (query )
9495 if err != nil {
9596 return fmt .Errorf ("Error while deleting %s: %s" , id , err )
9697 }
@@ -145,7 +146,7 @@ func (o *OrientDBBackend) searchNodes(t Context, where string) []*Node {
145146 query += " ORDER BY UpdatedAt"
146147 }
147148
148- result , err := o .client .Search (query )
149+ result , err := o .client .SQL (query )
149150 if err != nil {
150151 logging .GetLogger ().Errorf ("Error while retrieving nodes: %s" , err )
151152 return nil
@@ -172,7 +173,7 @@ func (o *OrientDBBackend) searchEdges(t Context, where string) []*Edge {
172173 query += " ORDER BY UpdatedAt"
173174 }
174175
175- result , err := o .client .Search (query )
176+ result , err := o .client .SQL (query )
176177 if err != nil {
177178 logging .GetLogger ().Errorf ("Error while retrieving edges: %s" , err )
178179 return nil
@@ -320,7 +321,44 @@ func (o *OrientDBBackend) IsHistorySupported() bool {
320321 return true
321322}
322323
323- func newOrientDBBackend (client orientdb.ClientInterface ) (* OrientDBBackend , error ) {
324+ func (o * OrientDBBackend ) flushGraph () error {
325+ logging .GetLogger ().Info ("Flush graph elements" )
326+
327+ now := TimeUTC ().Unix ()
328+
329+ query := fmt .Sprintf ("UPDATE Node SET DeletedAt = %d, ArchivedAt = %d WHERE DeletedAt IS NULL" , now , now )
330+ if _ , err := o .client .SQL (query ); err != nil {
331+ return fmt .Errorf ("Error while flushing graph: %s" , err )
332+ }
333+
334+ query = fmt .Sprintf ("UPDATE Edge SET DeletedAt = %d, ArchivedAt = %d WHERE DeletedAt IS NULL" , now , now )
335+ if _ , err := o .client .SQL (query ); err != nil {
336+ return fmt .Errorf ("Error while flushing graph: %s" , err )
337+ }
338+
339+ return nil
340+ }
341+
342+ // OnStarted implements storage client listener interface
343+ func (o * OrientDBBackend ) OnStarted () {
344+ if o .election != nil && o .election .IsMaster () {
345+ o .flushGraph ()
346+ }
347+ }
348+
349+ func newOrientDBBackend (client orientdb.ClientInterface , electionService common.MasterElectionService ) (* OrientDBBackend , error ) {
350+ o := & OrientDBBackend {
351+ client : client ,
352+ }
353+
354+ if electionService != nil {
355+ o .election = electionService .NewElection ("orientdb-graph-flush" )
356+ o .election .StartAndWait ()
357+ }
358+
359+ client .AddEventListener (o )
360+ client .Connect ()
361+
324362 if _ , err := client .GetDocumentClass ("Node" ); err != nil {
325363 class := orientdb.ClassDefinition {
326364 Name : "Node" ,
@@ -371,18 +409,16 @@ func newOrientDBBackend(client orientdb.ClientInterface) (*OrientDBBackend, erro
371409 }
372410 }
373411
374- return & OrientDBBackend {
375- client : client ,
376- }, nil
412+ return o , nil
377413}
378414
379415// NewOrientDBBackend creates a new graph backend and
380416// connect to an OrientDB instance
381- func NewOrientDBBackend (addr string , database string , username string , password string ) (* OrientDBBackend , error ) {
417+ func NewOrientDBBackend (addr string , database string , username string , password string , electionService common. MasterElectionService ) (* OrientDBBackend , error ) {
382418 client , err := orientdb .NewClient (addr , database , username , password )
383419 if err != nil {
384420 return nil , err
385421 }
386422
387- return newOrientDBBackend (client )
423+ return newOrientDBBackend (client , electionService )
388424}
0 commit comments