|
| 1 | +package dht |
| 2 | + |
| 3 | +import ( |
| 4 | + "context" |
| 5 | + "math/rand" |
| 6 | + "strings" |
| 7 | + "sync" |
| 8 | + "time" |
| 9 | + |
| 10 | + "github.com/gogo/protobuf/proto" |
| 11 | + ds "github.com/ipfs/go-datastore" |
| 12 | + "github.com/ipfs/go-datastore/query" |
| 13 | + u "github.com/ipfs/go-ipfs-util" |
| 14 | + "github.com/jbenet/goprocess" |
| 15 | + "github.com/jbenet/goprocess/context" |
| 16 | + "github.com/libp2p/go-libp2p-kad-dht/providers" |
| 17 | + recpb "github.com/libp2p/go-libp2p-record/pb" |
| 18 | +) |
| 19 | + |
| 20 | +// vars for cleaning up expired records |
| 21 | +var nonProvRecordCleanupInterval = time.Hour |
| 22 | + |
| 23 | +// maxNonProvRecordAge specifies the maximum time that any node will hold onto a record |
| 24 | +// from the time its received. This does not apply to any other forms of validity that |
| 25 | +// the record may contain. |
| 26 | +// For example, a record may contain an ipns entry with an EOL saying its valid |
| 27 | +// until the year 2020 (a great time in the future). For that record to stick around |
| 28 | +// it must be rebroadcasted more frequently than once every 'maxNonProvRecordAge' |
| 29 | +var maxNonProvRecordAge = time.Hour * 36 |
| 30 | + |
| 31 | +// vars for republishing records |
| 32 | +var nonProvRecordRePublishInterval = 1 * time.Hour |
| 33 | +var nonProvRecordRePublishAge = 1 * time.Hour |
| 34 | +var enableRepublishJitter = true |
| 35 | + |
| 36 | +type NonProvRecordsManager struct { |
| 37 | + dht *IpfsDHT |
| 38 | + ctx context.Context |
| 39 | + |
| 40 | + proc goprocess.Process |
| 41 | + dstore ds.Batching |
| 42 | + |
| 43 | + cleanupInterval time.Duration // scan interval for expiring records |
| 44 | + |
| 45 | + rePublishInterval time.Duration // scan interval for republishing records |
| 46 | +} |
| 47 | + |
| 48 | +func NewNonProvRecordsManager(ctx context.Context, dht *IpfsDHT, dstore ds.Batching) *NonProvRecordsManager { |
| 49 | + m := new(NonProvRecordsManager) |
| 50 | + m.dht = dht |
| 51 | + m.ctx = ctx |
| 52 | + m.dstore = dstore |
| 53 | + m.proc = goprocessctx.WithContext(ctx) |
| 54 | + |
| 55 | + // expire records beyond maxage |
| 56 | + m.cleanupInterval = nonProvRecordCleanupInterval |
| 57 | + m.proc.Go(m.expire) |
| 58 | + |
| 59 | + // republish records older than prescribed age |
| 60 | + m.rePublishInterval = nonProvRecordRePublishInterval |
| 61 | + m.proc.Go(m.rePublish) |
| 62 | + |
| 63 | + return m |
| 64 | +} |
| 65 | + |
| 66 | +func (m *NonProvRecordsManager) Process() goprocess.Process { |
| 67 | + return m.proc |
| 68 | +} |
| 69 | + |
| 70 | +func (m *NonProvRecordsManager) rePublish(proc goprocess.Process) { |
| 71 | + for { |
| 72 | + var d = 0 * time.Minute |
| 73 | + // minimizes the probability of all peers re-publishing together |
| 74 | + // the first peer that re-publishes resets the receivedAt time on the record |
| 75 | + // on all other peers that are among the K closest to the key, thus minimizing the number of republishes by other peers |
| 76 | + if enableRepublishJitter { |
| 77 | + d = time.Duration(rand.Intn(16)) * time.Minute |
| 78 | + } |
| 79 | + |
| 80 | + select { |
| 81 | + case <-proc.Closing(): |
| 82 | + return |
| 83 | + case <-time.After(m.rePublishInterval + d): |
| 84 | + } |
| 85 | + |
| 86 | + tFnc := func(t time.Time) bool { |
| 87 | + return time.Since(t) > nonProvRecordRePublishAge && time.Since(t) < maxNonProvRecordAge |
| 88 | + } |
| 89 | + |
| 90 | + res, err := m.dstore.Query(query.Query{Filters: []query.Filter{&nonProvRecordFilter{tFnc}}}) |
| 91 | + if err != nil { |
| 92 | + logger.Errorf("republish records proc: failed to run query against datastore, error is %s", err) |
| 93 | + continue |
| 94 | + } |
| 95 | + |
| 96 | + var wg sync.WaitGroup |
| 97 | + // semaphore to rate-limit number of concurrent PutValue calls |
| 98 | + semaphore := make(chan struct{}, 5) |
| 99 | + for { |
| 100 | + e, ok := res.NextSync() |
| 101 | + if !ok { |
| 102 | + break |
| 103 | + } |
| 104 | + |
| 105 | + semaphore <- struct{}{} |
| 106 | + wg.Add(1) |
| 107 | + go func(e query.Result) { |
| 108 | + defer func() { |
| 109 | + <-semaphore |
| 110 | + wg.Done() |
| 111 | + }() |
| 112 | + |
| 113 | + // unmarshal record |
| 114 | + rec := new(recpb.Record) |
| 115 | + if err := proto.Unmarshal(e.Value, rec); err != nil { |
| 116 | + logger.Debugf("republish records proc: failed to unmarshal DHT record from datastore, error is %s", err) |
| 117 | + return |
| 118 | + } |
| 119 | + |
| 120 | + // call put value |
| 121 | + putCtx, cancel := context.WithTimeout(m.ctx, 2*time.Minute) |
| 122 | + defer cancel() |
| 123 | + |
| 124 | + // do not use e.key here as that represents the transformed version of the original key |
| 125 | + // rec.GetKey is the original key sent by the peer who put this record to dht |
| 126 | + if err := m.dht.PutValue(putCtx, string(rec.GetKey()), rec.Value); err != nil { |
| 127 | + logger.Debugf("republish records proc: failed to re-publish to the network, error is %s", err) |
| 128 | + } |
| 129 | + }(e) |
| 130 | + } |
| 131 | + wg.Wait() |
| 132 | + } |
| 133 | +} |
| 134 | + |
| 135 | +func (m *NonProvRecordsManager) expire(proc goprocess.Process) { |
| 136 | + for { |
| 137 | + select { |
| 138 | + case <-proc.Closing(): |
| 139 | + return |
| 140 | + case <-time.After(m.cleanupInterval): |
| 141 | + } |
| 142 | + |
| 143 | + tFnc := func(t time.Time) bool { |
| 144 | + return time.Since(t) > maxNonProvRecordAge |
| 145 | + } |
| 146 | + |
| 147 | + res, err := m.dstore.Query(query.Query{Filters: []query.Filter{&nonProvRecordFilter{tFnc}}}) |
| 148 | + if err != nil { |
| 149 | + logger.Errorf("expire records proc: failed to run query against datastore, error is %s", err) |
| 150 | + continue |
| 151 | + } |
| 152 | + |
| 153 | + for { |
| 154 | + e, ok := res.NextSync() |
| 155 | + if !ok { |
| 156 | + break |
| 157 | + } |
| 158 | + if err := m.dstore.Delete(ds.RawKey(e.Key)); err != nil { |
| 159 | + logger.Errorf("expire records proc: failed to delete key %s from datastore, error is %s", e.Key, err) |
| 160 | + } |
| 161 | + } |
| 162 | + } |
| 163 | +} |
| 164 | + |
| 165 | +type timeFilterFnc = func(t time.Time) bool |
| 166 | + |
| 167 | +type nonProvRecordFilter struct { |
| 168 | + tFnc timeFilterFnc |
| 169 | +} |
| 170 | + |
| 171 | +func (f *nonProvRecordFilter) Filter(e query.Entry) bool { |
| 172 | + // unmarshal record |
| 173 | + rec := new(recpb.Record) |
| 174 | + if err := proto.Unmarshal(e.Value, rec); err != nil { |
| 175 | + logger.Debugf("expire records filter: failed to unmarshal DHT record from datastore, error is %s", err) |
| 176 | + return false |
| 177 | + } |
| 178 | + |
| 179 | + // should not be a provider record |
| 180 | + if strings.HasPrefix(e.Key, providers.ProvidersKeyPrefix) { |
| 181 | + return false |
| 182 | + } |
| 183 | + |
| 184 | + // parse received time |
| 185 | + t, err := u.ParseRFC3339(rec.TimeReceived) |
| 186 | + if err != nil { |
| 187 | + logger.Debugf("expire records filter: failed to parse time in DHT record, error is %s", err) |
| 188 | + return false |
| 189 | + } |
| 190 | + |
| 191 | + // apply the time filter fnc to the received time |
| 192 | + return f.tFnc(t) |
| 193 | +} |
0 commit comments