Skip to content
This repository has been archived by the owner on Mar 3, 2023. It is now read-only.

Commit

Permalink
Add support for custom evictor and trigger policies (#2653)
Browse files Browse the repository at this point in the history
* Add support for custom evictor and trigger policies

Add support in both the streamlet and topology apis for the usage of custom eviction policies extending com.twitter.heron.api.windowing.EvictionPolicy and trigger policies extending com.twitter.heron.api.windowing.TriggerPolicy to enable user-defined windowing schemes.

* Fixed Checkstyle Issues

Fixed Checkstyle Issues

* Fixed extra line errors, reorganized if statement in WIndowedBoltExecutor to require both Policies to be provided if one of them is

* Reversed policy order to fix null pointer exception

* Trial push (local compilation not working for some reason, going to push then pull clean copy)

* Added Changes necessary to allow custom trigger policies to attach WindowManager, Config object, TriggerHandler and EvictionPolicy

* Fixed test errors due to lack of setting necessary paramters and defining "started" again for CountTriggerPolicy

* Remove flags and checks on AbstractBaseTriggerPolicy
  • Loading branch information
dancollins34 authored and srkukarni committed Jan 18, 2018
1 parent 850461c commit 2d86042
Show file tree
Hide file tree
Showing 12 changed files with 320 additions and 114 deletions.
Expand Up @@ -38,7 +38,10 @@

import com.twitter.heron.api.topology.OutputFieldsDeclarer;
import com.twitter.heron.api.topology.TopologyContext;
import com.twitter.heron.api.tuple.Tuple;
import com.twitter.heron.api.windowing.EvictionPolicy;
import com.twitter.heron.api.windowing.TimestampExtractor;
import com.twitter.heron.api.windowing.TriggerPolicy;
import com.twitter.heron.api.windowing.TupleFieldTimestampExtractor;
import com.twitter.heron.api.windowing.WindowingConfigs;

Expand Down Expand Up @@ -312,6 +315,28 @@ public BaseWindowedBolt withWatermarkInterval(Duration interval) {
return this;
}

/**
* Sets a custom eviction policy to use for this bolt
*
* @param evictionPolicy the eviction policy to use
* @return this
*/
public BaseWindowedBolt withCustomEvictor(EvictionPolicy<Tuple, ?> evictionPolicy) {
windowConfiguration.setTopologyBoltsWindowCustomEvictor(evictionPolicy);
return this;
}

/**
* Sets a custom trigger policy to use for this bolt
*
* @param triggerPolicy the trigger policy to use
* @return this
*/
public BaseWindowedBolt withCustomTrigger(TriggerPolicy<Tuple, ?> triggerPolicy) {
windowConfiguration.setTopologyBoltsWindowCustomTrigger(triggerPolicy);
return this;
}

@Override
public void prepare(Map<String, Object> topoConf, TopologyContext context, OutputCollector
collector) {
Expand Down
Expand Up @@ -234,12 +234,35 @@ private WindowManager<Tuple> initWindowManager(WindowLifecycleListener<Tuple>
"Late tuple stream can be defined only when " + "specifying" + " a timestamp field");
}
}
// validate
validate(topoConf, windowLengthCount, windowLengthDurationMs, slidingIntervalCount,
slidingIntervalDurationMs);
evictionPolicy = getEvictionPolicy(windowLengthCount, windowLengthDurationMs);
triggerPolicy = getTriggerPolicy(slidingIntervalCount, slidingIntervalDurationMs, manager,
evictionPolicy, topoConf);

boolean hasCustomTrigger = topoConf
.containsKey(WindowingConfigs.TOPOLOGY_BOLTS_WINDOW_CUSTOM_TRIGGER);
boolean hasCustomEvictor = topoConf
.containsKey(WindowingConfigs.TOPOLOGY_BOLTS_WINDOW_CUSTOM_EVICTOR);

if (hasCustomTrigger && hasCustomEvictor) {
triggerPolicy = (TriggerPolicy<Tuple, ?>)
topoConf.get(WindowingConfigs.TOPOLOGY_BOLTS_WINDOW_CUSTOM_TRIGGER);
evictionPolicy = (EvictionPolicy<Tuple, ?>)
topoConf.get(WindowingConfigs.TOPOLOGY_BOLTS_WINDOW_CUSTOM_EVICTOR);
} else if (!hasCustomEvictor && !hasCustomTrigger) {
// validate
validate(topoConf, windowLengthCount, windowLengthDurationMs, slidingIntervalCount,
slidingIntervalDurationMs);

evictionPolicy = getEvictionPolicy(windowLengthCount, windowLengthDurationMs);
triggerPolicy = getTriggerPolicy(slidingIntervalCount, slidingIntervalDurationMs);
} else {
throw new IllegalArgumentException(
"If either a custom TriggerPolicy or EvictionPolicy is defined, both must be."
);
}

