Returns count which can be used to verify whether we have moved all keys for a predicate or not.
(stream pb.Worker_ReceivePredicateServer)
| 118 | // Returns count which can be used to verify whether we have moved all keys |
| 119 | // for a predicate or not. |
| 120 | func (w *grpcWorker) ReceivePredicate(stream pb.Worker_ReceivePredicateServer) error { |
| 121 | if !groups().Node.AmLeader() { |
| 122 | return errors.Errorf("ReceivePredicate failed: Not the leader of group") |
| 123 | } |
| 124 | // No new deletion/background cleanup would start after we start streaming tablet, |
| 125 | // so all the proposals for a particular tablet would atmost wait for deletion of |
| 126 | // single tablet. Only leader needs to do this. |
| 127 | mu := groups().blockDeletes |
| 128 | mu.Lock() |
| 129 | defer mu.Unlock() |
| 130 | |
| 131 | // Values can be pretty big so having less buffer is safer. |
| 132 | kvs := make(chan *pb.KVS, 3) |
| 133 | che := make(chan error, 1) |
| 134 | // We can use count to check the number of posting lists returned in tests. |
| 135 | count := 0 |
| 136 | ctx := stream.Context() |
| 137 | payload := &api.Payload{} |
| 138 | |
| 139 | glog.Infof("Got ReceivePredicate. Group: %d. Am leader: %v", |
| 140 | groups().groupId(), groups().Node.AmLeader()) |
| 141 | |
| 142 | go func() { |
| 143 | // Takes care of throttling and batching. |
| 144 | che <- batchAndProposeKeyValues(ctx, kvs) |
| 145 | }() |
| 146 | for { |
| 147 | kvBuf, err := stream.Recv() |
| 148 | if err == io.EOF { |
| 149 | payload.Data = []byte(fmt.Sprintf("%d", count)) |
| 150 | if err := stream.SendAndClose(payload); err != nil { |
| 151 | glog.Errorf("Received %d keys. Error in loop: %v", count, err) |
| 152 | return err |
| 153 | } |
| 154 | break |
| 155 | } |
| 156 | if err != nil { |
| 157 | glog.Errorf("Received %d keys. Error in loop: %v", count, err) |
| 158 | return err |
| 159 | } |
| 160 | glog.V(2).Infof("Received batch of size: %s", humanize.IBytes(uint64(len(kvBuf.Data)))) |
| 161 | |
| 162 | buf := z.NewBufferSlice(kvBuf.Data) |
| 163 | if err := buf.SliceIterate(func(_ []byte) error { |
| 164 | count++ |
| 165 | return nil |
| 166 | }); err != nil { |
| 167 | glog.Errorf("error while counting in buf: %v\n", err) |
| 168 | return err |
| 169 | } |
| 170 | |
| 171 | select { |
| 172 | case kvs <- kvBuf: |
| 173 | case <-ctx.Done(): |
| 174 | close(kvs) |
| 175 | <-che |
| 176 | glog.Infof("Received %d keys. Context deadline\n", count) |
| 177 | return ctx.Err() |
nothing calls this directly
no test coverage detected