Skip to content

Commit 514574b

Browse files
committed
feat(f3): add back the power-table compute semaphore
1 parent 801d5b0 commit 514574b

File tree

1 file changed

+18
-0
lines changed

1 file changed

+18
-0
lines changed

chain/lf3/ec.go

+18
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ package lf3
22

33
import (
44
"context"
5+
"runtime"
56
"sort"
67
"sync"
78
"time"
@@ -36,6 +37,7 @@ type ecWrapper struct {
3637

3738
powerTableComputeLock sync.Mutex
3839
powerTableComputeJobs map[types.TipSetKey]chan struct{}
40+
powerTableComputeSema chan struct{}
3941
}
4042

4143
func newEcWrapper(chainStore *store.ChainStore, syncer *chain.Syncer, stateManager *stmgr.StateManager) *ecWrapper {
@@ -50,6 +52,7 @@ func newEcWrapper(chainStore *store.ChainStore, syncer *chain.Syncer, stateManag
5052
cache: cache,
5153

5254
powerTableComputeJobs: make(map[types.TipSetKey]chan struct{}),
55+
powerTableComputeSema: make(chan struct{}, min(4, runtime.NumCPU()/2)),
5356
}
5457
}
5558

@@ -140,6 +143,7 @@ func (ec *ecWrapper) GetPowerTable(ctx context.Context, tskF3 gpbft.TipSetKey) (
140143
}
141144

142145
func (ec *ecWrapper) getPowerTableLotusTSK(ctx context.Context, tsk types.TipSetKey) (gpbft.PowerEntries, error) {
146+
// Either wait for someone else to compute the power table, or claim the job.
143147
for {
144148
// check the cache
145149
pe, ok := ec.cache.Get(tsk)
@@ -160,10 +164,13 @@ func (ec *ecWrapper) getPowerTableLotusTSK(ctx context.Context, tsk types.TipSet
160164
}
161165
}
162166

167+
// Ok, we have the lock and nobody else has claimed the job. Claim it.
163168
myWaitCh := make(chan struct{})
164169
ec.powerTableComputeJobs[tsk] = myWaitCh
165170
ec.powerTableComputeLock.Unlock()
166171

172+
// Make sure to "unlock" the job when we're done, even if we don't complete it. Someone else
173+
// will complete it in that case.
167174
defer func() {
168175
ec.powerTableComputeLock.Lock()
169176
delete(ec.powerTableComputeJobs, tsk)
@@ -172,6 +179,17 @@ func (ec *ecWrapper) getPowerTableLotusTSK(ctx context.Context, tsk types.TipSet
172179
close(myWaitCh)
173180
}()
174181

182+
// Then wait in line. We only allow 4 jobs at once.
183+
select {
184+
case ec.powerTableComputeSema <- struct{}{}:
185+
case <-ctx.Done():
186+
return nil, ctx.Err()
187+
}
188+
defer func() {
189+
<-ec.powerTableComputeSema
190+
}()
191+
192+
// Finally, do the actual compute.
175193
ts, err := ec.chainStore.GetTipSetFromKey(ctx, tsk)
176194
if err != nil {
177195
return nil, xerrors.Errorf("getting tipset by key for get parent: %w", err)

0 commit comments

Comments
 (0)