Search in sources :

Example 1 with EngineConfiguration

use of io.aklivity.zilla.runtime.engine.EngineConfiguration in project zilla by aklivity.

the class ZillaStartCommand method run.

@Override
public void run() {
    Runtime runtime = getRuntime();
    Properties props = new Properties();
    props.setProperty(ENGINE_DIRECTORY.name(), ".zilla/engine");
    Path path = Paths.get(properties != null ? properties : OPTION_PROPERTIES_DEFAULT);
    if (Files.exists(path) || properties != null) {
        try {
            props.load(Files.newInputStream(path));
        } catch (IOException ex) {
            System.out.println("Failed to load properties: " + path);
            rethrowUnchecked(ex);
        }
    }
    if (workers >= 0) {
        props.setProperty(ENGINE_WORKERS.name(), Integer.toString(workers));
    }
    if (verbose) {
        props.setProperty(ENGINE_VERBOSE.name(), Boolean.toString(verbose));
    }
    EngineConfiguration config = new EngineConfiguration(props);
    Consumer<Throwable> report = exceptions ? e -> e.printStackTrace(System.err) : e -> System.err.println(e.getMessage());
    try (Engine engine = Engine.builder().config(config).configURL(configURL.toURL()).errorHandler(this::onError).build()) {
        engine.start().get();
        System.out.println("started");
        runtime.addShutdownHook(new Thread(this::onShutdown));
        stop.await();
        errors.forEach(report);
        System.out.println("stopped");
        stopped.countDown();
    } catch (Throwable ex) {
        System.out.println("error");
        rethrowUnchecked(ex);
    }
}
Also used : Path(java.nio.file.Path) Runtime.getRuntime(java.lang.Runtime.getRuntime) EngineConfiguration(io.aklivity.zilla.runtime.engine.EngineConfiguration) IOException(java.io.IOException) Properties(java.util.Properties) Engine(io.aklivity.zilla.runtime.engine.Engine)

Example 2 with EngineConfiguration

use of io.aklivity.zilla.runtime.engine.EngineConfiguration in project zilla by aklivity.

the class EngineRule method apply.