triggerPolicy.setEvictionPolicy(evictionPolicy);
triggerPolicy.setTopologyConfig(topoConf);
triggerPolicy.setTriggerHandler(manager);
triggerPolicy.setWindowManager(manager);

manager.setEvictionPolicy(evictionPolicy);
manager.setTriggerPolicy(triggerPolicy);
// restore state if there is existing state
Expand Down Expand Up @@ -286,22 +309,18 @@ private boolean isTupleTs() {

@SuppressWarnings("HiddenField")
private TriggerPolicy<Tuple, ?> getTriggerPolicy(Count slidingIntervalCount, Long
slidingIntervalDurationMs, WindowManager<Tuple> manager, EvictionPolicy<Tuple, ?>
evictionPolicy, Map<String, Object> topoConf) {
slidingIntervalDurationMs) {
if (slidingIntervalCount != null) {
if (isTupleTs()) {
return new WatermarkCountTriggerPolicy<>(slidingIntervalCount.value, manager,
evictionPolicy, manager);
return new WatermarkCountTriggerPolicy<>(slidingIntervalCount.value);
} else {
return new CountTriggerPolicy<>(slidingIntervalCount.value, manager, evictionPolicy);
return new CountTriggerPolicy<>(slidingIntervalCount.value);
}
} else {
if (isTupleTs()) {
return new WatermarkTimeTriggerPolicy<>(slidingIntervalDurationMs, manager,
evictionPolicy, manager);
return new WatermarkTimeTriggerPolicy<>(slidingIntervalDurationMs);
} else {
return new TimeTriggerPolicy<>(slidingIntervalDurationMs, manager,
evictionPolicy, topoConf);
return new TimeTriggerPolicy<>(slidingIntervalDurationMs);
}
}
}
Expand Down
Expand Up @@ -30,10 +30,10 @@
* limitations under the License.
*/


package com.twitter.heron.api.windowing;

import java.io.Serializable;
import java.util.Map;

/**
* Triggers the window calculations based on the policy.
Expand Down Expand Up @@ -80,4 +80,32 @@ public interface TriggerPolicy<T extends Serializable, S> {
* @param state the state
*/
void restoreState(S state);

/**
* Set the eviction policy to whatever eviction policy to use this with
*
* @param evictionPolicy the eviction policy
*/
void setEvictionPolicy(EvictionPolicy<T, ?> evictionPolicy);

/**
* Set the trigger handler for this trigger policy to trigger
*
* @param triggerHandler the trigger handler
*/
void setTriggerHandler(TriggerHandler triggerHandler);

/**
* Sets the window manager that uses this trigger policy
*
* @param windowManager the window manager
*/
void setWindowManager(WindowManager<T> windowManager);

/**
* Sets the Config used for this topology
*
* @param config the configuration policy
*/
void setTopologyConfig(Map<String, Object> config);
}
Expand Up @@ -35,6 +35,8 @@
import java.util.HashMap;
import java.util.Map;

import com.twitter.heron.api.tuple.Tuple;

