Merge pull request #72 from mheon/file_locking

Move containers to file locks from c/storage
This commit is contained in:
Daniel J Walsh
2017-12-11 10:48:29 -06:00
committed by GitHub
6 changed files with 142 additions and 101 deletions

View File

@ -6,7 +6,6 @@ import (
"io/ioutil"
"os"
"github.com/containers/storage"
"github.com/pkg/errors"
"github.com/sirupsen/logrus"
@ -22,24 +21,17 @@ const DBSchema = 2
type SQLState struct {
db *sql.DB
specsDir string
lockDir string
runtime *Runtime
lock storage.Locker
valid bool
}
// NewSQLState initializes a SQL-backed state, created the database if necessary
func NewSQLState(dbPath, lockPath, specsDir string, runtime *Runtime) (State, error) {
func NewSQLState(dbPath, specsDir, lockDir string, runtime *Runtime) (State, error) {
state := new(SQLState)
state.runtime = runtime
// Make our lock file
lock, err := storage.GetLockfile(lockPath)
if err != nil {
return nil, errors.Wrapf(err, "error creating lockfile for state")
}
state.lock = lock
// Make the directory that will hold JSON copies of container runtime specs
if err := os.MkdirAll(specsDir, 0750); err != nil {
// The directory is allowed to exist
@ -49,9 +41,14 @@ func NewSQLState(dbPath, lockPath, specsDir string, runtime *Runtime) (State, er
}
state.specsDir = specsDir
// Acquire the lock while we open the database and perform initial setup
state.lock.Lock()
defer state.lock.Unlock()
// Make the directory that will hold container lockfiles
if err := os.MkdirAll(lockDir, 0750); err != nil {
// The directory is allowed to exist
if !os.IsExist(err) {
return nil, errors.Wrapf(err, "error creating lockfiles dir %s", lockDir)
}
}
state.lockDir = lockDir
// TODO add a separate temporary database for per-boot container
// state
@ -87,9 +84,6 @@ func NewSQLState(dbPath, lockPath, specsDir string, runtime *Runtime) (State, er
// Close the state's database connection
func (s *SQLState) Close() error {
s.lock.Lock()
defer s.lock.Unlock()
if !s.valid {
return ErrDBClosed
}
@ -176,7 +170,7 @@ func (s *SQLState) Container(id string) (*Container, error) {
row := s.db.QueryRow(query, id)
ctr, err := ctrFromScannable(row, s.runtime, s.specsDir)
ctr, err := ctrFromScannable(row, s.runtime, s.specsDir, s.lockDir)
if err != nil {
return nil, errors.Wrapf(err, "error retrieving container %s from database", id)
}
@ -223,7 +217,7 @@ func (s *SQLState) LookupContainer(idOrName string) (*Container, error) {
}
var err error
ctr, err = ctrFromScannable(rows, s.runtime, s.specsDir)
ctr, err = ctrFromScannable(rows, s.runtime, s.specsDir, s.lockDir)
if err != nil {
return nil, errors.Wrapf(err, "error retrieving container %s from database", idOrName)
}
@ -296,9 +290,6 @@ func (s *SQLState) AddContainer(ctr *Container) (err error) {
return errors.Wrapf(err, "error marshaling container %s labels to JSON", ctr.ID())
}
s.lock.Lock()
defer s.lock.Unlock()
tx, err := s.db.Begin()
if err != nil {
return errors.Wrapf(err, "error beginning database transaction")
@ -477,9 +468,6 @@ func (s *SQLState) SaveContainer(ctr *Container) error {
Pid=?
WHERE Id=?;`
s.lock.Lock()
defer s.lock.Unlock()
if !s.valid {
return ErrDBClosed
}
@ -537,9 +525,6 @@ func (s *SQLState) RemoveContainer(ctr *Container) error {
removeState = "DELETE FROM containerState WHERE ID=?;"
)
s.lock.Lock()
defer s.lock.Unlock()
if !s.valid {
return ErrDBClosed
}
@ -622,7 +607,7 @@ func (s *SQLState) AllContainers() ([]*Container, error) {
containers := []*Container{}
for rows.Next() {
ctr, err := ctrFromScannable(rows, s.runtime, s.specsDir)
ctr, err := ctrFromScannable(rows, s.runtime, s.specsDir, s.lockDir)
if err != nil {
return nil, err
}