Search in sources :

Example 26 with Packet

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

the class VespaBackEndSearcher method fillHits.

/**
 * Fills the hits.
 *
 * @return the number of hits that we did not return data for, and an optional error message.
 *         when things are working normally we return 0.
 */
protected FillHitsResult fillHits(Result result, Packet[] packets, String summaryClass) throws IOException {
    int skippedHits = 0;
    String lastError = null;
    int packetIndex = 0;
    for (Iterator<Hit> i = hitIterator(result); i.hasNext(); ) {
        Hit hit = i.next();
        if (hit instanceof FastHit && !hit.isFilled(summaryClass)) {
            FastHit fastHit = (FastHit) hit;
            ensureInstanceOf(DocsumPacket.class, packets[packetIndex], getName());
            DocsumPacket docsum = (DocsumPacket) packets[packetIndex];
            packetIndex++;
            FillHitResult fr = fillHit(fastHit, docsum, summaryClass);
            if (!fr.ok) {
                skippedHits++;
            }
            if (fr.error != null) {
                result.hits().addError(ErrorMessage.createTimeout(fr.error));
                skippedHits++;
                lastError = fr.error;
            }
        }
    }
    result.hits().setSorted(false);
    return new FillHitsResult(skippedHits, lastError);
}
Also used : ErrorHit(com.yahoo.search.result.ErrorHit) Hit(com.yahoo.search.result.Hit) DocsumPacket(com.yahoo.fs4.DocsumPacket)

Example 27 with Packet

use of com.yahoo.fs4.Packet 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 28 with Packet

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

the class QueryTestCase method testEncodeQueryPacketWithManyFeaturesFresnhessAsString.

/**
 * This test will tell you if you have screwed up the binary encoding, but it won't tell you how
 */
@Test
public void testEncodeQueryPacketWithManyFeaturesFresnhessAsString() {
    Query query = new Query("/?query=chain" + "&ranking.features.query(foo)=30.3&ranking.features.query(bar)=0" + "&ranking.properties.property.p1=v1&ranking.properties.property.p2=v2" + "&pos.ll=S22.4532;W123.9887&pos.radius=3&pos.attribute=place&ranking.freshness=37" + "&model.searchPath=7/3");
    query.getRanking().setFreshness("123456");
    query.getRanking().setSorting("+field1 -field2");
    query.getRanking().setProfile("two");
    Highlight highlight = new Highlight();
    highlight.addHighlightTerm("field1", "term1");
    highlight.addHighlightTerm("field1", "term2");
    query.getPresentation().setHighlight(highlight);
    query.prepare();
    QueryPacket packet = QueryPacket.create(query);
    byte[] encoded = packetToBytes(packet);
    byte[] correctBuffer = new byte[] { 0, 0, 1, 23, 0, 0, 0, -38, 0, 0, 0, 0, 0, 16, 0, -122, 0, 10, ignored, ignored, ignored, ignored, 0, 0, 0x40, 0x03, 3, 't', 'w', 'o', 0, 0, 0, 3, 0, 0, 0, 4, 'r', 'a', 'n', 'k', 0, 0, 0, 5, 0, 0, 0, 11, 'p', 'r', 'o', 'p', 'e', 'r', 't', 'y', 46, 'p', '2', 0, 0, 0, 2, 'v', '2', 0, 0, 0, 11, 'p', 'r', 'o', 'p', 'e', 'r', 't', 'y', 46, 'p', '1', 0, 0, 0, 2, 'v', '1', 0, 0, 0, 3, 'f', 'o', 'o', 0, 0, 0, 4, '3', '0', 46, '3', 0, 0, 0, 3, 'b', 'a', 'r', 0, 0, 0, 1, '0', 0, 0, 0, 9, 'v', 'e', 's', 'p', 'a', 46, 'n', 'o', 'w', 0, 0, 0, 6, '1', '2', '3', '4', '5', '6', 0, 0, 0, 14, 'h', 'i', 'g', 'h', 'l', 'i', 'g', 'h', 't', 't', 'e', 'r', 'm', 's', 0, 0, 0, 3, 0, 0, 0, 6, 'f', 'i', 'e', 'l', 'd', '1', 0, 0, 0, 1, '2', 0, 0, 0, 6, 'f', 'i', 'e', 'l', 'd', '1', 0, 0, 0, 5, 't', 'e', 'r', 'm', '1', 0, 0, 0, 6, 'f', 'i', 'e', 'l', 'd', '1', 0, 0, 0, 5, 't', 'e', 'r', 'm', '2', 0, 0, 0, 5, 'm', 'o', 'd', 'e', 'l', 0, 0, 0, 1, 0, 0, 0, 10, 's', 'e', 'a', 'r', 'c', 'h', 'p', 'a', 't', 'h', 0, 0, 0, 3, '7', 47, '3', 0, 0, 0, 15, 43, 'f', 'i', 'e', 'l', 'd', '1', 32, 45, 'f', 'i', 'e', 'l', 'd', '2', 0, 0, 0, 1, 0, 0, 0, 9, 68, 1, 0, 5, 'c', 'h', 'a', 'i', 'n' };
    assertEqualArrays(correctBuffer, encoded);
}
Also used : Highlight(com.yahoo.prelude.query.Highlight) Query(com.yahoo.search.Query) QueryPacket(com.yahoo.fs4.QueryPacket) Test(org.junit.Test)

