Add template for assignment 3

This commit is contained in:
2021-05-17 15:44:23 +02:00
parent 1363e83aa3
commit be629aa432
64 changed files with 3556 additions and 0 deletions
@@ -0,0 +1,15 @@
package dst.ass3.event;
/**
* Constants.
*/
public final class Constants {
/**
* The TCP port of the {@link EventPublisher}.
*/
public static final int EVENT_PUBLISHER_PORT = 1338;
private Constants() {
}
}
@@ -0,0 +1,16 @@
package dst.ass3.event;
/**
* Creates your {@link IEventProcessingEnvironment} and {@link IEventSourceFunction} implementation instances.
*/
public class EventProcessingFactory {
public static IEventProcessingEnvironment createEventProcessingEnvironment() {
// TODO
return null;
}
public static IEventSourceFunction createEventSourceFunction() {
// TODO
return null;
}
}
@@ -0,0 +1,173 @@
package dst.ass3.event;
import java.util.concurrent.atomic.AtomicBoolean;
import java.util.function.Function;
import dst.ass3.event.model.domain.ITripEventInfo;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.apache.flink.shaded.netty4.io.netty.bootstrap.ServerBootstrap;
import org.apache.flink.shaded.netty4.io.netty.channel.ChannelFuture;
import org.apache.flink.shaded.netty4.io.netty.channel.ChannelInitializer;
import org.apache.flink.shaded.netty4.io.netty.channel.EventLoopGroup;
import org.apache.flink.shaded.netty4.io.netty.channel.group.ChannelGroup;
import org.apache.flink.shaded.netty4.io.netty.channel.group.DefaultChannelGroup;
import org.apache.flink.shaded.netty4.io.netty.channel.nio.NioEventLoopGroup;
import org.apache.flink.shaded.netty4.io.netty.channel.socket.SocketChannel;
import org.apache.flink.shaded.netty4.io.netty.channel.socket.nio.NioServerSocketChannel;
import org.apache.flink.shaded.netty4.io.netty.handler.codec.serialization.ClassResolvers;
import org.apache.flink.shaded.netty4.io.netty.handler.codec.serialization.ObjectDecoder;
import org.apache.flink.shaded.netty4.io.netty.handler.codec.serialization.ObjectEncoder;
/**
* An EventPublisher accepts incoming TCP socket connections on a given port and is able to broadcast {@link ITripEventInfo}
* objects to these clients.
*/
public class EventPublisher {
private static final Logger LOG = LoggerFactory.getLogger(EventPublisher.class);
private final Object clientChannelMonitor = new Object();
private final int port;
private final AtomicBoolean closed;
private EventLoopGroup bossGroup;
private EventLoopGroup workerGroup;
private ChannelGroup clientChannels;
public EventPublisher(int port) {
this.port = port;
this.closed = new AtomicBoolean(false);
}
public int getPort() {
return port;
}
/**
* Broadcast an event to all listening channels. Does nothing if no clients are connected.
*
* @param event the event to publish
* @throws IllegalStateException if the publisher hasn't been started yet or has been closed
*/
public void publish(ITripEventInfo event) {
if (clientChannels == null || closed.get()) {
throw new IllegalStateException();
}
clientChannels.writeAndFlush(event).syncUninterruptibly();
// wait a bit for event to propagate
try {
Thread.sleep(10);
} catch (InterruptedException e) {}
}
/**
* Like {@link #publish(ITripEventInfo)} but waits for a given number of milliseconds and then passes the current system
* time to a factory function.
*
* @param delay the delay in ms
* @param provider the provider
*/
public void publish(long delay, Function<Long, ITripEventInfo> provider) {
if (delay > 0) {
try {
Thread.sleep(delay);
} catch (InterruptedException e) {
}
}
publish(provider.apply(System.currentTimeMillis()));
}
/**
* This method blocks if no clients are connected, and is notified as soon as a client connects. If clients are
* connected, the method returns immediately.
*/
public void waitForClients() {
if (clientChannels.isEmpty()) {
LOG.debug("Waiting for clients to connect...");
synchronized (clientChannelMonitor) {
try {
clientChannelMonitor.wait();
} catch (InterruptedException e) {
LOG.debug("Interrupted while waiting on client connections", e);
}
}
}
}
public int getConnectedClientCount() {
if (clientChannels == null || closed.get()) {
throw new IllegalStateException();
}
return clientChannels.size();
}
/**
* Closes all active client connections.
*/
public void dropClients() {
if (clientChannels == null || closed.get()) {
throw new IllegalStateException();
}
clientChannels.close().syncUninterruptibly().group().clear();
}
/**
* Start the server and accept incoming connections. Will call {@link #close()} if an error occurs during
* connection.
*/
public void start() {
bossGroup = new NioEventLoopGroup();
workerGroup = new NioEventLoopGroup();
clientChannels = new DefaultChannelGroup(workerGroup.next());
ServerBootstrap b = new ServerBootstrap();
b.group(bossGroup, workerGroup)
.channel(NioServerSocketChannel.class)
.childHandler(new ClientChannelInitializer());
// Bind and start to accept incoming connections
ChannelFuture f = b.bind(port).addListener(future -> {
if (!future.isSuccess()) {
LOG.error("Error while binding socket");
close();
}
}).syncUninterruptibly();
LOG.info("Accepting connections on {}", f.channel());
}
/**
* Closes all channels and resources.
*/
public void close() {
if (closed.compareAndSet(false, true)) {
LOG.info("Shutting down event loops");
if (bossGroup != null) {
bossGroup.shutdownGracefully();
}
if (workerGroup != null) {
workerGroup.shutdownGracefully();
}
}
}
private class ClientChannelInitializer extends ChannelInitializer<SocketChannel> {
@Override
public void initChannel(SocketChannel ch) throws Exception {
LOG.info("Initializing client channel {}", ch);
clientChannels.add(ch);
ch.pipeline()
.addFirst(new ObjectEncoder())
.addFirst(new ObjectDecoder(ClassResolvers.cacheDisabled(ClassLoader.getSystemClassLoader())));
synchronized (clientChannelMonitor) {
clientChannelMonitor.notifyAll();
}
}
}
}
@@ -0,0 +1,178 @@
package dst.ass3.event;
import java.lang.reflect.Proxy;
import java.net.SocketAddress;
import java.util.concurrent.BlockingQueue;
import java.util.concurrent.LinkedBlockingQueue;
import dst.ass3.event.model.domain.ITripEventInfo;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.apache.flink.shaded.netty4.io.netty.bootstrap.Bootstrap;
import org.apache.flink.shaded.netty4.io.netty.channel.Channel;
import org.apache.flink.shaded.netty4.io.netty.channel.ChannelHandlerContext;
import org.apache.flink.shaded.netty4.io.netty.channel.ChannelInboundHandlerAdapter;
import org.apache.flink.shaded.netty4.io.netty.channel.ChannelOption;
import org.apache.flink.shaded.netty4.io.netty.channel.EventLoopGroup;
import org.apache.flink.shaded.netty4.io.netty.channel.nio.NioEventLoopGroup;
import org.apache.flink.shaded.netty4.io.netty.channel.socket.nio.NioSocketChannel;
import org.apache.flink.shaded.netty4.io.netty.handler.codec.serialization.ClassResolvers;
import org.apache.flink.shaded.netty4.io.netty.handler.codec.serialization.ObjectDecoder;
import org.apache.flink.shaded.netty4.io.netty.handler.codec.serialization.ObjectEncoder;
import org.apache.flink.shaded.netty4.io.netty.util.concurrent.Future;
/**
* An EventSubscriber receives ITripEventInfo objects through a netty SocketChannel. Create and connect an
* EventSubscriber using {@link #subscribe(SocketAddress)}. To receive events, call {@link #receive()}.
*/
public class EventSubscriber {
private static final Logger LOG = LoggerFactory.getLogger(EventSubscriber.class);
private static final ITripEventInfo POISON_PILL = (ITripEventInfo) Proxy.newProxyInstance(
ITripEventInfo.class.getClassLoader(), new Class[]{ITripEventInfo.class}, (p, m, a) -> null);
private final SocketAddress publisherAddress;
private final BlockingQueue<ITripEventInfo> queue;
private volatile boolean closed;
private Channel channel;
private EventLoopGroup loop;
private EventSubscriber(SocketAddress publisherAddress) {
this.publisherAddress = publisherAddress;
this.queue = new LinkedBlockingQueue<>();
}
/**
* Blocks to receive the next ITripEventInfo published into the channel. Returns {@code null} if the underlying
* channel has been closed or the thread was interrupted.
*
* @return the next ITripEventInfo object
* @throws IllegalStateException thrown if the previous call returned null and the channel was closed
*/
public ITripEventInfo receive() throws IllegalStateException {
synchronized (queue) {
if (closed && queue.isEmpty()) {
throw new IllegalStateException();
}
}
ITripEventInfo event;
try {
event = queue.take();
if (event == POISON_PILL) {
return null;
} else {
return event;
}
} catch (InterruptedException e) {
return null;
}
}
private Future<?> start() {
loop = new NioEventLoopGroup();
channel = new Bootstrap()
.group(loop)
.channel(NioSocketChannel.class)
.option(ChannelOption.TCP_NODELAY, true)
.handler(new EventSubscriberHandler())
.connect(publisherAddress) // ChannelFuture
.addListener(future -> {
if (!future.isSuccess()) {
LOG.error("Error while connecting");
close();
}
})
.syncUninterruptibly()
.channel();
LOG.info("Connected to channel {}", channel);
return loop.submit(() -> {
try {
channel.closeFuture().sync();
} catch (InterruptedException e) {
// noop
} finally {
close();
}
});
}
/**
* Closes all resources and threads used by the EventSubscriber.
*/
public void close() {
try {
if (loop != null) {
synchronized (queue) {
if (!loop.isShutdown() && !loop.isTerminated() && !loop.isShuttingDown()) {
LOG.info("Shutting down event loop");
loop.shutdownGracefully();
}
}
}
} finally {
synchronized (queue) {
if (!closed) {
LOG.debug("Adding poison pill to queue");
closed = true;
queue.add(POISON_PILL);
}
}
}
}
/**
* Creates a new EventSubscriber that connects to given SocketAddress.
*
* @param address the socket address
* @return a new EventSubscriber
*/
public static EventSubscriber subscribe(SocketAddress address) {
EventSubscriber eventSubscriber = new EventSubscriber(address);
eventSubscriber.start();
return eventSubscriber;
}
private class EventSubscriberHandler extends ChannelInboundHandlerAdapter {
@Override
public void channelRead(ChannelHandlerContext ctx, Object msg) throws Exception {
ctx.read();
if (!(msg instanceof ITripEventInfo)) {
LOG.error("Unknown message type received {}", msg);
return;
}
synchronized (queue) {
if (!closed) {
queue.add((ITripEventInfo) msg);
}
}
}
@Override
public void exceptionCaught(ChannelHandlerContext ctx, Throwable cause) {
LOG.error("EventSubscriberHandler caught an exception", cause);
ctx.close();
close();
}
@Override
public void channelActive(ChannelHandlerContext ctx) {
ctx.pipeline()
.addFirst(new ObjectEncoder())
.addFirst(new ObjectDecoder(ClassResolvers.cacheDisabled(ClassLoader.getSystemClassLoader())));
}
}
}
@@ -0,0 +1,39 @@
package dst.ass3.event;
import dst.ass3.event.model.events.*;
import org.apache.flink.streaming.api.environment.StreamExecutionEnvironment;
import org.apache.flink.streaming.api.functions.sink.SinkFunction;
import org.apache.flink.streaming.api.windowing.time.Time;
/**
* This class should be used to implement the event processing steps as described in the assignment. The test classes
* will inject SinkFunctions, create a new StreamExecutionEnvironment and then call {@link
* #initialize(StreamExecutionEnvironment)}.
*/
public interface IEventProcessingEnvironment {
/**
* Initializes the event processing graph on the {@link StreamExecutionEnvironment}. This function is called
* after all sinks have been set.
*/
void initialize(StreamExecutionEnvironment env);
/**
* Sets the timeout limit of a streaming event.
*
* @param time the timeout limit
*/
void setMatchingDurationTimeout(Time time);
void setLifecycleEventStreamSink(SinkFunction<LifecycleEvent> sink);
void setMatchingDurationStreamSink(SinkFunction<MatchingDuration> sink);
void setAverageMatchingDurationStreamSink(SinkFunction<AverageMatchingDuration> sink);
void setMatchingTimeoutWarningStreamSink(SinkFunction<MatchingTimeoutWarning> sink);
void setTripFailedWarningStreamSink(SinkFunction<TripFailedWarning> sink);
void setAlertStreamSink(SinkFunction<Alert> sink);
}
@@ -0,0 +1,25 @@
package dst.ass3.event;
import org.apache.flink.api.common.functions.RichFunction;
import org.apache.flink.configuration.Configuration;
import org.apache.flink.streaming.api.functions.source.SourceFunction;
import dst.ass3.event.model.domain.ITripEventInfo;
/**
* A RichFunction & SourceFunction for ITripEventInfo objects.
*/
public interface IEventSourceFunction extends RichFunction, SourceFunction<ITripEventInfo> {
@Override
void open(Configuration parameters) throws Exception;
@Override
void close() throws Exception;
@Override
void run(SourceContext<ITripEventInfo> ctx) throws Exception;
@Override
void cancel();
}
@@ -0,0 +1,54 @@
package dst.ass3.event.dto;
import java.io.Serializable;
import dst.ass3.event.model.domain.ITripEventInfo;
import dst.ass3.event.model.domain.Region;
import dst.ass3.event.model.domain.TripState;
public class TripEventInfoDTO implements Serializable, ITripEventInfo {
private static final long serialVersionUID = 4134104076758220138L;
private Long tripId;
private Long timestamp;
private TripState state;
private Region region;
public TripEventInfoDTO(Long tripId, Long timestamp, TripState state, Region region) {
this.tripId = tripId;
this.timestamp = timestamp;
this.state = state;
this.region = region;
}
@Override
public Long getTripId() {
return tripId;
}
@Override
public Long getTimestamp() {
return timestamp;
}
@Override
public TripState getState() {
return state;
}
@Override
public Region getRegion() {
return region;
}
@Override
public String toString() {
return "TripEventInfoDTO{" +
"tripId=" + tripId +
", timestamp=" + timestamp +
", state=" + state +
", region=" + region +
'}';
}
}
@@ -0,0 +1,9 @@
package dst.ass3.event.model.domain;
public interface ITripEventInfo {
Long getTripId();
Long getTimestamp();
TripState getState();
Region getRegion();
}
@@ -0,0 +1,7 @@
package dst.ass3.event.model.domain;
public enum Region {
AT_VIENNA,
AT_LINZ,
DE_BERLIN
}
@@ -0,0 +1,5 @@
package dst.ass3.event.model.domain;
public enum TripState {
CREATED, QUEUED, MATCHED, APPROACHING, IN_PROGRESS, CANCELLED, COMPLETED
}
@@ -0,0 +1,49 @@
package dst.ass3.event.model.events;
import java.io.Serializable;
import java.util.List;
import dst.ass3.event.model.domain.Region;
/**
* A system alert that aggregates several warnings.
*/
public class Alert implements Serializable {
private static final long serialVersionUID = -4561132671849230635L;
private Region region;
private List<Warning> warnings;
public Alert() {
}
public Alert(Region region, List<Warning> warnings) {
this.region = region;
this.warnings = warnings;
}
public Region getRegion() {
return region;
}
public void setRegion(Region region) {
this.region = region;
}
public List<Warning> getWarnings() {
return warnings;
}
public void setWarnings(List<Warning> warnings) {
this.warnings = warnings;
}
@Override
public String toString() {
return "Alert{" +
"region='" + region + '\'' +
", warnings=" + warnings +
'}';
}
}
@@ -0,0 +1,48 @@
package dst.ass3.event.model.events;
import java.io.Serializable;
import dst.ass3.event.model.domain.Region;
/**
* The average of several {@link MatchingDuration} values.
*/
public class AverageMatchingDuration implements Serializable {
private static final long serialVersionUID = -3767582104941550250L;
private Region region;
private double duration;
public AverageMatchingDuration() {
}
public AverageMatchingDuration(Region region, double duration) {
this.region = region;
this.duration = duration;
}
public Region getRegion() {
return region;
}
public void setRegion(Region region) {
this.region = region;
}
public double getDuration() {
return duration;
}
public void setDuration(double duration) {
this.duration = duration;
}
@Override
public String toString() {
return "AverageMatchingDuration{" +
"region='" + region + '\'' +
", duration=" + duration +
'}';
}
}
@@ -0,0 +1,84 @@
package dst.ass3.event.model.events;
import java.io.Serializable;
import dst.ass3.event.model.domain.ITripEventInfo;
import dst.ass3.event.model.domain.Region;
import dst.ass3.event.model.domain.TripState;
/**
* Indicates a change in the lifecycle state of an ITripEventInfo.
*/
public class LifecycleEvent implements Serializable {
private static final long serialVersionUID = 8665269919851487210L;
/**
* The id of the trip, as returned by {@link ITripEventInfo#getTripId()}.
*/
private long tripId;
private TripState state;
private Region region;
/**
* The instant the event was recorded (unix epoch in milliseconds)
*/
private long timestamp;
public LifecycleEvent() {
}
public LifecycleEvent(ITripEventInfo eventInfo) {
this(eventInfo.getTripId(), eventInfo.getState(), eventInfo.getRegion(), eventInfo.getTimestamp());
}
public LifecycleEvent(long tripId, TripState state, Region region, long timestamp) {
this.tripId = tripId;
this.state = state;
this.region = region;
this.timestamp = timestamp;
}
public long getTripId() {
return tripId;
}
public void setTripId(long tripId) {
this.tripId = tripId;
}
public TripState getState() {
return state;
}
public void setState(TripState state) {
this.state = state;
}
public Region getRegion() {
return region;
}
public void setRegion(Region region) {
this.region = region;
}
public long getTimestamp() {
return timestamp;
}
public void setTimestamp(long timestamp) {
this.timestamp = timestamp;
}
@Override
public String toString() {
return "LifecycleEvent{" +
"tripId=" + tripId +
", state=" + state +
", region=" + region +
", timestamp=" + timestamp +
'}';
}
}
@@ -0,0 +1,59 @@
package dst.ass3.event.model.events;
import java.io.Serializable;
import dst.ass3.event.model.domain.Region;
/**
* Indicates the amount of time an ITripEventInfo took to get from CREATED to MATCHED.
*/
public class MatchingDuration implements Serializable {
private static final long serialVersionUID = -6976972381929291369L;
private long eventId;
private Region region;
private long duration;
public MatchingDuration() {
}
public MatchingDuration(long eventId, Region region, long duration) {
this.eventId = eventId;
this.region = region;
this.duration = duration;
}
public long getEventId() {
return eventId;
}
public void setEventId(long eventId) {
this.eventId = eventId;
}
public Region getRegion() {
return region;
}
public void setRegion(Region region) {
this.region = region;
}
public long getDuration() {
return duration;
}
public void setDuration(long duration) {
this.duration = duration;
}
@Override
public String toString() {
return "MatchingDuration{" +
"eventId=" + eventId +
", region='" + region + '\'' +
", duration=" + duration +
'}';
}
}
@@ -0,0 +1,37 @@
package dst.ass3.event.model.events;
import dst.ass3.event.model.domain.Region;
/**
* Warning that indicates that a matching event has not reached the lifecycle state MATCHED within a given time frame.
*/
public class MatchingTimeoutWarning extends Warning {
private static final long serialVersionUID = 7955599732178947649L;
private long tripId;
public MatchingTimeoutWarning() {
super(null);
}
public MatchingTimeoutWarning(long tripId, Region region) {
super(region);
this.tripId = tripId;
}
public long getTripId() {
return tripId;
}
public void setTripId(long tripId) {
this.tripId = tripId;
}
@Override
public String toString() {
return "MatchingTimeoutWarning{" +
"tripId=" + tripId +
'}';
}
}
@@ -0,0 +1,37 @@
package dst.ass3.event.model.events;
import dst.ass3.event.model.domain.Region;
/**
* Indicates that matching a trip has probably failed.
*/
public class TripFailedWarning extends Warning {
private static final long serialVersionUID = -9120187311385112769L;
private long eventId;
public TripFailedWarning() {
super(null);
}
public TripFailedWarning(long eventId, Region region) {
super(region);
this.eventId = eventId;
}
public long getEventId() {
return eventId;
}
public void setEventId(long eventId) {
this.eventId = eventId;
}
@Override
public String toString() {
return "TripFailedWarning{" +
"eventId=" + eventId +
'}';
}
}
@@ -0,0 +1,37 @@
package dst.ass3.event.model.events;
import java.io.Serializable;
import dst.ass3.event.model.domain.Region;
/**
* Base class for region warnings.
*/
public abstract class Warning implements Serializable {
private static final long serialVersionUID = 273266717303711974L;
private Region region;
public Warning() {
}
public Warning(Region region) {
this.region = region;
}
public Region getRegion() {
return region;
}
public void setRegion(Region region) {
this.region = region;
}
@Override
public String toString() {
return "Warning{" +
"region='" + region + '\'' +
'}';
}
}