Class QoSHandler

  • All Implemented Interfaces:
    org.swisspush.gateleen.core.logging.LoggableResource

    public class QoSHandler
    extends java.lang.Object
    implements org.swisspush.gateleen.core.logging.LoggableResource
    QoS Handler.
         {
            "config":{
                "percentile":75,
                "quorum":40,
                "period":5,
                "minSampleCount" : 1000,
                "minSentinelCount" : 5
            },
            "sentinels":{
                "sentinelA":{
                    "percentile":50
                },
                "sentinelB":{},
                "sentinelC":{},
                "sentinelD":{}
            },
            "rules":{
                "/test/myapi1/v1/.*":{
                    "reject":1.2,
                    "warn":0.5
                },
                "/test/myapi2/v1/.*":{
                    "reject":0.3
                }
            }
        }
     

    The config section defines the global settings of the QoS.
    percentile: Indicates which percentile value from the metrics will be used (eg. 50, 75, 95, 98, 999 or 99)
    quorum: Percentage of the the sentinels which have to be over the calculated threshold to trigger the given rule.
    period: The period (in seconds) after which a new calculation is triggered. If a rule is set to reject requests, it will reject requests until the next period.
    minSampleCount: The min. count of the samples a sentinel has to provide to be regarded for the QoS calculation.
    minSentinelCount:The min count of sentinels which have to be available to perform a QoS calculation. A sentinel is only available if it corresponds to the minSampleCount rule.

    The sentinels section defines which metrics (defined in the routing rules) will be used as sentinels. To determine the load, the lowest measured percentile value will be preserved for each sentinel and put in relation to the current percentile value. This calculated ratio is later used to check if a rule needs some actions or not. You can override the taken percentile value for a specific sentinel by setting the attribute percentile.

    The rules section defines the rules for the QoS. Each rule is based on a pattern like the routing rules.
    The possible attributes are:
    reject: The ratio (eg. 1.3 means that *quorum* % of all sentinels must have an even or greater current ratio) which defines when a rule rejects the given request.
    warn: The ratio which defines when a rule writes a warning in the log without rejecting the given request.

    You can combine warn and reject

    Author:
    https://github.com/ljucam [Mario Ljuca]
    • Field Summary

      Fields 
      Modifier and Type Field Description
      protected static java.lang.String REJECT_ACTION  
      protected static java.lang.String WARN_ACTION  
    • Constructor Summary

      Constructors 
      Constructor Description
      QoSHandler​(io.vertx.core.Vertx vertx, org.swisspush.gateleen.core.storage.ResourceStorage storage, java.lang.String qosSettingsPath, java.util.Map<java.lang.String,​java.lang.Object> properties, java.lang.String prefix)
      Creates a new instance of the QoSHandler.
    • Method Summary

      All Methods Instance Methods Concrete Methods 
      Modifier and Type Method Description
      protected boolean actionNecessary​(java.lang.Double ratio, double thresholdSentinelRatio)
      Takes the ratio of the rule and compares if the calculated sentinel ratio from the desc.
      protected void cancelTimer()
      Cancels the timer.
      protected QoSConfig createQoSConfig​(io.vertx.core.json.JsonObject qosSettings)
      Creates the QoS config object from the given JsonObject and returns it.
      protected java.util.List<QoSSentinel> createQoSSentinels​(io.vertx.core.json.JsonObject qosSettings)
      Creates the QoS sentinel objects from the given JsonObject and returns them in a list.
      void enableResourceLogging​(boolean resourceLoggingEnabled)  
      protected void evaluateQoSActions()
      Calculates the threshold and determines if any action for each rule has to be applied.
      QoSSentinel getOldSentinel​(java.lang.String sentinelName)
      We try to retrive the old sentinel (if available).
      protected java.util.List<QoSRule> getQosRules()
      Returns a list of the QoS rules.
      protected java.util.List<QoSSentinel> getQosSentinels()
      Gets a list of all sentinels.
      boolean handle​(io.vertx.core.http.HttpServerRequest request)
      Processes the request if it’s a request concerning the updates of the QoS settings or if it’s a request affected by the QoS rules.
      protected io.vertx.core.json.JsonObject parseQoSSettings​(io.vertx.core.buffer.Buffer buffer)
      Tries to parse the QoS settings.
      protected void setGlobalQoSConfig​(QoSConfig globalQoSConfig)
      Sets the global configuration for the QoS.
      protected void setMBeanServer​(javax.management.MBeanServer mbeanServer)
      Sets the mbean server.
      protected void setQosRules​(java.util.List<QoSRule> qosRules)
      Sets the QoS rules.
      protected void setQosSentinels​(java.util.List<QoSSentinel> qosSentinels)
      Sets a list of all sentinels.
      • Methods inherited from class java.lang.Object

        clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
    • Constructor Detail

      • QoSHandler

        public QoSHandler​(io.vertx.core.Vertx vertx,
                          org.swisspush.gateleen.core.storage.ResourceStorage storage,
                          java.lang.String qosSettingsPath,
                          java.util.Map<java.lang.String,​java.lang.Object> properties,
                          java.lang.String prefix)
        Creates a new instance of the QoSHandler.
        Parameters:
        vertx - vertx reference
        storage - storage reference
        qosSettingsPath - the url path to the QoS rules
        properties - the properties
        prefix - the prefix for the server
    • Method Detail

      • enableResourceLogging

        public void enableResourceLogging​(boolean resourceLoggingEnabled)
        Specified by:
        enableResourceLogging in interface org.swisspush.gateleen.core.logging.LoggableResource
      • setMBeanServer

        protected void setMBeanServer​(javax.management.MBeanServer mbeanServer)
        Sets the mbean server. This method is usefull for mocking in tests.
        Parameters:
        mbeanServer - a mbean server
      • handle

        public boolean handle​(io.vertx.core.http.HttpServerRequest request)
        Processes the request if it’s a request concerning the updates of the QoS settings or if it’s a request affected by the QoS rules. In either case the return value will be true otherwise false.
        Parameters:
        request -
        Returns:
        true if request is QoS update or affected by the rules, otherwise false.
      • createQoSConfig

        protected QoSConfig createQoSConfig​(io.vertx.core.json.JsonObject qosSettings)
        Creates the QoS config object from the given JsonObject and returns it.
        Parameters:
        qosSettings - json object
        Returns:
        the global QoS config object, null if the config section is not present.
      • createQoSSentinels

        protected java.util.List<QoSSentinel> createQoSSentinels​(io.vertx.core.json.JsonObject qosSettings)
        Creates the QoS sentinel objects from the given JsonObject and returns them in a list.
        Parameters:
        qosSettings - json object
        Returns:
        a list with the QoSSentinel objects, empty if the sentinel section is not present or empty.
      • getOldSentinel

        public QoSSentinel getOldSentinel​(java.lang.String sentinelName)
        We try to retrive the old sentinel (if available).
        Parameters:
        sentinelName - the name of the sentinel.
        Returns:
        old sentinel object or null if no sentinel was found
      • parseQoSSettings

        protected io.vertx.core.json.JsonObject parseQoSSettings​(io.vertx.core.buffer.Buffer buffer)
        Tries to parse the QoS settings.
        Parameters:
        buffer - buffer from the request
        Returns:
        a JsonObject containing the settings.
      • cancelTimer

        protected void cancelTimer()
        Cancels the timer.
        This method is protected to be used in tests where you wish to manually trigger the evaluation.
      • evaluateQoSActions

        protected void evaluateQoSActions()
        Calculates the threshold and determines if any action for each rule has to be applied.
      • actionNecessary

        protected boolean actionNecessary​(java.lang.Double ratio,
                                          double thresholdSentinelRatio)
        Takes the ratio of the rule and compares if the calculated sentinel ratio from the desc. sorted list at the index coresponding to the percentual value (quorum) of all sentinel api's is lower or even.
        Parameters:
        ratio -
        thresholdSentinelRatio -
        Returns:
        true if any action must be applied, otherwise false
      • setGlobalQoSConfig

        protected void setGlobalQoSConfig​(QoSConfig globalQoSConfig)
        Sets the global configuration for the QoS.
        Parameters:
        globalQoSConfig - the config object for QoS.
      • getQosRules

        protected java.util.List<QoSRule> getQosRules()
        Returns a list of the QoS rules.
        Returns:
        list of QoS rules.
      • setQosRules

        protected void setQosRules​(java.util.List<QoSRule> qosRules)
        Sets the QoS rules.
        Parameters:
        qosRules - a list of the QoS rule objects
      • setQosSentinels

        protected void setQosSentinels​(java.util.List<QoSSentinel> qosSentinels)
        Sets a list of all sentinels.
        Parameters:
        qosSentinels - list with sentinel objects
      • getQosSentinels

        protected java.util.List<QoSSentinel> getQosSentinels()
        Gets a list of all sentinels.
        Returns:
        list with sentinel objects