Search in sources :

Example 16 with ContainerStorageStats

use of com.github.ambry.server.storagestats.ContainerStorageStats in project ambry by linkedin.

the class AccountStatsMySqlStore method storeHostAccountStorageStats.

/**
 * Store the {@link HostAccountStorageStatsWrapper} to mysql database. This method ignores the error information from {@link HostAccountStorageStatsWrapper}
 * and only publish the container storage usages that are different from the previous one.
 * @param statsWrapper The {@link HostAccountStorageStatsWrapper} to publish.
 */
@Override
public void storeHostAccountStorageStats(HostAccountStorageStatsWrapper statsWrapper) throws Exception {
    AccountReportsDao.StorageBatchUpdater batch = accountReportsDao.new StorageBatchUpdater(config.updateBatchSize);
    int batchSize = 0;
    long startTimeMs = System.currentTimeMillis();
    HostAccountStorageStats prevHostStats = Optional.ofNullable(previousHostAccountStorageStatsWrapper).map(HostAccountStorageStatsWrapper::getStats).orElseGet(HostAccountStorageStats::new);
    HostAccountStorageStats currHostStats = statsWrapper.getStats();
    // Find the differences between two {@link HostAccountStorageStats} and apply them to the given {@link ContainerStorageStatsFunction}.
    // The difference is defined as
    // 1. If a container storage usage exists in both HostAccountStorageStats, and the values are different.
    // 2. If a container storage usage only exists in current HostAccountStorageStats.
    // If a container storage usage only exists in the previous HostAccountStorageStats, then it will not be applied to the given function.
    Map<Long, Map<Short, Map<Short, ContainerStorageStats>>> currPartitionMap = currHostStats.getStorageStats();
    Map<Long, Map<Short, Map<Short, ContainerStorageStats>>> prevPartitionMap = prevHostStats.getStorageStats();
    for (Map.Entry<Long, Map<Short, Map<Short, ContainerStorageStats>>> currPartitionMapEntry : currPartitionMap.entrySet()) {
        long partitionId = currPartitionMapEntry.getKey();
        Map<Short, Map<Short, ContainerStorageStats>> currAccountMap = currPartitionMapEntry.getValue();
        Map<Short, Map<Short, ContainerStorageStats>> prevAccountMap = prevPartitionMap.getOrDefault(partitionId, Collections.emptyMap());
        // as its value.
        for (Map.Entry<Short, Map<Short, ContainerStorageStats>> currAccountMapEntry : currAccountMap.entrySet()) {
            short accountId = currAccountMapEntry.getKey();
            Map<Short, ContainerStorageStats> currContainerMap = currAccountMapEntry.getValue();
            Map<Short, ContainerStorageStats> prevContainerMap = prevAccountMap.getOrDefault(accountId, Collections.emptyMap());
            for (Map.Entry<Short, ContainerStorageStats> currContainerMapEntry : currContainerMap.entrySet()) {
                short containerId = currContainerMapEntry.getKey();
                ContainerStorageStats currContainerStorageStats = currContainerMapEntry.getValue();
                if (!prevContainerMap.containsKey(containerId) || !currContainerStorageStats.equals(prevContainerMap.get(containerId))) {
                    batch.addUpdateToBatch(clusterName, hostname, (int) partitionId, accountId, currContainerStorageStats);
                    batchSize++;
                }
            }
        }
    }
    batch.flush();
    storeMetrics.batchSize.update(batchSize);
    storeMetrics.insertAccountStatsTimeMs.update(System.currentTimeMillis() - startTimeMs);
    deleteContainerAccountStats(prevPartitionMap, currPartitionMap);
    storeMetrics.publishTimeMs.update(System.currentTimeMillis() - startTimeMs);
    previousHostAccountStorageStatsWrapper = statsWrapper;
    writeStatsToLocalBackupFile();
}
Also used : HostAccountStorageStats(com.github.ambry.server.storagestats.HostAccountStorageStats) ContainerStorageStats(com.github.ambry.server.storagestats.ContainerStorageStats) AtomicLong(java.util.concurrent.atomic.AtomicLong) HashMap(java.util.HashMap) Map(java.util.Map)

