Skip to content

Commit

Permalink
fix
Browse files Browse the repository at this point in the history
  • Loading branch information
Abingcbc committed Jan 6, 2025
1 parent 3b27c1b commit 2cb4b7b
Show file tree
Hide file tree
Showing 4 changed files with 18 additions and 7 deletions.
7 changes: 5 additions & 2 deletions core/host_monitor/HostMonitorInputRunner.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@

#include "HostMonitorInputRunner.h"

#include <chrono>

#include <memory>
#include <mutex>
#include <shared_mutex>
Expand Down Expand Up @@ -100,13 +102,14 @@ bool HostMonitorInputRunner::HasRegisteredPlugins() const {
return false;
}

bool HostMonitorInputRunner::IsCollectTaskValid(const std::string& collectorName) {
bool HostMonitorInputRunner::IsCollectTaskValid(std::chrono::steady_clock::time_point execTime,
const std::string& collectorName) {
std::shared_lock lock(mRegisteredCollectorMapMutex);
auto it = mRegisteredCollectorMap.find(collectorName);
if (it == mRegisteredCollectorMap.end()) {
return false;
}
return it->second.IsEnabled();
return it->second.IsEnabled() && (it->second.mLastEnableTime <= execTime);
}

void HostMonitorInputRunner::ScheduleOnce(std::chrono::steady_clock::time_point execTime,
Expand Down
10 changes: 8 additions & 2 deletions core/host_monitor/HostMonitorInputRunner.h
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@

#pragma once

#include <chrono>

#include <atomic>
#include <memory>
#include <shared_mutex>
Expand All @@ -40,10 +42,14 @@ struct CollectorInstance {

std::unique_ptr<BaseCollector> mCollector;
bool mIsEnabled = false;
std::chrono::steady_clock::time_point mLastEnableTime;

BaseCollector* GetCollector() const { return mCollector.get(); }
bool IsEnabled() const { return mIsEnabled; }
void Enable() { mIsEnabled = true; }
void Enable() {
mIsEnabled = true;
mLastEnableTime = std::chrono::steady_clock::now();
}
void Disable() { mIsEnabled = false; }
};

Expand All @@ -67,7 +73,7 @@ class HostMonitorInputRunner : public InputRunner {
void Stop() override;
bool HasRegisteredPlugins() const override;

bool IsCollectTaskValid(const std::string& collectorName);
bool IsCollectTaskValid(std::chrono::steady_clock::time_point execTime, const std::string& collectorName);
void ScheduleOnce(std::chrono::steady_clock::time_point execTime,
HostMonitorTimerEvent::CollectConfig& collectConfig);

Expand Down
2 changes: 1 addition & 1 deletion core/host_monitor/HostMonitorTimerEvent.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@
namespace logtail {

bool HostMonitorTimerEvent::IsValid() const {
return HostMonitorInputRunner::GetInstance()->IsCollectTaskValid(mCollectConfig.mCollectorName);
return HostMonitorInputRunner::GetInstance()->IsCollectTaskValid(GetExecTime(), mCollectConfig.mCollectorName);
}

bool HostMonitorTimerEvent::Execute() {
Expand Down
6 changes: 4 additions & 2 deletions core/unittest/host_monitor/HostMonitorInputRunnerUnittest.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -53,11 +53,13 @@ void HostMonitorInputRunnerUnittest::TestUpdateAndRemoveCollector() const {
auto runner = HostMonitorInputRunner::GetInstance();
runner->Init();
runner->UpdateCollector({MockCollector::sName}, QueueKey{}, 0);
APSARA_TEST_TRUE_FATAL(runner->IsCollectTaskValid(MockCollector::sName));
APSARA_TEST_TRUE_FATAL(runner->IsCollectTaskValid(std::chrono::steady_clock::now(), MockCollector::sName));
APSARA_TEST_FALSE_FATAL(
runner->IsCollectTaskValid(std::chrono::steady_clock::now() - std::chrono::seconds(60), MockCollector::sName));
APSARA_TEST_TRUE_FATAL(runner->HasRegisteredPlugins());
APSARA_TEST_EQUAL_FATAL(1, Timer::GetInstance()->mQueue.size());
runner->RemoveCollector();
APSARA_TEST_FALSE_FATAL(runner->IsCollectTaskValid(MockCollector::sName));
APSARA_TEST_FALSE_FATAL(runner->IsCollectTaskValid(std::chrono::steady_clock::now(), MockCollector::sName));
APSARA_TEST_FALSE_FATAL(runner->HasRegisteredPlugins());
runner->Stop();
}
Expand Down

0 comments on commit 2cb4b7b

Please sign in to comment.