Sentinel Cluster Process Analysis

 When introduced in front of sentinel-core processes, making reference to the flow control judge, it will judge the current local limit, or limiting cluster, if cluster mode, it will go to another branch, this section will be on the cluster model for analysis .

I. Basic Concepts

 namespace: limiting the role to, used to distinguish one rule to act on what range

 flowId: represents the globally unique rule ID, Sentinel limiting cluster server to distinguish between the various rules by the ID, so be sure to remain globally unique. Usually flowId assigned by the unified control terminal, or written to generate a time DB.

 thresholdType: limit threshold represents the cluster mode. Connections wherein the configuration of the single shared equally mode threshold equivalent to a stand-alone can withstand limits, token server based on the client corresponding namespace (default application name project.name defined) for calculating a total threshold (such as the stand-alone mode there are three client is connected to the token server, and with the single shared equally threshold value is 10, calculated on the total amount of the cluster 30); global mode configured threshold value equal to that threshold value the entire cluster.

Two communications framework

 sentinel-cluster distance based netty provides a communications framework, divided into client and service, which uses jdk own SPI, provide default implementations of some interfaces. The default following picture shows the sentinel-cluster-client implementation of the client module class.

file

 InitFunc loading by InitExecutor loaded, InitExecutor in sentinel-core module. InitExecutor loads all InitFunc implementation class in the global access and call its init method completes the initialization. The module configuration InitFunc implementation class for all types of DefaultClusterClientInitFunc, such initializes communication protocols based encoding and decoding processes. Codec ResponseDataDecodeRegistry call registration methods and plant RequestDataWriterRegistry register, for subsequent use. The system provides PING, FLOW (flow control) and PARAM_FLOW (hot flow control parameter) three codec.

file

file

 The figure sentinel-cluster communication protocol format, in response to the request and have a 4-byte message id and the 1-byte message type, the message body is left, a response format, byte 1 status information. It should be noted that, during initialization Netty client, adds two filter:

file

 That is when sending a message, will automatically add a length of 2 bytes length header message, automatically omitted two byte message length header when read.
In order to resolve the above message formats, it provides a registration method above, sentinel also provides ClientEntityCodeProvider, unified processing messages.

file

 As above, in the static class static initialization code block using SPI, and obtain RequestEntityWriter ResponseEntityDecoder implementation class, these two classes are implemented in this module specifies the default implementation: DefaultResponseEntityDecoder and DefaultRequestEntityWriter. That process is


ClientEntityCodecProvider->ResponseEntityDecoder->ResponseDataDecodeRegisty-> EntityDecoder

ClientEntityCodecProvider->RequestEntityWriter->RequestDataWriterRegisty-> EntityWriter

 The system also provides TokenClientHandler class for response data stream corresponding treatment

file

 As it listed only the more important properties and methods. This class inherits ChannelInboundHandlerAdapter and implements the corresponding method, currentState attribute for marking the current state of the client, disconnectCallback is responsible for re-connection when a break occurs. TokenClientHandler achieve channelActive method, it will be sent when the connection is established PING request to the server; achieve channelUnregistered method will be called disconnectCallback when you disconnect, re-connect after a certain time, with the number of failures related to waiting time; achieve channelRead method, when there is a response data will be receiving a response content, and processed, the processing flow is as follows:

file

 After the message is processed and analyzed Netty type object, determines the type of the response, if PING response message, directly log output, or the content of the response message TokenClientPromiseHolder the id corresponding to the setting, so that the message sending thread can be obtained from response.
 The above-mentioned TokenClientPromiseHolder request message for caching. Below, after sending the message, we obtain the corresponding ChannelPromise objects, and in accordance with the message stored in TokenClientPromiseHolder. ChannelPromise waits Netty back a request response, corresponding to the processes above InBound process. Would then obtain a response result corresponding to the request from TokenClientPromiseHolder in accordance with a normal response message id.

file

file

 Core Cluster module interface for TokenService, ClusterTokenServer and ClusterTokenClient. Wherein the inner major class ClusterTokenClient NettyTransportClient, has been described above, following the said two other interfaces. TokenService, ClusterTokenServer relationship in the module below:

file

 Wherein the interface is given by SPI default implementation, as follows:

file

 Next, the interfaces and classes involved will be described.

 TokenService: token service interface, and provides requestToken requestParamToken methods, respectively, and obtaining the flow control token acquiring hot parameter token. The default implementation provided to DefaultTokenService, SPI will be used to load when TokenServiceProvider initialization.

 ClusterTokenServer: server-side upper layer interface that provides a method for starting start and stop the server and stop.

 NettyTransportServer: ClusterTokenServer netty achieve the same corresponding to the client, the pipeline has the following configuration

