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 String |
CLIENT_BALANCE_TABLE
MySQL table name where balance information can be found
|
static String |
FREEZE_COMPLETE |
static String |
FREEZE_REQUEST |
static double |
LEASE_CHUNK_STEP
Value to increase or decrease when adjusting lease chunk with demand
|
static String |
LEASE_EMPTY_STATUS |
static double |
MAXIMUM_LEASE_CHUNK
Maximum lease chunk Balance Manager tries to store locally from Redis for an account
when
@getLease is called |
static double |
MINIMUM_LEASE_CHUNK
Minimum lease chunk Balance Manager tries to store locally from Redis for an account
when
@getLease is called |
static long |
TIME_TO_DECREASE_LEASE_CHUNK
Time above which a lease request is received in Redis, we will decrease local balance chunk for a LeaseDTO
|
static long |
TIME_TO_INCREASE_LEASE_CHUNK
Time under which a lease request is received in Redis, we will increase local balance chunk for a LeaseDTO
|
static String |
TOKENIZER |
static String |
UNFREEZE_COMPLETE |
static String |
UNFREEZE_REQUEST |
static long |
WAIT_TIME_BEFORE_FULL_RELEASE
When a 100% (full) balance return request is made for an account, Balance manager will not process
any further full amount return request for this time
|
| Constructor and Description |
|---|
BalanceManager(String identifier,
com.reve.redisclusterconnector.cluster.ClusterObserver clusterObserver)
Create an instance of
BalanceManager. |
BalanceManager(String identifier,
String redisHost,
int redisPort,
String redisPass,
com.reve.redisclusterconnector.ssl.SSLConfiguration sslConfiguration)
Create an instance of
BalanceManager. |
| Modifier and Type | Method and Description |
|---|---|
void |
addOrgBilledAmount(long orgClientID,
double billedAmount)
Adds billed amount to total bill for corresponding account id
|
void |
addTerBilledAmount(long terClientID,
double billedAmount)
Adds billed amount to total billed amount for corresponding account id
|
com.reve.redisclusterconnector.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
|
void |
startMysqlBalanceUpdate(boolean start)
If anyone wants
BalanceManager to update mysql balance as well, then this method must be called with true
By default BalanceManager will not update mysql balance. |
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 MINIMUM_LEASE_CHUNK
@getLease is calledpublic static final double MAXIMUM_LEASE_CHUNK
@getLease is calledpublic static final double LEASE_CHUNK_STEP
public static final long TIME_TO_INCREASE_LEASE_CHUNK
public static final long TIME_TO_DECREASE_LEASE_CHUNK
public static final long WAIT_TIME_BEFORE_FULL_RELEASE
public static final String CLIENT_BALANCE_TABLE
public static final String BALANCE_FIELD
public BalanceManager(String identifier, String redisHost, int redisPort, String redisPass, com.reve.redisclusterconnector.ssl.SSLConfiguration sslConfiguration) throws Exception
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 clustersslConfiguration - if ssl/tls is configured in the redis cluster, then an instance of SSLConfiguration
must be passed, which will be used to connect to the redis server. If ssl is not configured
in the redis server, then this value should be nullExceptionpublic BalanceManager(String identifier, com.reve.redisclusterconnector.cluster.ClusterObserver clusterObserver) throws Exception
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.Exceptionpublic 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 needs to be unfrozenpublic void onProcessShutdown()
public String getIdentifier()
public com.reve.redisclusterconnector.cluster.ClusterObserver getClusterObserver()
clusterObserverClusterObserver instance that was used to cache balance information.public double getThresholdToReleaseFullAmount()
public void setThresholdToReleaseFullAmount(double thresholdToReleaseFullAmount)
thresholdToReleaseFullAmount - Amount to set as threshold to release full amountpublic double getPercentageToRelease()
returnLeaseOnRequest(long) is calledpublic void setPercentageToRelease(double percentageToRelease)
returnLeaseOnRequest(long) is calledpercentageToRelease - percentage tp release from available balancepublic PubSubPublisher getPubSubPublisher()
public void setBalanceFreezer(BalanceFreezer balanceFreezer)
balanceFreezer - Instance of balance freezer needed to freeze accountspublic void startMysqlBalanceUpdate(boolean start)
BalanceManager to update mysql balance as well, then this method must be called with true
By default BalanceManager will not update mysql balance.start - if mysql balance update is required then true. Otherwise false.public void addOrgBilledAmount(long orgClientID,
double billedAmount)
throws UnsupportedOperationException
orgClientID - originating client id for which bill should be addedbilledAmount - billed amount that should be addedUnsupportedOperationExceptionpublic void addTerBilledAmount(long terClientID,
double billedAmount)
throws UnsupportedOperationException
terClientID - terminating account id for which bill has happenedbilledAmount - billed amount that should be added in total billUnsupportedOperationExceptionCopyright © 2022. All rights reserved.