Search in sources :

Example 1 with Node

use of com.alibaba.csp.sentinel.node.Node in project Sentinel by alibaba.

the class ClientFilterTest method testClientGetHello.

@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);
}
Also used : Response(javax.ws.rs.core.Response) ClusterNode(com.alibaba.csp.sentinel.node.ClusterNode) EntranceNode(com.alibaba.csp.sentinel.node.EntranceNode) ClusterNode(com.alibaba.csp.sentinel.node.ClusterNode) Node(com.alibaba.csp.sentinel.node.Node) EntranceNode(com.alibaba.csp.sentinel.node.EntranceNode) Test(org.junit.Test)

Example 2 with Node

use of com.alibaba.csp.sentinel.node.Node in project Sentinel by alibaba.

the class StatisticSlot method exit.

@Override
public void exit(Context context, ResourceWrapper resourceWrapper, int count, Object... args) {
    Node node = context.getCurNode();
    if (context.getCurEntry().getBlockError() == null) {
        // Calculate response time (use completeStatTime as the time of completion).
        long completeStatTime = TimeUtil.currentTimeMillis();
        context.getCurEntry().setCompleteTimestamp(completeStatTime);
        long rt = completeStatTime - context.getCurEntry().getCreateTimestamp();
        Throwable error = context.getCurEntry().getError();
        // Record response time and success count.
        recordCompleteFor(node, count, rt, error);
        recordCompleteFor(context.getCurEntry().getOriginNode(), count, rt, error);
        if (resourceWrapper.getEntryType() == EntryType.IN) {
            recordCompleteFor(Constants.ENTRY_NODE, count, rt, error);
        }
    }
    // Handle exit event with registered exit callback handlers.
    Collection<ProcessorSlotExitCallback> exitCallbacks = StatisticSlotCallbackRegistry.getExitCallbacks();
    for (ProcessorSlotExitCallback handler : exitCallbacks) {
        handler.onExit(context, resourceWrapper, count, args);
    }
    // fix bug https://github.com/alibaba/Sentinel/issues/2374
    fireExit(context, resourceWrapper, count, args);
}
Also used : ProcessorSlotExitCallback(com.alibaba.csp.sentinel.slotchain.ProcessorSlotExitCallback) DefaultNode(com.alibaba.csp.sentinel.node.DefaultNode) Node(com.alibaba.csp.sentinel.node.Node) ClusterNode(com.alibaba.csp.sentinel.node.ClusterNode)

Example 3 with Node

use of com.alibaba.csp.sentinel.node.Node in project Sentinel by alibaba.

the class SentinelSofaRpcProviderFilterTest method verifyInvocationStructure.

/**
 * Verify Sentinel invocation structure in memory:
 * EntranceNode(methodResourceName)
 * --InterfaceNode(interfaceResourceName)
 * ----MethodNode(methodResourceName)
 */
private void verifyInvocationStructure(String applicationName, String interfaceResourceName, String methodResourceName) {
    Context context = ContextUtil.getContext();
    assertNotNull(context);
    assertEquals(methodResourceName, context.getName());
    assertEquals(applicationName, context.getOrigin());
    DefaultNode entranceNode = context.getEntranceNode();
    ResourceWrapper entranceResource = entranceNode.getId();
    assertEquals(methodResourceName, entranceResource.getName());
    assertSame(EntryType.IN, entranceResource.getEntryType());
    // As SphU.entry(interfaceResourceName, EntryType.IN);
    Set<Node> childList = entranceNode.getChildList();
    assertEquals(1, childList.size());
    DefaultNode interfaceNode = (DefaultNode) childList.iterator().next();
    ResourceWrapper interfaceResource = interfaceNode.getId();
    assertEquals(interfaceResourceName, interfaceResource.getName());
    assertSame(EntryType.IN, interfaceResource.getEntryType());
    // As SphU.entry(methodResourceName, EntryType.IN, 1, methodArguments);
    childList = interfaceNode.getChildList();
    assertEquals(1, childList.size());
    DefaultNode methodNode = (DefaultNode) childList.iterator().next();
    ResourceWrapper methodResource = methodNode.getId();
    assertEquals(methodResourceName, methodResource.getName());
    assertSame(EntryType.IN, methodResource.getEntryType());
    // Verify curEntry
    Entry curEntry = context.getCurEntry();
    assertSame(methodNode, curEntry.getCurNode());
    assertSame(interfaceNode, curEntry.getLastNode());
    // As context origin is not "", originNode should be created
    assertNotNull(curEntry.getOriginNode());
    // Verify clusterNode
    ClusterNode methodClusterNode = methodNode.getClusterNode();
    ClusterNode interfaceClusterNode = interfaceNode.getClusterNode();
    // Different resource->Different ProcessorSlot->Different ClusterNode
    assertNotSame(methodClusterNode, interfaceClusterNode);
    // As context origin is not "", the StatisticNode should be created in originCountMap of ClusterNode
    Map<String, StatisticNode> methodOriginCountMap = methodClusterNode.getOriginCountMap();
    assertEquals(1, methodOriginCountMap.size());
    assertTrue(methodOriginCountMap.containsKey(applicationName));
    Map<String, StatisticNode> interfaceOriginCountMap = interfaceClusterNode.getOriginCountMap();
    assertEquals(1, interfaceOriginCountMap.size());
    assertTrue(interfaceOriginCountMap.containsKey(applicationName));
}
Also used : Context(com.alibaba.csp.sentinel.context.Context) ResourceWrapper(com.alibaba.csp.sentinel.slotchain.ResourceWrapper) ClusterNode(com.alibaba.csp.sentinel.node.ClusterNode) Entry(com.alibaba.csp.sentinel.Entry) DefaultNode(com.alibaba.csp.sentinel.node.DefaultNode) Node(com.alibaba.csp.sentinel.node.Node) StatisticNode(com.alibaba.csp.sentinel.node.StatisticNode) ClusterNode(com.alibaba.csp.sentinel.node.ClusterNode) DefaultNode(com.alibaba.csp.sentinel.node.DefaultNode) StatisticNode(com.alibaba.csp.sentinel.node.StatisticNode)

