-
Notifications
You must be signed in to change notification settings - Fork 29
/
filestatsworker.go
117 lines (108 loc) · 3.54 KB
/
filestatsworker.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
package sdk
import (
"bytes"
"context"
"encoding/json"
"fmt"
"io/ioutil"
"mime/multipart"
"net/http"
"strings"
"sync"
"time"
"github.com/0chain/gosdk/zboxcore/blockchain"
"github.com/0chain/gosdk/zboxcore/fileref"
. "github.com/0chain/gosdk/zboxcore/logger"
"github.com/0chain/gosdk/zboxcore/zboxutil"
)
type FileStats struct {
Name string `json:"name"`
Size int64 `json:"size"`
PathHash string `json:"path_hash"`
Path string `json:"path"`
NumBlocks int64 `json:"num_of_blocks"`
NumUpdates int64 `json:"num_of_updates"`
NumBlockDownloads int64 `json:"num_of_block_downloads"`
SuccessChallenges int64 `json:"num_of_challenges"`
FailedChallenges int64 `json:"num_of_failed_challenges"`
LastChallengeResponseTxn string `json:"last_challenge_txn"`
WriteMarkerRedeemTxn string `json:"write_marker_txn"`
BlobberID string `json:"blobber_id"`
BlobberURL string `json:"blobber_url"`
BlockchainAware bool `json:"blockchain_aware"`
CreatedAt time.Time `json:"CreatedAt"`
}
type fileStatsResponse struct {
filestats *FileStats
responseStr string
blobberIdx int
err error
}
func (req *ListRequest) getFileStatsInfoFromBlobber(blobber *blockchain.StorageNode, blobberIdx int, rspCh chan<- *fileStatsResponse) {
defer req.wg.Done()
body := new(bytes.Buffer)
formWriter := multipart.NewWriter(body)
var fileStats *FileStats
var s strings.Builder
var err error
fileMetaRetFn := func() {
rspCh <- &fileStatsResponse{filestats: fileStats, responseStr: s.String(), blobberIdx: blobberIdx, err: err}
}
defer fileMetaRetFn()
if len(req.remotefilepath) > 0 {
req.remotefilepathhash = fileref.GetReferenceLookup(req.allocationID, req.remotefilepath)
}
formWriter.WriteField("path_hash", req.remotefilepathhash)
formWriter.Close()
httpreq, err := zboxutil.NewFileStatsRequest(blobber.Baseurl, req.allocationTx, body)
if err != nil {
Logger.Error("File meta info request error: ", err.Error())
return
}
httpreq.Header.Add("Content-Type", formWriter.FormDataContentType())
ctx, cncl := context.WithTimeout(req.ctx, (time.Second * 30))
err = zboxutil.HttpDo(ctx, cncl, httpreq, func(resp *http.Response, err error) error {
if err != nil {
Logger.Error("GetFileStats : ", err)
return err
}
defer resp.Body.Close()
resp_body, err := ioutil.ReadAll(resp.Body)
if err != nil {
return fmt.Errorf("Error: Resp : %s", err.Error())
}
s.WriteString(string(resp_body))
if resp.StatusCode == http.StatusOK {
err = json.Unmarshal(resp_body, &fileStats)
if err != nil {
return fmt.Errorf("file stats response parse error: %s", err.Error())
}
if len(fileStats.WriteMarkerRedeemTxn) > 0 {
fileStats.BlockchainAware = true
} else {
fileStats.BlockchainAware = false
}
fileStats.BlobberID = blobber.ID
fileStats.BlobberURL = blobber.Baseurl
return nil
}
return err
})
}
func (req *ListRequest) getFileStatsFromBlobbers() map[string]*FileStats {
numList := len(req.blobbers)
//fmt.Printf("%v\n", req.blobbers)
req.wg = &sync.WaitGroup{}
req.wg.Add(numList)
rspCh := make(chan *fileStatsResponse, numList)
for i := 0; i < numList; i++ {
go req.getFileStatsInfoFromBlobber(req.blobbers[i], i, rspCh)
}
req.wg.Wait()
fileInfos := make(map[string]*FileStats)
for i := 0; i < numList; i++ {
ch := <-rspCh
fileInfos[req.blobbers[i].ID] = ch.filestats
}
return fileInfos
}