Skip to content
This repository has been archived by the owner on Jul 16, 2020. It is now read-only.

Commit

Permalink
Merge pull request #647 from mcastelino/db_cleanup
Browse files Browse the repository at this point in the history
Networking: Cleanup the docker plugin database implementation
  • Loading branch information
kaccardi authored Oct 7, 2016
2 parents d3d4931 + 105155c commit c4b43e7
Show file tree
Hide file tree
Showing 2 changed files with 78 additions and 32 deletions.
38 changes: 9 additions & 29 deletions networking/libsnnet/docker_database.go
Original file line number Diff line number Diff line change
Expand Up @@ -76,47 +76,27 @@ func (db *dbProvider) DbClose() error {
return db.DB.Close()
}

//TODO: There must be a better way to do this (besides reflection)
func (db *dbProvider) DbMapRebuild(table string, dockerMap interface{}) error {
tables := []string{table}
func (db *dbProvider) DbTableRebuild(table DbTable) error {
tables := []string{table.Name()}
if err := db.DbTableInit(tables); err != nil {
return fmt.Errorf("dbInit failed %v", err)
}

switch dmap := dockerMap.(type) {
case *DockerNwMap:
dmap.m = make(map[string]*DockerNwVal)
case *DockerEpMap:
dmap.m = make(map[string]*DockerEpVal)
default:
return fmt.Errorf("error: invalid map type %T", dmap)
}
table.NewTable()

err := db.DB.View(func(tx *bolt.Tx) error {
b := tx.Bucket([]byte(table))
b := tx.Bucket([]byte(table.Name()))

err := b.ForEach(func(k, v []byte) error {
vr := bytes.NewReader(v)

switch dmap := dockerMap.(type) {
case *DockerNwMap:
val := &DockerNwVal{}
if err := gob.NewDecoder(vr).Decode(val); err != nil {
return fmt.Errorf("Decode Error: %v %v %v", string(k), string(v), err)
}
glog.Infof("%v key=%v, value=%v\n", table, string(k), val)

dmap.m[string(k)] = val
case *DockerEpMap:
val := &DockerEpVal{}
if err := gob.NewDecoder(vr).Decode(val); err != nil {
return fmt.Errorf("Decode Error: %v %v %v", string(k), string(v), err)
}
glog.Infof("%v key=%v, value=%v\n", table, string(k), val)
dmap.m[string(k)] = val
val := table.NewElement()
if err := gob.NewDecoder(vr).Decode(val); err != nil {
return fmt.Errorf("Decode Error: %v %v %v", string(k), string(v), err)
}
glog.Infof("%v key=%v, value=%v\n", table, string(k), val)

return nil
return table.Add(string(k), val)
})
return err
})
Expand Down
72 changes: 69 additions & 3 deletions networking/libsnnet/docker_plugin.go
Original file line number Diff line number Diff line change
Expand Up @@ -137,13 +137,29 @@ var DockerPluginCfg = struct {
Timeout: 1 * time.Second,
}

//DbTable interface that needs to be supported
//for the table to be handled by the database
type DbTable interface {
// Creates the backing map
NewTable()

// Name of the table as stored in the database
Name() string

// Allocates and returns a single value in the table
NewElement() interface{}

// Add an value to the in memory table
Add(k string, v interface{}) error
}

// A DockerDBProvider represents a persistent data base provider
// that can be used by the DockerPlugin to store its internal state
type DockerDBProvider interface {
//Initializes the Database
DbInit(dir string) error
//Populates the DockerPlugin cache from the database
DbMapRebuild(table string, dockerMap interface{}) error
DbTableRebuild(table DbTable) error
//Closes the database
DbClose() error
//Creates the tables if the tables do not already exist in the database
Expand Down Expand Up @@ -181,12 +197,62 @@ type DockerEpMap struct {
m map[string]*DockerEpVal //index: Docker End Point UUID
}

//NewTable creates a new map
func (d *DockerEpMap) NewTable() {
d.m = make(map[string]*DockerEpVal)
}

//Name provides the name of the map
func (d *DockerEpMap) Name() string {
return tableNetworkMap
}

//NewElement allocates and returns an endpoint value
func (d *DockerEpMap) NewElement() interface{} {
return &DockerEpVal{}
}

//Add adds a value to the map with the specified key
func (d *DockerEpMap) Add(k string, v interface{}) error {
val, ok := v.(*DockerEpVal)
if !ok {
return fmt.Errorf("Invalid value type %t", v)
}
d.m[k] = val
return nil
}

//DockerNwMap maintains the Docker Network UUID to ciao Network mappings
type DockerNwMap struct {
sync.Mutex
m map[string]*DockerNwVal //index: Docker Network UUID
}

//NewTable creates a new map
func (d *DockerNwMap) NewTable() {
d.m = make(map[string]*DockerNwVal)
}

//Name provides the name of the map
func (d *DockerNwMap) Name() string {
return tableEndPointMap
}

//NewElement allocates and returns an network value
func (d *DockerNwMap) NewElement() interface{} {
return &DockerNwVal{}
}

//Add adds a value to the map with the specified key
func (d *DockerNwMap) Add(k string, v interface{}) error {
val, ok := v.(*DockerNwVal)
if !ok {
return fmt.Errorf("Invalid value type %t", v)
}
d.m[k] = val
return nil
}

// DockerPlugin describes a single instance of a docker plugin
// In the current design the plugin acts as an IPAM and Network Plugin
type DockerPlugin struct {
Expand Down Expand Up @@ -756,10 +822,10 @@ func (d *DockerPlugin) Init() error {
if err := d.DbInit(DockerPluginCfg.DataDir); err != nil {
return err
}
if err := d.DbMapRebuild(tableNetworkMap, &d.DockerNwMap); err != nil {
if err := d.DbTableRebuild(&d.DockerNwMap); err != nil {
return err
}
if err := d.DbMapRebuild(tableEndPointMap, &d.DockerEpMap); err != nil {
if err := d.DbTableRebuild(&d.DockerEpMap); err != nil {
return err
}

Expand Down

0 comments on commit c4b43e7

Please sign in to comment.