Package com.github.ambry.account
Class MySqlAccountService
- java.lang.Object
-
- com.github.ambry.account.MySqlAccountService
-
- All Implemented Interfaces:
com.github.ambry.account.AccountService
,java.io.Closeable
,java.lang.AutoCloseable
public class MySqlAccountService extends java.lang.Object
An implementation ofAccountService
that employs MySql database as its underlying storage.
-
-
Field Summary
Fields Modifier and Type Field Description protected java.util.concurrent.atomic.AtomicReference<com.github.ambry.account.AccountInfoMap>
accountInfoMapRef
protected AccountServiceMetrics
accountServiceMetrics
protected java.util.concurrent.CopyOnWriteArraySet<java.util.function.Consumer<java.util.Collection<com.github.ambry.account.Account>>>
accountUpdateConsumers
protected java.util.concurrent.locks.ReentrantLock
lock
static java.lang.String
SEPARATOR
-
Constructor Summary
Constructors Constructor Description MySqlAccountService(AccountServiceMetrics accountServiceMetrics, com.github.ambry.config.MySqlAccountServiceConfig config, MySqlAccountStoreFactory mySqlAccountStoreFactory)
-
Method Summary
All Methods Static Methods Instance Methods Concrete Methods Modifier and Type Method Description boolean
addAccountUpdateConsumer(java.util.function.Consumer<java.util.Collection<com.github.ambry.account.Account>> accountUpdateConsumer)
protected void
checkOpen()
Ensures the account service is ready to process requests.void
close()
com.github.ambry.account.Account
getAccountById(short accountId)
com.github.ambry.account.Account
getAccountByName(java.lang.String accountName)
java.util.Collection<com.github.ambry.account.Account>
getAllAccounts()
com.github.ambry.account.Container
getContainerById(short accountId, java.lang.Short containerId)
Gets theContainer
by its Id and parentAccount
Id by looking up in in-memory cache.com.github.ambry.account.Container
getContainerByName(java.lang.String accountName, java.lang.String containerName)
Gets theContainer
by its name and parentAccount
name by looking up in in-memory cache.int
getContainerCount()
int
getTimeInSecondsSinceLastSync()
protected void
notifyAccountUpdateConsumers(java.util.Collection<com.github.ambry.account.Account> updatedAccounts, boolean isCalledFromListener)
Logs and notifies account updateConsumer
s about any new account changes/creations.boolean
removeAccountUpdateConsumer(java.util.function.Consumer<java.util.Collection<com.github.ambry.account.Account>> accountUpdateConsumer)
void
selectInactiveContainersAndMarkInStore(com.github.ambry.server.StatsSnapshot statsSnapshot)
SelectsContainer
s to be marked as INACTIVE and marked in underlying account store.static com.github.ambry.account.AccountServiceException
translateSQLException(java.sql.SQLException e)
Translate aSQLException
to aAccountServiceException
.void
updateAccounts(java.util.Collection<com.github.ambry.account.Account> accounts)
java.util.Collection<com.github.ambry.account.Container>
updateContainers(java.lang.String accountName, java.util.Collection<com.github.ambry.account.Container> containers)
protected void
updateResolvedContainers(com.github.ambry.account.Account account, java.util.Collection<com.github.ambry.account.Container> resolvedContainers)
Update the containers that have been vetted as non-duplicates and populated with ids.
-
-
-
Field Detail
-
SEPARATOR
public static final java.lang.String SEPARATOR
- See Also:
- Constant Field Values
-
accountInfoMapRef
protected final java.util.concurrent.atomic.AtomicReference<com.github.ambry.account.AccountInfoMap> accountInfoMapRef
-
lock
protected final java.util.concurrent.locks.ReentrantLock lock
-
accountUpdateConsumers
protected final java.util.concurrent.CopyOnWriteArraySet<java.util.function.Consumer<java.util.Collection<com.github.ambry.account.Account>>> accountUpdateConsumers
-
accountServiceMetrics
protected final AccountServiceMetrics accountServiceMetrics
-
-
Constructor Detail
-
MySqlAccountService
public MySqlAccountService(AccountServiceMetrics accountServiceMetrics, com.github.ambry.config.MySqlAccountServiceConfig config, MySqlAccountStoreFactory mySqlAccountStoreFactory) throws java.sql.SQLException, java.io.IOException
- Throws:
java.sql.SQLException
java.io.IOException
-
-
Method Detail
-
getAccountById
public com.github.ambry.account.Account getAccountById(short accountId)
- Specified by:
getAccountById
in interfacecom.github.ambry.account.AccountService
-
getAccountByName
public com.github.ambry.account.Account getAccountByName(java.lang.String accountName)
- Specified by:
getAccountByName
in interfacecom.github.ambry.account.AccountService
-
updateAccounts
public void updateAccounts(java.util.Collection<com.github.ambry.account.Account> accounts) throws com.github.ambry.account.AccountServiceException
- Throws:
com.github.ambry.account.AccountServiceException
-
getAllAccounts
public java.util.Collection<com.github.ambry.account.Account> getAllAccounts()
- Specified by:
getAllAccounts
in interfacecom.github.ambry.account.AccountService
-
close
public void close() throws java.io.IOException
- Throws:
java.io.IOException
-
checkOpen
protected void checkOpen()
Ensures the account service is ready to process requests. Throws an unchecked exception (for exampleIllegalStateException
) if the account service is not ready to process requests.
-
updateContainers
public java.util.Collection<com.github.ambry.account.Container> updateContainers(java.lang.String accountName, java.util.Collection<com.github.ambry.account.Container> containers) throws com.github.ambry.account.AccountServiceException
- Specified by:
updateContainers
in interfacecom.github.ambry.account.AccountService
- Throws:
com.github.ambry.account.AccountServiceException
-
updateResolvedContainers
protected void updateResolvedContainers(com.github.ambry.account.Account account, java.util.Collection<com.github.ambry.account.Container> resolvedContainers) throws com.github.ambry.account.AccountServiceException
Update the containers that have been vetted as non-duplicates and populated with ids.- Parameters:
account
- the account owning the containers.resolvedContainers
- the resolved containers.- Throws:
com.github.ambry.account.AccountServiceException
-
getContainerByName
public com.github.ambry.account.Container getContainerByName(java.lang.String accountName, java.lang.String containerName) throws com.github.ambry.account.AccountServiceException
Gets theContainer
by its name and parentAccount
name by looking up in in-memory cache. If it is not present in in-memory cache, it queries from mysql db and updates the cache.- Parameters:
accountName
- the name of account which container belongs to.containerName
- the name of container to get.- Returns:
Container
if found in cache or mysql db. Else, returnsnull
.- Throws:
com.github.ambry.account.AccountServiceException
-
getContainerById
public com.github.ambry.account.Container getContainerById(short accountId, java.lang.Short containerId) throws com.github.ambry.account.AccountServiceException
Gets theContainer
by its Id and parentAccount
Id by looking up in in-memory cache. If it is not present in in-memory cache, it queries from mysql db and updates the cache.- Parameters:
accountId
- the name of account which container belongs to.containerId
- the id of container to get.- Returns:
Container
if found in cache or mysql db. Else, returnsnull
.- Throws:
com.github.ambry.account.AccountServiceException
-
getTimeInSecondsSinceLastSync
public int getTimeInSecondsSinceLastSync()
- Returns:
- the time in seconds since the last database sync / cache refresh
-
getContainerCount
public int getContainerCount()
- Returns:
- the total number of containers in all accounts.
-
translateSQLException
public static com.github.ambry.account.AccountServiceException translateSQLException(java.sql.SQLException e)
Translate aSQLException
to aAccountServiceException
.- Parameters:
e
- the input exception.- Returns:
- the corresponding
AccountServiceException
.
-
addAccountUpdateConsumer
public boolean addAccountUpdateConsumer(java.util.function.Consumer<java.util.Collection<com.github.ambry.account.Account>> accountUpdateConsumer)
- Specified by:
addAccountUpdateConsumer
in interfacecom.github.ambry.account.AccountService
-
removeAccountUpdateConsumer
public boolean removeAccountUpdateConsumer(java.util.function.Consumer<java.util.Collection<com.github.ambry.account.Account>> accountUpdateConsumer)
- Specified by:
removeAccountUpdateConsumer
in interfacecom.github.ambry.account.AccountService
-
notifyAccountUpdateConsumers
protected void notifyAccountUpdateConsumers(java.util.Collection<com.github.ambry.account.Account> updatedAccounts, boolean isCalledFromListener)
Logs and notifies account updateConsumer
s about any new account changes/creations.- Parameters:
updatedAccounts
- collection of updated accountsisCalledFromListener
-true
if the caller is the account update listener,false
otherwise.
-
selectInactiveContainersAndMarkInStore
public void selectInactiveContainersAndMarkInStore(com.github.ambry.server.StatsSnapshot statsSnapshot)
SelectsContainer
s to be marked as INACTIVE and marked in underlying account store.- Specified by:
selectInactiveContainersAndMarkInStore
in interfacecom.github.ambry.account.AccountService
-
-