Search in sources :

Example 1 with ContainerStorageStats

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

the class StorageStatsTest method testHostAccountStorageStats.

/**
 * Test methods in {@link HostAccountStorageStats}.
 * @throws Exception
 */
@Test
public void testHostAccountStorageStats() throws Exception {
    Map<Long, Map<Short, Map<Short, ContainerStorageStats>>> storageStats = new HashMap<>();
    long partitionId = 10000L;
    short accountId = 0;
    short containerId = 100;
    int numberOfPartitions = 2;
    int numberOfAccounts = 2;
    int numberOfContainers = 2;
    for (int i = 0; i < numberOfPartitions; i++) {
        partitionId++;
        if (!storageStats.containsKey(partitionId)) {
            storageStats.put(partitionId, new HashMap<>());
        }
        for (int j = 0; j < numberOfAccounts; j++) {
            accountId++;
            if (!storageStats.get(partitionId).containsKey(accountId)) {
                storageStats.get(partitionId).put(accountId, new HashMap<>());
            }
            for (int k = 0; k < numberOfContainers; k++) {
                containerId++;
                if (!storageStats.get(partitionId).get(accountId).containsKey(containerId)) {
                    storageStats.get(partitionId).get(accountId).put(containerId, generateRandomContainerStorageStats(containerId));
                }
            }
        }
    }
    HostAccountStorageStats host1 = new HostAccountStorageStats(storageStats);
    HostAccountStorageStats host2 = new HostAccountStorageStats();
    for (Map.Entry<Long, Map<Short, Map<Short, ContainerStorageStats>>> partitionEntry : storageStats.entrySet()) {
        long pid = partitionEntry.getKey();
        for (Map.Entry<Short, Map<Short, ContainerStorageStats>> accountEntry : partitionEntry.getValue().entrySet()) {
            short aid = accountEntry.getKey();
            for (Map.Entry<Short, ContainerStorageStats> containerEntry : accountEntry.getValue().entrySet()) {
                host2.addContainerStorageStats(pid, aid, containerEntry.getValue());
            }
        }
    }
    Assert.assertEquals(host1.getStorageStats(), host2.getStorageStats());
    // Serialize the host account storage stats
    String serialized = objectMapper.writeValueAsString(host1);
    HostAccountStorageStats deserialized = objectMapper.readValue(serialized, HostAccountStorageStats.class);
    Assert.assertEquals(host1.getStorageStats(), deserialized.getStorageStats());
}
Also used : HostAccountStorageStats(com.github.ambry.server.storagestats.HostAccountStorageStats) HashMap(java.util.HashMap) ContainerStorageStats(com.github.ambry.server.storagestats.ContainerStorageStats) Map(java.util.Map) HashMap(java.util.HashMap) Test(org.junit.Test)

Example 2 with ContainerStorageStats

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

the class StorageStatsTest method testContainerStorageStats.

/**
 * Test methods in {@link ContainerStorageStats}.
 * @throws Exception
 */
@Test
public void testContainerStorageStats() throws Exception {
    short containerId = 10;
    long logicalStorageUsage = 10000;
    long physicalStorageUsage = 20000;
    long numberOfBlobs = 100;
    ContainerStorageStats stats = new ContainerStorageStats.Builder(containerId).logicalStorageUsage(logicalStorageUsage).physicalStorageUsage(physicalStorageUsage).numberOfBlobs(numberOfBlobs).build();
    assertContainerStorageStats(stats, containerId, logicalStorageUsage, physicalStorageUsage, numberOfBlobs);
    String serialized = objectMapper.writeValueAsString(stats);
    Map<String, Object> tempMap = objectMapper.readValue(serialized, new TypeReference<Map<String, Object>>() {
    });
    // We are only expecting "containerId", "logicalStorageUsage", "physicalStorageUsage" and "numberOfBlobs" in the serialized string
    Assert.assertEquals(4, tempMap.size());
    for (String key : new String[] { "containerId", "logicalStorageUsage", "physicalStorageUsage", "numberOfBlobs" }) {
        Assert.assertTrue(tempMap.containsKey(key));
    }
    ContainerStorageStats deserialized = objectMapper.readValue(serialized, ContainerStorageStats.class);
    Assert.assertEquals(stats, deserialized);
    ContainerStorageStats newStats = stats.add(deserialized);
    assertContainerStorageStats(stats, containerId, logicalStorageUsage, physicalStorageUsage, numberOfBlobs);
    assertContainerStorageStats(newStats, containerId, 2 * logicalStorageUsage, 2 * physicalStorageUsage, 2 * numberOfBlobs);
    serialized = "{`logicalStorageUsage`:1234, `physicalStorageUsage`:2345, `numberOfBlobs`: 12}".replace("`", "\"");
    try {
        objectMapper.readValue(serialized, ContainerStorageStats.class);
        Assert.fail("Missing container is should fail deserialization");
    } catch (Exception e) {
        Assert.assertTrue(e.getCause() instanceof IllegalStateException);
    }
}
Also used : ContainerStorageStats(com.github.ambry.server.storagestats.ContainerStorageStats) Map(java.util.Map) HashMap(java.util.HashMap) Test(org.junit.Test)

