Search in sources :

Example 11 with VectorClock

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

the class MysqlStorageEngine method put.

@Override
public void put(ByteArray key, Versioned<byte[]> value, byte[] transforms) throws PersistenceFailureException {
    StoreUtils.assertValidKey(key);
    boolean doCommit = false;
    Connection conn = null;
    PreparedStatement insert = null;
    PreparedStatement select = null;
    ResultSet results = null;
    String insertSql = "insert into " + getName() + " (key_, version_, value_) values (?, ?, ?)";
    String selectSql = "select version_ from " + getName() + " where key_ = ?";
    try {
        conn = datasource.getConnection();
        conn.setAutoCommit(false);
        // check for superior versions
        select = conn.prepareStatement(selectSql);
        select.setBytes(1, key.get());
        results = select.executeQuery();
        while (results.next()) {
            VectorClock version = new VectorClock(results.getBytes("version_"));
            Occurred occurred = value.getVersion().compare(version);
            if (occurred == Occurred.BEFORE)
                throw new ObsoleteVersionException("Attempt to put version " + value.getVersion() + " which is superceeded by " + version + ".");
            else if (occurred == Occurred.AFTER)
                delete(conn, key.get(), version.toBytes());
        }
        // Okay, cool, now put the value
        insert = conn.prepareStatement(insertSql);
        insert.setBytes(1, key.get());
        VectorClock clock = (VectorClock) value.getVersion();
        insert.setBytes(2, clock.toBytes());
        insert.setBytes(3, value.getValue());
        insert.executeUpdate();
        doCommit = true;
    } catch (SQLException e) {
        if (e.getErrorCode() == MYSQL_ERR_DUP_KEY || e.getErrorCode() == MYSQL_ERR_DUP_ENTRY) {
            throw new ObsoleteVersionException("Key or value already used.");
        } else {
            throw new PersistenceFailureException("Fix me!", e);
        }
    } finally {
        if (conn != null) {
            try {
                if (doCommit)
                    conn.commit();
                else
                    conn.rollback();
            } catch (SQLException e) {
            }
        }
        tryClose(results);
        tryClose(insert);
        tryClose(select);
        tryClose(conn);
    }
}
Also used : ObsoleteVersionException(voldemort.versioning.ObsoleteVersionException) SQLException(java.sql.SQLException) VectorClock(voldemort.versioning.VectorClock) Connection(java.sql.Connection) ResultSet(java.sql.ResultSet) PreparedStatement(java.sql.PreparedStatement) PersistenceFailureException(voldemort.store.PersistenceFailureException) Occurred(voldemort.versioning.Occurred)

Example 12 with VectorClock

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

the class QuotaUtils method setQuota.

public static void setQuota(String storeName, QuotaType type, StoreRepository repository, Set<Integer> nodeIds, long lquota) {
    FileBackedCachingStorageEngine quotaStore = getQuotaStore(repository);
    String quotaKey = makeQuotaKey(storeName, QuotaType.STORAGE_SPACE);
    ByteArray keyArray = convertToByteArray(quotaKey);
    List<Versioned<byte[]>> existingValue = quotaStore.get(keyArray, null);
    String quotaValue = Long.toString(lquota);
    ByteArray valueArray = convertToByteArray(quotaValue);
    VectorClock newClock = VectorClockUtils.makeClockWithCurrentTime(nodeIds);
    Versioned<byte[]> newValue = new Versioned<byte[]>(valueArray.get(), newClock);
    quotaStore.put(keyArray, newValue, null);
}
Also used : FileBackedCachingStorageEngine(voldemort.store.configuration.FileBackedCachingStorageEngine) Versioned(voldemort.versioning.Versioned) VectorClock(voldemort.versioning.VectorClock) ByteArray(voldemort.utils.ByteArray)

Example 13 with VectorClock

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

the class StorageEnginePerformanceTest method main.

