-
Notifications
You must be signed in to change notification settings - Fork 4k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Mongo client support #2171
Open
TousakaRin
wants to merge
2
commits into
apache:master
Choose a base branch
from
TousakaRin:helei_mongo_rewrite
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Mongo client support #2171
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -13,6 +13,7 @@ | |
*.so | ||
*.dylib | ||
*.rej | ||
*.patch | ||
/output | ||
/test/output | ||
build/ | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,69 @@ | ||
# 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. | ||
|
||
BRPC_PATH = ../../ | ||
include $(BRPC_PATH)/config.mk | ||
CXXFLAGS+=$(CPPFLAGS) -std=c++0x -DNDEBUG -O2 -pipe -W -Wall -fPIC -fno-omit-frame-pointer | ||
HDRS+=$(BRPC_PATH)/output/include | ||
LIBS+=$(BRPC_PATH)/output/lib | ||
HDRPATHS = $(addprefix -I, $(HDRS)) | ||
LIBPATHS = $(addprefix -L, $(LIBS)) | ||
COMMA=, | ||
SOPATHS=$(addprefix -Wl$(COMMA)-rpath$(COMMA), $(LIBS)) | ||
|
||
PRESS_SOURCES = mongo_press.cpp | ||
|
||
PRESS_OBJS = $(addsuffix .o, $(basename $(PRESS_SOURCES))) | ||
|
||
ifeq ($(SYSTEM),Darwin) | ||
ifneq ("$(LINK_SO)", "") | ||
STATIC_LINKINGS += -lbrpc | ||
else | ||
# *.a must be explicitly specified in clang | ||
STATIC_LINKINGS += $(BRPC_PATH)/output/lib/libbrpc.a | ||
endif | ||
LINK_OPTIONS_SO = $^ $(STATIC_LINKINGS) $(DYNAMIC_LINKINGS) | ||
LINK_OPTIONS = $^ $(STATIC_LINKINGS) $(DYNAMIC_LINKINGS) | ||
else ifeq ($(SYSTEM),Linux) | ||
STATIC_LINKINGS += -lbrpc | ||
LINK_OPTIONS_SO = -Xlinker "-(" $^ -Xlinker "-)" $(STATIC_LINKINGS) $(DYNAMIC_LINKINGS) | ||
LINK_OPTIONS = -Xlinker "-(" $^ -Wl,-Bstatic $(STATIC_LINKINGS) -Wl,-Bdynamic -Xlinker "-)" $(DYNAMIC_LINKINGS) | ||
endif | ||
|
||
.PHONY:all | ||
all: mongo_press | ||
|
||
.PHONY:clean | ||
clean: | ||
@echo "> Cleaning" | ||
rm -rf redis_press redis_cli $(PRESS_OBJS) $(CLI_OBJS) $(SERVER_OBJS) | ||
|
||
mongo_press:$(PRESS_OBJS) | ||
@echo "> Linking $@" | ||
ifneq ("$(LINK_SO)", "") | ||
$(CXX) $(LIBPATHS) $(SOPATHS) $(LINK_OPTIONS_SO) -o $@ | ||
else | ||
$(CXX) $(LIBPATHS) $(LINK_OPTIONS) -o $@ | ||
endif | ||
|
||
%.o:%.cpp | ||
@echo "> Compiling $@" | ||
$(CXX) -c $(HDRPATHS) $(CXXFLAGS) $< -o $@ | ||
|
||
%.o:%.cc | ||
@echo "> Compiling $@" | ||
$(CXX) -c $(HDRPATHS) $(CXXFLAGS) $< -o $@ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,107 @@ | ||
// 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 multi-threaded client getting keys from a redis-server constantly. | ||
|
||
#include <gflags/gflags.h> | ||
#include <bthread/bthread.h> | ||
#include <butil/logging.h> | ||
#include <butil/string_printf.h> | ||
#include <bvar/bvar.h> | ||
#include <brpc/channel.h> | ||
#include <brpc/server.h> | ||
#include <brpc/mongo.h> | ||
|
||
DEFINE_string(connection_type, "single", | ||
"Connection type. Available values: pooled, short"); | ||
DEFINE_string(server, "127.0.0.1", "IP Address of server"); | ||
DEFINE_int32(port, 27017, "Port of server"); | ||
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_string(collection, "test_collection", "collection name"); | ||
DEFINE_string(db, "test_db", "database name"); | ||
|
||
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_MONGO; | ||
options.connection_type = FLAGS_connection_type; | ||
options.timeout_ms = FLAGS_timeout_ms/*milliseconds*/; | ||
options.max_retry = FLAGS_max_retry; | ||
if (channel.Init(FLAGS_server.c_str(), FLAGS_port, &options) != 0) { | ||
LOG(ERROR) << "Fail to initialize channel"; | ||
return -1; | ||
} | ||
|
||
brpc::Controller cntl; | ||
butil::bson::UniqueBsonPtr command( | ||
BCON_NEW("insert", BCON_UTF8(FLAGS_collection.c_str()), | ||
"$db", BCON_UTF8(FLAGS_db.c_str()), | ||
"comment", BCON_UTF8("brpc mongo press"))); | ||
|
||
brpc::MongoMessage req; | ||
brpc::MongoMessage resp; | ||
req.set_body(std::move(command)); | ||
req.set_key("documents"); | ||
for (size_t i = 0; i < 10; i++) { | ||
char user_id[64]; | ||
char user_name[64]; | ||
::snprintf(user_id, sizeof(user_id), "user-%lu", i); | ||
::snprintf(user_name, sizeof(user_name), "user-name-%lu", i); | ||
req.add_doc_sequence(butil::bson::UniqueBsonPtr(BCON_NEW( | ||
"user", BCON_UTF8(user_id), | ||
"_id", BCON_INT32(i), | ||
"user_name", BCON_UTF8(user_name)))); | ||
} | ||
LOG(INFO) << "MongoRequest: " << req; | ||
channel.CallMethod(nullptr, &cntl, &req, &resp, nullptr); | ||
|
||
if (!cntl.Failed()) { | ||
LOG(INFO) << "OK: \n" << req << "\n" << resp; | ||
} else { | ||
LOG(INFO) << "Failed: \n" << req << "\n" << resp; | ||
LOG(INFO) << cntl.ErrorText(); | ||
return 0; | ||
} | ||
|
||
while (!brpc::IsAskedToQuit()) { | ||
brpc::Controller cntl; | ||
brpc::MongoMessage req; | ||
brpc::MongoMessage resp; | ||
butil::bson::UniqueBsonPtr command( | ||
BCON_NEW("find", BCON_UTF8(FLAGS_collection.c_str()), | ||
"$db", BCON_UTF8(FLAGS_db.c_str()), | ||
"comment", BCON_UTF8("brpc mongo press query"))); | ||
req.set_body(std::move(command)); | ||
channel.CallMethod(nullptr, &cntl, &req, &resp, nullptr); | ||
if (!cntl.Failed()) { | ||
LOG(INFO) << "OK: \n" << req << "\n" << resp; | ||
} else { | ||
LOG(INFO) << cntl.ErrorText(); | ||
} | ||
bthread_usleep(1000*1000); | ||
} | ||
return 0; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -509,10 +509,12 @@ static void GlobalInitializeOrDieImpl() { | |
} | ||
|
||
Protocol mongo_protocol = { ParseMongoMessage, | ||
NULL, NULL, | ||
ProcessMongoRequest, NULL, | ||
SerializeMongoRequest, | ||
PackMongoRequest, | ||
ProcessMongoRequest, | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. ProcessMongoRequest还要保留吗? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 我觉得可以不支持server端了,mongo协议变动挺大的。要做前后兼容工作量有点大 |
||
ProcessMongoResponse, | ||
NULL, NULL, NULL, | ||
CONNECTION_TYPE_POOLED, "mongo" }; | ||
CONNECTION_TYPE_ALL, "mongo" }; | ||
if (RegisterProtocol(PROTOCOL_MONGO, mongo_protocol) != 0) { | ||
exit(1); | ||
} | ||
|
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
这个是不是做成一个可选的编译选项比较好
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
ok, 整个mongo的client支持都做成一个编译选项吧