@Override public void init() { if (esClient != null) { return; } ElasticSearchAdmin esa = Framework.getService(ElasticSearchAdmin.class); esClient = esa.getClient(); indexName = esa.getIndexNameForType(ElasticSearchConstants.SEQ_ID_TYPE); try { boolean indexExists = esClient.indexExists(indexName); if (!indexExists) { throw new NuxeoException( String.format("Sequencer %s needs an elasticSearchIndex contribution with type %s", getName(), ElasticSearchConstants.SEQ_ID_TYPE)); } } catch (NoSuchElementException | NuxeoException e) { dispose(); throw e; } }
protected void initWriteAlias(ElasticSearchIndexConfig conf, boolean dropIfExists) { // init the write index and alias String writeAlias = conf.writeIndexOrAlias(); String writeIndex = getClient().getFirstIndexForAlias(writeAlias); String nextWriteIndex = conf.newWriteIndexForAlias(conf.getName(), writeIndex); if (writeIndex != null && !dropIfExists) { // alias exists make sure the index is well configured initIndex(writeIndex, conf, false); } else { // create a new write index and update the alias, we don't drop anything if (getClient().indexExists(nextWriteIndex)) { throw new IllegalStateException( String.format("New index name %s for the alias %s already exists", nextWriteIndex, writeAlias)); } initIndex(nextWriteIndex, conf, false); getClient().updateAlias(writeAlias, nextWriteIndex); } }
conf.getType())); boolean mappingExists = false; boolean indexExists = getClient().indexExists(indexName); if (indexExists) { if (!dropIfExists) {
protected void initSearchAlias(ElasticSearchIndexConfig conf) { // init the search alias String searchAlias = conf.getName(); String searchIndex = getClient().getFirstIndexForAlias(searchAlias); String writeAlias = conf.writeIndexOrAlias(); String writeIndex = getClient().getFirstIndexForAlias(writeAlias); if (searchIndex == null) { if (getClient().indexExists(searchAlias)) { if (Framework.isTestModeSet()) { // in test mode we drop an index that have the target alias name getClient().deleteIndex(searchAlias, TIMEOUT_DELETE_SECOND); } searchIndex = searchAlias; } else { // search alias is not created, point to the write index getClient().updateAlias(searchAlias, writeIndex); searchIndex = writeIndex; } } log.info(String.format("Managed index aliases: Alias: %s -> index: %s, alias: %s -> index: %s", searchAlias, searchIndex, writeAlias, writeIndex)); }
/** * Ensures the audit sequence returns an UID greater or equal than the maximum log entry id. */ protected void ensureUIDSequencer(ESClient esClient) { boolean auditIndexExists = esClient.indexExists(getESIndexName()); if (!auditIndexExists) { return; } // Get max log entry id SearchRequest request = createSearchRequest(); request.source(new SearchSourceBuilder().query(QueryBuilders.matchAllQuery()) .aggregation(AggregationBuilders.max("maxAgg").field("id"))); SearchResponse searchResponse = esClient.search(request); Max agg = searchResponse.getAggregations().get("maxAgg"); long maxLogEntryId = (long) agg.getValue(); // Get next sequence id UIDGeneratorService uidGeneratorService = Framework.getService(UIDGeneratorService.class); UIDSequencer seq = uidGeneratorService.getSequencer(); seq.init(); long nextSequenceId = seq.getNextLong(SEQ_NAME); // Increment sequence to max log entry id if needed if (nextSequenceId < maxLogEntryId) { log.info(String.format("Next UID returned by %s sequence is %d, initializing sequence to %d", SEQ_NAME, nextSequenceId, maxLogEntryId)); seq.initSequence(SEQ_NAME, maxLogEntryId); } }