Java Code Examples for org.elasticsearch.transport.TransportChannel#sendResponse()

The following examples show how to use org.elasticsearch.transport.TransportChannel#sendResponse() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: PullFullClusterStateAction.java    From Elasticsearch with Apache License 2.0 6 votes vote down vote up
@Override
public void messageReceived(PullFullClusterStateRequest request, final TransportChannel channel) throws Exception {
    ClusterStateWithDLSN clusterStateWithDLSN = clusterStateOpLog.getLatestClusterState();
    if (!clusterStateWithDLSN.state().getClusterName().equals(request.clusterName)) {
       throw new java.lang.Exception("master cluster name is [" + clusterStateWithDLSN.state().getClusterName() + "], request cluster name is [" + request.clusterName + "]");
    }
    if (!clusterStateWithDLSN.state().nodes().localNodeMaster()) {
        throw new java.lang.Exception("current node is no longer master node");
    }
    BytesStreamOutput bStream = new BytesStreamOutput();
    try (StreamOutput stream = CompressorFactory.defaultCompressor().streamOutput(bStream)) {
        clusterStateWithDLSN.writeTo(stream);
    }
    BytesReference fullStateBytes = bStream.bytes();
    channel.sendResponse(new org.elasticsearch.transport.BytesTransportResponse(fullStateBytes));
}
 
Example 2
Source File: BlobRecoveryTarget.java    From crate with Apache License 2.0 5 votes vote down vote up
@Override
public void messageReceived(BlobFinalizeRecoveryRequest request, TransportChannel channel, Task task) throws Exception {

    BlobRecoveryStatus status = onGoingBlobRecoveries.get(request.recoveryId);

    for (BlobRecoveryTransferStatus transferStatus : status.onGoingTransfers().values()) {
        if (transferStatus.outputStream().getChannel().isOpen()) {
            throw new IllegalBlobRecoveryStateException(
                "File channel was left open for "
            );
        }
    }
    onGoingBlobRecoveries.remove(request.recoveryId);
    channel.sendResponse(TransportResponse.Empty.INSTANCE);
}
 
Example 3
Source File: PeerRecoveryTargetService.java    From crate with Apache License 2.0 5 votes vote down vote up
@Override
public void messageReceived(final RecoveryHandoffPrimaryContextRequest request, final TransportChannel channel, Task task) throws Exception {
    try (RecoveryRef recoveryRef = onGoingRecoveries.getRecoverySafe(request.recoveryId(), request.shardId())) {
        recoveryRef.target().handoffPrimaryContext(request.primaryContext());
    }
    channel.sendResponse(TransportResponse.Empty.INSTANCE);
}
 
Example 4
Source File: PeerRecoveryTargetService.java    From crate with Apache License 2.0 5 votes vote down vote up
@Override
public void messageReceived(final RecoveryFileChunkRequest request, TransportChannel channel, Task task) throws Exception {
    try (RecoveryRef recoveryRef = onGoingRecoveries.getRecoverySafe(request.recoveryId(), request.shardId())) {
        final RecoveryTarget recoveryTarget = recoveryRef.target();
        final RecoveryState.Index indexState = recoveryTarget.state().getIndex();
        if (request.sourceThrottleTimeInNanos() != RecoveryState.Index.UNKNOWN) {
            indexState.addSourceThrottling(request.sourceThrottleTimeInNanos());
        }

        RateLimiter rateLimiter = recoverySettings.rateLimiter();
        if (rateLimiter != null) {
            long bytes = bytesSinceLastPause.addAndGet(request.content().length());
            if (bytes > rateLimiter.getMinPauseCheckBytes()) {
                // Time to pause
                bytesSinceLastPause.addAndGet(-bytes);
                long throttleTimeInNanos = rateLimiter.pause(bytes);
                indexState.addTargetThrottling(throttleTimeInNanos);
                recoveryTarget.indexShard().recoveryStats().addThrottleTime(throttleTimeInNanos);
            }
        }

        final ActionListener<TransportResponse> listener =
            new HandledTransportAction.ChannelActionListener<>(channel, Actions.FILE_CHUNK, request);
        recoveryTarget.writeFileChunk(
            request.metadata(),
            request.position(),
            request.content(),
            request.lastChunk(),
            request.totalTranslogOps(),
            ActionListener.wrap(
                nullVal -> listener.onResponse(TransportResponse.Empty.INSTANCE),
                listener::onFailure)
        );
    }
    channel.sendResponse(TransportResponse.Empty.INSTANCE);
}
 
