Search in sources :

Example 1 with Pipeline

use of loghub.Pipeline in project LogHub by fbacchella.

the class TestFire method test.

@Test
public void test() throws ProcessorException, InterruptedException, ConfigException, IOException {
    Properties conf = Tools.loadConf("fire.conf");
    for (Pipeline pipe : conf.pipelines) {
        Assert.assertTrue("configuration failed", pipe.configure(conf));
    }
    Event sent = Tools.getEvent();
    sent.put("count", 2);
    Tools.runProcessing(sent, conf.namedPipeLine.get("main"), conf);
    Event old = conf.mainQueue.remove();
    Event newevent = conf.mainQueue.remove();
    Assert.assertEquals("Not matching old event", old.get("count"), 2);
    Assert.assertEquals("Event not fired", 6, newevent.get("c"));
}
Also used : Event(loghub.Event) Properties(loghub.configuration.Properties) Pipeline(loghub.Pipeline) Test(org.junit.Test)

Example 2 with Pipeline

use of loghub.Pipeline in project LogHub by fbacchella.

the class Configuration method parsePipeline.

private Pipeline parsePipeline(ConfigListener.PipenodesList desc, String currentPipeLineName, int depth, AtomicInteger subPipeCount) throws ConfigException {
    List<Processor> allSteps = new ArrayList<Processor>() {

        @Override
        public String toString() {
            StringBuilder buffer = new StringBuilder();
            buffer.append("PipeList(");
            for (Processor i : this) {
                buffer.append(i);
                buffer.append(", ");
            }
            buffer.setLength(buffer.length() - 2);
            buffer.append(')');
            return buffer.toString();
        }
    };
    desc.processors.stream().map(i -> {
        return getProcessor(i, currentPipeLineName, depth, subPipeCount);
    }).forEach(allSteps::add);
    Pipeline pipe = new Pipeline(allSteps, currentPipeLineName + (depth == 0 ? "" : "$" + subPipeCount.getAndIncrement()), desc.nextPipelineName);
    return pipe;
}
Also used : PropertyContext(loghub.RouteParser.PropertyContext) Arrays(java.util.Arrays) ArrayContext(loghub.RouteParser.ArrayContext) Input(loghub.configuration.ConfigListener.Input) URL(java.net.URL) LoggerContext(org.apache.logging.log4j.core.LoggerContext) URISyntaxException(java.net.URISyntaxException) CommonTokenStream(org.antlr.v4.runtime.CommonTokenStream) URLClassLoader(java.net.URLClassLoader) CharStreams(org.antlr.v4.runtime.CharStreams) Helpers(loghub.Helpers) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) Locale(java.util.Locale) ObjectReference(loghub.configuration.ConfigListener.ObjectReference) Map(java.util.Map) URI(java.net.URI) Path(java.nio.file.Path) DateTimeException(java.time.DateTimeException) Receiver(loghub.Receiver) TimeZone(java.util.TimeZone) Collection(java.util.Collection) LiteralContext(loghub.RouteParser.LiteralContext) Set(java.util.Set) BlockingQueue(java.util.concurrent.BlockingQueue) Reader(java.io.Reader) RouteLexer(loghub.RouteLexer) ZoneId(java.time.ZoneId) RouteParser(loghub.RouteParser) InvocationTargetException(java.lang.reflect.InvocationTargetException) ArrayBlockingQueue(java.util.concurrent.ArrayBlockingQueue) Processor(loghub.Processor) List(java.util.List) Logger(org.apache.logging.log4j.Logger) Entry(java.util.Map.Entry) Pattern(java.util.regex.Pattern) ParserRuleContext(org.antlr.v4.runtime.ParserRuleContext) ThrowingFunction(loghub.Helpers.ThrowingFunction) ThrowingConsumer(loghub.Helpers.ThrowingConsumer) HashMap(java.util.HashMap) Function(java.util.function.Function) ArrayList(java.util.ArrayList) HashSet(java.util.HashSet) CharStream(org.antlr.v4.runtime.CharStream) Sender(loghub.Sender) ThrowingPredicate(loghub.Helpers.ThrowingPredicate) Output(loghub.configuration.ConfigListener.Output) BeanValueContext(loghub.RouteParser.BeanValueContext) MalformedURLException(java.net.MalformedURLException) Files(java.nio.file.Files) ParseTreeWalker(org.antlr.v4.runtime.tree.ParseTreeWalker) Pipeline(loghub.Pipeline) IOException(java.io.IOException) Source(loghub.Source) File(java.io.File) Consumer(java.util.function.Consumer) NamedSubPipeline(loghub.processors.NamedSubPipeline) RecognitionException(org.antlr.v4.runtime.RecognitionException) Paths(java.nio.file.Paths) AnonymousSubPipeline(loghub.processors.AnonymousSubPipeline) Collections(java.util.Collections) LogManager(org.apache.logging.log4j.LogManager) Event(loghub.Event) InputStream(java.io.InputStream) Processor(loghub.Processor) ArrayList(java.util.ArrayList) Pipeline(loghub.Pipeline) NamedSubPipeline(loghub.processors.NamedSubPipeline) AnonymousSubPipeline(loghub.processors.AnonymousSubPipeline)

