Search in sources :

Example 1 with Keyspace

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

the class InternalDbClient method resetFields.

public void resetFields(Class<? extends DataObject> clazz, Map<String, ColumnField> setFields, boolean ignore) throws Exception {
    DataObjectType doType = TypeMap.getDoType(clazz);
    if (doType == null) {
        throw new IllegalArgumentException();
    }
    try {
        Keyspace ks = getKeyspace(clazz);
        OperationResult<Rows<String, CompositeColumnName>> result = ks.prepareQuery(doType.getCF()).getAllRows().setRowLimit(DEFAULT_PAGE_SIZE).execute();
        Iterator<Row<String, CompositeColumnName>> it = result.getResult().iterator();
        RemovedColumnsList removedList = new RemovedColumnsList();
        List<DataObject> objects = new ArrayList<>(DEFAULT_PAGE_SIZE);
        String key = null;
        Exception lastEx = null;
        while (it.hasNext()) {
            try {
                Row<String, CompositeColumnName> row = it.next();
                if (row.getColumns().size() == 0) {
                    continue;
                }
                key = row.getKey();
                DataObject obj = DataObject.createInstance(clazz, URI.create(key));
                obj.trackChanges();
                objects.add(obj);
                Iterator<Column<CompositeColumnName>> columnIterator = row.getColumns().iterator();
                while (columnIterator.hasNext()) {
                    Column<CompositeColumnName> column = columnIterator.next();
                    ColumnField columnField = setFields.get(column.getName().getOne());
                    if (columnField != null) {
                        columnField.deserialize(column, obj);
                        removedList.add(key, column);
                    }
                }
                if (objects.size() == DEFAULT_PAGE_SIZE) {
                    boolean retryFailedWriteWithLocalQuorum = shouldRetryFailedWriteWithLocalQuorum(clazz);
                    RowMutator mutator = new RowMutator(ks, retryFailedWriteWithLocalQuorum);
                    _indexCleaner.removeColumnAndIndex(mutator, doType, removedList);
                    persistObject(objects);
                    objects.clear();
                    removedList.clear();
                }
            } catch (Exception e) {
                String message = String.format("DB migration failed reason: reset data key='%s'", key);
                log.error(message);
                log.error("e=", e);
                if (ignore) {
                    lastEx = e;
                    continue;
                }
                throw e;
            }
        }
        if (lastEx != null) {
            throw lastEx;
        }
        if (!objects.isEmpty()) {
            boolean retryFailedWriteWithLocalQuorum = shouldRetryFailedWriteWithLocalQuorum(clazz);
            RowMutator mutator = new RowMutator(ks, retryFailedWriteWithLocalQuorum);
            _indexCleaner.removeColumnAndIndex(mutator, doType, removedList);
            persistObject(objects);
        }
    } catch (ConnectionException e) {
        throw DatabaseException.retryables.connectionFailed(e);
    } catch (final InstantiationException e) {
        throw DatabaseException.fatals.queryFailed(e);
    } catch (final IllegalAccessException e) {
        throw DatabaseException.fatals.queryFailed(e);
    }
}
Also used : DatabaseException(com.emc.storageos.db.exceptions.DatabaseException) ConnectionException(com.netflix.astyanax.connectionpool.exceptions.ConnectionException) DataObject(com.emc.storageos.db.client.model.DataObject) Column(com.netflix.astyanax.model.Column) Keyspace(com.netflix.astyanax.Keyspace) Row(com.netflix.astyanax.model.Row) ConnectionException(com.netflix.astyanax.connectionpool.exceptions.ConnectionException) Rows(com.netflix.astyanax.model.Rows)

Example 2 with Keyspace

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

the class InternalDbClient method generateFieldIndex.

