Skip to content

Commit

Permalink
Fixing action execution lock
Browse files Browse the repository at this point in the history
  • Loading branch information
jruizgit committed Dec 15, 2019
1 parent 220d307 commit 1b3e658
Show file tree
Hide file tree
Showing 3 changed files with 40 additions and 35 deletions.
17 changes: 9 additions & 8 deletions src/rules/events.c
Original file line number Diff line number Diff line change
Expand Up @@ -2236,8 +2236,10 @@ unsigned int startActionForState(unsigned int handle,
unsigned int actionStateIndex;
unsigned int resultCount;
unsigned int resultFrameOffset;
time_t currentTime = time(NULL);

CHECK_RESULT(getNextResultInState(tree,
currentTime,
resultState,
&actionStateIndex,
&resultCount,
Expand All @@ -2256,7 +2258,7 @@ unsigned int startActionForState(unsigned int handle,
resultState->context.actionStateIndex = actionStateIndex;
resultState->context.resultCount = resultCount;
resultState->context.resultFrameOffset = resultFrameOffset;
resultState->lockExpireTime = time(NULL) + STATE_LEASE_TIME;
resultState->lockExpireTime = currentTime + STATE_LEASE_TIME;
*messages = resultState->context.messages;
*stateFact = resultState->context.stateFact;

Expand Down Expand Up @@ -2302,6 +2304,7 @@ static unsigned int deleteCurrentAction(ruleset *tree,

mid[midProperty->valueLength] = '\0';


CHECK_RESULT(handleDeleteMessage(tree,
state,
mid,
Expand All @@ -2311,10 +2314,7 @@ static unsigned int deleteCurrentAction(ruleset *tree,
}

resultFrameOffset = resultFrame->nextOffset;
unsigned int result = deleteDispatchingActionFrame(state, resultLocation);
if (result != RULES_OK) {
return result;
}
CHECK_RESULT(deleteDispatchingActionFrame(state, resultLocation));
}

return RULES_OK;
Expand Down Expand Up @@ -2345,16 +2345,17 @@ unsigned int completeAndStartAction(unsigned int handle,
resultState->context.actionStateIndex,
resultState->context.resultCount,
resultState->context.resultFrameOffset));



freeActionContext(resultState);

actionStateNode *resultAction;
unsigned int actionStateIndex;
unsigned int resultCount;
unsigned int resultFrameOffset;
time_t currentTime = time(NULL);

CHECK_RESULT(getNextResultInState(tree,
currentTime,
resultState,
&actionStateIndex,
&resultCount,
Expand All @@ -2370,7 +2371,7 @@ unsigned int completeAndStartAction(unsigned int handle,
resultState->context.actionStateIndex = actionStateIndex;
resultState->context.resultCount = resultCount;
resultState->context.resultFrameOffset = resultFrameOffset;
resultState->lockExpireTime = time(NULL) + STATE_LEASE_TIME;
resultState->lockExpireTime = currentTime + STATE_LEASE_TIME;
*messages = resultState->context.messages;
return RULES_OK;
}
Expand Down
57 changes: 30 additions & 27 deletions src/rules/state.c
Original file line number Diff line number Diff line change
Expand Up @@ -1126,6 +1126,7 @@ unsigned int serializeState(stateNode *state,
}

unsigned int getNextResultInState(void *tree,
time_t currentTime,
stateNode *state,
unsigned int *actionStateIndex,
unsigned int *resultCount,
Expand All @@ -1134,23 +1135,25 @@ unsigned int getNextResultInState(void *tree,

ruleset *rulesetTree = (ruleset*)tree;
*resultAction = NULL;
for (unsigned int index = 0; index < rulesetTree->actionCount; ++index) {
actionStateNode *actionNode = &state->actionState[index];
if (actionNode->reteNode) {
if ((actionNode->reteNode->value.c.cap && actionNode->resultPool.count) ||
(actionNode->reteNode->value.c.count && actionNode->resultPool.count >= actionNode->reteNode->value.c.count)) {
*resultAction = actionNode;
*actionStateIndex = index;
*resultFrameOffset = actionNode->resultIndex[0];
if (actionNode->reteNode->value.c.count) {
*resultCount = actionNode->reteNode->value.c.count;
} else {
*resultCount = (actionNode->reteNode->value.c.cap > actionNode->resultPool.count ?
actionNode->resultPool.count :
actionNode->reteNode->value.c.cap);
if (currentTime - state->lockExpireTime > STATE_LEASE_TIME) {
for (unsigned int index = 0; index < rulesetTree->actionCount; ++index) {
actionStateNode *actionNode = &state->actionState[index];
if (actionNode->reteNode) {
if ((actionNode->reteNode->value.c.cap && actionNode->resultPool.count) ||
(actionNode->reteNode->value.c.count && actionNode->resultPool.count >= actionNode->reteNode->value.c.count)) {
*resultAction = actionNode;
*actionStateIndex = index;
*resultFrameOffset = actionNode->resultIndex[0];
if (actionNode->reteNode->value.c.count) {
*resultCount = actionNode->reteNode->value.c.count;
} else {
*resultCount = (actionNode->reteNode->value.c.cap > actionNode->resultPool.count ?
actionNode->resultPool.count :
actionNode->reteNode->value.c.cap);
}

return RULES_OK;
}

return RULES_OK;
}
}
}
Expand All @@ -1177,18 +1180,18 @@ unsigned int getNextResult(void *tree,
while (count < rulesetTree->statePool.count && !*resultAction) {
unsigned int nodeOffset = rulesetTree->reverseStateIndex[rulesetTree->currentStateIndex];
*resultState = STATE_NODE(tree, nodeOffset);
if (currentTime - (*resultState)->lockExpireTime > STATE_LEASE_TIME) {
unsigned int result = getNextResultInState(tree,
*resultState,
actionStateIndex,
resultCount,
resultFrameOffset,
resultAction);
if (result != ERR_NO_ACTION_AVAILABLE) {
return result;
}

unsigned int result = getNextResultInState(tree,
currentTime,
*resultState,
actionStateIndex,
resultCount,
resultFrameOffset,
resultAction);
if (result != ERR_NO_ACTION_AVAILABLE) {
return result;
}

rulesetTree->currentStateIndex = (rulesetTree->currentStateIndex + 1) % rulesetTree->statePool.count;
++count;
}
Expand Down
1 change: 1 addition & 0 deletions src/rules/state.h
Original file line number Diff line number Diff line change
Expand Up @@ -351,6 +351,7 @@ unsigned int serializeState(stateNode *state,
char **stateFact);

unsigned int getNextResultInState(void *tree,
time_t currentTime,
stateNode *state,
unsigned int *actionStateIndex,
unsigned int *resultCount,
Expand Down

0 comments on commit 1b3e658

Please sign in to comment.