@Override
public Statement apply(Statement base, Description description) {
    Class<?> testClass = description.getTestClass();
    final String testMethod = description.getMethodName().replaceAll("\\[.*\\]", "");
    try {
        Configure[] configures = testClass.getDeclaredMethod(testMethod).getAnnotationsByType(Configure.class);
        Arrays.stream(configures).forEach(p -> properties.setProperty(p.name(), p.value()));
        Configuration config = description.getAnnotation(Configuration.class);
        if (config != null) {
            if (configurationRoot != null) {
                String resourceName = String.format("%s/%s", configurationRoot, config.value());
                configURL = testClass.getClassLoader().getResource(resourceName);
            } else {
                String resourceName = String.format("%s-%s", testClass.getSimpleName(), config.value());
                configURL = testClass.getResource(resourceName);
            }
        }
        cleanup();
    } catch (Exception e) {
        LangUtil.rethrowUnchecked(e);
    }
    return new Statement() {

        @Override
        public void evaluate() throws Throwable {
            EngineConfiguration config = configuration();
            final Thread baseThread = Thread.currentThread();
            final List<Throwable> errors = new ArrayList<>();
            final ErrorHandler errorHandler = ex -> {
                errors.add(ex);
                baseThread.interrupt();
            };
            engine = builder.config(config).configURL(configURL).errorHandler(errorHandler).build();
            try {
                engine.start().get();
                base.evaluate();
            } catch (Throwable t) {
                errors.add(t);
            } finally {
                try {
                    engine.close();
                } catch (Throwable t) {
                    errors.add(t);
                } finally {
                    assertEmpty(errors);
                }
            }
        }
    };
}
Also used : ENGINE_DRAIN_ON_CLOSE(io.aklivity.zilla.runtime.engine.EngineConfiguration.ENGINE_DRAIN_ON_CLOSE) EngineBuilder(io.aklivity.zilla.runtime.engine.EngineBuilder) ENGINE_STREAMS_BUFFER_CAPACITY(io.aklivity.zilla.runtime.engine.EngineConfiguration.ENGINE_STREAMS_BUFFER_CAPACITY) ENGINE_WORKERS(io.aklivity.zilla.runtime.engine.EngineConfiguration.ENGINE_WORKERS) Statement(org.junit.runners.model.Statement) Arrays(java.util.Arrays) ErrorHandler(org.agrona.ErrorHandler) PropertyDef(io.aklivity.zilla.runtime.engine.Configuration.PropertyDef) Configure(io.aklivity.zilla.runtime.engine.test.annotation.Configure) TestRule(org.junit.rules.TestRule) URL(java.net.URL) Engine(io.aklivity.zilla.runtime.engine.Engine) EngineConfiguration(io.aklivity.zilla.runtime.engine.EngineConfiguration) ArrayList(java.util.ArrayList) ENGINE_SYNTHETIC_ABORT(io.aklivity.zilla.runtime.engine.EngineConfiguration.ENGINE_SYNTHETIC_ABORT) Objects.requireNonNull(java.util.Objects.requireNonNull) MultipleFailureException.assertEmpty(org.junit.runners.model.MultipleFailureException.assertEmpty) LangUtil(org.agrona.LangUtil) EngineStats(io.aklivity.zilla.runtime.engine.EngineStats) Path(java.nio.file.Path) ENGINE_RESPONSE_BUFFER_CAPACITY(io.aklivity.zilla.runtime.engine.EngineConfiguration.ENGINE_RESPONSE_BUFFER_CAPACITY) ENGINE_ROUTED_DELAY_MILLIS(io.aklivity.zilla.runtime.engine.EngineConfiguration.ENGINE_ROUTED_DELAY_MILLIS) ENGINE_COMMAND_BUFFER_CAPACITY(io.aklivity.zilla.runtime.engine.EngineConfiguration.ENGINE_COMMAND_BUFFER_CAPACITY) Properties(java.util.Properties) Files(java.nio.file.Files) Binding(io.aklivity.zilla.runtime.engine.binding.Binding) FOLLOW_LINKS(java.nio.file.FileVisitOption.FOLLOW_LINKS) IOException(java.io.IOException) Description(org.junit.runner.Description) Configuration(io.aklivity.zilla.runtime.engine.test.annotation.Configuration) ENGINE_COUNTERS_BUFFER_CAPACITY(io.aklivity.zilla.runtime.engine.EngineConfiguration.ENGINE_COUNTERS_BUFFER_CAPACITY) File(java.io.File) List(java.util.List) Files.exists(java.nio.file.Files.exists) Pattern(java.util.regex.Pattern) ENGINE_DIRECTORY(io.aklivity.zilla.runtime.engine.EngineConfiguration.ENGINE_DIRECTORY) ErrorHandler(org.agrona.ErrorHandler) EngineConfiguration(io.aklivity.zilla.runtime.engine.EngineConfiguration) Configuration(io.aklivity.zilla.runtime.engine.test.annotation.Configuration) Statement(org.junit.runners.model.Statement) ArrayList(java.util.ArrayList) IOException(java.io.IOException) EngineConfiguration(io.aklivity.zilla.runtime.engine.EngineConfiguration) Configure(io.aklivity.zilla.runtime.engine.test.annotation.Configure)

Example 3 with EngineConfiguration

use of io.aklivity.zilla.runtime.engine.EngineConfiguration in project zilla by aklivity.

the class EngineTest method initConfig.

@Before
public void initConfig() {
    Properties properties = new Properties();
    properties.put(ENGINE_DIRECTORY.name(), "target/zilla-itests");
    properties.put(ENGINE_WORKERS.name(), "1");
    config = new EngineConfiguration(properties);
}
Also used : EngineConfiguration(io.aklivity.zilla.runtime.engine.EngineConfiguration) Properties(java.util.Properties) Before(org.junit.Before)

Example 4 with EngineConfiguration

use of io.aklivity.zilla.runtime.engine.EngineConfiguration in project zilla by aklivity.

the class LogCommand method main.