Example 3 with ContainerStorageStats

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

the class StorageStatsUtil method convertStatsSnapshotToHostAccountStorageStats.

/**
 * Convert a {@link StatsSnapshot} to {@link HostAccountStorageStats}. We assume the given {@link StatsSnapshot} follows
 * the proper format so we can construct a {@link HostAccountStorageStats} from it.
 * @param snapshot The {@link StatsSnapshot}.
 * @return The {@link HostAccountStorageStats}.
 */
public static HostAccountStorageStats convertStatsSnapshotToHostAccountStorageStats(StatsSnapshot snapshot) {
    if (snapshot == null) {
        return null;
    }
    HostAccountStorageStats hostAccountStorageStats = new HostAccountStorageStats();
    Map<String, StatsSnapshot> partitionMap = // The map whose key is the partition id
    Optional.ofNullable(snapshot.getSubMap()).orElseGet(HashMap::new);
    for (Map.Entry<String, StatsSnapshot> partitionEntry : partitionMap.entrySet()) {
        long partitionId = Utils.partitionIdFromStatsPartitionKey(partitionEntry.getKey());
        Map<String, StatsSnapshot> accountMap = Optional.ofNullable(partitionEntry.getValue().getSubMap()).orElseGet(// The map whose key is the account id
        HashMap::new);
        for (Map.Entry<String, StatsSnapshot> accountEntry : accountMap.entrySet()) {
            short accountId = Utils.accountIdFromStatsAccountKey(accountEntry.getKey());
            Map<String, StatsSnapshot> containerMap = Optional.ofNullable(accountEntry.getValue().getSubMap()).orElseGet(HashMap::new);
            for (Map.Entry<String, StatsSnapshot> containerEntry : containerMap.entrySet()) {
                short containerId = Utils.containerIdFromStatsContainerKey(containerEntry.getKey());
                long logicalStorageUsage = containerEntry.getValue().getValue();
                hostAccountStorageStats.addContainerStorageStats(partitionId, accountId, new ContainerStorageStats(containerId, logicalStorageUsage, logicalStorageUsage, 0));
            }
        }
    }
    return hostAccountStorageStats;
}
Also used : HostAccountStorageStats(com.github.ambry.server.storagestats.HostAccountStorageStats) ContainerStorageStats(com.github.ambry.server.storagestats.ContainerStorageStats) HashMap(java.util.HashMap) Map(java.util.Map) HashMap(java.util.HashMap)

Example 4 with ContainerStorageStats

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

the class StorageStatsUtil method convertStatsSnapshotToAggregatedPartitionClassStorageStats.

/**
 * Convert a {@link StatsSnapshot} to an {@link AggregatedPartitionClassStorageStats}. We assume the given {@link StatsSnapshot}
 * follows the proper format so we can construct an {@link AggregatedPartitionClassStorageStats} from it.
 * @param snapshot The {@link StatsSnapshot}.
 * @return The {@link AggregatedPartitionClassStorageStats}.
 */
public static AggregatedPartitionClassStorageStats convertStatsSnapshotToAggregatedPartitionClassStorageStats(StatsSnapshot snapshot) {
    if (snapshot == null) {
        return null;
    }
    AggregatedPartitionClassStorageStats aggregatedPartitionClassStorageStats = new AggregatedPartitionClassStorageStats(null);
    Map<String, StatsSnapshot> partitionClassNameSubMap = Optional.ofNullable(snapshot.getSubMap()).orElseGet(HashMap::new);
    for (Map.Entry<String, StatsSnapshot> partitionClasNameSubMapEntry : partitionClassNameSubMap.entrySet()) {
        String partitionClassName = partitionClasNameSubMapEntry.getKey();
        Map<String, StatsSnapshot> accountContainerSubMap = Optional.ofNullable(partitionClasNameSubMapEntry.getValue().getSubMap()).orElseGet(HashMap::new);
        for (Map.Entry<String, StatsSnapshot> accountContainerSubMapEntry : accountContainerSubMap.entrySet()) {
            short[] accountContainerIds = Utils.accountContainerIdFromPartitionClassStatsKey(accountContainerSubMapEntry.getKey());
            short accountId = accountContainerIds[0];
            short containerId = accountContainerIds[1];
            long logicalStorageUsage = accountContainerSubMapEntry.getValue().getValue();
            aggregatedPartitionClassStorageStats.addContainerStorageStats(partitionClassName, accountId, new ContainerStorageStats(containerId, logicalStorageUsage, logicalStorageUsage, 0));
        }
    }
    return aggregatedPartitionClassStorageStats;
}
Also used : ContainerStorageStats(com.github.ambry.server.storagestats.ContainerStorageStats) HashMap(java.util.HashMap) AggregatedPartitionClassStorageStats(com.github.ambry.server.storagestats.AggregatedPartitionClassStorageStats) Map(java.util.Map) HashMap(java.util.HashMap)

