feat: persist and transfer script command payloads
This commit is contained in:
@@ -1,7 +1,9 @@
|
||||
package store
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"context"
|
||||
"crypto/sha256"
|
||||
"crypto/subtle"
|
||||
"database/sql"
|
||||
"errors"
|
||||
@@ -9,7 +11,9 @@ import (
|
||||
"time"
|
||||
|
||||
"github.com/klauspost/compress/zstd"
|
||||
rvboxv1 "github.com/rvbox/rvbox/gen/go/rvbox/v1"
|
||||
"github.com/rvbox/rvbox/internal/domain"
|
||||
"google.golang.org/protobuf/proto"
|
||||
)
|
||||
|
||||
var (
|
||||
@@ -28,6 +32,12 @@ type QueueCommandInput struct {
|
||||
QueueExpiryTime *time.Time
|
||||
ImmutableSHA256 [32]byte
|
||||
ExecutionSpec []byte
|
||||
// ScriptPresent distinguishes a valid zero-byte script from a command-text
|
||||
// request with no script payload. When true, ScriptContent is checked
|
||||
// against the descriptor embedded in ExecutionSpec and stored as a
|
||||
// command-owned compressed payload.
|
||||
ScriptPresent bool
|
||||
ScriptContent []byte
|
||||
}
|
||||
|
||||
type QueueCommandResult struct {
|
||||
@@ -47,7 +57,18 @@ func (store *Store) QueueCommand(ctx context.Context, input QueueCommandInput) (
|
||||
if err != nil {
|
||||
return QueueCommandResult{}, err
|
||||
}
|
||||
charge, err := EstimateCharge(ChargeInput{EncodedBytes: uint64(len(stored)), SQLiteRows: 1, IndexEntries: 2})
|
||||
scriptStored, scriptDescriptor, err := validateAndCompressScript(input)
|
||||
if err != nil {
|
||||
return QueueCommandResult{}, err
|
||||
}
|
||||
rows, indexes := uint64(1), uint64(2)
|
||||
encodedBytes := uint64(len(stored))
|
||||
if input.ScriptPresent {
|
||||
rows++
|
||||
indexes++
|
||||
encodedBytes += uint64(len(scriptStored))
|
||||
}
|
||||
charge, err := EstimateCharge(ChargeInput{EncodedBytes: encodedBytes, SQLiteRows: rows, IndexEntries: indexes})
|
||||
if err != nil {
|
||||
return QueueCommandResult{}, err
|
||||
}
|
||||
@@ -94,7 +115,7 @@ FROM clients JOIN storage_counters ON storage_counters.singleton = 1 WHERE clien
|
||||
decision, err := CheckReservation(store.quotaLimits, ReservationState{
|
||||
ClientTotalCharged: clientCharged, ServerTotalCharged: serverCharged,
|
||||
CloseoutRemaining: store.quotaLimits.CloseoutReserveBytes, FilesystemFreeBytes: freeBytes,
|
||||
}, ReservationRequest{ChargedBytes: charge, PhysicalBytes: uint64(len(stored))})
|
||||
}, ReservationRequest{ChargedBytes: charge, PhysicalBytes: encodedBytes})
|
||||
if err != nil {
|
||||
return QueueCommandResult{}, err
|
||||
}
|
||||
@@ -109,10 +130,17 @@ closeout_remaining_bytes, immutable_request_sha256, execution_spec,
|
||||
execution_spec_raw_bytes, execution_spec_stored_bytes, execution_spec_compression
|
||||
) VALUES (?, ?, ?, ?, ?, 1, 1, ?, ?, ?, ?, ?, ?, ?, 2)`,
|
||||
input.IssueUUID[:], input.ClientID, input.IssueTime.UTC().UnixNano(), input.ReceiptTime.UTC().UnixNano(), expiry,
|
||||
len(stored), charge, decision.CloseoutRemaining, input.ImmutableSHA256[:], stored, len(input.ExecutionSpec), len(stored))
|
||||
encodedBytes, charge, decision.CloseoutRemaining, input.ImmutableSHA256[:], stored, len(input.ExecutionSpec), len(stored))
|
||||
if err != nil {
|
||||
return QueueCommandResult{}, err
|
||||
}
|
||||
if input.ScriptPresent {
|
||||
if _, err := tx.ExecContext(ctx, `INSERT INTO command_payloads (
|
||||
issue_uuid, kind, raw_bytes, stored_bytes, compression, sha256, inline_data, segment_path
|
||||
) VALUES (?, 'script', ?, ?, 2, ?, ?, NULL)`, input.IssueUUID[:], scriptDescriptor.GetSizeBytes(), len(scriptStored), scriptDescriptor.GetSha256(), scriptStored); err != nil {
|
||||
return QueueCommandResult{}, err
|
||||
}
|
||||
}
|
||||
if _, err := tx.ExecContext(ctx, `UPDATE clients SET charged_bytes = ? WHERE client_id = ?`, decision.ClientTotalCharged, input.ClientID); err != nil {
|
||||
return QueueCommandResult{}, err
|
||||
}
|
||||
@@ -129,9 +157,37 @@ func validCommandInput(input QueueCommandInput) bool {
|
||||
if isZeroUUID([16]byte(input.IssueUUID)) || input.ClientID == "" || len(input.ClientID) > 128 || input.IssueTime.IsZero() || input.ReceiptTime.IsZero() || len(input.ExecutionSpec) == 0 || allZero(input.ImmutableSHA256[:]) {
|
||||
return false
|
||||
}
|
||||
if !input.ScriptPresent && len(input.ScriptContent) != 0 {
|
||||
return false
|
||||
}
|
||||
return input.QueueExpiryTime == nil || input.QueueExpiryTime.After(input.IssueTime)
|
||||
}
|
||||
|
||||
const maxStoredScriptBytes = 10 << 20
|
||||
|
||||
func validateAndCompressScript(input QueueCommandInput) ([]byte, *rvboxv1.ScriptDescriptor, error) {
|
||||
if !input.ScriptPresent {
|
||||
return nil, nil, nil
|
||||
}
|
||||
var spec rvboxv1.ExecutionSpec
|
||||
if err := proto.Unmarshal(input.ExecutionSpec, &spec); err != nil {
|
||||
return nil, nil, fmt.Errorf("decode execution spec for script payload: %w", err)
|
||||
}
|
||||
descriptor := spec.GetScript()
|
||||
if descriptor == nil || descriptor.GetSizeBytes() > maxStoredScriptBytes || len(descriptor.GetSha256()) != sha256.Size || uint64(len(input.ScriptContent)) != descriptor.GetSizeBytes() {
|
||||
return nil, nil, errors.New("script payload does not match execution descriptor")
|
||||
}
|
||||
digest := sha256.Sum256(input.ScriptContent)
|
||||
if !bytes.Equal(digest[:], descriptor.GetSha256()) {
|
||||
return nil, nil, errors.New("script payload digest does not match execution descriptor")
|
||||
}
|
||||
stored, err := compressCommandSpec(input.ScriptContent)
|
||||
if err != nil {
|
||||
return nil, nil, err
|
||||
}
|
||||
return stored, descriptor, nil
|
||||
}
|
||||
|
||||
func compressCommandSpec(spec []byte) ([]byte, error) {
|
||||
encoder, err := zstd.NewWriter(nil, zstd.WithEncoderConcurrency(1))
|
||||
if err != nil {
|
||||
|
||||
Reference in New Issue
Block a user