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.runtime.common.driver.resourcemanager; 020 021import org.apache.reef.proto.ReefServiceProtos; 022import org.apache.reef.runtime.common.driver.evaluator.pojos.State; 023import org.apache.reef.util.BuilderUtils; 024import org.apache.reef.util.Optional; 025 026import java.util.ArrayList; 027import java.util.List; 028 029/** 030 * Default POJO implementation of RuntimeStatusEvent. 031 * Use newBuilder to construct an instance. 032 */ 033public final class RuntimeStatusEventImpl implements RuntimeStatusEvent { 034 035 private final String name; 036 private final State state; 037 private final List<String> containerAllocationList; 038 private final Optional<ReefServiceProtos.RuntimeErrorProto> error; 039 private final Optional<Integer> outstandingContainerRequests; 040 041 private RuntimeStatusEventImpl(final Builder builder) { 042 this.name = BuilderUtils.notNull(builder.name); 043 this.state = BuilderUtils.notNull(builder.state); 044 this.containerAllocationList = BuilderUtils.notNull(builder.containerAllocationList); 045 this.error = Optional.ofNullable(builder.error); 046 this.outstandingContainerRequests = Optional.ofNullable(builder.outstandingContainerRequests); 047 } 048 049 @Override 050 public String toString() { 051 052 // Replace with String.join() after migration to Java 1.8 053 final StringBuilder allocatedContainers = new StringBuilder(); 054 for (String container : this.containerAllocationList) { 055 if (allocatedContainers.length() > 0) { 056 allocatedContainers.append(','); 057 } 058 allocatedContainers.append(container); 059 } 060 061 return String.format( 062 "RuntimeStatusEventImpl:{name:%s, state:%s, allocated:[%s], outstanding:%d, error:%s}", 063 this.name, this.state, allocatedContainers, this.outstandingContainerRequests.orElse(0), 064 this.error.isPresent()); 065 } 066 067 @Override 068 public String getName() { 069 return name; 070 } 071 072 @Override 073 public State getState() { 074 return state; 075 } 076 077 @Override 078 public List<String> getContainerAllocationList() { 079 return containerAllocationList; 080 } 081 082 @Override 083 public Optional<ReefServiceProtos.RuntimeErrorProto> getError() { 084 return error; 085 } 086 087 @Override 088 public Optional<Integer> getOutstandingContainerRequests() { 089 return outstandingContainerRequests; 090 } 091 092 public static Builder newBuilder() { 093 return new Builder(); 094 } 095 096 /** 097 * Builder used to create RuntimeStatusEvent instances. 098 */ 099 public static final class Builder implements org.apache.reef.util.Builder<RuntimeStatusEvent> { 100 private String name; 101 private State state; 102 private List<String> containerAllocationList = new ArrayList<>(); 103 private ReefServiceProtos.RuntimeErrorProto error; 104 private Integer outstandingContainerRequests; 105 106 /** 107 * @see RuntimeStatusEvent#getName() 108 */ 109 public Builder setName(final String name) { 110 this.name = name; 111 return this; 112 } 113 114 /** 115 * @see RuntimeStatusEvent#getState() 116 */ 117 public Builder setState(final State state) { 118 this.state = state; 119 return this; 120 } 121 122 /** 123 * Add an entry to containerAllocationList. 124 * @see RuntimeStatusEvent#getContainerAllocationList() 125 */ 126 public Builder addContainerAllocation(final String containerAllocation) { 127 this.containerAllocationList.add(containerAllocation); 128 return this; 129 } 130 131 /** 132 * @see RuntimeStatusEvent#getError() 133 */ 134 public Builder setError(final ReefServiceProtos.RuntimeErrorProto error) { 135 this.error = error; 136 return this; 137 } 138 139 /** 140 * @see RuntimeStatusEvent#getOutstandingContainerRequests() 141 */ 142 public Builder setOutstandingContainerRequests(final int outstandingContainerRequests) { 143 this.outstandingContainerRequests = outstandingContainerRequests; 144 return this; 145 } 146 147 @Override 148 public RuntimeStatusEvent build() { 149 return new RuntimeStatusEventImpl(this); 150 } 151 } 152}