Search in sources :

Example 11 with Pair

use of com.github.ambry.utils.Pair in project ambry by linkedin.

the class MockBlobStoreStats method initializeBlobStore.

// helper methods
/**
 * Initializes {@link BlobStore}
 * @param minLogSizeToTriggerCompactionInPercentage Property value to be set for
 * {@link StoreConfig#storeMinUsedCapacityToTriggerCompactionInPercentage}
 * @param messageRetentionInHours Property value to be set for {@link StoreConfig#storeDeletedMessageRetentionHours
 * @throws InterruptedException
 */
static Pair<MockBlobStore, StoreConfig> initializeBlobStore(Properties properties, Time time, int minLogSizeToTriggerCompactionInPercentage, int messageRetentionInHours, long maxBlobSize) throws InterruptedException {
    if (minLogSizeToTriggerCompactionInPercentage != -1) {
        properties.setProperty("store.min.log.size.to.trigger.compaction.in.percent", String.valueOf(minLogSizeToTriggerCompactionInPercentage));
    }
    if (messageRetentionInHours != -1) {
        properties.setProperty("store.deleted.message.retention.hours", String.valueOf(messageRetentionInHours));
    }
    StoreConfig config = new StoreConfig(new VerifiableProperties(properties));
    time.sleep(10 * TimeUnit.HOURS.toMillis(config.storeDeletedMessageRetentionHours));
    MetricRegistry metricRegistry = new MetricRegistry();
    StoreMetrics metrics = new StoreMetrics(metricRegistry);
    MockBlobStoreStats mockBlobStoreStats = new MockBlobStoreStats(maxBlobSize);
    MockBlobStore blobStore = new MockBlobStore(config, metrics, time, CAPACITY_IN_BYTES, SEGMENT_CAPACITY_IN_BYTES, SEGMENT_HEADER_SIZE, DEFAULT_USED_CAPACITY_IN_BYTES, mockBlobStoreStats);
    return new Pair<>(blobStore, config);
}
Also used : VerifiableProperties(com.github.ambry.config.VerifiableProperties) MetricRegistry(com.codahale.metrics.MetricRegistry) StoreConfig(com.github.ambry.config.StoreConfig) Pair(com.github.ambry.utils.Pair)

Example 12 with Pair

use of com.github.ambry.utils.Pair in project ambry by linkedin.

the class LogTest method logSegmentCustomNamesTest.

/**
 * Checks that the constructor that receives segments and segment names iterator,
 * {@link Log#Log(String, long, DiskSpaceAllocator, StoreConfig, StoreMetrics, boolean, List, Iterator, DiskMetrics)}, loads
 * the segments correctly and uses the iterator to name new segments and uses the default algorithm once the names
 * run out.
 * @throws IOException
 */
@Test
public void logSegmentCustomNamesTest() throws IOException, StoreException {
    int numSegments = (int) (LOG_CAPACITY / SEGMENT_CAPACITY);
    LogSegment segment = getLogSegment(LogSegmentName.fromPositionAndGeneration(0, 0), SEGMENT_CAPACITY, true);
    long startPos = 2 * numSegments;
    List<Pair<LogSegmentName, String>> expectedSegmentAndFileNames = new ArrayList<>(numSegments);
    expectedSegmentAndFileNames.add(new Pair<>(segment.getName(), segment.getView().getFirst().getName()));
    List<Pair<LogSegmentName, String>> segmentNameAndFileNamesDesired = new ArrayList<>();
    LogSegmentName lastName = null;
    for (int i = 0; i < 2; i++) {
        lastName = LogSegmentName.fromPositionAndGeneration(startPos + i, 0);
        String fileName = lastName.toFilename() + "_modified";
        segmentNameAndFileNamesDesired.add(new Pair<>(lastName, fileName));
        expectedSegmentAndFileNames.add(new Pair<>(lastName, fileName));
    }
    for (int i = expectedSegmentAndFileNames.size(); i < numSegments; i++) {
        lastName = lastName.getNextPositionName();
        String fileName = lastName.toFilename();
        expectedSegmentAndFileNames.add(new Pair<>(lastName, fileName));
    }
    Log log = new Log(tempDir.getAbsolutePath(), LOG_CAPACITY, StoreTestUtils.DEFAULT_DISK_SPACE_ALLOCATOR, createStoreConfig(SEGMENT_CAPACITY, setFilePermissionEnabled), metrics, true, Collections.singletonList(segment), segmentNameAndFileNamesDesired.iterator(), null);
    // write enough so that all segments are allocated
    ByteBuffer buffer = ByteBuffer.allocate((int) (segment.getCapacityInBytes() - segment.getStartOffset()));
    for (int i = 0; i < numSegments; i++) {
        buffer.rewind();
        CHANNEL_APPENDER.append(log, buffer);
    }
    segment = log.getFirstSegment();
    for (Pair<LogSegmentName, String> nameAndFilename : expectedSegmentAndFileNames) {
        assertEquals("Segment name does not match", nameAndFilename.getFirst(), segment.getName());
        assertEquals("Segment file does not match", nameAndFilename.getSecond(), segment.getView().getFirst().getName());
        segment = log.getNextSegment(segment);
    }
    assertNull("There should be no more segments", segment);
}
Also used : ArrayList(java.util.ArrayList) ByteBuffer(java.nio.ByteBuffer) Pair(com.github.ambry.utils.Pair) Test(org.junit.Test)