Example 5 with ContainerStorageStats

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

the class BlobStoreStatsTest method getContainerStorageStats.

/**
 * Go over the referenceIndex to collect valid data size information per container. The result is used for
 * verification purposes.
 * @param deleteReferenceTimeInMs the reference time in ms until which deletes are relevant
 * @param expiryReferenceTimeInMs the reference time in ms until which expirations are relevant
 * @param deleteTombstoneStats a hashmap that tracks stats related delete tombstones in log segments.
 * @return a nested {@link Map} of serviceId to containerId to valid data size
 */
private Map<Short, Map<Short, ContainerStorageStats>> getContainerStorageStats(long deleteReferenceTimeInMs, long expiryReferenceTimeInMs, Map<String, Pair<AtomicLong, AtomicLong>> deleteTombstoneStats) {
    Map<Short, Map<Short, ContainerStorageStats>> containerStorageStats = new HashMap<>();
    Map<Short, Map<Short, Long>> validSizeMap = new HashMap<>();
    Map<Short, Map<Short, Long>> physicalSizeMap = new HashMap<>();
    Map<Short, Map<Short, Set<StoreKey>>> storeKeyMap = new HashMap<>();
    Pair<Set<MockId>, Set<MockId>> expiredDeletes = new Pair<>(new HashSet<>(), new HashSet<>());
    for (Offset indSegStartOffset : state.referenceIndex.keySet()) {
        state.getValidIndexEntriesForIndexSegment(indSegStartOffset, deleteReferenceTimeInMs, expiryReferenceTimeInMs, null, deleteTombstoneStats, expiredDeletes, true, (entry, isValid) -> {
            IndexValue indexValue = entry.getValue();
            if (indexValue.isPut() && isValid) {
                StatsUtils.updateNestedMapHelper(validSizeMap, indexValue.getAccountId(), indexValue.getContainerId(), indexValue.getSize());
            }
            StatsUtils.updateNestedMapHelper(physicalSizeMap, indexValue.getAccountId(), indexValue.getContainerId(), indexValue.getSize());
            storeKeyMap.computeIfAbsent(indexValue.getAccountId(), k -> new HashMap<>()).computeIfAbsent(indexValue.getContainerId(), k -> new HashSet<>()).add(entry.getKey());
        });
    }
    for (short accountId : validSizeMap.keySet()) {
        for (short containerId : validSizeMap.get(accountId).keySet()) {
            containerStorageStats.computeIfAbsent(accountId, k -> new HashMap<>()).put(containerId, new ContainerStorageStats(containerId, validSizeMap.get(accountId).get(containerId), physicalSizeMap.get(accountId).get(containerId), storeKeyMap.get(accountId).get(containerId).size()));
        }
    }
    return containerStorageStats;
}
Also used : Arrays(java.util.Arrays) RunWith(org.junit.runner.RunWith) TimeoutException(java.util.concurrent.TimeoutException) StoreStats(com.github.ambry.store.StoreStats) HashMap(java.util.HashMap) ContainerStorageStats(com.github.ambry.server.storagestats.ContainerStorageStats) ArrayList(java.util.ArrayList) HashSet(java.util.HashSet) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) TestUtils(com.github.ambry.utils.TestUtils) Map(java.util.Map) After(org.junit.After) SystemTime(com.github.ambry.utils.SystemTime) ScheduledExecutorService(java.util.concurrent.ScheduledExecutorService) Assume(org.junit.Assume) CuratedLogIndexState(com.github.ambry.store.CuratedLogIndexState) Time(com.github.ambry.utils.Time) EnumSet(java.util.EnumSet) Parameterized(org.junit.runners.Parameterized) Container(com.github.ambry.account.Container) MetricRegistry(com.codahale.metrics.MetricRegistry) Pair(com.github.ambry.utils.Pair) Set(java.util.Set) Utils(com.github.ambry.utils.Utils) IOException(java.io.IOException) Test(org.junit.Test) NavigableMap(java.util.NavigableMap) Collectors(java.util.stream.Collectors) File(java.io.File) TimeUnit(java.util.concurrent.TimeUnit) CountDownLatch(java.util.concurrent.CountDownLatch) AtomicLong(java.util.concurrent.atomic.AtomicLong) List(java.util.List) Throttler(com.github.ambry.utils.Throttler) MockTime(com.github.ambry.utils.MockTime) Account(com.github.ambry.account.Account) Optional(java.util.Optional) Comparator(java.util.Comparator) Assert(org.junit.Assert) Collections(java.util.Collections) HashSet(java.util.HashSet) EnumSet(java.util.EnumSet) Set(java.util.Set) HashMap(java.util.HashMap) ContainerStorageStats(com.github.ambry.server.storagestats.ContainerStorageStats) HashMap(java.util.HashMap) Map(java.util.Map) NavigableMap(java.util.NavigableMap) Pair(com.github.ambry.utils.Pair) HashSet(java.util.HashSet)

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