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 */
018package org.apache.hadoop.hbase.client;
019
020import org.apache.hadoop.hbase.ipc.RpcCallContext;
021import org.apache.hadoop.hbase.ipc.RpcServer;
022import org.apache.yetus.audience.InterfaceAudience;
023
024import org.apache.hadoop.hbase.shaded.protobuf.generated.HBaseProtos;
025
026/**
027 * Class to help with parsing the version info.
028 */
029@InterfaceAudience.Private
030public final class VersionInfoUtil {
031
032  private VersionInfoUtil() {
033    /* UTIL CLASS ONLY */
034  }
035
036  public static boolean currentClientHasMinimumVersion(int major, int minor) {
037    return hasMinimumVersion(getCurrentClientVersionInfo(), major, minor);
038  }
039
040  public static boolean hasMinimumVersion(HBaseProtos.VersionInfo versionInfo, int major,
041    int minor) {
042    if (versionInfo != null) {
043      if (versionInfo.hasVersionMajor() && versionInfo.hasVersionMinor()) {
044        int clientMajor = versionInfo.getVersionMajor();
045        if (clientMajor != major) {
046          return clientMajor > major;
047        }
048        int clientMinor = versionInfo.getVersionMinor();
049        return clientMinor >= minor;
050      }
051      try {
052        final String[] components = getVersionComponents(versionInfo);
053
054        int clientMajor = components.length > 0 ? Integer.parseInt(components[0]) : 0;
055        if (clientMajor != major) {
056          return clientMajor > major;
057        }
058
059        int clientMinor = components.length > 1 ? Integer.parseInt(components[1]) : 0;
060        return clientMinor >= minor;
061      } catch (NumberFormatException e) {
062        return false;
063      }
064    }
065    return false;
066  }
067
068  /** Returns the versionInfo extracted from the current RpcCallContext */
069  public static HBaseProtos.VersionInfo getCurrentClientVersionInfo() {
070    return RpcServer.getCurrentCall().map(RpcCallContext::getClientVersionInfo).orElse(null);
071  }
072
073  /**
074   * n * @return the passed-in <code>version</code> int as a version String (e.g. 0x0103004 is
075   * 1.3.4)
076   */
077  public static String versionNumberToString(final int version) {
078    return String.format("%d.%d.%d", ((version >> 20) & 0xff), ((version >> 12) & 0xff),
079      (version & 0xfff));
080  }
081
082  /**
083   * Pack the full number version in a int. by shifting each component by 8bit, except the dot
084   * release which has 12bit. Examples: (1.3.4 is 0x0103004, 2.1.0 is 0x0201000)
085   * @param versionInfo the VersionInfo object to pack
086   * @return the version number as int. (e.g. 0x0103004 is 1.3.4)
087   */
088  public static int getVersionNumber(final HBaseProtos.VersionInfo versionInfo) {
089    if (versionInfo != null) {
090      try {
091        final String[] components = getVersionComponents(versionInfo);
092        int clientMajor = components.length > 0 ? Integer.parseInt(components[0]) : 0;
093        int clientMinor = components.length > 1 ? Integer.parseInt(components[1]) : 0;
094        int clientPatch = components.length > 2 ? Integer.parseInt(components[2]) : 0;
095        return buildVersionNumber(clientMajor, clientMinor, clientPatch);
096      } catch (NumberFormatException e) {
097        int clientMajor = versionInfo.hasVersionMajor() ? versionInfo.getVersionMajor() : 0;
098        int clientMinor = versionInfo.hasVersionMinor() ? versionInfo.getVersionMinor() : 0;
099        return buildVersionNumber(clientMajor, clientMinor, 0);
100      }
101    }
102    return (0); // no version
103  }
104
105  /**
106   * Pack the full number version in a int. by shifting each component by 8bit, except the dot
107   * release which has 12bit. Examples: (1.3.4 is 0x0103004, 2.1.0 is 0x0201000)
108   * @param major version major number
109   * @param minor version minor number
110   * @param patch version patch number
111   * @return the version number as int. (e.g. 0x0103004 is 1.3.4)
112   */
113  private static int buildVersionNumber(int major, int minor, int patch) {
114    return (major << 20) | (minor << 12) | patch;
115  }
116
117  /**
118   * Returns the version components Examples: "1.4.3" returns [1, 4, 3], "4.5.6-SNAPSHOT" returns
119   * [4, 5, 6, "SNAPSHOT"]
120   * @return the components of the version string
121   */
122  private static String[] getVersionComponents(final HBaseProtos.VersionInfo versionInfo) {
123    return versionInfo.getVersion().split("[\\.-]");
124  }
125}