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