Search in sources :

Example 6 with Keyspace

use of com.netflix.astyanax.Keyspace in project coprhd-controller by CoprHD.

the class DbConsistencyCheckerHelperTest method testCheckIndexingCF_SkipRecordWithNoInactiveColumn.

@Test
public void testCheckIndexingCF_SkipRecordWithNoInactiveColumn() throws Exception {
    ColumnFamily<String, CompositeColumnName> cf = new ColumnFamily<String, CompositeColumnName>("FileShare", StringSerializer.get(), CompositeColumnNameSerializer.get());
    FileShare testData = new FileShare();
    testData.setId(URIUtil.createId(FileShare.class));
    testData.setPath("path1");
    testData.setMountPath("mountPath1");
    getDbClient().createObject(testData);
    Keyspace keyspace = ((DbClientImpl) getDbClient()).getLocalContext().getKeyspace();
    keyspace.prepareQuery(cf).withCql(String.format("delete from \"FileShare\" where key='%s' and column1='inactive'", testData.getId().toString())).execute();
    CheckResult checkResult = new CheckResult();
    helper.checkCFIndices(TypeMap.getDoType(FileShare.class), false, checkResult);
    assertEquals(0, checkResult.getTotal());
    testData = new FileShare();
    testData.setId(URIUtil.createId(FileShare.class));
    testData.setPath("path1");
    testData.setMountPath("mountPath1");
    getDbClient().createObject(testData);
    testData = (FileShare) getDbClient().queryObject(testData.getId());
    testData.setInactive(true);
    getDbClient().updateObject(testData);
    helper.checkCFIndices(TypeMap.getDoType(FileShare.class), false, checkResult);
    assertEquals(0, checkResult.getTotal());
}
Also used : CompositeColumnName(com.emc.storageos.db.client.impl.CompositeColumnName) Keyspace(com.netflix.astyanax.Keyspace) CheckResult(com.emc.storageos.db.client.impl.DbConsistencyCheckerHelper.CheckResult) FileShare(com.emc.storageos.db.client.model.FileShare) ColumnFamily(com.netflix.astyanax.model.ColumnFamily) Test(org.junit.Test)

Example 7 with Keyspace

use of com.netflix.astyanax.Keyspace in project coprhd-controller by CoprHD.

the class DbConsistencyCheckerHelperTest method testCheckIndexingCF.

@Test
public void testCheckIndexingCF() throws Exception {
    ColumnFamily<String, CompositeColumnName> cf = new ColumnFamily<String, CompositeColumnName>("FileShare", StringSerializer.get(), CompositeColumnNameSerializer.get());
    FileShare testData = new FileShare();
    testData.setId(URIUtil.createId(FileShare.class));
    testData.setPath("path1");
    testData.setMountPath("mountPath1");
    getDbClient().createObject(testData);
    Keyspace keyspace = ((DbClientImpl) getDbClient()).getLocalContext().getKeyspace();
    // delete data object
    MutationBatch mutationBatch = keyspace.prepareMutationBatch();
    mutationBatch.withRow(cf, testData.getId().toString()).delete();
    mutationBatch.execute();
    CheckResult checkResult = new CheckResult();
    ColumnFamily<String, IndexColumnName> indexCF = new ColumnFamily<String, IndexColumnName>("AltIdIndex", StringSerializer.get(), IndexColumnNameSerializer.get());
    // find inconsistency: index exits but data object is deleted
    IndexAndCf indexAndCf = new IndexAndCf(AltIdDbIndex.class, indexCF, keyspace);
    helper.checkIndexingCF(indexAndCf, false, checkResult);
    assertEquals(2, checkResult.getTotal());
    testData = new FileShare();
    testData.setId(URIUtil.createId(FileShare.class));
    testData.setPath("path2");
    testData.setMountPath("mountPath2");
    getDbClient().createObject(testData);
    // create duplicated index
    keyspace.prepareQuery(indexCF).withCql(String.format("INSERT INTO \"AltIdIndex\" (key, column1, column2, column3, column4, column5, value) VALUES ('pa', 'FileShare', '%s', '', '', now(), intasblob(10));", testData.getId().toString())).execute();
    checkResult = new CheckResult();
    helper.checkIndexingCF(indexAndCf, false, checkResult);
    assertEquals(3, checkResult.getTotal());
    keyspace.prepareQuery(indexCF).withCql("TRUNCATE \"AltIdIndex\"").execute();
    // test large columns for single row key
    for (int i = 0; i < 123; i++) {
        keyspace.prepareQuery(indexCF).withCql(String.format("INSERT INTO \"AltIdIndex\" (key, column1, column2, column3, column4, column5, value) VALUES ('sa', 'FileShare', '%s', '', '', now(), intasblob(10));", i)).execute();
    }
    checkResult = new CheckResult();
    helper.checkIndexingCF(indexAndCf, false, checkResult);
    assertEquals(123, checkResult.getTotal());
}
Also used : CompositeColumnName(com.emc.storageos.db.client.impl.CompositeColumnName) MutationBatch(com.netflix.astyanax.MutationBatch) CompositeIndexColumnName(com.emc.storageos.db.client.impl.CompositeIndexColumnName) TimeSeriesIndexColumnName(com.emc.storageos.db.client.impl.TimeSeriesIndexColumnName) ClassNameTimeSeriesIndexColumnName(com.emc.storageos.db.client.impl.ClassNameTimeSeriesIndexColumnName) IndexColumnName(com.emc.storageos.db.client.impl.IndexColumnName) Keyspace(com.netflix.astyanax.Keyspace) CheckResult(com.emc.storageos.db.client.impl.DbConsistencyCheckerHelper.CheckResult) IndexAndCf(com.emc.storageos.db.client.impl.DbConsistencyCheckerHelper.IndexAndCf) FileShare(com.emc.storageos.db.client.model.FileShare) ColumnFamily(com.netflix.astyanax.model.ColumnFamily) Test(org.junit.Test)

