kudu-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From mpe...@apache.org
Subject [3/4] kudu git commit: [tools] Add simple util for version parsing
Date Thu, 14 Jun 2018 23:40:58 GMT
[tools] Add simple util for version parsing

This adds a struct and parsing function to parse versions of the form

<major>.<minor>.<maintenance>-<extra>

This can be used along with ksck's version reporting or ad hoc
version-gathering to determine how a tool should function when operating
against a cluster running certain Kudu versions.

Its first application will be in a follow-up.

Change-Id: I0bcf3b10b699e89ad489144611990702dec8af19
Reviewed-on: http://gerrit.cloudera.org:8080/10698
Tested-by: Will Berkeley <wdberkeley@gmail.com>
Reviewed-by: Adar Dembo <adar@cloudera.com>


Project: http://git-wip-us.apache.org/repos/asf/kudu/repo
Commit: http://git-wip-us.apache.org/repos/asf/kudu/commit/c12e6352
Tree: http://git-wip-us.apache.org/repos/asf/kudu/tree/c12e6352
Diff: http://git-wip-us.apache.org/repos/asf/kudu/diff/c12e6352

Branch: refs/heads/master
Commit: c12e63528a2f2c265136e702a48cbc6c93b7f2fd
Parents: 5e70553
Author: Will Berkeley <wdberkeley@apache.org>
Authored: Mon Jun 11 16:49:43 2018 -0700
Committer: Will Berkeley <wdberkeley@gmail.com>
Committed: Thu Jun 14 19:55:57 2018 +0000

----------------------------------------------------------------------
 src/kudu/util/CMakeLists.txt       |  2 +
 src/kudu/util/version_util-test.cc | 66 ++++++++++++++++++++++++++
 src/kudu/util/version_util.cc      | 83 +++++++++++++++++++++++++++++++++
 src/kudu/util/version_util.h       | 58 +++++++++++++++++++++++
 4 files changed, 209 insertions(+)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/kudu/blob/c12e6352/src/kudu/util/CMakeLists.txt
----------------------------------------------------------------------
diff --git a/src/kudu/util/CMakeLists.txt b/src/kudu/util/CMakeLists.txt
index afaef7d..044660c 100644
--- a/src/kudu/util/CMakeLists.txt
+++ b/src/kudu/util/CMakeLists.txt
@@ -199,6 +199,7 @@ set(UTIL_SRCS
   user.cc
   url-coding.cc
   version_info.cc
+  version_util.cc
   website_util.cc
   zlib.cc
 )
@@ -403,6 +404,7 @@ ADD_KUDU_TEST(throttler-test)
 ADD_KUDU_TEST(trace-test PROCESSORS 4)
 ADD_KUDU_TEST(url-coding-test)
 ADD_KUDU_TEST(user-test)
+ADD_KUDU_TEST(version_util-test)
 
 if (NOT APPLE)
   ADD_KUDU_TEST(minidump-test)

http://git-wip-us.apache.org/repos/asf/kudu/blob/c12e6352/src/kudu/util/version_util-test.cc
----------------------------------------------------------------------
diff --git a/src/kudu/util/version_util-test.cc b/src/kudu/util/version_util-test.cc
new file mode 100644
index 0000000..54e8e76
--- /dev/null
+++ b/src/kudu/util/version_util-test.cc
@@ -0,0 +1,66 @@
+// Licensed to the Apache Software Foundation (ASF) under one
+// or more contributor license agreements.  See the NOTICE file
+// distributed with this work for additional information
+// regarding copyright ownership.  The ASF licenses this file
+// to you 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.
+#include "kudu/util/version_util.h"
+
+#include <string>
+#include <vector>
+
+#include <gtest/gtest.h>
+
+#include "kudu/util/status.h"
+#include "kudu/util/test_macros.h"
+#include "kudu/util/test_util.h"
+
+using std::string;
+using std::vector;
+
+namespace kudu {
+
+class VersionUtilTest : public KuduTest {};
+
+TEST_F(VersionUtilTest, TestVersion) {
+  const vector<Version> good_test_cases = {
+    { "0.0.0", 0, 0, 0, "" },
+    { "1.0.0", 1, 0, 0, "" },
+    { "1.1.0", 1, 1, 0, "" },
+    { "1.1.1", 1, 1, 1, "" },
+    { "1.10.100-1000", 1, 10, 100, "1000" },
+    { "1.2.3-SNAPSHOT", 1, 2, 3, "SNAPSHOT" },
+  };
+
+  Version v;
+  for (const auto& test_case : good_test_cases) {
+    ASSERT_OK(ParseVersion(test_case.raw_version, &v));
+    EXPECT_EQ(test_case, v);
+  }
+
+  const vector<string> bad_test_cases = {
+    "",
+    "foo",
+    "foo.1.0",
+    "1.bar.0",
+    "1.0.foo",
+    "1.0foo.bar",
+    "foo5-1.4.3",
+  };
+
+  for (const auto& test_case : bad_test_cases) {
+    ASSERT_TRUE(ParseVersion(test_case, &v).IsInvalidArgument());
+  }
+}
+
+} // namespace kudu

