YugabyteDB (2.13.1.0-b60, 21121d69985fbf76aa6958d8f04a9bfa936293b5)

Coverage Report

Created: 2022-03-22 16:43

/Users/deen/code/yugabyte-db/src/yb/tools/insert-generated-rows.cc
Line
Count
Source (jump to first uncovered line)
1
// Licensed to the Apache Software Foundation (ASF) under one
2
// or more contributor license agreements.  See the NOTICE file
3
// distributed with this work for additional information
4
// regarding copyright ownership.  The ASF licenses this file
5
// to you under the Apache License, Version 2.0 (the
6
// "License"); you may not use this file except in compliance
7
// with the License.  You may obtain a copy of the License at
8
//
9
//   http://www.apache.org/licenses/LICENSE-2.0
10
//
11
// Unless required by applicable law or agreed to in writing,
12
// software distributed under the License is distributed on an
13
// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
14
// KIND, either express or implied.  See the License for the
15
// specific language governing permissions and limitations
16
// under the License.
17
//
18
// The following only applies to changes made to this file as part of YugaByte development.
19
//
20
// Portions Copyright (c) YugaByte, Inc.
21
//
22
// Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except
23
// in compliance with the License.  You may obtain a copy of the License at
24
//
25
// http://www.apache.org/licenses/LICENSE-2.0
26
//
27
// Unless required by applicable law or agreed to in writing, software distributed under the License
28
// is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express
29
// or implied.  See the License for the specific language governing permissions and limitations
30
// under the License.
31
//
32
// Simple tool to insert "random junk" rows into an arbitrary table.
33
// First column is in ascending order, the rest are random data.
34
// Helps make things like availability demos a little easier.
35
36
#include <memory>
37
#include <vector>
38
39
#include <gflags/gflags.h>
40
#include <glog/logging.h>
41
42
#include "yb/client/client.h"
43
#include "yb/client/error.h"
44
#include "yb/client/schema.h"
45
#include "yb/client/session.h"
46
#include "yb/client/table.h"
47
#include "yb/client/table_handle.h"
48
#include "yb/client/yb_op.h"
49
#include "yb/client/yb_table_name.h"
50
51
#include "yb/gutil/strings/split.h"
52
#include "yb/gutil/strings/substitute.h"
53
54
#include "yb/tools/data_gen_util.h"
55
56
#include "yb/util/flags.h"
57
#include "yb/util/logging.h"
58
#include "yb/util/random.h"
59
#include "yb/util/random_util.h"
60
#include "yb/util/result.h"
61
#include "yb/util/status_log.h"
62
63
using namespace std::literals;
64
65
DEFINE_string(master_address, "localhost",
66
              "Comma separated list of master addresses to run against.");
67
68
namespace yb {
69
namespace tools {
70
71
using std::string;
72
using std::vector;
73
74
using client::YBClient;
75
using client::YBClientBuilder;
76
using client::YBColumnSchema;
77
using client::YBSchema;
78
using client::YBSession;
79
using client::YBTable;
80
using client::YBTableName;
81
using std::shared_ptr;
82
83
0
void PrintUsage(char** argv) {
84
0
  std::cerr << "usage: " << argv[0] << " [--master_address localhost] <table_name>"
85
0
            << std::endl;
86
0
}
87
88
0
static int WriteRandomDataToTable(int argc, char** argv) {
89
0
  ParseCommandLineFlags(&argc, &argv, true);
90
0
  if (argc != 2) {
91
0
    PrintUsage(argv);
92
0
    return 1;
93
0
  }
94
0
  InitGoogleLoggingSafe(argv[0]);
95
0
  FLAGS_logtostderr = true;
96
97
0
  YBTableName table_name(YQL_DATABASE_CQL, argv[1]); // Default namespace.
98
99
0
  vector<string> addrs = strings::Split(FLAGS_master_address, ",");
100
0
  CHECK(!addrs.empty()) << "At least one master address must be specified!";
101
102
  // Set up client.
103
0
  LOG(INFO) << "Connecting to YB Master...";
104
0
  auto client = CHECK_RESULT(YBClientBuilder()
105
0
      .master_server_addrs(addrs)
106
0
      .Build());
107
108
0
  LOG(INFO) << "Opening table...";
109
0
  client::TableHandle table;
110
0
  CHECK_OK(table.Open(table_name, client.get()));
111
0
  YBSchema schema = table->schema();
112
113
0
  shared_ptr<YBSession> session = client->NewSession();
114
0
  session->SetTimeout(5s); // Time out after 5 seconds.
115
116
0
  Random random(GetRandomSeed32());
117
118
0
  LOG(INFO) << "Inserting random rows...";
119
0
  for (uint64_t record_id = 0; true; ++record_id) {
120
0
    auto insert = table.NewInsertOp();
121
0
    auto req = insert->mutable_request();
122
0
    GenerateDataForRow(schema, record_id, &random, req);
123
124
0
    LOG(INFO) << "Inserting record: " << req->ShortDebugString();
125
0
    session->Apply(insert);
126
0
    auto flush_status = session->FlushAndGetOpsErrors();
127
0
    const auto& s = flush_status.status;
128
0
    if (PREDICT_FALSE(!s.ok())) {
129
0
      for (const auto& e : flush_status.errors) {
130
0
        if (e->status().IsAlreadyPresent()) {
131
0
          LOG(WARNING) << "Ignoring insert error: " << e->status().ToString();
132
0
        } else {
133
0
          LOG(FATAL) << "Unexpected insert error: " << e->status().ToString();
134
0
        }
135
0
      }
136
0
      continue;
137
0
    }
138
0
    LOG(INFO) << "OK";
139
0
  }
140
141
0
  return 0;
142
0
}
143
144
}  // namespace tools
145
}  // namespace yb
146
147
18.6k
int main(int argc, char** argv) {
148
18.6k
  return yb::tools::WriteRandomDataToTable(argc, argv);
149
18.6k
}