2022-05-27 13:33:41 +00:00
|
|
|
package state
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
2022-08-02 13:32:09 +00:00
|
|
|
"math"
|
2022-05-27 13:33:41 +00:00
|
|
|
|
|
|
|
"github.com/hashicorp/go-memdb"
|
2022-07-15 15:08:53 +00:00
|
|
|
|
|
|
|
"github.com/hashicorp/nomad/helper"
|
2022-05-27 13:33:41 +00:00
|
|
|
"github.com/hashicorp/nomad/nomad/structs"
|
|
|
|
)
|
|
|
|
|
2022-08-26 18:03:56 +00:00
|
|
|
// Variables queries all the variables and is used only for
|
2022-05-27 13:33:41 +00:00
|
|
|
// snapshot/restore and key rotation
|
2022-08-26 18:03:56 +00:00
|
|
|
func (s *StateStore) Variables(ws memdb.WatchSet) (memdb.ResultIterator, error) {
|
2022-05-27 13:33:41 +00:00
|
|
|
txn := s.db.ReadTxn()
|
|
|
|
|
2022-08-26 18:03:56 +00:00
|
|
|
iter, err := txn.Get(TableVariables, indexID)
|
2022-05-27 13:33:41 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
ws.Add(iter.WatchCh())
|
|
|
|
return iter, nil
|
|
|
|
}
|
|
|
|
|
2022-08-26 18:03:56 +00:00
|
|
|
// GetVariablesByNamespace returns an iterator that contains all
|
2022-05-27 13:33:41 +00:00
|
|
|
// variables belonging to the provided namespace.
|
2022-08-26 18:03:56 +00:00
|
|
|
func (s *StateStore) GetVariablesByNamespace(
|
2022-05-27 13:33:41 +00:00
|
|
|
ws memdb.WatchSet, namespace string) (memdb.ResultIterator, error) {
|
|
|
|
txn := s.db.ReadTxn()
|
2022-08-26 18:03:56 +00:00
|
|
|
return s.getVariablesByNamespaceImpl(txn, ws, namespace)
|
2022-07-22 14:06:35 +00:00
|
|
|
}
|
2022-05-27 13:33:41 +00:00
|
|
|
|
2022-08-26 18:03:56 +00:00
|
|
|
func (s *StateStore) getVariablesByNamespaceImpl(
|
2022-07-22 14:06:35 +00:00
|
|
|
txn *txn, ws memdb.WatchSet, namespace string) (memdb.ResultIterator, error) {
|
2022-05-27 13:33:41 +00:00
|
|
|
// Walk the entire table.
|
2022-08-26 18:03:56 +00:00
|
|
|
iter, err := txn.Get(TableVariables, indexID+"_prefix", namespace, "")
|
2022-05-27 13:33:41 +00:00
|
|
|
if err != nil {
|
2022-08-26 18:03:56 +00:00
|
|
|
return nil, fmt.Errorf("variable lookup failed: %v", err)
|
2022-05-27 13:33:41 +00:00
|
|
|
}
|
|
|
|
ws.Add(iter.WatchCh())
|
|
|
|
|
|
|
|
return iter, nil
|
|
|
|
}
|
|
|
|
|
2022-08-26 18:03:56 +00:00
|
|
|
// GetVariablesByNamespaceAndPrefix returns an iterator that contains all
|
2022-05-27 13:33:41 +00:00
|
|
|
// variables belonging to the provided namespace that match the prefix.
|
2022-08-26 18:03:56 +00:00
|
|
|
func (s *StateStore) GetVariablesByNamespaceAndPrefix(
|
2022-05-27 13:33:41 +00:00
|
|
|
ws memdb.WatchSet, namespace, prefix string) (memdb.ResultIterator, error) {
|
|
|
|
txn := s.db.ReadTxn()
|
|
|
|
|
|
|
|
// Walk the entire table.
|
2022-08-26 18:03:56 +00:00
|
|
|
iter, err := txn.Get(TableVariables, indexID+"_prefix", namespace, prefix)
|
2022-05-27 13:33:41 +00:00
|
|
|
if err != nil {
|
2022-08-26 18:03:56 +00:00
|
|
|
return nil, fmt.Errorf("variable lookup failed: %v", err)
|
2022-05-27 13:33:41 +00:00
|
|
|
}
|
|
|
|
ws.Add(iter.WatchCh())
|
|
|
|
|
|
|
|
return iter, nil
|
|
|
|
}
|
|
|
|
|
2022-08-26 18:03:56 +00:00
|
|
|
// GetVariablesByPrefix returns an iterator that contains all variables that
|
2022-06-20 03:39:00 +00:00
|
|
|
// match the prefix in any namespace. Namespace filtering is the responsibility
|
|
|
|
// of the caller.
|
2022-08-26 18:03:56 +00:00
|
|
|
func (s *StateStore) GetVariablesByPrefix(
|
2022-06-20 03:39:00 +00:00
|
|
|
ws memdb.WatchSet, prefix string) (memdb.ResultIterator, error) {
|
|
|
|
txn := s.db.ReadTxn()
|
|
|
|
|
|
|
|
// Walk the entire table.
|
2022-08-26 18:03:56 +00:00
|
|
|
iter, err := txn.Get(TableVariables, indexPath+"_prefix", prefix)
|
2022-06-20 03:39:00 +00:00
|
|
|
if err != nil {
|
2022-08-26 18:03:56 +00:00
|
|
|
return nil, fmt.Errorf("variable lookup failed: %v", err)
|
2022-06-20 03:39:00 +00:00
|
|
|
}
|
|
|
|
ws.Add(iter.WatchCh())
|
|
|
|
|
|
|
|
return iter, nil
|
|
|
|
}
|
|
|
|
|
2022-08-26 18:03:56 +00:00
|
|
|
// GetVariablesByKeyID returns an iterator that contains all
|
2022-06-01 15:48:26 +00:00
|
|
|
// variables that were encrypted with a particular key
|
2022-08-26 18:03:56 +00:00
|
|
|
func (s *StateStore) GetVariablesByKeyID(
|
2022-06-01 15:48:26 +00:00
|
|
|
ws memdb.WatchSet, keyID string) (memdb.ResultIterator, error) {
|
|
|
|
txn := s.db.ReadTxn()
|
|
|
|
|
2022-08-26 18:03:56 +00:00
|
|
|
iter, err := txn.Get(TableVariables, indexKeyID, keyID)
|
2022-06-01 15:48:26 +00:00
|
|
|
if err != nil {
|
2022-08-26 18:03:56 +00:00
|
|
|
return nil, fmt.Errorf("variable lookup failed: %v", err)
|
2022-06-01 15:48:26 +00:00
|
|
|
}
|
|
|
|
ws.Add(iter.WatchCh())
|
|
|
|
|
|
|
|
return iter, nil
|
|
|
|
}
|
|
|
|
|
2022-08-26 18:03:56 +00:00
|
|
|
// GetVariable returns a single variable at a given namespace and
|
2022-05-27 13:33:41 +00:00
|
|
|
// path.
|
2022-08-26 18:03:56 +00:00
|
|
|
func (s *StateStore) GetVariable(
|
|
|
|
ws memdb.WatchSet, namespace, path string) (*structs.VariableEncrypted, error) {
|
2022-05-27 13:33:41 +00:00
|
|
|
txn := s.db.ReadTxn()
|
|
|
|
|
2022-08-26 18:03:56 +00:00
|
|
|
// Try to fetch the variable.
|
|
|
|
watchCh, raw, err := txn.FirstWatch(TableVariables, indexID, namespace, path)
|
2022-05-27 13:33:41 +00:00
|
|
|
if err != nil { // error during fetch
|
2022-08-26 18:03:56 +00:00
|
|
|
return nil, fmt.Errorf("variable lookup failed: %v", err)
|
2022-05-27 13:33:41 +00:00
|
|
|
}
|
2022-08-22 20:37:21 +00:00
|
|
|
ws.Add(watchCh)
|
2022-05-27 13:33:41 +00:00
|
|
|
if raw == nil { // not found
|
|
|
|
return nil, nil
|
|
|
|
}
|
|
|
|
|
2022-08-26 18:03:56 +00:00
|
|
|
sv := raw.(*structs.VariableEncrypted)
|
2022-05-27 13:33:41 +00:00
|
|
|
return sv, nil
|
|
|
|
}
|
|
|
|
|
2022-08-26 18:03:56 +00:00
|
|
|
// VarSet is used to store a variable object.
|
|
|
|
func (s *StateStore) VarSet(idx uint64, sv *structs.VarApplyStateRequest) *structs.VarApplyStateResponse {
|
2022-08-15 15:19:53 +00:00
|
|
|
tx := s.db.WriteTxn(idx)
|
|
|
|
defer tx.Abort()
|
2022-05-27 13:33:41 +00:00
|
|
|
|
2022-08-15 15:19:53 +00:00
|
|
|
// Perform the actual set.
|
2022-08-26 18:03:56 +00:00
|
|
|
resp := s.varSetTxn(tx, idx, sv)
|
2022-08-15 15:19:53 +00:00
|
|
|
if resp.IsError() {
|
|
|
|
return resp
|
2022-05-27 13:33:41 +00:00
|
|
|
}
|
|
|
|
|
2022-08-15 15:19:53 +00:00
|
|
|
if err := tx.Commit(); err != nil {
|
|
|
|
return sv.ErrorResponse(idx, err)
|
2022-05-27 13:33:41 +00:00
|
|
|
}
|
2022-08-15 15:19:53 +00:00
|
|
|
return resp
|
|
|
|
}
|
|
|
|
|
2022-08-26 18:03:56 +00:00
|
|
|
// VarSetCAS is used to do a check-and-set operation on a
|
2022-08-15 15:19:53 +00:00
|
|
|
// variable. The ModifyIndex in the provided entry is used to determine if
|
2022-08-22 20:37:21 +00:00
|
|
|
// we should write the entry to the state store or not.
|
2022-08-26 18:03:56 +00:00
|
|
|
func (s *StateStore) VarSetCAS(idx uint64, sv *structs.VarApplyStateRequest) *structs.VarApplyStateResponse {
|
2022-08-15 15:19:53 +00:00
|
|
|
tx := s.db.WriteTxn(idx)
|
|
|
|
defer tx.Abort()
|
2022-05-27 13:33:41 +00:00
|
|
|
|
2022-08-26 18:03:56 +00:00
|
|
|
resp := s.varSetCASTxn(tx, idx, sv)
|
2022-08-15 15:19:53 +00:00
|
|
|
if resp.IsError() || resp.IsConflict() {
|
|
|
|
return resp
|
2022-05-27 13:33:41 +00:00
|
|
|
}
|
|
|
|
|
2022-08-15 15:19:53 +00:00
|
|
|
if err := tx.Commit(); err != nil {
|
|
|
|
return sv.ErrorResponse(idx, err)
|
|
|
|
}
|
|
|
|
return resp
|
2022-05-27 13:33:41 +00:00
|
|
|
}
|
|
|
|
|
2022-08-26 18:03:56 +00:00
|
|
|
// varSetCASTxn is the inner method used to do a CAS inside an existing
|
2022-08-15 15:19:53 +00:00
|
|
|
// transaction.
|
2022-08-26 18:03:56 +00:00
|
|
|
func (s *StateStore) varSetCASTxn(tx WriteTxn, idx uint64, req *structs.VarApplyStateRequest) *structs.VarApplyStateResponse {
|
2022-08-15 15:19:53 +00:00
|
|
|
sv := req.Var
|
2022-08-26 18:03:56 +00:00
|
|
|
raw, err := tx.First(TableVariables, indexID, sv.Namespace, sv.Path)
|
2022-05-27 13:33:41 +00:00
|
|
|
if err != nil {
|
2022-08-26 18:03:56 +00:00
|
|
|
return req.ErrorResponse(idx, fmt.Errorf("failed variable lookup: %s", err))
|
2022-08-15 15:19:53 +00:00
|
|
|
}
|
2022-08-26 18:03:56 +00:00
|
|
|
svEx, ok := raw.(*structs.VariableEncrypted)
|
2022-08-15 15:19:53 +00:00
|
|
|
|
|
|
|
// ModifyIndex of 0 means that we are doing a set-if-not-exists.
|
|
|
|
if sv.ModifyIndex == 0 && raw != nil {
|
|
|
|
return req.ConflictResponse(idx, svEx)
|
|
|
|
}
|
|
|
|
|
|
|
|
// If the ModifyIndex is set but the variable doesn't exist, return a
|
|
|
|
// plausible zero value as the conflict
|
|
|
|
if sv.ModifyIndex != 0 && raw == nil {
|
2022-08-26 18:03:56 +00:00
|
|
|
zeroVal := &structs.VariableEncrypted{
|
|
|
|
VariableMetadata: structs.VariableMetadata{
|
2022-08-15 15:19:53 +00:00
|
|
|
Namespace: sv.Namespace,
|
|
|
|
Path: sv.Path,
|
|
|
|
},
|
|
|
|
}
|
|
|
|
return req.ConflictResponse(idx, zeroVal)
|
2022-05-27 13:33:41 +00:00
|
|
|
}
|
|
|
|
|
2022-08-15 15:19:53 +00:00
|
|
|
// If the existing index does not match the provided CAS index arg, then we
|
|
|
|
// shouldn't update anything and can safely return early here.
|
|
|
|
if ok && sv.ModifyIndex != svEx.ModifyIndex {
|
|
|
|
return req.ConflictResponse(idx, svEx)
|
|
|
|
}
|
|
|
|
|
|
|
|
// If we made it this far, we should perform the set.
|
2022-08-26 18:03:56 +00:00
|
|
|
return s.varSetTxn(tx, idx, req)
|
2022-08-15 15:19:53 +00:00
|
|
|
}
|
|
|
|
|
2022-08-26 18:03:56 +00:00
|
|
|
// varSetTxn is used to insert or update a variable in the state
|
2022-08-15 15:19:53 +00:00
|
|
|
// store. It is the inner method used and handles only the actual storage.
|
2022-08-26 18:03:56 +00:00
|
|
|
func (s *StateStore) varSetTxn(tx WriteTxn, idx uint64, req *structs.VarApplyStateRequest) *structs.VarApplyStateResponse {
|
2022-08-15 15:19:53 +00:00
|
|
|
sv := req.Var
|
2022-08-26 18:03:56 +00:00
|
|
|
existingRaw, err := tx.First(TableVariables, indexID, sv.Namespace, sv.Path)
|
2022-06-27 17:17:22 +00:00
|
|
|
if err != nil {
|
2022-08-15 15:19:53 +00:00
|
|
|
return req.ErrorResponse(idx, fmt.Errorf("failed sve lookup: %s", err))
|
|
|
|
}
|
2022-08-26 18:03:56 +00:00
|
|
|
existing, _ := existingRaw.(*structs.VariableEncrypted)
|
2022-08-15 15:19:53 +00:00
|
|
|
|
2022-08-26 18:03:56 +00:00
|
|
|
existingQuota, err := tx.First(TableVariablesQuotas, indexID, sv.Namespace)
|
2022-08-15 15:19:53 +00:00
|
|
|
if err != nil {
|
2022-08-26 18:03:56 +00:00
|
|
|
return req.ErrorResponse(idx, fmt.Errorf("variable quota lookup failed: %v", err))
|
2022-06-27 17:17:22 +00:00
|
|
|
}
|
|
|
|
|
2022-08-02 13:32:09 +00:00
|
|
|
var quotaChange int64
|
2022-07-15 15:08:53 +00:00
|
|
|
|
2022-08-15 15:19:53 +00:00
|
|
|
// Set the CreateIndex and CreateTime
|
2022-05-27 13:33:41 +00:00
|
|
|
if existing != nil {
|
2022-08-15 15:19:53 +00:00
|
|
|
sv.CreateIndex = existing.CreateIndex
|
|
|
|
sv.CreateTime = existing.CreateTime
|
|
|
|
|
|
|
|
if existing.Equals(*sv) {
|
|
|
|
// Skip further writing in the state store if the entry is not actually
|
|
|
|
// changed. Nevertheless, the input's ModifyIndex should be reset
|
|
|
|
// since the TXN API returns a copy in the response.
|
|
|
|
sv.ModifyIndex = existing.ModifyIndex
|
|
|
|
sv.ModifyTime = existing.ModifyTime
|
|
|
|
return req.SuccessResponse(idx, nil)
|
2022-05-27 13:33:41 +00:00
|
|
|
}
|
2022-08-15 15:19:53 +00:00
|
|
|
sv.ModifyIndex = idx
|
|
|
|
quotaChange = int64(len(sv.Data) - len(existing.Data))
|
2022-05-27 13:33:41 +00:00
|
|
|
} else {
|
2022-08-15 15:19:53 +00:00
|
|
|
sv.CreateIndex = idx
|
|
|
|
sv.ModifyIndex = idx
|
2022-08-02 13:32:09 +00:00
|
|
|
quotaChange = int64(len(sv.Data))
|
2022-05-27 13:33:41 +00:00
|
|
|
}
|
|
|
|
|
2022-08-26 18:03:56 +00:00
|
|
|
if err := tx.Insert(TableVariables, sv); err != nil {
|
|
|
|
return req.ErrorResponse(idx, fmt.Errorf("failed inserting variable: %s", err))
|
2022-05-27 13:33:41 +00:00
|
|
|
}
|
2022-06-27 17:17:22 +00:00
|
|
|
|
2022-08-02 13:32:09 +00:00
|
|
|
// Track quota usage
|
2022-08-26 18:03:56 +00:00
|
|
|
var quotaUsed *structs.VariablesQuota
|
2022-08-02 13:32:09 +00:00
|
|
|
if existingQuota != nil {
|
2022-08-26 18:03:56 +00:00
|
|
|
quotaUsed = existingQuota.(*structs.VariablesQuota)
|
2022-08-02 13:32:09 +00:00
|
|
|
quotaUsed = quotaUsed.Copy()
|
|
|
|
} else {
|
2022-08-26 18:03:56 +00:00
|
|
|
quotaUsed = &structs.VariablesQuota{
|
2022-08-02 13:32:09 +00:00
|
|
|
Namespace: sv.Namespace,
|
2022-08-15 15:19:53 +00:00
|
|
|
CreateIndex: idx,
|
2022-07-15 15:08:53 +00:00
|
|
|
}
|
2022-08-02 13:32:09 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if quotaChange > math.MaxInt64-quotaUsed.Size {
|
|
|
|
// this limit is actually shared across all namespaces in the region's
|
|
|
|
// quota (if there is one), but we need this check here to prevent
|
|
|
|
// overflow as well
|
2022-08-26 18:03:56 +00:00
|
|
|
return req.ErrorResponse(idx, fmt.Errorf("variables can store a maximum of %d bytes of encrypted data per namespace", math.MaxInt))
|
2022-08-02 13:32:09 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if quotaChange > 0 {
|
|
|
|
quotaUsed.Size += quotaChange
|
|
|
|
} else if quotaChange < 0 {
|
|
|
|
quotaUsed.Size -= helper.Min(quotaUsed.Size, -quotaChange)
|
|
|
|
}
|
|
|
|
|
2022-08-26 18:03:56 +00:00
|
|
|
err = s.enforceVariablesQuota(idx, tx, sv.Namespace, quotaChange)
|
2022-08-02 13:32:09 +00:00
|
|
|
if err != nil {
|
2022-08-15 15:19:53 +00:00
|
|
|
return req.ErrorResponse(idx, err)
|
2022-08-02 13:32:09 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// we check enforcement above even if there's no change because another
|
|
|
|
// namespace may have used up quota to make this no longer valid, but we
|
|
|
|
// only update the table if this namespace has changed
|
|
|
|
if quotaChange != 0 {
|
2022-08-15 15:19:53 +00:00
|
|
|
quotaUsed.ModifyIndex = idx
|
2022-08-26 18:03:56 +00:00
|
|
|
if err := tx.Insert(TableVariablesQuotas, quotaUsed); err != nil {
|
|
|
|
return req.ErrorResponse(idx, fmt.Errorf("variable quota insert failed: %v", err))
|
2022-06-27 17:17:22 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-08-15 15:19:53 +00:00
|
|
|
if err := tx.Insert(tableIndex,
|
2022-08-26 18:03:56 +00:00
|
|
|
&IndexEntry{TableVariables, idx}); err != nil {
|
|
|
|
return req.ErrorResponse(idx, fmt.Errorf("failed updating variable index: %s", err))
|
2022-08-15 15:19:53 +00:00
|
|
|
}
|
|
|
|
|
2022-08-26 18:03:56 +00:00
|
|
|
return req.SuccessResponse(idx, &sv.VariableMetadata)
|
2022-05-27 13:33:41 +00:00
|
|
|
}
|
|
|
|
|
2022-08-26 18:03:56 +00:00
|
|
|
// VarGet is used to retrieve a key/value pair from the state store.
|
|
|
|
func (s *StateStore) VarGet(ws memdb.WatchSet, namespace, path string) (uint64, *structs.VariableEncrypted, error) {
|
2022-08-15 15:19:53 +00:00
|
|
|
tx := s.db.ReadTxn()
|
|
|
|
defer tx.Abort()
|
|
|
|
|
|
|
|
return svGetTxn(tx, ws, namespace, path)
|
|
|
|
}
|
|
|
|
|
2022-08-26 18:03:56 +00:00
|
|
|
// svGetTxn is the inner method that gets a variable inside an existing
|
2022-08-15 15:19:53 +00:00
|
|
|
// transaction.
|
|
|
|
func svGetTxn(tx ReadTxn,
|
2022-08-26 18:03:56 +00:00
|
|
|
ws memdb.WatchSet, namespace, path string) (uint64, *structs.VariableEncrypted, error) {
|
2022-08-15 15:19:53 +00:00
|
|
|
|
|
|
|
// Get the table index.
|
|
|
|
idx := svMaxIndex(tx)
|
|
|
|
|
2022-08-26 18:03:56 +00:00
|
|
|
watchCh, entry, err := tx.FirstWatch(TableVariables, indexID, namespace, path)
|
2022-08-15 15:19:53 +00:00
|
|
|
if err != nil {
|
2022-08-26 18:03:56 +00:00
|
|
|
return 0, nil, fmt.Errorf("failed variable lookup: %s", err)
|
2022-05-27 13:33:41 +00:00
|
|
|
}
|
2022-08-15 15:19:53 +00:00
|
|
|
ws.Add(watchCh)
|
|
|
|
if entry != nil {
|
2022-08-26 18:03:56 +00:00
|
|
|
return idx, entry.(*structs.VariableEncrypted), nil
|
2022-05-27 13:33:41 +00:00
|
|
|
}
|
2022-08-15 15:19:53 +00:00
|
|
|
return idx, nil, nil
|
2022-05-27 13:33:41 +00:00
|
|
|
}
|
|
|
|
|
2022-08-26 18:03:56 +00:00
|
|
|
// VarDelete is used to delete a single variable in the
|
2022-08-15 15:19:53 +00:00
|
|
|
// the state store.
|
2022-08-26 18:03:56 +00:00
|
|
|
func (s *StateStore) VarDelete(idx uint64, req *structs.VarApplyStateRequest) *structs.VarApplyStateResponse {
|
2022-08-15 15:19:53 +00:00
|
|
|
tx := s.db.WriteTxn(idx)
|
|
|
|
defer tx.Abort()
|
2022-05-27 13:33:41 +00:00
|
|
|
|
2022-08-15 15:19:53 +00:00
|
|
|
// Perform the actual delete
|
|
|
|
resp := s.svDeleteTxn(tx, idx, req)
|
|
|
|
if !resp.IsOk() {
|
|
|
|
return resp
|
2022-05-27 13:33:41 +00:00
|
|
|
}
|
|
|
|
|
2022-08-15 15:19:53 +00:00
|
|
|
err := tx.Commit()
|
|
|
|
if err != nil {
|
|
|
|
return req.ErrorResponse(idx, err)
|
2022-05-27 13:33:41 +00:00
|
|
|
}
|
2022-08-15 15:19:53 +00:00
|
|
|
|
|
|
|
return resp
|
2022-05-27 13:33:41 +00:00
|
|
|
}
|
|
|
|
|
2022-08-26 18:03:56 +00:00
|
|
|
// VarDeleteCAS is used to conditionally delete a variable if and only if it has
|
|
|
|
// a given modify index. If the CAS index (cidx) specified is not equal to the
|
|
|
|
// last observed index for the given variable, then the call is a noop,
|
|
|
|
// otherwise a normal delete is invoked.
|
|
|
|
func (s *StateStore) VarDeleteCAS(idx uint64, req *structs.VarApplyStateRequest) *structs.VarApplyStateResponse {
|
2022-08-15 15:19:53 +00:00
|
|
|
tx := s.db.WriteTxn(idx)
|
|
|
|
defer tx.Abort()
|
2022-05-27 13:33:41 +00:00
|
|
|
|
2022-08-15 15:19:53 +00:00
|
|
|
resp := s.svDeleteCASTxn(tx, idx, req)
|
|
|
|
if !resp.IsOk() {
|
|
|
|
return resp
|
2022-05-27 13:33:41 +00:00
|
|
|
}
|
2022-08-15 15:19:53 +00:00
|
|
|
|
|
|
|
err := tx.Commit()
|
|
|
|
if err != nil {
|
|
|
|
return req.ErrorResponse(idx, err)
|
|
|
|
}
|
|
|
|
|
|
|
|
return resp
|
2022-05-27 13:33:41 +00:00
|
|
|
}
|
|
|
|
|
2022-08-15 15:19:53 +00:00
|
|
|
// svDeleteCASTxn is an inner method used to check the existing value
|
2022-08-26 18:03:56 +00:00
|
|
|
// of a variable within an existing transaction as part of a
|
2022-08-15 15:19:53 +00:00
|
|
|
// conditional delete.
|
2022-08-26 18:03:56 +00:00
|
|
|
func (s *StateStore) svDeleteCASTxn(tx WriteTxn, idx uint64, req *structs.VarApplyStateRequest) *structs.VarApplyStateResponse {
|
2022-08-15 15:19:53 +00:00
|
|
|
sv := req.Var
|
2022-08-26 18:03:56 +00:00
|
|
|
raw, err := tx.First(TableVariables, indexID, sv.Namespace, sv.Path)
|
2022-05-27 13:33:41 +00:00
|
|
|
if err != nil {
|
2022-08-26 18:03:56 +00:00
|
|
|
return req.ErrorResponse(idx, fmt.Errorf("failed variable lookup: %s", err))
|
2022-05-27 13:33:41 +00:00
|
|
|
}
|
2022-08-15 15:19:53 +00:00
|
|
|
|
2022-08-26 18:03:56 +00:00
|
|
|
svEx, ok := raw.(*structs.VariableEncrypted)
|
2022-08-15 15:19:53 +00:00
|
|
|
|
|
|
|
// ModifyIndex of 0 means that we are doing a delete-if-not-exists.
|
|
|
|
if sv.ModifyIndex == 0 && raw != nil {
|
|
|
|
return req.ConflictResponse(idx, svEx)
|
2022-05-27 13:33:41 +00:00
|
|
|
}
|
2022-08-15 15:19:53 +00:00
|
|
|
|
|
|
|
// If the ModifyIndex is set but the variable doesn't exist, return a
|
|
|
|
// plausible zero value as the conflict
|
|
|
|
if sv.ModifyIndex != 0 && raw == nil {
|
2022-08-26 18:03:56 +00:00
|
|
|
zeroVal := &structs.VariableEncrypted{
|
|
|
|
VariableMetadata: structs.VariableMetadata{
|
2022-08-15 15:19:53 +00:00
|
|
|
Namespace: sv.Namespace,
|
|
|
|
Path: sv.Path,
|
|
|
|
},
|
|
|
|
}
|
|
|
|
return req.ConflictResponse(idx, zeroVal)
|
2022-06-27 17:17:22 +00:00
|
|
|
}
|
2022-05-27 13:33:41 +00:00
|
|
|
|
2022-08-15 15:19:53 +00:00
|
|
|
// If the existing index does not match the provided CAS index arg, then we
|
|
|
|
// shouldn't update anything and can safely return early here.
|
|
|
|
if !ok || sv.ModifyIndex != svEx.ModifyIndex {
|
|
|
|
return req.ConflictResponse(idx, svEx)
|
2022-05-27 13:33:41 +00:00
|
|
|
}
|
2022-08-15 15:19:53 +00:00
|
|
|
|
|
|
|
// Call the actual deletion if the above passed.
|
|
|
|
return s.svDeleteTxn(tx, idx, req)
|
|
|
|
}
|
|
|
|
|
|
|
|
// svDeleteTxn is the inner method used to perform the actual deletion
|
2022-08-26 18:03:56 +00:00
|
|
|
// of a variable within an existing transaction.
|
|
|
|
func (s *StateStore) svDeleteTxn(tx WriteTxn, idx uint64, req *structs.VarApplyStateRequest) *structs.VarApplyStateResponse {
|
2022-08-15 15:19:53 +00:00
|
|
|
|
|
|
|
// Look up the entry in the state store.
|
2022-08-26 18:03:56 +00:00
|
|
|
existingRaw, err := tx.First(TableVariables, indexID, req.Var.Namespace, req.Var.Path)
|
2022-08-15 15:19:53 +00:00
|
|
|
if err != nil {
|
2022-08-26 18:03:56 +00:00
|
|
|
return req.ErrorResponse(idx, fmt.Errorf("failed variable lookup: %s", err))
|
2022-08-15 15:19:53 +00:00
|
|
|
}
|
|
|
|
if existingRaw == nil {
|
|
|
|
return req.SuccessResponse(idx, nil)
|
|
|
|
}
|
|
|
|
|
2022-08-26 18:03:56 +00:00
|
|
|
existingQuota, err := tx.First(TableVariablesQuotas, indexID, req.Var.Namespace)
|
2022-08-15 15:19:53 +00:00
|
|
|
if err != nil {
|
2022-08-26 18:03:56 +00:00
|
|
|
return req.ErrorResponse(idx, fmt.Errorf("variable quota lookup failed: %v", err))
|
2022-05-27 13:33:41 +00:00
|
|
|
}
|
|
|
|
|
2022-08-26 18:03:56 +00:00
|
|
|
sv := existingRaw.(*structs.VariableEncrypted)
|
2022-08-15 15:19:53 +00:00
|
|
|
|
2022-06-27 17:17:22 +00:00
|
|
|
// Track quota usage
|
|
|
|
if existingQuota != nil {
|
2022-08-26 18:03:56 +00:00
|
|
|
quotaUsed := existingQuota.(*structs.VariablesQuota)
|
2022-06-27 17:17:22 +00:00
|
|
|
quotaUsed = quotaUsed.Copy()
|
2022-08-02 13:32:09 +00:00
|
|
|
quotaUsed.Size -= helper.Min(quotaUsed.Size, int64(len(sv.Data)))
|
2022-08-15 15:19:53 +00:00
|
|
|
quotaUsed.ModifyIndex = idx
|
2022-08-26 18:03:56 +00:00
|
|
|
if err := tx.Insert(TableVariablesQuotas, quotaUsed); err != nil {
|
|
|
|
return req.ErrorResponse(idx, fmt.Errorf("variable quota insert failed: %v", err))
|
2022-06-27 17:17:22 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-08-26 18:03:56 +00:00
|
|
|
// Delete the variable and update the index table.
|
|
|
|
if err := tx.Delete(TableVariables, sv); err != nil {
|
|
|
|
return req.ErrorResponse(idx, fmt.Errorf("failed deleting variable entry: %s", err))
|
2022-08-15 15:19:53 +00:00
|
|
|
}
|
|
|
|
|
2022-08-26 18:03:56 +00:00
|
|
|
if err := tx.Insert(tableIndex, &IndexEntry{TableVariables, idx}); err != nil {
|
|
|
|
return req.ErrorResponse(idx, fmt.Errorf("failed updating variable index: %s", err))
|
2022-08-15 15:19:53 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return req.SuccessResponse(idx, nil)
|
|
|
|
}
|
|
|
|
|
|
|
|
// This extra indirection is to facilitate the tombstone case if it matters.
|
|
|
|
func svMaxIndex(tx ReadTxn) uint64 {
|
2022-08-26 18:03:56 +00:00
|
|
|
return maxIndexTxn(tx, TableVariables)
|
2022-08-15 15:19:53 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// WriteTxn is implemented by memdb.Txn to perform write operations.
|
|
|
|
type WriteTxn interface {
|
|
|
|
ReadTxn
|
|
|
|
Defer(func())
|
|
|
|
Delete(table string, obj interface{}) error
|
|
|
|
DeleteAll(table, index string, args ...interface{}) (int, error)
|
|
|
|
DeletePrefix(table string, index string, prefix string) (bool, error)
|
|
|
|
Insert(table string, obj interface{}) error
|
|
|
|
}
|
|
|
|
|
|
|
|
// maxIndex is a helper used to retrieve the highest known index
|
|
|
|
// amongst a set of tables in the db.
|
|
|
|
func (s *StateStore) maxIndex(tables ...string) uint64 {
|
|
|
|
tx := s.db.ReadTxn()
|
|
|
|
defer tx.Abort()
|
|
|
|
return maxIndexTxn(tx, tables...)
|
|
|
|
}
|
|
|
|
|
|
|
|
// maxIndexTxn is a helper used to retrieve the highest known index
|
|
|
|
// amongst a set of tables in the db.
|
|
|
|
func maxIndexTxn(tx ReadTxn, tables ...string) uint64 {
|
|
|
|
return maxIndexWatchTxn(tx, nil, tables...)
|
|
|
|
}
|
|
|
|
|
|
|
|
func maxIndexWatchTxn(tx ReadTxn, ws memdb.WatchSet, tables ...string) uint64 {
|
|
|
|
var lindex uint64
|
|
|
|
for _, table := range tables {
|
|
|
|
ch, ti, err := tx.FirstWatch(tableIndex, "id", table)
|
|
|
|
if err != nil {
|
|
|
|
panic(fmt.Sprintf("unknown index: %s err: %s", table, err))
|
|
|
|
}
|
|
|
|
if idx, ok := ti.(*IndexEntry); ok && idx.Value > lindex {
|
|
|
|
lindex = idx.Value
|
|
|
|
}
|
|
|
|
ws.Add(ch)
|
|
|
|
}
|
|
|
|
return lindex
|
2022-05-27 13:33:41 +00:00
|
|
|
}
|
2022-06-27 17:17:22 +00:00
|
|
|
|
2022-08-26 18:03:56 +00:00
|
|
|
// VariablesQuotas queries all the quotas and is used only for
|
2022-06-27 17:17:22 +00:00
|
|
|
// snapshot/restore and key rotation
|
2022-08-26 18:03:56 +00:00
|
|
|
func (s *StateStore) VariablesQuotas(ws memdb.WatchSet) (memdb.ResultIterator, error) {
|
2022-06-27 17:17:22 +00:00
|
|
|
txn := s.db.ReadTxn()
|
|
|
|
|
2022-08-26 18:03:56 +00:00
|
|
|
iter, err := txn.Get(TableVariablesQuotas, indexID)
|
2022-06-27 17:17:22 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
ws.Add(iter.WatchCh())
|
|
|
|
return iter, nil
|
|
|
|
}
|
|
|
|
|
2022-08-26 18:03:56 +00:00
|
|
|
// VariablesQuotaByNamespace queries for quotas for a particular namespace
|
|
|
|
func (s *StateStore) VariablesQuotaByNamespace(ws memdb.WatchSet, namespace string) (*structs.VariablesQuota, error) {
|
2022-06-27 17:17:22 +00:00
|
|
|
txn := s.db.ReadTxn()
|
2022-08-26 18:03:56 +00:00
|
|
|
watchCh, raw, err := txn.FirstWatch(TableVariablesQuotas, indexID, namespace)
|
2022-06-27 17:17:22 +00:00
|
|
|
if err != nil {
|
2022-08-26 18:03:56 +00:00
|
|
|
return nil, fmt.Errorf("variable quota lookup failed: %v", err)
|
2022-06-27 17:17:22 +00:00
|
|
|
}
|
|
|
|
ws.Add(watchCh)
|
|
|
|
|
|
|
|
if raw == nil {
|
|
|
|
return nil, nil
|
|
|
|
}
|
2022-08-26 18:03:56 +00:00
|
|
|
quotaUsed := raw.(*structs.VariablesQuota)
|
2022-06-27 17:17:22 +00:00
|
|
|
return quotaUsed, nil
|
|
|
|
}
|