Skip to content

Commit 9c4a6a1

Browse files
committed
- readme update
- sync and lock fixes for the file handel pool
1 parent 54d0bf5 commit 9c4a6a1

File tree

2 files changed

+39
-28
lines changed

2 files changed

+39
-28
lines changed

README.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ Sources and targets are decoupled, this design enables the composition of variou
2929
Download, extract and set permissions:
3030

3131
```bash
32-
wget -O bp_linux.tar.gz https://github.com/Azure/blobporter/releases/download/v0.5.22/bp_linux.tar.gz
32+
wget -O bp_linux.tar.gz https://github.com/Azure/blobporter/releases/download/v0.5.23/bp_linux.tar.gz
3333
tar -xvf bp_linux.tar.gz linux_amd64/blobporter
3434
chmod +x ~/linux_amd64/blobporter
3535
cd ~/linux_amd64
@@ -46,7 +46,7 @@ export ACCOUNT_KEY=<STORAGE_ACCOUNT_KEY>
4646
4747
### Windows
4848

49-
Download [BlobPorter.exe](https://github.com/Azure/blobporter/releases/download/v0.5.22/bp_windows.zip)
49+
Download [BlobPorter.exe](https://github.com/Azure/blobporter/releases/download/v0.5.23/bp_windows.zip)
5050

5151
Set environment variables (if using the command prompt):
5252

targets/multifile.go

Lines changed: 37 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,6 @@ import (
88
"sync"
99
"time"
1010

11-
"github.com/Azure/blobporter/util"
12-
1311
"github.com/Azure/blobporter/pipeline"
1412
)
1513

@@ -116,13 +114,16 @@ func (h *fileHandleManager) getHandle(path string) (*os.File, error) {
116114

117115
if !isInit {
118116
fh, err = h.initFile(path)
119-
if err != nil {
120-
return nil, err
121-
}
122117
}
123118
h.initTracker.Store(path, true)
124119
h.Unlock()
125120

121+
if err != nil {
122+
return nil, err
123+
}
124+
125+
126+
126127
if !h.cacheEnabled {
127128
//we don't have handle from the initialization, so open a new one.
128129
if fh != nil {
@@ -135,42 +136,48 @@ func (h *fileHandleManager) getHandle(path string) (*os.File, error) {
135136

136137
//try to get it from the cache
137138
var fhQ chan *os.File
138-
var ok bool
139+
var incache bool
139140
var val interface{}
140-
val, ok = h.fileHandlesQMap.Load(path)
141-
if ok {
141+
142+
val, incache = h.fileHandlesQMap.Load(path)
143+
if incache {
142144
fhQ = val.(chan *os.File)
143145
fh := <-fhQ
146+
144147
return fh, nil
145148
}
146149

147-
//at this point we need to create a new handle and create the pool handles if not full if it hasn't be created.
148-
if fh, err = os.OpenFile(path, os.O_WRONLY, os.ModeAppend); err != nil {
149-
return nil, err
150+
//if init, the handle from the creation is not available (e.i. fh == nil)
151+
if isInit {
152+
if fh, err = os.OpenFile(path, os.O_WRONLY, os.ModeAppend); err != nil {
153+
return nil, err
154+
}
155+
150156
}
151157

152-
//the pool is full return the handle
158+
h.Lock()
159+
defer h.Unlock()
160+
//ok not in the cache so we need to check if the cache is full
153161
if h.cachedFileHandles+h.numOfHandlesPerFile > maxFileHandlesInCache {
154-
util.PrintfIfDebug("getHandle -> Cache is full: %v\n", h.cachedFileHandles+h.numOfHandlesPerFile)
155162
return fh, nil
156163
}
157164

158-
//prime the pool with additional handles
159-
if !isInit {
160-
fhQ = make(chan *os.File, h.numOfHandlesPerFile)
161-
h.cachedFileHandles = h.cachedFileHandles + 1
162-
for i := 1; i < h.numOfHandlesPerFile; i++ {
163-
var fhi *os.File
164-
if fhi, err = os.OpenFile(path, os.O_WRONLY, os.ModeAppend); err != nil {
165-
return nil, err
166-
}
167-
fhQ <- fhi
168-
h.cachedFileHandles = h.cachedFileHandles + 1
169-
}
165+
//add items to the cache
166+
fhQ = make(chan *os.File, h.numOfHandlesPerFile)
167+
h.cachedFileHandles = h.cachedFileHandles + 1
168+
for i := 1; i < h.numOfHandlesPerFile; i++ {
169+
var fhi *os.File
170170

171-
h.fileHandlesQMap.Store(path, fhQ)
171+
if fhi, err = os.OpenFile(path, os.O_WRONLY, os.ModeAppend); err != nil {
172+
return nil, err
173+
}
174+
175+
fhQ <- fhi
176+
h.cachedFileHandles = h.cachedFileHandles + 1
172177
}
173178

179+
h.fileHandlesQMap.Store(path, fhQ)
180+
174181
return fh, nil
175182

176183
}
@@ -187,14 +194,18 @@ func (h *fileHandleManager) returnHandle(path string, fileHandle *os.File) error
187194
var ok bool
188195

189196
val, ok = h.fileHandlesQMap.Load(path)
197+
190198
if ok {
191199
fhq = val.(chan *os.File)
200+
//fmt.Printf("return to dequeue %v\n", path)
192201
fhq <- fileHandle
202+
//fmt.Printf("after to dequeue %v\n", path)
193203
h.fileHandlesQMap.Store(path, fhq)
194204
return nil
195205
}
196206

197207
//not found in the map, which is the case when the cache is at capacity
208+
//fmt.Printf("close (not in the map) %v", path)
198209
return fileHandle.Close()
199210
}
200211

0 commit comments

Comments
 (0)