public class BalanceManager extends Object
CLIENT_BALANCE_TABLE table.
Maintains elastic property to sync balance in between mysql and redis cluster.| Modifier and Type | Field and Description |
|---|---|
static String |
ACK_FREEZE_REQUEST |
static String |
BALANCE_FIELD
Redis cluster balance field constant key.
|
static String |
BALANCE_FREEZE_CHANNEL |
static String |
BALANCE_SYNC_CHANNEL |
static int |
BALANCE_TAKER_COUNT
Thread count for balance taking process
|
static String |
CLIENT_BALANCE_TABLE
Mysql table name where balance information can be found
|
static double |
DEFAULT_LEASE_CHUNK
default amount Balance Manager tries to store locally from Redis for an account when
@getLease is called the first time |
static String |
FREEZE_COMPLETE |
static String |
FREEZE_REQUEST |
static String |
LEASE_EMPTY_STATUS |
LinkedBlockingQueue<LeaseDTO> |
leaseDTOsToIncrBalanceQ
Queue to track the
LeaseDTOs which need to increase balance |
static String |
TOKENIZER |
static String |
UNFREEZE_COMPLETE |
static String |
UNFREEZE_REQUEST |
| Constructor and Description |
|---|
BalanceManager(String identifier,
cluster.ClusterObserver clusterObserver)
Create an instance of
BalanceManager. |
BalanceManager(String identifier,
String redisHost,
int redisPort,
String redisPass)
Create an instance of
BalanceManager. |
| Modifier and Type | Method and Description |
|---|---|
BalanceFreezer |
getBalanceFreezer() |
cluster.ClusterObserver |
getClusterObserver()
Getter for
clusterObserver |
String |
getIdentifier() |
double |
getLease(long accountId,
double leaseAmount)
Try to get a lease from this account balance.
|
double |
getPercentageToRelease() |
PubSubPublisher |
getPubSubPublisher() |
double |
getThresholdToReleaseFullAmount() |
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.
|
void |
processAccFreezeRequest(long accountId)
When a freeze request is received from mitigator this method will be called.
|
void |
processAccUnfreezeRequest(long accountId)
When an unfreeze request is received from mitigator this method will be called.
|
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.
|
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).
|
void |
returnLeaseOnRequest(long accountId)
In case the balance of an account gets exhausted, balance manager will broadcast an empty status to all BMs.
|
void |
setBalanceFreezer(BalanceFreezer balanceFreezer)
Setter for the balance freezer needed to freeze accounts
|
void |
setPercentageToRelease(double percentageToRelease)
Setter for the percentage which Balance Manager will release when
returnLeaseOnRequest(long) is called |
void |
setThresholdToReleaseFullAmount(double thresholdToReleaseFullAmount)
Setter to configure threshold under which Balance Manager will release the full available amount
|
public static final String BALANCE_SYNC_CHANNEL
public static final String BALANCE_FREEZE_CHANNEL
public static final String LEASE_EMPTY_STATUS
public static final String FREEZE_REQUEST
public static final String UNFREEZE_REQUEST
public static final String ACK_FREEZE_REQUEST
public static final String FREEZE_COMPLETE
public static final String UNFREEZE_COMPLETE
public static final String TOKENIZER
public static final double DEFAULT_LEASE_CHUNK
@getLease is called the first timepublic static final int BALANCE_TAKER_COUNT
public static final String CLIENT_BALANCE_TABLE
public static final String BALANCE_FIELD
public LinkedBlockingQueue<LeaseDTO> leaseDTOsToIncrBalanceQ
LeaseDTOs which need to increase balancepublic BalanceManager(String identifier, String redisHost, int redisPort, String redisPass)
BalanceManager.identifier - Balance Manager unique identifier. Can not contain these characters in the string :
"-", "+", "*", ":", "\", "$", "%", "(", ")". Can not be null or empty.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 clusterpublic BalanceManager(String identifier, cluster.ClusterObserver clusterObserver)
BalanceManager.identifier - Balance Manager unique identifier. Can not contain these characters in the string : "-", "+", "*", ":", "\", "$", "%", "(", ")". Can not be null or empty.clusterObserver - Cluster Observer of which BalanceManager will be part of.public double readFromDb(long accountId)
accountId - Account ID for which balance information will be loaded into redis.public double getLease(long accountId,
double leaseAmount)
accountId - The account id lease needed fromleaseAmount - The amount to be added to the leasepublic void returnLease(long accountId,
double leaseAmount)
accountId - Account id the lease should be returned toleaseAmount - remaining lease amount. make sure that it's +vepublic void returnLeaseOnRequest(long accountId)
accountId - For which account the balance is exhausted/empty.public void processAccFreezeRequest(long accountId)
accountId - account id for which Freeze request is being processed.public void processAccUnfreezeRequest(long accountId)
accountId - the account id which has been frozenpublic void onProcessShutdown()
public String getIdentifier()
public cluster.ClusterObserver getClusterObserver()
clusterObserverClusterObserver instance that was used to cache balance information.public double getThresholdToReleaseFullAmount()
public void setThresholdToReleaseFullAmount(double thresholdToReleaseFullAmount)
thresholdToReleaseFullAmount - threshold to set for full amount releasepublic double getPercentageToRelease()
returnLeaseOnRequest(long) is calledpublic void setPercentageToRelease(double percentageToRelease)
returnLeaseOnRequest(long) is calledpercentageToRelease - balance percentage to releasepublic PubSubPublisher getPubSubPublisher()
public BalanceFreezer getBalanceFreezer()
public void setBalanceFreezer(BalanceFreezer balanceFreezer)
balanceFreezer - reference of BalanceFreezerCopyright © 2021. All rights reserved.