Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Merged
merged 2 commits into from
Jun 14, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,10 @@ public class AsyncEntry extends CtEntry {
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.
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,11 @@ class CtEntry extends Entry {
protected LinkedList<BiConsumer<Context, Entry>> exitHandlers;

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.context = context;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,7 @@ private AsyncEntry asyncEntryWithPriorityInternal(ResourceWrapper resourceWrappe
return asyncEntryWithNoChain(resourceWrapper, context);
}

AsyncEntry asyncEntry = new AsyncEntry(resourceWrapper, chain, context);
AsyncEntry asyncEntry = new AsyncEntry(resourceWrapper, chain, context, count, args);
try {
chain.entry(context, resourceWrapper, null, count, prioritized, args);
// Initiate the async context only when the entry successfully passed the slot chain.
Expand Down Expand Up @@ -143,7 +143,7 @@ private Entry entryWithPriority(ResourceWrapper resourceWrapper, int count, bool
return new CtEntry(resourceWrapper, null, context);
}

Entry e = new CtEntry(resourceWrapper, chain, context);
Entry e = new CtEntry(resourceWrapper, chain, context, count, args);
try {
chain.entry(context, resourceWrapper, null, count, prioritized, args);
} catch (BlockException e1) {
Expand Down
18 changes: 14 additions & 4 deletions sentinel-core/src/main/java/com/alibaba/csp/sentinel/Entry.java
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@
*/
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 long completeTimestamp;
Expand All @@ -69,9 +69,19 @@ public abstract class Entry implements AutoCloseable {

protected final ResourceWrapper resourceWrapper;

protected final int count;

protected final Object[] args;

public Entry(ResourceWrapper resourceWrapper) {
this(resourceWrapper, 1, OBJECTS0);
}

public Entry(ResourceWrapper resourceWrapper, int count, Object[] args) {
this.resourceWrapper = resourceWrapper;
this.createTimestamp = TimeUtil.currentTimeMillis();
this.count = count;
this.args = args;
}

public ResourceWrapper getResourceWrapper() {
Expand All @@ -80,15 +90,15 @@ public ResourceWrapper getResourceWrapper() {

/**
* 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
*/
public void exit() throws ErrorEntryFreeException {
exit(1, OBJECTS0);
exit(count, args);
}

public void exit(int count) throws ErrorEntryFreeException {
exit(count, OBJECTS0);
exit(count, args);
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -221,6 +221,6 @@ public static void exit(int count) {
}

public static void exit() {
exit(1, OBJECTS0);
ContextUtil.getContext().getCurEntry().exit();
}
}
10 changes: 10 additions & 0 deletions sentinel-core/src/test/java/com/alibaba/csp/sentinel/SphUTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -157,4 +157,14 @@ public void testMethodEntryAll() throws BlockException, NoSuchMethodException, S

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());
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This does not actually verify the case here.

}
}