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 * Returns the passed-in <code>version</code> int as a version String (e.g. 0x0103004 is 1.3.4) 075 */ 076 public static String versionNumberToString(final int version) { 077 return String.format("%d.%d.%d", ((version >> 20) & 0xff), ((version >> 12) & 0xff), 078 (version & 0xfff)); 079 } 080 081 /** 082 * Pack the full number version in a int. by shifting each component by 8bit, except the dot 083 * release which has 12bit. Examples: (1.3.4 is 0x0103004, 2.1.0 is 0x0201000) 084 * @param versionInfo the VersionInfo object to pack 085 * @return the version number as int. (e.g. 0x0103004 is 1.3.4) 086 */ 087 public static int getVersionNumber(final HBaseProtos.VersionInfo versionInfo) { 088 if (versionInfo != null) { 089 try { 090 final String[] components = getVersionComponents(versionInfo); 091 int clientMajor = components.length > 0 ? Integer.parseInt(components[0]) : 0; 092 int clientMinor = components.length > 1 ? Integer.parseInt(components[1]) : 0; 093 int clientPatch = components.length > 2 ? Integer.parseInt(components[2]) : 0; 094 return buildVersionNumber(clientMajor, clientMinor, clientPatch); 095 } catch (NumberFormatException e) { 096 int clientMajor = versionInfo.hasVersionMajor() ? versionInfo.getVersionMajor() : 0; 097 int clientMinor = versionInfo.hasVersionMinor() ? versionInfo.getVersionMinor() : 0; 098 return buildVersionNumber(clientMajor, clientMinor, 0); 099 } 100 } 101 return (0); // no version 102 } 103 104 /** 105 * Pack the full number version in a int. by shifting each component by 8bit, except the dot 106 * release which has 12bit. Examples: (1.3.4 is 0x0103004, 2.1.0 is 0x0201000) 107 * @param major version major number 108 * @param minor version minor number 109 * @param patch version patch number 110 * @return the version number as int. (e.g. 0x0103004 is 1.3.4) 111 */ 112 private static int buildVersionNumber(int major, int minor, int patch) { 113 return (major << 20) | (minor << 12) | patch; 114 } 115 116 /** 117 * Returns the version components Examples: "1.4.3" returns [1, 4, 3], "4.5.6-SNAPSHOT" returns 118 * [4, 5, 6, "SNAPSHOT"] 119 * @return the components of the version string 120 */ 121 private static String[] getVersionComponents(final HBaseProtos.VersionInfo versionInfo) { 122 return versionInfo.getVersion().split("[\\.-]"); 123 } 124}