Search in sources :

Example 6 with BasicPacket

use of com.yahoo.fs4.BasicPacket in project vespa by vespa-engine.

the class QueryResultTestCase method testDecodeQueryResultX.

@Test
public void testDecodeQueryResultX() {
    byte[] packetData = new byte[] { 0, 0, 0, 100, 0, 0, 0, 217 - 256, 0, 0, 0, 1, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 2, 0, 0, 0, 0, 0, 0, 0, 5, 0x40, 0x39, 0, 0, 0, 0, 0, 0, 0, 0, 0, 111, 0, 0, 0, 0, 0, 0, 0, 89, 0, 0, 0, 0, 0, 0, 0, 90, 0, 0, 0, 0, 0, 0, 0, 91, 0, 0, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0x40, 0x37, 0, 0, 0, 0, 0, 0, 0, 0, 0, 7, 0, 0, 0, 36, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 0x40, 0x35, 0, 0, 0, 0, 0, 0, 0, 0, 0, 8, 0, 0, 0, 37 };
    ByteBuffer buffer = ByteBuffer.allocate(200);
    buffer.put(packetData);
    buffer.flip();
    BasicPacket packet = PacketDecoder.decode(buffer);
    assertTrue(packet instanceof QueryResultPacket);
    QueryResultPacket result = (QueryResultPacket) packet;
    assertTrue(result.getMldFeature());
    assertEquals(5, result.getTotalDocumentCount());
    assertEquals(25, result.getMaxRank());
    assertEquals(111, result.getDocstamp());
    assertEquals(89, result.getCoverageDocs());
    assertEquals(90, result.getActiveDocs());
    assertEquals(91, result.getSoonActiveDocs());
    assertEquals(1, result.getDegradedReason());
    assertEquals(2, result.getDocuments().size());
    DocumentInfo document1 = result.getDocuments().get(0);
    assertEquals(gid1, document1.getGlobalId());
    assertEquals(23.0, document1.getMetric(), delta);
    assertEquals(7, document1.getPartId());
    assertEquals(36, document1.getDistributionKey());
    DocumentInfo document2 = result.getDocuments().get(1);
    assertEquals(gid2, document2.getGlobalId());
    assertEquals(21.0, document2.getMetric(), delta);
    assertEquals(8, document2.getPartId());
    assertEquals(37, document2.getDistributionKey());
}
Also used : QueryResultPacket(com.yahoo.fs4.QueryResultPacket) BasicPacket(com.yahoo.fs4.BasicPacket) ByteBuffer(java.nio.ByteBuffer) DocumentInfo(com.yahoo.fs4.DocumentInfo) Test(org.junit.Test)

Example 7 with BasicPacket

use of com.yahoo.fs4.BasicPacket in project vespa by vespa-engine.

the class FastSearcher method searchTwoPhase.