http://git-wip-us.apache.org/repos/asf/kudu/blob/c12e6352/src/kudu/util/version_util.cc
----------------------------------------------------------------------
diff --git a/src/kudu/util/version_util.cc b/src/kudu/util/version_util.cc
new file mode 100644
index 0000000..bd298f8
--- /dev/null
+++ b/src/kudu/util/version_util.cc
@@ -0,0 +1,83 @@
+// Licensed to the Apache Software Foundation (ASF) under one
+// or more contributor license agreements.  See the NOTICE file
+// distributed with this work for additional information
+// regarding copyright ownership.  The ASF licenses this file
+// to you 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.
+
+#include "kudu/util/version_util.h"
+
+#include <iostream>
+#include <string>
+#include <utility>
+#include <vector>
+
+#include <glog/logging.h>
+
+#include "kudu/gutil/strings/numbers.h"
+#include "kudu/gutil/strings/split.h"
+#include "kudu/gutil/strings/substitute.h"
+#include "kudu/util/status.h"
+
+using std::ostream;
+using std::string;
+using std::vector;
+using strings::Split;
+using strings::Substitute;
+
+namespace kudu {
+
+bool Version::operator==(const Version& other) const {
+  return this->major == other.major &&
+         this->minor == other.minor &&
+         this->maintenance == other.maintenance &&
+         this->extra == other.extra;
+}
+
+string Version::ToString() const {
+  return raw_version;
+}
+
+ostream& operator<<(ostream& os, const Version& v) {
+  return os << v.ToString();
+}
+
+Status ParseVersion(const string& version_str,
+                    Version* v) {
+  CHECK(v);
+  const Status invalid_ver_err =
+      Status::InvalidArgument("invalid version string", version_str);
+  Version temp_v;
+  const vector<string> main_and_extra = Split(version_str, "-");
+  if (main_and_extra.empty() || main_and_extra.size() > 2) {
+    return invalid_ver_err;
+  }
+  if (main_and_extra.size() == 2) {
+    temp_v.extra = main_and_extra[1];
+  }
+  const auto& main_ver_str = main_and_extra[0];
+  const vector<string> maj_min_maint = Split(main_ver_str, ".");
+  if (maj_min_maint.size() != 3) {
+    return invalid_ver_err;
+  }
+  if (!SimpleAtoi(maj_min_maint[0], &temp_v.major) ||
+      !SimpleAtoi(maj_min_maint[1], &temp_v.minor) ||
+      !SimpleAtoi(maj_min_maint[2], &temp_v.maintenance)) {
+    return invalid_ver_err;
+  }
+  temp_v.raw_version = version_str;
+  *v = std::move(temp_v);
+  return Status::OK();
+}
+
+} // namespace kudu

http://git-wip-us.apache.org/repos/asf/kudu/blob/c12e6352/src/kudu/util/version_util.h
----------------------------------------------------------------------
diff --git a/src/kudu/util/version_util.h b/src/kudu/util/version_util.h
new file mode 100644
index 0000000..5cde6cc
--- /dev/null
+++ b/src/kudu/util/version_util.h
@@ -0,0 +1,58 @@
+// Licensed to the Apache Software Foundation (ASF) under one
+// or more contributor license agreements.  See the NOTICE file
+// distributed with this work for additional information
+// regarding copyright ownership.  The ASF licenses this file
+// to you 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.
+#pragma once
+
+#include <iostream>
+#include <string>
+
+#include "kudu/util/status.h"
+
+namespace kudu {
+
+// A struct representing a parsed version. Versions are expected to look like
+//
+//  <major>.<minor>.<maintenance>[-<extra>]
+//
+// e.g. 1.6.0 or 1.7.1-SNAPSHOT.
+//
+// This struct can be used with versions reported by ksck to determine if and
+// how certain tools should function depending on what versions are running in
+// the cluster.
+struct Version {
+  bool operator==(const Version& other) const;
+
+  std::string ToString() const;
+
+  // The original version string.
+  std::string raw_version;
+
+  // The parsed version numbers.
+  int major;
+  int minor;
+  int maintenance;
+
+  // The extra component. Empty if there was no extra component.
+  std::string extra;
+};
+
+std::ostream& operator<<(std::ostream& os, const Version& v);
+
+// Parse 'version_str' into 'v'. 'v' must not be null.
+Status ParseVersion(const std::string& version_str,
+                    Version* v);
+
+} // namespace kudu


Mime
View raw message