Skip to content

Commit

Permalink
start to reforge storage sync
Browse files Browse the repository at this point in the history
  • Loading branch information
zyxkad committed Aug 11, 2024
1 parent 18ee907 commit 4fe7a79
Show file tree
Hide file tree
Showing 18 changed files with 589 additions and 263 deletions.
13 changes: 7 additions & 6 deletions cluster/cluster.go
Original file line number Diff line number Diff line change
Expand Up @@ -63,8 +63,9 @@ type Cluster struct {
client *http.Client
cachedCli *http.Client

authTokenMux sync.RWMutex
authToken *ClusterToken
authTokenMux sync.RWMutex
authToken *ClusterToken
fileListLastMod int64
}

func NewCluster(
Expand Down Expand Up @@ -95,12 +96,12 @@ func (cr *Cluster) Secret() string {

// Host returns the cluster public host
func (cr *Cluster) Host() string {
return cr.gcfg.Host
return cr.gcfg.PublicHost
}

// Port returns the cluster public port
func (cr *Cluster) Port() uint16 {
return cr.gcfg.Port
return cr.gcfg.PublicPort
}

// PublicHosts returns the cluster public hosts
Expand Down Expand Up @@ -168,8 +169,8 @@ func (cr *Cluster) enable(ctx context.Context) error {

log.TrInfof("info.cluster.enable.sending")
resCh, err := cr.socket.EmitWithAck("enable", EnableData{
Host: cr.gcfg.Host,
Port: cr.gcfg.Port,
Host: cr.gcfg.PublicHost,
Port: cr.gcfg.PublicPort,
Version: build.ClusterVersion,
Byoc: cr.gcfg.Byoc,
NoFastEnable: cr.gcfg.NoFastEnable,
Expand Down
64 changes: 0 additions & 64 deletions cluster/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,12 +29,8 @@ import (
"fmt"
"net/http"
"net/url"
"strconv"
"time"

"github.com/hamba/avro/v2"
"github.com/klauspost/compress/zstd"

"github.com/LiterMC/go-openbmclapi/log"
"github.com/LiterMC/go-openbmclapi/utils"
)
Expand Down Expand Up @@ -263,63 +259,3 @@ func (cr *Cluster) RequestCert(ctx context.Context) (ckp *CertKeyPair, err error
}
return
}

type FileInfo struct {
Path string `json:"path" avro:"path"`
Hash string `json:"hash" avro:"hash"`
Size int64 `json:"size" avro:"size"`
Mtime int64 `json:"mtime" avro:"mtime"`
}

// from <https://github.com/bangbang93/openbmclapi/blob/master/src/constants.ts>
var fileListSchema = avro.MustParse(`{
"type": "array",
"items": {
"type": "record",
"name": "fileinfo",
"fields": [
{"name": "path", "type": "string"},
{"name": "hash", "type": "string"},
{"name": "size", "type": "long"},
{"name": "mtime", "type": "long"}
]
}
}`)

func (cr *Cluster) GetFileList(ctx context.Context, lastMod int64) (files []FileInfo, err error) {
var query url.Values
if lastMod > 0 {
query = url.Values{
"lastModified": {strconv.FormatInt(lastMod, 10)},
}
}
req, err := cr.makeReqWithAuth(ctx, http.MethodGet, "/openbmclapi/files", query)
if err != nil {
return
}
res, err := cr.cachedCli.Do(req)
if err != nil {
return
}
defer res.Body.Close()
switch res.StatusCode {
case http.StatusOK:
//
case http.StatusNoContent, http.StatusNotModified:
return
default:
err = utils.NewHTTPStatusErrorFromResponse(res)
return
}
log.Debug("Parsing filelist body ...")
zr, err := zstd.NewReader(res.Body)
if err != nil {
return
}
defer zr.Close()
if err = avro.NewDecoderForSchema(fileListSchema, zr).Decode(&files); err != nil {
return
}
log.Debugf("Filelist parsed, length = %d", len(files))
return
}
4 changes: 2 additions & 2 deletions cluster/handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -113,7 +113,7 @@ func (cr *Cluster) HandleFile(req *http.Request, rw http.ResponseWriter, hash st
return true
})
if sto != nil {
api.SetAccessInfo(req, "storage", sto.Options().Id)
api.SetAccessInfo(req, "storage", sto.Id())
}
if ok {
return
Expand All @@ -135,7 +135,7 @@ func (cr *Cluster) HandleMeasure(req *http.Request, rw http.ResponseWriter, size

api.SetAccessInfo(req, "cluster", cr.ID())
storage := cr.storageManager.Storages[cr.storages[0]]
api.SetAccessInfo(req, "storage", storage.Options().Id)
api.SetAccessInfo(req, "storage", storage.Id())
if err := storage.ServeMeasure(rw, req, size); err != nil {
log.Errorf("Could not serve measure %d: %v", size, err)
api.SetAccessInfo(req, "error", err.Error())
Expand Down
Loading

0 comments on commit 4fe7a79

Please sign in to comment.