private Result searchTwoPhase(FS4Channel channel, Query query, QueryPacket queryPacket, CacheKey cacheKey) throws IOException {
    if (isLoggingFine())
        getLogger().finest("sending query packet");
    try {
        boolean couldSend = channel.sendPacket(queryPacket);
        if (!couldSend)
            return new Result(query, ErrorMessage.createBackendCommunicationError("Could not reach '" + getName() + "'"));
    } catch (InvalidChannelException e) {
        return new Result(query, ErrorMessage.createBackendCommunicationError("Invalid channel " + getName()));
    } catch (IllegalStateException e) {
        return new Result(query, ErrorMessage.createBackendCommunicationError("Illegal state in FS4: " + e.getMessage()));
    }
    BasicPacket[] basicPackets;
    try {
        basicPackets = channel.receivePackets(query.getTimeLeft(), 1);
    } catch (ChannelTimeoutException e) {
        return new Result(query, ErrorMessage.createTimeout("Timeout while waiting for " + getName()));
    } catch (InvalidChannelException e) {
        return new Result(query, ErrorMessage.createBackendCommunicationError("Invalid channel for " + getName()));
    }
    if (basicPackets.length == 0) {
        return new Result(query, ErrorMessage.createBackendCommunicationError(getName() + " got no packets back"));
    }
    if (isLoggingFine())
        getLogger().finest("got packets " + basicPackets.length + " packets");
    ensureInstanceOf(QueryResultPacket.class, basicPackets[0], getName());
    QueryResultPacket resultPacket = (QueryResultPacket) basicPackets[0];
    if (isLoggingFine())
        getLogger().finest("got query packet. " + "docsumClass=" + query.getPresentation().getSummary());
    if (query.getPresentation().getSummary() == null)
        query.getPresentation().setSummary(getDefaultDocsumClass());
    Result result = new Result(query);
    addMetaInfo(query, queryPacket.getQueryPacketData(), resultPacket, result, false);
    addUnfilledHits(result, resultPacket.getDocuments(), false, queryPacket.getQueryPacketData(), cacheKey);
    Packet[] packets;
    PacketWrapper packetWrapper = cacheControl.lookup(cacheKey, query);
    if (packetWrapper != null) {
        cacheControl.updateCacheEntry(cacheKey, query, resultPacket);
    } else {
        if (resultPacket.getCoverageFeature() && !resultPacket.getCoverageFull()) {
        // Don't add error here, it was done in first phase
        // No check if packetWrapper already exists, since incomplete
        // first phase data won't be cached anyway.
        } else {
            packets = new Packet[1];
            packets[0] = resultPacket;
            cacheControl.cache(cacheKey, query, new DocsumPacketKey[0], packets);
        }
    }
    return result;
}
Also used : InvalidChannelException(com.yahoo.fs4.mplex.InvalidChannelException) QueryResultPacket(com.yahoo.fs4.QueryResultPacket) QueryResultPacket(com.yahoo.fs4.QueryResultPacket) QueryPacket(com.yahoo.fs4.QueryPacket) PongPacket(com.yahoo.fs4.PongPacket) BasicPacket(com.yahoo.fs4.BasicPacket) GetDocSumsPacket(com.yahoo.fs4.GetDocSumsPacket) Packet(com.yahoo.fs4.Packet) PingPacket(com.yahoo.fs4.PingPacket) BasicPacket(com.yahoo.fs4.BasicPacket) ChannelTimeoutException(com.yahoo.fs4.ChannelTimeoutException) Result(com.yahoo.search.Result)

Example 8 with BasicPacket

use of com.yahoo.fs4.BasicPacket in project vespa by vespa-engine.

the class PacketWrapper method getDocuments.

/**
 * @return list of documents, null if not all are available
 */
public List<DocumentInfo> getDocuments(int offset, int hits) {
    // speculatively allocate list for the hits
    List<DocumentInfo> docs = new ArrayList<>(hits);
    int currentOffset = 0;
    QueryResultPacket r = getFirstResultPacket();
    if (offset >= r.getTotalDocumentCount()) {
        // offset == 0 && totalDocumentCount == 0
        return docs;
    }
    for (Iterator<BasicPacket> i = resultPackets.iterator(); i.hasNext(); ) {
        QueryResultPacket result = (QueryResultPacket) i.next();
        if (result.getOffset() > offset + currentOffset) {
            // we haven't got all the requested document info objects
            return null;
        }
        if (result.getOffset() + result.getDocumentCount() <= currentOffset + offset) {
            // no new hits available
            continue;
        }
        List<DocumentInfo> documents = result.getDocuments();
        int packetOffset = (offset + currentOffset) - result.getOffset();
        int afterLastDoc = Math.min(documents.size(), packetOffset + hits);
        for (Iterator<DocumentInfo> j = documents.subList(packetOffset, afterLastDoc).iterator(); docs.size() < hits && j.hasNext(); ++currentOffset) {
            docs.add(j.next());
        }
        if (hits == docs.size() || offset + docs.size() >= result.getTotalDocumentCount()) {
            // We have the hits we need, or there are no more hits available
            return docs;
        }
    }
    return null;
}
Also used : QueryResultPacket(com.yahoo.fs4.QueryResultPacket) BasicPacket(com.yahoo.fs4.BasicPacket) DocumentInfo(com.yahoo.fs4.DocumentInfo)

Example 9 with BasicPacket

use of com.yahoo.fs4.BasicPacket in project vespa by vespa-engine.

the class FS4Channel method receivePackets.

/**
 * Receives the given number of packets and returns them, OR
 * <ul>
 * <li>Returns a smaller number of packets if an error or eol packet is received
 * <li>Throws a ChannelTimeoutException if timeout occurs before all packets
 * are received. Packets received with the wrong channel id are ignored.
 * </ul>
 *
 * @param timeout the number of ms to attempt to get packets before throwing an exception
 * @param packetCount the number of packets to receive, or -1 to receive any number up to eol/error
 */
