Java Code Examples for org.apache.solr.common.SolrException#code()

The following examples show how to use org.apache.solr.common.SolrException#code() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: AbstractFullDistribZkTestBase.java    From lucene-solr with Apache License 2.0 6 votes vote down vote up
public static void waitForNon403or404or503(HttpSolrClient collectionClient)
    throws Exception {
  SolrException exp = null;
  final TimeOut timeout = new TimeOut(30, TimeUnit.SECONDS, TimeSource.NANO_TIME);

  while (! timeout.hasTimedOut()) {
    boolean missing = false;

    try {
      collectionClient.query(new SolrQuery("*:*"));
    } catch (SolrException e) {
      if (!(e.code() == 403 || e.code() == 503 || e.code() == 404)) {
        throw e;
      }
      exp = e;
      missing = true;
    }
    if (!missing) {
      return;
    }
    Thread.sleep(50);
  }

  fail("Could not find the new collection - " + exp.code() + " : " + collectionClient.getBaseURL());
}
 
Example 2
Source File: MockAuthorizationPlugin.java    From lucene-solr with Apache License 2.0 6 votes vote down vote up
@Override
public AuthorizationResponse authorize(AuthorizationContext context) {
  String uname = context.getUserPrincipal() == null ? null : context.getUserPrincipal().getName();
  if (predicate != null) {
    try {
      predicate.accept(context);
      return new AuthorizationResponse(200);
    } catch (SolrException e) {
      return new AuthorizationResponse(e.code());
    }
  } else {
    if (!protectedResources.contains(context.getResource())) {
      return new AuthorizationResponse(200);
    }
    if (uname == null) uname = context.getParams().get("uname");
    log.info("User request: {}", uname);
    if (uname == null || denyUsers.contains(uname))
      return new AuthorizationResponse(403);
    else
      return new AuthorizationResponse(200);
  }
}
 
Example 3
Source File: DefaultRetryPolicyFactory.java    From kite with Apache License 2.0 6 votes vote down vote up
@Override
public RetryPolicy getRetryPolicy(Throwable exception, SolrRequest request, SolrClient server,
    RetryPolicy currentPolicy) {
  if (exception instanceof SolrException) {
    SolrException sex = (SolrException) exception;
    if (sex.code() == ErrorCode.UNAUTHORIZED.code) {
      return RetryPolicyFactory.DONT_RETRY; // no point retrying that - would never succeed
    }
    if (sex.code() == ErrorCode.UNSUPPORTED_MEDIA_TYPE.code) {
      return RetryPolicyFactory.DONT_RETRY; // no point retrying that - would never succeed
    }
    if (sex.getMessage().startsWith("undefined field")) { // schema.xml mismatch, see IndexSchema.java
      return RetryPolicyFactory.DONT_RETRY; // no point retrying that - would never succeed
    }
  }
  if (currentPolicy == null) {
    return initialRetryPolicy; // init
  } else {
    return currentPolicy; // continue with current policy
  }
}
 
Example 4
Source File: SolrParamTest.java    From lucene-solr with Apache License 2.0 5 votes vote down vote up
public static int getReturnCode( Runnable runnable )
{
  try {
    runnable.run();
  }
  catch( SolrException sx ) {
    return sx.code();
  }
  catch( Exception ex ) {
    ex.printStackTrace();
    return 500;
  }
  return 200;
}
 
Example 5
Source File: DocBasedVersionConstraintsProcessor.java    From lucene-solr with Apache License 2.0 5 votes vote down vote up
@Override
public void processAdd(AddUpdateCommand cmd) throws IOException {
  if (isNotLeader(cmd)) {
    super.processAdd(cmd);
    return;
  }

  final SolrInputDocument newDoc = cmd.getSolrInputDocument();
  Object[] newVersions = getUserVersionsFromDocument(newDoc);
  validateUserVersions(newVersions, versionFieldNames, "Doc does not have versionField: ");

  for (int i=0; ;i++) {
    logOverlyFailedRetries(i, cmd);

    if (!isVersionNewEnough(cmd.getIndexedId(), newVersions)) {
      // drop older update
      return;
    }

    try {
      cmd.setVersion(oldSolrVersion);  // use optimistic concurrency to ensure that the doc has not changed in the meantime
      super.processAdd(cmd);
      return;
    } catch (SolrException e) {
      if (e.code() == 409) {
        continue;  // if a version conflict, retry
      }
      throw e;  // rethrow
    }

  }
}
 
