Search in sources :

Example 1 with TimeValue

use of org.opensearch.common.unit.TimeValue in project bw-calendar-engine by Bedework.

the class BwIndexEsImpl method fetchEntities.

private <T> List<T> fetchEntities(final String docType, final BuildEntity<T> be, final QueryBuilder filter, final int count) throws CalFacadeException {
    requireDocType(docType);
    int tries = 0;
    final int ourCount;
    if (count < 0) {
        ourCount = maxFetchCount;
    } else {
        ourCount = Math.min(maxFetchCount, count);
    }
    final SearchSourceBuilder ssb = new SearchSourceBuilder().size(ourCount).query(filter);
    final SearchRequest sr = new SearchRequest(targetIndex).source(ssb).scroll(new TimeValue(60000));
    if (debug()) {
        debug("fetchEntities: " + sr);
    }
    final List<T> res = new ArrayList<>();
    try {
        SearchResponse scrollResp = getClient().search(sr, RequestOptions.DEFAULT);
        if (scrollResp.status() != RestStatus.OK) {
            if (debug()) {
                debug("Search returned status " + scrollResp.status());
            }
        }
        for (; ; ) {
            if (tries > absoluteMaxTries) {
                // huge count or we screwed up
                warn("Indexer: too many tries");
                break;
            }
            if (scrollResp.status() != RestStatus.OK) {
                if (debug()) {
                    debug("Search returned status " + scrollResp.status());
                }
            }
            final SearchHits hits = scrollResp.getHits();
            // Break condition: No hits are returned
            if (hits.getHits().length == 0) {
                break;
            }
            for (final SearchHit hit : hits) {
                // Handle the hit...
                final T ent = be.make(getEntityBuilder(hit.getSourceAsMap()), hit.getId());
                if (ent == null) {
                    // No access
                    continue;
                }
                res.add(ent);
            // ourPos++;
            }
            tries++;
            final SearchScrollRequest scrollRequest = new SearchScrollRequest(scrollResp.getScrollId());
            scrollRequest.scroll(new TimeValue(60000));
            scrollResp = getClient().scroll(scrollRequest, RequestOptions.DEFAULT);
        }
    } catch (final Throwable t) {
        throw new CalFacadeException(t);
    }
    return res;
}
Also used : SearchRequest(org.opensearch.action.search.SearchRequest) SearchHit(org.opensearch.search.SearchHit) ArrayList(java.util.ArrayList) SearchScrollRequest(org.opensearch.action.search.SearchScrollRequest) CalFacadeException(org.bedework.calfacade.exc.CalFacadeException) SearchSourceBuilder(org.opensearch.search.builder.SearchSourceBuilder) SearchResponse(org.opensearch.action.search.SearchResponse) SearchHits(org.opensearch.search.SearchHits) TimeValue(org.opensearch.common.unit.TimeValue)

Example 2 with TimeValue

use of org.opensearch.common.unit.TimeValue in project bw-calendar-engine by Bedework.

the class BwIndexEsImpl method reindex.