Example 17 with ContainerStorageStats

use of com.github.ambry.server.storagestats.ContainerStorageStats in project ambry by linkedin.

the class AccountStatsMySqlStore method storeAggregatedPartitionClassStorageStats.

@Override
public void storeAggregatedPartitionClassStorageStats(AggregatedPartitionClassStorageStats aggregatedPartitionClassStorageStats) throws SQLException {
    long startTimeMs = System.currentTimeMillis();
    PartitionClassReportsDao.StorageBatchUpdater batch = partitionClassReportsDao.new StorageBatchUpdater(config.updateBatchSize);
    Map<String, Map<Short, Map<Short, ContainerStorageStats>>> storageStats = aggregatedPartitionClassStorageStats.getStorageStats();
    for (String partitionClassName : storageStats.keySet()) {
        Map<Short, Map<Short, ContainerStorageStats>> accountStorageStats = storageStats.get(partitionClassName);
        for (short accountId : accountStorageStats.keySet()) {
            for (ContainerStorageStats containerStats : accountStorageStats.get(accountId).values()) {
                batch.addUpdateToBatch(clusterName, partitionClassName, accountId, containerStats);
            }
        }
    }
    batch.flush();
    storeMetrics.storeAggregatedPartitionClassStatsTimeMs.update(System.currentTimeMillis() - startTimeMs);
}
Also used : ContainerStorageStats(com.github.ambry.server.storagestats.ContainerStorageStats) HashMap(java.util.HashMap) Map(java.util.Map)

Example 18 with ContainerStorageStats

use of com.github.ambry.server.storagestats.ContainerStorageStats in project ambry by linkedin.

the class AggregatedAccountReportsDao method queryContainerUsageForClusterInternal.

/**
 * Query container storage for the given {@code clusterName}. The result will be applied to the {@link AggregatedContainerStorageStatsFunction}.
 * @param forMonthly True to return the monthly snapshot of the container storage usage.
 * @param clusterName The clusterName.
 * @param func The {@link AggregatedContainerStorageStatsFunction} to call to process each container storage usage.
 * @throws SQLException
 */
private void queryContainerUsageForClusterInternal(boolean forMonthly, String clusterName, AggregatedContainerStorageStatsFunction func) throws SQLException {
    try (Connection connection = dataSource.getConnection()) {
        String sqlStatement = forMonthly ? queryMonthlyUsageSqlForCluster : queryUsageSqlForCluster;
        try (PreparedStatement queryStatement = connection.prepareStatement(sqlStatement)) {
            long startTimeMs = System.currentTimeMillis();
            queryStatement.setString(1, clusterName);
            try (ResultSet resultSet = queryStatement.executeQuery()) {
                while (resultSet.next()) {
                    int accountId = resultSet.getInt(ACCOUNT_ID_COLUMN);
                    int containerId = resultSet.getInt(CONTAINER_ID_COLUMN);
                    long storageUsage = resultSet.getLong(STORAGE_USAGE_COLUMN);
                    final long physicalStorageUsage = resultSet.getLong(PHYSICAL_STORAGE_USAGE_COLUMN);
                    final long numberOfBlobs = resultSet.getLong(NUMBER_OF_BLOBS_COLUMN);
                    func.apply((short) accountId, new ContainerStorageStats((short) containerId, storageUsage, physicalStorageUsage, numberOfBlobs));
                }
            }
            metrics.readTimeMs.update(System.currentTimeMillis() - startTimeMs);
            metrics.readSuccessCount.inc();
        }
    } catch (SQLException e) {
        metrics.readFailureCount.inc();
        logger.error(String.format("Failed to execute query on %s, with parameter %s", AGGREGATED_ACCOUNT_REPORTS_TABLE, clusterName), e);
        throw e;
    }
}
Also used : ContainerStorageStats(com.github.ambry.server.storagestats.ContainerStorageStats) SQLException(java.sql.SQLException) Connection(java.sql.Connection) ResultSet(java.sql.ResultSet) PreparedStatement(java.sql.PreparedStatement)

Example 19 with ContainerStorageStats