Example 5
Source File: TransportBroadcastByNodeAction.java    From crate with Apache License 2.0 5 votes vote down vote up
@Override
public void messageReceived(final NodeRequest request, TransportChannel channel, Task task) throws Exception {
    List<ShardRouting> shards = request.getShards();
    final int totalShards = shards.size();
    if (logger.isTraceEnabled()) {
        logger.trace("[{}] executing operation on [{}] shards", actionName, totalShards);
    }
    final Object[] shardResultOrExceptions = new Object[totalShards];

    int shardIndex = -1;
    for (final ShardRouting shardRouting : shards) {
        shardIndex++;
        onShardOperation(request, shardResultOrExceptions, shardIndex, shardRouting);
    }

    List<BroadcastShardOperationFailedException> accumulatedExceptions = new ArrayList<>();
    List<ShardOperationResult> results = new ArrayList<>();
    for (int i = 0; i < totalShards; i++) {
        if (shardResultOrExceptions[i] instanceof BroadcastShardOperationFailedException) {
            accumulatedExceptions.add((BroadcastShardOperationFailedException) shardResultOrExceptions[i]);
        } else {
            results.add((ShardOperationResult) shardResultOrExceptions[i]);
        }
    }

    channel.sendResponse(new NodeResponse(request.getNodeId(), totalShards, results, accumulatedExceptions));
}
 
Example 6
Source File: TransportCancelTasksAction.java    From Elasticsearch with Apache License 2.0 5 votes vote down vote up
@Override
public void messageReceived(final BanParentTaskRequest request, final TransportChannel channel) throws Exception {
    if (request.ban) {
        logger.debug("Received ban for the parent [{}] on the node [{}], reason: [{}]", request.parentTaskId,
            clusterService.localNode().getId(), request.reason);
        taskManager.setBan(request.parentTaskId, request.reason);
    } else {
        logger.debug("Removing ban for the parent [{}] on the node [{}]", request.parentTaskId,
            clusterService.localNode().getId());
        taskManager.removeBan(request.parentTaskId);
    }
    channel.sendResponse(TransportResponse.Empty.INSTANCE);
}
 
Example 7
Source File: BlobRecoveryTarget.java    From crate with Apache License 2.0 5 votes vote down vote up
@Override
public void messageReceived(BlobStartPrefixSyncRequest request, TransportChannel channel, Task task) throws Exception {
    BlobRecoveryStatus status = onGoingBlobRecoveries.get(request.recoveryId());
    if (status == null) {
        throw new IllegalBlobRecoveryStateException(
            "could not retrieve BlobRecoveryStatus"
        );
    }
    if (status.canceled()) {
        throw new IndexShardClosedException(status.shardId());
    }
    byte[][] currentDigests = status.blobShard.currentDigests(request.prefix());
    BlobStartPrefixResponse response = new BlobStartPrefixResponse(currentDigests);
    channel.sendResponse(response);
}
 
Example 8
Source File: PeerRecoveryTargetService.java    From crate with Apache License 2.0 5 votes vote down vote up
@Override
public void messageReceived(RecoveryWaitForClusterStateRequest request, TransportChannel channel, Task task) throws Exception {
    try (RecoveryRef recoveryRef = onGoingRecoveries.getRecoverySafe(request.recoveryId(), request.shardId()
    )) {
        recoveryRef.target().ensureClusterStateVersion(request.clusterStateVersion());
    }
    channel.sendResponse(TransportResponse.Empty.INSTANCE);
}
 