Example 8 with Keyspace

use of com.netflix.astyanax.Keyspace in project coprhd-controller by CoprHD.

the class DbClientImpl method internalPersistObject.

protected <T extends DataObject> void internalPersistObject(Class<? extends T> clazz, Collection<T> dataobjects, boolean updateIndex) {
    if (dataobjects == null || dataobjects.isEmpty()) {
        return;
    }
    Keyspace ks = getKeyspace(clazz);
    List<URI> objectsToCleanup = insertNewColumns(ks, dataobjects);
    if (updateIndex && !objectsToCleanup.isEmpty()) {
        Rows<String, CompositeColumnName> rows = fetchNewest(clazz, ks, objectsToCleanup);
        cleanupOldColumns(clazz, ks, rows);
    }
}
Also used : Keyspace(com.netflix.astyanax.Keyspace) NamedURI(com.emc.storageos.db.client.model.NamedURI) URI(java.net.URI)

Example 9 with Keyspace

use of com.netflix.astyanax.Keyspace in project coprhd-controller by CoprHD.

the class DbClientImpl method queryObject.

@Override
public <T extends DataObject> List<T> queryObject(Class<T> clazz, Collection<URI> ids, boolean activeOnly) {
    tracer.newTracer("read");
    DataObjectType doType = TypeMap.getDoType(clazz);
    if (doType == null) {
        throw new IllegalArgumentException();
    }
    if (!ids.iterator().hasNext()) {
        // nothing to do, just an empty list
        return new ArrayList<T>();
    }
    Keyspace ks = getKeyspace(clazz);
    Rows<String, CompositeColumnName> rows = queryRowsWithAllColumns(ks, ids, doType.getCF());
    List<T> objects = new ArrayList<T>(rows.size());
    IndexCleanupList cleanList = new IndexCleanupList();
    Iterator<Row<String, CompositeColumnName>> it = rows.iterator();
    while (it.hasNext()) {
        Row<String, CompositeColumnName> row = it.next();
        if (row == null || row.getColumns().size() == 0) {
            continue;
        }
        T object = doType.deserialize(clazz, row, cleanList, new LazyLoader(this));
        // filter base on activeOnly
        if (activeOnly) {
            if (!object.getInactive()) {
                objects.add(object);
            }
        } else {
            objects.add(object);
        }
    }
    if (!cleanList.isEmpty()) {
        boolean retryFailedWriteWithLocalQuorum = shouldRetryFailedWriteWithLocalQuorum(clazz);
        RowMutator mutator = new RowMutator(ks, retryFailedWriteWithLocalQuorum);
        SoftReference<IndexCleanupList> indexCleanUpRef = new SoftReference<IndexCleanupList>(cleanList);
        _indexCleaner.cleanIndexAsync(mutator, doType, indexCleanUpRef);
    }
    return objects;
}
Also used : SoftReference(java.lang.ref.SoftReference) Keyspace(com.netflix.astyanax.Keyspace) Row(com.netflix.astyanax.model.Row)