// TODO geo : migration only works for local keyspace; need to expand to use local or global
public <T extends DataObject> void generateFieldIndex(Class<T> clazz, String fieldName) {
    DataObjectType doType = TypeMap.getDoType(clazz);
    if (doType == null) {
        throw new IllegalArgumentException();
    }
    ColumnField columnField = doType.getColumnField(fieldName);
    if (columnField == null) {
        throw new IllegalArgumentException();
    }
    List<URI> allrecs = queryByType(clazz, false);
    Keyspace ks = getKeyspace(clazz);
    Iterator<URI> recIt = allrecs.iterator();
    List<URI> batch = getNextBatch(recIt);
    while (!batch.isEmpty()) {
        Rows<String, CompositeColumnName> rows = queryRowsWithAColumn(ks, batch, doType.getCF(), columnField);
        List<T> objects = new ArrayList<T>(rows.size());
        Iterator<Row<String, CompositeColumnName>> it = rows.iterator();
        while (it.hasNext()) {
            Row<String, CompositeColumnName> row = it.next();
            try {
                if (row.getColumns().size() == 0) {
                    continue;
                }
                DataObject obj = DataObject.createInstance(clazz, URI.create(row.getKey()));
                obj.trackChanges();
                Iterator<Column<CompositeColumnName>> columnIterator = row.getColumns().iterator();
                while (columnIterator.hasNext()) {
                    Column<CompositeColumnName> column = columnIterator.next();
                    columnField.deserialize(column, obj);
                }
                // set changed for ChangeTracking structures
                columnField.setChanged(obj);
                objects.add(clazz.cast(obj));
            } catch (final InstantiationException e) {
                throw DatabaseException.fatals.queryFailed(e);
            } catch (final IllegalAccessException e) {
                throw DatabaseException.fatals.queryFailed(e);
            }
        }
        updateAndReindexObject(objects);
        batch = getNextBatch(recIt);
    }
}
Also used : URI(java.net.URI) DataObject(com.emc.storageos.db.client.model.DataObject) Column(com.netflix.astyanax.model.Column) Keyspace(com.netflix.astyanax.Keyspace) Row(com.netflix.astyanax.model.Row)

Example 3 with Keyspace

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

the class FieldValueTimeUUIDPair method handleDataObjectClass.

public void handleDataObjectClass(Class<? extends DataObject> clazz) throws Exception {
    log.info("proccess model class {}", clazz);
    InternalDbClient dbClient = (InternalDbClient) getDbClient();
    DataObjectType doType = TypeMap.getDoType(clazz);
    Keyspace keyspace = dbClient.getLocalContext().getKeyspace();
    ColumnFamilyQuery<String, CompositeColumnName> query = keyspace.prepareQuery(doType.getCF());
    OperationResult<Rows<String, CompositeColumnName>> result = query.getAllRows().setRowLimit(100).execute();
    int totalCount = 0;
    List<Row<String, CompositeColumnName>> rows = new ArrayList<Row<String, CompositeColumnName>>();
    for (Row<String, CompositeColumnName> objRow : result.getResult()) {
        boolean inactiveObject = false;
        totalCount++;
        for (Column<CompositeColumnName> column : objRow.getColumns()) {
            if (DataObject.INACTIVE_FIELD_NAME.equals(column.getName().getOne()) && column.getBooleanValue()) {
                inactiveObject = true;
                break;
            }
        }
        if (inactiveObject) {
            continue;
        }
        rows.add(objRow);
        if (rows.size() > REBUILD_INDEX_BATCH_SIZE) {
            try {
                executor.submit(new RebuildIndexTask(doType, rows, keyspace));
                rows = new ArrayList<Row<String, CompositeColumnName>>();
            } catch (Exception e) {
                log.warn("Failed to submit rebuild index task, this may be caused by thread pool is full, try in next round", e);
            }
        }
    }
    executor.submit(new RebuildIndexTask(doType, rows, keyspace));
    log.info("Total data object count is {} for model {}", totalCount, clazz.getName());
    return;
}
Also used : CompositeColumnName(com.emc.storageos.db.client.impl.CompositeColumnName) InternalDbClient(com.emc.storageos.db.client.upgrade.InternalDbClient) ArrayList(java.util.ArrayList) MigrationCallbackException(com.emc.storageos.svcs.errorhandling.resources.MigrationCallbackException) InvocationTargetException(java.lang.reflect.InvocationTargetException) Keyspace(com.netflix.astyanax.Keyspace) Row(com.netflix.astyanax.model.Row) DataObjectType(com.emc.storageos.db.client.impl.DataObjectType) Rows(com.netflix.astyanax.model.Rows)

Example 4 with Keyspace

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

the class DbConsistencyCheckerHelperTest method testIsIndexExists.

