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