Class MySqlAccountService

  • All Implemented Interfaces:
    com.github.ambry.account.AccountService, java.io.Closeable, java.lang.AutoCloseable

    public class MySqlAccountService
    extends java.lang.Object
    An implementation of AccountService 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 com.github.ambry.commons.TopicListener<java.lang.String> changeTopicListener  
      protected java.util.concurrent.locks.ReentrantLock lock  
      protected com.github.ambry.commons.Notifier<java.lang.String> notifier  
      static java.lang.String SEPARATOR  
    • 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 the Container by its Id and parent Account Id by looking up in in-memory cache.
      com.github.ambry.account.Container getContainerByName​(java.lang.String accountName, java.lang.String containerName)
      Gets the Container by its name and parent Account name by looking up in in-memory cache.
      int getContainerCount()  
      int getTimeInSecondsSinceLastSync()  
      protected void maybeSubscribeChangeTopic​(boolean reportNull)  
      protected void maybeUnsubscribeChangeTopic()  
      protected void notifyAccountUpdateConsumers​(java.util.Collection<com.github.ambry.account.Account> updatedAccounts, boolean isCalledFromListener)
      Logs and notifies account update Consumers about any new account changes/creations.
      protected void onAccountChangeMessage​(java.lang.String topic, java.lang.String message)
      To be used to subscribe to a Notifier topic.
      protected void publishChangeNotice()  
      boolean removeAccountUpdateConsumer​(java.util.function.Consumer<java.util.Collection<com.github.ambry.account.Account>> accountUpdateConsumer)  
      void selectInactiveContainersAndMarkInStore​(com.github.ambry.server.StatsSnapshot statsSnapshot)
      Selects Containers to be marked as INACTIVE and marked in underlying account store.
      static com.github.ambry.account.AccountServiceException translateSQLException​(java.sql.SQLException e)
      Translate a SQLException to a AccountServiceException.
      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.
      • Methods inherited from class java.lang.Object

        clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
      • Methods inherited from interface com.github.ambry.account.AccountService

        getContainersByStatus
    • Field Detail

      • 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
      • notifier

        protected final com.github.ambry.commons.Notifier<java.lang.String> notifier
      • changeTopicListener

        protected final com.github.ambry.commons.TopicListener<java.lang.String> changeTopicListener
    • Constructor Detail

      • MySqlAccountService

        public MySqlAccountService​(AccountServiceMetrics accountServiceMetrics,
                                   com.github.ambry.config.MySqlAccountServiceConfig config,
                                   MySqlAccountStoreFactory mySqlAccountStoreFactory,
                                   com.github.ambry.commons.Notifier<java.lang.String> notifier)
                            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 interface com.github.ambry.account.AccountService
      • getAccountByName

        public com.github.ambry.account.Account getAccountByName​(java.lang.String accountName)
        Specified by:
        getAccountByName in interface com.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 interface com.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 example IllegalStateException) if the account service is not ready to process requests.
      • publishChangeNotice

        protected void publishChangeNotice()
      • onAccountChangeMessage

        protected void onAccountChangeMessage​(java.lang.String topic,
                                              java.lang.String message)
        To be used to subscribe to a Notifier topic. Upon receiving a change message, it will check for any account updates.
        Parameters:
        topic - The topic.
        message - The message for the topic.
      • 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 interface com.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 the Container by its name and parent Account 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, returns null.
        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 the Container by its Id and parent Account 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, returns null.
        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 a SQLException to a AccountServiceException.
        Parameters:
        e - the input exception.
        Returns:
        the corresponding AccountServiceException.
      • maybeSubscribeChangeTopic

        protected void maybeSubscribeChangeTopic​(boolean reportNull)
      • maybeUnsubscribeChangeTopic

        protected void maybeUnsubscribeChangeTopic()
      • addAccountUpdateConsumer

        public boolean addAccountUpdateConsumer​(java.util.function.Consumer<java.util.Collection<com.github.ambry.account.Account>> accountUpdateConsumer)
        Specified by:
        addAccountUpdateConsumer in interface com.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 interface com.github.ambry.account.AccountService
      • notifyAccountUpdateConsumers

        protected void notifyAccountUpdateConsumers​(java.util.Collection<com.github.ambry.account.Account> updatedAccounts,
                                                    boolean isCalledFromListener)
        Logs and notifies account update Consumers about any new account changes/creations.
        Parameters:
        updatedAccounts - collection of updated accounts
        isCalledFromListener - true if the caller is the account update listener, false otherwise.
      • selectInactiveContainersAndMarkInStore

        public void selectInactiveContainersAndMarkInStore​(com.github.ambry.server.StatsSnapshot statsSnapshot)
        Selects Containers to be marked as INACTIVE and marked in underlying account store.
        Specified by:
        selectInactiveContainersAndMarkInStore in interface com.github.ambry.account.AccountService