Skip to content

Commit aab19fd

Browse files
committed
redo: don't re-provide blocks we've provided very recently (take 2)
this redoes (again) 582e5de. License: MIT Signed-off-by: Kevin Atkinson <[email protected]>
1 parent f50bda3 commit aab19fd

File tree

6 files changed

+52
-38
lines changed

6 files changed

+52
-38
lines changed

blockservice/blockservice.go

+35-21
Original file line numberDiff line numberDiff line change
@@ -37,8 +37,9 @@ type BlockService interface {
3737

3838
type blockService struct {
3939
// TODO don't expose underlying impl details
40-
blockstore blockstore.Blockstore
41-
exchange exchange.Interface
40+
blockstore blockstore.Blockstore
41+
exchange exchange.Interface
42+
checkFirst bool
4243
}
4344

4445
// an Object is simply a typed block
@@ -56,6 +57,21 @@ func New(bs blockstore.Blockstore, rem exchange.Interface) BlockService {
5657
return &blockService{
5758
blockstore: bs,
5859
exchange: rem,
60+
checkFirst: true,
61+
}
62+
}
63+
64+
// NewWriteThrough ceates a BlockService that guarantees writes we go
65+
// through to the blockstore
66+
func NewWriteThrough(bs blockstore.Blockstore, rem exchange.Interface) BlockService {
67+
if rem == nil {
68+
log.Warning("blockservice running in local (offline) mode.")
69+
}
70+
71+
return &blockService{
72+
blockstore: bs,
73+
exchange: rem,
74+
checkFirst: false,
5975
}
6076
}
6177

@@ -70,22 +86,19 @@ func (bs *blockService) Exchange() exchange.Interface {
7086
// AddBlock adds a particular block to the service, Putting it into the datastore.
7187
// TODO pass a context into this if the remote.HasBlock is going to remain here.
7288
func (s *blockService) AddObject(o Object) (*cid.Cid, error) {
73-
// TODO: while this is a great optimization, we should think about the
74-
// possibility of streaming writes directly to disk. If we can pass this object
75-
// all the way down to the datastore without having to 'buffer' its data,
76-
// we could implement a `WriteTo` method on it that could do a streaming write
77-
// of the content, saving us (probably) considerable memory.
7889
c := o.Cid()
79-
has, err := s.blockstore.Has(key.Key(c.Hash()))
80-
if err != nil {
81-
return nil, err
82-
}
90+
if s.checkFirst {
91+
has, err := s.blockstore.Has(key.Key(c.Hash()))
92+
if err != nil {
93+
return nil, err
94+
}
8395

84-
if has {
85-
return c, nil
96+
if has {
97+
return c, nil
98+
}
8699
}
87100

88-
err = s.blockstore.Put(o)
101+
err := s.blockstore.Put(o)
89102
if err != nil {
90103
return nil, err
91104
}
@@ -103,13 +116,14 @@ func (s *blockService) AddObjects(bs []Object) ([]*cid.Cid, error) {
103116
for _, b := range bs {
104117
c := b.Cid()
105118

106-
has, err := s.blockstore.Has(key.Key(c.Hash()))
107-
if err != nil {
108-
return nil, err
109-
}
110-
111-
if has {
112-
continue
119+
if s.checkFirst {
120+
has, err := s.blockstore.Has(key.Key(c.Hash()))
121+
if err != nil {
122+
return nil, err
123+
}
124+
if has {
125+
continue
126+
}
113127
}
114128

115129
toput = append(toput, b)

core/commands/add.go

+2-2
Original file line numberDiff line numberDiff line change
@@ -179,7 +179,7 @@ You can now refer to the added file in a gateway, like so:
179179
return
180180
}
181181
blockstore := filestore_support.NewBlockstore(n.Blockstore, fs)
182-
blockService := bserv.New(blockstore, exchange)
182+
blockService := bserv.NewWriteThrough(blockstore, exchange)
183183
dagService := dag.NewDAGService(blockService)
184184
fileAdder, err = coreunix.NewAdder(req.Context(), n.Pinning, blockstore, dagService, useRoot)
185185
fileAdder.FullName = true
@@ -188,7 +188,7 @@ You can now refer to the added file in a gateway, like so:
188188
// add directly to the first mount bypassing
189189
// the Has() check of the multi-blockstore
190190
blockstore := bs.NewGCBlockstore(n.Blockstore.FirstMount(), n.Blockstore)
191-
blockService := bserv.New(blockstore, exchange)
191+
blockService := bserv.NewWriteThrough(blockstore, exchange)
192192
dagService := dag.NewDAGService(blockService)
193193
fileAdder, err = coreunix.NewAdder(req.Context(), n.Pinning, blockstore, dagService, useRoot)
194194
} else if exchange != n.Exchange {

test/sharness/lib/test-filestore-lib.sh

+3-3
Original file line numberDiff line numberDiff line change
@@ -80,7 +80,7 @@ test_post_add() {
8080
test_must_fail ipfs cat "$HASH" >/dev/null
8181
'
8282

83-
test_expect_failure "okay after re-adding under new name" '
83+
test_expect_success "okay after re-adding under new name" '
8484
ipfs $cmd "$dir"/mountdir/hello2.txt 2> add.output &&
8585
ipfs cat "$HASH" >/dev/null
8686
'
@@ -210,15 +210,15 @@ filestore_test_exact_paths() {
210210
echo "Hello Worlds!" > dirlink/hello.txt
211211
'
212212

213-
test_expect_failure "ipfs filestore add $opts adds under the expected path name (with symbolic links)" '
213+
test_expect_success "ipfs filestore add $opts adds under the expected path name (with symbolic links)" '
214214
FILEPATH="`pwd`/dirlink/hello.txt" &&
215215
ipfs filestore add $opt "$FILEPATH" &&
216216
echo "$FILEPATH" > ls-expected &&
217217
ipfs filestore ls-files -q QmVr26fY1tKyspEJBniVhqxQeEjhF78XerGiqWAwraVLQH > ls-actual &&
218218
test_cmp ls-expected ls-actual
219219
'
220220

221-
test_expect_failure "ipfs filestore ls dirlink/ works as expected" '
221+
test_expect_success "ipfs filestore ls dirlink/ works as expected" '
222222
echo "QmVr26fY1tKyspEJBniVhqxQeEjhF78XerGiqWAwraVLQH" > ls-expected
223223
ipfs filestore ls -q "`pwd`/dirlink/" > ls-actual
224224
test_cmp ls-expected ls-actual

test/sharness/t0260-filestore.sh

+2-2
Original file line numberDiff line numberDiff line change
@@ -169,13 +169,13 @@ test_expect_success "testing filestore dups pinned" '
169169
test_cmp dups-actual dups-expected
170170
'
171171

172-
test_expect_failure "testing filestore dups unpinned" '
172+
test_expect_success "testing filestore dups unpinned" '
173173
ipfs filestore dups unpinned > dups-actual &&
174174
echo QmPrrHqJzto9m7SyiRzarwkqPcCSsKR2EB1AyqJfe8L8tN > dups-expected &&
175175
test_cmp dups-actual dups-expected
176176
'
177177

178-
test_expect_failure "testing filestore dups" '
178+
test_expect_success "testing filestore dups" '
179179
ipfs filestore dups > dups-out &&
180180
grep QmZm53sWMaAQ59x56tFox8X9exJFELWC33NLjK6m8H7CpN dups-out &&
181181
grep QmPrrHqJzto9m7SyiRzarwkqPcCSsKR2EB1AyqJfe8L8tN dups-out

test/sharness/t0265-filestore-verify.sh

+8-8
Original file line numberDiff line numberDiff line change
@@ -163,7 +163,7 @@ interesting_prep() {
163163
ipfs filestore rm $E_HASH
164164
'
165165

166-
test_expect_failure "'filestore verify' produces expected output" '
166+
test_expect_success "'filestore verify' produces expected output" '
167167
cp verify-initial verify-now &&
168168
cmp_verify
169169
'
@@ -185,7 +185,7 @@ ok QmaVeSKhGmPYxRyqA236Y4N5e4Rn6LGZKdCgaYUarEo5Nu
185185
186186
ok QmcAkMdfBPYVzDCM6Fkrz1h8WXcprH8BLF6DmjNUGhXAnm
187187
EOF
188-
test_expect_failure "'filestore clean orphan' (should remove 'changed' orphan)" '
188+
test_expect_success "'filestore clean orphan' (should remove 'changed' orphan)" '
189189
ipfs filestore clean orphan &&
190190
cmp_verify
191191
'
@@ -205,7 +205,7 @@ orphan QmYswupx1AdGdTn6GeXVdaUBEe6rApd7GWSnobcuVZjeRV
205205
orphan QmfDSgGhGsEf7LHC6gc7FbBMhGuYzxTLnbKqFBkWhGt8Qp
206206
orphan QmSWnPbrLFmxfJ9vj2FvKKpVmu3SZprbt7KEbkUVjy7bMD
207207
EOF
208-
test_expect_failure "'filestore clean incomplete' (will create more orphans)" '
208+
test_expect_success "'filestore clean incomplete' (will create more orphans)" '
209209
ipfs filestore clean incomplete &&
210210
cmp_verify
211211
'
@@ -221,7 +221,7 @@ ok QmaVeSKhGmPYxRyqA236Y4N5e4Rn6LGZKdCgaYUarEo5Nu
221221
222222
ok QmcAkMdfBPYVzDCM6Fkrz1h8WXcprH8BLF6DmjNUGhXAnm
223223
EOF
224-
test_expect_failure "'filestore clean orphan'" '
224+
test_expect_success "'filestore clean orphan'" '
225225
ipfs filestore clean orphan &&
226226
cmp_verify
227227
'
@@ -238,7 +238,7 @@ orphan QmbZr7Fs6AJf7HpnTxDiYJqLXWDqAy3fKFXYVDkgSsH7DH
238238
orphan QmToAcacDnpqm17jV7rRHmXcS9686Mk59KCEYGAMkh9qCX
239239
orphan QmYtLWUVmevucXFN9q59taRT95Gxj5eJuLUhXKtwNna25t
240240
EOF
241-
test_expect_failure "'filestore clean changed incomplete' (will create more orphans)" '
241+
test_expect_success "'filestore clean changed incomplete' (will create more orphans)" '
242242
ipfs filestore clean changed incomplete &&
243243
cmp_verify
244244
'
@@ -255,7 +255,7 @@ orphan QmToAcacDnpqm17jV7rRHmXcS9686Mk59KCEYGAMkh9qCX
255255
orphan QmbZr7Fs6AJf7HpnTxDiYJqLXWDqAy3fKFXYVDkgSsH7DH
256256
orphan QmYtLWUVmevucXFN9q59taRT95Gxj5eJuLUhXKtwNna25t
257257
EOF
258-
test_expect_failure "'filestore clean no-file' (will create an incomplete)" '
258+
test_expect_success "'filestore clean no-file' (will create an incomplete)" '
259259
ipfs filestore clean no-file &&
260260
cmp_verify
261261
'
@@ -265,7 +265,7 @@ ok QmaVeSKhGmPYxRyqA236Y4N5e4Rn6LGZKdCgaYUarEo5Nu
265265
266266
ok QmcAkMdfBPYVzDCM6Fkrz1h8WXcprH8BLF6DmjNUGhXAnm
267267
EOF
268-
test_expect_failure "'filestore clean incomplete orphan' (cleanup)" '
268+
test_expect_success "'filestore clean incomplete orphan' (cleanup)" '
269269
cp verify-final verify-now &&
270270
ipfs filestore clean incomplete orphan &&
271271
cmp_verify
@@ -277,7 +277,7 @@ test_expect_failure "'filestore clean incomplete orphan' (cleanup)" '
277277

278278
interesting_prep
279279

280-
test_expect_failure "'filestore clean full'" '
280+
test_expect_success "'filestore clean full'" '
281281
cp verify-final verify-now &&
282282
ipfs filestore clean full &&
283283
cmp_verify

test/sharness/t0266-filestore-concurrent.sh

+2-2
Original file line numberDiff line numberDiff line change
@@ -75,11 +75,11 @@ test_expect_success "filestore clean invalid race condation" '(
7575
wait
7676
)'
7777

78-
test_expect_failure "filestore clean race condation: output looks good" '
78+
test_expect_success "filestore clean race condation: output looks good" '
7979
grep "cannot remove $HASH" clean-actual
8080
'
8181

82-
test_expect_failure "filestore clean race condation: file still available" '
82+
test_expect_success "filestore clean race condation: file still available" '
8383
ipfs cat "$HASH" > /dev/null
8484
'
8585

0 commit comments

Comments
 (0)