-
Notifications
You must be signed in to change notification settings - Fork 30
/
shard_peers.go
60 lines (49 loc) · 1.67 KB
/
shard_peers.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
package store
import (
"context"
"fmt"
"github.com/chrislusf/glog"
"github.com/chrislusf/vasto/pb"
"github.com/chrislusf/vasto/topology"
"google.golang.org/grpc"
)
func (s *shard) isBootstrapNeeded(ctx context.Context, bootstrapOption *topology.BootstrapPlan) (bestPeerToCopy topology.ClusterShard, isNeeded bool) {
peerShards := bootstrapOption.BootstrapSource
isBootstrapNeededChan := make(chan bool, len(peerShards))
maxSegment := uint32(0)
checkedServerCount := 0
glog.V(1).Infof("shard %s checkBinlogAvailable on peers %v", s, peerShards)
for _, peer := range peerShards {
_, ok := s.cluster.GetNode(peer.ServerId, 0)
if !ok {
continue
}
checkedServerCount++
go func(peer topology.ClusterShard) {
s.cluster.WithConnection(fmt.Sprintf("%s bootstrap_check peer %d.%d", s.String(), peer.ServerId, peer.ShardId), peer.ServerId, func(node *pb.ClusterNode, grpcConnection *grpc.ClientConn) error {
latestSegment, canTailBinlog, err := s.checkBinlogAvailable(ctx, grpcConnection, node)
if err != nil {
isBootstrapNeededChan <- false
glog.V(1).Infof("shard %v checkBinlogAvailable on %s: %v", s.id, node.StoreResource, err)
return err
}
if latestSegment >= maxSegment {
maxSegment = latestSegment
bestPeerToCopy = peer
}
isBootstrapNeededChan <- !canTailBinlog
return nil
})
}(peer)
}
for i := 0; i < checkedServerCount; i++ {
t := <-isBootstrapNeededChan
isNeeded = isNeeded || t
}
if isNeeded {
glog.V(1).Infof("shard %v found peer server %v to bootstrap from", s.String(), bestPeerToCopy)
} else {
glog.V(1).Infof("shard %v found bootstrapping is not needed", s.id)
}
return bestPeerToCopy, isNeeded
}