2022-12-13 14:36:35 +00:00
|
|
|
# frostfs-sdk-go
|
2022-12-29 10:46:18 +00:00
|
|
|
Go implementation of FrostFS SDK. It contains high-level version-independent wrappers
|
2023-03-07 11:20:03 +00:00
|
|
|
for structures from [frostfs-api-go](https://git.frostfs.info/TrueCloudLab/frostfs-api-go) as well as
|
2022-01-13 12:31:51 +00:00
|
|
|
helper functions for simplifying node/dApp implementations.
|
|
|
|
|
|
|
|
## Repository structure
|
|
|
|
|
|
|
|
### accounting
|
|
|
|
Contains fixed-point `Decimal` type for performing balance calculations.
|
|
|
|
|
|
|
|
### eacl
|
|
|
|
Contains Extended ACL types for fine-grained access control.
|
2022-12-29 10:46:18 +00:00
|
|
|
There is also a reference implementation of checking algorithm which is used in FrostFS node.
|
2022-01-13 12:31:51 +00:00
|
|
|
|
|
|
|
### checksum
|
|
|
|
Contains `Checksum` type encapsulating checksum as well as it's kind.
|
2023-03-07 11:20:03 +00:00
|
|
|
Currently Sha256 and [Tillich-Zemor hashsum](https://git.frostfs.info/TrueCloudLab/tzhash) are in use.
|
2022-01-13 12:31:51 +00:00
|
|
|
|
|
|
|
### owner
|
2022-12-29 10:46:18 +00:00
|
|
|
`owner.ID` type represents single account interacting with FrostFS. In v2 version of protocol
|
2022-01-13 12:31:51 +00:00
|
|
|
it is just raw bytes behing [base58-encoded address](https://docs.neo.org/docs/en-us/basic/concept/wallets.html#address)
|
|
|
|
in Neo blockchain. Note that for historical reasons it contains
|
|
|
|
version prefix and checksum in addition to script-hash.
|
|
|
|
|
|
|
|
### token
|
2022-12-29 10:46:18 +00:00
|
|
|
Contains Bearer token type with several FrostFS-specific methods.
|
2022-01-13 12:31:51 +00:00
|
|
|
|
2022-04-12 08:25:18 +00:00
|
|
|
### ns
|
2022-12-29 10:46:18 +00:00
|
|
|
In FrostFS there are 2 types of name resolution: DNS and NNS. NNS stands for Neo Name Service
|
2023-03-07 11:20:03 +00:00
|
|
|
is just a [contract](https://git.frostfs.info/TrueCloudLab/frostfs-contract) deployed on a Neo blockchain.
|
2022-01-13 12:31:51 +00:00
|
|
|
Basically, NNS is just a DNS-on-chain which can be used for resolving container nice-names as well
|
|
|
|
as any other name in dApps. See our [CoreDNS plugin](https://github.com/nspcc-dev/coredns/tree/master/plugin/nns)
|
|
|
|
for the example of how NNS can be integrated in DNS.
|
|
|
|
|
|
|
|
### session
|
2022-12-29 10:46:18 +00:00
|
|
|
To help lightweight clients interact with FrostFS without sacrificing trust, FrostFS has a concept
|
2022-01-13 12:31:51 +00:00
|
|
|
of session token. It is signed by client and allows any node with which a session is established
|
|
|
|
to perform certain actions on behalf of the user.
|
|
|
|
|
|
|
|
### client
|
2022-12-29 10:46:18 +00:00
|
|
|
Contains client for working with FrostFS.
|
2022-01-13 12:31:51 +00:00
|
|
|
```go
|
2022-07-04 15:33:33 +00:00
|
|
|
var prmInit client.PrmInit
|
|
|
|
prmInit.SetDefaultPrivateKey(key) // private key for request signing
|
2023-09-12 15:57:04 +00:00
|
|
|
prmInit.DontResolveFrostFSFailures() // disable erroneous status parsing
|
2022-07-04 15:33:33 +00:00
|
|
|
|
|
|
|
var c client.Client
|
|
|
|
c.Init(prmInit)
|
|
|
|
|
|
|
|
var prmDial client.PrmDial
|
|
|
|
prmDial.SetServerURI("grpcs://localhost:40005") // endpoint address
|
|
|
|
|
|
|
|
err := c.Dial(prmDial)
|
|
|
|
if err != nil {
|
|
|
|
return
|
|
|
|
}
|
2023-03-03 09:29:33 +00:00
|
|
|
|
2022-01-13 12:31:51 +00:00
|
|
|
ctx, cancel := context.WithTimeout(context.Background(), 5 * time.Second)
|
|
|
|
defer cancel()
|
|
|
|
|
2022-07-04 15:33:33 +00:00
|
|
|
var prm client.PrmBalanceGet
|
|
|
|
prm.SetAccount(acc)
|
|
|
|
|
|
|
|
res, err := c.BalanceGet(ctx, prm)
|
2022-01-13 12:31:51 +00:00
|
|
|
if err != nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2022-07-04 15:33:33 +00:00
|
|
|
fmt.Printf("Balance for %s: %v\n", acc, res.Amount())
|
2022-01-13 12:31:51 +00:00
|
|
|
```
|
|
|
|
|
|
|
|
#### Response status
|
2022-12-29 10:46:18 +00:00
|
|
|
In FrostFS every operation can fail on multiple levels, so a single `error` doesn't suffice,
|
2022-01-13 12:31:51 +00:00
|
|
|
e.g. consider a case when object was put on 4 out of 5 replicas. Thus, all request execution
|
|
|
|
details are contained in `Status` returned from every RPC call. dApp can inspect them
|
|
|
|
if needed and perform any desired action. In the case above we may want to report
|
|
|
|
these details to the user as well as retry an operation, possibly with different parameters.
|
|
|
|
Status wire-format is extendable and each node can report any set of details it wants.
|
|
|
|
The set of reserved status codes can be found in
|
2023-09-12 15:57:04 +00:00
|
|
|
[FrostFS API](https://git.frostfs.info/TrueCloudLab/frostfs-api/src/branch/master/status/types.proto).
|
|
|
|
Use `client.PrmInit.DontResolveFrostFSFailures()` to avoid returning erroneous statuses as Go built-in errors.
|
2022-01-13 12:31:51 +00:00
|
|
|
|
|
|
|
### policy
|
|
|
|
Contains helpers allowing conversion of placing policy from/to JSON representation
|
|
|
|
and SQL-like human-readable language.
|
|
|
|
```go
|
|
|
|
p, _ := policy.Parse(`
|
|
|
|
REP 2
|
|
|
|
SELECT 6 FROM F
|
|
|
|
FILTER StorageType EQ SSD AS F`)
|
|
|
|
|
|
|
|
// Convert parsed policy back to human-readable text and print.
|
|
|
|
println(strings.Join(policy.Encode(p), "\n"))
|
|
|
|
```
|
|
|
|
|
|
|
|
### netmap
|
|
|
|
Contains CRUSH-like implementation of container node selection algorithm. Relevant details
|
|
|
|
are described in this paper http://ceur-ws.org/Vol-2344/short10.pdf . Note that it can be
|
|
|
|
outdated in some details.
|
|
|
|
|
2023-03-03 09:29:33 +00:00
|
|
|
`netmap/json_tests` subfolder contains language-agnostic tests for selection algorithm.
|
2022-01-13 12:31:51 +00:00
|
|
|
|
|
|
|
```go
|
|
|
|
import (
|
2023-03-07 11:20:03 +00:00
|
|
|
"git.frostfs.info/TrueCloudLab/frostfs-sdk-go/netmap"
|
|
|
|
"git.frostfs.info/TrueCloudLab/frostfs-sdk-go/object"
|
2022-01-13 12:31:51 +00:00
|
|
|
)
|
|
|
|
|
2022-12-13 14:36:35 +00:00
|
|
|
func placementNodes(addr *object.Address, p *netmap.PlacementPolicy, frostfsNodes []netmap.NodeInfo) {
|
2022-12-29 10:46:18 +00:00
|
|
|
// Convert list of nodes in FrostFS API format to the intermediate representation.
|
2022-01-13 12:31:51 +00:00
|
|
|
nodes := netmap.NodesFromInfo(nodes)
|
|
|
|
|
2023-03-03 09:29:33 +00:00
|
|
|
// Create new netmap (errors are skipped for the sake of clarity).
|
2022-01-13 12:31:51 +00:00
|
|
|
nm, _ := NewNetmap(nodes)
|
|
|
|
|
|
|
|
// Calculate nodes of container.
|
|
|
|
cn, _ := nm.GetContainerNodes(p, addr.ContainerID().ToV2().GetValue())
|
|
|
|
|
|
|
|
// Return list of nodes for each replica to place object on in the order of priority.
|
|
|
|
return nm.GetPlacementVectors(cn, addr.ObjectID().ToV2().GetValue())
|
|
|
|
}
|
|
|
|
```
|
|
|
|
|
|
|
|
### pool
|
2022-12-29 10:46:18 +00:00
|
|
|
Simple pool for managing connections to FrostFS nodes.
|
2022-01-13 12:31:51 +00:00
|
|
|
|
|
|
|
### acl, checksum, version, signature
|
|
|
|
Contain simple API wrappers.
|
|
|
|
|
|
|
|
### logger
|
2022-12-29 10:46:18 +00:00
|
|
|
Wrapper over `zap.Logger` which is used across FrostFS codebase.
|
2022-01-13 12:31:51 +00:00
|
|
|
|
|
|
|
### util
|
2023-03-03 09:29:33 +00:00
|
|
|
Utilities for working with signature-related code.
|