Example 3 with Pipeline

use of loghub.Pipeline in project LogHub by fbacchella.

the class Configuration method analyze.

private Properties analyze(ConfigListener conf) throws ConfigException {
    final Map<String, Object> newProperties = new HashMap<String, Object>(conf.properties.size() + Properties.PROPSNAMES.values().length + System.getProperties().size());
    // Resolvers properties found and and it to new properties
    Function<Object, Object> resolve = i -> {
        return ((i instanceof ConfigListener.ObjectWrapped) ? ((ConfigListener.ObjectWrapped) i).wrapped : (i instanceof ConfigListener.ObjectReference) ? parseObjectDescription((ConfigListener.ObjectDescription) i, emptyConstructor) : i);
    };
    conf.properties.entrySet().stream().forEach(i -> newProperties.put(i.getKey(), resolve.apply(i.getValue())));
    // Resolve the sources
    ThrowingFunction<Class<Source>, Source> sourceConstructor = i -> {
        return i.getConstructor().newInstance();
    };
    conf.sources.forEach((name, sd) -> {
        Source s = parseObjectDescription(sd, sourceConstructor);
        s.setName(name);
        sources.put(name, s);
    });
    Map<String, Pipeline> namedPipeLine = new HashMap<>(conf.pipelines.size());
    newProperties.put(Properties.PROPSNAMES.CLASSLOADERNAME.toString(), classLoader);
    Set<Pipeline> pipelines = new HashSet<>();
    // Generate all the named pipeline
    for (Entry<String, ConfigListener.PipenodesList> e : conf.pipelines.entrySet()) {
        String name = e.getKey();
        Pipeline p = parsePipeline(e.getValue(), name, 0, new AtomicInteger());
        pipelines.add(p);
        namedPipeLine.put(name, p);
        if (p.nextPipeline != null) {
            topPipelines.add(p.nextPipeline);
        }
    }
    newProperties.put(Properties.PROPSNAMES.PIPELINES.toString(), Collections.unmodifiableSet(pipelines));
    namedPipeLine = Collections.unmodifiableMap(namedPipeLine);
    newProperties.put(Properties.PROPSNAMES.NAMEDPIPELINES.toString(), namedPipeLine);
    // Find the queue depth
    final int queuesDepth = newProperties.containsKey("queueDepth") ? (Integer) newProperties.remove("queueDepth") : DEFAULTQUEUEDEPTH;
    newProperties.put(Properties.PROPSNAMES.QUEUESDEPTH.toString(), queuesDepth);
    BlockingQueue<Event> mainQueue = new ArrayBlockingQueue<Event>(queuesDepth);
    Map<String, BlockingQueue<Event>> outputQueues = new HashMap<>(namedPipeLine.size());
    namedPipeLine.keySet().stream().forEach(i -> outputQueues.put(i, new ArrayBlockingQueue<Event>(queuesDepth)));
    newProperties.put(Properties.PROPSNAMES.FORMATTERS.toString(), conf.formatters);
    newProperties.put(Properties.PROPSNAMES.MAINQUEUE.toString(), mainQueue);
    newProperties.put(Properties.PROPSNAMES.OUTPUTQUEUE.toString(), outputQueues);
    // Fill the receivers list
    receivers = new ArrayList<>();
    for (Input i : conf.inputs) {
        if (i.piperef == null || !namedPipeLine.containsKey(i.piperef)) {
            throw new RuntimeException("Invalid input, no destination pipeline: " + i);
        }
        for (ConfigListener.ObjectDescription desc : i.receiver) {
            Pipeline p = namedPipeLine.get(i.piperef);
            ThrowingFunction<Class<Receiver>, Receiver> receiverConstructor = r -> {
                return r.getConstructor(BlockingQueue.class, Pipeline.class).newInstance(mainQueue, p);
            };
            Receiver r = (Receiver) parseObjectDescription(desc, receiverConstructor);
            receivers.add(r);
        }
        inputpipelines.add(i.piperef);
    }
    topPipelines.addAll(inputpipelines);
    inputpipelines = Collections.unmodifiableSet(inputpipelines);
    receivers = Collections.unmodifiableList(receivers);
    newProperties.put(Properties.PROPSNAMES.RECEIVERS.toString(), receivers);
    // Fill the senders list
    senders = new ArrayList<>();
    for (Output o : conf.outputs) {
        if (o.piperef == null || !namedPipeLine.containsKey(o.piperef)) {
            throw new RuntimeException("Invalid output, no source pipeline: " + o);
        }
        for (ConfigListener.ObjectDescription desc : o.sender) {
            BlockingQueue<Event> out = outputQueues.get(o.piperef);
            ThrowingFunction<Class<Sender>, Sender> senderConstructor = r -> {
                return r.getConstructor(BlockingQueue.class).newInstance(out);
            };
            Sender s = (Sender) parseObjectDescription(desc, senderConstructor);
            // logger.debug("sender {} source point will be {}", () -> s, () -> namedPipeLine.get(o.piperef).outQueue);
            senders.add(s);
        }
        outputpipelines.add(o.piperef);
    }
    topPipelines.addAll(outputpipelines);
    outputpipelines = Collections.unmodifiableSet(outputpipelines);
    senders = Collections.unmodifiableList(senders);
    newProperties.put(Properties.PROPSNAMES.SENDERS.toString(), senders);
    newProperties.put(Properties.PROPSNAMES.TOPPIPELINE.toString(), Collections.unmodifiableSet(topPipelines));
    newProperties.put(Properties.PROPSNAMES.SOURCES.toString(), Collections.unmodifiableMap(sources));
    // Allows the system properties to override any properties given in the configuration file
    // But only if they are not some of the special internal properties
    Set<String> privatepropsnames = new HashSet<>(Properties.PROPSNAMES.values().length);
    Arrays.stream(Properties.PROPSNAMES.values()).forEach(i -> privatepropsnames.add(i.toString()));
    ;
    System.getProperties().entrySet().stream().filter(i -> !privatepropsnames.contains(i.getKey())).forEach(i -> newProperties.put(i.getKey().toString(), i.getValue()));
    return new Properties(newProperties);
}
Also used : PropertyContext(loghub.RouteParser.PropertyContext) Arrays(java.util.Arrays) ArrayContext(loghub.RouteParser.ArrayContext) Input(loghub.configuration.ConfigListener.Input) URL(java.net.URL) LoggerContext(org.apache.logging.log4j.core.LoggerContext) URISyntaxException(java.net.URISyntaxException) CommonTokenStream(org.antlr.v4.runtime.CommonTokenStream) URLClassLoader(java.net.URLClassLoader) CharStreams(org.antlr.v4.runtime.CharStreams) Helpers(loghub.Helpers) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) Locale(java.util.Locale) ObjectReference(loghub.configuration.ConfigListener.ObjectReference) Map(java.util.Map) URI(java.net.URI) Path(java.nio.file.Path) DateTimeException(java.time.DateTimeException) Receiver(loghub.Receiver) TimeZone(java.util.TimeZone) Collection(java.util.Collection) LiteralContext(loghub.RouteParser.LiteralContext) Set(java.util.Set) BlockingQueue(java.util.concurrent.BlockingQueue) Reader(java.io.Reader) RouteLexer(loghub.RouteLexer) ZoneId(java.time.ZoneId) RouteParser(loghub.RouteParser) InvocationTargetException(java.lang.reflect.InvocationTargetException) ArrayBlockingQueue(java.util.concurrent.ArrayBlockingQueue) Processor(loghub.Processor) List(java.util.List) Logger(org.apache.logging.log4j.Logger) Entry(java.util.Map.Entry) Pattern(java.util.regex.Pattern) ParserRuleContext(org.antlr.v4.runtime.ParserRuleContext) ThrowingFunction(loghub.Helpers.ThrowingFunction) ThrowingConsumer(loghub.Helpers.ThrowingConsumer) HashMap(java.util.HashMap) Function(java.util.function.Function) ArrayList(java.util.ArrayList) HashSet(java.util.HashSet) CharStream(org.antlr.v4.runtime.CharStream) Sender(loghub.Sender) ThrowingPredicate(loghub.Helpers.ThrowingPredicate) Output(loghub.configuration.ConfigListener.Output) BeanValueContext(loghub.RouteParser.BeanValueContext) MalformedURLException(java.net.MalformedURLException) Files(java.nio.file.Files) ParseTreeWalker(org.antlr.v4.runtime.tree.ParseTreeWalker) Pipeline(loghub.Pipeline) IOException(java.io.IOException) Source(loghub.Source) File(java.io.File) Consumer(java.util.function.Consumer) NamedSubPipeline(loghub.processors.NamedSubPipeline) RecognitionException(org.antlr.v4.runtime.RecognitionException) Paths(java.nio.file.Paths) AnonymousSubPipeline(loghub.processors.AnonymousSubPipeline) Collections(java.util.Collections) LogManager(org.apache.logging.log4j.LogManager) Event(loghub.Event) InputStream(java.io.InputStream) HashMap(java.util.HashMap) Source(loghub.Source) Input(loghub.configuration.ConfigListener.Input) ObjectReference(loghub.configuration.ConfigListener.ObjectReference) ArrayBlockingQueue(java.util.concurrent.ArrayBlockingQueue) Output(loghub.configuration.ConfigListener.Output) HashSet(java.util.HashSet) BlockingQueue(java.util.concurrent.BlockingQueue) ArrayBlockingQueue(java.util.concurrent.ArrayBlockingQueue) Receiver(loghub.Receiver) Pipeline(loghub.Pipeline) NamedSubPipeline(loghub.processors.NamedSubPipeline) AnonymousSubPipeline(loghub.processors.AnonymousSubPipeline) Sender(loghub.Sender) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) Event(loghub.Event)