public BasicPacket[] receivePackets(long timeout, int packetCount) throws InvalidChannelException, ChannelTimeoutException {
    ensureValid();
    List<BasicPacket> packets = new ArrayList<>(12);
    long startTime = SystemTimer.INSTANCE.milliTime();
    long timeLeft = timeout;
    try {
        while (timeLeft >= 0) {
            BasicPacket p = nextPacket(timeLeft);
            if (p == null)
                throw new ChannelTimeoutException("Timed out");
            if (!isPingChannel && ((Packet) p).getChannel() != getChannelId().intValue()) {
                log.warning("Ignoring received " + p + ", when excepting channel " + getChannelId());
                continue;
            }
            packets.add(p);
            if (isLastPacket(p) || hasEnoughPackets(packetCount, packets)) {
                BasicPacket[] packetArray = new BasicPacket[packets.size()];
                packets.toArray(packetArray);
                return packetArray;
            }
            // doing this last might save us one system call for the last
            // packet.
            timeLeft = timeout - (SystemTimer.INSTANCE.milliTime() - startTime);
        }
    } catch (InvalidChannelException e) {
        // nop.  if we get this we want to return the default
        // zero length packet array indicating that we have no
        // valid response
        log.info("FS4Channel was invalid. timeLeft=" + timeLeft + ", timeout=" + timeout);
    } catch (InterruptedException e) {
        log.info("FS4Channel was interrupted. timeLeft=" + timeLeft + ", timeout=" + timeout);
        Thread.currentThread().interrupt();
    }
    // did not get the end of the packet stream
    throw new ChannelTimeoutException();
}
Also used : BasicPacket(com.yahoo.fs4.BasicPacket) Packet(com.yahoo.fs4.Packet) BasicPacket(com.yahoo.fs4.BasicPacket) ArrayList(java.util.ArrayList) ChannelTimeoutException(com.yahoo.fs4.ChannelTimeoutException)

Example 10 with BasicPacket

use of com.yahoo.fs4.BasicPacket in project vespa by vespa-engine.

the class BackendTestCase method testBackend.

@Test
public void testBackend() throws IOException, InvalidChannelException {
    FS4Channel channel = backend.openChannel();
    Query q = new Query("/?query=a");
    BasicPacket[] b = null;
    int channelId = channel.getChannelId();
    server.dispatch.channelId = channelId;
    assertTrue(backend.sendPacket(QueryPacket.create(q), channelId));
    try {
        b = channel.receivePackets(1000, 1);
    } catch (ChannelTimeoutException e) {
        fail("Could not get packets from simulated backend.");
    }
    assertEquals(1, b.length);
    assertEquals(217, b[0].getCode());
    channel.close();
}
Also used : BasicPacket(com.yahoo.fs4.BasicPacket) Query(com.yahoo.search.Query) ChannelTimeoutException(com.yahoo.fs4.ChannelTimeoutException) Test(org.junit.Test)

Aggregations

BasicPacket (com.yahoo.fs4.BasicPacket)12 QueryResultPacket (com.yahoo.fs4.QueryResultPacket)7 ChannelTimeoutException (com.yahoo.fs4.ChannelTimeoutException)4 DocumentInfo (com.yahoo.fs4.DocumentInfo)4 ByteBuffer (java.nio.ByteBuffer)4 Test (org.junit.Test)4 GetDocSumsPacket (com.yahoo.fs4.GetDocSumsPacket)3 PingPacket (com.yahoo.fs4.PingPacket)3 Packet (com.yahoo.fs4.Packet)2 PongPacket (com.yahoo.fs4.PongPacket)2 QueryPacket (com.yahoo.fs4.QueryPacket)2 InvalidChannelException (com.yahoo.fs4.mplex.InvalidChannelException)2 IOException (java.io.IOException)2 PacketDecoder (com.yahoo.fs4.PacketDecoder)1 DecodedPacket (com.yahoo.fs4.PacketDecoder.DecodedPacket)1 FS4Channel (com.yahoo.fs4.mplex.FS4Channel)1 Pong (com.yahoo.prelude.Pong)1 PacketWrapper (com.yahoo.prelude.fastsearch.PacketWrapper)1 Query (com.yahoo.search.Query)1 Result (com.yahoo.search.Result)1