Skip to content

Commit 966ac92

Browse files
committedSep 27, 2024
adaptation: implement plugin synchronization blocks.
Add a mechanism to block and unblock plugin synchronization. We use this first in our tests to ensure that test plugins get properly synchronized either before or after we create a pod or a container, but not halfway through it. We should also do the same in the corresponding paths of the NRI runtime integration core where otherwise processing NRI events concurrent to plugin registration could result in an inconsistent initial state of a new plugin (missing pods or containers during sync, or omitted event for the plugin). Signed-off-by: Krisztian Litkey <krisztian.litkey@intel.com>
1 parent 668be88 commit 966ac92

File tree

1 file changed

+33
-1
lines changed

1 file changed

+33
-1
lines changed
 

‎pkg/adaptation/adaptation.go

+33-1
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,7 @@ type Adaptation struct {
6565
serverOpts []ttrpc.ServerOpt
6666
listener net.Listener
6767
plugins []*plugin
68+
syncLock sync.RWMutex
6869
}
6970

7071
var (
@@ -135,6 +136,7 @@ func New(name, version string, syncFn SyncFn, updateFn UpdateFn, opts ...Option)
135136
pluginPath: DefaultPluginPath,
136137
dropinPath: DefaultPluginConfigPath,
137138
socketPath: DefaultSocketPath,
139+
syncLock: sync.RWMutex{},
138140
}
139141

140142
for _, o := range opts {
@@ -464,6 +466,8 @@ func (r *Adaptation) acceptPluginConnections(l net.Listener) error {
464466
continue
465467
}
466468

469+
r.requestPluginSync()
470+
467471
err = r.syncFn(ctx, p.synchronize)
468472
if err != nil {
469473
log.Infof(ctx, "failed to synchronize plugin: %v", err)
@@ -472,9 +476,10 @@ func (r *Adaptation) acceptPluginConnections(l net.Listener) error {
472476
r.plugins = append(r.plugins, p)
473477
r.sortPlugins()
474478
r.Unlock()
475-
476479
log.Infof(ctx, "plugin %q connected and synchronized", p.name())
477480
}
481+
482+
r.finishedPluginSync()
478483
}
479484
}()
480485

@@ -545,3 +550,30 @@ func (r *Adaptation) sortPlugins() {
545550
}
546551
}
547552
}
553+
554+
func (r *Adaptation) requestPluginSync() {
555+
r.syncLock.Lock()
556+
}
557+
558+
func (r *Adaptation) finishedPluginSync() {
559+
r.syncLock.Unlock()
560+
}
561+
562+
type PluginSyncBlock struct {
563+
r *Adaptation
564+
}
565+
566+
// BlockPluginSync blocks plugins from being synchronized/fully registered.
567+
func (r *Adaptation) BlockPluginSync() *PluginSyncBlock {
568+
r.syncLock.RLock()
569+
return &PluginSyncBlock{r: r}
570+
}
571+
572+
// Unblock a plugin sync. block put in place by BlockPluginSync. Safe to call
573+
// multiple times but only from a single goroutine.
574+
func (b *PluginSyncBlock) Unblock() {
575+
if b != nil && b.r != nil {
576+
b.r.syncLock.RUnlock()
577+
b.r = nil
578+
}
579+
}

0 commit comments

Comments
 (0)
Please sign in to comment.