@Test
public void testIsIndexExists() throws Exception {
    FileShare testData = new FileShare();
    testData.setId(URIUtil.createId(FileShare.class));
    testData.setPath("path1");
    testData.setMountPath("mountPath1");
    getDbClient().createObject(testData);
    ColumnFamily<String, IndexColumnName> indexCF = new ColumnFamily<String, IndexColumnName>("AltIdIndex", StringSerializer.get(), IndexColumnNameSerializer.get());
    Keyspace keyspace = ((DbClientImpl) getDbClient()).getLocalContext().getKeyspace();
    CompositeRangeBuilder builder = IndexColumnNameSerializer.get().buildRange();
    builder.withPrefix("FileShare").greaterThanEquals(testData.getId().toString()).lessThanEquals(testData.getId().toString());
    Rows<String, IndexColumnName> result = keyspace.prepareQuery(indexCF).getAllRows().withColumnRange(builder).execute().getResult();
    for (Row<String, IndexColumnName> row : result) {
        System.out.println(row.getColumns().getColumnByIndex(0).getName());
        assertTrue(helper.isIndexExists(keyspace, indexCF, row.getKey(), row.getColumns().getColumnByIndex(0).getName()));
    }
    ((DbClientImpl) getDbClient()).internalRemoveObjects(testData);
    for (Row<String, IndexColumnName> row : result) {
        assertFalse(helper.isIndexExists(keyspace, indexCF, row.getKey(), row.getColumns().getColumnByIndex(0).getName()));
    }
}
Also used : 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) DbClientImpl(com.emc.storageos.db.client.impl.DbClientImpl) Keyspace(com.netflix.astyanax.Keyspace) CompositeRangeBuilder(com.netflix.astyanax.serializers.CompositeRangeBuilder) FileShare(com.emc.storageos.db.client.model.FileShare) ColumnFamily(com.netflix.astyanax.model.ColumnFamily) Test(org.junit.Test)

Example 5 with Keyspace

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

the class DbConsistencyCheckerHelperTest method testTimeSeriesAlternateId.

@Test
public void testTimeSeriesAlternateId() throws Exception {
    DbConsistencyCheckerHelperMock mockHelper = new DbConsistencyCheckerHelperMock((DbClientImpl) getDbClient());
    Order order = new Order();
    order.setId(URIUtil.createId(Order.class));
    order.setLabel("order2");
    order.setTenant("tenant");
    order.setIndexed(true);
    getDbClient().createObject(order);
    Keyspace keyspace = ((DbClientImpl) getDbClient()).getLocalContext().getKeyspace();
    ColumnFamily<String, TimeSeriesIndexColumnName> indexCF = new ColumnFamily<String, TimeSeriesIndexColumnName>("AllOrdersByTimeStamp", StringSerializer.get(), TimeSeriesColumnNameSerializer.get());
    ColumnFamily<String, CompositeColumnName> cf = new ColumnFamily<String, CompositeColumnName>("Order", StringSerializer.get(), CompositeColumnNameSerializer.get());
    IndexAndCf indexAndCf = new IndexAndCf(TimeSeriesDbIndex.class, indexCF, keyspace);
    CheckResult checkResult = new CheckResult();
    mockHelper.checkIndexingCF(indexAndCf, false, checkResult);
    assertEquals(0, checkResult.getTotal());
    keyspace.prepareQuery(cf).withCql(String.format("delete from \"Order\" where key='%s'", order.getId())).execute();
    checkResult = new CheckResult();
    mockHelper.checkIndexingCF(indexAndCf, false, checkResult);
    assertEquals(1, checkResult.getTotal());
    keyspace.prepareQuery(indexCF).withCql(mockHelper.getCleanIndexCQL()).execute();
    checkResult = new CheckResult();
    mockHelper.checkIndexingCF(indexAndCf, false, checkResult);
    assertEquals(0, checkResult.getTotal());
}
Also used : Order(com.emc.storageos.db.client.model.uimodels.Order) CompositeColumnName(com.emc.storageos.db.client.impl.CompositeColumnName) Keyspace(com.netflix.astyanax.Keyspace) CheckResult(com.emc.storageos.db.client.impl.DbConsistencyCheckerHelper.CheckResult) IndexAndCf(com.emc.storageos.db.client.impl.DbConsistencyCheckerHelper.IndexAndCf) TimeSeriesIndexColumnName(com.emc.storageos.db.client.impl.TimeSeriesIndexColumnName) ClassNameTimeSeriesIndexColumnName(com.emc.storageos.db.client.impl.ClassNameTimeSeriesIndexColumnName) ColumnFamily(com.netflix.astyanax.model.ColumnFamily) Test(org.junit.Test)

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