simplify request statuses, eliminate blank and timedout, no propagate when chunk is found

This commit is contained in:
zelig 2015-05-13 18:12:09 +02:00
parent 26c7023120
commit 688f95ff5b

View file

@ -19,17 +19,13 @@ type NetStore struct {
/* /*
request status values: request status values:
- blank
- started searching - started searching
- timed out
- found - found
*/ */
const ( const (
reqBlank = iota reqSearching = iota // after search for chunk started until found or timed out
reqSearching reqFound // chunk found search terminated
reqTimedOut
reqFound
) )
const ( const (
@ -88,10 +84,12 @@ func (self *NetStore) Put(entry *Chunk) {
func (self *NetStore) put(entry *Chunk) { func (self *NetStore) put(entry *Chunk) {
self.localStore.Put(entry) self.localStore.Put(entry)
dpaLogger.Debugf("NetStore.put: localStore.Put of %064x completed, %d bytes (%p).", entry.Key, len(entry.SData), entry) dpaLogger.Debugf("NetStore.put: localStore.Put of %064x completed, %d bytes (%p).", entry.Key, len(entry.SData), entry)
if entry.req != nil && entry.req.status == reqSearching { if entry.req != nil {
if entry.req.status == reqSearching {
entry.req.status = reqFound entry.req.status = reqFound
close(entry.req.C) close(entry.req.C)
self.propagateResponse(entry) self.propagateResponse(entry)
}
} else { } else {
go self.store(entry) go self.store(entry)
} }
@ -137,7 +135,6 @@ func (self *NetStore) Get(key Key) (chunk *Chunk, err error) {
err = notFound err = notFound
case <-chunk.req.C: case <-chunk.req.C:
dpaLogger.Debugf("NetStore.Get: %064x retrieved, %d bytes (%p)", key, len(chunk.SData), chunk) dpaLogger.Debugf("NetStore.Get: %064x retrieved, %d bytes (%p)", key, len(chunk.SData), chunk)
} }
return return
} }
@ -169,31 +166,25 @@ func (self *NetStore) addRetrieveRequest(req *retrieveRequestMsgData) {
chunk := self.get(req.Key) chunk := self.get(req.Key)
if chunk.SData == nil { if chunk.SData == nil {
chunk.req.status = reqSearching t := time.Now().Add(10 * time.Second)
req.timeout = &t
} else { } else {
chunk.req.status = reqFound chunk.req.status = reqFound
} }
t := time.Now().Add(10 * time.Second) timeout := self.strategyUpdateRequest(chunk.req, req) // may change req status
req.timeout = &t
send, timeout := self.strategyUpdateRequest(chunk.req, req) // may change req status if timeout == nil {
if send == storeRequestMsg {
dpaLogger.Debugf("NetStore.addRetrieveRequest: %064x - content found, delivering...", req.Key) dpaLogger.Debugf("NetStore.addRetrieveRequest: %064x - content found, delivering...", req.Key)
self.deliver(req, chunk) self.deliver(req, chunk)
} else { } else {
// we might need chunk.req to cache relevant peers response, or would it expire? // we might need chunk.req to cache relevant peers response, or would it expire?
self.peers(req, chunk, timeout) self.peers(req, chunk, timeout)
dpaLogger.Debugf("NetStore.addRetrieveRequest: %064x - searching.... responding with peers...", req.Key) dpaLogger.Debugf("NetStore.addRetrieveRequest: %064x - searching.... responding with peers...", req.Key)
if timeout != nil {
self.startSearch(chunk, int64(req.Id), timeout) self.startSearch(chunk, int64(req.Id), timeout)
} }
} }
}
// it's assumed that caller holds the lock // it's assumed that caller holds the lock
func (self *NetStore) startSearch(chunk *Chunk, id int64, timeout *time.Time) { func (self *NetStore) startSearch(chunk *Chunk, id int64, timeout *time.Time) {
chunk.req.status = reqSearching chunk.req.status = reqSearching
@ -238,17 +229,11 @@ this is the most simplistic implementation:
- respond with peers and timeout if still searching - respond with peers and timeout if still searching
! in the last case as well, we should respond with reject if already got requesterCount peers with that exact id ! in the last case as well, we should respond with reject if already got requesterCount peers with that exact id
*/ */
func (self *NetStore) strategyUpdateRequest(rs *requestStatus, req *retrieveRequestMsgData) (msgTyp int, timeout *time.Time) { func (self *NetStore) strategyUpdateRequest(rs *requestStatus, req *retrieveRequestMsgData) (timeout *time.Time) {
dpaLogger.Debugf("NetStore.strategyUpdateRequest: key %064x", req.Key) dpaLogger.Debugf("NetStore.strategyUpdateRequest: key %064x", req.Key)
switch rs.status { if rs.status == reqSearching {
case reqSearching:
msgTyp = peersMsg
timeout = self.searchTimeout(rs, req) timeout = self.searchTimeout(rs, req)
case reqTimedOut:
msgTyp = peersMsg
case reqFound:
msgTyp = storeRequestMsg
} }
return return