Start line:  
End line:  

Snippet Preview

Snippet HTML Code

Stack Overflow Questions
  /*
   * Licensed under the Apache License, Version 2.0 (the "License");
   * you may not use this file except in compliance with the License.
   * You may obtain a copy of the License at
   *
   *     http://www.apache.org/licenses/LICENSE-2.0
   *
   * Unless required by applicable law or agreed to in writing, software
   * distributed under the License is distributed on an "AS IS" BASIS,
  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  * See the License for the specific language governing permissions and
  * limitations under the License.
  */
 package com.facebook.presto.metadata;
 
 
 
 import java.net.URI;
 import java.util.Set;
 
 import static com.facebook.presto.util.ImmutableCollectors.toImmutableSet;
 import static com.google.common.base.Preconditions.checkNotNull;
 import static com.google.common.base.Preconditions.checkState;
 import static java.util.Arrays.asList;
 import static java.util.Locale.ENGLISH;
 
 public final class DiscoveryNodeManager
         implements InternalNodeManager
 {
     private static final Duration MAX_AGE = new Duration(5, .);
 
     private static final Splitter DATASOURCES_SPLITTER = Splitter.on(',').trimResults().omitEmptyStrings();
     private final ServiceSelector serviceSelector;
     private final NodeInfo nodeInfo;
     private final FailureDetector failureDetector;
     private final NodeVersion expectedNodeVersion;
 
     @GuardedBy("this")
 
     @GuardedBy("this")
     private AllNodes allNodes;
 
     @GuardedBy("this")
     private long lastUpdateTimestamp;
 
     @GuardedBy("this")
     private PrestoNode currentNode;
 
     @GuardedBy("this")
     private Set<Nodecoordinators;
 
     @Inject
     public DiscoveryNodeManager(@ServiceType("presto"ServiceSelector serviceSelectorNodeInfo nodeInfoFailureDetector failureDetectorNodeVersion expectedNodeVersion)
     {
         this. = checkNotNull(serviceSelector"serviceSelector is null");
         this. = checkNotNull(nodeInfo"nodeInfo is null");
         this. = checkNotNull(failureDetector"failureDetector is null");
         this. = checkNotNull(expectedNodeVersion"expectedNodeVersion is null");
 
         refreshNodes();
     }
 
     @Override
     public synchronized void refreshNodes()
     {
          = System.nanoTime();
 
         // This is currently a blacklist.
         // TODO: make it a whitelist (a failure-detecting service selector) and maybe build in support for injecting this in airlift
         Set<ServiceDescriptorservices = .selectAllServices().stream()
                 .filter(service -> !.getFailed().contains(service))
                 .collect(toImmutableSet());
 
         // reset current node
          = null;
 
         ImmutableSet.Builder<NodeactiveNodesBuilder = ImmutableSet.builder();
         ImmutableSet.Builder<NodeinactiveNodesBuilder = ImmutableSet.builder();
         ImmutableSet.Builder<NodecoordinatorsBuilder = ImmutableSet.builder();
         ImmutableSetMultimap.Builder<StringNodebyDataSourceBuilder = ImmutableSetMultimap.builder();
        for (ServiceDescriptor service : services) {
            URI uri = getHttpUri(service);
            NodeVersion nodeVersion = getNodeVersion(service);
            if (uri != null && nodeVersion != null) {
                PrestoNode node = new PrestoNode(service.getNodeId(), urinodeVersion);
                // record current node
                if (node.getNodeIdentifier().equals(.getNodeId())) {
                     = node;
                    checkState(.getNodeVersion().equals(), "INVARIANT: current node version should be equal to expected node version");
                }
                if (isActive(node)) {
                    activeNodesBuilder.add(node);
                    if (Boolean.parseBoolean(service.getProperties().get("coordinator"))) {
                        coordinatorsBuilder.add(node);
                    }
                    // record available active nodes organized by data source
                    String dataSources = service.getProperties().get("datasources");
                    if (dataSources != null) {
                        dataSources = dataSources.toLowerCase();
                        for (String dataSource : .split(dataSources)) {
                            byDataSourceBuilder.put(dataSourcenode);
                        }
                    }
                    // always add system data source
                    byDataSourceBuilder.put(.node);
                }
                else {
                    inactiveNodesBuilder.add(node);
                }
            }
        }
         = new AllNodes(activeNodesBuilder.build(), inactiveNodesBuilder.build());
         = byDataSourceBuilder.build();
         = coordinatorsBuilder.build();
        checkState( != null"INVARIANT: current node not returned from service selector");
    }
    private synchronized void refreshIfNecessary()
    {
        if (Duration.nanosSince().compareTo() > 0) {
            refreshNodes();
        }
    }
    private boolean isActive(PrestoNode node)
    {
        return .equals(node.getNodeVersion());
    }
    @Override
    public synchronized AllNodes getAllNodes()
    {
        refreshIfNecessary();
        return ;
    }
    @Override
    public Set<NodegetActiveNodes()
    {
        return getAllNodes().getActiveNodes();
    }
    @Override
    public synchronized Set<NodegetActiveDatasourceNodes(String datasourceName)
    {
        refreshIfNecessary();
        return .get(datasourceName);
    }
    @Override
    public synchronized Node getCurrentNode()
    {
        refreshIfNecessary();
        return ;
    }
    @Override
    public synchronized Set<NodegetCoordinators()
    {
        refreshIfNecessary();
        return ;
    }
    private static URI getHttpUri(ServiceDescriptor descriptor)
    {
        // favor https over http
        for (String type : asList("https""http")) {
            String url = descriptor.getProperties().get(type);
            if (url != null) {
                try {
                    return new URI(url);
                }
                catch (URISyntaxException ignored) {
                }
            }
        }
        return null;
    }
    private static NodeVersion getNodeVersion(ServiceDescriptor descriptor)
    {
        String nodeVersion = descriptor.getProperties().get("node_version");
        return nodeVersion == null ? null : new NodeVersion(nodeVersion);
    }
New to GrepCode? Check out our FAQ X