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.catalog; 020 021import org.apache.reef.annotations.audience.Private; 022import org.apache.reef.driver.catalog.NodeDescriptor; 023import org.apache.reef.driver.catalog.RackDescriptor; 024import org.apache.reef.driver.catalog.ResourceCatalog; 025import org.apache.reef.runtime.common.driver.resourcemanager.NodeDescriptorEvent; 026 027import javax.inject.Inject; 028import java.net.InetSocketAddress; 029import java.util.*; 030import java.util.logging.Level; 031import java.util.logging.Logger; 032 033/** 034 * A catalog of the resources available to a REEF instance. 035 */ 036@Private 037public final class ResourceCatalogImpl implements ResourceCatalog { 038 039 public static final String DEFAULT_RACK = "/default-rack"; 040 private static final Logger LOG = Logger.getLogger(ResourceCatalog.class.getName()); 041 private final Map<String, RackDescriptorImpl> racks = new HashMap<>(); 042 043 private final Map<String, NodeDescriptorImpl> nodes = new HashMap<>(); 044 045 @Inject 046 ResourceCatalogImpl() { 047 LOG.log(Level.FINE, "Instantiated 'ResourceCatalogImpl'"); 048 } 049 050 @Override 051 public synchronized String toString() { 052 final StringBuilder sb = new StringBuilder(); 053 sb.append("=== Resource Catalog ==="); 054 for (final RackDescriptor rack : racks.values()) { 055 sb.append("\n" + rack); 056 } 057 return sb.toString(); 058 } 059 060 @Override 061 public synchronized Collection<NodeDescriptor> getNodes() { 062 return Collections.unmodifiableCollection(new ArrayList<NodeDescriptor>(this.nodes.values())); 063 } 064 065 @Override 066 public synchronized Collection<RackDescriptor> getRacks() { 067 return Collections.unmodifiableCollection(new ArrayList<RackDescriptor>(this.racks.values())); 068 } 069 070 public synchronized NodeDescriptor getNode(final String id) { 071 return this.nodes.get(id); 072 } 073 074 public synchronized void handle(final NodeDescriptorEvent node) { 075 final String rackName = node.getRackName().orElse(DEFAULT_RACK); 076 077 LOG.log(Level.FINEST, "Catalog new node: id[{0}], rack[{1}], host[{2}], port[{3}], memory[{4}]", 078 new Object[]{node.getIdentifier(), rackName, node.getHostName(), node.getPort(), 079 node.getMemorySize()} 080 ); 081 082 if (!this.racks.containsKey(rackName)) { 083 final RackDescriptorImpl rack = new RackDescriptorImpl(rackName); 084 this.racks.put(rackName, rack); 085 } 086 final RackDescriptorImpl rack = this.racks.get(rackName); 087 final InetSocketAddress address = new InetSocketAddress(node.getHostName(), node.getPort()); 088 final NodeDescriptorImpl nodeDescriptor = new NodeDescriptorImpl(node.getIdentifier(), address, rack, 089 node.getMemorySize()); 090 this.nodes.put(nodeDescriptor.getId(), nodeDescriptor); 091 } 092}