Example 29 with Packet

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

the class QueryTestCase method packetToBytes.

public static byte[] packetToBytes(Packet packet) {
    try {
        ByteBuffer buffer = ByteBuffer.allocate(500);
        buffer.limit(0);
        packet.encode(buffer, 0);
        byte[] encoded = new byte[buffer.position()];
        buffer.rewind();
        buffer.get(encoded);
        return encoded;
    } catch (BufferTooSmallException e) {
        throw new RuntimeException(e);
    }
}
Also used : BufferTooSmallException(com.yahoo.fs4.BufferTooSmallException) ByteBuffer(java.nio.ByteBuffer)

Example 30 with Packet

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

the class QueryTestCase method testEncodePacket.

@Test
public void testEncodePacket() {
    Query query = new Query("/?query=chain&timeout=0");
    query.setWindow(2, 8);
    QueryPacket packet = QueryPacket.create(query);
    assertEquals(2, packet.getOffset());
    assertEquals(8, packet.getHits());
    byte[] encoded = packetToBytes(packet);
    byte[] correctBuffer = new byte[] { // Header
    0, // Header
    0, // Header
    0, // Header
    46, // Header
    0, // Header
    0, // Header
    0, // Header
    -38, // Header
    0, // Header
    0, // Header
    0, // Header
    0, // Features
    0, // Features
    0, // Features
    0, // Features
    6, 2, 8, // querytimeout
    0, // querytimeout
    0, // querytimeout
    0, // querytimeout
    1, // qflags
    0, // qflags
    0, // qflags
    0x40, // qflags
    0x03, 7, 'd', 'e', 'f', 'a', 'u', 'l', 't', 0, 0, 0, 1, 0, 0, 0, 8, 4, 0, 5, 99, 104, 97, 105, 110 };
    assertEqualArrays(correctBuffer, encoded);
}
Also used : Query(com.yahoo.search.Query) QueryPacket(com.yahoo.fs4.QueryPacket) Test(org.junit.Test)

Aggregations

Test (org.junit.Test)14 ByteBuffer (java.nio.ByteBuffer)12 BasicPacket (com.yahoo.fs4.BasicPacket)10 QueryPacket (com.yahoo.fs4.QueryPacket)10 Query (com.yahoo.search.Query)9 QueryResultPacket (com.yahoo.fs4.QueryResultPacket)8 IOException (java.io.IOException)5 ChannelTimeoutException (com.yahoo.fs4.ChannelTimeoutException)4 GetDocSumsPacket (com.yahoo.fs4.GetDocSumsPacket)4 Packet (com.yahoo.fs4.Packet)4 PacketDecoder (com.yahoo.fs4.PacketDecoder)4 DecodedPacket (com.yahoo.fs4.PacketDecoder.DecodedPacket)4 Location (org.bukkit.Location)4 DocumentInfo (com.yahoo.fs4.DocumentInfo)3 PingPacket (com.yahoo.fs4.PingPacket)3 InvalidChannelException (com.yahoo.fs4.mplex.InvalidChannelException)3 Packet (ejip123.Packet)3 BufferTooSmallException (com.yahoo.fs4.BufferTooSmallException)2 DocsumPacket (com.yahoo.fs4.DocsumPacket)2 PongPacket (com.yahoo.fs4.PongPacket)2