001/* 002 * (C) Copyright 2018 Nuxeo SA (http://nuxeo.com/) and others. 003 * 004 * Licensed under the Apache License, Version 2.0 (the "License"); 005 * you may not use this file except in compliance with the License. 006 * You may obtain a copy of the License at 007 * 008 * http://www.apache.org/licenses/LICENSE-2.0 009 * 010 * Unless required by applicable law or agreed to in writing, software 011 * distributed under the License is distributed on an "AS IS" BASIS, 012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 013 * See the License for the specific language governing permissions and 014 * limitations under the License. 015 * 016 * Contributors: 017 * bdelbosc 018 */ 019package org.nuxeo.elasticsearch.bulk; 020 021import static org.nuxeo.elasticsearch.bulk.IndexAction.INDEX_UPDATE_ALIAS_PARAM; 022import static org.nuxeo.elasticsearch.bulk.IndexAction.REFRESH_INDEX_PARAM; 023 024import org.apache.logging.log4j.LogManager; 025import org.apache.logging.log4j.Logger; 026import org.nuxeo.ecm.core.bulk.BulkCodecs; 027import org.nuxeo.ecm.core.bulk.BulkService; 028import org.nuxeo.ecm.core.bulk.message.BulkCommand; 029import org.nuxeo.ecm.core.bulk.message.BulkStatus; 030import org.nuxeo.elasticsearch.api.ElasticSearchAdmin; 031import org.nuxeo.lib.stream.codec.Codec; 032import org.nuxeo.lib.stream.computation.AbstractComputation; 033import org.nuxeo.lib.stream.computation.ComputationContext; 034import org.nuxeo.lib.stream.computation.Record; 035import org.nuxeo.runtime.api.Framework; 036 037/** 038 * On indexing completion, do extra tasks like refresh or update index alias. 039 * 040 * @since 10.3 041 */ 042public class IndexCompletionComputation extends AbstractComputation { 043 044 private static final Logger log = LogManager.getLogger(IndexCompletionComputation.class); 045 046 public static final String NAME = "bulk/indexCompletion"; 047 048 protected Codec<BulkStatus> codec; 049 050 public IndexCompletionComputation() { 051 super(NAME, 1, 0); 052 } 053 054 @Override 055 public void init(ComputationContext context) { 056 super.init(context); 057 this.codec = BulkCodecs.getStatusCodec(); 058 } 059 060 @Override 061 public void processRecord(ComputationContext context, String inputStream, Record record) { 062 BulkStatus status = codec.decode(record.getData()); 063 if (IndexAction.ACTION_NAME.equals(status.getAction()) 064 && BulkStatus.State.COMPLETED.equals(status.getState())) { 065 logIndexing(status); 066 BulkService bulkService = Framework.getService(BulkService.class); 067 BulkCommand command = bulkService.getCommand(status.getId()); 068 if (command != null) { 069 refreshIndexIfNeeded(command); 070 updateAliasIfNeeded(command); 071 } else { 072 log.error("Command not found for id: {}", status::getId); 073 } 074 } 075 context.askForCheckpoint(); 076 } 077 078 protected void refreshIndexIfNeeded(BulkCommand command) { 079 Boolean refresh = command.getParam(REFRESH_INDEX_PARAM); 080 if (Boolean.TRUE.equals(refresh)) { 081 log.warn("Refresh index requested by command: {}", command::getId); 082 ElasticSearchAdmin esa = Framework.getService(ElasticSearchAdmin.class); 083 esa.refreshRepositoryIndex(command.getRepository()); 084 } 085 } 086 087 protected void updateAliasIfNeeded(BulkCommand command) { 088 Boolean updateAlias = command.getParam(INDEX_UPDATE_ALIAS_PARAM); 089 if (Boolean.TRUE.equals(updateAlias)) { 090 log.warn("Update alias requested by command: {}", command::getId); 091 ElasticSearchAdmin esa = Framework.getService(ElasticSearchAdmin.class); 092 esa.syncSearchAndWriteAlias(esa.getIndexNameForRepository(command.getRepository())); 093 } 094 } 095 096 protected void logIndexing(BulkStatus status) { 097 long elapsed = status.getCompletedTime().toEpochMilli() - status.getSubmitTime().toEpochMilli(); 098 long wait = status.getScrollStartTime().toEpochMilli() - status.getSubmitTime().toEpochMilli(); 099 long scroll = status.getScrollEndTime().toEpochMilli() - status.getScrollStartTime().toEpochMilli(); 100 double rate = 1000.0 * status.getTotal() / (elapsed); 101 log.warn("Index command: {} completed: {} in {}", status.getId(), status.getTotal(), 102 String.format("%.2fs (wait: %.2fs, scroll: %.2fs) rate: %.2f docs/s", elapsed / 1000.0, wait / 1000.0, 103 scroll / 1000.0, rate)); 104 } 105 106}