Search in sources :

Example 1 with ProcessorSlotChain

use of com.alibaba.csp.sentinel.slotchain.ProcessorSlotChain in project Sentinel by alibaba.

the class DefaultSlotChainBuilderTest method testBuild.

@Test
public void testBuild() {
    DefaultSlotChainBuilder builder = new DefaultSlotChainBuilder();
    ProcessorSlotChain slotChain = builder.build();
    assertNotNull(slotChain);
    // Verify the order of slot
    AbstractLinkedProcessorSlot<?> next = slotChain.getNext();
    assertTrue(next instanceof NodeSelectorSlot);
    // Store the first NodeSelectorSlot instance
    NodeSelectorSlot nodeSelectorSlot = (NodeSelectorSlot) next;
    next = next.getNext();
    assertTrue(next instanceof ClusterBuilderSlot);
    next = next.getNext();
    assertTrue(next instanceof LogSlot);
    next = next.getNext();
    assertTrue(next instanceof StatisticSlot);
    next = next.getNext();
    assertTrue(next instanceof AuthoritySlot);
    next = next.getNext();
    assertTrue(next instanceof SystemSlot);
    next = next.getNext();
    assertTrue(next instanceof FlowSlot);
    next = next.getNext();
    assertTrue(next instanceof DegradeSlot);
    next = next.getNext();
    assertNull(next);
    // Build again to verify different instances
    ProcessorSlotChain slotChain2 = builder.build();
    assertNotNull(slotChain2);
    // Verify the two ProcessorSlotChain instances are different
    assertNotSame(slotChain, slotChain2);
    next = slotChain2.getNext();
    assertTrue(next instanceof NodeSelectorSlot);
    // Store the second NodeSelectorSlot instance
    NodeSelectorSlot nodeSelectorSlot2 = (NodeSelectorSlot) next;
    // Verify the two NodeSelectorSlot instances are different
    assertNotSame(nodeSelectorSlot, nodeSelectorSlot2);
}
Also used : ClusterBuilderSlot(com.alibaba.csp.sentinel.slots.clusterbuilder.ClusterBuilderSlot) ProcessorSlotChain(com.alibaba.csp.sentinel.slotchain.ProcessorSlotChain) LogSlot(com.alibaba.csp.sentinel.slots.logger.LogSlot) StatisticSlot(com.alibaba.csp.sentinel.slots.statistic.StatisticSlot) NodeSelectorSlot(com.alibaba.csp.sentinel.slots.nodeselector.NodeSelectorSlot) DegradeSlot(com.alibaba.csp.sentinel.slots.block.degrade.DegradeSlot) FlowSlot(com.alibaba.csp.sentinel.slots.block.flow.FlowSlot) AuthoritySlot(com.alibaba.csp.sentinel.slots.block.authority.AuthoritySlot) SystemSlot(com.alibaba.csp.sentinel.slots.system.SystemSlot) Test(org.junit.Test)

Example 2 with ProcessorSlotChain

use of com.alibaba.csp.sentinel.slotchain.ProcessorSlotChain in project Sentinel by alibaba.

the class CtSph method lookProcessChain.

/**
 * Get {@link ProcessorSlotChain} of the resource. new {@link ProcessorSlotChain} will
 * be created if the resource doesn't relate one.
 *
 * <p>Same resource({@link ResourceWrapper#equals(Object)}) will share the same
 * {@link ProcessorSlotChain} globally, no matter in which {@link Context}.<p/>
 *
 * <p>
 * Note that total {@link ProcessorSlot} count must not exceed {@link Constants#MAX_SLOT_CHAIN_SIZE},
 * otherwise null will return.
 * </p>
 *
 * @param resourceWrapper target resource
 * @return {@link ProcessorSlotChain} of the resource
 */
ProcessorSlot<Object> lookProcessChain(ResourceWrapper resourceWrapper) {
    ProcessorSlotChain chain = chainMap.get(resourceWrapper);
    if (chain == null) {
        synchronized (LOCK) {
            chain = chainMap.get(resourceWrapper);
            if (chain == null) {
                // Entry size limit.
                if (chainMap.size() >= Constants.MAX_SLOT_CHAIN_SIZE) {
                    return null;
                }
                chain = SlotChainProvider.newSlotChain();
                Map<ResourceWrapper, ProcessorSlotChain> newMap = new HashMap<ResourceWrapper, ProcessorSlotChain>(chainMap.size() + 1);
                newMap.putAll(chainMap);
                newMap.put(resourceWrapper, chain);
                chainMap = newMap;
            }
        }
    }
    return chain;
}
Also used : StringResourceWrapper(com.alibaba.csp.sentinel.slotchain.StringResourceWrapper) ResourceWrapper(com.alibaba.csp.sentinel.slotchain.ResourceWrapper) MethodResourceWrapper(com.alibaba.csp.sentinel.slotchain.MethodResourceWrapper) ProcessorSlotChain(com.alibaba.csp.sentinel.slotchain.ProcessorSlotChain) HashMap(java.util.HashMap)

Example 3 with ProcessorSlotChain

