Package balance

Class BalanceManager

java.lang.Object
balance.BalanceManager

public class BalanceManager extends Object
Manages balance information for all clients presents in mysql CLIENT_BALANCE_TABLE table. Maintains elastic property to sync balance in between mysql and redis cluster.
  • Field Details

  • Constructor Details

    • BalanceManager

      public BalanceManager(String redisHost, int redisPort, String redisPass)
      Create an instance of BalanceManager. This instance should be singleton.
      Parameters:
      redisHost - Redis cluster server ip address where balance information will be cached. This cluster node must be a master node in cluster.
      redisPort - Redis cluster server port where balance information will be cached.
      redisPass - Auth Password for redis cluster
  • Method Details

    • readFromDb

      public double readFromDb(long accountId)
      If the given accountId is not present in redis then it will be loaded from mysql database and be saved in redis cluster.
      Parameters:
      accountId - Account ID for which balance information will be loaded into redis.
      Returns:
      Total available balance in mysql that can be used in calling or other purpose. = (cbBalance + cbCreditLimit - cbThresholdValue)
    • getLease

      public double getLease(long accountId, double leaseAmount)
      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
      Parameters:
      accountId - The account id lease needed from
      leaseAmount - The amount to be added to the lease
      Returns:
      the exact amount that lease was possible for. 0 when no balance available for this client
    • returnLease

      public void returnLease(long accountId, double leaseAmount)
      Extra amount left at the end of the call is added to the lease (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.
      Parameters:
      accountId - Account id the lease should be returned to
      leaseAmount - remaining lease amount. make sure that it's +ve
    • getLeasesFromOtherBMs

      public void getLeasesFromOtherBMs(long accountID)
    • onProcessShutdown

      public void onProcessShutdown()
      We have taken advanced amount from redis cluster, so When system is getting shut down, all the available cached balance should be returned to redis, otherwise some money will be lost. and calculation mismatch will happen between redis cluster and mysql. This method should be called when the process is going to shutdown.
    • getClusterObserver

      public cluster.ClusterObserver getClusterObserver()
      Getter for clusterObserver
      Returns:
      The ClusterObserver instance that was used to cache balance information.
    • getDefaultLeaseChunkAmount

      public double getDefaultLeaseChunkAmount()
      Returns:
      the default lease amount balance manager takes from redis
    • setDefaultLeaseChunkAmount

      public void setDefaultLeaseChunkAmount(double defaultLeaseChunkAmount)
      Parameters:
      defaultLeaseChunkAmount - sets the default lease amount balance manager takes from redis
    • getThresholdToReleaseFullAmount

      public double getThresholdToReleaseFullAmount()
    • setThresholdToReleaseFullAmount

      public void setThresholdToReleaseFullAmount(double thresholdToReleaseFullAmount)
    • getPercentageToRelease

      public double getPercentageToRelease()
    • setPercentageToRelease

      public void setPercentageToRelease(double percentageToRelease)
    • getPubSubPublisher

      public PubSubPublisher getPubSubPublisher()