Example 13 with Pair

use of com.github.ambry.utils.Pair in project ambry by linkedin.

the class MySqlClusterAggregatorTest method testAggregateHostPartitionClassStorageStats.

/**
 * Test basic functionality of {@link MySqlClusterAggregator#aggregateHostPartitionClassStorageStats}.
 * @throws Exception
 */
@Test
public void testAggregateHostPartitionClassStorageStats() throws Exception {
    int nodeCount = 3;
    int numberOfPartitions = 4;
    Map<String, Map<Long, Map<Short, Map<Short, ContainerStorageStats>>>> storageStatsMap = new HashMap<>();
    String[] partitionClassNames = { "default", "newClass" };
    for (int i = 0; i < numberOfPartitions; i++) {
        String partitionClassName = partitionClassNames[i % partitionClassNames.length];
        storageStatsMap.computeIfAbsent(partitionClassName, k -> new HashMap<>()).put((long) i, StorageStatsUtilTest.generateRandomAggregatedAccountStorageStats((short) 0, i + 3, 3, 10000L, 2, 10));
    }
    StatsHeader header = new StatsHeader(StatsHeader.StatsDescription.STORED_DATA_SIZE, DEFAULT_TIMESTAMP, numberOfPartitions, numberOfPartitions, Collections.emptyList());
    HostPartitionClassStorageStatsWrapper nodeStats = new HostPartitionClassStorageStatsWrapper(header, new HostPartitionClassStorageStats(storageStatsMap));
    header = new StatsHeader(StatsHeader.StatsDescription.STORED_DATA_SIZE, DEFAULT_TIMESTAMP, 0, 0, Collections.emptyList());
    HostPartitionClassStorageStatsWrapper emptyStats = new HostPartitionClassStorageStatsWrapper(header, new HostPartitionClassStorageStats());
    Map<String, HostPartitionClassStorageStatsWrapper> instanceToStatsMap = new HashMap<>();
    for (int i = 0; i < nodeCount; i++) {
        instanceToStatsMap.put("Instance_" + i, new HostPartitionClassStorageStatsWrapper(new StatsHeader(nodeStats.getHeader()), new HostPartitionClassStorageStats(nodeStats.getStats())));
    }
    instanceToStatsMap.put("Instance_" + nodeCount, emptyStats);
    Pair<AggregatedPartitionClassStorageStats, AggregatedPartitionClassStorageStats> aggregatedRawAndValidStats = clusterAggregator.aggregateHostPartitionClassStorageStatsWrappers(instanceToStatsMap);
    Map<String, Map<Short, Map<Short, ContainerStorageStats>>> expectedAggregatedStorageStatsMap = clusterAggregator.aggregateHostPartitionClassStorageStats(storageStatsMap);
    Assert.assertEquals(expectedAggregatedStorageStatsMap, aggregatedRawAndValidStats.getSecond().getStorageStats());
    assertAggregatedRawStatsForPartitionClassStorageStats(aggregatedRawAndValidStats.getFirst().getStorageStats(), expectedAggregatedStorageStatsMap, nodeCount);
}
Also used : HostPartitionClassStorageStats(com.github.ambry.server.storagestats.HostPartitionClassStorageStats) HostAccountStorageStatsWrapper(com.github.ambry.server.HostAccountStorageStatsWrapper) AggregatedPartitionClassStorageStats(com.github.ambry.server.storagestats.AggregatedPartitionClassStorageStats) Pair(com.github.ambry.utils.Pair) ObjectMapper(com.fasterxml.jackson.databind.ObjectMapper) StatsHeader(com.github.ambry.server.StatsHeader) HostAccountStorageStats(com.github.ambry.server.storagestats.HostAccountStorageStats) HashMap(java.util.HashMap) Test(org.junit.Test) ContainerStorageStats(com.github.ambry.server.storagestats.ContainerStorageStats) LinkedHashMap(java.util.LinkedHashMap) TimeUnit(java.util.concurrent.TimeUnit) AggregatedAccountStorageStats(com.github.ambry.server.storagestats.AggregatedAccountStorageStats) Map(java.util.Map) TypeReference(com.fasterxml.jackson.core.type.TypeReference) Assert(org.junit.Assert) HostPartitionClassStorageStatsWrapper(com.github.ambry.server.HostPartitionClassStorageStatsWrapper) Collections(java.util.Collections) StorageStatsUtilTest(com.github.ambry.server.StorageStatsUtilTest) HashMap(java.util.HashMap) LinkedHashMap(java.util.LinkedHashMap) StatsHeader(com.github.ambry.server.StatsHeader) HostPartitionClassStorageStats(com.github.ambry.server.storagestats.HostPartitionClassStorageStats) ContainerStorageStats(com.github.ambry.server.storagestats.ContainerStorageStats) HostPartitionClassStorageStatsWrapper(com.github.ambry.server.HostPartitionClassStorageStatsWrapper) AggregatedPartitionClassStorageStats(com.github.ambry.server.storagestats.AggregatedPartitionClassStorageStats) HashMap(java.util.HashMap) LinkedHashMap(java.util.LinkedHashMap) Map(java.util.Map) Test(org.junit.Test) StorageStatsUtilTest(com.github.ambry.server.StorageStatsUtilTest)

