@@ -36,24 +36,30 @@ const (
3636func (s * Server ) updateLeases () {
3737 var startTs uint64
3838 s .Lock ()
39- s .nextLeaseId = s .state .MaxLeaseId + 1
40- s .nextTxnTs = s .state .MaxTxnTs + 1
41- startTs = s .nextTxnTs
42- glog .Infof ("Updated Lease id: %d. Txn Ts: %d" , s .nextLeaseId , s .nextTxnTs )
39+ s .nextLease [pb .Num_UID ] = s .state .MaxUID + 1
40+ s .nextLease [pb .Num_TXN_TS ] = s .state .MaxTxnTs + 1
41+ s .nextLease [pb .Num_NS_ID ] = s .state .MaxNsID + 1
42+
43+ startTs = s .nextLease [pb .Num_TXN_TS ]
44+ glog .Infof ("Updated UID: %d. Txn Ts: %d. NsID: %d." ,
45+ s .nextLease [pb .Num_UID ], s .nextLease [pb .Num_TXN_TS ], s .nextLease [pb .Num_NS_ID ])
4346 s .Unlock ()
4447 s .orc .updateStartTxnTs (startTs )
4548}
4649
47- func (s * Server ) maxLeaseId () uint64 {
48- s .RLock ()
49- defer s .RUnlock ()
50- return s .state .MaxLeaseId
51- }
52-
53- func (s * Server ) maxTxnTs () uint64 {
50+ func (s * Server ) maxLease (typ pb.NumLeaseType ) uint64 {
5451 s .RLock ()
5552 defer s .RUnlock ()
56- return s .state .MaxTxnTs
53+ var maxlease uint64
54+ switch typ {
55+ case pb .Num_UID :
56+ maxlease = s .state .MaxUID
57+ case pb .Num_TXN_TS :
58+ maxlease = s .state .MaxTxnTs
59+ case pb .Num_NS_ID :
60+ maxlease = s .state .MaxNsID
61+ }
62+ return maxlease
5763}
5864
5965var errServedFromMemory = errors .New ("Lease was served from memory" )
@@ -62,7 +68,8 @@ var errServedFromMemory = errors.New("Lease was served from memory")
6268// This function is triggered by an RPC call. We ensure that only leader can assign new UIDs,
6369// so we can tackle any collisions that might happen with the leasemanager
6470// In essence, we just want one server to be handing out new uids.
65- func (s * Server ) lease (ctx context.Context , num * pb.Num , txn bool ) (* pb.AssignedIds , error ) {
71+ func (s * Server ) lease (ctx context.Context , num * pb.Num ) (* pb.AssignedIds , error ) {
72+ typ := num .GetType ()
6673 node := s .Node
6774 // TODO: Fix when we move to linearizable reads, need to check if we are the leader, might be
6875 // based on leader leases. If this node gets partitioned and unless checkquorum is enabled, this
@@ -75,21 +82,21 @@ func (s *Server) lease(ctx context.Context, num *pb.Num, txn bool) (*pb.Assigned
7582 return & emptyAssignedIds , errors .Errorf ("Nothing to be leased" )
7683 }
7784 if glog .V (3 ) {
78- glog .Infof ("Got lease request for txn : %v. Num: %+v\n " , txn , num )
85+ glog .Infof ("Got lease request for Type : %v. Num: %+v\n " , typ , num )
7986 }
8087
8188 s .leaseLock .Lock ()
8289 defer s .leaseLock .Unlock ()
8390
84- if txn {
91+ if typ == pb . Num_TXN_TS {
8592 if num .Val == 0 && num .ReadOnly {
8693 // If we're only asking for a readonly timestamp, we can potentially
8794 // service it directly.
8895 if glog .V (3 ) {
8996 glog .Infof ("Attempting to serve read only txn ts [%d, %d]" ,
90- s .readOnlyTs , s .nextTxnTs )
97+ s .readOnlyTs , s .nextLease [ pb . Num_TXN_TS ] )
9198 }
92- if s .readOnlyTs > 0 && s .readOnlyTs == s .nextTxnTs - 1 {
99+ if s .readOnlyTs > 0 && s .readOnlyTs == s .nextLease [ pb . Num_TXN_TS ] - 1 {
93100 return & pb.AssignedIds {ReadOnly : s .readOnlyTs }, errServedFromMemory
94101 }
95102 }
@@ -106,23 +113,24 @@ func (s *Server) lease(ctx context.Context, num *pb.Num, txn bool) (*pb.Assigned
106113 howMany = num .Val + leaseBandwidth
107114 }
108115
109- if s .nextLeaseId == 0 || s .nextTxnTs == 0 {
116+ if s .nextLease [pb .Num_UID ] == 0 || s .nextLease [pb .Num_TXN_TS ] == 0 ||
117+ s .nextLease [pb .Num_NS_ID ] == 0 {
110118 return nil , errors .New ("Server not initialized" )
111119 }
112120
113- var maxLease , available uint64
114121 var proposal pb.ZeroProposal
115122
116123 // Calculate how many ids do we have available in memory, before we need to
117124 // renew our lease.
118- if txn {
119- maxLease = s .maxTxnTs ()
120- available = maxLease - s .nextTxnTs + 1
125+ maxLease := s .maxLease (typ )
126+ available := maxLease - s .nextLease [typ ] + 1
127+ switch typ {
128+ case pb .Num_TXN_TS :
121129 proposal .MaxTxnTs = maxLease + howMany
122- } else {
123- maxLease = s . maxLeaseId ()
124- available = maxLease - s . nextLeaseId + 1
125- proposal .MaxLeaseId = maxLease + howMany
130+ case pb . Num_UID :
131+ proposal . MaxUID = maxLease + howMany
132+ case pb . Num_NS_ID :
133+ proposal .MaxNsID = maxLease + howMany
126134 }
127135
128136 // If we have less available than what we need, we need to renew our lease.
@@ -134,48 +142,55 @@ func (s *Server) lease(ctx context.Context, num *pb.Num, txn bool) (*pb.Assigned
134142 }
135143
136144 out := & pb.AssignedIds {}
137- if txn {
145+ if typ == pb . Num_TXN_TS {
138146 if num .Val > 0 {
139- out .StartId = s .nextTxnTs
147+ out .StartId = s .nextLease [ pb . Num_TXN_TS ]
140148 out .EndId = out .StartId + num .Val - 1
141- s .nextTxnTs = out .EndId + 1
149+ s .nextLease [ pb . Num_TXN_TS ] = out .EndId + 1
142150 }
143151 if num .ReadOnly {
144- s .readOnlyTs = s .nextTxnTs
145- s .nextTxnTs ++
152+ s .readOnlyTs = s .nextLease [ pb . Num_TXN_TS ]
153+ s .nextLease [ pb . Num_TXN_TS ] ++
146154 out .ReadOnly = s .readOnlyTs
147155 }
148156 s .orc .doneUntil .Begin (x .Max (out .EndId , out .ReadOnly ))
149- } else {
150- out .StartId = s .nextLeaseId
157+ } else if typ == pb .Num_UID {
158+ out .StartId = s .nextLease [pb .Num_UID ]
159+ out .EndId = out .StartId + num .Val - 1
160+ s .nextLease [pb .Num_UID ] = out .EndId + 1
161+ } else if typ == pb .Num_NS_ID {
162+ out .StartId = s .nextLease [pb .Num_NS_ID ]
151163 out .EndId = out .StartId + num .Val - 1
152- s .nextLeaseId = out .EndId + 1
164+ s .nextLease [pb .Num_NS_ID ] = out .EndId + 1
165+
166+ } else {
167+ return out , errors .Errorf ("Unknown lease type: %v\n " , typ )
153168 }
154169 return out , nil
155170}
156171
157- // AssignUids is used to assign new uids by communicating with the leader of the RAFT group
158- // responsible for handing out uids .
159- func (s * Server ) AssignUids (ctx context.Context , num * pb.Num ) (* pb.AssignedIds , error ) {
172+ // AssignIds is used to assign new ids (UIDs, NsIDs) by communicating with the leader of the
173+ // RAFT group responsible for handing out ids .
174+ func (s * Server ) AssignIds (ctx context.Context , num * pb.Num ) (* pb.AssignedIds , error ) {
160175 if ctx .Err () != nil {
161176 return & emptyAssignedIds , ctx .Err ()
162177 }
163- ctx , span := otrace .StartSpan (ctx , "Zero.AssignUids " )
178+ ctx , span := otrace .StartSpan (ctx , "Zero.AssignIds " )
164179 defer span .End ()
165180
166181 reply := & emptyAssignedIds
167182 lease := func () error {
168183 var err error
169184 if s .Node .AmLeader () {
170185 span .Annotatef (nil , "Zero leader leasing %d ids" , num .GetVal ())
171- reply , err = s .lease (ctx , num , false )
186+ reply , err = s .lease (ctx , num )
172187 return err
173188 }
174189 span .Annotate (nil , "Not Zero leader" )
175190 // I'm not the leader and this request was forwarded to me by a peer, who thought I'm the
176191 // leader.
177192 if num .Forwarded {
178- return errors .Errorf ("Invalid Zero received AssignUids request forward. Please retry" )
193+ return errors .Errorf ("Invalid Zero received AssignIds request forward. Please retry" )
179194 }
180195 // This is an original request. Forward it to the leader.
181196 pl := s .Leader (0 )
@@ -185,7 +200,7 @@ func (s *Server) AssignUids(ctx context.Context, num *pb.Num) (*pb.AssignedIds,
185200 span .Annotatef (nil , "Sending request to %v" , pl .Addr )
186201 zc := pb .NewZeroClient (pl .Get ())
187202 num .Forwarded = true
188- reply , err = zc .AssignUids (ctx , num )
203+ reply , err = zc .AssignIds (ctx , num )
189204 return err
190205 }
191206
0 commit comments