public static void main(String[] args) throws Exception {
    final CommandLineParser parser = new DefaultParser();
    Options options = new Options();
    options.addOption(builder("h").longOpt("help").desc("print this message").build());
    options.addOption(builder().longOpt("version").build());
    options.addOption(builder("t").hasArg().required(false).longOpt("type").desc("streams* | streams-[nowait|zero|head|tail] | counters | queues | routes").build());
    options.addOption(builder("f").hasArgs().required(false).longOpt("frameTypes").desc("log specific frame types only, e.g BEGIN").build());
    options.addOption(builder("d").longOpt("directory").hasArg().desc("configuration directory").build());
    options.addOption(builder("v").longOpt("verbose").desc("verbose").build());
    options.addOption(builder("e").hasArgs().required(false).longOpt("extensionTypes").desc("log specific extension types only, e.g. tcp").build());
    options.addOption(builder("i").hasArg().longOpt("interval").desc("run command continuously at interval").build());
    options.addOption(builder("s").longOpt("separator").desc("include thousands separator in integer values").build());
    options.addOption(builder("a").hasArg().longOpt("affinity").desc("affinity mask").build());
    final CommandLine cmdline = parser.parse(options, args);
    final Logger out = System.out::printf;
    final boolean hasVersion = cmdline.hasOption("version");
    final boolean hasDirectory = cmdline.hasOption("directory");
    final boolean hasHelp = cmdline.hasOption("help");
    if (hasVersion) {
        out.printf("version: %s\n", LogCommand.class.getPackage().getSpecificationVersion());
    }
    if (hasHelp || !hasDirectory) {
        if (hasHelp || !hasVersion) {
            HelpFormatter formatter = new HelpFormatter();
            formatter.printHelp("log", options);
        }
    } else {
        String directory = cmdline.getOptionValue("directory");
        boolean verbose = cmdline.hasOption("verbose");
        boolean separator = cmdline.hasOption("separator");
        String type = cmdline.getOptionValue("type", "streams");
        final int interval = Integer.parseInt(cmdline.getOptionValue("interval", "0"));
        final long affinity = Long.parseLong(cmdline.getOptionValue("affinity", Long.toString(0xffff_ffff_ffff_ffffL)));
        Properties properties = new Properties();
        properties.setProperty(ENGINE_DIRECTORY.name(), directory);
        final EngineConfiguration config = new EngineConfiguration(new Configuration(), properties);
        Runnable command = null;
        final Matcher matcher = Pattern.compile("streams(?:-(?<option>[a-z]+))?").matcher(type);
        if (matcher.matches()) {
            final String option = matcher.group("option");
            final boolean continuous = !"nowait".equals(option);
            final SpyPosition position = continuous && option != null ? SpyPosition.valueOf(option.toUpperCase()) : SpyPosition.ZERO;
            final String[] frameTypes = cmdline.getOptionValues("frameTypes");
            final String[] extensionTypes = cmdline.getOptionValues("extensionTypes");
            final Predicate<String> hasExtensionType = extensionTypes == null && !verbose ? t -> false : s -> verbose || Arrays.asList(extensionTypes).contains(s);
            final Predicate<String> hasFrameTypes = frameTypes == null ? t -> true : Arrays.asList(frameTypes)::contains;
            command = new LogStreamsCommand(config, out, hasFrameTypes, hasExtensionType, verbose, continuous, affinity, position);
        } else if ("buffers".equals(type)) {
            command = new LogBuffersCommand(config, out, verbose);
        } else if ("budgets".equals(type)) {
            command = new LogBudgetsCommand(config, out, verbose, affinity);
        } else if ("counters".equals(type)) {
            command = new LogCountersCommand(config, out, verbose, separator);
        } else if ("queues".equals(type)) {
            command = new LogQueueDepthCommand(config, out, verbose, separator);
        }
        do {
            command.run();
            Thread.sleep(TimeUnit.SECONDS.toMillis(interval));
        } while (interval > 0);
    }
}
Also used : Arrays(java.util.Arrays) Properties(java.util.Properties) SpyPosition(io.aklivity.zilla.runtime.command.log.internal.spy.RingBufferSpy.SpyPosition) CommandLineParser(org.apache.commons.cli.CommandLineParser) Predicate(java.util.function.Predicate) Options(org.apache.commons.cli.Options) EngineConfiguration(io.aklivity.zilla.runtime.engine.EngineConfiguration) HelpFormatter(org.apache.commons.cli.HelpFormatter) DefaultParser(org.apache.commons.cli.DefaultParser) TimeUnit(java.util.concurrent.TimeUnit) Matcher(java.util.regex.Matcher) Configuration(io.aklivity.zilla.runtime.engine.Configuration) CommandLine(org.apache.commons.cli.CommandLine) Pattern(java.util.regex.Pattern) ENGINE_DIRECTORY(io.aklivity.zilla.runtime.engine.EngineConfiguration.ENGINE_DIRECTORY) Option.builder(org.apache.commons.cli.Option.builder) Options(org.apache.commons.cli.Options) EngineConfiguration(io.aklivity.zilla.runtime.engine.EngineConfiguration) Configuration(io.aklivity.zilla.runtime.engine.Configuration) Matcher(java.util.regex.Matcher) Properties(java.util.Properties) HelpFormatter(org.apache.commons.cli.HelpFormatter) EngineConfiguration(io.aklivity.zilla.runtime.engine.EngineConfiguration) CommandLineParser(org.apache.commons.cli.CommandLineParser) DefaultParser(org.apache.commons.cli.DefaultParser) SpyPosition(io.aklivity.zilla.runtime.command.log.internal.spy.RingBufferSpy.SpyPosition) CommandLine(org.apache.commons.cli.CommandLine)