Example 6
Source File: AtomicUpdateProcessorFactory.java    From lucene-solr with Apache License 2.0 5 votes vote down vote up
private void processAddWithRetry(AddUpdateCommand cmd, int attempts, SolrInputDocument clonedOriginalDoc) throws IOException {
  try {
    super.processAdd(cmd);
  } catch (SolrException e) {
    if (attempts++ >= MAX_ATTEMPTS) {//maximum number of attempts allowed: 25
      throw new SolrException(SERVER_ERROR,
          "Atomic update failed after multiple attempts due to " + e.getMessage());
    }
    if (e.code() == ErrorCode.CONFLICT.code) { // version conflict
      log.warn("Atomic update failed due to {} Retrying with new version .... ({})"
          , e.getMessage(), attempts);

      Long lastVersion = vinfo.lookupVersion(cmd.getIndexedId());
      // if lastVersion is null then we put -1 to assert that document must not exist
      lastVersion = lastVersion == null ? -1 : lastVersion;

      // The AtomicUpdateDocumentMerger modifies the AddUpdateCommand.solrDoc to populate the real values of the
      // modified fields. We don't want those absolute values because they are out-of-date due to the conflict
      // so we restore the original document created in processAdd method and set the right version on it
      cmd.solrDoc = clonedOriginalDoc;
      clonedOriginalDoc = clonedOriginalDoc.deepCopy(); // copy again because the old cloned ref will be modified during processAdd
      cmd.solrDoc.setField(VERSION, lastVersion);

      processAddWithRetry(cmd, attempts, clonedOriginalDoc);
    }
  }
}
 
Example 7
Source File: TimeExceededStubException.java    From lucene-solr with Apache License 2.0 4 votes vote down vote up
public static boolean isIt(SolrException e) {
  return e.code() == HTTP_CODE && SolrQueryResponse.RESPONSE_HEADER_PARTIAL_RESULTS_KEY.equals(e.getMessage());
}
 
Example 8
Source File: ResponseUtils.java    From lucene-solr with Apache License 2.0 4 votes vote down vote up
/**
 * Adds the given Throwable's message to the given NamedList.
 * <p>
 * If the response code is not a regular code, the Throwable's
 * stack trace is both logged and added to the given NamedList.
 * <p>
 * Status codes less than 100 are adjusted to be 500.
 */
@SuppressWarnings({"unchecked"})
public static int getErrorInfo(Throwable ex, @SuppressWarnings({"rawtypes"})NamedList info, Logger log) {
  int code = 500;
  if (ex instanceof SolrException) {
    SolrException solrExc = (SolrException)ex;
    code = solrExc.code();
    NamedList<String> errorMetadata = solrExc.getMetadata();
    if (errorMetadata == null) {
      errorMetadata = new NamedList<>();
    }
    errorMetadata.add(SolrException.ERROR_CLASS, ex.getClass().getName());
    errorMetadata.add(SolrException.ROOT_ERROR_CLASS, SolrException.getRootCause(ex).getClass().getName());
    info.add("metadata", errorMetadata);
    if (ex instanceof ApiBag.ExceptionWithErrObject) {
      ApiBag.ExceptionWithErrObject exception = (ApiBag.ExceptionWithErrObject) ex;
      info.add("details", exception.getErrs() );
    }
  }
  
  for (Throwable th = ex; th != null; th = th.getCause()) {
    String msg = th.getMessage();
    if (msg != null) {
      info.add("msg", msg);
      break;
    }
  }
  
  // For any regular code, don't include the stack trace
  if (code == 500 || code < 100) {
    StringWriter sw = new StringWriter();
    ex.printStackTrace(new PrintWriter(sw));
    SolrException.log(log, null, ex);
    info.add("trace", sw.toString());

    // non standard codes have undefined results with various servers
    if (code < 100) {
      log.warn("invalid return code: {}", code);
      code = 500;
    }
  }
  
  info.add("code", code);
  return code;
}
 
Example 9
Source File: DirectSolrInputDocumentWriter.java    From hbase-indexer with Apache License 2.0 4 votes vote down vote up
private boolean isDocumentIssue(SolrException e) {
    return e.code() == ErrorCode.BAD_REQUEST.code;
}
 
Example 10
Source File: DirectSolrClassicInputDocumentWriter.java    From hbase-indexer with Apache License 2.0 4 votes vote down vote up
private boolean isDocumentIssue(SolrException e) {
    return e.code() == ErrorCode.BAD_REQUEST.code;
}