github.com/braveheart12/insolar-09-08-19@v0.8.7/ledger/heavyclient/sync.go (about)

     1  /*
     2   *    Copyright 2019 Insolar Technologies
     3   *
     4   *    Licensed under the Apache License, Version 2.0 (the "License");
     5   *    you may not use this file except in compliance with the License.
     6   *    You may obtain a copy of the License at
     7   *
     8   *        http://www.apache.org/licenses/LICENSE-2.0
     9   *
    10   *    Unless required by applicable law or agreed to in writing, software
    11   *    distributed under the License is distributed on an "AS IS" BASIS,
    12   *    WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    13   *    See the License for the specific language governing permissions and
    14   *    limitations under the License.
    15   */
    16  
    17  package heavyclient
    18  
    19  import (
    20  	"context"
    21  
    22  	"github.com/insolar/insolar/core"
    23  	"github.com/insolar/insolar/core/message"
    24  	"github.com/insolar/insolar/core/reply"
    25  	"github.com/insolar/insolar/instrumentation/inslogger"
    26  	"github.com/insolar/insolar/ledger/storage"
    27  )
    28  
    29  func messageToHeavy(ctx context.Context, bus core.MessageBus, msg core.Message) error {
    30  	busreply, buserr := bus.Send(ctx, msg, nil)
    31  	if buserr != nil {
    32  		return buserr
    33  	}
    34  	if busreply != nil {
    35  		herr, ok := busreply.(*reply.HeavyError)
    36  		if ok {
    37  			return herr
    38  		}
    39  	}
    40  	return nil
    41  }
    42  
    43  // HeavySync syncs records from light to heavy node, returns last synced pulse and error.
    44  //
    45  // It syncs records from start to end of provided pulse numbers.
    46  func (c *JetClient) HeavySync(
    47  	ctx context.Context,
    48  	pn core.PulseNumber,
    49  	retry bool,
    50  ) error {
    51  	jetID := c.jetID
    52  	inslog := inslogger.FromContext(ctx)
    53  	inslog = inslog.WithField("jetID", jetID.DebugString())
    54  	inslog = inslog.WithField("pulseNum", pn)
    55  
    56  	if retry {
    57  		inslog.Info("synchronize: send reset message (retry sync)")
    58  		resetMsg := &message.HeavyReset{
    59  			JetID:    jetID,
    60  			PulseNum: pn,
    61  		}
    62  		if err := messageToHeavy(ctx, c.bus, resetMsg); err != nil {
    63  			inslog.Error("synchronize: reset failed")
    64  			return err
    65  		}
    66  	}
    67  
    68  	signalMsg := &message.HeavyStartStop{
    69  		JetID:    jetID,
    70  		PulseNum: pn,
    71  	}
    72  	if err := messageToHeavy(ctx, c.bus, signalMsg); err != nil {
    73  		inslog.Error("synchronize: start failed")
    74  		return err
    75  	}
    76  
    77  	replicator := storage.NewReplicaIter(
    78  		ctx, c.db, jetID, pn, pn+1, c.opts.SyncMessageLimit)
    79  	for {
    80  		recs, err := replicator.NextRecords()
    81  		if err == storage.ErrReplicatorDone {
    82  			break
    83  		}
    84  		if err != nil {
    85  			panic(err)
    86  		}
    87  		msg := &message.HeavyPayload{
    88  			JetID:    jetID,
    89  			PulseNum: pn,
    90  			Records:  recs,
    91  		}
    92  		if err := messageToHeavy(ctx, c.bus, msg); err != nil {
    93  			inslog.Error("synchronize: payload failed")
    94  			return err
    95  		}
    96  	}
    97  
    98  	signalMsg.Finished = true
    99  	if err := messageToHeavy(ctx, c.bus, signalMsg); err != nil {
   100  		inslog.Error("synchronize: finish failed")
   101  		return err
   102  	}
   103  
   104  	return nil
   105  }