Example 14 with Pair

use of com.github.ambry.utils.Pair in project ambry by linkedin.

the class BlobIdTest method testComparisons.

/**
 * Tests blobIds comparisons. Among other things, ensures the following requirements are met:
 * <br>
 * V1s are always less than V2s and V3s.
 * V2s are always less than V3s.
 */
@Test
public void testComparisons() {
    // the version check is to do this inter-version test just once (since this is a parametrized test).
    assumeTrue(version == BLOB_ID_V1);
    for (int i = 0; i < 100; i++) {
        Map<Short, Pair<BlobId, BlobId>> blobIds = Arrays.stream(BlobId.getAllValidVersions()).collect(Collectors.toMap(Function.identity(), v -> new Pair<>(getRandomBlobId(v), getRandomBlobId(v))));
        for (short version : BlobId.getAllValidVersions()) {
            BlobId blobId = blobIds.get(version).getFirst();
            BlobId altBlobId = blobIds.get(version).getSecond();
            assertEquals("blobIdV" + version + " should be equal to itself", 0, blobId.compareTo(blobId));
            assertEquals("blobIdV" + version + " should be equal to itself", blobId, blobId);
            assertThat("Two randomly generated blobIdV" + version + "s should be unequal", blobId.compareTo(altBlobId), not(0));
            assertThat("Two randomly generated blobIdV" + version + "s should be unequal", blobId, not(altBlobId));
            for (short otherVersion = 1; otherVersion < version; otherVersion++) {
                BlobId otherBlobId = blobIds.get(otherVersion).getFirst();
                assertThat("blobIdV" + otherVersion + " should not equal blobIdV" + version, otherBlobId, not(blobId));
                assertThat("blobIdV" + version + " should not equal blobIdV" + otherVersion, blobId, not(otherBlobId));
                boolean differentVersionGroup = version < BLOB_ID_V3 || (version < BLOB_ID_V6 ? otherVersion < BLOB_ID_V3 : otherVersion < BLOB_ID_V6);
                if (differentVersionGroup) {
                    assertTrue("blobIdV" + otherVersion + " should be less than blobIdV" + version, otherBlobId.compareTo(blobId) < 0);
                    assertTrue("blobIdV" + version + " should be greater than blobIdV" + otherVersion, blobId.compareTo(otherBlobId) > 0);
                } else {
                    assertEquals("Comparison between blobIdV" + version + " and blobIDV" + otherVersion + " are based on uuid only", blobId.getUuid().compareTo(otherBlobId.getUuid()), blobId.compareTo(otherBlobId));
                    assertEquals("Comparison between blobIdV" + otherVersion + " and blobIDV" + version + " are based on uuid only", otherBlobId.getUuid().compareTo(blobId.getUuid()), otherBlobId.compareTo(blobId));
                }
            }
        }
    }
}
Also used : DataInputStream(java.io.DataInputStream) CoreMatchers(org.hamcrest.CoreMatchers) Arrays(java.util.Arrays) RunWith(org.junit.runner.RunWith) Random(java.util.Random) Function(java.util.function.Function) ByteBuffer(java.nio.ByteBuffer) Base64(org.apache.commons.codec.binary.Base64) ArrayList(java.util.ArrayList) ByteArrayInputStream(java.io.ByteArrayInputStream) TestUtils(com.github.ambry.utils.TestUtils) Map(java.util.Map) Assume(org.junit.Assume) Parameterized(org.junit.runners.Parameterized) Container(com.github.ambry.account.Container) Pair(com.github.ambry.utils.Pair) ClusterMap(com.github.ambry.clustermap.ClusterMap) IOException(java.io.IOException) Test(org.junit.Test) Utils(com.github.ambry.utils.Utils) MockPartitionId(com.github.ambry.clustermap.MockPartitionId) UUID(java.util.UUID) Collectors(java.util.stream.Collectors) List(java.util.List) ByteBufferInputStream(com.github.ambry.utils.ByteBufferInputStream) Account(com.github.ambry.account.Account) Assert(org.junit.Assert) Collections(java.util.Collections) MockClusterMap(com.github.ambry.clustermap.MockClusterMap) PartitionId(com.github.ambry.clustermap.PartitionId) BlobId(com.github.ambry.commons.BlobId) BlobId(com.github.ambry.commons.BlobId) Pair(com.github.ambry.utils.Pair) Test(org.junit.Test)

