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