@@ -57,8 +57,13 @@ func FLBPluginPreRegister(hotReloading C.int) int {
5757// can be provided.
5858//
5959//export FLBPluginRegister
60- func FLBPluginRegister (def unsafe.Pointer ) int {
61- defer registerWG .Done ()
60+ func FLBPluginRegister (def unsafe.Pointer ) (returnCode int ) {
61+ defer func () {
62+ // Only unblock waiters on registerWG if registration succeeds
63+ if returnCode == input .FLB_OK {
64+ registerWG .Done ()
65+ }
66+ }()
6267
6368 meta := pluginMeta .Load ()
6469 if meta == nil {
@@ -130,7 +135,7 @@ func (p *pluginInstance) inputPreRun() error {
130135 return nil
131136 }
132137
133- if p .state != instanceStateInitialized && p . state != instanceStatePaused {
138+ if p .state != instanceStateInitialized {
134139 return fmt .Errorf ("invalid plugin state %q" , p .state )
135140 }
136141
@@ -145,6 +150,7 @@ func (p *pluginInstance) inputPreRun() error {
145150 p .runCtx = runCtx
146151 p .runCancel = runCancel
147152 p .msgChannel = make (chan Message , maxBufferedMessages )
153+ p .state = instanceStateRunnable
148154
149155 go func () {
150156 err := p .meta .input .Collect (runCtx , p .msgChannel )
@@ -162,8 +168,8 @@ func (p *pluginInstance) inputPreRun() error {
162168 return nil
163169}
164170
165- // FLBPluginInputPreRun this method gets invoked by the fluent-bit runtime, once the plugin has been
166- // initialized, the plugin invoked only once before executing the input callbacks .
171+ // FLBPluginInputPreRun is invoked by the fluent-bit runtime after the plugin has been
172+ // initialized using FLBPluginRegister but before executing plugin callback functions .
167173//
168174//export FLBPluginInputPreRun
169175func FLBPluginInputPreRun (useHotReload C.int ) int {
@@ -208,14 +214,14 @@ func (p *pluginInstance) pause() error {
208214 p .mu .Lock ()
209215 defer p .mu .Unlock ()
210216
211- if ! ( p .state == instanceStateInitialized ) {
217+ if p .state != instanceStateRunnable {
212218 return fmt .Errorf ("cannot pause plugin in state %q" , p .state )
213219 }
214220
215221 p .runCancel ()
216222 close (p .msgChannel )
217223
218- p .state = instanceStatePaused
224+ p .state = instanceStateInitialized
219225 p .runCtx = nil
220226 p .runCancel = nil
221227 p .msgChannel = nil
@@ -227,7 +233,7 @@ func (p *pluginInstance) resume() error {
227233 p .mu .Lock ()
228234 defer p .mu .Unlock ()
229235
230- if p .state != instanceStatePaused {
236+ if p .state != instanceStateInitialized {
231237 return fmt .Errorf ("cannot resume plugin in state %q" , p .state )
232238 }
233239
@@ -310,19 +316,30 @@ func FLBPluginOutputPreRun(useHotReload C.int) int {
310316 panic ("plugin not initialized" )
311317 }
312318
313- instance .outputPreRun ()
319+ if err := instance .outputPreRun (); err != nil {
320+ fmt .Fprintf (os .Stderr , "plugin pre-run error: %v\n " , err )
321+ return flbReturnCode (err )
322+ }
314323
315324 return input .FLB_OK
316325}
317326
318- func (p * pluginInstance ) outputPreRun () {
327+ func (p * pluginInstance ) outputPreRun () error {
319328 p .mu .Lock ()
320329 defer p .mu .Unlock ()
321330
322- // TODO: state check
331+ // Only input plugins have a pre-run step
332+ if p .meta .output == nil {
333+ return fmt .Errorf ("plugin is not an output plugin" )
334+ }
335+
336+ if p .state != instanceStateInitialized {
337+ return fmt .Errorf ("invalid plugin state %q" , p .state )
338+ }
323339
324340 p .runCtx , p .runCancel = context .WithCancel (context .Background ())
325341 p .msgChannel = make (chan Message )
342+ p .state = instanceStateRunnable
326343
327344 go func () {
328345 if err := p .meta .output .Flush (p .runCtx , p .msgChannel ); err != nil {
@@ -335,6 +352,8 @@ func (p *pluginInstance) outputPreRun() {
335352
336353 log .Printf ("goroutine will be stopping: name=%q\n " , p .meta .name )
337354 }()
355+
356+ return nil
338357}
339358
340359// FLBPluginInputCallback this method gets invoked by the fluent-bit runtime, once the plugin has been
@@ -362,7 +381,7 @@ func (p *pluginInstance) callback(data *unsafe.Pointer, csize *C.size_t) int {
362381 p .mu .RLock ()
363382 defer p .mu .RUnlock ()
364383
365- if p .state != instanceStateInitialized {
384+ if p .state != instanceStateRunnable {
366385 return input .FLB_RETRY
367386 }
368387
@@ -428,7 +447,7 @@ func FLBPluginFlush(data unsafe.Pointer, clength C.int, ctag *C.char) int {
428447 instance := currInstance
429448 currInstanceMu .Unlock ()
430449
431- if instance == nil || instance .state != instanceStateInitialized { // TODO: ok?
450+ if instance == nil || instance .state != instanceStateRunnable {
432451 return output .FLB_RETRY
433452 }
434453
@@ -437,14 +456,9 @@ func FLBPluginFlush(data unsafe.Pointer, clength C.int, ctag *C.char) int {
437456 return output .FLB_RETRY
438457 }
439458
440- if instance == nil || instance .runCtx == nil {
441- return output .FLB_RETRY
442- }
443-
444- var err error
445459 select {
446460 case <- instance .runCtx .Done ():
447- err = instance .runCtx .Err ()
461+ err : = instance .runCtx .Err ()
448462 if err != nil && ! errors .Is (err , context .Canceled ) {
449463 fmt .Fprintf (os .Stderr , "run: %s\n " , err )
450464 return output .FLB_ERROR
@@ -465,10 +479,6 @@ func FLBPluginFlush(data unsafe.Pointer, clength C.int, ctag *C.char) int {
465479}
466480
467481func pluginFlush (instance * pluginInstance , tag string , b []byte ) error {
468- if instance .msgChannel == nil {
469- return fmt .Errorf ("no instance or channel available" )
470- }
471-
472482 dec := msgpack .NewDecoder (bytes .NewReader (b ))
473483 for {
474484 select {
0 commit comments