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 027/** 028 * The policy for local aggregation on the {@link org.apache.reef.vortex.evaluator.VortexWorker}s. 029 * The Aggregation function will be triggered on the individual {@link VortexFunction} results on 030 * an "OR" basis of what is specified by the policy. 031 */ 032@ClientSide 033@Public 034@Unstable 035public final class VortexAggregatePolicy { 036 private Optional<Integer> count; 037 private int periodMilliseconds; 038 039 /** 040 * No-arg constructor required for Kryo to serialize/deserialize. 041 */ 042 VortexAggregatePolicy() { 043 } 044 045 private VortexAggregatePolicy(final int periodMilliseconds, final Optional<Integer> count) { 046 this.periodMilliseconds = periodMilliseconds; 047 this.count = count; 048 } 049 050 /** 051 * @return the aggregation period in milliseconds. 052 */ 053 public int getPeriodMilliseconds() { 054 return periodMilliseconds; 055 } 056 057 /** 058 * @return the count trigger for the aggregation. 059 */ 060 public Optional<Integer> getCount() { 061 return count; 062 } 063 064 /** 065 * @return a new {@link Builder} for {@link VortexAggregatePolicy}. 066 */ 067 public static AggregatePolicyBuilder newBuilder() { 068 return new AggregatePolicyBuilder(); 069 } 070 071 /** 072 * A Builder class for {@link VortexAggregatePolicy}. 073 */ 074 public static final class AggregatePolicyBuilder implements Builder<VortexAggregatePolicy> { 075 private Integer periodMilliseconds = null; 076 private Optional<Integer> count = Optional.empty(); 077 078 private AggregatePolicyBuilder() { 079 } 080 081 /** 082 * Sets the period to trigger aggregation in milliseconds. Required parameter to build. 083 */ 084 public AggregatePolicyBuilder setTimerPeriodTrigger(final int pOffsetMilliseconds) { 085 periodMilliseconds = pOffsetMilliseconds; 086 return this; 087 } 088 089 /** 090 * Sets the count trigger for aggregation. Not required. 091 */ 092 public AggregatePolicyBuilder setCountTrigger(final int pCount) { 093 count = Optional.of(pCount); 094 return this; 095 } 096 097 /** 098 * Builds and returns a new {@link VortexAggregatePolicy} based on user's specification. 099 * The timer period is a required parameter for this to succeed. 100 * @throws IllegalArgumentException if required parameters are not set or if parameters are invalid. 101 */ 102 @Override 103 public VortexAggregatePolicy build() throws IllegalArgumentException { 104 if (periodMilliseconds == null) { 105 throw new IllegalArgumentException("The aggregate period must be set."); 106 } 107 108 if (count.isPresent() && count.get() <= 0) { 109 throw new IllegalArgumentException("The count trigger must be greater than zero."); 110 } 111 112 return new VortexAggregatePolicy(periodMilliseconds, count); 113 } 114 } 115}