Remove the requirement of carrying batchCount and args in entry.exit() (#3114)

This commit is contained in:
quguai 2023-06-14 23:48:28 +08:00 committed by LearningGp
parent f991f5f8a4
commit c4997eee15
6 changed files with 36 additions and 8 deletions

View File

@ -35,6 +35,10 @@ public class AsyncEntry extends CtEntry {
super(resourceWrapper, chain, context); super(resourceWrapper, chain, context);
} }
AsyncEntry(ResourceWrapper resourceWrapper, ProcessorSlot<Object> chain, Context context, int count, Object[] args) {
super(resourceWrapper, chain, context, count, args);
}
/** /**
* Remove current entry from local context, but does not exit. * Remove current entry from local context, but does not exit.
*/ */

View File

@ -42,7 +42,11 @@ class CtEntry extends Entry {
protected LinkedList<BiConsumer<Context, Entry>> exitHandlers; protected LinkedList<BiConsumer<Context, Entry>> exitHandlers;
CtEntry(ResourceWrapper resourceWrapper, ProcessorSlot<Object> chain, Context context) { CtEntry(ResourceWrapper resourceWrapper, ProcessorSlot<Object> chain, Context context) {
super(resourceWrapper); this(resourceWrapper, chain, context, 1, OBJECTS0);
}
CtEntry(ResourceWrapper resourceWrapper, ProcessorSlot<Object> chain, Context context, int count, Object[] args) {
super(resourceWrapper, count, args);
this.chain = chain; this.chain = chain;
this.context = context; this.context = context;

View File

@ -86,7 +86,7 @@ public class CtSph implements Sph {
return asyncEntryWithNoChain(resourceWrapper, context); return asyncEntryWithNoChain(resourceWrapper, context);
} }
AsyncEntry asyncEntry = new AsyncEntry(resourceWrapper, chain, context); AsyncEntry asyncEntry = new AsyncEntry(resourceWrapper, chain, context, count, args);
try { try {
chain.entry(context, resourceWrapper, null, count, prioritized, args); chain.entry(context, resourceWrapper, null, count, prioritized, args);
// Initiate the async context only when the entry successfully passed the slot chain. // Initiate the async context only when the entry successfully passed the slot chain.
@ -143,7 +143,7 @@ public class CtSph implements Sph {
return new CtEntry(resourceWrapper, null, context); return new CtEntry(resourceWrapper, null, context);
} }
Entry e = new CtEntry(resourceWrapper, chain, context); Entry e = new CtEntry(resourceWrapper, chain, context, count, args);
try { try {
chain.entry(context, resourceWrapper, null, count, prioritized, args); chain.entry(context, resourceWrapper, null, count, prioritized, args);
} catch (BlockException e1) { } catch (BlockException e1) {

View File

@ -53,7 +53,7 @@ import com.alibaba.csp.sentinel.context.Context;
*/ */
public abstract class Entry implements AutoCloseable { public abstract class Entry implements AutoCloseable {
private static final Object[] OBJECTS0 = new Object[0]; protected static final Object[] OBJECTS0 = new Object[0];
private final long createTimestamp; private final long createTimestamp;
private long completeTimestamp; private long completeTimestamp;
@ -69,9 +69,19 @@ public abstract class Entry implements AutoCloseable {
protected final ResourceWrapper resourceWrapper; protected final ResourceWrapper resourceWrapper;
protected final int count;
protected final Object[] args;
public Entry(ResourceWrapper resourceWrapper) { public Entry(ResourceWrapper resourceWrapper) {
this(resourceWrapper, 1, OBJECTS0);
}
public Entry(ResourceWrapper resourceWrapper, int count, Object[] args) {
this.resourceWrapper = resourceWrapper; this.resourceWrapper = resourceWrapper;
this.createTimestamp = TimeUtil.currentTimeMillis(); this.createTimestamp = TimeUtil.currentTimeMillis();
this.count = count;
this.args = args;
} }
public ResourceWrapper getResourceWrapper() { public ResourceWrapper getResourceWrapper() {
@ -80,15 +90,15 @@ public abstract class Entry implements AutoCloseable {
/** /**
* Complete the current resource entry and restore the entry stack in context. * Complete the current resource entry and restore the entry stack in context.
* * Do not need to carry count or args parameter, initialization does
* @throws ErrorEntryFreeException if entry in current context does not match current entry * @throws ErrorEntryFreeException if entry in current context does not match current entry
*/ */
public void exit() throws ErrorEntryFreeException { public void exit() throws ErrorEntryFreeException {
exit(1, OBJECTS0); exit(count, args);
} }
public void exit(int count) throws ErrorEntryFreeException { public void exit(int count) throws ErrorEntryFreeException {
exit(count, OBJECTS0); exit(count, args);
} }
/** /**

View File

@ -221,6 +221,6 @@ public class SphO {
} }
public static void exit() { public static void exit() {
exit(1, OBJECTS0); ContextUtil.getContext().getCurEntry().exit();
} }
} }

View File

@ -157,4 +157,14 @@ public class SphUTest {
e.exit(2, arg0, arg1); e.exit(2, arg0, arg1);
} }
@Test
public void testEntryExitAutomation() throws BlockException{
String[] args = {"foo", "baz"};
int batchCount = 3;
Entry e = SphU.entry("testEntryExitAutomation", EntryType.IN, 3, args);
e.exit();
// The number of success is automatically updated based on batchCount when exit
assertEquals(batchCount, e.getCurNode().totalSuccess());
}
} }