Skip to content

Commit

Permalink
Merge pull request #161 from robbietu/master
Browse files Browse the repository at this point in the history
some fix
  • Loading branch information
robbietu authored Apr 3, 2024
2 parents 9acab30 + 66f3b1e commit 7897b43
Show file tree
Hide file tree
Showing 6 changed files with 92 additions and 31 deletions.
2 changes: 1 addition & 1 deletion CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ endif ()
# set PKTMINERG_MAJOR_VERSION, PKTMINERG_MINOR_VERSION, etc.
set(PKTMINERG_MAJOR_VERSION "0")
set(PKTMINERG_MINOR_VERSION "7")
set(PKTMINERG_PATCH_VERSION "1")
set(PKTMINERG_PATCH_VERSION "3")
set(PKTMINERG_VERSION_STRING "${PKTMINERG_MAJOR_VERSION}.${PKTMINERG_MINOR_VERSION}.${PKTMINERG_PATCH_VERSION}")

if (WIN32)
Expand Down
91 changes: 67 additions & 24 deletions src/daemonManager.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,8 @@ void DaemonManager::killRunningPktg() {
LOG(ERROR) << strErr;
report_.addPacketAgentLogs("ERROR", strErr);
}
clearCgroupfolder(pid);
clearCgroupfolder(agentPid_);
agentPid_ = 0;
}
}
Expand All @@ -85,6 +87,35 @@ void getActiveInstanceNames(std::set<std::string> & names) {
return;
}

void DaemonManager::clearCgroupfolder(pid_t pid) {
std::string path1("/cgroup/");
std::string path2("/sys/fs/cgroup/");

std::vector<std::string> pathToRemove;

pathToRemove.push_back(path1+"cpu/pid-"+std::to_string(pid));
pathToRemove.push_back(path2+"cpu/pid-"+std::to_string(pid));
pathToRemove.push_back(path1+"memory/pid-"+std::to_string(pid));
pathToRemove.push_back(path2+"memory/pid-"+std::to_string(pid));

for(auto pStr:pathToRemove) {
boost::filesystem::path p(pStr);
if (boost::filesystem::exists(p)) {
if (rmdir(pStr.c_str()) == -1) {
output_buffer = boost::str(boost::format("Fail to remove path:%1%,%2% ")%pStr%strerror(errno));
ctx_.log(output_buffer, log4cpp::Priority::ERROR);
LOG(ERROR) << output_buffer;
} else {
output_buffer = boost::str(boost::format("Successfully remove path:%1%")%pStr);
ctx_.log(output_buffer, log4cpp::Priority::INFO);
LOG(INFO) << output_buffer;

}
}
}
return;
}

