Search in sources :

Example 1 with IndexingProxyStreamOutput

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));
    }
}
Also used : Path(java.nio.file.Path) IndexingProxyStreamOutput(org.codelibs.elasticsearch.idxproxy.stream.IndexingProxyStreamOutput) IOException(java.io.IOException) ElasticsearchException(org.elasticsearch.ElasticsearchException) ElasticsearchException(org.elasticsearch.ElasticsearchException) IOException(java.io.IOException)

Example 2 with IndexingProxyStreamOutput

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));
}
Also used : IndexRequestBuilder(org.elasticsearch.action.index.IndexRequestBuilder) ElasticsearchException(org.elasticsearch.ElasticsearchException) IndexingProxyPlugin(org.codelibs.elasticsearch.idxproxy.IndexingProxyPlugin) Arrays(java.util.Arrays) Date(java.util.Date) WriteRequestHandler(org.codelibs.elasticsearch.idxproxy.action.WriteRequestHandler) Environment(org.elasticsearch.env.Environment) FileTime(java.nio.file.attribute.FileTime) XContentBuilder(org.elasticsearch.common.xcontent.XContentBuilder) JsonXContent(org.elasticsearch.common.xcontent.json.JsonXContent) QueryBuilders(org.elasticsearch.index.query.QueryBuilders) DeleteRequest(org.elasticsearch.action.delete.DeleteRequest) IndexRequest(org.elasticsearch.action.index.IndexRequest) Settings(org.elasticsearch.common.settings.Settings) UpdateResponse(org.elasticsearch.action.update.UpdateResponse) LifecycleListener(org.elasticsearch.common.component.LifecycleListener) IndexNotFoundException(org.elasticsearch.index.IndexNotFoundException) Map(java.util.Map) DeleteByQueryRequest(org.elasticsearch.index.reindex.DeleteByQueryRequest) Streams(org.elasticsearch.common.io.Streams) ThreadPool(org.elasticsearch.threadpool.ThreadPool) Path(java.nio.file.Path) RefreshPolicy(org.elasticsearch.action.support.WriteRequest.RefreshPolicy) CreateRequest(org.codelibs.elasticsearch.idxproxy.action.CreateRequest) WriteResponse(org.codelibs.elasticsearch.idxproxy.action.WriteResponse) SearchHit(org.elasticsearch.search.SearchHit) ActionRequest(org.elasticsearch.action.ActionRequest) ActionFilter(org.elasticsearch.action.support.ActionFilter) ActionFilters(org.elasticsearch.action.support.ActionFilters) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap) Set(java.util.Set) ToXContent(org.elasticsearch.common.xcontent.ToXContent) Reader(java.io.Reader) PrivilegedAction(java.security.PrivilegedAction) Collectors(java.util.stream.Collectors) StandardCharsets(java.nio.charset.StandardCharsets) List(java.util.List) IndexingProxyStreamInput(org.codelibs.elasticsearch.idxproxy.stream.IndexingProxyStreamInput) IndexRequestBuilder(org.elasticsearch.action.index.IndexRequestBuilder) Strings(org.apache.logging.log4j.util.Strings) TransportResponseHandler(org.elasticsearch.transport.TransportResponseHandler) AccessController(java.security.AccessController) TransportException(org.elasticsearch.transport.TransportException) ProxyActionFilter(org.codelibs.elasticsearch.idxproxy.action.ProxyActionFilter) XContentFactory(org.elasticsearch.common.xcontent.XContentFactory) CreateRequestHandler(org.codelibs.elasticsearch.idxproxy.action.CreateRequestHandler) ClusterService(org.elasticsearch.cluster.service.ClusterService) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) UpdateRequestBuilder(org.elasticsearch.action.update.UpdateRequestBuilder) ActionListener.wrap(org.elasticsearch.action.ActionListener.wrap) HashMap(java.util.HashMap) IndexingProxyStreamOutput(org.codelibs.elasticsearch.idxproxy.stream.IndexingProxyStreamOutput) Names(org.elasticsearch.threadpool.ThreadPool.Names) StandardCopyOption(java.nio.file.StandardCopyOption) ArrayList(java.util.ArrayList) Inject(org.elasticsearch.common.inject.Inject) DiscoveryNode(org.elasticsearch.cluster.node.DiscoveryNode) NamedWriteableRegistry(org.elasticsearch.common.io.stream.NamedWriteableRegistry) RequestUtils(org.codelibs.elasticsearch.idxproxy.util.RequestUtils) ThreadLocalRandom(java.util.concurrent.ThreadLocalRandom) TimeValue(org.elasticsearch.common.unit.TimeValue) BiConsumer(java.util.function.BiConsumer) Result(org.elasticsearch.action.DocWriteResponse.Result) TransportService(org.elasticsearch.transport.TransportService) UpdateByQueryRequest(org.elasticsearch.index.reindex.UpdateByQueryRequest) CreateResponse(org.codelibs.elasticsearch.idxproxy.action.CreateResponse) RequestSender(org.codelibs.elasticsearch.idxproxy.sender.RequestSender) FileAccessUtils(org.codelibs.elasticsearch.idxproxy.util.FileAccessUtils) PluginComponent(org.codelibs.elasticsearch.idxproxy.IndexingProxyPlugin.PluginComponent) Iterator(java.util.Iterator) Files(java.nio.file.Files) ActionResponse(org.elasticsearch.action.ActionResponse) Client(org.elasticsearch.client.Client) IOException(java.io.IOException) PingResponse(org.codelibs.elasticsearch.idxproxy.action.PingResponse) ClusterHealthResponse(org.elasticsearch.action.admin.cluster.health.ClusterHealthResponse) UpdateRequest(org.elasticsearch.action.update.UpdateRequest) InputStreamReader(java.io.InputStreamReader) AbstractLifecycleComponent(org.elasticsearch.common.component.AbstractLifecycleComponent) PingRequest(org.codelibs.elasticsearch.idxproxy.action.PingRequest) Consumer(java.util.function.Consumer) LocalNodeMasterListener(org.elasticsearch.cluster.LocalNodeMasterListener) Paths(java.nio.file.Paths) WriteRequest(org.codelibs.elasticsearch.idxproxy.action.WriteRequest) PingRequestHandler(org.codelibs.elasticsearch.idxproxy.action.PingRequestHandler) Collections(java.util.Collections) BulkRequest(org.elasticsearch.action.bulk.BulkRequest) ActionListener(org.elasticsearch.action.ActionListener) Path(java.nio.file.Path) IndexingProxyStreamOutput(org.codelibs.elasticsearch.idxproxy.stream.IndexingProxyStreamOutput) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap) HashMap(java.util.HashMap) IOException(java.io.IOException) ElasticsearchException(org.elasticsearch.ElasticsearchException) Date(java.util.Date)

Aggregations

IOException (java.io.IOException)2 Path (java.nio.file.Path)2 IndexingProxyStreamOutput (org.codelibs.elasticsearch.idxproxy.stream.IndexingProxyStreamOutput)2 ElasticsearchException (org.elasticsearch.ElasticsearchException)2 InputStreamReader (java.io.InputStreamReader)1 Reader (java.io.Reader)1 StandardCharsets (java.nio.charset.StandardCharsets)1 Files (java.nio.file.Files)1 Paths (java.nio.file.Paths)1 StandardCopyOption (java.nio.file.StandardCopyOption)1 FileTime (java.nio.file.attribute.FileTime)1 AccessController (java.security.AccessController)1 PrivilegedAction (java.security.PrivilegedAction)1 ArrayList (java.util.ArrayList)1 Arrays (java.util.Arrays)1 Collections (java.util.Collections)1 Date (java.util.Date)1 HashMap (java.util.HashMap)1 Iterator (java.util.Iterator)1 List (java.util.List)1