github.com/voedger/voedger@v0.0.0-20240520144910-273e84102129/pkg/processors/command/types.go (about)

     1  /*
     2   * Copyright (c) 2020-present unTill Pro, Ltd.
     3   */
     4  
     5  package commandprocessor
     6  
     7  import (
     8  	"context"
     9  	"time"
    10  
    11  	"github.com/voedger/voedger/pkg/appdef"
    12  	"github.com/voedger/voedger/pkg/appparts"
    13  	"github.com/voedger/voedger/pkg/iauthnz"
    14  	"github.com/voedger/voedger/pkg/iprocbus"
    15  	"github.com/voedger/voedger/pkg/isecrets"
    16  	"github.com/voedger/voedger/pkg/istructs"
    17  	payloads "github.com/voedger/voedger/pkg/itokens-payloads"
    18  	imetrics "github.com/voedger/voedger/pkg/metrics"
    19  	"github.com/voedger/voedger/pkg/pipeline"
    20  	"github.com/voedger/voedger/pkg/projectors"
    21  	"github.com/voedger/voedger/pkg/state"
    22  	coreutils "github.com/voedger/voedger/pkg/utils"
    23  	ibus "github.com/voedger/voedger/staging/src/github.com/untillpro/airs-ibus"
    24  )
    25  
    26  type ServiceFactory func(commandsChannel CommandChannel, partitionID istructs.PartitionID) pipeline.IService
    27  type CommandChannel iprocbus.ServiceChannel
    28  type OperatorSyncActualizer pipeline.ISyncOperator
    29  type SyncActualizerFactory func(vvmCtx context.Context, partitionID istructs.PartitionID) pipeline.ISyncOperator
    30  type VVMName string
    31  
    32  type ValidateFunc func(ctx context.Context, appStructs istructs.IAppStructs, cudRow istructs.ICUDRow, wsid istructs.WSID) (err error)
    33  
    34  type ICommandMessage interface {
    35  	Body() []byte
    36  	AppQName() istructs.AppQName
    37  	WSID() istructs.WSID // url WSID
    38  	Sender() ibus.ISender
    39  	PartitionID() istructs.PartitionID
    40  	RequestCtx() context.Context
    41  	QName() appdef.QName
    42  	Token() string
    43  	Host() string
    44  }
    45  
    46  type xPath string
    47  
    48  type commandProcessorMetrics struct {
    49  	vvmName string
    50  	app     istructs.AppQName
    51  	metrics imetrics.IMetrics
    52  }
    53  
    54  func (m *commandProcessorMetrics) increase(metricName string, valueDelta float64) {
    55  	m.metrics.IncreaseApp(metricName, m.vvmName, m.app, valueDelta)
    56  }
    57  
    58  type cmdWorkpiece struct {
    59  	appParts                     appparts.IAppPartitions
    60  	appPart                      appparts.IAppPartition
    61  	appStructs                   istructs.IAppStructs
    62  	requestData                  coreutils.MapObject
    63  	cmdMes                       ICommandMessage
    64  	argsObject                   istructs.IObject
    65  	unloggedArgsObject           istructs.IObject
    66  	reb                          istructs.IRawEventBuilder
    67  	rawEvent                     istructs.IRawEvent
    68  	pLogEvent                    istructs.IPLogEvent
    69  	workspace                    *workspace
    70  	idGenerator                  *implIDGenerator
    71  	eca                          istructs.ExecCommandArgs
    72  	metrics                      commandProcessorMetrics
    73  	syncProjectorsStart          time.Time
    74  	principals                   []iauthnz.Principal
    75  	principalPayload             payloads.PrincipalPayload
    76  	parsedCUDs                   []parsedCUD
    77  	wsDesc                       istructs.IRecord
    78  	hostStateProvider            *hostStateProvider
    79  	wsInitialized                bool
    80  	cmdResultBuilder             istructs.IObjectBuilder
    81  	cmdResult                    istructs.IObject
    82  	resources                    istructs.IResources
    83  	cmdExec                      func(args istructs.ExecCommandArgs) error
    84  	iCommand                     appdef.ICommand
    85  	iWorkspace                   appdef.IWorkspace
    86  	appPartitionRestartScheduled bool
    87  }
    88  
    89  type implIDGenerator struct {
    90  	istructs.IIDGenerator
    91  	generatedIDs map[istructs.RecordID]istructs.RecordID
    92  }
    93  
    94  type parsedCUD struct {
    95  	opKind         iauthnz.OperationKindType
    96  	existingRecord istructs.IRecord // create -> nil
    97  	id             int64
    98  	qName          appdef.QName
    99  	fields         coreutils.MapObject
   100  	xPath          xPath
   101  }
   102  
   103  type implICommandMessage struct {
   104  	body        []byte
   105  	appQName    istructs.AppQName // need to determine where to send c.sys.Init request on create a new workspace
   106  	wsid        istructs.WSID
   107  	sender      ibus.ISender
   108  	partitionID istructs.PartitionID
   109  	requestCtx  context.Context
   110  	qName       appdef.QName
   111  	token       string
   112  	host        string
   113  }
   114  
   115  type wrongArgsCatcher struct {
   116  	pipeline.NOOP
   117  }
   118  
   119  type hostStateProvider struct {
   120  	as               istructs.IAppStructs
   121  	cud              istructs.ICUD
   122  	wsid             istructs.WSID
   123  	principals       []iauthnz.Principal
   124  	state            state.IHostState
   125  	token            string
   126  	cmdResultBuilder istructs.IObjectBuilder
   127  	wlogOffset       istructs.Offset
   128  }
   129  
   130  func newHostStateProvider(ctx context.Context, pid istructs.PartitionID, secretReader isecrets.ISecretReader) *hostStateProvider {
   131  	p := &hostStateProvider{}
   132  	// TODO: provide ArgFunc & UnloggedArgFunc
   133  	p.state = state.ProvideCommandProcessorStateFactory()(ctx, p.getAppStructs, state.SimplePartitionIDFunc(pid),
   134  		p.getWSID, secretReader, p.getCUD, p.getPrincipals, p.getToken, projectors.DefaultIntentsLimit,
   135  		p.getCmdResultBuilder, nil, nil, p.getWLogOffset)
   136  	return p
   137  }
   138  
   139  func (p *hostStateProvider) getAppStructs() istructs.IAppStructs { return p.as }
   140  func (p *hostStateProvider) getWSID() istructs.WSID              { return p.wsid }
   141  func (p *hostStateProvider) getCUD() istructs.ICUD               { return p.cud }
   142  func (p *hostStateProvider) getPrincipals() []iauthnz.Principal {
   143  	return p.principals
   144  }
   145  func (p *hostStateProvider) getToken() string                             { return p.token }
   146  func (p *hostStateProvider) getCmdResultBuilder() istructs.IObjectBuilder { return p.cmdResultBuilder }
   147  func (p *hostStateProvider) getWLogOffset() istructs.Offset               { return p.wlogOffset }
   148  func (p *hostStateProvider) get(appStructs istructs.IAppStructs, wsid istructs.WSID, cud istructs.ICUD, principals []iauthnz.Principal, token string,
   149  	cmdResultBuilder istructs.IObjectBuilder, wlogOffset istructs.Offset) state.IHostState {
   150  	p.as = appStructs
   151  	p.wsid = wsid
   152  	p.cud = cud
   153  	p.principals = principals
   154  	p.token = token
   155  	p.cmdResultBuilder = cmdResultBuilder
   156  	p.wlogOffset = wlogOffset
   157  	return p.state
   158  }