Search in sources :

Example 6 with Occurred

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

the class AbstractStorageEngine method resolveAndConstructVersionsToPersist.

/**
     * Computes the final list of versions to be stored, on top of what is
     * currently being stored. Final list is valuesInStorage modified in place
     * 
     * 
     * @param valuesInStorage list of versions currently in storage
     * @param multiPutValues list of new versions being written to storage
     * @return list of versions from multiPutVals that were rejected as obsolete
     */
protected List<Versioned<V>> resolveAndConstructVersionsToPersist(List<Versioned<V>> valuesInStorage, List<Versioned<V>> multiPutValues) {
    List<Versioned<V>> obsoleteVals = new ArrayList<Versioned<V>>(multiPutValues.size());
    // acceptable
    for (Versioned<V> value : multiPutValues) {
        Iterator<Versioned<V>> iter = valuesInStorage.iterator();
        boolean obsolete = false;
        // Compare the current version with a set of accepted versions
        while (iter.hasNext()) {
            Versioned<V> curr = iter.next();
            Occurred occurred = value.getVersion().compare(curr.getVersion());
            if (occurred == Occurred.BEFORE) {
                obsolete = true;
                break;
            } else if (occurred == Occurred.AFTER) {
                iter.remove();
            }
        }
        if (obsolete) {
            // add to return value if obsolete
            obsoleteVals.add(value);
        } else {
            // else update the set of accepted versions
            valuesInStorage.add(value);
        }
    }
    return obsoleteVals;
}
Also used : Versioned(voldemort.versioning.Versioned) ArrayList(java.util.ArrayList) Occurred(voldemort.versioning.Occurred)

Example 7 with Occurred

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

the class ReadRepairer method singleKeyGetRepairs.

private List<NodeValue<K, V>> singleKeyGetRepairs(List<NodeValue<K, V>> nodeValues) {
    int size = nodeValues.size();
    if (size <= 1)
        return Collections.emptyList();
    // 1. Create a multi-map of nodes to their existing Versions
    Multimap<Integer, NodeValue<K, V>> nodeVersionsMap = HashMultimap.create();
    for (NodeValue<K, V> nodeValue : nodeValues) {
        nodeVersionsMap.put(nodeValue.getNodeId(), nodeValue);
    }
    // 2. Create a map of the final set of versions (for this key)
    Map<Version, NodeValue<K, V>> mostCurrentVersionsMap = new HashMap<Version, NodeValue<K, V>>();
    // Initialize with the first element from the input
    mostCurrentVersionsMap.put(nodeValues.get(0).getVersion(), nodeValues.get(0));
    // check each value against the current set of most current versions
    for (int i = 1; i < nodeValues.size(); i++) {
        NodeValue<K, V> curr = nodeValues.get(i);
        boolean concurrentToAll = true;
        /*
             * Make a copy for the traversal. This is because the original map
             * can be modified during this traversal
             */
        Set<Version> knownGoodVersions = new HashSet<Version>(mostCurrentVersionsMap.keySet());
        for (Version currentGoodversion : knownGoodVersions) {
            // If the version already exists, do nothing
            if (curr.getVersion().equals(currentGoodversion)) {
                concurrentToAll = false;
                if (logger.isDebugEnabled()) {
                    logger.debug("Version already exists in the most current set: " + curr);
                }
                break;
            }
            // Check the ordering of the current value
            Occurred occurred = curr.getVersion().compare(currentGoodversion);
            if (occurred == Occurred.BEFORE) {
                // This value is obsolete! Break from the loop
                if (logger.isDebugEnabled()) {
                    logger.debug("Version is obsolete : " + curr);
                }
                concurrentToAll = false;
                break;
            } else if (occurred == Occurred.AFTER) {
                // This concurrent value is obsolete and the current value
                // should replace it
                mostCurrentVersionsMap.remove(currentGoodversion);
                concurrentToAll = false;
                mostCurrentVersionsMap.put(curr.getVersion(), curr);
                if (logger.isDebugEnabled()) {
                    logger.debug("Updating the current best - adding : " + curr);
                }
            }
        }
        // to the concurrent set
        if (concurrentToAll) {
            mostCurrentVersionsMap.put(curr.getVersion(), curr);
            if (logger.isDebugEnabled()) {
                logger.debug("Value is concurrent to all ! : " + curr);
            }
        }
    }
    // 3. Compare 1 and 2 and create the repair list
    List<NodeValue<K, V>> repairs = new ArrayList<NodeValue<K, V>>(3);
    for (int nodeId : nodeVersionsMap.keySet()) {
        Set<Version> finalVersions = new HashSet<Version>(mostCurrentVersionsMap.keySet());
        if (logger.isDebugEnabled()) {
            logger.debug("Set of final versions = " + finalVersions);
        }
        // Calculate the set difference between final Versions and
        // the versions currently existing for nodeId
        Set<Version> currentNodeVersions = new HashSet<Version>();
        for (NodeValue<K, V> nodeValue : nodeVersionsMap.get(nodeId)) {
            currentNodeVersions.add(nodeValue.getVersion());
        }
        finalVersions.removeAll(currentNodeVersions);
        if (logger.isDebugEnabled()) {
            logger.debug("Remaining versions to be repaired for this node after the set difference = " + finalVersions);
        }
        // Repair nodeId with the remaining Versioned values
        for (Version remainingVersion : finalVersions) {
            NodeValue<K, V> repair = new NodeValue<K, V>(nodeId, mostCurrentVersionsMap.get(remainingVersion).getKey(), mostCurrentVersionsMap.get(remainingVersion).getVersioned());
            if (logger.isDebugEnabled()) {
                logger.debug("Node value marked to be repaired : " + repair);
            }
            repairs.add(repair);
        }
    }
    return repairs;
}
Also used : HashMap(java.util.HashMap) ArrayList(java.util.ArrayList) Version(voldemort.versioning.Version) Occurred(voldemort.versioning.Occurred) HashSet(java.util.HashSet)

Aggregations

Occurred (voldemort.versioning.Occurred)7 ObsoleteVersionException (voldemort.versioning.ObsoleteVersionException)5 Versioned (voldemort.versioning.Versioned)5 ArrayList (java.util.ArrayList)4 PersistenceFailureException (voldemort.store.PersistenceFailureException)3 DatabaseEntry (com.sleepycat.je.DatabaseEntry)1 DatabaseException (com.sleepycat.je.DatabaseException)1 OperationStatus (com.sleepycat.je.OperationStatus)1 Transaction (com.sleepycat.je.Transaction)1 IOException (java.io.IOException)1 Connection (java.sql.Connection)1 PreparedStatement (java.sql.PreparedStatement)1 ResultSet (java.sql.ResultSet)1 SQLException (java.sql.SQLException)1 HashMap (java.util.HashMap)1 HashSet (java.util.HashSet)1 RocksDBException (org.rocksdb.RocksDBException)1 VoldemortException (voldemort.VoldemortException)1 AsyncOperationStatus (voldemort.server.protocol.admin.AsyncOperationStatus)1 VectorClock (voldemort.versioning.VectorClock)1