file

 Wherein the treatment with the client-side codec similar, but adds server processors: TokenServerHandler. Inherited from ChannelInboundHandlerAdapter TokenServerHandler for performing corresponding processing when the connection is established and data interaction:

  1. Achieve channelActive: When the connection is established to be cached

  2. Achieve channelInactive: remove the cache when you disconnect

  3. Achieve channelRead: when there is data arrives for processing. Here we use RequestProcessorProvider loaded RequestProcessor implementation class, select the appropriate treatment classes according to the type (type field) request. Processing system now offers classes have FlowRequestProcessor and ParamFlowRequestProcessor, both of which eventually will DefaultTokenService objects through TokenServiceProvider, calling it to complete the request.

 SentinelDefaultTokenServer: packed NettyTransportServer method, increasing the change ServerTransportConfigObserver for monitoring server configuration items to update itself.

 EmbeddedClusterTokenServer: inherited from TokenService and ClusterTokenServer, for the embedded server mode, the default implementation is DefaultEmbeddedClusterTokenServer.

 DefaultEmbeddedClusterTokenServer: Main DefaultTokenService and combined to achieve the object SentinelDefaultTokenServer interface methods.

 Combined to achieve the above-server, client requests can be a token of the process is as follows:

file

  1. The client calls requestToken DefaultClusterTokenClient method of obtaining token, it will be sent to the internal server-side coding commissioned NettyTransportClient
  2. NettyTransportServer server after receiving the request, the processing method TokenServerHandler channelRead herein based on the requested content type, entrusted to a corresponding message processing, such as FlowRequestProcessor
  3. FlowRequestProcessor calls TokenServiceProvider get TokenService corresponding implementation class, the default is DefaultTokenService. Then commissioned processing for the class.

III. Statistical Logic

 From the above, the cluster mode, the acquisition token is DefaultTokenService responsible, is divided into two types: general and hot flow control flow control parameters. Achieve both consistent here only made to explain the general flow control, i.e. requestToken DefaultTokenService method as in the following processing flow.

file

 When requested requestToken method, request parameters comprising:

 ruleId: rule id

 acquireCount: need to obtain a token number

 prioritized: whether to support priority

  1. DefaultTokenService will first according to ruleId, use ClusterFlowRuleManager get FlowRule rules corresponding to the object. ClusterFlowRuleManager based ruleId cache in a Map, and assign only one ClusterMetric when updating the rules or load rules.

  2. After obtaining the corresponding FlowRule object calls ClusterFlowRuleChecker, to determine whether the token can acquire needed

  3. Id ClusterFlowRuleChecker will first obtain the rules of the rule corresponding namespace, and then determines whether the state in the global namespace exceeds the flow control, which mainly provided by step GlobalRequestLimiter, such namespace corresponding to each stored subject RequestLimiter. RequestLimiter inherited from LeapArray, QPS provides only a sliding window of dimension implemented, default implementations for one second grid 10, as FIG. Global flow control method is mainly used tryPass RequestLimiter calculated current is greater than the global qps qps rule set.

  4. Control the global flow through, corresponding to ClusterMetric obtains ruleId according ClusterMetricStatistics, to obtain the corresponding statistical ruleId dimension. First determines whether the present time is available token, here based on the rules set thresholdtype, distinguishing threshold setting mode, if the mode is global, direct current limit value set according to, if it is shared equally standalone mode, will customer amount multiplied by the end of the existing value reaches a set threshold. If you have the updated statistics and returns a success, if not a priority and does not support, you get direct returns failure. If supported priority, try the next grid borrow token (Note: borrow will borrow from the back of the grid in local mode, they do not exceed the maximum waiting time), if the borrower is successful update statistics and returns a success, otherwise fail. ClusterMetric following structure inherits from ClusterMetriceLeapArray, the sliding statistics window provides a plurality of modes of the cluster mode, also supports priority request.

file

IV. The server startup mode

 Sentinel server startup mode can be divided into independent modes Alone and Embedded embedded mode.

 Standalone mode (Alone), ie as a separate token server process starts, independent deployment, good isolation, but require additional deployment. Stand-alone mode suitable as Global Rate Limiter provide traffic control services to the cluster.

file

  1. In standalone mode, we can create a direct instance of the corresponding ClusterTokenServer and start Token Server by the start method in the main function.

  2. Embedded mode (Embedded), that is to start in the same process as the built-in token server and services. In this mode, each instance in the cluster are peers, token server and client can change at any time, so no separate deployment, flexibility is better. But the isolation of the poor need to limit the total QPS server token, to prevent the impact of the application itself. Embedded mode for an internal application cluster flow control.

file

 The system provides a HTTP API to convert a cluster flow control identity in embedded mode:

