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_FREEZE_CHANNEL |
static String |
BALANCE_SYNC_CHANNEL |
static String |
CLIENT_BALANCE_TABLE
Mysql table name where balance information can be found
|
static String |
FREEZE_COMPLETE |
static String |
FREEZE_REQUEST |
static String |
LEASE_EMPTY_STATUS |
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 |
double |
getDefaultLeaseChunkAmount() |
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 |
setDefaultLeaseChunkAmount(double defaultLeaseChunkAmount) |
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 String CLIENT_BALANCE_TABLE
public 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 - public void processAccUnfreezeRequest(long accountID)
accountID - public void onProcessShutdown()
public String getIdentifier()
public cluster.ClusterObserver getClusterObserver()
clusterObserverClusterObserver instance that was used to cache balance information.public double getDefaultLeaseChunkAmount()
public void setDefaultLeaseChunkAmount(double defaultLeaseChunkAmount)
defaultLeaseChunkAmount - sets the default lease amount balance manager takes from redispublic double getThresholdToReleaseFullAmount()
public void setThresholdToReleaseFullAmount(double thresholdToReleaseFullAmount)
public double getPercentageToRelease()
returnLeaseOnRequest(long) is calledpublic void setPercentageToRelease(double percentageToRelease)
returnLeaseOnRequest(long) is calledpublic PubSubPublisher getPubSubPublisher()
public BalanceFreezer getBalanceFreezer()
public void setBalanceFreezer(BalanceFreezer balanceFreezer)
Copyright © 2021. All rights reserved.