@@ -81,10 +81,31 @@ func (c *Cluster) UnregisterPlugin(lifetime plugin_entities.PluginLifetime) erro
8181 log .Info ("unregistering plugin" , "identity" , identity .String ())
8282 }
8383
84- // remove plugin from cluster
85- err = c .removePluginState (c .id , plugin_entities .HashedIdentity (identity .String ()))
84+ hashedIdentity := plugin_entities .HashedIdentity (identity .String ())
85+ const UNREGISTER_PLUGIN_RETRY_BASE_DELAY = 10 * time .Millisecond
86+
87+ // remove plugin from cluster with retry logic
88+ maxRetries := 3
89+ for i := 0 ; i < maxRetries ; i ++ {
90+ err = c .removePluginState (c .id , hashedIdentity )
91+ if err == nil {
92+ break
93+ }
94+
95+ // Log retry attempt
96+ if i < maxRetries - 1 {
97+ log .Warn ("failed to remove plugin state, retrying" ,
98+ "identity" , identity .String (),
99+ "attempt" , i + 1 ,
100+ "max_retries" , maxRetries ,
101+ "error" , err ,
102+ )
103+ time .Sleep (time .Duration (i + 1 ) * UNREGISTER_PLUGIN_RETRY_BASE_DELAY )
104+ }
105+ }
106+
86107 if err != nil {
87- return errors .Join (err , errors .New ("failed to remove plugin state" ))
108+ return errors .Join (err , errors .New ("failed to remove plugin state after retries " ))
88109 }
89110
90111 c .plugins .Delete (identity .String ())
@@ -262,6 +283,23 @@ func (c *Cluster) isPluginActive(state *pluginState) bool {
262283 return true
263284}
264285
286+ // isPluginStateValid checks if a plugin runtime state is still valid
287+ // A state is considered valid if it has been scheduled within the deactivated timeout
288+ func (c * Cluster ) isPluginStateValid (state * plugin_entities.PluginRuntimeState ) bool {
289+ if state == nil {
290+ return false
291+ }
292+ if state .ScheduledAt == nil {
293+ return false
294+ }
295+ // Consider state invalid if it hasn't been updated in more than half the deactivated timeout
296+ // This allows us to clean up stale states faster
297+ if time .Since (* state .ScheduledAt ) > c .pluginDeactivatedTimeout / 2 {
298+ return false
299+ }
300+ return true
301+ }
302+
265303func (c * Cluster ) splitNodePluginJoin (node_plugin_join string ) (nodeId string , plugin_hashed_id string , err error ) {
266304 split := strings .Split (node_plugin_join , ":" )
267305 if len (split ) != 2 {
0 commit comments