From 740707c30f43a7062b295714457bb86729a2cdd6 Mon Sep 17 00:00:00 2001 From: Manohar Castelino Date: Fri, 14 Oct 2016 17:46:46 -0700 Subject: [PATCH 1/2] Networking: Make the boltDB+gob table database generic Make the boldDB+gob table database generic so that it can be reused by other components. Note: As the table name internally has changed, this will break any in place update of the CN launcher. For a proper update of the CN, it has to be hard reset to remove all instances and the database cleaned up. Signed-off-by: Manohar Castelino --- networking/libsnnet/docker_database.go | 58 ++++++++++++++++++++------ networking/libsnnet/docker_plugin.go | 43 +++---------------- 2 files changed, 50 insertions(+), 51 deletions(-) diff --git a/networking/libsnnet/docker_database.go b/networking/libsnnet/docker_database.go index 58ead1dd1..9d3dfd00c 100644 --- a/networking/libsnnet/docker_database.go +++ b/networking/libsnnet/docker_database.go @@ -28,30 +28,62 @@ import ( "github.com/golang/glog" ) -const ( - dbFile = "docker_plugin.db" -) +//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 TableDBProvider represents a persistent data base provider +// that can be used to store map, arrays or any other type of +// tables into a database +type TableDBProvider interface { + //Initializes the Database + DbInit(dir string, file string) error + //Populates the DockerPlugin cache from the database + DbTableRebuild(table DbTable) error + //Closes the database + DbClose() error + //Creates the tables if the tables do not already exist in the database + DbTableInit(tables []string) error + //Adds the key value pair to the table + DbAdd(table string, key string, value interface{}) error + //Adds the key value pair to the table + DbDelete(table string, key string) error + //Retrives the value corresponding to the key from the table + DbGet(table string, key string) (interface{}, error) +} -type dockerBoltDB struct { +type tableBoltDB struct { Name string DB *bolt.DB } -func newDockerBoltDb() *dockerBoltDB { - return &dockerBoltDB{ - Name: "docker_bolt.DB", +func newTableBoltDb() *tableBoltDB { + return &tableBoltDB{ + Name: "tableBolt.DB", } } -type dbProvider dockerBoltDB +type dbProvider tableBoltDB -//NewDockerBoltDBProvider returns a bolt based database that conforms -//to the DockerDBProvider interface -func NewDockerBoltDBProvider() DockerDBProvider { - return (*dbProvider)(newDockerBoltDb()) +//NewTableBoltDBProvider returns a bolt based database that conforms +//to the tableDBProvider interface +func NewTableBoltDBProvider() TableDBProvider { + return (*dbProvider)(newTableBoltDb()) } -func (db *dbProvider) DbInit(dbDir string) error { +func (db *dbProvider) DbInit(dbDir string, dbFile string) error { if err := os.MkdirAll(dbDir, 0755); err != nil { return fmt.Errorf("Unable to create db directory (%s) %v", dbDir, err) diff --git a/networking/libsnnet/docker_plugin.go b/networking/libsnnet/docker_plugin.go index b47f193dc..980f0822a 100644 --- a/networking/libsnnet/docker_plugin.go +++ b/networking/libsnnet/docker_plugin.go @@ -128,50 +128,17 @@ var DockerPluginCfg = struct { Dir string Addr string DataDir string + DbFile string Timeout time.Duration }{ Name: "ciao", Dir: "/etc/docker/plugins", Addr: "127.0.0.1:9999", DataDir: "/var/lib/ciao/networking", + DbFile: "docker_plugin.db", 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 - DbTableRebuild(table DbTable) error - //Closes the database - DbClose() error - //Creates the tables if the tables do not already exist in the database - DbTableInit(tables []string) error - //Adds the key value pair to the table - DbAdd(table string, key string, value interface{}) error - //Adds the key value pair to the table - DbDelete(table string, key string) error - //Retrives the value corresponding to the key from the table - DbGet(table string, key string) (interface{}, error) -} - //DockerEpVal stores ciao VNIC info for a particular docker endpoint type DockerEpVal struct { ID string @@ -256,7 +223,7 @@ func (d *DockerNwMap) Add(k string, v interface{}) error { // 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 { - DockerDBProvider //Database used to persist the Docker to ciao Mapping + TableDBProvider //Database used to persist the Docker to ciao Mapping //This is needed as the Docker Daemon and ciao have //different life cycles and UUIDs *mux.Router @@ -779,7 +746,7 @@ func DockerHandler(d *DockerPlugin, //NewDockerPlugin instantiates a new Docker Plugin instance func NewDockerPlugin() *DockerPlugin { return &DockerPlugin{ - DockerDBProvider: NewDockerBoltDBProvider(), + TableDBProvider: NewTableBoltDBProvider(), } } @@ -819,7 +786,7 @@ func (d *DockerPlugin) Init() error { return err } - if err := d.DbInit(DockerPluginCfg.DataDir); err != nil { + if err := d.DbInit(DockerPluginCfg.DataDir, DockerPluginCfg.DbFile); err != nil { return err } if err := d.DbTableRebuild(&d.DockerNwMap); err != nil { From fc0f78bf2d9d8f6c1b21d355de146f9157bf1e7e Mon Sep 17 00:00:00 2001 From: Manohar Castelino Date: Fri, 14 Oct 2016 17:49:26 -0700 Subject: [PATCH 2/2] Networking: Rename docker_database.go to database.go The database implementation is now generic and works with any table. Hence rename docker_database.go to database.go Signed-off-by: Manohar Castelino --- networking/libsnnet/{docker_database.go => database.go} | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) rename networking/libsnnet/{docker_database.go => database.go} (97%) diff --git a/networking/libsnnet/docker_database.go b/networking/libsnnet/database.go similarity index 97% rename from networking/libsnnet/docker_database.go rename to networking/libsnnet/database.go index 9d3dfd00c..9b932401f 100644 --- a/networking/libsnnet/docker_database.go +++ b/networking/libsnnet/database.go @@ -44,13 +44,13 @@ type DbTable interface { Add(k string, v interface{}) error } -// A TableDBProvider represents a persistent data base provider +// A TableDBProvider represents a persistent database provider // that can be used to store map, arrays or any other type of // tables into a database type TableDBProvider interface { //Initializes the Database DbInit(dir string, file string) error - //Populates the DockerPlugin cache from the database + //Populates the in-memory table from the database DbTableRebuild(table DbTable) error //Closes the database DbClose() error