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

io.github.aneudeveloper.func.engine.FuncStream Maven / Gradle / Ivy

There is a newer version: 1.2.3
Show newest version
/**
* Copyright 2022 aneuDeveloper
* 
* Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the * "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
* 
* The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
* 
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
* 
*/
package io.github.aneudeveloper.func.engine;

import java.util.Collection;
import java.util.Properties;

import org.apache.kafka.common.serialization.Serde;
import org.apache.kafka.common.serialization.Serdes;
import org.apache.kafka.streams.KafkaStreams;
import org.apache.kafka.streams.KeyValue;
import org.apache.kafka.streams.StreamsBuilder;
import org.apache.kafka.streams.StreamsConfig;
import org.apache.kafka.streams.kstream.Consumed;
import org.apache.kafka.streams.kstream.KStream;
import org.apache.kafka.streams.kstream.Produced;
import org.apache.kafka.streams.processor.RecordContext;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import io.github.aneudeveloper.func.engine.function.FuncEvent;
import io.github.aneudeveloper.func.engine.function.FuncEventTransformer;

public class FuncStream {
    private static final Logger LOGGER = LoggerFactory.getLogger(FuncStream.class);
    private FuncEngine processDefinition;
    private KafkaStreams streams;
    private FuncExecuter processEventExecuter;

    public FuncStream(FuncEngine processDefinition, FuncExecuter processEventExecuter) {
        this.processDefinition = processDefinition;
        this.processEventExecuter = processEventExecuter;
    }

    public void start(Collection topics) {
        if (topics == null || topics.isEmpty()) {
            throw new IllegalStateException("Cannot start workflow without any topics to observe.");
        }
        LOGGER.info("Starting workflow stream for following topics: {}", topics.toString());
        Serde> processEventSerde = this.processDefinition.getSerde();
        StreamsBuilder streamsBuilder = new StreamsBuilder();
        KStream> stream = streamsBuilder.stream(topics,
                Consumed.with(Serdes.String(), processEventSerde));

        stream.filter(this::shouldExecuteFuntion) //
                .mapValues(this.processEventExecuter::executeMessageAndDiscoverNextStep) //
                // .filter(null) // filter transient messages
                .transform(() -> this.createTransformer(), new String[0]) //
                .selectKey((key, functionEvent) -> this.selectTopicKey(key, functionEvent))
                .to((key, functionEvent, recordContext) -> this.toTopic(key, functionEvent, recordContext),
                        Produced.with(Serdes.String(), processEventSerde));

        Properties properties = this.prepareProperties(topics.iterator().next());
        this.streams = new KafkaStreams(streamsBuilder.build(), properties);
        this.streams.start();
    }

    private boolean shouldExecuteFuntion(String key, FuncEvent value) {
        boolean shouldExecute = (value.getType() == null || value.getType() == FuncEvent.Type.WORKFLOW)
                && value.getFunction() != null && !value.getFunction().isBlank();
        return shouldExecute;
    }

    private Properties prepareProperties(String topicName) {
        Properties properties = this.getWorkflowStreamProperties();
        String applicationNamePrefix = this.processDefinition.getProperty(FuncEngine.WORKFLOW_STREAM_PREFIX,
                this.processDefinition.getProcessName());
        properties.put(StreamsConfig.APPLICATION_ID_CONFIG, applicationNamePrefix + "-" + topicName);
        return properties;
    }

    private FuncEventTransformer createTransformer() {
        return new FuncEventTransformer() {

            @Override
            public KeyValue> transform(String key, FuncEvent functionEvent) {
                if (LOGGER.isTraceEnabled()) {
                    LOGGER.trace("Message received: {}", new String(FuncStream.this.processDefinition
                            .getSerde().serializer().serialize(null, functionEvent)));
                }
                if (functionEvent.getNextRetryAt() != null) {
                    functionEvent.setSourceTopic(this.context.topic());
                }
                return new KeyValue>(key, functionEvent);
            }
        };
    }

    private String selectTopicKey(String key, FuncEvent functionEvent) {
        if (functionEvent.getNextRetryAt() != null) {
            return functionEvent.getId();
        }
        return key;
    }

    private String toTopic(String key, FuncEvent functionEvent, RecordContext recordContext) {
        if(functionEvent.getNextRetryAt() != null){
            return this.processDefinition.getTopicResolver().getDelayTopic();
        }
        String topic = this.processDefinition.getTopicResolver().resolveTopicName(functionEvent.getType());
        return topic;
    }

    public void close() {
        LOGGER.info("Shutdown workflow stream");
        this.streams.close();
    }

    private Properties getWorkflowStreamProperties() {
        Properties properties = new Properties();
        properties.put("bootstrap.servers", this.processDefinition.getProperty("bootstrap.servers"));
        properties.put("auto.offset.reset", "earliest");
        properties.put("default.key.serde", Serdes.String().getClass());
        properties.put("num.stream.threads",
                this.processDefinition.getProperty("steps.num.stream.threads.config", "1"));
        properties.put("default.deserialization.exception.handler",
                this.processDefinition.getProperty("default.deserialization.exception.handler",
                        "org.apache.kafka.streams.errors.LogAndContinueExceptionHandler"));
        properties.put("processing.guarantee", "exactly_once_v2");
        return properties;
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy