Search in sources :

Example 21 with Versioned

use of voldemort.versioning.Versioned in project voldemort by voldemort.

the class MetadataStore method put.

/**
     * helper function to convert strings to bytes as needed.
     * 
     * @param key
     * @param value
     */
@SuppressWarnings("unchecked")
public void put(String key, Versioned<Object> value) {
    // acquire write lock
    writeLock.lock();
    try {
        if (this.storeNames.contains(key) || key.equals(STORES_KEY)) {
            // Check for backwards compatibility
            List<StoreDefinition> storeDefinitions = (List<StoreDefinition>) value.getValue();
            StoreDefinitionUtils.validateSchemasAsNeeded(storeDefinitions);
            // If the put is on the entire stores.xml key, delete the
            // additional stores which do not exist in the specified
            // stores.xml
            Set<String> storeNamesToDelete = new HashSet<String>();
            for (String storeName : this.storeNames) {
                storeNamesToDelete.add(storeName);
            }
            // Add / update the list of store definitions specified in the
            // value
            StoreDefinitionsMapper mapper = new StoreDefinitionsMapper();
            // Update the STORES directory and the corresponding entry in
            // metadata cache
            Set<String> specifiedStoreNames = new HashSet<String>();
            for (StoreDefinition storeDef : storeDefinitions) {
                specifiedStoreNames.add(storeDef.getName());
                String storeDefStr = mapper.writeStore(storeDef);
                Versioned<String> versionedValueStr = new Versioned<String>(storeDefStr, value.getVersion());
                this.storeDefinitionsStorageEngine.put(storeDef.getName(), versionedValueStr, "");
                // Update the metadata cache
                this.metadataCache.put(storeDef.getName(), new Versioned<Object>(storeDefStr, value.getVersion()));
            }
            if (key.equals(STORES_KEY)) {
                storeNamesToDelete.removeAll(specifiedStoreNames);
                resetStoreDefinitions(storeNamesToDelete);
            }
            // Re-initialize the store definitions
            initStoreDefinitions(value.getVersion());
            // Update routing strategies
            updateRoutingStrategies(getCluster(), getStoreDefList());
        } else if (METADATA_KEYS.contains(key)) {
            // try inserting into inner store first
            putInner(key, convertObjectToString(key, value));
            // cache all keys if innerStore put succeeded
            metadataCache.put(key, value);
            // do special stuff if needed
            if (CLUSTER_KEY.equals(key)) {
                updateRoutingStrategies((Cluster) value.getValue(), getStoreDefList());
            } else if (NODE_ID_KEY.equals(key)) {
                initNodeId(getNodeIdNoLock());
            } else if (SYSTEM_STORES_KEY.equals(key))
                throw new VoldemortException("Cannot overwrite system store definitions");
        } else {
            throw new VoldemortException("Unhandled Key:" + key + " for MetadataStore put()");
        }
    } finally {
        writeLock.unlock();
    }
}
Also used : Versioned(voldemort.versioning.Versioned) StoreDefinitionsMapper(voldemort.xml.StoreDefinitionsMapper) Cluster(voldemort.cluster.Cluster) VoldemortException(voldemort.VoldemortException) StoreDefinition(voldemort.store.StoreDefinition) List(java.util.List) ArrayList(java.util.ArrayList) HashSet(java.util.HashSet)

Example 22 with Versioned

use of voldemort.versioning.Versioned in project voldemort by voldemort.

the class MetadataStore method validate.