Example 4 with Pipeline

use of loghub.Pipeline in project LogHub by fbacchella.

the class TestServer method testSimple.

@Test(timeout = 2000)
public void testSimple() throws InterruptedException {
    Properties empty = new Properties(Collections.emptyMap());
    BlockingQueue<Event> receiver = new ArrayBlockingQueue<>(1);
    TesterReceiver r = new TesterReceiver(receiver, new Pipeline(Collections.emptyList(), "testone", null));
    r.configure(empty);
    final ChannelFuture[] sent = new ChannelFuture[1];
    EventLoopGroup workerGroup = new DefaultEventLoopGroup();
    Bootstrap b = new Bootstrap();
    b.group(workerGroup);
    b.channel(LocalChannel.class);
    b.handler(new SimpleChannelInboundHandler<ByteBuf>() {

        @Override
        public void channelActive(ChannelHandlerContext ctx) {
            sent[0] = ctx.writeAndFlush(Unpooled.copiedBuffer("Message\r\n", CharsetUtil.UTF_8));
        }

        @Override
        protected void channelRead0(ChannelHandlerContext ctx, ByteBuf msg) throws Exception {
        }
    });
    // Start the client.
    ChannelFuture f = b.connect(new LocalAddress(TestServer.class.getCanonicalName())).sync();
    Thread.sleep(100);
    sent[0].sync();
    f.channel().close();
    // Wait until the connection is closed.
    f.channel().closeFuture().sync();
    Event e = receiver.poll();
    Assert.assertEquals("Message", e.get("message"));
}
Also used : ChannelFuture(io.netty.channel.ChannelFuture) LocalAddress(io.netty.channel.local.LocalAddress) ChannelHandlerContext(io.netty.channel.ChannelHandlerContext) Properties(loghub.configuration.Properties) ByteBuf(io.netty.buffer.ByteBuf) DefaultEventLoopGroup(io.netty.channel.DefaultEventLoopGroup) IOException(java.io.IOException) Pipeline(loghub.Pipeline) ChannelPipeline(io.netty.channel.ChannelPipeline) EventLoopGroup(io.netty.channel.EventLoopGroup) DefaultEventLoopGroup(io.netty.channel.DefaultEventLoopGroup) ArrayBlockingQueue(java.util.concurrent.ArrayBlockingQueue) Event(loghub.Event) AbstractBootstrap(io.netty.bootstrap.AbstractBootstrap) Bootstrap(io.netty.bootstrap.Bootstrap) ServerBootstrap(io.netty.bootstrap.ServerBootstrap) Test(org.junit.Test)

