1558
}
1559
1560
>
func (s *ContextImpl) transition(request contextRequest) error {
context_impl.go
1561
>
/* State transitions:
1562
>
1563
>
The normal pattern:
1564
>
Initialized
1565
>
controller calls start()
1566
>
Acquiring
1567
>
acquireShard gets the shard
1568
>
Acquired
1569
>
1570
>
If we get a transient error from persistence:
1571
>
Acquired
1572
>
transient error: handleErrorLocked calls transition(contextRequestLost)
1573
>
Acquiring
1574
>
acquireShard gets the shard
1575
>
Acquired
1576
>
1577
>
If we get shard ownership lost:
1578
>
Acquired
1579
>
ShardOwnershipLostError: handleErrorLocked calls transition(contextRequestStop)
1580
>
Stopping
1581
>
controller removes from map and calls FinishStop()
1582
>
Stopped
1583
>
1584
>
Stopping can be triggered internally (if we get a ShardOwnershipLostError, or fail to acquire the rangeid
1585
>
lock after several minutes) or externally (from controller, e.g. controller shutting down or admin force-
1586
>
unload shard). If it's triggered internally, we transition to Stopping, then make an asynchronous callback
1587
>
to controller, which will remove us from the map and call FinishStop(), which will transition to Stopped and
1588
>
stop the engine. If it's triggered externally, we'll skip over Stopping and go straight to Stopped.
1589
>
1590
>
If we transition externally to Stopped, and the acquireShard goroutine is still running, we can't kill it,
1591
>
but we should make sure that it can't do anything: the context it uses for persistence ops will be
1592
>
canceled, and if it tries to transition states, it will fail.
1593
>
1594
>
Invariants:
1595
>
- Once state is Stopping, it can only go to Stopped.
1596
>
- Once state is Stopped, it can't go anywhere else.
1597
>
- At the start of acquireShard, state must be Acquiring.
1598
>
- By the end of acquireShard, state must not be Acquiring: either acquireShard set it to Acquired, or the
1599
>
controller set it to Stopped.
1600
>
- If state is Acquiring, acquireShard should be running in the background.
1601
>
- Only acquireShard can use contextRequestAcquired (i.e. transition from Acquiring to Acquired).
1602
>
- Once state has reached Acquired at least once, and not reached Stopped, engineFuture must be set.
1603
>
- Only the controller may call start() and FinishStop().
1604
>
- The controller must call FinishStop() for every ContextImpl it creates.
1605
>
1606
>
*/
1607
>
1608
>
s.stateLock.Lock()
1609
>
defer s.stateLock.Unlock()
1610
>
1611
>
setStateAcquiring := func() {
1612
s.state = contextStateAcquiring
1613
s.contextTaggedLogger.Info("", tag.LifeCycleStarted, tag.ComponentShardContext)