Class LeaseDTOImpl

java.lang.Object
com.reve.balancemanager.lease.LeaseDTO
com.reve.balancemanager.impl.LeaseDTOImpl
All Implemented Interfaces:
Serializable

public class LeaseDTOImpl extends LeaseDTO implements Serializable
This class is responsible for caching balance, taking advanced loan from mysql, giving lease when needed, syncing the balance information between mysql and redis cluster
See Also:
  • Constructor Details

    • LeaseDTOImpl

      public LeaseDTOImpl()
  • Method Details

    • init

      public void init(long accountId, BalanceManager balanceManager)
      Initialize a LeaseDTO instance for the given account id
      Specified by:
      init in class LeaseDTO
      Parameters:
      accountId - Account id for balance information
      balanceManager - Parent BalanceManager instance from where this LeaseDTO instance is being maintained.
    • init

      public void init(long accountId, double availableLocalBalance, BalanceManager balanceManager)
      This initialization is useful when take-over is happening. In that case, we have account id and available local balance of that account. So we will not load it into cluster this time.
      Specified by:
      init in class LeaseDTO
      Parameters:
      accountId - The account ID for which this instance belongs to.
      availableLocalBalance - At the time take-over what was the available local balance.
      balanceManager - Parent BalanceManager instance from where this LeaseDTO instance is being maintained.
    • getLease

      public double getLease(double lease, LeaseResponse response, boolean partialEnabled)
      Try to get a lease from this account balance. If possible take that lease of given amount. If full amount not possible then try to take lease of possible amount. If that is even not possible then return with zero lease
      Specified by:
      getLease in class LeaseDTO
      Parameters:
      lease - The amount to be added to the lease
      response - The response value will be updated into this instance. This instance must be passed from the caller, and values will be updated here.
      partialEnabled - if caller can be happy with partial lease, then this value must be true.
      Returns:
      the exact amount that lease was possible for. 0 when no balance available for this client
    • insertBMidIntoRedis

      protected void insertBMidIntoRedis() throws Exception
      insert balanceManager identifier into redis BalanceManager.BM_ID_FIELD field via Lua script
      Specified by:
      insertBMidIntoRedis in class LeaseDTO
      Throws:
      Exception
    • increaseRedisold

      @Deprecated protected double increaseRedisold(double amount) throws Exception
      Deprecated.
      Call hincrByFloat method in JedisCluster instance. with given amount.
      Specified by:
      increaseRedisold in class LeaseDTO
      Parameters:
      amount - this amount will be increased in redis
      Returns:
      the remaining amount in redis
      Throws:
      Exception
    • increaseRedis

      protected double increaseRedis(double amount) throws Exception
      use lua script in JedisCluster instance. with given amount.
      Specified by:
      increaseRedis in class LeaseDTO
      Parameters:
      amount - this amount will be increased in redis
      Returns:
      the remaining amount in redis
      Throws:
      Exception
    • addLocalBalance

      public double addLocalBalance(double extraLease)
      Extra amount left at the end of the operation is added to the available local balance. (make sure to provide +ve value). The optional total bill, if present, is also queued to be updated in the db at next update cycle. local available balance will accumulate gradually but lease-request-gap is getter than threshold e.g. termination accountID. So we must return some value to redis and decrease the local balance.
      Specified by:
      addLocalBalance in class LeaseDTO
      Parameters:
      extraLease - remaining lease amount. make sure that it's +ve
      Returns:
      the available local balance after the operation is done.
    • returnAvailableBalancePercentage

      public double returnAvailableBalancePercentage(double percentage)
      A percentage of available balance for this account will be returned to redis cluster when this method is called.
      Specified by:
      returnAvailableBalancePercentage in class LeaseDTO
      Parameters:
      percentage - What percentage of the available will be returned. For a full return percentage will be 100
      Returns:
      the available local balance after the operation is done.
    • updateAvailableRedisAmount

      public void updateAvailableRedisAmount()
      After completing account freeze request we must set available redis amount to 0. So that, balance mismatch detection method works properly. This method provides us with that facility.
      Specified by:
      updateAvailableRedisAmount in class LeaseDTO
    • getAccountId

      public long getAccountId()
      Specified by:
      getAccountId in class LeaseDTO
      Returns:
      The accountId for which lease is taken/calculated
    • getAvailableLocalBalance

      public double getAvailableLocalBalance()
      Specified by:
      getAvailableLocalBalance in class LeaseDTO
      Returns:
      the amount available in local balance
    • getAvailableRedisBalance

      public double getAvailableRedisBalance()
      Specified by:
      getAvailableRedisBalance in class LeaseDTO
      Returns:
      the amount available in redis balance
    • isFrozen

      public boolean isFrozen()
      Specified by:
      isFrozen in class LeaseDTO
    • setFrozen

      public void setFrozen(boolean frozen)
      Setter for freeze/unfreeze state of an account
      Specified by:
      setFrozen in class LeaseDTO
      Parameters:
      frozen - Boolean to freeze or unfreeze account
    • toString

      public String toString()
      For debugging purpose
      Overrides:
      toString in class Object
      Returns:
      String value describing this instance.
    • syncBalance

      public boolean syncBalance()
      Check and assign values in localBalanceSyncTime.
      Specified by:
      syncBalance in class LeaseDTO
      Returns:
      true, if balance sync is required for this LeaseDTO. Otherwise, return false
    • getLocalBalanceSyncTime

      public long getLocalBalanceSyncTime()
      Get the timestamp in millis when last time this instance was synced with slaved.
      Specified by:
      getLocalBalanceSyncTime in class LeaseDTO
      Returns:
      the timestamp in millis when last time this instance was synced with slaved.
    • setLocalBalanceSyncTime

      public void setLocalBalanceSyncTime(long localBalanceSyncTime)
      Set the timestamp in millis when last time this instance was synced with slaved.
      Specified by:
      setLocalBalanceSyncTime in class LeaseDTO
      Parameters:
      localBalanceSyncTime - timestamp in milliseconds to be set
    • appendAttributes

      public int appendAttributes(byte[] data, int index)
      Append all the required attributes into the given byte array, so that it can be sent to the slave. Sequence of the items to be added will be same as the sequence of the definition of the constants in the class LeaseDTOConstants
      Specified by:
      appendAttributes in class LeaseDTO
      Parameters:
      data - byte array where this LeaseDTO information will be appended.
      index - starting index of the byte array where this instance should be appended.
      Returns:
      the new index after appending this instance.
    • updateInformation

      public void updateInformation(byte[] data, int offset, int length)
      We have received information from master, Now we will update this LeaseDTO with this latest attributes.
      Specified by:
      updateInformation in class LeaseDTO
      Parameters:
      data - byte array received from master.
      offset - starting index of the byte array.
      length - length of the byte array that contains valid information.
    • getLastLeaseServedTime

      public long getLastLeaseServedTime()
      Specified by:
      getLastLeaseServedTime in class LeaseDTO
      Returns:
      last lease serve time. Which denotes when last lease request is served for this leaseDTO.
    • isValidAccountID

      public boolean isValidAccountID()
      It is getter method of validAccountID property.
      Specified by:
      isValidAccountID in class LeaseDTO
      Returns:
      true, if accountID is known to mysql, false otherwise.
    • setAccountIDValidationStatus

      public void setAccountIDValidationStatus(boolean status)
      It is setter method of validAccountID property.
      Specified by:
      setAccountIDValidationStatus in class LeaseDTO
      Parameters:
      status - true, if accountID is known to mysql, false otherwise.
    • onProcessShutdown

      public void onProcessShutdown(boolean returnBalance)
      Do all the task required to do while the application is shutting down.
      Specified by:
      onProcessShutdown in class LeaseDTO
      Parameters:
      returnBalance - If we want all the local balance to return in redis cluster then this value should be true, otherwise this value should be false. If the application is running in standalone or running as master this value should be true. It is considered that if the application is running in redundancy mode only master should return balance in redis cluster and slave should not.