Search in sources :

Example 1 with SessionToken

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

the class HttpReplicator method checkForUpdate.

@Override
public SessionToken checkForUpdate(String currVersion) throws IOException {
    String[] params = null;
    if (currVersion != null) {
        params = new String[] { ReplicationService.REPLICATE_VERSION_PARAM, currVersion };
    }
    final HttpResponse response = executeGET(ReplicationAction.UPDATE.name(), params);
    return doAction(response, new Callable<SessionToken>() {

        @Override
        public SessionToken call() throws Exception {
            final DataInputStream dis = new DataInputStream(responseInputStream(response));
            try {
                if (dis.readByte() == 0) {
                    return null;
                } else {
                    return new SessionToken(dis);
                }
            } finally {
                dis.close();
            }
        }
    });
}
Also used : SessionToken(org.apache.lucene.replicator.SessionToken) HttpResponse(org.apache.http.HttpResponse) DataInputStream(java.io.DataInputStream) IOException(java.io.IOException)

Example 2 with SessionToken

use of org.apache.lucene.replicator.SessionToken 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

IOException (java.io.IOException)2 SessionToken (org.apache.lucene.replicator.SessionToken)2 DataInputStream (java.io.DataInputStream)1 DataOutputStream (java.io.DataOutputStream)1 InputStream (java.io.InputStream)1 ObjectOutputStream (java.io.ObjectOutputStream)1 ServletException (javax.servlet.ServletException)1 ServletOutputStream (javax.servlet.ServletOutputStream)1 HttpResponse (org.apache.http.HttpResponse)1 Replicator (org.apache.lucene.replicator.Replicator)1