use of com.alibaba.csp.sentinel.slotchain.ProcessorSlotChain in project Sentinel by alibaba.

the class DefaultSlotChainBuilder method build.

@Override
public ProcessorSlotChain build() {
    ProcessorSlotChain chain = new DefaultProcessorSlotChain();
    List<ProcessorSlot> sortedSlotList = SpiLoader.of(ProcessorSlot.class).loadInstanceListSorted();
    for (ProcessorSlot slot : sortedSlotList) {
        if (!(slot instanceof AbstractLinkedProcessorSlot)) {
            RecordLog.warn("The ProcessorSlot(" + slot.getClass().getCanonicalName() + ") is not an instance of AbstractLinkedProcessorSlot, can't be added into ProcessorSlotChain");
            continue;
        }
        chain.addLast((AbstractLinkedProcessorSlot<?>) slot);
    }
    return chain;
}
Also used : DefaultProcessorSlotChain(com.alibaba.csp.sentinel.slotchain.DefaultProcessorSlotChain) ProcessorSlotChain(com.alibaba.csp.sentinel.slotchain.ProcessorSlotChain) DefaultProcessorSlotChain(com.alibaba.csp.sentinel.slotchain.DefaultProcessorSlotChain) AbstractLinkedProcessorSlot(com.alibaba.csp.sentinel.slotchain.AbstractLinkedProcessorSlot) ProcessorSlot(com.alibaba.csp.sentinel.slotchain.ProcessorSlot) AbstractLinkedProcessorSlot(com.alibaba.csp.sentinel.slotchain.AbstractLinkedProcessorSlot)

Example 4 with ProcessorSlotChain

use of com.alibaba.csp.sentinel.slotchain.ProcessorSlotChain in project Sentinel by alibaba.

the class CtSphTest method addShouldNotPassSlotFor.

private void addShouldNotPassSlotFor(ResourceWrapper resourceWrapper) {
    ProcessorSlotChain slotChain = new DefaultProcessorSlotChain();
    slotChain.addLast(new ShouldNotPassSlot());
    CtSph.getChainMap().put(resourceWrapper, slotChain);
}
Also used : ProcessorSlotChain(com.alibaba.csp.sentinel.slotchain.ProcessorSlotChain) DefaultProcessorSlotChain(com.alibaba.csp.sentinel.slotchain.DefaultProcessorSlotChain) DefaultProcessorSlotChain(com.alibaba.csp.sentinel.slotchain.DefaultProcessorSlotChain)

Example 5 with ProcessorSlotChain

use of com.alibaba.csp.sentinel.slotchain.ProcessorSlotChain in project Sentinel by alibaba.

the class CtSphTest method addMustBlockSlot.

private MustBlockSlot addMustBlockSlot(ResourceWrapper resourceWrapper) {
    ProcessorSlotChain slotChain = new DefaultProcessorSlotChain();
    MustBlockSlot mustBlockSlot = new MustBlockSlot();
    slotChain.addLast(mustBlockSlot);
    CtSph.getChainMap().put(resourceWrapper, slotChain);
    return mustBlockSlot;
}
Also used : ProcessorSlotChain(com.alibaba.csp.sentinel.slotchain.ProcessorSlotChain) DefaultProcessorSlotChain(com.alibaba.csp.sentinel.slotchain.DefaultProcessorSlotChain) DefaultProcessorSlotChain(com.alibaba.csp.sentinel.slotchain.DefaultProcessorSlotChain)

Aggregations

ProcessorSlotChain (com.alibaba.csp.sentinel.slotchain.ProcessorSlotChain)6 DefaultProcessorSlotChain (com.alibaba.csp.sentinel.slotchain.DefaultProcessorSlotChain)4 AbstractLinkedProcessorSlot (com.alibaba.csp.sentinel.slotchain.AbstractLinkedProcessorSlot)1 MethodResourceWrapper (com.alibaba.csp.sentinel.slotchain.MethodResourceWrapper)1 ProcessorSlot (com.alibaba.csp.sentinel.slotchain.ProcessorSlot)1 ResourceWrapper (com.alibaba.csp.sentinel.slotchain.ResourceWrapper)1 StringResourceWrapper (com.alibaba.csp.sentinel.slotchain.StringResourceWrapper)1 AuthoritySlot (com.alibaba.csp.sentinel.slots.block.authority.AuthoritySlot)1 DegradeSlot (com.alibaba.csp.sentinel.slots.block.degrade.DegradeSlot)1 FlowSlot (com.alibaba.csp.sentinel.slots.block.flow.FlowSlot)1 ClusterBuilderSlot (com.alibaba.csp.sentinel.slots.clusterbuilder.ClusterBuilderSlot)1 LogSlot (com.alibaba.csp.sentinel.slots.logger.LogSlot)1 NodeSelectorSlot (com.alibaba.csp.sentinel.slots.nodeselector.NodeSelectorSlot)1 StatisticSlot (com.alibaba.csp.sentinel.slots.statistic.StatisticSlot)1 SystemSlot (com.alibaba.csp.sentinel.slots.system.SystemSlot)1 HashMap (java.util.HashMap)1 Test (org.junit.Test)1