Files
cgrates/ers/libers.go
ionutboangiu d0a435aa6d Migrate to new jetstream API
Upgraded go.mod nats version due to an issue caused by version mismatch
between driver and server (uncertain).

Renamed function from getProcessOptions to getProcessedOptions.

## *NatsER.Serve

- Replaced ChanQueueSubscribe with QueueSubscribe for Core NATS consumer
to handle the message processing directly.

- Since QueueSubscribe is now used regardless of jetstream status, the
message handler has been assigned to a separate variable that can be
reused.

-  The message handler is now dealing with the message processing
directly, therefore the select case listening for the channel which is
feeding NATS messages can be removed together with the channel itself
and the select. Currently, the goroutine within Serve only has to block
until the rdrExit chan is closed.

- Moved the resource check inside the handler right before starting the
message processing goroutine.

## *NatsEE.parseOpts

- Renamed function from parseOpt to parseOpts.

- Handled the error coming from GetNatsOpts function.

## *NatsEE.Connect

- Updated function to return early in case of non-nil nats.Conn value
to reduce nesting.

## *NatsEE.ExportEvent

- Use defer to release resources and RUnlock.

## *NatsEE.Close

- Use defer to Unlock.

- Update function to return early in case of nil nats.Conn value to
reduce nesting.

## ees.GetNatsOpts

- Chose switch over if else when parsing client certificate and keys
opts.

- Updated function to return the errors directly instead of assigning
them to a separate variable right before returning.

## ers.GetNatsOpts

- Chose switch over if else when parsing client certificate and keys
opts.

- Updated function to return the errors directly instead of assigning
them to a separate variable right before returning.

Removed tab from commented natsJetStreamMaxWaitProcessed option
value in config_defaults.go under ers section.

Added integration test for ERs NATS.

Updated ees/ers implementation to use the jetstream package which
separates the jetstream context from Core NATS.

Removed the jsOpts fields from the NatsEE struct. We are now using the
jetStreamMaxWait option directly through a timeout context.

Added streamName option for NATS reader since it is now required to be
specified when creating a consumer (it is not inferred based on subject
anymore).

Updated nats ers integration tests.

Updated tests to also use the new jetstream package.

Updated tests to start the nats-server using their official driver
instead of using the std go exec package. time.Sleeps are now not
required anymore to wait for the server.

In test configurations for nats readers, made sure that natsStreamName
option is populated. It is now required for consumers to know where to
subscribe.
2023-09-28 20:48:39 +02:00

278 lines
8.2 KiB
Go