Example 9
Source File: BlobRecoveryTarget.java    From Elasticsearch with Apache License 2.0 5 votes vote down vote up
@Override
public void messageReceived(BlobFinalizeRecoveryRequest request, TransportChannel channel) throws Exception {

    BlobRecoveryStatus status = onGoingRecoveries.get(request.recoveryId);

    for (BlobRecoveryTransferStatus transferStatus : status.onGoingTransfers().values()) {
        if (transferStatus.outputStream().getChannel().isOpen()) {
            throw new IllegalBlobRecoveryStateException(
                "File channel was left open for "
            );
        }
    }
    onGoingRecoveries.remove(request.recoveryId);
    channel.sendResponse(TransportResponse.Empty.INSTANCE);
}
 
Example 10
Source File: BlobRecoveryTarget.java    From crate with Apache License 2.0 5 votes vote down vote up
@Override
public void messageReceived(BlobRecoveryChunkRequest request, TransportChannel channel, Task task) throws Exception {

    BlobRecoveryStatus onGoingRecovery = onGoingBlobRecoveries.get(request.recoveryId());
    if (onGoingRecovery == null) {
        // shard is getting closed on us
        throw new IllegalBlobRecoveryStateException("Could not retrieve onGoingRecoveryStatus");
    }


    BlobRecoveryTransferStatus transferStatus = onGoingRecovery.onGoingTransfers().get(request.transferId());
    BlobShard shard = onGoingRecovery.blobShard;
    if (onGoingRecovery.canceled()) {
        onGoingRecovery.sentCanceledToSource();
        throw new IndexShardClosedException(onGoingRecovery.shardId());
    }

    if (transferStatus == null) {
        throw new IndexShardClosedException(onGoingRecovery.shardId());
    }

    request.content().writeTo(transferStatus.outputStream());

    if (request.isLast()) {
        transferStatus.outputStream().close();
        Path baseDirectory = shard.blobContainer().getBaseDirectory();
        Path source = baseDirectory.resolve(transferStatus.sourcePath());
        Path target = baseDirectory.resolve(transferStatus.targetPath());

        Files.move(source, target, StandardCopyOption.ATOMIC_MOVE, StandardCopyOption.REPLACE_EXISTING);
        onGoingRecovery.onGoingTransfers().remove(request.transferId());
    }

    channel.sendResponse(TransportResponse.Empty.INSTANCE);
}
 
Example 11
Source File: PeerRecoveryTargetService.java    From crate with Apache License 2.0 5 votes vote down vote up
@Override
public void messageReceived(RecoveryFilesInfoRequest request, TransportChannel channel, Task task) throws Exception {
    try (RecoveryRef recoveryRef = onGoingRecoveries.getRecoverySafe(request.recoveryId(), request.shardId()
    )) {
        recoveryRef.target().receiveFileInfo(request.phase1FileNames, request.phase1FileSizes, request.phase1ExistingFileNames,
                request.phase1ExistingFileSizes, request.totalTranslogOps);
        channel.sendResponse(TransportResponse.Empty.INSTANCE);
    }
}
 
Example 12
Source File: BlobRecoveryTarget.java    From crate with Apache License 2.0 5 votes vote down vote up
@Override
public void messageReceived(BlobRecoveryDeleteRequest request, TransportChannel channel, Task task) throws Exception {
    BlobRecoveryStatus status = onGoingBlobRecoveries.get(request.recoveryId());
    if (status.canceled()) {
        throw new IndexShardClosedException(status.shardId());
    }
    for (BytesReference digest : request.digests) {
        status.blobShard.delete(Hex.encodeHexString(BytesReference.toBytes(digest)));
    }
    channel.sendResponse(TransportResponse.Empty.INSTANCE);
}
 
Example 13
Source File: VerifyNodeRepositoryAction.java    From crate with Apache License 2.0 5 votes vote down vote up
@Override
public void messageReceived(VerifyNodeRepositoryRequest request, TransportChannel channel, Task task) throws Exception {
    DiscoveryNode localNode = clusterService.state().nodes().getLocalNode();
    try {
        doVerify(request.repository, request.verificationToken, localNode);
    } catch (Exception ex) {
        LOGGER.warn(() -> new ParameterizedMessage("[{}] failed to verify repository", request.repository), ex);
        throw ex;
    }
    channel.sendResponse(TransportResponse.Empty.INSTANCE);
}
 
