com.alibaba.csp.sentinel.node.Node Java Examples
The following examples show how to use
com.alibaba.csp.sentinel.node.Node.
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: CommonFilterContextTest.java From Sentinel with Apache License 2.0 | 6 votes |
@Test public void testCommonFilterMiscellaneous() throws Exception { String url = "/hello"; this.mvc.perform(get(url)) .andExpect(status().isOk()) .andExpect(content().string(HELLO_STR)); ClusterNode cn = ClusterBuilderSlot.getClusterNode(url); assertNotNull(cn); assertEquals(1, cn.passQps(), 0.01); String context = ""; for (Node n : Constants.ROOT.getChildList()) { if (n instanceof EntranceNode) { String id = ((EntranceNode) n).getId().getName(); if (url.equals(id)) { context = ((EntranceNode) n).getId().getName(); } } } assertEquals(url, context); }
Example #2
Source File: FetchTreeCommandHandler.java From Sentinel-Dashboard-Nacos with Apache License 2.0 | 6 votes |
private void visitTree(int level, DefaultNode node, /*@NonNull*/ StringBuilder sb) { for (int i = 0; i < level; ++i) { sb.append("-"); } if (!(node instanceof EntranceNode)) { sb.append(String.format("%s(t:%s pq:%s bq:%s tq:%s rt:%s prq:%s 1mp:%s 1mb:%s 1mt:%s)", node.getId().getShowName(), node.curThreadNum(), node.passQps(), node.blockQps(), node.totalQps(), node.avgRt(), node.successQps(), node.totalRequest() - node.blockRequest(), node.blockRequest(), node.totalRequest())).append("\n"); } else { sb.append(String.format("EntranceNode: %s(t:%s pq:%s bq:%s tq:%s rt:%s prq:%s 1mp:%s 1mb:%s 1mt:%s)", node.getId().getShowName(), node.curThreadNum(), node.passQps(), node.blockQps(), node.totalQps(), node.avgRt(), node.successQps(), node.totalRequest() - node.blockRequest(), node.blockRequest(), node.totalRequest())).append("\n"); } for (Node n : node.getChildList()) { DefaultNode dn = (DefaultNode)n; visitTree(level + 1, dn, sb); } }
Example #3
Source File: FetchTreeCommandHandler.java From Sentinel with Apache License 2.0 | 6 votes |
private void visitTree(int level, DefaultNode node, /*@NonNull*/ StringBuilder sb) { for (int i = 0; i < level; ++i) { sb.append("-"); } if (!(node instanceof EntranceNode)) { sb.append(String.format("%s(t:%s pq:%s bq:%s tq:%s rt:%s prq:%s 1mp:%s 1mb:%s 1mt:%s)", node.getId().getShowName(), node.curThreadNum(), node.passQps(), node.blockQps(), node.totalQps(), node.avgRt(), node.successQps(), node.totalRequest() - node.blockRequest(), node.blockRequest(), node.totalRequest())).append("\n"); } else { sb.append(String.format("EntranceNode: %s(t:%s pq:%s bq:%s tq:%s rt:%s prq:%s 1mp:%s 1mb:%s 1mt:%s)", node.getId().getShowName(), node.curThreadNum(), node.passQps(), node.blockQps(), node.totalQps(), node.avgRt(), node.successQps(), node.totalRequest() - node.blockRequest(), node.blockRequest(), node.totalRequest())).append("\n"); } for (Node n : node.getChildList()) { DefaultNode dn = (DefaultNode)n; visitTree(level + 1, dn, sb); } }
Example #4
Source File: WarmUpController.java From Sentinel-Dashboard-Nacos with Apache License 2.0 | 6 votes |
@Override public boolean canPass(Node node, int acquireCount, boolean prioritized) { long passQps = (long) node.passQps(); long previousQps = (long) node.previousPassQps(); syncToken(previousQps); // 开始计算它的斜率 // 如果进入了警戒线,开始调整他的qps long restToken = storedTokens.get(); if (restToken >= warningToken) { long aboveToken = restToken - warningToken; // 消耗的速度要比warning快,但是要比慢 // current interval = restToken*slope+1/count double warningQps = Math.nextUp(1.0 / (aboveToken * slope + 1.0 / count)); if (passQps + acquireCount <= warningQps) { return true; } } else { if (passQps + acquireCount <= count) { return true; } } return false; }
Example #5
Source File: SentinelJaxRsQuarkusAdapterTest.java From Sentinel with Apache License 2.0 | 6 votes |
@Test public void testGetHello() { String url = "/test/hello"; String resourceName = "GET:" + url; Response response = given().get(url); response.then().statusCode(200).body(equalTo(HELLO_STR)); ClusterNode cn = ClusterBuilderSlot.getClusterNode(resourceName); assertNotNull(cn); assertEquals(1, cn.passQps(), 0.01); String context = ""; for (Node n : Constants.ROOT.getChildList()) { if (n instanceof EntranceNode) { String id = ((EntranceNode) n).getId().getName(); if (url.equals(id)) { context = ((EntranceNode) n).getId().getName(); } } } assertEquals("", context); }
Example #6
Source File: SentinelJaxRsQuarkusAdapterTest.java From Sentinel with Apache License 2.0 | 6 votes |
@Test public void testAsyncGetHello() { String url = "/test/async-hello"; String resourceName = "GET:" + url; Response response = given().get(url); response.then().statusCode(200).body(equalTo(HELLO_STR)); ClusterNode cn = ClusterBuilderSlot.getClusterNode(resourceName); assertNotNull(cn); assertEquals(1, cn.passQps(), 0.01); String context = ""; for (Node n : Constants.ROOT.getChildList()) { if (n instanceof EntranceNode) { String id = ((EntranceNode) n).getId().getName(); if (url.equals(id)) { context = ((EntranceNode) n).getId().getName(); } } } assertEquals("", context); }
Example #7
Source File: ProviderFilterTest.java From Sentinel with Apache License 2.0 | 6 votes |
@Test public void testGetHello() { String url = "/test/hello"; String resourceName = "GET:" + url; Response response = given().get(url); response.then().statusCode(200).body(equalTo(HELLO_STR)); ClusterNode cn = ClusterBuilderSlot.getClusterNode(resourceName); assertNotNull(cn); assertEquals(1, cn.passQps(), 0.01); String context = ""; for (Node n : Constants.ROOT.getChildList()) { if (n instanceof EntranceNode) { String id = ((EntranceNode) n).getId().getName(); if (url.equals(id)) { context = ((EntranceNode) n).getId().getName(); } } } assertEquals("", context); }
Example #8
Source File: ProviderFilterTest.java From Sentinel with Apache License 2.0 | 6 votes |
@Test public void testAsyncGetHello() { String url = "/test/async-hello"; String resourceName = "GET:" + url; Response response = given().get(url); response.then().statusCode(200).body(equalTo(HELLO_STR)); ClusterNode cn = ClusterBuilderSlot.getClusterNode(resourceName); assertNotNull(cn); assertEquals(1, cn.passQps(), 0.01); String context = ""; for (Node n : Constants.ROOT.getChildList()) { if (n instanceof EntranceNode) { String id = ((EntranceNode) n).getId().getName(); if (url.equals(id)) { context = ((EntranceNode) n).getId().getName(); } } } assertEquals("", context); }
Example #9
Source File: DefaultController.java From Sentinel with Apache License 2.0 | 6 votes |
@Override public boolean canPass(Node node, int acquireCount, boolean prioritized) { int curCount = avgUsedTokens(node); if (curCount + acquireCount > count) { if (prioritized && grade == RuleConstant.FLOW_GRADE_QPS) { long currentTime; long waitInMs; currentTime = TimeUtil.currentTimeMillis(); waitInMs = node.tryOccupyNext(currentTime, acquireCount, count); if (waitInMs < OccupyTimeoutProperty.getOccupyTimeout()) { node.addWaitingRequest(currentTime + waitInMs, acquireCount); node.addOccupiedPass(acquireCount); sleep(waitInMs); // PriorityWaitException indicates that the request will pass after waiting for {@link @waitInMs}. throw new PriorityWaitException(waitInMs); } } return false; } return true; }
Example #10
Source File: WarmUpController.java From Sentinel with Apache License 2.0 | 6 votes |
@Override public boolean canPass(Node node, int acquireCount, boolean prioritized) { long passQps = (long) node.passQps(); long previousQps = (long) node.previousPassQps(); syncToken(previousQps); // 开始计算它的斜率 // 如果进入了警戒线,开始调整他的qps long restToken = storedTokens.get(); if (restToken >= warningToken) { long aboveToken = restToken - warningToken; // 消耗的速度要比warning快,但是要比慢 // current interval = restToken*slope+1/count double warningQps = Math.nextUp(1.0 / (aboveToken * slope + 1.0 / count)); if (passQps + acquireCount <= warningQps) { return true; } } else { if (passQps + acquireCount <= count) { return true; } } return false; }
Example #11
Source File: FlowRuleChecker.java From Sentinel with Apache License 2.0 | 6 votes |
static Node selectReferenceNode(FlowRule rule, Context context, DefaultNode node) { String refResource = rule.getRefResource(); int strategy = rule.getStrategy(); if (StringUtil.isEmpty(refResource)) { return null; } if (strategy == RuleConstant.STRATEGY_RELATE) { return ClusterBuilderSlot.getClusterNode(refResource); } if (strategy == RuleConstant.STRATEGY_CHAIN) { if (!refResource.equals(context.getName())) { return null; } return node; } // No node. return null; }
Example #12
Source File: AsyncEntryIntegrationTest.java From Sentinel-Dashboard-Nacos with Apache License 2.0 | 5 votes |
private DefaultNode shouldHasChildFor(DefaultNode root, String resourceName, boolean check) { if (root == null) { if (check) { fail("Root node should not be empty"); } else { return null; } } Set<Node> nodeSet = root.getChildList(); if (nodeSet == null || nodeSet.isEmpty()) { if (check) { fail("Child nodes should not be empty: " + root.getId().getName()); } else { return null; } } for (Node node : nodeSet) { if (node instanceof DefaultNode) { DefaultNode dn = (DefaultNode) node; if (dn.getId().getName().equals(resourceName)) { return dn; } } } if (check) { fail(String.format("The given node <%s> does not have child for resource <%s>", root.getId().getName(), resourceName)); } return null; }
Example #13
Source File: ClusterNodeBuilderTest.java From Sentinel-Dashboard-Nacos with Apache License 2.0 | 5 votes |
@Test public void clusterNodeBuilder_normal() throws Exception { ContextUtil.enter("entry1", "caller1"); Entry nodeA = SphU.entry("nodeA"); Node curNode = nodeA.getCurNode(); assertSame(curNode.getClass(), DefaultNode.class); DefaultNode dN = (DefaultNode)curNode; assertTrue(dN.getClusterNode().getOriginCountMap().containsKey("caller1")); assertSame(nodeA.getOriginNode(), dN.getClusterNode().getOrCreateOriginNode("caller1")); if (nodeA != null) { nodeA.exit(); } ContextUtil.exit(); ContextUtil.enter("entry4", "caller2"); nodeA = SphU.entry("nodeA"); curNode = nodeA.getCurNode(); assertSame(curNode.getClass(), DefaultNode.class); DefaultNode dN1 = (DefaultNode)curNode; assertTrue(dN1.getClusterNode().getOriginCountMap().containsKey("caller2")); assertNotSame(dN1, dN); if (nodeA != null) { nodeA.exit(); } ContextUtil.exit(); }
Example #14
Source File: StatisticSlot.java From Sentinel with Apache License 2.0 | 5 votes |
private void recordCompleteFor(Node node, int batchCount, long rt, Throwable error) { if (node == null) { return; } node.addRtAndSuccess(rt, batchCount); node.decreaseThreadNum(); if (error != null && !(error instanceof BlockException)) { node.increaseExceptionQps(batchCount); } }
Example #15
Source File: EntryTest.java From Sentinel-Dashboard-Nacos with Apache License 2.0 | 5 votes |
@Test public void testEntryFieldsGetSet() { ResourceWrapper resourceWrapper = new StringResourceWrapper("resA", EntryType.IN); Entry entry = new TestEntry(resourceWrapper); assertSame(resourceWrapper, entry.getResourceWrapper()); Throwable error = new IllegalStateException(); entry.setError(error); assertSame(error, entry.getError()); Node curNode = mock(Node.class); entry.setCurNode(curNode); assertSame(curNode, entry.getCurNode()); Node originNode = mock(Node.class); entry.setOriginNode(originNode); assertSame(originNode, entry.getOriginNode()); }
Example #16
Source File: RateLimiterControllerTest.java From Sentinel-Dashboard-Nacos with Apache License 2.0 | 5 votes |
@Test public void testPaceController_normal() throws InterruptedException { RateLimiterController paceController = new RateLimiterController(500, 10d); Node node = mock(Node.class); long start = TimeUtil.currentTimeMillis(); for (int i = 0; i < 6; i++) { assertTrue(paceController.canPass(node, 1)); } long end = TimeUtil.currentTimeMillis(); assertTrue((end - start) > 400); }
Example #17
Source File: RateLimiterControllerTest.java From Sentinel-Dashboard-Nacos with Apache License 2.0 | 5 votes |
@Test public void testPaceController_timeout() throws InterruptedException { final RateLimiterController paceController = new RateLimiterController(500, 10d); final Node node = mock(Node.class); final AtomicInteger passcount = new AtomicInteger(); final AtomicInteger blockcount = new AtomicInteger(); final CountDownLatch countDown = new CountDownLatch(1); final AtomicInteger done = new AtomicInteger(); for (int i = 0; i < 10; i++) { Thread thread = new Thread(new Runnable() { @Override public void run() { boolean pass = paceController.canPass(node, 1); if (pass == true) { passcount.incrementAndGet(); } else { blockcount.incrementAndGet(); } done.incrementAndGet(); if (done.get() >= 10) { countDown.countDown(); } } }, "Thread " + i); thread.start(); } countDown.await(); System.out.println("pass:" + passcount.get()); System.out.println("block" + blockcount.get()); System.out.println("done" + done.get()); assertTrue(blockcount.get() > 0); }
Example #18
Source File: DefaultControllerTest.java From Sentinel-Dashboard-Nacos with Apache License 2.0 | 5 votes |
@Test public void testCanPassForQps() { double threshold = 10; TrafficShapingController controller = new DefaultController(threshold, RuleConstant.FLOW_GRADE_QPS); Node node = mock(Node.class); when(node.passQps()).thenReturn(threshold - 1) .thenReturn(threshold); assertTrue(controller.canPass(node, 1)); assertFalse(controller.canPass(node, 1)); }
Example #19
Source File: RateLimiterControllerTest.java From Sentinel-Dashboard-Nacos with Apache License 2.0 | 5 votes |
@Test public void testPaceController_zeroattack() throws InterruptedException { RateLimiterController paceController = new RateLimiterController(500, 0d); Node node = mock(Node.class); for (int i = 0; i < 2; i++) { assertFalse(paceController.canPass(node, 1)); assertTrue(paceController.canPass(node, 0)); } }
Example #20
Source File: DefaultControllerTest.java From Sentinel-Dashboard-Nacos with Apache License 2.0 | 5 votes |
@Test public void testCanPassForThreadCount() { int threshold = 8; TrafficShapingController controller = new DefaultController(threshold, RuleConstant.FLOW_GRADE_THREAD); Node node = mock(Node.class); when(node.curThreadNum()).thenReturn(threshold - 1) .thenReturn(threshold); assertTrue(controller.canPass(node, 1)); assertFalse(controller.canPass(node, 1)); }
Example #21
Source File: RateLimiterControllerTest.java From Sentinel with Apache License 2.0 | 5 votes |
@Test public void testPaceController_timeout() throws InterruptedException { final RateLimiterController paceController = new RateLimiterController(500, 10d); final Node node = mock(Node.class); final AtomicInteger passcount = new AtomicInteger(); final AtomicInteger blockcount = new AtomicInteger(); final CountDownLatch countDown = new CountDownLatch(1); final AtomicInteger done = new AtomicInteger(); for (int i = 0; i < 10; i++) { Thread thread = new Thread(new Runnable() { @Override public void run() { boolean pass = paceController.canPass(node, 1); if (pass == true) { passcount.incrementAndGet(); } else { blockcount.incrementAndGet(); } done.incrementAndGet(); if (done.get() >= 10) { countDown.countDown(); } } }, "Thread " + i); thread.start(); } countDown.await(); System.out.println("pass:" + passcount.get()); System.out.println("block" + blockcount.get()); System.out.println("done" + done.get()); assertTrue(blockcount.get() > 0); }
Example #22
Source File: WarmUpControllerTest.java From Sentinel-Dashboard-Nacos with Apache License 2.0 | 5 votes |
@Test public void testWarmUp() throws InterruptedException { WarmUpController warmupController = new WarmUpController(10, 10, 3); setCurrentMillis(System.currentTimeMillis()); Node node = mock(Node.class); when(node.passQps()).thenReturn(8d); when(node.previousPassQps()).thenReturn(1d); assertFalse(warmupController.canPass(node, 1)); when(node.passQps()).thenReturn(1d); when(node.previousPassQps()).thenReturn(1d); assertTrue(warmupController.canPass(node, 1)); when(node.previousPassQps()).thenReturn(10d); for (int i = 0; i < 100; i++) { sleep(100); warmupController.canPass(node, 1); } when(node.passQps()).thenReturn(8d); assertTrue(warmupController.canPass(node, 1)); when(node.passQps()).thenReturn(10d); assertFalse(warmupController.canPass(node, 1)); }
Example #23
Source File: CtEntryTest.java From Sentinel-Dashboard-Nacos with Apache License 2.0 | 5 votes |
@Test public void testGetLastNode() { Context context = new NullContext(); CtEntry entry = new CtEntry(new StringResourceWrapper("testGetLastNode", EntryType.IN), null, context); assertNull(entry.parent); assertNull(entry.getLastNode()); Entry parentEntry = mock(Entry.class); Node node = mock(Node.class); when(parentEntry.getCurNode()).thenReturn(node); entry.parent = parentEntry; assertSame(node, entry.getLastNode()); }
Example #24
Source File: FetchJsonTreeCommandHandler.java From Sentinel with Apache License 2.0 | 5 votes |
/** * Preorder traversal. */ private void visit(DefaultNode node, List<NodeVo> results, String parentId) { NodeVo vo = NodeVo.fromDefaultNode(node, parentId); results.add(vo); String id = vo.getId(); for (Node n : node.getChildList()) { visit((DefaultNode)n, results, id); } }
Example #25
Source File: FlowRuleChecker.java From Sentinel with Apache License 2.0 | 5 votes |
static Node selectNodeByRequesterAndStrategy(/*@NonNull*/ FlowRule rule, Context context, DefaultNode node) { // The limit app should not be empty. String limitApp = rule.getLimitApp(); int strategy = rule.getStrategy(); String origin = context.getOrigin(); if (limitApp.equals(origin) && filterOrigin(origin)) { if (strategy == RuleConstant.STRATEGY_DIRECT) { // Matches limit origin, return origin statistic node. return context.getOriginNode(); } return selectReferenceNode(rule, context, node); } else if (RuleConstant.LIMIT_APP_DEFAULT.equals(limitApp)) { if (strategy == RuleConstant.STRATEGY_DIRECT) { // Return the cluster node. return node.getClusterNode(); } return selectReferenceNode(rule, context, node); } else if (RuleConstant.LIMIT_APP_OTHER.equals(limitApp) && FlowRuleManager.isOtherOrigin(origin, rule.getResource())) { if (strategy == RuleConstant.STRATEGY_DIRECT) { return context.getOriginNode(); } return selectReferenceNode(rule, context, node); } return null; }
Example #26
Source File: SentinelDubboConsumerFilterTest.java From Sentinel with Apache License 2.0 | 5 votes |
private DefaultNode getNode(String resourceName, DefaultNode root) { Queue<DefaultNode> queue = new LinkedList<>(); queue.offer(root); while (!queue.isEmpty()) { DefaultNode temp = queue.poll(); if (temp.getId().getName().equals(resourceName)) { return temp; } for (Node node : temp.getChildList()) { queue.offer((DefaultNode) node); } } return null; }
Example #27
Source File: RateLimiterControllerTest.java From Sentinel with Apache License 2.0 | 5 votes |
@Test public void testPaceController_zeroattack() throws InterruptedException { RateLimiterController paceController = new RateLimiterController(500, 0d); Node node = mock(Node.class); for (int i = 0; i < 2; i++) { assertFalse(paceController.canPass(node, 1)); assertTrue(paceController.canPass(node, 0)); } }
Example #28
Source File: ClientFilterTest.java From Sentinel with Apache License 2.0 | 5 votes |
@Test public void testClientGetHello() { final String url = "/test/hello"; String resourceName = "GET:" + url; Response response = SentinelJaxRsClientTemplate.execute(resourceName, new Supplier<Response>() { @Override public Response get() { return client.target(host).path(url).request() .get(); } }); assertEquals(200, response.getStatus()); assertEquals(HELLO_STR, response.readEntity(String.class)); ClusterNode cn = ClusterBuilderSlot.getClusterNode(resourceName); assertNotNull(cn); assertEquals(1, cn.passQps(), 0.01); String context = ""; for (Node n : Constants.ROOT.getChildList()) { if (n instanceof EntranceNode) { String id = ((EntranceNode) n).getId().getName(); if (url.equals(id)) { context = ((EntranceNode) n).getId().getName(); } } } assertEquals("", context); }
Example #29
Source File: AsyncEntryIntegrationTest.java From Sentinel with Apache License 2.0 | 5 votes |
private DefaultNode shouldHasChildFor(DefaultNode root, String resourceName, boolean check) { if (root == null) { if (check) { fail("Root node should not be empty"); } else { return null; } } Set<Node> nodeSet = root.getChildList(); if (nodeSet == null || nodeSet.isEmpty()) { if (check) { fail("Child nodes should not be empty: " + root.getId().getName()); } else { return null; } } for (Node node : nodeSet) { if (node instanceof DefaultNode) { DefaultNode dn = (DefaultNode) node; if (dn.getId().getName().equals(resourceName)) { return dn; } } } if (check) { fail(String.format("The given node <%s> does not have child for resource <%s>", root.getId().getName(), resourceName)); } return null; }
Example #30
Source File: FlowRuleChecker.java From Sentinel with Apache License 2.0 | 5 votes |
private static boolean passLocalCheck(FlowRule rule, Context context, DefaultNode node, int acquireCount, boolean prioritized) { Node selectedNode = selectNodeByRequesterAndStrategy(rule, context, node); if (selectedNode == null) { return true; } return rule.getRater().canPass(selectedNode, acquireCount, prioritized); }