@Override
public ReindexResponse reindex() {
    if (currentReindexing == null) {
        currentReindexing = new ReindexResponse(docType);
    }
    final ReindexResponse resp = currentReindexing;
    if (resp.getStatus() == processing) {
        return resp;
    }
    // Create a new index.
    final String indexName;
    try {
        indexName = newIndex();
    } catch (final Throwable t) {
        return Response.error(resp, t);
    }
    // Only retrieve masters - we'll query for the overrides
    final QueryBuilder qb = getFilters(RecurringRetrievalMode.entityOnly).getAllForReindex(docType);
    // 1 minute
    final int timeoutMillis = 60000;
    final TimeValue tv = new TimeValue(timeoutMillis);
    final int batchSize = 100;
    final var clResp = sch.getClient();
    if (!clResp.isOk()) {
        return Response.fromResponse(resp, clResp);
    }
    final var cl = clResp.getEntity();
    final BulkListener listener = new BulkListener();
    final BulkProcessor.Builder builder = BulkProcessor.builder((request, bulkListener) -> cl.bulkAsync(request, RequestOptions.DEFAULT, bulkListener), listener);
    final BulkProcessor bulkProcessor = builder.setBulkActions(batchSize).setConcurrentRequests(3).setFlushInterval(tv).build();
    /*
    SearchResponse scrollResp = cl.prepareSearch(targetIndex)
                                  .setSearchType(SearchType.SCAN)
                                  .setScroll(tv)
                                  .setQuery(qb)
                                  .setSize(batchSize)
                                  .execute()
                                  .actionGet(); //100 hits per shard will be returned for each scroll
*/
    final SearchSourceBuilder ssb = new SearchSourceBuilder().size(batchSize).query(qb);
    final SearchRequest sr = new SearchRequest(targetIndex).source(ssb).scroll(tv);
    // Switch to new index
    targetIndex = indexName;
    try {
        SearchResponse scrollResp = cl.search(sr, RequestOptions.DEFAULT);
        if (scrollResp.status() != RestStatus.OK) {
            if (debug()) {
                debug("Search returned status " + scrollResp.status());
            }
        }
        // Scroll until no hits are returned
        while (true) {
            for (final SearchHit hit : scrollResp.getHits().getHits()) {
                resp.incProcessed();
                if ((resp.getProcessed() % 250) == 0) {
                    info("processed " + docType + ": " + resp.getProcessed());
                }
                resp.getStats().inc(docToType.getOrDefault(docType, unreachableEntities));
                final ReindexResponse.Failure hitResp = new ReindexResponse.Failure();
                final Object entity = makeEntity(hitResp, hit, null);
                if (entity == null) {
                    warn("Unable to build entity " + hit.getSourceAsString());
                    resp.incTotalFailed();
                    if (resp.getTotalFailed() < 50) {
                        resp.addFailure(hitResp);
                    }
                    continue;
                }
                if (entity instanceof BwShareableDbentity) {
                    final BwShareableDbentity<?> ent = (BwShareableDbentity<?>) entity;
                    principalHref = ent.getOwnerHref();
                }
                if (entity instanceof EventInfo) {
                    // This might be a single event or a recurring event.
                    final EventInfo ei = (EventInfo) entity;
                    final BwEvent ev = ei.getEvent();
                    if (ev.getRecurring()) {
                        resp.incRecurring();
                    }
                    if (!reindexEvent(hitResp, indexName, hit, ei, bulkProcessor)) {
                        warn("Unable to index event " + hit.getSourceAsString());
                        resp.incTotalFailed();
                        if (resp.getTotalFailed() < 50) {
                            resp.addFailure(hitResp);
                        }
                    }
                } else {
                    final EsDocInfo doc = makeDoc(resp, entity);
                    if (doc == null) {
                        if (resp.getStatus() != ok) {
                            resp.addFailure(hitResp);
                        }
                        continue;
                    }
                    final IndexRequest request = new IndexRequest(indexName);
                    request.id(doc.getId());
                    request.source(doc.getSource());
                    bulkProcessor.add(request);
                    if (entity instanceof BwEventProperty) {
                        caches.put((BwEventProperty<?>) entity);
                    }
                }
            }
            final SearchScrollRequest scrollRequest = new SearchScrollRequest(scrollResp.getScrollId());
            scrollRequest.scroll(tv);
            scrollResp = getClient().scroll(scrollRequest, RequestOptions.DEFAULT);
            // Break condition: No hits are returned
            if (scrollResp.getHits().getHits().length == 0) {
                break;
            }
        }
        try {
            bulkProcessor.awaitClose(10, TimeUnit.MINUTES);
        } catch (final InterruptedException e) {
            errorReturn(resp, "Final bulk close was interrupted. Records may be missing", failed);
        }
    } catch (final Throwable t) {
        errorReturn(resp, t);
    }
    return resp;
}
Also used : SearchRequest(org.opensearch.action.search.SearchRequest) SearchHit(org.opensearch.search.SearchHit) EventInfo(org.bedework.calfacade.svc.EventInfo) BwEvent(org.bedework.calfacade.BwEvent) BwEventProperty(org.bedework.calfacade.BwEventProperty) MatchQueryBuilder(org.opensearch.index.query.MatchQueryBuilder) MatchNoneQueryBuilder(org.opensearch.index.query.MatchNoneQueryBuilder) QueryBuilder(org.opensearch.index.query.QueryBuilder) CreateIndexRequest(org.opensearch.client.indices.CreateIndexRequest) DeleteIndexRequest(org.opensearch.action.admin.indices.delete.DeleteIndexRequest) IndexRequest(org.opensearch.action.index.IndexRequest) SearchScrollRequest(org.opensearch.action.search.SearchScrollRequest) SearchSourceBuilder(org.opensearch.search.builder.SearchSourceBuilder) ReindexResponse(org.bedework.calfacade.indexing.ReindexResponse) BwShareableDbentity(org.bedework.calfacade.base.BwShareableDbentity) BulkProcessor(org.opensearch.action.bulk.BulkProcessor) EsDocInfo(org.bedework.util.opensearch.EsDocInfo) TimeValue(org.opensearch.common.unit.TimeValue) SearchResponse(org.opensearch.action.search.SearchResponse)

