Add cache for route tables

This commit is contained in:
Pengfei Ni 2018-02-07 22:26:03 +08:00
parent 21c8a63689
commit daec2bd745
5 changed files with 36 additions and 12 deletions

View File

@ -138,6 +138,7 @@ type Cloud struct {
vmCache *timedCache vmCache *timedCache
lbCache *timedCache lbCache *timedCache
nsgCache *timedCache nsgCache *timedCache
rtCache *timedCache
*BlobDiskController *BlobDiskController
*ManagedDiskController *ManagedDiskController
@ -266,6 +267,12 @@ func NewCloud(configReader io.Reader) (cloudprovider.Interface, error) {
} }
az.nsgCache = nsgCache az.nsgCache = nsgCache
rtCache, err := az.newRouteTableCache()
if err != nil {
return nil, err
}
az.rtCache = rtCache
if err := initDiskControllers(&az); err != nil { if err := initDiskControllers(&az); err != nil {
return nil, err return nil, err
} }

View File

@ -98,10 +98,9 @@ func (az *Cloud) createRouteTable() error {
return err return err
} }
glog.V(10).Infof("RouteTablesClient.Get(%q): start", az.RouteTableName) // Invalidate the cache right after updating
_, err = az.RouteTablesClient.Get(az.ResourceGroup, az.RouteTableName, "") az.rtCache.Delete(az.RouteTableName)
glog.V(10).Infof("RouteTablesClient.Get(%q): end", az.RouteTableName) return nil
return err
} }
// CreateRoute creates the described managed route // CreateRoute creates the described managed route

View File

@ -37,6 +37,7 @@ func TestCreateRoute(t *testing.T) {
Location: "location", Location: "location",
}, },
} }
cloud.rtCache, _ = cloud.newRouteTableCache()
expectedTable := network.RouteTable{ expectedTable := network.RouteTable{
Name: &cloud.RouteTableName, Name: &cloud.RouteTableName,
Location: &cloud.Location, Location: &cloud.Location,

View File

@ -881,6 +881,7 @@ func getTestCloud() (az *Cloud) {
az.vmCache, _ = az.newVMCache() az.vmCache, _ = az.newVMCache()
az.lbCache, _ = az.newLBCache() az.lbCache, _ = az.newLBCache()
az.nsgCache, _ = az.newNSGCache() az.nsgCache, _ = az.newNSGCache()
az.rtCache, _ = az.newRouteTableCache()
return az return az
} }

View File

@ -33,6 +33,7 @@ var (
vmCacheTTL = time.Minute vmCacheTTL = time.Minute
lbCacheTTL = 2 * time.Minute lbCacheTTL = 2 * time.Minute
nsgCacheTTL = 2 * time.Minute nsgCacheTTL = 2 * time.Minute
rtCacheTTL = 2 * time.Minute
) )
// checkExistsFromError inspects an error and returns a true if err is nil, // checkExistsFromError inspects an error and returns a true if err is nil,
@ -83,19 +84,16 @@ func (az *Cloud) getVirtualMachine(nodeName types.NodeName) (vm compute.VirtualM
} }
func (az *Cloud) getRouteTable() (routeTable network.RouteTable, exists bool, err error) { func (az *Cloud) getRouteTable() (routeTable network.RouteTable, exists bool, err error) {
var realErr error cachedRt, err := az.rtCache.Get(az.RouteTableName)
if err != nil {
routeTable, err = az.RouteTablesClient.Get(az.ResourceGroup, az.RouteTableName, "") return routeTable, false, err
exists, realErr = checkResourceExistsFromError(err)
if realErr != nil {
return routeTable, false, realErr
} }
if !exists { if cachedRt == nil {
return routeTable, false, nil return routeTable, false, nil
} }
return routeTable, exists, err return *(cachedRt.(*network.RouteTable)), true, nil
} }
func (az *Cloud) getPublicIPAddress(pipResourceGroup string, pipName string) (pip network.PublicIPAddress, exists bool, err error) { func (az *Cloud) getPublicIPAddress(pipResourceGroup string, pipName string) (pip network.PublicIPAddress, exists bool, err error) {
@ -220,3 +218,21 @@ func (az *Cloud) newNSGCache() (*timedCache, error) {
return newTimedcache(nsgCacheTTL, getter) return newTimedcache(nsgCacheTTL, getter)
} }
func (az *Cloud) newRouteTableCache() (*timedCache, error) {
getter := func(key string) (interface{}, error) {
rt, err := az.RouteTablesClient.Get(az.ResourceGroup, key, "")
exists, realErr := checkResourceExistsFromError(err)
if realErr != nil {
return nil, realErr
}
if !exists {
return nil, nil
}
return &rt, nil
}
return newTimedcache(rtCacheTTL, getter)
}