Search in sources :

Example 6 with FS4Channel

use of com.yahoo.fs4.mplex.FS4Channel 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 7 with FS4Channel

use of com.yahoo.fs4.mplex.FS4Channel 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)

Example 8 with FS4Channel

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

the class BackendTestCase method requireStatistics.

@Test
public void requireStatistics() throws IOException, InvalidChannelException {
    FS4Channel channel = backend.openPingChannel();
    server.dispatch.channelId = -1;
    server.dispatch.packetData = PONG;
    assertTrue(channel.sendPacket(new PingPacket()));
    try {
        channel.receivePackets(1000, 1);
    } catch (ChannelTimeoutException e) {
        fail("Could not get packets from simulated backend.");
    }
    BackendStatistics stats = backend.getStatistics();
    assertEquals(1, stats.totalConnections());
}
Also used : PingPacket(com.yahoo.fs4.PingPacket) ChannelTimeoutException(com.yahoo.fs4.ChannelTimeoutException) BackendStatistics(com.yahoo.fs4.mplex.Backend.BackendStatistics) Test(org.junit.Test)

Aggregations

ChannelTimeoutException (com.yahoo.fs4.ChannelTimeoutException)7 BasicPacket (com.yahoo.fs4.BasicPacket)6 PingPacket (com.yahoo.fs4.PingPacket)4 IOException (java.io.IOException)4 GetDocSumsPacket (com.yahoo.fs4.GetDocSumsPacket)3 Packet (com.yahoo.fs4.Packet)3 FS4Channel (com.yahoo.fs4.mplex.FS4Channel)3 InvalidChannelException (com.yahoo.fs4.mplex.InvalidChannelException)3 PongPacket (com.yahoo.fs4.PongPacket)2 QueryPacket (com.yahoo.fs4.QueryPacket)2 QueryResultPacket (com.yahoo.fs4.QueryResultPacket)2 Query (com.yahoo.search.Query)2 Result (com.yahoo.search.Result)2 Test (org.junit.Test)2 CompressionType (com.yahoo.compress.CompressionType)1 BackendStatistics (com.yahoo.fs4.mplex.Backend.BackendStatistics)1 Pong (com.yahoo.prelude.Pong)1 Hit (com.yahoo.search.result.Hit)1 ArrayList (java.util.ArrayList)1