public static void main(String[] args) throws Exception {
    try {
        OptionParser parser = new OptionParser();
        parser.accepts("help", "print usage information");
        parser.accepts("requests", "[REQUIRED] number of requests to execute").withRequiredArg().ofType(Integer.class);
        parser.accepts("num-values", "[REQUIRED] number of values in the store").withRequiredArg().ofType(Integer.class);
        parser.accepts("data-dir", "Data directory for storage data").withRequiredArg().describedAs("directory");
        parser.accepts("threads", "number of threads").withRequiredArg().ofType(Integer.class);
        parser.accepts("storage-configuration-class", "[REQUIRED] class of the storage engine configuration to use [e.g. voldemort.store.bdb.BdbStorageConfiguration]").withRequiredArg().describedAs("class_name");
        parser.accepts("props", "Properties file with configuration for the engine").withRequiredArg().describedAs("config.properties");
        parser.accepts("value-size", "The size of the values in the store").withRequiredArg().describedAs("size").ofType(Integer.class);
        parser.accepts("cache-width", "Percentage of requests to save as possible re-requests").withRequiredArg().describedAs("width").ofType(Integer.class);
        parser.accepts("cache-hit-ratio", "Percentage of requests coming from the last cache-width requests").withRequiredArg().describedAs("ratio").ofType(Double.class);
        parser.accepts("clean-up", "Delete data directory when done.");
        OptionSet options = parser.parse(args);
        if (options.has("help")) {
            parser.printHelpOn(System.out);
            System.exit(0);
        }
        CmdUtils.croakIfMissing(parser, options, "requests");
        final int numThreads = CmdUtils.valueOf(options, "threads", 10);
        final int numRequests = (Integer) options.valueOf("requests");
        final int numValues = (Integer) options.valueOf("num-values");
        final int valueSize = CmdUtils.valueOf(options, "value-size", 1024);
        final int cacheWidth = CmdUtils.valueOf(options, "cache-width", 100000);
        final double cacheHitRatio = CmdUtils.valueOf(options, "cache-hit-ratio", 0.5);
        final String propsFile = (String) options.valueOf("props");
        final boolean cleanUp = options.has("clean-up");
        final String storageEngineClass = CmdUtils.valueOf(options, "storage-configuration-class", BdbStorageConfiguration.class.getName()).trim();
        File dataDir = null;
        if (options.has("data-dir"))
            dataDir = new File((String) options.valueOf("data-dir"));
        else
            dataDir = TestUtils.createTempDir();
        System.out.println("Data dir: " + dataDir);
        // create the storage engine
        Props props = new Props();
        if (propsFile != null)
            props = new Props(new File(propsFile));
        props.put("node.id", 0);
        props.put("data.directory", dataDir.getAbsolutePath());
        props.put("voldemort.home", System.getProperty("user.dir"));
        VoldemortConfig config = new VoldemortConfig(props);
        StorageConfiguration storageConfig = (StorageConfiguration) ReflectUtils.callConstructor(ReflectUtils.loadClass(storageEngineClass), new Object[] { config });
        StorageEngine<ByteArray, byte[], byte[]> engine = storageConfig.getStore(TestUtils.makeStoreDefinition("test"), TestUtils.makeSingleNodeRoutingStrategy());
        @SuppressWarnings("unchecked") final Store<String, byte[], byte[]> store = new SerializingStore(engine, new StringSerializer(), new IdentitySerializer(), null);
        final byte[] value = new byte[valueSize];
        new Random().nextBytes(value);
        // initialize test data
        for (int i = 0; i < numValues; i++) store.put(Integer.toString(i), Versioned.value(value), null);
        // initialize cache lookback data
        int[] recents = new int[cacheWidth];
        System.out.println("Write test:");
        CachedPerformanceTest writeTest = new CachedPerformanceTest(new PerformanceTest() {

            @Override
            public void doOperation(int index) {
                try {
                    String key = Integer.toString(index);
                    List<Versioned<byte[]>> vs = store.get(key, null);
                    VectorClock version;
                    if (vs.size() == 0)
                        version = new VectorClock();
                    else
                        version = (VectorClock) vs.get(0).getVersion();
                    version.incrementVersion(0, 847584375);
                    store.put(key, Versioned.value(value, version), null);
                } catch (ObsoleteVersionException e) {
                // do nothing
                } catch (RuntimeException e) {
                    e.printStackTrace();
                    throw e;
                }
            }
        }, recents, numValues, cacheHitRatio);
        writeTest.run(numRequests, numThreads);
        writeTest.printStats();
        System.out.println();
        System.out.println("Read test:");
        CachedPerformanceTest readTest = new CachedPerformanceTest(new PerformanceTest() {

            @Override
            public void doOperation(int index) {
                store.get(Integer.toString(index), null);
            }
        }, recents, numValues, cacheHitRatio);
        readTest.run(numRequests, numThreads);
        readTest.printStats();
        if (cleanUp)
            Utils.rm(dataDir);
    } catch (Exception e) {
        e.printStackTrace();
        System.exit(1);
    }
}
Also used : Props(voldemort.utils.Props) OptionParser(joptsimple.OptionParser) VoldemortConfig(voldemort.server.VoldemortConfig) Random(java.util.Random) ByteArray(voldemort.utils.ByteArray) IdentitySerializer(voldemort.serialization.IdentitySerializer) List(java.util.List) SerializingStore(voldemort.store.serialized.SerializingStore) StringSerializer(voldemort.serialization.StringSerializer) VectorClock(voldemort.versioning.VectorClock) BdbStorageConfiguration(voldemort.store.bdb.BdbStorageConfiguration) StorageConfiguration(voldemort.store.StorageConfiguration) ObsoleteVersionException(voldemort.versioning.ObsoleteVersionException) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) ObsoleteVersionException(voldemort.versioning.ObsoleteVersionException) OptionSet(joptsimple.OptionSet) File(java.io.File)

Example 14 with VectorClock

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

the class AdminServiceBasicTest method testUpdateClusterMetadata.

