mirror of
https://github.com/sot-tech/mochi.git
synced 2026-07-29 10:38:11 -07:00
(untested) Merge commit e56ad81 from https://github.com/jzelinskie/chihaya
* rename/replace redis keys
This commit is contained in:
+13
-12
@@ -6,6 +6,7 @@ import (
|
||||
"encoding/binary"
|
||||
"fmt"
|
||||
"math"
|
||||
"net/netip"
|
||||
"reflect"
|
||||
"runtime"
|
||||
"sync"
|
||||
@@ -228,12 +229,12 @@ func (ps *store) getClock() int64 {
|
||||
return timecache.NowUnixNano()
|
||||
}
|
||||
|
||||
func (ps *store) shardIndex(infoHash bittorrent.InfoHash, af bittorrent.AddressFamily) uint32 {
|
||||
func (ps *store) shardIndex(infoHash bittorrent.InfoHash, addr netip.Addr) uint32 {
|
||||
// There are twice the amount of shards specified by the user, the first
|
||||
// half is dedicated to IPv4 swarms and the second half is dedicated to
|
||||
// IPv6 swarms.
|
||||
idx := binary.BigEndian.Uint32([]byte(infoHash[:4])) % (uint32(len(ps.shards)) / 2)
|
||||
if af == bittorrent.IPv6 {
|
||||
if addr.Is6() && !addr.Is4In6() {
|
||||
idx += uint32(len(ps.shards) / 2)
|
||||
}
|
||||
return idx
|
||||
@@ -248,7 +249,7 @@ func (ps *store) PutSeeder(ih bittorrent.InfoHash, p bittorrent.Peer) error {
|
||||
|
||||
pk := p.RawString()
|
||||
|
||||
shard := ps.shards[ps.shardIndex(ih, p.IP.AddressFamily)]
|
||||
shard := ps.shards[ps.shardIndex(ih, p.Addr())]
|
||||
shard.Lock()
|
||||
|
||||
if _, ok := shard.swarms[ih]; !ok {
|
||||
@@ -279,7 +280,7 @@ func (ps *store) DeleteSeeder(ih bittorrent.InfoHash, p bittorrent.Peer) error {
|
||||
|
||||
pk := p.RawString()
|
||||
|
||||
shard := ps.shards[ps.shardIndex(ih, p.IP.AddressFamily)]
|
||||
shard := ps.shards[ps.shardIndex(ih, p.Addr())]
|
||||
shard.Lock()
|
||||
|
||||
if _, ok := shard.swarms[ih]; !ok {
|
||||
@@ -312,7 +313,7 @@ func (ps *store) PutLeecher(ih bittorrent.InfoHash, p bittorrent.Peer) error {
|
||||
|
||||
pk := p.RawString()
|
||||
|
||||
shard := ps.shards[ps.shardIndex(ih, p.IP.AddressFamily)]
|
||||
shard := ps.shards[ps.shardIndex(ih, p.Addr())]
|
||||
shard.Lock()
|
||||
|
||||
if _, ok := shard.swarms[ih]; !ok {
|
||||
@@ -343,7 +344,7 @@ func (ps *store) DeleteLeecher(ih bittorrent.InfoHash, p bittorrent.Peer) error
|
||||
|
||||
pk := p.RawString()
|
||||
|
||||
shard := ps.shards[ps.shardIndex(ih, p.IP.AddressFamily)]
|
||||
shard := ps.shards[ps.shardIndex(ih, p.Addr())]
|
||||
shard.Lock()
|
||||
|
||||
if _, ok := shard.swarms[ih]; !ok {
|
||||
@@ -376,7 +377,7 @@ func (ps *store) GraduateLeecher(ih bittorrent.InfoHash, p bittorrent.Peer) erro
|
||||
|
||||
pk := p.RawString()
|
||||
|
||||
shard := ps.shards[ps.shardIndex(ih, p.IP.AddressFamily)]
|
||||
shard := ps.shards[ps.shardIndex(ih, p.Addr())]
|
||||
shard.Lock()
|
||||
|
||||
if _, ok := shard.swarms[ih]; !ok {
|
||||
@@ -404,14 +405,14 @@ func (ps *store) GraduateLeecher(ih bittorrent.InfoHash, p bittorrent.Peer) erro
|
||||
return nil
|
||||
}
|
||||
|
||||
func (ps *store) AnnouncePeers(ih bittorrent.InfoHash, seeder bool, numWant int, announcer bittorrent.Peer) (peers []bittorrent.Peer, err error) {
|
||||
func (ps *store) AnnouncePeers(ih bittorrent.InfoHash, seeder bool, numWant int, peer bittorrent.Peer) (peers []bittorrent.Peer, err error) {
|
||||
select {
|
||||
case <-ps.closed:
|
||||
panic("attempted to interact with stopped memory store")
|
||||
default:
|
||||
}
|
||||
|
||||
shard := ps.shards[ps.shardIndex(ih, announcer.IP.AddressFamily)]
|
||||
shard := ps.shards[ps.shardIndex(ih, peer.Addr())]
|
||||
shard.RLock()
|
||||
|
||||
if _, ok := shard.swarms[ih]; !ok {
|
||||
@@ -445,7 +446,7 @@ func (ps *store) AnnouncePeers(ih bittorrent.InfoHash, seeder bool, numWant int,
|
||||
// Append leechers until we reach numWant.
|
||||
if numWant > 0 {
|
||||
leechers := shard.swarms[ih].leechers
|
||||
announcerPK := announcer.RawString()
|
||||
announcerPK := peer.RawString()
|
||||
for pk := range leechers {
|
||||
if pk == announcerPK {
|
||||
continue
|
||||
@@ -465,7 +466,7 @@ func (ps *store) AnnouncePeers(ih bittorrent.InfoHash, seeder bool, numWant int,
|
||||
return
|
||||
}
|
||||
|
||||
func (ps *store) ScrapeSwarm(ih bittorrent.InfoHash, addressFamily bittorrent.AddressFamily) (resp bittorrent.Scrape) {
|
||||
func (ps *store) ScrapeSwarm(ih bittorrent.InfoHash, peer bittorrent.Peer) (resp bittorrent.Scrape) {
|
||||
select {
|
||||
case <-ps.closed:
|
||||
panic("attempted to interact with stopped memory store")
|
||||
@@ -473,7 +474,7 @@ func (ps *store) ScrapeSwarm(ih bittorrent.InfoHash, addressFamily bittorrent.Ad
|
||||
}
|
||||
|
||||
resp.InfoHash = ih
|
||||
shard := ps.shards[ps.shardIndex(ih, addressFamily)]
|
||||
shard := ps.shards[ps.shardIndex(ih, peer.Addr())]
|
||||
shard.RLock()
|
||||
|
||||
swarm, ok := shard.swarms[ih]
|
||||
|
||||
+268
-232
@@ -2,24 +2,24 @@
|
||||
// BitTorrent tracker keeping peer data in redis with hash.
|
||||
// There two categories of hash:
|
||||
//
|
||||
// - IPv{4,6}_{L,S}_infohash
|
||||
// - CHI_{4,6}_{L,S}_infohash
|
||||
// To save peers that hold the infohash, used for fast searching,
|
||||
// deleting, and timeout handling
|
||||
//
|
||||
// - IPv{4,6}
|
||||
// - CHI_{4,6}
|
||||
// To save all the infohashes, used for garbage collection,
|
||||
// metrics aggregation and leecher graduation
|
||||
//
|
||||
// Tree keys are used to record the count of swarms, seeders
|
||||
// and leechers for each group (IPv4, IPv6).
|
||||
//
|
||||
// - IPv{4,6}_infohash_count
|
||||
// - CHI_{4,6}_I_C
|
||||
// To record the number of infohashes.
|
||||
//
|
||||
// - IPv{4,6}_S_count
|
||||
// - CHI_{4,6}_S_C
|
||||
// To record the number of seeders.
|
||||
//
|
||||
// - IPv{4,6}_L_count
|
||||
// - CHI_{4,6}_L_C
|
||||
// To record the number of leechers.
|
||||
package redis
|
||||
|
||||
@@ -53,6 +53,19 @@ const (
|
||||
defaultReadTimeout = time.Second * 15
|
||||
defaultWriteTimeout = time.Second * 15
|
||||
defaultConnectTimeout = time.Second * 15
|
||||
prefixKey = "CHI_"
|
||||
ih4Key = "CHI_4_I"
|
||||
ih6Key = "CHI_6_I"
|
||||
ih4SeederKey = "CHI_4_S_"
|
||||
ih6SeederKey = "CHI_6_S_"
|
||||
ih4LeecherKey = "CHI_4_L_"
|
||||
ih6LeecherKey = "CHI_6_L_"
|
||||
cnt4SeederKey = "CHI_4_C_S"
|
||||
cnt6SeederKey = "CHI_6_C_S"
|
||||
cnt4LeecherKey = "CHI_4_C_L"
|
||||
cnt6LeecherKey = "CHI_6_C_L"
|
||||
cnt4InfoHashKey = "CHI_4_C_I"
|
||||
cnt6InfoHashKey = "CHI_6_C_I"
|
||||
)
|
||||
|
||||
// ErrSentinelAndClusterChecked returned from initializer if both Config.Sentinel and Config.Cluster provided
|
||||
@@ -264,13 +277,11 @@ func New(conf Config) (storage.Storage, error) {
|
||||
ps.logFields = cfg.LogFields()
|
||||
|
||||
// Start a goroutine for garbage collection.
|
||||
ps.wg.Add(1)
|
||||
go ps.runGC(cfg.GarbageCollectionInterval, cfg.PeerLifetime)
|
||||
go ps.scheduleGC(cfg.GarbageCollectionInterval, cfg.PeerLifetime)
|
||||
|
||||
if cfg.PrometheusReportingInterval > 0 {
|
||||
// Start a goroutine for reporting statistics to Prometheus.
|
||||
ps.wg.Add(1)
|
||||
go ps.runProm(cfg.PrometheusReportingInterval)
|
||||
go ps.schedulerProm(cfg.PrometheusReportingInterval)
|
||||
} else {
|
||||
log.Info("prometheus disabled because of zero reporting interval")
|
||||
}
|
||||
@@ -278,21 +289,32 @@ func New(conf Config) (storage.Storage, error) {
|
||||
return ps, nil
|
||||
}
|
||||
|
||||
func (ps *store) runGC(gcInterval, peerLifeTime time.Duration) {
|
||||
func (ps *store) scheduleGC(gcInterval, peerLifeTime time.Duration) {
|
||||
ps.wg.Add(1)
|
||||
defer ps.wg.Done()
|
||||
t := time.NewTimer(gcInterval)
|
||||
defer t.Stop()
|
||||
for {
|
||||
select {
|
||||
case <-ps.closed:
|
||||
return
|
||||
case <-time.After(gcInterval):
|
||||
case <-t.C:
|
||||
before := time.Now().Add(-peerLifeTime)
|
||||
log.Debug("storage: purging peers with no announces since", log.Fields{"before": before})
|
||||
ps.collectGarbage(before)
|
||||
cutoffUnix := before.UnixNano()
|
||||
start := time.Now()
|
||||
ps.gc(cutoffUnix, false)
|
||||
ps.gc(cutoffUnix, true)
|
||||
duration := time.Since(start).Milliseconds()
|
||||
log.Debug("storage: recordGCDuration", log.Fields{"timeTaken(ms)": duration})
|
||||
storage.PromGCDurationMilliseconds.Observe(float64(duration))
|
||||
t.Reset(gcInterval)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func (ps *store) runProm(reportInterval time.Duration) {
|
||||
func (ps *store) schedulerProm(reportInterval time.Duration) {
|
||||
ps.wg.Add(1)
|
||||
defer ps.wg.Done()
|
||||
t := time.NewTicker(reportInterval)
|
||||
for {
|
||||
@@ -317,68 +339,39 @@ type store struct {
|
||||
logFields log.Fields
|
||||
}
|
||||
|
||||
var groups = []string{bittorrent.IPv4.String(), bittorrent.IPv6.String()}
|
||||
|
||||
// leecherInfoHashKey generates string IPvN_L_hash
|
||||
func leecherInfoHashKey(addressFamily, infoHash string) string {
|
||||
return addressFamily + "_L_" + infoHash
|
||||
}
|
||||
|
||||
// seederInfoHashKey generates string IPvN_S_hash
|
||||
func seederInfoHashKey(addressFamily, infoHash string) string {
|
||||
return addressFamily + "_S_" + infoHash
|
||||
}
|
||||
|
||||
// seederInfoHashKey generates string IPvN_infohash_count
|
||||
func infoHashCountKey(addressFamily string) string {
|
||||
return addressFamily + "_infohash_count"
|
||||
}
|
||||
|
||||
// seederInfoHashKey generates string IPvN_L_count
|
||||
func leecherCountKey(addressFamily string) string {
|
||||
return addressFamily + "_L_count"
|
||||
}
|
||||
|
||||
// seederInfoHashKey generates string IPvN_S_count
|
||||
func seederCountKey(addressFamily string) string {
|
||||
return addressFamily + "_S_count"
|
||||
func (ps *store) count(key string) (n uint64) {
|
||||
var err error
|
||||
if n, err = ps.con.Get(ps.ctx, key).Uint64(); err != nil && !errors.Is(err, redis.Nil) {
|
||||
log.Error("storage: GET counter failure", log.Fields{
|
||||
"key": key,
|
||||
"error": err,
|
||||
})
|
||||
}
|
||||
return
|
||||
}
|
||||
|
||||
// populateProm aggregates metrics over all groups and then posts them to
|
||||
// prometheus.
|
||||
func (ps *store) populateProm() {
|
||||
var numInfoHashes, numSeeders, numLeechers int64
|
||||
|
||||
for _, group := range groups {
|
||||
if n, err := ps.con.Get(ps.ctx, infoHashCountKey(group)).Int64(); err != nil && !errors.Is(err, redis.Nil) {
|
||||
log.Error("storage: GET counter failure", log.Fields{
|
||||
"key": infoHashCountKey(group),
|
||||
"error": err,
|
||||
})
|
||||
numInfoHashes, numSeeders, numLeechers := new(uint64), new(uint64), new(uint64)
|
||||
fetchFn := func(v6 bool) {
|
||||
var cntSeederKey, cntLeecherKey, cntInfoHashKey string
|
||||
if v6 {
|
||||
cntSeederKey, cntLeecherKey, cntInfoHashKey = cnt6SeederKey, cnt6LeecherKey, cnt6InfoHashKey
|
||||
} else {
|
||||
numInfoHashes += n
|
||||
}
|
||||
if n, err := ps.con.Get(ps.ctx, seederCountKey(group)).Int64(); err != nil && !errors.Is(err, redis.Nil) {
|
||||
log.Error("storage: GET counter failure", log.Fields{
|
||||
"key": seederCountKey(group),
|
||||
"error": err,
|
||||
})
|
||||
} else {
|
||||
numSeeders += n
|
||||
}
|
||||
if n, err := ps.con.Get(ps.ctx, leecherCountKey(group)).Int64(); err != nil && !errors.Is(err, redis.Nil) {
|
||||
log.Error("storage: GET counter failure", log.Fields{
|
||||
"key": leecherCountKey(group),
|
||||
"error": err,
|
||||
})
|
||||
} else {
|
||||
numLeechers += n
|
||||
cntSeederKey, cntLeecherKey, cntInfoHashKey = cnt4SeederKey, cnt4LeecherKey, cnt4InfoHashKey
|
||||
}
|
||||
*numInfoHashes += ps.count(cntInfoHashKey)
|
||||
*numSeeders += ps.count(cntSeederKey)
|
||||
*numLeechers += ps.count(cntLeecherKey)
|
||||
}
|
||||
|
||||
storage.PromInfoHashesCount.Set(float64(numInfoHashes))
|
||||
storage.PromSeedersCount.Set(float64(numSeeders))
|
||||
storage.PromLeechersCount.Set(float64(numLeechers))
|
||||
fetchFn(false)
|
||||
fetchFn(true)
|
||||
|
||||
storage.PromInfoHashesCount.Set(float64(*numInfoHashes))
|
||||
storage.PromSeedersCount.Set(float64(*numSeeders))
|
||||
storage.PromLeechersCount.Set(float64(*numLeechers))
|
||||
}
|
||||
|
||||
func (ps *store) getClock() int64 {
|
||||
@@ -409,154 +402,185 @@ func asNil(err error) error {
|
||||
return err
|
||||
}
|
||||
|
||||
func (ps *store) PutSeeder(ih bittorrent.InfoHash, p bittorrent.Peer) error {
|
||||
addressFamily := p.IP.AddressFamily.String()
|
||||
func (ps *store) PutSeeder(ih bittorrent.InfoHash, peer bittorrent.Peer) error {
|
||||
var ihSummaryKey, ihPeerKey, cntPeerKey, cntInfoHashKey string
|
||||
log.Debug("storage: PutSeeder", log.Fields{
|
||||
"InfoHash": ih,
|
||||
"Peer": p,
|
||||
"Peer": peer,
|
||||
})
|
||||
|
||||
encodedSeederInfoHash := seederInfoHashKey(addressFamily, ih.RawString())
|
||||
if peer.Addr().Is6() {
|
||||
ihSummaryKey, ihPeerKey, cntPeerKey, cntInfoHashKey = ih6Key, ih6SeederKey, cnt6SeederKey, cnt6InfoHashKey
|
||||
} else {
|
||||
ihSummaryKey, ihPeerKey, cntPeerKey, cntInfoHashKey = ih4Key, ih4SeederKey, cnt4SeederKey, cnt4InfoHashKey
|
||||
}
|
||||
ihPeerKey += ih.RawString()
|
||||
now := ps.getClock()
|
||||
|
||||
return ps.tx(func(tx redis.Pipeliner) (err error) {
|
||||
if err = tx.HSet(ps.ctx, encodedSeederInfoHash, p.RawString(), now).Err(); err != nil {
|
||||
if err = tx.HSet(ps.ctx, ihPeerKey, peer.RawString(), now).Err(); err != nil {
|
||||
return
|
||||
}
|
||||
if err = ps.con.Incr(ps.ctx, seederCountKey(addressFamily)).Err(); err != nil {
|
||||
if err = ps.con.Incr(ps.ctx, cntPeerKey).Err(); err != nil {
|
||||
return
|
||||
}
|
||||
if err = ps.con.HSet(ps.ctx, addressFamily, encodedSeederInfoHash, now).Err(); err != nil {
|
||||
var added int64
|
||||
if added, err = ps.con.SAdd(ps.ctx, ihSummaryKey, ihPeerKey).Result(); err != nil {
|
||||
return
|
||||
}
|
||||
err = ps.con.Incr(ps.ctx, infoHashCountKey(addressFamily)).Err()
|
||||
if added > 0 {
|
||||
err = ps.con.Incr(ps.ctx, cntInfoHashKey).Err()
|
||||
}
|
||||
return
|
||||
})
|
||||
}
|
||||
|
||||
func (ps *store) DeleteSeeder(ih bittorrent.InfoHash, p bittorrent.Peer) error {
|
||||
addressFamily := p.IP.AddressFamily.String()
|
||||
func (ps *store) DeleteSeeder(ih bittorrent.InfoHash, peer bittorrent.Peer) error {
|
||||
var ihPeerKey, cntPeerKey string
|
||||
log.Debug("storage: DeleteSeeder", log.Fields{
|
||||
"InfoHash": ih,
|
||||
"Peer": p,
|
||||
"Peer": peer,
|
||||
})
|
||||
if peer.Addr().Is6() {
|
||||
ihPeerKey, cntPeerKey = ih6SeederKey, cnt6SeederKey
|
||||
} else {
|
||||
ihPeerKey, cntPeerKey = ih4SeederKey, cnt4SeederKey
|
||||
}
|
||||
ihPeerKey += ih.RawString()
|
||||
|
||||
encodedSeederInfoHash := seederInfoHashKey(addressFamily, ih.RawString())
|
||||
deleted, err := ps.con.HDel(ps.ctx, encodedSeederInfoHash, p.RawString()).Uint64()
|
||||
deleted, err := ps.con.HDel(ps.ctx, ihPeerKey, peer.RawString()).Uint64()
|
||||
err = asNil(err)
|
||||
if err == nil {
|
||||
if deleted == 0 {
|
||||
err = storage.ErrResourceDoesNotExist
|
||||
} else {
|
||||
err = ps.con.Decr(ps.ctx, seederCountKey(addressFamily)).Err()
|
||||
err = ps.con.Decr(ps.ctx, cntPeerKey).Err()
|
||||
}
|
||||
}
|
||||
|
||||
return err
|
||||
}
|
||||
|
||||
func (ps *store) PutLeecher(ih bittorrent.InfoHash, p bittorrent.Peer) error {
|
||||
addressFamily := p.IP.AddressFamily.String()
|
||||
func (ps *store) PutLeecher(ih bittorrent.InfoHash, peer bittorrent.Peer) error {
|
||||
var ihSummaryKey, ihPeerKey, cntPeerKey string
|
||||
log.Debug("storage: PutLeecher", log.Fields{
|
||||
"InfoHash": ih,
|
||||
"Peer": p,
|
||||
"Peer": peer,
|
||||
})
|
||||
if peer.Addr().Is6() {
|
||||
ihSummaryKey, ihPeerKey, cntPeerKey = ih6Key, ih6LeecherKey, cnt6LeecherKey
|
||||
} else {
|
||||
ihSummaryKey, ihPeerKey, cntPeerKey = ih4Key, ih4LeecherKey, cnt4LeecherKey
|
||||
}
|
||||
ihPeerKey += ih.RawString()
|
||||
|
||||
// Update the peer in the swarm.
|
||||
encodedLeecherInfoHash := leecherInfoHashKey(addressFamily, ih.RawString())
|
||||
now := ps.getClock()
|
||||
|
||||
return ps.tx(func(tx redis.Pipeliner) (err error) {
|
||||
if err = tx.HSet(ps.ctx, encodedLeecherInfoHash, p.RawString(), now).Err(); err != nil {
|
||||
if err = tx.HSet(ps.ctx, ihPeerKey, peer.RawString(), now).Err(); err != nil {
|
||||
return
|
||||
}
|
||||
if err = tx.HSet(ps.ctx, addressFamily, encodedLeecherInfoHash, now).Err(); err != nil {
|
||||
if err = tx.Incr(ps.ctx, cntPeerKey).Err(); err != nil {
|
||||
return err
|
||||
}
|
||||
err = tx.Incr(ps.ctx, leecherCountKey(addressFamily)).Err()
|
||||
err = tx.HSet(ps.ctx, ihSummaryKey, ihPeerKey, now).Err()
|
||||
return
|
||||
})
|
||||
}
|
||||
|
||||
func (ps *store) DeleteLeecher(ih bittorrent.InfoHash, p bittorrent.Peer) error {
|
||||
addressFamily := p.IP.AddressFamily.String()
|
||||
func (ps *store) DeleteLeecher(ih bittorrent.InfoHash, peer bittorrent.Peer) error {
|
||||
var ihPeerKey, cntPeerKey string
|
||||
log.Debug("storage: DeleteLeecher", log.Fields{
|
||||
"InfoHash": ih,
|
||||
"Peer": p,
|
||||
"Peer": peer,
|
||||
})
|
||||
|
||||
encodedLeecherInfoHash := leecherInfoHashKey(addressFamily, ih.RawString())
|
||||
if peer.Addr().Is6() {
|
||||
ihPeerKey, cntPeerKey = ih6LeecherKey, cnt6LeecherKey
|
||||
} else {
|
||||
ihPeerKey, cntPeerKey = ih4LeecherKey, cnt4LeecherKey
|
||||
}
|
||||
ihPeerKey += ih.RawString()
|
||||
|
||||
deleted, err := ps.con.HDel(ps.ctx, encodedLeecherInfoHash, p.RawString()).Uint64()
|
||||
deleted, err := ps.con.HDel(ps.ctx, ihPeerKey, peer.RawString()).Uint64()
|
||||
err = asNil(err)
|
||||
if err == nil {
|
||||
if deleted == 0 {
|
||||
err = storage.ErrResourceDoesNotExist
|
||||
} else {
|
||||
err = ps.con.Decr(ps.ctx, leecherCountKey(addressFamily)).Err()
|
||||
err = ps.con.Decr(ps.ctx, cntPeerKey).Err()
|
||||
}
|
||||
}
|
||||
|
||||
return err
|
||||
}
|
||||
|
||||
func (ps *store) GraduateLeecher(ih bittorrent.InfoHash, p bittorrent.Peer) error {
|
||||
addressFamily := p.IP.AddressFamily.String()
|
||||
func (ps *store) GraduateLeecher(ih bittorrent.InfoHash, peer bittorrent.Peer) error {
|
||||
var ihSummaryKey, ihSeederKey, ihLeecherKey, cntSeederKey, cntLeecherKey, cntInfoHashKey string
|
||||
log.Debug("storage: GraduateLeecher", log.Fields{
|
||||
"InfoHash": ih,
|
||||
"Peer": p,
|
||||
"Peer": peer,
|
||||
})
|
||||
|
||||
encodedInfoHash := ih.RawString()
|
||||
encodedLeecherInfoHash := leecherInfoHashKey(addressFamily, encodedInfoHash)
|
||||
encodedSeederInfoHash := seederInfoHashKey(addressFamily, encodedInfoHash)
|
||||
peerKey := p.RawString()
|
||||
if peer.Addr().Is6() {
|
||||
ihSummaryKey, ihSeederKey, cntSeederKey = ih6Key, ih6SeederKey, cnt6SeederKey
|
||||
ihLeecherKey, cntLeecherKey, cntInfoHashKey = ih6LeecherKey, cnt6LeecherKey, cnt6InfoHashKey
|
||||
} else {
|
||||
ihSummaryKey, ihSeederKey, cntSeederKey = ih4Key, ih4SeederKey, cnt4SeederKey
|
||||
ihLeecherKey, cntLeecherKey, cntInfoHashKey = ih4LeecherKey, cnt4LeecherKey, cnt4InfoHashKey
|
||||
}
|
||||
infoHash, peerKey := ih.RawString(), peer.RawString()
|
||||
ihSeederKey, ihLeecherKey = ihSeederKey+infoHash, ihLeecherKey+infoHash
|
||||
|
||||
now := ps.getClock()
|
||||
|
||||
return ps.tx(func(tx redis.Pipeliner) error {
|
||||
deleted, err := tx.HDel(ps.ctx, encodedLeecherInfoHash, peerKey).Uint64()
|
||||
deleted, err := tx.HDel(ps.ctx, ihLeecherKey, peerKey).Uint64()
|
||||
err = asNil(err)
|
||||
if err == nil {
|
||||
if deleted > 0 {
|
||||
err = tx.Decr(ps.ctx, leecherCountKey(addressFamily)).Err()
|
||||
err = tx.Decr(ps.ctx, cntLeecherKey).Err()
|
||||
}
|
||||
}
|
||||
if err == nil {
|
||||
err = tx.HSet(ps.ctx, encodedSeederInfoHash, peerKey, now).Err()
|
||||
err = tx.HSet(ps.ctx, ihSeederKey, peerKey, now).Err()
|
||||
}
|
||||
if err == nil {
|
||||
err = tx.Incr(ps.ctx, seederCountKey(addressFamily)).Err()
|
||||
err = tx.Incr(ps.ctx, cntSeederKey).Err()
|
||||
}
|
||||
if err == nil {
|
||||
err = tx.HSet(ps.ctx, addressFamily, encodedSeederInfoHash, now).Err()
|
||||
err = tx.HSet(ps.ctx, ihSummaryKey, ihSeederKey, now).Err()
|
||||
}
|
||||
if err == nil {
|
||||
err = tx.Incr(ps.ctx, infoHashCountKey(addressFamily)).Err()
|
||||
err = tx.Incr(ps.ctx, cntInfoHashKey).Err()
|
||||
}
|
||||
return err
|
||||
})
|
||||
}
|
||||
|
||||
func (ps *store) AnnouncePeers(ih bittorrent.InfoHash, seeder bool, numWant int, announcer bittorrent.Peer) (peers []bittorrent.Peer, err error) {
|
||||
addressFamily := announcer.IP.AddressFamily.String()
|
||||
func (ps *store) AnnouncePeers(ih bittorrent.InfoHash, seeder bool, numWant int, peer bittorrent.Peer) (peers []bittorrent.Peer, err error) {
|
||||
var ihSeederKey, ihLeecherKey string
|
||||
log.Debug("storage: AnnouncePeers", log.Fields{
|
||||
"InfoHash": ih,
|
||||
"seeder": seeder,
|
||||
"numWant": numWant,
|
||||
"Peer": announcer,
|
||||
"Peer": peer,
|
||||
})
|
||||
|
||||
encodedInfoHash := ih.RawString()
|
||||
encodedLeecherInfoHash := leecherInfoHashKey(addressFamily, encodedInfoHash)
|
||||
encodedSeederInfoHash := seederInfoHashKey(addressFamily, encodedInfoHash)
|
||||
if peer.Addr().Is6() {
|
||||
ihSeederKey, ihLeecherKey = ih6SeederKey, cnt6LeecherKey
|
||||
} else {
|
||||
ihSeederKey, ihLeecherKey = ih4SeederKey, ih4LeecherKey
|
||||
}
|
||||
infoHash := ih.RawString()
|
||||
ihSeederKey, ihLeecherKey = ihSeederKey+infoHash, ihLeecherKey+infoHash
|
||||
|
||||
leechers, err := ps.con.HKeys(ps.ctx, encodedLeecherInfoHash).Result()
|
||||
leechers, err := ps.con.HKeys(ps.ctx, ihLeecherKey).Result()
|
||||
err = asNil(err)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
seeders, err := ps.con.HKeys(ps.ctx, encodedSeederInfoHash).Result()
|
||||
seeders, err := ps.con.HKeys(ps.ctx, ihSeederKey).Result()
|
||||
err = asNil(err)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
@@ -595,7 +619,7 @@ func (ps *store) AnnouncePeers(ih bittorrent.InfoHash, seeder bool, numWant int,
|
||||
|
||||
// Append leechers until we reach numWant.
|
||||
if numWant > 0 {
|
||||
announcerPK := announcer.RawString()
|
||||
announcerPK := peer.RawString()
|
||||
for _, peerKey := range leechers {
|
||||
if peerKey != announcerPK {
|
||||
if numWant == 0 {
|
||||
@@ -615,28 +639,36 @@ func (ps *store) AnnouncePeers(ih bittorrent.InfoHash, seeder bool, numWant int,
|
||||
return
|
||||
}
|
||||
|
||||
func (ps *store) ScrapeSwarm(ih bittorrent.InfoHash, af bittorrent.AddressFamily) (resp bittorrent.Scrape) {
|
||||
func (ps *store) ScrapeSwarm(ih bittorrent.InfoHash, peer bittorrent.Peer) (resp bittorrent.Scrape) {
|
||||
var ihSeederKey, ihLeecherKey string
|
||||
log.Debug("storage: ScrapeSwarm", log.Fields{
|
||||
"InfoHash": ih,
|
||||
"Peer": peer,
|
||||
})
|
||||
resp.InfoHash = ih
|
||||
addressFamily := af.String()
|
||||
encodedInfoHash := ih.RawString()
|
||||
encodedLeecherInfoHash := leecherInfoHashKey(addressFamily, encodedInfoHash)
|
||||
encodedSeederInfoHash := seederInfoHashKey(addressFamily, encodedInfoHash)
|
||||
if peer.Addr().Is6() {
|
||||
ihSeederKey, ihLeecherKey = ih6SeederKey, cnt6LeecherKey
|
||||
} else {
|
||||
ihSeederKey, ihLeecherKey = ih4SeederKey, ih4LeecherKey
|
||||
}
|
||||
infoHash := ih.RawString()
|
||||
ihSeederKey, ihLeecherKey = ihSeederKey+infoHash, ihLeecherKey+infoHash
|
||||
|
||||
leechersLen, err := ps.con.HLen(ps.ctx, encodedLeecherInfoHash).Result()
|
||||
leechersLen, err := ps.con.HLen(ps.ctx, ihLeecherKey).Result()
|
||||
err = asNil(err)
|
||||
if err != nil {
|
||||
log.Error("storage: Redis HLEN failure", log.Fields{
|
||||
"Hkey": encodedLeecherInfoHash,
|
||||
"Hkey": ihLeecherKey,
|
||||
"error": err,
|
||||
})
|
||||
return
|
||||
}
|
||||
|
||||
seedersLen, err := ps.con.HLen(ps.ctx, encodedSeederInfoHash).Result()
|
||||
seedersLen, err := ps.con.HLen(ps.ctx, ihSeederKey).Result()
|
||||
err = asNil(err)
|
||||
if err != nil {
|
||||
log.Error("storage: Redis HLEN failure", log.Fields{
|
||||
"Hkey": encodedSeederInfoHash,
|
||||
"Hkey": ihSeederKey,
|
||||
"error": err,
|
||||
})
|
||||
return
|
||||
@@ -649,11 +681,11 @@ func (ps *store) ScrapeSwarm(ih bittorrent.InfoHash, af bittorrent.AddressFamily
|
||||
}
|
||||
|
||||
func (ps *store) Put(ctx string, value storage.Entry) error {
|
||||
return ps.con.HSet(ps.ctx, ctx, value.Key, value.Value).Err()
|
||||
return ps.con.HSet(ps.ctx, prefixKey+ctx, value.Key, value.Value).Err()
|
||||
}
|
||||
|
||||
func (ps *store) Contains(ctx string, key string) (bool, error) {
|
||||
exist, err := ps.con.HExists(ps.ctx, ctx, key).Result()
|
||||
exist, err := ps.con.HExists(ps.ctx, prefixKey+ctx, key).Result()
|
||||
return exist, asNil(err)
|
||||
}
|
||||
|
||||
@@ -665,12 +697,12 @@ func (ps *store) BulkPut(ctx string, pairs ...storage.Entry) (err error) {
|
||||
for _, p := range pairs {
|
||||
args = append(args, p.Key, p.Value)
|
||||
}
|
||||
err = ps.con.HSet(ps.ctx, ctx, args...).Err()
|
||||
err = ps.con.HSet(ps.ctx, prefixKey+ctx, args...).Err()
|
||||
if err != nil {
|
||||
if strings.Contains(err.Error(), argNumErrorMsg) {
|
||||
log.Warn("This REDIS version/implementation does not support variadic arguments for HSET")
|
||||
for _, p := range pairs {
|
||||
if err = ps.con.HSet(ps.ctx, ctx, p.Key, p.Value).Err(); err != nil {
|
||||
if err = ps.con.HSet(ps.ctx, prefixKey+ctx, p.Key, p.Value).Err(); err != nil {
|
||||
break
|
||||
}
|
||||
}
|
||||
@@ -681,7 +713,7 @@ func (ps *store) BulkPut(ctx string, pairs ...storage.Entry) (err error) {
|
||||
}
|
||||
|
||||
func (ps *store) Load(ctx string, key string) (v any, err error) {
|
||||
v, err = ps.con.HGet(ps.ctx, ctx, key).Result()
|
||||
v, err = ps.con.HGet(ps.ctx, prefixKey+ctx, key).Result()
|
||||
if err != nil && errors.Is(err, redis.Nil) {
|
||||
v, err = nil, nil
|
||||
}
|
||||
@@ -690,12 +722,12 @@ func (ps *store) Load(ctx string, key string) (v any, err error) {
|
||||
|
||||
func (ps *store) Delete(ctx string, keys ...string) (err error) {
|
||||
if len(keys) > 0 {
|
||||
err = asNil(ps.con.HDel(ps.ctx, ctx, keys...).Err())
|
||||
err = asNil(ps.con.HDel(ps.ctx, prefixKey+ctx, keys...).Err())
|
||||
if err != nil {
|
||||
if strings.Contains(err.Error(), argNumErrorMsg) {
|
||||
log.Warn("This REDIS version/implementation does not support variadic arguments for HDEL")
|
||||
for _, k := range keys {
|
||||
if err = asNil(ps.con.HDel(ps.ctx, ctx, k).Err()); err != nil {
|
||||
if err = asNil(ps.con.HDel(ps.ctx, prefixKey+ctx, k).Err()); err != nil {
|
||||
break
|
||||
}
|
||||
}
|
||||
@@ -705,7 +737,7 @@ func (ps *store) Delete(ctx string, keys ...string) (err error) {
|
||||
return
|
||||
}
|
||||
|
||||
// collectGarbage deletes all Peers from the Storage which are older than the
|
||||
// gc deletes all Peers from the Storage which are older than the
|
||||
// cutoff time.
|
||||
//
|
||||
// This function must be able to execute while other methods on this interface
|
||||
@@ -717,11 +749,11 @@ func (ps *store) Delete(ctx string, keys ...string) (err error) {
|
||||
// - The Put(Seeder|Leecher) and GraduateLeecher methods only ever add infohash
|
||||
// keys to addressFamily hashes and increment the infohash counter.
|
||||
// - The only method that deletes from the addressFamily hashes is
|
||||
// collectGarbage, which also decrements the counters. That means that,
|
||||
// gc, which also decrements the counters. That means that,
|
||||
// even if a Delete(Seeder|Leecher) call removes the last peer from a swarm,
|
||||
// the infohash counter is not changed and the infohash is left in the
|
||||
// addressFamily hash until it will be cleaned up by collectGarbage.
|
||||
// - collectGarbage must run regularly.
|
||||
// addressFamily hash until it will be cleaned up by gc.
|
||||
// - gc must run regularly.
|
||||
// - A WATCH ... MULTI ... EXEC block fails, if between the WATCH and the 'EXEC'
|
||||
// any of the watched keys have changed. The location of the 'MULTI' doesn't
|
||||
// matter.
|
||||
@@ -740,120 +772,124 @@ func (ps *store) Delete(ctx string, keys ...string) (err error) {
|
||||
// not empty and start no transaction.
|
||||
// - If the change happens after the HLEN, we will attempt a transaction and it
|
||||
// will fail. This is okay, the swarm is not empty, we will try cleaning it up
|
||||
// next time collectGarbage runs.
|
||||
// next time gc runs.
|
||||
// 4. (1,0): Again, two ways:
|
||||
// - If the change happens before the HLEN, we will see an empty swarm. This
|
||||
// situation happens if a call to Delete(Seeder|Leecher) removed the last
|
||||
// peer asynchronously. We will attempt a transaction, but the transaction
|
||||
// will fail. This is okay, the infohash key will remain in the addressFamily
|
||||
// hash, we will attempt to clean it up the next time 'collectGarbage` runs.
|
||||
// hash, we will attempt to clean it up the next time 'gc` runs.
|
||||
// - If the change happens after the HLEN, we will not even attempt to make the
|
||||
// transaction. The infohash key will remain in the addressFamil hash and
|
||||
// we'll attempt to clean it up the next time collectGarbage runs.
|
||||
func (ps *store) collectGarbage(cutoff time.Time) {
|
||||
cutoffUnix := cutoff.UnixNano()
|
||||
start := time.Now()
|
||||
var err error
|
||||
for _, group := range groups {
|
||||
// list all infoHashes in the group
|
||||
var infoHashes []string
|
||||
infoHashes, err = ps.con.HKeys(ps.ctx, group).Result()
|
||||
err = asNil(err)
|
||||
if err == nil {
|
||||
for _, infoHash := range infoHashes {
|
||||
isSeeder := len(infoHash) > 5 && infoHash[5:6] == "S"
|
||||
// list all (peer, timeout) pairs for the ih
|
||||
peerList, err := ps.con.HGetAll(ps.ctx, infoHash).Result()
|
||||
err = asNil(err)
|
||||
if err == nil {
|
||||
var removedPeerCount int64
|
||||
for peerKey, timeStamp := range peerList {
|
||||
var peer bittorrent.Peer
|
||||
if peer, err = bittorrent.NewPeer(peerKey); err == nil {
|
||||
if mtime, err := strconv.ParseInt(timeStamp, 10, 64); err == nil {
|
||||
if mtime <= cutoffUnix {
|
||||
log.Debug("storage: deleting peer", log.Fields{
|
||||
"Peer": peer,
|
||||
})
|
||||
var count int64
|
||||
count, err = ps.con.HDel(ps.ctx, infoHash, peerKey).Result()
|
||||
err = asNil(err)
|
||||
if err == nil {
|
||||
removedPeerCount += count
|
||||
}
|
||||
// we'll attempt to clean it up the next time gc runs.
|
||||
func (ps *store) gc(cutoffUnix int64, v6 bool) {
|
||||
// list all infoHashKeys in the group
|
||||
var ihSummaryKey, ihSeederKey, ihLeecherKey, cntSeederKey, cntLeecherKey, cntInfoHashKey string
|
||||
if v6 {
|
||||
cntSeederKey, cntLeecherKey, cntInfoHashKey = cnt6SeederKey, cnt6LeecherKey, cnt6InfoHashKey
|
||||
ihSummaryKey, ihSeederKey, ihLeecherKey = ih6Key, ih6SeederKey, ih6LeecherKey
|
||||
} else {
|
||||
cntSeederKey, cntLeecherKey, cntInfoHashKey = cnt4SeederKey, cnt4LeecherKey, cnt4InfoHashKey
|
||||
ihSummaryKey, ihSeederKey, ihLeecherKey = ih4Key, ih4SeederKey, ih4LeecherKey
|
||||
}
|
||||
infoHashKeys, err := ps.con.SMembers(ps.ctx, ihSummaryKey).Result()
|
||||
err = asNil(err)
|
||||
if err == nil {
|
||||
for _, infoHashKey := range infoHashKeys {
|
||||
var cntKey string
|
||||
var seeder bool
|
||||
if seeder = strings.HasPrefix(infoHashKey, ihSeederKey); seeder {
|
||||
cntKey = cntSeederKey
|
||||
} else if strings.HasPrefix(infoHashKey, ihLeecherKey) {
|
||||
cntKey = cntLeecherKey
|
||||
} else {
|
||||
log.Warn("storage: Redis: unexpected record found in info hash set", log.Fields{
|
||||
"hashSet": ihSummaryKey,
|
||||
"infoHashKey": infoHashKey,
|
||||
})
|
||||
continue
|
||||
}
|
||||
// list all (peer, timeout) pairs for the ih
|
||||
peerList, err := ps.con.HGetAll(ps.ctx, infoHashKey).Result()
|
||||
err = asNil(err)
|
||||
if err == nil {
|
||||
var removedPeerCount int64
|
||||
for peerKey, timeStamp := range peerList {
|
||||
var peer bittorrent.Peer
|
||||
if peer, err = bittorrent.NewPeer(peerKey); err == nil {
|
||||
if mtime, err := strconv.ParseInt(timeStamp, 10, 64); err == nil {
|
||||
if mtime <= cutoffUnix {
|
||||
log.Debug("storage: Redis: deleting peer", log.Fields{
|
||||
"Peer": peer,
|
||||
})
|
||||
var count int64
|
||||
count, err = ps.con.HDel(ps.ctx, infoHashKey, peerKey).Result()
|
||||
err = asNil(err)
|
||||
if err == nil {
|
||||
removedPeerCount += count
|
||||
}
|
||||
}
|
||||
}
|
||||
if err != nil {
|
||||
log.Error("storage: Redis: unable to delete info hash peer", log.Fields{
|
||||
"group": group,
|
||||
"infoHash": infoHash,
|
||||
"peer": peer,
|
||||
"key": peerKey,
|
||||
"error": err,
|
||||
})
|
||||
}
|
||||
}
|
||||
// DECR seeder/leecher counter
|
||||
if removedPeerCount > 0 {
|
||||
var decrCounter string
|
||||
if isSeeder {
|
||||
decrCounter = seederCountKey(group)
|
||||
} else {
|
||||
decrCounter = leecherCountKey(group)
|
||||
}
|
||||
if err := ps.con.DecrBy(ps.ctx, decrCounter, removedPeerCount).Err(); err != nil {
|
||||
log.Error("storage: Redis: unable to decrement seeder/leecher peer count", log.Fields{
|
||||
"group": group,
|
||||
"infoHash": infoHash,
|
||||
"key": decrCounter,
|
||||
"error": err,
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
// use WATCH to avoid race condition
|
||||
// https://redis.io/topics/transactions
|
||||
err = asNil(ps.con.Watch(ps.ctx, func(tx *redis.Tx) (err error) {
|
||||
var infoHashCount int64
|
||||
infoHashCount, err = ps.con.HLen(ps.ctx, infoHash).Result()
|
||||
err = asNil(err)
|
||||
if err == nil && infoHashCount == 0 {
|
||||
// Empty hashes are not shown among existing keys,
|
||||
// in other words, it's removed automatically after `HDEL` the last field.
|
||||
// _, err := ps.con.Del(ps.ctx, infoHash)
|
||||
var deletedCount int64
|
||||
deletedCount, err = ps.con.HDel(ps.ctx, group, infoHash).Result()
|
||||
err = asNil(err)
|
||||
if err == nil && isSeeder && deletedCount > 0 {
|
||||
err = ps.con.Decr(ps.ctx, infoHashCountKey(group)).Err()
|
||||
}
|
||||
}
|
||||
return err
|
||||
}, infoHash))
|
||||
if err != nil {
|
||||
log.Error("storage: Redis: unable to clean info hash records", log.Fields{
|
||||
"group": group,
|
||||
"infoHash": infoHash,
|
||||
"error": err,
|
||||
log.Error("storage: Redis: unable to delete info hash peer", log.Fields{
|
||||
"hashSet": ihSummaryKey,
|
||||
"infoHashKey": infoHashKey,
|
||||
"peer": peer,
|
||||
"key": peerKey,
|
||||
"error": err,
|
||||
})
|
||||
}
|
||||
} else {
|
||||
log.Error("storage: Redis: unable to fetch info hash peers", log.Fields{
|
||||
"group": group,
|
||||
"infoHash": infoHash,
|
||||
"error": err,
|
||||
}
|
||||
// DECR seeder/leecher counter
|
||||
if removedPeerCount > 0 {
|
||||
if err := ps.con.DecrBy(ps.ctx, cntKey, removedPeerCount).Err(); err != nil {
|
||||
log.Error("storage: Redis: unable to decrement seeder/leecher peer count", log.Fields{
|
||||
"hashSet": ihSummaryKey,
|
||||
"infoHashKey": infoHashKey,
|
||||
"key": cntKey,
|
||||
"error": err,
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
// use WATCH to avoid race condition
|
||||
// https://redis.io/topics/transactions
|
||||
err = asNil(ps.con.Watch(ps.ctx, func(tx *redis.Tx) (err error) {
|
||||
var infoHashCount int64
|
||||
infoHashCount, err = ps.con.HLen(ps.ctx, infoHashKey).Result()
|
||||
err = asNil(err)
|
||||
if err == nil && infoHashCount == 0 {
|
||||
// Empty hashes are not shown among existing keys,
|
||||
// in other words, it's removed automatically after `HDEL` the last field.
|
||||
// _, err := ps.con.Del(ps.ctx, infoHashKey)
|
||||
var deletedCount int64
|
||||
deletedCount, err = ps.con.SRem(ps.ctx, ihSummaryKey, infoHashKey).Result()
|
||||
err = asNil(err)
|
||||
if err == nil && seeder && deletedCount > 0 {
|
||||
err = ps.con.Decr(ps.ctx, cntInfoHashKey).Err()
|
||||
}
|
||||
}
|
||||
return err
|
||||
}, infoHashKey))
|
||||
if err != nil {
|
||||
log.Error("storage: Redis: unable to clean info hash records", log.Fields{
|
||||
"hashSet": ihSummaryKey,
|
||||
"infoHashKey": infoHashKey,
|
||||
"error": err,
|
||||
})
|
||||
}
|
||||
} else {
|
||||
log.Error("storage: Redis: unable to fetch info hash peers", log.Fields{
|
||||
"hashSet": ihSummaryKey,
|
||||
"infoHashKey": infoHashKey,
|
||||
"error": err,
|
||||
})
|
||||
}
|
||||
} else {
|
||||
log.Error("storage: Redis: unable to fetch info hashes", log.Fields{"group": group, "error": err})
|
||||
}
|
||||
} else {
|
||||
log.Error("storage: Redis: unable to fetch info hash set", log.Fields{"hashSet": ihSummaryKey, "error": err})
|
||||
}
|
||||
|
||||
duration := time.Since(start).Milliseconds()
|
||||
log.Debug("storage: recordGCDuration", log.Fields{"timeTaken(ms)": duration})
|
||||
storage.PromGCDurationMilliseconds.Observe(float64(duration))
|
||||
}
|
||||
|
||||
func (ps *store) Stop() stop.Result {
|
||||
|
||||
+8
-8
@@ -55,33 +55,33 @@ var ErrDriverDoesNotExist = errors.New("peer store driver with that name does no
|
||||
type Storage interface {
|
||||
// PutSeeder adds a Seeder to the Swarm identified by the provided
|
||||
// InfoHash.
|
||||
PutSeeder(infoHash bittorrent.InfoHash, p bittorrent.Peer) error
|
||||
PutSeeder(infoHash bittorrent.InfoHash, peer bittorrent.Peer) error
|
||||
|
||||
// DeleteSeeder removes a Seeder from the Swarm identified by the
|
||||
// provided InfoHash.
|
||||
//
|
||||
// If the Swarm or Peer does not exist, this function returns
|
||||
// ErrResourceDoesNotExist.
|
||||
DeleteSeeder(infoHash bittorrent.InfoHash, p bittorrent.Peer) error
|
||||
DeleteSeeder(infoHash bittorrent.InfoHash, peer bittorrent.Peer) error
|
||||
|
||||
// PutLeecher adds a Leecher to the Swarm identified by the provided
|
||||
// InfoHash.
|
||||
// If the Swarm does not exist already, it is created.
|
||||
PutLeecher(infoHash bittorrent.InfoHash, p bittorrent.Peer) error
|
||||
PutLeecher(infoHash bittorrent.InfoHash, peer bittorrent.Peer) error
|
||||
|
||||
// DeleteLeecher removes a Leecher from the Swarm identified by the
|
||||
// provided InfoHash.
|
||||
//
|
||||
// If the Swarm or Peer does not exist, this function returns
|
||||
// ErrResourceDoesNotExist.
|
||||
DeleteLeecher(infoHash bittorrent.InfoHash, p bittorrent.Peer) error
|
||||
DeleteLeecher(infoHash bittorrent.InfoHash, peer bittorrent.Peer) error
|
||||
|
||||
// GraduateLeecher promotes a Leecher to a Seeder in the Swarm
|
||||
// identified by the provided InfoHash.
|
||||
//
|
||||
// If the given Peer is not present as a Leecher or the swarm does not exist
|
||||
// already, the Peer is added as a Seeder and no error is returned.
|
||||
GraduateLeecher(infoHash bittorrent.InfoHash, p bittorrent.Peer) error
|
||||
GraduateLeecher(infoHash bittorrent.InfoHash, peer bittorrent.Peer) error
|
||||
|
||||
// AnnouncePeers is a best effort attempt to return Peers from the Swarm
|
||||
// identified by the provided InfoHash.
|
||||
@@ -98,7 +98,7 @@ type Storage interface {
|
||||
// leechers
|
||||
//
|
||||
// Returns ErrResourceDoesNotExist if the provided InfoHash is not tracked.
|
||||
AnnouncePeers(infoHash bittorrent.InfoHash, seeder bool, numWant int, p bittorrent.Peer) (peers []bittorrent.Peer, err error)
|
||||
AnnouncePeers(infoHash bittorrent.InfoHash, seeder bool, numWant int, peer bittorrent.Peer) (peers []bittorrent.Peer, err error)
|
||||
|
||||
// ScrapeSwarm returns information required to answer a Scrape request
|
||||
// about a Swarm identified by the given InfoHash.
|
||||
@@ -108,7 +108,7 @@ type Storage interface {
|
||||
// filling the Snatches field is optional.
|
||||
//
|
||||
// If the Swarm does not exist, an empty Scrape and no error is returned.
|
||||
ScrapeSwarm(infoHash bittorrent.InfoHash, addressFamily bittorrent.AddressFamily) bittorrent.Scrape
|
||||
ScrapeSwarm(infoHash bittorrent.InfoHash, peer bittorrent.Peer) bittorrent.Scrape
|
||||
|
||||
// Put used to place arbitrary k-v data with specified context
|
||||
// into storage. ctx parameter used to group data
|
||||
@@ -117,7 +117,7 @@ type Storage interface {
|
||||
|
||||
// BulkPut used to place array of k-v data in specified context.
|
||||
// Useful when several data entries should be added in single transaction/connection
|
||||
BulkPut(ctx string, pairs ...Entry) error
|
||||
BulkPut(ctx string, values ...Entry) error
|
||||
|
||||
// Contains checks if any data in specified context exist
|
||||
Contains(ctx string, key string) (bool, error)
|
||||
|
||||
@@ -2,13 +2,13 @@ package test
|
||||
|
||||
import (
|
||||
"math/rand"
|
||||
"net"
|
||||
"net/netip"
|
||||
"runtime"
|
||||
"sync/atomic"
|
||||
"testing"
|
||||
|
||||
"github.com/sot-tech/mochi/bittorrent"
|
||||
"github.com/sot-tech/mochi/pkg/randseed"
|
||||
_ "github.com/sot-tech/mochi/pkg/randseed"
|
||||
"github.com/sot-tech/mochi/storage"
|
||||
)
|
||||
|
||||
@@ -28,23 +28,25 @@ func generateInfoHashes() (a [1000]bittorrent.InfoHash) {
|
||||
}
|
||||
|
||||
func generatePeers() (a [1000]bittorrent.Peer) {
|
||||
r := rand.New(rand.NewSource(randseed.GenSeed()))
|
||||
for i := range a {
|
||||
ip := make([]byte, 4)
|
||||
n, err := r.Read(ip)
|
||||
n, err := rand.Read(ip)
|
||||
if err != nil || n != 4 {
|
||||
panic("unable to create random bytes")
|
||||
}
|
||||
id := [bittorrent.PeerIDLen]byte{}
|
||||
n, err = r.Read(id[:])
|
||||
n, err = rand.Read(id[:])
|
||||
if err != nil || n != bittorrent.InfoHashV1Len {
|
||||
panic("unable to create random bytes")
|
||||
}
|
||||
port := uint16(r.Uint32())
|
||||
addr, ok := netip.AddrFromSlice(ip)
|
||||
if !ok {
|
||||
panic("unable to create ip from random bytes")
|
||||
}
|
||||
port := uint16(rand.Uint32())
|
||||
a[i] = bittorrent.Peer{
|
||||
ID: id,
|
||||
IP: bittorrent.IP{IP: net.IP(ip), AddressFamily: bittorrent.IPv4},
|
||||
Port: port,
|
||||
ID: id,
|
||||
AddrPort: netip.AddrPortFrom(addr, port),
|
||||
}
|
||||
}
|
||||
|
||||
@@ -442,7 +444,7 @@ func (bh *benchHolder) AnnounceSeeder1kInfoHash(b *testing.B) {
|
||||
// ScrapeSwarm can run in parallel.
|
||||
func (bh *benchHolder) ScrapeSwarm(b *testing.B) {
|
||||
bh.runBenchmark(b, true, putPeers, func(i int, ps storage.Storage, bd *benchData) error {
|
||||
ps.ScrapeSwarm(bd.infohashes[0], bittorrent.IPv4)
|
||||
ps.ScrapeSwarm(bd.infohashes[0], bd.peers[0])
|
||||
return nil
|
||||
})
|
||||
}
|
||||
@@ -452,7 +454,7 @@ func (bh *benchHolder) ScrapeSwarm(b *testing.B) {
|
||||
// ScrapeSwarm1kInfoHash can run in parallel.
|
||||
func (bh *benchHolder) ScrapeSwarm1kInfoHash(b *testing.B) {
|
||||
bh.runBenchmark(b, true, putPeers, func(i int, ps storage.Storage, bd *benchData) error {
|
||||
ps.ScrapeSwarm(bd.infohashes[i%1000], bittorrent.IPv4)
|
||||
ps.ScrapeSwarm(bd.infohashes[i%1000], bd.peers[0])
|
||||
return nil
|
||||
})
|
||||
}
|
||||
|
||||
@@ -34,7 +34,7 @@ func (th *testHolder) DeleteSeeder(t *testing.T) {
|
||||
func (th *testHolder) PutLeecher(t *testing.T) {
|
||||
for _, c := range testData {
|
||||
peer := v4Peer
|
||||
if c.peer.IP.AddressFamily == bittorrent.IPv6 {
|
||||
if c.peer.Addr().Is6() {
|
||||
peer = v6Peer
|
||||
}
|
||||
err := th.st.PutLeecher(c.ih, peer)
|
||||
@@ -52,7 +52,7 @@ func (th *testHolder) DeleteLeecher(t *testing.T) {
|
||||
func (th *testHolder) AnnouncePeers(t *testing.T) {
|
||||
for _, c := range testData {
|
||||
peer := v4Peer
|
||||
if c.peer.IP.AddressFamily == bittorrent.IPv6 {
|
||||
if c.peer.Addr().Is6() {
|
||||
peer = v6Peer
|
||||
}
|
||||
_, err := th.st.AnnouncePeers(c.ih, false, 50, peer)
|
||||
@@ -62,7 +62,7 @@ func (th *testHolder) AnnouncePeers(t *testing.T) {
|
||||
|
||||
func (th *testHolder) ScrapeSwarm(t *testing.T) {
|
||||
for _, c := range testData {
|
||||
scrape := th.st.ScrapeSwarm(c.ih, c.peer.IP.AddressFamily)
|
||||
scrape := th.st.ScrapeSwarm(c.ih, c.peer)
|
||||
require.Equal(t, uint32(0), scrape.Complete)
|
||||
require.Equal(t, uint32(0), scrape.Incomplete)
|
||||
require.Equal(t, uint32(0), scrape.Snatches)
|
||||
@@ -72,7 +72,7 @@ func (th *testHolder) ScrapeSwarm(t *testing.T) {
|
||||
func (th *testHolder) LeecherPutAnnounceDeleteAnnounce(t *testing.T) {
|
||||
for _, c := range testData {
|
||||
peer := v4Peer
|
||||
if c.peer.IP.AddressFamily == bittorrent.IPv6 {
|
||||
if c.peer.Addr().Is6() {
|
||||
peer = v6Peer
|
||||
}
|
||||
err := th.st.PutLeecher(c.ih, c.peer)
|
||||
@@ -87,7 +87,7 @@ func (th *testHolder) LeecherPutAnnounceDeleteAnnounce(t *testing.T) {
|
||||
require.Nil(t, err)
|
||||
require.True(t, containsPeer(peers, c.peer))
|
||||
|
||||
scrape := th.st.ScrapeSwarm(c.ih, c.peer.IP.AddressFamily)
|
||||
scrape := th.st.ScrapeSwarm(c.ih, c.peer)
|
||||
require.Equal(t, uint32(2), scrape.Incomplete)
|
||||
require.Equal(t, uint32(0), scrape.Complete)
|
||||
|
||||
@@ -103,7 +103,7 @@ func (th *testHolder) LeecherPutAnnounceDeleteAnnounce(t *testing.T) {
|
||||
func (th *testHolder) SeederPutAnnounceDeleteAnnounce(t *testing.T) {
|
||||
for _, c := range testData {
|
||||
peer := v4Peer
|
||||
if c.peer.IP.AddressFamily == bittorrent.IPv6 {
|
||||
if c.peer.Addr().Is6() {
|
||||
peer = v6Peer
|
||||
}
|
||||
err := th.st.PutSeeder(c.ih, c.peer)
|
||||
@@ -114,7 +114,7 @@ func (th *testHolder) SeederPutAnnounceDeleteAnnounce(t *testing.T) {
|
||||
require.Nil(t, err)
|
||||
require.True(t, containsPeer(peers, c.peer))
|
||||
|
||||
scrape := th.st.ScrapeSwarm(c.ih, c.peer.IP.AddressFamily)
|
||||
scrape := th.st.ScrapeSwarm(c.ih, c.peer)
|
||||
require.Equal(t, uint32(1), scrape.Incomplete)
|
||||
require.Equal(t, uint32(1), scrape.Complete)
|
||||
|
||||
@@ -130,7 +130,7 @@ func (th *testHolder) SeederPutAnnounceDeleteAnnounce(t *testing.T) {
|
||||
func (th *testHolder) LeecherPutGraduateAnnounceDeleteAnnounce(t *testing.T) {
|
||||
for _, c := range testData {
|
||||
peer := v4Peer
|
||||
if c.peer.IP.AddressFamily == bittorrent.IPv6 {
|
||||
if c.peer.Addr().Is6() {
|
||||
peer = v6Peer
|
||||
}
|
||||
err := th.st.PutLeecher(c.ih, c.peer)
|
||||
|
||||
@@ -1,33 +1,36 @@
|
||||
package test
|
||||
|
||||
import (
|
||||
"net"
|
||||
"net/netip"
|
||||
|
||||
"github.com/sot-tech/mochi/bittorrent"
|
||||
)
|
||||
|
||||
var (
|
||||
testIh1, testIh2 bittorrent.InfoHash
|
||||
testPeerID bittorrent.PeerID
|
||||
testData []hashPeer
|
||||
v4Peer, v6Peer bittorrent.Peer
|
||||
testIh1, testIh2 bittorrent.InfoHash
|
||||
testPeerID0, testPeerID1, testPeerID2, testPeerID3 bittorrent.PeerID
|
||||
testData []hashPeer
|
||||
v4Peer, v6Peer bittorrent.Peer
|
||||
)
|
||||
|
||||
func init() {
|
||||
testIh1, _ = bittorrent.NewInfoHash("00000000000000000001")
|
||||
testIh2, _ = bittorrent.NewInfoHash("00000000000000000002")
|
||||
testPeerID, _ = bittorrent.NewPeerID([]byte("00000000000000000001"))
|
||||
testPeerID0, _ = bittorrent.NewPeerID([]byte("00000000000000000001"))
|
||||
testPeerID1, _ = bittorrent.NewPeerID([]byte("00000000000000000002"))
|
||||
testPeerID2, _ = bittorrent.NewPeerID([]byte("99999999999999999994"))
|
||||
testPeerID3, _ = bittorrent.NewPeerID([]byte("99999999999999999996"))
|
||||
testData = []hashPeer{
|
||||
{
|
||||
testIh1,
|
||||
bittorrent.Peer{ID: testPeerID, Port: 1, IP: bittorrent.IP{IP: net.ParseIP("1.1.1.1").To4(), AddressFamily: bittorrent.IPv4}},
|
||||
bittorrent.Peer{ID: testPeerID0, AddrPort: netip.MustParseAddrPort("1.1.1.1:1")},
|
||||
},
|
||||
{
|
||||
testIh2,
|
||||
bittorrent.Peer{ID: testPeerID, Port: 2, IP: bittorrent.IP{IP: net.ParseIP("abab::0001"), AddressFamily: bittorrent.IPv6}},
|
||||
bittorrent.Peer{ID: testPeerID1, AddrPort: netip.MustParseAddrPort("[abab::0001]:2")},
|
||||
},
|
||||
}
|
||||
|
||||
v4Peer = bittorrent.Peer{ID: testPeerID, IP: bittorrent.IP{IP: net.ParseIP("99.99.99.99").To4(), AddressFamily: bittorrent.IPv4}, Port: 9994}
|
||||
v6Peer = bittorrent.Peer{ID: testPeerID, IP: bittorrent.IP{IP: net.ParseIP("fc00::0001"), AddressFamily: bittorrent.IPv6}, Port: 9996}
|
||||
v4Peer = bittorrent.Peer{ID: testPeerID2, AddrPort: netip.MustParseAddrPort("99.99.99.99:9994")}
|
||||
v6Peer = bittorrent.Peer{ID: testPeerID3, AddrPort: netip.MustParseAddrPort("[fc00::0001]:9996")}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user