Example 3 with TimeValue

use of org.opensearch.common.unit.TimeValue in project bw-calendar-engine by Bedework.

the class BwIndexEsImpl method multiFetch.

/*
  private SearchHits multiColFetch(final List<String> hrefs)
          throws CalFacadeException {
    final int batchSize = hrefs.size();

    final SearchRequestBuilder srb = getClient()
            .prepareSearch(searchIndexes);

    final TermsQueryBuilder tqb =
            new TermsQueryBuilder(
                    ESQueryFilter.getJname(PropertyInfoIndex.HREF),
                    hrefs);

    srb.setSearchType(SearchType.QUERY_THEN_FETCH)
       .setQuery(tqb);
    srb.setFrom(0);
    srb.setSize(batchSize);

    if (debug()) {
      debug("MultiColFetch: targetIndex=" + targetIndex +
                    "; srb=" + srb);
    }

    final SearchResponse resp = srb.execute().actionGet();

    if (resp.status() != RestStatus.OK) {
      if (debug()) {
        debug("Search returned status " + resp.status());
      }

      return null;
    }

    final SearchHits hits = resp.getHits();

    if ((hits.getHits() == null) ||
            (hits.getHits().length == 0)) {
      return null;
    }

    //Break condition: No hits are returned
    if (hits.getHits().length == 0) {
      return null;
    }

    return hits;
  }
  */
private List<SearchHit> multiFetch(final SearchHits hits, final RecurringRetrievalMode rmode) throws CalFacadeException {
    // Make an ored filter from keys
    // Dedup
    final Set<String> hrefs = new TreeSet<>();
    for (final SearchHit hit : hits) {
        final String kval = hit.getId();
        if (kval == null) {
            throw new CalFacadeException("org.bedework.index.noitemkey");
        }
        final Map<String, Object> map = hit.getSourceAsMap();
        final Object field = map.get(ESQueryFilter.hrefJname);
        if (field == null) {
            warn("Unable to get field " + ESQueryFilter.hrefJname + " from " + map);
        } else {
            hrefs.add(field.toString());
        }
    }
    final int batchSize = 1000;
    final List<SearchHit> res = new ArrayList<>();
    final SearchSourceBuilder ssb = new SearchSourceBuilder().size(batchSize).query(getFilters(null).multiHref(hrefs, rmode));
    final SearchRequest req = new SearchRequest(searchIndexes).searchType(SearchType.QUERY_THEN_FETCH).source(ssb).scroll(TimeValue.timeValueMinutes(1L));
    if (debug()) {
        debug("MultiFetch: targetIndex=" + targetIndex + "; ssb=" + ssb);
    }
    try {
        SearchResponse resp = getClient().search(req, RequestOptions.DEFAULT);
        int tries = 0;
        for (; ; ) {
            if (tries > absoluteMaxTries) {
                // huge count or we screwed up
                warn("Indexer: too many tries");
                break;
            }
            if (resp.status() != RestStatus.OK) {
                if (debug()) {
                    debug("Search returned status " + resp.status());
                }
                return null;
            }
            final SearchHit[] hits2 = resp.getHits().getHits();
            if ((hits2 == null) || (hits2.length == 0)) {
                // No more data - we're done
                break;
            }
            res.addAll(Arrays.asList(hits2));
            tries++;
            final SearchScrollRequest scrollRequest = new SearchScrollRequest(resp.getScrollId());
            scrollRequest.scroll(new TimeValue(60000));
            resp = getClient().scroll(scrollRequest, RequestOptions.DEFAULT);
        }
        return res;
    } catch (final Throwable t) {
        throw new CalFacadeException(t);
    }
}
Also used : SearchRequest(org.opensearch.action.search.SearchRequest) SearchHit(org.opensearch.search.SearchHit) ArrayList(java.util.ArrayList) CalFacadeException(org.bedework.calfacade.exc.CalFacadeException) SearchScrollRequest(org.opensearch.action.search.SearchScrollRequest) SearchSourceBuilder(org.opensearch.search.builder.SearchSourceBuilder) SearchResponse(org.opensearch.action.search.SearchResponse) TreeSet(java.util.TreeSet) TimeValue(org.opensearch.common.unit.TimeValue)