/*
Real-time Online/Offline Charging System (OCS) for Telecom & ISP environments
Copyright (C) ITsysCOM GmbH
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>
*/
package ers
import (
"fmt"
"sort"
"github.com/cgrates/cgrates/agents"
"github.com/cgrates/cgrates/config"
"github.com/cgrates/cgrates/engine"
"github.com/cgrates/cgrates/utils"
)
// getProcessedOptions assigns all non-nil fields ending in "Processed" from EventReaderOpts to their counterparts in EventExporterOpts
func getProcessedOptions(erOpts *config.EventReaderOpts) *config.EventExporterOpts {
var eeOpts *config.EventExporterOpts
if erOpts.AMQPExchangeProcessed != nil {
if eeOpts == nil {
eeOpts = new(config.EventExporterOpts)
}
eeOpts.AMQPExchange = erOpts.AMQPExchangeProcessed
}
if erOpts.AMQPExchangeTypeProcessed != nil {
if eeOpts == nil {
eeOpts = new(config.EventExporterOpts)
}
eeOpts.AMQPExchangeType = erOpts.AMQPExchangeTypeProcessed
}
if erOpts.AMQPQueueIDProcessed != nil {
if eeOpts == nil {
eeOpts = new(config.EventExporterOpts)
}
eeOpts.AMQPQueueID = erOpts.AMQPQueueIDProcessed
}
if erOpts.AMQPRoutingKeyProcessed != nil {
if eeOpts == nil {
eeOpts = new(config.EventExporterOpts)
}
eeOpts.AMQPRoutingKey = erOpts.AMQPRoutingKeyProcessed
}
if erOpts.AMQPUsernameProcessed != nil {
if eeOpts == nil {
eeOpts = new(config.EventExporterOpts)
}
eeOpts.AMQPUsername = erOpts.AMQPUsernameProcessed
}
if erOpts.AMQPPasswordProcessed != nil {
if eeOpts == nil {
eeOpts = new(config.EventExporterOpts)
}
eeOpts.AMQPPassword = erOpts.AMQPPasswordProcessed
}
if erOpts.AWSKeyProcessed != nil {
if eeOpts == nil {
eeOpts = new(config.EventExporterOpts)
}
eeOpts.AWSKey = erOpts.AWSKeyProcessed
}
if erOpts.AWSRegionProcessed != nil {
if eeOpts == nil {
eeOpts = new(config.EventExporterOpts)
}
eeOpts.AWSRegion = erOpts.AWSRegionProcessed
}
if erOpts.AWSSecretProcessed != nil {
if eeOpts == nil {
eeOpts = new(config.EventExporterOpts)
}
eeOpts.AWSSecret = erOpts.AWSSecretProcessed
}
if erOpts.AWSTokenProcessed != nil {
if eeOpts == nil {
eeOpts = new(config.EventExporterOpts)
}
eeOpts.AWSToken = erOpts.AWSTokenProcessed
}
if erOpts.KafkaTopicProcessed != nil {
if eeOpts == nil {
eeOpts = new(config.EventExporterOpts)
}
eeOpts.KafkaTopic = erOpts.KafkaTopicProcessed
}
if erOpts.KafkaTLSProcessed != nil {
if eeOpts == nil {
eeOpts = new(config.EventExporterOpts)
}
eeOpts.KafkaTLS = erOpts.KafkaTLSProcessed
}
if erOpts.KafkaCAPathProcessed != nil {
if eeOpts == nil {
eeOpts = new(config.EventExporterOpts)
}
eeOpts.KafkaCAPath = erOpts.KafkaCAPathProcessed
}
if erOpts.KafkaSkipTLSVerifyProcessed != nil {
if eeOpts == nil {
eeOpts = new(config.EventExporterOpts)
}
eeOpts.KafkaSkipTLSVerify = erOpts.KafkaSkipTLSVerifyProcessed
}
if erOpts.NATSCertificateAuthorityProcessed != nil {
if eeOpts == nil {
eeOpts = new(config.EventExporterOpts)
}
eeOpts.NATSCertificateAuthority = erOpts.NATSCertificateAuthorityProcessed
}
if erOpts.NATSClientCertificateProcessed != nil {
if eeOpts == nil {
eeOpts = new(config.EventExporterOpts)
}
eeOpts.NATSClientCertificate = erOpts.NATSClientCertificateProcessed
}
if erOpts.NATSClientKeyProcessed != nil {
if eeOpts == nil {
eeOpts = new(config.EventExporterOpts)
}
eeOpts.NATSClientKey = erOpts.NATSClientKeyProcessed
}
if erOpts.NATSJWTFileProcessed != nil {
if eeOpts == nil {
eeOpts = new(config.EventExporterOpts)
}
eeOpts.NATSJWTFile = erOpts.NATSJWTFileProcessed
}
if erOpts.NATSJetStreamMaxWaitProcessed != nil {
if eeOpts == nil {
eeOpts = new(config.EventExporterOpts)
}
eeOpts.NATSJetStreamMaxWait = erOpts.NATSJetStreamMaxWaitProcessed
}
if erOpts.NATSJetStreamProcessed != nil {
if eeOpts == nil {
eeOpts = new(config.EventExporterOpts)
}
eeOpts.NATSJetStream = erOpts.NATSJetStreamProcessed
}
if erOpts.NATSSeedFileProcessed != nil {
if eeOpts == nil {
eeOpts = new(config.EventExporterOpts)
}
eeOpts.NATSSeedFile = erOpts.NATSSeedFileProcessed
}
if erOpts.NATSSubjectProcessed != nil {
if eeOpts == nil {
eeOpts = new(config.EventExporterOpts)
}
eeOpts.NATSSubject = erOpts.NATSSubjectProcessed
}
if erOpts.S3BucketIDProcessed != nil {
if eeOpts == nil {
eeOpts = new(config.EventExporterOpts)
}
eeOpts.S3BucketID = erOpts.S3BucketIDProcessed
}
if erOpts.S3FolderPathProcessed != nil {
if eeOpts == nil {
eeOpts = new(config.EventExporterOpts)
}
eeOpts.S3FolderPath = erOpts.S3FolderPathProcessed
}
if erOpts.SQLDBNameProcessed != nil {
if eeOpts == nil {
eeOpts = new(config.EventExporterOpts)
}
eeOpts.SQLDBName = erOpts.SQLDBNameProcessed
}
if erOpts.SQLTableNameProcessed != nil {
if eeOpts == nil {
eeOpts = new(config.EventExporterOpts)
}
eeOpts.SQLTableName = erOpts.SQLTableNameProcessed
}
if erOpts.SQSQueueIDProcessed != nil {
if eeOpts == nil {
eeOpts = new(config.EventExporterOpts)
}
eeOpts.SQSQueueID = erOpts.SQSQueueIDProcessed
}
if erOpts.PgSSLModeProcessed != nil {
if eeOpts == nil {
eeOpts = new(config.EventExporterOpts)
}
eeOpts.PgSSLMode = erOpts.PgSSLModeProcessed
}
return eeOpts
}
// mergePartialEvents will unite the events using the reader configuration
func mergePartialEvents(cgrEvs []*utils.CGREvent, cfg *config.EventReaderCfg, fltrS *engine.FilterS, dftTnt, dftTmz, rsrSep string) (cgrEv *utils.CGREvent, err error) {
cgrEv = cgrEvs[0] // by default there is at least one event
if len(cgrEvs) != 1 { // need to merge the incoming events
// prepare the field after which the events are ordered
var ordFld string
if cfg.Opts.PartialOrderField != nil {
ordFld = *cfg.Opts.PartialOrderField
}
var ordPath config.RSRParsers
if ordPath, err = config.NewRSRParsers(ordFld, rsrSep); err != nil { // convert the option to rsrParsers
return nil, err
}
// get the field as interface in a slice
fields := make([]any, len(cgrEvs))
for i, ev := range cgrEvs {
if fields[i], err = ordPath.ParseDataProviderWithInterfaces(ev.AsDataProvider()); err != nil {
return
}
if fldStr, castStr := fields[i].(string); castStr { // attempt converting string since deserialization fails here (ie: time.Time fields)
fields[i] = utils.StringToInterface(fldStr)
}
}
//sort CGREvents based on partialOrderFieldOpt
sort.Slice(cgrEvs, func(i, j int) bool {
gt, serr := utils.GreaterThan(fields[i], fields[j], true)
if serr != nil { // save the last non nil error
err = serr
}
return !gt
})
if err != nil { // the fields are not comparable
return
}
// compose the CGREvent from slice
cgrEv = &utils.CGREvent{
Tenant: cgrEvs[0].Tenant,
ID: utils.UUIDSha1Prefix(),
Event: make(map[string]any),
APIOpts: make(map[string]any),
}
for _, ev := range cgrEvs { // merge the maps
for key, value := range ev.Event {
cgrEv.Event[key] = value
}
for key, val := range ev.APIOpts {
cgrEv.APIOpts[key] = val
}
}
}
if len(cfg.PartialCommitFields) != 0 { // apply the partial commit template
agReq := agents.NewAgentRequest(
utils.MapStorage(cgrEv.Event), nil,
nil, nil, cgrEv.APIOpts, cfg.Tenant, dftTnt,
utils.FirstNonEmpty(cfg.Timezone, dftTmz),
fltrS, nil) // create an AgentRequest
if err = agReq.SetFields(cfg.PartialCommitFields); err != nil {
utils.Logger.Warning(
fmt.Sprintf("<%s> processing partial event: <%s>, ignoring due to error: <%s>",
utils.ERs, utils.ToJSON(cgrEv), err.Error()))
return
}
if ev := utils.NMAsCGREvent(agReq.CGRRequest, agReq.Tenant,
utils.NestingSep, agReq.Opts); ev != nil { // add the modified fields in the event
for k, v := range ev.Event {
cgrEv.Event[k] = v
}
}
}
return
}