| // 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. |
| |
| #ifndef KUDU_MASTER_TEST_UTIL_H_ |
| #define KUDU_MASTER_TEST_UTIL_H_ |
| |
| #include <algorithm> |
| #include <string> |
| |
| #include "kudu/common/schema.h" |
| #include "kudu/common/wire_protocol.h" |
| #include "kudu/gutil/strings/substitute.h" |
| #include "kudu/master/catalog_manager.h" |
| #include "kudu/master/mini_master.h" |
| #include "kudu/master/master.h" |
| #include "kudu/master/master.pb.h" |
| #include "kudu/util/stopwatch.h" |
| #include "kudu/util/test_util.h" |
| |
| namespace kudu { |
| namespace master { |
| |
| Status WaitForRunningTabletCount(MiniMaster* mini_master, |
| const string& table_name, |
| int expected_count, |
| GetTableLocationsResponsePB* resp) { |
| int wait_time = 1000; |
| |
| SCOPED_LOG_TIMING(INFO, strings::Substitute("waiting for tablet count of $0", expected_count)); |
| while (true) { |
| GetTableLocationsRequestPB req; |
| resp->Clear(); |
| req.mutable_table()->set_table_name(table_name); |
| req.set_max_returned_locations(expected_count); |
| CatalogManager* catalog = mini_master->master()->catalog_manager(); |
| { |
| CatalogManager::ScopedLeaderSharedLock l(catalog); |
| RETURN_NOT_OK(l.first_failed_status()); |
| RETURN_NOT_OK(catalog->GetTableLocations(&req, resp)); |
| } |
| if (resp->tablet_locations_size() >= expected_count) { |
| return Status::OK(); |
| } |
| |
| LOG(INFO) << "Waiting for " << expected_count << " tablets for table " |
| << table_name << ". So far we have " << resp->tablet_locations_size(); |
| |
| SleepFor(MonoDelta::FromMicroseconds(wait_time)); |
| wait_time = std::min(wait_time * 5 / 4, 1000000); |
| } |
| |
| // Unreachable. |
| LOG(FATAL) << "Reached unreachable section"; |
| return Status::RuntimeError("Unreachable statement"); // Suppress compiler warnings. |
| } |
| |
| void CreateTabletForTesting(MiniMaster* mini_master, |
| const string& table_name, |
| const Schema& schema, |
| string *tablet_id) { |
| { |
| CreateTableRequestPB req; |
| CreateTableResponsePB resp; |
| |
| req.set_name(table_name); |
| req.set_num_replicas(1); |
| ASSERT_OK(SchemaToPB(schema, req.mutable_schema())); |
| CatalogManager* catalog = mini_master->master()->catalog_manager(); |
| CatalogManager::ScopedLeaderSharedLock l(catalog); |
| ASSERT_OK(l.first_failed_status()); |
| ASSERT_OK(catalog->CreateTable(&req, &resp, NULL)); |
| } |
| |
| int wait_time = 1000; |
| bool is_table_created = false; |
| for (int i = 0; i < 80; ++i) { |
| IsCreateTableDoneRequestPB req; |
| IsCreateTableDoneResponsePB resp; |
| |
| req.mutable_table()->set_table_name(table_name); |
| CatalogManager* catalog = mini_master->master()->catalog_manager(); |
| { |
| CatalogManager::ScopedLeaderSharedLock l(catalog); |
| ASSERT_OK(l.first_failed_status()); |
| ASSERT_OK(catalog->IsCreateTableDone(&req, &resp)); |
| } |
| if (resp.done()) { |
| is_table_created = true; |
| break; |
| } |
| |
| VLOG(1) << "Waiting for table '" << table_name << "'to be created"; |
| |
| SleepFor(MonoDelta::FromMicroseconds(wait_time)); |
| wait_time = std::min(wait_time * 5 / 4, 1000000); |
| } |
| ASSERT_TRUE(is_table_created); |
| |
| { |
| GetTableSchemaRequestPB req; |
| GetTableSchemaResponsePB resp; |
| req.mutable_table()->set_table_name(table_name); |
| CatalogManager* catalog = mini_master->master()->catalog_manager(); |
| CatalogManager::ScopedLeaderSharedLock l(catalog); |
| ASSERT_OK(l.first_failed_status()); |
| ASSERT_OK(catalog->GetTableSchema(&req, &resp)); |
| ASSERT_TRUE(resp.create_table_done()); |
| } |
| |
| GetTableLocationsResponsePB resp; |
| ASSERT_OK(WaitForRunningTabletCount(mini_master, table_name, 1, &resp)); |
| *tablet_id = resp.tablet_locations(0).tablet_id(); |
| LOG(INFO) << "Got tablet " << *tablet_id << " for table " << table_name; |
| } |
| |
| } // namespace master |
| } // namespace kudu |
| |
| #endif /* KUDU_MASTER_TEST_UTIL_H_ */ |