use of com.github.ambry.server.storagestats.ContainerStorageStats in project ambry by linkedin.

the class PartitionClassReportsDao method queryAggregatedPartitionClassReport.

/**
 * Query container storage usage for given {@code clusterName}. This usage is aggregated under each partition class name.
 * The results will be applied to the given {@link PartitionClassContainerStorageStatsFunction}.
 * @param clusterName The clusterName.
 * @param func The callback to apply query results
 * @throws SQLException
 */
void queryAggregatedPartitionClassReport(String clusterName, PartitionClassContainerStorageStatsFunction func) throws SQLException {
    try (Connection connection = dataSource.getConnection()) {
        try (PreparedStatement queryStatement = connection.prepareStatement(queryAggregatedSql)) {
            long startTimeMs = System.currentTimeMillis();
            queryStatement.setString(1, clusterName);
            try (ResultSet rs = queryStatement.executeQuery()) {
                while (rs.next()) {
                    String partitionClassName = rs.getString(NAME_COLUMN);
                    int accountId = rs.getInt(ACCOUNT_ID_COLUMN);
                    int containerId = rs.getInt(CONTAINER_ID_COLUMN);
                    long usage = rs.getLong(STORAGE_USAGE_COLUMN);
                    final long physicalStorageUsage = rs.getLong(PHYSICAL_STORAGE_USAGE_COLUMN);
                    final long numberOfBlobs = rs.getLong(NUMBER_OF_BLOBS_COLUMN);
                    long updatedAt = rs.getTimestamp(UPDATED_AT_COLUMN).getTime();
                    func.apply(partitionClassName, (short) accountId, new ContainerStorageStats((short) containerId, usage, physicalStorageUsage, numberOfBlobs), updatedAt);
                }
            }
            metrics.readTimeMs.update(System.currentTimeMillis() - startTimeMs);
            metrics.readSuccessCount.inc();
        }
    } catch (SQLException e) {
        metrics.readFailureCount.inc();
        logger.error("Failed to execute query {}, with parameter {}", queryAggregatedSql, clusterName, e);
        throw e;
    }
}
Also used : ContainerStorageStats(com.github.ambry.server.storagestats.ContainerStorageStats) SQLException(java.sql.SQLException) Connection(java.sql.Connection) ResultSet(java.sql.ResultSet) PreparedStatement(java.sql.PreparedStatement)

Example 20 with ContainerStorageStats

use of com.github.ambry.server.storagestats.ContainerStorageStats in project ambry by linkedin.

the class StatsManager method collectAndAggregatePartitionClassStorageStats.

/**
 * Fetch and aggregate partition class stats from a given {@link Store}
 * @param hostPartitionClassStorageStatsMap map from partition class to all partition storage stats.
 * @param partitionId specifies the {@link Store} to be fetched from
 * @param unreachablePartitions a {@link List} containing partition Ids that were unable to successfully fetch from
 */