Example 5 with Pipeline

use of loghub.Pipeline in project LogHub by fbacchella.

the class TestHttp method makeReceiver.

public void makeReceiver(Consumer<Http> prepare) throws IOException {
    // Generate a locally binded random socket
    ServerSocket socket = new ServerSocket(0, 10, InetAddress.getLoopbackAddress());
    hostname = socket.getInetAddress().getHostAddress();
    port = socket.getLocalPort();
    socket.close();
    queue = new ArrayBlockingQueue<>(1);
    receiver = new Http(queue, new Pipeline(Collections.emptyList(), "testhttp", null));
    receiver.setHost(hostname);
    receiver.setPort(port);
    receiver.setDecoder(new StringCodec());
    prepare.accept(receiver);
    Assert.assertTrue(receiver.configure(new Properties(Collections.emptyMap())));
    receiver.start();
}
Also used : StringCodec(loghub.decoders.StringCodec) ServerSocket(java.net.ServerSocket) Properties(loghub.configuration.Properties) Pipeline(loghub.Pipeline)

Aggregations

Pipeline (loghub.Pipeline)23 Event (loghub.Event)21 Properties (loghub.configuration.Properties)20 Test (org.junit.Test)17 ArrayBlockingQueue (java.util.concurrent.ArrayBlockingQueue)8 HashMap (java.util.HashMap)6 IOException (java.io.IOException)4 Map (java.util.Map)4 File (java.io.File)2 InputStream (java.io.InputStream)2 Reader (java.io.Reader)2 InvocationTargetException (java.lang.reflect.InvocationTargetException)2 MalformedURLException (java.net.MalformedURLException)2 URI (java.net.URI)2 URISyntaxException (java.net.URISyntaxException)2 URL (java.net.URL)2 URLClassLoader (java.net.URLClassLoader)2 Files (java.nio.file.Files)2 Path (java.nio.file.Path)2 Paths (java.nio.file.Paths)2