Package com.reve.balancemanager.impl
Class LeaseDTOImpl
java.lang.Object
com.reve.balancemanager.lease.LeaseDTO
com.reve.balancemanager.impl.LeaseDTOImpl
- All Implemented Interfaces:
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 Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptiondoubleaddLocalBalance(double extraLease) Extra amount left at the end of the call is added to the lease (make sure to provide +ve value).intappendAttributes(byte[] data, int index) Append all the required attributes into the given byte array, so that it can be sent to the slave.longdoubledoubledoublegetLease(double lease, LeaseResponse response, boolean partialEnabled) Try to get a lease from this account balance.longGet the timestamp in millis when last time this instance was synced with slaved.protected doubleincreaseRedis(double amount) use lua script inJedisClusterinstance.protected doubleincreaseRedisold(double amount) Call hincrByFloat method inJedisClusterinstance.voidinit(long accountId, double availableLocalBalance, BalanceManager balanceManager) This initialization is useful when take-over is happening.voidinit(long accountId, BalanceManager balanceManager) Initialize aLeaseDTOinstance for the given account idprotected voidinsertbalanceManageridentifier into redisBalanceManager.BM_ID_FIELDfield via Lua scriptbooleanisFrozen()voidonProcessShutdown(boolean returnBalance) Do all the task required to do while the application is shutting down.doublereturnAvailableBalancePercentage(double percentage) A percentage of available balance for this account will be returned to redis cluster when this method is called.voidsetFrozen(boolean frozen) Setter for freeze/unfreeze state of an accountvoidsetLocalBalanceSyncTime(long localBalanceSyncTime) Set the timestamp in millis when last time this instance was synced with slaved.booleanCheck and assign values inlocalBalanceSyncTime.toString()For debugging purposevoidAfter completing account freeze request we must set available redis amount to 0.voidupdateInformation(byte[] data, int offset, int length) We have received information from master, Now we will update thisLeaseDTOwith this latest attributes.Methods inherited from class com.reve.balancemanager.lease.LeaseDTO
getClassReference, getInstance
-
Constructor Details
-
LeaseDTOImpl
public LeaseDTOImpl()
-
-
Method Details
-
init
Initialize aLeaseDTOinstance for the given account id- Specified by:
initin classLeaseDTO- Parameters:
accountId- Account id for balance informationbalanceManager- ParentBalanceManagerinstance from where thisLeaseDTOinstance is being maintained.
-
init
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:
initin classLeaseDTO- Parameters:
accountId- The account ID for which this instance belongs to.availableLocalBalance- At the time take-over what was the available local balance.balanceManager- ParentBalanceManagerinstance from where thisLeaseDTOinstance is being maintained.
-
getLease
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:
getLeasein classLeaseDTO- Parameters:
lease- The amount to be added to the leaseresponse- 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
insertbalanceManageridentifier into redisBalanceManager.BM_ID_FIELDfield via Lua script- Specified by:
insertBMidIntoRedisin classLeaseDTO- Throws:
Exception
-
increaseRedisold
Call hincrByFloat method inJedisClusterinstance. with given amount.- Specified by:
increaseRedisoldin classLeaseDTO- Parameters:
amount- this amount will be increased in redis- Returns:
- the remaining amount in redis
- Throws:
Exception
-
increaseRedis
use lua script inJedisClusterinstance. with given amount.- Specified by:
increaseRedisin classLeaseDTO- 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 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.- Specified by:
addLocalBalancein classLeaseDTO- 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:
returnAvailableBalancePercentagein classLeaseDTO- 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:
updateAvailableRedisAmountin classLeaseDTO
-
getAccountId
public long getAccountId()- Specified by:
getAccountIdin classLeaseDTO- Returns:
- The accountId for which lease is taken/calculated
-
getAvailableLocalBalance
public double getAvailableLocalBalance()- Specified by:
getAvailableLocalBalancein classLeaseDTO- Returns:
- the amount available in local balance
-
getAvailableRedisBalance
public double getAvailableRedisBalance()- Specified by:
getAvailableRedisBalancein classLeaseDTO- Returns:
- the amount available in redis balance
-
isFrozen
public boolean isFrozen() -
setFrozen
public void setFrozen(boolean frozen) Setter for freeze/unfreeze state of an account -
toString
For debugging purpose -
syncBalance
public boolean syncBalance()Check and assign values inlocalBalanceSyncTime.- Specified by:
syncBalancein classLeaseDTO- 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:
getLocalBalanceSyncTimein classLeaseDTO- 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:
setLocalBalanceSyncTimein classLeaseDTO- 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 classLeaseDTOConstants- Specified by:
appendAttributesin classLeaseDTO- Parameters:
data- byte array where thisLeaseDTOinformation 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 thisLeaseDTOwith this latest attributes.- Specified by:
updateInformationin classLeaseDTO- Parameters:
data- byte array received from master.offset- starting index of the byte array.length- length of the byte array that contains valid information.
-
onProcessShutdown
public void onProcessShutdown(boolean returnBalance) Do all the task required to do while the application is shutting down.- Specified by:
onProcessShutdownin classLeaseDTO- 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.
-