001/*
002 * (C) Copyright 2018 Nuxeo (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 *     Benoit Delbosc
018 */
019package org.nuxeo.importer.stream.automation;
020
021import static org.nuxeo.importer.stream.StreamImporters.DEFAULT_LOG_CONFIG;
022import static org.nuxeo.importer.stream.StreamImporters.DEFAULT_LOG_DOC_NAME;
023
024import java.time.Duration;
025import java.util.concurrent.ExecutionException;
026import java.util.concurrent.TimeUnit;
027
028import org.apache.commons.logging.Log;
029import org.apache.commons.logging.LogFactory;
030import org.nuxeo.ecm.automation.OperationContext;
031import org.nuxeo.ecm.automation.OperationException;
032import org.nuxeo.ecm.automation.core.Constants;
033import org.nuxeo.ecm.automation.core.annotations.Context;
034import org.nuxeo.ecm.automation.core.annotations.Operation;
035import org.nuxeo.ecm.automation.core.annotations.OperationMethod;
036import org.nuxeo.ecm.automation.core.annotations.Param;
037import org.nuxeo.importer.stream.StreamImporters;
038import org.nuxeo.importer.stream.consumer.RedisDocumentMessageConsumerFactory;
039import org.nuxeo.importer.stream.message.DocumentMessage;
040import org.nuxeo.lib.stream.codec.Codec;
041import org.nuxeo.lib.stream.log.LogManager;
042import org.nuxeo.lib.stream.pattern.consumer.BatchPolicy;
043import org.nuxeo.lib.stream.pattern.consumer.ConsumerPolicy;
044import org.nuxeo.lib.stream.pattern.consumer.ConsumerPool;
045import org.nuxeo.runtime.api.Framework;
046import org.nuxeo.runtime.stream.StreamService;
047
048import net.jodah.failsafe.RetryPolicy;
049
050/**
051 * Import document message into Redis, so they can be used by Gatling simulation to create Nuxeo documents.
052 *
053 * @since 10.2
054 */
055@Operation(id = RedisDocumentConsumers.ID, category = Constants.CAT_SERVICES, label = "Imports document into Redis", since = "10.1", description = "Import documents into Redis.")
056public class RedisDocumentConsumers {
057    private static final Log log = LogFactory.getLog(RedisDocumentConsumers.class);
058
059    public static final String ID = "StreamImporter.runRedisDocumentConsumers";
060
061    @Context
062    protected OperationContext ctx;
063
064    @Param(name = "nbThreads", required = false)
065    protected Integer nbThreads;
066
067    @Param(name = "redisPrefix", required = false)
068    protected String redisPrefix;
069
070    @Param(name = "retryMax", required = false)
071    protected Integer retryMax = 3;
072
073    @Param(name = "retryDelayS", required = false)
074    protected Integer retryDelayS = 2;
075
076    @Param(name = "logName", required = false)
077    protected String logName = DEFAULT_LOG_DOC_NAME;
078
079    @Param(name = "waitMessageTimeoutSeconds", required = false)
080    protected Integer waitMessageTimeoutSeconds = 20;
081
082    @OperationMethod
083    public void run() throws OperationException {
084        RandomBlobProducers.checkAccess(ctx);
085        ConsumerPolicy consumerPolicy = ConsumerPolicy.builder()
086                                                      .name(ID)
087                                                      .batchPolicy(BatchPolicy.NO_BATCH)
088                                                      .retryPolicy(new RetryPolicy().withMaxRetries(retryMax).withDelay(
089                                                              retryDelayS, TimeUnit.SECONDS))
090                                                      .maxThreads(getNbThreads())
091                                                      .waitMessageTimeout(Duration.ofSeconds(waitMessageTimeoutSeconds))
092                                                      .build();
093        log.warn(String.format("Import documents into Redis from log: %s, with policy: %s", logName, consumerPolicy));
094        LogManager manager = Framework.getService(StreamService.class).getLogManager();
095        Codec<DocumentMessage> codec = StreamImporters.getDocCodec();
096        try (ConsumerPool<DocumentMessage> consumers = new ConsumerPool<>(logName, manager, codec,
097                new RedisDocumentMessageConsumerFactory(redisPrefix), consumerPolicy)) {
098            consumers.start().get();
099        } catch (InterruptedException e) {
100            Thread.currentThread().interrupt();
101            log.warn("Operation interrupted");
102            throw new RuntimeException(e);
103        } catch (ExecutionException e) {
104            log.error("fail", e);
105            throw new OperationException(e);
106        }
107    }
108
109    protected short getNbThreads() {
110        if (nbThreads != null) {
111            return nbThreads.shortValue();
112        }
113        return 0;
114    }
115}