Search in sources :

Example 21 with ReadOnlyStorageEngine

use of voldemort.store.readonly.ReadOnlyStorageEngine in project voldemort by voldemort.

the class AdminServiceBasicTest method generateAndFetchFiles.

@SuppressWarnings("unchecked")
private void generateAndFetchFiles(int numChunks, long versionId, long indexSize, long dataSize) throws IOException {
    Map<Integer, Set<Pair<Integer, Integer>>> buckets = ROTestUtils.getNodeIdToAllPartitions(cluster, StoreDefinitionUtils.getStoreDefinitionWithName(storeDefs, "test-readonly-fetchfiles"), true);
    for (Node node : cluster.getNodes()) {
        ReadOnlyStorageEngine store = (ReadOnlyStorageEngine) getStore(node.getId(), "test-readonly-fetchfiles");
        // Create list of buckets ( replica to partition )
        Set<Pair<Integer, Integer>> nodeBucketsSet = buckets.get(node.getId());
        HashMap<Integer, List<Integer>> nodeBuckets = ROTestUtils.flattenPartitionTuples(nodeBucketsSet);
        // Split the buckets into primary and replica buckets
        HashMap<Integer, List<Integer>> primaryNodeBuckets = Maps.newHashMap();
        primaryNodeBuckets.put(0, nodeBuckets.get(0));
        int primaryPartitions = nodeBuckets.get(0).size();
        HashMap<Integer, List<Integer>> replicaNodeBuckets = Maps.newHashMap(nodeBuckets);
        replicaNodeBuckets.remove(0);
        int replicaPartitions = 0;
        for (List<Integer> partitions : replicaNodeBuckets.values()) {
            replicaPartitions += partitions.size();
        }
        // Generate data...
        File newVersionDir = new File(store.getStoreDirPath(), "version-" + Long.toString(versionId));
        Utils.mkdirs(newVersionDir);
        generateROFiles(numChunks, indexSize, dataSize, nodeBuckets, newVersionDir);
        // Swap it...
        store.swapFiles(newVersionDir.getAbsolutePath());
        // Check if everything got mmap-ed correctly...
        HashMap<Object, Integer> chunkIdToNumChunks = store.getChunkedFileSet().getChunkIdToNumChunks();
        for (Object bucket : chunkIdToNumChunks.keySet()) {
            Pair<Integer, Integer> partitionToReplicaBucket = (Pair<Integer, Integer>) bucket;
            Pair<Integer, Integer> replicaToPartitionBucket = Pair.create(partitionToReplicaBucket.getSecond(), partitionToReplicaBucket.getFirst());
            assertTrue(nodeBucketsSet.contains(replicaToPartitionBucket));
        }
        // Test 0) Try to fetch a partition which doesn't exist
        File tempDir = TestUtils.createTempDir();
        HashMap<Integer, List<Integer>> dumbMap = Maps.newHashMap();
        dumbMap.put(0, Lists.newArrayList(100));
        try {
            getAdminClient().readonlyOps.fetchPartitionFiles(node.getId(), "test-readonly-fetchfiles", Lists.newArrayList(100), tempDir.getAbsolutePath(), null, running);
            fail("Should throw exception since partition map passed is bad");
        } catch (VoldemortException e) {
        }
        // Test 1) Fetch all the primary partitions...
        tempDir = TestUtils.createTempDir();
        getAdminClient().readonlyOps.fetchPartitionFiles(node.getId(), "test-readonly-fetchfiles", primaryNodeBuckets.get(0), tempDir.getAbsolutePath(), null, running);
        // Check it...
        assertEquals(tempDir.list().length, 2 * primaryPartitions * numChunks + 1);
        for (Entry<Integer, List<Integer>> entry : primaryNodeBuckets.entrySet()) {
            int replicaType = entry.getKey();
            for (int partitionId : entry.getValue()) {
                for (int chunkId = 0; chunkId < numChunks; chunkId++) {
                    File indexFile = new File(tempDir, Integer.toString(partitionId) + "_" + Integer.toString(replicaType) + "_" + Integer.toString(chunkId) + ".index");
                    File dataFile = new File(tempDir, Integer.toString(partitionId) + "_" + Integer.toString(replicaType) + "_" + Integer.toString(chunkId) + ".data");
                    assertTrue(indexFile.exists());
                    assertTrue(dataFile.exists());
                    assertEquals(indexFile.length(), indexSize);
                    assertEquals(dataFile.length(), dataSize);
                }
            }
        }
        // Check if metadata file exists
        File metadataFile = new File(tempDir, ".metadata");
        assertEquals(metadataFile.exists(), true);
        // Test 2) Fetch all the replica partitions...
        tempDir = TestUtils.createTempDir();
        for (Entry<Integer, List<Integer>> entry : replicaNodeBuckets.entrySet()) {
            getAdminClient().readonlyOps.fetchPartitionFiles(node.getId(), "test-readonly-fetchfiles", entry.getValue(), tempDir.getAbsolutePath(), null, running);
        }
        // Check it...
        assertEquals(tempDir.list().length, 2 * replicaPartitions * numChunks + 1);
        for (Entry<Integer, List<Integer>> entry : replicaNodeBuckets.entrySet()) {
            int replicaType = entry.getKey();
            for (int partitionId : entry.getValue()) {
                for (int chunkId = 0; chunkId < numChunks; chunkId++) {
                    File indexFile = new File(tempDir, Integer.toString(partitionId) + "_" + Integer.toString(replicaType) + "_" + Integer.toString(chunkId) + ".index");
                    File dataFile = new File(tempDir, Integer.toString(partitionId) + "_" + Integer.toString(replicaType) + "_" + Integer.toString(chunkId) + ".data");
                    assertTrue(indexFile.exists());
                    assertTrue(dataFile.exists());
                    assertEquals(indexFile.length(), indexSize);
                    assertEquals(dataFile.length(), dataSize);
                }
            }
        }
        // Check if metadata file exists
        metadataFile = new File(tempDir, ".metadata");
        assertEquals(metadataFile.exists(), true);
        // Test 3) Fetch all the partitions...
        tempDir = TestUtils.createTempDir();
        for (Entry<Integer, List<Integer>> entry : nodeBuckets.entrySet()) {
            getAdminClient().readonlyOps.fetchPartitionFiles(node.getId(), "test-readonly-fetchfiles", entry.getValue(), tempDir.getAbsolutePath(), null, running);
        }
        // Check it...
        assertEquals(tempDir.list().length, 2 * (primaryPartitions + replicaPartitions) * numChunks + 1);
        for (Entry<Integer, List<Integer>> entry : nodeBuckets.entrySet()) {
            int replicaType = entry.getKey();
            for (int partitionId : entry.getValue()) {
                for (int chunkId = 0; chunkId < numChunks; chunkId++) {
                    File indexFile = new File(tempDir, Integer.toString(partitionId) + "_" + Integer.toString(replicaType) + "_" + Integer.toString(chunkId) + ".index");
                    File dataFile = new File(tempDir, Integer.toString(partitionId) + "_" + Integer.toString(replicaType) + "_" + Integer.toString(chunkId) + ".data");
                    assertTrue(indexFile.exists());
                    assertTrue(dataFile.exists());
                    assertEquals(indexFile.length(), indexSize);
                    assertEquals(dataFile.length(), dataSize);
                }
            }
        }
        // Check if metadata file exists
        metadataFile = new File(tempDir, ".metadata");
        assertEquals(metadataFile.exists(), true);
        // testGetROStorageFileList
        List<String> fileList = getAdminClient().readonlyOps.getROStorageFileList(node.getId(), "test-readonly-fetchfiles");
        int fileCount = 0;
        for (Entry<Integer, List<Integer>> entry : nodeBuckets.entrySet()) {
            int replicaType = entry.getKey();
            for (int partitionId : entry.getValue()) {
                for (int chunkId = 0; chunkId < numChunks; chunkId++) {
                    String fileName = Integer.toString(partitionId) + "_" + Integer.toString(replicaType) + "_" + Integer.toString(chunkId);
                    assertTrue("Assuming file exists:" + fileName, fileList.contains(fileName));
                    fileCount++;
                }
            }
        }
        assertEquals(fileCount, fileList.size());
    }
}
Also used : Set(java.util.Set) HashSet(java.util.HashSet) Node(voldemort.cluster.Node) ReadOnlyStorageEngine(voldemort.store.readonly.ReadOnlyStorageEngine) VoldemortException(voldemort.VoldemortException) List(java.util.List) ArrayList(java.util.ArrayList) File(java.io.File) Pair(voldemort.utils.Pair)

Aggregations

ReadOnlyStorageEngine (voldemort.store.readonly.ReadOnlyStorageEngine)21 VoldemortException (voldemort.VoldemortException)15 File (java.io.File)12 StoreDefinition (voldemort.store.StoreDefinition)6 ByteArray (voldemort.utils.ByteArray)5 IOException (java.io.IOException)4 ServletException (javax.servlet.ServletException)4 Cluster (voldemort.cluster.Cluster)4 Pair (voldemort.utils.Pair)4 Versioned (voldemort.versioning.Versioned)4 HashMap (java.util.HashMap)3 Map (java.util.Map)3 VAdminProto (voldemort.client.protocol.pb.VAdminProto)3 RoutingStrategyFactory (voldemort.routing.RoutingStrategyFactory)3 SerializerDefinition (voldemort.serialization.SerializerDefinition)3 StoreDefinitionBuilder (voldemort.store.StoreDefinitionBuilder)3 Set (java.util.Set)2 Path (org.apache.hadoop.fs.Path)2 JobConf (org.apache.hadoop.mapred.JobConf)2 Test (org.junit.Test)2