2023-06-20 05:55:12 +00:00
|
|
|
package logstorage
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"net/http"
|
|
|
|
"strconv"
|
2023-07-20 08:10:55 +00:00
|
|
|
"strings"
|
2023-06-20 05:55:12 +00:00
|
|
|
|
|
|
|
"github.com/VictoriaMetrics/VictoriaMetrics/lib/encoding"
|
|
|
|
)
|
|
|
|
|
|
|
|
// TenantID is an id of a tenant for log streams.
|
|
|
|
//
|
|
|
|
// Each log stream is associated with a single TenantID.
|
|
|
|
type TenantID struct {
|
|
|
|
// AccountID is the id of the account for the log stream.
|
|
|
|
AccountID uint32
|
|
|
|
|
|
|
|
// ProjectID is the id of the project for the log stream.
|
|
|
|
ProjectID uint32
|
|
|
|
}
|
|
|
|
|
|
|
|
// Reset resets tid.
|
|
|
|
func (tid *TenantID) Reset() {
|
|
|
|
tid.AccountID = 0
|
|
|
|
tid.ProjectID = 0
|
|
|
|
}
|
|
|
|
|
|
|
|
// String returns human-readable representation of tid
|
|
|
|
func (tid *TenantID) String() string {
|
|
|
|
return fmt.Sprintf("{accountID=%d,projectID=%d}", tid.AccountID, tid.ProjectID)
|
|
|
|
}
|
|
|
|
|
|
|
|
// equal returns true if tid equals to a.
|
|
|
|
func (tid *TenantID) equal(a *TenantID) bool {
|
|
|
|
return tid.AccountID == a.AccountID && tid.ProjectID == a.ProjectID
|
|
|
|
}
|
|
|
|
|
|
|
|
// less returns true if tid is less than a.
|
|
|
|
func (tid *TenantID) less(a *TenantID) bool {
|
|
|
|
if tid.AccountID != a.AccountID {
|
|
|
|
return tid.AccountID < a.AccountID
|
|
|
|
}
|
|
|
|
return tid.ProjectID < a.ProjectID
|
|
|
|
}
|
|
|
|
|
2024-09-24 16:33:23 +00:00
|
|
|
func (tid *TenantID) marshalString(dst []byte) []byte {
|
|
|
|
n := uint64(tid.AccountID)<<32 | uint64(tid.ProjectID)
|
|
|
|
dst = marshalUint64Hex(dst, n)
|
|
|
|
return dst
|
|
|
|
}
|
|
|
|
|
2023-06-20 05:55:12 +00:00
|
|
|
// marshal appends the marshaled tid to dst and returns the result
|
|
|
|
func (tid *TenantID) marshal(dst []byte) []byte {
|
|
|
|
dst = encoding.MarshalUint32(dst, tid.AccountID)
|
|
|
|
dst = encoding.MarshalUint32(dst, tid.ProjectID)
|
|
|
|
return dst
|
|
|
|
}
|
|
|
|
|
|
|
|
// unmarshal unmarshals tid from src and returns the remaining tail.
|
|
|
|
func (tid *TenantID) unmarshal(src []byte) ([]byte, error) {
|
|
|
|
if len(src) < 8 {
|
|
|
|
return src, fmt.Errorf("cannot unmarshal tenantID from %d bytes; need at least 8 bytes", len(src))
|
|
|
|
}
|
|
|
|
tid.AccountID = encoding.UnmarshalUint32(src[:4])
|
|
|
|
tid.ProjectID = encoding.UnmarshalUint32(src[4:])
|
|
|
|
return src[8:], nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// GetTenantIDFromRequest returns tenantID from r.
|
|
|
|
func GetTenantIDFromRequest(r *http.Request) (TenantID, error) {
|
|
|
|
var tenantID TenantID
|
|
|
|
|
|
|
|
accountID, err := getUint32FromHeader(r, "AccountID")
|
|
|
|
if err != nil {
|
|
|
|
return tenantID, err
|
|
|
|
}
|
|
|
|
projectID, err := getUint32FromHeader(r, "ProjectID")
|
|
|
|
if err != nil {
|
|
|
|
return tenantID, err
|
|
|
|
}
|
|
|
|
|
|
|
|
tenantID.AccountID = accountID
|
|
|
|
tenantID.ProjectID = projectID
|
|
|
|
return tenantID, nil
|
|
|
|
}
|
|
|
|
|
2024-06-17 21:16:34 +00:00
|
|
|
// ParseTenantID returns tenantID from s.
|
|
|
|
//
|
|
|
|
// s is expected in the form of accountID:projectID. If s is empty, then zero tenantID is returned.
|
|
|
|
func ParseTenantID(s string) (TenantID, error) {
|
2023-07-20 08:10:55 +00:00
|
|
|
var tenantID TenantID
|
2024-06-17 21:16:34 +00:00
|
|
|
if s == "" {
|
|
|
|
return tenantID, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
n := strings.Index(s, ":")
|
|
|
|
if n < 0 {
|
2023-07-20 08:10:55 +00:00
|
|
|
account, err := getUint32FromString(s)
|
|
|
|
if err != nil {
|
2023-07-20 23:21:47 +00:00
|
|
|
return tenantID, fmt.Errorf("cannot parse accountID from %q: %w", s, err)
|
2023-07-20 08:10:55 +00:00
|
|
|
}
|
|
|
|
tenantID.AccountID = account
|
|
|
|
|
|
|
|
return tenantID, nil
|
|
|
|
}
|
|
|
|
|
2024-06-17 21:16:34 +00:00
|
|
|
account, err := getUint32FromString(s[:n])
|
2023-07-20 08:10:55 +00:00
|
|
|
if err != nil {
|
2023-07-20 23:21:47 +00:00
|
|
|
return tenantID, fmt.Errorf("cannot parse accountID part from %q: %w", s, err)
|
2023-07-20 08:10:55 +00:00
|
|
|
}
|
|
|
|
tenantID.AccountID = account
|
|
|
|
|
2024-06-17 21:16:34 +00:00
|
|
|
project, err := getUint32FromString(s[n+1:])
|
2023-07-20 08:10:55 +00:00
|
|
|
if err != nil {
|
2023-07-20 23:21:47 +00:00
|
|
|
return tenantID, fmt.Errorf("cannot parse projectID part from %q: %w", s, err)
|
2023-07-20 08:10:55 +00:00
|
|
|
}
|
|
|
|
tenantID.ProjectID = project
|
|
|
|
|
|
|
|
return tenantID, nil
|
|
|
|
}
|
|
|
|
|
2023-06-20 05:55:12 +00:00
|
|
|
func getUint32FromHeader(r *http.Request, headerName string) (uint32, error) {
|
|
|
|
s := r.Header.Get(headerName)
|
2023-07-20 08:10:55 +00:00
|
|
|
if len(s) == 0 {
|
|
|
|
return 0, nil
|
|
|
|
}
|
|
|
|
return getUint32FromString(s)
|
|
|
|
}
|
|
|
|
|
|
|
|
func getUint32FromString(s string) (uint32, error) {
|
2023-06-20 05:55:12 +00:00
|
|
|
if len(s) == 0 {
|
|
|
|
return 0, nil
|
|
|
|
}
|
|
|
|
n, err := strconv.ParseUint(s, 10, 32)
|
|
|
|
if err != nil {
|
2023-07-20 08:10:55 +00:00
|
|
|
return 0, fmt.Errorf("cannot parse %q as uint32: %w", s, err)
|
2023-06-20 05:55:12 +00:00
|
|
|
}
|
|
|
|
return uint32(n), nil
|
|
|
|
}
|