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, software
013 * distributed under the License is distributed on an "AS IS" BASIS,
014 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
015 * See the License for the specific language governing permissions and
016 * limitations under the License.
017 */
018
019package org.apache.hadoop.hdfs.server.protocol;
020
021import java.io.IOException;
022
023import org.apache.hadoop.classification.InterfaceAudience;
024import org.apache.hadoop.classification.InterfaceStability;
025import org.apache.hadoop.hdfs.protocol.HdfsConstants;
026import org.apache.hadoop.hdfs.server.common.Storage;
027import org.apache.hadoop.hdfs.server.common.StorageInfo;
028import org.apache.hadoop.hdfs.server.common.HdfsServerConstants.NodeType;
029import org.apache.hadoop.hdfs.server.namenode.NNStorage;
030import org.apache.hadoop.util.VersionInfo;
031
032import com.google.common.annotations.VisibleForTesting;
033import com.google.common.base.Preconditions;
034
035/**
036 * NamespaceInfo is returned by the name-node in reply 
037 * to a data-node handshake.
038 * 
039 */
040@InterfaceAudience.Private
041@InterfaceStability.Evolving
042public class NamespaceInfo extends StorageInfo {
043  final String  buildVersion;
044  String blockPoolID = "";    // id of the block pool
045  String softwareVersion;
046  long capabilities;
047
048  // only authoritative on the server-side to determine advertisement to
049  // clients.  enum will update the supported values
050  private static long CAPABILITIES_SUPPORTED = 0;
051
052  public enum Capability {
053    UNKNOWN(false),
054    STORAGE_BLOCK_REPORT_BUFFERS(true); // use optimized ByteString buffers
055    private final long mask;
056    Capability(boolean isSupported) {
057      int bits = ordinal() - 1;
058      mask = (bits < 0) ? 0 : (1L << bits);
059      if (isSupported) {
060        CAPABILITIES_SUPPORTED |= mask;
061      }
062    }
063    public long getMask() {
064      return mask;
065    }
066  }
067
068  // defaults to enabled capabilites since this ctor is for server
069  public NamespaceInfo() {
070    super(NodeType.NAME_NODE);
071    buildVersion = null;
072    capabilities = CAPABILITIES_SUPPORTED;
073  }
074
075  // defaults to enabled capabilites since this ctor is for server
076  public NamespaceInfo(int nsID, String clusterID, String bpID,
077      long cT, String buildVersion, String softwareVersion) {
078    this(nsID, clusterID, bpID, cT, buildVersion, softwareVersion,
079        CAPABILITIES_SUPPORTED);
080  }
081
082  // for use by server and/or client
083  public NamespaceInfo(int nsID, String clusterID, String bpID,
084      long cT, String buildVersion, String softwareVersion,
085      long capabilities) {
086    super(HdfsConstants.NAMENODE_LAYOUT_VERSION, nsID, clusterID, cT,
087        NodeType.NAME_NODE);
088    blockPoolID = bpID;
089    this.buildVersion = buildVersion;
090    this.softwareVersion = softwareVersion;
091    this.capabilities = capabilities;
092  }
093
094  public NamespaceInfo(int nsID, String clusterID, String bpID, 
095      long cT) {
096    this(nsID, clusterID, bpID, cT, Storage.getBuildVersion(),
097        VersionInfo.getVersion());
098  }
099  
100  public long getCapabilities() {
101    return capabilities;
102  }
103
104  @VisibleForTesting
105  public void setCapabilities(long capabilities) {
106    this.capabilities = capabilities;
107  }
108
109  public boolean isCapabilitySupported(Capability capability) {
110    Preconditions.checkArgument(capability != Capability.UNKNOWN,
111        "cannot test for unknown capability");
112    long mask = capability.getMask();
113    return (capabilities & mask) == mask;
114  }
115
116  public String getBuildVersion() {
117    return buildVersion;
118  }
119
120  public String getBlockPoolID() {
121    return blockPoolID;
122  }
123  
124  public String getSoftwareVersion() {
125    return softwareVersion;
126  }
127
128  @Override
129  public String toString(){
130    return super.toString() + ";bpid=" + blockPoolID;
131  }
132
133  public void validateStorage(NNStorage storage) throws IOException {
134    if (layoutVersion != storage.getLayoutVersion() ||
135        namespaceID != storage.getNamespaceID() ||
136        cTime != storage.cTime ||
137        !clusterID.equals(storage.getClusterID()) ||
138        !blockPoolID.equals(storage.getBlockPoolID())) {
139      throw new IOException("Inconsistent namespace information:\n" +
140          "NamespaceInfo has:\n" +
141          "LV=" + layoutVersion + ";" +
142          "NS=" + namespaceID + ";" +
143          "cTime=" + cTime + ";" +
144          "CID=" + clusterID + ";" +
145          "BPID=" + blockPoolID +
146          ".\nStorage has:\n" +
147          "LV=" + storage.getLayoutVersion() + ";" +
148          "NS=" + storage.getNamespaceID() + ";" +
149          "cTime=" + storage.getCTime() + ";" +
150          "CID=" + storage.getClusterID() + ";" +
151          "BPID=" + storage.getBlockPoolID() + ".");
152    }
153  }
154}