2017-01-13 07:08:52 +00:00
|
|
|
// Copyright 2017 clair authors
|
2016-01-19 20:16:45 +00:00
|
|
|
//
|
|
|
|
// Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
// you may not use this file except in compliance with the License.
|
|
|
|
// You may obtain a copy of the License at
|
|
|
|
//
|
|
|
|
// http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
//
|
|
|
|
// Unless required by applicable law or agreed to in writing, software
|
|
|
|
// distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
// See the License for the specific language governing permissions and
|
|
|
|
// limitations under the License.
|
|
|
|
|
2016-01-08 19:42:07 +00:00
|
|
|
package pgsql
|
|
|
|
|
|
|
|
import (
|
2017-07-26 23:23:54 +00:00
|
|
|
"errors"
|
2016-01-08 19:42:07 +00:00
|
|
|
"time"
|
|
|
|
|
2017-05-04 17:21:25 +00:00
|
|
|
log "github.com/sirupsen/logrus"
|
|
|
|
|
2017-01-13 07:08:52 +00:00
|
|
|
"github.com/coreos/clair/pkg/commonerr"
|
2016-01-08 19:42:07 +00:00
|
|
|
)
|
|
|
|
|
2018-09-19 19:38:07 +00:00
|
|
|
const (
|
|
|
|
soiLock = `INSERT INTO lock(name, owner, until) VALUES ($1, $2, $3)`
|
2019-01-07 19:19:36 +00:00
|
|
|
searchLock = `SELECT until FROM Lock WHERE name = $1`
|
2018-09-19 19:38:07 +00:00
|
|
|
updateLock = `UPDATE Lock SET until = $3 WHERE name = $1 AND owner = $2`
|
|
|
|
removeLock = `DELETE FROM Lock WHERE name = $1 AND owner = $2`
|
|
|
|
removeLockExpired = `DELETE FROM LOCK WHERE until < CURRENT_TIMESTAMP`
|
|
|
|
)
|
|
|
|
|
2017-07-26 23:23:54 +00:00
|
|
|
var (
|
|
|
|
errLockNotFound = errors.New("lock is not in database")
|
|
|
|
)
|
|
|
|
|
2016-01-08 19:42:07 +00:00
|
|
|
// Lock tries to set a temporary lock in the database.
|
|
|
|
//
|
|
|
|
// Lock does not block, instead, it returns true and its expiration time
|
2017-07-26 23:23:54 +00:00
|
|
|
// is the lock has been successfully acquired or false otherwise.
|
|
|
|
func (tx *pgSession) Lock(name string, owner string, duration time.Duration, renew bool) (bool, time.Time, error) {
|
2016-01-08 19:42:07 +00:00
|
|
|
if name == "" || owner == "" || duration == 0 {
|
|
|
|
log.Warning("could not create an invalid lock")
|
2017-07-26 23:23:54 +00:00
|
|
|
return false, time.Time{}, commonerr.NewBadRequestError("Invalid Lock Parameters")
|
2016-01-08 19:42:07 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
until := time.Now().Add(duration)
|
|
|
|
if renew {
|
2017-07-26 23:23:54 +00:00
|
|
|
defer observeQueryTime("Lock", "update", time.Now())
|
2016-01-08 19:42:07 +00:00
|
|
|
// Renew lock.
|
2017-07-26 23:23:54 +00:00
|
|
|
r, err := tx.Exec(updateLock, name, owner, until)
|
2016-01-08 19:42:07 +00:00
|
|
|
if err != nil {
|
2017-07-26 23:23:54 +00:00
|
|
|
return false, until, handleError("updateLock", err)
|
2016-01-08 19:42:07 +00:00
|
|
|
}
|
2017-07-26 23:23:54 +00:00
|
|
|
|
|
|
|
if n, err := r.RowsAffected(); err == nil {
|
|
|
|
return n > 0, until, nil
|
2016-01-08 19:42:07 +00:00
|
|
|
}
|
2017-07-26 23:23:54 +00:00
|
|
|
return false, until, handleError("updateLock", err)
|
|
|
|
} else if err := tx.pruneLocks(); err != nil {
|
|
|
|
return false, until, err
|
2016-01-08 19:42:07 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Lock.
|
2017-07-26 23:23:54 +00:00
|
|
|
defer observeQueryTime("Lock", "soiLock", time.Now())
|
|
|
|
_, err := tx.Exec(soiLock, name, owner, until)
|
2016-01-08 19:42:07 +00:00
|
|
|
if err != nil {
|
2017-07-26 23:23:54 +00:00
|
|
|
if isErrUniqueViolation(err) {
|
2019-01-07 19:19:36 +00:00
|
|
|
// Return the existing locks expiration.
|
|
|
|
err := tx.QueryRow(searchLock, name).Scan(&until)
|
|
|
|
return false, until, handleError("searchLock", err)
|
2016-01-08 19:42:07 +00:00
|
|
|
}
|
2017-07-26 23:23:54 +00:00
|
|
|
return false, until, handleError("insertLock", err)
|
2016-01-08 19:42:07 +00:00
|
|
|
}
|
2017-07-26 23:23:54 +00:00
|
|
|
return true, until, nil
|
2016-01-08 19:42:07 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Unlock unlocks a lock specified by its name if I own it
|
2017-07-26 23:23:54 +00:00
|
|
|
func (tx *pgSession) Unlock(name, owner string) error {
|
2016-01-08 19:42:07 +00:00
|
|
|
if name == "" || owner == "" {
|
2017-07-26 23:23:54 +00:00
|
|
|
return commonerr.NewBadRequestError("Invalid Lock Parameters")
|
2016-01-08 19:42:07 +00:00
|
|
|
}
|
|
|
|
|
2016-01-24 03:02:34 +00:00
|
|
|
defer observeQueryTime("Unlock", "all", time.Now())
|
|
|
|
|
2017-07-26 23:23:54 +00:00
|
|
|
_, err := tx.Exec(removeLock, name, owner)
|
|
|
|
return err
|
2016-01-08 19:42:07 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// pruneLocks removes every expired locks from the database
|
2017-07-26 23:23:54 +00:00
|
|
|
func (tx *pgSession) pruneLocks() error {
|
2016-01-24 03:02:34 +00:00
|
|
|
defer observeQueryTime("pruneLocks", "all", time.Now())
|
|
|
|
|
2017-07-26 23:23:54 +00:00
|
|
|
if r, err := tx.Exec(removeLockExpired); err != nil {
|
|
|
|
return handleError("removeLockExpired", err)
|
|
|
|
} else if affected, err := r.RowsAffected(); err != nil {
|
|
|
|
return handleError("removeLockExpired", err)
|
|
|
|
} else {
|
|
|
|
log.Debugf("Pruned %d Locks", affected)
|
2016-01-08 19:42:07 +00:00
|
|
|
}
|
2017-07-26 23:23:54 +00:00
|
|
|
|
|
|
|
return nil
|
2016-01-08 19:42:07 +00:00
|
|
|
}
|