Example 4 with TimeValue

use of org.opensearch.common.unit.TimeValue in project fess-crawler by codelibs.

the class EsUrlQueueService method updateSessionId.

@Override
public void updateSessionId(final String oldSessionId, final String newSessionId) {
    SearchResponse response = getClient().get(c -> c.prepareSearch(index).setScroll(new TimeValue(scrollTimeout)).setQuery(QueryBuilders.boolQuery().filter(QueryBuilders.termQuery(SESSION_ID, oldSessionId))).setSize(scrollSize).execute());
    String scrollId = response.getScrollId();
    try {
        while (scrollId != null) {
            final SearchHits searchHits = response.getHits();
            if (searchHits.getHits().length == 0) {
                break;
            }
            final BulkResponse bulkResponse = getClient().get(c -> {
                final BulkRequestBuilder builder = c.prepareBulk();
                for (final SearchHit searchHit : searchHits) {
                    final UpdateRequestBuilder updateRequest = c.prepareUpdate().setIndex(index).setId(searchHit.getId()).setDoc(SESSION_ID, newSessionId);
                    builder.add(updateRequest);
                }
                return builder.execute();
            });
            if (bulkResponse.hasFailures()) {
                throw new EsAccessException(bulkResponse.buildFailureMessage());
            }
            final String sid = scrollId;
            response = getClient().get(c -> c.prepareSearchScroll(sid).setScroll(new TimeValue(scrollTimeout)).execute());
            if (!scrollId.equals(response.getScrollId())) {
                getClient().clearScroll(scrollId);
            }
            scrollId = response.getScrollId();
        }
    } finally {
        getClient().clearScroll(scrollId);
    }
}
Also used : LoggerFactory(org.slf4j.LoggerFactory) SortBuilders(org.opensearch.search.sort.SortBuilders) SearchHits(org.opensearch.search.SearchHits) BulkRequestBuilder(org.opensearch.action.bulk.BulkRequestBuilder) ArrayList(java.util.ArrayList) PreDestroy(javax.annotation.PreDestroy) SortOrder(org.opensearch.search.sort.SortOrder) EsUrlQueue(org.codelibs.fess.crawler.entity.EsUrlQueue) Map(java.util.Map) SearchResponse(org.opensearch.action.search.SearchResponse) EsCrawlerConfig(org.codelibs.fess.crawler.util.EsCrawlerConfig) QueryBuilders(org.opensearch.index.query.QueryBuilders) TimeValue(org.opensearch.common.unit.TimeValue) Logger(org.slf4j.Logger) EsAccessException(org.codelibs.fess.crawler.exception.EsAccessException) SearchHit(org.opensearch.search.SearchHit) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap) Resource(javax.annotation.Resource) StringUtil(org.codelibs.core.lang.StringUtil) Collectors(java.util.stream.Collectors) UpdateRequestBuilder(org.opensearch.action.update.UpdateRequestBuilder) Constants(org.codelibs.fess.crawler.Constants) UrlQueueService(org.codelibs.fess.crawler.service.UrlQueueService) List(java.util.List) OpType(org.opensearch.action.DocWriteRequest.OpType) BulkResponse(org.opensearch.action.bulk.BulkResponse) PostConstruct(javax.annotation.PostConstruct) AccessResult(org.codelibs.fess.crawler.entity.AccessResult) Queue(java.util.Queue) UrlQueue(org.codelibs.fess.crawler.entity.UrlQueue) ConcurrentLinkedQueue(java.util.concurrent.ConcurrentLinkedQueue) RefreshPolicy(org.opensearch.action.support.WriteRequest.RefreshPolicy) SearchHit(org.opensearch.search.SearchHit) EsAccessException(org.codelibs.fess.crawler.exception.EsAccessException) UpdateRequestBuilder(org.opensearch.action.update.UpdateRequestBuilder) BulkResponse(org.opensearch.action.bulk.BulkResponse) SearchHits(org.opensearch.search.SearchHits) BulkRequestBuilder(org.opensearch.action.bulk.BulkRequestBuilder) TimeValue(org.opensearch.common.unit.TimeValue) SearchResponse(org.opensearch.action.search.SearchResponse)

