8000 fixed periodic jobs: only one instance should be running · lethalbrains/arangodb@670b642 · GitHub
[go: up one dir, main page]

Skip to content

Commit 670b642

Browse files
committed
fixed periodic jobs: only one instance should be running
1 parent 88f529f commit 670b642

File tree

6 files56

-57
lines changed

6 files changed

+56
-57
lines changed

CHANGELOG

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,9 @@
11
v3.0.1 (XXXX-XX-XX)
22
-------------------
33

4+
* fixed periodic jobs: there should be only one instance running - even if it
5+
runs longer than the period
6+
47
* increase max. number of collections in AQL queries from 32 to 256
58

69
* fixed issue #1916: header "authorization" is required" when opening
@@ -10,10 +13,10 @@ v3.0.1 (XXXX-XX-XX)
1013

1114
* fixed issue #1914: fix unterminated buffer
1215

13-
* don't remove lockfile if we are the same (now stale) pid
16+
* don't remove lockfile if we are the same (now stale) pid
1417
fixes docker setups (our pid will always be 1)
1518

16-
* do not use revision id comparisons in compaction for determining whether a
19+
* do not use revision id comparisons in compaction for determining whether a
1720
revision is obsolete, but marker memory addresses
1821
this ensures revision ids don't matter when compacting documents
1922

arangod/V8Server/V8Job.cpp

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@
3333
#include "V8/v8-vpack.h"
3434
#include "V8Server/V8Context.h"
3535
#include "V8Server/V8DealerFeature.h"
36+
#include "V8Server/V8PeriodicTask.h"
3637
#include "VocBase/vocbase.h"
3738

3839
using namespace arangodb;
@@ -44,13 +45,15 @@ using namespace arangodb::rest;
4445
////////////////////////////////////////////////////////////////////////////////
4546

