-
Notifications
You must be signed in to change notification settings - Fork 2
/
longtermshipper.go
151 lines (115 loc) · 4.28 KB
/
longtermshipper.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
package longtermshipper
import (
"errors"
"github.com/boltdb/bolt"
"github.com/function61/eventhorizon/config"
"github.com/function61/eventhorizon/cursor"
"github.com/function61/eventhorizon/reader/store"
"github.com/function61/eventhorizon/scalablestore"
"github.com/function61/eventhorizon/writer/transaction"
wtypes "github.com/function61/eventhorizon/writer/types"
"log"
"os"
"sync"
"time"
)
/* longtermshipper is responsible for orchestrating compression, encryption and
uploading processing of sealed blocks in a durable manner.
Flow per shippable file:
1) Writer calls MarkFileToBeShipped() which:
- writes shipping metadata to database so failed shippings / crashes
can still guarantee that the block will get uploaded
- attaches the shippable file to the transaction metadata so it can
be executed in the side effects after transaction is committed
2) Writer's side effect processor calls Ship() with every shippable file.
3) Upon block shipment success the entry is removed from database.
Reliability: on Writer startup it calls RecoverUnfinishedShipments(), after which
the side effects processor calls Ship() if any files-to-recover were detected.
*/
type Shipper struct {
shipmentOperationsDone *sync.WaitGroup
compressedEncryptedStore *store.CompressedEncryptedStore
s3Manager *scalablestore.S3Manager
}
func New(confCtx *config.Context) *Shipper {
return &Shipper{
shipmentOperationsDone: &sync.WaitGroup{},
compressedEncryptedStore: store.NewCompressedEncryptedStore(confCtx),
s3Manager: scalablestore.NewS3Manager(confCtx),
}
}
func (s *Shipper) MarkFileToBeShipped(fileToShip *wtypes.LongTermShippableFile, tx *transaction.EventstoreTransaction) error {
filesToShipBucket, createBucketErr := tx.BoltTx.CreateBucketIfNotExists([]byte("_filestoship"))
if createBucketErr != nil {
return createBucketErr
}
filesToShipBucket.Put([]byte(fileToShip.Block.Serialize()), []byte(fileToShip.FilePath))
tx.ShipFiles = append(tx.ShipFiles, fileToShip)
return nil
}
// need DB reference so we can start transaction once the process finishes
func (s *Shipper) Ship(ltsf *wtypes.LongTermShippableFile, database *bolt.DB) {
// don't return from Close() until all shipments finish
s.shipmentOperationsDone.Add(1)
go func() {
defer s.shipmentOperationsDone.Done()
if err := s.shipOne(ltsf); err != nil {
log.Printf("Shipper: error %s", err.Error())
return
}
// ok shipping succeeded, so delete entry from database
// so it won't be re-tried
if err := database.Update(func(boltTx *bolt.Tx) error {
filesToShipBucket := boltTx.Bucket([]byte("_filestoship"))
if filesToShipBucket == nil {
return errors.New("Unable to get _filestoship bucket")
}
return filesToShipBucket.Delete([]byte(ltsf.Block.Serialize()))
}); err != nil {
panic(err)
}
}()
}
func (s *Shipper) shipOne(ltsf *wtypes.LongTermShippableFile) error {
started := time.Now()
log.Printf("Shipper: compressing & encrypting %s", ltsf.Block.ToChunkPath())
fd, err := os.Open(ltsf.FilePath)
if err != nil {
return err
}
defer fd.Close()
if err := s.compressedEncryptedStore.SaveFromLiveFile(ltsf.Block, fd); err != nil {
return err
}
if err := s.compressedEncryptedStore.UploadToS3(ltsf.Block, s.s3Manager); err != nil {
return err
}
log.Printf("Shipper: completed %s in %s", ltsf.Block.ToChunkPath(), time.Since(started))
return nil
}
func (s *Shipper) RecoverUnfinishedShipments(tx *transaction.EventstoreTransaction) error {
filesToShipBucket, createBucketErr := tx.BoltTx.CreateBucketIfNotExists([]byte("_filestoship"))
if createBucketErr != nil {
return createBucketErr
}
// do essentially the same as MarkFileToBeSent() for each recovered file,
// except for the fact that we have to deserialize this and not save it to DB
err := filesToShipBucket.ForEach(func(key, value []byte) error {
ltsf := &wtypes.LongTermShippableFile{
Block: cursor.CursorFromserializedMust(string(key)),
FilePath: string(value),
}
log.Printf("Shipper: recovering unshipped file %s", ltsf.FilePath)
tx.ShipFiles = append(tx.ShipFiles, ltsf)
return nil
})
if err != nil {
panic(err)
}
return nil
}
func (s *Shipper) Close() {
log.Printf("Shipper: stopping")
s.shipmentOperationsDone.Wait()
log.Printf("Shipper: stopped")
}