use of org.apache.ignite.IgniteException in project ignite by apache.
the class CassandraSessionImpl method execute.
/** {@inheritDoc} */
@Override
public <R, V> R execute(BatchExecutionAssistant<R, V> assistant, Iterable<? extends V> data) {
if (data == null || !data.iterator().hasNext())
return assistant.processedData();
int attempt = 0;
String errorMsg = "Failed to execute Cassandra " + assistant.operationName() + " operation";
Throwable error = new IgniteException(errorMsg);
RandomSleeper sleeper = newSleeper();
int dataSize = 0;
incrementSessionRefs();
try {
while (attempt < CQL_EXECUTION_ATTEMPTS_COUNT) {
if (attempt != 0) {
log.warning("Trying " + (attempt + 1) + " attempt to execute Cassandra batch " + assistant.operationName() + " operation to process rest " + (dataSize - assistant.processedCount()) + " of " + dataSize + " elements");
}
//clean errors info before next communication with Cassandra
Throwable unknownEx = null;
Throwable tblAbsenceEx = null;
Throwable hostsAvailEx = null;
Throwable prepStatEx = null;
List<Cache.Entry<Integer, ResultSetFuture>> futResults = new LinkedList<>();
PreparedStatement preparedSt = prepareStatement(assistant.getTable(), assistant.getStatement(), assistant.getPersistenceSettings(), assistant.tableExistenceRequired());
if (preparedSt == null)
return null;
int seqNum = 0;
for (V obj : data) {
if (!assistant.alreadyProcessed(seqNum)) {
try {
Statement statement = tuneStatementExecutionOptions(assistant.bindStatement(preparedSt, obj));
ResultSetFuture fut = session().executeAsync(statement);
futResults.add(new CacheEntryImpl<>(seqNum, fut));
} catch (Throwable e) {
if (CassandraHelper.isTableAbsenceError(e)) {
// If there are table absence error and it is not required for the operation we can return.
if (!assistant.tableExistenceRequired())
return assistant.processedData();
tblAbsenceEx = e;
handleTableAbsenceError(assistant.getTable(), assistant.getPersistenceSettings());
} else if (CassandraHelper.isHostsAvailabilityError(e)) {
hostsAvailEx = e;
// Handle host availability only once.
if (hostsAvailEx == null)
handleHostsAvailabilityError(e, attempt, errorMsg);
} else if (CassandraHelper.isPreparedStatementClusterError(e)) {
prepStatEx = e;
handlePreparedStatementClusterError(e);
} else
unknownEx = e;
}
}
seqNum++;
}
dataSize = seqNum;
// For an error which we don't know how to handle, we will not try next attempts and terminate.
if (unknownEx != null)
throw new IgniteException(errorMsg, unknownEx);
// Remembering any of last errors.
if (tblAbsenceEx != null)
error = tblAbsenceEx;
else if (hostsAvailEx != null)
error = hostsAvailEx;
else if (prepStatEx != null)
error = prepStatEx;
// Clean errors info before next communication with Cassandra.
unknownEx = null;
tblAbsenceEx = null;
hostsAvailEx = null;
prepStatEx = null;
for (Cache.Entry<Integer, ResultSetFuture> futureResult : futResults) {
try {
ResultSet resSet = futureResult.getValue().getUninterruptibly();
Row row = resSet != null && resSet.iterator().hasNext() ? resSet.iterator().next() : null;
if (row != null)
assistant.process(row, futureResult.getKey());
} catch (Throwable e) {
if (CassandraHelper.isTableAbsenceError(e))
tblAbsenceEx = e;
else if (CassandraHelper.isHostsAvailabilityError(e))
hostsAvailEx = e;
else if (CassandraHelper.isPreparedStatementClusterError(e))
prepStatEx = e;
else
unknownEx = e;
}
}
// For an error which we don't know how to handle, we will not try next attempts and terminate.
if (unknownEx != null)
throw new IgniteException(errorMsg, unknownEx);
// If there are no errors occurred it means that operation successfully completed and we can return.
if (tblAbsenceEx == null && hostsAvailEx == null && prepStatEx == null)
return assistant.processedData();
if (tblAbsenceEx != null) {
// If there are table absence error and it is not required for the operation we can return.
if (!assistant.tableExistenceRequired())
return assistant.processedData();
error = tblAbsenceEx;
handleTableAbsenceError(assistant.getTable(), assistant.getPersistenceSettings());
}
if (hostsAvailEx != null) {
error = hostsAvailEx;
handleHostsAvailabilityError(hostsAvailEx, attempt, errorMsg);
}
if (prepStatEx != null) {
error = prepStatEx;
handlePreparedStatementClusterError(prepStatEx);
}
if (!CassandraHelper.isTableAbsenceError(error))
sleeper.sleep();
attempt++;
}
} catch (Throwable e) {
error = e;
} finally {
decrementSessionRefs();
}
errorMsg = "Failed to process " + (dataSize - assistant.processedCount()) + " of " + dataSize + " elements, during " + assistant.operationName() + " operation with Cassandra";
log.error(errorMsg, error);
throw new IgniteException(errorMsg, error);
}
use of org.apache.ignite.IgniteException in project ignite by apache.
the class RandomSleeper method sleep.
/**
* Sleeps
*/
public void sleep() {
try {
int timeout = random.nextInt(max - min + 1) + min;
if (log != null)
log.info("Sleeping for " + timeout + "ms");
Thread.sleep(timeout);
summary += timeout;
if (log != null)
log.info("Sleep completed");
} catch (InterruptedException e) {
throw new IgniteException("Random sleep interrupted", e);
}
min += incr;
max += incr;
}
use of org.apache.ignite.IgniteException in project ignite by apache.
the class KeyPersistenceSettings method getPartitionKeyDescriptors.
/**
* @return POJO field descriptors for partition key.
*/
private List<PropertyDescriptor> getPartitionKeyDescriptors() {
List<PropertyDescriptor> primitivePropDescriptors = PropertyMappingHelper.getPojoPropertyDescriptors(getJavaClass(), true);
boolean valid = false;
for (PropertyDescriptor desc : primitivePropDescriptors) {
if (desc.getWriteMethod() != null) {
valid = true;
break;
}
}
if (!valid) {
throw new IgniteException("Partition key can't have only calculated read-only fields, there should be " + "some fields with setter method");
}
return primitivePropDescriptors;
}
use of org.apache.ignite.IgniteException in project ignite by apache.
the class KeyValuePersistenceSettings method loadSettings.
/**
* Loads Ignite cache persistence settings from resource.
*
* @param in Input stream.
* @return String containing xml with Ignite cache persistence settings.
*/
private String loadSettings(InputStream in) {
StringBuilder settings = new StringBuilder();
BufferedReader reader = null;
try {
reader = new BufferedReader(new InputStreamReader(in));
String line = reader.readLine();
while (line != null) {
if (settings.length() != 0)
settings.append(SystemHelper.LINE_SEPARATOR);
settings.append(line);
line = reader.readLine();
}
} catch (Throwable e) {
throw new IgniteException("Failed to read input stream for Cassandra persistence settings", e);
} finally {
U.closeQuiet(reader);
U.closeQuiet(in);
}
return settings.toString();
}
use of org.apache.ignite.IgniteException in project ignite by apache.
the class PojoField method getValueFromObject.
/**
* Gets field value as an object having Cassandra compatible type.
* This it could be stored directly into Cassandra without any conversions.
*
* @param obj Object instance.
* @param serializer {@link org.apache.ignite.cache.store.cassandra.serializer.Serializer} to use.
* @return Object to store in Cassandra table column.
*/
public Object getValueFromObject(Object obj, Serializer serializer) {
try {
Object val = propDesc().getReadMethod().invoke(obj);
if (val == null)
return null;
DataType.Name cassandraType = PropertyMappingHelper.getCassandraType(val.getClass());
if (cassandraType != null)
return val;
if (serializer == null) {
throw new IllegalStateException("Can't serialize value from object '" + val.getClass().getName() + "' field '" + name + "', cause there is no BLOB serializer specified");
}
return serializer.serialize(val);
} catch (Throwable e) {
throw new IgniteException("Failed to get value of the field '" + name + "' from the instance " + " of '" + obj.getClass().toString() + "' class", e);
}
}
Aggregations