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_BLOB_NAME; 022import static org.nuxeo.importer.stream.StreamImporters.DEFAULT_LOG_CONFIG; 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.ecm.core.api.NuxeoPrincipal; 036import org.nuxeo.importer.stream.StreamImporters; 037import org.nuxeo.importer.stream.message.BlobMessage; 038import org.nuxeo.importer.stream.producer.RandomStringBlobMessageProducerFactory; 039import org.nuxeo.lib.stream.codec.Codec; 040import org.nuxeo.lib.stream.log.LogManager; 041import org.nuxeo.lib.stream.log.Name; 042import org.nuxeo.lib.stream.pattern.producer.ProducerPool; 043import org.nuxeo.runtime.api.Framework; 044import org.nuxeo.runtime.stream.StreamService; 045 046/** 047 * @since 9.1 048 */ 049@Operation(id = RandomBlobProducers.ID, category = Constants.CAT_SERVICES, label = "Produces random blobs", since = "9.1", description = "Produces random blobs in a Log.") 050public class RandomBlobProducers { 051 private static final Log log = LogFactory.getLog(RandomBlobProducers.class); 052 053 public static final String ID = "StreamImporter.runRandomBlobProducers"; 054 055 @Context 056 protected OperationContext ctx; 057 058 @Param(name = "nbBlobs") 059 protected Integer nbBlobs; 060 061 @Param(name = "nbThreads", required = false) 062 protected Integer nbThreads = 8; 063 064 @Param(name = "avgBlobSizeKB", required = false) 065 protected Integer avgBlobSizeKB = 1; 066 067 @Param(name = "lang", required = false) 068 protected String lang = "en_US"; 069 070 @Param(name = "logName", required = false) 071 protected String logName = DEFAULT_LOG_BLOB_NAME; 072 073 @Param(name = "logSize", required = false) 074 protected Integer logSize; 075 076 @Param(name = "blobMarker", required = false) 077 protected String blobMarker; 078 079 @OperationMethod 080 public void run() throws OperationException { 081 checkAccess(ctx); 082 LogManager manager = Framework.getService(StreamService.class).getLogManager(); 083 manager.createIfNotExists(Name.ofUrn(logName), getLogSize()); 084 Codec<BlobMessage> codec = StreamImporters.getBlobCodec(); 085 try (ProducerPool<BlobMessage> producers = new ProducerPool<>(logName, manager, codec, 086 new RandomStringBlobMessageProducerFactory(nbBlobs, lang, avgBlobSizeKB, blobMarker), 087 nbThreads.shortValue())) { 088 producers.start().get(); 089 } catch (InterruptedException e) { 090 Thread.currentThread().interrupt(); 091 log.warn("Operation interrupted"); 092 throw new RuntimeException(e); 093 } catch (ExecutionException e) { 094 log.error("fail", e); 095 throw new OperationException(e); 096 } 097 } 098 099 protected int getLogSize() { 100 if (logSize != null && logSize > 0) { 101 return logSize; 102 } 103 return nbThreads; 104 } 105 106 protected static void checkAccess(OperationContext context) { 107 NuxeoPrincipal principal = context.getPrincipal(); 108 if (principal == null || !principal.isAdministrator()) { 109 throw new RuntimeException("Unauthorized access: " + principal); 110 } 111 } 112 113}