2023-03-28 18:39:22 +00:00
|
|
|
// Copyright (c) HashiCorp, Inc.
|
2023-08-11 13:12:13 +00:00
|
|
|
// SPDX-License-Identifier: BUSL-1.1
|
2023-03-28 18:39:22 +00:00
|
|
|
|
2016-05-11 08:35:27 +00:00
|
|
|
package structs
|
|
|
|
|
|
|
|
import (
|
2018-10-18 12:45:37 +00:00
|
|
|
"errors"
|
2016-05-11 08:35:27 +00:00
|
|
|
"fmt"
|
2017-04-19 23:00:11 +00:00
|
|
|
|
|
|
|
"github.com/hashicorp/consul/api"
|
2018-10-18 12:45:37 +00:00
|
|
|
multierror "github.com/hashicorp/go-multierror"
|
2016-05-11 08:35:27 +00:00
|
|
|
)
|
|
|
|
|
2016-05-11 17:58:27 +00:00
|
|
|
// TxnKVOp is used to define a single operation on the KVS inside a
|
2018-10-29 18:41:42 +00:00
|
|
|
// transaction.
|
2016-05-11 17:58:27 +00:00
|
|
|
type TxnKVOp struct {
|
2017-04-19 23:00:11 +00:00
|
|
|
Verb api.KVOp
|
2016-05-11 08:35:27 +00:00
|
|
|
DirEnt DirEntry
|
|
|
|
}
|
|
|
|
|
2016-05-11 17:58:27 +00:00
|
|
|
// TxnKVResult is used to define the result of a single operation on the KVS
|
2016-05-11 08:35:27 +00:00
|
|
|
// inside a transaction.
|
2016-05-11 20:48:03 +00:00
|
|
|
type TxnKVResult *DirEntry
|
2016-05-11 08:35:27 +00:00
|
|
|
|
2018-12-03 07:51:18 +00:00
|
|
|
// TxnNodeOp is used to define a single operation on a node in the catalog inside
|
|
|
|
// a transaction.
|
|
|
|
type TxnNodeOp struct {
|
|
|
|
Verb api.NodeOp
|
|
|
|
Node Node
|
|
|
|
}
|
|
|
|
|
|
|
|
// TxnNodeResult is used to define the result of a single operation on a node
|
|
|
|
// in the catalog inside a transaction.
|
|
|
|
type TxnNodeResult *Node
|
|
|
|
|
|
|
|
// TxnServiceOp is used to define a single operation on a service in the catalog inside
|
|
|
|
// a transaction.
|
|
|
|
type TxnServiceOp struct {
|
|
|
|
Verb api.ServiceOp
|
2018-12-03 08:41:24 +00:00
|
|
|
Node string
|
2018-12-03 07:51:18 +00:00
|
|
|
Service NodeService
|
|
|
|
}
|
|
|
|
|
|
|
|
// TxnServiceResult is used to define the result of a single operation on a service
|
|
|
|
// in the catalog inside a transaction.
|
|
|
|
type TxnServiceResult *NodeService
|
|
|
|
|
2018-10-29 18:41:42 +00:00
|
|
|
// TxnCheckOp is used to define a single operation on a health check inside a
|
|
|
|
// transaction.
|
|
|
|
type TxnCheckOp struct {
|
|
|
|
Verb api.CheckOp
|
|
|
|
Check HealthCheck
|
|
|
|
}
|
|
|
|
|
2019-11-25 17:57:35 +00:00
|
|
|
// TxnCheckResult is used to define the result of a single operation on a
|
|
|
|
// session inside a transaction.
|
2018-10-29 18:41:42 +00:00
|
|
|
type TxnCheckResult *HealthCheck
|
|
|
|
|
2019-11-25 17:57:35 +00:00
|
|
|
// TxnSessionOp is used to define a single operation on a session inside a
|
|
|
|
// transaction.
|
|
|
|
type TxnSessionOp struct {
|
|
|
|
Verb api.SessionOp
|
|
|
|
Session Session
|
|
|
|
}
|
|
|
|
|
2020-10-06 18:24:05 +00:00
|
|
|
// TxnIntentionOp is used to define a single operation on an Intention inside a
|
2018-10-18 12:45:37 +00:00
|
|
|
// transaction.
|
2020-10-06 18:24:05 +00:00
|
|
|
//
|
|
|
|
// Deprecated: see TxnOp.Intention description
|
2018-10-18 12:45:37 +00:00
|
|
|
type TxnIntentionOp IntentionRequest
|
|
|
|
|
2016-05-11 08:35:27 +00:00
|
|
|
// TxnOp is used to define a single operation inside a transaction. Only one
|
|
|
|
// of the types should be filled out per entry.
|
|
|
|
type TxnOp struct {
|
2020-10-06 18:24:05 +00:00
|
|
|
KV *TxnKVOp
|
|
|
|
Node *TxnNodeOp
|
|
|
|
Service *TxnServiceOp
|
|
|
|
Check *TxnCheckOp
|
|
|
|
Session *TxnSessionOp
|
|
|
|
|
|
|
|
// Intention was an internal-only (not exposed in API or RPC)
|
|
|
|
// implementation detail of legacy intention replication. This is
|
|
|
|
// deprecated but retained for backwards compatibility with versions
|
|
|
|
// of consul pre-dating 1.9.0. We need it for two reasons:
|
|
|
|
//
|
|
|
|
// 1. If a secondary DC is upgraded first, we need to continue to
|
|
|
|
// replicate legacy intentions UNTIL the primary DC is upgraded.
|
|
|
|
// Legacy intention replication exclusively writes using a TxnOp.
|
|
|
|
// 2. If we attempt to reprocess raft-log contents pre-dating 1.9.0
|
|
|
|
// (such as when updating a secondary DC) we need to be able to
|
|
|
|
// recreate the state machine from the snapshot and whatever raft logs are
|
|
|
|
// present.
|
2018-10-18 12:45:37 +00:00
|
|
|
Intention *TxnIntentionOp
|
2016-05-11 08:35:27 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// TxnOps is a list of operations within a transaction.
|
|
|
|
type TxnOps []*TxnOp
|
|
|
|
|
|
|
|
// TxnRequest is used to apply multiple operations to the state store in a
|
|
|
|
// single transaction
|
|
|
|
type TxnRequest struct {
|
|
|
|
Datacenter string
|
|
|
|
Ops TxnOps
|
|
|
|
WriteRequest
|
|
|
|
}
|
|
|
|
|
|
|
|
func (r *TxnRequest) RequestDatacenter() string {
|
|
|
|
return r.Datacenter
|
|
|
|
}
|
|
|
|
|
2016-05-13 00:38:25 +00:00
|
|
|
// TxnReadRequest is used as a fast path for read-only transactions that don't
|
|
|
|
// modify the state store.
|
|
|
|
type TxnReadRequest struct {
|
|
|
|
Datacenter string
|
|
|
|
Ops TxnOps
|
|
|
|
QueryOptions
|
|
|
|
}
|
|
|
|
|
|
|
|
func (r *TxnReadRequest) RequestDatacenter() string {
|
|
|
|
return r.Datacenter
|
|
|
|
}
|
|
|
|
|
2016-05-11 08:35:27 +00:00
|
|
|
// TxnError is used to return information about an error for a specific
|
|
|
|
// operation.
|
|
|
|
type TxnError struct {
|
|
|
|
OpIndex int
|
|
|
|
What string
|
|
|
|
}
|
|
|
|
|
|
|
|
// Error returns the string representation of an atomic error.
|
|
|
|
func (e TxnError) Error() string {
|
|
|
|
return fmt.Sprintf("op %d: %s", e.OpIndex, e.What)
|
|
|
|
}
|
|
|
|
|
|
|
|
// TxnErrors is a list of TxnError entries.
|
|
|
|
type TxnErrors []*TxnError
|
|
|
|
|
|
|
|
// TxnResult is used to define the result of a given operation inside a
|
|
|
|
// transaction. Only one of the types should be filled out per entry.
|
|
|
|
type TxnResult struct {
|
2019-01-16 00:54:07 +00:00
|
|
|
KV TxnKVResult `json:",omitempty"`
|
|
|
|
Node TxnNodeResult `json:",omitempty"`
|
|
|
|
Service TxnServiceResult `json:",omitempty"`
|
|
|
|
Check TxnCheckResult `json:",omitempty"`
|
2016-05-11 08:35:27 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// TxnResults is a list of TxnResult entries.
|
|
|
|
type TxnResults []*TxnResult
|
|
|
|
|
|
|
|
// TxnResponse is the structure returned by a TxnRequest.
|
|
|
|
type TxnResponse struct {
|
|
|
|
Results TxnResults
|
|
|
|
Errors TxnErrors
|
|
|
|
}
|
2016-05-13 00:38:25 +00:00
|
|
|
|
2018-10-18 12:45:37 +00:00
|
|
|
// Error returns an aggregate of all errors in this TxnResponse.
|
|
|
|
func (r TxnResponse) Error() error {
|
|
|
|
var errs error
|
|
|
|
for _, err := range r.Errors {
|
|
|
|
errs = multierror.Append(errs, errors.New(err.Error()))
|
|
|
|
}
|
|
|
|
return errs
|
|
|
|
}
|
|
|
|
|
2016-05-13 00:38:25 +00:00
|
|
|
// TxnReadResponse is the structure returned by a TxnReadRequest.
|
|
|
|
type TxnReadResponse struct {
|
|
|
|
TxnResponse
|
|
|
|
QueryMeta
|
|
|
|
}
|