use of com.thinkaurelius.titan.diskstorage.PermanentStorageException in project titan by thinkaurelius.
the class CTConnectionFactory method validateSchemaIsSettled.
/* This method was adapted from cassandra 0.7.5 cli/CliClient.java */
public static void validateSchemaIsSettled(Cassandra.Client thriftClient, String currentVersionId) throws InterruptedException, StorageException {
log.debug("Waiting for Cassandra schema propagation...");
Map<String, List<String>> versions = null;
final TimeUUIDType ti = TimeUUIDType.instance;
final long start = System.currentTimeMillis();
long lastTry = 0;
final long limit = start + SCHEMA_WAIT_MAX;
final long minSleep = SCHEMA_WAIT_INCREMENT;
boolean inAgreement = false;
outer: while (limit - System.currentTimeMillis() >= 0 && !inAgreement) {
// Block for a little while if we're looping too fast
final long now = System.currentTimeMillis();
long sinceLast = now - lastTry;
long willSleep = minSleep - sinceLast;
if (0 < willSleep) {
log.debug("Schema not yet propagated; " + "rechecking in {} ms", willSleep);
Thread.sleep(willSleep);
}
// Issue thrift query
try {
lastTry = System.currentTimeMillis();
// getting schema version for nodes of the ring
versions = thriftClient.describe_schema_versions();
} catch (Exception e) {
throw new PermanentStorageException("Failed to fetch Cassandra Thrift schema versions: " + ((e instanceof InvalidRequestException) ? ((InvalidRequestException) e).getWhy() : e.getMessage()));
}
int nodeCount = 0;
// Check schema version
UUID benchmark = UUID.fromString(currentVersionId);
ByteBuffer benchmarkBB = ti.decompose(benchmark);
for (String version : versions.keySet()) {
if (version.equals(StorageProxy.UNREACHABLE)) {
nodeCount += versions.get(version).size();
continue;
}
UUID uuid = UUID.fromString(version);
ByteBuffer uuidBB = ti.decompose(uuid);
if (-1 < ti.compare(uuidBB, benchmarkBB)) {
log.debug("Version {} equals or comes after required version {}", uuid, benchmark);
nodeCount += versions.get(version).size();
continue;
}
continue outer;
}
log.debug("Found {} unreachable or out-of-date Cassandra nodes", nodeCount);
inAgreement = true;
}
if (null == versions) {
throw new TemporaryStorageException("Couldn't contact Cassandra nodes before timeout");
}
if (versions.containsKey(StorageProxy.UNREACHABLE))
log.warn("Warning: unreachable nodes: {}", Joiner.on(", ").join(versions.get(StorageProxy.UNREACHABLE)));
if (!inAgreement) {
throw new TemporaryStorageException("The schema has not settled in " + SCHEMA_WAIT_MAX + " ms. Wanted version " + currentVersionId + "; Versions are " + FBUtilities.toString(versions));
} else {
log.debug("Cassandra schema version {} propagated in about {} ms; Versions are {}", new Object[] { currentVersionId, System.currentTimeMillis() - start, FBUtilities.toString(versions) });
}
}
use of com.thinkaurelius.titan.diskstorage.PermanentStorageException in project titan by thinkaurelius.
the class ElasticSearchIndex method getContent.
public XContentBuilder getContent(List<IndexEntry> additions) throws StorageException {
try {
XContentBuilder builder = XContentFactory.jsonBuilder().startObject();
for (IndexEntry add : additions) {
if (add.value instanceof Number) {
if (AttributeUtil.isWholeNumber((Number) add.value)) {
builder.field(add.key, ((Number) add.value).longValue());
} else {
// double or float
builder.field(add.key, ((Number) add.value).doubleValue());
}
} else if (AttributeUtil.isString(add.value)) {
builder.field(add.key, (String) add.value);
} else if (add.value instanceof Geoshape) {
Geoshape shape = (Geoshape) add.value;
if (shape.getType() == Geoshape.Type.POINT) {
Geoshape.Point p = shape.getPoint();
builder.field(add.key, new double[] { p.getLongitude(), p.getLatitude() });
} else
throw new UnsupportedOperationException("Geo type is not supported: " + shape.getType());
// builder.startObject(add.key);
// switch (shape.getType()) {
// case POINT:
// Geoshape.Point p = shape.getPoint();
// builder.field("type","point");
// builder.field("coordinates",new double[]{p.getLongitude(),p.getLatitude()});
// break;
// case BOX:
// Geoshape.Point southwest = shape.getPoint(0), northeast = shape.getPoint(1);
// builder.field("type","envelope");
// builder.field("coordinates",new double[][]{
// {southwest.getLongitude(),northeast.getLatitude()},
// {northeast.getLongitude(),southwest.getLatitude()}});
// break;
// default: throw new UnsupportedOperationException("Geo type is not supported: " + shape.getType());
// }
// builder.endObject();
} else
throw new IllegalArgumentException("Unsupported type: " + add.value);
}
builder.endObject();
return builder;
} catch (IOException e) {
throw new PermanentStorageException("Could not write json");
}
}
use of com.thinkaurelius.titan.diskstorage.PermanentStorageException in project titan by thinkaurelius.
the class ElasticSearchIndex method register.
@Override
public void register(String store, String key, KeyInformation information, TransactionHandle tx) throws StorageException {
XContentBuilder mapping = null;
Class<?> dataType = information.getDataType();
Mapping map = Mapping.getMapping(information);
Preconditions.checkArgument(map == Mapping.DEFAULT || AttributeUtil.isString(dataType), "Specified illegal mapping [%s] for data type [%s]", map, dataType);
try {
mapping = XContentFactory.jsonBuilder().startObject().startObject(store).startObject("properties").startObject(key);
if (AttributeUtil.isString(dataType)) {
log.debug("Registering string type for {}", key);
mapping.field("type", "string");
if (map == Mapping.STRING)
mapping.field("index", "not_analyzed");
} else if (dataType == Float.class || dataType == FullFloat.class) {
log.debug("Registering float type for {}", key);
mapping.field("type", "float");
} else if (dataType == Double.class || dataType == FullDouble.class) {
log.debug("Registering double type for {}", key);
mapping.field("type", "double");
} else if (dataType == Byte.class) {
log.debug("Registering byte type for {}", key);
mapping.field("type", "byte");
} else if (dataType == Short.class) {
log.debug("Registering short type for {}", key);
mapping.field("type", "short");
} else if (dataType == Integer.class) {
log.debug("Registering integer type for {}", key);
mapping.field("type", "integer");
} else if (dataType == Long.class) {
log.debug("Registering long type for {}", key);
mapping.field("type", "long");
} else if (dataType == Boolean.class) {
log.debug("Registering boolean type for {}", key);
mapping.field("type", "boolean");
} else if (dataType == Geoshape.class) {
log.debug("Registering geo_point type for {}", key);
mapping.field("type", "geo_point");
}
mapping.endObject().endObject().endObject().endObject();
} catch (IOException e) {
throw new PermanentStorageException("Could not render json for put mapping request", e);
}
try {
PutMappingResponse response = client.admin().indices().preparePutMapping(indexName).setIgnoreConflicts(false).setType(store).setSource(mapping).execute().actionGet();
} catch (Exception e) {
throw convert(e);
}
}
use of com.thinkaurelius.titan.diskstorage.PermanentStorageException in project titan by thinkaurelius.
the class HBaseStoreManager method ensureColumnFamilyExists.
private void ensureColumnFamilyExists(String tableName, String columnFamily) throws StorageException {
HBaseAdmin adm = getAdminInterface();
HTableDescriptor desc = ensureTableExists(tableName);
Preconditions.checkNotNull(desc);
HColumnDescriptor cf = desc.getFamily(columnFamily.getBytes());
// Create our column family, if necessary
if (cf == null) {
try {
adm.disableTable(tableName);
desc.addFamily(new HColumnDescriptor(columnFamily).setCompressionType(Compression.Algorithm.GZ));
adm.modifyTable(tableName.getBytes(), desc);
try {
logger.debug("Added HBase ColumnFamily {}, waiting for 1 sec. to propogate.", columnFamily);
Thread.sleep(1000L);
} catch (InterruptedException ie) {
throw new TemporaryStorageException(ie);
}
adm.enableTable(tableName);
} catch (TableNotFoundException ee) {
logger.error("TableNotFoundException", ee);
throw new PermanentStorageException(ee);
} catch (org.apache.hadoop.hbase.TableExistsException ee) {
logger.debug("Swallowing exception {}", ee);
} catch (IOException ee) {
throw new TemporaryStorageException(ee);
}
} else {
// check if compression was enabled, if not - enable it
if (cf.getCompressionType() == null || cf.getCompressionType() == Compression.Algorithm.NONE) {
try {
adm.disableTable(tableName);
adm.modifyColumn(tableName, cf.setCompressionType(Compression.Algorithm.GZ));
adm.enableTable(tableName);
} catch (IOException e) {
throw new TemporaryStorageException(e);
}
}
}
}
use of com.thinkaurelius.titan.diskstorage.PermanentStorageException in project titan by thinkaurelius.
the class LuceneIndex method query.
@Override
public Iterable<RawQuery.Result<String>> query(RawQuery query, KeyInformation.IndexRetriever informations, TransactionHandle tx) throws StorageException {
Query q;
try {
q = new QueryParser(LUCENE_VERSION, "_all", analyzer).parse(query.getQuery());
} catch (ParseException e) {
throw new PermanentStorageException("Could not parse raw query: " + query.getQuery(), e);
}
try {
IndexSearcher searcher = ((Transaction) tx).getSearcher(query.getStore());
// Index does not yet exist
if (searcher == null)
return ImmutableList.of();
long time = System.currentTimeMillis();
TopDocs docs = searcher.search(q, query.hasLimit() ? query.getLimit() : Integer.MAX_VALUE - 1);
log.debug("Executed query [{}] in {} ms", q, System.currentTimeMillis() - time);
List<RawQuery.Result<String>> result = new ArrayList<RawQuery.Result<String>>(docs.scoreDocs.length);
for (int i = 0; i < docs.scoreDocs.length; i++) {
result.add(new RawQuery.Result<String>(searcher.doc(docs.scoreDocs[i].doc).getField(DOCID).stringValue(), docs.scoreDocs[i].score));
}
return result;
} catch (IOException e) {
throw new TemporaryStorageException("Could not execute Lucene query", e);
}
}
Aggregations