/
resources.go
884 lines (809 loc) · 26.5 KB
/
resources.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
/*
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 engine
import (
"fmt"
"math/rand"
"sort"
"sync"
"time"
"github.com/cgrates/cgrates/config"
"github.com/cgrates/cgrates/guardian"
"github.com/cgrates/cgrates/utils"
)
func init() {
rand.Seed(time.Now().UnixNano())
}
// ResourceProfile represents the user configuration for the resource
type ResourceProfile struct {
Tenant string
ID string // identifier of this resource
FilterIDs []string
ActivationInterval *utils.ActivationInterval // time when this resource becomes active and expires
UsageTTL time.Duration // auto-expire the usage after this duration
Limit float64 // limit value
AllocationMessage string // message returned by the winning resource on allocation
Blocker bool // blocker flag to stop processing on filters matched
Stored bool
Weight float64 // Weight to sort the resources
ThresholdIDs []string // Thresholds to check after changing Limit
lkID string // holds the reference towards guardian lock key
}
// TenantID returns unique identifier of the ResourceProfile in a multi-tenant environment
func (rp *ResourceProfile) TenantID() string {
return utils.ConcatenatedKey(rp.Tenant, rp.ID)
}
// resourceProfileLockKey returns the ID used to lock a resourceProfile with guardian
func resourceProfileLockKey(tnt, id string) string {
return utils.ConcatenatedKey(utils.CacheResourceProfiles, tnt, id)
}
// lock will lock the resourceProfile using guardian and store the lock within r.lkID
// if lkID is passed as argument, the lock is considered as executed
func (rp *ResourceProfile) lock(lkID string) {
if lkID == utils.EmptyString {
lkID = guardian.Guardian.GuardIDs("",
config.CgrConfig().GeneralCfg().LockingTimeout,
resourceProfileLockKey(rp.Tenant, rp.ID))
}
rp.lkID = lkID
}
// unlock will unlock the resourceProfile and clear rp.lkID
func (rp *ResourceProfile) unlock() {
if rp.lkID == utils.EmptyString {
return
}
guardian.Guardian.UnguardIDs(rp.lkID)
rp.lkID = utils.EmptyString
}
// isLocked returns the locks status of this resourceProfile
func (rp *ResourceProfile) isLocked() bool {
return rp.lkID != utils.EmptyString
}
// ResourceUsage represents an usage counted
type ResourceUsage struct {
Tenant string
ID string // Unique identifier of this ResourceUsage, Eg: FreeSWITCH UUID
ExpiryTime time.Time
Units float64 // Number of units used
}
func (ru *ResourceUsage) TenantID() string {
return utils.ConcatenatedKey(ru.Tenant, ru.ID)
}
// isActive checks ExpiryTime at some time
func (ru *ResourceUsage) isActive(atTime time.Time) bool {
return ru.ExpiryTime.IsZero() || ru.ExpiryTime.Sub(atTime) > 0
}
// clone duplicates ru
func (ru *ResourceUsage) Clone() (cln *ResourceUsage) {
cln = new(ResourceUsage)
*cln = *ru
return
}
// Resource represents a resource in the system
// not thread safe, needs locking at process level
type Resource struct {
Tenant string
ID string
Usages map[string]*ResourceUsage
TTLIdx []string // holds ordered list of ResourceIDs based on their TTL, empty if feature is disableda
lkID string // ID of the lock used when matching the resource
ttl *time.Duration // time to leave for this resource, picked up on each Resource initialization out of config
tUsage *float64 // sum of all usages
dirty *bool // the usages were modified, needs save, *bool so we only save if enabled in config
rPrf *ResourceProfile // for ordering purposes
}
// resourceLockKey returns the ID used to lock a resource with guardian
func resourceLockKey(tnt, id string) string {
return utils.ConcatenatedKey(utils.CacheResources, tnt, id)
}
// lock will lock the resource using guardian and store the lock within r.lkID
// if lkID is passed as argument, the lock is considered as executed
func (r *Resource) lock(lkID string) {
if lkID == utils.EmptyString {
lkID = guardian.Guardian.GuardIDs("",
config.CgrConfig().GeneralCfg().LockingTimeout,
resourceLockKey(r.Tenant, r.ID))
}
r.lkID = lkID
}
// unlock will unlock the resource and clear r.lkID
func (r *Resource) unlock() {
if r.lkID == utils.EmptyString {
return
}
guardian.Guardian.UnguardIDs(r.lkID)
r.lkID = utils.EmptyString
}
// isLocked returns the locks status of this resource
func (r *Resource) isLocked() bool {
return r.lkID != utils.EmptyString
}
// TenantID returns the unique ID in a multi-tenant environment
func (r *Resource) TenantID() string {
return utils.ConcatenatedKey(r.Tenant, r.ID)
}
// removeExpiredUnits removes units which are expired from the resource
func (r *Resource) removeExpiredUnits() {
var firstActive int
for _, rID := range r.TTLIdx {
if r, has := r.Usages[rID]; has && r.isActive(time.Now()) {
break
}
firstActive += 1
}
if firstActive == 0 {
return
}
for _, rID := range r.TTLIdx[:firstActive] {
ru, has := r.Usages[rID]
if !has {
continue
}
delete(r.Usages, rID)
*r.tUsage -= ru.Units
if *r.tUsage < 0 { // something went wrong
utils.Logger.Warning(
fmt.Sprintf("resetting total usage for resourceID: %s, usage smaller than 0: %f", r.ID, *r.tUsage))
r.tUsage = nil
}
}
r.TTLIdx = r.TTLIdx[firstActive:]
r.tUsage = nil
}
// TotalUsage returns the sum of all usage units
// Exported to be used in FilterS
func (r *Resource) TotalUsage() (tU float64) {
if r.tUsage == nil {
var tu float64
for _, ru := range r.Usages {
tu += ru.Units
}
r.tUsage = &tu
}
if r.tUsage != nil {
tU = *r.tUsage
}
return
}
// recordUsage records a new usage
func (r *Resource) recordUsage(ru *ResourceUsage) (err error) {
if _, hasID := r.Usages[ru.ID]; hasID {
return fmt.Errorf("duplicate resource usage with id: %s", ru.TenantID())
}
if r.ttl != nil && *r.ttl != -1 {
if *r.ttl == 0 {
return // no recording for ttl of 0
}
ru = ru.Clone() // don't influence the initial ru
ru.ExpiryTime = time.Now().Add(*r.ttl)
}
r.Usages[ru.ID] = ru
if r.tUsage != nil {
*r.tUsage += ru.Units
}
if !ru.ExpiryTime.IsZero() {
r.TTLIdx = append(r.TTLIdx, ru.ID)
}
return
}
// clearUsage clears the usage for an ID
func (r *Resource) clearUsage(ruID string) (err error) {
ru, hasIt := r.Usages[ruID]
if !hasIt {
return fmt.Errorf("cannot find usage record with id: %s", ruID)
}
if !ru.ExpiryTime.IsZero() {
for i, ruIDIdx := range r.TTLIdx {
if ruIDIdx == ruID {
r.TTLIdx = append(r.TTLIdx[:i], r.TTLIdx[i+1:]...)
break
}
}
}
if r.tUsage != nil {
*r.tUsage -= ru.Units
}
delete(r.Usages, ruID)
return
}
// Resources is an orderable list of Resources based on Weight
type Resources []*Resource
// sort based on Weight
func (rs Resources) Sort() {
sort.Slice(rs, func(i, j int) bool { return rs[i].rPrf.Weight > rs[j].rPrf.Weight })
}
// unlock will unlock resources part of this slice
func (rs Resources) unlock() {
for _, r := range rs {
r.unlock()
if r.rPrf != nil {
r.rPrf.unlock()
}
}
}
// resIDsMp returns a map of resource IDs which is used for caching
func (rs Resources) resIDsMp() (mp utils.StringMap) {
mp = make(utils.StringMap)
for _, r := range rs {
mp[r.ID] = true
}
return mp
}
func (rs Resources) tenatIDs() []string {
ids := make([]string, len(rs))
for i, r := range rs {
ids[i] = r.TenantID()
}
return ids
}
func (rs Resources) IDs() []string {
ids := make([]string, len(rs))
for i, r := range rs {
ids[i] = r.ID
}
return ids
}
// recordUsage will record the usage in all the resource limits, failing back on errors
func (rs Resources) recordUsage(ru *ResourceUsage) (err error) {
var nonReservedIdx int // index of first resource not reserved
for _, r := range rs {
if err = r.recordUsage(ru); err != nil {
utils.Logger.Warning(fmt.Sprintf("<ResourceLimits>, err: %s", err.Error()))
break
}
nonReservedIdx += 1
}
if err != nil {
for _, r := range rs[:nonReservedIdx] {
r.clearUsage(ru.ID) // best effort
}
}
return
}
// clearUsage gives back the units to the pool
func (rs Resources) clearUsage(ruTntID string) (err error) {
for _, r := range rs {
if errClear := r.clearUsage(ruTntID); errClear != nil &&
r.ttl != nil && *r.ttl != 0 { // we only consider not found error in case of ttl different than 0
utils.Logger.Warning(fmt.Sprintf("<ResourceLimits>, clear ruID: %s, err: %s", ruTntID, errClear.Error()))
err = errClear
}
}
return
}
// allocateResource attempts allocating resources for a *ResourceUsage
// simulates on dryRun
// returns utils.ErrResourceUnavailable if allocation is not possible
func (rs Resources) allocateResource(ru *ResourceUsage, dryRun bool) (alcMessage string, err error) {
if len(rs) == 0 {
return "", utils.ErrResourceUnavailable
}
// Simulate resource usage
for _, r := range rs {
r.removeExpiredUnits()
if _, hasID := r.Usages[ru.ID]; hasID && !dryRun { // update
r.clearUsage(ru.ID) // clearUsage returns error only when ru.ID does not exist in the Usages map
}
if r.rPrf == nil {
err = fmt.Errorf("empty configuration for resourceID: %s", r.TenantID())
return
}
if alcMessage == utils.EmptyString &&
(r.rPrf.Limit >= r.TotalUsage()+ru.Units || r.rPrf.Limit == -1) {
alcMessage = utils.FirstNonEmpty(r.rPrf.AllocationMessage, r.rPrf.ID)
}
}
if alcMessage == "" {
err = utils.ErrResourceUnavailable
return
}
if dryRun {
return
}
if err = rs.recordUsage(ru); err != nil {
return utils.EmptyString, err
}
return
}
// NewResourceService returns a new ResourceService
func NewResourceService(dm *DataManager, cgrcfg *config.CGRConfig,
filterS *FilterS, connMgr *ConnManager) (*ResourceService, error) {
return &ResourceService{dm: dm,
storedResources: make(utils.StringMap),
cgrcfg: cgrcfg,
filterS: filterS,
loopStoped: make(chan struct{}),
stopBackup: make(chan struct{}),
connMgr: connMgr}, nil
}
// ResourceService is the service handling resources
type ResourceService struct {
dm *DataManager // So we can load the data in cache and index it
filterS *FilterS
storedResources utils.StringMap // keep a record of resources which need saving, map[resID]bool
srMux sync.RWMutex // protects storedResources
cgrcfg *config.CGRConfig
stopBackup chan struct{} // control storing process
loopStoped chan struct{}
connMgr *ConnManager
}
// Called to start the service
func (rS *ResourceService) ListenAndServe(exitChan chan bool) error {
utils.Logger.Info(fmt.Sprintf("<%s> starting <%s> subsystem", utils.CoreS, utils.ResourceS))
go rS.runBackup() // start backup loop
e := <-exitChan
exitChan <- e // put back for the others listening for shutdown request
return nil
}
// Called to shutdown the service
func (rS *ResourceService) Shutdown() error {
utils.Logger.Info("<ResourceS> service shutdown initialized")
close(rS.stopBackup)
rS.storeResources()
utils.Logger.Info("<ResourceS> service shutdown complete")
return nil
}
// backup will regularly store resources changed to dataDB
func (rS *ResourceService) runBackup() {
storeInterval := rS.cgrcfg.ResourceSCfg().StoreInterval
if storeInterval <= 0 {
rS.loopStoped <- struct{}{}
return
}
for {
rS.storeResources()
select {
case <-rS.stopBackup:
rS.loopStoped <- struct{}{}
return
case <-time.After(storeInterval):
}
}
}
// storeResources represents one task of complete backup
func (rS *ResourceService) storeResources() {
var failedRIDs []string
for { // don't stop until we store all dirty resources
rS.srMux.Lock()
rID := rS.storedResources.GetOne()
if rID != "" {
delete(rS.storedResources, rID)
}
rS.srMux.Unlock()
if rID == "" {
break // no more keys, backup completed
}
rIf, ok := Cache.Get(utils.CacheResources, rID)
if !ok || rIf == nil {
utils.Logger.Warning(fmt.Sprintf("<%s> failed retrieving from cache resource with ID: %s", utils.ResourceS, rID))
continue
}
r := rIf.(*Resource)
r.lock(utils.EmptyString)
if err := rS.storeResource(r); err != nil {
failedRIDs = append(failedRIDs, rID) // record failure so we can schedule it for next backup
}
r.unlock()
// randomize the CPU load and give up thread control
time.Sleep(time.Duration(rand.Intn(1000)) * time.Nanosecond)
}
if len(failedRIDs) != 0 { // there were errors on save, schedule the keys for next backup
rS.srMux.Lock()
for _, rID := range failedRIDs {
rS.storedResources[rID] = true
}
rS.srMux.Unlock()
}
}
// StoreResource stores the resource in DB and corrects dirty flag
func (rS *ResourceService) storeResource(r *Resource) (err error) {
if r.dirty == nil || !*r.dirty {
return
}
if err = rS.dm.SetResource(r); err != nil {
utils.Logger.Warning(
fmt.Sprintf("<ResourceS> failed saving Resource with ID: %s, error: %s",
r.ID, err.Error()))
return
}
//since we no longer handle cache in DataManager do here a manual caching
if err = rS.dm.CacheDataFromDB(utils.ResourcesPrefix, []string{r.TenantID()}, true); err != nil {
utils.Logger.Warning(
fmt.Sprintf("<ResourceS> failed caching Resource with ID: %s, error: %s",
r.TenantID(), err.Error()))
return
}
*r.dirty = false
return
}
// storeMatchedResources will store the list of resources based on the StoreInterval
func (rS *ResourceService) storeMatchedResources(mtcRLs Resources) (err error) {
if rS.cgrcfg.ResourceSCfg().StoreInterval == 0 {
return
}
if rS.cgrcfg.ResourceSCfg().StoreInterval > 0 {
rS.srMux.Lock()
defer rS.srMux.Unlock()
}
for _, r := range mtcRLs {
if r.dirty != nil {
*r.dirty = true // mark it to be saved
if rS.cgrcfg.ResourceSCfg().StoreInterval > 0 {
rS.storedResources[r.TenantID()] = true
continue
}
if err = rS.storeResource(r); err != nil {
return
}
}
}
return
}
// processThresholds will pass the event for resource to ThresholdS
func (rS *ResourceService) processThresholds(rs Resources, argDispatcher *utils.ArgDispatcher) (err error) {
if len(rS.cgrcfg.ResourceSCfg().ThresholdSConns) == 0 {
return
}
var withErrs bool
for _, r := range rs {
var thIDs []string
if len(r.rPrf.ThresholdIDs) != 0 {
if len(r.rPrf.ThresholdIDs) == 1 &&
r.rPrf.ThresholdIDs[0] == utils.META_NONE {
continue
}
thIDs = r.rPrf.ThresholdIDs
}
thEv := &ArgsProcessEvent{ThresholdIDs: thIDs,
CGREvent: &utils.CGREvent{
Tenant: r.Tenant,
ID: utils.GenUUID(),
Event: map[string]any{
utils.EventType: utils.ResourceUpdate,
utils.ResourceID: r.ID,
utils.Usage: r.TotalUsage(),
},
},
ArgDispatcher: argDispatcher,
}
var tIDs []string
if err := rS.connMgr.Call(rS.cgrcfg.ResourceSCfg().ThresholdSConns, nil,
utils.ThresholdSv1ProcessEvent, thEv, &tIDs); err != nil &&
(len(thIDs) != 0 || err.Error() != utils.ErrNotFound.Error()) {
utils.Logger.Warning(
fmt.Sprintf("<%s> error: %s processing event %+v with %s.",
utils.ResourceS, err.Error(), thEv, utils.ThresholdS))
withErrs = true
}
}
if withErrs {
err = utils.ErrPartiallyExecuted
}
return
}
// matchingResourcesForEvent returns ordered list of matching resources which are active by the time of the call
func (rS *ResourceService) matchingResourcesForEvent(ev *utils.CGREvent,
evUUID string, usageTTL *time.Duration) (rs Resources, err error) {
var rIDs utils.StringMap
if x, ok := Cache.Get(utils.CacheEventResources, evUUID); ok { // The ResourceIDs were cached as utils.StringMap{"resID":bool}
if x == nil {
return nil, utils.ErrNotFound
}
rIDs = x.(utils.StringMap)
defer func() { // make sure we uncache if we find errors
if err != nil {
Cache.Remove(utils.CacheEventResources, evUUID,
cacheCommit(utils.NonTransactional), utils.NonTransactional)
}
}()
} else { // select the resourceIDs out of dataDB
rIDs, err = MatchingItemIDsForEvent(ev.Event,
rS.cgrcfg.ResourceSCfg().StringIndexedFields,
rS.cgrcfg.ResourceSCfg().PrefixIndexedFields,
rS.dm, utils.CacheResourceFilterIndexes, ev.Tenant,
rS.cgrcfg.ResourceSCfg().IndexedSelects,
rS.cgrcfg.ResourceSCfg().NestedFields,
)
if err != nil {
if err == utils.ErrNotFound {
Cache.Set(utils.CacheEventResources, evUUID, nil, nil, true, "") // cache negative match
}
return
}
}
evNm := utils.MapStorage{utils.MetaReq: ev.Event}
rs = make(Resources, 0, len(rIDs))
for resName := range rIDs {
lkPrflID := guardian.Guardian.GuardIDs("",
config.CgrConfig().GeneralCfg().LockingTimeout,
resourceProfileLockKey(ev.Tenant, resName))
var rPrf *ResourceProfile
if rPrf, err = rS.dm.GetResourceProfile(ev.Tenant, resName,
true, true, utils.NonTransactional); err != nil {
guardian.Guardian.UnguardIDs(lkPrflID)
if err == utils.ErrNotFound {
continue
}
rs.unlock()
return
}
rPrf.lock(lkPrflID)
if rPrf.ActivationInterval != nil && ev.Time != nil &&
!rPrf.ActivationInterval.IsActiveAtTime(*ev.Time) { // not active
rPrf.unlock()
continue
}
var pass bool
if pass, err = rS.filterS.Pass(ev.Tenant, rPrf.FilterIDs,
evNm); err != nil {
rPrf.unlock()
rs.unlock()
return nil, err
} else if !pass {
rPrf.unlock()
continue
}
lkID := guardian.Guardian.GuardIDs(utils.EmptyString,
config.CgrConfig().GeneralCfg().LockingTimeout,
resourceLockKey(rPrf.Tenant, rPrf.ID))
var r *Resource
if r, err = rS.dm.GetResource(rPrf.Tenant, rPrf.ID, true, true, ""); err != nil {
guardian.Guardian.UnguardIDs(lkID)
rPrf.unlock()
rs.unlock()
return nil, err
}
r.lock(lkID) // pass the lock into resource so we have it as reference
if rPrf.Stored && r.dirty == nil {
r.dirty = utils.BoolPointer(false)
}
if usageTTL != nil {
if *usageTTL != 0 {
r.ttl = usageTTL
}
} else if rPrf.UsageTTL >= 0 {
r.ttl = utils.DurationPointer(rPrf.UsageTTL)
}
r.rPrf = rPrf
rs = append(rs, r)
}
if len(rs) == 0 {
return nil, utils.ErrNotFound
}
// All good, convert from Map to Slice so we can sort
rs.Sort()
for i, r := range rs {
if r.rPrf.Blocker && i != len(rs)-1 { // blocker will stop processing and we are not at last index
Resources(rs[i+1:]).unlock()
rs = rs[:i+1]
break
}
}
Cache.Set(utils.CacheEventResources, evUUID, rs.resIDsMp(), nil, true, "")
return
}
// V1ResourcesForEvent returns active resource configs matching the event
func (rS *ResourceService) V1ResourcesForEvent(args utils.ArgRSv1ResourceUsage, reply *Resources) (err error) {
if args.CGREvent == nil {
return utils.NewErrMandatoryIeMissing(utils.Event)
}
if missing := utils.MissingStructFields(args.CGREvent, []string{utils.Tenant, utils.ID, utils.Event}); len(missing) != 0 { //Params missing
return utils.NewErrMandatoryIeMissing(missing...)
} else if args.UsageID == "" {
return utils.NewErrMandatoryIeMissing(utils.UsageID)
}
// RPC caching
if config.CgrConfig().CacheCfg()[utils.CacheRPCResponses].Limit != 0 {
cacheKey := utils.ConcatenatedKey(utils.ResourceSv1GetResourcesForEvent, args.TenantID())
refID := guardian.Guardian.GuardIDs("",
config.CgrConfig().GeneralCfg().LockingTimeout, cacheKey) // RPC caching needs to be atomic
defer guardian.Guardian.UnguardIDs(refID)
if itm, has := Cache.Get(utils.CacheRPCResponses, cacheKey); has {
cachedResp := itm.(*utils.CachedRPCResponse)
if cachedResp.Error == nil {
*reply = *cachedResp.Result.(*Resources)
}
return cachedResp.Error
}
defer Cache.Set(utils.CacheRPCResponses, cacheKey,
&utils.CachedRPCResponse{Result: reply, Error: err},
nil, true, utils.NonTransactional)
}
// end of RPC caching
var mtcRLs Resources
if mtcRLs, err = rS.matchingResourcesForEvent(args.CGREvent, args.UsageID, args.UsageTTL); err != nil {
return err
}
*reply = mtcRLs
mtcRLs.unlock()
return
}
// V1AuthorizeResources queries service to find if an Usage is allowed
func (rS *ResourceService) V1AuthorizeResources(args utils.ArgRSv1ResourceUsage, reply *string) (err error) {
if args.CGREvent == nil {
return utils.NewErrMandatoryIeMissing(utils.Event)
}
if missing := utils.MissingStructFields(args.CGREvent, []string{utils.Tenant, utils.ID, utils.Event}); len(missing) != 0 { //Params missing
return utils.NewErrMandatoryIeMissing(missing...)
} else if args.UsageID == "" {
return utils.NewErrMandatoryIeMissing(utils.UsageID)
}
// RPC caching
if config.CgrConfig().CacheCfg()[utils.CacheRPCResponses].Limit != 0 {
cacheKey := utils.ConcatenatedKey(utils.ResourceSv1AuthorizeResources, args.TenantID())
refID := guardian.Guardian.GuardIDs("",
config.CgrConfig().GeneralCfg().LockingTimeout, cacheKey) // RPC caching needs to be atomic
defer guardian.Guardian.UnguardIDs(refID)
if itm, has := Cache.Get(utils.CacheRPCResponses, cacheKey); has {
cachedResp := itm.(*utils.CachedRPCResponse)
if cachedResp.Error == nil {
*reply = *cachedResp.Result.(*string)
}
return cachedResp.Error
}
defer Cache.Set(utils.CacheRPCResponses, cacheKey,
&utils.CachedRPCResponse{Result: reply, Error: err},
nil, true, utils.NonTransactional)
}
// end of RPC caching
var mtcRLs Resources
if mtcRLs, err = rS.matchingResourcesForEvent(args.CGREvent, args.UsageID, args.UsageTTL); err != nil {
return err
}
defer mtcRLs.unlock()
var alcMessage string
if alcMessage, err = mtcRLs.allocateResource(
&ResourceUsage{
Tenant: args.CGREvent.Tenant,
ID: args.UsageID,
Units: args.Units}, true); err != nil {
if err == utils.ErrResourceUnavailable {
err = utils.ErrResourceUnauthorized
}
return
}
*reply = alcMessage
return
}
// V1AllocateResource is called when a resource requires allocation
func (rS *ResourceService) V1AllocateResource(args utils.ArgRSv1ResourceUsage, reply *string) (err error) {
if args.CGREvent == nil {
return utils.NewErrMandatoryIeMissing(utils.Event)
}
if missing := utils.MissingStructFields(args.CGREvent, []string{utils.Tenant, utils.ID, utils.Event}); len(missing) != 0 { //Params missing
return utils.NewErrMandatoryIeMissing(missing...)
} else if args.UsageID == "" {
return utils.NewErrMandatoryIeMissing(utils.UsageID)
}
// RPC caching
if config.CgrConfig().CacheCfg()[utils.CacheRPCResponses].Limit != 0 {
cacheKey := utils.ConcatenatedKey(utils.ResourceSv1AllocateResources, args.TenantID())
refID := guardian.Guardian.GuardIDs("",
config.CgrConfig().GeneralCfg().LockingTimeout, cacheKey) // RPC caching needs to be atomic
defer guardian.Guardian.UnguardIDs(refID)
if itm, has := Cache.Get(utils.CacheRPCResponses, cacheKey); has {
cachedResp := itm.(*utils.CachedRPCResponse)
if cachedResp.Error == nil {
*reply = *cachedResp.Result.(*string)
}
return cachedResp.Error
}
defer Cache.Set(utils.CacheRPCResponses, cacheKey,
&utils.CachedRPCResponse{Result: reply, Error: err},
nil, true, utils.NonTransactional)
}
// end of RPC caching
var mtcRLs Resources
if mtcRLs, err = rS.matchingResourcesForEvent(args.CGREvent, args.UsageID,
args.UsageTTL); err != nil {
return err
}
defer mtcRLs.unlock()
var alcMsg string
if alcMsg, err = mtcRLs.allocateResource(
&ResourceUsage{Tenant: args.CGREvent.Tenant, ID: args.UsageID,
Units: args.Units}, false); err != nil {
return
}
// index it for storing
if err = rS.storeMatchedResources(mtcRLs); err != nil {
return
}
if err = rS.processThresholds(mtcRLs, args.ArgDispatcher); err != nil {
return
}
*reply = alcMsg
return
}
// V1ReleaseResource is called when we need to clear an allocation
func (rS *ResourceService) V1ReleaseResource(args utils.ArgRSv1ResourceUsage, reply *string) (err error) {
if args.CGREvent == nil {
return utils.NewErrMandatoryIeMissing(utils.Event)
}
if missing := utils.MissingStructFields(args.CGREvent, []string{utils.Tenant, utils.ID, utils.Event}); len(missing) != 0 { //Params missing
return utils.NewErrMandatoryIeMissing(missing...)
} else if args.UsageID == "" {
return utils.NewErrMandatoryIeMissing(utils.UsageID)
}
// RPC caching
if config.CgrConfig().CacheCfg()[utils.CacheRPCResponses].Limit != 0 {
cacheKey := utils.ConcatenatedKey(utils.ResourceSv1ReleaseResources, args.TenantID())
refID := guardian.Guardian.GuardIDs("",
config.CgrConfig().GeneralCfg().LockingTimeout, cacheKey) // RPC caching needs to be atomic
defer guardian.Guardian.UnguardIDs(refID)
if itm, has := Cache.Get(utils.CacheRPCResponses, cacheKey); has {
cachedResp := itm.(*utils.CachedRPCResponse)
if cachedResp.Error == nil {
*reply = *cachedResp.Result.(*string)
}
return cachedResp.Error
}
defer Cache.Set(utils.CacheRPCResponses, cacheKey,
&utils.CachedRPCResponse{Result: reply, Error: err},
nil, true, utils.NonTransactional)
}
// end of RPC caching
var mtcRLs Resources
if mtcRLs, err = rS.matchingResourcesForEvent(args.CGREvent, args.UsageID,
args.UsageTTL); err != nil {
return
}
defer mtcRLs.unlock()
if err = mtcRLs.clearUsage(args.UsageID); err != nil {
return
}
// Handle storing
if err = rS.storeMatchedResources(mtcRLs); err != nil {
return
}
if err = rS.processThresholds(mtcRLs, args.ArgDispatcher); err != nil {
return
}
*reply = utils.OK
return
}
// GetResource returns a resource configuration
func (rS *ResourceService) V1GetResource(arg *utils.TenantID, reply *Resource) error {
if missing := utils.MissingStructFields(arg, []string{"Tenant", "ID"}); len(missing) != 0 { //Params missing
return utils.NewErrMandatoryIeMissing(missing...)
}
tnt := arg.Tenant
if tnt == utils.EmptyString {
tnt = rS.cgrcfg.GeneralCfg().DefaultTenant
}
// make sure resource is locked at process level
lkID := guardian.Guardian.GuardIDs(utils.EmptyString,
config.CgrConfig().GeneralCfg().LockingTimeout,
resourceLockKey(tnt, arg.ID))
defer guardian.Guardian.UnguardIDs(lkID)
res, err := rS.dm.GetResource(tnt, arg.ID, true, true, utils.NonTransactional)
if err != nil {
return err
} else {
*reply = *res
}
return nil
}
// Reload stops the backupLoop and restarts it
func (rS *ResourceService) Reload() {
close(rS.stopBackup)
<-rS.loopStoped // wait until the loop is done
rS.stopBackup = make(chan struct{})
go rS.runBackup()
}
// StartLoop starts the gorutine with the backup loop
func (rS *ResourceService) StartLoop() {
go rS.runBackup()
}