Search in sources :

Example 1 with ProcessorSlot

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

the class SpiLoaderTest method testLoadLowestPriorityInstance.

@Test
public void testLoadLowestPriorityInstance() {
    ProcessorSlot slot = SpiLoader.of(ProcessorSlot.class).loadLowestPriorityInstance();
    assertNotNull(slot);
    // NodeSelectorSlot is lowest order priority with @Spi(order = -1000) among all slots
    assertTrue(slot instanceof DegradeSlot);
}
Also used : ProcessorSlot(com.alibaba.csp.sentinel.slotchain.ProcessorSlot) DegradeSlot(com.alibaba.csp.sentinel.slots.block.degrade.DegradeSlot) Test(org.junit.Test)

Example 2 with ProcessorSlot

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

the class SpiLoaderTest method testLoadHighestPriorityInstance.

@Test
public void testLoadHighestPriorityInstance() {
    ProcessorSlot slot = SpiLoader.of(ProcessorSlot.class).loadHighestPriorityInstance();
    assertNotNull(slot);
    // NodeSelectorSlot is highest order priority with @Spi(order = -10000) among all slots
    assertTrue(slot instanceof NodeSelectorSlot);
}
Also used : ProcessorSlot(com.alibaba.csp.sentinel.slotchain.ProcessorSlot) NodeSelectorSlot(com.alibaba.csp.sentinel.slots.nodeselector.NodeSelectorSlot) Test(org.junit.Test)

Example 3 with ProcessorSlot

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

the class SpiLoaderTest method testLoadFirstInstance.

@Test
public void testLoadFirstInstance() {
    ProcessorSlot slot = SpiLoader.of(ProcessorSlot.class).loadFirstInstance();
    assertNotNull(slot);
    assertTrue(slot instanceof NodeSelectorSlot);
    SlotChainBuilder chainBuilder = SpiLoader.of(SlotChainBuilder.class).loadFirstInstance();
    assertNotNull(chainBuilder);
    assertTrue(chainBuilder instanceof SlotChainBuilder);
    InitFunc initFunc = SpiLoader.of(InitFunc.class).loadFirstInstance();
    assertNotNull(initFunc);
    assertTrue(initFunc instanceof MetricCallbackInit);
}
Also used : InitFunc(com.alibaba.csp.sentinel.init.InitFunc) SlotChainBuilder(com.alibaba.csp.sentinel.slotchain.SlotChainBuilder) DefaultSlotChainBuilder(com.alibaba.csp.sentinel.slots.DefaultSlotChainBuilder) MetricCallbackInit(com.alibaba.csp.sentinel.metric.extension.MetricCallbackInit) ProcessorSlot(com.alibaba.csp.sentinel.slotchain.ProcessorSlot) NodeSelectorSlot(com.alibaba.csp.sentinel.slots.nodeselector.NodeSelectorSlot) Test(org.junit.Test)

Example 4 with ProcessorSlot

use of com.alibaba.csp.sentinel.slotchain.ProcessorSlot 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 5 with ProcessorSlot

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

the class SpiLoaderTest method testLoadInstanceList.

@Test
public void testLoadInstanceList() {
    SpiLoader spiLoader = SpiLoader.of(ProcessorSlot.class);
    List<ProcessorSlot> slots1 = spiLoader.loadInstanceList();
    List<ProcessorSlot> slots2 = spiLoader.loadInstanceList();
    assertNotSame(slots1, slots2);
    List<Class<? extends ProcessorSlot>> prototypeSlotClasses = new ArrayList<>(2);
    prototypeSlotClasses.add(NodeSelectorSlot.class);
    prototypeSlotClasses.add(ClusterBuilderSlot.class);
    List<Class<? extends ProcessorSlot>> singletonSlotClasses = new ArrayList<>(6);
    singletonSlotClasses.add(LogSlot.class);
    singletonSlotClasses.add(StatisticSlot.class);
    singletonSlotClasses.add(AuthoritySlot.class);
    singletonSlotClasses.add(SystemSlot.class);
    singletonSlotClasses.add(FlowSlot.class);
    singletonSlotClasses.add(DegradeSlot.class);
    for (int i = 0; i < slots1.size(); i++) {
        ProcessorSlot slot1 = slots1.get(i);
        ProcessorSlot slot2 = slots2.get(i);
        assertSame(slot1.getClass(), slot2.getClass());
        boolean found = false;
        for (Class<? extends ProcessorSlot> prototypeSlotClass : prototypeSlotClasses) {
            if (prototypeSlotClass.equals(slot1.getClass())) {
                found = true;
                assertTrue(prototypeSlotClass.equals(slot2.getClass()));
                // Verify prototype function
                assertNotSame(slot1, slot2);
                break;
            }
        }
        if (found) {
            continue;
        }
        for (Class<? extends ProcessorSlot> singletonSlotClass : singletonSlotClasses) {
            if (singletonSlotClass.equals(slot1.getClass())) {
                found = true;
                assertTrue(singletonSlotClass.equals(slot2.getClass()));
                // Verify single function
                assertSame(slot1, slot2);
                break;
            }
        }
        if (!found) {
            fail("Should found and not go through here");
        }
    }
}
Also used : ArrayList(java.util.ArrayList) ProcessorSlot(com.alibaba.csp.sentinel.slotchain.ProcessorSlot) Test(org.junit.Test)

Aggregations

ProcessorSlot (com.alibaba.csp.sentinel.slotchain.ProcessorSlot)7 Test (org.junit.Test)6 NodeSelectorSlot (com.alibaba.csp.sentinel.slots.nodeselector.NodeSelectorSlot)2 StatisticSlot (com.alibaba.csp.sentinel.slots.statistic.StatisticSlot)2 InitFunc (com.alibaba.csp.sentinel.init.InitFunc)1 MetricCallbackInit (com.alibaba.csp.sentinel.metric.extension.MetricCallbackInit)1 AbstractLinkedProcessorSlot (com.alibaba.csp.sentinel.slotchain.AbstractLinkedProcessorSlot)1 DefaultProcessorSlotChain (com.alibaba.csp.sentinel.slotchain.DefaultProcessorSlotChain)1 ProcessorSlotChain (com.alibaba.csp.sentinel.slotchain.ProcessorSlotChain)1 SlotChainBuilder (com.alibaba.csp.sentinel.slotchain.SlotChainBuilder)1 DefaultSlotChainBuilder (com.alibaba.csp.sentinel.slots.DefaultSlotChainBuilder)1 DegradeSlot (com.alibaba.csp.sentinel.slots.block.degrade.DegradeSlot)1 ArrayList (java.util.ArrayList)1