Search in sources :

Example 1 with Document

use of com.yahoo.vespa.http.client.core.Document in project vespa by vespa-engine.

the class DryRunGatewayConnection method writeOperations.

@Override
public InputStream writeOperations(List<Document> docs) throws ServerResponseException, IOException {
    StringBuilder result = new StringBuilder();
    for (Document doc : docs) {
        OperationStatus operationStatus = new OperationStatus("ok", doc.getOperationId(), ErrorCode.OK, false, "");
        result.append(operationStatus.render());
    }
    return new ByteArrayInputStream(result.toString().getBytes(StandardCharsets.UTF_8));
}
Also used : ByteArrayInputStream(java.io.ByteArrayInputStream) OperationStatus(com.yahoo.vespa.http.client.core.OperationStatus) Document(com.yahoo.vespa.http.client.core.Document)

Example 2 with Document

use of com.yahoo.vespa.http.client.core.Document in project vespa by vespa-engine.

the class OperationProcessor method process.

private Result process(EndpointResult endpointResult, int clusterId) {
    synchronized (monitor) {
        if (!docSendInfoByOperationId.containsKey(endpointResult.getOperationId())) {
            log.finer("Received out-of-order or too late result, discarding: " + endpointResult);
            return null;
        }
        DocumentSendInfo documentSendInfo = docSendInfoByOperationId.get(endpointResult.getOperationId());
        if (retriedThis(endpointResult, documentSendInfo, clusterId)) {
            return null;
        }
        if (!documentSendInfo.addIfNotAlreadyThere(endpointResult.getDetail(), clusterId)) {
            // Duplicate message, we have seen this operation before.
            return null;
        }
        // Is this the last operation we are waiting for?
        if (documentSendInfo.detailCount() != numDestinations) {
            return null;
        }
        Result result = documentSendInfo.createResult();
        docSendInfoByOperationId.remove(endpointResult.getOperationId());
        String documentId = documentSendInfo.getDocument().getDocumentId();
        /**
         * If we got a pending operation against this document
         * dont't remove it from inflightDocuments and send blocked document operation
         */
        List<Document> blockedDocuments = blockedDocumentsByDocumentId.get(documentId);
        if (blockedDocuments.isEmpty()) {
            inflightDocumentIds.remove(documentId);
        } else {
            sendToClusters(blockedDocuments.remove(0));
        }
        return result;
    }
}
Also used : Document(com.yahoo.vespa.http.client.core.Document) Result(com.yahoo.vespa.http.client.Result) EndpointResult(com.yahoo.vespa.http.client.core.EndpointResult)

Example 3 with Document

use of com.yahoo.vespa.http.client.core.Document in project vespa by vespa-engine.

the class ApacheGatewayConnectionTest method testProtocolV3.

@Test
public void testProtocolV3() throws Exception {
    final Endpoint endpoint = Endpoint.create("hostname", 666, false);
    final FeedParams feedParams = new FeedParams.Builder().setDataFormat(FeedParams.DataFormat.JSON_UTF8).build();
    final String clusterSpecificRoute = "";
    final ConnectionParams connectionParams = new ConnectionParams.Builder().setEnableV3Protocol(true).build();
    final List<Document> documents = new ArrayList<>();
    final CountDownLatch verifyContentSentLatch = new CountDownLatch(1);
    final String vespaDocContent = "Hello, I a JSON doc.";
    final String docId = "42";
    final AtomicInteger requestsReceived = new AtomicInteger(0);
    // This is the fake server, takes header client ID and uses this as session Id.
    ApacheGatewayConnection.HttpClientFactory mockFactory = mockHttpClientFactory(post -> {
        final Header clientIdHeader = post.getFirstHeader(Headers.CLIENT_ID);
        verifyContentSentLatch.countDown();
        return httpResponse(clientIdHeader.getValue(), "3");
    });
    ApacheGatewayConnection apacheGatewayConnection = new ApacheGatewayConnection(endpoint, feedParams, clusterSpecificRoute, connectionParams, mockFactory, "clientId");
    apacheGatewayConnection.connect();
    apacheGatewayConnection.handshake();
    documents.add(createDoc(docId, vespaDocContent, true));
    apacheGatewayConnection.writeOperations(documents);
    assertTrue(verifyContentSentLatch.await(10, TimeUnit.SECONDS));
}
Also used : ArrayList(java.util.ArrayList) Document(com.yahoo.vespa.http.client.core.Document) CountDownLatch(java.util.concurrent.CountDownLatch) ConnectionParams(com.yahoo.vespa.http.client.config.ConnectionParams) FeedParams(com.yahoo.vespa.http.client.config.FeedParams) Endpoint(com.yahoo.vespa.http.client.config.Endpoint) Header(org.apache.http.Header) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) Test(org.junit.Test)