Example 4 with Node

use of com.alibaba.csp.sentinel.node.Node in project Sentinel by alibaba.

the class ProviderFilterTest method testGetHello.

@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);
}
Also used : Response(io.restassured.response.Response) ClusterNode(com.alibaba.csp.sentinel.node.ClusterNode) EntranceNode(com.alibaba.csp.sentinel.node.EntranceNode) ClusterNode(com.alibaba.csp.sentinel.node.ClusterNode) Node(com.alibaba.csp.sentinel.node.Node) EntranceNode(com.alibaba.csp.sentinel.node.EntranceNode) Test(org.junit.Test)

Example 5 with Node

use of com.alibaba.csp.sentinel.node.Node in project Sentinel by alibaba.

the class ProviderFilterTest method testAsyncGetHello.

@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);
}
Also used : Response(io.restassured.response.Response) ClusterNode(com.alibaba.csp.sentinel.node.ClusterNode) EntranceNode(com.alibaba.csp.sentinel.node.EntranceNode) ClusterNode(com.alibaba.csp.sentinel.node.ClusterNode) Node(com.alibaba.csp.sentinel.node.Node) EntranceNode(com.alibaba.csp.sentinel.node.EntranceNode) Test(org.junit.Test)

Aggregations

Node (com.alibaba.csp.sentinel.node.Node)31 Test (org.junit.Test)18 ClusterNode (com.alibaba.csp.sentinel.node.ClusterNode)15 DefaultNode (com.alibaba.csp.sentinel.node.DefaultNode)14 EntranceNode (com.alibaba.csp.sentinel.node.EntranceNode)11 Entry (com.alibaba.csp.sentinel.Entry)8 StatisticNode (com.alibaba.csp.sentinel.node.StatisticNode)8 ResourceWrapper (com.alibaba.csp.sentinel.slotchain.ResourceWrapper)7 Context (com.alibaba.csp.sentinel.context.Context)6 Response (io.restassured.response.Response)4 StringResourceWrapper (com.alibaba.csp.sentinel.slotchain.StringResourceWrapper)3 DemoService (com.alibaba.csp.sentinel.adapter.dubbo.provider.DemoService)2 TrafficShapingController (com.alibaba.csp.sentinel.slots.block.flow.TrafficShapingController)2 WarmUpRateLimiterController (com.alibaba.csp.sentinel.slots.block.flow.controller.WarmUpRateLimiterController)2 QuarkusUnitTest (io.quarkus.test.QuarkusUnitTest)2 Test (org.junit.jupiter.api.Test)2 SpringBootTest (org.springframework.boot.test.context.SpringBootTest)2 NodeVo (com.alibaba.csp.sentinel.command.vo.NodeVo)1 NullContext (com.alibaba.csp.sentinel.context.NullContext)1 ProcessorSlotExitCallback (com.alibaba.csp.sentinel.slotchain.ProcessorSlotExitCallback)1