----------------------------------------------------------------------------- Limits index definitions from being introduced into the system based on the rateLimiter settings
(indexDef *cbgt.IndexDef)
| 100 | // Limits index definitions from being introduced into the system based on |
| 101 | // the rateLimiter settings |
| 102 | func limitIndexDef(indexDef *cbgt.IndexDef) (*cbgt.IndexDef, error) { |
| 103 | if limiter == nil || !limiter.isActive() { |
| 104 | // rateLimiter not active |
| 105 | return indexDef, nil |
| 106 | } |
| 107 | |
| 108 | if indexDef == nil { |
| 109 | return nil, fmt.Errorf("indexDef not available") |
| 110 | } |
| 111 | |
| 112 | partitionsLimit := int(getIndexPartitionsLimit()) |
| 113 | if indexDef.PlanParams.IndexPartitions > partitionsLimit { |
| 114 | return nil, |
| 115 | fmt.Errorf("partition limit exceeded (%v > %v)", |
| 116 | indexDef.PlanParams.IndexPartitions, partitionsLimit) |
| 117 | } |
| 118 | |
| 119 | replicasLimit := int(getIndexReplicasLimit()) |
| 120 | if indexDef.PlanParams.NumReplicas > replicasLimit { |
| 121 | return nil, |
| 122 | fmt.Errorf("replica limit exceeded (%v > %v)", |
| 123 | indexDef.PlanParams.NumReplicas, replicasLimit) |
| 124 | } |
| 125 | |
| 126 | bucket := indexDef.SourceName |
| 127 | scope, _, _ := GetScopeCollectionsFromIndexDef(indexDef) |
| 128 | numIndexesLimit, err := obtainIndexesLimitForScope(limiter.mgr, bucket, scope) |
| 129 | if err != nil { |
| 130 | return nil, err |
| 131 | } |
| 132 | |
| 133 | key := getBucketScopeKey(bucket, scope) |
| 134 | |
| 135 | limiter.m.Lock() |
| 136 | defer limiter.m.Unlock() |
| 137 | |
| 138 | entry, exists := limiter.indexCache[key] |
| 139 | if !exists { |
| 140 | limiter.indexCache[key] = make(map[string]struct{}) |
| 141 | limiter.pendingIndexes[indexDef.Name] = key |
| 142 | return indexDef, nil |
| 143 | } |
| 144 | |
| 145 | if _, exists := entry[indexDef.Name]; exists { |
| 146 | // allow index update |
| 147 | return indexDef, nil |
| 148 | } |
| 149 | |
| 150 | numActiveIndexes := len(entry) |
| 151 | numPendingIndexes := 0 |
| 152 | for _, v := range limiter.pendingIndexes { |
| 153 | if v == key { |
| 154 | numPendingIndexes++ |
| 155 | } |
| 156 | } |
| 157 | |
| 158 | if numIndexesLimit > 0 && |
| 159 | (numActiveIndexes >= numIndexesLimit || |
no test coverage detected