All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.emc.mongoose.run.scenario.step.LoadStep Maven / Gradle / Ivy

The newest version!
package com.emc.mongoose.run.scenario.step;

import com.github.akurilov.commons.system.SizeInBytes;
import com.emc.mongoose.api.common.exception.OmgShootMyFootException;
import com.emc.mongoose.load.controller.BasicLoadController;
import com.emc.mongoose.api.model.data.DataInput;
import com.github.akurilov.commons.io.Output;
import com.emc.mongoose.api.model.item.ItemFactory;
import com.emc.mongoose.api.model.item.ItemInfoFileOutput;
import com.emc.mongoose.api.model.item.ItemType;
import com.emc.mongoose.api.model.load.LoadGenerator;
import com.emc.mongoose.api.model.load.LoadController;
import com.emc.mongoose.api.model.storage.StorageDriver;
import com.emc.mongoose.load.generator.BasicLoadGeneratorBuilder;
import com.emc.mongoose.run.scenario.util.StorageDriverUtil;
import com.emc.mongoose.ui.config.Config;
import com.emc.mongoose.ui.config.item.ItemConfig;
import com.emc.mongoose.ui.config.item.data.DataConfig;
import com.emc.mongoose.ui.config.item.data.input.InputConfig;
import com.emc.mongoose.ui.config.item.data.input.layer.LayerConfig;
import com.emc.mongoose.ui.config.load.LoadConfig;
import com.emc.mongoose.ui.config.output.OutputConfig;
import com.emc.mongoose.ui.config.output.metrics.MetricsConfig;
import com.emc.mongoose.ui.config.output.metrics.average.AverageConfig;
import com.emc.mongoose.ui.config.storage.StorageConfig;
import com.emc.mongoose.ui.config.test.step.StepConfig;
import com.emc.mongoose.ui.config.test.step.limit.LimitConfig;
import com.emc.mongoose.ui.log.LogUtil;
import com.emc.mongoose.ui.log.Loggers;

import org.apache.logging.log4j.Level;

import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.rmi.RemoteException;
import java.util.ArrayList;
import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.concurrent.CancellationException;
import java.util.concurrent.TimeUnit;

/**
 Created by kurila on 02.02.16.
 */
