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