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