4647
V8Job::V8Job(TRI_vocbase_t* vocbase, std::string const& command,
47-
std::shared_ptr<VPackBuilder> parameters, bool allowUseDatabase)
48+
std::shared_ptr<VPackBuilder> parameters, bool allowUseDatabase,
49+
Task* task)
4850
: Job("V8 Job"),
4951
_vocbase(vocbase),
5052
_command(command),
5153
_parameters(parameters),
5254
_canceled(false),
53-
_allowUseDatabase(allowUseDatabase) {
55+
_allowUseDatabase(allowUseDatabase),
56+
_task(task) {
5457
TRI_UseVocBase(vocbase);
5558
}
5659

@@ -60,6 +63,10 @@ V8Job::V8Job(TRI_vocbase_t* vocbase, std::string const& command,
6063

6164
V8Job::~V8Job() {
6265
TRI_ReleaseVocBase(_vocbase);
66+
67+
if (_task != nullptr) {
68+
V8PeriodicTask::jobDone(_task);
69+
}
6370
}
6471

6572
void V8Job::work() {

arangod/V8Server/V8Job.h

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,13 +30,17 @@
3030
struct TRI_vocbase_t;
3131

3232
namespace arangodb {
33+
namespace rest {
34+
class Task;
35+
}
36+
3337
class V8Job : public rest::Job {
3438
V8Job(V8Job const&) = delete;
3539
V8Job& operator=(V8Job const&) = delete;
3640

3741
public:
3842
V8Job(TRI_vocbase_t*, std::string const&,
39-
std::shared_ptr<arangodb::velocypack::Builder> const, bool);
43+
std::shared_ptr<arangodb::velocypack::Builder> const, bool, rest::Task*);
4044
~V8Job();
4145

4246
public:
@@ -54,6 +58,7 @@ class V8Job : public rest::Job {
5458
std::shared_ptr<arangodb::velocypack::Builder> _parameters;
5559
std::atomic<bool> _canceled;
5660
bool const _allowUseDatabase;
61+
rest::Task* _task;
5762
};
5863
}
5964

arangod/V8Server/V8PeriodicTask.cpp

Lines changed: 29 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,17 @@ using namespace arangodb;
3636
using namespace arangodb::application_features;
3737
using namespace arangodb::rest;
3838

39+
std::unordered_set<Task*> V8PeriodicTask::RUNNING;
40+
Mutex V8PeriodicTask::RUNNING_LOCK;
41+
42+
void V8PeriodicTask::jobDone(Task* task) {
43+
try {
44+
RUNNING.erase(task);
45+
} catch (...) {
46+
// ignore any memory error
47+
}
48+
}
49+
3950
V8PeriodicTask::V8PeriodicTask(std::string const& id, std::string const& name,
4051
TRI_vocbase_t* vocbase,
4152
double offset, double period,
@@ -60,10 +71,7 @@ V8PeriodicTask::~V8PeriodicTask() {
6071
TRI_ReleaseVocBase(_vocbase);
6172
}
6273

63-
////////////////////////////////////////////////////////////////////////////////
64-
/// @brief get a task specific description in JSON format
65-
////////////////////////////////////////////////////////////////////////////////
66-
74+
// get a task specific description in JSON format
6775
void V8PeriodicTask::getDescription(VPackBuilder& builder) const {
6876
PeriodicTask::getDescription(builder);
6977
TRI_ASSERT(builder.isOpenObject());
@@ -73,22 +81,28 @@ void V8PeriodicTask::getDescription(VPackBuilder& builder) const {
7381
builder.add("database", VPackValue(_vocbase->_name));
7482
}
7583

76-
////////////////////////////////////////////////////////////////////////////////
77-
/// @brief handles the next tick
78-
////////////////////////////////////////////////////////////////////////////////
79-
84+
// handles the next tick
8085
bool V8PeriodicTask::handlePeriod() {
81-
TRI_ASSERT(DispatcherFeature::DISPATCHER != nullptr);
82-
83-
std::unique_ptr<Job> job(new V8Job(
84-
_vocbase, "(function (params) { " + _command + " } )(params);",
85-
_parameters, _allowUseDatabase));
86-
8786
if (DispatcherFeature::DISPATCHER == nullptr) {
88-
LOG(WARN) << "could not add task " << _command << " to non-existing queue";
87+
LOG(WARN) << "could not add task " << _command << ", no dispatcher known";
8988
return false;
9089
}
9190

91+
{
92+
MUTEX_LOCKER(guard, V8PeriodicTask::RUNNING_LOCK);
93+
94+
if (RUNNING.find(this) != RUNNING.end()) {
95+
LOG(DEBUG) << "old task still running, skipping";
96+
return true;
97+
}
98+
99+
RUNNING.insert(this);
100+
}
101+
102+
std::unique_ptr<Job> job(new V8Job(
103+
_vocbase, "(function (params) { " + _command + " } )(params);",
104+
_parameters, _allowUseDatabase, this));
105+
92106
DispatcherFeature::DISPATCHER->addJob(job, false);
93107

94108
return true;

arangod/V8Server/V8PeriodicTask.h

Lines changed: 6 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -41,55 +41,25 @@ class V8PeriodicTask : public rest::PeriodicTask {
4141

4242
~V8PeriodicTask();
4343

44-
protected:
45-
//////////////////////////////////////////////////////////////////////////////
46-
/// @brief get a task specific description in VelocyPack format
47-
//////////////////////////////////////////////////////////////////////////////
44+
public:
45+
static void jobDone(Task*);
4846

47+
protected:
4948
void getDescription(arangodb::velocypack::Builder&) const override;
50-
51-
//////////////////////////////////////////////////////////////////////////////
52-
/// @brief whether or not the task is user-defined
53-
//////////////////////////////////////////////////////////////////////////////
54-
5549
bool isUserDefined() const override { return true; }
5650

5751
public:
58-
//////////////////////////////////////////////////////////////////////////////
59-
/// @brief handles the next tick
60-
//////////////////////////////////////////////////////////////////////////////
61-
6252
bool handlePeriod() override;
6353

6454
private:
65-
//////////////////////////////////////////////////////////////////////////////
66-
/// @brief system vocbase
67-
//////////////////////////////////////////////////////////////////////////////
55+
static Mutex RUNNING_LOCK;
56+
static std::unordered_set<Task*> RUNNING;
6857

58+
private:
6959
TRI_vocbase_t* _vocbase;
70-
71-
//////////////////////////////////////////////////////////////////////////////
72-
/// @brief command to execute
73-
//////////////////////////////////////////////////////////////////////////////
74-
7560
std::string const _command;
76-
77-
//////////////////////////////////////////////////////////////////////////////
78-
/// @brief paramaters
79-
//////////////////////////////////////////////////////////////////////////////
80-
8161
std::shared_ptr<arangodb::velocypack::Builder> _parameters;
82-
83-
//////////////////////////////////////////////////////////////////////////////
84-
/// @brief creation timestamp
85-
//////////////////////////////////////////////////////////////////////////////
86-
8762
double _created;
88-
89-
//////////////////////////////////////////////////////////////////////////////
90-
/// @brief whether or not the task is allowed to switch the database
91-
//////////////////////////////////////////////////////////////////////////////
92-
9363
bool _allowUseDatabase;
9464
};
9565
}

arangod/V8Server/V8TimerTask.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -83,7 +83,7 @@ bool V8TimerTask::handleTimeout() {
8383

8484
std::unique_ptr<Job> job(
8585
new V8Job(_vocbase, "(function (params) { " + _command + " } )(params);",
86-
_parameters, _allowUseDatabase));
86+
_parameters, _allowUseDatabase, nullptr));
8787

8888
if (DispatcherFeature::DISPATCHER == nullptr) {
8989
LOG(WARN) << "could not add task " << _command << " to non-existing queue";

0 commit comments

Comments
 (0)
0