@@ -118,8 +118,11 @@ func (s *DBService) CreateDatabase(req *CreateDatabaseRequest, resp *CreateDatab
118118 DatabaseID : dbID ,
119119 Peers : peers ,
120120 ResourceMeta : req .ResourceMeta ,
121+ GenesisBlock : genesisBlock ,
121122 }
122123
124+ log .Debugf ("generated instance meta: %v" , instanceMeta )
125+
123126 if err = s .ServiceMap .Set (instanceMeta ); err != nil {
124127 // critical error
125128 // TODO(xq262144): critical error recover
@@ -192,32 +195,42 @@ func (s *DBService) GetDatabase(req *GetDatabaseRequest, resp *GetDatabaseRespon
192195func (s * DBService ) GetNodeDatabases (req * wt.InitService , resp * wt.InitServiceResponse ) (err error ) {
193196 // fetch from meta
194197 var instances []wt.ServiceInstance
195- if instances , err = s .ServiceMap .GetDatabases (proto . NodeID ( req .GetNodeID ().String () )); err != nil {
198+ if instances , err = s .ServiceMap .GetDatabases (req .GetNodeID ().ToNodeID ( )); err != nil {
196199 return
197200 }
198201
202+ log .Debugf ("current instance for node %v: %v" , req .GetNodeID ().ToNodeID (), instances )
203+
199204 // send response to client
200205 resp .Instances = instances
201206
202207 return
203208}
204209
205210func (s * DBService ) generateDatabaseID (reqNodeID * proto.RawNodeID ) (dbID proto.DatabaseID , err error ) {
206- nonceCh := make (chan cpuminer.NonceInfo )
207- quitCh := make (chan struct {})
208- miner := cpuminer .NewCPUMiner (quitCh )
209- go miner .ComputeBlockNonce (cpuminer.MiningBlock {
210- Data : reqNodeID .CloneBytes (),
211- NonceChan : nonceCh ,
212- Stop : nil ,
213- }, cpuminer.Uint256 {}, 4 )
214-
215- defer close (nonceCh )
216- defer close (quitCh )
217-
218- for nonce := range nonceCh {
211+ var startNonce cpuminer.Uint256
212+
213+ for {
214+ nonceCh := make (chan cpuminer.NonceInfo )
215+ quitCh := make (chan struct {})
216+ miner := cpuminer .NewCPUMiner (quitCh )
217+ go miner .ComputeBlockNonce (cpuminer.MiningBlock {
218+ Data : reqNodeID .CloneBytes (),
219+ NonceChan : nonceCh ,
220+ Stop : nil ,
221+ }, startNonce , 4 )
222+
223+ nonce := <- nonceCh
224+ close (quitCh )
225+ close (nonceCh )
226+
227+ // set start nonceCh
228+ startNonce = nonce .Nonce
229+ startNonce .Inc ()
219230 dbID = proto .DatabaseID (nonce .Hash .String ())
220231
232+ log .Debugf ("try generated database id %v" , dbID )
233+
221234 // check existence
222235 if _ , err = s .ServiceMap .Get (dbID ); err == ErrNoSuchDatabase {
223236 err = nil
@@ -231,7 +244,7 @@ func (s *DBService) generateDatabaseID(reqNodeID *proto.RawNodeID) (dbID proto.D
231244func (s * DBService ) allocateNodes (lastTerm uint64 , dbID proto.DatabaseID , resourceMeta wt.ResourceMeta ) (peers * kayak.Peers , err error ) {
232245 curRange := int (resourceMeta .Node )
233246 excludeNodes := make (map [proto.NodeID ]bool )
234- allocated := make ( []proto.NodeID , 0 )
247+ var allocated []proto.NodeID
235248
236249 if resourceMeta .Node <= 0 {
237250 err = ErrDatabaseAllocation
@@ -252,10 +265,17 @@ func (s *DBService) allocateNodes(lastTerm uint64, dbID proto.DatabaseID, resour
252265
253266 // clear previous allocated
254267 allocated = allocated [:0 ]
268+ rolesFilter := []proto.ServerRole {
269+ proto .Miner ,
270+ }
255271
256- nodes , err = s .Consistent .GetNeighbors (string (dbID ), curRange )
272+ if s .includeBPNodesForAllocation {
273+ rolesFilter = append (rolesFilter , proto .Leader , proto .Follower )
274+ }
275+
276+ nodes , err = s .Consistent .GetNeighborsEx (string (dbID ), curRange , proto .ServerRoles (rolesFilter ))
257277
258- log .Debugf ("found %d neighbour nodes" , len (nodes ))
278+ log .Debugf ("found %d neighbor nodes" , len (nodes ))
259279
260280 // TODO(xq262144): brute force implementation to be optimized
261281 var nodeIDs []proto.NodeID
@@ -266,7 +286,7 @@ func (s *DBService) allocateNodes(lastTerm uint64, dbID proto.DatabaseID, resour
266286 }
267287 }
268288
269- log .Debugf ("found %d suitable nodes" , len (nodeIDs ))
289+ log .Debugf ("found %d suitable nodes: %v " , len (nodeIDs ), nodeIDs )
270290
271291 if len (nodeIDs ) < int (resourceMeta .Node ) {
272292 continue
@@ -341,6 +361,7 @@ func (s *DBService) getMetric(metric metric.MetricMap, keys []string) (value uin
341361}
342362
343363func (s * DBService ) buildPeers (term uint64 , nodes []proto.Node , allocated []proto.NodeID ) (peers * kayak.Peers , err error ) {
364+ log .Debugf ("build peers for allocated nodes with term: %v, allocated nodes: %v" , term , allocated )
344365 // get local private key
345366 var pubKey * asymmetric.PublicKey
346367 if pubKey , err = kms .GetLocalPublicKey (); err != nil {
@@ -362,7 +383,9 @@ func (s *DBService) buildPeers(term uint64, nodes []proto.Node, allocated []prot
362383 allocatedNodes := make ([]proto.Node , 0 , len (allocated ))
363384
364385 for _ , node := range nodes {
365- allocatedNodes = append (allocatedNodes , node )
386+ if allocatedMap [node .ID ] {
387+ allocatedNodes = append (allocatedNodes , node )
388+ }
366389 }
367390
368391 peers = & kayak.Peers {
0 commit comments