Skip to content

Commit

Permalink
Merge branch 'master' into 4932
Browse files Browse the repository at this point in the history
  • Loading branch information
loongs-zhang authored Aug 2, 2023
2 parents 30c97bf + 3082629 commit d3f6b61
Show file tree
Hide file tree
Showing 25 changed files with 248 additions and 383 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -88,9 +88,9 @@ public interface SelectorMapper extends ExistProvider {
* select select by name.
*
* @param name the name
* @return selector do
* @return selector do list
*/
SelectorDO selectByName(String name);
List<SelectorDO> selectByName(String name);

/**
* Find by name and plugin id selector do.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,15 +36,15 @@
* this is selector service.
*/
public interface SelectorService extends PageService<SelectorQueryCondition, SelectorVO> {

/**
* Register string.
*
* @param selectorDTO the selector dto
* @return the string
*/
String registerDefault(SelectorDTO selectorDTO);

/**
* handler selector need upstream check.
*
Expand All @@ -54,7 +54,7 @@ public interface SelectorService extends PageService<SelectorQueryCondition, Sel
* @return the id of selector.
*/
String registerDefault(MetaDataRegisterDTO dto, String pluginName, String selectorHandler);

/**
* create or update selector.
*
Expand All @@ -73,7 +73,7 @@ default int createOrUpdate(SelectorDTO selectorDTO) {
}
return StringUtils.isEmpty(selectorDTO.getId()) ? create(selectorDTO) : update(selectorDTO);
}

/**
* create selector.
* <ul>
Expand All @@ -87,47 +87,58 @@ default int createOrUpdate(SelectorDTO selectorDTO) {
* @return rows int
*/
int create(SelectorDTO selectorDTO);

/**
* update selector.
*
* @param selectorDTO {@linkplain SelectorDTO}
* @return rows int
*/
int update(SelectorDTO selectorDTO);

/**
* update selective selector.
*
* @param selectorDO {@linkplain SelectorDO}
* @return rows int
*/
int updateSelective(SelectorDO selectorDO);

/**
* delete selectors.
*
* @param ids primary key.
* @return rows int
*/
int delete(List<String> ids);

/**
* find selector by id.
*
* @param id primary key.
* @return {@linkplain SelectorVO}
*/
SelectorVO findById(String id);

/**
* find selector by name.
*
* @param name the name
* @return selector do
* @deprecated sice 2.6.0 Deprecated. By querying under this condition, multiple data are usually obtained.
* Therefore, it is recommended to: {@linkplain SelectorService#findListByName(java.lang.String)}
*/
@Deprecated
SelectorDO findByName(String name);

/**
* find selector list by name.
*
* @param name name
* @return list
*/
List<SelectorDO> findListByName(String name);

/**
* Find by name and plugin id selector do.
*
Expand All @@ -136,15 +147,24 @@ default int createOrUpdate(SelectorDTO selectorDTO) {
* @return the selector do
*/
SelectorDO findByNameAndPluginName(String name, String pluginName);


/**
* Find selectorDO list by name and plugin name list.
*
* @param name name
* @param pluginNames pluginNames
* @return selectorDO list
*/
List<SelectorDO> findByNameAndPluginNames(String name, List<String> pluginNames);

/**
* Build by name selector data.
*
* @param name the name
* @return the selector data
*/
SelectorData buildByName(String name);

/**
* Build by name selector data.
*
Expand All @@ -153,31 +173,31 @@ default int createOrUpdate(SelectorDTO selectorDTO) {
* @return the selector data
*/
SelectorData buildByName(String name, String pluginName);

/**
* find page of selector by query.
*
* @param selectorQuery {@linkplain SelectorQuery}
* @return {@linkplain CommonPager}
*/
CommonPager<SelectorVO> listByPageWithPermission(SelectorQuery selectorQuery);

/**
* find page of selector by query.
*
* @param selectorQuery selectorQuery
* @return CommonPager
*/
CommonPager<SelectorVO> listByPage(SelectorQuery selectorQuery);

/**
* Find by plugin id list.
*
* @param pluginId the plugin id
* @return the list
*/
List<SelectorData> findByPluginId(String pluginId);

/**
* List all list.
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
import org.apache.shenyu.common.utils.GsonUtils;

import java.util.List;
import java.util.Objects;
import java.util.concurrent.CopyOnWriteArrayList;
import java.util.concurrent.TimeUnit;
import java.util.stream.Collectors;
Expand Down Expand Up @@ -68,7 +69,7 @@ public String handler(final String handle, final List<CommonUpstream> aliveList)
*/
@Override
public <T extends CommonUpstream> List<T> updateStatusAndFilter(final List<T> existList, final List<? extends CommonUpstream> aliveList) {
if (CollectionUtils.isEmpty(aliveList) || CollectionUtils.isEmpty(existList)) {
if (Objects.isNull(aliveList)) {
return Lists.newArrayList();
}
long currentTimeMillis = System.currentTimeMillis();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
package org.apache.shenyu.admin.service.impl;

import com.google.common.collect.Lists;
import java.util.ArrayList;
import org.apache.commons.collections4.CollectionUtils;
import org.apache.commons.lang3.StringUtils;
import org.apache.shenyu.admin.disruptor.RegisterClientServerDisruptorPublisher;
Expand All @@ -27,6 +28,7 @@
import org.apache.shenyu.admin.model.bean.DocItem;
import org.apache.shenyu.admin.model.dto.ApiDTO;
import org.apache.shenyu.admin.model.entity.ApiDO;
import org.apache.shenyu.admin.model.entity.SelectorDO;
import org.apache.shenyu.admin.model.entity.TagDO;
import org.apache.shenyu.admin.model.entity.TagRelationDO;
import org.apache.shenyu.admin.model.page.CommonPager;
Expand All @@ -39,6 +41,7 @@
import org.apache.shenyu.admin.model.vo.TagVO;
import org.apache.shenyu.admin.service.ApiService;
import org.apache.shenyu.common.enums.ApiSourceEnum;
import org.apache.shenyu.common.enums.PluginEnum;
import org.apache.shenyu.common.utils.JsonUtils;
import org.apache.shenyu.common.utils.ListUtil;
import org.apache.shenyu.admin.service.MetaDataService;
Expand Down Expand Up @@ -125,7 +128,7 @@ private String update(final ApiDTO apiDTO) {
if (ApiStateEnum.PUBLISHED.getState() == apiDO.getState()) {
register(apiDO);
} else if (ApiStateEnum.OFFLINE.getState() == apiDO.getState()) {
unregister(apiDO);
removeRegister(apiDO);
}
}
return ShenyuResultMessage.UPDATE_SUCCESS;
Expand Down Expand Up @@ -156,14 +159,12 @@ private String create(final ApiDTO apiDTO) {
}
if (ApiStateEnum.PUBLISHED.getState() == apiDO.getState()) {
register(apiDO);
} else if (ApiStateEnum.OFFLINE.getState() == apiDO.getState()) {
unregister(apiDO);
}
}
return ShenyuResultMessage.CREATE_SUCCESS;
}

private void unregister(final ApiDO apiDO) {
private void removeRegister(final ApiDO apiDO) {
final String path = apiDO.getApiPath();
RuleQueryCondition condition = new RuleQueryCondition();
condition.setKeyword(path);
Expand All @@ -176,14 +177,18 @@ private void unregister(final ApiDO apiDO) {
.collect(Collectors.toList()));
}
//clean selector
Optional.ofNullable(selectorService.findByName(apiDO.getContextPath()))
.ifPresent(selectorDO -> {
final String selectorId = selectorDO.getId();
final List<RuleData> data = ruleService.findBySelectorId(selectorId);
if (CollectionUtils.isEmpty(data)) {
selectorService.delete(Lists.newArrayList(selectorId));
}
});
List<SelectorDO> selectorDOList = selectorService.findByNameAndPluginNames(apiDO.getContextPath(), PluginEnum.getUpstreamNames());
ArrayList<String> selectorIds = Lists.newArrayList();
Optional.ofNullable(selectorDOList).orElseGet(ArrayList::new).stream().forEach(selectorDO -> {
final String selectorId = selectorDO.getId();
final List<RuleData> data = ruleService.findBySelectorId(selectorId);
if (CollectionUtils.isEmpty(data)) {
selectorIds.add(selectorId);
}
});
if (CollectionUtils.isNotEmpty(selectorIds)) {
selectorService.delete(selectorIds);
}
//clean metadata
Optional.ofNullable(metaDataService.findByPath(path))
.ifPresent(metaDataDO -> metaDataService.delete(Lists.newArrayList(metaDataDO.getId())));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -241,11 +241,17 @@ public SelectorVO findById(final String id) {

@Override
public SelectorDO findByName(final String name) {
List<SelectorDO> doList = selectorMapper.selectByName(name);
return CollectionUtils.isNotEmpty(doList) ? doList.get(0) : null;
}

@Override
public List<SelectorDO> findListByName(final String name) {
return selectorMapper.selectByName(name);
}

/**
* Find by name and plugin id selector do.
* Find selectorDO by name and plugin name.
*
* @param name the name
* @param pluginName the plugin name
Expand All @@ -257,9 +263,22 @@ public SelectorDO findByNameAndPluginName(final String name, final String plugin
return selectorMapper.findByNameAndPluginId(name, pluginDO.getId());
}

@Override
public List<SelectorDO> findByNameAndPluginNames(final String name, final List<String> pluginNames) {
final List<PluginDO> pluginDOList = pluginMapper.selectByNames(pluginNames);
if (CollectionUtils.isEmpty(pluginDOList)) {
return Lists.newArrayList();
}
List<String> pluginIds = pluginDOList.stream().map(it -> it.getId()).collect(Collectors.toList());
SelectorQuery selectorQuery = new SelectorQuery();
selectorQuery.setName(name);
selectorQuery.setPluginIds(pluginIds);
return selectorMapper.selectByQuery(selectorQuery);
}

@Override
public SelectorData buildByName(final String name) {
return buildSelectorData(selectorMapper.selectByName(name));
return buildSelectorData(this.findByName(name));
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -187,15 +187,14 @@ public static void removeByKey(final String selectorId) {
}

/**
* Submit.
* Submit client health check.
*
* @param selectorId the selector id
* @param commonUpstream the common upstream
* @return whether this module handles
*/
public boolean submit(final String selectorId, final CommonUpstream commonUpstream) {
public void submit(final String selectorId, final CommonUpstream commonUpstream) {
if (!REGISTER_TYPE_HTTP.equalsIgnoreCase(registerType) || !checked) {
return false;
return;
}

List<CommonUpstream> upstreams = MapUtils.computeIfAbsent(UPSTREAM_MAP, selectorId, k -> new CopyOnWriteArrayList<>());
Expand All @@ -213,7 +212,6 @@ public boolean submit(final String selectorId, final CommonUpstream commonUpstre
PENDING_SYNC.add(NumberUtils.INTEGER_ZERO);
}
executor.execute(() -> updateHandler(selectorId, upstreams, upstreams));
return true;
}

/**
Expand All @@ -234,7 +232,8 @@ public boolean submit(final String selectorId, final CommonUpstream commonUpstre
public boolean checkAndSubmit(final String selectorId, final CommonUpstream commonUpstream) {
final boolean pass = UpstreamCheckUtils.checkUrl(commonUpstream.getUpstreamUrl());
if (pass) {
return submit(selectorId, commonUpstream);
this.submit(selectorId, commonUpstream);
return false;
}
ZOMBIE_SET.add(ZombieUpstream.transform(commonUpstream, zombieCheckTimes, selectorId));
LOG.error("add zombie node, url={}", commonUpstream.getUpstreamUrl());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -112,7 +112,7 @@ public void addDocInfo(final UpstreamInstance instance, final String docInfoJson
.tags(Collections.singletonList(docInfo.getContextPath()))
.apiPath(docItem.getName())
.apiSource(ApiSourceEnum.SWAGGER.getValue())
.state(ApiStateEnum.PUBLISHED.getState())
.state(ApiStateEnum.UNPUBLISHED.getState())
.apiOwner("admin")
.eventType(EventType.REGISTER)
.build();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -107,20 +107,7 @@ protected String buildHandle(final List<URIRegisterDTO> uriList, final SelectorD
if (doSubmit(selectorDO.getId(), canAddList)) {
return null;
}

List<DivideUpstream> handleList;
if (CollectionUtils.isEmpty(existList)) {
handleList = addList;
} else {
List<DivideUpstream> aliveList;
if (isEventDeleted) {
aliveList = existList.stream().filter(e -> e.isStatus() && !e.equals(addList.get(0))).collect(Collectors.toList());
} else {
aliveList = addList;
}
handleList = divideSelectorHandleConverter.updateStatusAndFilter(existList, aliveList);
}
return GsonUtils.getInstance().toJson(handleList);
return GsonUtils.getInstance().toJson(CollectionUtils.isEmpty(existList) ? canAddList : existList);
}

private List<DivideUpstream> buildDivideUpstreamList(final List<URIRegisterDTO> uriList) {
Expand Down
Loading

0 comments on commit d3f6b61

Please sign in to comment.