@SuppressWarnings("unchecked")
public void validate(ByteArray keyBytes, Versioned<byte[]> valueBytes, byte[] transforms) throws VoldemortException {
    String key = ByteUtils.getString(keyBytes.get(), "UTF-8");
    Versioned<String> value = new Versioned<String>(ByteUtils.getString(valueBytes.getValue(), "UTF-8"), valueBytes.getVersion());
    Versioned<Object> valueObject = convertStringToObject(key, value);
    if (key.equals(MetadataStore.STORES_KEY)) {
        List<StoreDefinition> storeDefinitions = (List<StoreDefinition>) valueObject.getValue();
        Set<String> existingStores = new HashSet<String>(this.storeNames);
        Set<String> specifiedStoreNames = new HashSet<String>();
        for (StoreDefinition storeDef : storeDefinitions) {
            String storeName = storeDef.getName();
            if (specifiedStoreNames.contains(storeName)) {
                throw new VoldemortException(" Duplicate store names in Stores.xml for storeName " + storeName);
            }
            specifiedStoreNames.add(storeName);
        }
        existingStores.removeAll(specifiedStoreNames);
        // adding it back to allow again.
        if (existingStores.size() > 0) {
            logger.warn(" Set metadata does not support store deletion. This will leave the store in an " + "inconsistent state. Stores (Inconsistent) missing in set metadata " + Arrays.toString(existingStores.toArray()));
        }
        specifiedStoreNames.removeAll(this.storeNames);
        if (specifiedStoreNames.size() > 0) {
            logger.warn(" Set metadata does not support store addition . This will leave the store in an " + "inconsistent state. Stores (Inconsistent) added in set metadata " + Arrays.toString(specifiedStoreNames.toArray()));
        }
    }
}
Also used : Versioned(voldemort.versioning.Versioned) StoreDefinition(voldemort.store.StoreDefinition) List(java.util.List) ArrayList(java.util.ArrayList) VoldemortException(voldemort.VoldemortException) HashSet(java.util.HashSet)

Example 23 with Versioned

use of voldemort.versioning.Versioned in project voldemort by voldemort.

the class MetadataStore method createInMemoryMetadataStore.

public static MetadataStore createInMemoryMetadataStore(Store<String, String, String> innerStore, int nodeId) {
    StorageEngine<String, String, String> storesRepo = new InMemoryStorageEngine<String, String, String>("stores-repo");
    List<Versioned<String>> versionedStoreList = innerStore.get(STORES_KEY, "");
    if (versionedStoreList != null && versionedStoreList.size() > 0) {
        String stores = versionedStoreList.get(0).getValue();
        StoreDefinitionsMapper mapper = new StoreDefinitionsMapper();
        List<StoreDefinition> storeDefinitions = mapper.readStoreList(new StringReader(stores));
        for (StoreDefinition storeDef : storeDefinitions) {
            Versioned<String> versionedStoreValue = new Versioned<String>(mapper.writeStore(storeDef));
            storesRepo.put(storeDef.getName(), versionedStoreValue, null);
        }
    }
    MetadataStore store = new MetadataStore(innerStore, storesRepo);
    store.initNodeId(nodeId);
    return store;
}
Also used : InMemoryStorageEngine(voldemort.store.memory.InMemoryStorageEngine) Versioned(voldemort.versioning.Versioned) StoreDefinition(voldemort.store.StoreDefinition) StoreDefinitionsMapper(voldemort.xml.StoreDefinitionsMapper) StringReader(java.io.StringReader)

Example 24 with Versioned

use of voldemort.versioning.Versioned in project voldemort by voldemort.

the class ReadOnlyStorePerformanceTest method main.

