-
Notifications
You must be signed in to change notification settings - Fork 2k
Add monitor feature #653
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
Open
kukey
wants to merge
9
commits into
twitter:master
Choose a base branch
from
kukey:add-monitor-feature
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
Add monitor feature #653
Changes from 3 commits
Commits
Show all changes
9 commits
Select commit
Hold shift + click to select a range
251d86f
add proxy monitor
kukey 23c6384
fix core_start not call monitor_init() funcation due to delete rbnode…
kukey ec8b937
event_add_out fail dequeue_outq fake req info
79e5d87
Apply suggestions from code review
kukey 4170eb9
use struct array replace struct rbtree to save conn point
97e3b14
string_printf() support '%.*s' modifier
5f71565
fix compile warning
kukey 38a3028
implement memcached monitor protocol that is only for twemproxy
fde8372
Merge branch 'add-monitor-feature' of https://github.com/kukey/twempr…
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 hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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,149 @@ | ||
| /* | ||
| * twemproxy - A fast and lightweight proxy for memcached protocol. | ||
| * | ||
| * Copyright (C) 2021, wei huang <wei.kukey@gmail.com> | ||
| * All rights reserved. | ||
| * | ||
| * Licensed 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 <nc_monitor.h> | ||
| #include <nc_rbtree.h> | ||
|
|
||
| struct rbtree monitor_tree; | ||
| struct rbnode monitor_sentinel_node; | ||
|
|
||
| void monitor_init() | ||
|
kukey marked this conversation as resolved.
Outdated
|
||
| { | ||
| rbtree_init(&monitor_tree, &monitor_sentinel_node); | ||
| } | ||
|
|
||
| void monitor_deinit(struct context *ctx) | ||
| { | ||
| struct rbnode *node = NULL; | ||
|
|
||
| while ((node = rbtree_min(&monitor_tree) != NULL)) | ||
|
kukey marked this conversation as resolved.
Outdated
|
||
| { | ||
| struct conn *c = node->data; | ||
| if (event_del_conn(ctx->evb, c) != NC_OK) { | ||
| log_warn("event del conn c %d failed, ignored: %s", | ||
| c->sd, strerror(errno)); | ||
| } | ||
| c->close(ctx, c); | ||
|
|
||
| rbtree_delete(&monitor_tree, node); | ||
| nc_free(node); | ||
| } | ||
| } | ||
|
|
||
| int mointor_is_empty() | ||
|
kukey marked this conversation as resolved.
Outdated
|
||
| { | ||
| return rbtree_is_empty(&monitor_tree); | ||
| } | ||
|
|
||
| rstatus_t add_to_monitor(struct conn *c) | ||
| { | ||
| ASSERT(c->client); | ||
|
|
||
| struct rbnode *node = nc_alloc(sizeof(struct rbnode)); | ||
| if (node == NULL) | ||
| { | ||
| return NC_ENOMEM; | ||
| } | ||
|
|
||
| c->monitor_client = 1; | ||
| node->key = c->sd; | ||
| node->data = c; | ||
|
|
||
| rbtree_insert(&monitor_tree, node); | ||
| } | ||
|
|
||
| void del_from_monitor(struct conn *c) | ||
| { | ||
| ASSERT(c->client && c->monitor_client); | ||
|
|
||
| struct rbnode *node = rbtree_find(&monitor_tree, c->sd); | ||
| ASSERT(node != NULL); | ||
| rbtree_delete(&monitor_tree, node); | ||
| nc_free(node); | ||
| } | ||
|
|
||
| struct monitor_data | ||
| { | ||
| struct msg *m; | ||
| struct conn *c; | ||
| struct context *ctx; | ||
| struct string *d; | ||
| }; | ||
|
|
||
| static void monitor_callback(struct rbnode *node, void *data) | ||
| { | ||
| struct monitor_data *mdata = data; | ||
| struct conn *req_c = node->data; | ||
|
|
||
| struct msg *req = req_get(req_c); | ||
| if (req == NULL) { | ||
| return; | ||
| } | ||
| struct msg *rsp = msg_get(req_c, 0, mdata->c->redis); | ||
| if (rsp == NULL) { | ||
| msg_put(req); | ||
| return; | ||
| } | ||
|
|
||
| req->peer = rsp; | ||
| rsp->peer = req; | ||
|
|
||
| req->done = 1; | ||
| rsp->done = 1; | ||
|
|
||
| if (msg_append_full(rsp, mdata->d->data, mdata->d->len) != NC_OK) { | ||
| msg_put(req); | ||
| msg_put(rsp); | ||
| return; | ||
| } | ||
| req_c->enqueue_outq(mdata->ctx, req_c, req); | ||
| if (event_add_out(mdata->ctx->evb, req_c) != NC_OK) { | ||
| req_c->err = errno; | ||
| req_c->dequeue_outq(mdata->ctx, req_c, req); | ||
| msg_put(req); | ||
| msg_put(rsp); | ||
| } | ||
|
|
||
| return; | ||
| } | ||
|
|
||
| rstatus_t make_monitor(struct context *ctx, struct conn *c, struct msg *m) | ||
|
kukey marked this conversation as resolved.
Outdated
|
||
| { | ||
| ASSERT(c->client); | ||
|
|
||
| struct string monitor_message = null_string; | ||
| struct monitor_data mdata = {0}; | ||
| mdata.m = m; | ||
| mdata.c = c; | ||
| mdata.d = &monitor_message; | ||
| mdata.ctx = ctx; | ||
| struct keypos *kpos = array_get(m->keys, 0); | ||
|
|
||
| string_printf(&monitor_message, "+%ld.%06ld [%s] command=%s key0=", | ||
|
kukey marked this conversation as resolved.
Outdated
|
||
| m->start_ts/1000000, m->start_ts%1000000, | ||
| nc_unresolve_peer_desc(c->sd), | ||
| (msg_type_string(m->type))->data); | ||
| string_cat_len(&monitor_message, kpos->start, kpos->end - kpos->start); | ||
| string_cat_len(&monitor_message, "\r\n", 2); | ||
|
|
||
| rbtree_inorder_traversal(monitor_tree.root, monitor_tree.sentinel, monitor_callback, &mdata); | ||
|
|
||
| string_deinit(&monitor_message); | ||
| return NC_OK; | ||
| } | ||
This file contains hidden or 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,33 @@ | ||
| /* | ||
| * twemproxy - A fast and lightweight proxy for memcached protocol. | ||
| * | ||
| * Copyright (C) 2021, wei huang <wei.kukey@gmail.com> | ||
| * All rights reserved. | ||
| * | ||
| * Licensed 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 NC_MONITOR_H | ||
| #define NC_MONITOR_H | ||
|
|
||
| #include <nc_core.h> | ||
|
|
||
| void monitor_init(); | ||
| void monitor_deinit(struct context *ctx); | ||
| int mointor_is_empty(); | ||
|
kukey marked this conversation as resolved.
Outdated
|
||
|
|
||
| rstatus_t add_to_monitor(struct conn *c); | ||
| void del_from_monitor(struct conn *c); | ||
| rstatus_t make_monitor(struct context *ctx, struct conn *c, struct msg *m); | ||
|
|
||
| #endif | ||
This file contains hidden or 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 hidden or 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
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.
Uh oh!
There was an error while loading. Please reload this page.