This project has retired. For details please refer to its Attic page.
Source code
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.proto.DriverRuntimeProtocol.NodeDescriptorProto;
026
027import javax.inject.Inject;
028import java.net.InetSocketAddress;
029import java.util.*;
030import java.util.logging.Level;
031import java.util.logging.Logger;
032
033@Private
034public final class ResourceCatalogImpl implements ResourceCatalog {
035
036  public final static String DEFAULT_RACK = "/default-rack";
037  private static final Logger LOG = Logger.getLogger(ResourceCatalog.class.getName());
038  private final Map<String, RackDescriptorImpl> racks = new HashMap<>();
039
040  private final Map<String, NodeDescriptorImpl> nodes = new HashMap<>();
041
042  @Inject
043  ResourceCatalogImpl() {
044    LOG.log(Level.FINE, "Instantiated 'ResourceCatalogImpl'");
045  }
046
047  @Override
048  public synchronized String toString() {
049    final StringBuilder sb = new StringBuilder();
050    sb.append("=== Resource Catalog ===");
051    for (final RackDescriptor rack : racks.values()) {
052      sb.append("\n" + rack);
053    }
054    return sb.toString();
055  }
056
057  @Override
058  public synchronized Collection<NodeDescriptor> getNodes() {
059    return Collections.unmodifiableCollection(new ArrayList<NodeDescriptor>(this.nodes.values()));
060  }
061
062  @Override
063  public synchronized Collection<RackDescriptor> getRacks() {
064    return Collections.unmodifiableCollection(new ArrayList<RackDescriptor>(this.racks.values()));
065  }
066
067  public synchronized final NodeDescriptor getNode(final String id) {
068    return this.nodes.get(id);
069  }
070
071  public synchronized final void handle(final NodeDescriptorProto node) {
072    final String rack_name = (node.hasRackName() ? node.getRackName() : DEFAULT_RACK);
073
074    LOG.log(Level.FINEST, "Catalog new node: id[{0}], rack[{1}], host[{2}], port[{3}], memory[{4}]",
075        new Object[]{node.getIdentifier(), rack_name, node.getHostName(), node.getPort(),
076            node.getMemorySize()}
077    );
078
079    if (!this.racks.containsKey(rack_name)) {
080      final RackDescriptorImpl rack = new RackDescriptorImpl(rack_name);
081      this.racks.put(rack_name, rack);
082    }
083    final RackDescriptorImpl rack = this.racks.get(rack_name);
084    final InetSocketAddress address = new InetSocketAddress(node.getHostName(), node.getPort());
085    final NodeDescriptorImpl nodeDescriptor = new NodeDescriptorImpl(node.getIdentifier(), address, rack, node.getMemorySize());
086    this.nodes.put(nodeDescriptor.getId(), nodeDescriptor);
087  }
088}