Example 4 with Document

use of com.yahoo.vespa.http.client.core.Document in project vespa by vespa-engine.

the class ApacheGatewayConnectionTest method dynamic_headers_are_added_to_the_response.

@Test
public void dynamic_headers_are_added_to_the_response() throws IOException, ServerResponseException, InterruptedException {
    ConnectionParams.HeaderProvider headerProvider = mock(ConnectionParams.HeaderProvider.class);
    when(headerProvider.getHeaderValue()).thenReturn("v1").thenReturn("v2").thenReturn("v3");
    ConnectionParams connectionParams = new ConnectionParams.Builder().addDynamicHeader("foo", headerProvider).build();
    CountDownLatch verifyContentSentLatch = new CountDownLatch(1);
    AtomicInteger counter = new AtomicInteger(1);
    ApacheGatewayConnection.HttpClientFactory mockFactory = mockHttpClientFactory(post -> {
        Header[] fooHeader = post.getHeaders("foo");
        assertEquals(1, fooHeader.length);
        assertEquals("foo", fooHeader[0].getName());
        assertEquals("v" + counter.getAndIncrement(), fooHeader[0].getValue());
        verifyContentSentLatch.countDown();
        return httpResponse("clientId", "3");
    });
    ApacheGatewayConnection apacheGatewayConnection = new ApacheGatewayConnection(Endpoint.create("hostname", 666, false), new FeedParams.Builder().build(), "", connectionParams, mockFactory, "clientId");
    apacheGatewayConnection.connect();
    apacheGatewayConnection.handshake();
    List<Document> documents = new ArrayList<>();
    documents.add(createDoc("42", "content", true));
    apacheGatewayConnection.writeOperations(documents);
    apacheGatewayConnection.writeOperations(documents);
    assertTrue(verifyContentSentLatch.await(10, TimeUnit.SECONDS));
    // 1x connect(), 2x writeOperations()
    verify(headerProvider, times(3)).getHeaderValue();
}
Also used : Header(org.apache.http.Header) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) ArrayList(java.util.ArrayList) CountDownLatch(java.util.concurrent.CountDownLatch) Document(com.yahoo.vespa.http.client.core.Document) ConnectionParams(com.yahoo.vespa.http.client.config.ConnectionParams) Test(org.junit.Test)

Example 5 with Document

use of com.yahoo.vespa.http.client.core.Document in project vespa by vespa-engine.

the class CloseableQTestCase method requestThatPutIsInterruptedOnClose.

@Test
public void requestThatPutIsInterruptedOnClose() throws InterruptedException {
    final DocumentQueue q = new DocumentQueue(1);
    q.put(new Document("id", "data", null));
    Thread t = new Thread(new Runnable() {

        @Override
        public void run() {
            try {
                Thread.sleep(3000);
            } catch (InterruptedException e) {
            }
            q.close();
            q.clear();
        }
    });
    t.start();
    try {
        q.put(new Document("id2", "data2", null));
        fail("This shouldn't have worked.");
    } catch (IllegalStateException ise) {
    // ok!
    }
    try {
        t.join();
    } catch (InterruptedException e) {
    }
}
Also used : Document(com.yahoo.vespa.http.client.core.Document) Test(org.junit.Test)

Aggregations

Document (com.yahoo.vespa.http.client.core.Document)18 Endpoint (com.yahoo.vespa.http.client.config.Endpoint)8 ArrayList (java.util.ArrayList)7 Test (org.junit.Test)7 ConnectionParams (com.yahoo.vespa.http.client.config.ConnectionParams)5 FeedParams (com.yahoo.vespa.http.client.config.FeedParams)4 EndpointResult (com.yahoo.vespa.http.client.core.EndpointResult)4 CountDownLatch (java.util.concurrent.CountDownLatch)4 Header (org.apache.http.Header)4 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)3 Result (com.yahoo.vespa.http.client.Result)1 Cluster (com.yahoo.vespa.http.client.config.Cluster)1 SessionParams (com.yahoo.vespa.http.client.config.SessionParams)1 OperationStatus (com.yahoo.vespa.http.client.core.OperationStatus)1 ServerResponseException (com.yahoo.vespa.http.client.core.ServerResponseException)1 ByteArrayInputStream (java.io.ByteArrayInputStream)1 IOException (java.io.IOException)1 ByteBuffer (java.nio.ByteBuffer)1 CharsetEncoder (java.nio.charset.CharsetEncoder)1 ArrayDeque (java.util.ArrayDeque)1