| 1 |
package cache |
| 2 |
|
| 3 |
import ( |
| 4 |
"context" |
| 5 |
"time" |
| 6 |
|
| 7 |
"github.com/LINBIT/golinstor/client" |
| 8 |
) |
| 9 |
|
| 10 |
// NodeCache caches respones from a client.NodeProvider. |
| 11 |
type NodeCache struct { |
| 12 |
// Timeout for the cached responses. |
| 13 |
Timeout time.Duration |
| 14 |
|
| 15 |
nodeCache cache |
| 16 |
storagePoolCache cache |
| 17 |
physicalStorageCache cache |
| 18 |
} |
| 19 |
|
| 20 |
func (n *NodeCache) apply(c *client.Client) { |
| 21 |
c.Nodes = &nodeCacheProvider{ |
| 22 |
cl: c.Nodes, |
| 23 |
cache: n, |
| 24 |
} |
| 25 |
} |
| 26 |
|
| 27 |
type nodeCacheProvider struct { |
| 28 |
cl client.NodeProvider |
| 29 |
cache *NodeCache |
| 30 |
} |
| 31 |
|
| 32 |
var _ client.NodeProvider = &nodeCacheProvider{} |
| 33 |
|
| 34 |
func (n *nodeCacheProvider) GetAll(ctx context.Context, opts ...*client.ListOpts) ([]client.Node, error) { |
| 35 |
c, err := n.cache.nodeCache.Get(n.cache.Timeout, func() (any, error) { |
| 36 |
return n.cl.GetAll(ctx, cacheOpt) |
| 37 |
}) |
| 38 |
if err != nil { |
| 39 |
return nil, err |
| 40 |
} |
| 41 |
|
| 42 |
return filterNodeAndPoolOpts(c.([]client.Node), opts...), nil |
| 43 |
} |
| 44 |
|
| 45 |
func (n *nodeCacheProvider) Get(ctx context.Context, nodeName string, opts ...*client.ListOpts) (client.Node, error) { |
| 46 |
nodes, err := n.GetAll(ctx, opts...) |
| 47 |
if err != nil { |
| 48 |
return client.Node{}, err |
| 49 |
} |
| 50 |
|
| 51 |
for i := range nodes { |
| 52 |
if nodes[i].Name == nodeName { |
| 53 |
return nodes[i], nil |
| 54 |
} |
| 55 |
} |
| 56 |
|
| 57 |
return client.Node{}, client.NotFoundError |
| 58 |
} |
| 59 |
|
| 60 |
func (n *nodeCacheProvider) Create(ctx context.Context, node client.Node) error { |
| 61 |
defer n.cache.nodeCache.Invalidate() |
| 62 |
return n.cl.Create(ctx, node) |
| 63 |
} |
| 64 |
|
| 65 |
func (n *nodeCacheProvider) CreateEbsNode(ctx context.Context, name string, remoteName string) error { |
| 66 |
defer n.cache.nodeCache.Invalidate() |
| 67 |
return n.cl.CreateEbsNode(ctx, name, remoteName) |
| 68 |
} |
| 69 |
|
| 70 |
func (n *nodeCacheProvider) Modify(ctx context.Context, nodeName string, props client.NodeModify) error { |
| 71 |
defer n.cache.nodeCache.Invalidate() |
| 72 |
return n.cl.Modify(ctx, nodeName, props) |
| 73 |
} |
| 74 |
|
| 75 |
func (n *nodeCacheProvider) Delete(ctx context.Context, nodeName string) error { |
| 76 |
defer n.cache.nodeCache.Invalidate() |
| 77 |
return n.cl.Delete(ctx, nodeName) |
| 78 |
} |
| 79 |
|
| 80 |
func (n *nodeCacheProvider) Lost(ctx context.Context, nodeName string) error { |
| 81 |
defer n.cache.nodeCache.Invalidate() |
| 82 |
return n.cl.Lost(ctx, nodeName) |
| 83 |
} |
| 84 |
|
| 85 |
func (n *nodeCacheProvider) Reconnect(ctx context.Context, nodeName string) error { |
| 86 |
defer n.cache.nodeCache.Invalidate() |
| 87 |
return n.cl.Reconnect(ctx, nodeName) |
| 88 |
} |
| 89 |
|
| 90 |
func (n *nodeCacheProvider) GetNetInterfaces(ctx context.Context, nodeName string, opts ...*client.ListOpts) ([]client.NetInterface, error) { |
| 91 |
node, err := n.Get(ctx, nodeName, opts...) |
| 92 |
if err != nil { |
| 93 |
return nil, err |
| 94 |
} |
| 95 |
|
| 96 |
return node.NetInterfaces, nil |
| 97 |
} |
| 98 |
|
| 99 |
func (n *nodeCacheProvider) GetNetInterface(ctx context.Context, nodeName, nifName string, opts ...*client.ListOpts) (client.NetInterface, error) { |
| 100 |
interfaces, err := n.GetNetInterfaces(ctx, nodeName, opts...) |
| 101 |
if err != nil { |
| 102 |
return client.NetInterface{}, err |
| 103 |
} |
| 104 |
|
| 105 |
for i := range interfaces { |
| 106 |
if interfaces[i].Name == nifName { |
| 107 |
return interfaces[i], nil |
| 108 |
} |
| 109 |
} |
| 110 |
|
| 111 |
return client.NetInterface{}, client.NotFoundError |
| 112 |
} |
| 113 |
|
| 114 |
func (n *nodeCacheProvider) CreateNetInterface(ctx context.Context, nodeName string, nif client.NetInterface) error { |
| 115 |
defer n.cache.nodeCache.Invalidate() |
| 116 |
return n.cl.CreateNetInterface(ctx, nodeName, nif) |
| 117 |
} |
| 118 |
|
| 119 |
func (n *nodeCacheProvider) ModifyNetInterface(ctx context.Context, nodeName, nifName string, nif client.NetInterface) error { |
| 120 |
defer n.cache.nodeCache.Invalidate() |
| 121 |
return n.cl.ModifyNetInterface(ctx, nodeName, nifName, nif) |
| 122 |
} |
| 123 |
|
| 124 |
func (n *nodeCacheProvider) DeleteNetinterface(ctx context.Context, nodeName, nifName string) error { |
| 125 |
defer n.cache.nodeCache.Invalidate() |
| 126 |
return n.cl.DeleteNetinterface(ctx, nodeName, nifName) |
| 127 |
} |
| 128 |
|
| 129 |
func (n *nodeCacheProvider) Evict(ctx context.Context, nodeName string) error { |
| 130 |
defer n.cache.nodeCache.Invalidate() |
| 131 |
return n.cl.Evict(ctx, nodeName) |
| 132 |
} |
| 133 |
|
| 134 |
func (n *nodeCacheProvider) Restore(ctx context.Context, nodeName string, restore client.NodeRestore) error { |
| 135 |
defer n.cache.nodeCache.Invalidate() |
| 136 |
return n.cl.Restore(ctx, nodeName, restore) |
| 137 |
} |
| 138 |
|
| 139 |
func (n *nodeCacheProvider) Evacuate(ctx context.Context, nodeName string, evacuate client.NodeEvacuate) error { |
| 140 |
defer n.cache.nodeCache.Invalidate() |
| 141 |
return n.cl.Evacuate(ctx, nodeName, evacuate) |
| 142 |
} |
| 143 |
|
| 144 |
func (n *nodeCacheProvider) GetStoragePoolView(ctx context.Context, opts ...*client.ListOpts) ([]client.StoragePool, error) { |
| 145 |
result, err := n.cache.storagePoolCache.Get(n.cache.Timeout, func() (any, error) { |
| 146 |
return n.cl.GetStoragePoolView(ctx, cacheOpt) |
| 147 |
}) |
| 148 |
if err != nil { |
| 149 |
return nil, err |
| 150 |
} |
| 151 |
|
| 152 |
return filterNodeAndPoolOpts(result.([]client.StoragePool), opts...), nil |
| 153 |
} |
| 154 |
|
| 155 |
func (n *nodeCacheProvider) GetStoragePools(ctx context.Context, nodeName string, opts ...*client.ListOpts) ([]client.StoragePool, error) { |
| 156 |
allPools, err := n.GetStoragePoolView(ctx, opts...) |
| 157 |
if err != nil { |
| 158 |
return nil, err |
| 159 |
} |
| 160 |
|
| 161 |
var pools []client.StoragePool |
| 162 |
|
| 163 |
for i := range allPools { |
| 164 |
if allPools[i].NodeName == nodeName { |
| 165 |
pools = append(pools, allPools[i]) |
| 166 |
} |
| 167 |
} |
| 168 |
|
| 169 |
return pools, nil |
| 170 |
} |
| 171 |
|
| 172 |
func (n *nodeCacheProvider) GetStoragePool(ctx context.Context, nodeName, spName string, opts ...*client.ListOpts) (client.StoragePool, error) { |
| 173 |
pools, err := n.GetStoragePools(ctx, nodeName, opts...) |
| 174 |
if err != nil { |
| 175 |
return client.StoragePool{}, err |
| 176 |
} |
| 177 |
|
| 178 |
for i := range pools { |
| 179 |
if pools[i].StoragePoolName == spName { |
| 180 |
return pools[i], nil |
| 181 |
} |
| 182 |
} |
| 183 |
|
| 184 |
return client.StoragePool{}, client.NotFoundError |
| 185 |
} |
| 186 |
|
| 187 |
func (n *nodeCacheProvider) CreateStoragePool(ctx context.Context, nodeName string, sp client.StoragePool) error { |
| 188 |
defer n.cache.storagePoolCache.Invalidate() |
| 189 |
return n.cl.CreateStoragePool(ctx, nodeName, sp) |
| 190 |
} |
| 191 |
|
| 192 |
func (n *nodeCacheProvider) ModifyStoragePool(ctx context.Context, nodeName, spName string, genericProps client.GenericPropsModify) error { |
| 193 |
defer n.cache.storagePoolCache.Invalidate() |
| 194 |
return n.cl.ModifyStoragePool(ctx, nodeName, spName, genericProps) |
| 195 |
} |
| 196 |
|
| 197 |
func (n *nodeCacheProvider) DeleteStoragePool(ctx context.Context, nodeName, spName string) error { |
| 198 |
defer n.cache.storagePoolCache.Invalidate() |
| 199 |
return n.cl.DeleteStoragePool(ctx, nodeName, spName) |
| 200 |
} |
| 201 |
|
| 202 |
func (n *nodeCacheProvider) GetPhysicalStorageView(ctx context.Context, opts ...*client.ListOpts) ([]client.PhysicalStorageViewItem, error) { |
| 203 |
result, err := n.cache.physicalStorageCache.Get(n.cache.Timeout, func() (any, error) { |
| 204 |
return n.cl.GetPhysicalStorageView(ctx, cacheOpt) |
| 205 |
}) |
| 206 |
if err != nil { |
| 207 |
return nil, err |
| 208 |
} |
| 209 |
|
| 210 |
return filterNodeAndPoolOpts(result.([]client.PhysicalStorageViewItem), opts...), nil |
| 211 |
} |
| 212 |
|
| 213 |
func (n *nodeCacheProvider) GetPhysicalStorage(ctx context.Context, nodeName string) ([]client.PhysicalStorageNode, error) { |
| 214 |
view, err := n.GetPhysicalStorageView(ctx) |
| 215 |
if err != nil { |
| 216 |
return nil, err |
| 217 |
} |
| 218 |
|
| 219 |
var result []client.PhysicalStorageNode |
| 220 |
for i := range view { |
| 221 |
for j := range view[i].Nodes[nodeName] { |
| 222 |
result = append(result, client.PhysicalStorageNode{ |
| 223 |
Rotational: view[i].Rotational, |
| 224 |
Size: view[i].Size, |
| 225 |
PhysicalStorageDevice: view[i].Nodes[nodeName][j], |
| 226 |
}) |
| 227 |
} |
| 228 |
} |
| 229 |
|
| 230 |
return result, nil |
| 231 |
} |
| 232 |
|
| 233 |
func (n *nodeCacheProvider) CreateDevicePool(ctx context.Context, nodeName string, psc client.PhysicalStorageCreate) error { |
| 234 |
defer n.cache.physicalStorageCache.Invalidate() |
| 235 |
defer n.cache.storagePoolCache.Invalidate() |
| 236 |
return n.cl.CreateDevicePool(ctx, nodeName, psc) |
| 237 |
} |
| 238 |
|
| 239 |
func (n *nodeCacheProvider) GetStoragePoolPropsInfos(ctx context.Context, nodeName string, opts ...*client.ListOpts) ([]client.PropsInfo, error) { |
| 240 |
return n.cl.GetStoragePoolPropsInfos(ctx, nodeName, opts...) |
| 241 |
} |
| 242 |
|
| 243 |
func (n *nodeCacheProvider) GetPropsInfos(ctx context.Context, opts ...*client.ListOpts) ([]client.PropsInfo, error) { |
| 244 |
return n.cl.GetPropsInfos(ctx, opts...) |
| 245 |
} |