Example 15 with Pair

use of com.github.ambry.utils.Pair in project ambry by linkedin.

the class AccountStatsMySqlStoreIntegrationTest method testHostPartitionClassStats.

/**
 * Test methods to store and fetch partition class, partition name partition id and partition class stats.
 * @throws Exception
 */
@Test
public void testHostPartitionClassStats() throws Exception {
    // First write some stats to account reports
    testMultiStoreStats();
    StatsWrapper accountStats1 = mySqlStore.queryAccountStatsByHost(hostname1, port1);
    StatsWrapper accountStats2 = mySqlStore.queryAccountStatsByHost(hostname2, port2);
    AccountStatsMySqlStore mySqlStore3 = createAccountStatsMySqlStore(clusterName2, hostname3, port3);
    StatsWrapper accountStats3 = mySqlStore3.queryAccountStatsByHost(hostname3, port3);
    // From this account stats, create partition class stats;
    Set<String> allPartitionKeys = new HashSet<String>() {

        {
            addAll(accountStats1.getSnapshot().getSubMap().keySet());
            addAll(accountStats2.getSnapshot().getSubMap().keySet());
            addAll(accountStats3.getSnapshot().getSubMap().keySet());
        }
    };
    List<String> partitionClassNames = Arrays.asList("default", "new");
    Map<String, String> partitionKeyToClassName = new HashMap<>();
    int ind = 0;
    for (String partitionKey : allPartitionKeys) {
        partitionKeyToClassName.put(partitionKey, partitionClassNames.get(ind % partitionClassNames.size()));
        ind++;
    }
    StatsWrapper partitionClassStats1 = convertAccountStatsToPartitionClassStats(accountStats1, partitionKeyToClassName);
    StatsWrapper partitionClassStats2 = convertAccountStatsToPartitionClassStats(accountStats2, partitionKeyToClassName);
    StatsWrapper partitionClassStats3 = convertAccountStatsToPartitionClassStats(accountStats3, partitionKeyToClassName);
    mySqlStore.storePartitionClassStats(partitionClassStats1);
    mySqlStore.storePartitionClassStats(partitionClassStats2);
    mySqlStore3.storePartitionClassStats(partitionClassStats3);
    Map<String, Set<Integer>> partitionNameAndIds = mySqlStore.queryPartitionNameAndIds();
    assertEquals(new HashSet<>(partitionClassNames), partitionNameAndIds.keySet());
    Map<String, String> dbPartitionKeyToClassName = partitionNameAndIds.entrySet().stream().flatMap(ent -> ent.getValue().stream().map(pid -> new Pair<String, String>(ent.getKey(), "Partition[" + pid + "]"))).collect(Collectors.toMap(Pair::getSecond, Pair::getFirst));
    assertEquals(partitionKeyToClassName, dbPartitionKeyToClassName);
    StatsWrapper obtainedStats1 = mySqlStore.queryPartitionClassStatsByHost(hostname1, port1, partitionNameAndIds);
    assertEquals(partitionClassStats1.getSnapshot(), obtainedStats1.getSnapshot());
    StatsWrapper obtainedStats2 = mySqlStore.queryPartitionClassStatsByHost(hostname2, port2, partitionNameAndIds);
    assertEquals(partitionClassStats2.getSnapshot(), obtainedStats2.getSnapshot());
    StatsWrapper obtainedStats3 = mySqlStore3.queryPartitionClassStatsByHost(hostname3, port3, partitionNameAndIds);
    assertEquals(partitionClassStats3.getSnapshot(), obtainedStats3.getSnapshot());
    mySqlStore3.shutdown();
}
Also used : HostPartitionClassStorageStats(com.github.ambry.server.storagestats.HostPartitionClassStorageStats) HostAccountStorageStatsWrapper(com.github.ambry.server.HostAccountStorageStatsWrapper) Arrays(java.util.Arrays) Connection(java.sql.Connection) AggregatedPartitionClassStorageStats(com.github.ambry.server.storagestats.AggregatedPartitionClassStorageStats) StatsHeader(com.github.ambry.server.StatsHeader) RunWith(org.junit.runner.RunWith) HostAccountStorageStats(com.github.ambry.server.storagestats.HostAccountStorageStats) HashMap(java.util.HashMap) Random(java.util.Random) ContainerStorageStats(com.github.ambry.server.storagestats.ContainerStorageStats) ArrayList(java.util.ArrayList) HashSet(java.util.HashSet) SQLException(java.sql.SQLException) TestUtils(com.github.ambry.utils.TestUtils) ResultSet(java.sql.ResultSet) Map(java.util.Map) After(org.junit.After) Path(java.nio.file.Path) Parameterized(org.junit.runners.Parameterized) StatsWrapper(com.github.ambry.server.StatsWrapper) Before(org.junit.Before) MetricRegistry(com.codahale.metrics.MetricRegistry) Properties(java.util.Properties) Pair(com.github.ambry.utils.Pair) Files(java.nio.file.Files) VerifiableProperties(com.github.ambry.config.VerifiableProperties) Set(java.util.Set) Utils(com.github.ambry.utils.Utils) IOException(java.io.IOException) Test(org.junit.Test) Collectors(java.util.stream.Collectors) AccountStatsMySqlConfig(com.github.ambry.config.AccountStatsMySqlConfig) AggregatedAccountStorageStats(com.github.ambry.server.storagestats.AggregatedAccountStorageStats) List(java.util.List) StorageStatsUtil(com.github.ambry.server.StorageStatsUtil) StatsReportType(com.github.ambry.server.StatsReportType) StatsSnapshot(com.github.ambry.server.StatsSnapshot) ClusterMapConfig(com.github.ambry.config.ClusterMapConfig) Statement(java.sql.Statement) Assert(org.junit.Assert) HostPartitionClassStorageStatsWrapper(com.github.ambry.server.HostPartitionClassStorageStatsWrapper) Collections(java.util.Collections) StorageStatsUtilTest(com.github.ambry.server.StorageStatsUtilTest) HashSet(java.util.HashSet) ResultSet(java.sql.ResultSet) Set(java.util.Set) HashMap(java.util.HashMap) HostAccountStorageStatsWrapper(com.github.ambry.server.HostAccountStorageStatsWrapper) StatsWrapper(com.github.ambry.server.StatsWrapper) HostPartitionClassStorageStatsWrapper(com.github.ambry.server.HostPartitionClassStorageStatsWrapper) HashSet(java.util.HashSet) Test(org.junit.Test) StorageStatsUtilTest(com.github.ambry.server.StorageStatsUtilTest)

Aggregations

Pair (com.github.ambry.utils.Pair)64 ArrayList (java.util.ArrayList)29 HashMap (java.util.HashMap)28 Map (java.util.Map)28 Test (org.junit.Test)20 IOException (java.io.IOException)15 MetricRegistry (com.codahale.metrics.MetricRegistry)14 List (java.util.List)14 ByteBuffer (java.nio.ByteBuffer)13 Collections (java.util.Collections)13 File (java.io.File)12 Assert (org.junit.Assert)12 VerifiableProperties (com.github.ambry.config.VerifiableProperties)11 Utils (com.github.ambry.utils.Utils)10 HashSet (java.util.HashSet)10 Properties (java.util.Properties)10 Container (com.github.ambry.account.Container)9 TestUtils (com.github.ambry.utils.TestUtils)9 Arrays (java.util.Arrays)9 Set (java.util.Set)9