2018-10-10 23:42:57 +02:00
|
|
|
package pg
|
|
|
|
|
|
|
|
import (
|
2019-02-26 01:05:53 +01:00
|
|
|
"context"
|
2018-10-10 23:42:57 +02:00
|
|
|
"crypto/md5"
|
|
|
|
"database/sql"
|
|
|
|
"fmt"
|
2019-02-26 22:45:16 +01:00
|
|
|
"sync"
|
2018-10-10 23:42:57 +02:00
|
|
|
|
|
|
|
uuid "github.com/hashicorp/go-uuid"
|
|
|
|
"github.com/hashicorp/terraform/state"
|
|
|
|
"github.com/hashicorp/terraform/state/remote"
|
|
|
|
_ "github.com/lib/pq"
|
|
|
|
)
|
|
|
|
|
|
|
|
// RemoteClient is a remote client that stores data in a Postgres database
|
|
|
|
type RemoteClient struct {
|
|
|
|
Client *sql.DB
|
|
|
|
Name string
|
|
|
|
SchemaName string
|
|
|
|
|
2019-02-26 01:05:53 +01:00
|
|
|
// In-flight database transaction. Empty unless Locked.
|
2019-02-26 22:45:16 +01:00
|
|
|
txn *sql.Tx
|
|
|
|
txnMux sync.Mutex
|
|
|
|
info *state.LockInfo
|
2018-10-10 23:42:57 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
func (c *RemoteClient) Get() (*remote.Payload, error) {
|
|
|
|
query := `SELECT data FROM %s.%s WHERE name = $1`
|
2019-02-26 01:05:53 +01:00
|
|
|
var row *sql.Row
|
2019-02-26 22:45:16 +01:00
|
|
|
// Take exclusive access to the database transaction
|
|
|
|
c.txnMux.Lock()
|
|
|
|
defer c.txnMux.Unlock()
|
2019-02-26 01:05:53 +01:00
|
|
|
// Use the open transaction when present
|
|
|
|
if c.txn != nil {
|
|
|
|
row = c.txn.QueryRow(fmt.Sprintf(query, c.SchemaName, statesTableName), c.Name)
|
|
|
|
} else {
|
|
|
|
row = c.Client.QueryRow(fmt.Sprintf(query, c.SchemaName, statesTableName), c.Name)
|
|
|
|
}
|
2018-10-10 23:42:57 +02:00
|
|
|
var data []byte
|
|
|
|
err := row.Scan(&data)
|
2019-02-26 01:05:53 +01:00
|
|
|
switch {
|
|
|
|
case err == sql.ErrNoRows:
|
|
|
|
// No existing state returns empty.
|
2018-10-10 23:42:57 +02:00
|
|
|
return nil, nil
|
2019-02-26 01:05:53 +01:00
|
|
|
case err != nil:
|
|
|
|
return nil, err
|
|
|
|
default:
|
|
|
|
md5 := md5.Sum(data)
|
|
|
|
return &remote.Payload{
|
|
|
|
Data: data,
|
|
|
|
MD5: md5[:],
|
|
|
|
}, nil
|
2018-10-10 23:42:57 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *RemoteClient) Put(data []byte) error {
|
|
|
|
query := `INSERT INTO %s.%s (name, data) VALUES ($1, $2)
|
|
|
|
ON CONFLICT (name) DO UPDATE
|
|
|
|
SET data = $2 WHERE %s.name = $1`
|
2019-02-26 01:05:53 +01:00
|
|
|
var err error
|
2019-02-26 22:45:16 +01:00
|
|
|
// Take exclusive access to the database transaction
|
|
|
|
c.txnMux.Lock()
|
|
|
|
defer c.txnMux.Unlock()
|
2019-02-26 01:05:53 +01:00
|
|
|
// Use the open transaction when present
|
|
|
|
if c.txn != nil {
|
|
|
|
_, err = c.txn.Exec(fmt.Sprintf(query, c.SchemaName, statesTableName, statesTableName), c.Name, data)
|
|
|
|
} else {
|
|
|
|
_, err = c.Client.Exec(fmt.Sprintf(query, c.SchemaName, statesTableName, statesTableName), c.Name, data)
|
|
|
|
}
|
2018-10-10 23:42:57 +02:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *RemoteClient) Delete() error {
|
|
|
|
query := `DELETE FROM %s.%s WHERE name = $1`
|
2019-02-26 01:05:53 +01:00
|
|
|
var err error
|
2019-02-26 22:45:16 +01:00
|
|
|
// Take exclusive access to the database transaction
|
|
|
|
c.txnMux.Lock()
|
|
|
|
defer c.txnMux.Unlock()
|
2019-02-26 01:05:53 +01:00
|
|
|
// Use the open transaction when present
|
|
|
|
if c.txn != nil {
|
|
|
|
_, err = c.txn.Exec(fmt.Sprintf(query, c.SchemaName, statesTableName), c.Name)
|
|
|
|
} else {
|
|
|
|
_, err = c.Client.Exec(fmt.Sprintf(query, c.SchemaName, statesTableName), c.Name)
|
|
|
|
}
|
2018-10-10 23:42:57 +02:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *RemoteClient) Lock(info *state.LockInfo) (string, error) {
|
2019-02-26 01:05:53 +01:00
|
|
|
var err error
|
|
|
|
var lockID string
|
|
|
|
var txn *sql.Tx
|
2018-10-10 23:42:57 +02:00
|
|
|
|
|
|
|
if info.ID == "" {
|
2019-02-26 01:05:53 +01:00
|
|
|
lockID, err = uuid.GenerateUUID()
|
2018-10-10 23:42:57 +02:00
|
|
|
if err != nil {
|
|
|
|
return "", err
|
|
|
|
}
|
2019-02-26 01:05:53 +01:00
|
|
|
info.Operation = "client"
|
2018-10-10 23:42:57 +02:00
|
|
|
info.ID = lockID
|
|
|
|
}
|
|
|
|
|
2019-02-26 22:45:16 +01:00
|
|
|
// Take exclusive access to the database transaction
|
|
|
|
c.txnMux.Lock()
|
|
|
|
defer c.txnMux.Unlock()
|
|
|
|
|
2019-02-26 01:05:53 +01:00
|
|
|
if c.txn == nil {
|
|
|
|
// Most strict transaction isolation to prevent cross-talk
|
|
|
|
// between incomplete state transactions.
|
|
|
|
txn, err = c.Client.BeginTx(context.Background(), &sql.TxOptions{
|
|
|
|
Isolation: sql.LevelSerializable,
|
|
|
|
})
|
|
|
|
if err != nil {
|
2019-02-26 22:20:40 +01:00
|
|
|
return "", &state.LockError{Info: info, Err: err}
|
2018-10-10 23:42:57 +02:00
|
|
|
}
|
2019-02-26 01:05:53 +01:00
|
|
|
c.txn = txn
|
|
|
|
} else {
|
2019-02-26 22:20:40 +01:00
|
|
|
return "", &state.LockError{Info: info, Err: fmt.Errorf("Client is already in a locking transaction")}
|
2018-10-10 23:42:57 +02:00
|
|
|
}
|
|
|
|
|
2019-02-26 01:05:53 +01:00
|
|
|
// Do not wait before giving up on a contended lock.
|
|
|
|
_, err = c.Client.Exec(`SET LOCAL lock_timeout = 0`)
|
2018-10-10 23:42:57 +02:00
|
|
|
if err != nil {
|
2019-02-26 01:05:53 +01:00
|
|
|
c.rollback(info)
|
2018-10-10 23:42:57 +02:00
|
|
|
return "", err
|
|
|
|
}
|
|
|
|
|
2019-02-26 01:05:53 +01:00
|
|
|
// Try to acquire lock for the existing row.
|
|
|
|
query := `SELECT pg_try_advisory_xact_lock(%s.id) FROM %s.%s WHERE %s.name = $1`
|
|
|
|
row := c.txn.QueryRow(fmt.Sprintf(query, statesTableName, c.SchemaName, statesTableName, statesTableName), c.Name)
|
|
|
|
var didLock []byte
|
|
|
|
err = row.Scan(&didLock)
|
|
|
|
switch {
|
|
|
|
case err == sql.ErrNoRows:
|
|
|
|
// When the row does not yet exist in state, take
|
|
|
|
// the `-1` lock to create the new row.
|
|
|
|
innerRow := c.txn.QueryRow(`SELECT pg_try_advisory_xact_lock(-1)`)
|
|
|
|
var innerDidLock []byte
|
|
|
|
err := innerRow.Scan(&innerDidLock)
|
|
|
|
if err != nil {
|
|
|
|
c.rollback(info)
|
2019-02-26 22:20:40 +01:00
|
|
|
return "", &state.LockError{Info: info, Err: err}
|
2018-10-10 23:42:57 +02:00
|
|
|
}
|
2019-02-26 01:05:53 +01:00
|
|
|
if string(innerDidLock) == "false" {
|
|
|
|
c.rollback(info)
|
2019-02-26 22:20:40 +01:00
|
|
|
return "", &state.LockError{Info: info, Err: fmt.Errorf("Workspace is already locked: %s", c.Name)}
|
2018-10-10 23:42:57 +02:00
|
|
|
}
|
2019-02-26 01:05:53 +01:00
|
|
|
case err != nil:
|
|
|
|
c.rollback(info)
|
2019-02-26 22:20:40 +01:00
|
|
|
return "", &state.LockError{Info: info, Err: err}
|
2019-02-26 01:05:53 +01:00
|
|
|
case string(didLock) == "false":
|
|
|
|
c.rollback(info)
|
2019-02-26 22:20:40 +01:00
|
|
|
return "", &state.LockError{Info: info, Err: fmt.Errorf("Workspace is already locked: %s", c.Name)}
|
2019-02-26 01:05:53 +01:00
|
|
|
default:
|
2018-10-10 23:42:57 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
return info.ID, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *RemoteClient) getLockInfo() (*state.LockInfo, error) {
|
2019-02-26 01:05:53 +01:00
|
|
|
return c.info, nil
|
2018-10-10 23:42:57 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
func (c *RemoteClient) Unlock(id string) error {
|
2019-02-26 22:45:16 +01:00
|
|
|
// Take exclusive access to the database transaction
|
|
|
|
c.txnMux.Lock()
|
|
|
|
defer c.txnMux.Unlock()
|
2019-02-26 01:05:53 +01:00
|
|
|
if c.txn != nil {
|
|
|
|
err := c.txn.Commit()
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
c.txn = nil
|
2018-10-10 23:42:57 +02:00
|
|
|
}
|
2019-02-26 01:05:53 +01:00
|
|
|
c.info = nil
|
|
|
|
return nil
|
|
|
|
}
|
2018-10-10 23:42:57 +02:00
|
|
|
|
2019-02-26 01:05:53 +01:00
|
|
|
// This must be called from any code path where the
|
|
|
|
// transaction would not be committed (unlocked),
|
|
|
|
// otherwise the transactions will leak and prevent
|
|
|
|
// the process from exiting cleanly.
|
2019-02-26 22:45:16 +01:00
|
|
|
//
|
|
|
|
// Does not use mutex because this will implicitly be
|
|
|
|
// called from within an already mutex'd scope.
|
2019-02-26 01:05:53 +01:00
|
|
|
func (c *RemoteClient) rollback(info *state.LockInfo) error {
|
|
|
|
if c.txn != nil {
|
|
|
|
err := c.txn.Rollback()
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
c.txn = nil
|
2018-10-10 23:42:57 +02:00
|
|
|
}
|
|
|
|
c.info = nil
|
|
|
|
return nil
|
|
|
|
}
|