001/* 002 * Licensed to the Apache Software Foundation (ASF) under one 003 * or more contributor license agreements. See the NOTICE file 004 * distributed with this work for additional information 005 * regarding copyright ownership. The ASF licenses this file 006 * to you under the Apache License, Version 2.0 (the 007 * "License"); you may not use this file except in compliance 008 * with the License. You may obtain a copy of the License at 009 * 010 * http://www.apache.org/licenses/LICENSE-2.0 011 * 012 * Unless required by applicable law or agreed to in writing, 013 * software distributed under the License is distributed on an 014 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY 015 * KIND, either express or implied. See the License for the 016 * specific language governing permissions and limitations 017 * under the License. 018 */ 019package org.apache.reef.vortex.api; 020 021import org.apache.reef.annotations.Unstable; 022import org.apache.reef.annotations.audience.ClientSide; 023import org.apache.reef.annotations.audience.Public; 024import org.apache.reef.util.Builder; 025import org.apache.reef.util.Optional; 026 027import java.io.Serializable; 028 029/** 030 * The policy for local aggregation on the {@link org.apache.reef.vortex.evaluator.VortexWorker}s. 031 * The Aggregation function will be triggered on the individual {@link VortexFunction} results on 032 * an "OR" basis of what is specified by the policy. 033 * TODO[REEF-504]: Clean up Serializable in Vortex. 034 */ 035@ClientSide 036@Public 037@Unstable 038public final class VortexAggregatePolicy implements Serializable { 039 private final Optional<Integer> count; 040 private final int periodMilliseconds; 041 042 private VortexAggregatePolicy(final int periodMilliseconds, final Optional<Integer> count) { 043 this.periodMilliseconds = periodMilliseconds; 044 this.count = count; 045 } 046 047 /** 048 * @return the aggregation period in milliseconds. 049 */ 050 public int getPeriodMilliseconds() { 051 return periodMilliseconds; 052 } 053 054 /** 055 * @return the count trigger for the aggregation. 056 */ 057 public Optional<Integer> getCount() { 058 return count; 059 } 060 061 /** 062 * @return a new {@link Builder} for {@link VortexAggregatePolicy}. 063 */ 064 public static AggregatePolicyBuilder newBuilder() { 065 return new AggregatePolicyBuilder(); 066 } 067 068 /** 069 * A Builder class for {@link VortexAggregatePolicy}. 070 */ 071 public static final class AggregatePolicyBuilder implements Builder<VortexAggregatePolicy> { 072 private Integer periodMilliseconds = null; 073 private Optional<Integer> count = Optional.empty(); 074 075 private AggregatePolicyBuilder() { 076 } 077 078 /** 079 * Sets the period to trigger aggregation in milliseconds. Required parameter to build. 080 */ 081 public AggregatePolicyBuilder setTimerPeriodTrigger(final int pOffsetMilliseconds) { 082 periodMilliseconds = pOffsetMilliseconds; 083 return this; 084 } 085 086 /** 087 * Sets the count trigger for aggregation. Not required. 088 */ 089 public AggregatePolicyBuilder setCountTrigger(final int pCount) { 090 count = Optional.of(pCount); 091 return this; 092 } 093 094 /** 095 * Builds and returns a new {@link VortexAggregatePolicy} based on user's specification. 096 * The timer period is a required parameter for this to succeed. 097 * @throws IllegalArgumentException if required parameters are not set or if parameters are invalid. 098 */ 099 @Override 100 public VortexAggregatePolicy build() throws IllegalArgumentException { 101 if (periodMilliseconds == null) { 102 throw new IllegalArgumentException("The aggregate period must be set."); 103 } 104 105 if (count.isPresent() && count.get() <= 0) { 106 throw new IllegalArgumentException("The count trigger must be greater than zero."); 107 } 108 109 return new VortexAggregatePolicy(periodMilliseconds, count); 110 } 111 } 112}