github.com/braveheart12/just@v0.8.7/network/state/switcher.go (about)

     1  /*
     2   * The Clear BSD License
     3   *
     4   * Copyright (c) 2019 Insolar Technologies
     5   *
     6   * All rights reserved.
     7   *
     8   * Redistribution and use in source and binary forms, with or without modification, are permitted (subject to the limitations in the disclaimer below) provided that the following conditions are met:
     9   *
    10   *  Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer.
    11   *  Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer in the documentation and/or other materials provided with the distribution.
    12   *  Neither the name of Insolar Technologies nor the names of its contributors may be used to endorse or promote products derived from this software without specific prior written permission.
    13   *
    14   * NO EXPRESS OR IMPLIED LICENSES TO ANY PARTY'S PATENT RIGHTS ARE GRANTED BY THIS LICENSE. THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
    15   *
    16   */
    17  
    18  package state
    19  
    20  import (
    21  	"context"
    22  	"sync"
    23  	"time"
    24  
    25  	"github.com/insolar/insolar/core"
    26  	"github.com/insolar/insolar/instrumentation/inslogger"
    27  	"github.com/insolar/insolar/instrumentation/instracer"
    28  	"github.com/insolar/insolar/metrics"
    29  	"go.opencensus.io/trace"
    30  )
    31  
    32  //go:generate minimock -i github.com/insolar/insolar/network/state.messageBusLocker -o ./ -s _mock.go
    33  type messageBusLocker interface {
    34  	Lock(ctx context.Context)
    35  	Unlock(ctx context.Context)
    36  }
    37  
    38  // NetworkSwitcher is a network FSM using for bootstrapping
    39  type NetworkSwitcher struct {
    40  	NodeNetwork        core.NodeNetwork        `inject:""`
    41  	SwitcherWorkAround core.SwitcherWorkAround `inject:""`
    42  	MBLocker           messageBusLocker        `inject:""`
    43  
    44  	counter uint64
    45  
    46  	state     core.NetworkState
    47  	stateLock sync.RWMutex
    48  	span      *trace.Span
    49  }
    50  
    51  // NewNetworkSwitcher creates new NetworkSwitcher
    52  func NewNetworkSwitcher() (*NetworkSwitcher, error) {
    53  	return &NetworkSwitcher{
    54  		state:     core.NoNetworkState,
    55  		stateLock: sync.RWMutex{},
    56  		counter:   1,
    57  	}, nil
    58  }
    59  
    60  // GetState method returns current network state
    61  func (ns *NetworkSwitcher) GetState() core.NetworkState {
    62  	ns.stateLock.RLock()
    63  	defer ns.stateLock.RUnlock()
    64  
    65  	return ns.state
    66  }
    67  
    68  // OnPulse method checks current state and finds out reasons to update this state
    69  func (ns *NetworkSwitcher) OnPulse(ctx context.Context, pulse core.Pulse) error {
    70  	ns.stateLock.Lock()
    71  	defer ns.stateLock.Unlock()
    72  
    73  	ctx, span := instracer.StartSpan(ctx, "NetworkSwitcher.OnPulse")
    74  	span.AddAttributes(
    75  		trace.StringAttribute("NetworkSwitcher state: ", ns.state.String()),
    76  	)
    77  	defer span.End()
    78  	inslogger.FromContext(ctx).Infof("Current NetworkSwitcher state is: %s", ns.state)
    79  
    80  	if ns.SwitcherWorkAround.IsBootstrapped() && ns.state != core.CompleteNetworkState {
    81  		ns.state = core.CompleteNetworkState
    82  		ns.Release(ctx)
    83  		metrics.NetworkComplete.Set(float64(time.Now().Unix()))
    84  		inslogger.FromContext(ctx).Infof("Current NetworkSwitcher state switched to: %s", ns.state)
    85  	}
    86  
    87  	return nil
    88  }
    89  
    90  // Acquire increases lock counter and locks message bus if it wasn't lock before
    91  func (ns *NetworkSwitcher) Acquire(ctx context.Context) {
    92  	ctx, span := instracer.StartSpan(ctx, "NetworkSwitcher.Acquire")
    93  	defer span.End()
    94  	inslogger.FromContext(ctx).Info("Call Acquire in NetworkSwitcher: ", ns.counter)
    95  	ns.counter = ns.counter + 1
    96  	if ns.counter-1 == 0 {
    97  		inslogger.FromContext(ctx).Info("Lock MB")
    98  		ctx, ns.span = instracer.StartSpan(context.Background(), "GIL Lock (Lock MB)")
    99  		ns.MBLocker.Lock(ctx)
   100  	}
   101  }
   102  
   103  // Release decreases lock counter and unlocks message bus if it wasn't lock by someone else
   104  func (ns *NetworkSwitcher) Release(ctx context.Context) {
   105  	ctx, span := instracer.StartSpan(ctx, "NetworkSwitcher.Release")
   106  	defer span.End()
   107  	inslogger.FromContext(ctx).Info("Call Release in NetworkSwitcher: ", ns.counter)
   108  	if ns.counter == 0 {
   109  		panic("Trying to unlock without locking")
   110  	}
   111  	ns.counter = ns.counter - 1
   112  	if ns.counter == 0 {
   113  		inslogger.FromContext(ctx).Info("Unlock MB")
   114  		ns.MBLocker.Unlock(ctx)
   115  		ns.span.End()
   116  	}
   117  }