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

Add support for custom evictor and trigger policies #2653

Merged
merged 8 commits into from
Jan 18, 2018
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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);

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Place remove extra line

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
Original file line number Diff line number Diff line change
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);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think we should remove this from the interface. Having this interface is confusing to users since WindowManager implements the TriggerHandler interface. WindowManager is also not a interface that users can implement thus it shouldn't be part of an interface that users will have to implement. I think having "setTriggerHandler" should suffice. For build in triggers (e.g. WatermarkCountTriggerPolicy, WatermarkTimeTriggerPolicy), we can just pass in the WindowManager via their constructor.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The reasoning behind this was that the WindowManager, TriggerHandler (which is the WindowManager) and TopologyConfig are three things the users may not have access to directly. I was aiming to provide access to the same structures used in the internal TriggerPolicies to custom TriggerPolicies, should users wish to use them. The idea was that, if someone wanted to, it should be possible to use the existing trigger policies in this manner.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looking more closely at the code I am confused how the a custom trigger policy will work. All the existing trigger policies require the WindowManager to actually trigger windows. How will a user implementing a custom TriggerPolicy do this. They have no reference to the WindowManager. Shouldn't the windowManager be passed into the custom trigger implicitly? As of right now, trigger polices can't trigger windows without the WindowManager. That is why I suggested removing the setWindowManager interface, since trigger policies need the window manager regardless of what kind of trigger policy it is.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Take a look at the changes i’ve made to windowedBoltExecutor. Essentially, in order to allow these to attach to arbitrary triggerPolicies, Ive replaced their inclusion in the constructor with the setters you see in the interface for all trigger policies.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I see


/**
* Sets the Config used for this topology
*
* @param config the configuration policy
*/
void setTopologyConfig(Map<String, Object> config);
}
Original file line number Diff line number Diff line change
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);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,128 @@
// 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>
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not sure if this interface is necessary, if we remove WindowManager from the TriggerPolicy interface

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Regardless, you would still need a way to get the TriggerHandler (WindowPolicy) attached to the custom TriggerPolicy that was passed to the config. AbstractBaseTriggerPolicy was just made as a way to standardize how these attachments were handled for the internal classes, and provide a way to use those same attachment variables in custom classes.

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(boolean requiresEvictionPolicy,
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not sure if we need these checks. I think it should be up to the user to make sure to pass in the correct objects he or she needs for a custom trigger. Having to pass these booleans into the constructor also makes the interface confusing to some reading to code. If you really want these checks in place, I would suggest using setters to set these. Then it is clear to the reader of the code what is being set.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@jerrypeng These checks were in place to keep the existing trigger policies as restrictive as they were before wrt which of the objects (evictionPolicy, windowManager) were attached. I could provide an alternate no-arg constructor that sets them all to false, then use setters instead? However, since this is the abstract class which already partially implements the TriggerPolicy interface, I would think it would be okay to perform some checks, but I have no issue with removing them either.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

lets remove them to keep the interface as simple and clean as possible

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@jerrypeng will do

boolean requiresWindowManager,
boolean requiresTopologyConfig) {
this.requiresEvictionPolicy = requiresEvictionPolicy;
this.requiresWindowManager = requiresWindowManager;
this.requiresTopologyConfig = requiresTopologyConfig;
}

/**
* 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() {
if (this.evictionPolicy == null && this.requiresEvictionPolicy) {
throw new RuntimeException("EvictionPolicy of TriggerPolicy was not set.");
}

if (this.handler == null) {
throw new RuntimeException("TriggerHandler of TriggerPolicy was not set.");
}

if (this.windowManager == null && this.requiresWindowManager) {
throw new RuntimeException("WindowManager of TriggerPolicy was not set.");
}

if (this.topoConf == null && this.requiresTopologyConfig) {
throw new RuntimeException("WindowManager of TriggerPolicy was not set.");
}

started = true;
}
}
Original file line number Diff line number Diff line change
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(true, false, false);

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