forked from dgraph-io/dgraph
/
assign.go
147 lines (126 loc) · 3.84 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
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
/*
* 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 zero
import (
"errors"
"golang.org/x/net/context"
"github.com/dgraph-io/dgraph/protos/api"
"github.com/dgraph-io/dgraph/protos/intern"
"github.com/dgraph-io/dgraph/x"
)
var (
emptyNum intern.Num
emptyAssignedIds api.AssignedIds
)
const (
leaseBandwidth = uint64(10000)
)
func (s *Server) updateLeases() {
var startTs uint64
s.Lock()
s.nextLeaseId = s.state.MaxLeaseId + 1
s.nextTxnTs = s.state.MaxTxnTs + 1
startTs = s.nextTxnTs
s.Unlock()
s.orc.updateStartTxnTs(startTs)
}
func (s *Server) maxLeaseId() uint64 {
s.RLock()
defer s.RUnlock()
return s.state.MaxLeaseId
}
func (s *Server) maxTxnTs() uint64 {
s.RLock()
defer s.RUnlock()
return s.state.MaxTxnTs
}
// lease would either allocate ids or timestamps.
// 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) lease(ctx context.Context, num *intern.Num, txn bool) (*api.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 IDs 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 || s.nextTxnTs == 0 {
return nil, errors.New("Server not initialized.")
}
var maxLease, available uint64
var proposal intern.ZeroProposal
if txn {
maxLease = s.maxTxnTs()
available = maxLease - s.nextTxnTs + 1
proposal.MaxTxnTs = maxLease + howMany
} else {
maxLease = s.maxLeaseId()
available = maxLease - s.nextLeaseId + 1
proposal.MaxLeaseId = maxLease + howMany
}
if available < num.Val {
// Blocking propose to get more ids or timestamps.
if err := s.Node.proposeAndWait(ctx, &proposal); err != nil {
return nil, err
}
}
out := &api.AssignedIds{}
if txn {
out.StartId = s.nextTxnTs
out.EndId = out.StartId + num.Val - 1
s.nextTxnTs = out.EndId + 1
s.orc.doneUntil.Begin(out.EndId)
} else {
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 *intern.Num) (*api.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.lease(ctx, num, false)
c <- err
}()
select {
case <-ctx.Done():
return reply, ctx.Err()
case err := <-c:
return reply, err
}
}