forked from alibaba/Sentinel
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Refactor the context and entry to support asynchronous invocation chain
Signed-off-by: Eric Zhao <[email protected]>
- Loading branch information
Showing
7 changed files
with
366 additions
and
74 deletions.
There are no files selected for viewing
84 changes: 84 additions & 0 deletions
84
sentinel-core/src/main/java/com/alibaba/csp/sentinel/AsyncEntry.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,84 @@ | ||
/* | ||
* Copyright 1999-2018 Alibaba Group Holding Ltd. | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
package com.alibaba.csp.sentinel; | ||
|
||
import com.alibaba.csp.sentinel.context.Context; | ||
import com.alibaba.csp.sentinel.slotchain.ProcessorSlot; | ||
import com.alibaba.csp.sentinel.slotchain.ResourceWrapper; | ||
|
||
/** | ||
* The entry for asynchronous resources. | ||
* | ||
* @author Eric Zhao | ||
* @since 0.2.0 | ||
*/ | ||
public class AsyncEntry extends CtEntry { | ||
|
||
private Context asyncContext; | ||
|
||
AsyncEntry(ResourceWrapper resourceWrapper, ProcessorSlot<Object> chain, Context context) { | ||
super(resourceWrapper, chain, context); | ||
} | ||
|
||
/** | ||
* Remove current entry from local context, but does not exit. | ||
*/ | ||
void cleanCurrentEntryInLocal() { | ||
Context originalContext = context; | ||
if (originalContext != null) { | ||
Entry curEntry = originalContext.getCurEntry(); | ||
if (curEntry == this) { | ||
Entry parent = this.parent; | ||
originalContext.setCurEntry(parent); | ||
if (parent != null) { | ||
((CtEntry)parent).child = null; | ||
} | ||
} else { | ||
throw new IllegalStateException("Bad async context state"); | ||
} | ||
} | ||
} | ||
|
||
public Context getAsyncContext() { | ||
return asyncContext; | ||
} | ||
|
||
/** | ||
* The async context should not be initialized until the node for current resource has been set to current entry. | ||
*/ | ||
void initAsyncContext() { | ||
if (asyncContext == null) { | ||
this.asyncContext = Context.newAsyncContext(context.getEntranceNode(), context.getName()) | ||
.setOrigin(context.getOrigin()) | ||
.setCurEntry(this); | ||
} else { | ||
throw new IllegalStateException("Duplicate initialize of async context"); | ||
} | ||
} | ||
|
||
@Override | ||
protected void clearEntryContext() { | ||
super.clearEntryContext(); | ||
this.asyncContext = null; | ||
} | ||
|
||
@Override | ||
protected Entry trueExit(int count, Object... args) throws ErrorEntryFreeException { | ||
exitForContext(asyncContext, count, args); | ||
|
||
return parent; | ||
} | ||
} |
103 changes: 103 additions & 0 deletions
103
sentinel-core/src/main/java/com/alibaba/csp/sentinel/CtEntry.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,103 @@ | ||
/* | ||
* Copyright 1999-2018 Alibaba Group Holding Ltd. | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
package com.alibaba.csp.sentinel; | ||
|
||
import com.alibaba.csp.sentinel.context.Context; | ||
import com.alibaba.csp.sentinel.context.ContextUtil; | ||
import com.alibaba.csp.sentinel.node.Node; | ||
import com.alibaba.csp.sentinel.slotchain.ProcessorSlot; | ||
import com.alibaba.csp.sentinel.slotchain.ResourceWrapper; | ||
|
||
/** | ||
* Linked entry within current context. | ||
* | ||
* @author jialiang.linjl | ||
* @author Eric Zhao | ||
*/ | ||
class CtEntry extends Entry { | ||
|
||
protected Entry parent = null; | ||
protected Entry child = null; | ||
|
||
protected ProcessorSlot<Object> chain; | ||
protected Context context; | ||
|
||
CtEntry(ResourceWrapper resourceWrapper, ProcessorSlot<Object> chain, Context context) { | ||
super(resourceWrapper); | ||
this.chain = chain; | ||
this.context = context; | ||
|
||
setUpEntryFor(context); | ||
} | ||
|
||
private void setUpEntryFor(Context context) { | ||
this.parent = context.getCurEntry(); | ||
if (parent != null) { | ||
((CtEntry)parent).child = this; | ||
} | ||
context.setCurEntry(this); | ||
} | ||
|
||
@Override | ||
public void exit(int count, Object... args) throws ErrorEntryFreeException { | ||
trueExit(count, args); | ||
} | ||
|
||
protected void exitForContext(Context context, int count, Object... args) throws ErrorEntryFreeException { | ||
if (context != null) { | ||
if (context.getCurEntry() != this) { | ||
// Clean previous call stack. | ||
CtEntry e = (CtEntry)context.getCurEntry(); | ||
while (e != null) { | ||
e.exit(count, args); | ||
e = (CtEntry)e.parent; | ||
} | ||
throw new ErrorEntryFreeException("The order of entry free is can't be paired with the order of entry"); | ||
} else { | ||
if (chain != null) { | ||
chain.exit(context, resourceWrapper, count, args); | ||
} | ||
// Restore the call stack. | ||
context.setCurEntry(parent); | ||
if (parent != null) { | ||
((CtEntry)parent).child = null; | ||
} | ||
if (parent == null) { | ||
// Auto-created entry indicates immediate exit. | ||
ContextUtil.exit(); | ||
} | ||
// Clean the reference of context in current entry to avoid duplicate exit. | ||
clearEntryContext(); | ||
} | ||
} | ||
} | ||
|
||
protected void clearEntryContext() { | ||
this.context = null; | ||
} | ||
|
||
@Override | ||
protected Entry trueExit(int count, Object... args) throws ErrorEntryFreeException { | ||
exitForContext(context, count, args); | ||
|
||
return parent; | ||
} | ||
|
||
@Override | ||
public Node getLastNode() { | ||
return parent == null ? null : parent.getCurNode(); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.