controller_impl.go ×8

Frontier kind: Code frontier

unlabeled · c_52569ff3cb3a

29 tests · 3125 LOC · 140 files · introduces 0 tests · 51 LOC · 1 file

Introduces — evidence that enters the hierarchy at this concept

Code
8 ranges51 lines · 1 files
Tests
0 tests

Contains — complete concept membership

All code (extent)
428 ranges3125 lines · 140 files · Browse complete extent
All tests (intent)
29 testsBrowse complete intent

Neighbourhood graph

The orange circle is the focus. Violet and green circles are every ancestor and descendant, broader and narrower, at any distance; blue squares and pink diamonds are the introduced files and exact introduced tests of every visible concept, not only the focus's. Arrows point from broader to narrower concepts and bridge only concepts omitted from this view. Undirected links show source or test introduction. Concept and file size follows LOC; exact test nodes use test-count units.

Introduced files, introduced tests, and structurally relevant concept specialization

In the embedded map, ordinary wheel input scrolls the page; use the visible controls to zoom and drag to pan. Open the full-screen map for canvas navigation: wheel pans, Ctrl/Command plus wheel zooms, and arrow keys pan when this region is focused. On touch screens, open the full-screen map to pan or pinch. If JavaScript or WebGL is unavailable, use the native relationship evidence on this page.

Graph controls are ready.

Interactive rendering requires JavaScript and WebGL. Use the native relationship evidence on this page while the interactive map is unavailable.

Native relationship evidence

Every exact file and test below is linked only from the concept that introduces it.

Introduced tests

Every collected test enters the hierarchy at exactly one concept.

No tests are introduced at this concept. Its intent tests are introduced by other concepts.

Introduced code

Every collected source range enters the hierarchy at exactly one concept.

1 file ranked by introduced lines: 51 introduced LOC across 8 ranges. Expand a file to inspect source; the > gutter marks introduced lines.

go.temporal.io/server/service/history/shard/controller_impl.go 51 introduced LOC · 8 ranges

Open complete file

381 }
382
383 > func (c *ControllerImpl) acquireShards(ctx context.Context) { controller_impl.go
384 > metrics.AcquireShardsCounter.With(c.taggedMetricsHandler).Record(1)
385 > startTime := time.Now().UTC()
386 > defer func() {
387 > metrics.AcquireShardsLatency.With(c.taggedMetricsHandler).Record(time.Since(startTime))
388 > }()
389
390 > ctx = headers.SetCallerInfo(ctx, headers.SystemBackgroundHighCallerInfo) controller_impl.go
391 >
392 > // Readiness check: if we haven't marked readiness yet, then we need to set up a context to
393 > // run the readiness check on owned shards.
394 > var readinessCtx context.Context
395 > var readinessCancel context.CancelFunc
396 > if !c.initialShardsAcquired.Ready() {
397 > readinessCtx, readinessCancel = context.WithCancel(ctx)
398 > } else {
399 readinessCancel = func() {} // we need a non-nil func for Swap
400 }
401 // Cancel previous readiness check to ensure that the readiness check is always running on
402 // the most recent set of owned shards (e.g. after a membership change).
403 > if prevCancel := c.shardReadinessCancel.Swap(readinessCancel); prevCancel != nil { controller_impl.go
404 prevCancel.(context.CancelFunc)()
405 }
406
407 > var ownedShardsLock sync.Mutex controller_impl.go
408 > var ownedShards []int32 // only populated if we are doing a readiness check
409 >
410 > tryAcquire := func(shardID int32) {
411 > if err := c.ownership.verifyOwnership(shardID); err != nil {
412 if IsShardOwnershipLostError(err) {
413 // current host is not owner of shard, unload it if it is already loaded.
441 }
442
443 > concurrency := int64(max(c.config.AcquireShardConcurrency(), 1)) controller_impl.go
444 > sem := semaphore.NewWeighted(concurrency)
445 > numShards := c.config.NumberOfShards
446 > randomStartOffset := rand.Int31n(numShards)
447 > for index := range numShards {
448 > shardID := (index+randomStartOffset)%numShards + 1
449 > if err := sem.Acquire(ctx, 1); err != nil {
450 break
451 }
452 > go func() { controller_impl.go
453 > defer sem.Release(1)
454 > tryAcquire(shardID)
455 > }()
456 }
457 > _ = sem.Acquire(ctx, concurrency) controller_impl.go
458 >
459 > c.RLock()
460 > // note that this count includes lingering shards
461 > numOfOwnedShards := len(c.historyShards)
462 > c.RUnlock()
463 > metrics.NumShardsGauge.With(c.taggedMetricsHandler).Record(float64(numOfOwnedShards))
464 > c.publishShardCountUpdate(numOfOwnedShards)
465 >
466 > // Readiness check: We should set initialShardsAcquired when:
467 > // 1. It's not already set.
468 > // 2. We should own at least one shard (i.e. not before we join membership).
469 > // 3. We have ownership of all the shards we're supposed to own.
470 > if readinessCtx != nil {
471 > if len(ownedShards) > 0 {
472 go func() {
473 defer readinessCancel()
523 // publishShardCountUpdate publishes the current number of shards that this controller owns to all shard count
524 // subscribers in a non-blocking manner.
525 > func (c *ControllerImpl) publishShardCountUpdate(shardCount int) { controller_impl.go
526 > c.RLock()
527 > defer c.RUnlock()
528 > for sub := range c.shardCountSubscriptions {
529 select {
530 case sub.ch <- shardCount: