public interface ResourceContainer
ResourceContainer
defines a set of resource Constraint
s
that limit resource usage by threads.
Zero or more threads can be attached to one ResourceContainer
.
+-------------------------------+ +-----------------------------+ |ResourceContainer | |RootContainer | | | | | | +---------------+ | | | | |CPU Constraint | | | +----------+ | | +---------------+ <------------------+ Thread | | | +---------------+ | | +----------+ | | |Mem Constraint |command.run()| | container.run(command) | | +---------------+ | | | | | |----------------> v | | | | resources are command.run()| | | | controlled by returns | | back to root | | constraints +-----------------> container | | | | | | | | | +-------------------------------+ +-----------------------------+The figure above describes the structure and usage of
ResourceContainer
.
The main Thread is bounded to root container which can be fetched by
root()
. Root Container is the system default
ResourceContainer
without any resource restrictions.
Newly created threads are implicitly bounded to ResourceContainer
of the parent thread.
A Thread can invoke run(Runnable command)
to
attach to the ResourceContainer
and run the command
,
The resource usage of the thread are controlled by the ResourceContainer
's
constraints while running the command.
When the execution of the command is either finished normally
or terminated by Exception, the thread will be detached from the container automatically.
Components of a ResourceContainer
implementation:
ResourceType
: an implementation can customize some ResourceTypeResourceContainer
: Implements a class extends
AbstractResourceContainer
ResourceProvider
: service provider
ResourceContainer
needs to be created from a set of Constraint
s
from ContainerBuilder
.
In most cases, the following idiom should be used:
ResourceContainer resourceContainer = new ContainerBuilder() // configure some Constraints .build(resourceProvider); resourceContainer.run(requestHandler); resourceContainer.destroy();
ContainerBuilder
,
ResourceProvider
Modifier and Type | Interface and Description |
---|---|
static class |
ResourceContainer.State
An enumeration of Container state
|
Modifier and Type | Method and Description |
---|---|
static AbstractResourceContainer |
current()
Returns the ResourceContainer associated with the current thread.
|
void |
destroy()
Destroys this resource container, also kills the attached threads and releases
resources described in
getConstraints() . |
Iterable<Constraint> |
getConstraints()
Gets container's
Constraint s |
ResourceContainer.State |
getState()
Returns the current ResourceContainer state.
|
static ResourceContainer |
root()
Returns the system wide "root" Resource container.
|
void |
run(Runnable command)
Attach the current thread to the ResourceContainer to run the
command ,
and detach the ResourceContainer when command is either normally finished
or terminated by Exception. |
void |
updateConstraint(Constraint constraint)
Updates
Constraint of this resource container. |
static ResourceContainer root()
Root ResourceContainer is a virtual container that indicates
the default resource container for any thread, which is not attached
to a ResourceContainer created by user. Root ResourceContainer does
not have any resource constrains.
run(Runnable)
method of root container is a special
implementation that detaches from the current container and returns
to root container.
It is very useful in ResourceContainer switch scenario:
// Assume we already attach to a non-root resourceContainer1
resourceContainer2.run(command);
// throws exception, because it is illegal to switch between non-root
// ResourceContainers
ResourceContainer.root(() -> resourceContainer2.run(command));
static AbstractResourceContainer current()
root()
is returned.ResourceContainer.State getState()
void run(Runnable command)
command
,
and detach the ResourceContainer when command
is either normally finished
or terminated by Exception.
At the same time, it is not allowed to switch directly between any two
containers. If the switch is indeed required, the
root()
container should be used.
This way restricts the container attach/detach mode for the API users, but is less error-prone.
ResourceContainer resourceContainer = .... assert ResourceContainer.current() == ResourceContainer.root(); resourceContainer.run(() -> { assert ResourceContainer.current() == resourceContainer; }); assert ResourceContainer.current() == ResourceContainer.root();
command
- the target codevoid updateConstraint(Constraint constraint)
Constraint
of this resource container.constraint
- constraints listUnsupportedOperationException
- Constraint.getResourceType()
is not
supported by the implementationIterable<Constraint> getConstraints()
Constraint
sConstraint
svoid destroy()
getConstraints()
.
Once this method is called, the state will become ResourceContainer.State.STOPPING
.
And the caller thread will be blocked until all the resources have been released.
Then the container state will become ResourceContainer.State.DEAD
.
"Copyright 2018, Alibaba, Inc."