void collectAndAggregatePartitionClassStorageStats(Map<String, Map<Long, Map<Short, Map<Short, ContainerStorageStats>>>> hostPartitionClassStorageStatsMap, PartitionId partitionId, List<PartitionId> unreachablePartitions) {
    Store store = storageManager.getStore(partitionId, false);
    if (store == null) {
        unreachablePartitions.add(partitionId);
    } else {
        try {
            long fetchAndAggregatePerStoreStartTimeMs = time.milliseconds();
            StoreStats storeStats = store.getStoreStats();
            Map<Short, Map<Short, ContainerStorageStats>> containerStatsMap = storeStats.getContainerStorageStats(time.milliseconds(), publishExcludeAccountIds);
            String partitionClassName = partitionId.getPartitionClass();
            hostPartitionClassStorageStatsMap.computeIfAbsent(partitionClassName, k -> new HashMap<>()).put(partitionId.getId(), containerStatsMap);
            metrics.fetchAndAggregateTimePerStoreMs.update(time.milliseconds() - fetchAndAggregatePerStoreStartTimeMs);
        } catch (StoreException e) {
            unreachablePartitions.add(partitionId);
        }
    }
}
Also used : HostPartitionClassStorageStats(com.github.ambry.server.storagestats.HostPartitionClassStorageStats) StorageManager(com.github.ambry.store.StorageManager) LoggerFactory(org.slf4j.LoggerFactory) StoreStats(com.github.ambry.store.StoreStats) AccountService(com.github.ambry.account.AccountService) HostAccountStorageStats(com.github.ambry.server.storagestats.HostAccountStorageStats) HashMap(java.util.HashMap) AtomicReference(java.util.concurrent.atomic.AtomicReference) Function(java.util.function.Function) ContainerStorageStats(com.github.ambry.server.storagestats.ContainerStorageStats) ArrayList(java.util.ArrayList) ConcurrentMap(java.util.concurrent.ConcurrentMap) HashSet(java.util.HashSet) Map(java.util.Map) ThreadLocalRandom(java.util.concurrent.ThreadLocalRandom) ClusterParticipant(com.github.ambry.clustermap.ClusterParticipant) ScheduledExecutorService(java.util.concurrent.ScheduledExecutorService) StoreException(com.github.ambry.store.StoreException) Time(com.github.ambry.utils.Time) StatsManagerConfig(com.github.ambry.config.StatsManagerConfig) StateModelListenerType(com.github.ambry.clustermap.StateModelListenerType) MetricRegistry(com.codahale.metrics.MetricRegistry) Logger(org.slf4j.Logger) Pair(com.github.ambry.utils.Pair) Iterator(java.util.Iterator) ObjectMapper(com.fasterxml.jackson.databind.ObjectMapper) Set(java.util.Set) Utils(com.github.ambry.utils.Utils) Collectors(java.util.stream.Collectors) File(java.io.File) AccountStatsStore(com.github.ambry.accountstats.AccountStatsStore) Objects(java.util.Objects) TimeUnit(java.util.concurrent.TimeUnit) Store(com.github.ambry.store.Store) List(java.util.List) PartitionStateChangeListener(com.github.ambry.clustermap.PartitionStateChangeListener) StateTransitionException(com.github.ambry.clustermap.StateTransitionException) ReplicaId(com.github.ambry.clustermap.ReplicaId) Account(com.github.ambry.account.Account) TransitionErrorCode(com.github.ambry.clustermap.StateTransitionException.TransitionErrorCode) Collections(java.util.Collections) PartitionId(com.github.ambry.clustermap.PartitionId) StoreStats(com.github.ambry.store.StoreStats) HashMap(java.util.HashMap) AccountStatsStore(com.github.ambry.accountstats.AccountStatsStore) Store(com.github.ambry.store.Store) HashMap(java.util.HashMap) ConcurrentMap(java.util.concurrent.ConcurrentMap) Map(java.util.Map) StoreException(com.github.ambry.store.StoreException)

Aggregations

ContainerStorageStats (com.github.ambry.server.storagestats.ContainerStorageStats)39 HashMap (java.util.HashMap)36 Map (java.util.Map)36 Test (org.junit.Test)20 HostAccountStorageStats (com.github.ambry.server.storagestats.HostAccountStorageStats)16 HostAccountStorageStatsWrapper (com.github.ambry.server.HostAccountStorageStatsWrapper)12 StorageStatsUtilTest (com.github.ambry.server.StorageStatsUtilTest)12 StatsHeader (com.github.ambry.server.StatsHeader)10 AggregatedAccountStorageStats (com.github.ambry.server.storagestats.AggregatedAccountStorageStats)10 Pair (com.github.ambry.utils.Pair)8 ArrayList (java.util.ArrayList)7 LinkedHashMap (java.util.LinkedHashMap)7 HostPartitionClassStorageStats (com.github.ambry.server.storagestats.HostPartitionClassStorageStats)6 Utils (com.github.ambry.utils.Utils)6 MetricRegistry (com.codahale.metrics.MetricRegistry)5 AggregatedPartitionClassStorageStats (com.github.ambry.server.storagestats.AggregatedPartitionClassStorageStats)5 Collections (java.util.Collections)5 HashSet (java.util.HashSet)5 NavigableMap (java.util.NavigableMap)5 Set (java.util.Set)5