-
Notifications
You must be signed in to change notification settings - Fork 34
[DPM] Refactor db/cache codes to support GoalStateV2 design #713
Changes from all commits
04cbb1e
4c100e5
e38bcf9
067a988
dd5dafd
aa76562
8ae37db
f0462ae
976883f
ef7a2d2
670dcc6
59a65f0
1178f61
5a98d10
e4e0792
ae15be0
9c88b19
9be2bb9
733872a
b3154ae
e2bed17
7c5013e
ac7acf8
bcc0716
c499d9c
2ba5f1f
98f1e69
397bd47
2ec5c12
bc938fd
cef9f65
19e7f0b
78e3da2
2a7fa5d
70cd3d8
f88ef73
6d41307
df72c67
059f949
276926a
3ca631f
367086f
8732c43
43c0c7c
329e216
f5b7f75
971bc83
fd65364
d996559
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| 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; | ||
|
|
||
| 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 |
|---|---|---|
|
|
@@ -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); | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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.
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Will update doc.
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Thanks. Create an issue #715 for tracking.
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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); | ||
| } | ||
| } | ||
Uh oh!
There was an error while loading. Please reload this page.