Search in sources :

Example 1 with Replicator

use of org.apache.lucene.replicator.Replicator in project lucene-solr by apache.

the class HttpReplicatorTest method testServerErrors.

@Test
public void testServerErrors() throws Exception {
    // tests the behaviour of the client when the server sends an error
    // must use BasicClientConnectionManager to test whether the client is closed correctly
    BasicHttpClientConnectionManager conMgr = new BasicHttpClientConnectionManager();
    Replicator replicator = new HttpReplicator(host, port, ReplicationService.REPLICATION_CONTEXT + "/s1", conMgr);
    ReplicationClient client = new ReplicationClient(replicator, new IndexReplicationHandler(handlerIndexDir, null), new PerSessionDirectoryFactory(clientWorkDir));
    try {
        publishRevision(5);
        try {
            replicationServlet.setRespondWithError(true);
            client.updateNow();
            fail("expected exception");
        } catch (Throwable t) {
        // expected
        }
        replicationServlet.setRespondWithError(false);
        // now it should work
        client.updateNow();
        reopenReader();
        assertEquals(5, Integer.parseInt(reader.getIndexCommit().getUserData().get("ID"), 16));
        client.close();
    } finally {
        replicationServlet.setRespondWithError(false);
    }
}
Also used : PerSessionDirectoryFactory(org.apache.lucene.replicator.PerSessionDirectoryFactory) Replicator(org.apache.lucene.replicator.Replicator) LocalReplicator(org.apache.lucene.replicator.LocalReplicator) IndexReplicationHandler(org.apache.lucene.replicator.IndexReplicationHandler) ReplicationClient(org.apache.lucene.replicator.ReplicationClient) BasicHttpClientConnectionManager(org.apache.http.impl.conn.BasicHttpClientConnectionManager) Test(org.junit.Test)

Example 2 with Replicator

use of org.apache.lucene.replicator.Replicator in project lucene-solr by apache.

the class HttpReplicatorTest method testBasic.

@Test
public void testBasic() throws Exception {
    Replicator replicator = new HttpReplicator(host, port, ReplicationService.REPLICATION_CONTEXT + "/s1", getClientConnectionManager());
    ReplicationClient client = new ReplicationClient(replicator, new IndexReplicationHandler(handlerIndexDir, null), new PerSessionDirectoryFactory(clientWorkDir));
    publishRevision(1);
    client.updateNow();
    reopenReader();
    assertEquals(1, Integer.parseInt(reader.getIndexCommit().getUserData().get("ID"), 16));
    publishRevision(2);
    client.updateNow();
    reopenReader();
    assertEquals(2, Integer.parseInt(reader.getIndexCommit().getUserData().get("ID"), 16));
    client.close();
}
Also used : PerSessionDirectoryFactory(org.apache.lucene.replicator.PerSessionDirectoryFactory) Replicator(org.apache.lucene.replicator.Replicator) LocalReplicator(org.apache.lucene.replicator.LocalReplicator) IndexReplicationHandler(org.apache.lucene.replicator.IndexReplicationHandler) ReplicationClient(org.apache.lucene.replicator.ReplicationClient) Test(org.junit.Test)

Example 3 with Replicator

use of org.apache.lucene.replicator.Replicator in project lucene-solr by apache.

the class ReplicationService method perform.

/** Executes the replication task. */
public void perform(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
    String[] pathElements = getPathElements(req);
    if (pathElements.length != 2) {
        throw new ServletException("invalid path, must contain shard ID and action, e.g. */s1/update");
    }
    final ReplicationAction action;
    try {
        action = ReplicationAction.valueOf(pathElements[ACTION_IDX].toUpperCase(Locale.ENGLISH));
    } catch (IllegalArgumentException e) {
        throw new ServletException("Unsupported action provided: " + pathElements[ACTION_IDX]);
    }
    final Replicator replicator = replicators.get(pathElements[SHARD_IDX]);
    if (replicator == null) {
        throw new ServletException("unrecognized shard ID " + pathElements[SHARD_IDX]);
    }
    // SOLR-8933 Don't close this stream.
    ServletOutputStream resOut = resp.getOutputStream();
    try {
        switch(action) {
            case OBTAIN:
                final String sessionID = extractRequestParam(req, REPLICATE_SESSION_ID_PARAM);
                final String fileName = extractRequestParam(req, REPLICATE_FILENAME_PARAM);
                final String source = extractRequestParam(req, REPLICATE_SOURCE_PARAM);
                InputStream in = replicator.obtainFile(sessionID, source, fileName);
                try {
                    copy(in, resOut);
                } finally {
                    in.close();
                }
                break;
            case RELEASE:
                replicator.release(extractRequestParam(req, REPLICATE_SESSION_ID_PARAM));
                break;
            case UPDATE:
                String currVersion = req.getParameter(REPLICATE_VERSION_PARAM);
                SessionToken token = replicator.checkForUpdate(currVersion);
                if (token == null) {
                    // marker for null token
                    resOut.write(0);
                } else {
                    resOut.write(1);
                    token.serialize(new DataOutputStream(resOut));
                }
                break;
        }
    } catch (Exception e) {
        // propagate the failure
        resp.setStatus(HttpStatus.SC_INTERNAL_SERVER_ERROR);
        try {
            /*
         * Note: it is assumed that "identified exceptions" are thrown before
         * anything was written to the stream.
         */
            ObjectOutputStream oos = new ObjectOutputStream(resOut);
            oos.writeObject(e);
            oos.flush();
        } catch (Exception e2) {
            throw new IOException("Could not serialize", e2);
        }
    } finally {
        resp.flushBuffer();
    }
}
Also used : ServletOutputStream(javax.servlet.ServletOutputStream) SessionToken(org.apache.lucene.replicator.SessionToken) InputStream(java.io.InputStream) DataOutputStream(java.io.DataOutputStream) Replicator(org.apache.lucene.replicator.Replicator) IOException(java.io.IOException) ObjectOutputStream(java.io.ObjectOutputStream) ServletException(javax.servlet.ServletException) IOException(java.io.IOException) ServletException(javax.servlet.ServletException)

Aggregations

Replicator (org.apache.lucene.replicator.Replicator)3 IndexReplicationHandler (org.apache.lucene.replicator.IndexReplicationHandler)2 LocalReplicator (org.apache.lucene.replicator.LocalReplicator)2 PerSessionDirectoryFactory (org.apache.lucene.replicator.PerSessionDirectoryFactory)2 ReplicationClient (org.apache.lucene.replicator.ReplicationClient)2 Test (org.junit.Test)2 DataOutputStream (java.io.DataOutputStream)1 IOException (java.io.IOException)1 InputStream (java.io.InputStream)1 ObjectOutputStream (java.io.ObjectOutputStream)1 ServletException (javax.servlet.ServletException)1 ServletOutputStream (javax.servlet.ServletOutputStream)1 BasicHttpClientConnectionManager (org.apache.http.impl.conn.BasicHttpClientConnectionManager)1 SessionToken (org.apache.lucene.replicator.SessionToken)1