Example 10 with Keyspace

use of com.netflix.astyanax.Keyspace in project coprhd-controller by CoprHD.

the class DbClientImpl method queryObjectFields.

@Override
public <T extends DataObject> Collection<T> queryObjectFields(Class<T> clazz, Collection<String> fieldNames, Collection<URI> ids) {
    tracer.newTracer("read");
    DataObjectType doType = TypeMap.getDoType(clazz);
    if (doType == null || ids == null) {
        throw new IllegalArgumentException();
    }
    if (ids.isEmpty()) {
        // nothing to do, just an empty list
        return new ArrayList<T>();
    }
    Set<ColumnField> columnFields = new HashSet<ColumnField>(fieldNames.size());
    for (String fieldName : fieldNames) {
        ColumnField columnField = doType.getColumnField(fieldName);
        if (columnField == null) {
            throw new IllegalArgumentException();
        }
        columnFields.add(columnField);
    }
    Keyspace ks = getKeyspace(clazz);
    Map<URI, T> objectMap = new HashMap<URI, T>();
    for (ColumnField columnField : columnFields) {
        Rows<String, CompositeColumnName> rows = queryRowsWithAColumn(ks, ids, doType.getCF(), columnField);
        Iterator<Row<String, CompositeColumnName>> it = rows.iterator();
        while (it.hasNext()) {
            Row<String, CompositeColumnName> row = it.next();
            try {
                // we have to create an object to track both id and label, for now, lets use the same type
                if (row.getColumns().size() == 0) {
                    continue;
                }
                URI key = URI.create(row.getKey());
                T obj = objectMap.get(key);
                if (obj == null) {
                    obj = (T) DataObject.createInstance(clazz, URI.create(row.getKey()));
                    objectMap.put(key, obj);
                }
                Iterator<Column<CompositeColumnName>> columnIterator = row.getColumns().iterator();
                while (columnIterator.hasNext()) {
                    Column<CompositeColumnName> column = columnIterator.next();
                    columnField.deserialize(column, obj);
                }
            } catch (final InstantiationException e) {
                throw DatabaseException.fatals.queryFailed(e);
            } catch (final IllegalAccessException e) {
                throw DatabaseException.fatals.queryFailed(e);
            }
        }
    }
    // Begin tracking changes
    for (T obj : objectMap.values()) {
        obj.trackChanges();
    }
    return objectMap.values();
}
Also used : NamedURI(com.emc.storageos.db.client.model.NamedURI) URI(java.net.URI) Column(com.netflix.astyanax.model.Column) Keyspace(com.netflix.astyanax.Keyspace) Row(com.netflix.astyanax.model.Row)

Aggregations

Keyspace (com.netflix.astyanax.Keyspace)29 CompositeColumnName (com.emc.storageos.db.client.impl.CompositeColumnName)8 ConnectionException (com.netflix.astyanax.connectionpool.exceptions.ConnectionException)8 ColumnFamily (com.netflix.astyanax.model.ColumnFamily)8 Test (org.junit.Test)7 ClassNameTimeSeriesIndexColumnName (com.emc.storageos.db.client.impl.ClassNameTimeSeriesIndexColumnName)6 CheckResult (com.emc.storageos.db.client.impl.DbConsistencyCheckerHelper.CheckResult)6 Column (com.netflix.astyanax.model.Column)6 Row (com.netflix.astyanax.model.Row)6 TimeSeriesIndexColumnName (com.emc.storageos.db.client.impl.TimeSeriesIndexColumnName)5 Rows (com.netflix.astyanax.model.Rows)5 IndexAndCf (com.emc.storageos.db.client.impl.DbConsistencyCheckerHelper.IndexAndCf)4 IndexColumnName (com.emc.storageos.db.client.impl.IndexColumnName)4 FileShare (com.emc.storageos.db.client.model.FileShare)4 Order (com.emc.storageos.db.client.model.uimodels.Order)4 CompositeIndexColumnName (com.emc.storageos.db.client.impl.CompositeIndexColumnName)3 DataObject (com.emc.storageos.db.client.model.DataObject)3 DatabaseException (com.emc.storageos.db.exceptions.DatabaseException)3 KeyspaceDefinition (com.netflix.astyanax.ddl.KeyspaceDefinition)3 URI (java.net.URI)3