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);
}
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();
}
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);
}
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);
}
}
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);
}
Aggregations