Example 5 with EngineConfiguration

use of io.aklivity.zilla.runtime.engine.EngineConfiguration in project zilla by aklivity.

the class EngineRule method cleanup.

private void cleanup() throws IOException {
    EngineConfiguration config = configuration();
    Path directory = config.directory();
    Path cacheDirectory = config.cacheDirectory();
    if (clean && exists(directory)) {
        Files.walk(directory, FOLLOW_LINKS).filter(this::shouldDeletePath).map(Path::toFile).forEach(File::delete);
    }
    if (clean && exists(cacheDirectory)) {
        Files.walk(cacheDirectory).map(Path::toFile).forEach(File::delete);
    }
}
Also used : Path(java.nio.file.Path) EngineConfiguration(io.aklivity.zilla.runtime.engine.EngineConfiguration) File(java.io.File)

Aggregations

EngineConfiguration (io.aklivity.zilla.runtime.engine.EngineConfiguration)5 Properties (java.util.Properties)4 Path (java.nio.file.Path)3 Engine (io.aklivity.zilla.runtime.engine.Engine)2 ENGINE_DIRECTORY (io.aklivity.zilla.runtime.engine.EngineConfiguration.ENGINE_DIRECTORY)2 File (java.io.File)2 IOException (java.io.IOException)2 Arrays (java.util.Arrays)2 Pattern (java.util.regex.Pattern)2 SpyPosition (io.aklivity.zilla.runtime.command.log.internal.spy.RingBufferSpy.SpyPosition)1 Configuration (io.aklivity.zilla.runtime.engine.Configuration)1 PropertyDef (io.aklivity.zilla.runtime.engine.Configuration.PropertyDef)1 EngineBuilder (io.aklivity.zilla.runtime.engine.EngineBuilder)1 ENGINE_COMMAND_BUFFER_CAPACITY (io.aklivity.zilla.runtime.engine.EngineConfiguration.ENGINE_COMMAND_BUFFER_CAPACITY)1 ENGINE_COUNTERS_BUFFER_CAPACITY (io.aklivity.zilla.runtime.engine.EngineConfiguration.ENGINE_COUNTERS_BUFFER_CAPACITY)1 ENGINE_DRAIN_ON_CLOSE (io.aklivity.zilla.runtime.engine.EngineConfiguration.ENGINE_DRAIN_ON_CLOSE)1 ENGINE_RESPONSE_BUFFER_CAPACITY (io.aklivity.zilla.runtime.engine.EngineConfiguration.ENGINE_RESPONSE_BUFFER_CAPACITY)1 ENGINE_ROUTED_DELAY_MILLIS (io.aklivity.zilla.runtime.engine.EngineConfiguration.ENGINE_ROUTED_DELAY_MILLIS)1 ENGINE_STREAMS_BUFFER_CAPACITY (io.aklivity.zilla.runtime.engine.EngineConfiguration.ENGINE_STREAMS_BUFFER_CAPACITY)1 ENGINE_SYNTHETIC_ABORT (io.aklivity.zilla.runtime.engine.EngineConfiguration.ENGINE_SYNTHETIC_ABORT)1