| 1 |
/* Definition of the worker thread instance (wti) class. |
| 2 |
* |
| 3 |
* Copyright 2008-2017 Adiscon GmbH. |
| 4 |
* |
| 5 |
* This file is part of the rsyslog runtime library. |
| 6 |
* |
| 7 |
* Licensed under the Apache License, Version 2.0 (the "License"); |
| 8 |
* you may not use this file except in compliance with the License. |
| 9 |
* You may obtain a copy of the License at |
| 10 |
* |
| 11 |
* http://www.apache.org/licenses/LICENSE-2.0 |
| 12 |
* -or- |
| 13 |
* see COPYING.ASL20 in the source distribution |
| 14 |
* |
| 15 |
* Unless required by applicable law or agreed to in writing, software |
| 16 |
* distributed under the License is distributed on an "AS IS" BASIS, |
| 17 |
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 18 |
* See the License for the specific language governing permissions and |
| 19 |
* limitations under the License. |
| 20 |
*/ |
| 21 |
|
| 22 |
#ifndef WTI_H_INCLUDED |
| 23 |
#define WTI_H_INCLUDED |
| 24 |
|
| 25 |
#include <pthread.h> |
| 26 |
#include <stdlib.h> |
| 27 |
#include "wtp.h" |
| 28 |
#include "obj.h" |
| 29 |
#include "batch.h" |
| 30 |
#include "action.h" |
| 31 |
|
| 32 |
|
| 33 |
#define ACT_STATE_RDY 0 /* action ready, waiting for new transaction */ |
| 34 |
#define ACT_STATE_ITX 1 /* transaction active, waiting for new data or commit */ |
| 35 |
/* 2 currently not being used */ |
| 36 |
#define ACT_STATE_RTRY 3 /* failure occurred, trying to restablish ready state */ |
| 37 |
#define ACT_STATE_SUSP 4 /* suspended due to failure (return fail until timeout expired) */ |
| 38 |
#define ACT_STATE_DATAFAIL \ |
| 39 |
5 /* suspended due to failure in data, which means the message in \ |
| 40 |
questions needs to be dropped as it will always fail. The \ |
| 41 |
action must still do a "normal" retry in order to bring \ |
| 42 |
it back to regular state. */ |
| 43 |
#define ACT_STATE_DISABLED \ |
| 44 |
6 /* action encountered an unrecoverable failure and is \ |
| 45 |
permanently disabled for this process lifetime */ |
| 46 |
/* note: 3 bit bit field --> highest value is 7! */ |
| 47 |
|
| 48 |
typedef struct actWrkrInfo { |
| 49 |
action_t *pAction; |
| 50 |
void *actWrkrData; |
| 51 |
uint16_t uResumeOKinRow; /* number of times in a row that resume said OK with an |
| 52 |
immediate failure following */ |
| 53 |
int iNbrResRtry; /* number of retries since last suspend */ |
| 54 |
sbool bHadAutoCommit; /* did an auto-commit happen during doAction()? */ |
| 55 |
struct { |
| 56 |
unsigned actState : 3; |
| 57 |
} flags; |
| 58 |
union { |
| 59 |
struct { |
| 60 |
actWrkrIParams_t *iparams; /* dynamically sized array for transactional outputs */ |
| 61 |
int currIParam; |
| 62 |
int maxIParams; /* current max */ |
| 63 |
} tx; |
| 64 |
struct { |
| 65 |
actWrkrIParams_t actParams[CONF_OMOD_NUMSTRINGS_MAXSIZE]; |
| 66 |
} nontx; |
| 67 |
} p; /* short name for "parameters" */ |
| 68 |
} actWrkrInfo_t; |
| 69 |
|
| 70 |
/* the worker thread instance class */ |
| 71 |
struct wti_s { |
| 72 |
BEGINobjInstance |
| 73 |
; |
| 74 |
pthread_t thrdID; /* thread ID */ |
| 75 |
int bIsRunning; /* is this thread currently running? (must be int for atomic op!) */ |
| 76 |
sbool bAlwaysRunning; /* should this thread always run? */ |
| 77 |
int *pbShutdownImmediate; /* end processing of this batch immediately if set to 1 */ |
| 78 |
wtp_t *pWtp; /* my worker thread pool (important if only the work thread instance is passed! */ |
| 79 |
batch_t batch; /* pointer to an object array meaningful for current user |
| 80 |
pointer (e.g. queue pUsr data elemt) */ |
| 81 |
uchar *pszDbgHdr; /* header string for debug messages */ |
| 82 |
actWrkrInfo_t *actWrkrInfo; /* *array* of action wrkr infos for all actions |
| 83 |
(sized for max nbr of actions in config!) */ |
| 84 |
pthread_cond_t pcondBusy; /* condition to wake up the worker, protected by pmutUsr in wtp */ |
| 85 |
DEF_ATOMIC_HELPER_MUT(mutIsRunning); |
| 86 |
struct { |
| 87 |
uint8_t script_errno; /* errno-type interface for RainerScript functions */ |
| 88 |
uint8_t bPrevWasSuspended; |
| 89 |
uint8_t bDoAutoCommit; /* do a commit after each message |
| 90 |
* this is usually set for batches with 0 element, but may |
| 91 |
* also be added as a user-selectable option (not implemented yet) |
| 92 |
*/ |
| 93 |
} execState; /* state for the execution engine */ |
| 94 |
}; |
| 95 |
|
| 96 |
|
| 97 |
/* prototypes */ |
| 98 |
rsRetVal wtiConstruct(wti_t **ppThis); |
| 99 |
rsRetVal wtiConstructFinalize(wti_t *const pThis); |
| 100 |
rsRetVal wtiDestruct(wti_t **ppThis); |
| 101 |
rsRetVal wtiWorker(wti_t *const pThis); |
| 102 |
rsRetVal wtiSetDbgHdr(wti_t *const pThis, uchar *pszMsg, size_t lenMsg); |
| 103 |
uchar *ATTR_NONNULL() wtiGetDbgHdr(const wti_t *const pThis); |
| 104 |
rsRetVal wtiCancelThrd(wti_t *const pThis, const uchar *const cancelobj); |
| 105 |
void ATTR_NONNULL() wtiJoinThrd(wti_t *const pThis); |
| 106 |
rsRetVal wtiSetAlwaysRunning(wti_t *const pThis); |
| 107 |
rsRetVal wtiSetState(wti_t *const pThis, int bNew); |
| 108 |
rsRetVal wtiCASState(wti_t *const pThis, const int oldVal, const int newVal); |
| 109 |
rsRetVal wtiWakeupThrd(wti_t *const pThis); |
| 110 |
int wtiGetState(wti_t *const pThis); |
| 111 |
wti_t *wtiGetDummy(void); |
| 112 |
int ATTR_NONNULL() wtiWaitNonEmpty(wti_t *const pThis, const struct timespec timeout); |
| 113 |
PROTOTYPEObjClassInit(wti); |
| 114 |
PROTOTYPEObjClassExit(wti); |
| 115 |
PROTOTYPEpropSetMeth(wti, pszDbgHdr, uchar *); |
| 116 |
PROTOTYPEpropSetMeth(wti, pWtp, wtp_t *); |
| 117 |
|
| 118 |
#define getActionStateByNbr(pWti, iActNbr) ((uint8_t)((pWti)->actWrkrInfo[(iActNbr)].flags.actState)) |
| 119 |
#define getActionState(pWti, pAction) (((uint8_t)(pWti)->actWrkrInfo[(pAction)->iActionNbr].flags.actState)) |
| 120 |
#define setActionState(pWti, pAction, newState) ((pWti)->actWrkrInfo[(pAction)->iActionNbr].flags.actState = (newState)) |
| 121 |
#define getActionResumeInRow(pWti, pAction) (((pWti)->actWrkrInfo[(pAction)->iActionNbr].uResumeOKinRow)) |
| 122 |
#define setActionResumeInRow(pWti, pAction, val) ((pWti)->actWrkrInfo[(pAction)->iActionNbr].uResumeOKinRow = (val)) |
| 123 |
#define incActionResumeInRow(pWti, pAction) ((pWti)->actWrkrInfo[(pAction)->iActionNbr].uResumeOKinRow++) |
| 124 |
#define getActionNbrResRtry(pWti, pAction) (((pWti)->actWrkrInfo[(pAction)->iActionNbr].iNbrResRtry)) |
| 125 |
#define setActionNbrResRtry(pWti, pAction, val) ((pWti)->actWrkrInfo[(pAction)->iActionNbr].iNbrResRtry = (val)) |
| 126 |
#define incActionNbrResRtry(pWti, pAction) ((pWti)->actWrkrInfo[(pAction)->iActionNbr].iNbrResRtry++) |
| 127 |
#define wtiInitIParam(piparams) (memset((piparams), 0, sizeof(actWrkrIParams_t))) |
| 128 |
|
| 129 |
#define wtiGetScriptErrno(pWti) ((pWti)->execState.script_errno) |
| 130 |
#define wtiSetScriptErrno(pWti, newval) (pWti)->execState.script_errno = (newval) |
| 131 |
|
| 132 |
static inline uint8_t ATTR_UNUSED ATTR_NONNULL(1) wtiGetPrevWasSuspended(const wti_t *const pWti) { |
| 133 |
assert(pWti != NULL); |
| 134 |
return pWti->execState.bPrevWasSuspended; |
| 135 |
} |
| 136 |
|
| 137 |
static inline void __attribute__((unused)) wtiResetExecState(wti_t *const pWti, batch_t *const pBatch) { |
| 138 |
wtiSetScriptErrno(pWti, 0); |
| 139 |
pWti->execState.bPrevWasSuspended = 0; |
| 140 |
pWti->execState.bDoAutoCommit = (batchNumMsgs(pBatch) == 1); |
| 141 |
} |
| 142 |
|
| 143 |
|
| 144 |
rsRetVal wtiNewIParam(wti_t *const pWti, action_t *const pAction, actWrkrIParams_t **piparams); |
| 145 |
#endif /* #ifndef WTI_H_INCLUDED */ |