Example 5 with TimeValue

use of org.opensearch.common.unit.TimeValue in project OpenSearch by opensearch-project.

the class SnapshotLifecycleStats method toXContent.

@Override
public XContentBuilder toXContent(XContentBuilder builder, Params params) throws IOException {
    builder.startObject();
    builder.field(RETENTION_RUNS.getPreferredName(), this.retentionRunCount);
    builder.field(RETENTION_FAILED.getPreferredName(), this.retentionFailedCount);
    builder.field(RETENTION_TIMED_OUT.getPreferredName(), this.retentionTimedOut);
    TimeValue retentionTime = TimeValue.timeValueMillis(this.retentionTimeMs);
    builder.field(RETENTION_TIME.getPreferredName(), retentionTime);
    builder.field(RETENTION_TIME_MILLIS.getPreferredName(), retentionTime.millis());
    Map<String, SnapshotPolicyStats> metrics = getMetrics();
    long totalTaken = metrics.values().stream().mapToLong(s -> s.snapshotsTaken).sum();
    long totalFailed = metrics.values().stream().mapToLong(s -> s.snapshotsFailed).sum();
    long totalDeleted = metrics.values().stream().mapToLong(s -> s.snapshotsDeleted).sum();
    long totalDeleteFailures = metrics.values().stream().mapToLong(s -> s.snapshotDeleteFailures).sum();
    builder.field(TOTAL_TAKEN.getPreferredName(), totalTaken);
    builder.field(TOTAL_FAILED.getPreferredName(), totalFailed);
    builder.field(TOTAL_DELETIONS.getPreferredName(), totalDeleted);
    builder.field(TOTAL_DELETION_FAILURES.getPreferredName(), totalDeleteFailures);
    builder.startObject(POLICY_STATS.getPreferredName());
    for (Map.Entry<String, SnapshotPolicyStats> policy : metrics.entrySet()) {
        SnapshotPolicyStats perPolicyMetrics = policy.getValue();
        builder.startObject(perPolicyMetrics.policyId);
        perPolicyMetrics.toXContent(builder, params);
        builder.endObject();
    }
    builder.endObject();
    builder.endObject();
    return builder;
}
Also used : TimeValue(org.opensearch.common.unit.TimeValue) ConstructingObjectParser(org.opensearch.common.xcontent.ConstructingObjectParser) ToXContentFragment(org.opensearch.common.xcontent.ToXContentFragment) IOException(java.io.IOException) ParseField(org.opensearch.common.ParseField) Function(java.util.function.Function) Collectors(java.util.stream.Collectors) Strings(org.opensearch.common.Strings) XContentBuilder(org.opensearch.common.xcontent.XContentBuilder) XContentParser(org.opensearch.common.xcontent.XContentParser) Objects(java.util.Objects) List(java.util.List) ToXContentObject(org.opensearch.common.xcontent.ToXContentObject) Map(java.util.Map) Collections(java.util.Collections) Map(java.util.Map) TimeValue(org.opensearch.common.unit.TimeValue)

Aggregations

TimeValue (org.opensearch.common.unit.TimeValue)260 IOException (java.io.IOException)47 ClusterState (org.opensearch.cluster.ClusterState)43 Settings (org.opensearch.common.settings.Settings)38 ArrayList (java.util.ArrayList)35 List (java.util.List)31 Matchers.containsString (org.hamcrest.Matchers.containsString)31 ThreadPool (org.opensearch.threadpool.ThreadPool)31 CountDownLatch (java.util.concurrent.CountDownLatch)30 ParameterizedMessage (org.apache.logging.log4j.message.ParameterizedMessage)30 ActionListener (org.opensearch.action.ActionListener)29 Map (java.util.Map)28 Logger (org.apache.logging.log4j.Logger)25 AtomicBoolean (java.util.concurrent.atomic.AtomicBoolean)23 SearchResponse (org.opensearch.action.search.SearchResponse)22 HashMap (java.util.HashMap)20 LogManager (org.apache.logging.log4j.LogManager)20 TimeUnit (java.util.concurrent.TimeUnit)19 ClusterStateUpdateTask (org.opensearch.cluster.ClusterStateUpdateTask)19 DiscoveryNode (org.opensearch.cluster.node.DiscoveryNode)19