void DaemonManager::getDaemonImpl() {
std::lock_guard<std::recursive_mutex> lock{mtx_};
std::shared_ptr<CURL> curl(::curl_easy_init(), [](CURL *curl) {
Expand Down Expand Up @@ -179,7 +210,7 @@ void DaemonManager::getDaemonImpl() {

startPA(agent_, result);

if(agent_.SyncIntervalIsSet() != agent_.SyncIntervalIsSet() && agent_.getSyncInterval() > 0) {
if(agent_.SyncIntervalIsSet() && agent_.getSyncInterval() > 0) {
int periodUs = agent_.getSyncInterval() * TICK_US_SEC;
timer_tasks_cancel(tasks_, getDaemonTid_);
getDaemonTid_ = timer_tasks_push(tasks_, getDaemon, this, periodUs);
Expand All @@ -200,16 +231,17 @@ void DaemonManager::getDaemonImpl() {
ctx_.log(str, log4cpp::Priority::ERROR);
LOG(ERROR) << str;
report_.addPacketAgentLogs("ERROR", str);
clearCgroupfolder(agentPid_);
zmqPortAvlPush(zmqPort_);
}

std::stringstream result;

std::stringstream result;
startPA(agent_, result);
}
if (agentPid_ != 0)
report_.setPid(agentPid_);

if (agentPid_ != 0 && agent_.startTimeIsSet()) {
if (agentPid_ != 0 && agent_.startTimeIsSet() && report_.getPacketAgentMetrics()) {
report_.getPacketAgentMetrics()->setStartTime(agent_.getStartTime());
}
std::stringstream jsonS;
Expand Down Expand Up @@ -441,11 +473,9 @@ std::string DaemonManager::createParams(std::shared_ptr<io::swagger::server::mod
int DaemonManager::startPA(io::swagger::server::model::Agent& body, std::stringstream &result) {
uint16_t port;
if (!body.packetAgentStrategiesIsSet()) {
return -1;
}
std::vector<std::string> commandStr;
std::string str;
commandStr.push_back("pktminerg");
Expand All @@ -454,7 +484,15 @@ int DaemonManager::startPA(io::swagger::server::model::Agent& body, std::strings
if (datas.size() == 0) {
return -1;
}
std::set<std::string> names;
try{
getActiveInstanceNames(names);
}
catch (...) {
std::string str = boost::str(boost::format("Can't get active instance."));
ctx_.log(str, log4cpp::Priority::INFO);
LOG(INFO) << str;
}
uint64_t buffSize;
if(body.getMemLimit() == 0) {
buffSize = 256;
Expand All @@ -466,15 +504,19 @@ int DaemonManager::startPA(io::swagger::server::model::Agent& body, std::strings
} else if (data->interfaceNamesIsSet()) {
stratigies += data->getInterfaceNames().size();
} else if (data->instanceNamesIsSet()) {
instanceCheck_ = true;
stratigies += data->getInstanceNames().size();
for (auto & i: data->getInstanceNames()) {
if(std::find(names.begin(), names.end(), i) != names.end()) {
stratigies += 1;
}
}
}
}
if (stratigies == 0 || stratigies > body.getMemLimit()) {
return -1;
}
buffSize = body.getMemLimit()/stratigies;
}
Expand All @@ -485,7 +527,6 @@ int DaemonManager::startPA(io::swagger::server::model::Agent& body, std::strings
result << error.toJson();
return -1;
}
int noOfData = 0;
//get buffer size
for (auto &data: datas){
Expand Down Expand Up @@ -534,8 +575,7 @@ int DaemonManager::startPA(io::swagger::server::model::Agent& body, std::strings
}
} else if (data->instanceNamesIsSet()) {
std::set<std::string> names;
getActiveInstanceNames(names);
bool needRestart = false;
if (checkProcessRunning()) {
for (auto & item: instanceStatus) {
Expand Down Expand Up @@ -1024,6 +1064,7 @@ bool DaemonManager::delAgent() {
LOG(ERROR) << strErr;
report_.addPacketAgentLogs("ERROR", strErr);
}
clearCgroupfolder(agentPid_);
agentPid_ = 0;
zmqPortAvlPush(zmqPort_);
return true;
Expand All @@ -1050,17 +1091,6 @@ DaemonManager::DaemonManager(const boost::program_options::variables_map &vm, ti
daemon_.setNodeName(nodeName);
}

if(vm_.count("includingNICs")) {
std::vector<std::string> nics;
boost::split(nics, vm_["includingNICs"].as<std::string>(), boost::is_any_of(","));
if (0 == daemon_.filterNics(nics)) {
ctx_.log("No nics left after filtering, please check the config of \"includingNICs\"", log4cpp::Priority::ERROR);
std::cerr << "No nics left after filtering, please check the config of \"includingNICs\"" << std::endl;
timer_tasks_stop(tasks_);
return;
}
}

//set podname (and name space) for sidecard mode
if(vm_.count("podname")) {
char* podName = getenv(vm_["podname"].as<std::string>().c_str());
Expand Down Expand Up @@ -1098,7 +1128,7 @@ DaemonManager::DaemonManager(const boost::program_options::variables_map &vm, ti
}
}

daemon_.setClientVersion("0.7.0");
daemon_.setClientVersion("0.7.3");

split(strs, SUPPORT_API_VERSIONS, boost::algorithm::is_any_of(","));
for (const auto& str:strs) {
Expand Down Expand Up @@ -1176,6 +1206,19 @@ void DaemonManager::scanNetworkInterfaceImpl() {
{
ctx_.log("scanNetworkInterface changed ", log4cpp::Priority::INFO);
LOG(INFO) << "scanNetworkInterface changed ";
daemon_.updateNics();
if(vm_.count("includingNICs")) {
std::vector<std::string> nics;
boost::split(nics, vm_["includingNICs"].as<std::string>(), boost::is_any_of(","));
if (0 == daemon_.filterNics(nics)) {
ctx_.log("No nics left after filtering, please check the config of \"includingNICs\"", log4cpp::Priority::ERROR);
std::cerr << "No nics left after filtering, please check the config of \"includingNICs\"" << std::endl;
timer_tasks_stop(tasks_);
return;
}

}

if(!daemonReg())
interfaces_ = interfaces;
}
Expand Down
6 changes: 5 additions & 1 deletion src/daemonManager.h
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@
#include <signal.h>
#include <sys/wait.h>
#include <curl/curl.h>
#include <unistd.h>
#include <cerrno>
#include "zmq.hpp"

#include <boost/algorithm/string.hpp>
Expand All @@ -24,11 +26,11 @@
#include <boost/format.hpp>
#include <boost/program_options/variables_map.hpp>
#include <boost/date_time/posix_time/posix_time.hpp>
#include <boost/format.hpp>
#include <boost/algorithm/string.hpp>
#include <boost/uuid/uuid.hpp>
#include <boost/uuid/uuid_io.hpp>
#include <boost/uuid/uuid_generators.hpp>
#include <boost/filesystem.hpp>

#include "glog/logging.h"
#include "log4cpp/Category.hh"
Expand Down Expand Up @@ -124,6 +126,8 @@ class DaemonManager

void killRunningPktg();

void clearCgroupfolder(pid_t pid);

private:
const boost::program_options::variables_map& vm_;
std::unordered_set<std::string> interfaces_;
Expand Down
17 changes: 14 additions & 3 deletions src/pktminerg.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -94,10 +94,14 @@ static std::string getProccssIdWithContainer(const std::string &containerId, Log
fp=popen(cmd.c_str(),"r");
fgets(buffer,sizeof(buffer),fp);
pclose(fp);
#endif

if (strlen(buffer) == 0) {
std::string output_buffer = std::string("Can't get pid for the containerId:") + id;
ctx.log(output_buffer, log4cpp::Priority::ERROR);
std::cerr << StatisLogContext::getTimeString() << output_buffer << std::endl;
return std::string();
}
}
#endif
return std::string(buffer, strlen(buffer) - 1);
}

Expand Down Expand Up @@ -665,7 +669,14 @@ Allowed options for each interface:");
uint16_t daemon_zmq_port = vm["control"].as<uint16_t>();
if (daemon_zmq_port) {
if (agent_control_plane == nullptr) {
agent_control_plane = std::make_shared<AgentControlPlane>(ctx, daemon_zmq_port);
try {
agent_control_plane = std::make_shared<AgentControlPlane>(ctx, daemon_zmq_port);
} catch (zmq::error_t& e) {
output_buffer = std::string("Can not bind daemon zmq port:") + std::to_string(daemon_zmq_port) + "," +e.what();
ctx.log(output_buffer, log4cpp::Priority::ERROR);
std::cerr << output_buffer << std::endl;
return 1;
}
}
update_status = 1;
}
Expand Down
6 changes: 4 additions & 2 deletions src/restful/model/Daemon.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -171,8 +171,6 @@ namespace io {

m_PlatformId = "";
m_PlatformIdIsSet = false;

nics = getNetworkInterfaces();
}

Daemon::~Daemon() {
Expand Down Expand Up @@ -608,6 +606,10 @@ namespace io {
}
return nics.size();
}
int Daemon::updateNics() {
nics.clear();
nics = getNetworkInterfaces();
}
}
}
}
Expand Down
1 change: 1 addition & 0 deletions src/restful/model/Daemon.h
Original file line number Diff line number Diff line change
Expand Up @@ -167,6 +167,7 @@ class Daemon
int filterNics(std::vector<std::string> & nicNames);

void setLogFileContext(LogFileContext& ctx) {m_ctx = ctx;};
int updateNics();

protected:
int64_t m_Id;
Expand Down

0 comments on commit 7897b43

Please sign in to comment.