khenaidoo | 1a0d622 | 2021-06-30 16:48:44 -0400 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2018-present Open Networking Foundation |
| 3 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | * you may not use this file except in compliance with the License. |
| 5 | * You may obtain a copy of the License at |
| 6 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 7 | * Unless required by applicable law or agreed to in writing, software |
| 8 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 9 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 10 | * See the License for the specific language governing permissions and |
| 11 | * limitations under the License. |
| 12 | */ |
| 13 | package meter |
| 14 | |
| 15 | import ( |
| 16 | "context" |
| 17 | "fmt" |
khenaidoo | d948f77 | 2021-08-11 17:49:24 -0400 | [diff] [blame] | 18 | "sync" |
| 19 | |
khenaidoo | 1a0d622 | 2021-06-30 16:48:44 -0400 | [diff] [blame] | 20 | "github.com/opencord/voltha-go/db/model" |
khenaidoo | d948f77 | 2021-08-11 17:49:24 -0400 | [diff] [blame] | 21 | "github.com/opencord/voltha-lib-go/v7/pkg/log" |
| 22 | ofp "github.com/opencord/voltha-protos/v5/go/openflow_13" |
khenaidoo | 1a0d622 | 2021-06-30 16:48:44 -0400 | [diff] [blame] | 23 | "google.golang.org/grpc/codes" |
| 24 | "google.golang.org/grpc/status" |
khenaidoo | 1a0d622 | 2021-06-30 16:48:44 -0400 | [diff] [blame] | 25 | ) |
| 26 | |
| 27 | // Loader hides all low-level locking & synchronization related to meter state updates |
| 28 | type Loader struct { |
| 29 | dbProxy *model.Proxy |
| 30 | // this lock protects the meters map, it does not protect individual meters |
| 31 | lock sync.RWMutex |
| 32 | meters map[uint32]*chunk |
| 33 | } |
| 34 | |
| 35 | // chunk keeps a meter and the lock for this meter |
| 36 | type chunk struct { |
| 37 | // this lock is used to synchronize all access to the meter, and also to the "deleted" variable |
| 38 | lock sync.Mutex |
| 39 | deleted bool |
| 40 | meter *ofp.OfpMeterEntry |
| 41 | } |
| 42 | |
| 43 | func NewLoader(dbProxy *model.Proxy) *Loader { |
| 44 | return &Loader{ |
| 45 | dbProxy: dbProxy, |
| 46 | meters: make(map[uint32]*chunk), |
| 47 | } |
| 48 | } |
| 49 | |
| 50 | // Load queries existing meters from the kv, |
| 51 | // and should only be called once when first created. |
| 52 | func (loader *Loader) Load(ctx context.Context) { |
| 53 | loader.lock.Lock() |
| 54 | defer loader.lock.Unlock() |
| 55 | var meters []*ofp.OfpMeterEntry |
| 56 | if err := loader.dbProxy.List(ctx, &meters); err != nil { |
| 57 | logger.Errorw(ctx, "failed-to-list-meters-from-cluster-data-proxy", log.Fields{"error": err}) |
| 58 | return |
| 59 | } |
| 60 | for _, meter := range meters { |
| 61 | loader.meters[meter.Config.MeterId] = &chunk{meter: meter} |
| 62 | } |
| 63 | } |
| 64 | |
| 65 | // LockOrCreate locks this meter if it exists, or creates a new meter if it does not. |
| 66 | // In the case of meter creation, the provided "meter" must not be modified afterwards. |
| 67 | func (loader *Loader) LockOrCreate(ctx context.Context, meter *ofp.OfpMeterEntry) (*Handle, bool, error) { |
| 68 | // try to use read lock instead of full lock if possible |
| 69 | if handle, have := loader.Lock(meter.Config.MeterId); have { |
| 70 | return handle, false, nil |
| 71 | } |
| 72 | loader.lock.Lock() |
| 73 | entry, have := loader.meters[meter.Config.MeterId] |
| 74 | if !have { |
| 75 | entry := &chunk{meter: meter} |
| 76 | loader.meters[meter.Config.MeterId] = entry |
| 77 | entry.lock.Lock() |
| 78 | loader.lock.Unlock() |
| 79 | if err := loader.dbProxy.Set(ctx, fmt.Sprint(meter.Config.MeterId), meter); err != nil { |
| 80 | // revert the map |
| 81 | loader.lock.Lock() |
| 82 | delete(loader.meters, meter.Config.MeterId) |
| 83 | loader.lock.Unlock() |
| 84 | entry.deleted = true |
| 85 | entry.lock.Unlock() |
| 86 | return nil, false, err |
| 87 | } |
| 88 | return &Handle{loader: loader, chunk: entry}, true, nil |
| 89 | } |
| 90 | loader.lock.Unlock() |
| 91 | entry.lock.Lock() |
| 92 | if entry.deleted { |
| 93 | entry.lock.Unlock() |
| 94 | return loader.LockOrCreate(ctx, meter) |
| 95 | } |
| 96 | return &Handle{loader: loader, chunk: entry}, false, nil |
| 97 | } |
| 98 | |
| 99 | // Lock acquires the lock for this meter, and returns a handle which can be used to access the meter until it's unlocked. |
| 100 | // This handle ensures that the meter cannot be accessed if the lock is not held. |
| 101 | // Returns false if the meter is not present. |
| 102 | // TODO: consider accepting a ctx and aborting the lock attempt on cancellation |
| 103 | func (loader *Loader) Lock(id uint32) (*Handle, bool) { |
| 104 | loader.lock.RLock() |
| 105 | entry, have := loader.meters[id] |
| 106 | loader.lock.RUnlock() |
| 107 | if !have { |
| 108 | return nil, false |
| 109 | } |
| 110 | entry.lock.Lock() |
| 111 | if entry.deleted { |
| 112 | entry.lock.Unlock() |
| 113 | return loader.Lock(id) |
| 114 | } |
| 115 | return &Handle{loader: loader, chunk: entry}, true |
| 116 | } |
| 117 | |
| 118 | // Handle is allocated for each Lock() call, all modifications are made using it, and it is invalidated by Unlock() |
| 119 | // This enforces correct Lock()-Usage()-Unlock() ordering. |
| 120 | type Handle struct { |
| 121 | loader *Loader |
| 122 | chunk *chunk |
| 123 | } |
| 124 | |
| 125 | // GetReadOnly returns an *ofp.OfpMeterEntry which MUST NOT be modified externally, but which is safe to keep indefinitely |
| 126 | func (h *Handle) GetReadOnly() *ofp.OfpMeterEntry { |
| 127 | return h.chunk.meter |
| 128 | } |
| 129 | |
| 130 | // Update updates an existing meter in the kv. |
| 131 | // The provided "meter" must not be modified afterwards. |
| 132 | func (h *Handle) Update(ctx context.Context, meter *ofp.OfpMeterEntry) error { |
| 133 | if err := h.loader.dbProxy.Set(ctx, fmt.Sprint(meter.Config.MeterId), meter); err != nil { |
| 134 | return status.Errorf(codes.Internal, "failed-update-meter-%v: %s", meter.Config.MeterId, err) |
| 135 | } |
| 136 | h.chunk.meter = meter |
| 137 | return nil |
| 138 | } |
| 139 | |
| 140 | // Delete removes the device from the kv |
| 141 | func (h *Handle) Delete(ctx context.Context) error { |
| 142 | if err := h.loader.dbProxy.Remove(ctx, fmt.Sprint(h.chunk.meter.Config.MeterId)); err != nil { |
| 143 | return fmt.Errorf("couldnt-delete-meter-from-store-%v", h.chunk.meter.Config.MeterId) |
| 144 | } |
| 145 | h.chunk.deleted = true |
| 146 | h.loader.lock.Lock() |
| 147 | delete(h.loader.meters, h.chunk.meter.Config.MeterId) |
| 148 | h.loader.lock.Unlock() |
| 149 | h.Unlock() |
| 150 | return nil |
| 151 | } |
| 152 | |
| 153 | // Unlock releases the lock on the meter |
| 154 | func (h *Handle) Unlock() { |
| 155 | if h.chunk != nil { |
| 156 | h.chunk.lock.Unlock() |
| 157 | h.chunk = nil // attempting to access the meter through this handle in future will panic |
| 158 | } |
| 159 | } |
| 160 | |
| 161 | // ListIDs returns a snapshot of all the managed meter IDs |
| 162 | // TODO: iterating through meters safely is expensive now, since all meters are stored & locked separately |
| 163 | // should avoid this where possible |
| 164 | func (loader *Loader) ListIDs() map[uint32]struct{} { |
| 165 | loader.lock.RLock() |
| 166 | defer loader.lock.RUnlock() |
| 167 | // copy the IDs so caller can safely iterate |
| 168 | ret := make(map[uint32]struct{}, len(loader.meters)) |
| 169 | for id := range loader.meters { |
| 170 | ret[id] = struct{}{} |
| 171 | } |
| 172 | return ret |
| 173 | } |