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.util.BuilderUtils; 022import org.apache.reef.util.Optional; 023 024/** 025 * Default POJO implementation of ResourceAllocationEvent and ResourceRecoverEvent. 026 * Use newAllocationBuilder to construct an instance for ResourceAllocationEvent and 027 * use newRecoveryBuilder to construct an instance for ResourceRecoverEvent. 028 */ 029public final class ResourceEventImpl implements ResourceAllocationEvent, ResourceRecoverEvent { 030 private final String identifier; 031 private final int resourceMemory; 032 private final String nodeId; 033 private final Optional<Integer> virtualCores; 034 private final Optional<String> rackName; 035 private final String runtimeName; 036 037 038 private ResourceEventImpl(final Builder builder) { 039 this.identifier = BuilderUtils.notNull(builder.identifier); 040 this.resourceMemory = builder.recovery ? builder.resourceMemory : BuilderUtils.notNull(builder.resourceMemory); 041 this.nodeId = builder.recovery ? builder.nodeId : BuilderUtils.notNull(builder.nodeId); 042 this.virtualCores = Optional.ofNullable(builder.virtualCores); 043 this.rackName = Optional.ofNullable(builder.rackName); 044 this.runtimeName = BuilderUtils.notNull(builder.runtimeName); 045 } 046 047 @Override 048 public String getIdentifier() { 049 return identifier; 050 } 051 052 @Override 053 public int getResourceMemory() { 054 return resourceMemory; 055 } 056 057 @Override 058 public String getNodeId() { 059 return nodeId; 060 } 061 062 @Override 063 public Optional<Integer> getVirtualCores() { 064 return virtualCores; 065 } 066 067 @Override 068 public Optional<String> getRackName() { 069 return rackName; 070 } 071 072 @Override 073 public String getRuntimeName() { 074 return runtimeName; 075 } 076 077 public static Builder newAllocationBuilder() { 078 return new Builder(false); 079 } 080 081 public static Builder newRecoveryBuilder() { 082 return new Builder(true); 083 } 084 085 /** 086 * Builder used to create ResourceAllocationEvent instances. 087 */ 088 public static final class Builder implements org.apache.reef.util.Builder<ResourceEventImpl> { 089 private final boolean recovery; 090 091 private String identifier; 092 private Integer resourceMemory; 093 private String nodeId; 094 private Integer virtualCores; 095 private String rackName; 096 private String runtimeName; 097 098 private Builder(final boolean recovery){ 099 this.recovery = recovery; 100 } 101 102 /** 103 * @see ResourceAllocationEvent#getIdentifier() 104 */ 105 public Builder setIdentifier(final String identifier) { 106 this.identifier = identifier; 107 return this; 108 } 109 110 /** 111 * @see ResourceAllocationEvent#getResourceMemory() 112 */ 113 public Builder setResourceMemory(final int resourceMemory) { 114 this.resourceMemory = resourceMemory; 115 return this; 116 } 117 118 /** 119 * @see ResourceAllocationEvent#getNodeId() 120 */ 121 public Builder setNodeId(final String nodeId) { 122 this.nodeId = nodeId; 123 return this; 124 } 125 126 /** 127 * @see ResourceAllocationEvent#getVirtualCores() 128 */ 129 public Builder setVirtualCores(final int virtualCores) { 130 this.virtualCores = virtualCores; 131 return this; 132 } 133 134 /** 135 * @see ResourceAllocationEvent#getRackName() 136 */ 137 public Builder setRackName(final String rackName) { 138 this.rackName = rackName; 139 return this; 140 } 141 142 /** 143 * @see ResourceAllocationEvent#getRuntimeName() 144 */ 145 public Builder setRuntimeName(final String runtimeName) { 146 this.runtimeName = runtimeName; 147 return this; 148 } 149 150 @Override 151 public ResourceEventImpl build() { 152 return new ResourceEventImpl(this); 153 } 154 } 155}