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.automation.BlobConsumers.DEFAULT_LOG_CONFIG;
022
023import java.time.Duration;
024import java.util.concurrent.ExecutionException;
025import java.util.concurrent.TimeUnit;
026
027import org.apache.commons.logging.Log;
028import org.apache.commons.logging.LogFactory;
029import org.nuxeo.ecm.automation.OperationContext;
030import org.nuxeo.ecm.automation.OperationException;
031import org.nuxeo.ecm.automation.core.Constants;
032import org.nuxeo.ecm.automation.core.annotations.Context;
033import org.nuxeo.ecm.automation.core.annotations.Operation;
034import org.nuxeo.ecm.automation.core.annotations.OperationMethod;
035import org.nuxeo.ecm.automation.core.annotations.Param;
036import org.nuxeo.importer.stream.consumer.RedisDocumentMessageConsumerFactory;
037import org.nuxeo.importer.stream.message.DocumentMessage;
038import org.nuxeo.lib.stream.log.LogManager;
039import org.nuxeo.lib.stream.pattern.consumer.BatchPolicy;
040import org.nuxeo.lib.stream.pattern.consumer.ConsumerPolicy;
041import org.nuxeo.lib.stream.pattern.consumer.ConsumerPool;
042import org.nuxeo.runtime.api.Framework;
043import org.nuxeo.runtime.stream.StreamService;
044
045import net.jodah.failsafe.RetryPolicy;
046
047/**
048 * Import document message into Redis, so they can be used by Gatling simulation to create Nuxeo documents.
049 *
050 * @since 10.2
051 */
052@Operation(id = RedisDocumentConsumers.ID, category = Constants.CAT_SERVICES, label = "Imports document into Redis", since = "10.1", description = "Import documents into Redis.")
053public class RedisDocumentConsumers {
054    private static final Log log = LogFactory.getLog(RedisDocumentConsumers.class);
055
056    public static final String ID = "StreamImporter.runRedisDocumentConsumers";
057
058    @Context
059    protected OperationContext ctx;
060
061    @Param(name = "nbThreads", required = false)
062    protected Integer nbThreads;
063
064    @Param(name = "redisPrefix", required = false)
065    protected String redisPrefix;
066
067    @Param(name = "retryMax", required = false)
068    protected Integer retryMax = 3;
069
070    @Param(name = "retryDelayS", required = false)
071    protected Integer retryDelayS = 2;
072
073    @Param(name = "logName", required = false)
074    protected String logName;
075
076    @Param(name = "logConfig", required = false)
077    protected String logConfig;
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", getLogName(),
094                consumerPolicy));
095        StreamService service = Framework.getService(StreamService.class);
096        LogManager manager = service.getLogManager(getLogConfig());
097        try (ConsumerPool<DocumentMessage> consumers = new ConsumerPool<>(getLogName(), manager,
098                new RedisDocumentMessageConsumerFactory(redisPrefix), consumerPolicy)) {
099            consumers.start().get();
100        } catch (InterruptedException e) {
101            Thread.currentThread().interrupt();
102            log.warn("Operation interrupted");
103            throw new RuntimeException(e);
104        } catch (ExecutionException e) {
105            log.error("fail", e);
106            throw new OperationException(e);
107        }
108    }
109
110    protected short getNbThreads() {
111        if (nbThreads != null) {
112            return nbThreads.shortValue();
113        }
114        return 0;
115    }
116
117    protected String getLogName() {
118        if (logName != null) {
119            return logName;
120        }
121        return RandomDocumentProducers.DEFAULT_DOC_LOG_NAME;
122    }
123
124    protected String getLogConfig() {
125        if (logConfig != null) {
126            return logConfig;
127        }
128        return DEFAULT_LOG_CONFIG;
129    }
130}