func (s *scheduler) NotifyAndFindParent(ctx context.Context, peer *resource.Peer, blocklist set.SafeSet) ([]*resource.Peer, bool) {
	// Only PeerStateRunning peers need to be rescheduled,
	// and other states including the PeerStateBackToSource indicate that
	// they have been scheduled
	if !peer.FSM.Is(resource.PeerStateRunning) {
		peer.Log.Infof("peer state is %s, can not schedule parent", peer.FSM.Current())
		return []*resource.Peer{}, false
	}

	// Find the parent that can be scheduled
	parents := s.filterParents(peer, blocklist)
	if len(parents) == 0 {
		peer.Log.Info("can not find parents")
		return []*resource.Peer{}, false
	}

	// Sort parents by evaluation score
	taskTotalPieceCount := peer.Task.TotalPieceCount.Load()
	sort.Slice(
		parents,
		func(i, j int) bool {
			return s.evaluator.Evaluate(parents[i], peer, taskTotalPieceCount) > s.evaluator.Evaluate(parents[j], peer, taskTotalPieceCount)
		},
	)

	// Send scheduling success message
	stream, ok := peer.LoadStream()
	if !ok {
		peer.Log.Error("load peer stream failed")
		return []*resource.Peer{}, false
	}

	if err := stream.Send(constructSuccessPeerPacket(peer, parents[0], parents[1:])); err != nil {
		peer.Log.Error(err)
		return []*resource.Peer{}, false
	}

	peer.ReplaceParent(parents[0])
	peer.Log.Infof("schedule parent successful, replace parent to %s", parents[0].ID)
	return parents, true
}
// NotifyAndFindParent finds parent that best matches the evaluation and notify peer
NotifyAndFindParent finds parent that best matches the evaluation and notify peer
// NotifyAndFindParent finds parent that best matches the evaluation and notify peer
func (s *scheduler) NotifyAndFindParent(ctx context.Context, peer *resource.Peer, blocklist set.SafeSet) ([]*resource.Peer, bool) {
	if !peer.FSM.Is(resource.PeerStateRunning) {
		peer.Log.Infof("peer state is %s, can not schedule parent", peer.FSM.Current())
		return []*resource.Peer{}, false
	}
	parents := s.filterParents(peer, blocklist)
	if len(parents) == 0 {
		peer.Log.Info("can not find parents")
		return []*resource.Peer{}, false
	}
	taskTotalPieceCount := peer.Task.TotalPieceCount.Load()
	sort.Slice(
		parents,
		func(i, j int) bool {
			return s.evaluator.Evaluate(parents[i], peer, taskTotalPieceCount) > s.evaluator.Evaluate(parents[j], peer, taskTotalPieceCount)
		},
	)
	stream, ok := peer.LoadStream()
	if !ok {
		peer.Log.Error("load peer stream failed")
		return []*resource.Peer{}, false
	}
	if err := stream.Send(constructSuccessPeerPacket(peer, parents[0], parents[1:])); err != nil {
		peer.Log.Error(err)
		return []*resource.Peer{}, false
	}
	peer.ReplaceParent(parents[0])
	peer.Log.Infof("schedule parent successful, replace parent to %s", parents[0].ID)
	return parents, true
}
func ( s * scheduler ) NotifyAndFindParent ( ctx context . Context , peer * resource . Peer , blocklist set . SafeSet ) ( [ ] * resource . Peer , bool ) { if ! peer . FSM . Is ( resource . PeerStateRunning ) { peer . Log . Infof ( " " , peer . FSM . Current ( ) ) return [ ] * resource . Peer { } , false } parents := s . filterParents ( peer , blocklist ) if len ( parents ) == 0 { peer . Log . Info ( " " ) return [ ] * resource . Peer { } , false } taskTotalPieceCount := peer . Task . TotalPieceCount . Load ( ) sort . Slice ( parents , func ( i , j int ) bool { return s . evaluator . Evaluate ( parents [ i ] , peer , taskTotalPieceCount ) > s . evaluator . Evaluate ( parents [ j ] , peer , taskTotalPieceCount ) } , ) stream , ok := peer . LoadStream ( ) if ! ok { peer . Log . Error ( " " ) return [ ] * resource . Peer { } , false } if err := stream . Send ( constructSuccessPeerPacket ( peer , parents [ 0 ] , parents [ 1 : ] ) ) ; err != nil { peer . Log . Error ( err ) return [ ] * resource . Peer { } , false } peer . ReplaceParent ( parents [ 0 ] ) peer . Log . Infof ( " " , parents [ 0 ] . ID ) return parents , true }
NotifyAndFindParent finds parent that best matches the evaluation and notify peer
NotifyAndFindParent finds parent that best matches the evaluation and notify peer
// Only PeerStateRunning peers need to be rescheduled, // and other states including the PeerStateBackToSource indicate that // they have been scheduled // Find the parent that can be scheduled // Sort parents by evaluation score // Send scheduling success message