mirror of
https://github.com/seaweedfs/seaweedfs.git
synced 2026-12-05 18:47:20 +01:00
Compare commits
29
Commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
1b3bd81eae | ||
|
|
ec8f979350 | ||
|
|
1a0485a43d | ||
|
|
8c1a0a0cc2 | ||
|
|
0c32eab48e | ||
|
|
85470416cb | ||
|
|
e442eda88e | ||
|
|
216c1032e6 | ||
|
|
03f773f087 | ||
|
|
a32d021f6f | ||
|
|
c871f3f6e6 | ||
|
|
36e6a2c5e1 | ||
|
|
f9dac64374 | ||
|
|
7b43dec665 | ||
|
|
efe80b1938 | ||
|
|
95d8a709e8 | ||
|
|
e432b75822 | ||
|
|
93aba4412e | ||
|
|
71cf9d4f21 | ||
|
|
347d02cf9e | ||
|
|
8311221fb4 | ||
|
|
d6b4290527 | ||
|
|
ff4b43e0f1 | ||
|
|
44235b5def | ||
|
|
8de67b6c38 | ||
|
|
eeeda21a67 | ||
|
|
c551bea9ce | ||
|
|
ff5433e63b | ||
|
|
30981f4665 |
No files matched your search
@@ -4,6 +4,7 @@ on:
|
||||
pull_request:
|
||||
paths:
|
||||
- 'weed/s3api/**'
|
||||
- 'weed/remote_storage/**'
|
||||
- 'weed/filer/**'
|
||||
- 'weed/server/**'
|
||||
- 'test/s3/**'
|
||||
|
||||
@@ -1,6 +1,6 @@
|
||||
apiVersion: v1
|
||||
description: SeaweedFS
|
||||
name: seaweedfs
|
||||
appVersion: "4.49"
|
||||
appVersion: "4.50"
|
||||
# Dev note: Trigger a helm chart release by `git tag -a helm-<version>`
|
||||
version: 4.49.0
|
||||
version: 4.50.0
|
||||
@@ -170,9 +170,6 @@ spec:
|
||||
{{- if .Values.filer.metricsIp }}
|
||||
-metricsIp={{ .Values.filer.metricsIp }} \
|
||||
{{- end }}
|
||||
{{- if .Values.filer.redirectOnRead }}
|
||||
-redirectOnRead \
|
||||
{{- end }}
|
||||
{{- if .Values.filer.disableHttp }}
|
||||
-disableHttp \
|
||||
{{- end }}
|
||||
|
||||
@@ -740,9 +740,6 @@ filer:
|
||||
# encrypt data on volume servers
|
||||
encryptVolumeData: false
|
||||
|
||||
# Whether proxy or redirect to volume server during file GET request
|
||||
redirectOnRead: false
|
||||
|
||||
# Limit sub dir listing size (default 100000)
|
||||
dirListLimit: 100000
|
||||
|
||||
|
||||
+1034
-1080
File diff suppressed because it is too large.
Load diff
|
Before Width: | Height: | Size: 55 KiB After Width: | Height: | Size: 53 KiB |
@@ -784,6 +784,7 @@ message FilerConf {
|
||||
optional bool worm = 14;
|
||||
uint64 worm_grace_period_seconds = 15;
|
||||
uint64 worm_retention_time_seconds = 16;
|
||||
bool inode_index = 17;
|
||||
}
|
||||
repeated PathConf locations = 2;
|
||||
}
|
||||
|
||||
@@ -3039,50 +3039,62 @@ pub async fn post_handler(
|
||||
let needle_etag = n.etag();
|
||||
let needle_has_name = n.has_name();
|
||||
|
||||
let write_result = write_upload_needle(&state, vid, n, fsync).await;
|
||||
|
||||
// Replicate to remote volume servers if this volume has replicas.
|
||||
// Matches Go's GetWritableRemoteReplications: skip if copy_count == 1.
|
||||
let needs_replication = !is_replicate && {
|
||||
let store = state.store.read().unwrap();
|
||||
store
|
||||
.find_volume(vid)
|
||||
.is_some_and(|(_, v)| v.super_block().replica_placement.get_copy_count() > 1)
|
||||
};
|
||||
|
||||
// The fan-out runs concurrently with the local needle write, like Go's
|
||||
// ReplicatedWrite: it reads the already-buffered request body, so the two
|
||||
// sides share no mutable state. The ack still waits for both.
|
||||
let replication = if needs_replication {
|
||||
let state_clone = state.clone();
|
||||
let path_clone = path.clone();
|
||||
let query_clone = query.clone();
|
||||
let headers_clone = headers.clone();
|
||||
let body_clone = body.clone();
|
||||
Some(tokio::spawn(async move {
|
||||
do_replicated_request(
|
||||
&state_clone,
|
||||
vid.0,
|
||||
Method::POST,
|
||||
&path_clone,
|
||||
&query_clone,
|
||||
&headers_clone,
|
||||
Some(body_clone),
|
||||
)
|
||||
.await
|
||||
}))
|
||||
} else {
|
||||
None
|
||||
};
|
||||
|
||||
let write_result = write_upload_needle(&state, vid, n, fsync).await;
|
||||
|
||||
// No leader-known gate: when the lookup cannot run, the write must fail
|
||||
// rather than be acknowledged on fewer copies than required.
|
||||
if !is_replicate && write_result.is_ok() {
|
||||
let needs_replication = {
|
||||
let store = state.store.read().unwrap();
|
||||
store
|
||||
.find_volume(vid)
|
||||
.is_some_and(|(_, v)| v.super_block().replica_placement.get_copy_count() > 1)
|
||||
};
|
||||
if needs_replication {
|
||||
let state_clone = state.clone();
|
||||
let path_clone = path.clone();
|
||||
let query_clone = query.clone();
|
||||
let headers_clone = headers.clone();
|
||||
let body_clone = body.clone();
|
||||
let replication = tokio::spawn(async move {
|
||||
do_replicated_request(
|
||||
&state_clone,
|
||||
vid.0,
|
||||
Method::POST,
|
||||
&path_clone,
|
||||
&query_clone,
|
||||
&headers_clone,
|
||||
Some(body_clone),
|
||||
)
|
||||
.await
|
||||
});
|
||||
let replication_result = replication
|
||||
.await
|
||||
// rather than be acknowledged on fewer copies than required. The local
|
||||
// write result takes precedence, matching Go's error ordering.
|
||||
let replication_result = match replication {
|
||||
Some(task) => Some(
|
||||
task.await
|
||||
.map_err(|e| format!("replication task failed: {}", e))
|
||||
.flatten();
|
||||
if let Err(e) = replication_result {
|
||||
tracing::error!("replicated write failed: {}", e);
|
||||
return json_error_with_query(
|
||||
StatusCode::INTERNAL_SERVER_ERROR,
|
||||
format!("replication failed: {}", e),
|
||||
Some(&query),
|
||||
);
|
||||
}
|
||||
}
|
||||
.flatten(),
|
||||
),
|
||||
None => None,
|
||||
};
|
||||
if write_result.is_ok()
|
||||
&& let Some(Err(e)) = replication_result
|
||||
{
|
||||
tracing::error!("replicated write failed: {}", e);
|
||||
return json_error_with_query(
|
||||
StatusCode::INTERNAL_SERVER_ERROR,
|
||||
format!("replication failed: {}", e),
|
||||
Some(&query),
|
||||
);
|
||||
}
|
||||
|
||||
let resp = match write_result {
|
||||
|
||||
@@ -466,6 +466,7 @@ func (env *TestEnvironment) startTrinoContainer(t *testing.T, configDir string)
|
||||
|
||||
cmd := exec.Command("docker", "run", "-d",
|
||||
"--name", containerName,
|
||||
"--memory", "4g",
|
||||
"--add-host", "host.docker.internal:host-gateway",
|
||||
"-v", fmt.Sprintf("%s:/etc/trino/catalog", configDir),
|
||||
"-v", fmt.Sprintf("%s:/test", env.dataDir),
|
||||
@@ -484,7 +485,7 @@ func waitForTrino(t *testing.T, containerName string, timeout time.Duration) {
|
||||
|
||||
deadline := time.Now().Add(timeout)
|
||||
var lastOutput []byte
|
||||
retryCount := 0
|
||||
containerDead := false
|
||||
for time.Now().Before(deadline) {
|
||||
// Try system catalog query as a readiness check
|
||||
cmd := exec.Command("docker", "exec", containerName,
|
||||
@@ -495,26 +496,51 @@ func waitForTrino(t *testing.T, containerName string, timeout time.Duration) {
|
||||
return
|
||||
} else {
|
||||
lastOutput = output
|
||||
outputStr := string(output)
|
||||
if strings.Contains(outputStr, "No such container") ||
|
||||
strings.Contains(outputStr, "is not running") {
|
||||
if trinoContainerExited(output) {
|
||||
containerDead = true
|
||||
break
|
||||
}
|
||||
retryCount++
|
||||
}
|
||||
time.Sleep(1 * time.Second)
|
||||
}
|
||||
|
||||
// If we can't connect to system catalog, try to at least connect to Trino server
|
||||
cmd := exec.Command("docker", "exec", containerName, "trino", "--version")
|
||||
if err := cmd.Run(); err == nil {
|
||||
if output, err := exec.Command("docker", "exec", containerName, "trino", "--version").CombinedOutput(); err == nil {
|
||||
// Trino process is running, even if catalog isn't ready yet
|
||||
// Give it a bit more time
|
||||
time.Sleep(5 * time.Second)
|
||||
return
|
||||
} else {
|
||||
lastOutput = output
|
||||
containerDead = containerDead || trinoContainerExited(output)
|
||||
}
|
||||
|
||||
t.Fatalf("Timed out waiting for Trino to be ready\nLast output:\n%s", string(lastOutput))
|
||||
if containerDead {
|
||||
t.Fatalf("Trino container %s exited before becoming ready\nLast output:\n%s\n%s", containerName, string(lastOutput), trinoContainerDiagnostics(containerName))
|
||||
}
|
||||
t.Fatalf("Timed out waiting for Trino to be ready\nLast output:\n%s\n%s", string(lastOutput), trinoContainerDiagnostics(containerName))
|
||||
}
|
||||
|
||||
func trinoContainerExited(output []byte) bool {
|
||||
s := string(output)
|
||||
return strings.Contains(s, "No such container") || strings.Contains(s, "is not running")
|
||||
}
|
||||
|
||||
func trinoContainerDiagnostics(containerName string) string {
|
||||
var b strings.Builder
|
||||
if out, err := exec.Command("docker", "inspect", "-f",
|
||||
"status={{.State.Status}} exitCode={{.State.ExitCode}} oomKilled={{.State.OOMKilled}} error={{.State.Error}}",
|
||||
containerName).CombinedOutput(); err == nil {
|
||||
fmt.Fprintf(&b, "docker inspect: %s\n", strings.TrimSpace(string(out)))
|
||||
} else {
|
||||
fmt.Fprintf(&b, "docker inspect failed: %v (%s)\n", err, strings.TrimSpace(string(out)))
|
||||
}
|
||||
if out, err := exec.Command("docker", "logs", "--tail", "80", containerName).CombinedOutput(); err == nil {
|
||||
fmt.Fprintf(&b, "docker logs:\n%s", string(out))
|
||||
} else {
|
||||
fmt.Fprintf(&b, "docker logs failed: %v (%s)", err, strings.TrimSpace(string(out)))
|
||||
}
|
||||
return b.String()
|
||||
}
|
||||
|
||||
func runTrinoSQL(t *testing.T, containerName, sql string) string {
|
||||
|
||||
@@ -170,11 +170,11 @@ func TestConditionalHeaderPrecedenceAndInvalidIfModifiedSince(t *testing.T) {
|
||||
precedenceReq.Header.Set("If-None-Match", "\"definitely-different-etag\"")
|
||||
precedenceResp := framework.DoRequest(t, client, precedenceReq)
|
||||
precedenceBody := framework.ReadAllAndClose(t, precedenceResp)
|
||||
if precedenceResp.StatusCode != http.StatusNotModified {
|
||||
t.Fatalf("conditional precedence expected 304, got %d", precedenceResp.StatusCode)
|
||||
if precedenceResp.StatusCode != http.StatusOK {
|
||||
t.Fatalf("mismatched If-None-Match overrides If-Modified-Since: expected 200, got %d", precedenceResp.StatusCode)
|
||||
}
|
||||
if len(precedenceBody) != 0 {
|
||||
t.Fatalf("conditional precedence expected empty body, got %d bytes", len(precedenceBody))
|
||||
if string(precedenceBody) != string(payload) {
|
||||
t.Fatalf("mismatched If-None-Match body mismatch: got %q want %q", string(precedenceBody), string(payload))
|
||||
}
|
||||
|
||||
invalidIMSReq := mustNewRequest(t, http.MethodGet, clusterHarness.VolumeAdminURL()+"/"+fid)
|
||||
|
||||
@@ -181,7 +181,7 @@ func (store *EtcdStore) DeleteFolderChildren(ctx context.Context, fullpath weed_
|
||||
func (store *EtcdStore) ListDirectoryPrefixedEntries(ctx context.Context, dirPath weed_util.FullPath, startFileName string, includeStartFile bool, limit int64, prefix string, eachEntryFunc filer.ListEachEntryFunc) (lastFileName string, err error) {
|
||||
directoryPrefix := genDirectoryKeyPrefix(dirPath, prefix)
|
||||
lastFileStart := directoryPrefix
|
||||
if startFileName != "" {
|
||||
if startFileName > prefix {
|
||||
lastFileStart = genDirectoryKeyPrefix(dirPath, startFileName)
|
||||
}
|
||||
|
||||
|
||||
+4
-1
@@ -364,7 +364,10 @@ func (f *Filer) ListExistingPeerUpdates(ctx context.Context) (existingNodes []*m
|
||||
}
|
||||
|
||||
func (f *Filer) SetStore(store FilerStore) (isFresh bool) {
|
||||
f.Store = NewFilerStoreWrapper(store)
|
||||
fsw := NewFilerStoreWrapper(store)
|
||||
// f.FilerConf is swapped on filer.conf reloads, so resolve it per call.
|
||||
fsw.filerConfFn = func() *FilerConf { return f.FilerConf }
|
||||
f.Store = fsw
|
||||
|
||||
isFresh = f.setOrLoadFilerStoreSignature(store)
|
||||
|
||||
|
||||
@@ -255,9 +255,51 @@ func ClonePathConf(src *filer_pb.FilerConf_PathConf) *filer_pb.FilerConf_PathCon
|
||||
Worm: worm,
|
||||
WormGracePeriodSeconds: src.WormGracePeriodSeconds,
|
||||
WormRetentionTimeSeconds: src.WormRetentionTimeSeconds,
|
||||
InodeIndex: src.InodeIndex,
|
||||
}
|
||||
}
|
||||
|
||||
// MatchInodeIndexRule reports whether an inode_index rule covers path. Unlike
|
||||
// MatchStorageRule it requires the rule's location to end at a path boundary,
|
||||
// so a rule on /exports does not index /exports2 or /exports_old.
|
||||
func (fc *FilerConf) MatchInodeIndexRule(path string) bool {
|
||||
active := false
|
||||
fc.rules.MatchPrefix([]byte(path), func(key []byte, value *filer_pb.FilerConf_PathConf) bool {
|
||||
if !value.InodeIndex {
|
||||
return true
|
||||
}
|
||||
rulePath := strings.TrimSuffix(string(key), "/")
|
||||
if path == rulePath || strings.HasPrefix(path, rulePath+"/") {
|
||||
active = true
|
||||
return false
|
||||
}
|
||||
return true
|
||||
})
|
||||
return active
|
||||
}
|
||||
|
||||
// InodeIndexActiveUnder reports whether entries inside dirPath may carry inode
|
||||
// index rows: either dirPath itself is covered by an inode_index rule, or such
|
||||
// a rule applies to a subtree inside dirPath.
|
||||
func (fc *FilerConf) InodeIndexActiveUnder(dirPath string) bool {
|
||||
if fc.MatchInodeIndexRule(dirPath) {
|
||||
return true
|
||||
}
|
||||
prefix := dirPath
|
||||
if !strings.HasSuffix(prefix, "/") {
|
||||
prefix += "/"
|
||||
}
|
||||
active := false
|
||||
fc.rules.Walk(func(key []byte, value *filer_pb.FilerConf_PathConf) bool {
|
||||
if value.InodeIndex && (string(key) == dirPath || strings.HasPrefix(string(key), prefix)) {
|
||||
active = true
|
||||
return false
|
||||
}
|
||||
return true
|
||||
})
|
||||
return active
|
||||
}
|
||||
|
||||
// ApplyBucketQuotaReadOnly sets read-only when usedSize exceeds quota and clears it
|
||||
// once back under, reporting whether the flag changed. A non-positive quota is left
|
||||
// untouched so a manually locked bucket is never reopened.
|
||||
@@ -535,6 +577,7 @@ func mergePathConf(a, b *filer_pb.FilerConf_PathConf) {
|
||||
// copy the value: a is often a scratch conf while b is a live trie entry
|
||||
a.Worm = proto.Bool(*b.Worm)
|
||||
}
|
||||
a.InodeIndex = b.InodeIndex || a.InodeIndex
|
||||
if b.WormRetentionTimeSeconds > 0 {
|
||||
a.WormRetentionTimeSeconds = b.WormRetentionTimeSeconds
|
||||
}
|
||||
|
||||
@@ -206,6 +206,7 @@ func TestClonePathConf(t *testing.T) {
|
||||
Worm: proto.Bool(true),
|
||||
WormGracePeriodSeconds: 3600,
|
||||
WormRetentionTimeSeconds: 86400,
|
||||
InodeIndex: true,
|
||||
}
|
||||
|
||||
clone := ClonePathConf(src)
|
||||
|
||||
@@ -0,0 +1,357 @@
|
||||
package filer
|
||||
|
||||
import (
|
||||
"context"
|
||||
"encoding/binary"
|
||||
"encoding/json"
|
||||
"sort"
|
||||
"sync"
|
||||
|
||||
"github.com/seaweedfs/seaweedfs/weed/glog"
|
||||
"github.com/seaweedfs/seaweedfs/weed/util"
|
||||
)
|
||||
|
||||
const inodeIndexKeyPrefix = "filer.inode.path."
|
||||
const InodeIndexInitialGeneration uint64 = 1
|
||||
|
||||
// inodeRowLocks serializes the read-modify-write of one inode's index row so
|
||||
// concurrent writers (hard links share an inode) cannot overwrite each other.
|
||||
var inodeRowLocks [256]sync.Mutex
|
||||
|
||||
func inodeRowLock(inode uint64) *sync.Mutex {
|
||||
return &inodeRowLocks[inode%uint64(len(inodeRowLocks))]
|
||||
}
|
||||
|
||||
type inodeIndexEntry struct {
|
||||
path util.FullPath
|
||||
inode uint64
|
||||
}
|
||||
|
||||
// InodeIndexRecord is the inode→path reverse-lookup row consumed by the NFS
|
||||
// gateway to resolve filehandles. Rows are maintained only for paths covered
|
||||
// by an inode_index rule in filer.conf (see shell nfs.enable); everywhere else
|
||||
// the filer writes nothing.
|
||||
type InodeIndexRecord struct {
|
||||
Generation uint64 `json:"generation,omitempty"`
|
||||
Paths []string `json:"paths,omitempty"`
|
||||
}
|
||||
|
||||
func InodeIndexKey(inode uint64) []byte {
|
||||
key := make([]byte, len(inodeIndexKeyPrefix)+8)
|
||||
copy(key, inodeIndexKeyPrefix)
|
||||
binary.BigEndian.PutUint64(key[len(inodeIndexKeyPrefix):], inode)
|
||||
return key
|
||||
}
|
||||
|
||||
func DecodeInodeIndexRecord(value []byte) (*InodeIndexRecord, error) {
|
||||
if len(value) == 0 {
|
||||
return &InodeIndexRecord{}, nil
|
||||
}
|
||||
|
||||
// The first foundation slice stored the current path as raw bytes. Keep that
|
||||
// format readable so existing records are transparently upgraded on write.
|
||||
if value[0] != '{' {
|
||||
record := &InodeIndexRecord{Generation: InodeIndexInitialGeneration}
|
||||
record.AddPath(util.FullPath(value))
|
||||
return record, nil
|
||||
}
|
||||
|
||||
record := &InodeIndexRecord{}
|
||||
if err := json.Unmarshal(value, record); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
record.normalize()
|
||||
return record, nil
|
||||
}
|
||||
|
||||
func (record *InodeIndexRecord) Encode() ([]byte, error) {
|
||||
record.normalize()
|
||||
return json.Marshal(record)
|
||||
}
|
||||
|
||||
func (record *InodeIndexRecord) normalize() {
|
||||
if len(record.Paths) == 0 {
|
||||
return
|
||||
}
|
||||
if record.Generation == 0 {
|
||||
record.Generation = InodeIndexInitialGeneration
|
||||
}
|
||||
|
||||
sanitized := make([]string, 0, len(record.Paths))
|
||||
for _, path := range record.Paths {
|
||||
if path == "" {
|
||||
continue
|
||||
}
|
||||
sanitized = append(sanitized, path)
|
||||
}
|
||||
if len(sanitized) == 0 {
|
||||
record.Paths = nil
|
||||
return
|
||||
}
|
||||
|
||||
sort.Strings(sanitized)
|
||||
deduped := sanitized[:1]
|
||||
for _, path := range sanitized[1:] {
|
||||
if path == deduped[len(deduped)-1] {
|
||||
continue
|
||||
}
|
||||
deduped = append(deduped, path)
|
||||
}
|
||||
record.Paths = deduped
|
||||
}
|
||||
|
||||
func (record *InodeIndexRecord) AddPath(path util.FullPath) bool {
|
||||
if path == "" {
|
||||
return false
|
||||
}
|
||||
record.normalize()
|
||||
target := string(path)
|
||||
index := sort.SearchStrings(record.Paths, target)
|
||||
if index < len(record.Paths) && record.Paths[index] == target {
|
||||
return false
|
||||
}
|
||||
record.Paths = append(record.Paths, "")
|
||||
copy(record.Paths[index+1:], record.Paths[index:])
|
||||
record.Paths[index] = target
|
||||
return true
|
||||
}
|
||||
|
||||
func (record *InodeIndexRecord) RemovePath(path util.FullPath) bool {
|
||||
if len(record.Paths) == 0 || path == "" {
|
||||
return false
|
||||
}
|
||||
record.normalize()
|
||||
target := string(path)
|
||||
index := sort.SearchStrings(record.Paths, target)
|
||||
if index >= len(record.Paths) || record.Paths[index] != target {
|
||||
return false
|
||||
}
|
||||
record.Paths = append(record.Paths[:index], record.Paths[index+1:]...)
|
||||
if len(record.Paths) == 0 {
|
||||
record.Paths = nil
|
||||
}
|
||||
return true
|
||||
}
|
||||
|
||||
func (record *InodeIndexRecord) CanonicalPath() util.FullPath {
|
||||
record.normalize()
|
||||
if len(record.Paths) == 0 {
|
||||
return ""
|
||||
}
|
||||
return util.FullPath(record.Paths[0])
|
||||
}
|
||||
|
||||
func (record *InodeIndexRecord) FullPaths() []util.FullPath {
|
||||
record.normalize()
|
||||
if len(record.Paths) == 0 {
|
||||
return nil
|
||||
}
|
||||
paths := make([]util.FullPath, 0, len(record.Paths))
|
||||
for _, path := range record.Paths {
|
||||
paths = append(paths, util.FullPath(path))
|
||||
}
|
||||
return paths
|
||||
}
|
||||
|
||||
// inodeIndexInScope reports whether the entry at path gets an inode index row.
|
||||
// The index is opt-in per filer.conf inode_index location rules; with no
|
||||
// matching rule (the default) nothing is ever written.
|
||||
func (fsw *FilerStoreWrapper) inodeIndexInScope(path util.FullPath) bool {
|
||||
if fsw.filerConfFn == nil {
|
||||
return false
|
||||
}
|
||||
fc := fsw.filerConfFn()
|
||||
if fc == nil {
|
||||
return false
|
||||
}
|
||||
return fc.MatchInodeIndexRule(string(path))
|
||||
}
|
||||
|
||||
// inodeIndexUnder reports whether any entry inside dirPath may carry an inode
|
||||
// index row, so DeleteFolderChildren can skip the housekeeping walk entirely
|
||||
// when the subtree does not intersect any inode_index rule.
|
||||
func (fsw *FilerStoreWrapper) inodeIndexUnder(dirPath util.FullPath) bool {
|
||||
if fsw.filerConfFn == nil {
|
||||
return false
|
||||
}
|
||||
fc := fsw.filerConfFn()
|
||||
if fc == nil {
|
||||
return false
|
||||
}
|
||||
return fc.InodeIndexActiveUnder(string(dirPath))
|
||||
}
|
||||
|
||||
func (fsw *FilerStoreWrapper) lookupInodeIndex(ctx context.Context, inode uint64) (*InodeIndexRecord, error) {
|
||||
if inode == 0 {
|
||||
return nil, ErrKvNotFound
|
||||
}
|
||||
|
||||
value, err := fsw.KvGet(ctx, InodeIndexKey(inode))
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
return DecodeInodeIndexRecord(value)
|
||||
}
|
||||
|
||||
func (fsw *FilerStoreWrapper) storeInodeIndex(ctx context.Context, path util.FullPath, inode uint64) error {
|
||||
if inode == 0 || path == "" {
|
||||
return nil
|
||||
}
|
||||
|
||||
lock := inodeRowLock(inode)
|
||||
lock.Lock()
|
||||
defer lock.Unlock()
|
||||
|
||||
record, err := fsw.lookupInodeIndex(ctx, inode)
|
||||
if err != nil {
|
||||
if err != ErrKvNotFound {
|
||||
return err
|
||||
}
|
||||
record = &InodeIndexRecord{Generation: InodeIndexInitialGeneration}
|
||||
}
|
||||
if !record.AddPath(path) {
|
||||
return nil
|
||||
}
|
||||
|
||||
value, err := record.Encode()
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
return fsw.KvPut(ctx, InodeIndexKey(inode), value)
|
||||
}
|
||||
|
||||
func (fsw *FilerStoreWrapper) lookupInodePath(ctx context.Context, inode uint64) (util.FullPath, error) {
|
||||
record, err := fsw.lookupInodeIndex(ctx, inode)
|
||||
if err != nil {
|
||||
return "", err
|
||||
}
|
||||
|
||||
path := record.CanonicalPath()
|
||||
if path == "" {
|
||||
return "", ErrKvNotFound
|
||||
}
|
||||
return path, nil
|
||||
}
|
||||
|
||||
func (fsw *FilerStoreWrapper) lookupInodePaths(ctx context.Context, inode uint64) ([]util.FullPath, error) {
|
||||
record, err := fsw.lookupInodeIndex(ctx, inode)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
paths := record.FullPaths()
|
||||
if len(paths) == 0 {
|
||||
return nil, ErrKvNotFound
|
||||
}
|
||||
return paths, nil
|
||||
}
|
||||
|
||||
func (fsw *FilerStoreWrapper) removePathFromInodeIndex(ctx context.Context, path util.FullPath, inode uint64) error {
|
||||
if inode == 0 || path == "" {
|
||||
return nil
|
||||
}
|
||||
|
||||
lock := inodeRowLock(inode)
|
||||
lock.Lock()
|
||||
defer lock.Unlock()
|
||||
|
||||
record, err := fsw.lookupInodeIndex(ctx, inode)
|
||||
if err != nil {
|
||||
if err == ErrKvNotFound {
|
||||
return nil
|
||||
}
|
||||
return err
|
||||
}
|
||||
|
||||
if !record.RemovePath(path) {
|
||||
return nil
|
||||
}
|
||||
if len(record.Paths) == 0 {
|
||||
return fsw.KvDelete(ctx, InodeIndexKey(inode))
|
||||
}
|
||||
|
||||
value, err := record.Encode()
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
return fsw.KvPut(ctx, InodeIndexKey(inode), value)
|
||||
}
|
||||
|
||||
func (fsw *FilerStoreWrapper) collectInodeIndexEntries(ctx context.Context, dirPath util.FullPath) ([]inodeIndexEntry, error) {
|
||||
if !fsw.inodeIndexUnder(dirPath) {
|
||||
return nil, nil
|
||||
}
|
||||
// Honor caller cancellation during the walk: a DeleteFolderChildren on a
|
||||
// pathological directory could otherwise loop indefinitely gathering
|
||||
// entries even after the client has given up, turning into a DoS vector.
|
||||
// If the walk is aborted, the caller treats the index cleanup as
|
||||
// best-effort and drops the partial result.
|
||||
var collected []inodeIndexEntry
|
||||
if err := fsw.collectInodeIndexEntriesRecursive(ctx, dirPath, &collected); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return collected, nil
|
||||
}
|
||||
|
||||
func (fsw *FilerStoreWrapper) collectInodeIndexEntriesRecursive(ctx context.Context, dirPath util.FullPath, collected *[]inodeIndexEntry) error {
|
||||
actualStore := fsw.getActualStore(dirPath + "/")
|
||||
|
||||
lastFileName := ""
|
||||
includeStartFile := false
|
||||
for {
|
||||
if err := ctx.Err(); err != nil {
|
||||
return err
|
||||
}
|
||||
page := make([]*Entry, 0, PaginationSize)
|
||||
nextLastFileName, err := actualStore.ListDirectoryEntries(ctx, dirPath, lastFileName, includeStartFile, PaginationSize, func(entry *Entry) (bool, error) {
|
||||
page = append(page, entry)
|
||||
return true, nil
|
||||
})
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
for _, entry := range page {
|
||||
if entry.Attr.Inode != 0 {
|
||||
*collected = append(*collected, inodeIndexEntry{path: entry.FullPath, inode: entry.Attr.Inode})
|
||||
}
|
||||
if entry.IsDirectory() {
|
||||
if err := fsw.collectInodeIndexEntriesRecursive(ctx, entry.FullPath, collected); err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if len(page) < PaginationSize {
|
||||
return nil
|
||||
}
|
||||
lastFileName = nextLastFileName
|
||||
includeStartFile = false
|
||||
}
|
||||
}
|
||||
|
||||
// recordInodeIndexWrite updates the inode→path secondary index after the
|
||||
// primary store mutation has already succeeded. The index is best-effort: a
|
||||
// failure here must not surface as an operation error, because the caller
|
||||
// would then observe a failed create/update even though the entry was
|
||||
// persisted, and a retry cannot heal the index (DeleteEntry exits early once
|
||||
// the entry is gone). We log and let later writes rebuild the record.
|
||||
func (fsw *FilerStoreWrapper) recordInodeIndexWrite(ctx context.Context, op string, path util.FullPath, inode uint64) {
|
||||
if inode == 0 || path == "" || !fsw.inodeIndexInScope(path) {
|
||||
return
|
||||
}
|
||||
if err := fsw.storeInodeIndex(ctx, path, inode); err != nil {
|
||||
glog.WarningfCtx(ctx, "%s: update inode index for %s (inode %d): %v", op, path, inode, err)
|
||||
}
|
||||
}
|
||||
|
||||
// recordInodeIndexRemoval mirrors recordInodeIndexWrite for removals.
|
||||
func (fsw *FilerStoreWrapper) recordInodeIndexRemoval(ctx context.Context, op string, path util.FullPath, inode uint64) {
|
||||
if inode == 0 || path == "" || !fsw.inodeIndexInScope(path) {
|
||||
return
|
||||
}
|
||||
if err := fsw.removePathFromInodeIndex(ctx, path, inode); err != nil {
|
||||
glog.WarningfCtx(ctx, "%s: clear inode index for %s (inode %d): %v", op, path, inode, err)
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,316 @@
|
||||
package filer
|
||||
|
||||
import (
|
||||
"context"
|
||||
"os"
|
||||
"strings"
|
||||
"testing"
|
||||
|
||||
"github.com/seaweedfs/seaweedfs/weed/pb/filer_pb"
|
||||
"github.com/seaweedfs/seaweedfs/weed/util"
|
||||
"github.com/stretchr/testify/assert"
|
||||
"github.com/stretchr/testify/require"
|
||||
)
|
||||
|
||||
func inodeIndexConf(prefixes ...string) *FilerConf {
|
||||
fc := NewFilerConf()
|
||||
for _, prefix := range prefixes {
|
||||
_ = fc.SetLocationConf(&filer_pb.FilerConf_PathConf{LocationPrefix: prefix, InodeIndex: true})
|
||||
}
|
||||
return fc
|
||||
}
|
||||
|
||||
func newInodeIndexWrapper(prefixes ...string) (*FilerStoreWrapper, *stubFilerStore) {
|
||||
store := newStubFilerStore()
|
||||
wrapper := NewFilerStoreWrapper(store)
|
||||
fc := inodeIndexConf(prefixes...)
|
||||
wrapper.filerConfFn = func() *FilerConf { return fc }
|
||||
return wrapper, store
|
||||
}
|
||||
|
||||
func countInodeIndexRows(store *stubFilerStore) int {
|
||||
store.mu.Lock()
|
||||
defer store.mu.Unlock()
|
||||
count := 0
|
||||
for key := range store.kv {
|
||||
if strings.HasPrefix(key, inodeIndexKeyPrefix) {
|
||||
count++
|
||||
}
|
||||
}
|
||||
return count
|
||||
}
|
||||
|
||||
func TestFilerStoreWrapperMaintainsInodeIndexLifecycle(t *testing.T) {
|
||||
wrapper, _ := newInodeIndexWrapper("/")
|
||||
ctx := context.Background()
|
||||
|
||||
created := &Entry{
|
||||
FullPath: util.FullPath("/docs/report.txt"),
|
||||
Attr: Attr{
|
||||
Mode: 0o644,
|
||||
Inode: 42,
|
||||
},
|
||||
}
|
||||
|
||||
require.NoError(t, wrapper.InsertEntry(ctx, created))
|
||||
path, err := wrapper.lookupInodePath(ctx, created.Attr.Inode)
|
||||
require.NoError(t, err)
|
||||
assert.Equal(t, created.FullPath, path)
|
||||
paths, err := wrapper.lookupInodePaths(ctx, created.Attr.Inode)
|
||||
require.NoError(t, err)
|
||||
assert.Equal(t, []util.FullPath{created.FullPath}, paths)
|
||||
record, err := wrapper.lookupInodeIndex(ctx, created.Attr.Inode)
|
||||
require.NoError(t, err)
|
||||
assert.Equal(t, InodeIndexInitialGeneration, record.Generation)
|
||||
|
||||
updated := &Entry{
|
||||
FullPath: util.FullPath("/docs/report.txt"),
|
||||
Attr: Attr{
|
||||
Mode: 0o600,
|
||||
Inode: 42,
|
||||
},
|
||||
}
|
||||
require.NoError(t, wrapper.UpdateEntry(ctx, updated))
|
||||
path, err = wrapper.lookupInodePath(ctx, updated.Attr.Inode)
|
||||
require.NoError(t, err)
|
||||
assert.Equal(t, updated.FullPath, path)
|
||||
|
||||
require.NoError(t, wrapper.DeleteEntry(ctx, created.FullPath))
|
||||
_, err = wrapper.lookupInodePath(ctx, created.Attr.Inode)
|
||||
require.ErrorIs(t, err, ErrKvNotFound)
|
||||
}
|
||||
|
||||
func TestFilerStoreWrapperMaintainsMultiplePathsPerInode(t *testing.T) {
|
||||
wrapper, _ := newInodeIndexWrapper("/")
|
||||
ctx := context.Background()
|
||||
inode := uint64(88)
|
||||
hardLinkId := NewHardLinkId()
|
||||
|
||||
require.NoError(t, wrapper.InsertEntry(ctx, &Entry{
|
||||
FullPath: util.FullPath("/links/b.txt"),
|
||||
Attr: Attr{
|
||||
Mode: 0o644,
|
||||
Inode: inode,
|
||||
},
|
||||
HardLinkId: hardLinkId,
|
||||
HardLinkCounter: 2,
|
||||
}))
|
||||
require.NoError(t, wrapper.InsertEntry(ctx, &Entry{
|
||||
FullPath: util.FullPath("/links/a.txt"),
|
||||
Attr: Attr{
|
||||
Mode: 0o644,
|
||||
Inode: inode,
|
||||
},
|
||||
HardLinkId: hardLinkId,
|
||||
HardLinkCounter: 2,
|
||||
}))
|
||||
|
||||
paths, err := wrapper.lookupInodePaths(ctx, inode)
|
||||
require.NoError(t, err)
|
||||
assert.Equal(t, []util.FullPath{"/links/a.txt", "/links/b.txt"}, paths)
|
||||
record, err := wrapper.lookupInodeIndex(ctx, inode)
|
||||
require.NoError(t, err)
|
||||
assert.Equal(t, InodeIndexInitialGeneration, record.Generation)
|
||||
|
||||
path, err := wrapper.lookupInodePath(ctx, inode)
|
||||
require.NoError(t, err)
|
||||
assert.Equal(t, util.FullPath("/links/a.txt"), path)
|
||||
|
||||
require.NoError(t, wrapper.DeleteEntry(ctx, util.FullPath("/links/a.txt")))
|
||||
|
||||
paths, err = wrapper.lookupInodePaths(ctx, inode)
|
||||
require.NoError(t, err)
|
||||
assert.Equal(t, []util.FullPath{"/links/b.txt"}, paths)
|
||||
|
||||
path, err = wrapper.lookupInodePath(ctx, inode)
|
||||
require.NoError(t, err)
|
||||
assert.Equal(t, util.FullPath("/links/b.txt"), path)
|
||||
}
|
||||
|
||||
func TestFilerStoreWrapperUpgradesLegacySinglePathInodeIndexRecords(t *testing.T) {
|
||||
wrapper := NewFilerStoreWrapper(newStubFilerStore())
|
||||
ctx := context.Background()
|
||||
inode := uint64(91)
|
||||
|
||||
require.NoError(t, wrapper.KvPut(ctx, InodeIndexKey(inode), []byte("/legacy/path.txt")))
|
||||
|
||||
path, err := wrapper.lookupInodePath(ctx, inode)
|
||||
require.NoError(t, err)
|
||||
assert.Equal(t, util.FullPath("/legacy/path.txt"), path)
|
||||
|
||||
paths, err := wrapper.lookupInodePaths(ctx, inode)
|
||||
require.NoError(t, err)
|
||||
assert.Equal(t, []util.FullPath{"/legacy/path.txt"}, paths)
|
||||
|
||||
require.NoError(t, wrapper.storeInodeIndex(ctx, util.FullPath("/legacy/second.txt"), inode))
|
||||
|
||||
paths, err = wrapper.lookupInodePaths(ctx, inode)
|
||||
require.NoError(t, err)
|
||||
assert.Equal(t, []util.FullPath{"/legacy/path.txt", "/legacy/second.txt"}, paths)
|
||||
|
||||
value, err := wrapper.KvGet(ctx, InodeIndexKey(inode))
|
||||
require.NoError(t, err)
|
||||
assert.JSONEq(t, `{"generation":1,"paths":["/legacy/path.txt","/legacy/second.txt"]}`, string(value))
|
||||
}
|
||||
|
||||
func TestFilerStoreWrapperKeepsInodeIndexWhenDeleteArrivesAfterRenameInsert(t *testing.T) {
|
||||
wrapper, _ := newInodeIndexWrapper("/")
|
||||
ctx := context.Background()
|
||||
inode := uint64(77)
|
||||
|
||||
require.NoError(t, wrapper.InsertEntry(ctx, &Entry{
|
||||
FullPath: util.FullPath("/old/name.txt"),
|
||||
Attr: Attr{
|
||||
Mode: 0o644,
|
||||
Inode: inode,
|
||||
},
|
||||
}))
|
||||
require.NoError(t, wrapper.InsertEntry(ctx, &Entry{
|
||||
FullPath: util.FullPath("/new/name.txt"),
|
||||
Attr: Attr{
|
||||
Mode: 0o644,
|
||||
Inode: inode,
|
||||
},
|
||||
}))
|
||||
require.NoError(t, wrapper.DeleteEntry(ctx, util.FullPath("/old/name.txt")))
|
||||
|
||||
path, err := wrapper.lookupInodePath(ctx, inode)
|
||||
require.NoError(t, err)
|
||||
assert.Equal(t, util.FullPath("/new/name.txt"), path)
|
||||
|
||||
paths, err := wrapper.lookupInodePaths(ctx, inode)
|
||||
require.NoError(t, err)
|
||||
assert.Equal(t, []util.FullPath{"/new/name.txt"}, paths)
|
||||
}
|
||||
|
||||
func TestInodeIndexDisabledWritesNothing(t *testing.T) {
|
||||
// No filer.conf wired at all, and a conf with no inode_index rules: both
|
||||
// must leave the KV store free of index rows.
|
||||
t.Run("no conf", func(t *testing.T) {
|
||||
store := newStubFilerStore()
|
||||
runInodeIndexDisabledScenario(t, NewFilerStoreWrapper(store), store)
|
||||
})
|
||||
t.Run("conf without rules", func(t *testing.T) {
|
||||
wrapper, store := newInodeIndexWrapper()
|
||||
runInodeIndexDisabledScenario(t, wrapper, store)
|
||||
})
|
||||
}
|
||||
|
||||
func runInodeIndexDisabledScenario(t *testing.T, wrapper *FilerStoreWrapper, store *stubFilerStore) {
|
||||
ctx := context.Background()
|
||||
entry := &Entry{
|
||||
FullPath: util.FullPath("/docs/report.txt"),
|
||||
Attr: Attr{
|
||||
Mode: 0o644,
|
||||
Inode: 42,
|
||||
},
|
||||
}
|
||||
require.NoError(t, wrapper.InsertEntry(ctx, entry))
|
||||
require.NoError(t, wrapper.UpdateEntry(ctx, entry))
|
||||
assert.Zero(t, countInodeIndexRows(store))
|
||||
require.NoError(t, wrapper.DeleteEntry(ctx, entry.FullPath))
|
||||
assert.Zero(t, countInodeIndexRows(store))
|
||||
}
|
||||
|
||||
func TestInodeIndexScopedToConfiguredPrefixes(t *testing.T) {
|
||||
wrapper, store := newInodeIndexWrapper("/exports")
|
||||
ctx := context.Background()
|
||||
|
||||
require.NoError(t, wrapper.InsertEntry(ctx, &Entry{
|
||||
FullPath: util.FullPath("/exports/docs/in.txt"),
|
||||
Attr: Attr{Mode: 0o644, Inode: 1001},
|
||||
}))
|
||||
require.NoError(t, wrapper.InsertEntry(ctx, &Entry{
|
||||
FullPath: util.FullPath("/other/out.txt"),
|
||||
Attr: Attr{Mode: 0o644, Inode: 1002},
|
||||
}))
|
||||
|
||||
assert.Equal(t, 1, countInodeIndexRows(store))
|
||||
path, err := wrapper.lookupInodePath(ctx, 1001)
|
||||
require.NoError(t, err)
|
||||
assert.Equal(t, util.FullPath("/exports/docs/in.txt"), path)
|
||||
_, err = wrapper.lookupInodePath(ctx, 1002)
|
||||
require.ErrorIs(t, err, ErrKvNotFound)
|
||||
|
||||
require.NoError(t, wrapper.InsertEntry(ctx, &Entry{
|
||||
FullPath: util.FullPath("/exports/docs"),
|
||||
Attr: Attr{Mode: os.ModeDir | 0o755, Inode: 1000},
|
||||
}))
|
||||
require.NoError(t, wrapper.DeleteFolderChildren(ctx, util.FullPath("/exports")))
|
||||
_, err = wrapper.lookupInodePath(ctx, 1001)
|
||||
require.ErrorIs(t, err, ErrKvNotFound)
|
||||
}
|
||||
|
||||
func TestRecursiveDeleteRemovesDescendantInodeIndexes(t *testing.T) {
|
||||
f, _ := newTestFilerWithStubStore()
|
||||
fc := inodeIndexConf("/tree")
|
||||
f.Store.(*FilerStoreWrapper).filerConfFn = func() *FilerConf { return fc }
|
||||
ctx := context.Background()
|
||||
|
||||
entries := []*Entry{
|
||||
{
|
||||
FullPath: util.FullPath("/tree"),
|
||||
Attr: Attr{
|
||||
Mode: os.ModeDir | 0o755,
|
||||
Inode: 100,
|
||||
},
|
||||
},
|
||||
{
|
||||
FullPath: util.FullPath("/tree/file.txt"),
|
||||
Attr: Attr{
|
||||
Mode: 0o644,
|
||||
Inode: 101,
|
||||
},
|
||||
},
|
||||
{
|
||||
FullPath: util.FullPath("/tree/subdir"),
|
||||
Attr: Attr{
|
||||
Mode: os.ModeDir | 0o755,
|
||||
Inode: 102,
|
||||
},
|
||||
},
|
||||
{
|
||||
FullPath: util.FullPath("/tree/subdir/nested.txt"),
|
||||
Attr: Attr{
|
||||
Mode: 0o644,
|
||||
Inode: 103,
|
||||
},
|
||||
},
|
||||
}
|
||||
|
||||
for _, entry := range entries {
|
||||
require.NoError(t, f.Store.InsertEntry(ctx, entry))
|
||||
}
|
||||
|
||||
require.NoError(t, f.DeleteEntryMetaAndData(ctx, util.FullPath("/tree"), true, false, false, false, nil, 0))
|
||||
|
||||
for _, inode := range []uint64{100, 101, 102, 103} {
|
||||
_, err := f.Store.(*FilerStoreWrapper).lookupInodePath(ctx, inode)
|
||||
require.ErrorIs(t, err, ErrKvNotFound)
|
||||
}
|
||||
}
|
||||
|
||||
func TestInodeIndexActiveUnder(t *testing.T) {
|
||||
fc := inodeIndexConf("/exports")
|
||||
assert.True(t, fc.InodeIndexActiveUnder("/"))
|
||||
assert.True(t, fc.InodeIndexActiveUnder("/exports"))
|
||||
assert.True(t, fc.InodeIndexActiveUnder("/exports/sub"))
|
||||
assert.False(t, fc.InodeIndexActiveUnder("/other"))
|
||||
assert.False(t, fc.InodeIndexActiveUnder("/exp"))
|
||||
assert.False(t, fc.InodeIndexActiveUnder("/export"))
|
||||
|
||||
assert.False(t, NewFilerConf().InodeIndexActiveUnder("/"))
|
||||
}
|
||||
|
||||
func TestMatchInodeIndexRule(t *testing.T) {
|
||||
fc := inodeIndexConf("/exports")
|
||||
assert.True(t, fc.MatchInodeIndexRule("/exports"))
|
||||
assert.True(t, fc.MatchInodeIndexRule("/exports/sub/file"))
|
||||
assert.False(t, fc.MatchInodeIndexRule("/exports2"))
|
||||
assert.False(t, fc.MatchInodeIndexRule("/exports_old/x"))
|
||||
assert.False(t, fc.MatchInodeIndexRule("/exp"))
|
||||
assert.False(t, fc.MatchInodeIndexRule("/other"))
|
||||
|
||||
assert.True(t, inodeIndexConf("/").MatchInodeIndexRule("/anything"))
|
||||
assert.False(t, NewFilerConf().MatchInodeIndexRule("/exports"))
|
||||
}
|
||||
@@ -39,7 +39,8 @@ type FilerStoreWrapper struct {
|
||||
defaultStore FilerStore
|
||||
pathToStore ptrie.Trie[string]
|
||||
storeIdToStore map[string]FilerStore
|
||||
hasPathSpecificStore bool // fast check to skip MatchPrefix when no path-specific stores
|
||||
hasPathSpecificStore bool // fast check to skip MatchPrefix when no path-specific stores
|
||||
filerConfFn func() *FilerConf // set by Filer.SetStore; nil disables the inode index
|
||||
}
|
||||
|
||||
func NewFilerStoreWrapper(store FilerStore) *FilerStoreWrapper {
|
||||
@@ -154,7 +155,11 @@ func (fsw *FilerStoreWrapper) InsertEntry(ctx context.Context, entry *Entry) err
|
||||
return err
|
||||
}
|
||||
|
||||
return actualStore.InsertEntry(ctx, entry)
|
||||
if err := actualStore.InsertEntry(ctx, entry); err != nil {
|
||||
return err
|
||||
}
|
||||
fsw.recordInodeIndexWrite(ctx, "InsertEntry", entry.FullPath, entry.Attr.Inode)
|
||||
return nil
|
||||
}
|
||||
|
||||
// InsertEntryKnownAbsent skips the pre-insert FindEntry path when the caller has
|
||||
@@ -183,7 +188,11 @@ func (fsw *FilerStoreWrapper) InsertEntryKnownAbsent(ctx context.Context, entry
|
||||
}
|
||||
}
|
||||
|
||||
return actualStore.InsertEntry(ctx, entry)
|
||||
if err := actualStore.InsertEntry(ctx, entry); err != nil {
|
||||
return err
|
||||
}
|
||||
fsw.recordInodeIndexWrite(ctx, "InsertEntryKnownAbsent", entry.FullPath, entry.Attr.Inode)
|
||||
return nil
|
||||
}
|
||||
|
||||
func (fsw *FilerStoreWrapper) UpdateEntry(ctx context.Context, entry *Entry) error {
|
||||
@@ -211,7 +220,11 @@ func (fsw *FilerStoreWrapper) UpdateEntry(ctx context.Context, entry *Entry) err
|
||||
return err
|
||||
}
|
||||
|
||||
return actualStore.UpdateEntry(ctx, entry)
|
||||
if err := actualStore.UpdateEntry(ctx, entry); err != nil {
|
||||
return err
|
||||
}
|
||||
fsw.recordInodeIndexWrite(ctx, "UpdateEntry", entry.FullPath, entry.Attr.Inode)
|
||||
return nil
|
||||
}
|
||||
|
||||
func normalizeEntryMimeForStore(entry *Entry) {
|
||||
@@ -263,6 +276,8 @@ func (fsw *FilerStoreWrapper) DeleteEntry(ctx context.Context, fp util.FullPath)
|
||||
if findErr == filer_pb.ErrNotFound || existingEntry == nil {
|
||||
return nil
|
||||
}
|
||||
inode := existingEntry.Attr.Inode
|
||||
fullPath := existingEntry.FullPath
|
||||
if len(existingEntry.HardLinkId) != 0 {
|
||||
// remove hard link
|
||||
op := ctx.Value("OP")
|
||||
@@ -277,7 +292,11 @@ func (fsw *FilerStoreWrapper) DeleteEntry(ctx context.Context, fp util.FullPath)
|
||||
}
|
||||
}
|
||||
|
||||
return actualStore.DeleteEntry(ctx, fp)
|
||||
if err := actualStore.DeleteEntry(ctx, fp); err != nil {
|
||||
return err
|
||||
}
|
||||
fsw.recordInodeIndexRemoval(ctx, "DeleteEntry", fullPath, inode)
|
||||
return nil
|
||||
}
|
||||
|
||||
func (fsw *FilerStoreWrapper) DeleteOneEntry(ctx context.Context, existingEntry *Entry) (err error) {
|
||||
@@ -285,6 +304,8 @@ func (fsw *FilerStoreWrapper) DeleteOneEntry(ctx context.Context, existingEntry
|
||||
return err
|
||||
}
|
||||
ctx = context.WithoutCancel(ctx)
|
||||
fullPath := existingEntry.FullPath
|
||||
inode := existingEntry.Attr.Inode
|
||||
actualStore := fsw.getActualStore(existingEntry.FullPath)
|
||||
stats.FilerStoreCounter.WithLabelValues(actualStore.GetName(), "delete").Inc()
|
||||
start := time.Now()
|
||||
@@ -307,7 +328,11 @@ func (fsw *FilerStoreWrapper) DeleteOneEntry(ctx context.Context, existingEntry
|
||||
}
|
||||
}
|
||||
|
||||
return actualStore.DeleteEntry(ctx, existingEntry.FullPath)
|
||||
if err := actualStore.DeleteEntry(ctx, existingEntry.FullPath); err != nil {
|
||||
return err
|
||||
}
|
||||
fsw.recordInodeIndexRemoval(ctx, "DeleteOneEntry", fullPath, inode)
|
||||
return nil
|
||||
}
|
||||
|
||||
func (fsw *FilerStoreWrapper) DeleteFolderChildren(ctx context.Context, fp util.FullPath) (err error) {
|
||||
@@ -322,7 +347,20 @@ func (fsw *FilerStoreWrapper) DeleteFolderChildren(ctx context.Context, fp util.
|
||||
stats.FilerStoreHistogram.WithLabelValues(actualStore.GetName(), "deleteFolderChildren").Observe(time.Since(start).Seconds())
|
||||
}()
|
||||
|
||||
return actualStore.DeleteFolderChildren(ctx, fp)
|
||||
collected, collectErr := fsw.collectInodeIndexEntries(ctx, fp)
|
||||
if collectErr != nil {
|
||||
// Index collection is best-effort: a failure here only prevents inode
|
||||
// index housekeeping, not the directory removal itself.
|
||||
glog.WarningfCtx(ctx, "collectInodeIndexEntries %s: %v; deleting folder children without index cleanup", fp, collectErr)
|
||||
collected = nil
|
||||
}
|
||||
if err := actualStore.DeleteFolderChildren(ctx, fp); err != nil {
|
||||
return err
|
||||
}
|
||||
for _, entry := range collected {
|
||||
fsw.recordInodeIndexRemoval(ctx, "DeleteFolderChildren", entry.path, entry.inode)
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func (fsw *FilerStoreWrapper) ListDirectoryEntries(ctx context.Context, dirPath util.FullPath, startFileName string, includeStartFile bool, limit int64, eachEntryFunc ListEachEntryFunc) (string, error) {
|
||||
@@ -400,46 +438,39 @@ func (fsw *FilerStoreWrapper) prefixFilterEntries(ctx context.Context, dirPath u
|
||||
return actualStore.ListDirectoryEntries(ctx, dirPath, startFileName, includeStartFile, limit, eachEntryFunc)
|
||||
}
|
||||
|
||||
var notPrefixed []*Entry
|
||||
lastFileName, err = actualStore.ListDirectoryEntries(ctx, dirPath, startFileName, includeStartFile, limit, func(entry *Entry) (bool, error) {
|
||||
notPrefixed = append(notPrefixed, entry)
|
||||
return true, nil
|
||||
})
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
|
||||
// lastFileName must stay at the last name delivered to eachEntryFunc; the
|
||||
// raw scan cursor runs ahead over non-matching names and cannot be returned.
|
||||
count := int64(0)
|
||||
for count < limit && len(notPrefixed) > 0 {
|
||||
for _, entry := range notPrefixed {
|
||||
if strings.HasPrefix(entry.Name(), prefix) {
|
||||
count++
|
||||
res, resErr := eachEntryFunc(entry)
|
||||
|
||||
if resErr != nil {
|
||||
err = fmt.Errorf("failed to process eachEntryFunc for entry %q: %w", entry.Name(), resErr)
|
||||
return
|
||||
}
|
||||
|
||||
if !res {
|
||||
return
|
||||
}
|
||||
if count >= limit {
|
||||
break
|
||||
}
|
||||
}
|
||||
scanName, scanInclusive := startFileName, includeStartFile
|
||||
for count < limit {
|
||||
var page []*Entry
|
||||
if scanName, err = actualStore.ListDirectoryEntries(ctx, dirPath, scanName, scanInclusive, limit, func(entry *Entry) (bool, error) {
|
||||
page = append(page, entry)
|
||||
return true, nil
|
||||
}); err != nil {
|
||||
return
|
||||
}
|
||||
if count < limit && lastFileName < prefix {
|
||||
notPrefixed = notPrefixed[:0]
|
||||
lastFileName, err = actualStore.ListDirectoryEntries(ctx, dirPath, lastFileName, false, limit, func(entry *Entry) (bool, error) {
|
||||
notPrefixed = append(notPrefixed, entry)
|
||||
return true, nil
|
||||
})
|
||||
if err != nil {
|
||||
scanInclusive = false
|
||||
if len(page) == 0 {
|
||||
return
|
||||
}
|
||||
for _, entry := range page {
|
||||
if !strings.HasPrefix(entry.Name(), prefix) {
|
||||
continue
|
||||
}
|
||||
count++
|
||||
res, resErr := eachEntryFunc(entry)
|
||||
if resErr != nil {
|
||||
err = fmt.Errorf("failed to process eachEntryFunc for entry %q: %w", entry.Name(), resErr)
|
||||
return
|
||||
}
|
||||
} else {
|
||||
break
|
||||
lastFileName = entry.Name()
|
||||
if !res || count >= limit {
|
||||
return
|
||||
}
|
||||
}
|
||||
if scanName >= prefix && !strings.HasPrefix(scanName, prefix) {
|
||||
return
|
||||
}
|
||||
}
|
||||
return
|
||||
|
||||
@@ -205,3 +205,41 @@ func TestFilerStoreWrapperRollbackSucceedsWithCancelledContext(t *testing.T) {
|
||||
assert.NoError(t, wrapper.RollbackTransaction(cancelledCtx()))
|
||||
assert.NoError(t, wrapper.RollbackTransaction(expiredCtx()))
|
||||
}
|
||||
|
||||
type noPrefixListStore struct {
|
||||
*stubFilerStore
|
||||
}
|
||||
|
||||
func (s *noPrefixListStore) ListDirectoryPrefixedEntries(context.Context, util.FullPath, string, bool, int64, string, ListEachEntryFunc) (string, error) {
|
||||
return "", ErrUnsupportedListDirectoryPrefixed
|
||||
}
|
||||
|
||||
// The wrapper fallback must return the last delivered name as the cursor, not
|
||||
// the end of the raw page, and must keep paging while prefix matches can still
|
||||
// follow the raw cursor.
|
||||
func TestPrefixFilterEntriesCursorStaysAtLastDelivered(t *testing.T) {
|
||||
store := &noPrefixListStore{newStubFilerStore()}
|
||||
wrapper := NewFilerStoreWrapper(store)
|
||||
ctx := context.Background()
|
||||
for _, name := range []string{"a", "b1", "b2", "b3", "b4"} {
|
||||
require.NoError(t, store.InsertEntry(ctx, &Entry{FullPath: util.FullPath("/d/" + name)}))
|
||||
}
|
||||
|
||||
var delivered []string
|
||||
lastFileName, err := wrapper.ListDirectoryPrefixedEntries(ctx, util.FullPath("/d"), "", false, 2, "b", func(entry *Entry) (bool, error) {
|
||||
delivered = append(delivered, entry.Name())
|
||||
return true, nil
|
||||
})
|
||||
require.NoError(t, err)
|
||||
assert.Equal(t, []string{"b1", "b2"}, delivered)
|
||||
assert.Equal(t, "b2", lastFileName)
|
||||
|
||||
delivered = delivered[:0]
|
||||
lastFileName, err = wrapper.ListDirectoryPrefixedEntries(ctx, util.FullPath("/d"), lastFileName, false, 10, "b", func(entry *Entry) (bool, error) {
|
||||
delivered = append(delivered, entry.Name())
|
||||
return true, nil
|
||||
})
|
||||
require.NoError(t, err)
|
||||
assert.Equal(t, []string{"b3", "b4"}, delivered)
|
||||
assert.Equal(t, "b4", lastFileName)
|
||||
}
|
||||
@@ -156,7 +156,11 @@ func (store *HbaseStore) ListDirectoryEntries(ctx context.Context, dirPath util.
|
||||
func (store *HbaseStore) ListDirectoryPrefixedEntries(ctx context.Context, dirPath util.FullPath, startFileName string, includeStartFile bool, limit int64, prefix string, eachEntryFunc filer.ListEachEntryFunc) (lastFileName string, err error) {
|
||||
family := map[string][]string{store.cfMetaDir: {COLUMN_NAME}}
|
||||
expectedPrefix := []byte(dirPath.Child(prefix))
|
||||
scan, err := hrpc.NewScanRange(ctx, store.table, expectedPrefix, nil, hrpc.Families(family))
|
||||
scanStart := expectedPrefix
|
||||
if resumeKey := []byte(dirPath.Child(startFileName)); bytes.Compare(resumeKey, scanStart) > 0 {
|
||||
scanStart = resumeKey
|
||||
}
|
||||
scan, err := hrpc.NewScanRange(ctx, store.table, scanStart, nil, hrpc.Families(family))
|
||||
if err != nil {
|
||||
return lastFileName, err
|
||||
}
|
||||
@@ -188,7 +192,7 @@ func (store *HbaseStore) ListDirectoryPrefixedEntries(ctx context.Context, dirPa
|
||||
|
||||
value := cell.Value
|
||||
|
||||
if fileName == startFileName && !includeStartFile {
|
||||
if fileName < startFileName || (fileName == startFileName && !includeStartFile) {
|
||||
continue
|
||||
}
|
||||
|
||||
|
||||
@@ -221,7 +221,7 @@ func (store *LevelDBStore) ListDirectoryPrefixedEntries(ctx context.Context, dir
|
||||
|
||||
directoryPrefix := genDirectoryKeyPrefix(dirPath, prefix)
|
||||
lastFileStart := directoryPrefix
|
||||
if startFileName != "" {
|
||||
if startFileName > prefix {
|
||||
lastFileStart = genDirectoryKeyPrefix(dirPath, startFileName)
|
||||
}
|
||||
|
||||
|
||||
@@ -110,3 +110,28 @@ func BenchmarkInsertEntry(b *testing.B) {
|
||||
store.InsertEntry(ctx, entry)
|
||||
}
|
||||
}
|
||||
|
||||
func TestListDirectoryPrefixedEntries_StartBelowPrefix(t *testing.T) {
|
||||
store := &LevelDBStore{}
|
||||
store.initialize(t.TempDir())
|
||||
defer store.Shutdown()
|
||||
|
||||
ctx := context.Background()
|
||||
for _, name := range []string{"item-a", "item-a.versions", "item-b.versions"} {
|
||||
if err := store.InsertEntry(ctx, &filer.Entry{FullPath: util.FullPath("/d/" + name)}); err != nil {
|
||||
t.Fatalf("insert %s: %v", name, err)
|
||||
}
|
||||
}
|
||||
|
||||
var names []string
|
||||
_, err := store.ListDirectoryPrefixedEntries(ctx, util.FullPath("/d"), "item", true, 100, "item-b", func(entry *filer.Entry) (bool, error) {
|
||||
names = append(names, entry.Name())
|
||||
return true, nil
|
||||
})
|
||||
if err != nil {
|
||||
t.Fatalf("list: %v", err)
|
||||
}
|
||||
if len(names) != 1 || names[0] != "item-b.versions" {
|
||||
t.Fatalf("want [item-b.versions], got %v", names)
|
||||
}
|
||||
}
|
||||
@@ -239,7 +239,7 @@ func (store *LevelDB2Store) ListDirectoryPrefixedEntries(ctx context.Context, di
|
||||
|
||||
directoryPrefix, partitionId := genDirectoryKeyPrefix(dirPath, prefix, store.dbCount)
|
||||
lastFileStart := directoryPrefix
|
||||
if startFileName != "" {
|
||||
if startFileName > prefix {
|
||||
lastFileStart, _ = genDirectoryKeyPrefix(dirPath, startFileName, store.dbCount)
|
||||
}
|
||||
|
||||
|
||||
@@ -151,6 +151,31 @@ func TestInitializeRejectsOversizedOption(t *testing.T) {
|
||||
}
|
||||
}
|
||||
|
||||
func TestListDirectoryPrefixedEntries_StartBelowPrefix(t *testing.T) {
|
||||
store := &LevelDB2Store{}
|
||||
store.initialize(t.TempDir(), 2)
|
||||
defer store.Shutdown()
|
||||
|
||||
ctx := context.Background()
|
||||
for _, name := range []string{"item-a", "item-a.versions", "item-b.versions"} {
|
||||
if err := store.InsertEntry(ctx, &filer.Entry{FullPath: util.FullPath("/d/" + name)}); err != nil {
|
||||
t.Fatalf("insert %s: %v", name, err)
|
||||
}
|
||||
}
|
||||
|
||||
var names []string
|
||||
_, err := store.ListDirectoryPrefixedEntries(ctx, util.FullPath("/d"), "item", true, 100, "item-b", func(entry *filer.Entry) (bool, error) {
|
||||
names = append(names, entry.Name())
|
||||
return true, nil
|
||||
})
|
||||
if err != nil {
|
||||
t.Fatalf("list: %v", err)
|
||||
}
|
||||
if len(names) != 1 || names[0] != "item-b.versions" {
|
||||
t.Fatalf("want [item-b.versions], got %v", names)
|
||||
}
|
||||
}
|
||||
|
||||
func TestEmptyRoot(t *testing.T) {
|
||||
testFiler := filer.NewFiler(pb.ServerDiscovery{}, nil, "", "", "", "", "", 255, nil)
|
||||
dir := t.TempDir()
|
||||
|
||||
@@ -370,7 +370,7 @@ func (store *LevelDB3Store) ListDirectoryPrefixedEntries(ctx context.Context, di
|
||||
|
||||
directoryPrefix := genDirectoryKeyPrefix(shortPath, prefix)
|
||||
lastFileStart := directoryPrefix
|
||||
if startFileName != "" {
|
||||
if startFileName > prefix {
|
||||
lastFileStart = genDirectoryKeyPrefix(shortPath, startFileName)
|
||||
}
|
||||
|
||||
|
||||
@@ -182,3 +182,28 @@ func TestEmptyRoot(t *testing.T) {
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
func TestListDirectoryPrefixedEntries_StartBelowPrefix(t *testing.T) {
|
||||
store := &LevelDB3Store{}
|
||||
store.initialize(t.TempDir())
|
||||
defer store.Shutdown()
|
||||
|
||||
ctx := context.Background()
|
||||
for _, name := range []string{"item-a", "item-a.versions", "item-b.versions"} {
|
||||
if err := store.InsertEntry(ctx, &filer.Entry{FullPath: util.FullPath("/d/" + name)}); err != nil {
|
||||
t.Fatalf("insert %s: %v", name, err)
|
||||
}
|
||||
}
|
||||
|
||||
var names []string
|
||||
_, err := store.ListDirectoryPrefixedEntries(ctx, util.FullPath("/d"), "item", true, 100, "item-b", func(entry *filer.Entry) (bool, error) {
|
||||
names = append(names, entry.Name())
|
||||
return true, nil
|
||||
})
|
||||
if err != nil {
|
||||
t.Fatalf("list: %v", err)
|
||||
}
|
||||
if len(names) != 1 || names[0] != "item-b.versions" {
|
||||
t.Fatalf("want [item-b.versions], got %v", names)
|
||||
}
|
||||
}
|
||||
@@ -15,6 +15,10 @@ import (
|
||||
|
||||
const (
|
||||
DIR_LIST_MARKER = "\x00"
|
||||
|
||||
// a listing whose callback stops early reads at most one batch more than
|
||||
// it uses, and no single pipeline grows with the listing limit
|
||||
listFetchBatchSize = 128
|
||||
)
|
||||
|
||||
type UniversalRedis2Store struct {
|
||||
@@ -96,7 +100,12 @@ func (store *UniversalRedis2Store) UpdateEntry(ctx context.Context, entry *filer
|
||||
|
||||
func (store *UniversalRedis2Store) FindEntry(ctx context.Context, fullpath util.FullPath) (entry *filer.Entry, err error) {
|
||||
|
||||
data, err := store.Client.Get(ctx, store.getKey(string(fullpath))).Result()
|
||||
return decodeEntry(fullpath, store.Client.Get(ctx, store.getKey(string(fullpath))))
|
||||
}
|
||||
|
||||
func decodeEntry(fullpath util.FullPath, get *redis.StringCmd) (entry *filer.Entry, err error) {
|
||||
|
||||
data, err := get.Result()
|
||||
if err == redis.Nil {
|
||||
return nil, filer_pb.ErrNotFound
|
||||
}
|
||||
@@ -202,9 +211,13 @@ func (store *UniversalRedis2Store) ListDirectoryEntries(ctx context.Context, dir
|
||||
|
||||
// fetch entry meta
|
||||
var entry *filer.Entry
|
||||
for _, fileName := range members {
|
||||
var gets []*redis.StringCmd
|
||||
for i, fileName := range members {
|
||||
if i%listFetchBatchSize == 0 {
|
||||
gets = store.getEntries(ctx, dirPath, members[i:])
|
||||
}
|
||||
path := util.NewFullPath(string(dirPath), fileName)
|
||||
entry, err = store.FindEntry(ctx, path)
|
||||
entry, err = decodeEntry(path, gets[i%listFetchBatchSize])
|
||||
lastFileName = fileName
|
||||
if err != nil {
|
||||
glog.V(0).InfofCtx(ctx, "list %s : %v", path, err)
|
||||
@@ -235,6 +248,23 @@ func (store *UniversalRedis2Store) ListDirectoryEntries(ctx context.Context, dir
|
||||
return lastFileName, err
|
||||
}
|
||||
|
||||
// getEntries pipelines the GETs for up to listFetchBatchSize of members. Each
|
||||
// command keeps its own error, so a missing value still reads as redis.Nil and
|
||||
// fails only that member.
|
||||
func (store *UniversalRedis2Store) getEntries(ctx context.Context, dirPath util.FullPath, members []string) []*redis.StringCmd {
|
||||
if len(members) > listFetchBatchSize {
|
||||
members = members[:listFetchBatchSize]
|
||||
}
|
||||
gets := make([]*redis.StringCmd, len(members))
|
||||
store.Client.Pipelined(ctx, func(pipe redis.Pipeliner) error {
|
||||
for i, fileName := range members {
|
||||
gets[i] = pipe.Get(ctx, store.getKey(string(util.NewFullPath(string(dirPath), fileName))))
|
||||
}
|
||||
return nil
|
||||
})
|
||||
return gets
|
||||
}
|
||||
|
||||
func (store *UniversalRedis2Store) removeOrphanedDirectoryListMember(ctx context.Context, dirPath util.FullPath, fileName string) {
|
||||
// a directory converted to super large after accumulating members still has a legacy index
|
||||
if store.isSuperLargeDirectory(string(dirPath)) {
|
||||
|
||||
@@ -256,6 +256,95 @@ func TestListDirectoryEntriesDeletesLogicallyExpiredEntries(t *testing.T) {
|
||||
}
|
||||
}
|
||||
|
||||
func TestListDirectoryEntriesSkipsMissingAndExpiredMembersInOrder(t *testing.T) {
|
||||
for _, keyPrefix := range []string{"", "sw:"} {
|
||||
t.Run("keyPrefix="+keyPrefix, func(t *testing.T) {
|
||||
store, dir := newTestStore(t, keyPrefix)
|
||||
|
||||
insertTestEntry(t, store, dir.Child("a"), 0)
|
||||
insertTestEntry(t, store, dir.Child("b"), 0)
|
||||
insertLogicallyExpiredTestEntry(t, store, dir.Child("c"))
|
||||
insertTestEntry(t, store, dir.Child("d"), 0)
|
||||
|
||||
if err := store.Client.Del(context.Background(), store.getKey(string(dir.Child("b")))).Err(); err != nil {
|
||||
t.Fatalf("drop value key: %v", err)
|
||||
}
|
||||
|
||||
if names := listNames(t, store, dir); fmt.Sprint(names) != "[a d]" {
|
||||
t.Fatalf("listed %v, want [a d]", names)
|
||||
}
|
||||
|
||||
if members := indexMembers(t, store, dir); fmt.Sprint(members) != "[a d]" {
|
||||
t.Fatalf("directory index holds %v, want [a d]", members)
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
func TestListDirectoryEntriesAcrossFetchBatches(t *testing.T) {
|
||||
store, dir := newTestStore(t, "")
|
||||
|
||||
var want []string
|
||||
for i := 0; i < listFetchBatchSize+2; i++ {
|
||||
name := fmt.Sprintf("f%04d", i)
|
||||
insertTestEntry(t, store, dir.Child(name), 0)
|
||||
if i != listFetchBatchSize {
|
||||
want = append(want, name)
|
||||
}
|
||||
}
|
||||
|
||||
// the first member of the second batch
|
||||
missing := dir.Child(fmt.Sprintf("f%04d", listFetchBatchSize))
|
||||
if err := store.Client.Del(context.Background(), store.getKey(string(missing))).Err(); err != nil {
|
||||
t.Fatalf("drop value key: %v", err)
|
||||
}
|
||||
|
||||
var names []string
|
||||
if _, err := store.ListDirectoryEntries(context.Background(), dir, "", true, 1000, func(entry *filer.Entry) (bool, error) {
|
||||
names = append(names, entry.Name())
|
||||
return true, nil
|
||||
}); err != nil {
|
||||
t.Fatalf("ListDirectoryEntries %s: %v", dir, err)
|
||||
}
|
||||
if fmt.Sprint(names) != fmt.Sprint(want) {
|
||||
t.Fatalf("listed %v, want %v", names, want)
|
||||
}
|
||||
|
||||
if members := indexMembers(t, store, dir); fmt.Sprint(members) != fmt.Sprint(want) {
|
||||
t.Fatalf("directory index holds %v, want %v", members, want)
|
||||
}
|
||||
}
|
||||
|
||||
func TestListDirectoryEntriesStopsAtUnreadableMember(t *testing.T) {
|
||||
store, dir := newTestStore(t, "")
|
||||
ctx := context.Background()
|
||||
|
||||
insertTestEntry(t, store, dir.Child("a"), 0)
|
||||
insertTestEntry(t, store, dir.Child("b"), 0)
|
||||
insertTestEntry(t, store, dir.Child("c"), 0)
|
||||
|
||||
// a value of the wrong type makes its GET fail with something other than redis.Nil
|
||||
key := store.getKey(string(dir.Child("b")))
|
||||
if err := store.Client.Del(ctx, key).Err(); err != nil {
|
||||
t.Fatalf("drop value key: %v", err)
|
||||
}
|
||||
if err := store.Client.HSet(ctx, key, "f", "v").Err(); err != nil {
|
||||
t.Fatalf("plant hash: %v", err)
|
||||
}
|
||||
|
||||
var names []string
|
||||
lastFileName, err := store.ListDirectoryEntries(ctx, dir, "", true, 100, func(entry *filer.Entry) (bool, error) {
|
||||
names = append(names, entry.Name())
|
||||
return true, nil
|
||||
})
|
||||
if err == nil {
|
||||
t.Fatal("ListDirectoryEntries returned no error for an unreadable member")
|
||||
}
|
||||
if fmt.Sprint(names) != "[a]" || lastFileName != "b" {
|
||||
t.Fatalf("listed %v up to %q, want [a] up to \"b\"", names, lastFileName)
|
||||
}
|
||||
}
|
||||
|
||||
func TestDeleteExpiredEntryKeepsRecreatedValue(t *testing.T) {
|
||||
store, dir := newTestStore(t, "")
|
||||
|
||||
|
||||
@@ -243,7 +243,7 @@ func (store *RocksDBStore) ListDirectoryPrefixedEntries(ctx context.Context, dir
|
||||
|
||||
directoryPrefix := genDirectoryKeyPrefix(dirPath, prefix)
|
||||
lastFileStart := directoryPrefix
|
||||
if startFileName != "" {
|
||||
if startFileName > prefix {
|
||||
lastFileStart = genDirectoryKeyPrefix(dirPath, startFileName)
|
||||
}
|
||||
|
||||
|
||||
@@ -246,7 +246,7 @@ func (store *TikvStore) ListDirectoryPrefixedEntries(ctx context.Context, dirPat
|
||||
lastFileName := ""
|
||||
directoryPrefix := store.genDirectoryKeyPrefix(dirPath, prefix)
|
||||
lastFileStart := directoryPrefix
|
||||
if startFileName != "" {
|
||||
if startFileName > prefix {
|
||||
lastFileStart = store.genDirectoryKeyPrefix(dirPath, startFileName)
|
||||
}
|
||||
|
||||
|
||||
@@ -799,6 +799,7 @@ message FilerConf {
|
||||
optional bool worm = 14;
|
||||
uint64 worm_grace_period_seconds = 15;
|
||||
uint64 worm_retention_time_seconds = 16;
|
||||
bool inode_index = 17;
|
||||
}
|
||||
repeated PathConf locations = 2;
|
||||
}
|
||||
|
||||
@@ -7121,6 +7121,7 @@ type FilerConf_PathConf struct {
|
||||
Worm *bool `protobuf:"varint,14,opt,name=worm,proto3,oneof" json:"worm,omitempty"`
|
||||
WormGracePeriodSeconds uint64 `protobuf:"varint,15,opt,name=worm_grace_period_seconds,json=wormGracePeriodSeconds,proto3" json:"worm_grace_period_seconds,omitempty"`
|
||||
WormRetentionTimeSeconds uint64 `protobuf:"varint,16,opt,name=worm_retention_time_seconds,json=wormRetentionTimeSeconds,proto3" json:"worm_retention_time_seconds,omitempty"`
|
||||
InodeIndex bool `protobuf:"varint,17,opt,name=inode_index,json=inodeIndex,proto3" json:"inode_index,omitempty"`
|
||||
unknownFields protoimpl.UnknownFields
|
||||
sizeCache protoimpl.SizeCache
|
||||
}
|
||||
@@ -7267,6 +7268,13 @@ func (x *FilerConf_PathConf) GetWormRetentionTimeSeconds() uint64 {
|
||||
return 0
|
||||
}
|
||||
|
||||
func (x *FilerConf_PathConf) GetInodeIndex() bool {
|
||||
if x != nil {
|
||||
return x.InodeIndex
|
||||
}
|
||||
return false
|
||||
}
|
||||
|
||||
var File_filer_proto protoreflect.FileDescriptor
|
||||
|
||||
const file_filer_proto_rawDesc = "" +
|
||||
@@ -7765,10 +7773,10 @@ const file_filer_proto_rawDesc = "" +
|
||||
"\x03key\x18\x01 \x01(\fR\x03key\x12\x14\n" +
|
||||
"\x05value\x18\x02 \x01(\fR\x05value\"%\n" +
|
||||
"\rKvPutResponse\x12\x14\n" +
|
||||
"\x05error\x18\x01 \x01(\tR\x05error\"\xcf\x05\n" +
|
||||
"\x05error\x18\x01 \x01(\tR\x05error\"\xf0\x05\n" +
|
||||
"\tFilerConf\x12\x18\n" +
|
||||
"\aversion\x18\x01 \x01(\x05R\aversion\x12:\n" +
|
||||
"\tlocations\x18\x02 \x03(\v2\x1c.filer_pb.FilerConf.PathConfR\tlocations\x1a\xeb\x04\n" +
|
||||
"\tlocations\x18\x02 \x03(\v2\x1c.filer_pb.FilerConf.PathConfR\tlocations\x1a\x8c\x05\n" +
|
||||
"\bPathConf\x12'\n" +
|
||||
"\x0flocation_prefix\x18\x01 \x01(\tR\x0elocationPrefix\x12\x1e\n" +
|
||||
"\n" +
|
||||
@@ -7789,7 +7797,9 @@ const file_filer_proto_rawDesc = "" +
|
||||
"\x16disable_chunk_deletion\x18\r \x01(\bR\x14disableChunkDeletion\x12\x17\n" +
|
||||
"\x04worm\x18\x0e \x01(\bH\x01R\x04worm\x88\x01\x01\x129\n" +
|
||||
"\x19worm_grace_period_seconds\x18\x0f \x01(\x04R\x16wormGracePeriodSeconds\x12=\n" +
|
||||
"\x1bworm_retention_time_seconds\x18\x10 \x01(\x04R\x18wormRetentionTimeSecondsB\b\n" +
|
||||
"\x1bworm_retention_time_seconds\x18\x10 \x01(\x04R\x18wormRetentionTimeSeconds\x12\x1f\n" +
|
||||
"\vinode_index\x18\x11 \x01(\bR\n" +
|
||||
"inodeIndexB\b\n" +
|
||||
"\x06_fsyncB\a\n" +
|
||||
"\x05_worm\"\xba\x01\n" +
|
||||
"&CacheRemoteObjectToLocalClusterRequest\x12\x1c\n" +
|
||||
|
||||
@@ -4921,6 +4921,18 @@ func (m *FilerConf_PathConf) MarshalToSizedBufferVT(dAtA []byte) (int, error) {
|
||||
i -= len(m.unknownFields)
|
||||
copy(dAtA[i:], m.unknownFields)
|
||||
}
|
||||
if m.InodeIndex {
|
||||
i--
|
||||
if m.InodeIndex {
|
||||
dAtA[i] = 1
|
||||
} else {
|
||||
dAtA[i] = 0
|
||||
}
|
||||
i--
|
||||
dAtA[i] = 0x1
|
||||
i--
|
||||
dAtA[i] = 0x88
|
||||
}
|
||||
if m.WormRetentionTimeSeconds != 0 {
|
||||
i = protohelpers.EncodeVarint(dAtA, i, uint64(m.WormRetentionTimeSeconds))
|
||||
i--
|
||||
@@ -8550,6 +8562,9 @@ func (m *FilerConf_PathConf) SizeVT() (n int) {
|
||||
if m.WormRetentionTimeSeconds != 0 {
|
||||
n += 2 + protohelpers.SizeOfVarint(uint64(m.WormRetentionTimeSeconds))
|
||||
}
|
||||
if m.InodeIndex {
|
||||
n += 3
|
||||
}
|
||||
n += len(m.unknownFields)
|
||||
return n
|
||||
}
|
||||
@@ -22531,6 +22546,26 @@ func (m *FilerConf_PathConf) UnmarshalVT(dAtA []byte) error {
|
||||
break
|
||||
}
|
||||
}
|
||||
case 17:
|
||||
if wireType != 0 {
|
||||
return fmt.Errorf("proto: wrong wireType = %d for field InodeIndex", wireType)
|
||||
}
|
||||
var v int
|
||||
for shift := uint(0); ; shift += 7 {
|
||||
if shift >= 64 {
|
||||
return protohelpers.ErrIntOverflow
|
||||
}
|
||||
if iNdEx >= l {
|
||||
return io.ErrUnexpectedEOF
|
||||
}
|
||||
b := dAtA[iNdEx]
|
||||
iNdEx++
|
||||
v |= int(b&0x7F) << shift
|
||||
if b < 0x80 {
|
||||
break
|
||||
}
|
||||
}
|
||||
m.InodeIndex = bool(v != 0)
|
||||
default:
|
||||
iNdEx = preIndex
|
||||
skippy, err := protohelpers.Skip(dAtA[iNdEx:])
|
||||
|
||||
@@ -9,6 +9,7 @@ import (
|
||||
"net/url"
|
||||
"os"
|
||||
"reflect"
|
||||
"sort"
|
||||
"strings"
|
||||
|
||||
"github.com/aws/aws-sdk-go/aws"
|
||||
@@ -25,6 +26,7 @@ import (
|
||||
"github.com/seaweedfs/seaweedfs/weed/pb/filer_pb"
|
||||
"github.com/seaweedfs/seaweedfs/weed/pb/remote_pb"
|
||||
"github.com/seaweedfs/seaweedfs/weed/remote_storage"
|
||||
"github.com/seaweedfs/seaweedfs/weed/s3api/s3_constants"
|
||||
"github.com/seaweedfs/seaweedfs/weed/util"
|
||||
"github.com/seaweedfs/seaweedfs/weed/util/version"
|
||||
)
|
||||
@@ -567,18 +569,12 @@ func (s *s3RemoteStorageClient) WriteFile(loc *remote_pb.RemoteStorageLocation,
|
||||
u.Concurrency = s.uploadConcurrency()
|
||||
})
|
||||
|
||||
// process tagging
|
||||
tags := ""
|
||||
var awsTags *string
|
||||
// openstack swift doesn't support s3 object tagging
|
||||
if s.conf.S3SupportTagging {
|
||||
for k, v := range entry.Extended {
|
||||
if len(tags) > 0 {
|
||||
tags = tags + "&"
|
||||
}
|
||||
tags = tags + k + "=" + string(v)
|
||||
if tagging := toTagging(entry.Extended); len(tagging.TagSet) > 0 {
|
||||
awsTags = aws.String(s3TaggingHeader(tagging.TagSet))
|
||||
}
|
||||
awsTags = aws.String(tags)
|
||||
}
|
||||
|
||||
// Upload the file to S3.
|
||||
@@ -609,14 +605,23 @@ func (s *s3RemoteStorageClient) WriteFile(loc *remote_pb.RemoteStorageLocation,
|
||||
|
||||
}
|
||||
|
||||
// toTagging maps the entry's extended attributes to the remote object's tag
|
||||
// set. Only X-Amz-Tagging-* attributes are S3 tags; the rest of Extended is
|
||||
// Seaweed-internal metadata whose values are not legal tag values.
|
||||
func toTagging(attributes map[string][]byte) *s3.Tagging {
|
||||
tagging := &s3.Tagging{}
|
||||
for k, v := range attributes {
|
||||
if !strings.HasPrefix(k, s3_constants.AmzObjectTaggingPrefix) {
|
||||
continue
|
||||
}
|
||||
tagging.TagSet = append(tagging.TagSet, &s3.Tag{
|
||||
Key: aws.String(k),
|
||||
Key: aws.String(k[len(s3_constants.AmzObjectTaggingPrefix):]),
|
||||
Value: aws.String(string(v)),
|
||||
})
|
||||
}
|
||||
sort.Slice(tagging.TagSet, func(i, j int) bool {
|
||||
return aws.StringValue(tagging.TagSet[i].Key) < aws.StringValue(tagging.TagSet[j].Key)
|
||||
})
|
||||
return tagging
|
||||
}
|
||||
|
||||
@@ -707,6 +712,9 @@ func (s *s3RemoteStorageClient) UpdateFileMetadata(loc *remote_pb.RemoteStorageL
|
||||
}
|
||||
|
||||
tagging := toTagging(newEntry.Extended)
|
||||
if reflect.DeepEqual(tagging.TagSet, toTagging(oldEntry.Extended).TagSet) {
|
||||
return
|
||||
}
|
||||
if len(tagging.TagSet) > 0 {
|
||||
_, err = s.conn.PutObjectTagging(&s3.PutObjectTaggingInput{
|
||||
Bucket: aws.String(loc.Bucket),
|
||||
|
||||
@@ -364,7 +364,7 @@ func TestS3UpdateFileMetadataSkipsTaggingWhenUnsupported(t *testing.T) {
|
||||
client, rt := newRecordingS3Client(t, false)
|
||||
loc := &remote_pb.RemoteStorageLocation{Name: "test", Bucket: "bucket", Path: "/dir/file.bin"}
|
||||
plain := &filer_pb.Entry{}
|
||||
tagged := &filer_pb.Entry{Extended: map[string][]byte{"k1": []byte("v1")}}
|
||||
tagged := &filer_pb.Entry{Extended: map[string][]byte{"X-Amz-Tagging-k1": []byte("v1")}}
|
||||
|
||||
_, err := client.UpdateFileMetadata(loc, plain, tagged)
|
||||
require.NoError(t, err)
|
||||
@@ -375,11 +375,22 @@ func TestS3UpdateFileMetadataSkipsTaggingWhenUnsupported(t *testing.T) {
|
||||
require.Empty(t, rt.requests, "removing attributes must not send DeleteObjectTagging")
|
||||
}
|
||||
|
||||
func TestS3UpdateFileMetadataIgnoresNonTagAttributes(t *testing.T) {
|
||||
client, rt := newRecordingS3Client(t, true)
|
||||
loc := &remote_pb.RemoteStorageLocation{Name: "test", Bucket: "bucket", Path: "/dir/file.bin"}
|
||||
old := &filer_pb.Entry{Extended: map[string][]byte{"k1": []byte("v1")}}
|
||||
updated := &filer_pb.Entry{Extended: map[string][]byte{"k1": []byte("v2")}}
|
||||
|
||||
_, err := client.UpdateFileMetadata(loc, old, updated)
|
||||
require.NoError(t, err)
|
||||
require.Empty(t, rt.requests, "non-tag attribute changes must not send tagging calls")
|
||||
}
|
||||
|
||||
func TestS3UpdateFileMetadataSendsTaggingWhenSupported(t *testing.T) {
|
||||
client, rt := newRecordingS3Client(t, true)
|
||||
loc := &remote_pb.RemoteStorageLocation{Name: "test", Bucket: "bucket", Path: "/dir/file.bin"}
|
||||
plain := &filer_pb.Entry{}
|
||||
tagged := &filer_pb.Entry{Extended: map[string][]byte{"k1": []byte("v1")}}
|
||||
tagged := &filer_pb.Entry{Extended: map[string][]byte{"X-Amz-Tagging-k1": []byte("v1")}}
|
||||
|
||||
_, err := client.UpdateFileMetadata(loc, plain, tagged)
|
||||
require.NoError(t, err)
|
||||
@@ -395,6 +406,49 @@ func TestS3UpdateFileMetadataSendsTaggingWhenSupported(t *testing.T) {
|
||||
require.Contains(t, rt.requests[0].URL.RawQuery, "tagging")
|
||||
}
|
||||
|
||||
func TestS3WriteFileSendsOnlyTagAttributes(t *testing.T) {
|
||||
rt := &captureRoundTripper{}
|
||||
client := newS3ClientWithTransport(t, rt, true)
|
||||
loc := &remote_pb.RemoteStorageLocation{
|
||||
Name: "test",
|
||||
Bucket: "bucket",
|
||||
Path: "/dir/test.bin",
|
||||
}
|
||||
entry := &filer_pb.Entry{
|
||||
Extended: map[string][]byte{
|
||||
"X-Amz-Tagging-a b": []byte("x=y"),
|
||||
"Seaweed-X-Amz-Acl": []byte(`[{"Permission":"FULL_CONTROL"}]`),
|
||||
"Seaweed-X-Amz-ETag": []byte("etag-1"),
|
||||
"Cache-Control": []byte("no-cache"),
|
||||
},
|
||||
}
|
||||
|
||||
_, err := client.WriteFile(loc, entry, bytes.NewReader([]byte("data")))
|
||||
require.NoError(t, err)
|
||||
|
||||
require.NotNil(t, rt.uploadReq, "uploader should have issued a PUT")
|
||||
require.Equal(t, "a%20b=x%3Dy", rt.uploadReq.Header.Get("X-Amz-Tagging"))
|
||||
}
|
||||
|
||||
func TestS3WriteFileOmitsTaggingWhenNoTagAttributes(t *testing.T) {
|
||||
rt := &captureRoundTripper{}
|
||||
client := newS3ClientWithTransport(t, rt, true)
|
||||
loc := &remote_pb.RemoteStorageLocation{
|
||||
Name: "test",
|
||||
Bucket: "bucket",
|
||||
Path: "/dir/test.bin",
|
||||
}
|
||||
entry := &filer_pb.Entry{
|
||||
Extended: map[string][]byte{"Seaweed-X-Amz-Acl": []byte(`[{"Permission":"FULL_CONTROL"}]`)},
|
||||
}
|
||||
|
||||
_, err := client.WriteFile(loc, entry, bytes.NewReader([]byte("data")))
|
||||
require.NoError(t, err)
|
||||
|
||||
require.NotNil(t, rt.uploadReq, "uploader should have issued a PUT")
|
||||
require.Empty(t, rt.uploadReq.Header.Get("X-Amz-Tagging"))
|
||||
}
|
||||
|
||||
func TestS3WriteFileOmitsContentTypeWhenMimeMissing(t *testing.T) {
|
||||
client, rt := newCapturingS3Client(t)
|
||||
loc := &remote_pb.RemoteStorageLocation{
|
||||
|
||||
@@ -139,7 +139,7 @@ func (s3a *S3ApiServer) PostPolicyBucketHandler(w http.ResponseWriter, r *http.R
|
||||
// Forward validated POST form fields to the underlying PUT as headers.
|
||||
applyPostPolicyFormHeaders(r, formValues)
|
||||
|
||||
// Authorize the object like the PUT path; the coarse Write check above does not consult the bucket policy.
|
||||
// Authorize the object like the PUT path.
|
||||
if errCode := s3a.iam.AuthorizeObjectWrite(r, identity, bucket, object); errCode != s3err.ErrNone {
|
||||
s3err.WriteErrorResponse(w, r, refineDenialForBucketName(r, identity, errCode))
|
||||
return
|
||||
@@ -382,25 +382,15 @@ func getRedirectPostRawQuery(bucket, key, etag string) string {
|
||||
}
|
||||
|
||||
// Check to see if Policy is signed correctly, returning the signing identity.
|
||||
// The verified form's access key is recorded in ctx's identity holder. The
|
||||
// identity's Write permission on the bucket is checked only once the signature
|
||||
// verifies, as on the header-signed path, so a form that fails verification
|
||||
// learns nothing about the key's permissions. A verified identity that may not
|
||||
// write is returned with ErrAccessDenied.
|
||||
// The verified form's access key is recorded in ctx's identity holder.
|
||||
// Write authorization happens later in the handler via AuthorizeObjectWrite,
|
||||
// which evaluates the identity's attached IAM policies, session policies, and
|
||||
// the bucket policy against the real object key; gating here on the legacy
|
||||
// Actions projection would deny identities those mechanisms allow.
|
||||
func (iam *IdentityAccessManagement) doesPolicySignatureMatch(ctx context.Context, formValues http.Header) (*Identity, s3err.ErrorCode) {
|
||||
var identity *Identity
|
||||
var errCode s3err.ErrorCode
|
||||
// For SignV2 - Signature field will be valid
|
||||
if _, ok := formValues["Signature"]; ok {
|
||||
identity, errCode = iam.doesPolicySignatureV2Match(ctx, formValues)
|
||||
} else {
|
||||
identity, errCode = iam.doesPolicySignatureV4Match(ctx, formValues)
|
||||
return iam.doesPolicySignatureV2Match(ctx, formValues)
|
||||
}
|
||||
if errCode != s3err.ErrNone {
|
||||
return nil, errCode
|
||||
}
|
||||
if !identity.CanDo(s3_constants.ACTION_WRITE, formValues.Get("bucket"), "") {
|
||||
return identity, s3err.ErrAccessDenied
|
||||
}
|
||||
return identity, s3err.ErrNone
|
||||
return iam.doesPolicySignatureV4Match(ctx, formValues)
|
||||
}
|
||||
@@ -32,6 +32,11 @@ const (
|
||||
postAclAccessKey = "AKIAACLTESTTEST"
|
||||
postAclSecretKey = "secret-key-for-acl-tests"
|
||||
postAclPrincipal = "arn:aws:iam::000000000000:user/aclwriter"
|
||||
|
||||
postPolicyAccessKey = "AKIAPOLICYONLY0000"
|
||||
postPolicySecretKey = "secret-key-for-policy-only"
|
||||
postReadAccessKey = "AKIAPOLICYREAD0000"
|
||||
postReadSecretKey = "secret-key-for-policy-read"
|
||||
)
|
||||
|
||||
// newPostAuthzServer builds an S3ApiServer whose non-admin "tester" identity
|
||||
@@ -274,3 +279,118 @@ func TestAuthorizeObjectAclWrite(t *testing.T) {
|
||||
s3a.iam.AuthorizeObjectAclWrite(req, admin, postAuthzBucket, "acl-denied/secret.txt"),
|
||||
"an admin is not confined by the bucket policy")
|
||||
}
|
||||
|
||||
// newPostPolicyOnlyServer builds an S3ApiServer with identities that hold no
|
||||
// legacy Write action: "poster" carries an attached IAM policy allowing
|
||||
// s3:PutObject on the test bucket, and "reader" one allowing only s3:GetObject.
|
||||
func newPostPolicyOnlyServer(t *testing.T) *S3ApiServer {
|
||||
t.Helper()
|
||||
|
||||
iam := &IdentityAccessManagement{
|
||||
hashes: make(map[string]*sync.Pool),
|
||||
hashCounters: make(map[string]*int32),
|
||||
}
|
||||
err := iam.loadS3ApiConfiguration(&iam_pb.S3ApiConfiguration{
|
||||
Identities: []*iam_pb.Identity{{
|
||||
Name: "poster",
|
||||
Account: &iam_pb.Account{Id: "000000000000", DisplayName: "poster"},
|
||||
Credentials: []*iam_pb.Credential{{AccessKey: postPolicyAccessKey, SecretKey: postPolicySecretKey}},
|
||||
Actions: []string{"List:" + postAuthzBucket},
|
||||
PolicyNames: []string{"post-write"},
|
||||
}, {
|
||||
Name: "reader",
|
||||
Account: &iam_pb.Account{Id: "000000000000", DisplayName: "reader"},
|
||||
Credentials: []*iam_pb.Credential{{AccessKey: postReadAccessKey, SecretKey: postReadSecretKey}},
|
||||
Actions: []string{"Read:" + postAuthzBucket, "List:" + postAuthzBucket},
|
||||
PolicyNames: []string{"post-read"},
|
||||
}},
|
||||
Policies: []*iam_pb.Policy{{
|
||||
Name: "post-write",
|
||||
Content: fmt.Sprintf(`{
|
||||
"Version": "2012-10-17",
|
||||
"Statement": [{
|
||||
"Effect": "Allow",
|
||||
"Action": ["s3:PutObject", "s3:GetObject", "s3:DeleteObject"],
|
||||
"Resource": ["arn:aws:s3:::%s/*"]
|
||||
}, {
|
||||
"Effect": "Allow",
|
||||
"Action": ["s3:ListBucket", "s3:GetBucketLocation"],
|
||||
"Resource": ["arn:aws:s3:::%s"]
|
||||
}]
|
||||
}`, postAuthzBucket, postAuthzBucket),
|
||||
}, {
|
||||
Name: "post-read",
|
||||
Content: fmt.Sprintf(`{
|
||||
"Version": "2012-10-17",
|
||||
"Statement": [{
|
||||
"Effect": "Allow",
|
||||
"Action": ["s3:GetObject"],
|
||||
"Resource": ["arn:aws:s3:::%s/*"]
|
||||
}]
|
||||
}`, postAuthzBucket),
|
||||
}},
|
||||
})
|
||||
require.NoError(t, err)
|
||||
|
||||
s3a := &S3ApiServer{
|
||||
option: &S3ApiServerOption{BucketsPath: "/buckets"},
|
||||
iam: iam,
|
||||
}
|
||||
s3a.bucketRegistry = NewBucketRegistry(s3a)
|
||||
s3a.bucketRegistry.setMetadataCache(&BucketMetaData{Name: postAuthzBucket, IsTableBucket: false})
|
||||
return s3a
|
||||
}
|
||||
|
||||
// postFormValues canonicalizes the signed request's multipart fields the way
|
||||
// extractPostPolicyFormValues does, for driving doesPolicySignatureMatch.
|
||||
func postFormValues(t *testing.T, r *http.Request) http.Header {
|
||||
t.Helper()
|
||||
require.NoError(t, r.ParseMultipartForm(1<<20))
|
||||
formValues := make(http.Header)
|
||||
for k, v := range r.MultipartForm.Value {
|
||||
formValues[http.CanonicalHeaderKey(k)] = v
|
||||
}
|
||||
return formValues
|
||||
}
|
||||
|
||||
// TestPostPolicySignatureAllowsAttachedPolicyIdentity signs a POST Object form
|
||||
// with a key whose holder has no legacy Write action - only an attached IAM
|
||||
// policy granting s3:PutObject - and asserts the signature check accepts it
|
||||
// and the shared object-write authorization allows the key. Gating the
|
||||
// signature check on the Actions projection used to fail this with
|
||||
// AccessDenied before authorization ever ran.
|
||||
func TestPostPolicySignatureAllowsAttachedPolicyIdentity(t *testing.T) {
|
||||
s3a := newPostPolicyOnlyServer(t)
|
||||
|
||||
req := newSignedPostRequestFor(t, "uploads/pic.png", postPolicyAccessKey, postPolicySecretKey, nil)
|
||||
identity, errCode := s3a.iam.doesPolicySignatureMatch(req.Context(), postFormValues(t, req))
|
||||
require.Equal(t, s3err.ErrNone, errCode,
|
||||
"a verified POST signature must not require the legacy Write action")
|
||||
require.NotNil(t, identity)
|
||||
|
||||
assert.Equal(t, s3err.ErrNone,
|
||||
s3a.iam.AuthorizeObjectWrite(req, identity, postAuthzBucket, "uploads/pic.png"),
|
||||
"an attached IAM policy granting s3:PutObject must authorize the key like the PUT path")
|
||||
}
|
||||
|
||||
// TestPostPolicyAttachedPolicyDeny covers the deny leg: a verified identity
|
||||
// whose attached policy does not grant s3:PutObject is still refused by
|
||||
// AuthorizeObjectWrite and by the full handler.
|
||||
func TestPostPolicyAttachedPolicyDeny(t *testing.T) {
|
||||
s3a := newPostPolicyOnlyServer(t)
|
||||
|
||||
req := newSignedPostRequestFor(t, "uploads/pic.png", postReadAccessKey, postReadSecretKey, nil)
|
||||
identity, errCode := s3a.iam.doesPolicySignatureMatch(req.Context(), postFormValues(t, req))
|
||||
require.Equal(t, s3err.ErrNone, errCode)
|
||||
require.NotNil(t, identity)
|
||||
|
||||
assert.Equal(t, s3err.ErrAccessDenied,
|
||||
s3a.iam.AuthorizeObjectWrite(req, identity, postAuthzBucket, "uploads/pic.png"),
|
||||
"an attached policy without s3:PutObject must still deny the upload")
|
||||
|
||||
rec := httptest.NewRecorder()
|
||||
s3a.PostPolicyBucketHandler(rec, newSignedPostRequestFor(t, "uploads/pic.png", postReadAccessKey, postReadSecretKey, nil))
|
||||
assert.Equal(t, http.StatusForbidden, rec.Code,
|
||||
"POST from a read-only identity must stay AccessDenied, body: %s", rec.Body.String())
|
||||
assert.Contains(t, rec.Body.String(), "AccessDenied")
|
||||
}
|
||||
@@ -0,0 +1,181 @@
|
||||
package shell
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"context"
|
||||
"errors"
|
||||
"flag"
|
||||
"fmt"
|
||||
"io"
|
||||
"strings"
|
||||
"sync/atomic"
|
||||
|
||||
"github.com/seaweedfs/seaweedfs/weed/filer"
|
||||
"github.com/seaweedfs/seaweedfs/weed/pb/filer_pb"
|
||||
"github.com/seaweedfs/seaweedfs/weed/util"
|
||||
"google.golang.org/protobuf/proto"
|
||||
)
|
||||
|
||||
func init() {
|
||||
Commands = append(Commands, &commandNfsDisable{})
|
||||
}
|
||||
|
||||
type commandNfsDisable struct {
|
||||
}
|
||||
|
||||
func (c *commandNfsDisable) Name() string {
|
||||
return "nfs.disable"
|
||||
}
|
||||
|
||||
func (c *commandNfsDisable) Help() string {
|
||||
return `disable NFS export support for a filer path
|
||||
|
||||
# preview the filer.conf change
|
||||
nfs.disable -path=/exports
|
||||
|
||||
# persist it and delete the inode index rows under the path
|
||||
nfs.disable -path=/exports -apply
|
||||
|
||||
Clears the inode_index mark that nfs.enable set on the path, then
|
||||
walks the entries under it and removes their inode->path index rows,
|
||||
so disabling leaves no index data behind. Pass -keepIndex to skip
|
||||
the cleanup walk.
|
||||
`
|
||||
}
|
||||
|
||||
func (c *commandNfsDisable) HasTag(CommandTag) bool {
|
||||
return false
|
||||
}
|
||||
|
||||
func (c *commandNfsDisable) Do(args []string, commandEnv *CommandEnv, writer io.Writer) (err error) {
|
||||
|
||||
nfsDisableCommand := flag.NewFlagSet(c.Name(), flag.ContinueOnError)
|
||||
path := nfsDisableCommand.String("path", "", "filer path that nfs.enable was applied to")
|
||||
apply := nfsDisableCommand.Bool("apply", false, "update filer.conf and delete the inode index rows")
|
||||
keepIndex := nfsDisableCommand.Bool("keepIndex", false, "keep existing inode index rows")
|
||||
if err = nfsDisableCommand.Parse(args); err != nil {
|
||||
return nil
|
||||
}
|
||||
if !strings.HasPrefix(*path, "/") {
|
||||
return fmt.Errorf("-path must be an absolute filer path")
|
||||
}
|
||||
|
||||
fc, err := filer.ReadFilerConf(commandEnv.option.FilerAddress, commandEnv.option.GrpcDialOption, commandEnv.MasterClient)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
if locConf, found := fc.GetLocationConf(*path); found && locConf.InodeIndex {
|
||||
locConf = filer.ClonePathConf(locConf)
|
||||
locConf.InodeIndex = false
|
||||
if proto.Equal(locConf, &filer_pb.FilerConf_PathConf{LocationPrefix: *path}) {
|
||||
fc.DeleteLocationConf(*path)
|
||||
} else if err = fc.SetLocationConf(locConf); err != nil {
|
||||
return err
|
||||
}
|
||||
} else {
|
||||
fmt.Fprintf(writer, "inode_index is not set on %s in filer.conf\n", *path)
|
||||
}
|
||||
|
||||
var buf bytes.Buffer
|
||||
fc.ToText(&buf)
|
||||
fmt.Fprint(writer, buf.String())
|
||||
fmt.Fprintln(writer)
|
||||
|
||||
if !*apply {
|
||||
infoAboutSimulationMode(writer, *apply, "-apply")
|
||||
return nil
|
||||
}
|
||||
|
||||
if err = commandEnv.WithFilerClient(false, func(client filer_pb.SeaweedFilerClient) error {
|
||||
return filer.SaveInsideFiler(context.Background(), client, filer.DirectoryEtcSeaweedFS, filer.FilerConfName, buf.Bytes())
|
||||
}); err != nil && err != filer_pb.ErrNotFound {
|
||||
return err
|
||||
}
|
||||
|
||||
if *keepIndex {
|
||||
return nil
|
||||
}
|
||||
return cleanupInodeIndex(commandEnv, writer, util.FullPath(*path))
|
||||
}
|
||||
|
||||
func cleanupInodeIndex(commandEnv *CommandEnv, writer io.Writer, exportPath util.FullPath) error {
|
||||
var cleared atomic.Int64
|
||||
var locks inodeRowLocks
|
||||
|
||||
if exportPath != "/" {
|
||||
rootEntry, err := lookupPathEntry(commandEnv, exportPath)
|
||||
if err == filer_pb.ErrNotFound {
|
||||
fmt.Fprintf(writer, "%s does not exist; nothing to clean up\n", exportPath)
|
||||
return nil
|
||||
}
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
if err := removeInodeIndexRow(commandEnv, exportPath, rootEntry.GetAttributes().GetInode(), &cleared, &locks); err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
|
||||
err := filer_pb.TraverseBfs(context.Background(), commandEnv, exportPath, func(parentPath util.FullPath, entry *filer_pb.Entry) error {
|
||||
fullPath := util.NewFullPath(string(parentPath), entry.Name)
|
||||
if err := removeInodeIndexRow(commandEnv, fullPath, entry.GetAttributes().GetInode(), &cleared, &locks); err != nil {
|
||||
return err
|
||||
}
|
||||
return nil
|
||||
})
|
||||
if err != nil {
|
||||
return fmt.Errorf("clean up inode index under %s: %v", exportPath, err)
|
||||
}
|
||||
|
||||
fmt.Fprintf(writer, "cleared %d inode index rows\n", cleared.Load())
|
||||
return nil
|
||||
}
|
||||
|
||||
func removeInodeIndexRow(commandEnv *CommandEnv, fullPath util.FullPath, inode uint64, cleared *atomic.Int64, locks *inodeRowLocks) error {
|
||||
if inode == 0 {
|
||||
return nil
|
||||
}
|
||||
key := filer.InodeIndexKey(inode)
|
||||
lock := locks.forInode(inode)
|
||||
lock.Lock()
|
||||
defer lock.Unlock()
|
||||
err := commandEnv.WithFilerClient(false, func(client filer_pb.SeaweedFilerClient) error {
|
||||
resp, kvErr := client.KvGet(context.Background(), &filer_pb.KvGetRequest{Key: key})
|
||||
if kvErr != nil {
|
||||
return kvErr
|
||||
}
|
||||
if resp.GetError() != "" {
|
||||
return errors.New(resp.GetError())
|
||||
}
|
||||
if len(resp.GetValue()) == 0 {
|
||||
return nil
|
||||
}
|
||||
record, decodeErr := filer.DecodeInodeIndexRecord(resp.GetValue())
|
||||
if decodeErr != nil || !record.RemovePath(fullPath) {
|
||||
return nil
|
||||
}
|
||||
// A hard link can keep paths outside the export; only drop the row
|
||||
// once no path remains. An empty KvPut value deletes the key.
|
||||
var value []byte
|
||||
if len(record.Paths) > 0 {
|
||||
var encodeErr error
|
||||
if value, encodeErr = record.Encode(); encodeErr != nil {
|
||||
return encodeErr
|
||||
}
|
||||
}
|
||||
putResp, putErr := client.KvPut(context.Background(), &filer_pb.KvPutRequest{Key: key, Value: value})
|
||||
if putErr != nil {
|
||||
return putErr
|
||||
}
|
||||
if putResp.GetError() != "" {
|
||||
return errors.New(putResp.GetError())
|
||||
}
|
||||
cleared.Add(1)
|
||||
return nil
|
||||
})
|
||||
if err != nil {
|
||||
return fmt.Errorf("clear inode %d for %s: %v", inode, fullPath, err)
|
||||
}
|
||||
return nil
|
||||
}
|
||||
@@ -0,0 +1,236 @@
|
||||
package shell
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"context"
|
||||
"errors"
|
||||
"flag"
|
||||
"fmt"
|
||||
"io"
|
||||
"strings"
|
||||
"sync"
|
||||
"sync/atomic"
|
||||
|
||||
"github.com/seaweedfs/seaweedfs/weed/filer"
|
||||
"github.com/seaweedfs/seaweedfs/weed/pb/filer_pb"
|
||||
"github.com/seaweedfs/seaweedfs/weed/util"
|
||||
)
|
||||
|
||||
func init() {
|
||||
Commands = append(Commands, &commandNfsEnable{})
|
||||
}
|
||||
|
||||
type commandNfsEnable struct {
|
||||
}
|
||||
|
||||
func (c *commandNfsEnable) Name() string {
|
||||
return "nfs.enable"
|
||||
}
|
||||
|
||||
func (c *commandNfsEnable) Help() string {
|
||||
return `enable NFS export support for a filer path
|
||||
|
||||
# preview the filer.conf change
|
||||
nfs.enable -path=/exports
|
||||
|
||||
# persist it and backfill the index for existing entries
|
||||
nfs.enable -path=/exports -apply
|
||||
|
||||
The weed nfs gateway resolves NFS filehandles through an inode->path
|
||||
index. This command marks the path with inode_index in filer.conf, so
|
||||
every filer maintains index rows for entries under it (no restarts
|
||||
needed), then walks the existing entries and writes their rows so
|
||||
pre-existing files are immediately resolvable. Reverse with nfs.disable.
|
||||
`
|
||||
}
|
||||
|
||||
func (c *commandNfsEnable) HasTag(CommandTag) bool {
|
||||
return false
|
||||
}
|
||||
|
||||
func (c *commandNfsEnable) Do(args []string, commandEnv *CommandEnv, writer io.Writer) (err error) {
|
||||
|
||||
nfsEnableCommand := flag.NewFlagSet(c.Name(), flag.ContinueOnError)
|
||||
path := nfsEnableCommand.String("path", "", "filer path to export over NFS")
|
||||
apply := nfsEnableCommand.Bool("apply", false, "update filer.conf and backfill the inode index")
|
||||
if err = nfsEnableCommand.Parse(args); err != nil {
|
||||
return nil
|
||||
}
|
||||
if !strings.HasPrefix(*path, "/") {
|
||||
return fmt.Errorf("-path must be an absolute filer path")
|
||||
}
|
||||
|
||||
fc, err := filer.ReadFilerConf(commandEnv.option.FilerAddress, commandEnv.option.GrpcDialOption, commandEnv.MasterClient)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
if err = fc.AddLocationConf(&filer_pb.FilerConf_PathConf{
|
||||
LocationPrefix: *path,
|
||||
InodeIndex: true,
|
||||
}); err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
var buf bytes.Buffer
|
||||
fc.ToText(&buf)
|
||||
fmt.Fprint(writer, buf.String())
|
||||
fmt.Fprintln(writer)
|
||||
|
||||
if !*apply {
|
||||
infoAboutSimulationMode(writer, *apply, "-apply")
|
||||
return nil
|
||||
}
|
||||
|
||||
if err = commandEnv.WithFilerClient(false, func(client filer_pb.SeaweedFilerClient) error {
|
||||
return filer.SaveInsideFiler(context.Background(), client, filer.DirectoryEtcSeaweedFS, filer.FilerConfName, buf.Bytes())
|
||||
}); err != nil && err != filer_pb.ErrNotFound {
|
||||
return err
|
||||
}
|
||||
|
||||
return backfillInodeIndex(commandEnv, writer, util.FullPath(*path))
|
||||
}
|
||||
|
||||
// inodeRowLocks serializes the read-modify-write of one inode's index row:
|
||||
// TraverseBfs runs its callback on several workers, and hard-linked entries
|
||||
// share an inode, so two rows updates on the same key could otherwise
|
||||
// overwrite each other and drop a path.
|
||||
type inodeRowLocks [256]sync.Mutex
|
||||
|
||||
func (l *inodeRowLocks) forInode(inode uint64) *sync.Mutex {
|
||||
return &l[inode%uint64(len(l))]
|
||||
}
|
||||
|
||||
func backfillInodeIndex(commandEnv *CommandEnv, writer io.Writer, exportPath util.FullPath) error {
|
||||
var indexed, assigned atomic.Int64
|
||||
var locks inodeRowLocks
|
||||
var printMu sync.Mutex
|
||||
|
||||
if exportPath != "/" {
|
||||
rootEntry, err := lookupPathEntry(commandEnv, exportPath)
|
||||
if err == filer_pb.ErrNotFound {
|
||||
fmt.Fprintf(writer, "%s does not exist yet; nothing to backfill\n", exportPath)
|
||||
return nil
|
||||
}
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
changed, err := backfillEntryInodeIndex(commandEnv, exportPath, rootEntry, &assigned, &locks)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
if changed {
|
||||
indexed.Add(1)
|
||||
}
|
||||
}
|
||||
|
||||
err := filer_pb.TraverseBfs(context.Background(), commandEnv, exportPath, func(parentPath util.FullPath, entry *filer_pb.Entry) error {
|
||||
fullPath := util.NewFullPath(string(parentPath), entry.Name)
|
||||
changed, err := backfillEntryInodeIndex(commandEnv, fullPath, entry, &assigned, &locks)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
if changed {
|
||||
if count := indexed.Add(1); count%10000 == 0 {
|
||||
printMu.Lock()
|
||||
fmt.Fprintf(writer, "indexed %d entries ...\n", count)
|
||||
printMu.Unlock()
|
||||
}
|
||||
}
|
||||
return nil
|
||||
})
|
||||
if err != nil {
|
||||
return fmt.Errorf("backfill inode index under %s: %v", exportPath, err)
|
||||
}
|
||||
|
||||
fmt.Fprintf(writer, "indexed %d entries, assigned %d missing inodes\n", indexed.Load(), assigned.Load())
|
||||
return nil
|
||||
}
|
||||
|
||||
func backfillEntryInodeIndex(commandEnv *CommandEnv, fullPath util.FullPath, entry *filer_pb.Entry, assigned *atomic.Int64, locks *inodeRowLocks) (changed bool, err error) {
|
||||
inode := entry.GetAttributes().GetInode()
|
||||
if inode == 0 {
|
||||
// Entry predates filer-assigned inodes: re-save it so the filer
|
||||
// assigns and persists one, then read the assignment back.
|
||||
dir, name := fullPath.DirAndName()
|
||||
if err := commandEnv.WithFilerClient(false, func(client filer_pb.SeaweedFilerClient) error {
|
||||
if _, updateErr := client.UpdateEntry(context.Background(), &filer_pb.UpdateEntryRequest{
|
||||
Directory: dir,
|
||||
Entry: entry,
|
||||
}); updateErr != nil {
|
||||
return updateErr
|
||||
}
|
||||
resp, lookupErr := filer_pb.LookupEntry(context.Background(), client, &filer_pb.LookupDirectoryEntryRequest{
|
||||
Directory: dir,
|
||||
Name: name,
|
||||
})
|
||||
if lookupErr != nil {
|
||||
return lookupErr
|
||||
}
|
||||
inode = resp.Entry.GetAttributes().GetInode()
|
||||
return nil
|
||||
}); err != nil {
|
||||
return false, fmt.Errorf("assign inode for %s: %v", fullPath, err)
|
||||
}
|
||||
if inode == 0 {
|
||||
return false, fmt.Errorf("filer did not assign an inode for %s", fullPath)
|
||||
}
|
||||
assigned.Add(1)
|
||||
}
|
||||
return upsertInodeIndexRow(commandEnv, fullPath, inode, locks.forInode(inode))
|
||||
}
|
||||
|
||||
func lookupPathEntry(commandEnv *CommandEnv, fullPath util.FullPath) (entry *filer_pb.Entry, err error) {
|
||||
dir, name := fullPath.DirAndName()
|
||||
err = commandEnv.WithFilerClient(false, func(client filer_pb.SeaweedFilerClient) error {
|
||||
resp, lookupErr := filer_pb.LookupEntry(context.Background(), client, &filer_pb.LookupDirectoryEntryRequest{
|
||||
Directory: dir,
|
||||
Name: name,
|
||||
})
|
||||
if lookupErr != nil {
|
||||
return lookupErr
|
||||
}
|
||||
entry = resp.Entry
|
||||
return nil
|
||||
})
|
||||
return
|
||||
}
|
||||
|
||||
func upsertInodeIndexRow(commandEnv *CommandEnv, fullPath util.FullPath, inode uint64, lock *sync.Mutex) (changed bool, err error) {
|
||||
key := filer.InodeIndexKey(inode)
|
||||
lock.Lock()
|
||||
defer lock.Unlock()
|
||||
err = commandEnv.WithFilerClient(false, func(client filer_pb.SeaweedFilerClient) error {
|
||||
resp, kvErr := client.KvGet(context.Background(), &filer_pb.KvGetRequest{Key: key})
|
||||
if kvErr != nil {
|
||||
return kvErr
|
||||
}
|
||||
if resp.GetError() != "" {
|
||||
return errors.New(resp.GetError())
|
||||
}
|
||||
record, decodeErr := filer.DecodeInodeIndexRecord(resp.GetValue())
|
||||
if decodeErr != nil {
|
||||
// Unreadable row: rebuild it from this path.
|
||||
record = &filer.InodeIndexRecord{}
|
||||
}
|
||||
if !record.AddPath(fullPath) {
|
||||
return nil
|
||||
}
|
||||
value, encodeErr := record.Encode()
|
||||
if encodeErr != nil {
|
||||
return encodeErr
|
||||
}
|
||||
putResp, putErr := client.KvPut(context.Background(), &filer_pb.KvPutRequest{Key: key, Value: value})
|
||||
if putErr != nil {
|
||||
return putErr
|
||||
}
|
||||
if putResp.GetError() != "" {
|
||||
return errors.New(putResp.GetError())
|
||||
}
|
||||
changed = true
|
||||
return nil
|
||||
})
|
||||
if err != nil {
|
||||
return false, fmt.Errorf("index inode %d for %s: %v", inode, fullPath, err)
|
||||
}
|
||||
return changed, nil
|
||||
}
|
||||
@@ -6,6 +6,7 @@ import (
|
||||
"flag"
|
||||
"fmt"
|
||||
"io"
|
||||
"sync"
|
||||
|
||||
"github.com/seaweedfs/seaweedfs/weed/pb"
|
||||
|
||||
@@ -33,6 +34,9 @@ func (c *commandVolumeConfigureReplication) Help() string {
|
||||
|
||||
This command changes a volume replication value. It should be followed by "volume.fix.replication".
|
||||
|
||||
Up to -maxParallelization volume servers are configured at a time, one volume at a time on each,
|
||||
and replicas of the same volume on different servers take turns.
|
||||
|
||||
`
|
||||
}
|
||||
|
||||
@@ -46,6 +50,7 @@ func (c *commandVolumeConfigureReplication) Do(args []string, commandEnv *Comman
|
||||
volumeIdInt := configureReplicationCommand.Int("volumeId", 0, "the volume id")
|
||||
replicationString := configureReplicationCommand.String("replication", "", "the intended replication value")
|
||||
collectionPattern := configureReplicationCommand.String("collectionPattern", "", "comma-separated collection names, with '*' and '?' wildcards; empty matches all")
|
||||
maxParallelization := configureReplicationCommand.Int("maxParallelization", DefaultMaxParallelization, "run up to X tasks in parallel, whenever possible")
|
||||
if err = configureReplicationCommand.Parse(args); err != nil {
|
||||
return nil
|
||||
}
|
||||
@@ -75,6 +80,19 @@ func (c *commandVolumeConfigureReplication) Do(args []string, commandEnv *Comman
|
||||
return err
|
||||
}
|
||||
|
||||
return configureReplicationOnVolumeServers(commandEnv, topologyInfo, volumeFilter, replicaPlacement.String(), *maxParallelization)
|
||||
}
|
||||
|
||||
func configureReplicationOnVolumeServers(commandEnv *CommandEnv, topologyInfo *master_pb.TopologyInfo, volumeFilter func(*master_pb.VolumeInformationMessage) bool, replication string, maxParallelization int) error {
|
||||
type serverVolumes struct {
|
||||
addr pb.ServerAddress
|
||||
volumeIds []uint32
|
||||
}
|
||||
var servers []serverVolumes
|
||||
// VolumeConfigure unmounts and remounts the volume, so replicas take turns
|
||||
// to keep at least one of them serving.
|
||||
volumeLocks := make(map[uint32]*sync.Mutex)
|
||||
|
||||
// find all data nodes with volumes that needs replication change
|
||||
eachDataNode(topologyInfo, func(dc DataCenterId, rack RackId, dn *master_pb.DataNodeInfo) {
|
||||
var targetVolumeIds []uint32
|
||||
@@ -82,33 +100,45 @@ func (c *commandVolumeConfigureReplication) Do(args []string, commandEnv *Comman
|
||||
for _, v := range diskInfo.VolumeInfos {
|
||||
if volumeFilter(v) {
|
||||
targetVolumeIds = append(targetVolumeIds, v.Id)
|
||||
if volumeLocks[v.Id] == nil {
|
||||
volumeLocks[v.Id] = &sync.Mutex{}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
if len(targetVolumeIds) == 0 {
|
||||
return
|
||||
}
|
||||
err = operation.WithVolumeServerClient(false, pb.NewServerAddressFromDataNode(dn), commandEnv.option.GrpcDialOption, func(volumeServerClient volume_server_pb.VolumeServerClient) error {
|
||||
for _, targetVolumeId := range targetVolumeIds {
|
||||
resp, configureErr := volumeServerClient.VolumeConfigure(context.Background(), &volume_server_pb.VolumeConfigureRequest{
|
||||
VolumeId: targetVolumeId,
|
||||
Replication: replicaPlacement.String(),
|
||||
})
|
||||
if configureErr != nil {
|
||||
return configureErr
|
||||
}
|
||||
if resp.Error != "" {
|
||||
return errors.New(resp.Error)
|
||||
}
|
||||
}
|
||||
return nil
|
||||
})
|
||||
if err != nil {
|
||||
return
|
||||
if len(targetVolumeIds) > 0 {
|
||||
servers = append(servers, serverVolumes{pb.NewServerAddressFromDataNode(dn), targetVolumeIds})
|
||||
}
|
||||
})
|
||||
|
||||
return err
|
||||
ewg := NewErrorWaitGroup(maxParallelization)
|
||||
for _, server := range servers {
|
||||
ewg.Add(func() error {
|
||||
err := operation.WithVolumeServerClient(false, server.addr, commandEnv.option.GrpcDialOption, func(volumeServerClient volume_server_pb.VolumeServerClient) error {
|
||||
for _, targetVolumeId := range server.volumeIds {
|
||||
volumeLocks[targetVolumeId].Lock()
|
||||
resp, configureErr := volumeServerClient.VolumeConfigure(context.Background(), &volume_server_pb.VolumeConfigureRequest{
|
||||
VolumeId: targetVolumeId,
|
||||
Replication: replication,
|
||||
})
|
||||
volumeLocks[targetVolumeId].Unlock()
|
||||
if configureErr != nil {
|
||||
return configureErr
|
||||
}
|
||||
if resp.Error != "" {
|
||||
return errors.New(resp.Error)
|
||||
}
|
||||
}
|
||||
return nil
|
||||
})
|
||||
if err != nil {
|
||||
return fmt.Errorf("volume server %s: %w", server.addr, err)
|
||||
}
|
||||
return nil
|
||||
})
|
||||
}
|
||||
|
||||
return ewg.Wait()
|
||||
}
|
||||
|
||||
func getVolumeFilter(replicaPlacement *super_block.ReplicaPlacement, volumeId uint32, collectionPattern string) (func(message *master_pb.VolumeInformationMessage) bool, error) {
|
||||
|
||||
@@ -0,0 +1,226 @@
|
||||
package shell
|
||||
|
||||
import (
|
||||
"context"
|
||||
"fmt"
|
||||
"maps"
|
||||
"net"
|
||||
"strings"
|
||||
"sync"
|
||||
"testing"
|
||||
"time"
|
||||
|
||||
"github.com/seaweedfs/seaweedfs/weed/pb/master_pb"
|
||||
"github.com/seaweedfs/seaweedfs/weed/pb/volume_server_pb"
|
||||
"google.golang.org/grpc"
|
||||
"google.golang.org/grpc/credentials/insecure"
|
||||
)
|
||||
|
||||
type configureReplicationTestVolumeServer struct {
|
||||
volume_server_pb.UnimplementedVolumeServerServer
|
||||
failure string
|
||||
mu sync.Mutex
|
||||
configured map[uint32]string
|
||||
}
|
||||
|
||||
func (s *configureReplicationTestVolumeServer) VolumeConfigure(_ context.Context, req *volume_server_pb.VolumeConfigureRequest) (*volume_server_pb.VolumeConfigureResponse, error) {
|
||||
s.mu.Lock()
|
||||
defer s.mu.Unlock()
|
||||
if s.configured == nil {
|
||||
s.configured = make(map[uint32]string)
|
||||
}
|
||||
s.configured[req.VolumeId] = req.Replication
|
||||
return &volume_server_pb.VolumeConfigureResponse{Error: s.failure}, nil
|
||||
}
|
||||
|
||||
// configureReplicationOverlapServer can back several volume servers at once and
|
||||
// records whether two calls for the same volume were ever in flight together.
|
||||
type configureReplicationOverlapServer struct {
|
||||
volume_server_pb.UnimplementedVolumeServerServer
|
||||
mu sync.Mutex
|
||||
inFlight map[uint32]int
|
||||
overlapped bool
|
||||
}
|
||||
|
||||
func (s *configureReplicationOverlapServer) VolumeConfigure(_ context.Context, req *volume_server_pb.VolumeConfigureRequest) (*volume_server_pb.VolumeConfigureResponse, error) {
|
||||
s.mu.Lock()
|
||||
s.inFlight[req.VolumeId]++
|
||||
if s.inFlight[req.VolumeId] > 1 {
|
||||
s.overlapped = true
|
||||
}
|
||||
s.mu.Unlock()
|
||||
|
||||
time.Sleep(20 * time.Millisecond)
|
||||
|
||||
s.mu.Lock()
|
||||
s.inFlight[req.VolumeId]--
|
||||
s.mu.Unlock()
|
||||
return &volume_server_pb.VolumeConfigureResponse{}, nil
|
||||
}
|
||||
|
||||
func startConfigureReplicationTestVolumeServer(t *testing.T, server volume_server_pb.VolumeServerServer, volumeIds ...uint32) *master_pb.DataNodeInfo {
|
||||
t.Helper()
|
||||
lis, err := net.Listen("tcp", "127.0.0.1:0")
|
||||
if err != nil {
|
||||
t.Fatalf("listen: %v", err)
|
||||
}
|
||||
srv := grpc.NewServer()
|
||||
volume_server_pb.RegisterVolumeServerServer(srv, server)
|
||||
go srv.Serve(lis)
|
||||
t.Cleanup(srv.Stop)
|
||||
|
||||
port := lis.Addr().(*net.TCPAddr).Port
|
||||
disk := &master_pb.DiskInfo{}
|
||||
for _, vid := range volumeIds {
|
||||
disk.VolumeInfos = append(disk.VolumeInfos, &master_pb.VolumeInformationMessage{Id: vid})
|
||||
}
|
||||
return &master_pb.DataNodeInfo{
|
||||
Id: fmt.Sprintf("127.0.0.1:%d", port),
|
||||
GrpcPort: uint32(port),
|
||||
DiskInfos: map[string]*master_pb.DiskInfo{"": disk},
|
||||
}
|
||||
}
|
||||
|
||||
func configureReplicationTestEnv(dataNodes ...*master_pb.DataNodeInfo) (*CommandEnv, *master_pb.TopologyInfo) {
|
||||
commandEnv := &CommandEnv{option: &ShellOptions{
|
||||
GrpcDialOption: grpc.WithTransportCredentials(insecure.NewCredentials()),
|
||||
}}
|
||||
topologyInfo := &master_pb.TopologyInfo{DataCenterInfos: []*master_pb.DataCenterInfo{{
|
||||
RackInfos: []*master_pb.RackInfo{{DataNodeInfos: dataNodes}},
|
||||
}}}
|
||||
return commandEnv, topologyInfo
|
||||
}
|
||||
|
||||
func allVolumes(*master_pb.VolumeInformationMessage) bool { return true }
|
||||
|
||||
func TestConfigureReplicationOnVolumeServersReportsEveryFailingServer(t *testing.T) {
|
||||
for _, maxParallelization := range []int{1, DefaultMaxParallelization} {
|
||||
t.Run(fmt.Sprintf("maxParallelization=%d", maxParallelization), func(t *testing.T) {
|
||||
first := &configureReplicationTestVolumeServer{failure: "first server failed"}
|
||||
healthy := &configureReplicationTestVolumeServer{}
|
||||
last := &configureReplicationTestVolumeServer{failure: "last server failed"}
|
||||
commandEnv, topologyInfo := configureReplicationTestEnv(
|
||||
startConfigureReplicationTestVolumeServer(t, first, 1),
|
||||
startConfigureReplicationTestVolumeServer(t, healthy, 2, 3),
|
||||
startConfigureReplicationTestVolumeServer(t, last, 4),
|
||||
)
|
||||
|
||||
err := configureReplicationOnVolumeServers(commandEnv, topologyInfo, allVolumes, "001", maxParallelization)
|
||||
|
||||
if err == nil {
|
||||
t.Fatal("expected an error")
|
||||
}
|
||||
for _, failure := range []string{first.failure, last.failure} {
|
||||
if !strings.Contains(err.Error(), failure) {
|
||||
t.Errorf("error %q does not mention %q", err, failure)
|
||||
}
|
||||
}
|
||||
healthy.mu.Lock()
|
||||
defer healthy.mu.Unlock()
|
||||
if expected := map[uint32]string{2: "001", 3: "001"}; !maps.Equal(healthy.configured, expected) {
|
||||
t.Errorf("healthy server configured %v, expected %v", healthy.configured, expected)
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
func TestConfigureReplicationOnVolumeServersConfiguresReplicasOneAtATime(t *testing.T) {
|
||||
server := &configureReplicationOverlapServer{inFlight: make(map[uint32]int)}
|
||||
commandEnv, topologyInfo := configureReplicationTestEnv(
|
||||
startConfigureReplicationTestVolumeServer(t, server, 1, 2),
|
||||
startConfigureReplicationTestVolumeServer(t, server, 1, 2),
|
||||
startConfigureReplicationTestVolumeServer(t, server, 1, 2),
|
||||
)
|
||||
|
||||
if err := configureReplicationOnVolumeServers(commandEnv, topologyInfo, allVolumes, "002", DefaultMaxParallelization); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
|
||||
server.mu.Lock()
|
||||
defer server.mu.Unlock()
|
||||
if server.overlapped {
|
||||
t.Error("replicas of one volume were configured at the same time")
|
||||
}
|
||||
}
|
||||
|
||||
// configureReplicationGateServer blocks inside VolumeConfigure until the test
|
||||
// releases it, so the test can observe how many servers run at once.
|
||||
type configureReplicationGateServer struct {
|
||||
volume_server_pb.UnimplementedVolumeServerServer
|
||||
gate *configureReplicationGate
|
||||
}
|
||||
|
||||
type configureReplicationGate struct {
|
||||
entered chan struct{}
|
||||
release chan struct{}
|
||||
mu sync.Mutex
|
||||
active int
|
||||
maxActive int
|
||||
calls int
|
||||
}
|
||||
|
||||
func (s *configureReplicationGateServer) VolumeConfigure(_ context.Context, _ *volume_server_pb.VolumeConfigureRequest) (*volume_server_pb.VolumeConfigureResponse, error) {
|
||||
s.gate.mu.Lock()
|
||||
s.gate.calls++
|
||||
s.gate.active++
|
||||
if s.gate.active > s.gate.maxActive {
|
||||
s.gate.maxActive = s.gate.active
|
||||
}
|
||||
s.gate.mu.Unlock()
|
||||
|
||||
s.gate.entered <- struct{}{}
|
||||
<-s.gate.release
|
||||
|
||||
s.gate.mu.Lock()
|
||||
s.gate.active--
|
||||
s.gate.mu.Unlock()
|
||||
return &volume_server_pb.VolumeConfigureResponse{}, nil
|
||||
}
|
||||
|
||||
func TestConfigureReplicationOnVolumeServersRunsServersInParallelUpToTheLimit(t *testing.T) {
|
||||
const servers = 4
|
||||
const maxParallelization = 2
|
||||
gate := &configureReplicationGate{
|
||||
entered: make(chan struct{}, servers),
|
||||
release: make(chan struct{}),
|
||||
}
|
||||
var dataNodes []*master_pb.DataNodeInfo
|
||||
for i := 0; i < servers; i++ {
|
||||
dataNodes = append(dataNodes, startConfigureReplicationTestVolumeServer(t,
|
||||
&configureReplicationGateServer{gate: gate}, uint32(i+1)))
|
||||
}
|
||||
commandEnv, topologyInfo := configureReplicationTestEnv(dataNodes...)
|
||||
|
||||
done := make(chan error, 1)
|
||||
go func() {
|
||||
done <- configureReplicationOnVolumeServers(commandEnv, topologyInfo, allVolumes, "001", maxParallelization)
|
||||
}()
|
||||
|
||||
for i := 0; i < maxParallelization; i++ {
|
||||
select {
|
||||
case <-gate.entered:
|
||||
case <-time.After(10 * time.Second):
|
||||
t.Fatalf("only %d servers started; serial execution would hold the second call", i)
|
||||
}
|
||||
}
|
||||
|
||||
select {
|
||||
case <-gate.entered:
|
||||
t.Fatalf("more than %d servers ran while calls were held open", maxParallelization)
|
||||
case <-time.After(100 * time.Millisecond):
|
||||
}
|
||||
|
||||
close(gate.release)
|
||||
if err := <-done; err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
|
||||
gate.mu.Lock()
|
||||
defer gate.mu.Unlock()
|
||||
if gate.maxActive != maxParallelization {
|
||||
t.Errorf("at most %d servers ran together, expected %d", gate.maxActive, maxParallelization)
|
||||
}
|
||||
if gate.calls != servers {
|
||||
t.Errorf("%d servers ran, expected %d", gate.calls, servers)
|
||||
}
|
||||
}
|
||||
@@ -9,6 +9,7 @@ import (
|
||||
"net/url"
|
||||
"strconv"
|
||||
"strings"
|
||||
"sync"
|
||||
"time"
|
||||
|
||||
"github.com/seaweedfs/seaweedfs/weed/glog"
|
||||
@@ -46,8 +47,8 @@ func ReplicatedWrite(ctx context.Context, masterFn operation.GetMasterFn, grpcDi
|
||||
}
|
||||
|
||||
// with no local volume and no replica to forward to, the write would be acknowledged without being stored
|
||||
hasLocalVolume := s.HasVolume(volumeId)
|
||||
if !hasLocalVolume && len(remoteLocations) == 0 {
|
||||
vol := s.GetVolume(volumeId)
|
||||
if vol == nil && len(remoteLocations) == 0 {
|
||||
err = fmt.Errorf("volume %d not found on %s:%d", volumeId, s.Ip, s.Port)
|
||||
glog.V(0).Infoln(err)
|
||||
return
|
||||
@@ -68,109 +69,141 @@ func ReplicatedWrite(ctx context.Context, masterFn operation.GetMasterFn, grpcDi
|
||||
}(time.Now())
|
||||
}
|
||||
|
||||
if hasLocalVolume {
|
||||
start := time.Now()
|
||||
|
||||
inFlightGauge := stats.VolumeServerInFlightRequestsGauge.WithLabelValues(stats.WriteToLocalDisk)
|
||||
inFlightGauge.Inc()
|
||||
defer inFlightGauge.Dec()
|
||||
|
||||
isUnchanged, err = s.WriteVolumeNeedle(volumeId, n, true, fsync)
|
||||
stats.VolumeServerRequestHistogram.WithLabelValues(stats.WriteToLocalDisk).Observe(time.Since(start).Seconds())
|
||||
if err != nil {
|
||||
stats.VolumeServerHandlerCounter.WithLabelValues(stats.ErrorWriteToLocalDisk).Inc()
|
||||
err = fmt.Errorf("failed to write to local disk: %w", err)
|
||||
glog.V(0).Infoln(err)
|
||||
return
|
||||
// Snapshot the fields the fan-out reads before the concurrent local write
|
||||
// mutates the needle (it stamps the volume TTL and flags), so the two cannot
|
||||
// race; the fan-out then touches only these locals plus read-only n.Data.
|
||||
replicaTtl := n.Ttl
|
||||
if vol != nil && replicaTtl == needle.EMPTY_TTL && vol.Ttl != needle.EMPTY_TTL {
|
||||
replicaTtl = vol.Ttl
|
||||
}
|
||||
replicaLastModified := n.LastModified
|
||||
replicaIsChunked := n.IsChunkedManifest()
|
||||
replicaIsCompressed := n.IsCompressed()
|
||||
replicaName := string(n.Name)
|
||||
replicaMime := string(n.Mime)
|
||||
replicaData := n.Data
|
||||
replicaPairMap := make(map[string]string)
|
||||
if replicaCount > 0 && n.HasPairs() {
|
||||
tmpMap := make(map[string]string)
|
||||
if pe := json.Unmarshal(n.Pairs, &tmpMap); pe != nil {
|
||||
stats.VolumeServerHandlerCounter.WithLabelValues(stats.ErrorUnmarshalPairs).Inc()
|
||||
glog.V(0).Infoln("Unmarshal pairs error:", pe)
|
||||
}
|
||||
for k, v := range tmpMap {
|
||||
replicaPairMap[needle.PairNamePrefix+k] = v
|
||||
}
|
||||
}
|
||||
|
||||
// Observe replication targets histogram for all operations (including zero)
|
||||
stats.VolumeServerReplicationTargets.Observe(float64(replicaCount))
|
||||
|
||||
if replicaCount > 0 { //send to other replica locations
|
||||
start := time.Now()
|
||||
// Run the local disk write and the replica fan-out concurrently against the
|
||||
// buffered needle (latency max(local, replicas), not the sum); ack after both.
|
||||
var wg sync.WaitGroup
|
||||
var localErr, remoteErr error
|
||||
var localIsUnchanged bool
|
||||
|
||||
inFlightGauge := stats.VolumeServerInFlightRequestsGauge.WithLabelValues(stats.WriteToReplicas)
|
||||
inFlightGauge.Inc()
|
||||
defer inFlightGauge.Dec()
|
||||
if vol != nil {
|
||||
wg.Add(1)
|
||||
go func() {
|
||||
defer wg.Done()
|
||||
start := time.Now()
|
||||
inFlightGauge := stats.VolumeServerInFlightRequestsGauge.WithLabelValues(stats.WriteToLocalDisk)
|
||||
inFlightGauge.Inc()
|
||||
defer inFlightGauge.Dec()
|
||||
|
||||
err = DistributedOperation(ctx, remoteLocations, func(ctx context.Context, location operation.Location) error {
|
||||
u := url.URL{
|
||||
Scheme: "http",
|
||||
Host: location.Url,
|
||||
Path: r.URL.Path,
|
||||
localIsUnchanged, localErr = s.WriteVolumeNeedle(volumeId, n, true, fsync)
|
||||
stats.VolumeServerRequestHistogram.WithLabelValues(stats.WriteToLocalDisk).Observe(time.Since(start).Seconds())
|
||||
if localErr != nil {
|
||||
stats.VolumeServerHandlerCounter.WithLabelValues(stats.ErrorWriteToLocalDisk).Inc()
|
||||
localErr = fmt.Errorf("failed to write to local disk: %w", localErr)
|
||||
glog.V(0).Infoln(localErr)
|
||||
}
|
||||
q := url.Values{
|
||||
"type": {"replicate"},
|
||||
"ttl": {n.Ttl.String()},
|
||||
}
|
||||
if fsync {
|
||||
q.Set("fsync", "true")
|
||||
}
|
||||
if n.LastModified > 0 {
|
||||
q.Set("ts", strconv.FormatUint(n.LastModified, 10))
|
||||
}
|
||||
if n.IsChunkedManifest() {
|
||||
q.Set("cm", "true")
|
||||
}
|
||||
u.RawQuery = q.Encode()
|
||||
|
||||
pairMap := make(map[string]string)
|
||||
if n.HasPairs() {
|
||||
tmpMap := make(map[string]string)
|
||||
err := json.Unmarshal(n.Pairs, &tmpMap)
|
||||
if err != nil {
|
||||
stats.VolumeServerHandlerCounter.WithLabelValues(stats.ErrorUnmarshalPairs).Inc()
|
||||
glog.V(0).Infoln("Unmarshal pairs error:", err)
|
||||
}
|
||||
for k, v := range tmpMap {
|
||||
pairMap[needle.PairNamePrefix+k] = v
|
||||
}
|
||||
}
|
||||
bytesBuffer := buffer_pool.SyncPoolGetBuffer()
|
||||
defer buffer_pool.SyncPoolPutBuffer(bytesBuffer)
|
||||
|
||||
// volume server do not know about encryption
|
||||
// TODO optimize here to compress data only once
|
||||
uploadOption := &operation.UploadOption{
|
||||
UploadUrl: u.String(),
|
||||
Filename: string(n.Name),
|
||||
Cipher: false,
|
||||
IsInputCompressed: n.IsCompressed(),
|
||||
IsReplication: true,
|
||||
MimeType: string(n.Mime),
|
||||
PairMap: pairMap,
|
||||
Jwt: jwt,
|
||||
Md5: contentMd5,
|
||||
BytesBuffer: bytesBuffer,
|
||||
MaxAttempts: 1, // fail fast on a dead replica; the client write retries
|
||||
}
|
||||
|
||||
uploader, err := operation.NewUploader()
|
||||
if err != nil {
|
||||
glog.Errorf("replication-UploadData, err:%v, url:%s", err, u.String())
|
||||
return err
|
||||
}
|
||||
_, err = uploader.UploadData(ctx, n.Data, uploadOption)
|
||||
if err != nil {
|
||||
glog.Errorf("replication-UploadData, err:%v, url:%s", err, u.String())
|
||||
}
|
||||
return err
|
||||
})
|
||||
stats.VolumeServerRequestHistogram.WithLabelValues(stats.WriteToReplicas).Observe(time.Since(start).Seconds())
|
||||
if err != nil {
|
||||
stats.VolumeServerHandlerCounter.WithLabelValues(stats.ErrorWriteToReplicas).Inc()
|
||||
stats.VolumeServerReplicationCounter.WithLabelValues(stats.ReplicationOpWrite, stats.ReplicationFailure).Inc()
|
||||
reason := classifyReplicationError(err)
|
||||
stats.VolumeServerReplicationFailures.WithLabelValues(stats.ReplicationOpWrite, reason).Inc()
|
||||
err = fmt.Errorf("failed to write to replicas for volume %d: %v", volumeId, err)
|
||||
glog.V(0).Infoln(err)
|
||||
return false, err
|
||||
}
|
||||
stats.VolumeServerReplicationCounter.WithLabelValues(stats.ReplicationOpWrite, stats.ReplicationSuccess).Inc()
|
||||
}()
|
||||
}
|
||||
return
|
||||
|
||||
if replicaCount > 0 {
|
||||
wg.Add(1)
|
||||
go func() {
|
||||
defer wg.Done()
|
||||
start := time.Now()
|
||||
inFlightGauge := stats.VolumeServerInFlightRequestsGauge.WithLabelValues(stats.WriteToReplicas)
|
||||
inFlightGauge.Inc()
|
||||
defer inFlightGauge.Dec()
|
||||
|
||||
remoteErr = DistributedOperation(ctx, remoteLocations, func(ctx context.Context, location operation.Location) error {
|
||||
u := url.URL{
|
||||
Scheme: "http",
|
||||
Host: location.Url,
|
||||
Path: r.URL.Path,
|
||||
}
|
||||
q := url.Values{
|
||||
"type": {"replicate"},
|
||||
"ttl": {replicaTtl.String()},
|
||||
}
|
||||
if fsync {
|
||||
q.Set("fsync", "true")
|
||||
}
|
||||
if replicaLastModified > 0 {
|
||||
q.Set("ts", strconv.FormatUint(replicaLastModified, 10))
|
||||
}
|
||||
if replicaIsChunked {
|
||||
q.Set("cm", "true")
|
||||
}
|
||||
u.RawQuery = q.Encode()
|
||||
|
||||
bytesBuffer := buffer_pool.SyncPoolGetBuffer()
|
||||
defer buffer_pool.SyncPoolPutBuffer(bytesBuffer)
|
||||
|
||||
// volume server do not know about encryption
|
||||
uploadOption := &operation.UploadOption{
|
||||
UploadUrl: u.String(),
|
||||
Filename: replicaName,
|
||||
Cipher: false,
|
||||
IsInputCompressed: replicaIsCompressed,
|
||||
IsReplication: true,
|
||||
MimeType: replicaMime,
|
||||
PairMap: replicaPairMap,
|
||||
Jwt: jwt,
|
||||
Md5: contentMd5,
|
||||
BytesBuffer: bytesBuffer,
|
||||
MaxAttempts: 1, // fail fast on a dead replica; the client write retries
|
||||
}
|
||||
|
||||
uploader, uploaderErr := operation.NewUploader()
|
||||
if uploaderErr != nil {
|
||||
glog.Errorf("replication-UploadData, err:%v, url:%s", uploaderErr, u.String())
|
||||
return uploaderErr
|
||||
}
|
||||
_, uploadErr := uploader.UploadData(ctx, replicaData, uploadOption)
|
||||
if uploadErr != nil {
|
||||
glog.Errorf("replication-UploadData, err:%v, url:%s", uploadErr, u.String())
|
||||
}
|
||||
return uploadErr
|
||||
})
|
||||
stats.VolumeServerRequestHistogram.WithLabelValues(stats.WriteToReplicas).Observe(time.Since(start).Seconds())
|
||||
if remoteErr != nil {
|
||||
stats.VolumeServerHandlerCounter.WithLabelValues(stats.ErrorWriteToReplicas).Inc()
|
||||
stats.VolumeServerReplicationCounter.WithLabelValues(stats.ReplicationOpWrite, stats.ReplicationFailure).Inc()
|
||||
reason := classifyReplicationError(remoteErr)
|
||||
stats.VolumeServerReplicationFailures.WithLabelValues(stats.ReplicationOpWrite, reason).Inc()
|
||||
remoteErr = fmt.Errorf("failed to write to replicas for volume %d: %v", volumeId, remoteErr)
|
||||
glog.V(0).Infoln(remoteErr)
|
||||
} else {
|
||||
stats.VolumeServerReplicationCounter.WithLabelValues(stats.ReplicationOpWrite, stats.ReplicationSuccess).Inc()
|
||||
}
|
||||
}()
|
||||
}
|
||||
|
||||
wg.Wait()
|
||||
|
||||
if localErr != nil {
|
||||
return false, localErr
|
||||
}
|
||||
if remoteErr != nil {
|
||||
return false, remoteErr
|
||||
}
|
||||
return localIsUnchanged, nil
|
||||
}
|
||||
|
||||
// ReplicatedDelete deletes a needle from the local volume and sends delete
|
||||
|
||||
@@ -16,8 +16,11 @@ import (
|
||||
"github.com/seaweedfs/seaweedfs/weed/operation"
|
||||
"github.com/seaweedfs/seaweedfs/weed/pb"
|
||||
"github.com/seaweedfs/seaweedfs/weed/pb/master_pb"
|
||||
"github.com/seaweedfs/seaweedfs/weed/stats"
|
||||
"github.com/seaweedfs/seaweedfs/weed/storage"
|
||||
"github.com/seaweedfs/seaweedfs/weed/storage/needle"
|
||||
"github.com/seaweedfs/seaweedfs/weed/storage/types"
|
||||
"github.com/seaweedfs/seaweedfs/weed/util"
|
||||
"google.golang.org/grpc"
|
||||
"google.golang.org/grpc/credentials/insecure"
|
||||
)
|
||||
@@ -230,6 +233,176 @@ func TestReplicatedWriteForwardsFsyncToReplicas(t *testing.T) {
|
||||
}
|
||||
}
|
||||
|
||||
// newReplicateTestStore builds a single-disk store in dir, draining the notify
|
||||
// channels so volume mounts never block on a heartbeat reader.
|
||||
func newReplicateTestStore(t *testing.T, dir string) *storage.Store {
|
||||
t.Helper()
|
||||
store := storage.NewStore(nil, "127.0.0.1", 8080, 18080, "http://127.0.0.1:8080", "store-id",
|
||||
[]string{dir}, []int32{100}, []util.MinFreeSpace{{}}, "",
|
||||
storage.NeedleMapInMemory, []types.DiskType{types.HardDriveType}, nil, 3,
|
||||
stats.DefaultDiskIOProbeConfig())
|
||||
done := make(chan struct{})
|
||||
go func() {
|
||||
for {
|
||||
select {
|
||||
case <-store.NewVolumesChan:
|
||||
case <-store.DeletedVolumesChan:
|
||||
case <-store.NewEcShardsChan:
|
||||
case <-store.DeletedEcShardsChan:
|
||||
case <-store.StateUpdateChan:
|
||||
case <-done:
|
||||
return
|
||||
}
|
||||
}
|
||||
}()
|
||||
t.Cleanup(func() {
|
||||
close(done)
|
||||
store.Close()
|
||||
})
|
||||
return store
|
||||
}
|
||||
|
||||
// TestReplicatedWriteOverlapsLocalAndReplicaWrites runs the write path with a
|
||||
// real local volume and a replica that stalls mid-request: the fan-out starts
|
||||
// while the local write is still free to run, the local write lands before the
|
||||
// replica unblocks, and the acknowledged write waits for both legs.
|
||||
func TestReplicatedWriteOverlapsLocalAndReplicaWrites(t *testing.T) {
|
||||
replicaStarted := make(chan url.Values, 1)
|
||||
releaseReplica := make(chan struct{})
|
||||
var releaseOnce sync.Once
|
||||
release := func() { releaseOnce.Do(func() { close(releaseReplica) }) }
|
||||
replica := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
||||
replicaStarted <- r.URL.Query()
|
||||
<-releaseReplica
|
||||
w.WriteHeader(http.StatusCreated)
|
||||
if _, err := w.Write([]byte(`{"size":21}`)); err != nil {
|
||||
t.Errorf("replica write response: %v", err)
|
||||
}
|
||||
}))
|
||||
defer replica.Close()
|
||||
defer release()
|
||||
replicaHost := strings.TrimPrefix(replica.URL, "http://")
|
||||
|
||||
masterFn, dialOption := startMockMasterServer(t, &mockMasterServer{
|
||||
locations: []*master_pb.Location{{Url: "127.0.0.1:8080"}, {Url: replicaHost}},
|
||||
})
|
||||
|
||||
store := newReplicateTestStore(t, t.TempDir())
|
||||
volumeId := needle.VolumeId(7)
|
||||
operation.InvalidateVolumeIdLocationCache(volumeId.String())
|
||||
if err := store.AddVolume(volumeId, "", storage.NeedleMapInMemory, "001", "15d", 0, needle.GetCurrentVersion(), 0, types.HardDriveType, 0); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
|
||||
data := []byte("overlapped write data")
|
||||
n := &needle.Needle{
|
||||
Id: 1,
|
||||
Data: data,
|
||||
Checksum: needle.NewCRC(data),
|
||||
Ttl: needle.EMPTY_TTL,
|
||||
}
|
||||
r := httptest.NewRequest(http.MethodPost, "http://127.0.0.1:8080/7,01637037d6", nil)
|
||||
if err := r.ParseForm(); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
|
||||
done := make(chan error, 1)
|
||||
go func() {
|
||||
_, err := ReplicatedWrite(context.Background(), masterFn, dialOption, store, volumeId, n, r, "")
|
||||
done <- err
|
||||
}()
|
||||
|
||||
var q url.Values
|
||||
select {
|
||||
case q = <-replicaStarted:
|
||||
case <-time.After(5 * time.Second):
|
||||
t.Fatal("replica never received the fan-out request")
|
||||
}
|
||||
if got := q.Get("type"); got != "replicate" {
|
||||
t.Errorf("replica type = %q, want replicate", got)
|
||||
}
|
||||
if got := q.Get("ttl"); got != "15d" {
|
||||
t.Errorf("replica ttl = %q, want the inherited volume ttl 15d", got)
|
||||
}
|
||||
|
||||
select {
|
||||
case err := <-done:
|
||||
t.Fatalf("write returned before the replica responded: %v", err)
|
||||
case <-time.After(50 * time.Millisecond):
|
||||
}
|
||||
|
||||
deadline := time.Now().Add(5 * time.Second)
|
||||
for {
|
||||
readBack := &needle.Needle{Id: n.Id}
|
||||
if _, err := store.ReadVolumeNeedle(volumeId, readBack, nil, nil); err == nil && string(readBack.Data) == string(data) {
|
||||
break
|
||||
}
|
||||
if time.Now().After(deadline) {
|
||||
t.Fatal("local write did not land while the replica was still blocked")
|
||||
}
|
||||
time.Sleep(5 * time.Millisecond)
|
||||
}
|
||||
|
||||
release()
|
||||
if err := <-done; err != nil {
|
||||
t.Fatalf("ReplicatedWrite: %v", err)
|
||||
}
|
||||
}
|
||||
|
||||
// TestReplicatedWriteFailsWhenEitherLegFails checks that a failed local write
|
||||
// or a failed replica fan-out fails the whole request even when the other leg
|
||||
// succeeded.
|
||||
func TestReplicatedWriteFailsWhenEitherLegFails(t *testing.T) {
|
||||
for _, tc := range []struct {
|
||||
name string
|
||||
volumeId needle.VolumeId
|
||||
readonlyLocal bool
|
||||
replicaStatus int
|
||||
}{
|
||||
{name: "local write fails", volumeId: 8, readonlyLocal: true, replicaStatus: http.StatusCreated},
|
||||
{name: "replica write fails", volumeId: 9, replicaStatus: http.StatusInternalServerError},
|
||||
} {
|
||||
t.Run(tc.name, func(t *testing.T) {
|
||||
replica := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
||||
w.WriteHeader(tc.replicaStatus)
|
||||
}))
|
||||
defer replica.Close()
|
||||
replicaHost := strings.TrimPrefix(replica.URL, "http://")
|
||||
|
||||
masterFn, dialOption := startMockMasterServer(t, &mockMasterServer{
|
||||
locations: []*master_pb.Location{{Url: "127.0.0.1:8080"}, {Url: replicaHost}},
|
||||
})
|
||||
|
||||
store := newReplicateTestStore(t, t.TempDir())
|
||||
operation.InvalidateVolumeIdLocationCache(tc.volumeId.String())
|
||||
if err := store.AddVolume(tc.volumeId, "", storage.NeedleMapInMemory, "001", "", 0, needle.GetCurrentVersion(), 0, types.HardDriveType, 0); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if tc.readonlyLocal {
|
||||
if err := store.MarkVolumeReadonly(tc.volumeId, false, false); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
}
|
||||
|
||||
data := []byte("write data")
|
||||
n := &needle.Needle{
|
||||
Id: 1,
|
||||
Data: data,
|
||||
Checksum: needle.NewCRC(data),
|
||||
Ttl: needle.EMPTY_TTL,
|
||||
}
|
||||
r := httptest.NewRequest(http.MethodPost, fmt.Sprintf("http://127.0.0.1:8080/%d,01637037d6", tc.volumeId), nil)
|
||||
if err := r.ParseForm(); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
|
||||
if _, err := ReplicatedWrite(context.Background(), masterFn, dialOption, store, tc.volumeId, n, r, ""); err == nil {
|
||||
t.Fatal("ReplicatedWrite succeeded despite a failed leg")
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
// TestReplicatedWriteRejectsWriteWithNoTarget verifies that a write this server
|
||||
// cannot store and has no replica to forward to fails instead of being
|
||||
// acknowledged with nothing written.
|
||||
|
||||
@@ -9,7 +9,7 @@ import (
|
||||
|
||||
var (
|
||||
MAJOR_VERSION = int32(4)
|
||||
MINOR_VERSION = int32(49)
|
||||
MINOR_VERSION = int32(50)
|
||||
VERSION_NUMBER = fmt.Sprintf("%d.%02d", MAJOR_VERSION, MINOR_VERSION)
|
||||
VERSION = util.SizeLimit + " " + VERSION_NUMBER
|
||||
COMMIT = ""
|
||||
|
||||
Reference in new issue
Block a user