You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@trafficserver.apache.org by am...@apache.org on 2018/10/17 15:11:02 UTC
[trafficserver] branch master updated: TCL: Remove TCL dependency
from TSControl and EventControl
This is an automated email from the ASF dual-hosted git repository.
amc pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/trafficserver.git
The following commit(s) were added to refs/heads/master by this push:
new 2421b8d TCL: Remove TCL dependency from TSControl and EventControl
2421b8d is described below
commit 2421b8df1d5276aa98587d38623252f592a6ad0c
Author: Xavier Chi <ch...@gmail.com>
AuthorDate: Tue Oct 16 10:55:41 2018 -0500
TCL: Remove TCL dependency from TSControl and EventControl
---
mgmt/api/EventControlMain.cc | 64 +++++++++++++++----------------------------
mgmt/api/EventControlMain.h | 4 ++-
mgmt/api/TSControlMain.cc | 65 ++++++++++++++++----------------------------
3 files changed, 48 insertions(+), 85 deletions(-)
diff --git a/mgmt/api/EventControlMain.cc b/mgmt/api/EventControlMain.cc
index 59bbb19..4fc2668 100644
--- a/mgmt/api/EventControlMain.cc
+++ b/mgmt/api/EventControlMain.cc
@@ -43,7 +43,7 @@
// variables that are very important
ink_mutex mgmt_events_lock;
LLQ *mgmt_events;
-InkHashTable *accepted_clients; // list of all accepted client connections
+std::unordered_map<int, EventClientT *> accepted_clients; // list of all accepted client connections
static TSMgmtError handle_event_message(EventClientT *client, void *req, size_t reqlen);
@@ -96,13 +96,13 @@ delete_event_client(EventClientT *client)
* output:
*********************************************************************/
void
-remove_event_client(EventClientT *client, InkHashTable *table)
+remove_event_client(EventClientT *client, std::unordered_map<int, EventClientT *> &table)
{
// close client socket
- close_socket(client->fd); // close client socket
+ close_socket(client->fd);
// remove client binding from hash table
- ink_hash_table_delete(table, (char *)&client->fd);
+ table.erase(client->fd);
// free ClientT
delete_event_client(client);
@@ -245,14 +245,8 @@ event_callback_main(void *arg)
Debug("event", "[event_callback_main] listen on socket = %d", con_socket_fd);
- // initialize queue for accepted con
- accepted_clients = ink_hash_table_create(InkHashTableKeyType_Word);
- if (!accepted_clients) {
- return nullptr;
- }
// initialize queue for holding mgmt events
if ((ret = init_mgmt_events()) != TS_ERR_OKAY) {
- ink_hash_table_destroy(accepted_clients);
return nullptr;
}
// register callback with alarms processor
@@ -261,11 +255,9 @@ event_callback_main(void *arg)
// now we can start listening, accepting connections and servicing requests
int new_con_fd; // new connection fd when socket accepts connection
- fd_set selectFDs; // for select call
- InkHashTableEntry *con_entry; // used to obtain fd to alarms mapping
- EventClientT *client_entry; // an entry of fd to alarms mapping
- InkHashTableIteratorState con_state; // used to iterate through hash table
- int fds_ready; // return value for select go here
+ fd_set selectFDs; // for select call
+ EventClientT *client_entry; // an entry of fd to alarms mapping
+ int fds_ready; // return value for select go here
struct timeval timeout;
while (true) {
@@ -280,15 +272,11 @@ event_callback_main(void *arg)
Debug("event", "[event_callback_main] add fd %d to select set", con_socket_fd);
}
// see if there are more fd to set
- con_entry = ink_hash_table_iterator_first(accepted_clients, &con_state);
-
- // iterate through all entries in hash table
- while (con_entry) {
- client_entry = (EventClientT *)ink_hash_table_entry_value(accepted_clients, con_entry);
+ for (auto &&it : accepted_clients) {
+ client_entry = it.second;
if (client_entry->fd >= 0) { // add fd to select set
FD_SET(client_entry->fd, &selectFDs);
}
- con_entry = ink_hash_table_iterator_next(accepted_clients, &con_state);
}
// select call - timeout is set so we can check events at regular intervals
@@ -312,7 +300,7 @@ event_callback_main(void *arg)
socklen_t addr_len = (sizeof(struct sockaddr));
new_con_fd = mgmt_accept(con_socket_fd, new_client_con->adr, &addr_len);
new_client_con->fd = new_con_fd;
- ink_hash_table_insert(accepted_clients, (char *)&new_client_con->fd, new_client_con);
+ accepted_clients.emplace(new_client_con->fd, new_client_con);
Debug("event", "[event_callback_main] Accept new connection: fd=%d", new_con_fd);
}
} // end if (new_con_fd >= 0 && FD_ISSET(new_con_fd, &selectFDs))
@@ -320,9 +308,9 @@ event_callback_main(void *arg)
// some other file descriptor; for each one, service request
if (fds_ready > 0) { // RECEIVED A REQUEST from remote API client
// see if there are more fd to set - iterate through all entries in hash table
- con_entry = ink_hash_table_iterator_first(accepted_clients, &con_state);
- while (con_entry) {
- client_entry = (EventClientT *)ink_hash_table_entry_value(accepted_clients, con_entry);
+ for (auto it = accepted_clients.begin(); it != accepted_clients.end();) {
+ client_entry = it->second;
+ ++it; // prevent the breaking of remove_event_client
// got information check
if (client_entry->fd && FD_ISSET(client_entry->fd, &selectFDs)) {
// SERVICE REQUEST - read the op and message into a buffer
@@ -334,7 +322,6 @@ event_callback_main(void *arg)
if (ret == TS_ERR_NET_READ || ret == TS_ERR_NET_EOF) { // preprocess_msg FAILED!
Debug("event", "[event_callback_main] preprocess_msg FAILED; skip!");
remove_event_client(client_entry, accepted_clients);
- con_entry = ink_hash_table_iterator_next(accepted_clients, &con_state);
continue;
}
@@ -344,15 +331,12 @@ event_callback_main(void *arg)
if (ret == TS_ERR_NET_WRITE || ret == TS_ERR_NET_EOF) {
Debug("event", "[event_callback_main] ERROR: handle_control_message");
remove_event_client(client_entry, accepted_clients);
- con_entry = ink_hash_table_iterator_next(accepted_clients, &con_state);
continue;
}
} // end if(client_entry->fd && FD_ISSET(client_entry->fd, &selectFDs))
-
- con_entry = ink_hash_table_iterator_next(accepted_clients, &con_state);
- } // end while (con_entry)
- } // end if (fds_ready > 0)
+ } // end for (auto it = accepted_clients.begin(); it != accepted_clients.end();)
+ } // end if (fds_ready > 0)
} // end if (fds_ready > 0)
@@ -381,9 +365,8 @@ event_callback_main(void *arg)
// fprintf(stderr, "[event_callback_main] have an EVENT TO PROCESS\n");
// iterate through all entries in hash table, if any
- con_entry = ink_hash_table_iterator_first(accepted_clients, &con_state);
- while (con_entry) {
- client_entry = (EventClientT *)ink_hash_table_entry_value(accepted_clients, con_entry);
+ for (auto &&it : accepted_clients) {
+ client_entry = it.second;
if (client_entry->events_registered[event->id]) {
OpType optype = OpType::EVENT_NOTIFY;
MgmtMarshallString name = event->name;
@@ -395,7 +378,6 @@ event_callback_main(void *arg)
}
}
// get next client connection, if any
- con_entry = ink_hash_table_iterator_next(accepted_clients, &con_state);
} // end while(con_entry)
// now we can delete the event
@@ -409,18 +391,16 @@ event_callback_main(void *arg)
delete_mgmt_events();
// iterate through hash table; close client socket connections and remove entry
- con_entry = ink_hash_table_iterator_first(accepted_clients, &con_state);
- while (con_entry) {
- client_entry = (EventClientT *)ink_hash_table_entry_value(accepted_clients, con_entry);
+ for (auto &&it : accepted_clients) {
+ client_entry = it.second;
if (client_entry->fd >= 0) {
close_socket(client_entry->fd);
}
- ink_hash_table_delete(accepted_clients, (char *)&client_entry->fd); // remove binding
- delete_event_client(client_entry); // free ClientT
- con_entry = ink_hash_table_iterator_next(accepted_clients, &con_state);
+ accepted_clients.erase(client_entry->fd); // remove binding
+ delete_event_client(client_entry); // free ClientT
}
// all entries should be removed and freed already
- ink_hash_table_destroy(accepted_clients);
+ accepted_clients.clear();
ink_thread_exit(nullptr);
return nullptr;
diff --git a/mgmt/api/EventControlMain.h b/mgmt/api/EventControlMain.h
index 7e1d8e1..0a58a7a 100644
--- a/mgmt/api/EventControlMain.h
+++ b/mgmt/api/EventControlMain.h
@@ -35,6 +35,8 @@
#include "CoreAPIShared.h" // for NUM_EVENTS
#include "Alarms.h"
+#include <unordered_map>
+
// use events_registered[event_id] as index to check if alarm is registered
typedef struct {
int fd; // client socket
@@ -44,7 +46,7 @@ typedef struct {
EventClientT *new_event_client();
void delete_event_client(EventClientT *client);
-void remove_event_client(EventClientT *client, InkHashTable *table);
+void remove_event_client(EventClientT *client, std::unordered_map<int, EventClientT *> &table);
TSMgmtError init_mgmt_events();
void delete_mgmt_events();
diff --git a/mgmt/api/TSControlMain.cc b/mgmt/api/TSControlMain.cc
index b7626bc..2f59f53 100644
--- a/mgmt/api/TSControlMain.cc
+++ b/mgmt/api/TSControlMain.cc
@@ -42,9 +42,11 @@
#include "CoreAPIShared.h"
#include "NetworkUtilsLocal.h"
+#include <unordered_map>
+
#define TIMEOUT_SECS 1 // the num secs for select timeout
-static InkHashTable *accepted_con; // a list of all accepted client connections
+static std::unordered_map<int, ClientT *> accepted_con; // a list of all accepted client connections
static TSMgmtError handle_control_message(int fd, void *msg, size_t msglen);
@@ -93,13 +95,13 @@ delete_client(ClientT *client)
* output:
*********************************************************************/
static void
-remove_client(ClientT *client, InkHashTable *table)
+remove_client(ClientT *client, std::unordered_map<int, ClientT *> &table)
{
// close client socket
- close_socket(client->fd); // close client socket
+ close_socket(client->fd);
// remove client binding from hash table
- ink_hash_table_delete(table, (char *)&client->fd);
+ table.erase(client->fd);
// free ClientT
delete_client(client);
@@ -126,20 +128,12 @@ ts_ctrl_main(void *arg)
socket_fd = (int *)arg;
con_socket_fd = *socket_fd;
- // initialize queue for accepted con
- accepted_con = ink_hash_table_create(InkHashTableKeyType_Word);
- if (!accepted_con) {
- return nullptr;
- }
-
// now we can start listening, accepting connections and servicing requests
int new_con_fd; // new socket fd when accept connection
- fd_set selectFDs; // for select call
- InkHashTableEntry *con_entry; // used to obtain client connection info
- ClientT *client_entry; // an entry of fd to alarms mapping
- InkHashTableIteratorState con_state; // used to iterate through hash table
- int fds_ready; // stores return value for select
+ fd_set selectFDs; // for select call
+ ClientT *client_entry; // an entry of fd to alarms mapping
+ int fds_ready; // stores return value for select
struct timeval timeout;
// loops until TM dies; waits for and processes requests from clients
@@ -154,17 +148,13 @@ ts_ctrl_main(void *arg)
FD_SET(con_socket_fd, &selectFDs);
// Debug("ts_main", "[ts_ctrl_main] add fd %d to select set", con_socket_fd);
}
- // see if there are more fd to set
- con_entry = ink_hash_table_iterator_first(accepted_con, &con_state);
- // iterate through all entries in hash table
- while (con_entry) {
- client_entry = (ClientT *)ink_hash_table_entry_value(accepted_con, con_entry);
+ for (auto &&it : accepted_con) {
+ client_entry = it.second;
if (client_entry->fd >= 0) { // add fd to select set
FD_SET(client_entry->fd, &selectFDs);
Debug("ts_main", "[ts_ctrl_main] add fd %d to select set", client_entry->fd);
}
- con_entry = ink_hash_table_iterator_next(accepted_con, &con_state);
}
// select call - timeout is set so we can check events at regular intervals
@@ -187,7 +177,7 @@ ts_ctrl_main(void *arg)
socklen_t addr_len = (sizeof(struct sockaddr));
new_con_fd = mgmt_accept(con_socket_fd, new_client_con->adr, &addr_len);
new_client_con->fd = new_con_fd;
- ink_hash_table_insert(accepted_con, (char *)&new_client_con->fd, new_client_con);
+ accepted_con.emplace(new_client_con->fd, new_client_con);
Debug("ts_main", "[ts_ctrl_main] Add new client connection");
}
} // end if(new_con_fd >= 0 && FD_ISSET(new_con_fd, &selectFDs))
@@ -195,10 +185,11 @@ ts_ctrl_main(void *arg)
// some other file descriptor; for each one, service request
if (fds_ready > 0) { // RECEIVED A REQUEST from remote API client
// see if there are more fd to set - iterate through all entries in hash table
- con_entry = ink_hash_table_iterator_first(accepted_con, &con_state);
- while (con_entry) {
+
+ for (auto it = accepted_con.begin(); it != accepted_con.end();) {
Debug("ts_main", "[ts_ctrl_main] We have a remote client request!");
- client_entry = (ClientT *)ink_hash_table_entry_value(accepted_con, con_entry);
+ client_entry = it->second;
+ ++it; // prevent the breaking of remove_client
// got information; check
if (client_entry->fd && FD_ISSET(client_entry->fd, &selectFDs)) {
void *req = nullptr;
@@ -209,8 +200,6 @@ ts_ctrl_main(void *arg)
// occurs when remote API client terminates connection
Debug("ts_main", "[ts_ctrl_main] ERROR: preprocess_msg - remove client %d ", client_entry->fd);
remove_client(client_entry, accepted_con);
- // get next client connection (if any)
- con_entry = ink_hash_table_iterator_next(accepted_con, &con_state);
continue;
}
@@ -219,19 +208,14 @@ ts_ctrl_main(void *arg)
if (ret != TS_ERR_OKAY) {
Debug("ts_main", "[ts_ctrl_main] ERROR: sending response for message (%d)", ret);
-
// XXX this doesn't actually send a error response ...
-
remove_client(client_entry, accepted_con);
- con_entry = ink_hash_table_iterator_next(accepted_con, &con_state);
continue;
}
} // end if(client_entry->fd && FD_ISSET(client_entry->fd, &selectFDs))
-
- con_entry = ink_hash_table_iterator_next(accepted_con, &con_state);
- } // end while (con_entry)
- } // end if (fds_ready > 0)
+ } // end for (auto it = accepted_con.begin(); it != accepted_con.end();)
+ } // end if (fds_ready > 0)
} // end if (fds_ready > 0)
@@ -241,19 +225,16 @@ ts_ctrl_main(void *arg)
Debug("ts_main", "[ts_ctrl_main] CLOSING AND SHUTTING DOWN OPERATIONS");
close_socket(con_socket_fd);
- // iterate through hash table; close client socket connections and remove entry
- con_entry = ink_hash_table_iterator_first(accepted_con, &con_state);
- while (con_entry) {
- client_entry = (ClientT *)ink_hash_table_entry_value(accepted_con, con_entry);
+ for (auto &&it : accepted_con) {
+ client_entry = it.second;
if (client_entry->fd >= 0) {
close_socket(client_entry->fd); // close socket
}
- ink_hash_table_delete(accepted_con, (char *)&client_entry->fd); // remove binding
- delete_client(client_entry); // free ClientT
- con_entry = ink_hash_table_iterator_next(accepted_con, &con_state);
+ accepted_con.erase(client_entry->fd);
+ delete_client(client_entry); // free ClientT
}
// all entries should be removed and freed already
- ink_hash_table_destroy(accepted_con);
+ accepted_con.clear();
ink_thread_exit(nullptr);
return nullptr;