public final class LoadStep
extends StepBase {
	
	private final boolean preconditionFlag;

	public LoadStep(final Config config) {
		this(config, Collections.EMPTY_MAP, false);
	}
	
	public LoadStep(
		final Config appConfig, final Map subTree, final boolean preconditionFlag
	) {
		super(appConfig);
		final Map nodeConfig = (Map) subTree.get(KEY_NODE_CONFIG);
		localConfig.apply(nodeConfig, "load-" + LogUtil.getDateTimeStamp() + "-" + hashCode());
		this.preconditionFlag = preconditionFlag;
	}
	
	@Override
	protected final void invoke()
	throws CancellationException {

		final StepConfig stepConfig = localConfig.getTestConfig().getStepConfig();
		final String stepId = stepConfig.getId();
		Loggers.MSG.info("Run the load step \"{}\"", stepId);
		final LoadConfig loadConfig = localConfig.getLoadConfig();
		final LimitConfig limitConfig = stepConfig.getLimitConfig();
		final OutputConfig outputConfig = localConfig.getOutputConfig();
		final MetricsConfig metricsConfig = outputConfig.getMetricsConfig();
		final AverageConfig avgMetricsConfig = metricsConfig.getAverageConfig();
		if(preconditionFlag) {
			avgMetricsConfig.setPersist(false);
			metricsConfig.getSummaryConfig().setPersist(false);
			metricsConfig.getSummaryConfig().setPerfDbResultsFile(false);
			metricsConfig.getTraceConfig().setPersist(false);
		}
		final ItemConfig itemConfig = localConfig.getItemConfig();
		final DataConfig dataConfig = itemConfig.getDataConfig();
		final InputConfig dataInputConfig = dataConfig.getInputConfig();
		final StorageConfig storageConfig = localConfig.getStorageConfig();
		final LayerConfig dataLayerConfig = dataInputConfig.getLayerConfig();
		
		final DataInput dataInput;
		try {
			dataInput = DataInput.getInstance(
				dataInputConfig.getFile(), dataInputConfig.getSeed(), dataLayerConfig.getSize(),
				dataLayerConfig.getCache()
			);
		} catch(final IOException e) {
			throw new RuntimeException(e);
		}
		
		final List drivers = new ArrayList<>();
		try {
			StorageDriverUtil.init(
				drivers, itemConfig, loadConfig, avgMetricsConfig, storageConfig, stepConfig,
				dataInput
			);
		} catch(final InterruptedException e) {
			throw new CancellationException();
		}

		final ItemType itemType = ItemType.valueOf(itemConfig.getType().toUpperCase());
		final ItemFactory itemFactory = ItemType.getItemFactory(itemType);
		Loggers.MSG.info("Work on the " + itemType.toString().toLowerCase() + " items");

		final LoadGenerator loadGenerator;
		try {
			loadGenerator = new BasicLoadGeneratorBuilder<>()
				.setItemConfig(itemConfig)
				.setLoadConfig(loadConfig)
				.setLimitConfig(limitConfig)
				.setItemType(itemType)
				.setItemFactory(itemFactory)
				.setStorageDrivers(drivers)
				.setAuthConfig(storageConfig.getAuthConfig())
				.build();
		} catch(final OmgShootMyFootException e) {
			throw new RuntimeException(e);
		}
		Loggers.MSG.info("Load generators initialized");

		final long timeLimitSec;
		long t = limitConfig.getTime();
		if(t > 0) {
			timeLimitSec = t;
		} else {
			timeLimitSec = Long.MAX_VALUE;
		}
		
		final Map> driversMap = new HashMap<>();
		driversMap.put(loadGenerator, drivers);
		final Map itemDataSizes = new HashMap<>();
		itemDataSizes.put(loadGenerator, dataConfig.getSize());
		final Map loadConfigMap = new HashMap<>();
		loadConfigMap.put(loadGenerator, loadConfig);
		final Map outputConfigMap = new HashMap<>();
		outputConfigMap.put(loadGenerator, outputConfig);
		try(
			final LoadController controller = new BasicLoadController(
				stepId, driversMap, null, itemDataSizes, loadConfigMap, stepConfig, outputConfigMap
			)
		) {
			final String itemOutputFile = itemConfig.getOutputConfig().getFile();
			if(itemOutputFile != null && itemOutputFile.length() > 0) {
				final Path itemOutputPath = Paths.get(itemOutputFile);
				if(Files.exists(itemOutputPath)) {
					Loggers.ERR.warn("Items output file \"{}\" already exists", itemOutputPath);
				}
				final Output itemOutput = new ItemInfoFileOutput<>(itemOutputPath);
				controller.setIoResultsOutput(itemOutput);
			}
			controller.start();
			Loggers.MSG.info("Load step \"{}\" started", stepId);
			if(controller.await(timeLimitSec, TimeUnit.SECONDS)) {
				Loggers.MSG.info("Load step \"{}\" done", stepId);
			} else {
				Loggers.MSG.info("Load step \"{}\" timeout", stepId);
			}
		} catch(final RemoteException e) {
			LogUtil.exception(Level.ERROR, e, "Unexpected failure");
		} catch(final IOException e) {
			LogUtil.exception(Level.WARN, e, "Failed to open the item output file");
		} catch(final InterruptedException e) {
			Loggers.MSG.debug("Load step \"{}\" interrupted", stepId);
			throw new CancellationException();
		}
	}
	
	@Override
	public final String toString() {
		return "singleLoadStepContainer#" + hashCode();
	}
	
	@Override
	public final void close()
	throws IOException {
	}
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy