chore: import upstream snapshot with attribution

This commit is contained in:
wehub-resource-sync
2026-07-13 13:29:29 +08:00
commit f52cf32e56
1718 changed files with 366293 additions and 0 deletions
+148
View File
@@ -0,0 +1,148 @@
cmake_minimum_required(VERSION 2.8.10)
project(mysql_c++ C CXX)
# Install dependencies:
# With apt:
# sudo apt-get install libreadline-dev
# sudo apt-get install ncurses-dev
# With yum:
# sudo yum install readline-devel
# sudo yum install ncurses-devel
option(EXAMPLE_LINK_SO "Whether examples are linked dynamically" OFF)
execute_process(
COMMAND bash -c "find ${PROJECT_SOURCE_DIR}/../.. -type d -regex \".*output/include$\" | head -n1 | xargs dirname | tr -d '\n'"
OUTPUT_VARIABLE OUTPUT_PATH
)
set(CMAKE_PREFIX_PATH ${OUTPUT_PATH})
include(FindThreads)
include(FindProtobuf)
# Search for libthrift* by best effort. If it is not found and brpc is
# compiled with thrift protocol enabled, a link error would be reported.
find_library(THRIFT_LIB NAMES thrift)
if (NOT THRIFT_LIB)
set(THRIFT_LIB "")
endif()
find_library(THRIFTNB_LIB NAMES thriftnb)
if (NOT THRIFTNB_LIB)
set(THRIFTNB_LIB "")
endif()
find_path(BRPC_INCLUDE_PATH NAMES brpc/server.h)
if(EXAMPLE_LINK_SO)
find_library(BRPC_LIB NAMES brpc)
else()
find_library(BRPC_LIB NAMES libbrpc.a brpc)
endif()
if((NOT BRPC_INCLUDE_PATH) OR (NOT BRPC_LIB))
message(FATAL_ERROR "Fail to find brpc")
endif()
include_directories(${BRPC_INCLUDE_PATH})
find_path(GFLAGS_INCLUDE_PATH gflags/gflags.h)
find_library(GFLAGS_LIBRARY NAMES gflags libgflags)
if((NOT GFLAGS_INCLUDE_PATH) OR (NOT GFLAGS_LIBRARY))
message(FATAL_ERROR "Fail to find gflags")
endif()
include_directories(${GFLAGS_INCLUDE_PATH})
execute_process(
COMMAND bash -c "grep \"namespace [_A-Za-z0-9]\\+ {\" ${GFLAGS_INCLUDE_PATH}/gflags/gflags_declare.h | head -1 | awk '{print $2}' | tr -d '\n'"
OUTPUT_VARIABLE GFLAGS_NS
)
if(${GFLAGS_NS} STREQUAL "GFLAGS_NAMESPACE")
execute_process(
COMMAND bash -c "grep \"#define GFLAGS_NAMESPACE [_A-Za-z0-9]\\+\" ${GFLAGS_INCLUDE_PATH}/gflags/gflags_declare.h | head -1 | awk '{print $3}' | tr -d '\n'"
OUTPUT_VARIABLE GFLAGS_NS
)
endif()
if(CMAKE_SYSTEM_NAME STREQUAL "Darwin")
include(CheckFunctionExists)
CHECK_FUNCTION_EXISTS(clock_gettime HAVE_CLOCK_GETTIME)
if(NOT HAVE_CLOCK_GETTIME)
set(DEFINE_CLOCK_GETTIME "-DNO_CLOCK_GETTIME_IN_MAC")
endif()
endif()
set(CMAKE_CPP_FLAGS "${DEFINE_CLOCK_GETTIME} -DGFLAGS_NS=${GFLAGS_NS}")
set(CMAKE_CXX_FLAGS "${CMAKE_CPP_FLAGS} -DNDEBUG -O2 -D__const__= -pipe -W -Wall -Wno-unused-parameter -fPIC -fno-omit-frame-pointer")
if(CMAKE_VERSION VERSION_LESS "3.1.3")
if(CMAKE_CXX_COMPILER_ID STREQUAL "GNU")
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=c++11")
endif()
if(CMAKE_CXX_COMPILER_ID STREQUAL "Clang")
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=c++11")
endif()
else()
set(CMAKE_CXX_STANDARD 11)
set(CMAKE_CXX_STANDARD_REQUIRED ON)
endif()
find_path(LEVELDB_INCLUDE_PATH NAMES leveldb/db.h)
find_library(LEVELDB_LIB NAMES leveldb)
if ((NOT LEVELDB_INCLUDE_PATH) OR (NOT LEVELDB_LIB))
message(FATAL_ERROR "Fail to find leveldb")
endif()
include_directories(${LEVELDB_INCLUDE_PATH})
find_library(SSL_LIB NAMES ssl)
if (NOT SSL_LIB)
message(FATAL_ERROR "Fail to find ssl")
endif()
find_library(CRYPTO_LIB NAMES crypto)
if (NOT CRYPTO_LIB)
message(FATAL_ERROR "Fail to find crypto")
endif()
# find_path(MYSQL_INCLUDE_PATH NAMES mysql/mysql.h)
# find_library(MYSQL_LIB NAMES mysqlclient)
# if (NOT MYSQL_LIB)
# message(FATAL_ERROR "Fail to find mysqlclient")
# endif()
# include_directories(${MYSQL_INCLUDE_PATH})
set(DYNAMIC_LIB
${CMAKE_THREAD_LIBS_INIT}
${GFLAGS_LIBRARY}
${PROTOBUF_LIBRARIES}
${LEVELDB_LIB}
${SSL_LIB}
${CRYPTO_LIB}
${THRIFT_LIB}
${THRIFTNB_LIB}
# ${MYSQL_LIB}
dl
)
if(CMAKE_SYSTEM_NAME STREQUAL "Darwin")
set(DYNAMIC_LIB ${DYNAMIC_LIB}
pthread
"-framework CoreFoundation"
"-framework CoreGraphics"
"-framework CoreData"
"-framework CoreText"
"-framework Security"
"-framework Foundation"
"-Wl,-U,_MallocExtension_ReleaseFreeMemory"
"-Wl,-U,_ProfilerStart"
"-Wl,-U,_ProfilerStop")
endif()
add_executable(mysql_cli mysql_cli.cpp)
add_executable(mysql_tx mysql_tx.cpp)
add_executable(mysql_stmt mysql_stmt.cpp)
add_executable(mysql_press mysql_press.cpp)
# add_executable(mysqlclient_press mysqlclient_press.cpp)
set(AUX_LIB readline ncurses)
target_link_libraries(mysql_cli ${BRPC_LIB} ${DYNAMIC_LIB} ${AUX_LIB})
target_link_libraries(mysql_tx ${BRPC_LIB} ${DYNAMIC_LIB})
target_link_libraries(mysql_stmt ${BRPC_LIB} ${DYNAMIC_LIB})
target_link_libraries(mysql_press ${BRPC_LIB} ${DYNAMIC_LIB})
# target_link_libraries(mysqlclient_press ${BRPC_LIB} ${DYNAMIC_LIB})
+173
View File
@@ -0,0 +1,173 @@
/*
* 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.
*/
// A brpc based command-line interface to talk with mysql-server
#include <signal.h>
#include <stdio.h>
#include <readline/readline.h>
#include <readline/history.h>
#include <gflags/gflags.h>
#include <butil/logging.h>
#include <brpc/channel.h>
#include "brpc/policy/mysql/mysql.h"
#include "brpc/policy/mysql/mysql_authenticator.h"
DEFINE_string(connection_type, "pooled", "Connection type. Available values: pooled, short");
DEFINE_string(server, "127.0.0.1", "IP Address of server");
DEFINE_int32(port, 3306, "Port of server");
DEFINE_string(user, "brpcuser", "user name");
DEFINE_string(password, "12345678", "password");
DEFINE_string(schema, "brpc_test", "schema");
DEFINE_string(params, "", "params");
DEFINE_string(collation, "utf8mb4_general_ci", "collation");
DEFINE_int32(timeout_ms, 5000, "RPC timeout in milliseconds");
DEFINE_int32(connect_timeout_ms, 5000, "RPC timeout in milliseconds");
DEFINE_int32(max_retry, 0, "Max retries(not including the first RPC)");
namespace brpc {
const char* logo();
}
// Send `command' to mysql-server via `channel'
static bool access_mysql(brpc::Channel& channel, const char* command) {
brpc::MysqlRequest request;
if (!request.Query(command)) {
LOG(ERROR) << "Fail to add command";
return false;
}
brpc::MysqlResponse response;
brpc::Controller cntl;
channel.CallMethod(NULL, &cntl, &request, &response, NULL);
if (!cntl.Failed()) {
std::cout << response << std::endl;
} else {
LOG(ERROR) << "Fail to access mysql, " << cntl.ErrorText();
return false;
}
return true;
}
// For freeing the memory returned by readline().
struct Freer {
void operator()(char* mem) {
free(mem);
}
};
static void dummy_handler(int) {}
// The getc for readline. The default getc retries reading when meeting
// EINTR, which is not what we want.
static bool g_canceled = false;
static int cli_getc(FILE* stream) {
int c = getc(stream);
if (c == EOF && errno == EINTR) {
g_canceled = true;
return '\n';
}
return c;
}
int main(int argc, char* argv[]) {
// Parse gflags. We recommend you to use gflags as well.
GFLAGS_NS::ParseCommandLineFlags(&argc, &argv, true);
// A Channel represents a communication line to a Server. Notice that
// Channel is thread-safe and can be shared by all threads in your program.
brpc::Channel channel;
// Initialize the channel, NULL means using default options.
brpc::ChannelOptions options;
options.protocol = brpc::PROTOCOL_MYSQL;
options.connection_type = FLAGS_connection_type;
options.timeout_ms = FLAGS_timeout_ms /*milliseconds*/;
options.connect_timeout_ms = FLAGS_connect_timeout_ms;
options.max_retry = FLAGS_max_retry;
options.auth = new brpc::policy::MysqlAuthenticator(
FLAGS_user, FLAGS_password, FLAGS_schema, FLAGS_params, FLAGS_collation);
if (channel.Init(FLAGS_server.c_str(), FLAGS_port, &options) != 0) {
LOG(ERROR) << "Fail to initialize channel";
return -1;
}
if (argc <= 1) { // interactive mode
// We need this dummy signal hander to interrupt getc (and returning
// EINTR), SIG_IGN did not work.
signal(SIGINT, dummy_handler);
// Hook getc of readline.
rl_getc_function = cli_getc;
// Print welcome information.
printf("%s\n", brpc::logo());
printf(
"This command-line tool mimics the look-n-feel of official "
"mysql-cli, as a demostration of brpc's capability of"
" talking to mysql-server. The output and behavior is "
"not exactly same with the official one.\n\n");
for (;;) {
char prompt[128];
snprintf(prompt, sizeof(prompt), "mysql %s> ", FLAGS_server.c_str());
std::unique_ptr<char, Freer> command(readline(prompt));
if (command == NULL || *command == '\0') {
if (g_canceled) {
// No input after the prompt and user pressed Ctrl-C,
// quit the CLI.
return 0;
}
// User entered an empty command by just pressing Enter.
continue;
}
if (g_canceled) {
// User entered sth. and pressed Ctrl-C, start a new prompt.
g_canceled = false;
continue;
}
// Add user's command to history so that it's browse-able by
// UP-key and search-able by Ctrl-R.
add_history(command.get());
if (!strcmp(command.get(), "help")) {
printf("This is a mysql CLI written in brpc.\n");
continue;
}
if (!strcmp(command.get(), "quit")) {
// Although quit is a valid mysql command, it does not make
// too much sense to run it in this CLI, just quit.
return 0;
}
access_mysql(channel, command.get());
}
} else {
std::string command;
command.reserve(argc * 16);
for (int i = 1; i < argc; ++i) {
if (i != 1) {
command.push_back(';');
}
command.append(argv[i]);
}
if (!access_mysql(channel, command.c_str())) {
return -1;
}
}
return 0;
}
+80
View File
@@ -0,0 +1,80 @@
// 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.
package main
import (
"database/sql"
"flag"
"fmt"
_ "github.com/go-sql-driver/mysql"
"log"
"sync/atomic"
"time"
)
var thread_num int
func init() {
flag.IntVar(&thread_num, "thread_num", 1, "thread number")
}
var cost int64
var qps int64 = 1
func main() {
flag.Parse()
db, err := sql.Open("mysql", "brpcuser:12345678@tcp(127.0.0.1:3306)/brpc_test?charset=utf8")
if err != nil {
log.Fatal(err)
}
for i := 0; i < thread_num; i++ {
go func() {
for {
var (
id int
col1 string
col2 string
col3 string
col4 string
)
start := time.Now()
rows, err := db.Query("select * from brpc_press where id = 1")
if err != nil {
log.Fatal(err)
}
for rows.Next() {
if err := rows.Scan(&id, &col1, &col2, &col3, &col4); err != nil {
log.Fatal(err)
}
}
atomic.AddInt64(&cost, time.Since(start).Nanoseconds())
atomic.AddInt64(&qps, 1)
}
}()
}
var q int64 = 0
for {
fmt.Println("qps =", qps-q, "latency =", cost/(qps-q)/1000)
q = atomic.LoadInt64(&qps)
atomic.StoreInt64(&cost, 0)
time.Sleep(1 * time.Second)
}
}
+242
View File
@@ -0,0 +1,242 @@
/*
* 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.
*/
// A brpc based command-line interface to talk with mysql-server
#include <sstream>
#include <gflags/gflags.h>
#include <butil/logging.h>
#include <brpc/channel.h>
#include "brpc/policy/mysql/mysql.h"
#include "brpc/policy/mysql/mysql_authenticator.h"
#include <bvar/bvar.h>
#include <bthread/bthread.h>
#include <brpc/server.h>
DEFINE_string(connection_type, "pooled", "Connection type. Available values: pooled, short");
DEFINE_string(server, "127.0.0.1", "IP Address of server");
DEFINE_int32(port, 3306, "Port of server");
DEFINE_string(user, "brpcuser", "user name");
DEFINE_string(password, "12345678", "password");
DEFINE_string(schema, "brpc_test", "schema");
DEFINE_string(params, "", "params");
DEFINE_string(collation, "utf8mb4_general_ci", "collation");
DEFINE_string(data, "ABCDEF", "data");
DEFINE_int32(timeout_ms, 5000, "RPC timeout in milliseconds");
DEFINE_int32(connect_timeout_ms, 5000, "RPC timeout in milliseconds");
DEFINE_int32(max_retry, 3, "Max retries(not including the first RPC)");
DEFINE_int32(thread_num, 50, "Number of threads to send requests");
DEFINE_bool(use_bthread, false, "Use bthread to send requests");
DEFINE_int32(dummy_port, -1, "port of dummy server(for monitoring)");
DEFINE_int32(op_type, 0, "CRUD operation, 0:INSERT, 1:SELECT, 2:UPDATE");
DEFINE_bool(dont_fail, false, "Print fatal when some call failed");
bvar::LatencyRecorder g_latency_recorder("client");
bvar::Adder<int> g_error_count("client_error_count");
struct SenderArgs {
int base_index;
brpc::Channel* mysql_channel;
};
const std::string insert =
"insert into brpc_press(col1,col2,col3,col4) values "
"('"
"ABCABCABCABCABCABCABCABCABCABCABCABCABCABCABCABCABCABCABCABCABCABCABCABCABCABCA"
"BCABCABCABCABCABCABCA', '" +
FLAGS_data +
"' ,1.5, "
"now())";
// Send `command' to mysql-server via `channel'
static void* sender(void* void_args) {
SenderArgs* args = (SenderArgs*)void_args;
std::stringstream command;
if (FLAGS_op_type == 0) {
command << insert;
} else if (FLAGS_op_type == 1) {
command << "select * from brpc_press where id = " << args->base_index + 1;
} else if (FLAGS_op_type == 2) {
command << "update brpc_press set col2 = '" + FLAGS_data + "' where id = "
<< args->base_index + 1;
} else {
LOG(ERROR) << "wrong op type " << FLAGS_op_type;
}
brpc::MysqlRequest request;
if (!request.Query(command.str())) {
LOG(ERROR) << "Fail to execute command";
return NULL;
}
while (!brpc::IsAskedToQuit()) {
brpc::MysqlResponse response;
brpc::Controller cntl;
args->mysql_channel->CallMethod(NULL, &cntl, &request, &response, NULL);
const int64_t elp = cntl.latency_us();
if (!cntl.Failed()) {
g_latency_recorder << elp;
if (FLAGS_op_type == 0) {
CHECK_EQ(response.reply(0).is_ok(), true);
} else if (FLAGS_op_type == 1) {
CHECK_EQ(response.reply(0).row_count(), 1);
} else if (FLAGS_op_type == 2) {
CHECK_EQ(response.reply(0).is_ok(), true);
}
} else {
g_error_count << 1;
CHECK(brpc::IsAskedToQuit() || !FLAGS_dont_fail)
<< "error=" << cntl.ErrorText() << " latency=" << elp;
// We can't connect to the server, sleep a while. Notice that this
// is a specific sleeping to prevent this thread from spinning too
// fast. You should continue the business logic in a production
// server rather than sleeping.
bthread_usleep(50000);
}
}
return NULL;
}
int main(int argc, char* argv[]) {
// Parse gflags. We recommend you to use gflags as well.
GFLAGS_NS::ParseCommandLineFlags(&argc, &argv, true);
// A Channel represents a communication line to a Server. Notice that
// Channel is thread-safe and can be shared by all threads in your program.
brpc::Channel channel;
// Initialize the channel, NULL means using default options.
brpc::ChannelOptions options;
options.protocol = brpc::PROTOCOL_MYSQL;
options.connection_type = FLAGS_connection_type;
options.timeout_ms = FLAGS_timeout_ms /*milliseconds*/;
options.connect_timeout_ms = FLAGS_connect_timeout_ms;
options.max_retry = FLAGS_max_retry;
options.auth = new brpc::policy::MysqlAuthenticator(
FLAGS_user, FLAGS_password, FLAGS_schema, FLAGS_params, FLAGS_collation);
if (channel.Init(FLAGS_server.c_str(), FLAGS_port, &options) != 0) {
LOG(ERROR) << "Fail to initialize channel";
return -1;
}
// create table brpc_press
{
brpc::MysqlRequest request;
if (!request.Query(
"CREATE TABLE IF NOT EXISTS `brpc_press`(`id` INT UNSIGNED AUTO_INCREMENT, `col1` "
"VARCHAR(100) NOT NULL, `col2` VARCHAR(1024) NOT NULL, `col3` decimal(10,0) NOT "
"NULL, `col4` DATE, PRIMARY KEY ( `id` )) ENGINE=InnoDB DEFAULT CHARSET=utf8;")) {
LOG(ERROR) << "Fail to create table";
return -1;
}
brpc::MysqlResponse response;
brpc::Controller cntl;
channel.CallMethod(NULL, &cntl, &request, &response, NULL);
if (!cntl.Failed()) {
std::cout << response << std::endl;
} else {
LOG(ERROR) << "Fail to access mysql, " << cntl.ErrorText();
return -1;
}
}
// truncate table
{
brpc::MysqlRequest request;
if (!request.Query("truncate table brpc_press")) {
LOG(ERROR) << "Fail to truncate table";
return -1;
}
brpc::MysqlResponse response;
brpc::Controller cntl;
channel.CallMethod(NULL, &cntl, &request, &response, NULL);
if (!cntl.Failed()) {
std::cout << response << std::endl;
} else {
LOG(ERROR) << "Fail to access mysql, " << cntl.ErrorText();
return -1;
}
}
// prepare data for select, update
if (FLAGS_op_type != 0) {
for (int i = 0; i < FLAGS_thread_num; ++i) {
brpc::MysqlRequest request;
if (!request.Query(insert)) {
LOG(ERROR) << "Fail to execute command";
return -1;
}
brpc::MysqlResponse response;
brpc::Controller cntl;
channel.CallMethod(NULL, &cntl, &request, &response, NULL);
if (cntl.Failed()) {
LOG(ERROR) << cntl.ErrorText();
return -1;
}
if (!response.reply(0).is_ok()) {
LOG(ERROR) << "prepare data failed";
return -1;
}
}
}
if (FLAGS_dummy_port >= 0) {
brpc::StartDummyServerAt(FLAGS_dummy_port);
}
// test CRUD operations
std::vector<bthread_t> bids;
std::vector<pthread_t> pids;
bids.resize(FLAGS_thread_num);
pids.resize(FLAGS_thread_num);
std::vector<SenderArgs> args;
args.resize(FLAGS_thread_num);
for (int i = 0; i < FLAGS_thread_num; ++i) {
args[i].base_index = i;
args[i].mysql_channel = &channel;
if (!FLAGS_use_bthread) {
if (pthread_create(&pids[i], NULL, sender, &args[i]) != 0) {
LOG(ERROR) << "Fail to create pthread";
return -1;
}
} else {
if (bthread_start_background(&bids[i], NULL, sender, &args[i]) != 0) {
LOG(ERROR) << "Fail to create bthread";
return -1;
}
}
}
while (!brpc::IsAskedToQuit()) {
sleep(1);
LOG(INFO) << "Accessing mysql-server at qps=" << g_latency_recorder.qps(1)
<< " latency=" << g_latency_recorder.latency(1);
}
LOG(INFO) << "mysql_client is going to quit";
for (int i = 0; i < FLAGS_thread_num; ++i) {
if (!FLAGS_use_bthread) {
pthread_join(pids[i], NULL);
} else {
bthread_join(bids[i], NULL);
}
}
return 0;
}
+205
View File
@@ -0,0 +1,205 @@
/*
* 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.
*/
// A brpc based mysql transaction example
#include <gflags/gflags.h>
#include <butil/logging.h>
#include <bthread/bthread.h>
#include <brpc/channel.h>
#include "brpc/policy/mysql/mysql.h"
#include "brpc/policy/mysql/mysql_authenticator.h"
DEFINE_string(connection_type, "pooled", "Connection type. Available values: pooled, short");
DEFINE_string(server, "127.0.0.1", "IP Address of server");
DEFINE_int32(port, 3306, "Port of server");
DEFINE_string(user, "brpcuser", "user name");
DEFINE_string(password, "12345678", "password");
DEFINE_string(schema, "brpc_test", "schema");
DEFINE_string(params, "", "params");
DEFINE_string(collation, "utf8mb4_general_ci", "collation");
DEFINE_int32(timeout_ms, 5000, "RPC timeout in milliseconds");
DEFINE_int32(connect_timeout_ms, 5000, "RPC timeout in milliseconds");
DEFINE_int32(max_retry, 0, "Max retries(not including the first RPC)");
DEFINE_int32(thread_num, 1, "Number of threads to send requests");
DEFINE_int32(count, 1, "Number of request to send pre thread");
namespace brpc {
const char* logo();
}
struct SenderArgs {
brpc::Channel* mysql_channel;
brpc::MysqlStatement* mysql_stmt;
std::vector<std::string> commands;
};
// Send `command' to mysql-server via `channel'
static void* access_mysql(void* void_args) {
SenderArgs* args = (SenderArgs*)void_args;
brpc::Channel* channel = args->mysql_channel;
brpc::MysqlStatement* stmt = args->mysql_stmt;
const std::vector<std::string>& commands = args->commands;
for (int i = 0; i < FLAGS_count; ++i) {
brpc::MysqlRequest request(stmt);
for (size_t i = 1; i < commands.size(); i += 2) {
if (commands[i] == "int8") {
int8_t val = strtol(commands[i + 1].c_str(), NULL, 10);
if (!request.AddParam(val)) {
LOG(ERROR) << "Fail to add int8 param";
return NULL;
}
} else if (commands[i] == "uint8") {
uint8_t val = strtoul(commands[i + 1].c_str(), NULL, 10);
if (!request.AddParam(val)) {
LOG(ERROR) << "Fail to add uint8 param";
return NULL;
}
} else if (commands[i] == "int16") {
int16_t val = strtol(commands[i + 1].c_str(), NULL, 10);
if (!request.AddParam(val)) {
LOG(ERROR) << "Fail to add uint16 param";
return NULL;
}
} else if (commands[i] == "uint16") {
uint16_t val = strtoul(commands[i + 1].c_str(), NULL, 10);
if (!request.AddParam(val)) {
LOG(ERROR) << "Fail to add uint16 param";
return NULL;
}
} else if (commands[i] == "int32") {
int32_t val = strtol(commands[i + 1].c_str(), NULL, 10);
if (!request.AddParam(val)) {
LOG(ERROR) << "Fail to add int32 param";
return NULL;
}
} else if (commands[i] == "uint32") {
uint32_t val = strtoul(commands[i + 1].c_str(), NULL, 10);
if (!request.AddParam(val)) {
LOG(ERROR) << "Fail to add uint32 param";
return NULL;
}
} else if (commands[i] == "int64") {
int64_t val = strtol(commands[i + 1].c_str(), NULL, 10);
if (!request.AddParam(val)) {
LOG(ERROR) << "Fail to add int64 param";
return NULL;
}
} else if (commands[i] == "uint64") {
uint64_t val = strtoul(commands[i + 1].c_str(), NULL, 10);
if (!request.AddParam(val)) {
LOG(ERROR) << "Fail to add uint64 param";
return NULL;
}
} else if (commands[i] == "float") {
float val = strtof(commands[i + 1].c_str(), NULL);
if (!request.AddParam(val)) {
LOG(ERROR) << "Fail to add float param";
return NULL;
}
} else if (commands[i] == "double") {
double val = strtod(commands[i + 1].c_str(), NULL);
if (!request.AddParam(val)) {
LOG(ERROR) << "Fail to add double param";
return NULL;
}
} else if (commands[i] == "string") {
if (!request.AddParam(commands[i + 1])) {
LOG(ERROR) << "Fail to add string param";
return NULL;
}
} else {
LOG(ERROR) << "Wrong param type " << commands[i];
}
}
brpc::MysqlResponse response;
brpc::Controller cntl;
channel->CallMethod(NULL, &cntl, &request, &response, NULL);
if (cntl.Failed()) {
LOG(ERROR) << "Fail to access mysql, " << cntl.ErrorText();
return NULL;
}
std::cout << response << std::endl;
}
return NULL;
}
int main(int argc, char* argv[]) {
// Parse gflags. We recommend you to use gflags as well.
GFLAGS_NS::ParseCommandLineFlags(&argc, &argv, true);
// A Channel represents a communication line to a Server. Notice that
// Channel is thread-safe and can be shared by all threads in your program.
brpc::Channel channel;
// Initialize the channel, NULL means using default options.
brpc::ChannelOptions options;
options.protocol = brpc::PROTOCOL_MYSQL;
options.connection_type = FLAGS_connection_type;
options.timeout_ms = FLAGS_timeout_ms /*milliseconds*/;
options.connect_timeout_ms = FLAGS_connect_timeout_ms;
options.max_retry = FLAGS_max_retry;
options.auth = new brpc::policy::MysqlAuthenticator(
FLAGS_user, FLAGS_password, FLAGS_schema, FLAGS_params, FLAGS_collation);
if (channel.Init(FLAGS_server.c_str(), FLAGS_port, &options) != 0) {
LOG(ERROR) << "Fail to initialize channel";
return -1;
}
if (argc <= 1) {
LOG(ERROR) << "No sql statement args";
} else {
std::vector<std::string> commands;
commands.reserve(argc * 16);
for (int i = 1; i < argc; ++i) {
commands.push_back(argv[i]);
}
auto stmt(brpc::NewMysqlStatement(channel, commands[0]));
if (stmt == NULL) {
LOG(ERROR) << "Fail to create mysql statement";
return -1;
}
std::vector<SenderArgs> args;
std::vector<bthread_t> bids;
args.resize(FLAGS_thread_num);
bids.resize(FLAGS_thread_num);
for (int i = 0; i < FLAGS_thread_num; ++i) {
args[i].mysql_channel = &channel;
args[i].mysql_stmt = stmt.get();
args[i].commands = commands;
if (bthread_start_background(&bids[i], NULL, access_mysql, &args[i]) != 0) {
LOG(ERROR) << "Fail to create bthread";
return -1;
}
}
for (int i = 0; i < FLAGS_thread_num; ++i) {
bthread_join(bids[i], NULL);
}
}
return 0;
}
/* vim: set expandtab ts=4 sw=4 sts=4 tw=100: */
+121
View File
@@ -0,0 +1,121 @@
/*
* 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.
*/
// A brpc based mysql transaction example
#include <gflags/gflags.h>
#include <butil/logging.h>
#include <brpc/channel.h>
#include "brpc/policy/mysql/mysql.h"
#include "brpc/policy/mysql/mysql_authenticator.h"
DEFINE_string(connection_type, "pooled", "Connection type. Available values: pooled, short");
DEFINE_string(server, "127.0.0.1", "IP Address of server");
DEFINE_int32(port, 3306, "Port of server");
DEFINE_string(user, "brpcuser", "user name");
DEFINE_string(password, "12345678", "password");
DEFINE_string(schema, "brpc_test", "schema");
DEFINE_string(params, "", "params");
DEFINE_string(collation, "utf8mb4_general_ci", "collation");
DEFINE_int32(timeout_ms, 5000, "RPC timeout in milliseconds");
DEFINE_int32(connect_timeout_ms, 5000, "RPC timeout in milliseconds");
DEFINE_int32(max_retry, 0, "Max retries(not including the first RPC)");
DEFINE_bool(readonly, false, "readonly transaction");
DEFINE_int32(isolation_level, 0, "transaction isolation level");
namespace brpc {
const char* logo();
}
// Send `command' to mysql-server via `channel'
static bool access_mysql(brpc::Channel& channel, const std::vector<std::string>& commands) {
brpc::MysqlTransactionOptions options;
options.readonly = FLAGS_readonly;
options.isolation_level = brpc::MysqlIsolationLevel(FLAGS_isolation_level);
auto tx(brpc::NewMysqlTransaction(channel, options));
if (tx == NULL) {
LOG(ERROR) << "Fail to create transaction";
return false;
}
for (auto it = commands.begin(); it != commands.end(); ++it) {
brpc::MysqlRequest request(tx.get());
if (!request.Query(*it)) {
LOG(ERROR) << "Fail to add command";
tx->rollback();
return false;
}
brpc::MysqlResponse response;
brpc::Controller cntl;
channel.CallMethod(NULL, &cntl, &request, &response, NULL);
if (cntl.Failed()) {
LOG(ERROR) << "Fail to access mysql, " << cntl.ErrorText();
tx->rollback();
return false;
}
// check response
std::cout << response << std::endl;
for (size_t i = 0; i < response.reply_size(); ++i) {
if (response.reply(i).is_error()) {
tx->rollback();
return false;
}
}
}
tx->commit();
return true;
}
int main(int argc, char* argv[]) {
// Parse gflags. We recommend you to use gflags as well.
GFLAGS_NS::ParseCommandLineFlags(&argc, &argv, true);
// A Channel represents a communication line to a Server. Notice that
// Channel is thread-safe and can be shared by all threads in your program.
brpc::Channel channel;
// Initialize the channel, NULL means using default options.
brpc::ChannelOptions options;
options.protocol = brpc::PROTOCOL_MYSQL;
options.connection_type = FLAGS_connection_type;
options.timeout_ms = FLAGS_timeout_ms /*milliseconds*/;
options.connect_timeout_ms = FLAGS_connect_timeout_ms;
options.max_retry = FLAGS_max_retry;
options.auth = new brpc::policy::MysqlAuthenticator(
FLAGS_user, FLAGS_password, FLAGS_schema, FLAGS_params, FLAGS_collation);
if (channel.Init(FLAGS_server.c_str(), FLAGS_port, &options) != 0) {
LOG(ERROR) << "Fail to initialize channel";
return -1;
}
if (argc <= 1) {
LOG(ERROR) << "No sql statement args";
} else {
std::vector<std::string> commands;
commands.reserve(argc * 16);
for (int i = 1; i < argc; ++i) {
commands.push_back(argv[i]);
}
if (!access_mysql(channel, commands)) {
return -1;
}
}
return 0;
}
/* vim: set expandtab ts=4 sw=4 sts=4 tw=100: */
+244
View File
@@ -0,0 +1,244 @@
/*
* 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.
*/
// A brpc based command-line interface to talk with mysql-server
#include <sstream>
#include <gflags/gflags.h>
#include <butil/logging.h>
extern "C" {
#include <mysql/mysql.h>
}
#include <brpc/controller.h>
#include <bvar/bvar.h>
#include <bthread/bthread.h>
#include <brpc/server.h>
DEFINE_string(server, "127.0.0.1", "IP Address of server");
DEFINE_int32(port, 3306, "Port of server");
DEFINE_string(user, "brpcuser", "user name");
DEFINE_string(password, "12345678", "password");
DEFINE_string(schema, "brpc_test", "schema");
DEFINE_string(params, "", "params");
DEFINE_string(data, "ABCDEF", "data");
DEFINE_int32(thread_num, 50, "Number of threads to send requests");
DEFINE_bool(use_bthread, false, "Use bthread to send requests");
DEFINE_int32(dummy_port, -1, "port of dummy server(for monitoring)");
DEFINE_int32(op_type, 0, "CRUD operation, 0:INSERT, 1:SELECT, 3:UPDATE");
DEFINE_bool(dont_fail, false, "Print fatal when some call failed");
bvar::LatencyRecorder g_latency_recorder("client");
bvar::Adder<int> g_error_count("client_error_count");
struct SenderArgs {
int base_index;
MYSQL* mysql_conn;
};
const std::string insert =
"insert into mysqlclient_press(col1,col2,col3,col4) values "
"('"
"ABCABCABCABCABCABCABCABCABCABCABCABCABCABCABCABCABCABCABCABCABCABCABCABCABCABCA"
"BCABCABCABCABCABCABCA', '" +
FLAGS_data +
"' ,1.5, "
"now())";
// Send `command' to mysql-server via `channel'
static void* sender(void* void_args) {
SenderArgs* args = (SenderArgs*)void_args;
std::stringstream command;
if (FLAGS_op_type == 0) {
command << insert;
} else if (FLAGS_op_type == 1) {
command << "select * from mysqlclient_press where id = " << args->base_index + 1;
} else if (FLAGS_op_type == 2) {
command << "update brpc_press set col2 = '" + FLAGS_data + "' where id = "
<< args->base_index + 1;
} else {
LOG(ERROR) << "wrong op type " << FLAGS_op_type;
}
std::string command_str = command.str();
while (!brpc::IsAskedToQuit()) {
const int64_t begin_time_us = butil::cpuwide_time_us();
const int rc = mysql_real_query(args->mysql_conn, command_str.c_str(), command_str.size());
if (rc != 0) {
goto ERROR;
}
if (mysql_errno(args->mysql_conn) == 0) {
if (FLAGS_op_type == 0) {
CHECK_EQ(mysql_affected_rows(args->mysql_conn), 1);
} else if (FLAGS_op_type == 1) {
MYSQL_RES* res = mysql_store_result(args->mysql_conn);
if (res == NULL) {
LOG(INFO) << "not found";
} else {
CHECK_EQ(mysql_num_rows(res), 1);
mysql_free_result(res);
}
} else if (FLAGS_op_type == 2) {
}
const int64_t elp = butil::cpuwide_time_us() - begin_time_us;
g_latency_recorder << elp;
} else {
goto ERROR;
}
if (false) {
ERROR:
const int64_t elp = butil::cpuwide_time_us() - begin_time_us;
g_error_count << 1;
CHECK(brpc::IsAskedToQuit() || !FLAGS_dont_fail)
<< "error=" << mysql_error(args->mysql_conn) << " latency=" << elp;
// We can't connect to the server, sleep a while. Notice that this
// is a specific sleeping to prevent this thread from spinning too
// fast. You should continue the business logic in a production
// server rather than sleeping.
bthread_usleep(50000);
}
}
return NULL;
}
int main(int argc, char* argv[]) {
// Parse gflags. We recommend you to use gflags as well.
GFLAGS_NS::ParseCommandLineFlags(&argc, &argv, true);
if (FLAGS_dummy_port >= 0) {
brpc::StartDummyServerAt(FLAGS_dummy_port);
}
MYSQL* conn = mysql_init(NULL);
if (!mysql_real_connect(conn,
FLAGS_server.c_str(),
FLAGS_user.c_str(),
FLAGS_password.c_str(),
FLAGS_schema.c_str(),
FLAGS_port,
NULL,
0)) {
LOG(ERROR) << mysql_error(conn);
return -1;
}
// create table mysqlclient_press
{
const char* sql =
"CREATE TABLE IF NOT EXISTS `mysqlclient_press`(`id` INT UNSIGNED AUTO_INCREMENT, "
"`col1` "
"VARCHAR(100) NOT NULL, `col2` VARCHAR(1024) NOT NULL, `col3` decimal(10,0) NOT "
"NULL, `col4` DATE, PRIMARY KEY ( `id` )) ENGINE=InnoDB DEFAULT CHARSET=utf8;";
const int rc = mysql_real_query(conn, sql, strlen(sql));
if (rc != 0) {
LOG(ERROR) << "Fail to execute sql, " << mysql_error(conn);
return -1;
}
if (mysql_errno(conn) != 0) {
LOG(ERROR) << "Fail to store result, " << mysql_error(conn);
return -1;
}
}
// truncate table
{
const char* sql = "truncate table mysqlclient_press";
const int rc = mysql_real_query(conn, sql, strlen(sql));
if (rc != 0) {
LOG(ERROR) << "Fail to execute sql, " << mysql_error(conn);
return -1;
}
if (mysql_errno(conn) != 0) {
LOG(ERROR) << "Fail to store result, " << mysql_error(conn);
return -1;
}
}
// prepare data for select, update
if (FLAGS_op_type != 0) {
for (int i = 0; i < FLAGS_thread_num; ++i) {
const int rc = mysql_real_query(conn, insert.c_str(), insert.size());
if (rc != 0) {
LOG(ERROR) << "Fail to execute sql, " << mysql_error(conn);
return -1;
}
if (mysql_errno(conn) != 0) {
LOG(ERROR) << "Fail to store result, " << mysql_error(conn);
return -1;
}
}
}
// test CRUD operations
std::vector<bthread_t> bids;
std::vector<pthread_t> pids;
bids.resize(FLAGS_thread_num);
pids.resize(FLAGS_thread_num);
std::vector<SenderArgs> args;
args.resize(FLAGS_thread_num);
for (int i = 0; i < FLAGS_thread_num; ++i) {
MYSQL* conn = mysql_init(NULL);
if (!mysql_real_connect(conn,
FLAGS_server.c_str(),
FLAGS_user.c_str(),
FLAGS_password.c_str(),
FLAGS_schema.c_str(),
FLAGS_port,
NULL,
0)) {
LOG(ERROR) << mysql_error(conn);
return -1;
}
args[i].base_index = i;
args[i].mysql_conn = conn;
if (!FLAGS_use_bthread) {
if (pthread_create(&pids[i], NULL, sender, &args[i]) != 0) {
LOG(ERROR) << "Fail to create pthread";
return -1;
}
} else {
if (bthread_start_background(&bids[i], NULL, sender, &args[i]) != 0) {
LOG(ERROR) << "Fail to create bthread";
return -1;
}
}
}
while (!brpc::IsAskedToQuit()) {
sleep(1);
LOG(INFO) << "Accessing mysql-server at qps=" << g_latency_recorder.qps(1)
<< " latency=" << g_latency_recorder.latency(1);
}
LOG(INFO) << "mysql_client is going to quit";
for (int i = 0; i < FLAGS_thread_num; ++i) {
if (!FLAGS_use_bthread) {
pthread_join(pids[i], NULL);
} else {
bthread_join(bids[i], NULL);
}
}
return 0;
}