http://<ip>:<port>/setClusterMode?mode=<xxx>

 Wherein the mode is 0 for the client, 1 Representative server, -1 representative Close.

 The request is processed by the final call ClusterStateManager.applyState ModifyClusterModeCommandHandler and methods to set the state of the current node. Incidentally, the embedded display mode can not start the server, but is set by the above applyState mode, the method will start the service in the interior. Of course, the client may not be displayed to start, by the same above method, the current node may be set to the client mode. When the current node is set to a client, it will first obtain the server objects in the current embedded mode, if not empty, stop the object and start the server; conversely when setting the server, will first obtain the client object , if not empty, is first stopped, and then start the server object is embedded mode. After the application starts access dashboard, the role of each node can be controlled through the management console, or to change the rules by loading configuration rules from the center.

五 .Handler

 sentinel-transport-common handler defined set of interfaces for the same system provides HTTP interactive interface, thereby enabling data acquisition system or application to send the command nodes.

 common few basic module provides the following interfaces:

  1. CommandCenter: Command Center, started as a service, define the start and stop method, which offers handler initialization and registration services.

  2. HeartbeatSender: heartbeat transmission interface for sending a heartbeat to the timing of the console dashboard

  3. CommandHandler: request processing interface, the request object CommandRequest, the response object is CommandResponse

  4. CommandMapping: annotation, Handler for adding metadata, including a processor name (URL path name) and described

file

 For the above interface, Common module provides the corresponding Provider class, for loading in a default manner SPI / custom implementation, as shown above, comprising:

  1. CommandCenterProvider: The SPI implementation, loading the set, if multiple implementations, the annotation according to Order, a highest priority is selected

  2. HeartbeatSenderProvider: The SPI implementation, loading the set, if multiple implementations, the annotation according to Order, a highest priority is selected

  3. CommandHandlerProvider: loads all the implementation class Handler, provided the different modules implemented as long as the Handler SPI way to provide the fully qualified name corresponding to the META-INF class will be scanned in and used. Implementation class needs to be increased CommandMapping annotation to specify the URL.

 Handler for the following common module implementation

file

file

 SPI interface in the above figure and a common InitFunc implemented, including two HeartbeatSenderInitFunc CommandCenterInitFunc and implementation class, class implements two interfaces InitFunc, initializes all InitFunc is achieved when the called InitExecutor. Corresponding role:

 CommandCenterInitFunc: Use CommandCenterProvider acquire the corresponding CommandCenter achieved, followed by the implementation beforeStart and start method to start the service. As long as that is loaded sentinel-transport-common module and provide an implementation CommandCenter by SPI, will start services on InitFunc is called.

 HeartbeatSenderInitFun: HeartbeatSenderProvider acquire the corresponding HeartbeatSender achieved, start the timer every 5 seconds to perform a method sendHeartbeat. As long as that is loaded sentinel-transport-common module and provide an implementation HeartbeatSender by SPI, will start the heartbeat timer when InitFunc is called.

 As mentioned above, and as long as the realization HeartbeatSender CommandCenter and SPI achieved by a corresponding register, and corresponding service starts automatically located sentinel-transport-simple-http and sentinel-transport-netty modules for both interface provides a default implementation.

 Achieve sentinel-transport-simple-http provided for SimpleHttpCommandCenter and SimpleHttpHeartbeatSender.

 SimpleHttpCommandCenter: Based socket, blocking mode to provide a simple http server, caches all Handler object through the CommandHandlerProvider before the start, when a new open thread, and the thread call the corresponding Handler request comes in for processing and returns

 SimpleHttpHeartbeatSender: transmitting using the built SimpleHttpRequest Http request to the dashboard heartbeat

 Achieve sentinel-transport-netty provide for NettyHttpCommandCenter and HttpHeartbeatSender.

 NettyHttpCommandCenter: Based Netty, starts in the server mode, all of the caches by CommandHandlerProvider Handler object before starting, the built-in objects HttpServerHandler obtain decoded when the object request comes in, and processed according to the type of the request and call the corresponding Handler return

 HttpHeartbeatSender: httpclient client wants to use the dashboard sends Http request heartbeat

 In summary, sentinel-cluster-server-default module provides the following Handelr implementation, the dashboard to provide cluster information to and receive commands sent over from the dashboard.

file

 Wherein Fetch message beginning to read, to modify the system at the beginning of the Modify message.

VI. Cluster Management Interface

 Sentinel many reservation management interface for dynamic loading rule or configuration, and then updates the local state, where several management interface relates to cluster mode will be described. Prior to this, first introduced in the demo mode Nacos access for the configuration of the center.

 Further Nacos access involves two modules, sentinel-datasource-extension and sentinel-datasource-nacos. Extension module defines ReadableDataSource interface for reading data from a data source to return the configuration data SentinelProperty. Extension module provides an abstract class implements AbstractDataSource, achieved loadConfig method. This class introduces the Converter interface and DynamicSentinelProperty classes, interfaces Converter for converting the data structure of the data source is read SentienlProperty stored data format; DynameicSentinelProperty SentinelPorperty default class implementation, a plurality of such PropertyListener listeners can be added, in when add configLoad trigger method listener for the initial action listener, and when data is changed, one by one notification listener, call configLoad method listener to remind listeners updated. AbstractDataSource realized loadConfig method, which calls readSource method, read the original data from the data source, and calls Converter data conversion.

