001/*
002 * (C) Copyright 2015 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 *     Benoit Delbosc
018 */
019package org.nuxeo.importer.stream.automation;
020
021import static org.nuxeo.importer.stream.automation.BlobConsumers.DEFAULT_LOG_CONFIG;
022
023import java.util.concurrent.ExecutionException;
024
025import org.apache.commons.logging.Log;
026import org.apache.commons.logging.LogFactory;
027import org.nuxeo.ecm.automation.OperationContext;
028import org.nuxeo.ecm.automation.OperationException;
029import org.nuxeo.ecm.automation.core.Constants;
030import org.nuxeo.ecm.automation.core.annotations.Context;
031import org.nuxeo.ecm.automation.core.annotations.Operation;
032import org.nuxeo.ecm.automation.core.annotations.OperationMethod;
033import org.nuxeo.ecm.automation.core.annotations.Param;
034import org.nuxeo.importer.stream.message.DocumentMessage;
035import org.nuxeo.importer.stream.producer.RandomDocumentMessageProducerFactory;
036import org.nuxeo.lib.stream.log.LogManager;
037import org.nuxeo.lib.stream.pattern.producer.ProducerPool;
038import org.nuxeo.runtime.api.Framework;
039import org.nuxeo.runtime.stream.StreamService;
040
041/**
042 * @since 9.1
043 */
044@Operation(id = RandomDocumentProducers.ID, category = Constants.CAT_SERVICES, label = "Produces random blobs", since = "9.1", description = "Produces random blobs in a Log.")
045public class RandomDocumentProducers {
046    private static final Log log = LogFactory.getLog(RandomDocumentProducers.class);
047
048    public static final String ID = "StreamImporter.runRandomDocumentProducers";
049
050    public static final String DEFAULT_DOC_LOG_NAME = "import-doc";
051
052    @Context
053    protected OperationContext ctx;
054
055    @Param(name = "nbDocuments")
056    protected Integer nbDocuments;
057
058    @Param(name = "nbThreads", required = false)
059    protected Integer nbThreads = 8;
060
061    @Param(name = "avgBlobSizeKB", required = false)
062    protected Integer avgBlobSizeKB = 1;
063
064    @Param(name = "lang", required = false)
065    protected String lang = "en_US";
066
067    @Param(name = "logName", required = false)
068    protected String logName;
069
070    @Param(name = "logSize", required = false)
071    protected Integer logSize;
072
073    @Param(name = "logBlobInfo", required = false)
074    protected String logBlobInfoName;
075
076    @Param(name = "logConfig", required = false)
077    protected String logConfig;
078
079    @Param(name = "countFolderAsDocument", required = false)
080    protected Boolean countFolderAsDocument = true;
081
082    @OperationMethod
083    public void run() throws OperationException {
084        RandomBlobProducers.checkAccess(ctx);
085        StreamService service = Framework.getService(StreamService.class);
086        LogManager manager = service.getLogManager(getLogConfig());
087        manager.createIfNotExists(getLogName(), getLogSize());
088        RandomDocumentMessageProducerFactory factory;
089        if (logBlobInfoName != null) {
090            factory = new RandomDocumentMessageProducerFactory(nbDocuments, lang, manager, logBlobInfoName,
091                    countFolderAsDocument);
092        } else {
093            factory = new RandomDocumentMessageProducerFactory(nbDocuments, lang, avgBlobSizeKB, countFolderAsDocument);
094        }
095        try (ProducerPool<DocumentMessage> producers = new ProducerPool<>(getLogName(), manager, factory,
096                nbThreads.shortValue())) {
097            producers.start().get();
098        } catch (InterruptedException e) {
099            Thread.currentThread().interrupt();
100            log.warn("Operation interrupted");
101            throw new RuntimeException(e);
102        } catch (ExecutionException e) {
103            log.error("Operation fails", e);
104            throw new OperationException(e);
105        }
106    }
107
108    protected String getLogConfig() {
109        if (logConfig != null) {
110            return logConfig;
111        }
112        return DEFAULT_LOG_CONFIG;
113    }
114
115    protected String getLogName() {
116        if (logName != null) {
117            return logName;
118        }
119        return DEFAULT_DOC_LOG_NAME;
120    }
121
122    protected int getLogSize() {
123        if (logSize != null && logSize > 0) {
124            return logSize;
125        }
126        return nbThreads;
127    }
128
129}