public static void main(String[] args) throws FileNotFoundException, IOException {
    OptionParser parser = new OptionParser();
    parser.accepts("help", "print usage information");
    parser.accepts("threads", "number of threads").withRequiredArg().ofType(Integer.class);
    parser.accepts("requests", "[REQUIRED] number of requests").withRequiredArg().ofType(Integer.class);
    parser.accepts("store-dir", "[REQUIRED] store directory").withRequiredArg().describedAs("directory");
    parser.accepts("cluster-xml", "Path to cluster.xml").withRequiredArg().describedAs("path");
    parser.accepts("node-id", "Id of node").withRequiredArg().ofType(Integer.class).describedAs("node-id");
    parser.accepts("search-strategy", "class of the search strategy to use").withRequiredArg().describedAs("class_name");
    parser.accepts("build", "If present, first build the data");
    parser.accepts("num-values", "The number of values in the store").withRequiredArg().describedAs("count").ofType(Integer.class);
    parser.accepts("num-chunks", "The number of chunks per partition").withRequiredArg().describedAs("chunks").ofType(Integer.class);
    parser.accepts("internal-sort-size", "The number of items to sort in memory at a time").withRequiredArg().describedAs("size").ofType(Integer.class);
    parser.accepts("value-size", "The size of the values in the store").withRequiredArg().describedAs("size").ofType(Integer.class);
    parser.accepts("working-dir", "The directory in which to store temporary data").withRequiredArg().describedAs("dir");
    parser.accepts("gzip", "Compress the intermediate temp files used in building the store");
    parser.accepts("request-file", "file get request ids from").withRequiredArg();
    parser.accepts("version", "Version of read-only store [" + ReadOnlyStorageFormat.READONLY_V0 + "," + ReadOnlyStorageFormat.READONLY_V1 + "," + ReadOnlyStorageFormat.READONLY_V2 + " (default)]").withRequiredArg().describedAs("version");
    parser.accepts("test-gz", "Path to gzip containing data. Works with --build only").withRequiredArg().describedAs("path");
    OptionSet options = parser.parse(args);
    if (options.has("help")) {
        parser.printHelpOn(System.out);
        System.exit(0);
    }
    CmdUtils.croakIfMissing(parser, options, "requests", "store-dir");
    final int numThreads = CmdUtils.valueOf(options, "threads", 10);
    final int numRequests = (Integer) options.valueOf("requests");
    final int internalSortSize = CmdUtils.valueOf(options, "internal-sort-size", 500000);
    int numValues = numRequests;
    final String inputFile = (String) options.valueOf("request-file");
    final String searcherClass = CmdUtils.valueOf(options, "search-strategy", BinarySearchStrategy.class.getName()).trim();
    final boolean gzipIntermediate = options.has("gzip");
    final SearchStrategy searcher = (SearchStrategy) ReflectUtils.callConstructor(ReflectUtils.loadClass(searcherClass));
    final File workingDir = new File(CmdUtils.valueOf(options, "working-dir", System.getProperty("java.io.tmpdir")));
    String storeDir = (String) options.valueOf("store-dir");
    ReadOnlyStorageFormat format = ReadOnlyStorageFormat.fromCode(CmdUtils.valueOf(options, "version", ReadOnlyStorageFormat.READONLY_V2.toString()));
    Cluster cluster = null;
    int nodeId = 0;
    SerializerDefinition sdef = new SerializerDefinition("json", "'string'");
    StoreDefinition storeDef = new StoreDefinitionBuilder().setName("test").setKeySerializer(sdef).setValueSerializer(sdef).setRequiredReads(1).setReplicationFactor(1).setRequiredWrites(1).setType("read-only").setRoutingStrategyType(RoutingStrategyType.CONSISTENT_STRATEGY).setRoutingPolicy(RoutingTier.CLIENT).build();
    if (options.has("build")) {
        CmdUtils.croakIfMissing(parser, options, "num-values", "value-size");
        numValues = (Integer) options.valueOf("num-values");
        int numChunks = 1;
        if (options.has("num-chunks"))
            numChunks = (Integer) options.valueOf("num-chunks");
        int valueSize = (Integer) options.valueOf("value-size");
        // generate test data
        File temp = null;
        if (options.has("test-gz")) {
            temp = new File((String) options.valueOf("test-gz"));
        } else {
            temp = File.createTempFile("json-data", ".txt.gz", workingDir);
            temp.deleteOnExit();
            System.out.println("Generating test data in " + temp);
            OutputStream outputStream = new GZIPOutputStream(new FileOutputStream(temp));
            Writer writer = new BufferedWriter(new OutputStreamWriter(outputStream), 10 * 1024 * 1024);
            String value = TestUtils.randomLetters(valueSize);
            for (int i = 0; i < numValues; i++) {
                writer.write("\"");
                writer.write(Integer.toString(i));
                writer.write("\" \"");
                writer.write(value);
                writer.write("\"");
                writer.write("\n");
            }
            writer.close();
            writer = null;
        }
        System.out.println("Building store.");
        InputStream inputStream = new GZIPInputStream(new FileInputStream(temp));
        Reader r = new BufferedReader(new InputStreamReader(inputStream), 1 * 1024 * 1024);
        File output = TestUtils.createTempDir(workingDir);
        File tempDir = TestUtils.createTempDir(workingDir);
        cluster = ServerTestUtils.getLocalCluster(1);
        nodeId = 0;
        JsonStoreBuilder builder = new JsonStoreBuilder(new JsonReader(r), cluster, storeDef, new ConsistentRoutingStrategy(cluster, 1), output, tempDir, internalSortSize, 2, numChunks, 64 * 1024, gzipIntermediate);
        builder.build(format);
        // copy to store dir
        File dir = new File(storeDir);
        Utils.rm(dir);
        dir.mkdirs();
        System.out.println("Moving store data from " + output + " to " + dir);
        boolean copyWorked = new File(output, "node-0").renameTo(new File(dir, "version-0"));
        if (!copyWorked)
            Utils.croak("Copy of data from " + output + " to " + dir + " failed.");
    } else {
        CmdUtils.croakIfMissing(parser, options, "cluster-xml", "node-id");
        String clusterXmlPath = (String) options.valueOf("cluster-xml");
        nodeId = (Integer) options.valueOf("node-id");
        File clusterXml = new File(clusterXmlPath);
        if (!clusterXml.exists()) {
            Utils.croak("Cluster.xml does not exist");
        }
        cluster = new ClusterMapper().readCluster(clusterXml);
    }
    final Store<ByteArray, byte[], byte[]> store = new ReadOnlyStorageEngine("test", searcher, new RoutingStrategyFactory().updateRoutingStrategy(storeDef, cluster), nodeId, new File(storeDir), 0);
    final AtomicInteger obsoletes = new AtomicInteger(0);
    final AtomicInteger nullResults = new AtomicInteger(0);
    final AtomicInteger totalResults = new AtomicInteger(0);
    final BlockingQueue<String> requestIds = new ArrayBlockingQueue<String>(20000);
    final Executor executor = Executors.newFixedThreadPool(1);
    // if they have given us a file make a request generator that reads from
    // it, otherwise just generate random values
    final int numVals = numValues;
    Runnable requestGenerator;
    if (inputFile == null) {
        requestGenerator = new Runnable() {

            public void run() {
                System.out.println("Generating random requests.");
                Random random = new Random();
                try {
                    while (true) requestIds.put(Integer.toString(random.nextInt(numRequests) % numVals));
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
        };
    } else {
        requestGenerator = new Runnable() {

            public void run() {
                try {
                    System.out.println("Using request file to generate requests.");
                    BufferedReader reader = new BufferedReader(new FileReader(inputFile), 1000000);
                    while (true) {
                        String line = reader.readLine();
                        if (line == null)
                            return;
                        requestIds.put(line.trim());
                    }
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        };
    }
    executor.execute(requestGenerator);
    final Serializer<Object> keySerializer = new JsonTypeSerializer(JsonTypeDefinition.fromJson("'string'"), true);
    final AtomicInteger current = new AtomicInteger();
    final int progressIncrement = numRequests / 5;
    PerformanceTest readWriteTest = new PerformanceTest() {

        @Override
        public void doOperation(int index) throws Exception {
            try {
                totalResults.incrementAndGet();
                int curr = current.getAndIncrement();
                List<Versioned<byte[]>> results = store.get(new ByteArray(keySerializer.toBytes(requestIds.take())), null);
                if (curr % progressIncrement == 0)
                    System.out.println(curr);
                if (results.size() == 0)
                    nullResults.incrementAndGet();
            } catch (ObsoleteVersionException e) {
                obsoletes.incrementAndGet();
            }
        }
    };
    System.out.println("Running test...");
    readWriteTest.run(numRequests, numThreads);
    System.out.println("Random Access Read Only store Results:");
    System.out.println("Null reads ratio:" + (nullResults.doubleValue()) / totalResults.doubleValue());
    readWriteTest.printStats();
    System.exit(0);
}
Also used : BinarySearchStrategy(voldemort.store.readonly.BinarySearchStrategy) SearchStrategy(voldemort.store.readonly.SearchStrategy) GZIPOutputStream(java.util.zip.GZIPOutputStream) OutputStream(java.io.OutputStream) FileOutputStream(java.io.FileOutputStream) ArrayBlockingQueue(java.util.concurrent.ArrayBlockingQueue) Random(java.util.Random) GZIPOutputStream(java.util.zip.GZIPOutputStream) ByteArray(voldemort.utils.ByteArray) StoreDefinitionBuilder(voldemort.store.StoreDefinitionBuilder) ReadOnlyStorageEngine(voldemort.store.readonly.ReadOnlyStorageEngine) ReadOnlyStorageFormat(voldemort.store.readonly.ReadOnlyStorageFormat) ClusterMapper(voldemort.xml.ClusterMapper) FileInputStream(java.io.FileInputStream) JsonStoreBuilder(voldemort.store.readonly.JsonStoreBuilder) ObsoleteVersionException(voldemort.versioning.ObsoleteVersionException) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) FileOutputStream(java.io.FileOutputStream) File(java.io.File) JsonTypeSerializer(voldemort.serialization.json.JsonTypeSerializer) Versioned(voldemort.versioning.Versioned) RoutingStrategyFactory(voldemort.routing.RoutingStrategyFactory) Reader(java.io.Reader) JsonReader(voldemort.serialization.json.JsonReader) InputStreamReader(java.io.InputStreamReader) BufferedReader(java.io.BufferedReader) FileReader(java.io.FileReader) OptionParser(joptsimple.OptionParser) BufferedWriter(java.io.BufferedWriter) GZIPInputStream(java.util.zip.GZIPInputStream) Executor(java.util.concurrent.Executor) StoreDefinition(voldemort.store.StoreDefinition) JsonReader(voldemort.serialization.json.JsonReader) ConsistentRoutingStrategy(voldemort.routing.ConsistentRoutingStrategy) FileReader(java.io.FileReader) InputStreamReader(java.io.InputStreamReader) GZIPInputStream(java.util.zip.GZIPInputStream) FileInputStream(java.io.FileInputStream) InputStream(java.io.InputStream) Cluster(voldemort.cluster.Cluster) FileNotFoundException(java.io.FileNotFoundException) ObsoleteVersionException(voldemort.versioning.ObsoleteVersionException) IOException(java.io.IOException) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) BufferedReader(java.io.BufferedReader) OutputStreamWriter(java.io.OutputStreamWriter) OptionSet(joptsimple.OptionSet) SerializerDefinition(voldemort.serialization.SerializerDefinition) Writer(java.io.Writer) OutputStreamWriter(java.io.OutputStreamWriter) BufferedWriter(java.io.BufferedWriter)

Example 25 with Versioned

use of voldemort.versioning.Versioned in project voldemort by voldemort.

the class AdminServiceBasicTest method testQuery.

@Test
public void testQuery() {
    HashMap<ByteArray, byte[]> belongToAndInsideServer0 = new HashMap<ByteArray, byte[]>();
    HashMap<ByteArray, byte[]> belongToAndInsideServer1 = new HashMap<ByteArray, byte[]>();
    HashMap<ByteArray, byte[]> notBelongServer0ButInsideServer0 = new HashMap<ByteArray, byte[]>();
    HashMap<ByteArray, byte[]> belongToServer0ButOutsideBoth = new HashMap<ByteArray, byte[]>();
    HashMap<ByteArray, byte[]> notBelongToServer0AndOutsideBoth = new HashMap<ByteArray, byte[]>();
    Store<ByteArray, byte[], byte[]> store0 = getStore(0, testStoreName);
    Store<ByteArray, byte[], byte[]> store1 = getStore(1, testStoreName);
    HashMap<ByteArray, byte[]> entrySet = null;
    Iterator<ByteArray> keys = null;
    RoutingStrategy strategy = servers[0].getMetadataStore().getRoutingStrategy(testStoreName);
    while (true) {
        ByteArray key;
        byte[] value;
        if (keys == null || !keys.hasNext()) {
            entrySet = ServerTestUtils.createRandomKeyValuePairs(100);
            keys = entrySet.keySet().iterator();
        }
        key = keys.next();
        value = entrySet.get(key);
        List<Node> routedNodes = strategy.routeRequest(key.get());
        boolean keyShouldBeInNode0 = false;
        boolean keyShouldBeInNode1 = false;
        for (Node node : routedNodes) {
            keyShouldBeInNode0 = keyShouldBeInNode0 || (node.getId() == 0);
            keyShouldBeInNode1 = keyShouldBeInNode1 || (node.getId() == 1);
        }
        if (belongToAndInsideServer0.size() < 10) {
            if (keyShouldBeInNode0) {
                belongToAndInsideServer0.put(key, value);
                store0.put(key, new Versioned<byte[]>(value), null);
            }
        } else if (belongToAndInsideServer1.size() < 10) {
            if (keyShouldBeInNode1) {
                belongToAndInsideServer1.put(key, value);
                store1.put(key, new Versioned<byte[]>(value), null);
            }
        } else if (notBelongServer0ButInsideServer0.size() < 5) {
            if (!keyShouldBeInNode0) {
                notBelongServer0ButInsideServer0.put(key, value);
                store0.put(key, new Versioned<byte[]>(value), null);
            }
        } else if (belongToServer0ButOutsideBoth.size() < 5) {
            if (keyShouldBeInNode0) {
                belongToServer0ButOutsideBoth.put(key, value);
            }
        } else if (notBelongToServer0AndOutsideBoth.size() < 5) {
            if (!keyShouldBeInNode0) {
                notBelongToServer0AndOutsideBoth.put(key, value);
            }
        } else {
            break;
        }
    }
    ArrayList<ByteArray> belongToAndInsideServer0Keys = new ArrayList<ByteArray>(belongToAndInsideServer0.keySet());
    ArrayList<ByteArray> belongToAndInsideServer1Keys = new ArrayList<ByteArray>(belongToAndInsideServer1.keySet());
    ArrayList<ByteArray> notBelongServer0ButInsideServer0Keys = new ArrayList<ByteArray>(notBelongServer0ButInsideServer0.keySet());
    ArrayList<ByteArray> belongToServer0ButOutsideBothKeys = new ArrayList<ByteArray>(belongToServer0ButOutsideBoth.keySet());
    ArrayList<ByteArray> notBelongToServer0AndOutsideBothKeys = new ArrayList<ByteArray>(notBelongToServer0AndOutsideBoth.keySet());
    List<ByteArray> queryKeys;
    Iterator<QueryKeyResult> results;
    QueryKeyResult entry;
    // test one key on store 0
    queryKeys = new ArrayList<ByteArray>();
    queryKeys.add(belongToAndInsideServer0Keys.get(0));
    results = getAdminClient().streamingOps.queryKeys(0, testStoreName, queryKeys.iterator());
    assertTrue("Results should not be empty", results.hasNext());
    entry = results.next();
    assertEquals(queryKeys.get(0), entry.getKey());
    assertNull("There should not be exception in response", entry.getException());
    assertEquals("There should be only 1 value in versioned list", 1, entry.getValues().size());
    assertEquals("Two byte[] should be equal", 0, ByteUtils.compare(belongToAndInsideServer0.get(queryKeys.get(0)), entry.getValues().get(0).getValue()));
    assertFalse("There should be only one result", results.hasNext());
    // test one key belongs to but not exists in server 0
    queryKeys = new ArrayList<ByteArray>();
    queryKeys.add(belongToServer0ButOutsideBothKeys.get(0));
    results = getAdminClient().streamingOps.queryKeys(0, testStoreName, queryKeys.iterator());
    assertTrue("Results should not be empty", results.hasNext());
    entry = results.next();
    assertFalse("There should not be more results", results.hasNext());
    assertEquals("Not the right key", queryKeys.get(0), entry.getKey());
    assertFalse("There should not be exception", entry.hasException());
    assertTrue("There should be values", entry.hasValues());
    assertNotNull("Response should be non-null", entry.getValues());
    assertEquals("Value should be empty list", 0, entry.getValues().size());
    assertNull("There should not be exception", entry.getException());
    // test one key not exist and does not belong to server 0
    queryKeys = new ArrayList<ByteArray>();
    queryKeys.add(notBelongToServer0AndOutsideBothKeys.get(0));
    results = getAdminClient().streamingOps.queryKeys(0, testStoreName, queryKeys.iterator());
    assertTrue("Results should not be empty", results.hasNext());
    entry = results.next();
    assertFalse("There should not be more results", results.hasNext());
    assertEquals("Not the right key", queryKeys.get(0), entry.getKey());
    assertTrue("There should be exception", entry.hasException());
    assertFalse("There should not be values", entry.hasValues());
    assertNull("Value should be null", entry.getValues());
    assertTrue("There should be InvalidMetadataException exception", entry.getException() instanceof InvalidMetadataException);
    // test one key that exists on server 0 but does not belong to server 0
    queryKeys = new ArrayList<ByteArray>();
    queryKeys.add(notBelongServer0ButInsideServer0Keys.get(0));
    results = getAdminClient().streamingOps.queryKeys(0, testStoreName, queryKeys.iterator());
    assertTrue("Results should not be empty", results.hasNext());
    entry = results.next();
    assertFalse("There should not be more results", results.hasNext());
    assertEquals("Not the right key", queryKeys.get(0), entry.getKey());
    assertTrue("There should be exception", entry.hasException());
    assertFalse("There should not be values", entry.hasValues());
    assertNull("Value should be null", entry.getValues());
    assertTrue("There should be InvalidMetadataException exception", entry.getException() instanceof InvalidMetadataException);
    // test one key deleted
    store0.delete(belongToAndInsideServer0Keys.get(4), null);
    queryKeys = new ArrayList<ByteArray>();
    queryKeys.add(belongToAndInsideServer0Keys.get(4));
    results = getAdminClient().streamingOps.queryKeys(0, testStoreName, queryKeys.iterator());
    assertTrue("Results should not be empty", results.hasNext());
    entry = results.next();
    assertFalse("There should not be more results", results.hasNext());
    assertFalse("There should not be exception", entry.hasException());
    assertTrue("There should be values", entry.hasValues());
    assertEquals("Not the right key", queryKeys.get(0), entry.getKey());
    assertEquals("Value should be empty list", 0, entry.getValues().size());
    // test empty request
    queryKeys = new ArrayList<ByteArray>();
    results = getAdminClient().streamingOps.queryKeys(0, testStoreName, queryKeys.iterator());
    assertFalse("Results should be empty", results.hasNext());
    // test null key
    queryKeys = new ArrayList<ByteArray>();
    queryKeys.add(null);
    assertEquals(1, queryKeys.size());
    results = getAdminClient().streamingOps.queryKeys(0, testStoreName, queryKeys.iterator());
    assertTrue("Results should not be empty", results.hasNext());
    entry = results.next();
    assertFalse("There should not be more results", results.hasNext());
    assertTrue("There should be exception", entry.hasException());
    assertFalse("There should not be values", entry.hasValues());
    assertNull("Value should be null", entry.getValues());
    assertTrue("There should be IllegalArgumentException exception", entry.getException() instanceof IllegalArgumentException);
    // test multiple keys (3) on store 1
    queryKeys = new ArrayList<ByteArray>();
    queryKeys.add(belongToAndInsideServer1Keys.get(0));
    queryKeys.add(belongToAndInsideServer1Keys.get(1));
    queryKeys.add(belongToAndInsideServer1Keys.get(2));
    results = getAdminClient().streamingOps.queryKeys(1, testStoreName, queryKeys.iterator());
    assertTrue("Results should not be empty", results.hasNext());
    Map<ByteArray, List<Versioned<byte[]>>> entries = new HashMap<ByteArray, List<Versioned<byte[]>>>();
    int resultCount = 0;
    while (results.hasNext()) {
        resultCount++;
        entry = results.next();
        assertNull("There should not be exception in response", entry.getException());
        assertNotNull("Value should not be null for Key: ", entry.getValues());
        entries.put(entry.getKey(), entry.getValues());
    }
    assertEquals("There should 3 and only 3 results", 3, resultCount);
    for (ByteArray key : queryKeys) {
        // this loop and the count ensure one-to-one mapping
        assertNotNull("This key should exist in the results: " + key, entries.get(key));
        assertEquals("Two byte[] should be equal for key: " + key, 0, ByteUtils.compare(belongToAndInsideServer1.get(key), entries.get(key).get(0).getValue()));
    }
    // test multiple keys, mixed situation
    // key 0: Exists and belongs to
    // key 1: Exists but does not belong to
    // key 2: Does not exist but belongs to
    // key 3: Does not belong and not exist
    // key 4: Same situation with key0
    // key 5: Deleted
    // key 6: Same situation with key2
    store0.delete(belongToAndInsideServer0Keys.get(5), null);
    queryKeys = new ArrayList<ByteArray>();
    queryKeys.add(belongToAndInsideServer0Keys.get(2));
    queryKeys.add(notBelongServer0ButInsideServer0Keys.get(1));
    queryKeys.add(belongToServer0ButOutsideBothKeys.get(1));
    queryKeys.add(notBelongToServer0AndOutsideBothKeys.get(1));
    queryKeys.add(belongToAndInsideServer0Keys.get(3));
    queryKeys.add(belongToAndInsideServer0Keys.get(5));
    queryKeys.add(notBelongServer0ButInsideServer0Keys.get(2));
    results = getAdminClient().streamingOps.queryKeys(0, testStoreName, queryKeys.iterator());
    // key 0
    entry = results.next();
    assertEquals(0, ByteUtils.compare(queryKeys.get(0).get(), entry.getKey().get()));
    assertEquals(0, ByteUtils.compare(belongToAndInsideServer0.get(queryKeys.get(0)), entry.getValues().get(0).getValue()));
    assertNull(entry.getException());
    // key 1
    entry = results.next();
    assertEquals(0, ByteUtils.compare(queryKeys.get(1).get(), entry.getKey().get()));
    assertTrue("There should be InvalidMetadataException exception", entry.getException() instanceof InvalidMetadataException);
    // key 2
    entry = results.next();
    assertEquals(0, ByteUtils.compare(queryKeys.get(2).get(), entry.getKey().get()));
    assertEquals(0, entry.getValues().size());
    assertNull(entry.getException());
    // key 3
    entry = results.next();
    assertEquals(0, ByteUtils.compare(queryKeys.get(3).get(), entry.getKey().get()));
    assertTrue("There should be InvalidMetadataException exception", entry.getException() instanceof InvalidMetadataException);
    // key 4
    entry = results.next();
    assertEquals(0, ByteUtils.compare(queryKeys.get(4).get(), entry.getKey().get()));
    assertEquals(0, ByteUtils.compare(belongToAndInsideServer0.get(queryKeys.get(4)), entry.getValues().get(0).getValue()));
    assertNull(entry.getException());
    // key 5
    entry = results.next();
    assertEquals(0, ByteUtils.compare(queryKeys.get(5).get(), entry.getKey().get()));
    assertEquals(0, entry.getValues().size());
    assertNull(entry.getException());
    // key 6
    entry = results.next();
    assertEquals(0, ByteUtils.compare(queryKeys.get(6).get(), entry.getKey().get()));
    assertTrue("There should be InvalidMetadataException exception", entry.getException() instanceof InvalidMetadataException);
    // no more keys
    assertFalse(results.hasNext());
}
Also used : Versioned(voldemort.versioning.Versioned) HashMap(java.util.HashMap) Node(voldemort.cluster.Node) InvalidMetadataException(voldemort.store.InvalidMetadataException) ArrayList(java.util.ArrayList) QueryKeyResult(voldemort.client.protocol.admin.QueryKeyResult) RoutingStrategy(voldemort.routing.RoutingStrategy) ByteArray(voldemort.utils.ByteArray) List(java.util.List) ArrayList(java.util.ArrayList) Test(org.junit.Test)

Aggregations

Versioned (voldemort.versioning.Versioned)214 ByteArray (voldemort.utils.ByteArray)130 Test (org.junit.Test)88 VectorClock (voldemort.versioning.VectorClock)73 ArrayList (java.util.ArrayList)56 VoldemortException (voldemort.VoldemortException)48 List (java.util.List)36 StoreDefinition (voldemort.store.StoreDefinition)29 HashMap (java.util.HashMap)28 IOException (java.io.IOException)27 Node (voldemort.cluster.Node)27 Pair (voldemort.utils.Pair)25 Slop (voldemort.store.slop.Slop)22 ObsoleteVersionException (voldemort.versioning.ObsoleteVersionException)22 Map (java.util.Map)21 Cluster (voldemort.cluster.Cluster)17 AbstractByteArrayStoreTest (voldemort.store.AbstractByteArrayStoreTest)15 InvalidMetadataException (voldemort.store.InvalidMetadataException)13 PersistenceFailureException (voldemort.store.PersistenceFailureException)13 Version (voldemort.versioning.Version)13