use of org.codelibs.elasticsearch.idxproxy.stream.IndexingProxyStreamOutput in project elasticsearch-indexing-proxy by codelibs.
the class RequestSender method writeError.
private <Request extends ActionRequest, Response extends ActionResponse> void writeError(final long position, final Request request, final ActionListener<Response> listener) {
final String fileId = String.format(dataFileFormat, version);
final Path outputPath = dataPath.resolve(fileId + "_" + position + ERROR_EXTENTION);
logger.info("Saving " + outputPath.toAbsolutePath());
final short classType = RequestUtils.getClassType(request);
if (classType > 0) {
try (IndexingProxyStreamOutput out = AccessController.doPrivileged((PrivilegedAction<IndexingProxyStreamOutput>) () -> {
try {
return new IndexingProxyStreamOutput(Files.newOutputStream(outputPath));
} catch (final IOException e) {
throw new ElasticsearchException("Could not open " + outputPath, e);
}
})) {
out.writeShort(classType);
request.writeTo(out);
out.flush();
} catch (final Exception e) {
listener.onFailure(e);
}
} else {
listener.onFailure(new ElasticsearchException("Unknown request: " + request));
}
}
use of org.codelibs.elasticsearch.idxproxy.stream.IndexingProxyStreamOutput in project elasticsearch-indexing-proxy by codelibs.
the class IndexingProxyService method createStreamOutput.
private <Response extends ActionResponse> void createStreamOutput(final ActionListener<Response> listener, final long version) {
final String oldFileId = fileId;
final Map<String, Object> source = new HashMap<>();
source.put(DOC_TYPE, FILE_ID);
source.put(IndexingProxyPlugin.NODE_NAME, nodeName());
source.put(IndexingProxyPlugin.TIMESTAMP, new Date());
final IndexRequestBuilder builder = client.prepareIndex(IndexingProxyPlugin.INDEX_NAME, IndexingProxyPlugin.TYPE_NAME, FILE_ID);
if (version > 0) {
builder.setVersion(version);
} else {
builder.setCreate(true);
}
builder.setSource(source).setRefreshPolicy(RefreshPolicy.WAIT_UNTIL).execute(wrap(res -> {
synchronized (this) {
if (oldFileId == null || oldFileId.equals(fileId)) {
if (streamOutput != null) {
closeStreamOutput();
}
fileId = String.format(dataFileFormat, res.getVersion());
final Path outputPath = dataPath.resolve(fileId + WORKING_EXTENTION);
if (FileAccessUtils.existsFile(outputPath)) {
finalizeDataFile();
createStreamOutput(listener, res.getVersion());
return;
}
streamOutput = AccessController.doPrivileged((PrivilegedAction<IndexingProxyStreamOutput>) () -> {
try {
return new IndexingProxyStreamOutput(Files.newOutputStream(outputPath));
} catch (final IOException e) {
throw new ElasticsearchException("Could not open " + outputPath, e);
}
});
logger.info("[Writer] Opening  " + outputPath.toAbsolutePath());
}
}
listener.onResponse(null);
}, listener::onFailure));
}
Aggregations