Skip to content
This repository has been archived by the owner on Mar 31, 2023. It is now read-only.

[DPM] Refactor db/cache codes to support GoalStateV2 design #713

Merged
merged 49 commits into from
Dec 11, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
49 commits
Select commit Hold shift + click to select a range
04cbb1e
Update router manager api
DavidLiu506 Jun 29, 2021
4c100e5
Update code
DavidLiu506 Jun 29, 2021
e38bcf9
Update code
DavidLiu506 Jun 30, 2021
067a988
Update router manager
DavidLiu506 Jul 1, 2021
dd5dafd
Update router manager
DavidLiu506 Jul 1, 2021
aa76562
Merge router manager service api code
DavidLiu506 Jul 2, 2021
8ae37db
Fix getVpcRouteTables
DavidLiu506 Jul 2, 2021
f0462ae
Fix getVpcRouteTable
DavidLiu506 Jul 2, 2021
976883f
Update RouterController
DavidLiu506 Jul 6, 2021
ef7a2d2
Update code
DavidLiu506 Jul 6, 2021
670dcc6
Update code
DavidLiu506 Jun 29, 2021
59a65f0
Update code
DavidLiu506 Jun 30, 2021
1178f61
pull alcor code
DavidLiu506 Jul 9, 2021
5a98d10
Merge branch 'futurewei-cloud:master' into master
DavidLiu506 Jul 21, 2021
e4e0792
Merge branch 'master' of https://github.com/futurewei-cloud/alcor
DavidLiu506 Aug 13, 2021
ae15be0
Merge branch 'futurewei-cloud:master' into master
DavidLiu506 Aug 15, 2021
9c88b19
Merge branch 'futurewei-cloud:master' into master
DavidLiu506 Aug 21, 2021
9be2bb9
Merge branch 'futurewei-cloud:master' into master
DavidLiu506 Sep 13, 2021
733872a
Merge branch 'master' of https://github.com/futurewei-cloud/alcor
DavidLiu506 Oct 19, 2021
b3154ae
Merge branch 'futurewei-cloud:master' into master
DavidLiu506 Oct 21, 2021
e2bed17
Merge branch 'master' of https://github.com/DavidLiu506/alcor
DavidLiu506 Oct 21, 2021
7c5013e
Merge branch 'futurewei-cloud:master' into master
DavidLiu506 Oct 23, 2021
ac7acf8
Merge branch 'master' of https://github.com/futurewei-cloud/alcor
DavidLiu506 Oct 28, 2021
bcc0716
Merge branch 'futurewei-cloud:master' into master
DavidLiu506 Nov 3, 2021
c499d9c
Merge branch 'futurewei-cloud:master' into master
DavidLiu506 Nov 30, 2021
2ba5f1f
Implement GoalStateV2 cache design in DPM
DavidLiu506 Dec 6, 2021
98f1e69
Update code
DavidLiu506 Dec 6, 2021
397bd47
Update code
DavidLiu506 Dec 6, 2021
2ec5c12
Update code
DavidLiu506 Dec 7, 2021
bc938fd
Update code
DavidLiu506 Dec 7, 2021
cef9f65
Update code
DavidLiu506 Dec 7, 2021
19e7f0b
Update code
DavidLiu506 Dec 8, 2021
78e3da2
Update code
DavidLiu506 Dec 8, 2021
2a7fa5d
Update code
DavidLiu506 Dec 8, 2021
70cd3d8
Update code
DavidLiu506 Dec 8, 2021
f88ef73
Update code
DavidLiu506 Dec 8, 2021
6d41307
Update code
DavidLiu506 Dec 8, 2021
df72c67
Update code
DavidLiu506 Dec 8, 2021
059f949
Update code
DavidLiu506 Dec 9, 2021
276926a
Update code
DavidLiu506 Dec 9, 2021
3ca631f
Update code
DavidLiu506 Dec 9, 2021
367086f
Update code
DavidLiu506 Dec 9, 2021
8732c43
Update code
DavidLiu506 Dec 9, 2021
43c0c7c
Update code
DavidLiu506 Dec 9, 2021
329e216
Merge branch 'master' of https://github.com/futurewei-cloud/alcor
DavidLiu506 Dec 9, 2021
f5b7f75
Update code
DavidLiu506 Dec 10, 2021
971bc83
Update code
DavidLiu506 Dec 11, 2021
fd65364
Merge branch 'master' into alcor/data_plane_manager
DavidLiu506 Dec 11, 2021
d996559
Update code
DavidLiu506 Dec 11, 2021
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -43,11 +43,15 @@ public class LocalCacheImpl implements LocalCache {
@Autowired
private SubnetPortsCache subnetPortsCache;

@Autowired
private PortHostInfoCache portHostInfoCache;

@Autowired
private NodeInfoCache nodeInfoCache;

@Override
public void setSubnetPorts(NetworkConfiguration networkConfig) throws Exception {

List<InternalPortEntity> portEntities = networkConfig.getPortEntities();
if (portEntities == null) {
return;
Expand All @@ -70,6 +74,7 @@ public void setSubnetPorts(NetworkConfiguration networkConfig) throws Exception
portHostInfo.setPortId(portEntity.getId());
portHostInfo.setHostIp(portEntity.getBindingHostIP());
portHostInfo.setHostId(portEntity.getBindingHostId());
portHostInfo.setSubnetId(subnetId);

InternalSubnetPorts subnetPorts = subnetPortsMap.get(subnetId);
if (subnetPorts == null) {
Expand All @@ -85,7 +90,6 @@ public void setSubnetPorts(NetworkConfiguration networkConfig) throws Exception
subnetPorts.setVpcId(subnetEntity.getVpcId());
subnetPorts.setTunnelId(subnetEntity.getTunnelId());
subnetPorts.setDhcpEnable(subnetEntity.getDhcpEnable());
subnetPorts.setPorts(new ArrayList<>());

List<InternalRouterInfo> routers = networkConfig.getInternalRouterInfos();
if (routers != null && routers.size() > 0) {
Expand All @@ -106,14 +110,13 @@ public void setSubnetPorts(NetworkConfiguration networkConfig) throws Exception

subnetPortsMap.put(subnetId, subnetPorts);
}

subnetPorts.getPorts().add(portHostInfo);
}
}

for (Map.Entry<String, InternalSubnetPorts> entry: subnetPortsMap.entrySet()) {
subnetPortsCache.updateSubnetPorts(entry.getValue());
}

}

private InternalSubnetEntity getSubnetEntity(NetworkConfiguration networkConfig, String subnetId) throws Exception {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,87 @@
/*
MIT License
Copyright(c) 2020 Futurewei Cloud

Permission is hereby granted,
free of charge, to any person obtaining a copy of this software and associated documentation files(the "Software"), to deal in the Software without restriction,
including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and / or sell copies of the Software, and to permit persons
to whom the Software is furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*/
package com.futurewei.alcor.dataplane.cache;
xieus marked this conversation as resolved.
Show resolved Hide resolved

import com.futurewei.alcor.common.db.CacheException;
import com.futurewei.alcor.common.db.CacheFactory;
import com.futurewei.alcor.common.db.ICache;
import com.futurewei.alcor.web.entity.dataplane.v2.NetworkConfiguration;
import com.futurewei.alcor.web.entity.port.PortHostInfo;
import com.futurewei.alcor.web.entity.subnet.InternalSubnetPorts;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.stereotype.Repository;

import java.util.Collection;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.function.Function;
import java.util.stream.Collectors;

@Repository
@ComponentScan(value="com.futurewei.alcor.common.db")
public class PortHostInfoCache {

private ICache<String, PortHostInfo> portHostInfoCache;
private CacheFactory cacheFactory;

@Autowired
public PortHostInfoCache(CacheFactory cacheFactory) {
this.cacheFactory = cacheFactory;
portHostInfoCache = cacheFactory.getCache(PortHostInfo.class);
}

public Map<String, PortHostInfo> getPortHostInfo(NetworkConfiguration networkConfig) {
Map<String, PortHostInfo> portHostInfoMap = networkConfig
.getPortEntities()
.stream()
.filter(portEntity -> portEntity.getFixedIps().size() > 0)
.flatMap(portEntity -> portEntity.getFixedIps()
.stream()
.filter(fixedIp -> fixedIp != null)
.map(fixedIp -> new PortHostInfo(portEntity.getId()
, fixedIp.getIpAddress()
, portEntity.getMacAddress()
, portEntity.getBindingHostId()
, portEntity.getBindingHostIP()
, fixedIp.getSubnetId())))
.collect(Collectors.toMap(portHostInfo -> portHostInfo.getSubnetId() + cacheFactory.KEY_DELIMITER + portHostInfo.getPortIp(), Function.identity()));
return portHostInfoMap;
}

public void updatePortHostInfo(Map<String, PortHostInfo> portHostInfoMap) throws CacheException {
portHostInfoCache.putAll(portHostInfoMap);
}

public synchronized Collection<PortHostInfo> getPortHostInfos(String subnetId) throws CacheException {
Map<String, Object[]> queryParams = new HashMap<>();
Object[] values = new Object[1];
values[0] = subnetId;
queryParams.put("subnetId", values);
// Use sql index
return portHostInfoCache.getAll(queryParams).values();
xieus marked this conversation as resolved.
Show resolved Hide resolved
}

public synchronized PortHostInfo getPortHostInfoByIp(String subnetId, String ip) throws CacheException {
return portHostInfoCache.get(subnetId + cacheFactory.KEY_DELIMITER + ip);
}

public synchronized void deletePortHostInfo(String subnetId, String portIp) throws CacheException {
portHostInfoCache.remove(subnetId + cacheFactory.KEY_DELIMITER + portIp);
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -20,89 +20,62 @@ free of charge, to any person obtaining a copy of this software and associated d
import com.futurewei.alcor.common.db.ICache;
import com.futurewei.alcor.common.db.repo.ICacheRepository;
import com.futurewei.alcor.common.stats.DurationStatistics;
import com.futurewei.alcor.dataplane.entity.InternalSubnetRouterMap;
import com.futurewei.alcor.dataplane.entity.InternalSubnets;
import com.futurewei.alcor.web.entity.dataplane.v2.NetworkConfiguration;
import com.futurewei.alcor.web.entity.port.PortHostInfo;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.stereotype.Repository;

import java.util.List;
import java.util.Map;
import java.util.*;
import java.util.function.Function;
import java.util.stream.Collectors;

@Slf4j
@Repository
@ComponentScan(value="com.futurewei.alcor.common.db")
public class RouterSubnetsCache implements ICacheRepository<InternalSubnets> {
public class RouterSubnetsCache {
// The cache is a map(routerId, subnetIds)
private final ICache<String, InternalSubnets> routerSubnetsCache;
private final ICache<String, InternalSubnetRouterMap> routerSubnetsCache;
private CacheFactory cacheFactory;

@Autowired
public RouterSubnetsCache(CacheFactory cacheFactory) {
this.routerSubnetsCache = cacheFactory.getCache(InternalSubnets.class);
this.cacheFactory = cacheFactory;
this.routerSubnetsCache = cacheFactory.getCache(InternalSubnetRouterMap.class);
}

@DurationStatistics
public InternalSubnets getRouterSubnets(String routerId) throws CacheException {
return routerSubnetsCache.get(routerId);
}
public Collection<InternalSubnetRouterMap> getRouterSubnets(String routerId) throws CacheException {
Map<String, Object[]> queryParams = new HashMap<>();
Object[] values = new Object[1];
values[0] = routerId;
queryParams.put("routerId", values);
return routerSubnetsCache.getAll(queryParams).values();

@DurationStatistics
public Map<String, InternalSubnets> getAllSubnets() throws CacheException {
return routerSubnetsCache.getAll();
}

@DurationStatistics
public Map<String, InternalSubnets> getAllSubnets(Map<String, Object[]> queryParams) throws CacheException {
return routerSubnetsCache.getAll(queryParams);
}

@DurationStatistics
public synchronized void addVpcSubnets(InternalSubnets subnets) throws CacheException {
routerSubnetsCache.put(subnets.getRouterId(), subnets);
public Collection<InternalSubnetRouterMap> updateVpcSubnets(NetworkConfiguration networkConfig) throws CacheException {
Map<String, InternalSubnetRouterMap> internalSubnetsMap = networkConfig
.getInternalRouterInfos()
.stream()
.filter(routerInfo -> routerInfo.getRouterConfiguration().getSubnetRoutingTables().size() > 0)
.flatMap(routerInfo -> routerInfo.getRouterConfiguration().getSubnetRoutingTables()
.stream()
.map(routingTable -> new InternalSubnetRouterMap(routerInfo.getRouterConfiguration().getId()
, routingTable.getSubnetId())))
.collect(Collectors.toMap(routerInfo -> routerInfo.getRouterId() + cacheFactory.KEY_DELIMITER + routerInfo.getSubnetId(), Function.identity()));
routerSubnetsCache.putAll(internalSubnetsMap);
return internalSubnetsMap.values();
}

@DurationStatistics
public void updateVpcSubnets(InternalSubnets subnets) throws CacheException {
routerSubnetsCache.put(subnets.getRouterId(), subnets);
}

@DurationStatistics
public void deleteVpcGatewayInfo(String routerId) throws CacheException {
routerSubnetsCache.remove(routerId);
}

@Override
public InternalSubnets findItem(String id) throws CacheException {
return routerSubnetsCache.get(id);
public void deleteVpcGatewayInfo(String routerId, String subnetId) throws CacheException {
routerSubnetsCache.remove(routerId + cacheFactory.KEY_DELIMITER + subnetId);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We have some key change in the key-value store to improve performance in multiple caches in this PR.

Since DPM design doc is quite outdated, we should update the design doc by putting major design points and gathering optimization techniques applied to DPM cache. @DavidLiu506 @cj-chung

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Agree, we need to rewrite the DPM design doc and including the new cache design into the doc.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Will update doc.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks. Create an issue #715 for tracking.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks!

}

@Override
public Map<String, InternalSubnets> findAllItems() throws CacheException {
return routerSubnetsCache.getAll();
}

@Override
public Map<String, InternalSubnets> findAllItems(Map<String, Object[]> queryParams) throws CacheException {
return routerSubnetsCache.getAll(queryParams);
}

@Override
public void addItem(InternalSubnets subnets) throws CacheException {
log.debug("Add Subnets {} to Router {}", subnets.toString(), subnets.getRouterId());
routerSubnetsCache.put(subnets.getRouterId(), subnets);
}

@Override
public void addItems(List<InternalSubnets> items) throws CacheException {
Map<String, InternalSubnets> subnetsMap = items.stream().collect(Collectors.toMap(InternalSubnets::getRouterId, Function.identity()));
routerSubnetsCache.putAll(subnetsMap);
}

@Override
public void deleteItem(String id) throws CacheException {
log.debug("Delete Router {}", id);
routerSubnetsCache.remove(id);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -18,22 +18,34 @@ free of charge, to any person obtaining a copy of this software and associated d
import com.futurewei.alcor.common.db.CacheException;
import com.futurewei.alcor.common.db.CacheFactory;
import com.futurewei.alcor.common.db.ICache;
import com.futurewei.alcor.common.db.Transaction;
import com.futurewei.alcor.common.stats.DurationStatistics;
import com.futurewei.alcor.dataplane.entity.InternalSubnetRouterMap;
import com.futurewei.alcor.dataplane.entity.InternalSubnets;
import com.futurewei.alcor.schema.Subnet;
import com.futurewei.alcor.web.entity.dataplane.InternalSubnetEntity;
import com.futurewei.alcor.web.entity.dataplane.v2.NetworkConfiguration;
import com.futurewei.alcor.web.entity.port.PortHostInfo;
import com.futurewei.alcor.web.entity.subnet.InternalSubnetPorts;
import com.futurewei.alcor.web.entity.subnet.SubnetEntity;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.stereotype.Repository;

import java.util.Map;
import java.util.*;
import java.util.function.Function;
import java.util.stream.Collectors;

@Repository
@ComponentScan(value="com.futurewei.alcor.common.db")
public class SubnetPortsCache {
// The cache is a map(subnetId, subnetPorts)
private ICache<String, InternalSubnetPorts> subnetPortsCache;
private CacheFactory cacheFactory;

@Autowired
public SubnetPortsCache(CacheFactory cacheFactory) {
this.cacheFactory = cacheFactory;
subnetPortsCache = cacheFactory.getCache(InternalSubnetPorts.class);
}

Expand All @@ -42,6 +54,16 @@ public InternalSubnetPorts getSubnetPorts(String subnetId) throws CacheException
return subnetPortsCache.get(subnetId);
}

@DurationStatistics
public Map<String, InternalSubnetPorts> getSubnetPortsByRouterId(String routerId) throws CacheException {
Map<String, Object[]> queryParams = new HashMap<>();
Object[] values = new Object[1];
values[0] = routerId;
queryParams.put("routerId", values);
// Use sql index
return subnetPortsCache.getAll(queryParams);
}

@DurationStatistics
public Map<String, InternalSubnetPorts> getAllSubnetPorts() throws CacheException {
return subnetPortsCache.getAll();
Expand All @@ -57,6 +79,74 @@ public synchronized void addSubnetPorts(InternalSubnetPorts internalSubnetPorts)
subnetPortsCache.put(internalSubnetPorts.getSubnetId(), internalSubnetPorts);
}

@DurationStatistics
public Map<String, String> getInternalSubnetRouterMap(NetworkConfiguration networkConfig) {
if (networkConfig.getInternalRouterInfos() != null) {
Map<String, String> internalSubnetsRouterMap = networkConfig
.getInternalRouterInfos()
.stream()
.filter(routerInfo -> routerInfo.getRouterConfiguration().getSubnetRoutingTables().size() > 0)
.flatMap(routerInfo -> routerInfo.getRouterConfiguration().getSubnetRoutingTables()
.stream()
.map(routingTable -> new InternalSubnetRouterMap(routerInfo.getRouterConfiguration().getId()
, routingTable.getSubnetId())))
.distinct()
.collect(Collectors.toMap(routerInfo -> routerInfo.getSubnetId(), routerInfo -> routerInfo.getRouterId()));
return internalSubnetsRouterMap;
}
return new HashMap<>();
}

@DurationStatistics
public Map<String, InternalSubnetPorts> attacheRouter(Map<String, String> subnetIdRouterIdMap) throws CacheException {
Map<String, InternalSubnetPorts> internalSubnetEntityMap = new TreeMap<>();
for (Map.Entry<String, String> subnetIdRouterId : subnetIdRouterIdMap.entrySet()) {
InternalSubnetPorts internalSubnetPorts = null;
try {
if (subnetPortsCache.containsKey(subnetIdRouterId.getKey())) {
internalSubnetPorts = subnetPortsCache.get(subnetIdRouterId.getKey());
internalSubnetPorts.setRouterId(subnetIdRouterId.getValue());
internalSubnetEntityMap.put(subnetIdRouterId.getKey(), internalSubnetPorts);
}
} catch (CacheException e) {
e.printStackTrace();
}
}

subnetPortsCache.putAll(internalSubnetEntityMap);
return internalSubnetEntityMap;
}


@DurationStatistics
public Map<String, InternalSubnetPorts> getSubnetPorts(NetworkConfiguration networkConfig) throws CacheException {
Map<String, String> internalSubnetsRouterMap = getInternalSubnetRouterMap(networkConfig);
Map<String, InternalSubnetPorts> internalSubnetPortsMap = new TreeMap<>();
for (InternalSubnetEntity subnetEntity : networkConfig.getSubnets()) {
internalSubnetPortsMap.put(subnetEntity.getId(), getInternalSubnetPorts(subnetEntity, internalSubnetsRouterMap.getOrDefault(subnetEntity.getId(), null)));
}
return internalSubnetPortsMap;
}

private InternalSubnetPorts getInternalSubnetPorts(InternalSubnetEntity subnetEntity, String routerId) {
InternalSubnetPorts internalSubnetPorts = new InternalSubnetPorts(subnetEntity.getId()
,subnetEntity.getGatewayPortDetail().getGatewayPortId()
,subnetEntity.getGatewayIp()
,subnetEntity.getGatewayPortDetail().getGatewayMacAddress()
,subnetEntity.getName()
,subnetEntity.getCidr()
,subnetEntity.getVpcId()
,subnetEntity.getTunnelId()
,subnetEntity.getDhcpEnable()
,routerId);
return internalSubnetPorts;
}

@DurationStatistics
public void updateSubnetPorts(Map<String, InternalSubnetPorts> internalSubnetPortsMap) throws Exception {
subnetPortsCache.putAll(internalSubnetPortsMap);
}

@DurationStatistics
public void updateSubnetPorts(InternalSubnetPorts internalSubnetPorts) throws Exception {
subnetPortsCache.put(internalSubnetPorts.getSubnetId(), internalSubnetPorts);
Expand All @@ -67,4 +157,8 @@ public void deleteSubnetPorts(String subnetId) throws Exception {
subnetPortsCache.remove(subnetId);
}

public Transaction getTransaction() {
return subnetPortsCache.getTransaction();
}

}
Loading