Package com.reve.balancemanager.lease
Class LeaseDTO
java.lang.Object
com.reve.balancemanager.lease.LeaseDTO
- Direct Known Subclasses:
LeaseDTOImpl
-
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionabstract doubleaddLocalBalance(double extraLease) Extra amount left at the end of the call is added to the lease (make sure to provide +ve value).abstract intappendAttributes(byte[] data, int index) Append all the required attributes into the given byte array, so that it can be sent to the slave.abstract longabstract doubleabstract doubleprotected static voidstatic LeaseDTOabstract doublegetLease(double lease, LeaseResponse response, boolean partialEnabled) Try to get a lease from this account balance.abstract longGet the timestamp in millis when last time this instance was synced with slaved.protected abstract doubleincreaseRedis(double amount) protected abstract doubleincreaseRedisold(double amount) Call hincrByFloat method inJedisClusterinstance.abstract voidinit(long accountId, double availableLocalBalance, BalanceManager balanceManager) This initialization is useful when take-over is happening.abstract voidinit(long accountId, BalanceManager balanceManager) Initialize aLeaseDTOinstance for the given account idprotected abstract voidabstract booleanisFrozen()abstract voidonProcessShutdown(boolean returnBalance) Do all the task required to do while the application is shutting down.abstract doublereturnAvailableBalancePercentage(double percentage) A percentage of available balance for this account will be returned to redis cluster when this method is called.abstract voidsetFrozen(boolean frozen) Setter for freeze/unfreeze state of an accountabstract voidsetLocalBalanceSyncTime(long localBalanceSyncTime) Set the timestamp in millis when last time this instance was synced with slaved.abstract booleanCheck and assign values in #localBalanceSyncTime.abstract voidAfter completing account freeze request we must set available redis amount to 0.abstract voidupdateInformation(byte[] data, int offset, int length) We have received information from master, Now we will update thisLeaseDTOwith this latest attributes.
-
Constructor Details
-
LeaseDTO
public LeaseDTO()
-
-
Method Details
-
init
Initialize aLeaseDTOinstance for the given account id- Parameters:
accountId- Account id for balance informationbalanceManager- ParentBalanceManagerinstance from where thisLeaseDTOinstance is being maintained.
-
init
public abstract void init(long accountId, double availableLocalBalance, BalanceManager balanceManager) 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.- 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- 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
- Throws:
Exception
-
increaseRedisold
Call hincrByFloat method inJedisClusterinstance. with given amount.- Parameters:
amount- this amount will be increased in redis- Returns:
- the remaining amount in redis
- Throws:
Exception
-
increaseRedis
- Throws:
Exception
-
addLocalBalance
public abstract 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.- Parameters:
extraLease- remaining lease amount. make sure that it's +ve- Returns:
- the available local balance after the operation is done.
-
returnAvailableBalancePercentage
public abstract double returnAvailableBalancePercentage(double percentage) A percentage of available balance for this account will be returned to redis cluster when this method is called.- 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 abstract 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. -
getAccountId
public abstract long getAccountId()- Returns:
- The accountId for which lease is taken/calculated
-
getAvailableLocalBalance
public abstract double getAvailableLocalBalance()- Returns:
- the amount available in local balance
-
getAvailableRedisBalance
public abstract double getAvailableRedisBalance()- Returns:
- the amount available in redis
-
setFrozen
public abstract void setFrozen(boolean frozen) Setter for freeze/unfreeze state of an account- Parameters:
frozen- Boolean to freeze or unfreeze account
-
isFrozen
public abstract boolean isFrozen() -
syncBalance
public abstract boolean syncBalance()Check and assign values in #localBalanceSyncTime.- Returns:
- true, if balance sync is required for this
LeaseDTO. Otherwise, return false
-
getLocalBalanceSyncTime
public abstract long getLocalBalanceSyncTime()Get the timestamp in millis when last time this instance was synced with slaved.- Returns:
- the timestamp in millis when last time this instance was synced with slaved.
-
setLocalBalanceSyncTime
public abstract void setLocalBalanceSyncTime(long localBalanceSyncTime) Set the timestamp in millis when last time this instance was synced with slaved.- Parameters:
localBalanceSyncTime- timestamp in milliseconds to be set
-
appendAttributes
public abstract 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- 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 abstract void updateInformation(byte[] data, int offset, int length) We have received information from master, Now we will update thisLeaseDTOwith this latest attributes.- 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 abstract void onProcessShutdown(boolean returnBalance) Do all the task required to do while the application is shutting down.- 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.
-
getInstance
-
getClassReference
protected static void getClassReference()
-