From efda6fe23136b7e70d38f73635d03e3d0acb1c63 Mon Sep 17 00:00:00 2001 From: aias00 Date: Fri, 13 Sep 2024 14:06:31 +0800 Subject: [PATCH 1/2] [type:bugfix] grpc ci error debug (#5646) * [type:bugfix] grpc ci error debug * [type:bugfix] ingress sofa ci error debug * [type:bugfix] grpc e2e ci error debug * [type:bugfix] zookeeper client create or update * [type:bugfix] zookeeper client create or update * [type:bugfix] zookeeper client create or update * [type:bugfix] zookeeper client create or update * [type:bugfix] ingress sofa ci error debug * [type:bugfix] ingress sofa ci error debug * [type:bugfix] grpc etcd sync e2e ci error debug * [type:bugfix] ingress sofa ci error debug * [type:bugfix] remove unused log * [type:bugfix] grpc e2e ci error debug * [type:bugfix] grpc e2e ci error debug * [type:bugfix] ingress sofa ci error debug * [type:bugfix] zookeeper client create or update bug fix * [type:bugfix] remove debug log * [type:bugfix] zookeeper client create or update bug fix * [type:bugfix] zookeeper client create or update bug fix * [type:bugfix] etcd grpc e2e ci error debug * [type:bugfix] etcd grpc e2e ci error debug * [type:bugfix] etcd grpc e2e ci error debug * [type:bugfix] grpc e2e ci error debug * [type:bugfix] grpc e2e ci error debug * [type:bugfix] grpc e2e ci error debug * [type:bugfix] grpc e2e ci error debug * [type:bugfix] grpc e2e ci error debug * [type:bugfix] grpc e2e ci error debug * [type:bugfix] grpc e2e ci error debug * [type:bugfix] grpc e2e ci error debug * Revert "[type:bugfix] grpc e2e ci error debug" This reverts commit 42c807b9bef8b96f5203ca4690b3dc58f8168670. * [type:bugfix] grpc e2e ci error debug * [type:bugfix] fix grpc e2e upstream check error * [type:bugfix] fix grpc e2e error, undo some debug * [type:bugfix] fix grpc e2e error * [type:bugfix] fix grpc e2e error, undo some debug * Update ZookeeperClient.java * Update ClusterZookeeperClient.java * Update ZookeeperClient.java * Update ZookeeperClient.java --------- Co-authored-by: moremind Co-authored-by: xcsnx <1192709687@qq.com> --- .../AbstractPathDataChangedListener.java | 3 ++- .../admin/listener/etcd/EtcdClient.java | 8 ++++---- .../listener/zookeeper/ZookeeperClient.java | 20 +++++++++++++------ ...DiscoveryDataChangedEventSyncListener.java | 12 +++++------ .../zookeeper/ClusterZookeeperClient.java | 13 ++++++++++-- .../service/impl/UpstreamCheckService.java | 7 +++++++ ...stractShenyuClientRegisterServiceImpl.java | 2 +- .../FallbackShenyuClientRegisterService.java | 10 +++++----- ...ShenyuClientRegisterDivideServiceImpl.java | 5 ----- .../ShenyuClientRegisterGrpcServiceImpl.java | 6 ------ .../admin/transfer/DiscoveryTransfer.java | 11 +++++++++- ...yuClientRegisterDivideServiceImplTest.java | 4 ---- .../plugin/grpc/cache/GrpcClientCache.java | 3 +-- .../shenyu/registry/etcd/EtcdClient.java | 10 +++++----- .../registry/zookeeper/ZookeeperClient.java | 17 ++++++++++++---- .../shenyu/sync/data/etcd/EtcdClient.java | 8 ++++---- .../sync/data/etcd/EtcdSyncDataService.java | 4 +++- .../sync/data/zookeeper/ZookeeperClient.java | 19 +++++++++++++----- 18 files changed, 100 insertions(+), 62 deletions(-) diff --git a/shenyu-admin-listener/shenyu-admin-listener-api/src/main/java/org/apache/shenyu/admin/listener/AbstractPathDataChangedListener.java b/shenyu-admin-listener/shenyu-admin-listener-api/src/main/java/org/apache/shenyu/admin/listener/AbstractPathDataChangedListener.java index c0a5d8f8b515..b44d8ac54a67 100644 --- a/shenyu-admin-listener/shenyu-admin-listener-api/src/main/java/org/apache/shenyu/admin/listener/AbstractPathDataChangedListener.java +++ b/shenyu-admin-listener/shenyu-admin-listener-api/src/main/java/org/apache/shenyu/admin/listener/AbstractPathDataChangedListener.java @@ -26,6 +26,7 @@ import org.apache.shenyu.common.dto.MetaData; import org.apache.shenyu.common.dto.DiscoverySyncData; import org.apache.shenyu.common.enums.DataEventTypeEnum; +import org.apache.shenyu.common.utils.GsonUtils; import org.slf4j.Logger; import org.slf4j.LoggerFactory; @@ -102,7 +103,7 @@ public void onDiscoveryUpstreamChanged(final List changed, fi } // create or update createOrUpdate(upstreamPath, data); - LOG.info("[DataChangedListener] change discoveryUpstream path={}|data={}", upstreamPath, data); + LOG.info("[DataChangedListener] change discoveryUpstream path={}|data={}", upstreamPath, GsonUtils.getInstance().toJson(data)); } } diff --git a/shenyu-admin-listener/shenyu-admin-listener-etcd/src/main/java/org/apache/shenyu/admin/listener/etcd/EtcdClient.java b/shenyu-admin-listener/shenyu-admin-listener-etcd/src/main/java/org/apache/shenyu/admin/listener/etcd/EtcdClient.java index dbeb358a7450..7df9b4db276c 100644 --- a/shenyu-admin-listener/shenyu-admin-listener-etcd/src/main/java/org/apache/shenyu/admin/listener/etcd/EtcdClient.java +++ b/shenyu-admin-listener/shenyu-admin-listener-etcd/src/main/java/org/apache/shenyu/admin/listener/etcd/EtcdClient.java @@ -63,8 +63,8 @@ public Boolean exists(final String key) { List keyValues = client.getKVClient().get(ByteSequence.from(key, StandardCharsets.UTF_8), option).get().getKvs(); return !keyValues.isEmpty(); } catch (Exception e) { - LOG.error("check node exists error. {}", e.getMessage()); - throw new ShenyuException(e.getMessage()); + LOG.error("check node exists error", e); + throw new ShenyuException(e); } } @@ -78,7 +78,7 @@ public void put(final String key, final String value) { client.getKVClient().put(ByteSequence.from(key, StandardCharsets.UTF_8), ByteSequence.from(value, StandardCharsets.UTF_8)).get(); } catch (Exception e) { LOG.error("update value of node error.", e); - throw new ShenyuException(e.getMessage()); + throw new ShenyuException(e); } } @@ -102,7 +102,7 @@ public void deleteEtcdPathRecursive(final String path) { client.getKVClient().delete(ByteSequence.from(path, StandardCharsets.UTF_8), option).get(10, TimeUnit.SECONDS); } catch (Exception e) { LOG.error("delete node of recursive error.", e); - throw new ShenyuException(e.getMessage()); + throw new ShenyuException(e); } } } diff --git a/shenyu-admin-listener/shenyu-admin-listener-zookeeper/src/main/java/org/apache/shenyu/admin/listener/zookeeper/ZookeeperClient.java b/shenyu-admin-listener/shenyu-admin-listener-zookeeper/src/main/java/org/apache/shenyu/admin/listener/zookeeper/ZookeeperClient.java index a9148c61a1e7..3b1b6aabe969 100644 --- a/shenyu-admin-listener/shenyu-admin-listener-zookeeper/src/main/java/org/apache/shenyu/admin/listener/zookeeper/ZookeeperClient.java +++ b/shenyu-admin-listener/shenyu-admin-listener-zookeeper/src/main/java/org/apache/shenyu/admin/listener/zookeeper/ZookeeperClient.java @@ -41,7 +41,7 @@ public class ZookeeperClient { - private static final Logger LOGGER = LoggerFactory.getLogger(ZookeeperClient.class); + private static final Logger LOG = LoggerFactory.getLogger(ZookeeperClient.class); private final ZookeeperConfig config; @@ -75,7 +75,7 @@ public void start() { try { this.client.blockUntilConnected(); } catch (InterruptedException e) { - LOGGER.warn("Interrupted during zookeeper client starting."); + LOG.warn("Interrupted during zookeeper client starting."); Thread.currentThread().interrupt(); } } @@ -111,7 +111,6 @@ public boolean isExist(final String key) { try { return null != client.checkExists().forPath(key); } catch (Exception e) { - LOGGER.error("check if key exist error", e); return false; } } @@ -138,7 +137,7 @@ public String getDirectly(final String key) { * @return value. */ public String get(final String key) { - TreeCache cache = findFromcache(key); + TreeCache cache = findFromCache(key); if (Objects.isNull(cache)) { return getDirectly(key); } @@ -159,8 +158,17 @@ public String get(final String key) { public void createOrUpdate(final String key, final String value, final CreateMode mode) { String val = StringUtils.isEmpty(value) ? "" : value; try { - client.create().orSetData().creatingParentsIfNeeded().withMode(mode).forPath(key, val.getBytes(StandardCharsets.UTF_8)); + synchronized (ZookeeperClient.class) { + if (Objects.nonNull(client.checkExists()) && Objects.nonNull(client.checkExists().forPath(key))) { + LOG.debug("path exists, update zookeeper key={} with value={}", key, val); + client.setData().forPath(key, val.getBytes(StandardCharsets.UTF_8)); + return; + } + LOG.debug("path not exists, set zookeeper key={} with value={}", key, val); + client.create().orSetData().creatingParentsIfNeeded().withMode(mode).forPath(key, val.getBytes(StandardCharsets.UTF_8)); + } } catch (Exception e) { + LOG.error("create or update key with value error, key:{} value:{}", key, value, e); throw new ShenyuException(e); } } @@ -244,7 +252,7 @@ public TreeCache addCache(final String path, final TreeCacheListener... listener * @param key key. * @return cache. */ - private TreeCache findFromcache(final String key) { + private TreeCache findFromCache(final String key) { for (Map.Entry cache : caches.entrySet()) { if (key.startsWith(cache.getKey())) { return cache.getValue(); diff --git a/shenyu-admin/src/main/java/org/apache/shenyu/admin/discovery/DiscoveryDataChangedEventSyncListener.java b/shenyu-admin/src/main/java/org/apache/shenyu/admin/discovery/DiscoveryDataChangedEventSyncListener.java index afa968a977d3..ea981f7dd863 100644 --- a/shenyu-admin/src/main/java/org/apache/shenyu/admin/discovery/DiscoveryDataChangedEventSyncListener.java +++ b/shenyu-admin/src/main/java/org/apache/shenyu/admin/discovery/DiscoveryDataChangedEventSyncListener.java @@ -48,7 +48,7 @@ */ public class DiscoveryDataChangedEventSyncListener implements DataChangedEventListener { - private static final Logger LOGGER = LoggerFactory.getLogger(DiscoveryDataChangedEventSyncListener.class); + private static final Logger LOG = LoggerFactory.getLogger(DiscoveryDataChangedEventSyncListener.class); private final KeyValueParser keyValueParser; @@ -82,7 +82,7 @@ public void onChange(final DiscoveryDataChangedEvent event) { DiscoverySyncData discoverySyncData = buildProxySelectorData(event.getValue()); final List upstreamDataList = discoverySyncData.getUpstreamDataList(); if (CollectionUtils.isEmpty(upstreamDataList)) { - LOGGER.warn("shenyu proxySelectorData#discoveryUpstreamList is empty"); + LOG.warn("shenyu proxySelectorData#discoveryUpstreamList is empty"); return; } switch (currentEvent) { @@ -95,10 +95,10 @@ public void onChange(final DiscoveryDataChangedEvent event) { d.setDateCreated(new Timestamp(System.currentTimeMillis())); d.setDateUpdated(new Timestamp(System.currentTimeMillis())); discoveryUpstreamMapper.insert(DiscoveryTransfer.INSTANCE.mapToDo(d)); - LOGGER.info("shenyu [DiscoveryDataChangedEventSyncListener] ADDED Upstream {}", d.getUrl()); + LOG.info("shenyu [DiscoveryDataChangedEventSyncListener] ADDED Upstream {}", d.getUrl()); } } catch (DuplicateKeyException ex) { - LOGGER.info("shenyu [DiscoveryDataChangedEventSyncListener] Upstream {} exist", d.getUrl()); + LOG.info("shenyu [DiscoveryDataChangedEventSyncListener] Upstream {} exist", d.getUrl()); } }); break; @@ -106,14 +106,14 @@ public void onChange(final DiscoveryDataChangedEvent event) { upstreamDataList.stream().map(DiscoveryTransfer.INSTANCE::mapToDo).forEach(discoveryUpstreamDO -> { discoveryUpstreamDO.setDiscoveryHandlerId(discoveryHandlerId); int effect = discoveryUpstreamMapper.updateDiscoveryHandlerIdAndUrl(discoveryUpstreamDO); - LOGGER.info("shenyu [DiscoveryDataChangedEventSyncListener] UPDATE Upstream {}, effect = {} ", discoveryUpstreamDO.getUrl(), effect); + LOG.info("shenyu [DiscoveryDataChangedEventSyncListener] UPDATE Upstream {}, effect = {} ", discoveryUpstreamDO.getUrl(), effect); }); break; case DELETED: if (CollectionUtils.isNotEmpty(upstreamDataList)) { upstreamDataList.forEach(up -> { discoveryUpstreamMapper.deleteByUrl(discoveryHandlerId, up.getUrl()); - LOGGER.info("shenyu [DiscoveryDataChangedEventSyncListener] DELETE Upstream {}", up.getUrl()); + LOG.info("shenyu [DiscoveryDataChangedEventSyncListener] DELETE Upstream {}", up.getUrl()); }); } break; diff --git a/shenyu-admin/src/main/java/org/apache/shenyu/admin/mode/cluster/impl/zookeeper/ClusterZookeeperClient.java b/shenyu-admin/src/main/java/org/apache/shenyu/admin/mode/cluster/impl/zookeeper/ClusterZookeeperClient.java index cf047b491794..28afe6640c9b 100644 --- a/shenyu-admin/src/main/java/org/apache/shenyu/admin/mode/cluster/impl/zookeeper/ClusterZookeeperClient.java +++ b/shenyu-admin/src/main/java/org/apache/shenyu/admin/mode/cluster/impl/zookeeper/ClusterZookeeperClient.java @@ -148,7 +148,7 @@ public String get(final String key) { } return Objects.isNull(data.getData()) ? null : new String(data.getData(), StandardCharsets.UTF_8); } - + /** * create or update key with value. * @@ -159,8 +159,17 @@ public String get(final String key) { public void createOrUpdate(final String key, final String value, final CreateMode mode) { String val = StringUtils.isEmpty(value) ? "" : value; try { - client.create().orSetData().creatingParentsIfNeeded().withMode(mode).forPath(key, val.getBytes(StandardCharsets.UTF_8)); + synchronized (ClusterZookeeperClient.class) { + if (Objects.nonNull(client.checkExists()) && Objects.nonNull(client.checkExists().forPath(key))) { + LOGGER.debug("path exists, update zookeeper key={} with value={}", key, val); + client.setData().forPath(key, val.getBytes(StandardCharsets.UTF_8)); + return; + } + LOGGER.debug("path not exists, set zookeeper key={} with value={}", key, val); + client.create().orSetData().creatingParentsIfNeeded().withMode(mode).forPath(key, val.getBytes(StandardCharsets.UTF_8)); + } } catch (Exception e) { + LOGGER.error("create or update key with value error, key:{} value:{}", key, value, e); throw new ShenyuException(e); } } diff --git a/shenyu-admin/src/main/java/org/apache/shenyu/admin/service/impl/UpstreamCheckService.java b/shenyu-admin/src/main/java/org/apache/shenyu/admin/service/impl/UpstreamCheckService.java index ac7c66bdb2f5..0cd3e6573f2d 100644 --- a/shenyu-admin/src/main/java/org/apache/shenyu/admin/service/impl/UpstreamCheckService.java +++ b/shenyu-admin/src/main/java/org/apache/shenyu/admin/service/impl/UpstreamCheckService.java @@ -408,6 +408,11 @@ private void updateSelectorHandler(final String selectorId, final List discoveryUpstreamDataList = discoveryUpstreamService.findBySelectorId(selectorId); + + if (CollectionUtils.isEmpty(discoveryUpstreamDataList)) { + discoveryUpstreamDataList = aliveList.stream().map(DiscoveryTransfer.INSTANCE::mapToDiscoveryUpstreamData).collect(Collectors.toList()); + } + discoveryUpstreamDataList.removeIf(u -> { for (CommonUpstream alive : aliveList) { if (alive.getUpstreamUrl().equals(u.getUrl())) { @@ -421,11 +426,13 @@ private void updateSelectorHandler(final String selectorId, final List fallsRegisters = new ConcurrentHashMap<>(); @@ -64,9 +64,9 @@ public String registerURI(final String selectorName, final List try { this.removeFallBack(key); result = this.doRegisterURI(selectorName, uriList); - logger.info("Register success: {},{}", selectorName, uriList); + LOG.info("Register success: {},{}", selectorName, uriList); } catch (Exception ex) { - logger.warn("Register exception: cause:{}", ex.getMessage()); + LOG.error("Register exception: cause:", ex); result = ""; this.addFallback(key, new FallbackHolder(selectorName, uriList)); } @@ -81,7 +81,7 @@ private void addFallback(final String key, final FallbackHolder holder) { FallbackRegisterTask registryTask = new FallbackRegisterTask(key, this); fallsRegisters.put(key, holder); timer.add(registryTask); - logger.info("Add to Fallback and wait for execution, {}:{}", holder.getSelectorName(), holder.getUriList()); + LOG.info("Add to Fallback and wait for execution, {}:{}", holder.getSelectorName(), holder.getUriList()); } private void removeFallBack(final String key) { @@ -94,7 +94,7 @@ private void recover(final String key) { List uriList = fallbackHolder.getUriList(); String selectorName = fallbackHolder.getSelectorName(); this.doRegisterURI(selectorName, uriList); - logger.info("Register success: {},{}", selectorName, uriList); + LOG.info("Register success: {},{}", selectorName, uriList); } } diff --git a/shenyu-admin/src/main/java/org/apache/shenyu/admin/service/register/ShenyuClientRegisterDivideServiceImpl.java b/shenyu-admin/src/main/java/org/apache/shenyu/admin/service/register/ShenyuClientRegisterDivideServiceImpl.java index a3ce16bc64ed..1c623ace8ba5 100644 --- a/shenyu-admin/src/main/java/org/apache/shenyu/admin/service/register/ShenyuClientRegisterDivideServiceImpl.java +++ b/shenyu-admin/src/main/java/org/apache/shenyu/admin/service/register/ShenyuClientRegisterDivideServiceImpl.java @@ -24,7 +24,6 @@ import org.apache.shenyu.admin.model.entity.SelectorDO; import org.apache.shenyu.admin.service.MetaDataService; import org.apache.shenyu.admin.service.SelectorService; -import org.apache.shenyu.admin.service.converter.DivideSelectorHandleConverter; import org.apache.shenyu.admin.utils.CommonUpstreamUtils; import org.apache.shenyu.common.constant.Constants; import org.apache.shenyu.common.dto.DiscoverySyncData; @@ -41,7 +40,6 @@ import org.apache.shenyu.register.common.enums.EventType; import org.springframework.stereotype.Service; -import jakarta.annotation.Resource; import java.util.Collections; import java.util.List; import java.util.Objects; @@ -56,9 +54,6 @@ @Service public class ShenyuClientRegisterDivideServiceImpl extends AbstractContextPathRegisterService { - @Resource - private DivideSelectorHandleConverter divideSelectorHandleConverter; - @Override public String rpcType() { return RpcTypeEnum.HTTP.getName(); diff --git a/shenyu-admin/src/main/java/org/apache/shenyu/admin/service/register/ShenyuClientRegisterGrpcServiceImpl.java b/shenyu-admin/src/main/java/org/apache/shenyu/admin/service/register/ShenyuClientRegisterGrpcServiceImpl.java index 7bff6fb6bec1..f991ea91f046 100644 --- a/shenyu-admin/src/main/java/org/apache/shenyu/admin/service/register/ShenyuClientRegisterGrpcServiceImpl.java +++ b/shenyu-admin/src/main/java/org/apache/shenyu/admin/service/register/ShenyuClientRegisterGrpcServiceImpl.java @@ -17,7 +17,6 @@ package org.apache.shenyu.admin.service.register; -import jakarta.annotation.Resource; import org.apache.commons.collections4.CollectionUtils; import org.apache.commons.lang3.StringUtils; import org.apache.shenyu.admin.listener.DataChangedEvent; @@ -25,7 +24,6 @@ import org.apache.shenyu.admin.model.entity.SelectorDO; import org.apache.shenyu.admin.service.MetaDataService; import org.apache.shenyu.admin.service.SelectorService; -import org.apache.shenyu.admin.service.converter.GrpcSelectorHandleConverter; import org.apache.shenyu.admin.utils.CommonUpstreamUtils; import org.apache.shenyu.common.constant.Constants; import org.apache.shenyu.common.dto.DiscoverySyncData; @@ -58,9 +56,6 @@ public class ShenyuClientRegisterGrpcServiceImpl extends AbstractShenyuClientReg private static final Logger LOG = LoggerFactory.getLogger(ShenyuClientRegisterGrpcServiceImpl.class); - @Resource - private GrpcSelectorHandleConverter grpcSelectorHandleConverter; - @Override public String rpcType() { return RpcTypeEnum.GRPC.getName(); @@ -116,7 +111,6 @@ protected String buildHandle(final List uriList, final SelectorD canAddList.addAll(diffStatusList); } } - if (doSubmit(selectorDO.getId(), canAddList)) { return null; } diff --git a/shenyu-admin/src/main/java/org/apache/shenyu/admin/transfer/DiscoveryTransfer.java b/shenyu-admin/src/main/java/org/apache/shenyu/admin/transfer/DiscoveryTransfer.java index bd60b695188b..e3e0e84369fc 100644 --- a/shenyu-admin/src/main/java/org/apache/shenyu/admin/transfer/DiscoveryTransfer.java +++ b/shenyu-admin/src/main/java/org/apache/shenyu/admin/transfer/DiscoveryTransfer.java @@ -31,6 +31,7 @@ import org.apache.shenyu.admin.model.vo.DiscoveryRelVO; import org.apache.shenyu.admin.model.vo.DiscoveryUpstreamVO; import org.apache.shenyu.admin.model.vo.DiscoveryVO; +import org.apache.shenyu.admin.utils.CommonUpstreamUtils; import org.apache.shenyu.common.dto.DiscoveryUpstreamData; import org.apache.shenyu.common.dto.ProxySelectorData; import org.apache.shenyu.common.dto.convert.selector.CommonUpstream; @@ -329,5 +330,13 @@ public DiscoveryUpstreamDTO mapToDTO(DiscoveryUpstreamDO discoveryUpstreamDO) { return discoveryUpstreamDTO; }).orElse(null); } - + + /** + * mapToDiscoveryUpstreamData. + * @param commonUpstream commonUpstream + * @return DiscoveryUpstreamData + */ + public DiscoveryUpstreamData mapToDiscoveryUpstreamData(CommonUpstream commonUpstream) { + return mapToData(CommonUpstreamUtils.buildDefaultDiscoveryUpstreamDTO(commonUpstream.getUpstreamUrl().split(":")[0], Integer.valueOf(commonUpstream.getUpstreamUrl().split(":")[1]), commonUpstream.getProtocol())); + } } diff --git a/shenyu-admin/src/test/java/org/apache/shenyu/admin/service/register/ShenyuClientRegisterDivideServiceImplTest.java b/shenyu-admin/src/test/java/org/apache/shenyu/admin/service/register/ShenyuClientRegisterDivideServiceImplTest.java index d7c205f0e146..1b30a356b878 100644 --- a/shenyu-admin/src/test/java/org/apache/shenyu/admin/service/register/ShenyuClientRegisterDivideServiceImplTest.java +++ b/shenyu-admin/src/test/java/org/apache/shenyu/admin/service/register/ShenyuClientRegisterDivideServiceImplTest.java @@ -20,7 +20,6 @@ import org.apache.commons.lang3.StringUtils; import org.apache.shenyu.admin.model.entity.MetaDataDO; import org.apache.shenyu.admin.model.entity.SelectorDO; -import org.apache.shenyu.admin.service.converter.DivideSelectorHandleConverter; import org.apache.shenyu.admin.service.impl.MetaDataServiceImpl; import org.apache.shenyu.common.dto.convert.rule.impl.DivideRuleHandle; import org.apache.shenyu.common.dto.convert.selector.DivideUpstream; @@ -38,7 +37,6 @@ import org.mockito.junit.jupiter.MockitoExtension; import org.mockito.junit.jupiter.MockitoSettings; import org.mockito.quality.Strictness; -import org.springframework.test.util.ReflectionTestUtils; import java.lang.reflect.Method; import java.util.ArrayList; @@ -69,8 +67,6 @@ public final class ShenyuClientRegisterDivideServiceImplTest { @BeforeEach public void setUp() { - DivideSelectorHandleConverter divideSelectorHandleConverter = new DivideSelectorHandleConverter(); - ReflectionTestUtils.setField(shenyuClientRegisterDivideService, "divideSelectorHandleConverter", divideSelectorHandleConverter); } @Test diff --git a/shenyu-plugin/shenyu-plugin-proxy/shenyu-plugin-rpc/shenyu-plugin-grpc/src/main/java/org/apache/shenyu/plugin/grpc/cache/GrpcClientCache.java b/shenyu-plugin/shenyu-plugin-proxy/shenyu-plugin-rpc/shenyu-plugin-grpc/src/main/java/org/apache/shenyu/plugin/grpc/cache/GrpcClientCache.java index f9aeb512fa03..0ef8fa5bba27 100644 --- a/shenyu-plugin/shenyu-plugin-proxy/shenyu-plugin-rpc/shenyu-plugin-grpc/src/main/java/org/apache/shenyu/plugin/grpc/cache/GrpcClientCache.java +++ b/shenyu-plugin/shenyu-plugin-proxy/shenyu-plugin-rpc/shenyu-plugin-grpc/src/main/java/org/apache/shenyu/plugin/grpc/cache/GrpcClientCache.java @@ -18,7 +18,6 @@ package org.apache.shenyu.plugin.grpc.cache; import com.google.common.collect.Maps; -import org.apache.shenyu.common.utils.MapUtils; import org.apache.shenyu.plugin.grpc.client.GrpcClientBuilder; import org.apache.shenyu.plugin.grpc.client.ShenyuGrpcClient; @@ -51,7 +50,7 @@ private GrpcClientCache() { * @param selectorId selectorId */ public static void initGrpcClient(final String selectorId) { - MapUtils.computeIfAbsent(CLIENT_CACHE, selectorId, s -> GrpcClientBuilder.buildClient(selectorId)); + CLIENT_CACHE.putIfAbsent(selectorId, GrpcClientBuilder.buildClient(selectorId)); } /** diff --git a/shenyu-registry/shenyu-registry-etcd/src/main/java/org/apache/shenyu/registry/etcd/EtcdClient.java b/shenyu-registry/shenyu-registry-etcd/src/main/java/org/apache/shenyu/registry/etcd/EtcdClient.java index 7be597c7850d..2946013b3ab5 100644 --- a/shenyu-registry/shenyu-registry-etcd/src/main/java/org/apache/shenyu/registry/etcd/EtcdClient.java +++ b/shenyu-registry/shenyu-registry-etcd/src/main/java/org/apache/shenyu/registry/etcd/EtcdClient.java @@ -46,7 +46,7 @@ public class EtcdClient { public static final Charset UTF_8 = StandardCharsets.UTF_8; - private static final Logger LOGGER = LoggerFactory.getLogger(EtcdClient.class); + private static final Logger LOG = LoggerFactory.getLogger(EtcdClient.class); private final Client client; @@ -73,7 +73,7 @@ public void onNext(final LeaseKeepAliveResponse leaseKeepAliveResponse) { @Override public void onError(final Throwable throwable) { - LOGGER.error("keep alive error", throwable); + LOG.error("keep alive error", throwable); } @Override @@ -81,7 +81,7 @@ public void onCompleted() { } }); } catch (InterruptedException | ExecutionException e) { - LOGGER.error("initLease error.", e); + LOG.error("initLease error.", e); } } @@ -125,7 +125,7 @@ public GetResponse getRange(final String key, final GetOption getOption) { try { return this.client.getKVClient().get(bytesOf(key), getOption).get(); } catch (ExecutionException | InterruptedException e) { - LOGGER.error("etcd getRange key {} error", key, e); + LOG.error("etcd getRange key {} error", key, e); throw new ShenyuException(e); } } @@ -158,7 +158,7 @@ public void putEphemeral(final String key, final String value) { PutOption.newBuilder().withLeaseId(globalLeaseId).build()) .get(timeout, TimeUnit.MILLISECONDS); } catch (InterruptedException | ExecutionException | TimeoutException e) { - LOGGER.error("putEphemeral(key:{},value:{}) error.", key, value, e); + LOG.error("putEphemeral(key:{},value:{}) error.", key, value, e); } } } diff --git a/shenyu-registry/shenyu-registry-zookeeper/src/main/java/org/apache/shenyu/registry/zookeeper/ZookeeperClient.java b/shenyu-registry/shenyu-registry-zookeeper/src/main/java/org/apache/shenyu/registry/zookeeper/ZookeeperClient.java index 103425323430..da6b6a1f4887 100644 --- a/shenyu-registry/shenyu-registry-zookeeper/src/main/java/org/apache/shenyu/registry/zookeeper/ZookeeperClient.java +++ b/shenyu-registry/shenyu-registry-zookeeper/src/main/java/org/apache/shenyu/registry/zookeeper/ZookeeperClient.java @@ -41,7 +41,7 @@ public class ZookeeperClient { - private static final Logger LOGGER = LoggerFactory.getLogger(ZookeeperClient.class); + private static final Logger LOG = LoggerFactory.getLogger(ZookeeperClient.class); private final ZookeeperConfig config; @@ -75,7 +75,7 @@ public void start() { try { this.client.blockUntilConnected(); } catch (InterruptedException e) { - LOGGER.warn("Interrupted during zookeeper client starting."); + LOG.warn("Interrupted during zookeeper client starting."); Thread.currentThread().interrupt(); } } @@ -147,7 +147,7 @@ public String get(final String key) { } return Objects.isNull(data.getData()) ? null : new String(data.getData(), StandardCharsets.UTF_8); } - + /** * create or update key with value. * @@ -158,8 +158,17 @@ public String get(final String key) { public void createOrUpdate(final String key, final String value, final CreateMode mode) { String val = StringUtils.isEmpty(value) ? "" : value; try { - client.create().orSetData().creatingParentsIfNeeded().withMode(mode).forPath(key, val.getBytes(StandardCharsets.UTF_8)); + synchronized (ZookeeperClient.class) { + if (Objects.nonNull(client.checkExists()) && Objects.nonNull(client.checkExists().forPath(key))) { + LOG.debug("path exists, update zookeeper key={} with value={}", key, val); + client.setData().forPath(key, val.getBytes(StandardCharsets.UTF_8)); + return; + } + LOG.debug("path not exists, set zookeeper key={} with value={}", key, val); + client.create().orSetData().creatingParentsIfNeeded().withMode(mode).forPath(key, val.getBytes(StandardCharsets.UTF_8)); + } } catch (Exception e) { + LOG.error("create or update key with value error, key:{} value:{}", key, value, e); throw new ShenyuException(e); } } diff --git a/shenyu-sync-data-center/shenyu-sync-data-etcd/src/main/java/org/apache/shenyu/sync/data/etcd/EtcdClient.java b/shenyu-sync-data-center/shenyu-sync-data-etcd/src/main/java/org/apache/shenyu/sync/data/etcd/EtcdClient.java index 22b08b94ab3c..a109a2a8b4f8 100644 --- a/shenyu-sync-data-center/shenyu-sync-data-etcd/src/main/java/org/apache/shenyu/sync/data/etcd/EtcdClient.java +++ b/shenyu-sync-data-center/shenyu-sync-data-etcd/src/main/java/org/apache/shenyu/sync/data/etcd/EtcdClient.java @@ -17,10 +17,10 @@ package org.apache.shenyu.sync.data.etcd; +import io.etcd.jetcd.ByteSequence; import io.etcd.jetcd.Client; import io.etcd.jetcd.KeyValue; import io.etcd.jetcd.Watch; -import io.etcd.jetcd.ByteSequence; import io.etcd.jetcd.options.GetOption; import io.etcd.jetcd.options.WatchOption; import io.etcd.jetcd.watch.WatchEvent; @@ -79,13 +79,13 @@ public String get(final String key) { try { keyValues = client.getKVClient().get(bytesOf(key)).get().getKvs(); } catch (InterruptedException | ExecutionException e) { - LOG.error(e.getMessage(), e); + LOG.error("get key error, key:{}", key, e); } if (CollectionUtils.isEmpty(keyValues)) { + LOG.warn("get key {} is empty", key); return null; } - return keyValues.iterator().next().getValue().toString(UTF_8); } @@ -104,7 +104,7 @@ public Map getKeysMapByPrefix(final String prefix) { .get().getKvs().stream() .collect(Collectors.toMap(e -> e.getKey().toString(UTF_8), e -> e.getValue().toString(UTF_8))); } catch (ExecutionException | InterruptedException e) { - LOG.error("etcd getKeysMapByPrefix key {} error {}", prefix, e); + LOG.error("etcd getKeysMapByPrefix key {} error", prefix, e); throw new ShenyuException(e); } diff --git a/shenyu-sync-data-center/shenyu-sync-data-etcd/src/main/java/org/apache/shenyu/sync/data/etcd/EtcdSyncDataService.java b/shenyu-sync-data-center/shenyu-sync-data-etcd/src/main/java/org/apache/shenyu/sync/data/etcd/EtcdSyncDataService.java index 407e9b723917..3021ea91ebc3 100644 --- a/shenyu-sync-data-center/shenyu-sync-data-etcd/src/main/java/org/apache/shenyu/sync/data/etcd/EtcdSyncDataService.java +++ b/shenyu-sync-data-center/shenyu-sync-data-etcd/src/main/java/org/apache/shenyu/sync/data/etcd/EtcdSyncDataService.java @@ -85,9 +85,11 @@ private void watcherData0(final String registerPath) { final String nodeData = etcdClient.get(updatePath); super.event(updatePath, nodeData, registerPath, EventType.PUT); }); + } else { + LOG.info("etcd watcher path {} is empty", registerPath); } } catch (Exception e) { - LOG.error(e.getMessage(), e); + LOG.error("watcherData0 errors", e); } } diff --git a/shenyu-sync-data-center/shenyu-sync-data-zookeeper/src/main/java/org/apache/shenyu/sync/data/zookeeper/ZookeeperClient.java b/shenyu-sync-data-center/shenyu-sync-data-zookeeper/src/main/java/org/apache/shenyu/sync/data/zookeeper/ZookeeperClient.java index 7fc699c804e2..ea2d6fc43b7d 100644 --- a/shenyu-sync-data-center/shenyu-sync-data-zookeeper/src/main/java/org/apache/shenyu/sync/data/zookeeper/ZookeeperClient.java +++ b/shenyu-sync-data-center/shenyu-sync-data-zookeeper/src/main/java/org/apache/shenyu/sync/data/zookeeper/ZookeeperClient.java @@ -41,7 +41,7 @@ public class ZookeeperClient implements AutoCloseable { - private static final Logger LOGGER = LoggerFactory.getLogger(ZookeeperClient.class); + private static final Logger LOG = LoggerFactory.getLogger(ZookeeperClient.class); private final ZookeeperConfig config; @@ -75,7 +75,7 @@ public void start() { try { this.client.blockUntilConnected(); } catch (InterruptedException e) { - LOGGER.warn("Interrupted during zookeeper client starting."); + LOG.warn("Interrupted during zookeeper client starting."); Thread.currentThread().interrupt(); } } @@ -148,7 +148,7 @@ public String get(final String key) { } return Objects.isNull(data.getData()) ? null : new String(data.getData(), StandardCharsets.UTF_8); } - + /** * create or update key with value. * @@ -159,8 +159,17 @@ public String get(final String key) { public void createOrUpdate(final String key, final String value, final CreateMode mode) { String val = StringUtils.isEmpty(value) ? "" : value; try { - client.create().orSetData().creatingParentsIfNeeded().withMode(mode).forPath(key, val.getBytes(StandardCharsets.UTF_8)); + synchronized (ZookeeperClient.class) { + if (Objects.nonNull(client.checkExists()) && Objects.nonNull(client.checkExists().forPath(key))) { + LOG.debug("path exists, update zookeeper key={} with value={}", key, val); + client.setData().forPath(key, val.getBytes(StandardCharsets.UTF_8)); + return; + } + LOG.debug("path not exists, set zookeeper key={} with value={}", key, val); + client.create().orSetData().creatingParentsIfNeeded().withMode(mode).forPath(key, val.getBytes(StandardCharsets.UTF_8)); + } } catch (Exception e) { + LOG.error("create or update key with value error, key:{} value:{}", key, value, e); throw new ShenyuException(e); } } @@ -204,7 +213,7 @@ public List getChildren(final String key) { try { return client.getChildren().forPath(key); } catch (Exception e) { - LOGGER.error("zookeeper get child error=", e); + LOG.error("zookeeper get child error=", e); return Collections.emptyList(); } } From ea27db835ae797a0e59ed9a1906e6b450b3ce7ef Mon Sep 17 00:00:00 2001 From: moremind Date: Sat, 14 Sep 2024 23:26:53 +0800 Subject: [PATCH 2/2] [type: fix] fix e2e cluster error (#5657) * refactor namespace id * refactor namespace id * refactor namespace id * refactor namespace id * refactor namespace id --- .github/workflows/e2e-k8s.yml | 10 ++-------- .../shenyu-e2e-case-cluster/k8s/script/healthcheck.sh | 2 +- 2 files changed, 3 insertions(+), 9 deletions(-) diff --git a/.github/workflows/e2e-k8s.yml b/.github/workflows/e2e-k8s.yml index a50b21aeb68a..08cd17a8b2b8 100644 --- a/.github/workflows/e2e-k8s.yml +++ b/.github/workflows/e2e-k8s.yml @@ -65,22 +65,16 @@ jobs: java-version: '17' distribution: 'temurin' - - uses: dorny/paths-filter@v2 - id: filter - with: - filters: '.github/filters.yml' - list-files: json - - name: Build with Maven - if: steps.filter.outputs.changed == 'true' run: ./mvnw -B clean install -Prelease,docker -Dmaven.javadoc.skip=true -B -Drat.skip=true -Dmaven.test.skip=true -Djacoco.skip=true -DskipITs -DskipTests package -T1C - name: Save ShenYu Maven Repos - if: steps.filter.outputs.changed == 'true' && steps.restore-maven-cache.outputs.cache-hit != 'true' uses: actions/cache/save@v3 with: path: ~/.m2/repository key: ${{ runner.os }}-maven-${{ hashFiles('**/pom.xml') }} + restore-keys: | + ${{ runner.os }}-maven- # - name: Build shenyu-e2e-engine with Maven # run: ./mvnw -B -f ./shenyu-e2e/pom.xml -pl shenyu-e2e-engine -am clean install diff --git a/shenyu-e2e/shenyu-e2e-case/shenyu-e2e-case-cluster/k8s/script/healthcheck.sh b/shenyu-e2e/shenyu-e2e-case/shenyu-e2e-case-cluster/k8s/script/healthcheck.sh index 7dbb32189efc..53fdb1e6dfc2 100644 --- a/shenyu-e2e/shenyu-e2e-case/shenyu-e2e-case-cluster/k8s/script/healthcheck.sh +++ b/shenyu-e2e/shenyu-e2e-case/shenyu-e2e-case-cluster/k8s/script/healthcheck.sh @@ -29,7 +29,7 @@ do break fi - sleep 2 + sleep 5 done done