2019-02-28 19:17:09 +00:00
|
|
|
// Copyright 2019 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.
|
|
|
|
|
2019-03-06 21:29:31 +00:00
|
|
|
package lock
|
2016-01-08 19:42:07 +00:00
|
|
|
|
|
|
|
import (
|
2019-03-06 21:29:31 +00:00
|
|
|
"database/sql"
|
2016-01-08 19:42:07 +00:00
|
|
|
"time"
|
|
|
|
|
2019-03-06 21:29:31 +00:00
|
|
|
"github.com/coreos/clair/database/pgsql/monitoring"
|
|
|
|
"github.com/coreos/clair/database/pgsql/util"
|
2017-05-04 17:21:25 +00:00
|
|
|
log "github.com/sirupsen/logrus"
|
2016-01-08 19:42:07 +00:00
|
|
|
)
|
|
|
|
|
2018-09-19 19:38:07 +00:00
|
|
|
const (
|
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`
|
2019-03-06 18:44:23 +00:00
|
|
|
removeLockExpired = `DELETE FROM LOCK WHERE until < $1`
|
2019-02-28 19:17:09 +00:00
|
|
|
|
2019-03-06 18:44:23 +00:00
|
|
|
soiLock = `
|
|
|
|
WITH new_lock AS (
|
2019-02-28 19:17:09 +00:00
|
|
|
INSERT INTO lock (name, owner, until)
|
2019-03-06 18:44:23 +00:00
|
|
|
SELECT CAST ($1 AS TEXT), CAST ($2 AS TEXT), CAST ($3 AS TIMESTAMP)
|
|
|
|
WHERE NOT EXISTS (SELECT id FROM lock WHERE name = $1)
|
|
|
|
RETURNING owner, until
|
2019-02-28 19:17:09 +00:00
|
|
|
)
|
|
|
|
SELECT * FROM new_lock
|
|
|
|
UNION
|
|
|
|
SELECT owner, until FROM lock WHERE name = $1`
|
2018-09-19 19:38:07 +00:00
|
|
|
)
|
|
|
|
|
2019-03-06 21:29:31 +00:00
|
|
|
func AcquireLock(tx *sql.Tx, lockName, whoami string, desiredDuration time.Duration) (bool, time.Time, error) {
|
2019-02-28 19:17:09 +00:00
|
|
|
if lockName == "" || whoami == "" || desiredDuration == 0 {
|
|
|
|
panic("invalid lock parameters")
|
2016-01-08 19:42:07 +00:00
|
|
|
}
|
|
|
|
|
2019-03-06 21:29:31 +00:00
|
|
|
if err := PruneLocks(tx); err != nil {
|
2019-02-28 19:17:09 +00:00
|
|
|
return false, time.Time{}, err
|
2016-01-08 19:42:07 +00:00
|
|
|
}
|
|
|
|
|
2019-02-28 19:17:09 +00:00
|
|
|
var (
|
2019-03-06 18:44:23 +00:00
|
|
|
desiredLockedUntil = time.Now().UTC().Add(desiredDuration)
|
2019-02-28 19:17:09 +00:00
|
|
|
|
|
|
|
lockedUntil time.Time
|
|
|
|
lockOwner string
|
|
|
|
)
|
|
|
|
|
2019-03-06 21:29:31 +00:00
|
|
|
defer monitoring.ObserveQueryTime("Lock", "soiLock", time.Now())
|
2019-02-28 19:17:09 +00:00
|
|
|
err := tx.QueryRow(soiLock, lockName, whoami, desiredLockedUntil).Scan(&lockOwner, &lockedUntil)
|
2019-03-06 21:29:31 +00:00
|
|
|
return lockOwner == whoami, lockedUntil, util.HandleError("AcquireLock", err)
|
2019-02-28 19:17:09 +00:00
|
|
|
}
|
|
|
|
|
2019-03-06 21:29:31 +00:00
|
|
|
func ExtendLock(tx *sql.Tx, lockName, whoami string, desiredDuration time.Duration) (bool, time.Time, error) {
|
2019-02-28 19:17:09 +00:00
|
|
|
if lockName == "" || whoami == "" || desiredDuration == 0 {
|
|
|
|
panic("invalid lock parameters")
|
|
|
|
}
|
|
|
|
|
|
|
|
desiredLockedUntil := time.Now().Add(desiredDuration)
|
|
|
|
|
2019-03-06 21:29:31 +00:00
|
|
|
defer monitoring.ObserveQueryTime("Lock", "update", time.Now())
|
2019-02-28 19:17:09 +00:00
|
|
|
result, err := tx.Exec(updateLock, lockName, whoami, desiredLockedUntil)
|
2016-01-08 19:42:07 +00:00
|
|
|
if err != nil {
|
2019-03-06 21:29:31 +00:00
|
|
|
return false, time.Time{}, util.HandleError("updateLock", err)
|
2016-01-08 19:42:07 +00:00
|
|
|
}
|
2019-02-28 19:17:09 +00:00
|
|
|
|
|
|
|
if numRows, err := result.RowsAffected(); err == nil {
|
|
|
|
// This is the only happy path.
|
|
|
|
return numRows > 0, desiredLockedUntil, nil
|
|
|
|
}
|
|
|
|
|
2019-03-06 21:29:31 +00:00
|
|
|
return false, time.Time{}, util.HandleError("updateLock", err)
|
2016-01-08 19:42:07 +00:00
|
|
|
}
|
|
|
|
|
2019-03-06 21:29:31 +00:00
|
|
|
func ReleaseLock(tx *sql.Tx, name, owner string) error {
|
2016-01-08 19:42:07 +00:00
|
|
|
if name == "" || owner == "" {
|
2019-02-28 19:17:09 +00:00
|
|
|
panic("invalid lock parameters")
|
2016-01-08 19:42:07 +00:00
|
|
|
}
|
|
|
|
|
2019-03-06 21:29:31 +00:00
|
|
|
defer monitoring.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
|
2019-03-06 21:29:31 +00:00
|
|
|
func PruneLocks(tx *sql.Tx) error {
|
|
|
|
defer monitoring.ObserveQueryTime("pruneLocks", "all", time.Now())
|
2016-01-24 03:02:34 +00:00
|
|
|
|
2019-03-06 18:44:23 +00:00
|
|
|
if r, err := tx.Exec(removeLockExpired, time.Now().UTC()); err != nil {
|
2019-03-06 21:29:31 +00:00
|
|
|
return util.HandleError("removeLockExpired", err)
|
2017-07-26 23:23:54 +00:00
|
|
|
} else if affected, err := r.RowsAffected(); err != nil {
|
2019-03-06 21:29:31 +00:00
|
|
|
return util.HandleError("removeLockExpired", err)
|
2017-07-26 23:23:54 +00:00
|
|
|
} 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
|
|
|
}
|