Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Re-use TxIDs in listener manager instead of maintaining a different s… #683

Merged
merged 1 commit into from
Sep 23, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 2 additions & 17 deletions platform/common/core/generic/committer/finality.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ package committer

import (
"context"
"sync"
"time"

"github.com/hyperledger-labs/fabric-smart-client/platform/common/driver"
Expand Down Expand Up @@ -44,9 +43,7 @@ type FinalityManager[V comparable] struct {
eventQueue chan driver.FinalityEvent[V]
vault Vault[V]
postStatuses collections.Set[V]
txIDs collections.Set[string]
tracer trace.Tracer
mutex sync.RWMutex
eventQueueWorkers int
}

Expand All @@ -57,7 +54,6 @@ func NewFinalityManager[V comparable](listenerManager driver.ListenerManager[V],
eventQueue: make(chan driver.FinalityEvent[V], defaultEventQueueSize),
vault: vault,
postStatuses: collections.NewSet(statuses...),
txIDs: collections.NewSet[string](),
tracer: tracerProvider.Tracer("finality_manager", tracing.WithMetricsOpts(tracing.MetricsOpts{
Namespace: "core",
})),
Expand All @@ -66,19 +62,10 @@ func NewFinalityManager[V comparable](listenerManager driver.ListenerManager[V],
}

func (c *FinalityManager[V]) AddListener(txID driver.TxID, toAdd driver.FinalityListener[V]) error {
c.mutex.Lock()
defer c.mutex.Unlock()
if err := c.listenerManager.AddListener(txID, toAdd); err != nil {
return err
}
c.txIDs.Add(txID)
return nil
return c.listenerManager.AddListener(txID, toAdd)
}

func (c *FinalityManager[V]) RemoveListener(txID driver.TxID, toRemove driver.FinalityListener[V]) {
c.mutex.Lock()
defer c.mutex.Unlock()
c.txIDs.Remove(txID)
c.listenerManager.RemoveListener(txID, toRemove)
}

Expand Down Expand Up @@ -112,9 +99,7 @@ func (c *FinalityManager[V]) runStatusListener(ctx context.Context) {
case <-ctx.Done():
return
case <-ticker.C:
c.mutex.RLock()
txIDs := c.txIDs.ToSlice()
c.mutex.RUnlock()
txIDs := c.listenerManager.TxIDs()
if len(txIDs) == 0 {
c.logger.Debugf("no transactions to check vault status")
break
Expand Down
10 changes: 5 additions & 5 deletions platform/common/core/generic/committer/finality_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -45,13 +45,13 @@ func TestFinalityManager_AddListener(t *testing.T) {

err := manager.AddListener("txID", listener)
assert.NoError(t, err)
assert.Equal(t, manager.txIDs.Length(), 1)
assert.True(t, manager.txIDs.Contains("txID"))
assert.Equal(t, 1, len(manager.listenerManager.TxIDs()))
assert.Contains(t, manager.listenerManager.TxIDs(), "txID")

// Adding listener with empty txID should return an error
err = manager.AddListener("", listener)
assert.Error(t, err)
assert.Equal(t, manager.txIDs.Length(), 1)
assert.Equal(t, 1, len(manager.listenerManager.TxIDs()))
}

func TestFinalityManager_RemoveListener(t *testing.T) {
Expand All @@ -63,11 +63,11 @@ func TestFinalityManager_RemoveListener(t *testing.T) {
assert.NoError(t, manager.AddListener("txID", listener))

manager.RemoveListener("txID", listener)
assert.True(t, manager.txIDs.Empty())
assert.Empty(t, manager.listenerManager.TxIDs())

// Removing non-existing listener should do nothing
manager.RemoveListener("non-existing", listener)
assert.True(t, manager.txIDs.Empty())
assert.Empty(t, manager.listenerManager.TxIDs())
}

func TestFinalityManager_Run(t *testing.T) {
Expand Down
6 changes: 6 additions & 0 deletions platform/common/core/generic/committer/listenermgr.go
Original file line number Diff line number Diff line change
Expand Up @@ -110,3 +110,9 @@ func (c *finalityListenerManager[V]) cloneListeners(txID driver.TxID) []driver.F

return clone
}

func (c *finalityListenerManager[V]) TxIDs() []driver.TxID {
c.mutex.RLock()
defer c.mutex.RUnlock()
return collections.Keys(c.txIDListeners)
}
1 change: 1 addition & 0 deletions platform/common/driver/finality.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ type ListenerManager[V comparable] interface {
AddListener(txID TxID, toAdd FinalityListener[V]) error
RemoveListener(txID TxID, toRemove FinalityListener[V])
InvokeListeners(event FinalityEvent[V])
TxIDs() []TxID
}

// FinalityEvent contains information about the finality of a given transaction
Expand Down