Example 14
Source File: BlobRecoverySource.java    From Elasticsearch with Apache License 2.0 4 votes vote down vote up
@Override
public void messageReceived(final StartRecoveryRequest request, final TransportChannel channel) throws Exception {
    RecoveryResponse response = recover(request);
    channel.sendResponse(response);
}
 
Example 15
Source File: TransportNodesAction.java    From crate with Apache License 2.0 4 votes vote down vote up
@Override
public void messageReceived(NodeRequest request, TransportChannel channel, Task task) throws Exception {
    channel.sendResponse(nodeOperation(request, task));
}
 
Example 16
Source File: ShardStateAction.java    From Elasticsearch with Apache License 2.0 4 votes vote down vote up
@Override
public void messageReceived(ShardRoutingEntry request, TransportChannel channel) throws Exception {
    handleShardFailureOnMaster(request);
    channel.sendResponse(TransportResponse.Empty.INSTANCE);
}
 
Example 17
Source File: ShardStateAction.java    From Elasticsearch with Apache License 2.0 4 votes vote down vote up
@Override
public void messageReceived(ShardRoutingEntry request, TransportChannel channel) throws Exception {
    shardStartedOnMaster(request);
    channel.sendResponse(TransportResponse.Empty.INSTANCE);
}
 
Example 18
Source File: RestoreService.java    From Elasticsearch with Apache License 2.0 4 votes vote down vote up
@Override
public void messageReceived(UpdateIndexShardRestoreStatusRequest request, final TransportChannel channel) throws Exception {
    updateRestoreStateOnMaster(request);
    channel.sendResponse(TransportResponse.Empty.INSTANCE);
}
 
Example 19
Source File: BlobRecoveryTarget.java    From Elasticsearch with Apache License 2.0 4 votes vote down vote up
@Override
public void messageReceived(BlobRecoveryStartTransferRequest request, TransportChannel channel) throws Exception {
    BlobRecoveryStatus status = onGoingRecoveries.get(request.recoveryId());
    logger.debug("received BlobRecoveryStartTransferRequest for file {} with size {}",
        request.path(), request.size());
    if (status == null) {
        throw new IllegalBlobRecoveryStateException("Could not retrieve onGoingRecoveryStatus");
    }
    if (status.canceled()) {
        throw new IndexShardClosedException(status.shardId());
    }

    BlobShard shard = status.blobShard;
    String tmpPath = request.path() + "." + request.transferId();
    FileOutputStream outputStream = new FileOutputStream(
        new File(shard.blobContainer().getBaseDirectory(), tmpPath)
    );

    BytesReference content = request.content();
    if (!content.hasArray()) {
        content = content.toBytesArray();
    }
    outputStream.write(content.array(), content.arrayOffset(), content.length());

    if (request.size() == request.content().length()) {  // start request contains the whole file.
        outputStream.close();
        File source = new File(shard.blobContainer().getBaseDirectory(), tmpPath);
        File target = new File(shard.blobContainer().getBaseDirectory(), request.path());
        if (!target.exists()) {
            if (!source.renameTo(target)) {
                throw new IllegalBlobRecoveryStateException(
                    "couldn't rename file to " + request.path()
                );
            }
        }
    } else {
        BlobRecoveryTransferStatus transferStatus= new BlobRecoveryTransferStatus(
            request.transferId(), outputStream, tmpPath, request.path()
        );
        status.onGoingTransfers().put(request.transferId(), transferStatus);
    }

    channel.sendResponse(TransportResponse.Empty.INSTANCE);
}
 
Example 20
Source File: TransportTasksAction.java    From Elasticsearch with Apache License 2.0 4 votes vote down vote up
@Override
public void messageReceived(final NodeTaskRequest request, final TransportChannel channel) throws Exception {
    channel.sendResponse(nodeOperation(request));
}