public class WindowingConfigs extends HashMap<String, Object> {

private static final long serialVersionUID = 1395902349429869055L;
Expand Down Expand Up @@ -90,6 +92,12 @@ public class WindowingConfigs extends HashMap<String, Object> {
public static final String TOPOLOGY_BOLTS_WATERMARK_EVENT_INTERVAL_MS = "topology.bolts"
+ ".watermark.event.interval.ms";

public static final String TOPOLOGY_BOLTS_WINDOW_CUSTOM_EVICTOR =
"topology.bolts.window.custom.evictor";

public static final String TOPOLOGY_BOLTS_WINDOW_CUSTOM_TRIGGER =
"topology.bolts.window.custom.trigger";

public void setTopologyBoltsWindowLengthCount(long value) {
setTopologyBoltsWindowLengthCount(this, value);
}
Expand Down Expand Up @@ -147,4 +155,22 @@ public static void setTopologyBoltsWatermarkEventIntervalMs(
Map<String, Object> conf, long value) {
conf.put(TOPOLOGY_BOLTS_WATERMARK_EVENT_INTERVAL_MS, value);
}

public void setTopologyBoltsWindowCustomEvictor(EvictionPolicy<Tuple, ?> value) {
setTopologyBoltsWindowCustomEvictor(this, value);
}

public static void setTopologyBoltsWindowCustomEvictor(Map<String, Object> conf,
EvictionPolicy<Tuple, ?> value) {
conf.put(TOPOLOGY_BOLTS_WINDOW_CUSTOM_EVICTOR, value);
}

public void setTopologyBoltsWindowCustomTrigger(TriggerPolicy<Tuple, ?> value) {
setTopologyBoltsWindowCustomTrigger(this, value);
}

public static void setTopologyBoltsWindowCustomTrigger(Map<String, Object> conf,
TriggerPolicy<Tuple, ?> value) {
conf.put(TOPOLOGY_BOLTS_WINDOW_CUSTOM_TRIGGER, value);
}
}
@@ -0,0 +1,107 @@
// Copyright 2017 Twitter. All rights reserved.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

/**
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance
* with the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package com.twitter.heron.api.windowing.triggers;

import java.io.Serializable;
import java.util.Map;

import com.twitter.heron.api.windowing.EvictionPolicy;
import com.twitter.heron.api.windowing.TriggerHandler;
import com.twitter.heron.api.windowing.TriggerPolicy;
import com.twitter.heron.api.windowing.WindowManager;


public abstract class AbstractBaseTriggerPolicy<T extends Serializable, S>
implements TriggerPolicy<T, S> {
protected TriggerHandler handler;
protected EvictionPolicy<T, ?> evictionPolicy;
protected WindowManager<T> windowManager;
protected Boolean started;
protected Map<String, Object> topoConf;

private boolean requiresEvictionPolicy = false;
private boolean requiresWindowManager = false;
private boolean requiresTopologyConfig = false;

/**
* Set the requirements in the constructor
*/
public AbstractBaseTriggerPolicy() {
}

/**
* Set the eviction policy to whatever eviction policy to use this with
*
* @param evictionPolicy the eviction policy
*/
public void setEvictionPolicy(EvictionPolicy<T, ?> evictionPolicy) {
this.evictionPolicy = evictionPolicy;
}

/**
* Set the trigger handler for this trigger policy to trigger
*
* @param triggerHandler the trigger handler
*/
public void setTriggerHandler(TriggerHandler triggerHandler) {
this.handler = triggerHandler;
}

/**
* Sets the window manager that uses this trigger policy
*
* @param windowManager the window manager
*/
public void setWindowManager(WindowManager<T> windowManager) {
this.windowManager = windowManager;
}

/**
* Sets the Config used for this topology
*
* @param config the configuration object
*/
public void setTopologyConfig(Map<String, Object> config) {
this.topoConf = config;
}

/**
* Starts the trigger policy. This can be used
* during recovery to start the triggers after
* recovery is complete.
*/
@Override
public void start() {
started = true;
}
}
Expand Up @@ -37,30 +37,24 @@

import com.twitter.heron.api.windowing.DefaultEvictionContext;
import com.twitter.heron.api.windowing.Event;
import com.twitter.heron.api.windowing.EvictionPolicy;
import com.twitter.heron.api.windowing.TriggerHandler;
import com.twitter.heron.api.windowing.TriggerPolicy;

/**
* A trigger that tracks event counts and calls back {@link TriggerHandler#onTrigger()}
* when the count threshold is hit.
*
* @param <T> the type of event tracked by this policy.
*/
public class CountTriggerPolicy<T extends Serializable> implements TriggerPolicy<T, Integer> {
public class CountTriggerPolicy<T extends Serializable> extends
AbstractBaseTriggerPolicy<T, Integer> {
private final int count;
private final AtomicInteger currentCount;
private final TriggerHandler handler;
private final EvictionPolicy<T, ?> evictionPolicy;
private boolean started;

public CountTriggerPolicy(int count, TriggerHandler handler, EvictionPolicy<T, ?>
evictionPolicy) {
public CountTriggerPolicy(int count) {
super();

this.count = count;
this.currentCount = new AtomicInteger();
this.handler = handler;
this.evictionPolicy = evictionPolicy;
this.started = false;
}

@Override
Expand All @@ -78,11 +72,6 @@ public void reset() {
currentCount.set(0);
}

@Override
public void start() {
started = true;
}

@Override
public void shutdown() {
// NOOP
Expand Down

0 comments on commit 2d86042

Please sign in to comment.