com.google.appengine.api.search.StatusCode Java Examples
The following examples show how to use
com.google.appengine.api.search.StatusCode.
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: GallerySearchIndex.java From appinventor-extensions with Apache License 2.0 | 6 votes |
/** * index gallery app into search index * @param app galleryapp */ public void indexApp (GalleryApp app) { // take the title, description, and the user name and index it // need to build up a string with all meta data String indexWords = app.getTitle()+" "+app.getDescription() + " " + app.getDeveloperName(); // now create the doc Document doc = Document.newBuilder() .setId(String.valueOf(app.getGalleryAppId())) .addField(Field.newBuilder().setName("content").setText(indexWords)) .build(); Index index = getIndex(); try { index.put(doc); } catch (PutException e) { if (StatusCode.TRANSIENT_ERROR.equals(e.getOperationResult().getCode())) { // retry putting the document } } }
Example #2
Source File: Utils.java From java-docs-samples with Apache License 2.0 | 6 votes |
/** * Put a given document into an index with the given indexName. * * @param indexName The name of the index. * @param document A document to add. * @throws InterruptedException When Thread.sleep is interrupted. */ // [START putting_document_with_retry] public static void indexADocument(String indexName, Document document) throws InterruptedException { IndexSpec indexSpec = IndexSpec.newBuilder().setName(indexName).build(); Index index = SearchServiceFactory.getSearchService().getIndex(indexSpec); final int maxRetry = 3; int attempts = 0; int delay = 2; while (true) { try { index.put(document); } catch (PutException e) { if (StatusCode.TRANSIENT_ERROR.equals(e.getOperationResult().getCode()) && ++attempts < maxRetry) { // retrying Thread.sleep(delay * 1000); delay *= 2; // easy exponential backoff continue; } else { throw e; // otherwise throw } } break; } }
Example #3
Source File: SearchManager.java From teammates with GNU General Public License v2.0 | 4 votes |
/** * Tries putting a document, handling transient errors by retrying with exponential backoff. * * @throws PutException if a non-transient error is encountered. * @throws MaximumRetriesExceededException with final {@link OperationResult}'s message as final message, * if operation fails after maximum retries. */ private static void putDocumentWithRetry(String indexName, Document document) throws MaximumRetriesExceededException { Index index = getIndex(indexName); /* * The GAE Search API signals put document failure in two ways: it either * returns a PutResponse containing an OperationResult with a non-OK StatusCode, or * throws a PutException that also contains an embedded OperationResult. * We handle both ways by examining the OperationResult to determine what kind of error it is. If it is * transient, we use RetryManager to retry the operation; if it is * non-transient, we do not retry but throw a PutException upwards instead. */ RM.runUntilSuccessful(new RetryableTaskThrows<PutException>("Put document") { private OperationResult lastResult; @Override public void run() { try { PutResponse response = index.put(document); lastResult = response.getResults().get(0); } catch (PutException e) { lastResult = e.getOperationResult(); } } @Override public boolean isSuccessful() { // Update the final message to be shown if the task fails after maximum retries finalMessage = lastResult.getMessage(); if (StatusCode.OK.equals(lastResult.getCode())) { return true; } else if (StatusCode.TRANSIENT_ERROR.equals(lastResult.getCode())) { // A transient error can be retried return false; } else { // A non-transient error signals that the operation should not be retried throw new PutException(lastResult); } } }); }
Example #4
Source File: SearchManager.java From teammates with GNU General Public License v2.0 | 4 votes |
/** * Tries putting multiple documents, handling transient errors by retrying with exponential backoff. * * @throws PutException when only non-transient errors are encountered. * @throws MaximumRetriesExceededException with list of failed {@link Document}s as final data and * final {@link OperationResult}'s message as final message, if operation fails after maximum retries. */ private static void putDocumentsWithRetry(String indexName, List<Document> documents) throws MaximumRetriesExceededException { Index index = getIndex(indexName); /* * The GAE Search API allows batch putting a List of Documents. * Results for each document are reported via a List of OperationResults. * We use RetryManager to retry putting a List of Documents, with each retry re-putting only * the documents that failed in the previous retry. * If we encounter one or more transient errors, we retry the operation. * If all results are non-transient errors, we give up and throw a PutException upwards. */ RM.runUntilSuccessful(new RetryableTaskThrows<PutException>("Put documents") { private List<Document> documentsToPut = documents; private List<OperationResult> lastResults; private List<String> lastIds; @Override public void run() { try { PutResponse response = index.put(documentsToPut); lastResults = response.getResults(); lastIds = response.getIds(); } catch (PutException e) { lastResults = e.getResults(); lastIds = e.getIds(); } } @Override public boolean isSuccessful() { boolean hasTransientError = false; List<Document> failedDocuments = new ArrayList<>(); for (int i = 0; i < documentsToPut.size(); i++) { StatusCode code = lastResults.get(i).getCode(); if (!StatusCode.OK.equals(code)) { failedDocuments.add(documentsToPut.get(i)); if (StatusCode.TRANSIENT_ERROR.equals(code)) { hasTransientError = true; } } } // Update the list of documents to be put during the next retry documentsToPut = failedDocuments; // Update the final message and data to be shown if the task fails after maximum retries finalMessage = lastResults.get(0).getMessage(); finalData = documentsToPut; if (documentsToPut.isEmpty()) { return true; } else if (hasTransientError) { // If there is at least one transient error, continue retrying return false; } else { // If all errors are non-transient, do not continue retrying throw new PutException(lastResults.get(0), lastResults, lastIds); } } }); }