|
| 1 | +package federation |
| 2 | + |
| 3 | +import ( |
| 4 | + "context" |
| 5 | + "errors" |
| 6 | + "net/http" |
| 7 | + "sync" |
| 8 | + "time" |
| 9 | + |
| 10 | + "github.com/ipfs/go-cid" |
| 11 | + "github.com/ipfs/go-datastore" |
| 12 | + logging "github.com/ipfs/go-log/v2" |
| 13 | + "github.com/ipld/go-ipld-prime" |
| 14 | + cidlink "github.com/ipld/go-ipld-prime/linking/cid" |
| 15 | + "github.com/ipld/go-ipld-prime/node/basicnode" |
| 16 | + "github.com/ipld/go-ipld-prime/node/bindnode" |
| 17 | + "github.com/libp2p/go-libp2p/core/crypto" |
| 18 | + "github.com/libp2p/go-libp2p/core/peer" |
| 19 | +) |
| 20 | + |
| 21 | +var ( |
| 22 | + headSnapshotKey = datastore.NewKey("head-snapshot") |
| 23 | + logger = logging.Logger("federation") |
| 24 | +) |
| 25 | + |
| 26 | +type ( |
| 27 | + Federation struct { |
| 28 | + *options |
| 29 | + members []peer.ID |
| 30 | + vc vectorClock |
| 31 | + |
| 32 | + mu sync.Mutex |
| 33 | + snapshotTicker *time.Ticker |
| 34 | + reconciliationTicker *time.Ticker |
| 35 | + headNodeCache ipld.Node |
| 36 | + ctx context.Context |
| 37 | + cancel context.CancelFunc |
| 38 | + } |
| 39 | +) |
| 40 | + |
| 41 | +func New(o ...Option) (*Federation, error) { |
| 42 | + opts, err := newOptions(o...) |
| 43 | + if err != nil { |
| 44 | + return nil, err |
| 45 | + } |
| 46 | + ctx, cancel := context.WithCancel(context.Background()) |
| 47 | + return &Federation{ |
| 48 | + options: opts, |
| 49 | + snapshotTicker: time.NewTicker(opts.snapshotInterval), |
| 50 | + reconciliationTicker: time.NewTicker(opts.reconciliationInterval), |
| 51 | + vc: newVectorClock(), |
| 52 | + ctx: ctx, |
| 53 | + cancel: cancel, |
| 54 | + }, nil |
| 55 | +} |
| 56 | + |
| 57 | +func (f *Federation) Start(_ context.Context) error { |
| 58 | + go func() { |
| 59 | + for { |
| 60 | + select { |
| 61 | + case <-f.ctx.Done(): |
| 62 | + logger.Warnw("Stopping federation event loop", "err", f.ctx.Err()) |
| 63 | + return |
| 64 | + case t := <-f.snapshotTicker.C: |
| 65 | + if err := f.snapshot(f.ctx, t); err != nil { |
| 66 | + logger.Errorw("Failed to take snapshot", "err", err) |
| 67 | + } |
| 68 | + case t := <-f.reconciliationTicker.C: |
| 69 | + if err := f.reconcile(f.ctx, t); err != nil { |
| 70 | + logger.Errorw("Failed to reconcile", "err", err) |
| 71 | + } |
| 72 | + } |
| 73 | + } |
| 74 | + }() |
| 75 | + return nil |
| 76 | +} |
| 77 | + |
| 78 | +func (f *Federation) ServeMux() *http.ServeMux { |
| 79 | + mux := http.NewServeMux() |
| 80 | + mux.HandleFunc("/ipni/v1/fed/head", f.handleV1FedHead) |
| 81 | + mux.HandleFunc("/ipni/v1/fed/", f.handleV1FedHead) |
| 82 | + return mux |
| 83 | +} |
| 84 | + |
| 85 | +func (f *Federation) snapshot(ctx context.Context, t time.Time) error { |
| 86 | + f.mu.Lock() |
| 87 | + defer f.mu.Unlock() |
| 88 | + |
| 89 | + previous, err := f.getHeadLink(ctx) |
| 90 | + if err != nil { |
| 91 | + return err |
| 92 | + } |
| 93 | + newSnapshot := &Snapshot{ |
| 94 | + Epoch: uint64(t.UTC().Unix()), // TODO: consider time dilution? |
| 95 | + VectorClock: f.vc.tick(f.host.ID()), |
| 96 | + Providers: ProvidersIngestStatusMap{ |
| 97 | + Values: make(map[string]IngestStatus), |
| 98 | + }, |
| 99 | + Previous: previous, |
| 100 | + } |
| 101 | + for _, info := range f.registry.AllProviderInfo() { |
| 102 | + var latestProcessedAdLink ipld.Link |
| 103 | + if !info.LastAdvertisement.Equals(cid.Undef) { |
| 104 | + latestProcessedAdLink = cidlink.Link{Cid: info.LastAdvertisement} |
| 105 | + } |
| 106 | + key := info.AddrInfo.ID.String() |
| 107 | + value := IngestStatus{ |
| 108 | + LastAdvertisement: latestProcessedAdLink, |
| 109 | + // TODO: Add height hint once it is measured by indexers. |
| 110 | + } |
| 111 | + newSnapshot.Providers.Put(key, value) |
| 112 | + } |
| 113 | + |
| 114 | + node := bindnode.Wrap(newSnapshot, Prototypes.Snapshot.Type()) |
| 115 | + headSnapshotLink, err := f.linkSystem.Store(ipld.LinkContext{Ctx: ctx}, Prototypes.link, node) |
| 116 | + if err != nil { |
| 117 | + f.vc.untick(f.host.ID()) |
| 118 | + return err |
| 119 | + } |
| 120 | + err = f.setHeadLink(ctx, headSnapshotLink) |
| 121 | + if err != nil { |
| 122 | + f.vc.untick(f.host.ID()) |
| 123 | + } |
| 124 | + return err |
| 125 | +} |
| 126 | + |
| 127 | +func (f *Federation) getHeadNode(ctx context.Context) (ipld.Node, error) { |
| 128 | + f.mu.Lock() |
| 129 | + defer f.mu.Unlock() |
| 130 | + |
| 131 | + if f.headNodeCache != nil { |
| 132 | + return f.headNodeCache, nil |
| 133 | + } |
| 134 | + |
| 135 | + headLink, err := f.getHeadLink(ctx) |
| 136 | + switch { |
| 137 | + case errors.Is(err, datastore.ErrNotFound): |
| 138 | + // Set headNodeCache to an empty node to avoid hitting datastore every time head is fetched. |
| 139 | + f.headNodeCache = basicnode.Prototype.Any.NewBuilder().Build() |
| 140 | + return f.headNodeCache, nil |
| 141 | + case err != nil: |
| 142 | + return nil, err |
| 143 | + default: |
| 144 | + key, err := f.host.ID().ExtractPublicKey() |
| 145 | + if err != nil { |
| 146 | + logger.Errorw("Failed to get public key", "err", err) |
| 147 | + return nil, err |
| 148 | + } |
| 149 | + marshalledPubKey, err := crypto.MarshalPublicKey(key) |
| 150 | + if err != nil { |
| 151 | + logger.Errorw("Failed to marshal public key", "err", err) |
| 152 | + return nil, err |
| 153 | + } |
| 154 | + head := Head{ |
| 155 | + Head: headLink, |
| 156 | + PublicKey: marshalledPubKey, |
| 157 | + } |
| 158 | + if err := head.Sign(f.host.Peerstore().PrivKey(f.host.ID())); err != nil { |
| 159 | + logger.Errorw("Failed to sign head", "err", err) |
| 160 | + return nil, err |
| 161 | + } |
| 162 | + f.headNodeCache = bindnode.Wrap(head, Prototypes.Head.Type()) |
| 163 | + return f.headNodeCache, nil |
| 164 | + } |
| 165 | +} |
| 166 | + |
| 167 | +func (f *Federation) getHeadLink(ctx context.Context) (ipld.Link, error) { |
| 168 | + switch v, err := f.datastore.Get(ctx, headSnapshotKey); { |
| 169 | + case errors.Is(err, datastore.ErrNotFound): |
| 170 | + return nil, nil |
| 171 | + case err != nil: |
| 172 | + return nil, err |
| 173 | + default: |
| 174 | + _, c, err := cid.CidFromBytes(v) |
| 175 | + if err != nil { |
| 176 | + return nil, err |
| 177 | + } |
| 178 | + return cidlink.Link{Cid: c}, nil |
| 179 | + } |
| 180 | +} |
| 181 | + |
| 182 | +func (f *Federation) setHeadLink(ctx context.Context, head ipld.Link) error { |
| 183 | + return f.datastore.Put(ctx, headSnapshotKey, head.(cidlink.Link).Cid.Bytes()) |
| 184 | +} |
| 185 | + |
| 186 | +func (f *Federation) Shutdown(_ context.Context) error { |
| 187 | + f.snapshotTicker.Stop() |
| 188 | + f.cancel() |
| 189 | + return nil |
| 190 | +} |
| 191 | + |
| 192 | +func (f *Federation) reconcile(ctx context.Context, t time.Time) error { |
| 193 | + // TODO: For each f.peers, get head, get snapshot, f.vc.reconcile, aggregate providers. |
| 194 | + return nil |
| 195 | +} |
0 commit comments