file

 Nacos realization module provides NacosDataSource, inherited from AbstractDataSource This, in order to access Nacos distribution center. NacosDataSource will apply during initialization on Nacos a configuration set, and add the listener, and then do it again loadConfig, again loaded from the distribution center to configure and update the value of the property and informs listeners on the configuration set. Listeners will be when the configuration is changed, calling on Nacos Convert memory processing, and updates the configuration set, at the same time inform listeners on the configuration set.

 From the above, it can be configured by using a change DynamicSentinelProperty listener on the dynamic configuration set, with the data eye monitor, so that the operation of the system accordingly. In fact, sentinel built most of the management interface is handled this way, as a cluster of related key management interfaces, are ending Manager. These structures have the same management interface as FlowRuleManager, which maintain internal configuration of one or more sources, and set up a listener on the configuration of the source, when configuring the source data changes, the configuration method calls updateValue source, data source configuration update and informs listeners.

  1. FlowRuleManager

     When explaining this sentinel-core modules have been introduced, mostly local storage limiting rule set SentinelProperty <List >. There FlowPropertyListener listeners on the rule set will be rebuilt when the rule is changed, loading rules.

  2. ParamFlowRuleManager

     With FlowRuleManager, mainly for hot parameter limiting rules management.

  3. ClusterClientConfigManager

     Cluster client configuration management, key management:

    1) cluster client configuration for the timeout time setting client, configured to set SentinelProperty And listeners ClientConfigPropertyListener. When the rule will be changed to update the client's request timeout

    2) the cluster server configuration information for ip and port information setting service terminal, arranged to set SentinelProperty And listeners ClientAssignPropertyListener. Will be sent when the rule change, update the local configuration and notifies the server node sends ServerChangeObserver observers had changed from the previous contents can be seen, DefaultClusterTokenClient observer for the interface will be disconnected when the server sends the information to change before the same link, and then concentric server node to establish a new link.

  4. ClusterServerConfigManager

     Cluster server configuration management, key management:

    1) transmission server cluster configuration, and for setting the idle time of the server port, configured to set SentinelProperty And listeners ServerGlobalTransportPropertyListener. When the rule will be changed to update the local configuration and informs the viewer ServerTransportConfigObserver configuration changed. We can see from the previous content, SentinelDefaultTokenServer for the viewer interface, will change when the server sends the information to stop their application, then restart.

    2) the cluster server configured global flow control, flow control for setting a global configuration items, comprising a sliding window size to achieve, the number of grid window, the maximum allowed by qps the like. Configuration set to SentinelProperty And listeners ServerGlobalFlowPropertyListener, resets the contents of these configurations when the rule is updated.

    3) the set of namespace server cluster configuration, the set of namespace for setting the cluster is configured to set SentinelProperty <Set > And listeners ServerNamespaceSetPropertyListener, will be removed when configuration changes occur namesapce old configuration, and reload the new namesapce configuration, including the corresponding full limitations of current GlobalRequestLimiter, the rule limiting the cluster, cluster rules limiting hotspots.

  5. ClusterFlowRuleManager

     Rules limiting cluster configuration management, key management:

    1) cluster configuration rules, rules for setting the cluster is configured to set SentinelProperty <List > And listeners FlowRulePropertyListener, will occur when the configuration changes, remove the cache configuration corresponding namespace, and rebuild the corresponding rules. For a new flowId, it is assigned a corresponding node ClusterMetricStatistics statistics.

  6. ClusterParamFlowRuleManager

     Hot rule limiting the cluster configuration management, with ClusterFlowRuleManager

  7. ClusterStateManager

     Cluster global state management, key management:

    1) The role configuration unit configured to set SentinelProperty And listeners ClusterStatePropertyListener, when the rule will be changed to adjust the role of the machine. Roles include: server, client and non-clustered mode. If the rule is non-trunked mode, or will stop the relevant customer terminal server; if the server mode is set, the service will start embedded mode, if the client before the client connection will be closed; if set for the client mode, it will start the client connection, if before the end of the service will be out of service.

 Several above can access the management interface, such as distribution center Nacos, to vary the configuration items by the distribution center, a management station.

file

Personal Public Number: ah camel

Guess you like

Origin www.cnblogs.com/cxyAtuo/p/11614749.html