| /* |
| * Licensed to the Apache Software Foundation (ASF) under one |
| * or more contributor license agreements. See the NOTICE file |
| * distributed with this work for additional information |
| * regarding copyright ownership. The ASF licenses this file |
| * to you under the Apache License, Version 2.0 (the |
| * "License"); you may not use this file except in compliance |
| * with the License. You may obtain a copy of the License at |
| * |
| * http://www.apache.org/licenses/LICENSE-2.0 |
| * |
| * Unless required by applicable law or agreed to in writing, |
| * software distributed under the License is distributed on an |
| * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY |
| * KIND, either express or implied. See the License for the |
| * specific language governing permissions and limitations |
| * under the License. |
| */ |
| package org.apache.usergrid.corepersistence.asyncevents.direct; |
| |
| import org.apache.usergrid.corepersistence.asyncevents.AsyncEventServiceImpl; |
| import org.apache.usergrid.corepersistence.asyncevents.EventBuilder; |
| import org.apache.usergrid.corepersistence.asyncevents.model.ElasticsearchIndexEvent; |
| import org.apache.usergrid.corepersistence.index.IndexLocationStrategyFactory; |
| import org.apache.usergrid.corepersistence.index.IndexProcessorFig; |
| import org.apache.usergrid.corepersistence.index.IndexingStrategy; |
| import org.apache.usergrid.persistence.collection.EntityCollectionManagerFactory; |
| import org.apache.usergrid.persistence.core.metrics.MetricsFactory; |
| import org.apache.usergrid.persistence.core.rx.RxTaskScheduler; |
| import org.apache.usergrid.persistence.index.EntityIndexFactory; |
| import org.apache.usergrid.persistence.index.impl.IndexOperationMessage; |
| import org.apache.usergrid.persistence.index.impl.IndexProducer; |
| import org.apache.usergrid.persistence.map.MapManagerFactory; |
| import org.apache.usergrid.persistence.queue.LegacyQueueFig; |
| import org.apache.usergrid.persistence.queue.LegacyQueueManagerFactory; |
| import org.apache.usergrid.persistence.queue.LegacyQueueMessage; |
| import org.slf4j.Logger; |
| import org.slf4j.LoggerFactory; |
| |
| import java.io.Serializable; |
| import java.util.ArrayList; |
| import java.util.List; |
| import java.util.UUID; |
| import java.util.stream.Collectors; |
| |
| /** |
| * Implementation of the AsyncEventService that writes first directly to ES |
| * and then submits to ASW as a backup. |
| * |
| * Created by peterajohnson on 8/29/17. |
| */ |
| public class DirectFirstEventServiceImpl extends AsyncEventServiceImpl { |
| |
| |
| private static final Logger logger = LoggerFactory.getLogger(DirectFirstEventServiceImpl.class); |
| |
| private IndexingStrategy configIndexingStrategy = IndexingStrategy.ASYNC; |
| |
| private BufferedQueue<Serializable> bufferedBatchQueue = new BufferedQueueNOP<>(); |
| |
| public DirectFirstEventServiceImpl(LegacyQueueManagerFactory queueManagerFactory, IndexProcessorFig indexProcessorFig, IndexProducer indexProducer, MetricsFactory metricsFactory, EntityCollectionManagerFactory entityCollectionManagerFactory, IndexLocationStrategyFactory indexLocationStrategyFactory, EntityIndexFactory entityIndexFactory, EventBuilder eventBuilder, MapManagerFactory mapManagerFactory, LegacyQueueFig queueFig, RxTaskScheduler rxTaskScheduler) { |
| super(queueManagerFactory, indexProcessorFig, indexProducer, metricsFactory, entityCollectionManagerFactory, indexLocationStrategyFactory, entityIndexFactory, eventBuilder, mapManagerFactory, queueFig, rxTaskScheduler); |
| |
| //bufferedBatchQueue = new BufferedQueueImpl<>(5000, 100, TimeUnit.MILLISECONDS); |
| bufferedBatchQueue.setConsumer((c) -> { dispatchToES(c); }); |
| |
| configIndexingStrategy = IndexingStrategy.get(queueFig.getQueueStrategy()); |
| |
| } |
| |
| protected void dispatchToES(final List<Serializable> bodies) { |
| |
| List<LegacyQueueMessage> messages = new ArrayList<>(); |
| for (Serializable body : bodies) { |
| String uuid = UUID.randomUUID().toString(); |
| LegacyQueueMessage message = new LegacyQueueMessage(uuid, "handle_" + uuid, body, "put type here"); |
| messages.add(message); |
| } |
| |
| List<IndexEventResult> result = callEventHandlers(messages); |
| |
| // failed to dispatch send to SQS |
| try { |
| List<LegacyQueueMessage> indexedMessages = submitToIndex(result, false); |
| } catch (Exception e) { |
| e.printStackTrace(); |
| for (Serializable body : bodies) { |
| super.offer(body); |
| } |
| } |
| |
| |
| } |
| |
| /** |
| * Offer the EntityIdScope to SQS |
| * |
| * The body will be an implementation of one of the following: |
| * EntityIndexEvent |
| * EntityDeleteEvent |
| * EdgeIndexEvent |
| * EdgeDeleteEvent |
| */ |
| protected void offer(final Serializable body) { |
| List<LegacyQueueMessage> messages = getMessageArray(body); |
| List<IndexEventResult> result = callEventHandlers(messages); |
| submitToIndex( result, false ); |
| super.offer(body); |
| } |
| |
| private List<LegacyQueueMessage> getMessageArray(final Serializable body) { |
| String uuid = UUID.randomUUID().toString(); |
| |
| LegacyQueueMessage message = new LegacyQueueMessage(uuid, "handle_" + uuid, body, "put type here"); |
| |
| if (logger.isDebugEnabled()) { |
| logger.debug("Sync Handler called for body of class {} ", body.getClass().getSimpleName()); |
| } |
| |
| List<LegacyQueueMessage> messages = new ArrayList<>(); |
| messages.add(message); |
| return messages; |
| } |
| |
| |
| protected void offer(final Serializable operation, IndexingStrategy indexingStrategy) { |
| if (shouldSendToDirectToES(indexingStrategy)) { |
| List<LegacyQueueMessage> messages = getMessageArray(operation); |
| List<IndexEventResult> result = callEventHandlers(messages); |
| submitToIndex( result, false ); |
| } |
| |
| // only if single region. |
| if (shouldSendToAWS(indexingStrategy)) { |
| super.offer(operation, indexingStrategy); |
| } |
| } |
| |
| |
| protected List<LegacyQueueMessage> submitToIndex(List<IndexEventResult> indexEventResults, boolean forUtilityQueue) { |
| |
| // if nothing came back then return empty list |
| if(indexEventResults==null){ |
| return new ArrayList<>(0); |
| } |
| |
| IndexOperationMessage combined = new IndexOperationMessage(); |
| List<LegacyQueueMessage> queueMessages = indexEventResults.stream() |
| |
| // filter out messages that are not present, they were not processed and put into the results |
| .filter( result -> result.getQueueMessage().isPresent() ) |
| .map(indexEventResult -> { |
| |
| //record the cycle time |
| getMessageCycye().update(System.currentTimeMillis() - indexEventResult.getCreationTime()); |
| |
| // ingest each index op into our combined, single index op for the index producer |
| if(indexEventResult.getIndexOperationMessage().isPresent()){ |
| combined.ingest(indexEventResult.getIndexOperationMessage().get()); |
| } |
| |
| return indexEventResult.getQueueMessage().get(); |
| }) |
| // collect into a list of QueueMessages that can be ack'd later |
| .collect(Collectors.toList()); |
| |
| |
| // dispatch to ES |
| ElasticsearchIndexEvent elasticsearchIndexEvent = getIndexOperationMessage(combined); |
| handleIndexOperation(elasticsearchIndexEvent); |
| return queueMessages; |
| } |
| |
| private boolean shouldSendToDirectToES(IndexingStrategy indexingStrategy) { |
| if (indexingStrategy == IndexingStrategy.DEFAULT) { |
| indexingStrategy = configIndexingStrategy; |
| } |
| return (indexingStrategy == IndexingStrategy.DIRECT || indexingStrategy == IndexingStrategy.DIRECTONLY); |
| } |
| |
| private boolean shouldSendToAWS(IndexingStrategy indexingStrategy) { |
| if (indexingStrategy == IndexingStrategy.DEFAULT) { |
| indexingStrategy = configIndexingStrategy; |
| } |
| // and is in same region. |
| return (indexingStrategy != IndexingStrategy.DIRECTONLY); |
| } |
| } |