| 1 |
package cache |
| 2 |
|
| 3 |
import ( |
| 4 |
"context" |
| 5 |
"time" |
| 6 |
|
| 7 |
"github.com/LINBIT/golinstor/client" |
| 8 |
) |
| 9 |
|
| 10 |
// ResourceCache caches responses from a client.ResourceProvider. |
| 11 |
type ResourceCache struct { |
| 12 |
// Timeout for the cached responses. |
| 13 |
Timeout time.Duration |
| 14 |
|
| 15 |
resourceCache cache |
| 16 |
snapshotCache cache |
| 17 |
} |
| 18 |
|
| 19 |
// backupShim hooks into the backup provider and invalidates the resource cache on certain operations. |
| 20 |
type backupShim struct { |
| 21 |
client.BackupProvider |
| 22 |
resourceCache *cache |
| 23 |
snapshotCache *cache |
| 24 |
} |
| 25 |
|
| 26 |
func (r *ResourceCache) apply(c *client.Client) { |
| 27 |
c.Resources = &resourceCacheProvider{ |
| 28 |
cl: c.Resources, |
| 29 |
cache: r, |
| 30 |
} |
| 31 |
c.Backup = backupShim{ |
| 32 |
BackupProvider: c.Backup, |
| 33 |
resourceCache: &r.resourceCache, |
| 34 |
snapshotCache: &r.snapshotCache, |
| 35 |
} |
| 36 |
} |
| 37 |
|
| 38 |
type resourceCacheProvider struct { |
| 39 |
cl client.ResourceProvider |
| 40 |
cache *ResourceCache |
| 41 |
} |
| 42 |
|
| 43 |
var _ client.ResourceProvider = &resourceCacheProvider{} |
| 44 |
var _ client.BackupProvider = backupShim{} |
| 45 |
|
| 46 |
func (r *resourceCacheProvider) GetResourceView(ctx context.Context, opts ...*client.ListOpts) ([]client.ResourceWithVolumes, error) { |
| 47 |
result, err := r.cache.resourceCache.Get(r.cache.Timeout, func() (any, error) { |
| 48 |
return r.cl.GetResourceView(ctx, cacheOpt) |
| 49 |
}) |
| 50 |
if err != nil { |
| 51 |
return nil, err |
| 52 |
} |
| 53 |
|
| 54 |
return filterNodeAndPoolOpts(result.([]client.ResourceWithVolumes), opts...), nil |
| 55 |
} |
| 56 |
|
| 57 |
func (r *resourceCacheProvider) GetAll(ctx context.Context, resName string, opts ...*client.ListOpts) ([]client.Resource, error) { |
| 58 |
ress, err := r.GetResourceView(ctx, opts...) |
| 59 |
if err != nil { |
| 60 |
return nil, err |
| 61 |
} |
| 62 |
|
| 63 |
var result []client.Resource |
| 64 |
|
| 65 |
for i := range ress { |
| 66 |
if ress[i].Name == resName { |
| 67 |
result = append(result, ress[i].Resource) |
| 68 |
} |
| 69 |
} |
| 70 |
|
| 71 |
return result, nil |
| 72 |
} |
| 73 |
|
| 74 |
func (r *resourceCacheProvider) Get(ctx context.Context, resName, nodeName string, opts ...*client.ListOpts) (client.Resource, error) { |
| 75 |
ress, err := r.GetAll(ctx, resName, opts...) |
| 76 |
if err != nil { |
| 77 |
return client.Resource{}, err |
| 78 |
} |
| 79 |
|
| 80 |
for i := range ress { |
| 81 |
if ress[i].NodeName == nodeName { |
| 82 |
return ress[i], nil |
| 83 |
} |
| 84 |
} |
| 85 |
return client.Resource{}, client.NotFoundError |
| 86 |
} |
| 87 |
|
| 88 |
func (r *resourceCacheProvider) GetVolumes(ctx context.Context, resName, nodeName string, opts ...*client.ListOpts) ([]client.Volume, error) { |
| 89 |
ress, err := r.GetResourceView(ctx, opts...) |
| 90 |
if err != nil { |
| 91 |
return nil, err |
| 92 |
} |
| 93 |
|
| 94 |
var result []client.Volume |
| 95 |
for i := range ress { |
| 96 |
if ress[i].NodeName != nodeName || ress[i].Name != resName { |
| 97 |
continue |
| 98 |
} |
| 99 |
|
| 100 |
result = append(result, ress[i].Volumes...) |
| 101 |
} |
| 102 |
|
| 103 |
return result, nil |
| 104 |
} |
| 105 |
|
| 106 |
func (r *resourceCacheProvider) GetVolume(ctx context.Context, resName, nodeName string, volNr int, opts ...*client.ListOpts) (client.Volume, error) { |
| 107 |
volumes, err := r.GetVolumes(ctx, resName, nodeName, opts...) |
| 108 |
if err != nil { |
| 109 |
return client.Volume{}, err |
| 110 |
} |
| 111 |
|
| 112 |
for i := range volumes { |
| 113 |
if int(volumes[i].VolumeNumber) == volNr { |
| 114 |
return volumes[i], nil |
| 115 |
} |
| 116 |
} |
| 117 |
|
| 118 |
return client.Volume{}, client.NotFoundError |
| 119 |
} |
| 120 |
|
| 121 |
func (r *resourceCacheProvider) Create(ctx context.Context, res client.ResourceCreate) error { |
| 122 |
defer r.cache.resourceCache.Invalidate() |
| 123 |
return r.cl.Create(ctx, res) |
| 124 |
} |
| 125 |
|
| 126 |
func (r *resourceCacheProvider) Modify(ctx context.Context, resName, nodeName string, props client.GenericPropsModify) error { |
| 127 |
defer r.cache.resourceCache.Invalidate() |
| 128 |
return r.cl.Modify(ctx, resName, nodeName, props) |
| 129 |
} |
| 130 |
|
| 131 |
func (r *resourceCacheProvider) Delete(ctx context.Context, resName, nodeName string, opts ...*client.ResourceDeleteOpts) error { |
| 132 |
defer r.cache.resourceCache.Invalidate() |
| 133 |
return r.cl.Delete(ctx, resName, nodeName, opts...) |
| 134 |
} |
| 135 |
|
| 136 |
func (r *resourceCacheProvider) ModifyVolume(ctx context.Context, resName, nodeName string, volNr int, props client.GenericPropsModify) error { |
| 137 |
defer r.cache.resourceCache.Invalidate() |
| 138 |
return r.cl.ModifyVolume(ctx, resName, nodeName, volNr, props) |
| 139 |
} |
| 140 |
|
| 141 |
func (r *resourceCacheProvider) Diskless(ctx context.Context, resName, nodeName, disklessPoolName string) error { |
| 142 |
defer r.cache.resourceCache.Invalidate() |
| 143 |
return r.cl.Diskless(ctx, resName, nodeName, disklessPoolName) |
| 144 |
} |
| 145 |
|
| 146 |
func (r *resourceCacheProvider) Diskful(ctx context.Context, resName, nodeName, storagePoolName string, props *client.ToggleDiskDiskfulProps) error { |
| 147 |
defer r.cache.resourceCache.Invalidate() |
| 148 |
return r.cl.Diskful(ctx, resName, nodeName, storagePoolName, props) |
| 149 |
} |
| 150 |
|
| 151 |
func (r *resourceCacheProvider) Migrate(ctx context.Context, resName, fromNodeName, toNodeName, storagePoolName string) error { |
| 152 |
defer r.cache.resourceCache.Invalidate() |
| 153 |
return r.cl.Migrate(ctx, resName, fromNodeName, toNodeName, storagePoolName) |
| 154 |
} |
| 155 |
|
| 156 |
func (r *resourceCacheProvider) Autoplace(ctx context.Context, resName string, apr client.AutoPlaceRequest) error { |
| 157 |
defer r.cache.resourceCache.Invalidate() |
| 158 |
return r.cl.Autoplace(ctx, resName, apr) |
| 159 |
} |
| 160 |
|
| 161 |
func (r *resourceCacheProvider) Activate(ctx context.Context, resName string, nodeName string) error { |
| 162 |
defer r.cache.resourceCache.Invalidate() |
| 163 |
return r.cl.Activate(ctx, resName, nodeName) |
| 164 |
} |
| 165 |
|
| 166 |
func (r *resourceCacheProvider) Deactivate(ctx context.Context, resName string, nodeName string) error { |
| 167 |
defer r.cache.resourceCache.Invalidate() |
| 168 |
return r.cl.Deactivate(ctx, resName, nodeName) |
| 169 |
} |
| 170 |
|
| 171 |
func (r *resourceCacheProvider) MakeAvailable(ctx context.Context, resName, nodeName string, makeAvailable client.ResourceMakeAvailable) error { |
| 172 |
defer r.cache.resourceCache.Invalidate() |
| 173 |
return r.cl.MakeAvailable(ctx, resName, nodeName, makeAvailable) |
| 174 |
} |
| 175 |
|
| 176 |
func (r *resourceCacheProvider) UnmakeAvailable(ctx context.Context, resName, nodeName string) error { |
| 177 |
defer r.cache.resourceCache.Invalidate() |
| 178 |
return r.cl.UnmakeAvailable(ctx, resName, nodeName) |
| 179 |
} |
| 180 |
|
| 181 |
func (r *resourceCacheProvider) GetSnapshotView(ctx context.Context, opts ...*client.ListOpts) ([]client.Snapshot, error) { |
| 182 |
result, err := r.cache.snapshotCache.Get(r.cache.Timeout, func() (any, error) { |
| 183 |
return r.cl.GetSnapshotView(ctx, cacheOpt) |
| 184 |
}) |
| 185 |
if err != nil { |
| 186 |
return nil, err |
| 187 |
} |
| 188 |
|
| 189 |
return filterNodeAndPoolOpts(result.([]client.Snapshot), opts...), nil |
| 190 |
} |
| 191 |
|
| 192 |
func (r *resourceCacheProvider) GetSnapshots(ctx context.Context, resName string, opts ...*client.ListOpts) ([]client.Snapshot, error) { |
| 193 |
snaps, err := r.GetSnapshotView(ctx, opts...) |
| 194 |
if err != nil { |
| 195 |
return nil, err |
| 196 |
} |
| 197 |
|
| 198 |
var result []client.Snapshot |
| 199 |
for i := range snaps { |
| 200 |
if snaps[i].ResourceName == resName { |
| 201 |
result = append(result, snaps[i]) |
| 202 |
} |
| 203 |
} |
| 204 |
|
| 205 |
return result, nil |
| 206 |
} |
| 207 |
|
| 208 |
func (r *resourceCacheProvider) GetSnapshot(ctx context.Context, resName, snapName string, opts ...*client.ListOpts) (client.Snapshot, error) { |
| 209 |
snaps, err := r.GetSnapshots(ctx, resName, opts...) |
| 210 |
if err != nil { |
| 211 |
return client.Snapshot{}, err |
| 212 |
} |
| 213 |
|
| 214 |
for i := range snaps { |
| 215 |
if snaps[i].Name == snapName { |
| 216 |
return snaps[i], nil |
| 217 |
} |
| 218 |
} |
| 219 |
|
| 220 |
return client.Snapshot{}, client.NotFoundError |
| 221 |
} |
| 222 |
|
| 223 |
func (r *resourceCacheProvider) CreateSnapshot(ctx context.Context, snapshot client.Snapshot) error { |
| 224 |
defer r.cache.snapshotCache.Invalidate() |
| 225 |
return r.cl.CreateSnapshot(ctx, snapshot) |
| 226 |
} |
| 227 |
|
| 228 |
func (r *resourceCacheProvider) CreateSnapshots(ctx context.Context, snapshots ...client.Snapshot) error { |
| 229 |
defer r.cache.snapshotCache.Invalidate() |
| 230 |
return r.cl.CreateSnapshots(ctx, snapshots...) |
| 231 |
} |
| 232 |
|
| 233 |
func (r *resourceCacheProvider) DeleteSnapshot(ctx context.Context, resName, snapName string, opts ...*client.SnapshotDeleteOpts) error { |
| 234 |
defer r.cache.snapshotCache.Invalidate() |
| 235 |
return r.cl.DeleteSnapshot(ctx, resName, snapName, opts...) |
| 236 |
} |
| 237 |
|
| 238 |
func (r *resourceCacheProvider) RestoreSnapshot(ctx context.Context, origResName, snapName string, snapRestoreConf client.SnapshotRestore) error { |
| 239 |
// This will create new resources, not touch snapshots |
| 240 |
defer r.cache.resourceCache.Invalidate() |
| 241 |
return r.cl.RestoreSnapshot(ctx, origResName, snapName, snapRestoreConf) |
| 242 |
} |
| 243 |
|
| 244 |
func (r *resourceCacheProvider) RestoreVolumeDefinitionSnapshot(ctx context.Context, origResName, snapName string, snapRestoreConf client.SnapshotRestore) error { |
| 245 |
// This will create new resources, not touch snapshots |
| 246 |
defer r.cache.resourceCache.Invalidate() |
| 247 |
return r.cl.RestoreVolumeDefinitionSnapshot(ctx, origResName, snapName, snapRestoreConf) |
| 248 |
} |
| 249 |
|
| 250 |
func (r *resourceCacheProvider) RollbackSnapshot(ctx context.Context, resName, snapName string) error { |
| 251 |
return r.cl.RollbackSnapshot(ctx, resName, snapName) |
| 252 |
} |
| 253 |
|
| 254 |
func (r *resourceCacheProvider) GetConnections(ctx context.Context, resName, nodeAName, nodeBName string, opts ...*client.ListOpts) ([]client.ResourceConnection, error) { |
| 255 |
return r.cl.GetConnections(ctx, resName, nodeAName, nodeBName, opts...) |
| 256 |
} |
| 257 |
|
| 258 |
func (r *resourceCacheProvider) ModifyConnection(ctx context.Context, resName, nodeAName, nodeBName string, props client.GenericPropsModify) error { |
| 259 |
return r.cl.ModifyConnection(ctx, resName, nodeAName, nodeBName, props) |
| 260 |
} |
| 261 |
|
| 262 |
func (r *resourceCacheProvider) ModifyDRBDProxy(ctx context.Context, resName string, props client.DrbdProxyModify) error { |
| 263 |
return r.cl.ModifyDRBDProxy(ctx, resName, props) |
| 264 |
} |
| 265 |
|
| 266 |
func (r *resourceCacheProvider) EnableDRBDProxy(ctx context.Context, resName, nodeAName, nodeBName string) error { |
| 267 |
return r.cl.EnableDRBDProxy(ctx, resName, nodeAName, nodeBName) |
| 268 |
} |
| 269 |
|
| 270 |
func (r *resourceCacheProvider) DisableDRBDProxy(ctx context.Context, resName, nodeAName, nodeBName string) error { |
| 271 |
return r.cl.DisableDRBDProxy(ctx, resName, nodeAName, nodeBName) |
| 272 |
} |
| 273 |
|
| 274 |
func (r *resourceCacheProvider) QueryMaxVolumeSize(ctx context.Context, filter client.AutoSelectFilter) (client.MaxVolumeSizes, error) { |
| 275 |
return r.cl.QueryMaxVolumeSize(ctx, filter) |
| 276 |
} |
| 277 |
|
| 278 |
func (r *resourceCacheProvider) GetPropsInfos(ctx context.Context, resName string, opts ...*client.ListOpts) ([]client.PropsInfo, error) { |
| 279 |
return r.cl.GetPropsInfos(ctx, resName, opts...) |
| 280 |
} |
| 281 |
|
| 282 |
func (r *resourceCacheProvider) GetVolumeDefinitionPropsInfos(ctx context.Context, resName string, opts ...*client.ListOpts) ([]client.PropsInfo, error) { |
| 283 |
return r.cl.GetVolumeDefinitionPropsInfos(ctx, resName, opts...) |
| 284 |
} |
| 285 |
|
| 286 |
func (r *resourceCacheProvider) GetVolumePropsInfos(ctx context.Context, resName, nodeName string, opts ...*client.ListOpts) ([]client.PropsInfo, error) { |
| 287 |
return r.cl.GetVolumePropsInfos(ctx, resName, nodeName, opts...) |
| 288 |
} |
| 289 |
|
| 290 |
func (r *resourceCacheProvider) GetConnectionPropsInfos(ctx context.Context, resName string, opts ...*client.ListOpts) ([]client.PropsInfo, error) { |
| 291 |
return r.cl.GetConnectionPropsInfos(ctx, resName, opts...) |
| 292 |
} |
| 293 |
|
| 294 |
func (b backupShim) Restore(ctx context.Context, remoteName string, request client.BackupRestoreRequest) error { |
| 295 |
defer b.snapshotCache.Invalidate() |
| 296 |
defer b.resourceCache.Invalidate() |
| 297 |
return b.BackupProvider.Restore(ctx, remoteName, request) |
| 298 |
} |
| 299 |
|
| 300 |
func (b backupShim) Create(ctx context.Context, remoteName string, request client.BackupCreate) (string, error) { |
| 301 |
defer b.snapshotCache.Invalidate() |
| 302 |
return b.BackupProvider.Create(ctx, remoteName, request) |
| 303 |
} |
| 304 |
|
| 305 |
func (b backupShim) Ship(ctx context.Context, remoteName string, request client.BackupShipRequest) (string, error) { |
| 306 |
defer b.snapshotCache.Invalidate() |
| 307 |
return b.BackupProvider.Ship(ctx, remoteName, request) |
| 308 |
} |