Search in sources :

Example 1 with MissingHTTPEndpointException

use of io.crate.blob.exceptions.MissingHTTPEndpointException in project crate by crate.

the class BlobService method getRedirectAddress.

/**
     * @param index  the name of blob-enabled index
     * @param digest sha-1 hash value of the file
     * @return null if no redirect is required, Otherwise the address to which should be redirected.
     */
public String getRedirectAddress(String index, String digest) throws MissingHTTPEndpointException {
    ShardIterator shards = clusterService.operationRouting().getShards(clusterService.state(), index, null, null, digest, "_local");
    String localNodeId = clusterService.state().nodes().getLocalNodeId();
    DiscoveryNodes nodes = clusterService.state().getNodes();
    ShardRouting shard;
    while ((shard = shards.nextOrNull()) != null) {
        if (!shard.active()) {
            continue;
        }
        if (shard.currentNodeId().equals(localNodeId)) {
            // no redirect required if the shard is on this node
            return null;
        }
        DiscoveryNode node = nodes.get(shard.currentNodeId());
        String httpAddress = node.getAttributes().get("http_address");
        if (httpAddress != null) {
            return httpAddress + "/_blobs/" + BlobIndex.stripPrefix(index) + "/" + digest;
        }
    }
    throw new MissingHTTPEndpointException("Can't find a suitable http server to serve the blob");
}
Also used : DiscoveryNode(org.elasticsearch.cluster.node.DiscoveryNode) ShardIterator(org.elasticsearch.cluster.routing.ShardIterator) ShardRouting(org.elasticsearch.cluster.routing.ShardRouting) MissingHTTPEndpointException(io.crate.blob.exceptions.MissingHTTPEndpointException) DiscoveryNodes(org.elasticsearch.cluster.node.DiscoveryNodes)

Aggregations

MissingHTTPEndpointException (io.crate.blob.exceptions.MissingHTTPEndpointException)1 DiscoveryNode (org.elasticsearch.cluster.node.DiscoveryNode)1 DiscoveryNodes (org.elasticsearch.cluster.node.DiscoveryNodes)1 ShardIterator (org.elasticsearch.cluster.routing.ShardIterator)1 ShardRouting (org.elasticsearch.cluster.routing.ShardRouting)1