com.alibaba.csp.sentinel.util.function.Tuple2 Java Examples
The following examples show how to use
com.alibaba.csp.sentinel.util.function.Tuple2.
You can vote up the ones you like or vote down the ones you don't like,
and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: SentinelAutoConfigurationTests.java From spring-cloud-alibaba with Apache License 2.0 | 6 votes |
private void checkEndpoint() { SentinelEndpoint sentinelEndpoint = new SentinelEndpoint(sentinelProperties); Map<String, Object> map = sentinelEndpoint.invoke(); assertThat(map.get("logUsePid")).isEqualTo(Boolean.TRUE); assertThat(map.get("consoleServer").toString()).isEqualTo( Arrays.asList(Tuple2.of("localhost", 8080), Tuple2.of("localhost", 8081)) .toString()); assertThat(map.get("clientPort")).isEqualTo("9999"); assertThat(map.get("heartbeatIntervalMs")).isEqualTo(20000L); assertThat(map.get("clientIp")).isEqualTo("1.1.1.1"); assertThat(map.get("metricsFileSize")).isEqualTo(9999L); assertThat(map.get("totalMetricsFileCount")).isEqualTo(100); assertThat(map.get("metricsFileCharset")).isEqualTo("UTF-8"); assertThat(map.get("blockPage")).isEqualTo("/error"); }
Example #2
Source File: ClusterAssignServiceImpl.java From Sentinel-Dashboard-Nacos with Apache License 2.0 | 6 votes |
private void applyAllRemainingMachineSet(String app, Set<String> remainingSet, Set<String> failedSet) { if (remainingSet == null || remainingSet.isEmpty()) { return; } remainingSet.parallelStream() .filter(Objects::nonNull) .map(MachineUtils::parseCommandIpAndPort) .filter(Optional::isPresent) .map(Optional::get) .map(ipPort -> { String ip = ipPort.r1; int commandPort = ipPort.r2; CompletableFuture<Void> f = modifyMode(ip, commandPort, ClusterStateManager.CLUSTER_NOT_STARTED); return Tuple2.of(ip + '@' + commandPort, f); }) .forEach(t -> handleFutureSync(t, failedSet)); }
Example #3
Source File: ClusterAssignServiceImpl.java From Sentinel-Dashboard-Nacos with Apache License 2.0 | 6 votes |
private void applyAllClientConfigChange(String app, ClusterAppAssignMap assignMap, Set<String> failedSet) { Set<String> clientSet = assignMap.getClientSet(); if (clientSet == null || clientSet.isEmpty()) { return; } final String serverIp = assignMap.getIp(); final int serverPort = assignMap.getPort(); clientSet.stream() .map(MachineUtils::parseCommandIpAndPort) .filter(Optional::isPresent) .map(Optional::get) .map(ipPort -> { CompletableFuture<Void> f = sentinelApiClient .modifyClusterMode(ipPort.r1, ipPort.r2, ClusterStateManager.CLUSTER_CLIENT) .thenCompose(v -> sentinelApiClient.modifyClusterClientConfig(app, ipPort.r1, ipPort.r2, new ClusterClientConfig().setRequestTimeout(20) .setServerHost(serverIp) .setServerPort(serverPort) )); return Tuple2.of(ipPort.r1 + '@' + ipPort.r2, f); }) .forEach(t -> handleFutureSync(t, failedSet)); }
Example #4
Source File: ClusterAssignServiceImpl.java From Sentinel with Apache License 2.0 | 6 votes |
private void applyAllClientConfigChange(String app, ClusterAppAssignMap assignMap, Set<String> failedSet) { Set<String> clientSet = assignMap.getClientSet(); if (clientSet == null || clientSet.isEmpty()) { return; } final String serverIp = assignMap.getIp(); final int serverPort = assignMap.getPort(); clientSet.stream() .map(MachineUtils::parseCommandIpAndPort) .filter(Optional::isPresent) .map(Optional::get) .map(ipPort -> { CompletableFuture<Void> f = sentinelApiClient .modifyClusterMode(ipPort.r1, ipPort.r2, ClusterStateManager.CLUSTER_CLIENT) .thenCompose(v -> sentinelApiClient.modifyClusterClientConfig(app, ipPort.r1, ipPort.r2, new ClusterClientConfig().setRequestTimeout(20) .setServerHost(serverIp) .setServerPort(serverPort) )); return Tuple2.of(ipPort.r1 + '@' + ipPort.r2, f); }) .forEach(t -> handleFutureSync(t, failedSet)); }
Example #5
Source File: ClusterAssignServiceImpl.java From Sentinel with Apache License 2.0 | 6 votes |
private void applyAllRemainingMachineSet(String app, Set<String> remainingSet, Set<String> failedSet) { if (remainingSet == null || remainingSet.isEmpty()) { return; } remainingSet.parallelStream() .filter(Objects::nonNull) .map(MachineUtils::parseCommandIpAndPort) .filter(Optional::isPresent) .map(Optional::get) .map(ipPort -> { String ip = ipPort.r1; int commandPort = ipPort.r2; CompletableFuture<Void> f = modifyMode(ip, commandPort, ClusterStateManager.CLUSTER_NOT_STARTED); return Tuple2.of(ip + '@' + commandPort, f); }) .forEach(t -> handleFutureSync(t, failedSet)); }
Example #6
Source File: HttpHeartbeatSender.java From Sentinel with Apache License 2.0 | 5 votes |
public HttpHeartbeatSender() { this.client = HttpClients.createDefault(); List<Tuple2<String, Integer>> dashboardList = TransportConfig.getConsoleServerList(); if (dashboardList == null || dashboardList.isEmpty()) { RecordLog.info("[NettyHttpHeartbeatSender] No dashboard server available"); consoleHost = null; consolePort = -1; } else { consoleHost = dashboardList.get(0).r1; consolePort = dashboardList.get(0).r2; RecordLog.info( "[NettyHttpHeartbeatSender] Dashboard address parsed: <" + consoleHost + ':' + consolePort + ">"); } }
Example #7
Source File: SentinelAutoConfigurationTests.java From spring-cloud-alibaba with Apache License 2.0 | 5 votes |
@Test public void testSentinelSystemProperties() { assertThat(LogBase.isLogNameUsePid()).isEqualTo(true); assertThat(TransportConfig.getConsoleServerList().toString()).isEqualTo( Arrays.asList(Tuple2.of("localhost", 8080), Tuple2.of("localhost", 8081)) .toString()); assertThat(TransportConfig.getPort()).isEqualTo("9999"); assertThat(TransportConfig.getHeartbeatIntervalMs().longValue()) .isEqualTo(20000L); assertThat(TransportConfig.getHeartbeatClientIp()).isEqualTo("1.1.1.1"); assertThat(SentinelConfig.singleMetricFileSize()).isEqualTo(9999); assertThat(SentinelConfig.totalMetricFileCount()).isEqualTo(100); assertThat(SentinelConfig.charset()).isEqualTo("UTF-8"); assertThat(SentinelConfig.getConfig(BLOCK_PAGE_URL_CONF_KEY)).isEqualTo("/error"); }
Example #8
Source File: MachineUtils.java From Sentinel with Apache License 2.0 | 5 votes |
public static Optional<Tuple2<String, Integer>> parseCommandIpAndPort(String machineIp) { try { if (StringUtil.isEmpty(machineIp) || !machineIp.contains("@")) { return Optional.empty(); } String[] str = machineIp.split("@"); if (str.length <= 1) { return Optional.empty(); } return Optional.of(Tuple2.of(str[0], Integer.parseInt(str[1]))); } catch (Exception ex) { return Optional.empty(); } }
Example #9
Source File: ClusterAssignServiceImpl.java From Sentinel with Apache License 2.0 | 5 votes |
private void handleFutureSync(Tuple2<String, CompletableFuture<Void>> t, Set<String> failedSet) { try { t.r2.get(10, TimeUnit.SECONDS); } catch (Exception ex) { if (ex instanceof ExecutionException) { LOGGER.error("Request for <{}> failed", t.r1, ex.getCause()); } else { LOGGER.error("Request for <{}> failed", t.r1, ex); } failedSet.add(t.r1); } }
Example #10
Source File: ClusterAssignServiceImpl.java From Sentinel with Apache License 2.0 | 5 votes |
@Override public ClusterAppAssignResultVO applyAssignToApp(String app, List<ClusterAppAssignMap> clusterMap, Set<String> remainingSet) { AssertUtil.assertNotBlank(app, "app cannot be blank"); AssertUtil.notNull(clusterMap, "clusterMap cannot be null"); Set<String> failedServerSet = new HashSet<>(); Set<String> failedClientSet = new HashSet<>(); // Assign server and apply config. clusterMap.stream() .filter(Objects::nonNull) .filter(ClusterAppAssignMap::getBelongToApp) .map(e -> { String ip = e.getIp(); int commandPort = parsePort(e); CompletableFuture<Void> f = modifyMode(ip, commandPort, ClusterStateManager.CLUSTER_SERVER) .thenCompose(v -> applyServerConfigChange(app, ip, commandPort, e)); return Tuple2.of(e.getMachineId(), f); }) .forEach(t -> handleFutureSync(t, failedServerSet)); // Assign client of servers and apply config. clusterMap.parallelStream() .filter(Objects::nonNull) .forEach(e -> applyAllClientConfigChange(app, e, failedClientSet)); // Unbind remaining (unassigned) machines. applyAllRemainingMachineSet(app, remainingSet, failedClientSet); return new ClusterAppAssignResultVO() .setFailedClientSet(failedClientSet) .setFailedServerSet(failedServerSet); }
Example #11
Source File: ClusterAssignServiceImpl.java From Sentinel with Apache License 2.0 | 5 votes |
private void modifyToNonStarted(Set<String> toModifySet, Set<String> failedSet) { toModifySet.parallelStream() .map(MachineUtils::parseCommandIpAndPort) .filter(Optional::isPresent) .map(Optional::get) .map(e -> { CompletableFuture<Void> f = modifyMode(e.r1, e.r2, ClusterStateManager.CLUSTER_NOT_STARTED); return Tuple2.of(e.r1 + '@' + e.r2, f); }) .forEach(f -> handleFutureSync(f, failedSet)); }
Example #12
Source File: SentinelEnvoyRlsServiceImplTest.java From Sentinel with Apache License 2.0 | 5 votes |
@Test public void testShouldRateLimitPass() { SentinelEnvoyRlsServiceImpl rlsService = mock(SentinelEnvoyRlsServiceImpl.class); StreamObserver<RateLimitResponse> streamObserver = mock(StreamObserver.class); String domain = "testShouldRateLimitPass"; int acquireCount = 1; RateLimitDescriptor descriptor1 = RateLimitDescriptor.newBuilder() .addEntries(RateLimitDescriptor.Entry.newBuilder().setKey("a1").setValue("b1").build()) .build(); RateLimitDescriptor descriptor2 = RateLimitDescriptor.newBuilder() .addEntries(RateLimitDescriptor.Entry.newBuilder().setKey("a2").setValue("b2").build()) .addEntries(RateLimitDescriptor.Entry.newBuilder().setKey("a3").setValue("b3").build()) .build(); ArgumentCaptor<RateLimitResponse> responseCapture = ArgumentCaptor.forClass(RateLimitResponse.class); doNothing().when(streamObserver) .onNext(responseCapture.capture()); doCallRealMethod().when(rlsService).shouldRateLimit(any(), any()); when(rlsService.checkToken(eq(domain), same(descriptor1), eq(acquireCount))) .thenReturn(Tuple2.of(new FlowRule(), new TokenResult(TokenResultStatus.OK))); when(rlsService.checkToken(eq(domain), same(descriptor2), eq(acquireCount))) .thenReturn(Tuple2.of(new FlowRule(), new TokenResult(TokenResultStatus.OK))); RateLimitRequest rateLimitRequest = RateLimitRequest.newBuilder() .addDescriptors(descriptor1) .addDescriptors(descriptor2) .setDomain(domain) .setHitsAddend(acquireCount) .build(); rlsService.shouldRateLimit(rateLimitRequest, streamObserver); RateLimitResponse response = responseCapture.getValue(); assertEquals(Code.OK, response.getOverallCode()); response.getStatusesList() .forEach(e -> assertEquals(Code.OK, e.getCode())); }
Example #13
Source File: SentinelEnvoyRlsServiceImpl.java From Sentinel with Apache License 2.0 | 5 votes |
protected Tuple2<FlowRule, TokenResult> checkToken(String domain, RateLimitDescriptor descriptor, int acquireCount) { long ruleId = EnvoySentinelRuleConverter.generateFlowId(generateKey(domain, descriptor)); FlowRule rule = ClusterFlowRuleManager.getFlowRuleById(ruleId); if (rule == null) { // Pass if the target rule is absent. return Tuple2.of(null, new TokenResult(TokenResultStatus.NO_RULE_EXISTS)); } // If the rule is present, it should be valid. return Tuple2.of(rule, SimpleClusterFlowChecker.acquireClusterToken(rule, acquireCount)); }
Example #14
Source File: HttpHeartbeatSender.java From Sentinel-Dashboard-Nacos with Apache License 2.0 | 5 votes |
public HttpHeartbeatSender() { this.client = HttpClients.createDefault(); List<Tuple2<String, Integer>> dashboardList = parseDashboardList(); if (dashboardList == null || dashboardList.isEmpty()) { RecordLog.info("[NettyHttpHeartbeatSender] No dashboard available"); } else { consoleHost = dashboardList.get(0).r1; consolePort = dashboardList.get(0).r2; RecordLog.info("[NettyHttpHeartbeatSender] Dashboard address parsed: <" + consoleHost + ':' + consolePort + ">"); } }
Example #15
Source File: SimpleHttpHeartbeatSender.java From Sentinel with Apache License 2.0 | 5 votes |
private Tuple2<String, Integer> getAvailableAddress() { if (addressList == null || addressList.isEmpty()) { return null; } if (currentAddressIdx < 0) { currentAddressIdx = 0; } int index = currentAddressIdx % addressList.size(); return addressList.get(index); }
Example #16
Source File: SimpleHttpHeartbeatSender.java From Sentinel with Apache License 2.0 | 5 votes |
@Override public boolean sendHeartbeat() throws Exception { if (TransportConfig.getRuntimePort() <= 0) { RecordLog.info("[SimpleHttpHeartbeatSender] Command server port not initialized, won't send heartbeat"); return false; } Tuple2<String, Integer> addrInfo = getAvailableAddress(); if (addrInfo == null) { return false; } InetSocketAddress addr = new InetSocketAddress(addrInfo.r1, addrInfo.r2); SimpleHttpRequest request = new SimpleHttpRequest(addr, TransportConfig.getHeartbeatApiPath()); request.setParams(heartBeat.generateCurrentMessage()); try { SimpleHttpResponse response = httpClient.post(request); if (response.getStatusCode() == OK_STATUS) { return true; } else if (clientErrorCode(response.getStatusCode()) || serverErrorCode(response.getStatusCode())) { RecordLog.warn("[SimpleHttpHeartbeatSender] Failed to send heartbeat to " + addr + ", http status code: " + response.getStatusCode()); } } catch (Exception e) { RecordLog.warn("[SimpleHttpHeartbeatSender] Failed to send heartbeat to " + addr, e); } return false; }
Example #17
Source File: SimpleHttpHeartbeatSender.java From Sentinel with Apache License 2.0 | 5 votes |
public SimpleHttpHeartbeatSender() { // Retrieve the list of default addresses. List<Tuple2<String, Integer>> newAddrs = TransportConfig.getConsoleServerList(); if (newAddrs.isEmpty()) { RecordLog.warn("[SimpleHttpHeartbeatSender] Dashboard server address not configured or not available"); } else { RecordLog.info("[SimpleHttpHeartbeatSender] Default console address list retrieved: " + newAddrs); } this.addressList = newAddrs; }
Example #18
Source File: MachineUtils.java From Sentinel-Dashboard-Nacos with Apache License 2.0 | 5 votes |
public static Optional<Tuple2<String, Integer>> parseCommandIpAndPort(String machineIp) { try { if (StringUtil.isEmpty(machineIp) || !machineIp.contains("@")) { return Optional.empty(); } String[] str = machineIp.split("@"); if (str.length <= 1) { return Optional.empty(); } return Optional.of(Tuple2.of(str[0], Integer.parseInt(str[1]))); } catch (Exception ex) { return Optional.empty(); } }
Example #19
Source File: ClusterAssignServiceImpl.java From Sentinel-Dashboard-Nacos with Apache License 2.0 | 5 votes |
private void handleFutureSync(Tuple2<String, CompletableFuture<Void>> t, Set<String> failedSet) { try { t.r2.get(10, TimeUnit.SECONDS); } catch (Exception ex) { if (ex instanceof ExecutionException) { LOGGER.error("Request for <{}> failed", t.r1, ex.getCause()); } else { LOGGER.error("Request for <{}> failed", t.r1, ex); } failedSet.add(t.r1); } }
Example #20
Source File: ClusterAssignServiceImpl.java From Sentinel-Dashboard-Nacos with Apache License 2.0 | 5 votes |
@Override public ClusterAppAssignResultVO applyAssignToApp(String app, List<ClusterAppAssignMap> clusterMap, Set<String> remainingSet) { AssertUtil.assertNotBlank(app, "app cannot be blank"); AssertUtil.notNull(clusterMap, "clusterMap cannot be null"); Set<String> failedServerSet = new HashSet<>(); Set<String> failedClientSet = new HashSet<>(); // Assign server and apply config. clusterMap.stream() .filter(Objects::nonNull) .filter(ClusterAppAssignMap::getBelongToApp) .map(e -> { String ip = e.getIp(); int commandPort = parsePort(e); CompletableFuture<Void> f = modifyMode(ip, commandPort, ClusterStateManager.CLUSTER_SERVER) .thenCompose(v -> applyServerConfigChange(app, ip, commandPort, e)); return Tuple2.of(e.getMachineId(), f); }) .forEach(t -> handleFutureSync(t, failedServerSet)); // Assign client of servers and apply config. clusterMap.parallelStream() .filter(Objects::nonNull) .forEach(e -> applyAllClientConfigChange(app, e, failedClientSet)); // Unbind remaining (unassigned) machines. applyAllRemainingMachineSet(app, remainingSet, failedClientSet); return new ClusterAppAssignResultVO() .setFailedClientSet(failedClientSet) .setFailedServerSet(failedServerSet); }
Example #21
Source File: ClusterAssignServiceImpl.java From Sentinel-Dashboard-Nacos with Apache License 2.0 | 5 votes |
private void modifyToNonStarted(Set<String> toModifySet, Set<String> failedSet) { toModifySet.parallelStream() .map(MachineUtils::parseCommandIpAndPort) .filter(Optional::isPresent) .map(Optional::get) .map(e -> { CompletableFuture<Void> f = modifyMode(e.r1, e.r2, ClusterStateManager.CLUSTER_NOT_STARTED); return Tuple2.of(e.r1 + '@' + e.r2, f); }) .forEach(f -> handleFutureSync(f, failedSet)); }
Example #22
Source File: HttpHeartbeatSenderTest.java From Sentinel-Dashboard-Nacos with Apache License 2.0 | 5 votes |
@Test public void testAddr() { setAddr(""); assertEquals(0, HttpHeartbeatSender.parseDashboardList().size()); setAddr("a.com"); List<Tuple2<String, Integer>> list = HttpHeartbeatSender.parseDashboardList(); assertEquals(1, list.size()); assertEquals("a.com", list.get(0).r1); assertEquals(Integer.valueOf(80), list.get(0).r2); setAddr("a.com:88"); list = HttpHeartbeatSender.parseDashboardList(); assertEquals(1, list.size()); assertEquals("a.com", list.get(0).r1); assertEquals(Integer.valueOf(88), list.get(0).r2); setAddr("a.com:88,,,,"); list = HttpHeartbeatSender.parseDashboardList(); assertEquals(1, list.size()); assertEquals("a.com", list.get(0).r1); assertEquals(Integer.valueOf(88), list.get(0).r2); setAddr("a.com:88,b.com"); list = HttpHeartbeatSender.parseDashboardList(); assertEquals(2, list.size()); assertEquals("a.com", list.get(0).r1); assertEquals(Integer.valueOf(88), list.get(0).r2); assertEquals("b.com", list.get(1).r1); assertEquals(Integer.valueOf(80), list.get(1).r2); setAddr("a.com:88,b.com:99999"); list = HttpHeartbeatSender.parseDashboardList(); assertEquals(2, list.size()); assertEquals("a.com", list.get(0).r1); assertEquals(Integer.valueOf(88), list.get(0).r2); assertEquals("b.com", list.get(1).r1); assertEquals(Integer.valueOf(99999), list.get(1).r2); }
Example #23
Source File: HttpHeartbeatSender.java From Sentinel-Dashboard-Nacos with Apache License 2.0 | 5 votes |
protected static List<Tuple2<String, Integer>> parseDashboardList() { List<Tuple2<String, Integer>> list = new ArrayList<Tuple2<String, Integer>>(); try { String ipsStr = TransportConfig.getConsoleServer(); if (StringUtil.isBlank(ipsStr)) { RecordLog.warn("[NettyHttpHeartbeatSender] Dashboard server address is not configured"); return list; } for (String ipPortStr : ipsStr.split(",")) { if (ipPortStr.trim().length() == 0) { continue; } ipPortStr = ipPortStr.trim(); if (ipPortStr.startsWith("http://")) { ipPortStr = ipPortStr.substring(7); } if (ipPortStr.startsWith(":")) { continue; } String[] ipPort = ipPortStr.trim().split(":"); int port = 80; if (ipPort.length > 1) { port = Integer.parseInt(ipPort[1].trim()); } list.add(Tuple2.of(ipPort[0].trim(), port)); } } catch (Exception ex) { RecordLog.warn("[NettyHttpHeartbeatSender] Parse dashboard list failed, current address list: " + list, ex); ex.printStackTrace(); } return list; }
Example #24
Source File: SentinelEnvoyRlsServiceImpl.java From Sentinel with Apache License 2.0 | 4 votes |
@Override public void shouldRateLimit(RateLimitRequest request, StreamObserver<RateLimitResponse> responseObserver) { int acquireCount = request.getHitsAddend(); if (acquireCount < 0) { responseObserver.onError(new IllegalArgumentException( "acquireCount should be positive, but actual: " + acquireCount)); return; } if (acquireCount == 0) { // Not present, use the default "1" by default. acquireCount = 1; } String domain = request.getDomain(); boolean blocked = false; List<DescriptorStatus> statusList = new ArrayList<>(request.getDescriptorsCount()); for (RateLimitDescriptor descriptor : request.getDescriptorsList()) { Tuple2<FlowRule, TokenResult> t = checkToken(domain, descriptor, acquireCount); TokenResult r = t.r2; printAccessLogIfNecessary(domain, descriptor, r); if (r.getStatus() == TokenResultStatus.NO_RULE_EXISTS) { // If the rule of the descriptor is absent, the request will pass directly. r.setStatus(TokenResultStatus.OK); } if (!blocked && r.getStatus() != TokenResultStatus.OK) { blocked = true; } Code statusCode = r.getStatus() == TokenResultStatus.OK ? Code.OK : Code.OVER_LIMIT; DescriptorStatus.Builder descriptorStatusBuilder = DescriptorStatus.newBuilder() .setCode(statusCode); if (t.r1 != null) { descriptorStatusBuilder .setCurrentLimit(RateLimit.newBuilder().setUnit(Unit.SECOND) .setRequestsPerUnit((int)t.r1.getCount()) .build()) .setLimitRemaining(r.getRemaining()); } statusList.add(descriptorStatusBuilder.build()); } Code overallStatus = blocked ? Code.OVER_LIMIT : Code.OK; RateLimitResponse response = RateLimitResponse.newBuilder() .setOverallCode(overallStatus) .addAllStatuses(statusList) .build(); responseObserver.onNext(response); responseObserver.onCompleted(); }
Example #25
Source File: SentinelEnvoyRlsServiceImplTest.java From Sentinel with Apache License 2.0 | 4 votes |
@Test public void testShouldRatePartialBlock() { SentinelEnvoyRlsServiceImpl rlsService = mock(SentinelEnvoyRlsServiceImpl.class); StreamObserver<RateLimitResponse> streamObserver = mock(StreamObserver.class); String domain = "testShouldRatePartialBlock"; int acquireCount = 1; RateLimitDescriptor descriptor1 = RateLimitDescriptor.newBuilder() .addEntries(RateLimitDescriptor.Entry.newBuilder().setKey("a1").setValue("b1").build()) .build(); RateLimitDescriptor descriptor2 = RateLimitDescriptor.newBuilder() .addEntries(RateLimitDescriptor.Entry.newBuilder().setKey("a2").setValue("b2").build()) .addEntries(RateLimitDescriptor.Entry.newBuilder().setKey("a3").setValue("b3").build()) .build(); ArgumentCaptor<RateLimitResponse> responseCapture = ArgumentCaptor.forClass(RateLimitResponse.class); doNothing().when(streamObserver) .onNext(responseCapture.capture()); doCallRealMethod().when(rlsService).shouldRateLimit(any(), any()); when(rlsService.checkToken(eq(domain), same(descriptor1), eq(acquireCount))) .thenReturn(Tuple2.of(new FlowRule(), new TokenResult(TokenResultStatus.BLOCKED))); when(rlsService.checkToken(eq(domain), same(descriptor2), eq(acquireCount))) .thenReturn(Tuple2.of(new FlowRule(), new TokenResult(TokenResultStatus.OK))); RateLimitRequest rateLimitRequest = RateLimitRequest.newBuilder() .addDescriptors(descriptor1) .addDescriptors(descriptor2) .setDomain(domain) .setHitsAddend(acquireCount) .build(); rlsService.shouldRateLimit(rateLimitRequest, streamObserver); RateLimitResponse response = responseCapture.getValue(); assertEquals(Code.OVER_LIMIT, response.getOverallCode()); assertEquals(2, response.getStatusesCount()); assertTrue(response.getStatusesList().stream() .anyMatch(e -> e.getCode().equals(Code.OVER_LIMIT))); assertFalse(response.getStatusesList().stream() .allMatch(e -> e.getCode().equals(Code.OVER_LIMIT))); }
Example #26
Source File: TransportConfigTest.java From Sentinel with Apache License 2.0 | 4 votes |
@Test public void testGetConsoleServerList() { // empty SentinelConfig.setConfig(TransportConfig.CONSOLE_SERVER, ""); List<Tuple2<String, Integer>> list = TransportConfig.getConsoleServerList(); assertNotNull(list); assertEquals(0, list.size()); // single ip SentinelConfig.setConfig(TransportConfig.CONSOLE_SERVER, "112.13.223.3"); list = TransportConfig.getConsoleServerList(); assertNotNull(list); assertEquals(1, list.size()); assertEquals("112.13.223.3", list.get(0).r1); assertEquals(new Integer(80), list.get(0).r2); // single domain SentinelConfig.setConfig(TransportConfig.CONSOLE_SERVER, "www.dashboard.org"); list = TransportConfig.getConsoleServerList(); assertNotNull(list); assertEquals(1, list.size()); assertEquals("www.dashboard.org", list.get(0).r1); assertEquals(new Integer(80), list.get(0).r2); // single ip including port SentinelConfig.setConfig(TransportConfig.CONSOLE_SERVER, "www.dashboard.org:81"); list = TransportConfig.getConsoleServerList(); assertNotNull(list); assertEquals(1, list.size()); assertEquals("www.dashboard.org", list.get(0).r1); assertEquals(new Integer(81), list.get(0).r2); // mixed SentinelConfig.setConfig(TransportConfig.CONSOLE_SERVER, "www.dashboard.org:81,112.13.223.3,112.13.223.4:8080,www.dashboard.org"); list = TransportConfig.getConsoleServerList(); assertNotNull(list); assertEquals(4, list.size()); assertEquals("www.dashboard.org", list.get(0).r1); assertEquals(new Integer(81), list.get(0).r2); assertEquals("112.13.223.3", list.get(1).r1); assertEquals(new Integer(80), list.get(1).r2); assertEquals("112.13.223.4", list.get(2).r1); assertEquals(new Integer(8080), list.get(2).r2); assertEquals("www.dashboard.org", list.get(3).r1); assertEquals(new Integer(80), list.get(3).r2); // malformed SentinelConfig.setConfig(TransportConfig.CONSOLE_SERVER, "www.dashboard.org:0"); list = TransportConfig.getConsoleServerList(); assertNotNull(list); assertEquals(0, list.size()); SentinelConfig.setConfig(TransportConfig.CONSOLE_SERVER, "www.dashboard.org:-1"); list = TransportConfig.getConsoleServerList(); assertNotNull(list); assertEquals(0, list.size()); SentinelConfig.setConfig(TransportConfig.CONSOLE_SERVER, ":80"); list = TransportConfig.getConsoleServerList(); assertNotNull(list); assertEquals(0, list.size()); SentinelConfig.setConfig(TransportConfig.CONSOLE_SERVER, "www.dashboard.org:"); list = TransportConfig.getConsoleServerList(); assertNotNull(list); assertEquals(0, list.size()); SentinelConfig.setConfig(TransportConfig.CONSOLE_SERVER, "www.dashboard.org:80000"); list = TransportConfig.getConsoleServerList(); assertNotNull(list); assertEquals(0, list.size()); SentinelConfig.setConfig(TransportConfig.CONSOLE_SERVER, "www.dashboard.org:80000,www.dashboard.org:81,:80"); list = TransportConfig.getConsoleServerList(); assertNotNull(list); assertEquals(1, list.size()); assertEquals("www.dashboard.org", list.get(0).r1); assertEquals(new Integer(81), list.get(0).r2); }
Example #27
Source File: TransportConfig.java From Sentinel with Apache License 2.0 | 4 votes |
/** * Get a list of (ip/domain, port) indicating Sentinel Dashboard's address.<br> * NOTE: only support <b>HTTP</b> protocol * * @return list of (ip/domain, port) pair. <br> * <b>May not be null</b>. <br> * An empty list returned when not configured. */ public static List<Tuple2<String, Integer>> getConsoleServerList() { String config = SentinelConfig.getConfig(CONSOLE_SERVER); List<Tuple2<String, Integer>> list = new ArrayList<Tuple2<String, Integer>>(); if (StringUtil.isBlank(config)) { return list; } int pos = -1; int cur = 0; while (true) { pos = config.indexOf(',', cur); if (cur < config.length() - 1 && pos < 0) { // for single segment, pos move to the end pos = config.length(); } if (pos < 0) { break; } if (pos <= cur) { cur ++; continue; } // parsing String ipPortStr = config.substring(cur, pos); cur = pos + 1; if (StringUtil.isBlank(ipPortStr)) { continue; } ipPortStr = ipPortStr.trim(); if (ipPortStr.startsWith("http://")) { ipPortStr = ipPortStr.substring(7); } int index = ipPortStr.indexOf(":"); int port = 80; if (index == 0) { // skip continue; } String host = ipPortStr; if (index >= 0) { try { port = Integer.parseInt(ipPortStr.substring(index + 1)); if (port <= 1 || port >= 65535) { throw new RuntimeException("Port number [" + port + "] over range"); } } catch (Exception e) { RecordLog.warn("Parse port of dashboard server failed: " + ipPortStr, e); // skip continue; } host = ipPortStr.substring(0, index); } list.add(Tuple2.of(host, port)); } return list; }
Example #28
Source File: SentinelHealthIndicator.java From spring-cloud-alibaba with Apache License 2.0 | 4 votes |
@Override protected void doHealthCheck(Health.Builder builder) throws Exception { Map<String, Object> detailMap = new HashMap<>(); // If sentinel isn't enabled, set the status up and set the enabled to false in // detail if (!sentinelProperties.isEnabled()) { detailMap.put("enabled", false); builder.up().withDetails(detailMap); return; } detailMap.put("enabled", true); // Check health of Dashboard boolean dashboardUp = true; List<Tuple2<String, Integer>> consoleServerList = TransportConfig .getConsoleServerList(); if (CollectionUtils.isEmpty(consoleServerList)) { // If Dashboard isn't configured, it's OK and mark the status of Dashboard // with UNKNOWN. detailMap.put("dashboard", new Status(Status.UNKNOWN.getCode(), "dashboard isn't configured")); } else { // If Dashboard is configured, send a heartbeat message to it and check the // result HeartbeatSender heartbeatSender = HeartbeatSenderProvider .getHeartbeatSender(); boolean result = heartbeatSender.sendHeartbeat(); if (result) { detailMap.put("dashboard", Status.UP); } else { // If failed to send heartbeat message, means that the Dashboard is DOWN dashboardUp = false; detailMap.put("dashboard", new Status(Status.DOWN.getCode(), String.format( "the dashboard servers [%s] one of them can't be connected", consoleServerList))); } } // Check health of DataSource boolean dataSourceUp = true; Map<String, Object> dataSourceDetailMap = new HashMap<>(); detailMap.put("dataSource", dataSourceDetailMap); // Get all DataSources and each call loadConfig to check if it's OK // If no Exception thrown, it's OK // Note: // Even if the dynamic config center is down, the loadConfig() might return // successfully // e.g. for Nacos client, it might retrieve from the local cache) // But in most circumstances it's okay Map<String, AbstractDataSource> dataSourceMap = beanFactory .getBeansOfType(AbstractDataSource.class); for (Map.Entry<String, AbstractDataSource> dataSourceMapEntry : dataSourceMap .entrySet()) { String dataSourceBeanName = dataSourceMapEntry.getKey(); AbstractDataSource dataSource = dataSourceMapEntry.getValue(); try { dataSource.loadConfig(); dataSourceDetailMap.put(dataSourceBeanName, Status.UP); } catch (Exception e) { // If one DataSource failed to loadConfig, means that the DataSource is // DOWN dataSourceUp = false; dataSourceDetailMap.put(dataSourceBeanName, new Status(Status.DOWN.getCode(), e.getMessage())); } } // If Dashboard and DataSource are both OK, the health status is UP if (dashboardUp && dataSourceUp) { builder.up().withDetails(detailMap); } else { builder.down().withDetails(detailMap); } }