@Test
public void testUpdateClusterMetadata() {
    Cluster updatedCluster = ServerTestUtils.getLocalCluster(4);
    AdminClient client = getAdminClient();
    for (int i = 0; i < NUM_RUNS; i++) {
        VectorClock existingClock = ((VectorClock) client.metadataMgmtOps.getRemoteCluster(0).getVersion());
        VectorClock clock = existingClock.incremented(0, System.currentTimeMillis());
        client.metadataMgmtOps.updateRemoteCluster(0, updatedCluster, clock);
        assertEquals("Cluster should match", updatedCluster, getVoldemortServer(0).getMetadataStore().getCluster());
        assertEquals("AdminClient.getMetdata() should match", client.metadataMgmtOps.getRemoteCluster(0).getValue(), updatedCluster);
        // version should match
        assertEquals("versions should match as well.", clock, client.metadataMgmtOps.getRemoteCluster(0).getVersion());
    }
}
Also used : VectorClock(voldemort.versioning.VectorClock) Cluster(voldemort.cluster.Cluster) AdminClient(voldemort.client.protocol.admin.AdminClient) Test(org.junit.Test)

Example 15 with VectorClock

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

the class StorageService method initializeMetadataVersions.

protected void initializeMetadataVersions(List<StoreDefinition> storeDefs) {
    Store<ByteArray, byte[], byte[]> versionStore = storeRepository.getLocalStore(SystemStoreConstants.SystemStoreName.voldsys$_metadata_version_persistence.name());
    Properties props = new Properties();
    try {
        boolean isPropertyAdded = false;
        ByteArray metadataVersionsKey = new ByteArray(SystemStoreConstants.VERSIONS_METADATA_KEY.getBytes());
        List<Versioned<byte[]>> versionList = versionStore.get(metadataVersionsKey, null);
        VectorClock newClock = null;
        if (versionList != null && versionList.size() > 0) {
            byte[] versionsByteArray = versionList.get(0).getValue();
            if (versionsByteArray != null) {
                props.load(new ByteArrayInputStream(versionsByteArray));
            }
            newClock = (VectorClock) versionList.get(0).getVersion();
            newClock = newClock.incremented(0, System.currentTimeMillis());
        } else {
            newClock = new VectorClock();
        }
        // Check if version exists for cluster.xml
        if (!props.containsKey(SystemStoreConstants.CLUSTER_VERSION_KEY)) {
            props.setProperty(SystemStoreConstants.CLUSTER_VERSION_KEY, "0");
            isPropertyAdded = true;
        }
        // Check if version exists for stores.xml
        if (!props.containsKey(SystemStoreConstants.STORES_VERSION_KEY)) {
            props.setProperty(SystemStoreConstants.STORES_VERSION_KEY, "0");
            isPropertyAdded = true;
        }
        // Check if version exists for each store
        for (StoreDefinition def : storeDefs) {
            if (!props.containsKey(def.getName())) {
                props.setProperty(def.getName(), "0");
                isPropertyAdded = true;
            }
        }
        if (isPropertyAdded) {
            StringBuilder finalVersionList = new StringBuilder();
            for (String propName : props.stringPropertyNames()) {
                finalVersionList.append(propName + "=" + props.getProperty(propName) + "\n");
            }
            versionStore.put(metadataVersionsKey, new Versioned<byte[]>(finalVersionList.toString().getBytes(), newClock), null);
        }
    } catch (Exception e) {
        logger.error("Error while intializing metadata versions ", e);
    }
}
Also used : Versioned(voldemort.versioning.Versioned) VectorClock(voldemort.versioning.VectorClock) Properties(java.util.Properties) ConfigurationException(voldemort.utils.ConfigurationException) NoSuchCapabilityException(voldemort.store.NoSuchCapabilityException) DisabledStoreException(voldemort.store.DisabledStoreException) VoldemortException(voldemort.VoldemortException) ByteArrayInputStream(java.io.ByteArrayInputStream) StoreDefinition(voldemort.store.StoreDefinition) ByteArray(voldemort.utils.ByteArray)

Aggregations

VectorClock (voldemort.versioning.VectorClock)162 Versioned (voldemort.versioning.Versioned)73 Test (org.junit.Test)65 ByteArray (voldemort.utils.ByteArray)65 ArrayList (java.util.ArrayList)33 IOException (java.io.IOException)25 VoldemortException (voldemort.VoldemortException)24 List (java.util.List)22 HashMap (java.util.HashMap)21 ObsoleteVersionException (voldemort.versioning.ObsoleteVersionException)21 Version (voldemort.versioning.Version)16 Node (voldemort.cluster.Node)15 AbstractByteArrayStoreTest (voldemort.store.AbstractByteArrayStoreTest)11 StoreDefinition (voldemort.store.StoreDefinition)11 AdminClient (voldemort.client.protocol.admin.AdminClient)10 VoldemortServer (voldemort.server.VoldemortServer)10 Pair (voldemort.utils.Pair)10 ClockEntry (voldemort.versioning.ClockEntry)10 File (java.io.File)8 Map (java.util.Map)8