use of com.netflix.astyanax.model.ColumnFamily in project coprhd-controller by CoprHD.
the class UserToOrdersMigration method process.
@Override
public void process() throws MigrationCallbackException {
log.info("Adding new index records for class: {} field: {} annotation: {} name={}", new Object[] { Order.class, Order.SUBMITTED_BY_USER_ID, ClassNameTimeSeries.class.getName(), name });
DataObjectType doType = TypeMap.getDoType(Order.class);
ColumnField field = doType.getColumnField(Order.SUBMITTED_BY_USER_ID);
newIndexCF = field.getIndexCF();
ColumnFamily<String, IndexColumnName> userToOrders = new ColumnFamily<>(SOURCE_INDEX_CF_NAME, StringSerializer.get(), IndexColumnNameSerializer.get());
DbClientImpl client = (DbClientImpl) dbClient;
ks = client.getKeyspace(Order.class);
MutationBatch mutationBatch = ks.prepareMutationBatch();
long m = 0;
try {
OperationResult<Rows<String, IndexColumnName>> result = ks.prepareQuery(userToOrders).getAllRows().setRowLimit(1000).withColumnRange(new RangeBuilder().setLimit(0).build()).execute();
ColumnList<IndexColumnName> cols;
for (Row<String, IndexColumnName> row : result.getResult()) {
RowQuery<String, IndexColumnName> rowQuery = ks.prepareQuery(userToOrders).getKey(row.getKey()).autoPaginate(true).withColumnRange(new RangeBuilder().setLimit(5).build());
while (!(cols = rowQuery.execute().getResult()).isEmpty()) {
m++;
for (Column<IndexColumnName> col : cols) {
String indexKey = row.getKey();
String orderId = col.getName().getTwo();
ClassNameTimeSeriesIndexColumnName newCol = new ClassNameTimeSeriesIndexColumnName(col.getName().getOne(), orderId, col.getName().getTimeUUID());
mutationBatch.withRow(newIndexCF, indexKey).putEmptyColumn(newCol, null);
if (m % 10000 == 0) {
mutationBatch.execute();
}
}
}
}
mutationBatch.execute();
} catch (Exception e) {
log.error("Migration to {} failed e=", newIndexCF.getName(), e);
}
}
use of com.netflix.astyanax.model.ColumnFamily 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.model.ColumnFamily 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.model.ColumnFamily 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());
}
use of com.netflix.astyanax.model.ColumnFamily 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());
}
Aggregations