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);
}
}
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);
}
}
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;
}
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()));
}
}
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());
}
Aggregations