forked from dgraph-io/dgraph
-
Notifications
You must be signed in to change notification settings - Fork 1
/
assign.go
122 lines (102 loc) · 3.28 KB
/
assign.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
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
/*
* Copyright (C) 2017 Dgraph Labs, Inc. and Contributors
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
package main
import (
"errors"
"golang.org/x/net/context"
"gopkg.in/adibiarsotp/dgraph.v83/protos"
"gopkg.in/adibiarsotp/dgraph.v83/x"
)
var (
emptyNum protos.Num
emptyAssignedIds protos.AssignedIds
)
const (
leaseBandwidth = uint64(10000)
)
func (s *Server) updateNextLeaseId() {
s.Lock()
defer s.Unlock()
s.nextLeaseId = s.state.MaxLeaseId + 1
}
func (s *Server) maxLeaseId() uint64 {
s.RLock()
defer s.RUnlock()
return s.state.MaxLeaseId
}
// assignUids returns a byte slice containing uids.
// This function is triggered by an RPC call. We ensure that only leader can assign new UIDs,
// so we can tackle any collisions that might happen with the leasemanager
// In essence, we just want one server to be handing out new uids.
func (s *Server) assignUids(ctx context.Context, num *protos.Num) (*protos.AssignedIds, error) {
node := s.Node
// TODO: Fix when we move to linearizable reads, need to check if we are the leader, might be
// based on leader leases. If this node gets partitioned and unless checkquorum is enabled, this
// node would still think that it's the leader.
if !node.AmLeader() {
return &emptyAssignedIds, x.Errorf("Assigning UIDs is only allowed on leader.")
}
val := int(num.Val)
if val == 0 {
return &emptyAssignedIds, x.Errorf("Nothing to be marked or assigned")
}
s.leaseLock.Lock()
defer s.leaseLock.Unlock()
howMany := leaseBandwidth
if num.Val > leaseBandwidth {
howMany = num.Val + leaseBandwidth
}
if s.nextLeaseId == 0 {
return nil, errors.New("Server not initialized.")
}
maxLease := s.maxLeaseId()
available := maxLease - s.nextLeaseId + 1
if available < num.Val {
var proposal protos.ZeroProposal
proposal.MaxLeaseId = maxLease + howMany
if err := s.Node.proposeAndWait(ctx, &proposal); err != nil {
return nil, err
}
x.AssertTrue(s.maxLeaseId() == proposal.MaxLeaseId)
}
out := &protos.AssignedIds{}
out.StartId = s.nextLeaseId
out.EndId = out.StartId + num.Val - 1
s.nextLeaseId = out.EndId + 1
return out, nil
}
// AssignUids is used to assign new uids by communicating with the leader of the RAFT group
// responsible for handing out uids.
func (s *Server) AssignUids(ctx context.Context, num *protos.Num) (*protos.AssignedIds, error) {
if ctx.Err() != nil {
return &emptyAssignedIds, ctx.Err()
}
// TODO: Forward it to the leader, if I'm not the leader.
reply := &emptyAssignedIds
c := make(chan error, 1)
go func() {
var err error
reply, err = s.assignUids(ctx, num)
c <- err
}()
select {
case <-ctx.Done():
return reply, ctx.Err()
case err := <-c:
return reply, err
}
}