mirror of
https://github.com/milvus-io/milvus.git
synced 2026-10-12 02:19:10 +00:00
issue: #53051, #53052, #53152 ## Summary Align local storage around one path contract: a key is a complete absolute filesystem path, and the local loon/Arrow filesystem is rooted at `/`. Previously, some writers used `minio.rootPath` while others supplied complete local paths to an already-prefixed filesystem, placing files in different physical locations from those expected by readers and GC. Remote keys remain complete bucket-relative object keys; their filesystem-root convention is unchanged. ## Changes - Unify Go/C++ local filesystem roots and StorageV3 manifest-base construction across writers. Preserve the existing base/version pair when continuing a growing-source manifest. - Fix local JSON/TEXT stats reads, rejected-stats cleanup, and explore temporary paths. Exercise resolved V3 stats paths through the actual `LocalChunkManager.MultiRead` interface. - Resolve legacy local manifest bases in loaded metadata and locally referenced snapshots, without eager catalog or manifest-file writes. Preserve manifest CAS compatibility and pathless V3 summaries; copy/restore writes use the target storage root. - Require an absolute, nonempty `localStorage.path` at initialization, including on remote deployments where it is a cache directory. Freeze forbidden parameters against configuration-source refresh while retaining explicit runtime/test overrides, and derive mmap, pprof, and disk-capacity defaults from the same canonical startup value. - Make segment deletion prefixes segment-boundary aware, retain root/identity and snapshot checks, and add BM25 orphan scanning. These shared GC changes also apply to remote storage; this PR does not claim every remote code path is untouched. - Remove the local test-only root override and unused local-only property initialization chain; use the production storage-config initialization path. ## Automatic local upgrade Let `R = localStorage.path` and `M = minio.rootPath`. Before standalone components start, local deployments check the known legacy locations: | Existing data | Upgrade handling | | --- | --- | | Canonical files under `R/<layout>` | Leave in place. | | Double-root files under `R/<R-without-leading-slash>/<layout>` | Move into `R/<layout>` for `insert_log`, `text_log`, `json_stats`, `index_files`, and `index_v1`. | | Recognized late-2.6 unified packed indexes under the process CWD | Move into the corresponding directory under `R`, if visible to the new process. | | Legacy manifest base `M/insert_log/<c>/<p>/<s>`, with files under `R/M/insert_log/<c>/<p>/<s>` | **Do not move the files.** Resolve the loaded manifest base to that existing absolute location. New segment layouts use the canonical root; updates to an existing legacy manifest can retain its base. | Migration prefers directory/subtree rename and recursively merges overlapping directories. Cross-filesystem moves fall back to copy-then-remove. Existing same-name files cause a conflict rather than content comparison or overwrite, except a published copy recognized as this migration's own interrupted work. The copy path synchronizes the file and target directory chain before deleting the source. Remaining sources are rediscovered on retry; empty legacy directories are pruned. Once migrated, restarts probe the known old locations without rescanning canonical data. The initial pass walks legacy file metadata but does not hash file contents. There is no configured migration timeout. SIGTERM/SIGINT cancels the migration and prevents etcd or components from starting; directory walks and cross-filesystem copies observe cancellation between filesystem reads. A canceled partial copy is never published and is recopied from the intact source on the next start. No user migration script, offline command, reverse migration, journal, or backup is provided. Successfully moved sources are removed. ## Compatibility and limits - Preserve the absolute local root, data mounts, and legacy `minio.rootPath` when upgrading. Canonical 2.6 data stays in place; misplaced late-2.6 indexes and affected 3.0.0/3.0.1 layouts are handled as above. Relative roots from older versions are unsupported. - Automatic migration is standalone/local-only. If old CWD indexes disappeared with an old container, they cannot be discovered: preserve/make those files visible or rebuild the derived indexes. Cross-filesystem copy has no configured timeout and can extend the first startup, but a shutdown signal cancels it and the next start resumes from the remaining source. - Downgrade to the affected old layout is unsupported after migration. Symlink layouts require manual resolution. - Known legacy LOB/orphan GC gaps under `R/M/insert_log`, pending old external-refresh paths, and rare interrupted-copy publication residue remain outside scope. This is not a claim that every historical file is recovered or reclaimable. ## Validation At head `898dba4b41`, `ci-v2/build-ut-cov` passed Build, Code Check, Integration UT, Go UT, and C++ UT (9,145/9,145). Diff coverage was 100% for C++ and 81.2% for Go; total patch coverage was 82.8%. Focused local tests for canonical/frozen `localStorage.path` derivation and migration cancellation/deadline/copy recovery passed with `-tags dynamic,test -gcflags='all=-N -l' -count=1`, and `git diff --check` passed before publication. Full paramtable execution remains locally blocked by tests attempting to create `/var/lib/milvus/data`; roles/native-dependent Go tests remain build-blocked by local C++ header/library mismatches. A real 3.0.0/3.0.1 version-to-version upgrade was not executed locally. `ci-v2/e2e-default` and `ci-v2/go-sdk` are still pending for this head. Signed-off-by: xiaofanluan <[email protected]> Co-authored-by: xiaofanluan <[email protected]>
539 lines
19 KiB
Go
539 lines
19 KiB
Go
package packed
|
|
|
|
/*
|
|
#cgo pkg-config: milvus_core milvus-storage
|
|
#include <stdlib.h>
|
|
#include "milvus-storage/ffi_c.h"
|
|
#include "storage/loon_ffi/external_spec_c.h"
|
|
#include "arrow/c/abi.h"
|
|
#include "arrow/c/helpers.h"
|
|
*/
|
|
import "C"
|
|
|
|
import (
|
|
"encoding/json"
|
|
"fmt"
|
|
"strconv"
|
|
"unsafe"
|
|
|
|
"github.com/cockroachdb/errors"
|
|
|
|
"github.com/milvus-io/milvus/internal/storagev2"
|
|
_ "github.com/milvus-io/milvus/internal/util/cgo"
|
|
"github.com/milvus-io/milvus/pkg/v3/proto/indexpb"
|
|
"github.com/milvus-io/milvus/pkg/v3/util/merr"
|
|
"github.com/milvus-io/milvus/pkg/v3/util/paramtable"
|
|
)
|
|
|
|
// ErrLoonTransient marks any failure surfaced by the loon FFI layer. Some
|
|
// milvus-storage paths can still lose their structured error detail and fall
|
|
// back to a generic error code, so callers cannot reliably distinguish a
|
|
// transient failure from a permanent one. Treat all loon failures as retryable
|
|
// for now and rely on a bounded retry budget plus outer error handling to keep
|
|
// the worst case finite.
|
|
//
|
|
// TODO(storage v3): once every milvus-storage FFI path preserves explicit error
|
|
// codes end-to-end, narrow this sentinel to the retryable cases and let other
|
|
// errors propagate immediately as retry.Unrecoverable.
|
|
var ErrLoonTransient = errors.New("loon FFI transient error")
|
|
|
|
// ErrLoonPermanent marks a loon FFI failure whose err_code the producer
|
|
// itself reports as non-retryable (loon_ffi_is_retryable_errcode == 0):
|
|
// access denied, malformed input, corrupt data. Retrying cannot succeed;
|
|
// callers' retry guards must terminate on it.
|
|
var ErrLoonPermanent = errors.New("loon FFI permanent error")
|
|
|
|
// Property keys exported by milvus-storage/ffi_c.h.
|
|
var (
|
|
PropertyFSAddress = C.GoString(C.loon_properties_fs_address)
|
|
PropertyFSBucketName = C.GoString(C.loon_properties_fs_bucket_name)
|
|
PropertyFSAccessKeyID = C.GoString(C.loon_properties_fs_access_key_id)
|
|
PropertyFSAccessKeyValue = C.GoString(C.loon_properties_fs_access_key_value)
|
|
PropertyFSRootPath = C.GoString(C.loon_properties_fs_root_path)
|
|
PropertyFSStorageType = C.GoString(C.loon_properties_fs_storage_type)
|
|
PropertyFSCloudProvider = C.GoString(C.loon_properties_fs_cloud_provider)
|
|
PropertyFSIAMEndpoint = C.GoString(C.loon_properties_fs_iam_endpoint)
|
|
PropertyFSLogLevel = C.GoString(C.loon_properties_fs_log_level)
|
|
PropertyFSRegion = C.GoString(C.loon_properties_fs_region)
|
|
PropertyFSUseSSL = C.GoString(C.loon_properties_fs_use_ssl)
|
|
PropertyFSSSLCACert = C.GoString(C.loon_properties_fs_ssl_ca_cert)
|
|
PropertyFSUseIAM = C.GoString(C.loon_properties_fs_use_iam)
|
|
PropertyFSUseVirtualHost = C.GoString(C.loon_properties_fs_use_virtual_host)
|
|
PropertyFSRequestTimeoutMS = C.GoString(C.loon_properties_fs_request_timeout_ms)
|
|
PropertyFSGCPCredentialJSON = C.GoString(C.loon_properties_fs_gcp_credential_json)
|
|
PropertyFSUseCustomPartUpload = C.GoString(C.loon_properties_fs_use_custom_part_upload)
|
|
PropertyFSMaxConnections = C.GoString(C.loon_properties_fs_max_connections)
|
|
PropertyFSTLSMinVersion = C.GoString(C.loon_properties_fs_tls_min_version)
|
|
PropertyFSUseCRC32CChecksum = C.GoString(C.loon_properties_fs_use_crc32c_checksum)
|
|
|
|
PropertyWriterPolicy = C.GoString(C.loon_properties_writer_policy)
|
|
PropertyWriterFormat = C.GoString(C.loon_properties_writer_format)
|
|
PropertyWriterSchemaBasedPattern = C.GoString(C.loon_properties_writer_schema_base_patterns)
|
|
PropertyWriterSchemaBasedFormats = "writer.split.schema_based.formats"
|
|
|
|
// CMEK (Customer Managed Encryption Keys) writer properties
|
|
PropertyWriterEncEnable = C.GoString(C.loon_properties_writer_enc_enable) // Enable encryption for written data
|
|
PropertyWriterEncKey = C.GoString(C.loon_properties_writer_enc_key) // Encryption key for data encryption
|
|
PropertyWriterEncMeta = C.GoString(C.loon_properties_writer_enc_meta) // Encoded metadata containing zone ID, collection ID, and key version
|
|
PropertyWriterEncAlgo = C.GoString(C.loon_properties_writer_enc_algorithm) // Encryption algorithm (e.g., "AES_GCM_V1")
|
|
)
|
|
|
|
// ExtfsPrefixForCollection returns the per-collection extfs property prefix.
|
|
func ExtfsPrefixForCollection(collectionID int64) string {
|
|
return fmt.Sprintf("extfs.%d.", collectionID)
|
|
}
|
|
|
|
// MakePropertiesFromStorageConfig creates a Properties object from StorageConfig.
|
|
// All configuration fields are mapped to corresponding property key-value pairs,
|
|
// with the local filesystem root normalized separately from the key prefix.
|
|
func MakePropertiesFromStorageConfig(storageConfig *indexpb.StorageConfig, extraKVs map[string]string) (*C.LoonProperties, error) {
|
|
if storageConfig == nil {
|
|
return nil, merr.WrapErrStorageMsg("storageConfig is required")
|
|
}
|
|
|
|
// Prepare key-value pairs from StorageConfig
|
|
var keys []string
|
|
var values []string
|
|
|
|
// Add non-empty string fields
|
|
if storageConfig.GetAddress() != "" {
|
|
keys = append(keys, PropertyFSAddress)
|
|
values = append(values, storageConfig.GetAddress())
|
|
}
|
|
if storageConfig.GetBucketName() != "" {
|
|
keys = append(keys, PropertyFSBucketName)
|
|
values = append(values, storageConfig.GetBucketName())
|
|
}
|
|
if storageConfig.GetAccessKeyID() != "" {
|
|
keys = append(keys, PropertyFSAccessKeyID)
|
|
values = append(values, storageConfig.GetAccessKeyID())
|
|
}
|
|
if storageConfig.GetSecretAccessKey() != "" {
|
|
keys = append(keys, PropertyFSAccessKeyValue)
|
|
values = append(values, storageConfig.GetSecretAccessKey())
|
|
}
|
|
if fsRoot := storagev2.LoonFSRootPath(storageConfig); fsRoot != "" {
|
|
keys = append(keys, PropertyFSRootPath)
|
|
values = append(values, fsRoot)
|
|
}
|
|
if storageConfig.GetStorageType() != "" {
|
|
keys = append(keys, PropertyFSStorageType)
|
|
values = append(values, storageConfig.GetStorageType())
|
|
}
|
|
if storageConfig.GetCloudProvider() != "" {
|
|
keys = append(keys, PropertyFSCloudProvider)
|
|
values = append(values, storageConfig.GetCloudProvider())
|
|
}
|
|
if storageConfig.GetIAMEndpoint() != "" {
|
|
keys = append(keys, PropertyFSIAMEndpoint)
|
|
values = append(values, storageConfig.GetIAMEndpoint())
|
|
}
|
|
keys = append(keys, PropertyFSLogLevel)
|
|
values = append(values, "warn")
|
|
|
|
if storageConfig.GetRegion() != "" {
|
|
keys = append(keys, PropertyFSRegion)
|
|
values = append(values, storageConfig.GetRegion())
|
|
}
|
|
if storageConfig.GetSslCACert() != "" {
|
|
keys = append(keys, PropertyFSSSLCACert)
|
|
values = append(values, storageConfig.GetSslCACert())
|
|
}
|
|
if storageConfig.GetGcpCredentialJSON() != "" {
|
|
keys = append(keys, PropertyFSGCPCredentialJSON)
|
|
values = append(values, storageConfig.GetGcpCredentialJSON())
|
|
}
|
|
|
|
// Add boolean fields
|
|
keys = append(keys, PropertyFSUseSSL)
|
|
if storageConfig.GetUseSSL() {
|
|
values = append(values, "true")
|
|
} else {
|
|
values = append(values, "false")
|
|
}
|
|
|
|
keys = append(keys, PropertyFSUseIAM)
|
|
if storageConfig.GetUseIAM() {
|
|
values = append(values, "true")
|
|
} else {
|
|
values = append(values, "false")
|
|
}
|
|
|
|
keys = append(keys, PropertyFSUseVirtualHost)
|
|
if storageConfig.GetUseVirtualHost() {
|
|
values = append(values, "true")
|
|
} else {
|
|
values = append(values, "false")
|
|
}
|
|
|
|
keys = append(keys, PropertyFSUseCustomPartUpload)
|
|
values = append(values, "true") // hardcoded to true as in the original code
|
|
|
|
// Add integer fields
|
|
keys = append(keys, PropertyFSRequestTimeoutMS)
|
|
values = append(values, strconv.FormatInt(storageConfig.GetRequestTimeoutMs(), 10))
|
|
// 0 means "not set by the producer" — leave the key absent so
|
|
// milvus-storage applies its registered default (100). Emitting "0"
|
|
// instead would clobber that default: the registry only falls back when
|
|
// the key is missing, and s3_client_builder takes
|
|
// max(max(io_capacity, 25), max_connections), so an explicit 0 lowers the
|
|
// connection cap. It would also change ArrowFileSystemConfig's cache key
|
|
// and split the filesystem cache against producers that do set it. Same
|
|
// convention as ChunkManager.cpp / MinioChunkManager.cpp, which apply the
|
|
// value only when > 0.
|
|
if maxConns := storageConfig.GetMaxConnections(); maxConns > 0 {
|
|
keys = append(keys, PropertyFSMaxConnections)
|
|
values = append(values, strconv.FormatUint(uint64(maxConns), 10))
|
|
}
|
|
|
|
// Add TLS min version (skip "default" — consistent with C++ layer filtering)
|
|
if v := storageConfig.GetSslTlsMinVersion(); v != "" && v != "default" {
|
|
keys = append(keys, PropertyFSTLSMinVersion)
|
|
values = append(values, v)
|
|
}
|
|
|
|
// Add CRC32C checksum
|
|
keys = append(keys, PropertyFSUseCRC32CChecksum)
|
|
if storageConfig.GetUseCrc32CChecksum() {
|
|
values = append(values, "true")
|
|
} else {
|
|
values = append(values, "false")
|
|
}
|
|
|
|
keys = append(keys, PropertyWriterFormat)
|
|
values = append(values, paramtable.Get().DataNodeCfg.StorageFormat.GetValue())
|
|
|
|
// No extfs.default.* properties here. Per-collection extfs properties
|
|
// (extfs.{collectionID}.*) are injected downstream via
|
|
// InjectExternalSpecProperties (C++ InjectExternalSpecProperties pipeline).
|
|
|
|
// Add extra kvs (override existing keys if present)
|
|
for k, v := range extraKVs {
|
|
found := false
|
|
for i, existingKey := range keys {
|
|
if existingKey == k {
|
|
values[i] = v
|
|
found = true
|
|
break
|
|
}
|
|
}
|
|
if !found {
|
|
keys = append(keys, k)
|
|
values = append(values, v)
|
|
}
|
|
}
|
|
|
|
// Convert to C arrays
|
|
cKeys := make([]*C.char, len(keys))
|
|
cValues := make([]*C.char, len(values))
|
|
for i := range keys {
|
|
cKeys[i] = C.CString(keys[i])
|
|
cValues[i] = C.CString(values[i])
|
|
}
|
|
// Defer cleanup of all C strings
|
|
defer func() {
|
|
for i := range cKeys {
|
|
C.free(unsafe.Pointer(cKeys[i]))
|
|
C.free(unsafe.Pointer(cValues[i]))
|
|
}
|
|
}()
|
|
|
|
// Create Properties using FFI
|
|
properties := &C.LoonProperties{}
|
|
var cKeysPtr **C.char
|
|
var cValuesPtr **C.char
|
|
if len(cKeys) > 0 {
|
|
cKeysPtr = &cKeys[0]
|
|
cValuesPtr = &cValues[0]
|
|
}
|
|
|
|
result := C.loon_properties_create(
|
|
(**C.char)(unsafe.Pointer(cKeysPtr)),
|
|
(**C.char)(unsafe.Pointer(cValuesPtr)),
|
|
C.size_t(len(keys)),
|
|
properties,
|
|
)
|
|
|
|
err := HandleLoonFFIResult(result)
|
|
if err != nil {
|
|
return nil, merr.WrapErrStorage(err, "loon properties_create failed")
|
|
}
|
|
return properties, nil
|
|
}
|
|
|
|
// FreeProperties releases a C-allocated LoonProperties object.
|
|
func FreeProperties(props *C.LoonProperties) {
|
|
if props != nil {
|
|
C.loon_properties_free(props)
|
|
}
|
|
}
|
|
|
|
// MilvusTablePrimaryKeyMode describes whether a milvus-table target segment
|
|
// keeps source primary keys or uses target-generated virtual primary keys.
|
|
type MilvusTablePrimaryKeyMode int
|
|
|
|
const (
|
|
// MilvusTablePrimaryKeyModeUnspecified keeps the legacy real-PK behavior for
|
|
// callers that do not know the collection schema.
|
|
MilvusTablePrimaryKeyModeUnspecified MilvusTablePrimaryKeyMode = iota
|
|
// MilvusTablePrimaryKeyModeExternal means source primary keys are preserved.
|
|
MilvusTablePrimaryKeyModeExternal
|
|
// MilvusTablePrimaryKeyModeVirtual means DataNode generates virtual PKs.
|
|
MilvusTablePrimaryKeyModeVirtual
|
|
)
|
|
|
|
func (m MilvusTablePrimaryKeyMode) usesExternalPrimaryKey() bool {
|
|
return m != MilvusTablePrimaryKeyModeVirtual
|
|
}
|
|
|
|
// ExternalSpecContext carries the raw external-table inputs that C++
|
|
// InjectExternalSpecProperties needs to derive both extfs.{collectionID}.*
|
|
// (storage layer) and format-layer properties (e.g. reader.exttable.snapshot_id)
|
|
// from a single external_spec JSON. Zero value (CollectionID=0, Source="")
|
|
// signals an internal (non-external) collection — injectExternalSpecProperties
|
|
// treats it as a no-op.
|
|
type ExternalSpecContext struct {
|
|
CollectionID int64
|
|
Source string
|
|
Spec string // raw JSON; C++ InjectExternalSpecProperties parses
|
|
|
|
// MilvusTablePKMode is only used by the milvus-table format. The zero
|
|
// value keeps the legacy real-PK behavior for direct storage helpers; callers
|
|
// with a collection schema should set this explicitly.
|
|
MilvusTablePKMode MilvusTablePrimaryKeyMode
|
|
}
|
|
|
|
// injectExternalSpecProperties appends External Table filesystem and
|
|
// format-layer properties onto an existing LoonProperties via the C++
|
|
// InjectExternalSpecProperties pipeline. The process-local IOPS policy is
|
|
// applied only to the extfs.<collectionID> namespace. No-op when
|
|
// externalSource is empty.
|
|
func injectExternalSpecProperties(properties *C.LoonProperties, collectionID int64,
|
|
externalSource, externalSpec string,
|
|
) error {
|
|
if properties == nil {
|
|
return merr.WrapErrStorageMsg("injectExternalSpecProperties: properties is nil")
|
|
}
|
|
if externalSource == "" {
|
|
return nil
|
|
}
|
|
params := paramtable.Get()
|
|
cSource := C.CString(externalSource)
|
|
defer C.free(unsafe.Pointer(cSource))
|
|
var cSpec *C.char
|
|
if externalSpec != "" {
|
|
cSpec = C.CString(externalSpec)
|
|
defer C.free(unsafe.Pointer(cSpec))
|
|
}
|
|
result := C.loon_properties_inject_external_spec(
|
|
properties,
|
|
C.int64_t(collectionID),
|
|
cSource,
|
|
cSpec,
|
|
C.uint32_t(params.CommonCfg.StorageIopsInitialRate.GetAsUint32()),
|
|
C.uint32_t(params.CommonCfg.StorageIopsMaxRate.GetAsUint32()),
|
|
)
|
|
if err := HandleLoonFFIResult(result); err != nil {
|
|
return merr.WrapErrStorage(err, "loon inject_external_spec failed")
|
|
}
|
|
return nil
|
|
}
|
|
|
|
func HandleLoonFFIResult(ffiResult C.LoonFFIResult) error {
|
|
defer C.loon_ffi_free_result(&ffiResult)
|
|
if C.loon_ffi_is_success(&ffiResult) == 0 {
|
|
errMsg := C.loon_ffi_get_errmsg(&ffiResult)
|
|
errStr := "Unknown error"
|
|
if errMsg != nil {
|
|
errStr = C.GoString(errMsg)
|
|
}
|
|
// Classify by the err_code the FFI already carries instead of
|
|
// flattening every failure to transient: the producer's own
|
|
// loon_ffi_is_retryable_errcode decides, so a 404/access-denied/
|
|
// corrupt-data failure stops retry loops instead of spinning them.
|
|
code := int32(ffiResult.err_code)
|
|
if C.loon_ffi_is_retryable_errcode(C.int(code)) != 0 {
|
|
return merr.Wrapf(ErrLoonTransient,
|
|
"FFI operation failed (code=%d): %s", code, errStr)
|
|
}
|
|
return merr.Wrapf(ErrLoonPermanent,
|
|
"FFI operation failed (code=%d): %s", code, errStr)
|
|
}
|
|
return nil
|
|
}
|
|
|
|
type ManifestJSON struct {
|
|
ManifestVersion int64 `json:"ver"`
|
|
BasePath string `json:"base_path"`
|
|
}
|
|
|
|
func MarshalManifestPath(basePath string, version int64) string {
|
|
bs, err := json.Marshal(ManifestJSON{
|
|
ManifestVersion: version,
|
|
BasePath: basePath,
|
|
})
|
|
if err != nil {
|
|
// json.Marshal on string+int64 struct should never fail, but log if it does
|
|
return fmt.Sprintf(`{"ver":%d,"base_path":"%s"}`, version, basePath)
|
|
}
|
|
return string(bs)
|
|
}
|
|
|
|
func UnmarshalManifestPath(manifestPath string) (string, int64, error) {
|
|
var manifestJSON ManifestJSON
|
|
err := json.Unmarshal([]byte(manifestPath), &manifestJSON)
|
|
if err != nil {
|
|
return "", 0, err
|
|
}
|
|
return manifestJSON.BasePath, manifestJSON.ManifestVersion, nil
|
|
}
|
|
|
|
// CompareManifestPath compares two manifest paths by their version.
|
|
func CompareManifestPath(a, b string) (int, error) {
|
|
if a == b {
|
|
return 0, nil
|
|
}
|
|
|
|
aBase, aVer, aErr := UnmarshalManifestPath(a)
|
|
bBase, bVer, bErr := UnmarshalManifestPath(b)
|
|
|
|
if aErr != nil {
|
|
return 0, merr.WrapErrStorage(aErr, "failed to parse manifest path %q", a)
|
|
}
|
|
if bErr != nil {
|
|
return 0, merr.WrapErrStorage(bErr, "failed to parse manifest path %q", b)
|
|
}
|
|
|
|
if aBase != bBase {
|
|
return 0, merr.WrapErrServiceInternalMsg("manifest paths have different base paths: %q vs %q", aBase, bBase)
|
|
}
|
|
|
|
switch {
|
|
case aVer < bVer:
|
|
return -1, nil
|
|
case aVer > bVer:
|
|
return 1, nil
|
|
default:
|
|
return 0, nil
|
|
}
|
|
}
|
|
|
|
// LobFileInfo represents metadata for a LOB (Large Object) file.
|
|
// used for TEXT column compaction strategy decision (hole ratio calculation)
|
|
type LobFileInfo struct {
|
|
Path string // relative path to the LOB file
|
|
FieldID int64 // field ID this LOB file belongs to
|
|
TotalRows int64 // total number of rows in the LOB file
|
|
ValidRows int64 // number of valid (non-deleted) rows
|
|
FileSizeBytes int64 // size of the LOB file in bytes
|
|
}
|
|
|
|
// AddLobFilesToTransaction adds multiple LOB files to a transaction in a single commit.
|
|
// this is used during compaction REUSE_ALL mode to merge LOB file references.
|
|
// returns the new committed version after the transaction.
|
|
func AddLobFilesToTransaction(basePath string, version int64, storageConfig *indexpb.StorageConfig, lobFiles []LobFileInfo) (int64, error) {
|
|
if len(lobFiles) == 0 {
|
|
return version, nil
|
|
}
|
|
|
|
cProperties, err := MakePropertiesFromStorageConfig(storageConfig, nil)
|
|
if err != nil {
|
|
return 0, merr.Wrap(err, "failed to make properties")
|
|
}
|
|
defer C.loon_properties_free(cProperties)
|
|
|
|
cBasePath := C.CString(basePath)
|
|
defer C.free(unsafe.Pointer(cBasePath))
|
|
|
|
// open transaction
|
|
var cTransactionHandle C.LoonTransactionHandle
|
|
result := C.loon_transaction_begin(cBasePath, cProperties, C.int64_t(version), C.int32_t(0) /* resolve_id */, C.uint32_t(1) /* retry_limit */, &cTransactionHandle)
|
|
if err := HandleLoonFFIResult(result); err != nil {
|
|
return 0, merr.WrapErrStorage(err, "failed to begin transaction")
|
|
}
|
|
defer C.loon_transaction_destroy(cTransactionHandle)
|
|
|
|
// add all LOB files
|
|
for _, lobFile := range lobFiles {
|
|
cPath := C.CString(lobFile.Path)
|
|
|
|
cLobFile := C.LoonLobFileInfo{
|
|
path: cPath,
|
|
field_id: C.int64_t(lobFile.FieldID),
|
|
total_rows: C.int64_t(lobFile.TotalRows),
|
|
valid_rows: C.int64_t(lobFile.ValidRows),
|
|
file_size_bytes: C.int64_t(lobFile.FileSizeBytes),
|
|
}
|
|
|
|
result = C.loon_transaction_add_lob_file(cTransactionHandle, &cLobFile)
|
|
C.free(unsafe.Pointer(cPath))
|
|
|
|
if err := HandleLoonFFIResult(result); err != nil {
|
|
return 0, merr.WrapErrStorage(err, "failed to add LOB file %s", lobFile.Path)
|
|
}
|
|
}
|
|
|
|
// commit transaction
|
|
var committedVersion C.int64_t
|
|
result = C.loon_transaction_commit(cTransactionHandle, &committedVersion)
|
|
if err := HandleLoonFFIResult(result); err != nil {
|
|
return 0, merr.WrapErrStorage(err, "failed to commit transaction")
|
|
}
|
|
|
|
return int64(committedVersion), nil
|
|
}
|
|
|
|
// GetManifestLobFiles retrieves LOB file information from a manifest.
|
|
// this is used by compaction to calculate hole ratios for TEXT columns.
|
|
func GetManifestLobFiles(manifestPath string, storageConfig *indexpb.StorageConfig) ([]LobFileInfo, error) {
|
|
basePath, version, err := UnmarshalManifestPath(manifestPath)
|
|
if err != nil {
|
|
return nil, merr.WrapErrStorage(err, "failed to unmarshal manifest path")
|
|
}
|
|
|
|
cProperties, err := MakePropertiesFromStorageConfig(storageConfig, nil)
|
|
if err != nil {
|
|
return nil, merr.Wrap(err, "failed to make properties")
|
|
}
|
|
defer C.loon_properties_free(cProperties)
|
|
|
|
cBasePath := C.CString(basePath)
|
|
defer C.free(unsafe.Pointer(cBasePath))
|
|
|
|
// open transaction to get manifest
|
|
var cTransactionHandle C.LoonTransactionHandle
|
|
result := C.loon_transaction_begin(cBasePath, cProperties, C.int64_t(version), C.int32_t(0) /* resolve_id */, C.uint32_t(1) /* retry_limit */, &cTransactionHandle)
|
|
if err := HandleLoonFFIResult(result); err != nil {
|
|
return nil, merr.WrapErrStorage(err, "failed to begin transaction")
|
|
}
|
|
defer C.loon_transaction_destroy(cTransactionHandle)
|
|
|
|
// get manifest
|
|
var cManifest *C.LoonManifest
|
|
result = C.loon_transaction_get_manifest(cTransactionHandle, &cManifest)
|
|
if err := HandleLoonFFIResult(result); err != nil {
|
|
return nil, merr.WrapErrStorage(err, "failed to get manifest")
|
|
}
|
|
defer C.loon_manifest_destroy(cManifest)
|
|
|
|
// extract LOB files from manifest
|
|
numFiles := int(cManifest.lob_files.num_files)
|
|
lobFiles := make([]LobFileInfo, 0, numFiles)
|
|
|
|
if numFiles > 0 && cManifest.lob_files.files != nil {
|
|
// convert C array to Go slice
|
|
cFiles := unsafe.Slice(cManifest.lob_files.files, numFiles)
|
|
for _, cFile := range cFiles {
|
|
lobFiles = append(lobFiles, LobFileInfo{
|
|
Path: C.GoString(cFile.path),
|
|
FieldID: int64(cFile.field_id),
|
|
TotalRows: int64(cFile.total_rows),
|
|
ValidRows: int64(cFile.valid_rows),
|
|
FileSizeBytes: int64(cFile.file_size_bytes),
|
|
})
|
|
}
|
|
}
|
|
|
|
return lobFiles, nil
|
|
}
|